nir: Add a foreach_block_reverse function
[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 namespace {
53
54 class acp_entry : public exec_node
55 {
56 public:
57 acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
58 {
59 this->lhs = lhs;
60 this->rhs = rhs;
61 this->write_mask = write_mask;
62 memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
63 }
64
65 acp_entry(acp_entry *a)
66 {
67 this->lhs = a->lhs;
68 this->rhs = a->rhs;
69 this->write_mask = a->write_mask;
70 memcpy(this->swizzle, a->swizzle, sizeof(this->swizzle));
71 }
72
73 ir_variable *lhs;
74 ir_variable *rhs;
75 unsigned int write_mask;
76 int swizzle[4];
77 };
78
79
80 class kill_entry : public exec_node
81 {
82 public:
83 kill_entry(ir_variable *var, int write_mask)
84 {
85 this->var = var;
86 this->write_mask = write_mask;
87 }
88
89 ir_variable *var;
90 unsigned int write_mask;
91 };
92
93 class ir_copy_propagation_elements_visitor : public ir_rvalue_visitor {
94 public:
95 ir_copy_propagation_elements_visitor()
96 {
97 this->progress = false;
98 this->killed_all = false;
99 this->mem_ctx = ralloc_context(NULL);
100 this->shader_mem_ctx = NULL;
101 this->acp = new(mem_ctx) exec_list;
102 this->kills = new(mem_ctx) exec_list;
103 }
104 ~ir_copy_propagation_elements_visitor()
105 {
106 ralloc_free(mem_ctx);
107 }
108
109 virtual ir_visitor_status visit_enter(class ir_loop *);
110 virtual ir_visitor_status visit_enter(class ir_function_signature *);
111 virtual ir_visitor_status visit_leave(class ir_assignment *);
112 virtual ir_visitor_status visit_enter(class ir_call *);
113 virtual ir_visitor_status visit_enter(class ir_if *);
114 virtual ir_visitor_status visit_leave(class ir_swizzle *);
115
116 void handle_rvalue(ir_rvalue **rvalue);
117
118 void add_copy(ir_assignment *ir);
119 void kill(kill_entry *k);
120 void handle_if_block(exec_list *instructions);
121
122 /** List of acp_entry: The available copies to propagate */
123 exec_list *acp;
124 /**
125 * List of kill_entry: The variables whose values were killed in this
126 * block.
127 */
128 exec_list *kills;
129
130 bool progress;
131
132 bool killed_all;
133
134 /* Context for our local data structures. */
135 void *mem_ctx;
136 /* Context for allocating new shader nodes. */
137 void *shader_mem_ctx;
138 };
139
140 } /* unnamed namespace */
141
142 ir_visitor_status
143 ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
144 {
145 /* Treat entry into a function signature as a completely separate
146 * block. Any instructions at global scope will be shuffled into
147 * main() at link time, so they're irrelevant to us.
148 */
149 exec_list *orig_acp = this->acp;
150 exec_list *orig_kills = this->kills;
151 bool orig_killed_all = this->killed_all;
152
153 this->acp = new(mem_ctx) exec_list;
154 this->kills = new(mem_ctx) exec_list;
155 this->killed_all = false;
156
157 visit_list_elements(this, &ir->body);
158
159 this->kills = orig_kills;
160 this->acp = orig_acp;
161 this->killed_all = orig_killed_all;
162
163 return visit_continue_with_parent;
164 }
165
166 ir_visitor_status
167 ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
168 {
169 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
170 ir_variable *var = ir->lhs->variable_referenced();
171
172 if (var->type->is_scalar() || var->type->is_vector()) {
173 kill_entry *k;
174
175 if (lhs)
176 k = new(mem_ctx) kill_entry(var, ir->write_mask);
177 else
178 k = new(mem_ctx) kill_entry(var, ~0);
179
180 kill(k);
181 }
182
183 add_copy(ir);
184
185 return visit_continue;
186 }
187
188 ir_visitor_status
189 ir_copy_propagation_elements_visitor::visit_leave(ir_swizzle *)
190 {
191 /* Don't visit the values of swizzles since they are handled while
192 * visiting the swizzle itself.
193 */
194 return visit_continue;
195 }
196
197 /**
198 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
199 *
200 * This is where the actual copy propagation occurs. Note that the
201 * rewriting of ir_dereference means that the ir_dereference instance
202 * must not be shared by multiple IR operations!
203 */
204 void
205 ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
206 {
207 int swizzle_chan[4];
208 ir_dereference_variable *deref_var;
209 ir_variable *source[4] = {NULL, NULL, NULL, NULL};
210 int source_chan[4] = {0, 0, 0, 0};
211 int chans;
212 bool noop_swizzle = true;
213
214 if (!*ir)
215 return;
216
217 ir_swizzle *swizzle = (*ir)->as_swizzle();
218 if (swizzle) {
219 deref_var = swizzle->val->as_dereference_variable();
220 if (!deref_var)
221 return;
222
223 swizzle_chan[0] = swizzle->mask.x;
224 swizzle_chan[1] = swizzle->mask.y;
225 swizzle_chan[2] = swizzle->mask.z;
226 swizzle_chan[3] = swizzle->mask.w;
227 chans = swizzle->type->vector_elements;
228 } else {
229 deref_var = (*ir)->as_dereference_variable();
230 if (!deref_var)
231 return;
232
233 swizzle_chan[0] = 0;
234 swizzle_chan[1] = 1;
235 swizzle_chan[2] = 2;
236 swizzle_chan[3] = 3;
237 chans = deref_var->type->vector_elements;
238 }
239
240 if (this->in_assignee)
241 return;
242
243 ir_variable *var = deref_var->var;
244
245 /* Try to find ACP entries covering swizzle_chan[], hoping they're
246 * the same source variable.
247 */
248 foreach_in_list(acp_entry, entry, this->acp) {
249 if (var == entry->lhs) {
250 for (int c = 0; c < chans; c++) {
251 if (entry->write_mask & (1 << swizzle_chan[c])) {
252 source[c] = entry->rhs;
253 source_chan[c] = entry->swizzle[swizzle_chan[c]];
254
255 if (source_chan[c] != swizzle_chan[c])
256 noop_swizzle = false;
257 }
258 }
259 }
260 }
261
262 /* Make sure all channels are copying from the same source variable. */
263 if (!source[0])
264 return;
265 for (int c = 1; c < chans; c++) {
266 if (source[c] != source[0])
267 return;
268 }
269
270 if (!shader_mem_ctx)
271 shader_mem_ctx = ralloc_parent(deref_var);
272
273 /* Don't pointlessly replace the rvalue with itself (or a noop swizzle
274 * of itself, which would just be deleted by opt_noop_swizzle).
275 */
276 if (source[0] == var && noop_swizzle)
277 return;
278
279 if (debug) {
280 printf("Copy propagation from:\n");
281 (*ir)->print();
282 }
283
284 deref_var = new(shader_mem_ctx) ir_dereference_variable(source[0]);
285 *ir = new(shader_mem_ctx) ir_swizzle(deref_var,
286 source_chan[0],
287 source_chan[1],
288 source_chan[2],
289 source_chan[3],
290 chans);
291 progress = true;
292
293 if (debug) {
294 printf("to:\n");
295 (*ir)->print();
296 printf("\n");
297 }
298 }
299
300
301 ir_visitor_status
302 ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
303 {
304 /* Do copy propagation on call parameters, but skip any out params */
305 foreach_two_lists(formal_node, &ir->callee->parameters,
306 actual_node, &ir->actual_parameters) {
307 ir_variable *sig_param = (ir_variable *) formal_node;
308 ir_rvalue *ir = (ir_rvalue *) actual_node;
309 if (sig_param->data.mode != ir_var_function_out
310 && sig_param->data.mode != ir_var_function_inout) {
311 ir->accept(this);
312 }
313 }
314
315 /* Since we're unlinked, we don't (necessarily) know the side effects of
316 * this call. So kill all copies.
317 */
318 acp->make_empty();
319 this->killed_all = true;
320
321 return visit_continue_with_parent;
322 }
323
324 void
325 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
326 {
327 exec_list *orig_acp = this->acp;
328 exec_list *orig_kills = this->kills;
329 bool orig_killed_all = this->killed_all;
330
331 this->acp = new(mem_ctx) exec_list;
332 this->kills = new(mem_ctx) exec_list;
333 this->killed_all = false;
334
335 /* Populate the initial acp with a copy of the original */
336 foreach_in_list(acp_entry, a, orig_acp) {
337 this->acp->push_tail(new(this->mem_ctx) acp_entry(a));
338 }
339
340 visit_list_elements(this, instructions);
341
342 if (this->killed_all) {
343 orig_acp->make_empty();
344 }
345
346 exec_list *new_kills = this->kills;
347 this->kills = orig_kills;
348 this->acp = orig_acp;
349 this->killed_all = this->killed_all || orig_killed_all;
350
351 /* Move the new kills into the parent block's list, removing them
352 * from the parent's ACP list in the process.
353 */
354 foreach_in_list_safe(kill_entry, k, new_kills) {
355 kill(k);
356 }
357 }
358
359 ir_visitor_status
360 ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
361 {
362 ir->condition->accept(this);
363
364 handle_if_block(&ir->then_instructions);
365 handle_if_block(&ir->else_instructions);
366
367 /* handle_if_block() already descended into the children. */
368 return visit_continue_with_parent;
369 }
370
371 ir_visitor_status
372 ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
373 {
374 exec_list *orig_acp = this->acp;
375 exec_list *orig_kills = this->kills;
376 bool orig_killed_all = this->killed_all;
377
378 /* FINISHME: For now, the initial acp for loops is totally empty.
379 * We could go through once, then go through again with the acp
380 * cloned minus the killed entries after the first run through.
381 */
382 this->acp = new(mem_ctx) exec_list;
383 this->kills = new(mem_ctx) exec_list;
384 this->killed_all = false;
385
386 visit_list_elements(this, &ir->body_instructions);
387
388 if (this->killed_all) {
389 orig_acp->make_empty();
390 }
391
392 exec_list *new_kills = this->kills;
393 this->kills = orig_kills;
394 this->acp = orig_acp;
395 this->killed_all = this->killed_all || orig_killed_all;
396
397 foreach_in_list_safe(kill_entry, k, new_kills) {
398 kill(k);
399 }
400
401 /* already descended into the children. */
402 return visit_continue_with_parent;
403 }
404
405 /* Remove any entries currently in the ACP for this kill. */
406 void
407 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
408 {
409 foreach_in_list_safe(acp_entry, entry, acp) {
410 if (entry->lhs == k->var) {
411 entry->write_mask = entry->write_mask & ~k->write_mask;
412 if (entry->write_mask == 0) {
413 entry->remove();
414 continue;
415 }
416 }
417 if (entry->rhs == k->var) {
418 entry->remove();
419 }
420 }
421
422 /* If we were on a list, remove ourselves before inserting */
423 if (k->next)
424 k->remove();
425
426 this->kills->push_tail(k);
427 }
428
429 /**
430 * Adds directly-copied channels between vector variables to the available
431 * copy propagation list.
432 */
433 void
434 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
435 {
436 acp_entry *entry;
437 int orig_swizzle[4] = {0, 1, 2, 3};
438 int swizzle[4];
439
440 if (ir->condition)
441 return;
442
443 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
444 if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
445 return;
446
447 ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
448 if (!rhs) {
449 ir_swizzle *swiz = ir->rhs->as_swizzle();
450 if (!swiz)
451 return;
452
453 rhs = swiz->val->as_dereference_variable();
454 if (!rhs)
455 return;
456
457 orig_swizzle[0] = swiz->mask.x;
458 orig_swizzle[1] = swiz->mask.y;
459 orig_swizzle[2] = swiz->mask.z;
460 orig_swizzle[3] = swiz->mask.w;
461 }
462
463 /* Move the swizzle channels out to the positions they match in the
464 * destination. We don't want to have to rewrite the swizzle[]
465 * array every time we clear a bit of the write_mask.
466 */
467 int j = 0;
468 for (int i = 0; i < 4; i++) {
469 if (ir->write_mask & (1 << i))
470 swizzle[i] = orig_swizzle[j++];
471 }
472
473 int write_mask = ir->write_mask;
474 if (lhs->var == rhs->var) {
475 /* If this is a copy from the variable to itself, then we need
476 * to be sure not to include the updated channels from this
477 * instruction in the set of new source channels to be
478 * copy-propagated from.
479 */
480 for (int i = 0; i < 4; i++) {
481 if (ir->write_mask & (1 << orig_swizzle[i]))
482 write_mask &= ~(1 << i);
483 }
484 }
485
486 entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, write_mask,
487 swizzle);
488 this->acp->push_tail(entry);
489 }
490
491 bool
492 do_copy_propagation_elements(exec_list *instructions)
493 {
494 ir_copy_propagation_elements_visitor v;
495
496 visit_list_elements(&v, instructions);
497
498 return v.progress;
499 }