glsl: dump explicit location when printing IR
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
49 #include "util/hash_table.h"
50
51 static bool debug = false;
52
53 namespace {
54
55 class acp_entry;
56
57 /* Class that refers to acp_entry in another exec_list. Used
58 * when making removals based on rhs.
59 */
60 class acp_ref : public exec_node
61 {
62 public:
63 acp_ref(acp_entry *e)
64 {
65 entry = e;
66 }
67 acp_entry *entry;
68 };
69
70 class acp_entry : public exec_node
71 {
72 public:
73 acp_entry(ir_variable *lhs, ir_variable *rhs, int write_mask, int swizzle[4])
74 : rhs_node(this)
75 {
76 this->lhs = lhs;
77 this->rhs = rhs;
78 this->write_mask = write_mask;
79 memcpy(this->swizzle, swizzle, sizeof(this->swizzle));
80 }
81
82 ir_variable *lhs;
83 ir_variable *rhs;
84 unsigned int write_mask;
85 int swizzle[4];
86 acp_ref rhs_node;
87 };
88
89
90 class kill_entry : public exec_node
91 {
92 public:
93 kill_entry(ir_variable *var, int write_mask)
94 {
95 this->var = var;
96 this->write_mask = write_mask;
97 }
98
99 ir_variable *var;
100 unsigned int write_mask;
101 };
102
103 class ir_copy_propagation_elements_visitor : public ir_rvalue_visitor {
104 public:
105 ir_copy_propagation_elements_visitor()
106 {
107 this->progress = false;
108 this->killed_all = false;
109 this->mem_ctx = ralloc_context(NULL);
110 this->shader_mem_ctx = NULL;
111 this->kills = new(mem_ctx) exec_list;
112
113 create_acp();
114 }
115 ~ir_copy_propagation_elements_visitor()
116 {
117 ralloc_free(mem_ctx);
118 }
119
120 void create_acp()
121 {
122 lhs_ht = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
123 _mesa_key_pointer_equal);
124 rhs_ht = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
125 _mesa_key_pointer_equal);
126 }
127
128 void destroy_acp()
129 {
130 _mesa_hash_table_destroy(lhs_ht, NULL);
131 _mesa_hash_table_destroy(rhs_ht, NULL);
132 }
133
134 void populate_acp(hash_table *lhs, hash_table *rhs)
135 {
136 struct hash_entry *entry;
137
138 hash_table_foreach(lhs, entry) {
139 _mesa_hash_table_insert(lhs_ht, entry->key, entry->data);
140 }
141
142 hash_table_foreach(rhs, entry) {
143 _mesa_hash_table_insert(rhs_ht, entry->key, entry->data);
144 }
145 }
146
147 void handle_loop(ir_loop *, bool keep_acp);
148 virtual ir_visitor_status visit_enter(class ir_loop *);
149 virtual ir_visitor_status visit_enter(class ir_function_signature *);
150 virtual ir_visitor_status visit_leave(class ir_assignment *);
151 virtual ir_visitor_status visit_enter(class ir_call *);
152 virtual ir_visitor_status visit_enter(class ir_if *);
153 virtual ir_visitor_status visit_leave(class ir_swizzle *);
154
155 void handle_rvalue(ir_rvalue **rvalue);
156
157 void add_copy(ir_assignment *ir);
158 void kill(kill_entry *k);
159 void handle_if_block(exec_list *instructions);
160
161 /** Hash of acp_entry: The available copies to propagate */
162 hash_table *lhs_ht;
163 hash_table *rhs_ht;
164
165 /**
166 * List of kill_entry: The variables whose values were killed in this
167 * block.
168 */
169 exec_list *kills;
170
171 bool progress;
172
173 bool killed_all;
174
175 /* Context for our local data structures. */
176 void *mem_ctx;
177 /* Context for allocating new shader nodes. */
178 void *shader_mem_ctx;
179 };
180
181 } /* unnamed namespace */
182
183 ir_visitor_status
184 ir_copy_propagation_elements_visitor::visit_enter(ir_function_signature *ir)
185 {
186 /* Treat entry into a function signature as a completely separate
187 * block. Any instructions at global scope will be shuffled into
188 * main() at link time, so they're irrelevant to us.
189 */
190 exec_list *orig_kills = this->kills;
191 bool orig_killed_all = this->killed_all;
192
193 hash_table *orig_lhs_ht = lhs_ht;
194 hash_table *orig_rhs_ht = rhs_ht;
195
196 this->kills = new(mem_ctx) exec_list;
197 this->killed_all = false;
198
199 create_acp();
200
201 visit_list_elements(this, &ir->body);
202
203 ralloc_free(this->kills);
204
205 destroy_acp();
206
207 this->kills = orig_kills;
208 this->killed_all = orig_killed_all;
209
210 lhs_ht = orig_lhs_ht;
211 rhs_ht = orig_rhs_ht;
212
213 return visit_continue_with_parent;
214 }
215
216 ir_visitor_status
217 ir_copy_propagation_elements_visitor::visit_leave(ir_assignment *ir)
218 {
219 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
220 ir_variable *var = ir->lhs->variable_referenced();
221
222 if (var->type->is_scalar() || var->type->is_vector()) {
223 kill_entry *k;
224
225 if (lhs)
226 k = new(this->kills) kill_entry(var, ir->write_mask);
227 else
228 k = new(this->kills) kill_entry(var, ~0);
229
230 kill(k);
231 }
232
233 add_copy(ir);
234
235 return visit_continue;
236 }
237
238 ir_visitor_status
239 ir_copy_propagation_elements_visitor::visit_leave(ir_swizzle *)
240 {
241 /* Don't visit the values of swizzles since they are handled while
242 * visiting the swizzle itself.
243 */
244 return visit_continue;
245 }
246
247 /**
248 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
249 *
250 * This is where the actual copy propagation occurs. Note that the
251 * rewriting of ir_dereference means that the ir_dereference instance
252 * must not be shared by multiple IR operations!
253 */
254 void
255 ir_copy_propagation_elements_visitor::handle_rvalue(ir_rvalue **ir)
256 {
257 int swizzle_chan[4];
258 ir_dereference_variable *deref_var;
259 ir_variable *source[4] = {NULL, NULL, NULL, NULL};
260 int source_chan[4] = {0, 0, 0, 0};
261 int chans;
262 bool noop_swizzle = true;
263
264 if (!*ir)
265 return;
266
267 ir_swizzle *swizzle = (*ir)->as_swizzle();
268 if (swizzle) {
269 deref_var = swizzle->val->as_dereference_variable();
270 if (!deref_var)
271 return;
272
273 swizzle_chan[0] = swizzle->mask.x;
274 swizzle_chan[1] = swizzle->mask.y;
275 swizzle_chan[2] = swizzle->mask.z;
276 swizzle_chan[3] = swizzle->mask.w;
277 chans = swizzle->type->vector_elements;
278 } else {
279 deref_var = (*ir)->as_dereference_variable();
280 if (!deref_var)
281 return;
282
283 swizzle_chan[0] = 0;
284 swizzle_chan[1] = 1;
285 swizzle_chan[2] = 2;
286 swizzle_chan[3] = 3;
287 chans = deref_var->type->vector_elements;
288 }
289
290 if (this->in_assignee)
291 return;
292
293 ir_variable *var = deref_var->var;
294
295 /* Try to find ACP entries covering swizzle_chan[], hoping they're
296 * the same source variable.
297 */
298 hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, var);
299 if (ht_entry) {
300 exec_list *ht_list = (exec_list *) ht_entry->data;
301 foreach_in_list(acp_entry, entry, ht_list) {
302 for (int c = 0; c < chans; c++) {
303 if (entry->write_mask & (1 << swizzle_chan[c])) {
304 source[c] = entry->rhs;
305 source_chan[c] = entry->swizzle[swizzle_chan[c]];
306
307 if (source_chan[c] != swizzle_chan[c])
308 noop_swizzle = false;
309 }
310 }
311 }
312 }
313
314 /* Make sure all channels are copying from the same source variable. */
315 if (!source[0])
316 return;
317 for (int c = 1; c < chans; c++) {
318 if (source[c] != source[0])
319 return;
320 }
321
322 if (!shader_mem_ctx)
323 shader_mem_ctx = ralloc_parent(deref_var);
324
325 /* Don't pointlessly replace the rvalue with itself (or a noop swizzle
326 * of itself, which would just be deleted by opt_noop_swizzle).
327 */
328 if (source[0] == var && noop_swizzle)
329 return;
330
331 if (debug) {
332 printf("Copy propagation from:\n");
333 (*ir)->print();
334 }
335
336 deref_var = new(shader_mem_ctx) ir_dereference_variable(source[0]);
337 *ir = new(shader_mem_ctx) ir_swizzle(deref_var,
338 source_chan[0],
339 source_chan[1],
340 source_chan[2],
341 source_chan[3],
342 chans);
343 progress = true;
344
345 if (debug) {
346 printf("to:\n");
347 (*ir)->print();
348 printf("\n");
349 }
350 }
351
352
353 ir_visitor_status
354 ir_copy_propagation_elements_visitor::visit_enter(ir_call *ir)
355 {
356 /* Do copy propagation on call parameters, but skip any out params */
357 foreach_two_lists(formal_node, &ir->callee->parameters,
358 actual_node, &ir->actual_parameters) {
359 ir_variable *sig_param = (ir_variable *) formal_node;
360 ir_rvalue *ir = (ir_rvalue *) actual_node;
361 if (sig_param->data.mode != ir_var_function_out
362 && sig_param->data.mode != ir_var_function_inout) {
363 ir->accept(this);
364 }
365 }
366
367 /* Since we're unlinked, we don't (necessarily) know the side effects of
368 * this call. So kill all copies.
369 */
370 _mesa_hash_table_clear(lhs_ht, NULL);
371 _mesa_hash_table_clear(rhs_ht, NULL);
372
373 this->killed_all = true;
374
375 return visit_continue_with_parent;
376 }
377
378 void
379 ir_copy_propagation_elements_visitor::handle_if_block(exec_list *instructions)
380 {
381 exec_list *orig_kills = this->kills;
382 bool orig_killed_all = this->killed_all;
383
384 hash_table *orig_lhs_ht = lhs_ht;
385 hash_table *orig_rhs_ht = rhs_ht;
386
387 this->kills = new(mem_ctx) exec_list;
388 this->killed_all = false;
389
390 create_acp();
391
392 /* Populate the initial acp with a copy of the original */
393 populate_acp(orig_lhs_ht, orig_rhs_ht);
394
395 visit_list_elements(this, instructions);
396
397 if (this->killed_all) {
398 _mesa_hash_table_clear(orig_lhs_ht, NULL);
399 _mesa_hash_table_clear(orig_rhs_ht, NULL);
400 }
401
402 exec_list *new_kills = this->kills;
403 this->kills = orig_kills;
404 this->killed_all = this->killed_all || orig_killed_all;
405
406 destroy_acp();
407
408 lhs_ht = orig_lhs_ht;
409 rhs_ht = orig_rhs_ht;
410
411 /* Move the new kills into the parent block's list, removing them
412 * from the parent's ACP list in the process.
413 */
414 foreach_in_list_safe(kill_entry, k, new_kills) {
415 kill(k);
416 }
417
418 ralloc_free(new_kills);
419 }
420
421 ir_visitor_status
422 ir_copy_propagation_elements_visitor::visit_enter(ir_if *ir)
423 {
424 ir->condition->accept(this);
425
426 handle_if_block(&ir->then_instructions);
427 handle_if_block(&ir->else_instructions);
428
429 /* handle_if_block() already descended into the children. */
430 return visit_continue_with_parent;
431 }
432
433 void
434 ir_copy_propagation_elements_visitor::handle_loop(ir_loop *ir, bool keep_acp)
435 {
436 exec_list *orig_kills = this->kills;
437 bool orig_killed_all = this->killed_all;
438
439 hash_table *orig_lhs_ht = lhs_ht;
440 hash_table *orig_rhs_ht = rhs_ht;
441
442 /* FINISHME: For now, the initial acp for loops is totally empty.
443 * We could go through once, then go through again with the acp
444 * cloned minus the killed entries after the first run through.
445 */
446 this->kills = new(mem_ctx) exec_list;
447 this->killed_all = false;
448
449 create_acp();
450
451 if (keep_acp) {
452 /* Populate the initial acp with a copy of the original */
453 populate_acp(orig_lhs_ht, orig_rhs_ht);
454 }
455
456 visit_list_elements(this, &ir->body_instructions);
457
458 if (this->killed_all) {
459 _mesa_hash_table_clear(orig_lhs_ht, NULL);
460 _mesa_hash_table_clear(orig_rhs_ht, NULL);
461 }
462
463 exec_list *new_kills = this->kills;
464 this->kills = orig_kills;
465 this->killed_all = this->killed_all || orig_killed_all;
466
467 destroy_acp();
468
469 lhs_ht = orig_lhs_ht;
470 rhs_ht = orig_rhs_ht;
471
472 foreach_in_list_safe(kill_entry, k, new_kills) {
473 kill(k);
474 }
475
476 ralloc_free(new_kills);
477 }
478
479 ir_visitor_status
480 ir_copy_propagation_elements_visitor::visit_enter(ir_loop *ir)
481 {
482 handle_loop(ir, false);
483 handle_loop(ir, true);
484
485 /* already descended into the children. */
486 return visit_continue_with_parent;
487 }
488
489 /* Remove any entries currently in the ACP for this kill. */
490 void
491 ir_copy_propagation_elements_visitor::kill(kill_entry *k)
492 {
493 /* removal of lhs entries */
494 hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, k->var);
495 if (ht_entry) {
496 exec_list *lhs_list = (exec_list *) ht_entry->data;
497 foreach_in_list_safe(acp_entry, entry, lhs_list) {
498 entry->write_mask = entry->write_mask & ~k->write_mask;
499 if (entry->write_mask == 0) {
500 entry->remove();
501 continue;
502 }
503 }
504 }
505
506 /* removal of rhs entries */
507 ht_entry = _mesa_hash_table_search(rhs_ht, k->var);
508 if (ht_entry) {
509 exec_list *rhs_list = (exec_list *) ht_entry->data;
510 acp_ref *ref;
511
512 while ((ref = (acp_ref *) rhs_list->pop_head()) != NULL) {
513 acp_entry *entry = ref->entry;
514
515 /* If entry is still in a list (not already removed by lhs entry
516 * removal above), remove it.
517 */
518 if (entry->prev || entry->next)
519 entry->remove();
520 }
521 }
522
523 /* If we were on a list, remove ourselves before inserting */
524 if (k->next)
525 k->remove();
526
527 ralloc_steal(this->kills, k);
528 this->kills->push_tail(k);
529 }
530
531 /**
532 * Adds directly-copied channels between vector variables to the available
533 * copy propagation list.
534 */
535 void
536 ir_copy_propagation_elements_visitor::add_copy(ir_assignment *ir)
537 {
538 acp_entry *entry;
539 int orig_swizzle[4] = {0, 1, 2, 3};
540 int swizzle[4];
541
542 if (ir->condition)
543 return;
544
545 ir_dereference_variable *lhs = ir->lhs->as_dereference_variable();
546 if (!lhs || !(lhs->type->is_scalar() || lhs->type->is_vector()))
547 return;
548
549 ir_dereference_variable *rhs = ir->rhs->as_dereference_variable();
550 if (!rhs) {
551 ir_swizzle *swiz = ir->rhs->as_swizzle();
552 if (!swiz)
553 return;
554
555 rhs = swiz->val->as_dereference_variable();
556 if (!rhs)
557 return;
558
559 orig_swizzle[0] = swiz->mask.x;
560 orig_swizzle[1] = swiz->mask.y;
561 orig_swizzle[2] = swiz->mask.z;
562 orig_swizzle[3] = swiz->mask.w;
563 }
564
565 /* Move the swizzle channels out to the positions they match in the
566 * destination. We don't want to have to rewrite the swizzle[]
567 * array every time we clear a bit of the write_mask.
568 */
569 int j = 0;
570 for (int i = 0; i < 4; i++) {
571 if (ir->write_mask & (1 << i))
572 swizzle[i] = orig_swizzle[j++];
573 }
574
575 int write_mask = ir->write_mask;
576 if (lhs->var == rhs->var) {
577 /* If this is a copy from the variable to itself, then we need
578 * to be sure not to include the updated channels from this
579 * instruction in the set of new source channels to be
580 * copy-propagated from.
581 */
582 for (int i = 0; i < 4; i++) {
583 if (ir->write_mask & (1 << orig_swizzle[i]))
584 write_mask &= ~(1 << i);
585 }
586 }
587
588 if (lhs->var->data.precise != rhs->var->data.precise)
589 return;
590
591 entry = new(this->mem_ctx) acp_entry(lhs->var, rhs->var, write_mask,
592 swizzle);
593
594 /* lhs hash, hash of lhs -> acp_entry lists */
595 hash_entry *ht_entry = _mesa_hash_table_search(lhs_ht, lhs->var);
596 if (ht_entry) {
597 exec_list *lhs_list = (exec_list *) ht_entry->data;
598 lhs_list->push_tail(entry);
599 } else {
600 exec_list *lhs_list = new(mem_ctx) exec_list;
601 lhs_list->push_tail(entry);
602 _mesa_hash_table_insert(lhs_ht, lhs->var, lhs_list);
603 }
604
605 /* rhs hash, hash of rhs -> acp_entry pointers to lhs lists */
606 ht_entry = _mesa_hash_table_search(rhs_ht, rhs->var);
607 if (ht_entry) {
608 exec_list *rhs_list = (exec_list *) ht_entry->data;
609 rhs_list->push_tail(&entry->rhs_node);
610 } else {
611 exec_list *rhs_list = new(mem_ctx) exec_list;
612 rhs_list->push_tail(&entry->rhs_node);
613 _mesa_hash_table_insert(rhs_ht, rhs->var, rhs_list);
614 }
615 }
616
617 bool
618 do_copy_propagation_elements(exec_list *instructions)
619 {
620 ir_copy_propagation_elements_visitor v;
621
622 visit_list_elements(&v, instructions);
623
624 return v.progress;
625 }