2 * Copyright © 2012 Intel Corporation
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:
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
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
24 /** @file brw_fs_copy_propagation.cpp
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.
31 * See Muchnick's Advanced Compiler Design and Implementation, section
35 #define ACP_HASH_SIZE 64
37 #include "util/bitset.h"
38 #include "util/u_math.h"
40 #include "brw_fs_live_variables.h"
46 namespace { /* avoid conflict with opt_copy_propagation_elements */
47 struct acp_entry
: public exec_node
{
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
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.
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
81 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
82 * course of this block.
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.
93 class fs_copy_prop_dataflow
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
]);
100 void setup_initial_values();
103 void dump_block_data() const UNUSED
;
107 const fs_live_variables
*live
;
113 struct block_data
*bd
;
115 } /* anonymous namespace */
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
)
122 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
125 foreach_block (block
, cfg
) {
126 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
127 num_acp
+= out_acp
[block
->num
][i
].length();
131 acp
= rzalloc_array(mem_ctx
, struct acp_entry
*, num_acp
);
133 bitset_words
= BITSET_WORDS(num_acp
);
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
);
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
;
147 entry
->global_idx
= next_acp
;
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.
153 BITSET_SET(bd
[block
->num
].copy
, next_acp
);
160 assert(next_acp
== num_acp
);
162 setup_initial_values();
167 * Set up initial values for each of the data flow sets, prior to running
168 * the fixed-point algorithm.
171 fs_copy_prop_dataflow::setup_initial_values()
173 /* Initialize the COPY and KILL sets. */
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.
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.
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
];
193 /* First, get all the KILLs for instructions which overwrite ACP
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
]);
201 foreach_block (block
, cfg
) {
202 foreach_inst_in_block(fs_inst
, inst
, block
) {
203 if (inst
->dst
.file
!= VGRF
)
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
);
215 /* Clear the table for the second pass */
216 for (unsigned i
= 0; i
< acp_table_size
; i
++)
217 acp_table
[i
].make_empty();
219 /* Next, get all the KILLs for instructions which overwrite ACP
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
]);
227 foreach_block (block
, cfg
) {
228 foreach_inst_in_block(fs_inst
, inst
, block
) {
229 if (inst
->dst
.file
!= VGRF
)
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
);
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).
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
];
255 for (int i
= 0; i
< bitset_words
; i
++) {
256 bd
[block
->num
].liveout
[i
] = ~0u;
257 bd
[block
->num
].livein
[i
] = ~0u;
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
);
276 * Walk the set of instructions in the block, marking which entries in the acp
277 * are killed by the block.
280 fs_copy_prop_dataflow::run()
287 foreach_block (block
, cfg
) {
288 if (block
->parents
.is_empty())
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;
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.
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.
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
];
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.
318 bd
[block
->num
].livein
[i
] &= livein_from_any_block
;
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
]);
325 if (old_liveout
!= bd
[block
->num
].liveout
[i
])
333 fs_copy_prop_dataflow::dump_block_data() const
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
);
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");
360 is_logic_op(enum opcode opcode
)
362 return (opcode
== BRW_OPCODE_AND
||
363 opcode
== BRW_OPCODE_OR
||
364 opcode
== BRW_OPCODE_XOR
||
365 opcode
== BRW_OPCODE_NOT
);
369 can_take_stride(fs_inst
*inst
, unsigned arg
, unsigned stride
,
370 const gen_device_info
*devinfo
)
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.
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
||
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
392 * This is applicable to 32b datatypes and 16b datatype. 64b datatypes
393 * cannot use the replicate control.
395 if (inst
->is_3src(devinfo
)) {
396 if (type_sz(inst
->src
[arg
].type
) > 4)
399 return stride
== 1 || stride
== 0;
402 /* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
403 * page 391 ("Extended Math Function"):
405 * The following restrictions apply for align1 mode: Scalar source is
406 * supported. Source and destination horizontal stride must be the
409 * From the Haswell PRM Volume 2b "Command Reference - Instructions", page
410 * 134 ("Extended Math Function"):
412 * Scalar source is supported. Source and destination horizontal stride
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
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;
432 instruction_requires_packed_data(fs_inst
*inst
)
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
:
446 fs_visitor::try_copy_propagate(fs_inst
*inst
, int arg
, acp_entry
*entry
)
448 if (inst
->src
[arg
].file
!= VGRF
)
451 if (entry
->src
.file
== IMM
)
453 assert(entry
->src
.file
== VGRF
|| entry
->src
.file
== UNIFORM
||
454 entry
->src
.file
== ATTR
);
456 if (entry
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
457 inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
)
460 assert(entry
->dst
.file
== VGRF
);
461 if (inst
->src
[arg
].nr
!= entry
->dst
.nr
)
464 /* Bail if inst is reading a range that isn't contained in the range
465 * that entry is writing.
467 if (!region_contained_in(inst
->src
[arg
], inst
->size_read(arg
),
468 entry
->dst
, entry
->size_written
))
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.
476 if (entry
->src
.type
== BRW_REGISTER_TYPE_UD
&&
480 bool has_source_modifiers
= entry
->src
.abs
|| entry
->src
.negate
;
482 if ((has_source_modifiers
|| entry
->src
.file
== UNIFORM
||
483 !entry
->src
.is_contiguous()) &&
484 !inst
->can_do_source_mods(devinfo
))
487 if (has_source_modifiers
&&
488 inst
->opcode
== SHADER_OPCODE_GEN4_SCRATCH_WRITE
)
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.
495 if (instruction_requires_packed_data(inst
) && entry
->src
.stride
> 1)
498 /* Bail if the result of composing both strides would exceed the
501 if (!can_take_stride(inst
, arg
, entry
->src
.stride
* inst
->src
[arg
].stride
,
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.
510 if (type_sz(entry
->dst
.type
) < type_sz(inst
->src
[arg
].type
))
513 /* Bail if the result of composing both strides cannot be expressed
514 * as another stride. This avoids, for example, trying to transform
517 * MOV (8) rX<1>UD rY<0;1,0>UD
518 * FOO (8) ... rX<8;8,1>UW
522 * FOO (8) ... rY<0;1,0>UW
524 * Which would have different semantics.
526 if (entry
->src
.stride
!= 1 &&
527 (inst
->src
[arg
].stride
*
528 type_sz(inst
->src
[arg
].type
)) % type_sz(entry
->src
.type
) != 0)
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.
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
)))
543 if (devinfo
->gen
>= 8 && (entry
->src
.negate
|| entry
->src
.abs
) &&
544 is_logic_op(inst
->opcode
)) {
548 if (entry
->saturate
) {
549 switch(inst
->opcode
) {
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) {
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
;
569 /* Compute the offset of inst->src[arg] relative to entry->dst */
570 const unsigned rel_offset
= inst
->src
[arg
].offset
- entry
->dst
.offset
;
572 /* Compute the first component of the copy that the instruction is
573 * reading, and the base byte offset within that component.
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
);
579 /* Calculate the byte offset at the origin of the copy of the given
580 * component and suboffset.
582 inst
->src
[arg
].offset
= suboffset
+
583 component
* entry
->src
.stride
* type_sz(entry
->src
.type
) +
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.
592 assert(inst
->can_change_types());
593 for (int i
= 0; i
< inst
->sources
; i
++) {
594 inst
->src
[i
].type
= entry
->dst
.type
;
596 inst
->dst
.type
= entry
->dst
.type
;
599 if (!inst
->src
[arg
].abs
) {
600 inst
->src
[arg
].abs
= entry
->src
.abs
;
601 inst
->src
[arg
].negate
^= entry
->src
.negate
;
610 fs_visitor::try_constant_propagate(fs_inst
*inst
, acp_entry
*entry
)
612 bool progress
= false;
614 if (entry
->src
.file
!= IMM
)
616 if (type_sz(entry
->src
.type
) > 4)
621 for (int i
= inst
->sources
- 1; i
>= 0; i
--) {
622 if (inst
->src
[i
].file
!= VGRF
)
625 assert(entry
->dst
.file
== VGRF
);
626 if (inst
->src
[i
].nr
!= entry
->dst
.nr
)
629 /* Bail if inst is reading a range that isn't contained in the range
630 * that entry is writing.
632 if (!region_contained_in(inst
->src
[i
], inst
->size_read(i
),
633 entry
->dst
, entry
->size_written
))
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.
641 if (type_sz(inst
->src
[i
].type
) != type_sz(entry
->dst
.type
))
644 fs_reg val
= entry
->src
;
645 val
.type
= inst
->src
[i
].type
;
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())) {
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())) {
661 switch (inst
->opcode
) {
663 case SHADER_OPCODE_LOAD_PAYLOAD
:
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)
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.
680 if (devinfo
->gen
== 6)
683 case BRW_OPCODE_BFI1
:
687 case BRW_OPCODE_SUBB
:
694 case BRW_OPCODE_MACH
:
696 case SHADER_OPCODE_MULH
:
701 case BRW_OPCODE_ADDC
:
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.
710 * The BSpec says for Broadwell that
712 * "When multiplying DW x DW, the dst cannot be accumulator."
714 * Integer MUL with a non-accumulator destination will be lowered
715 * by lower_integer_multiplication(), so don't restrict it.
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
))
723 inst
->src
[0] = inst
->src
[1];
734 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
735 enum brw_conditional_mod new_cmod
;
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
742 inst
->src
[0] = inst
->src
[1];
744 inst
->conditional_mod
= new_cmod
;
754 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
755 inst
->src
[0] = inst
->src
[1];
758 /* If this was predicated, flipping operands means
759 * we also need to flip the predicate.
761 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
762 inst
->predicate_inverse
=
763 !inst
->predicate_inverse
;
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.
773 if (i
!= FB_WRITE_LOGICAL_SRC_SRC_STENCIL
&&
774 i
!= FB_WRITE_LOGICAL_SRC_OMASK
) {
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
:
806 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
807 case SHADER_OPCODE_BROADCAST
:
827 can_propagate_from(fs_inst
*inst
)
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());
841 /* Walks a basic block and does copy propagation on it using the acp
845 fs_visitor::opt_copy_propagation_local(void *copy_prop_ctx
, bblock_t
*block
,
848 bool progress
= false;
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
)
856 foreach_in_list(acp_entry
, entry
, &acp
[inst
->src
[i
].nr
% ACP_HASH_SIZE
]) {
857 if (try_constant_propagate(inst
, entry
))
859 else if (try_copy_propagate(inst
, i
, entry
))
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
))
872 /* Oops, we only have the chaining hash based on the destination, not
873 * the source, so walk across the entire table.
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
880 if (regions_overlap(entry
->src
, entry
->size_read
,
881 inst
->dst
, inst
->size_written
))
887 /* If this instruction's source could potentially be folded into the
888 * operand of another instruction, add it to the ACP.
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
) {
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
);
920 offset
+= size_written
;
929 fs_visitor::opt_copy_propagation()
931 bool progress
= false;
932 void *copy_prop_ctx
= ralloc_context(NULL
);
933 exec_list
*out_acp
[cfg
->num_blocks
];
935 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
936 out_acp
[i
] = new exec_list
[ACP_HASH_SIZE
];
938 calculate_live_intervals();
940 /* First, walk through each block doing local copy propagation and getting
941 * the set of copies available at the end of the block.
943 foreach_block (block
, cfg
) {
944 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
,
945 out_acp
[block
->num
]) || progress
;
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.
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.
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
)
967 /* Do dataflow analysis for those available copies. */
968 fs_copy_prop_dataflow
dataflow(copy_prop_ctx
, cfg
, live_intervals
, out_acp
);
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.
973 foreach_block (block
, cfg
) {
974 exec_list in_acp
[ACP_HASH_SIZE
];
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
);
983 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
, in_acp
) ||
987 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
988 delete [] out_acp
[i
];
989 ralloc_free(copy_prop_ctx
);
992 invalidate_live_intervals();