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 #include "util/bitset.h"
36 #include "util/u_math.h"
37 #include "util/rb_tree.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 {
47 struct rb_node by_dst;
48 struct rb_node by_src;
49 fs_reg dst;
50 fs_reg src;
51 unsigned global_idx;
52 unsigned size_written;
53 unsigned size_read;
54 enum opcode opcode;
55 bool is_partial_write;
56 bool force_writemask_all;
57 };
58
59 /**
60 * Compare two acp_entry::src.nr
61 *
62 * This is intended to be used as the comparison function for rb_tree.
63 */
64 static int
cmp_entry_dst_entry_dst(const struct rb_node * a_node,const struct rb_node * b_node)65 cmp_entry_dst_entry_dst(const struct rb_node *a_node, const struct rb_node *b_node)
66 {
67 const struct acp_entry *a_entry =
68 rb_node_data(struct acp_entry, a_node, by_dst);
69
70 const struct acp_entry *b_entry =
71 rb_node_data(struct acp_entry, b_node, by_dst);
72
73 return a_entry->dst.nr - b_entry->dst.nr;
74 }
75
76 static int
cmp_entry_dst_nr(const struct rb_node * a_node,const void * b_key)77 cmp_entry_dst_nr(const struct rb_node *a_node, const void *b_key)
78 {
79 const struct acp_entry *a_entry =
80 rb_node_data(struct acp_entry, a_node, by_dst);
81
82 return a_entry->dst.nr - (uintptr_t) b_key;
83 }
84
85 static int
cmp_entry_src_entry_src(const struct rb_node * a_node,const struct rb_node * b_node)86 cmp_entry_src_entry_src(const struct rb_node *a_node, const struct rb_node *b_node)
87 {
88 const struct acp_entry *a_entry =
89 rb_node_data(struct acp_entry, a_node, by_src);
90
91 const struct acp_entry *b_entry =
92 rb_node_data(struct acp_entry, b_node, by_src);
93
94 return a_entry->src.nr - b_entry->src.nr;
95 }
96
97 /**
98 * Compare an acp_entry::src.nr with a raw nr.
99 *
100 * This is intended to be used as the comparison function for rb_tree.
101 */
102 static int
cmp_entry_src_nr(const struct rb_node * a_node,const void * b_key)103 cmp_entry_src_nr(const struct rb_node *a_node, const void *b_key)
104 {
105 const struct acp_entry *a_entry =
106 rb_node_data(struct acp_entry, a_node, by_src);
107
108 return a_entry->src.nr - (uintptr_t) b_key;
109 }
110
111 class acp_forward_iterator {
112 public:
acp_forward_iterator(struct rb_node * n,unsigned offset)113 acp_forward_iterator(struct rb_node *n, unsigned offset)
114 : curr(n), next(nullptr), offset(offset)
115 {
116 next = rb_node_next_or_null(curr);
117 }
118
operator ++()119 acp_forward_iterator &operator++()
120 {
121 curr = next;
122 next = rb_node_next_or_null(curr);
123
124 return *this;
125 }
126
operator !=(const acp_forward_iterator & other) const127 bool operator!=(const acp_forward_iterator &other) const
128 {
129 return curr != other.curr;
130 }
131
operator *() const132 struct acp_entry *operator*() const
133 {
134 /* This open-codes part of rb_node_data. */
135 return curr != NULL ? (struct acp_entry *)(((char *)curr) - offset)
136 : NULL;
137 }
138
139 private:
140 struct rb_node *curr;
141 struct rb_node *next;
142 unsigned offset;
143 };
144
145 struct acp {
146 struct rb_tree by_dst;
147 struct rb_tree by_src;
148
acp__anon7b6d3a4e0111::acp149 acp()
150 {
151 rb_tree_init(&by_dst);
152 rb_tree_init(&by_src);
153 }
154
begin__anon7b6d3a4e0111::acp155 acp_forward_iterator begin()
156 {
157 return acp_forward_iterator(rb_tree_first(&by_src),
158 rb_tree_offsetof(struct acp_entry, by_src, 0));
159 }
160
end__anon7b6d3a4e0111::acp161 const acp_forward_iterator end() const
162 {
163 return acp_forward_iterator(nullptr, 0);
164 }
165
length__anon7b6d3a4e0111::acp166 unsigned length()
167 {
168 unsigned l = 0;
169
170 for (rb_node *iter = rb_tree_first(&by_src);
171 iter != NULL; iter = rb_node_next(iter))
172 l++;
173
174 return l;
175 }
176
add__anon7b6d3a4e0111::acp177 void add(acp_entry *entry)
178 {
179 rb_tree_insert(&by_dst, &entry->by_dst, cmp_entry_dst_entry_dst);
180 rb_tree_insert(&by_src, &entry->by_src, cmp_entry_src_entry_src);
181 }
182
remove__anon7b6d3a4e0111::acp183 void remove(acp_entry *entry)
184 {
185 rb_tree_remove(&by_dst, &entry->by_dst);
186 rb_tree_remove(&by_src, &entry->by_src);
187 }
188
find_by_src__anon7b6d3a4e0111::acp189 acp_forward_iterator find_by_src(unsigned nr)
190 {
191 struct rb_node *rbn = rb_tree_search(&by_src,
192 (void *)(uintptr_t) nr,
193 cmp_entry_src_nr);
194
195 return acp_forward_iterator(rbn, rb_tree_offsetof(struct acp_entry,
196 by_src, rbn));
197 }
198
find_by_dst__anon7b6d3a4e0111::acp199 acp_forward_iterator find_by_dst(unsigned nr)
200 {
201 struct rb_node *rbn = rb_tree_search(&by_dst,
202 (void *)(uintptr_t) nr,
203 cmp_entry_dst_nr);
204
205 return acp_forward_iterator(rbn, rb_tree_offsetof(struct acp_entry,
206 by_dst, rbn));
207 }
208 };
209
210 struct block_data {
211 /**
212 * Which entries in the fs_copy_prop_dataflow acp table are live at the
213 * start of this block. This is the useful output of the analysis, since
214 * it lets us plug those into the local copy propagation on the second
215 * pass.
216 */
217 BITSET_WORD *livein;
218
219 /**
220 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
221 * of this block. This is done in initial setup from the per-block acps
222 * returned by the first local copy prop pass.
223 */
224 BITSET_WORD *liveout;
225
226 /**
227 * Which entries in the fs_copy_prop_dataflow acp table are generated by
228 * instructions in this block which reach the end of the block without
229 * being killed.
230 */
231 BITSET_WORD *copy;
232
233 /**
234 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
235 * course of this block.
236 */
237 BITSET_WORD *kill;
238
239 /**
240 * Which entries in the fs_copy_prop_dataflow acp table are guaranteed to
241 * have a fully uninitialized destination at the end of this block.
242 */
243 BITSET_WORD *undef;
244
245 /**
246 * Which entries in the fs_copy_prop_dataflow acp table can the
247 * start of this block be reached from. Note that this is a weaker
248 * condition than livein.
249 */
250 BITSET_WORD *reachin;
251
252 /**
253 * Which entries in the fs_copy_prop_dataflow acp table are
254 * overwritten by an instruction with channel masks inconsistent
255 * with the copy instruction (e.g. due to force_writemask_all).
256 * Such an overwrite can cause the copy entry to become invalid
257 * even if the copy instruction is subsequently re-executed for any
258 * given channel i, since the execution of the overwrite for
259 * channel i may corrupt other channels j!=i inactive for the
260 * subsequent copy.
261 */
262 BITSET_WORD *exec_mismatch;
263 };
264
265 class fs_copy_prop_dataflow
266 {
267 public:
268 fs_copy_prop_dataflow(linear_ctx *lin_ctx, cfg_t *cfg,
269 const fs_live_variables &live,
270 struct acp *out_acp);
271
272 void setup_initial_values();
273 void run();
274
275 void dump_block_data() const UNUSED;
276
277 cfg_t *cfg;
278 const fs_live_variables &live;
279
280 acp_entry **acp;
281 int num_acp;
282 int bitset_words;
283
284 struct block_data *bd;
285 };
286 } /* anonymous namespace */
287
fs_copy_prop_dataflow(linear_ctx * lin_ctx,cfg_t * cfg,const fs_live_variables & live,struct acp * out_acp)288 fs_copy_prop_dataflow::fs_copy_prop_dataflow(linear_ctx *lin_ctx, cfg_t *cfg,
289 const fs_live_variables &live,
290 struct acp *out_acp)
291 : cfg(cfg), live(live)
292 {
293 bd = linear_zalloc_array(lin_ctx, struct block_data, cfg->num_blocks);
294
295 num_acp = 0;
296 foreach_block (block, cfg)
297 num_acp += out_acp[block->num].length();
298
299 bitset_words = BITSET_WORDS(num_acp);
300
301 foreach_block (block, cfg) {
302 bd[block->num].livein = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
303 bd[block->num].liveout = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
304 bd[block->num].copy = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
305 bd[block->num].kill = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
306 bd[block->num].undef = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
307 bd[block->num].reachin = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
308 bd[block->num].exec_mismatch = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
309 }
310
311 acp = linear_zalloc_array(lin_ctx, struct acp_entry *, num_acp);
312
313 int next_acp = 0;
314 foreach_block (block, cfg) {
315 for (auto iter = out_acp[block->num].begin();
316 iter != out_acp[block->num].end(); ++iter) {
317 acp[next_acp] = *iter;
318
319 (*iter)->global_idx = next_acp;
320
321 /* opt_copy_propagation_local populates out_acp with copies created
322 * in a block which are still live at the end of the block. This
323 * is exactly what we want in the COPY set.
324 */
325 BITSET_SET(bd[block->num].copy, next_acp);
326
327 next_acp++;
328 }
329 }
330
331 assert(next_acp == num_acp);
332
333 setup_initial_values();
334 run();
335 }
336
337 /**
338 * Like reg_offset, but register must be VGRF or FIXED_GRF.
339 */
340 static inline unsigned
grf_reg_offset(const fs_reg & r)341 grf_reg_offset(const fs_reg &r)
342 {
343 return (r.file == VGRF ? 0 : r.nr) * REG_SIZE +
344 r.offset +
345 (r.file == FIXED_GRF ? r.subnr : 0);
346 }
347
348 /**
349 * Like regions_overlap, but register must be VGRF or FIXED_GRF.
350 */
351 static inline bool
grf_regions_overlap(const fs_reg & r,unsigned dr,const fs_reg & s,unsigned ds)352 grf_regions_overlap(const fs_reg &r, unsigned dr, const fs_reg &s, unsigned ds)
353 {
354 return reg_space(r) == reg_space(s) &&
355 !(grf_reg_offset(r) + dr <= grf_reg_offset(s) ||
356 grf_reg_offset(s) + ds <= grf_reg_offset(r));
357 }
358
359 /**
360 * Set up initial values for each of the data flow sets, prior to running
361 * the fixed-point algorithm.
362 */
363 void
setup_initial_values()364 fs_copy_prop_dataflow::setup_initial_values()
365 {
366 /* Initialize the COPY and KILL sets. */
367 {
368 struct acp acp_table;
369
370 /* First, get all the KILLs for instructions which overwrite ACP
371 * destinations.
372 */
373 for (int i = 0; i < num_acp; i++)
374 acp_table.add(acp[i]);
375
376 foreach_block (block, cfg) {
377 foreach_inst_in_block(fs_inst, inst, block) {
378 if (inst->dst.file != VGRF &&
379 inst->dst.file != FIXED_GRF)
380 continue;
381
382 for (auto iter = acp_table.find_by_src(inst->dst.nr);
383 iter != acp_table.end() && (*iter)->src.nr == inst->dst.nr;
384 ++iter) {
385 if (grf_regions_overlap(inst->dst, inst->size_written,
386 (*iter)->src, (*iter)->size_read)) {
387 BITSET_SET(bd[block->num].kill, (*iter)->global_idx);
388 if (inst->force_writemask_all && !(*iter)->force_writemask_all)
389 BITSET_SET(bd[block->num].exec_mismatch, (*iter)->global_idx);
390 }
391 }
392
393 if (inst->dst.file != VGRF)
394 continue;
395
396 for (auto iter = acp_table.find_by_dst(inst->dst.nr);
397 iter != acp_table.end() && (*iter)->dst.nr == inst->dst.nr;
398 ++iter) {
399 if (grf_regions_overlap(inst->dst, inst->size_written,
400 (*iter)->dst, (*iter)->size_written)) {
401 BITSET_SET(bd[block->num].kill, (*iter)->global_idx);
402 if (inst->force_writemask_all && !(*iter)->force_writemask_all)
403 BITSET_SET(bd[block->num].exec_mismatch, (*iter)->global_idx);
404 }
405 }
406 }
407 }
408 }
409
410 /* Populate the initial values for the livein and liveout sets. For the
411 * block at the start of the program, livein = 0 and liveout = copy.
412 * For the others, set liveout and livein to ~0 (the universal set).
413 */
414 foreach_block (block, cfg) {
415 if (block->parents.is_empty()) {
416 for (int i = 0; i < bitset_words; i++) {
417 bd[block->num].livein[i] = 0u;
418 bd[block->num].liveout[i] = bd[block->num].copy[i];
419 }
420 } else {
421 for (int i = 0; i < bitset_words; i++) {
422 bd[block->num].liveout[i] = ~0u;
423 bd[block->num].livein[i] = ~0u;
424 }
425 }
426 }
427
428 /* Initialize the undef set. */
429 foreach_block (block, cfg) {
430 for (int i = 0; i < num_acp; i++) {
431 BITSET_SET(bd[block->num].undef, i);
432 for (unsigned off = 0; off < acp[i]->size_written; off += REG_SIZE) {
433 if (BITSET_TEST(live.block_data[block->num].defout,
434 live.var_from_reg(byte_offset(acp[i]->dst, off))))
435 BITSET_CLEAR(bd[block->num].undef, i);
436 }
437 }
438 }
439 }
440
441 /**
442 * Walk the set of instructions in the block, marking which entries in the acp
443 * are killed by the block.
444 */
445 void
run()446 fs_copy_prop_dataflow::run()
447 {
448 bool progress;
449
450 do {
451 progress = false;
452
453 foreach_block (block, cfg) {
454 if (block->parents.is_empty())
455 continue;
456
457 for (int i = 0; i < bitset_words; i++) {
458 const BITSET_WORD old_liveout = bd[block->num].liveout[i];
459 const BITSET_WORD old_reachin = bd[block->num].reachin[i];
460 BITSET_WORD livein_from_any_block = 0;
461
462 /* Update livein for this block. If a copy is live out of all
463 * parent blocks, it's live coming in to this block.
464 */
465 bd[block->num].livein[i] = ~0u;
466 foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
467 bblock_t *parent = parent_link->block;
468 /* Consider ACP entries with a known-undefined destination to
469 * be available from the parent. This is valid because we're
470 * free to set the undefined variable equal to the source of
471 * the ACP entry without breaking the application's
472 * expectations, since the variable is undefined.
473 */
474 bd[block->num].livein[i] &= (bd[parent->num].liveout[i] |
475 bd[parent->num].undef[i]);
476 livein_from_any_block |= bd[parent->num].liveout[i];
477
478 /* Update reachin for this block. If the end of any
479 * parent block is reachable from the copy, the start
480 * of this block is reachable from it as well.
481 */
482 bd[block->num].reachin[i] |= (bd[parent->num].reachin[i] |
483 bd[parent->num].copy[i]);
484 }
485
486 /* Limit to the set of ACP entries that can possibly be available
487 * at the start of the block, since propagating from a variable
488 * which is guaranteed to be undefined (rather than potentially
489 * undefined for some dynamic control-flow paths) doesn't seem
490 * particularly useful.
491 */
492 bd[block->num].livein[i] &= livein_from_any_block;
493
494 /* Update liveout for this block. */
495 bd[block->num].liveout[i] =
496 bd[block->num].copy[i] | (bd[block->num].livein[i] &
497 ~bd[block->num].kill[i]);
498
499 if (old_liveout != bd[block->num].liveout[i] ||
500 old_reachin != bd[block->num].reachin[i])
501 progress = true;
502 }
503 }
504 } while (progress);
505
506 /* Perform a second fixed-point pass in order to propagate the
507 * exec_mismatch bitsets. Note that this requires an accurate
508 * value of the reachin bitsets as input, which isn't available
509 * until the end of the first propagation pass, so this loop cannot
510 * be folded into the previous one.
511 */
512 do {
513 progress = false;
514
515 foreach_block (block, cfg) {
516 for (int i = 0; i < bitset_words; i++) {
517 const BITSET_WORD old_exec_mismatch = bd[block->num].exec_mismatch[i];
518
519 /* Update exec_mismatch for this block. If the end of a
520 * parent block is reachable by an overwrite with
521 * inconsistent execution masking, the start of this block
522 * is reachable by such an overwrite as well.
523 */
524 foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
525 bblock_t *parent = parent_link->block;
526 bd[block->num].exec_mismatch[i] |= (bd[parent->num].exec_mismatch[i] &
527 bd[parent->num].reachin[i]);
528 }
529
530 /* Only consider overwrites with inconsistent execution
531 * masking if they are reachable from the copy, since
532 * overwrites unreachable from a copy are harmless to that
533 * copy.
534 */
535 bd[block->num].exec_mismatch[i] &= bd[block->num].reachin[i];
536 if (old_exec_mismatch != bd[block->num].exec_mismatch[i])
537 progress = true;
538 }
539 }
540 } while (progress);
541 }
542
543 void
dump_block_data() const544 fs_copy_prop_dataflow::dump_block_data() const
545 {
546 foreach_block (block, cfg) {
547 fprintf(stderr, "Block %d [%d, %d] (parents ", block->num,
548 block->start_ip, block->end_ip);
549 foreach_list_typed(bblock_link, link, link, &block->parents) {
550 bblock_t *parent = link->block;
551 fprintf(stderr, "%d ", parent->num);
552 }
553 fprintf(stderr, "):\n");
554 fprintf(stderr, " livein = 0x");
555 for (int i = 0; i < bitset_words; i++)
556 fprintf(stderr, "%08x", bd[block->num].livein[i]);
557 fprintf(stderr, ", liveout = 0x");
558 for (int i = 0; i < bitset_words; i++)
559 fprintf(stderr, "%08x", bd[block->num].liveout[i]);
560 fprintf(stderr, ",\n copy = 0x");
561 for (int i = 0; i < bitset_words; i++)
562 fprintf(stderr, "%08x", bd[block->num].copy[i]);
563 fprintf(stderr, ", kill = 0x");
564 for (int i = 0; i < bitset_words; i++)
565 fprintf(stderr, "%08x", bd[block->num].kill[i]);
566 fprintf(stderr, "\n");
567 }
568 }
569
570 static bool
is_logic_op(enum opcode opcode)571 is_logic_op(enum opcode opcode)
572 {
573 return (opcode == BRW_OPCODE_AND ||
574 opcode == BRW_OPCODE_OR ||
575 opcode == BRW_OPCODE_XOR ||
576 opcode == BRW_OPCODE_NOT);
577 }
578
579 static bool
can_take_stride(fs_inst * inst,brw_reg_type dst_type,unsigned arg,unsigned stride,const struct brw_compiler * compiler)580 can_take_stride(fs_inst *inst, brw_reg_type dst_type,
581 unsigned arg, unsigned stride,
582 const struct brw_compiler *compiler)
583 {
584 const struct intel_device_info *devinfo = compiler->devinfo;
585
586 if (stride > 4)
587 return false;
588
589 /* Bail if the channels of the source need to be aligned to the byte offset
590 * of the corresponding channel of the destination, and the provided stride
591 * would break this restriction.
592 */
593 if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&
594 !(type_sz(inst->src[arg].type) * stride ==
595 type_sz(dst_type) * inst->dst.stride ||
596 stride == 0))
597 return false;
598
599 /* 3-source instructions can only be Align16, which restricts what strides
600 * they can take. They can only take a stride of 1 (the usual case), or 0
601 * with a special "repctrl" bit. But the repctrl bit doesn't work for
602 * 64-bit datatypes, so if the source type is 64-bit then only a stride of
603 * 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page
604 * 944:
605 *
606 * This is applicable to 32b datatypes and 16b datatype. 64b datatypes
607 * cannot use the replicate control.
608 */
609 if (inst->is_3src(compiler)) {
610 if (type_sz(inst->src[arg].type) > 4)
611 return stride == 1;
612 else
613 return stride == 1 || stride == 0;
614 }
615
616 /* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
617 * page 391 ("Extended Math Function"):
618 *
619 * The following restrictions apply for align1 mode: Scalar source is
620 * supported. Source and destination horizontal stride must be the
621 * same.
622 */
623 if (inst->is_math())
624 return stride == inst->dst.stride || stride == 0;
625
626 return true;
627 }
628
629 static bool
instruction_requires_packed_data(fs_inst * inst)630 instruction_requires_packed_data(fs_inst *inst)
631 {
632 switch (inst->opcode) {
633 case FS_OPCODE_DDX_FINE:
634 case FS_OPCODE_DDX_COARSE:
635 case FS_OPCODE_DDY_FINE:
636 case FS_OPCODE_DDY_COARSE:
637 case SHADER_OPCODE_QUAD_SWIZZLE:
638 return true;
639 default:
640 return false;
641 }
642 }
643
644 static bool
try_copy_propagate(const brw_compiler * compiler,fs_inst * inst,acp_entry * entry,int arg,const brw::simple_allocator & alloc,uint8_t max_polygons)645 try_copy_propagate(const brw_compiler *compiler, fs_inst *inst,
646 acp_entry *entry, int arg,
647 const brw::simple_allocator &alloc,
648 uint8_t max_polygons)
649 {
650 if (inst->src[arg].file != VGRF)
651 return false;
652
653 const struct intel_device_info *devinfo = compiler->devinfo;
654
655 assert(entry->src.file == VGRF || entry->src.file == UNIFORM ||
656 entry->src.file == ATTR || entry->src.file == FIXED_GRF);
657
658 /* Avoid propagating a LOAD_PAYLOAD instruction into another if there is a
659 * good chance that we'll be able to eliminate the latter through register
660 * coalescing. If only part of the sources of the second LOAD_PAYLOAD can
661 * be simplified through copy propagation we would be making register
662 * coalescing impossible, ending up with unnecessary copies in the program.
663 * This is also the case for is_multi_copy_payload() copies that can only
664 * be coalesced when the instruction is lowered into a sequence of MOVs.
665 *
666 * Worse -- In cases where the ACP entry was the result of CSE combining
667 * multiple LOAD_PAYLOAD subexpressions, propagating the first LOAD_PAYLOAD
668 * into the second would undo the work of CSE, leading to an infinite
669 * optimization loop. Avoid this by detecting LOAD_PAYLOAD copies from CSE
670 * temporaries which should match is_coalescing_payload().
671 */
672 if (entry->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
673 (is_coalescing_payload(alloc, inst) || is_multi_copy_payload(inst)))
674 return false;
675
676 assert(entry->dst.file == VGRF);
677 if (inst->src[arg].nr != entry->dst.nr)
678 return false;
679
680 /* Bail if inst is reading a range that isn't contained in the range
681 * that entry is writing.
682 */
683 if (!region_contained_in(inst->src[arg], inst->size_read(arg),
684 entry->dst, entry->size_written))
685 return false;
686
687 /* Send messages with EOT set are restricted to use g112-g127 (and we
688 * sometimes need g127 for other purposes), so avoid copy propagating
689 * anything that would make it impossible to satisfy that restriction.
690 */
691 if (inst->eot) {
692 /* Avoid propagating a FIXED_GRF register, as that's already pinned. */
693 if (entry->src.file == FIXED_GRF)
694 return false;
695
696 /* We might be propagating from a large register, while the SEND only
697 * is reading a portion of it (say the .A channel in an RGBA value).
698 * We need to pin both split SEND sources in g112-g126/127, so only
699 * allow this if the registers aren't too large.
700 */
701 if (inst->opcode == SHADER_OPCODE_SEND && entry->src.file == VGRF) {
702 int other_src = arg == 2 ? 3 : 2;
703 unsigned other_size = inst->src[other_src].file == VGRF ?
704 alloc.sizes[inst->src[other_src].nr] :
705 inst->size_read(other_src);
706 unsigned prop_src_size = alloc.sizes[entry->src.nr];
707 if (other_size + prop_src_size > 15)
708 return false;
709 }
710 }
711
712 /* we can't generally copy-propagate UD negations because we
713 * can end up accessing the resulting values as signed integers
714 * instead. See also resolve_ud_negate() and comment in
715 * fs_generator::generate_code.
716 */
717 if (entry->src.type == BRW_REGISTER_TYPE_UD &&
718 entry->src.negate)
719 return false;
720
721 bool has_source_modifiers = entry->src.abs || entry->src.negate;
722
723 if (has_source_modifiers && !inst->can_do_source_mods(devinfo))
724 return false;
725
726 /* Reject cases that would violate register regioning restrictions. */
727 if ((entry->src.file == UNIFORM || !entry->src.is_contiguous()) &&
728 (inst->is_send_from_grf() ||
729 inst->uses_indirect_addressing())) {
730 return false;
731 }
732
733 /* Some instructions implemented in the generator backend, such as
734 * derivatives, assume that their operands are packed so we can't
735 * generally propagate strided regions to them.
736 */
737 const unsigned entry_stride = (entry->src.file == FIXED_GRF ? 1 :
738 entry->src.stride);
739 if (instruction_requires_packed_data(inst) && entry_stride != 1)
740 return false;
741
742 const brw_reg_type dst_type = (has_source_modifiers &&
743 entry->dst.type != inst->src[arg].type) ?
744 entry->dst.type : inst->dst.type;
745
746 /* Bail if the result of composing both strides would exceed the
747 * hardware limit.
748 */
749 if (!can_take_stride(inst, dst_type, arg,
750 entry_stride * inst->src[arg].stride,
751 compiler))
752 return false;
753
754 /* From the Cherry Trail/Braswell PRMs, Volume 7: 3D Media GPGPU:
755 * EU Overview
756 * Register Region Restrictions
757 * Special Requirements for Handling Double Precision Data Types :
758 *
759 * "When source or destination datatype is 64b or operation is integer
760 * DWord multiply, regioning in Align1 must follow these rules:
761 *
762 * 1. Source and Destination horizontal stride must be aligned to the
763 * same qword.
764 * 2. Regioning must ensure Src.Vstride = Src.Width * Src.Hstride.
765 * 3. Source and Destination offset must be the same, except the case
766 * of scalar source."
767 *
768 * Most of this is already checked in can_take_stride(), we're only left
769 * with checking 3.
770 */
771 if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&
772 entry_stride != 0 &&
773 (reg_offset(inst->dst) % REG_SIZE) != (reg_offset(entry->src) % REG_SIZE))
774 return false;
775
776 /* The <8;8,0> regions used for FS attributes in multipolygon
777 * dispatch mode could violate regioning restrictions, don't copy
778 * propagate them in such cases.
779 */
780 if (entry->src.file == ATTR && max_polygons > 1 &&
781 (has_dst_aligned_region_restriction(devinfo, inst, dst_type) ||
782 instruction_requires_packed_data(inst) ||
783 (inst->is_3src(compiler) && arg == 2) ||
784 entry->dst.type != inst->src[arg].type))
785 return false;
786
787 /* Bail if the source FIXED_GRF region of the copy cannot be trivially
788 * composed with the source region of the instruction -- E.g. because the
789 * copy uses some extended stride greater than 4 not supported natively by
790 * the hardware as a horizontal stride, or because instruction compression
791 * could require us to use a vertical stride shorter than a GRF.
792 */
793 if (entry->src.file == FIXED_GRF &&
794 (inst->src[arg].stride > 4 ||
795 inst->dst.component_size(inst->exec_size) >
796 inst->src[arg].component_size(inst->exec_size)))
797 return false;
798
799 /* Bail if the instruction type is larger than the execution type of the
800 * copy, what implies that each channel is reading multiple channels of the
801 * destination of the copy, and simply replacing the sources would give a
802 * program with different semantics.
803 */
804 if ((type_sz(entry->dst.type) < type_sz(inst->src[arg].type) ||
805 entry->is_partial_write) &&
806 inst->opcode != BRW_OPCODE_MOV) {
807 return false;
808 }
809
810 /* Bail if the result of composing both strides cannot be expressed
811 * as another stride. This avoids, for example, trying to transform
812 * this:
813 *
814 * MOV (8) rX<1>UD rY<0;1,0>UD
815 * FOO (8) ... rX<8;8,1>UW
816 *
817 * into this:
818 *
819 * FOO (8) ... rY<0;1,0>UW
820 *
821 * Which would have different semantics.
822 */
823 if (entry_stride != 1 &&
824 (inst->src[arg].stride *
825 type_sz(inst->src[arg].type)) % type_sz(entry->src.type) != 0)
826 return false;
827
828 /* Since semantics of source modifiers are type-dependent we need to
829 * ensure that the meaning of the instruction remains the same if we
830 * change the type. If the sizes of the types are different the new
831 * instruction will read a different amount of data than the original
832 * and the semantics will always be different.
833 */
834 if (has_source_modifiers &&
835 entry->dst.type != inst->src[arg].type &&
836 (!inst->can_change_types() ||
837 type_sz(entry->dst.type) != type_sz(inst->src[arg].type)))
838 return false;
839
840 if ((entry->src.negate || entry->src.abs) &&
841 is_logic_op(inst->opcode)) {
842 return false;
843 }
844
845 /* Save the offset of inst->src[arg] relative to entry->dst for it to be
846 * applied later.
847 */
848 const unsigned rel_offset = inst->src[arg].offset - entry->dst.offset;
849
850 /* Fold the copy into the instruction consuming it. */
851 inst->src[arg].file = entry->src.file;
852 inst->src[arg].nr = entry->src.nr;
853 inst->src[arg].subnr = entry->src.subnr;
854 inst->src[arg].offset = entry->src.offset;
855
856 /* Compose the strides of both regions. */
857 if (entry->src.file == FIXED_GRF) {
858 if (inst->src[arg].stride) {
859 const unsigned orig_width = 1 << entry->src.width;
860 const unsigned reg_width = REG_SIZE / (type_sz(inst->src[arg].type) *
861 inst->src[arg].stride);
862 inst->src[arg].width = cvt(MIN2(orig_width, reg_width)) - 1;
863 inst->src[arg].hstride = cvt(inst->src[arg].stride);
864 inst->src[arg].vstride = inst->src[arg].hstride + inst->src[arg].width;
865 } else {
866 inst->src[arg].vstride = inst->src[arg].hstride =
867 inst->src[arg].width = 0;
868 }
869
870 inst->src[arg].stride = 1;
871
872 /* Hopefully no Align16 around here... */
873 assert(entry->src.swizzle == BRW_SWIZZLE_XYZW);
874 inst->src[arg].swizzle = entry->src.swizzle;
875 } else {
876 inst->src[arg].stride *= entry->src.stride;
877 }
878
879 /* Compute the first component of the copy that the instruction is
880 * reading, and the base byte offset within that component.
881 */
882 assert((entry->dst.offset % REG_SIZE == 0 || inst->opcode == BRW_OPCODE_MOV) &&
883 entry->dst.stride == 1);
884 const unsigned component = rel_offset / type_sz(entry->dst.type);
885 const unsigned suboffset = rel_offset % type_sz(entry->dst.type);
886
887 /* Calculate the byte offset at the origin of the copy of the given
888 * component and suboffset.
889 */
890 inst->src[arg] = byte_offset(inst->src[arg],
891 component * entry_stride * type_sz(entry->src.type) + suboffset);
892
893 if (has_source_modifiers) {
894 if (entry->dst.type != inst->src[arg].type) {
895 /* We are propagating source modifiers from a MOV with a different
896 * type. If we got here, then we can just change the source and
897 * destination types of the instruction and keep going.
898 */
899 for (int i = 0; i < inst->sources; i++) {
900 inst->src[i].type = entry->dst.type;
901 }
902 inst->dst.type = entry->dst.type;
903 }
904
905 if (!inst->src[arg].abs) {
906 inst->src[arg].abs = entry->src.abs;
907 inst->src[arg].negate ^= entry->src.negate;
908 }
909 }
910
911 return true;
912 }
913
914
915 static bool
try_constant_propagate(const brw_compiler * compiler,fs_inst * inst,acp_entry * entry,int arg)916 try_constant_propagate(const brw_compiler *compiler, fs_inst *inst,
917 acp_entry *entry, int arg)
918 {
919 bool progress = false;
920
921 if (type_sz(entry->src.type) > 4)
922 return false;
923
924 if (inst->src[arg].file != VGRF)
925 return false;
926
927 assert(entry->dst.file == VGRF);
928 if (inst->src[arg].nr != entry->dst.nr)
929 return false;
930
931 /* Bail if inst is reading a range that isn't contained in the range
932 * that entry is writing.
933 */
934 if (!region_contained_in(inst->src[arg], inst->size_read(arg),
935 entry->dst, entry->size_written))
936 return false;
937
938 /* If the size of the use type is larger than the size of the entry
939 * type, the entry doesn't contain all of the data that the user is
940 * trying to use.
941 */
942 if (type_sz(inst->src[arg].type) > type_sz(entry->dst.type))
943 return false;
944
945 fs_reg val = entry->src;
946
947 /* If the size of the use type is smaller than the size of the entry,
948 * clamp the value to the range of the use type. This enables constant
949 * copy propagation in cases like
950 *
951 *
952 * mov(8) g12<1>UD 0x0000000cUD
953 * ...
954 * mul(8) g47<1>D g86<8,8,1>D g12<16,8,2>W
955 */
956 if (type_sz(inst->src[arg].type) < type_sz(entry->dst.type)) {
957 if (type_sz(inst->src[arg].type) != 2 || type_sz(entry->dst.type) != 4)
958 return false;
959
960 assert(inst->src[arg].subnr == 0 || inst->src[arg].subnr == 2);
961
962 /* When subnr is 0, we want the lower 16-bits, and when it's 2, we
963 * want the upper 16-bits. No other values of subnr are valid for a
964 * UD source.
965 */
966 const uint16_t v = inst->src[arg].subnr == 2 ? val.ud >> 16 : val.ud;
967
968 val.ud = v | (uint32_t(v) << 16);
969 }
970
971 val.type = inst->src[arg].type;
972
973 if (inst->src[arg].abs) {
974 if (is_logic_op(inst->opcode) ||
975 !brw_abs_immediate(val.type, &val.as_brw_reg())) {
976 return false;
977 }
978 }
979
980 if (inst->src[arg].negate) {
981 if (is_logic_op(inst->opcode) ||
982 !brw_negate_immediate(val.type, &val.as_brw_reg())) {
983 return false;
984 }
985 }
986
987 switch (inst->opcode) {
988 case BRW_OPCODE_MOV:
989 case SHADER_OPCODE_LOAD_PAYLOAD:
990 case FS_OPCODE_PACK:
991 inst->src[arg] = val;
992 progress = true;
993 break;
994
995 case SHADER_OPCODE_POW:
996 if (arg == 1) {
997 inst->src[arg] = val;
998 progress = true;
999 }
1000 break;
1001
1002 case BRW_OPCODE_SUBB:
1003 if (arg == 1) {
1004 inst->src[arg] = val;
1005 progress = true;
1006 }
1007 break;
1008
1009 case BRW_OPCODE_MACH:
1010 case BRW_OPCODE_MUL:
1011 case SHADER_OPCODE_MULH:
1012 case BRW_OPCODE_ADD:
1013 case BRW_OPCODE_XOR:
1014 case BRW_OPCODE_ADDC:
1015 if (arg == 1) {
1016 inst->src[arg] = val;
1017 progress = true;
1018 } else if (arg == 0 && inst->src[1].file != IMM) {
1019 /* Don't copy propagate the constant in situations like
1020 *
1021 * mov(8) g8<1>D 0x7fffffffD
1022 * mul(8) g16<1>D g8<8,8,1>D g15<16,8,2>W
1023 *
1024 * On platforms that only have a 32x16 multiplier, this will
1025 * result in lowering the multiply to
1026 *
1027 * mul(8) g15<1>D g14<8,8,1>D 0xffffUW
1028 * mul(8) g16<1>D g14<8,8,1>D 0x7fffUW
1029 * add(8) g15.1<2>UW g15.1<16,8,2>UW g16<16,8,2>UW
1030 *
1031 * On Gfx8 and Gfx9, which have the full 32x32 multiplier, it
1032 * results in
1033 *
1034 * mul(8) g16<1>D g15<16,8,2>W 0x7fffffffD
1035 *
1036 * Volume 2a of the Skylake PRM says:
1037 *
1038 * When multiplying a DW and any lower precision integer, the
1039 * DW operand must on src0.
1040 */
1041 if (inst->opcode == BRW_OPCODE_MUL &&
1042 type_sz(inst->src[1].type) < 4 &&
1043 type_sz(val.type) == 4)
1044 break;
1045
1046 /* Fit this constant in by commuting the operands.
1047 * Exception: we can't do this for 32-bit integer MUL/MACH
1048 * because it's asymmetric.
1049 *
1050 * The BSpec says for Broadwell that
1051 *
1052 * "When multiplying DW x DW, the dst cannot be accumulator."
1053 *
1054 * Integer MUL with a non-accumulator destination will be lowered
1055 * by lower_integer_multiplication(), so don't restrict it.
1056 */
1057 if (((inst->opcode == BRW_OPCODE_MUL &&
1058 inst->dst.is_accumulator()) ||
1059 inst->opcode == BRW_OPCODE_MACH) &&
1060 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
1061 inst->src[1].type == BRW_REGISTER_TYPE_UD))
1062 break;
1063 inst->src[0] = inst->src[1];
1064 inst->src[1] = val;
1065 progress = true;
1066 }
1067 break;
1068
1069 case BRW_OPCODE_ADD3:
1070 /* add3 can have a single imm16 source. Proceed if the source type is
1071 * already W or UW or the value can be coerced to one of those types.
1072 */
1073 if (val.type == BRW_REGISTER_TYPE_W || val.type == BRW_REGISTER_TYPE_UW)
1074 ; /* Nothing to do. */
1075 else if (val.ud <= 0xffff)
1076 val = brw_imm_uw(val.ud);
1077 else if (val.d >= -0x8000 && val.d <= 0x7fff)
1078 val = brw_imm_w(val.d);
1079 else
1080 break;
1081
1082 if (arg == 2) {
1083 inst->src[arg] = val;
1084 progress = true;
1085 } else if (inst->src[2].file != IMM) {
1086 inst->src[arg] = inst->src[2];
1087 inst->src[2] = val;
1088 progress = true;
1089 }
1090
1091 break;
1092
1093 case BRW_OPCODE_CMP:
1094 case BRW_OPCODE_IF:
1095 if (arg == 1) {
1096 inst->src[arg] = val;
1097 progress = true;
1098 } else if (arg == 0 && inst->src[1].file != IMM) {
1099 enum brw_conditional_mod new_cmod;
1100
1101 new_cmod = brw_swap_cmod(inst->conditional_mod);
1102 if (new_cmod != BRW_CONDITIONAL_NONE) {
1103 /* Fit this constant in by swapping the operands and
1104 * flipping the test
1105 */
1106 inst->src[0] = inst->src[1];
1107 inst->src[1] = val;
1108 inst->conditional_mod = new_cmod;
1109 progress = true;
1110 }
1111 }
1112 break;
1113
1114 case BRW_OPCODE_SEL:
1115 if (arg == 1) {
1116 inst->src[arg] = val;
1117 progress = true;
1118 } else if (arg == 0) {
1119 if (inst->src[1].file != IMM &&
1120 (inst->conditional_mod == BRW_CONDITIONAL_NONE ||
1121 /* Only GE and L are commutative. */
1122 inst->conditional_mod == BRW_CONDITIONAL_GE ||
1123 inst->conditional_mod == BRW_CONDITIONAL_L)) {
1124 inst->src[0] = inst->src[1];
1125 inst->src[1] = val;
1126
1127 /* If this was predicated, flipping operands means
1128 * we also need to flip the predicate.
1129 */
1130 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
1131 inst->predicate_inverse =
1132 !inst->predicate_inverse;
1133 }
1134 } else {
1135 inst->src[0] = val;
1136 }
1137
1138 progress = true;
1139 }
1140 break;
1141
1142 case FS_OPCODE_FB_WRITE_LOGICAL:
1143 /* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are
1144 * bit-cast using a strided region so they cannot be immediates.
1145 */
1146 if (arg != FB_WRITE_LOGICAL_SRC_SRC_STENCIL &&
1147 arg != FB_WRITE_LOGICAL_SRC_OMASK) {
1148 inst->src[arg] = val;
1149 progress = true;
1150 }
1151 break;
1152
1153 case SHADER_OPCODE_INT_QUOTIENT:
1154 case SHADER_OPCODE_INT_REMAINDER:
1155 case BRW_OPCODE_AND:
1156 case BRW_OPCODE_ASR:
1157 case BRW_OPCODE_BFE:
1158 case BRW_OPCODE_BFI1:
1159 case BRW_OPCODE_BFI2:
1160 case BRW_OPCODE_ROL:
1161 case BRW_OPCODE_ROR:
1162 case BRW_OPCODE_SHL:
1163 case BRW_OPCODE_SHR:
1164 case BRW_OPCODE_OR:
1165 case SHADER_OPCODE_TEX_LOGICAL:
1166 case SHADER_OPCODE_TXD_LOGICAL:
1167 case SHADER_OPCODE_TXF_LOGICAL:
1168 case SHADER_OPCODE_TXL_LOGICAL:
1169 case SHADER_OPCODE_TXS_LOGICAL:
1170 case FS_OPCODE_TXB_LOGICAL:
1171 case SHADER_OPCODE_TXF_CMS_LOGICAL:
1172 case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
1173 case SHADER_OPCODE_TXF_CMS_W_GFX12_LOGICAL:
1174 case SHADER_OPCODE_TXF_UMS_LOGICAL:
1175 case SHADER_OPCODE_TXF_MCS_LOGICAL:
1176 case SHADER_OPCODE_LOD_LOGICAL:
1177 case SHADER_OPCODE_TG4_BIAS_LOGICAL:
1178 case SHADER_OPCODE_TG4_EXPLICIT_LOD_LOGICAL:
1179 case SHADER_OPCODE_TG4_IMPLICIT_LOD_LOGICAL:
1180 case SHADER_OPCODE_TG4_LOGICAL:
1181 case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
1182 case SHADER_OPCODE_TG4_OFFSET_LOD_LOGICAL:
1183 case SHADER_OPCODE_TG4_OFFSET_BIAS_LOGICAL:
1184 case SHADER_OPCODE_SAMPLEINFO_LOGICAL:
1185 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:
1186 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:
1187 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:
1188 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:
1189 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:
1190 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:
1191 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:
1192 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:
1193 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:
1194 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
1195 case SHADER_OPCODE_BROADCAST:
1196 case BRW_OPCODE_MAD:
1197 case BRW_OPCODE_LRP:
1198 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
1199 case SHADER_OPCODE_SHUFFLE:
1200 inst->src[arg] = val;
1201 progress = true;
1202 break;
1203
1204 default:
1205 break;
1206 }
1207
1208 return progress;
1209 }
1210
1211 static bool
can_propagate_from(fs_inst * inst)1212 can_propagate_from(fs_inst *inst)
1213 {
1214 return (inst->opcode == BRW_OPCODE_MOV &&
1215 inst->dst.file == VGRF &&
1216 ((inst->src[0].file == VGRF &&
1217 !grf_regions_overlap(inst->dst, inst->size_written,
1218 inst->src[0], inst->size_read(0))) ||
1219 inst->src[0].file == ATTR ||
1220 inst->src[0].file == UNIFORM ||
1221 inst->src[0].file == IMM ||
1222 (inst->src[0].file == FIXED_GRF &&
1223 inst->src[0].is_contiguous())) &&
1224 inst->src[0].type == inst->dst.type &&
1225 !inst->saturate &&
1226 /* Subset of !is_partial_write() conditions. */
1227 !inst->predicate && inst->dst.is_contiguous()) ||
1228 is_identity_payload(FIXED_GRF, inst);
1229 }
1230
1231 /* Walks a basic block and does copy propagation on it using the acp
1232 * list.
1233 */
1234 static bool
opt_copy_propagation_local(const brw_compiler * compiler,linear_ctx * lin_ctx,bblock_t * block,struct acp & acp,const brw::simple_allocator & alloc,uint8_t max_polygons)1235 opt_copy_propagation_local(const brw_compiler *compiler, linear_ctx *lin_ctx,
1236 bblock_t *block, struct acp &acp,
1237 const brw::simple_allocator &alloc,
1238 uint8_t max_polygons)
1239 {
1240 bool progress = false;
1241
1242 foreach_inst_in_block(fs_inst, inst, block) {
1243 /* Try propagating into this instruction. */
1244 bool instruction_progress = false;
1245 for (int i = inst->sources - 1; i >= 0; i--) {
1246 if (inst->src[i].file != VGRF)
1247 continue;
1248
1249 for (auto iter = acp.find_by_dst(inst->src[i].nr);
1250 iter != acp.end() && (*iter)->dst.nr == inst->src[i].nr;
1251 ++iter) {
1252 if ((*iter)->src.file == IMM) {
1253 if (try_constant_propagate(compiler, inst, *iter, i)) {
1254 instruction_progress = true;
1255 break;
1256 }
1257 } else {
1258 if (try_copy_propagate(compiler, inst, *iter, i, alloc,
1259 max_polygons)) {
1260 instruction_progress = true;
1261 break;
1262 }
1263 }
1264 }
1265 }
1266
1267 if (instruction_progress) {
1268 progress = true;
1269
1270 /* ADD3 can only have the immediate as src0. */
1271 if (inst->opcode == BRW_OPCODE_ADD3) {
1272 if (inst->src[2].file == IMM) {
1273 const auto src0 = inst->src[0];
1274 inst->src[0] = inst->src[2];
1275 inst->src[2] = src0;
1276 }
1277 }
1278
1279 /* If only one of the sources of a 2-source, commutative instruction (e.g.,
1280 * AND) is immediate, it must be src1. If both are immediate, opt_algebraic
1281 * should fold it away.
1282 */
1283 if (inst->sources == 2 && inst->is_commutative() &&
1284 inst->src[0].file == IMM && inst->src[1].file != IMM) {
1285 const auto src1 = inst->src[1];
1286 inst->src[1] = inst->src[0];
1287 inst->src[0] = src1;
1288 }
1289 }
1290
1291 /* kill the destination from the ACP */
1292 if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {
1293 for (auto iter = acp.find_by_dst(inst->dst.nr);
1294 iter != acp.end() && (*iter)->dst.nr == inst->dst.nr;
1295 ++iter) {
1296 if (grf_regions_overlap((*iter)->dst, (*iter)->size_written,
1297 inst->dst, inst->size_written))
1298 acp.remove(*iter);
1299 }
1300
1301 for (auto iter = acp.find_by_src(inst->dst.nr);
1302 iter != acp.end() && (*iter)->src.nr == inst->dst.nr;
1303 ++iter) {
1304 /* Make sure we kill the entry if this instruction overwrites
1305 * _any_ of the registers that it reads
1306 */
1307 if (grf_regions_overlap((*iter)->src, (*iter)->size_read,
1308 inst->dst, inst->size_written))
1309 acp.remove(*iter);
1310 }
1311 }
1312
1313 /* If this instruction's source could potentially be folded into the
1314 * operand of another instruction, add it to the ACP.
1315 */
1316 if (can_propagate_from(inst)) {
1317 acp_entry *entry = linear_zalloc(lin_ctx, acp_entry);
1318 entry->dst = inst->dst;
1319 entry->src = inst->src[0];
1320 entry->size_written = inst->size_written;
1321 for (unsigned i = 0; i < inst->sources; i++)
1322 entry->size_read += inst->size_read(i);
1323 entry->opcode = inst->opcode;
1324 entry->is_partial_write = inst->is_partial_write();
1325 entry->force_writemask_all = inst->force_writemask_all;
1326 acp.add(entry);
1327 } else if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&
1328 inst->dst.file == VGRF) {
1329 int offset = 0;
1330 for (int i = 0; i < inst->sources; i++) {
1331 int effective_width = i < inst->header_size ? 8 : inst->exec_size;
1332 const unsigned size_written = effective_width *
1333 type_sz(inst->src[i].type);
1334 if (inst->src[i].file == VGRF ||
1335 (inst->src[i].file == FIXED_GRF &&
1336 inst->src[i].is_contiguous())) {
1337 const brw_reg_type t = i < inst->header_size ?
1338 BRW_REGISTER_TYPE_UD : inst->src[i].type;
1339 fs_reg dst = byte_offset(retype(inst->dst, t), offset);
1340 if (!dst.equals(inst->src[i])) {
1341 acp_entry *entry = linear_zalloc(lin_ctx, acp_entry);
1342 entry->dst = dst;
1343 entry->src = retype(inst->src[i], t);
1344 entry->size_written = size_written;
1345 entry->size_read = inst->size_read(i);
1346 entry->opcode = inst->opcode;
1347 entry->force_writemask_all = inst->force_writemask_all;
1348 acp.add(entry);
1349 }
1350 }
1351 offset += size_written;
1352 }
1353 }
1354 }
1355
1356 return progress;
1357 }
1358
1359 bool
brw_fs_opt_copy_propagation(fs_visitor & s)1360 brw_fs_opt_copy_propagation(fs_visitor &s)
1361 {
1362 bool progress = false;
1363 void *copy_prop_ctx = ralloc_context(NULL);
1364 linear_ctx *lin_ctx = linear_context(copy_prop_ctx);
1365 struct acp out_acp[s.cfg->num_blocks];
1366
1367 const fs_live_variables &live = s.live_analysis.require();
1368
1369 /* First, walk through each block doing local copy propagation and getting
1370 * the set of copies available at the end of the block.
1371 */
1372 foreach_block (block, s.cfg) {
1373 progress = opt_copy_propagation_local(s.compiler, lin_ctx, block,
1374 out_acp[block->num], s.alloc,
1375 s.max_polygons) || progress;
1376
1377 /* If the destination of an ACP entry exists only within this block,
1378 * then there's no need to keep it for dataflow analysis. We can delete
1379 * it from the out_acp table and avoid growing the bitsets any bigger
1380 * than we absolutely have to.
1381 *
1382 * Because nothing in opt_copy_propagation_local touches the block
1383 * start/end IPs and opt_copy_propagation_local is incapable of
1384 * extending the live range of an ACP destination beyond the block,
1385 * it's safe to use the liveness information in this way.
1386 */
1387 for (auto iter = out_acp[block->num].begin();
1388 iter != out_acp[block->num].end(); ++iter) {
1389 assert((*iter)->dst.file == VGRF);
1390 if (block->start_ip <= live.vgrf_start[(*iter)->dst.nr] &&
1391 live.vgrf_end[(*iter)->dst.nr] <= block->end_ip) {
1392 out_acp[block->num].remove(*iter);
1393 }
1394 }
1395 }
1396
1397 /* Do dataflow analysis for those available copies. */
1398 fs_copy_prop_dataflow dataflow(lin_ctx, s.cfg, live, out_acp);
1399
1400 /* Next, re-run local copy propagation, this time with the set of copies
1401 * provided by the dataflow analysis available at the start of a block.
1402 */
1403 foreach_block (block, s.cfg) {
1404 struct acp in_acp;
1405
1406 for (int i = 0; i < dataflow.num_acp; i++) {
1407 if (BITSET_TEST(dataflow.bd[block->num].livein, i) &&
1408 !BITSET_TEST(dataflow.bd[block->num].exec_mismatch, i)) {
1409 struct acp_entry *entry = dataflow.acp[i];
1410 in_acp.add(entry);
1411 }
1412 }
1413
1414 progress = opt_copy_propagation_local(s.compiler, lin_ctx, block,
1415 in_acp, s.alloc, s.max_polygons) ||
1416 progress;
1417 }
1418
1419 ralloc_free(copy_prop_ctx);
1420
1421 if (progress)
1422 s.invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW |
1423 DEPENDENCY_INSTRUCTION_DETAIL);
1424
1425 return progress;
1426 }
1427