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"
42 namespace { /* avoid conflict with opt_copy_propagation_elements */
43 struct acp_entry
: public exec_node
{
54 * Which entries in the fs_copy_prop_dataflow acp table are live at the
55 * start of this block. This is the useful output of the analysis, since
56 * it lets us plug those into the local copy propagation on the second
62 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
63 * of this block. This is done in initial setup from the per-block acps
64 * returned by the first local copy prop pass.
69 * Which entries in the fs_copy_prop_dataflow acp table are generated by
70 * instructions in this block which reach the end of the block without
76 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
77 * course of this block.
82 class fs_copy_prop_dataflow
85 fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
86 exec_list
*out_acp
[ACP_HASH_SIZE
]);
88 void setup_initial_values();
91 void dump_block_data() const UNUSED
;
100 struct block_data
*bd
;
102 } /* anonymous namespace */
104 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
105 exec_list
*out_acp
[ACP_HASH_SIZE
])
106 : mem_ctx(mem_ctx
), cfg(cfg
)
108 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
111 foreach_block (block
, cfg
) {
112 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
113 num_acp
+= out_acp
[block
->num
][i
].length();
117 acp
= rzalloc_array(mem_ctx
, struct acp_entry
*, num_acp
);
119 bitset_words
= BITSET_WORDS(num_acp
);
122 foreach_block (block
, cfg
) {
123 bd
[block
->num
].livein
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
124 bd
[block
->num
].liveout
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
125 bd
[block
->num
].copy
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
126 bd
[block
->num
].kill
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
128 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
129 foreach_in_list(acp_entry
, entry
, &out_acp
[block
->num
][i
]) {
130 acp
[next_acp
] = entry
;
132 /* opt_copy_propagation_local populates out_acp with copies created
133 * in a block which are still live at the end of the block. This
134 * is exactly what we want in the COPY set.
136 BITSET_SET(bd
[block
->num
].copy
, next_acp
);
143 assert(next_acp
== num_acp
);
145 setup_initial_values();
150 * Set up initial values for each of the data flow sets, prior to running
151 * the fixed-point algorithm.
154 fs_copy_prop_dataflow::setup_initial_values()
156 /* Initialize the COPY and KILL sets. */
157 foreach_block (block
, cfg
) {
158 foreach_inst_in_block(fs_inst
, inst
, block
) {
159 if (inst
->dst
.file
!= VGRF
)
162 /* Mark ACP entries which are killed by this instruction. */
163 for (int i
= 0; i
< num_acp
; i
++) {
164 if (regions_overlap(inst
->dst
, inst
->size_written
,
165 acp
[i
]->dst
, acp
[i
]->size_written
) ||
166 regions_overlap(inst
->dst
, inst
->size_written
,
167 acp
[i
]->src
, acp
[i
]->size_read
)) {
168 BITSET_SET(bd
[block
->num
].kill
, i
);
174 /* Populate the initial values for the livein and liveout sets. For the
175 * block at the start of the program, livein = 0 and liveout = copy.
176 * For the others, set liveout to 0 (the empty set) and livein to ~0
177 * (the universal set).
179 foreach_block (block
, cfg
) {
180 if (block
->parents
.is_empty()) {
181 for (int i
= 0; i
< bitset_words
; i
++) {
182 bd
[block
->num
].livein
[i
] = 0u;
183 bd
[block
->num
].liveout
[i
] = bd
[block
->num
].copy
[i
];
186 for (int i
= 0; i
< bitset_words
; i
++) {
187 bd
[block
->num
].liveout
[i
] = 0u;
188 bd
[block
->num
].livein
[i
] = ~0u;
195 * Walk the set of instructions in the block, marking which entries in the acp
196 * are killed by the block.
199 fs_copy_prop_dataflow::run()
206 /* Update liveout for all blocks. */
207 foreach_block (block
, cfg
) {
208 if (block
->parents
.is_empty())
211 for (int i
= 0; i
< bitset_words
; i
++) {
212 const BITSET_WORD old_liveout
= bd
[block
->num
].liveout
[i
];
214 bd
[block
->num
].liveout
[i
] =
215 bd
[block
->num
].copy
[i
] | (bd
[block
->num
].livein
[i
] &
216 ~bd
[block
->num
].kill
[i
]);
218 if (old_liveout
!= bd
[block
->num
].liveout
[i
])
223 /* Update livein for all blocks. If a copy is live out of all parent
224 * blocks, it's live coming in to this block.
226 foreach_block (block
, cfg
) {
227 if (block
->parents
.is_empty())
230 for (int i
= 0; i
< bitset_words
; i
++) {
231 const BITSET_WORD old_livein
= bd
[block
->num
].livein
[i
];
233 bd
[block
->num
].livein
[i
] = ~0u;
234 foreach_list_typed(bblock_link
, parent_link
, link
, &block
->parents
) {
235 bblock_t
*parent
= parent_link
->block
;
236 bd
[block
->num
].livein
[i
] &= bd
[parent
->num
].liveout
[i
];
239 if (old_livein
!= bd
[block
->num
].livein
[i
])
247 fs_copy_prop_dataflow::dump_block_data() const
249 foreach_block (block
, cfg
) {
250 fprintf(stderr
, "Block %d [%d, %d] (parents ", block
->num
,
251 block
->start_ip
, block
->end_ip
);
252 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
253 bblock_t
*parent
= link
->block
;
254 fprintf(stderr
, "%d ", parent
->num
);
256 fprintf(stderr
, "):\n");
257 fprintf(stderr
, " livein = 0x");
258 for (int i
= 0; i
< bitset_words
; i
++)
259 fprintf(stderr
, "%08x", bd
[block
->num
].livein
[i
]);
260 fprintf(stderr
, ", liveout = 0x");
261 for (int i
= 0; i
< bitset_words
; i
++)
262 fprintf(stderr
, "%08x", bd
[block
->num
].liveout
[i
]);
263 fprintf(stderr
, ",\n copy = 0x");
264 for (int i
= 0; i
< bitset_words
; i
++)
265 fprintf(stderr
, "%08x", bd
[block
->num
].copy
[i
]);
266 fprintf(stderr
, ", kill = 0x");
267 for (int i
= 0; i
< bitset_words
; i
++)
268 fprintf(stderr
, "%08x", bd
[block
->num
].kill
[i
]);
269 fprintf(stderr
, "\n");
274 is_logic_op(enum opcode opcode
)
276 return (opcode
== BRW_OPCODE_AND
||
277 opcode
== BRW_OPCODE_OR
||
278 opcode
== BRW_OPCODE_XOR
||
279 opcode
== BRW_OPCODE_NOT
);
283 can_take_stride(fs_inst
*inst
, unsigned arg
, unsigned stride
,
284 const gen_device_info
*devinfo
)
289 /* 3-source instructions can only be Align16, which restricts what strides
290 * they can take. They can only take a stride of 1 (the usual case), or 0
291 * with a special "repctrl" bit. But the repctrl bit doesn't work for
292 * 64-bit datatypes, so if the source type is 64-bit then only a stride of
293 * 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page
296 * This is applicable to 32b datatypes and 16b datatype. 64b datatypes
297 * cannot use the replicate control.
299 if (inst
->is_3src(devinfo
)) {
300 if (type_sz(inst
->src
[arg
].type
) > 4)
303 return stride
== 1 || stride
== 0;
306 /* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",
307 * page 391 ("Extended Math Function"):
309 * The following restrictions apply for align1 mode: Scalar source is
310 * supported. Source and destination horizontal stride must be the
313 * From the Haswell PRM Volume 2b "Command Reference - Instructions", page
314 * 134 ("Extended Math Function"):
316 * Scalar source is supported. Source and destination horizontal stride
319 * and similar language exists for IVB and SNB. Pre-SNB, math instructions
320 * are sends, so the sources are moved to MRF's and there are no
323 if (inst
->is_math()) {
324 if (devinfo
->gen
== 6 || devinfo
->gen
== 7) {
325 assert(inst
->dst
.stride
== 1);
326 return stride
== 1 || stride
== 0;
327 } else if (devinfo
->gen
>= 8) {
328 return stride
== inst
->dst
.stride
|| stride
== 0;
336 fs_visitor::try_copy_propagate(fs_inst
*inst
, int arg
, acp_entry
*entry
)
338 if (inst
->src
[arg
].file
!= VGRF
)
341 if (entry
->src
.file
== IMM
)
343 assert(entry
->src
.file
== VGRF
|| entry
->src
.file
== UNIFORM
||
344 entry
->src
.file
== ATTR
);
346 if (entry
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
347 inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
)
350 assert(entry
->dst
.file
== VGRF
);
351 if (inst
->src
[arg
].nr
!= entry
->dst
.nr
)
354 /* Bail if inst is reading a range that isn't contained in the range
355 * that entry is writing.
357 if (!region_contained_in(inst
->src
[arg
], inst
->size_read(arg
),
358 entry
->dst
, entry
->size_written
))
361 /* we can't generally copy-propagate UD negations because we
362 * can end up accessing the resulting values as signed integers
363 * instead. See also resolve_ud_negate() and comment in
364 * fs_generator::generate_code.
366 if (entry
->src
.type
== BRW_REGISTER_TYPE_UD
&&
370 bool has_source_modifiers
= entry
->src
.abs
|| entry
->src
.negate
;
372 if ((has_source_modifiers
|| entry
->src
.file
== UNIFORM
||
373 !entry
->src
.is_contiguous()) &&
374 !inst
->can_do_source_mods(devinfo
))
377 if (has_source_modifiers
&&
378 inst
->opcode
== SHADER_OPCODE_GEN4_SCRATCH_WRITE
)
381 /* Bail if the result of composing both strides would exceed the
384 if (!can_take_stride(inst
, arg
, entry
->src
.stride
* inst
->src
[arg
].stride
,
388 /* Bail if the instruction type is larger than the execution type of the
389 * copy, what implies that each channel is reading multiple channels of the
390 * destination of the copy, and simply replacing the sources would give a
391 * program with different semantics.
393 if (type_sz(entry
->dst
.type
) < type_sz(inst
->src
[arg
].type
))
396 /* Bail if the result of composing both strides cannot be expressed
397 * as another stride. This avoids, for example, trying to transform
400 * MOV (8) rX<1>UD rY<0;1,0>UD
401 * FOO (8) ... rX<8;8,1>UW
405 * FOO (8) ... rY<0;1,0>UW
407 * Which would have different semantics.
409 if (entry
->src
.stride
!= 1 &&
410 (inst
->src
[arg
].stride
*
411 type_sz(inst
->src
[arg
].type
)) % type_sz(entry
->src
.type
) != 0)
414 /* Since semantics of source modifiers are type-dependent we need to
415 * ensure that the meaning of the instruction remains the same if we
416 * change the type. If the sizes of the types are different the new
417 * instruction will read a different amount of data than the original
418 * and the semantics will always be different.
420 if (has_source_modifiers
&&
421 entry
->dst
.type
!= inst
->src
[arg
].type
&&
422 (!inst
->can_change_types() ||
423 type_sz(entry
->dst
.type
) != type_sz(inst
->src
[arg
].type
)))
426 if (devinfo
->gen
>= 8 && (entry
->src
.negate
|| entry
->src
.abs
) &&
427 is_logic_op(inst
->opcode
)) {
431 if (entry
->saturate
) {
432 switch(inst
->opcode
) {
434 if ((inst
->conditional_mod
!= BRW_CONDITIONAL_GE
&&
435 inst
->conditional_mod
!= BRW_CONDITIONAL_L
) ||
436 inst
->src
[1].file
!= IMM
||
437 inst
->src
[1].f
< 0.0 ||
438 inst
->src
[1].f
> 1.0) {
447 inst
->src
[arg
].file
= entry
->src
.file
;
448 inst
->src
[arg
].nr
= entry
->src
.nr
;
449 inst
->src
[arg
].stride
*= entry
->src
.stride
;
450 inst
->saturate
= inst
->saturate
|| entry
->saturate
;
452 /* Compute the offset of inst->src[arg] relative to entry->dst */
453 const unsigned rel_offset
= inst
->src
[arg
].offset
- entry
->dst
.offset
;
455 /* Compute the first component of the copy that the instruction is
456 * reading, and the base byte offset within that component.
458 assert(entry
->dst
.offset
% REG_SIZE
== 0 && entry
->dst
.stride
== 1);
459 const unsigned component
= rel_offset
/ type_sz(entry
->dst
.type
);
460 const unsigned suboffset
= rel_offset
% type_sz(entry
->dst
.type
);
462 /* Calculate the byte offset at the origin of the copy of the given
463 * component and suboffset.
465 inst
->src
[arg
].offset
= suboffset
+
466 component
* entry
->src
.stride
* type_sz(entry
->src
.type
) +
469 if (has_source_modifiers
) {
470 if (entry
->dst
.type
!= inst
->src
[arg
].type
) {
471 /* We are propagating source modifiers from a MOV with a different
472 * type. If we got here, then we can just change the source and
473 * destination types of the instruction and keep going.
475 assert(inst
->can_change_types());
476 for (int i
= 0; i
< inst
->sources
; i
++) {
477 inst
->src
[i
].type
= entry
->dst
.type
;
479 inst
->dst
.type
= entry
->dst
.type
;
482 if (!inst
->src
[arg
].abs
) {
483 inst
->src
[arg
].abs
= entry
->src
.abs
;
484 inst
->src
[arg
].negate
^= entry
->src
.negate
;
493 fs_visitor::try_constant_propagate(fs_inst
*inst
, acp_entry
*entry
)
495 bool progress
= false;
497 if (entry
->src
.file
!= IMM
)
499 if (type_sz(entry
->src
.type
) > 4)
504 for (int i
= inst
->sources
- 1; i
>= 0; i
--) {
505 if (inst
->src
[i
].file
!= VGRF
)
508 assert(entry
->dst
.file
== VGRF
);
509 if (inst
->src
[i
].nr
!= entry
->dst
.nr
)
512 /* Bail if inst is reading a range that isn't contained in the range
513 * that entry is writing.
515 if (!region_contained_in(inst
->src
[i
], inst
->size_read(i
),
516 entry
->dst
, entry
->size_written
))
519 /* If the type sizes don't match each channel of the instruction is
520 * either extracting a portion of the constant (which could be handled
521 * with some effort but the code below doesn't) or reading multiple
522 * channels of the source at once.
524 if (type_sz(inst
->src
[i
].type
) != type_sz(entry
->dst
.type
))
527 fs_reg val
= entry
->src
;
528 val
.type
= inst
->src
[i
].type
;
530 if (inst
->src
[i
].abs
) {
531 if ((devinfo
->gen
>= 8 && is_logic_op(inst
->opcode
)) ||
532 !brw_abs_immediate(val
.type
, &val
.as_brw_reg())) {
537 if (inst
->src
[i
].negate
) {
538 if ((devinfo
->gen
>= 8 && is_logic_op(inst
->opcode
)) ||
539 !brw_negate_immediate(val
.type
, &val
.as_brw_reg())) {
544 switch (inst
->opcode
) {
546 case SHADER_OPCODE_LOAD_PAYLOAD
:
552 case SHADER_OPCODE_INT_QUOTIENT
:
553 case SHADER_OPCODE_INT_REMAINDER
:
554 /* FINISHME: Promote non-float constants and remove this. */
555 if (devinfo
->gen
< 8)
558 case SHADER_OPCODE_POW
:
559 /* Allow constant propagation into src1 (except on Gen 6 which
560 * doesn't support scalar source math), and let constant combining
561 * promote the constant on Gen < 8.
563 if (devinfo
->gen
== 6)
566 case BRW_OPCODE_BFI1
:
570 case BRW_OPCODE_SUBB
:
577 case BRW_OPCODE_MACH
:
579 case SHADER_OPCODE_MULH
:
584 case BRW_OPCODE_ADDC
:
588 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
589 /* Fit this constant in by commuting the operands.
590 * Exception: we can't do this for 32-bit integer MUL/MACH
591 * because it's asymmetric.
593 * The BSpec says for Broadwell that
595 * "When multiplying DW x DW, the dst cannot be accumulator."
597 * Integer MUL with a non-accumulator destination will be lowered
598 * by lower_integer_multiplication(), so don't restrict it.
600 if (((inst
->opcode
== BRW_OPCODE_MUL
&&
601 inst
->dst
.is_accumulator()) ||
602 inst
->opcode
== BRW_OPCODE_MACH
) &&
603 (inst
->src
[1].type
== BRW_REGISTER_TYPE_D
||
604 inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
))
606 inst
->src
[0] = inst
->src
[1];
617 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
618 enum brw_conditional_mod new_cmod
;
620 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
621 if (new_cmod
!= BRW_CONDITIONAL_NONE
) {
622 /* Fit this constant in by swapping the operands and
625 inst
->src
[0] = inst
->src
[1];
627 inst
->conditional_mod
= new_cmod
;
637 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
638 inst
->src
[0] = inst
->src
[1];
641 /* If this was predicated, flipping operands means
642 * we also need to flip the predicate.
644 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
645 inst
->predicate_inverse
=
646 !inst
->predicate_inverse
;
652 case SHADER_OPCODE_UNTYPED_ATOMIC
:
653 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
654 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
655 case SHADER_OPCODE_TYPED_ATOMIC
:
656 case SHADER_OPCODE_TYPED_SURFACE_READ
:
657 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
658 case SHADER_OPCODE_BYTE_SCATTERED_WRITE
:
659 case SHADER_OPCODE_BYTE_SCATTERED_READ
:
660 /* We only propagate into the surface argument of the
661 * instruction. Everything else goes through LOAD_PAYLOAD.
669 case FS_OPCODE_FB_WRITE_LOGICAL
:
670 /* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are
671 * bit-cast using a strided region so they cannot be immediates.
673 if (i
!= FB_WRITE_LOGICAL_SRC_SRC_STENCIL
&&
674 i
!= FB_WRITE_LOGICAL_SRC_OMASK
) {
680 case SHADER_OPCODE_TEX_LOGICAL
:
681 case SHADER_OPCODE_TXD_LOGICAL
:
682 case SHADER_OPCODE_TXF_LOGICAL
:
683 case SHADER_OPCODE_TXL_LOGICAL
:
684 case SHADER_OPCODE_TXS_LOGICAL
:
685 case FS_OPCODE_TXB_LOGICAL
:
686 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
687 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
688 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
689 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
690 case SHADER_OPCODE_LOD_LOGICAL
:
691 case SHADER_OPCODE_TG4_LOGICAL
:
692 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
693 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
694 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
695 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
696 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
697 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
698 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
699 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
700 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
705 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
706 case SHADER_OPCODE_BROADCAST
:
726 can_propagate_from(fs_inst
*inst
)
728 return (inst
->opcode
== BRW_OPCODE_MOV
&&
729 inst
->dst
.file
== VGRF
&&
730 ((inst
->src
[0].file
== VGRF
&&
731 !regions_overlap(inst
->dst
, inst
->size_written
,
732 inst
->src
[0], inst
->size_read(0))) ||
733 inst
->src
[0].file
== ATTR
||
734 inst
->src
[0].file
== UNIFORM
||
735 inst
->src
[0].file
== IMM
) &&
736 inst
->src
[0].type
== inst
->dst
.type
&&
737 !inst
->is_partial_write());
740 /* Walks a basic block and does copy propagation on it using the acp
744 fs_visitor::opt_copy_propagation_local(void *copy_prop_ctx
, bblock_t
*block
,
747 bool progress
= false;
749 foreach_inst_in_block(fs_inst
, inst
, block
) {
750 /* Try propagating into this instruction. */
751 for (int i
= 0; i
< inst
->sources
; i
++) {
752 if (inst
->src
[i
].file
!= VGRF
)
755 foreach_in_list(acp_entry
, entry
, &acp
[inst
->src
[i
].nr
% ACP_HASH_SIZE
]) {
756 if (try_constant_propagate(inst
, entry
))
758 else if (try_copy_propagate(inst
, i
, entry
))
763 /* kill the destination from the ACP */
764 if (inst
->dst
.file
== VGRF
) {
765 foreach_in_list_safe(acp_entry
, entry
, &acp
[inst
->dst
.nr
% ACP_HASH_SIZE
]) {
766 if (regions_overlap(entry
->dst
, entry
->size_written
,
767 inst
->dst
, inst
->size_written
))
771 /* Oops, we only have the chaining hash based on the destination, not
772 * the source, so walk across the entire table.
774 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
775 foreach_in_list_safe(acp_entry
, entry
, &acp
[i
]) {
776 /* Make sure we kill the entry if this instruction overwrites
777 * _any_ of the registers that it reads
779 if (regions_overlap(entry
->src
, entry
->size_read
,
780 inst
->dst
, inst
->size_written
))
786 /* If this instruction's source could potentially be folded into the
787 * operand of another instruction, add it to the ACP.
789 if (can_propagate_from(inst
)) {
790 acp_entry
*entry
= ralloc(copy_prop_ctx
, acp_entry
);
791 entry
->dst
= inst
->dst
;
792 entry
->src
= inst
->src
[0];
793 entry
->size_written
= inst
->size_written
;
794 entry
->size_read
= inst
->size_read(0);
795 entry
->opcode
= inst
->opcode
;
796 entry
->saturate
= inst
->saturate
;
797 acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
798 } else if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
799 inst
->dst
.file
== VGRF
) {
801 for (int i
= 0; i
< inst
->sources
; i
++) {
802 int effective_width
= i
< inst
->header_size
? 8 : inst
->exec_size
;
803 assert(effective_width
* type_sz(inst
->src
[i
].type
) % REG_SIZE
== 0);
804 const unsigned size_written
= effective_width
*
805 type_sz(inst
->src
[i
].type
);
806 if (inst
->src
[i
].file
== VGRF
) {
807 acp_entry
*entry
= rzalloc(copy_prop_ctx
, acp_entry
);
808 entry
->dst
= byte_offset(inst
->dst
, offset
);
809 entry
->src
= inst
->src
[i
];
810 entry
->size_written
= size_written
;
811 entry
->size_read
= inst
->size_read(i
);
812 entry
->opcode
= inst
->opcode
;
813 if (!entry
->dst
.equals(inst
->src
[i
])) {
814 acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
819 offset
+= size_written
;
828 fs_visitor::opt_copy_propagation()
830 bool progress
= false;
831 void *copy_prop_ctx
= ralloc_context(NULL
);
832 exec_list
*out_acp
[cfg
->num_blocks
];
834 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
835 out_acp
[i
] = new exec_list
[ACP_HASH_SIZE
];
837 /* First, walk through each block doing local copy propagation and getting
838 * the set of copies available at the end of the block.
840 foreach_block (block
, cfg
) {
841 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
,
842 out_acp
[block
->num
]) || progress
;
845 /* Do dataflow analysis for those available copies. */
846 fs_copy_prop_dataflow
dataflow(copy_prop_ctx
, cfg
, out_acp
);
848 /* Next, re-run local copy propagation, this time with the set of copies
849 * provided by the dataflow analysis available at the start of a block.
851 foreach_block (block
, cfg
) {
852 exec_list in_acp
[ACP_HASH_SIZE
];
854 for (int i
= 0; i
< dataflow
.num_acp
; i
++) {
855 if (BITSET_TEST(dataflow
.bd
[block
->num
].livein
, i
)) {
856 struct acp_entry
*entry
= dataflow
.acp
[i
];
857 in_acp
[entry
->dst
.nr
% ACP_HASH_SIZE
].push_tail(entry
);
861 progress
= opt_copy_propagation_local(copy_prop_ctx
, block
, in_acp
) ||
865 for (int i
= 0; i
< cfg
->num_blocks
; i
++)
866 delete [] out_acp
[i
];
867 ralloc_free(copy_prop_ctx
);
870 invalidate_live_intervals();