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