intel/fs: Don't let undefined values prevent copy propagation.
[mesa.git] / src / intel / compiler / 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 "util/bitset.h"
38 #include "brw_fs.h"
39 #include "brw_fs_live_variables.h"
40 #include "brw_cfg.h"
41 #include "brw_eu.h"
42
43 using namespace brw;
44
45 namespace { /* avoid conflict with opt_copy_propagation_elements */
46 struct acp_entry : public exec_node {
47 fs_reg dst;
48 fs_reg src;
49 uint8_t size_written;
50 uint8_t size_read;
51 enum opcode opcode;
52 bool saturate;
53 };
54
55 struct block_data {
56 /**
57 * Which entries in the fs_copy_prop_dataflow acp table are live at the
58 * start of this block. This is the useful output of the analysis, since
59 * it lets us plug those into the local copy propagation on the second
60 * pass.
61 */
62 BITSET_WORD *livein;
63
64 /**
65 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
66 * of this block. This is done in initial setup from the per-block acps
67 * returned by the first local copy prop pass.
68 */
69 BITSET_WORD *liveout;
70
71 /**
72 * Which entries in the fs_copy_prop_dataflow acp table are generated by
73 * instructions in this block which reach the end of the block without
74 * being killed.
75 */
76 BITSET_WORD *copy;
77
78 /**
79 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
80 * course of this block.
81 */
82 BITSET_WORD *kill;
83
84 /**
85 * Which entries in the fs_copy_prop_dataflow acp table are guaranteed to
86 * have a fully uninitialized destination at the end of this block.
87 */
88 BITSET_WORD *undef;
89 };
90
91 class fs_copy_prop_dataflow
92 {
93 public:
94 fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
95 const fs_live_variables *live,
96 exec_list *out_acp[ACP_HASH_SIZE]);
97
98 void setup_initial_values();
99 void run();
100
101 void dump_block_data() const UNUSED;
102
103 void *mem_ctx;
104 cfg_t *cfg;
105 const fs_live_variables *live;
106
107 acp_entry **acp;
108 int num_acp;
109 int bitset_words;
110
111 struct block_data *bd;
112 };
113 } /* anonymous namespace */
114
115 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
116 const fs_live_variables *live,
117 exec_list *out_acp[ACP_HASH_SIZE])
118 : mem_ctx(mem_ctx), cfg(cfg), live(live)
119 {
120 bd = rzalloc_array(mem_ctx, struct block_data, cfg->num_blocks);
121
122 num_acp = 0;
123 foreach_block (block, cfg) {
124 for (int i = 0; i < ACP_HASH_SIZE; i++) {
125 num_acp += out_acp[block->num][i].length();
126 }
127 }
128
129 acp = rzalloc_array(mem_ctx, struct acp_entry *, num_acp);
130
131 bitset_words = BITSET_WORDS(num_acp);
132
133 int next_acp = 0;
134 foreach_block (block, cfg) {
135 bd[block->num].livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
136 bd[block->num].liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
137 bd[block->num].copy = rzalloc_array(bd, BITSET_WORD, bitset_words);
138 bd[block->num].kill = rzalloc_array(bd, BITSET_WORD, bitset_words);
139 bd[block->num].undef = rzalloc_array(bd, BITSET_WORD, bitset_words);
140
141 for (int i = 0; i < ACP_HASH_SIZE; i++) {
142 foreach_in_list(acp_entry, entry, &out_acp[block->num][i]) {
143 acp[next_acp] = entry;
144
145 /* opt_copy_propagation_local populates out_acp with copies created
146 * in a block which are still live at the end of the block. This
147 * is exactly what we want in the COPY set.
148 */
149 BITSET_SET(bd[block->num].copy, next_acp);
150
151 next_acp++;
152 }
153 }
154 }
155
156 assert(next_acp == num_acp);
157
158 setup_initial_values();
159 run();
160 }
161
162 /**
163 * Set up initial values for each of the data flow sets, prior to running
164 * the fixed-point algorithm.
165 */
166 void
167 fs_copy_prop_dataflow::setup_initial_values()
168 {
169 /* Initialize the COPY and KILL sets. */
170 foreach_block (block, cfg) {
171 foreach_inst_in_block(fs_inst, inst, block) {
172 if (inst->dst.file != VGRF)
173 continue;
174
175 /* Mark ACP entries which are killed by this instruction. */
176 for (int i = 0; i < num_acp; i++) {
177 if (regions_overlap(inst->dst, inst->size_written,
178 acp[i]->dst, acp[i]->size_written) ||
179 regions_overlap(inst->dst, inst->size_written,
180 acp[i]->src, acp[i]->size_read)) {
181 BITSET_SET(bd[block->num].kill, i);
182 }
183 }
184 }
185 }
186
187 /* Populate the initial values for the livein and liveout sets. For the
188 * block at the start of the program, livein = 0 and liveout = copy.
189 * For the others, set liveout to 0 (the empty set) and livein to ~0
190 * (the universal set).
191 */
192 foreach_block (block, cfg) {
193 if (block->parents.is_empty()) {
194 for (int i = 0; i < bitset_words; i++) {
195 bd[block->num].livein[i] = 0u;
196 bd[block->num].liveout[i] = bd[block->num].copy[i];
197 }
198 } else {
199 for (int i = 0; i < bitset_words; i++) {
200 bd[block->num].liveout[i] = 0u;
201 bd[block->num].livein[i] = ~0u;
202 }
203 }
204 }
205
206 /* Initialize the undef set. */
207 foreach_block (block, cfg) {
208 for (int i = 0; i < num_acp; i++) {
209 BITSET_SET(bd[block->num].undef, i);
210 for (unsigned off = 0; off < acp[i]->size_written; off += REG_SIZE) {
211 if (BITSET_TEST(live->block_data[block->num].defout,
212 live->var_from_reg(byte_offset(acp[i]->dst, off))))
213 BITSET_CLEAR(bd[block->num].undef, i);
214 }
215 }
216 }
217 }
218
219 /**
220 * Walk the set of instructions in the block, marking which entries in the acp
221 * are killed by the block.
222 */
223 void
224 fs_copy_prop_dataflow::run()
225 {
226 bool progress;
227
228 do {
229 progress = false;
230
231 /* Update liveout for all blocks. */
232 foreach_block (block, cfg) {
233 if (block->parents.is_empty())
234 continue;
235
236 for (int i = 0; i < bitset_words; i++) {
237 const BITSET_WORD old_liveout = bd[block->num].liveout[i];
238
239 bd[block->num].liveout[i] =
240 bd[block->num].copy[i] | (bd[block->num].livein[i] &
241 ~bd[block->num].kill[i]);
242
243 if (old_liveout != bd[block->num].liveout[i])
244 progress = true;
245 }
246 }
247
248 /* Update livein for all blocks. If a copy is live out of all parent
249 * blocks, it's live coming in to this block.
250 */
251 foreach_block (block, cfg) {
252 if (block->parents.is_empty())
253 continue;
254
255 for (int i = 0; i < bitset_words; i++) {
256 const BITSET_WORD old_livein = bd[block->num].livein[i];
257 BITSET_WORD livein_from_any_block = 0;
258
259 bd[block->num].livein[i] = ~0u;
260 foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
261 bblock_t *parent = parent_link->block;
262 /* Consider ACP entries with a known-undefined destination to
263 * be available from the parent. This is valid because we're
264 * free to set the undefined variable equal to the source of
265 * the ACP entry without breaking the application's
266 * expectations, since the variable is undefined.
267 */
268 bd[block->num].livein[i] &= (bd[parent->num].liveout[i] |
269 bd[parent->num].undef[i]);
270 livein_from_any_block |= bd[parent->num].liveout[i];
271 }
272
273 /* Limit to the set of ACP entries that can possibly be available
274 * at the start of the block, since propagating from a variable
275 * which is guaranteed to be undefined (rather than potentially
276 * undefined for some dynamic control-flow paths) doesn't seem
277 * particularly useful.
278 */
279 bd[block->num].livein[i] &= livein_from_any_block;
280
281 if (old_livein != bd[block->num].livein[i])
282 progress = true;
283 }
284 }
285 } while (progress);
286 }
287
288 void
289 fs_copy_prop_dataflow::dump_block_data() const
290 {
291 foreach_block (block, cfg) {
292 fprintf(stderr, "Block %d [%d, %d] (parents ", block->num,
293 block->start_ip, block->end_ip);
294 foreach_list_typed(bblock_link, link, link, &block->parents) {
295 bblock_t *parent = link->block;
296 fprintf(stderr, "%d ", parent->num);
297 }
298 fprintf(stderr, "):\n");
299 fprintf(stderr, " livein = 0x");
300 for (int i = 0; i < bitset_words; i++)
301 fprintf(stderr, "%08x", bd[block->num].livein[i]);
302 fprintf(stderr, ", liveout = 0x");
303 for (int i = 0; i < bitset_words; i++)
304 fprintf(stderr, "%08x", bd[block->num].liveout[i]);
305 fprintf(stderr, ",\n copy = 0x");
306 for (int i = 0; i < bitset_words; i++)
307 fprintf(stderr, "%08x", bd[block->num].copy[i]);
308 fprintf(stderr, ", kill = 0x");
309 for (int i = 0; i < bitset_words; i++)
310 fprintf(stderr, "%08x", bd[block->num].kill[i]);
311 fprintf(stderr, "\n");
312 }
313 }
314
315 static bool
316 is_logic_op(enum opcode opcode)
317 {
318 return (opcode == BRW_OPCODE_AND ||
319 opcode == BRW_OPCODE_OR ||
320 opcode == BRW_OPCODE_XOR ||
321 opcode == BRW_OPCODE_NOT);
322 }
323
324 static bool
325 can_take_stride(fs_inst *inst, unsigned arg, unsigned stride,
326 const gen_device_info *devinfo)
327 {
328 if (stride > 4)
329 return false;
330
331 /* 3-source instructions can only be Align16, which restricts what strides
332 * they can take. They can only take a stride of 1 (the usual case), or 0
333 * with a special "repctrl" bit. But the repctrl bit doesn't work for
334 * 64-bit datatypes, so if the source type is 64-bit then only a stride of
335 * 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page
336 * 944:
337 *
338 * This is applicable to 32b datatypes and 16b datatype. 64b datatypes
339 * cannot use the replicate control.
340 */
341 if (inst->is_3src(devinfo)) {
342 if (type_sz(inst->src[arg].type) > 4)
343 return stride == 1;
344 else
345 return stride == 1 || stride == 0;
346 }
347
348 /* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
349 * page 391 ("Extended Math Function"):
350 *
351 * The following restrictions apply for align1 mode: Scalar source is
352 * supported. Source and destination horizontal stride must be the
353 * same.
354 *
355 * From the Haswell PRM Volume 2b "Command Reference - Instructions", page
356 * 134 ("Extended Math Function"):
357 *
358 * Scalar source is supported. Source and destination horizontal stride
359 * must be 1.
360 *
361 * and similar language exists for IVB and SNB. Pre-SNB, math instructions
362 * are sends, so the sources are moved to MRF's and there are no
363 * restrictions.
364 */
365 if (inst->is_math()) {
366 if (devinfo->gen == 6 || devinfo->gen == 7) {
367 assert(inst->dst.stride == 1);
368 return stride == 1 || stride == 0;
369 } else if (devinfo->gen >= 8) {
370 return stride == inst->dst.stride || stride == 0;
371 }
372 }
373
374 return true;
375 }
376
377 bool
378 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
379 {
380 if (inst->src[arg].file != VGRF)
381 return false;
382
383 if (entry->src.file == IMM)
384 return false;
385 assert(entry->src.file == VGRF || entry->src.file == UNIFORM ||
386 entry->src.file == ATTR);
387
388 if (entry->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
389 inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD)
390 return false;
391
392 assert(entry->dst.file == VGRF);
393 if (inst->src[arg].nr != entry->dst.nr)
394 return false;
395
396 /* Bail if inst is reading a range that isn't contained in the range
397 * that entry is writing.
398 */
399 if (!region_contained_in(inst->src[arg], inst->size_read(arg),
400 entry->dst, entry->size_written))
401 return false;
402
403 /* we can't generally copy-propagate UD negations because we
404 * can end up accessing the resulting values as signed integers
405 * instead. See also resolve_ud_negate() and comment in
406 * fs_generator::generate_code.
407 */
408 if (entry->src.type == BRW_REGISTER_TYPE_UD &&
409 entry->src.negate)
410 return false;
411
412 bool has_source_modifiers = entry->src.abs || entry->src.negate;
413
414 if ((has_source_modifiers || entry->src.file == UNIFORM ||
415 !entry->src.is_contiguous()) &&
416 !inst->can_do_source_mods(devinfo))
417 return false;
418
419 if (has_source_modifiers &&
420 inst->opcode == SHADER_OPCODE_GEN4_SCRATCH_WRITE)
421 return false;
422
423 /* Bail if the result of composing both strides would exceed the
424 * hardware limit.
425 */
426 if (!can_take_stride(inst, arg, entry->src.stride * inst->src[arg].stride,
427 devinfo))
428 return false;
429
430 /* Bail if the instruction type is larger than the execution type of the
431 * copy, what implies that each channel is reading multiple channels of the
432 * destination of the copy, and simply replacing the sources would give a
433 * program with different semantics.
434 */
435 if (type_sz(entry->dst.type) < type_sz(inst->src[arg].type))
436 return false;
437
438 /* Bail if the result of composing both strides cannot be expressed
439 * as another stride. This avoids, for example, trying to transform
440 * this:
441 *
442 * MOV (8) rX<1>UD rY<0;1,0>UD
443 * FOO (8) ... rX<8;8,1>UW
444 *
445 * into this:
446 *
447 * FOO (8) ... rY<0;1,0>UW
448 *
449 * Which would have different semantics.
450 */
451 if (entry->src.stride != 1 &&
452 (inst->src[arg].stride *
453 type_sz(inst->src[arg].type)) % type_sz(entry->src.type) != 0)
454 return false;
455
456 /* Since semantics of source modifiers are type-dependent we need to
457 * ensure that the meaning of the instruction remains the same if we
458 * change the type. If the sizes of the types are different the new
459 * instruction will read a different amount of data than the original
460 * and the semantics will always be different.
461 */
462 if (has_source_modifiers &&
463 entry->dst.type != inst->src[arg].type &&
464 (!inst->can_change_types() ||
465 type_sz(entry->dst.type) != type_sz(inst->src[arg].type)))
466 return false;
467
468 if (devinfo->gen >= 8 && (entry->src.negate || entry->src.abs) &&
469 is_logic_op(inst->opcode)) {
470 return false;
471 }
472
473 if (entry->saturate) {
474 switch(inst->opcode) {
475 case BRW_OPCODE_SEL:
476 if ((inst->conditional_mod != BRW_CONDITIONAL_GE &&
477 inst->conditional_mod != BRW_CONDITIONAL_L) ||
478 inst->src[1].file != IMM ||
479 inst->src[1].f < 0.0 ||
480 inst->src[1].f > 1.0) {
481 return false;
482 }
483 break;
484 default:
485 return false;
486 }
487 }
488
489 inst->src[arg].file = entry->src.file;
490 inst->src[arg].nr = entry->src.nr;
491 inst->src[arg].stride *= entry->src.stride;
492 inst->saturate = inst->saturate || entry->saturate;
493
494 /* Compute the offset of inst->src[arg] relative to entry->dst */
495 const unsigned rel_offset = inst->src[arg].offset - entry->dst.offset;
496
497 /* Compute the first component of the copy that the instruction is
498 * reading, and the base byte offset within that component.
499 */
500 assert(entry->dst.offset % REG_SIZE == 0 && entry->dst.stride == 1);
501 const unsigned component = rel_offset / type_sz(entry->dst.type);
502 const unsigned suboffset = rel_offset % type_sz(entry->dst.type);
503
504 /* Calculate the byte offset at the origin of the copy of the given
505 * component and suboffset.
506 */
507 inst->src[arg].offset = suboffset +
508 component * entry->src.stride * type_sz(entry->src.type) +
509 entry->src.offset;
510
511 if (has_source_modifiers) {
512 if (entry->dst.type != inst->src[arg].type) {
513 /* We are propagating source modifiers from a MOV with a different
514 * type. If we got here, then we can just change the source and
515 * destination types of the instruction and keep going.
516 */
517 assert(inst->can_change_types());
518 for (int i = 0; i < inst->sources; i++) {
519 inst->src[i].type = entry->dst.type;
520 }
521 inst->dst.type = entry->dst.type;
522 }
523
524 if (!inst->src[arg].abs) {
525 inst->src[arg].abs = entry->src.abs;
526 inst->src[arg].negate ^= entry->src.negate;
527 }
528 }
529
530 return true;
531 }
532
533
534 bool
535 fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)
536 {
537 bool progress = false;
538
539 if (entry->src.file != IMM)
540 return false;
541 if (type_sz(entry->src.type) > 4)
542 return false;
543 if (entry->saturate)
544 return false;
545
546 for (int i = inst->sources - 1; i >= 0; i--) {
547 if (inst->src[i].file != VGRF)
548 continue;
549
550 assert(entry->dst.file == VGRF);
551 if (inst->src[i].nr != entry->dst.nr)
552 continue;
553
554 /* Bail if inst is reading a range that isn't contained in the range
555 * that entry is writing.
556 */
557 if (!region_contained_in(inst->src[i], inst->size_read(i),
558 entry->dst, entry->size_written))
559 continue;
560
561 /* If the type sizes don't match each channel of the instruction is
562 * either extracting a portion of the constant (which could be handled
563 * with some effort but the code below doesn't) or reading multiple
564 * channels of the source at once.
565 */
566 if (type_sz(inst->src[i].type) != type_sz(entry->dst.type))
567 continue;
568
569 fs_reg val = entry->src;
570 val.type = inst->src[i].type;
571
572 if (inst->src[i].abs) {
573 if ((devinfo->gen >= 8 && is_logic_op(inst->opcode)) ||
574 !brw_abs_immediate(val.type, &val.as_brw_reg())) {
575 continue;
576 }
577 }
578
579 if (inst->src[i].negate) {
580 if ((devinfo->gen >= 8 && is_logic_op(inst->opcode)) ||
581 !brw_negate_immediate(val.type, &val.as_brw_reg())) {
582 continue;
583 }
584 }
585
586 switch (inst->opcode) {
587 case BRW_OPCODE_MOV:
588 case SHADER_OPCODE_LOAD_PAYLOAD:
589 case FS_OPCODE_PACK:
590 inst->src[i] = val;
591 progress = true;
592 break;
593
594 case SHADER_OPCODE_INT_QUOTIENT:
595 case SHADER_OPCODE_INT_REMAINDER:
596 /* FINISHME: Promote non-float constants and remove this. */
597 if (devinfo->gen < 8)
598 break;
599 /* fallthrough */
600 case SHADER_OPCODE_POW:
601 /* Allow constant propagation into src1 (except on Gen 6 which
602 * doesn't support scalar source math), and let constant combining
603 * promote the constant on Gen < 8.
604 */
605 if (devinfo->gen == 6)
606 break;
607 /* fallthrough */
608 case BRW_OPCODE_BFI1:
609 case BRW_OPCODE_ASR:
610 case BRW_OPCODE_SHL:
611 case BRW_OPCODE_SHR:
612 case BRW_OPCODE_SUBB:
613 if (i == 1) {
614 inst->src[i] = val;
615 progress = true;
616 }
617 break;
618
619 case BRW_OPCODE_MACH:
620 case BRW_OPCODE_MUL:
621 case SHADER_OPCODE_MULH:
622 case BRW_OPCODE_ADD:
623 case BRW_OPCODE_OR:
624 case BRW_OPCODE_AND:
625 case BRW_OPCODE_XOR:
626 case BRW_OPCODE_ADDC:
627 if (i == 1) {
628 inst->src[i] = val;
629 progress = true;
630 } else if (i == 0 && inst->src[1].file != IMM) {
631 /* Fit this constant in by commuting the operands.
632 * Exception: we can't do this for 32-bit integer MUL/MACH
633 * because it's asymmetric.
634 *
635 * The BSpec says for Broadwell that
636 *
637 * "When multiplying DW x DW, the dst cannot be accumulator."
638 *
639 * Integer MUL with a non-accumulator destination will be lowered
640 * by lower_integer_multiplication(), so don't restrict it.
641 */
642 if (((inst->opcode == BRW_OPCODE_MUL &&
643 inst->dst.is_accumulator()) ||
644 inst->opcode == BRW_OPCODE_MACH) &&
645 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
646 inst->src[1].type == BRW_REGISTER_TYPE_UD))
647 break;
648 inst->src[0] = inst->src[1];
649 inst->src[1] = val;
650 progress = true;
651 }
652 break;
653
654 case BRW_OPCODE_CMP:
655 case BRW_OPCODE_IF:
656 if (i == 1) {
657 inst->src[i] = val;
658 progress = true;
659 } else if (i == 0 && inst->src[1].file != IMM) {
660 enum brw_conditional_mod new_cmod;
661
662 new_cmod = brw_swap_cmod(inst->conditional_mod);
663 if (new_cmod != BRW_CONDITIONAL_NONE) {
664 /* Fit this constant in by swapping the operands and
665 * flipping the test
666 */
667 inst->src[0] = inst->src[1];
668 inst->src[1] = val;
669 inst->conditional_mod = new_cmod;
670 progress = true;
671 }
672 }
673 break;
674
675 case BRW_OPCODE_SEL:
676 if (i == 1) {
677 inst->src[i] = val;
678 progress = true;
679 } else if (i == 0 && inst->src[1].file != IMM) {
680 inst->src[0] = inst->src[1];
681 inst->src[1] = val;
682
683 /* If this was predicated, flipping operands means
684 * we also need to flip the predicate.
685 */
686 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
687 inst->predicate_inverse =
688 !inst->predicate_inverse;
689 }
690 progress = true;
691 }
692 break;
693
694 case SHADER_OPCODE_UNTYPED_ATOMIC:
695 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
696 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
697 case SHADER_OPCODE_TYPED_ATOMIC:
698 case SHADER_OPCODE_TYPED_SURFACE_READ:
699 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
700 case SHADER_OPCODE_BYTE_SCATTERED_WRITE:
701 case SHADER_OPCODE_BYTE_SCATTERED_READ:
702 /* We only propagate into the surface argument of the
703 * instruction. Everything else goes through LOAD_PAYLOAD.
704 */
705 if (i == 1) {
706 inst->src[i] = val;
707 progress = true;
708 }
709 break;
710
711 case FS_OPCODE_FB_WRITE_LOGICAL:
712 /* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are
713 * bit-cast using a strided region so they cannot be immediates.
714 */
715 if (i != FB_WRITE_LOGICAL_SRC_SRC_STENCIL &&
716 i != FB_WRITE_LOGICAL_SRC_OMASK) {
717 inst->src[i] = val;
718 progress = true;
719 }
720 break;
721
722 case SHADER_OPCODE_TEX_LOGICAL:
723 case SHADER_OPCODE_TXD_LOGICAL:
724 case SHADER_OPCODE_TXF_LOGICAL:
725 case SHADER_OPCODE_TXL_LOGICAL:
726 case SHADER_OPCODE_TXS_LOGICAL:
727 case FS_OPCODE_TXB_LOGICAL:
728 case SHADER_OPCODE_TXF_CMS_LOGICAL:
729 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
730 case SHADER_OPCODE_TXF_UMS_LOGICAL:
731 case SHADER_OPCODE_TXF_MCS_LOGICAL:
732 case SHADER_OPCODE_LOD_LOGICAL:
733 case SHADER_OPCODE_TG4_LOGICAL:
734 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
735 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
736 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
737 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
738 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
739 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
740 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
741 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
742 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
743 inst->src[i] = val;
744 progress = true;
745 break;
746
747 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
748 case SHADER_OPCODE_BROADCAST:
749 inst->src[i] = val;
750 progress = true;
751 break;
752
753 case BRW_OPCODE_MAD:
754 case BRW_OPCODE_LRP:
755 inst->src[i] = val;
756 progress = true;
757 break;
758
759 default:
760 break;
761 }
762 }
763
764 return progress;
765 }
766
767 static bool
768 can_propagate_from(fs_inst *inst)
769 {
770 return (inst->opcode == BRW_OPCODE_MOV &&
771 inst->dst.file == VGRF &&
772 ((inst->src[0].file == VGRF &&
773 !regions_overlap(inst->dst, inst->size_written,
774 inst->src[0], inst->size_read(0))) ||
775 inst->src[0].file == ATTR ||
776 inst->src[0].file == UNIFORM ||
777 inst->src[0].file == IMM) &&
778 inst->src[0].type == inst->dst.type &&
779 !inst->is_partial_write());
780 }
781
782 /* Walks a basic block and does copy propagation on it using the acp
783 * list.
784 */
785 bool
786 fs_visitor::opt_copy_propagation_local(void *copy_prop_ctx, bblock_t *block,
787 exec_list *acp)
788 {
789 bool progress = false;
790
791 foreach_inst_in_block(fs_inst, inst, block) {
792 /* Try propagating into this instruction. */
793 for (int i = 0; i < inst->sources; i++) {
794 if (inst->src[i].file != VGRF)
795 continue;
796
797 foreach_in_list(acp_entry, entry, &acp[inst->src[i].nr % ACP_HASH_SIZE]) {
798 if (try_constant_propagate(inst, entry))
799 progress = true;
800 else if (try_copy_propagate(inst, i, entry))
801 progress = true;
802 }
803 }
804
805 /* kill the destination from the ACP */
806 if (inst->dst.file == VGRF) {
807 foreach_in_list_safe(acp_entry, entry, &acp[inst->dst.nr % ACP_HASH_SIZE]) {
808 if (regions_overlap(entry->dst, entry->size_written,
809 inst->dst, inst->size_written))
810 entry->remove();
811 }
812
813 /* Oops, we only have the chaining hash based on the destination, not
814 * the source, so walk across the entire table.
815 */
816 for (int i = 0; i < ACP_HASH_SIZE; i++) {
817 foreach_in_list_safe(acp_entry, entry, &acp[i]) {
818 /* Make sure we kill the entry if this instruction overwrites
819 * _any_ of the registers that it reads
820 */
821 if (regions_overlap(entry->src, entry->size_read,
822 inst->dst, inst->size_written))
823 entry->remove();
824 }
825 }
826 }
827
828 /* If this instruction's source could potentially be folded into the
829 * operand of another instruction, add it to the ACP.
830 */
831 if (can_propagate_from(inst)) {
832 acp_entry *entry = ralloc(copy_prop_ctx, acp_entry);
833 entry->dst = inst->dst;
834 entry->src = inst->src[0];
835 entry->size_written = inst->size_written;
836 entry->size_read = inst->size_read(0);
837 entry->opcode = inst->opcode;
838 entry->saturate = inst->saturate;
839 acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);
840 } else if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
841 inst->dst.file == VGRF) {
842 int offset = 0;
843 for (int i = 0; i < inst->sources; i++) {
844 int effective_width = i < inst->header_size ? 8 : inst->exec_size;
845 assert(effective_width * type_sz(inst->src[i].type) % REG_SIZE == 0);
846 const unsigned size_written = effective_width *
847 type_sz(inst->src[i].type);
848 if (inst->src[i].file == VGRF) {
849 acp_entry *entry = rzalloc(copy_prop_ctx, acp_entry);
850 entry->dst = byte_offset(inst->dst, offset);
851 entry->src = inst->src[i];
852 entry->size_written = size_written;
853 entry->size_read = inst->size_read(i);
854 entry->opcode = inst->opcode;
855 if (!entry->dst.equals(inst->src[i])) {
856 acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);
857 } else {
858 ralloc_free(entry);
859 }
860 }
861 offset += size_written;
862 }
863 }
864 }
865
866 return progress;
867 }
868
869 bool
870 fs_visitor::opt_copy_propagation()
871 {
872 bool progress = false;
873 void *copy_prop_ctx = ralloc_context(NULL);
874 exec_list *out_acp[cfg->num_blocks];
875
876 for (int i = 0; i < cfg->num_blocks; i++)
877 out_acp[i] = new exec_list [ACP_HASH_SIZE];
878
879 calculate_live_intervals();
880
881 /* First, walk through each block doing local copy propagation and getting
882 * the set of copies available at the end of the block.
883 */
884 foreach_block (block, cfg) {
885 progress = opt_copy_propagation_local(copy_prop_ctx, block,
886 out_acp[block->num]) || progress;
887 }
888
889 /* Do dataflow analysis for those available copies. */
890 fs_copy_prop_dataflow dataflow(copy_prop_ctx, cfg, live_intervals, out_acp);
891
892 /* Next, re-run local copy propagation, this time with the set of copies
893 * provided by the dataflow analysis available at the start of a block.
894 */
895 foreach_block (block, cfg) {
896 exec_list in_acp[ACP_HASH_SIZE];
897
898 for (int i = 0; i < dataflow.num_acp; i++) {
899 if (BITSET_TEST(dataflow.bd[block->num].livein, i)) {
900 struct acp_entry *entry = dataflow.acp[i];
901 in_acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);
902 }
903 }
904
905 progress = opt_copy_propagation_local(copy_prop_ctx, block, in_acp) ||
906 progress;
907 }
908
909 for (int i = 0; i < cfg->num_blocks; i++)
910 delete [] out_acp[i];
911 ralloc_free(copy_prop_ctx);
912
913 if (progress)
914 invalidate_live_intervals();
915
916 return progress;
917 }