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