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