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 16
37 #include "util/bitset.h"
39 #include "brw_fs_live_variables.h"
45 namespace { /* avoid conflict with opt_copy_propagation_elements */
46 struct acp_entry
: public exec_node
{
57 * Which entries in the fs_copy_prop_dataflow acp table are live at the
58 * start of this block. This is the useful output of the analysis, since
59 * it lets us plug those into the local copy propagation on the second
65 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
66 * of this block. This is done in initial setup from the per-block acps
67 * returned by the first local copy prop pass.
72 * Which entries in the fs_copy_prop_dataflow acp table are generated by
73 * instructions in this block which reach the end of the block without
79 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
80 * course of this block.
85 * Which entries in the fs_copy_prop_dataflow acp table are guaranteed to
86 * have a fully uninitialized destination at the end of this block.
91 class fs_copy_prop_dataflow
94 fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
95 const fs_live_variables
*live
,
96 exec_list
*out_acp
[ACP_HASH_SIZE
]);
98 void setup_initial_values();
101 void dump_block_data() const UNUSED
;
105 const fs_live_variables
*live
;
111 struct block_data
*bd
;
113 } /* anonymous namespace */
115 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
116 const fs_live_variables
*live
,
117 exec_list
*out_acp
[ACP_HASH_SIZE
])
118 : mem_ctx(mem_ctx
), cfg(cfg
), live(live
)
120 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
123 foreach_block (block
, cfg
) {
124 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
125 num_acp
+= out_acp
[block
->num
][i
].length();
129 acp
= rzalloc_array(mem_ctx
, struct acp_entry
*, num_acp
);
131 bitset_words
= BITSET_WORDS(num_acp
);
134 foreach_block (block
, cfg
) {
135 bd
[block
->num
].livein
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
136 bd
[block
->num
].liveout
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
137 bd
[block
->num
].copy
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
138 bd
[block
->num
].kill
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
139 bd
[block
->num
].undef
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
141 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
142 foreach_in_list(acp_entry
, entry
, &out_acp
[block
->num
][i
]) {
143 acp
[next_acp
] = entry
;
145 /* opt_copy_propagation_local populates out_acp with copies created
146 * in a block which are still live at the end of the block. This
147 * is exactly what we want in the COPY set.
149 BITSET_SET(bd
[block
->num
].copy
, next_acp
);
156 assert(next_acp
== num_acp
);
158 setup_initial_values();
163 * Set up initial values for each of the data flow sets, prior to running
164 * the fixed-point algorithm.
167 fs_copy_prop_dataflow::setup_initial_values()
169 /* Initialize the COPY and KILL sets. */
170 foreach_block (block
, cfg
) {
171 foreach_inst_in_block(fs_inst
, inst
, block
) {
172 if (inst
->dst
.file
!= VGRF
)
175 /* Mark ACP entries which are killed by this instruction. */
176 for (int i
= 0; i
< num_acp
; i
++) {
177 if (regions_overlap(inst
->dst
, inst
->size_written
,
178 acp
[i
]->dst
, acp
[i
]->size_written
) ||
179 regions_overlap(inst
->dst
, inst
->size_written
,
180 acp
[i
]->src
, acp
[i
]->size_read
)) {
181 BITSET_SET(bd
[block
->num
].kill
, i
);
187 /* Populate the initial values for the livein and liveout sets. For the
188 * block at the start of the program, livein = 0 and liveout = copy.
189 * For the others, set liveout to 0 (the empty set) and livein to ~0
190 * (the universal set).
192 foreach_block (block
, cfg
) {
193 if (block
->parents
.is_empty()) {
194 for (int i
= 0; i
< bitset_words
; i
++) {
195 bd
[block
->num
].livein
[i
] = 0u;
196 bd
[block
->num
].liveout
[i
] = bd
[block
->num
].copy
[i
];
199 for (int i
= 0; i
< bitset_words
; i
++) {
200 bd
[block
->num
].liveout
[i
] = 0u;
201 bd
[block
->num
].livein
[i
] = ~0u;
206 /* Initialize the undef set. */
207 foreach_block (block
, cfg
) {
208 for (int i
= 0; i
< num_acp
; i
++) {
209 BITSET_SET(bd
[block
->num
].undef
, i
);
210 for (unsigned off
= 0; off
< acp
[i
]->size_written
; off
+= REG_SIZE
) {
211 if (BITSET_TEST(live
->block_data
[block
->num
].defout
,
212 live
->var_from_reg(byte_offset(acp
[i
]->dst
, off
))))
213 BITSET_CLEAR(bd
[block
->num
].undef
, i
);
220 * Walk the set of instructions in the block, marking which entries in the acp
221 * are killed by the block.
224 fs_copy_prop_dataflow::run()
231 /* Update liveout for all blocks. */
232 foreach_block (block
, cfg
) {
233 if (block
->parents
.is_empty())
236 for (int i
= 0; i
< bitset_words
; i
++) {
237 const BITSET_WORD old_liveout
= bd
[block
->num
].liveout
[i
];
239 bd
[block
->num
].liveout
[i
] =
240 bd
[block
->num
].copy
[i
] | (bd
[block
->num
].livein
[i
] &
241 ~bd
[block
->num
].kill
[i
]);
243 if (old_liveout
!= bd
[block
->num
].liveout
[i
])
248 /* Update livein for all blocks. If a copy is live out of all parent
249 * blocks, it's live coming in to this block.
251 foreach_block (block
, cfg
) {
252 if (block
->parents
.is_empty())
255 for (int i
= 0; i
< bitset_words
; i
++) {
256 const BITSET_WORD old_livein
= bd
[block
->num
].livein
[i
];
257 BITSET_WORD livein_from_any_block
= 0;
259 bd
[block
->num
].livein
[i
] = ~0u;
260 foreach_list_typed(bblock_link
, parent_link
, link
, &block
->parents
) {
261 bblock_t
*parent
= parent_link
->block
;
262 /* Consider ACP entries with a known-undefined destination to
263 * be available from the parent. This is valid because we're
264 * free to set the undefined variable equal to the source of
265 * the ACP entry without breaking the application's
266 * expectations, since the variable is undefined.
268 bd
[block
->num
].livein
[i
] &= (bd
[parent
->num
].liveout
[i
] |
269 bd
[parent
->num
].undef
[i
]);
270 livein_from_any_block
|= bd
[parent
->num
].liveout
[i
];
273 /* Limit to the set of ACP entries that can possibly be available
274 * at the start of the block, since propagating from a variable
275 * which is guaranteed to be undefined (rather than potentially
276 * undefined for some dynamic control-flow paths) doesn't seem
277 * particularly useful.
279 bd
[block
->num
].livein
[i
] &= livein_from_any_block
;
281 if (old_livein
!= bd
[block
->num
].livein
[i
])
289 fs_copy_prop_dataflow::dump_block_data() const
291 foreach_block (block
, cfg
) {
292 fprintf(stderr
, "Block %d [%d, %d] (parents ", block
->num
,
293 block
->start_ip
, block
->end_ip
);
294 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
295 bblock_t
*parent
= link
->block
;
296 fprintf(stderr
, "%d ", parent
->num
);
298 fprintf(stderr
, "):\n");
299 fprintf(stderr
, " livein = 0x");
300 for (int i
= 0; i
< bitset_words
; i
++)
301 fprintf(stderr
, "%08x", bd
[block
->num
].livein
[i
]);
302 fprintf(stderr
, ", liveout = 0x");
303 for (int i
= 0; i
< bitset_words
; i
++)
304 fprintf(stderr
, "%08x", bd
[block
->num
].liveout
[i
]);
305 fprintf(stderr
, ",\n copy = 0x");
306 for (int i
= 0; i
< bitset_words
; i
++)
307 fprintf(stderr
, "%08x", bd
[block
->num
].copy
[i
]);
308 fprintf(stderr
, ", kill = 0x");
309 for (int i
= 0; i
< bitset_words
; i
++)
310 fprintf(stderr
, "%08x", bd
[block
->num
].kill
[i
]);
311 fprintf(stderr
, "\n");
316 is_logic_op(enum opcode opcode
)
318 return (opcode
== BRW_OPCODE_AND
||
319 opcode
== BRW_OPCODE_OR
||
320 opcode
== BRW_OPCODE_XOR
||
321 opcode
== BRW_OPCODE_NOT
);
325 can_take_stride(fs_inst
*inst
, unsigned arg
, unsigned stride
,
326 const gen_device_info
*devinfo
)
331 /* 3-source instructions can only be Align16, which restricts what strides
332 * they can take. They can only take a stride of 1 (the usual case), or 0
333 * with a special "repctrl" bit. But the repctrl bit doesn't work for
334 * 64-bit datatypes, so if the source type is 64-bit then only a stride of
335 * 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page
338 * This is applicable to 32b datatypes and 16b datatype. 64b datatypes
339 * cannot use the replicate control.
341 if (inst
->is_3src(devinfo
)) {
342 if (type_sz(inst
->src
[arg
].type
) > 4)
345 return stride
== 1 || stride
== 0;
348 /* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
349 * page 391 ("Extended Math Function"):
351 * The following restrictions apply for align1 mode: Scalar source is
352 * supported. Source and destination horizontal stride must be the
355 * From the Haswell PRM Volume 2b "Command Reference - Instructions", page
356 * 134 ("Extended Math Function"):
358 * Scalar source is supported. Source and destination horizontal stride
361 * and similar language exists for IVB and SNB. Pre-SNB, math instructions
362 * are sends, so the sources are moved to MRF's and there are no
365 if (inst
->is_math()) {
366 if (devinfo
->gen
== 6 || devinfo
->gen
== 7) {
367 assert(inst
->dst
.stride
== 1);
368 return stride
== 1 || stride
== 0;
369 } else if (devinfo
->gen
>= 8) {
370 return stride
== inst
->dst
.stride
|| stride
== 0;
378 fs_visitor::try_copy_propagate(fs_inst
*inst
, int arg
, acp_entry
*entry
)
380 if (inst
->src
[arg
].file
!= VGRF
)
383 if (entry
->src
.file
== IMM
)
385 assert(entry
->src
.file
== VGRF
|| entry
->src
.file
== UNIFORM
||
386 entry
->src
.file
== ATTR
);
388 if (entry
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
389 inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
)
392 assert(entry
->dst
.file
== VGRF
);
393 if (inst
->src
[arg
].nr
!= entry
->dst
.nr
)
396 /* Bail if inst is reading a range that isn't contained in the range
397 * that entry is writing.
399 if (!region_contained_in(inst
->src
[arg
], inst
->size_read(arg
),
400 entry
->dst
, entry
->size_written
))
403 /* we can't generally copy-propagate UD negations because we
404 * can end up accessing the resulting values as signed integers
405 * instead. See also resolve_ud_negate() and comment in
406 * fs_generator::generate_code.
408 if (entry
->src
.type
== BRW_REGISTER_TYPE_UD
&&
412 bool has_source_modifiers
= entry
->src
.abs
|| entry
->src
.negate
;
414 if ((has_source_modifiers
|| entry
->src
.file
== UNIFORM
||
415 !entry
->src
.is_contiguous()) &&
416 !inst
->can_do_source_mods(devinfo
))
419 if (has_source_modifiers
&&
420 inst
->opcode
== SHADER_OPCODE_GEN4_SCRATCH_WRITE
)
423 /* Bail if the result of composing both strides would exceed the
426 if (!can_take_stride(inst
, arg
, entry
->src
.stride
* inst
->src
[arg
].stride
,
430 /* Bail if the instruction type is larger than the execution type of the
431 * copy, what implies that each channel is reading multiple channels of the
432 * destination of the copy, and simply replacing the sources would give a
433 * program with different semantics.
435 if (type_sz(entry
->dst
.type
) < type_sz(inst
->src
[arg
].type
))
438 /* Bail if the result of composing both strides cannot be expressed
439 * as another stride. This avoids, for example, trying to transform
442 * MOV (8) rX<1>UD rY<0;1,0>UD
443 * FOO (8) ... rX<8;8,1>UW
447 * FOO (8) ... rY<0;1,0>UW
449 * Which would have different semantics.
451 if (entry
->src
.stride
!= 1 &&
452 (inst
->src
[arg
].stride
*
453 type_sz(inst
->src
[arg
].type
)) % type_sz(entry
->src
.type
) != 0)
456 /* Since semantics of source modifiers are type-dependent we need to
457 * ensure that the meaning of the instruction remains the same if we
458 * change the type. If the sizes of the types are different the new
459 * instruction will read a different amount of data than the original
460 * and the semantics will always be different.
462 if (has_source_modifiers
&&
463 entry
->dst
.type
!= inst
->src
[arg
].type
&&
464 (!inst
->can_change_types() ||
465 type_sz(entry
->dst
.type
) != type_sz(inst
->src
[arg
].type
)))
468 if (devinfo
->gen
>= 8 && (entry
->src
.negate
|| entry
->src
.abs
) &&
469 is_logic_op(inst
->opcode
)) {
473 if (entry
->saturate
) {
474 switch(inst
->opcode
) {
476 if ((inst
->conditional_mod
!= BRW_CONDITIONAL_GE
&&
477 inst
->conditional_mod
!= BRW_CONDITIONAL_L
) ||
478 inst
->src
[1].file
!= IMM
||
479 inst
->src
[1].f
< 0.0 ||
480 inst
->src
[1].f
> 1.0) {
489 inst
->src
[arg
].file
= entry
->src
.file
;
490 inst
->src
[arg
].nr
= entry
->src
.nr
;
491 inst
->src
[arg
].stride
*= entry
->src
.stride
;
492 inst
->saturate
= inst
->saturate
|| entry
->saturate
;
494 /* Compute the offset of inst->src[arg] relative to entry->dst */
495 const unsigned rel_offset
= inst
->src
[arg
].offset
- entry
->dst
.offset
;
497 /* Compute the first component of the copy that the instruction is
498 * reading, and the base byte offset within that component.
500 assert(entry
->dst
.offset
% REG_SIZE
== 0 && entry
->dst
.stride
== 1);
501 const unsigned component
= rel_offset
/ type_sz(entry
->dst
.type
);
502 const unsigned suboffset
= rel_offset
% type_sz(entry
->dst
.type
);
504 /* Calculate the byte offset at the origin of the copy of the given
505 * component and suboffset.
507 inst
->src
[arg
].offset
= suboffset
+
508 component
* entry
->src
.stride
* type_sz(entry
->src
.type
) +
511 if (has_source_modifiers
) {
512 if (entry
->dst
.type
!= inst
->src
[arg
].type
) {
513 /* We are propagating source modifiers from a MOV with a different
514 * type. If we got here, then we can just change the source and
515 * destination types of the instruction and keep going.
517 assert(inst
->can_change_types());
518 for (int i
= 0; i
< inst
->sources
; i
++) {
519 inst
->src
[i
].type
= entry
->dst
.type
;
521 inst
->dst
.type
= entry
->dst
.type
;
524 if (!inst
->src
[arg
].abs
) {
525 inst
->src
[arg
].abs
= entry
->src
.abs
;
526 inst
->src
[arg
].negate
^= entry
->src
.negate
;
535 fs_visitor::try_constant_propagate(fs_inst
*inst
, acp_entry
*entry
)
537 bool progress
= false;
539 if (entry
->src
.file
!= IMM
)
541 if (type_sz(entry
->src
.type
) > 4)
546 for (int i
= inst
->sources
- 1; i
>= 0; i
--) {
547 if (inst
->src
[i
].file
!= VGRF
)
550 assert(entry
->dst
.file
== VGRF
);
551 if (inst
->src
[i
].nr
!= entry
->dst
.nr
)
554 /* Bail if inst is reading a range that isn't contained in the range
555 * that entry is writing.
557 if (!region_contained_in(inst
->src
[i
], inst
->size_read(i
),
558 entry
->dst
, entry
->size_written
))
561 /* If the type sizes don't match each channel of the instruction is
562 * either extracting a portion of the constant (which could be handled
563 * with some effort but the code below doesn't) or reading multiple
564 * channels of the source at once.
566 if (type_sz(inst
->src
[i
].type
) != type_sz(entry
->dst
.type
))
569 fs_reg val
= entry
->src
;
570 val
.type
= inst
->src
[i
].type
;
572 if (inst
->src
[i
].abs
) {
573 if ((devinfo
->gen
>= 8 && is_logic_op(inst
->opcode
)) ||
574 !brw_abs_immediate(val
.type
, &val
.as_brw_reg())) {
579 if (inst
->src
[i
].negate
) {
580 if ((devinfo
->gen
>= 8 && is_logic_op(inst
->opcode
)) ||
581 !brw_negate_immediate(val
.type
, &val
.as_brw_reg())) {
586 switch (inst
->opcode
) {
588 case SHADER_OPCODE_LOAD_PAYLOAD
:
594 case SHADER_OPCODE_INT_QUOTIENT
:
595 case SHADER_OPCODE_INT_REMAINDER
:
596 /* FINISHME: Promote non-float constants and remove this. */
597 if (devinfo
->gen
< 8)
600 case SHADER_OPCODE_POW
:
601 /* Allow constant propagation into src1 (except on Gen 6 which
602 * doesn't support scalar source math), and let constant combining
603 * promote the constant on Gen < 8.
605 if (devinfo
->gen
== 6)
608 case BRW_OPCODE_BFI1
:
612 case BRW_OPCODE_SUBB
:
619 case BRW_OPCODE_MACH
:
621 case SHADER_OPCODE_MULH
:
626 case BRW_OPCODE_ADDC
:
630 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
631 /* Fit this constant in by commuting the operands.
632 * Exception: we can't do this for 32-bit integer MUL/MACH
633 * because it's asymmetric.
635 * The BSpec says for Broadwell that
637 * "When multiplying DW x DW, the dst cannot be accumulator."
639 * Integer MUL with a non-accumulator destination will be lowered
640 * by lower_integer_multiplication(), so don't restrict it.
642 if (((inst
->opcode
== BRW_OPCODE_MUL
&&
643 inst
->dst
.is_accumulator()) ||
644 inst
->opcode
== BRW_OPCODE_MACH
) &&
645 (inst
->src
[1].type
== BRW_REGISTER_TYPE_D
||
646 inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
))
648 inst
->src
[0] = inst
->src
[1];
659 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
660 enum brw_conditional_mod new_cmod
;
662 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
663 if (new_cmod
!= BRW_CONDITIONAL_NONE
) {
664 /* Fit this constant in by swapping the operands and
667 inst
->src
[0] = inst
->src
[1];
669 inst
->conditional_mod
= new_cmod
;
679 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
680 inst
->src
[0] = inst
->src
[1];
683 /* If this was predicated, flipping operands means
684 * we also need to flip the predicate.
686 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
687 inst
->predicate_inverse
=
688 !inst
->predicate_inverse
;
694 case SHADER_OPCODE_UNTYPED_ATOMIC
:
695 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
696 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
697 case SHADER_OPCODE_TYPED_ATOMIC
:
698 case SHADER_OPCODE_TYPED_SURFACE_READ
:
699 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
700 case SHADER_OPCODE_BYTE_SCATTERED_WRITE
:
701 case SHADER_OPCODE_BYTE_SCATTERED_READ
:
702 /* We only propagate into the surface argument of the
703 * instruction. Everything else goes through LOAD_PAYLOAD.
711 case FS_OPCODE_FB_WRITE_LOGICAL
:
712 /* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are
713 * bit-cast using a strided region so they cannot be immediates.
715 if (i
!= FB_WRITE_LOGICAL_SRC_SRC_STENCIL
&&
716 i
!= FB_WRITE_LOGICAL_SRC_OMASK
) {
722 case SHADER_OPCODE_TEX_LOGICAL
:
723 case SHADER_OPCODE_TXD_LOGICAL
:
724 case SHADER_OPCODE_TXF_LOGICAL
:
725 case SHADER_OPCODE_TXL_LOGICAL
:
726 case SHADER_OPCODE_TXS_LOGICAL
:
727 case FS_OPCODE_TXB_LOGICAL
:
728 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
729 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
730 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
731 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
732 case SHADER_OPCODE_LOD_LOGICAL
:
733 case SHADER_OPCODE_TG4_LOGICAL
:
734 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
735 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
736 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
737 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
738 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
739 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
740 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
741 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
742 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
747 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
748 case SHADER_OPCODE_BROADCAST
:
768 can_propagate_from(fs_inst
*inst
)
770 return (inst
->opcode
== BRW_OPCODE_MOV
&&
771 inst
->dst
.file
== VGRF
&&
772 ((inst
->src
[0].file
== VGRF
&&
773 !regions_overlap(inst
->dst
, inst
->size_written
,
774 inst
->src
[0], inst
->size_read(0))) ||
775 inst
->src
[0].file
== ATTR
||
776 inst
->src
[0].file
== UNIFORM
||
777 inst
->src
[0].file
== IMM
) &&
778 inst
->src
[0].type
== inst
->dst
.type
&&
779 !inst
->is_partial_write());
782 /* Walks a basic block and does copy propagation on it using the acp
786 fs_visitor::opt_copy_propagation_local(void *copy_prop_ctx
, bblock_t
*block
,
789 bool progress
= false;
791 foreach_inst_in_block(fs_inst
, inst
, block
) {
792 /* Try propagating into this instruction. */
793 for (int i
= 0; i
< inst
->sources
; i
++) {
794 if (inst
->src
[i
].file
!= VGRF
)
797 foreach_in_list(acp_entry
, entry
, &acp
[inst
->src
[i
].nr
% ACP_HASH_SIZE
]) {
798 if (try_constant_propagate(inst
, entry
))
800 else if (try_copy_propagate(inst
, i
, entry
))
805 /* kill the destination from the ACP */
806 if (inst
->dst
.file
== VGRF
) {
807 foreach_in_list_safe(acp_entry
, entry
, &acp
[inst
->dst
.nr
% ACP_HASH_SIZE
]) {
808 if (regions_overlap(entry
->dst
, entry
->size_written
,
809 inst
->dst
, inst
->size_written
))
813 /* Oops, we only have the chaining hash based on the destination, not
814 * the source, so walk across the entire table.
816 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
817 foreach_in_list_safe(acp_entry
, entry
, &acp
[i
]) {
818 /* Make sure we kill the entry if this instruction overwrites
819 * _any_ of the registers that it reads
821 if (regions_overlap(entry
->src
, entry
->size_read
,
822 inst
->dst
, inst
->size_written
))
828 /* If this instruction's source could potentially be folded into the
829 * operand of another instruction, add it to the ACP.
831 if (can_propagate_from(inst
)) {
832 acp_entry
*entry
= ralloc(copy_prop_ctx
, acp_entry
);
833 entry
->dst
= inst
->dst
;
834 entry
->src
= inst
->src
[0];
835 entry
->size_written
= inst
->size_written
;
836 entry
->size_read
= inst
->size_read(0);
837 entry
->opcode
= inst
->opcode
;
838 entry
->saturate
= inst
->saturate
;
839 acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
840 } else if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
841 inst
->dst
.file
== VGRF
) {
843 for (int i
= 0; i
< inst
->sources
; i
++) {
844 int effective_width
= i
< inst
->header_size
? 8 : inst
->exec_size
;
845 assert(effective_width
* type_sz(inst
->src
[i
].type
) % REG_SIZE
== 0);
846 const unsigned size_written
= effective_width
*
847 type_sz(inst
->src
[i
].type
);
848 if (inst
->src
[i
].file
== VGRF
) {
849 acp_entry
*entry
= rzalloc(copy_prop_ctx
, acp_entry
);
850 entry
->dst
= byte_offset(inst
->dst
, offset
);
851 entry
->src
= inst
->src
[i
];
852 entry
->size_written
= size_written
;
853 entry
->size_read
= inst
->size_read(i
);
854 entry
->opcode
= inst
->opcode
;
855 if (!entry
->dst
.equals(inst
->src
[i
])) {
856 acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
861 offset
+= size_written
;
870 fs_visitor::opt_copy_propagation()
872 bool progress
= false;
873 void *copy_prop_ctx
= ralloc_context(NULL
);
874 exec_list
*out_acp
[cfg
->num_blocks
];
876 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
877 out_acp
[i
] = new exec_list
[ACP_HASH_SIZE
];
879 calculate_live_intervals();
881 /* First, walk through each block doing local copy propagation and getting
882 * the set of copies available at the end of the block.
884 foreach_block (block
, cfg
) {
885 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
,
886 out_acp
[block
->num
]) || progress
;
889 /* Do dataflow analysis for those available copies. */
890 fs_copy_prop_dataflow
dataflow(copy_prop_ctx
, cfg
, live_intervals
, out_acp
);
892 /* Next, re-run local copy propagation, this time with the set of copies
893 * provided by the dataflow analysis available at the start of a block.
895 foreach_block (block
, cfg
) {
896 exec_list in_acp
[ACP_HASH_SIZE
];
898 for (int i
= 0; i
< dataflow
.num_acp
; i
++) {
899 if (BITSET_TEST(dataflow
.bd
[block
->num
].livein
, i
)) {
900 struct acp_entry
*entry
= dataflow
.acp
[i
];
901 in_acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
905 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
, in_acp
) ||
909 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
910 delete [] out_acp
[i
];
911 ralloc_free(copy_prop_ctx
);
914 invalidate_live_intervals();