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