glsl: Initialize member variable in ir_copy_propagation_elements_visitor.
[mesa.git] / src / glsl / opt_copy_propagation_elements.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file opt_copy_propagation_elements.cpp
26 *
27 * Replaces usage of recently-copied components of variables with the
28 * previous copy of the variable.
29 *
30 * This pass can be compared with opt_copy_propagation, which operands
31 * on arbitrary whole-variable copies. However, in order to handle
32 * the copy propagation of swizzled variables or writemasked writes,
33 * we want to track things on a channel-wise basis. I found that
34 * trying to mix the swizzled/writemasked support here with the
35 * whole-variable stuff in opt_copy_propagation.cpp just made a mess,
36 * so this is separate despite the ACP handling being somewhat
37 * similar.
38 *
39 * This should reduce the number of MOV instructions in the generated
40 * programs unless copy propagation is also done on the LIR, and may
41 * help anyway by triggering other optimizations that live in the HIR.
42 */
43
44 #include "ir.h"
45 #include "ir_rvalue_visitor.h"
46 #include "ir_basic_block.h"
47 #include "ir_optimization.h"
48 #include "glsl_types.h"
49
50 static bool debug = false;
51
52 class acp_entry : public exec_node
53 {
54 public:
55 acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
56 {
57 this->lhs = lhs;
58 this->rhs = rhs;
59 this->write_mask = write_mask;
60 memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
61 }
62
63 acp_entry(acp_entry *a)
64 {
65 this->lhs = a->lhs;
66 this->rhs = a->rhs;
67 this->write_mask = a->write_mask;
68 memcpy(this->swizzle, a->swizzle, sizeof(this->swizzle));
69 }
70
71 ir_variable *lhs;
72 ir_variable *rhs;
73 unsigned int write_mask;
74 int swizzle[4];
75 };
76
77
78 class kill_entry : public exec_node
79 {
80 public:
81 kill_entry(ir_variable *var, int write_mask)
82 {
83 this->var = var;
84 this->write_mask = write_mask;
85 }
86
87 ir_variable *var;
88 unsigned int write_mask;
89 };
90
91 class ir_copy_propagation_elements_visitor : public ir_rvalue_visitor {
92 public:
93 ir_copy_propagation_elements_visitor()
94 {
95 this->progress = false;
96 this->killed_all = false;
97 this->mem_ctx = ralloc_context(NULL);
98 this->shader_mem_ctx = NULL;
99 this->acp = new(mem_ctx) exec_list;
100 this->kills = new(mem_ctx) exec_list;
101 }
102 ~ir_copy_propagation_elements_visitor()
103 {
104 ralloc_free(mem_ctx);
105 }
106
107 virtual ir_visitor_status visit_enter(class ir_loop *);
108 virtual ir_visitor_status visit_enter(class ir_function_signature *);
109 virtual ir_visitor_status visit_leave(class ir_assignment *);
110 virtual ir_visitor_status visit_enter(class ir_call *);
111 virtual ir_visitor_status visit_enter(class ir_if *);
112 virtual ir_visitor_status visit_leave(class ir_swizzle *);
113
114 void handle_rvalue(ir_rvalue **rvalue);
115
116 void add_copy(ir_assignment *ir);
117 void kill(kill_entry *k);
118 void handle_if_block(exec_list *instructions);
119
120 /** List of acp_entry: The available copies to propagate */
121 exec_list *acp;
122 /**
123 * List of kill_entry: The variables whose values were killed in this
124 * block.
125 */
126 exec_list *kills;
127
128 bool progress;
129
130 bool killed_all;
131
132 /* Context for our local data structures. */
133 void *mem_ctx;
134 /* Context for allocating new shader nodes. */
135 void *shader_mem_ctx;
136 };
137
138 ir_visitor_status
139 ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
140 {
141 /* Treat entry into a function signature as a completely separate
142 * block. Any instructions at global scope will be shuffled into
143 * main() at link time, so they're irrelevant to us.
144 */
145 exec_list *orig_acp = this->acp;
146 exec_list *orig_kills = this->kills;
147 bool orig_killed_all = this->killed_all;
148
149 this->acp = new(mem_ctx) exec_list;
150 this->kills = new(mem_ctx) exec_list;
151 this->killed_all = false;
152
153 visit_list_elements(this, &ir->body);
154
155 this->kills = orig_kills;
156 this->acp = orig_acp;
157 this->killed_all = orig_killed_all;
158
159 return visit_continue_with_parent;
160 }
161
162 ir_visitor_status
163 ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
164 {
165 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
166 ir_variable *var = ir->lhs->variable_referenced();
167
168 if (var->type->is_scalar() || var->type->is_vector()) {
169 kill_entry *k;
170
171 if (lhs)
172 k = new(mem_ctx) kill_entry(var, ir->write_mask);
173 else
174 k = new(mem_ctx) kill_entry(var, ~0);
175
176 kill(k);
177 }
178
179 add_copy(ir);
180
181 return visit_continue;
182 }
183
184 ir_visitor_status
185 ir_copy_propagation_elements_visitor::visit_leave(ir_swizzle *ir)
186 {
187 /* Don't visit the values of swizzles since they are handled while
188 * visiting the swizzle itself.
189 */
190 return visit_continue;
191 }
192
193 /**
194 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
195 *
196 * This is where the actual copy propagation occurs. Note that the
197 * rewriting of ir_dereference means that the ir_dereference instance
198 * must not be shared by multiple IR operations!
199 */
200 void
201 ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
202 {
203 int swizzle_chan[4];
204 ir_dereference_variable *deref_var;
205 ir_variable *source[4] = {NULL, NULL, NULL, NULL};
206 int source_chan[4];
207 int chans;
208
209 if (!*ir)
210 return;
211
212 ir_swizzle *swizzle = (*ir)->as_swizzle();
213 if (swizzle) {
214 deref_var = swizzle->val->as_dereference_variable();
215 if (!deref_var)
216 return;
217
218 swizzle_chan[0] = swizzle->mask.x;
219 swizzle_chan[1] = swizzle->mask.y;
220 swizzle_chan[2] = swizzle->mask.z;
221 swizzle_chan[3] = swizzle->mask.w;
222 chans = swizzle->type->vector_elements;
223 } else {
224 deref_var = (*ir)->as_dereference_variable();
225 if (!deref_var)
226 return;
227
228 swizzle_chan[0] = 0;
229 swizzle_chan[1] = 1;
230 swizzle_chan[2] = 2;
231 swizzle_chan[3] = 3;
232 chans = deref_var->type->vector_elements;
233 }
234
235 if (this->in_assignee)
236 return;
237
238 ir_variable *var = deref_var->var;
239
240 /* Try to find ACP entries covering swizzle_chan[], hoping they're
241 * the same source variable.
242 */
243 foreach_iter(exec_list_iterator, iter, *this->acp) {
244 acp_entry *entry = (acp_entry *)iter.get();
245
246 if (var == entry->lhs) {
247 for (int c = 0; c < chans; c++) {
248 if (entry->write_mask & (1 << swizzle_chan[c])) {
249 source[c] = entry->rhs;
250 source_chan[c] = entry->swizzle[swizzle_chan[c]];
251 }
252 }
253 }
254 }
255
256 /* Make sure all channels are copying from the same source variable. */
257 if (!source[0])
258 return;
259 for (int c = 1; c < chans; c++) {
260 if (source[c] != source[0])
261 return;
262 }
263
264 if (!shader_mem_ctx)
265 shader_mem_ctx = ralloc_parent(deref_var);
266
267 if (debug) {
268 printf("Copy propagation from:\n");
269 (*ir)->print();
270 }
271
272 deref_var = new(shader_mem_ctx) ir_dereference_variable(source[0]);
273 *ir = new(shader_mem_ctx) ir_swizzle(deref_var,
274 source_chan[0],
275 source_chan[1],
276 source_chan[2],
277 source_chan[3],
278 chans);
279
280 if (debug) {
281 printf("to:\n");
282 (*ir)->print();
283 printf("\n");
284 }
285 }
286
287
288 ir_visitor_status
289 ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
290 {
291 /* Do copy propagation on call parameters, but skip any out params */
292 exec_list_iterator sig_param_iter = ir->callee->parameters.iterator();
293 foreach_iter(exec_list_iterator, iter, ir->actual_parameters) {
294 ir_variable *sig_param = (ir_variable *)sig_param_iter.get();
295 ir_instruction *ir = (ir_instruction *)iter.get();
296 if (sig_param->mode != ir_var_out && sig_param->mode != ir_var_inout) {
297 ir->accept(this);
298 }
299 sig_param_iter.next();
300 }
301
302 /* Since we're unlinked, we don't (necessarily) know the side effects of
303 * this call. So kill all copies.
304 */
305 acp->make_empty();
306 this->killed_all = true;
307
308 return visit_continue_with_parent;
309 }
310
311 void
312 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
313 {
314 exec_list *orig_acp = this->acp;
315 exec_list *orig_kills = this->kills;
316 bool orig_killed_all = this->killed_all;
317
318 this->acp = new(mem_ctx) exec_list;
319 this->kills = new(mem_ctx) exec_list;
320 this->killed_all = false;
321
322 /* Populate the initial acp with a copy of the original */
323 foreach_iter(exec_list_iterator, iter, *orig_acp) {
324 acp_entry *a = (acp_entry *)iter.get();
325 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
326 }
327
328 visit_list_elements(this, instructions);
329
330 if (this->killed_all) {
331 orig_acp->make_empty();
332 }
333
334 exec_list *new_kills = this->kills;
335 this->kills = orig_kills;
336 this->acp = orig_acp;
337 this->killed_all = this->killed_all || orig_killed_all;
338
339 /* Move the new kills into the parent block's list, removing them
340 * from the parent's ACP list in the process.
341 */
342 foreach_list_safe(node, new_kills) {
343 kill_entry *k = (kill_entry *)node;
344 kill(k);
345 }
346 }
347
348 ir_visitor_status
349 ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
350 {
351 ir->condition->accept(this);
352
353 handle_if_block(&ir->then_instructions);
354 handle_if_block(&ir->else_instructions);
355
356 /* handle_if_block() already descended into the children. */
357 return visit_continue_with_parent;
358 }
359
360 ir_visitor_status
361 ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
362 {
363 exec_list *orig_acp = this->acp;
364 exec_list *orig_kills = this->kills;
365 bool orig_killed_all = this->killed_all;
366
367 /* FINISHME: For now, the initial acp for loops is totally empty.
368 * We could go through once, then go through again with the acp
369 * cloned minus the killed entries after the first run through.
370 */
371 this->acp = new(mem_ctx) exec_list;
372 this->kills = new(mem_ctx) exec_list;
373 this->killed_all = false;
374
375 visit_list_elements(this, &ir->body_instructions);
376
377 if (this->killed_all) {
378 orig_acp->make_empty();
379 }
380
381 exec_list *new_kills = this->kills;
382 this->kills = orig_kills;
383 this->acp = orig_acp;
384 this->killed_all = this->killed_all || orig_killed_all;
385
386 foreach_list_safe(node, new_kills) {
387 kill_entry *k = (kill_entry *)node;
388 kill(k);
389 }
390
391 /* already descended into the children. */
392 return visit_continue_with_parent;
393 }
394
395 /* Remove any entries currently in the ACP for this kill. */
396 void
397 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
398 {
399 foreach_list_safe(node, acp) {
400 acp_entry *entry = (acp_entry *)node;
401
402 if (entry->lhs == k->var) {
403 entry->write_mask = entry->write_mask & ~k->write_mask;
404 if (entry->write_mask == 0) {
405 entry->remove();
406 continue;
407 }
408 }
409 if (entry->rhs == k->var) {
410 entry->remove();
411 }
412 }
413
414 /* If we were on a list, remove ourselves before inserting */
415 if (k->next)
416 k->remove();
417
418 this->kills->push_tail(k);
419 }
420
421 /**
422 * Adds directly-copied channels between vector variables to the available
423 * copy propagation list.
424 */
425 void
426 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
427 {
428 acp_entry *entry;
429 int orig_swizzle[4] = {0, 1, 2, 3};
430 int swizzle[4];
431
432 if (ir->condition)
433 return;
434
435 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
436 if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
437 return;
438
439 ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
440 if (!rhs) {
441 ir_swizzle *swiz = ir->rhs->as_swizzle();
442 if (!swiz)
443 return;
444
445 rhs = swiz->val->as_dereference_variable();
446 if (!rhs)
447 return;
448
449 orig_swizzle[0] = swiz->mask.x;
450 orig_swizzle[1] = swiz->mask.y;
451 orig_swizzle[2] = swiz->mask.z;
452 orig_swizzle[3] = swiz->mask.w;
453 }
454
455 /* Move the swizzle channels out to the positions they match in the
456 * destination. We don't want to have to rewrite the swizzle[]
457 * array every time we clear a bit of the write_mask.
458 */
459 int j = 0;
460 for (int i = 0; i < 4; i++) {
461 if (ir->write_mask & (1 << i))
462 swizzle[i] = orig_swizzle[j++];
463 }
464
465 int write_mask = ir->write_mask;
466 if (lhs->var == rhs->var) {
467 /* If this is a copy from the variable to itself, then we need
468 * to be sure not to include the updated channels from this
469 * instruction in the set of new source channels to be
470 * copy-propagated from.
471 */
472 for (int i = 0; i < 4; i++) {
473 if (ir->write_mask & (1 << orig_swizzle[i]))
474 write_mask &= ~(1 << i);
475 }
476 }
477
478 entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, write_mask,
479 swizzle);
480 this->acp->push_tail(entry);
481 }
482
483 bool
484 do_copy_propagation_elements(exec_list *instructions)
485 {
486 ir_copy_propagation_elements_visitor v;
487
488 visit_list_elements(&v, instructions);
489
490 return v.progress;
491 }