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