cc6e86f55e48521479a018a20cd5f351cdf1f447
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_copy_propagation.cpp
1 /*
2 * Copyright © 2012 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /** @file brw_fs_copy_propagation.cpp
25 *
26 * Support for global copy propagation in two passes: A local pass that does
27 * intra-block copy (and constant) propagation, and a global pass that uses
28 * dataflow analysis on the copies available at the end of each block to re-do
29 * local copy propagation with more copies available.
30 *
31 * See Muchnick's Advanced Compiler Design and Implementation, section
32 * 12.5 (p356).
33 */
34
35 #define ACP_HASH_SIZE 16
36
37 #include "main/bitset.h"
38 #include "brw_fs.h"
39 #include "brw_cfg.h"
40
41 namespace { /* avoid conflict with opt_copy_propagation_elements */
42 struct acp_entry : public exec_node {
43 fs_reg dst;
44 fs_reg src;
45 enum opcode opcode;
46 };
47
48 struct block_data {
49 /**
50 * Which entries in the fs_copy_prop_dataflow acp table are live at the
51 * start of this block. This is the useful output of the analysis, since
52 * it lets us plug those into the local copy propagation on the second
53 * pass.
54 */
55 BITSET_WORD *livein;
56
57 /**
58 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
59 * of this block. This is done in initial setup from the per-block acps
60 * returned by the first local copy prop pass.
61 */
62 BITSET_WORD *liveout;
63
64 /**
65 * Which entries in the fs_copy_prop_dataflow acp table are generated by
66 * instructions in this block which reach the end of the block without
67 * being killed.
68 */
69 BITSET_WORD *copy;
70
71 /**
72 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
73 * course of this block.
74 */
75 BITSET_WORD *kill;
76 };
77
78 class fs_copy_prop_dataflow
79 {
80 public:
81 fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
82 exec_list *out_acp[ACP_HASH_SIZE]);
83
84 void setup_initial_values();
85 void run();
86
87 void dump_block_data() const;
88
89 void *mem_ctx;
90 cfg_t *cfg;
91
92 acp_entry **acp;
93 int num_acp;
94 int bitset_words;
95
96 struct block_data *bd;
97 };
98 } /* anonymous namespace */
99
100 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
101 exec_list *out_acp[ACP_HASH_SIZE])
102 : mem_ctx(mem_ctx), cfg(cfg)
103 {
104 bd = rzalloc_array(mem_ctx, struct block_data, cfg->num_blocks);
105
106 num_acp = 0;
107 for (int b = 0; b < cfg->num_blocks; b++) {
108 for (int i = 0; i < ACP_HASH_SIZE; i++) {
109 foreach_list(entry_node, &out_acp[b][i]) {
110 num_acp++;
111 }
112 }
113 }
114
115 acp = rzalloc_array(mem_ctx, struct acp_entry *, num_acp);
116
117 bitset_words = BITSET_WORDS(num_acp);
118
119 int next_acp = 0;
120 for (int b = 0; b < cfg->num_blocks; b++) {
121 bd[b].livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
122 bd[b].liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
123 bd[b].copy = rzalloc_array(bd, BITSET_WORD, bitset_words);
124 bd[b].kill = rzalloc_array(bd, BITSET_WORD, bitset_words);
125
126 for (int i = 0; i < ACP_HASH_SIZE; i++) {
127 foreach_list(entry_node, &out_acp[b][i]) {
128 acp_entry *entry = (acp_entry *)entry_node;
129
130 acp[next_acp] = entry;
131
132 /* opt_copy_propagate_local populates out_acp with copies created
133 * in a block which are still live at the end of the block. This
134 * is exactly what we want in the COPY set.
135 */
136 BITSET_SET(bd[b].copy, next_acp);
137
138 next_acp++;
139 }
140 }
141 }
142
143 assert(next_acp == num_acp);
144
145 setup_initial_values();
146 run();
147 }
148
149 /**
150 * Set up initial values for each of the data flow sets, prior to running
151 * the fixed-point algorithm.
152 */
153 void
154 fs_copy_prop_dataflow::setup_initial_values()
155 {
156 /* Initialize the COPY and KILL sets. */
157 for (int b = 0; b < cfg->num_blocks; b++) {
158 bblock_t *block = cfg->blocks[b];
159
160 for (fs_inst *inst = (fs_inst *)block->start;
161 inst != block->end->next;
162 inst = (fs_inst *)inst->next) {
163 if (inst->dst.file != GRF)
164 continue;
165
166 /* Mark ACP entries which are killed by this instruction. */
167 for (int i = 0; i < num_acp; i++) {
168 if (inst->overwrites_reg(acp[i]->dst) ||
169 inst->overwrites_reg(acp[i]->src)) {
170 BITSET_SET(bd[b].kill, i);
171 }
172 }
173 }
174 }
175
176 /* Populate the initial values for the livein and liveout sets. For the
177 * block at the start of the program, livein = 0 and liveout = copy.
178 * For the others, set liveout to 0 (the empty set) and livein to ~0
179 * (the universal set).
180 */
181 for (int b = 0; b < cfg->num_blocks; b++) {
182 bblock_t *block = cfg->blocks[b];
183 if (block->parents.is_empty()) {
184 for (int i = 0; i < bitset_words; i++) {
185 bd[b].livein[i] = 0u;
186 bd[b].liveout[i] = bd[b].copy[i];
187 }
188 } else {
189 for (int i = 0; i < bitset_words; i++) {
190 bd[b].liveout[i] = 0u;
191 bd[b].livein[i] = ~0u;
192 }
193 }
194 }
195 }
196
197 /**
198 * Walk the set of instructions in the block, marking which entries in the acp
199 * are killed by the block.
200 */
201 void
202 fs_copy_prop_dataflow::run()
203 {
204 bool progress;
205
206 do {
207 progress = false;
208
209 /* Update liveout for all blocks. */
210 for (int b = 0; b < cfg->num_blocks; b++) {
211 if (cfg->blocks[b]->parents.is_empty())
212 continue;
213
214 for (int i = 0; i < bitset_words; i++) {
215 const BITSET_WORD old_liveout = bd[b].liveout[i];
216
217 bd[b].liveout[i] =
218 bd[b].copy[i] | (bd[b].livein[i] & ~bd[b].kill[i]);
219
220 if (old_liveout != bd[b].liveout[i])
221 progress = true;
222 }
223 }
224
225 /* Update livein for all blocks. If a copy is live out of all parent
226 * blocks, it's live coming in to this block.
227 */
228 for (int b = 0; b < cfg->num_blocks; b++) {
229 if (cfg->blocks[b]->parents.is_empty())
230 continue;
231
232 for (int i = 0; i < bitset_words; i++) {
233 const BITSET_WORD old_livein = bd[b].livein[i];
234
235 bd[b].livein[i] = ~0u;
236 foreach_list_typed(bblock_link, link, link, &cfg->blocks[b]->parents) {
237 bblock_t *block = link->block;
238 bd[b].livein[i] &= bd[block->block_num].liveout[i];
239 }
240
241 if (old_livein != bd[b].livein[i])
242 progress = true;
243 }
244 }
245 } while (progress);
246 }
247
248 void
249 fs_copy_prop_dataflow::dump_block_data() const
250 {
251 for (int b = 0; b < cfg->num_blocks; b++) {
252 bblock_t *block = cfg->blocks[b];
253 fprintf(stderr, "Block %d [%d, %d] (parents ", block->block_num,
254 block->start_ip, block->end_ip);
255 foreach_list_typed(bblock_link, link, link, &block->parents) {
256 bblock_t *parent = link->block;
257 fprintf(stderr, "%d ", parent->block_num);
258 }
259 fprintf(stderr, "):\n");
260 fprintf(stderr, " livein = 0x");
261 for (int i = 0; i < bitset_words; i++)
262 fprintf(stderr, "%08x", bd[b].livein[i]);
263 fprintf(stderr, ", liveout = 0x");
264 for (int i = 0; i < bitset_words; i++)
265 fprintf(stderr, "%08x", bd[b].liveout[i]);
266 fprintf(stderr, ",\n copy = 0x");
267 for (int i = 0; i < bitset_words; i++)
268 fprintf(stderr, "%08x", bd[b].copy[i]);
269 fprintf(stderr, ", kill = 0x");
270 for (int i = 0; i < bitset_words; i++)
271 fprintf(stderr, "%08x", bd[b].kill[i]);
272 fprintf(stderr, "\n");
273 }
274 }
275
276 static bool
277 is_logic_op(enum opcode opcode)
278 {
279 return (opcode == BRW_OPCODE_AND ||
280 opcode == BRW_OPCODE_OR ||
281 opcode == BRW_OPCODE_XOR ||
282 opcode == BRW_OPCODE_NOT);
283 }
284
285 bool
286 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
287 {
288 if (entry->src.file == IMM)
289 return false;
290
291 if (entry->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
292 inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD)
293 return false;
294
295 /* Bail if inst is reading more than entry is writing. */
296 if ((inst->regs_read(this, arg) * inst->src[arg].stride *
297 type_sz(inst->src[arg].type)) > type_sz(entry->dst.type))
298 return false;
299
300 if (inst->src[arg].file != entry->dst.file ||
301 inst->src[arg].reg != entry->dst.reg ||
302 inst->src[arg].reg_offset != entry->dst.reg_offset ||
303 inst->src[arg].subreg_offset != entry->dst.subreg_offset) {
304 return false;
305 }
306
307 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
308 if (inst->conditional_mod &&
309 inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
310 entry->src.negate)
311 return false;
312
313 bool has_source_modifiers = entry->src.abs || entry->src.negate;
314
315 if ((has_source_modifiers || entry->src.file == UNIFORM ||
316 !entry->src.is_contiguous()) &&
317 !can_do_source_mods(inst))
318 return false;
319
320 /* Bail if the result of composing both strides would exceed the
321 * hardware limit.
322 */
323 if (entry->src.stride * inst->src[arg].stride > 4)
324 return false;
325
326 /* Bail if the result of composing both strides cannot be expressed
327 * as another stride. This avoids, for example, trying to transform
328 * this:
329 *
330 * MOV (8) rX<1>UD rY<0;1,0>UD
331 * FOO (8) ... rX<8;8,1>UW
332 *
333 * into this:
334 *
335 * FOO (8) ... rY<0;1,0>UW
336 *
337 * Which would have different semantics.
338 */
339 if (entry->src.stride != 1 &&
340 (inst->src[arg].stride *
341 type_sz(inst->src[arg].type)) % type_sz(entry->src.type) != 0)
342 return false;
343
344 if (has_source_modifiers && entry->dst.type != inst->src[arg].type)
345 return false;
346
347 if (brw->gen >= 8) {
348 if (entry->src.negate) {
349 if (is_logic_op(inst->opcode)) {
350 return false;
351 }
352 }
353 }
354
355 inst->src[arg].file = entry->src.file;
356 inst->src[arg].reg = entry->src.reg;
357 inst->src[arg].reg_offset = entry->src.reg_offset;
358 inst->src[arg].subreg_offset = entry->src.subreg_offset;
359 inst->src[arg].stride *= entry->src.stride;
360
361 if (!inst->src[arg].abs) {
362 inst->src[arg].abs = entry->src.abs;
363 inst->src[arg].negate ^= entry->src.negate;
364 }
365
366 return true;
367 }
368
369
370 bool
371 fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)
372 {
373 bool progress = false;
374
375 if (entry->src.file != IMM)
376 return false;
377
378 for (int i = inst->sources - 1; i >= 0; i--) {
379 if (inst->src[i].file != entry->dst.file ||
380 inst->src[i].reg != entry->dst.reg ||
381 inst->src[i].reg_offset != entry->dst.reg_offset ||
382 inst->src[i].subreg_offset != entry->dst.subreg_offset ||
383 inst->src[i].type != entry->dst.type ||
384 inst->src[i].stride > 1)
385 continue;
386
387 /* Don't bother with cases that should have been taken care of by the
388 * GLSL compiler's constant folding pass.
389 */
390 if (inst->src[i].negate || inst->src[i].abs)
391 continue;
392
393 switch (inst->opcode) {
394 case BRW_OPCODE_MOV:
395 inst->src[i] = entry->src;
396 progress = true;
397 break;
398
399 case BRW_OPCODE_BFI1:
400 case BRW_OPCODE_ASR:
401 case BRW_OPCODE_SHL:
402 case BRW_OPCODE_SHR:
403 case BRW_OPCODE_SUBB:
404 if (i == 1) {
405 inst->src[i] = entry->src;
406 progress = true;
407 }
408 break;
409
410 case BRW_OPCODE_MACH:
411 case BRW_OPCODE_MUL:
412 case BRW_OPCODE_ADD:
413 case BRW_OPCODE_OR:
414 case BRW_OPCODE_AND:
415 case BRW_OPCODE_XOR:
416 case BRW_OPCODE_ADDC:
417 if (i == 1) {
418 inst->src[i] = entry->src;
419 progress = true;
420 } else if (i == 0 && inst->src[1].file != IMM) {
421 /* Fit this constant in by commuting the operands.
422 * Exception: we can't do this for 32-bit integer MUL/MACH
423 * because it's asymmetric.
424 */
425 if ((inst->opcode == BRW_OPCODE_MUL ||
426 inst->opcode == BRW_OPCODE_MACH) &&
427 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
428 inst->src[1].type == BRW_REGISTER_TYPE_UD))
429 break;
430 inst->src[0] = inst->src[1];
431 inst->src[1] = entry->src;
432 progress = true;
433 }
434 break;
435
436 case BRW_OPCODE_CMP:
437 case BRW_OPCODE_IF:
438 if (i == 1) {
439 inst->src[i] = entry->src;
440 progress = true;
441 } else if (i == 0 && inst->src[1].file != IMM) {
442 uint32_t new_cmod;
443
444 new_cmod = brw_swap_cmod(inst->conditional_mod);
445 if (new_cmod != ~0u) {
446 /* Fit this constant in by swapping the operands and
447 * flipping the test
448 */
449 inst->src[0] = inst->src[1];
450 inst->src[1] = entry->src;
451 inst->conditional_mod = new_cmod;
452 progress = true;
453 }
454 }
455 break;
456
457 case BRW_OPCODE_SEL:
458 if (i == 1) {
459 inst->src[i] = entry->src;
460 progress = true;
461 } else if (i == 0 && inst->src[1].file != IMM) {
462 inst->src[0] = inst->src[1];
463 inst->src[1] = entry->src;
464
465 /* If this was predicated, flipping operands means
466 * we also need to flip the predicate.
467 */
468 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
469 inst->predicate_inverse =
470 !inst->predicate_inverse;
471 }
472 progress = true;
473 }
474 break;
475
476 case SHADER_OPCODE_RCP:
477 /* The hardware doesn't do math on immediate values
478 * (because why are you doing that, seriously?), but
479 * the correct answer is to just constant fold it
480 * anyway.
481 */
482 assert(i == 0);
483 if (inst->src[0].imm.f != 0.0f) {
484 inst->opcode = BRW_OPCODE_MOV;
485 inst->src[0] = entry->src;
486 inst->src[0].imm.f = 1.0f / inst->src[0].imm.f;
487 progress = true;
488 }
489 break;
490
491 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
492 inst->src[i] = entry->src;
493 progress = true;
494 break;
495
496 default:
497 break;
498 }
499 }
500
501 return progress;
502 }
503
504 static bool
505 can_propagate_from(fs_inst *inst)
506 {
507 return (inst->opcode == BRW_OPCODE_MOV &&
508 inst->dst.file == GRF &&
509 ((inst->src[0].file == GRF &&
510 (inst->src[0].reg != inst->dst.reg ||
511 inst->src[0].reg_offset != inst->dst.reg_offset)) ||
512 inst->src[0].file == UNIFORM ||
513 inst->src[0].file == IMM) &&
514 inst->src[0].type == inst->dst.type &&
515 !inst->saturate &&
516 !inst->is_partial_write());
517 }
518
519 /* Walks a basic block and does copy propagation on it using the acp
520 * list.
521 */
522 bool
523 fs_visitor::opt_copy_propagate_local(void *copy_prop_ctx, bblock_t *block,
524 exec_list *acp)
525 {
526 bool progress = false;
527
528 for (fs_inst *inst = (fs_inst *)block->start;
529 inst != block->end->next;
530 inst = (fs_inst *)inst->next) {
531
532 /* Try propagating into this instruction. */
533 for (int i = 0; i < inst->sources; i++) {
534 if (inst->src[i].file != GRF)
535 continue;
536
537 foreach_list(entry_node, &acp[inst->src[i].reg % ACP_HASH_SIZE]) {
538 acp_entry *entry = (acp_entry *)entry_node;
539
540 if (try_constant_propagate(inst, entry))
541 progress = true;
542
543 if (try_copy_propagate(inst, i, entry))
544 progress = true;
545 }
546 }
547
548 /* kill the destination from the ACP */
549 if (inst->dst.file == GRF) {
550 foreach_list_safe(entry_node, &acp[inst->dst.reg % ACP_HASH_SIZE]) {
551 acp_entry *entry = (acp_entry *)entry_node;
552
553 if (inst->overwrites_reg(entry->dst)) {
554 entry->remove();
555 }
556 }
557
558 /* Oops, we only have the chaining hash based on the destination, not
559 * the source, so walk across the entire table.
560 */
561 for (int i = 0; i < ACP_HASH_SIZE; i++) {
562 foreach_list_safe(entry_node, &acp[i]) {
563 acp_entry *entry = (acp_entry *)entry_node;
564 if (inst->overwrites_reg(entry->src))
565 entry->remove();
566 }
567 }
568 }
569
570 /* If this instruction's source could potentially be folded into the
571 * operand of another instruction, add it to the ACP.
572 */
573 if (can_propagate_from(inst)) {
574 acp_entry *entry = ralloc(copy_prop_ctx, acp_entry);
575 entry->dst = inst->dst;
576 entry->src = inst->src[0];
577 entry->opcode = inst->opcode;
578 acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
579 } else if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
580 inst->dst.file == GRF) {
581 for (int i = 0; i < inst->sources; i++) {
582 if (inst->src[i].file == GRF) {
583 acp_entry *entry = ralloc(copy_prop_ctx, acp_entry);
584 entry->dst = inst->dst;
585 entry->dst.reg_offset = i;
586 entry->src = inst->src[i];
587 entry->opcode = inst->opcode;
588 if (!entry->dst.equals(inst->src[i])) {
589 acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
590 } else {
591 ralloc_free(entry);
592 }
593 }
594 }
595 }
596 }
597
598 return progress;
599 }
600
601 bool
602 fs_visitor::opt_copy_propagate()
603 {
604 bool progress = false;
605 void *copy_prop_ctx = ralloc_context(NULL);
606 cfg_t cfg(&instructions);
607 exec_list *out_acp[cfg.num_blocks];
608 for (int i = 0; i < cfg.num_blocks; i++)
609 out_acp[i] = new exec_list [ACP_HASH_SIZE];
610
611 /* First, walk through each block doing local copy propagation and getting
612 * the set of copies available at the end of the block.
613 */
614 for (int b = 0; b < cfg.num_blocks; b++) {
615 bblock_t *block = cfg.blocks[b];
616
617 progress = opt_copy_propagate_local(copy_prop_ctx, block,
618 out_acp[b]) || progress;
619 }
620
621 /* Do dataflow analysis for those available copies. */
622 fs_copy_prop_dataflow dataflow(copy_prop_ctx, &cfg, out_acp);
623
624 /* Next, re-run local copy propagation, this time with the set of copies
625 * provided by the dataflow analysis available at the start of a block.
626 */
627 for (int b = 0; b < cfg.num_blocks; b++) {
628 bblock_t *block = cfg.blocks[b];
629 exec_list in_acp[ACP_HASH_SIZE];
630
631 for (int i = 0; i < dataflow.num_acp; i++) {
632 if (BITSET_TEST(dataflow.bd[b].livein, i)) {
633 struct acp_entry *entry = dataflow.acp[i];
634 in_acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
635 }
636 }
637
638 progress = opt_copy_propagate_local(copy_prop_ctx, block, in_acp) || progress;
639 }
640
641 for (int i = 0; i < cfg.num_blocks; i++)
642 delete [] out_acp[i];
643 ralloc_free(copy_prop_ctx);
644
645 if (progress)
646 invalidate_live_intervals();
647
648 return progress;
649 }