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