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 "main/bitset.h"
41 namespace { /* avoid conflict with opt_copy_propagation_elements */
42 struct acp_entry
: public exec_node
{
50 * Which entries in the fs_copy_prop_dataflow acp table are live at the
51 * start of this block. This is the useful output of the analysis, since
52 * it lets us plug those into the local copy propagation on the second
58 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
59 * of this block. This is done in initial setup from the per-block acps
60 * returned by the first local copy prop pass.
65 * Which entries in the fs_copy_prop_dataflow acp table are generated by
66 * instructions in this block which reach the end of the block without
72 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
73 * course of this block.
78 class fs_copy_prop_dataflow
81 fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
82 exec_list
*out_acp
[ACP_HASH_SIZE
]);
84 void setup_initial_values();
87 void dump_block_data() const;
96 struct block_data
*bd
;
98 } /* anonymous namespace */
100 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
101 exec_list
*out_acp
[ACP_HASH_SIZE
])
102 : mem_ctx(mem_ctx
), cfg(cfg
)
104 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
107 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
108 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
109 foreach_list(entry_node
, &out_acp
[b
][i
]) {
115 acp
= rzalloc_array(mem_ctx
, struct acp_entry
*, num_acp
);
117 bitset_words
= BITSET_WORDS(num_acp
);
120 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
121 bd
[b
].livein
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
122 bd
[b
].liveout
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
123 bd
[b
].copy
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
124 bd
[b
].kill
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
126 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
127 foreach_list(entry_node
, &out_acp
[b
][i
]) {
128 acp_entry
*entry
= (acp_entry
*)entry_node
;
130 acp
[next_acp
] = entry
;
132 /* opt_copy_propagate_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
[b
].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 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
158 bblock_t
*block
= cfg
->blocks
[b
];
160 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
161 inst
!= block
->end
->next
;
162 inst
= (fs_inst
*)inst
->next
) {
163 if (inst
->dst
.file
!= GRF
)
166 /* Mark ACP entries which are killed by this instruction. */
167 for (int i
= 0; i
< num_acp
; i
++) {
168 if (inst
->overwrites_reg(acp
[i
]->dst
) ||
169 inst
->overwrites_reg(acp
[i
]->src
)) {
170 BITSET_SET(bd
[b
].kill
, i
);
176 /* Populate the initial values for the livein and liveout sets. For the
177 * block at the start of the program, livein = 0 and liveout = copy.
178 * For the others, set liveout to 0 (the empty set) and livein to ~0
179 * (the universal set).
181 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
182 bblock_t
*block
= cfg
->blocks
[b
];
183 if (block
->parents
.is_empty()) {
184 for (int i
= 0; i
< bitset_words
; i
++) {
185 bd
[b
].livein
[i
] = 0u;
186 bd
[b
].liveout
[i
] = bd
[b
].copy
[i
];
189 for (int i
= 0; i
< bitset_words
; i
++) {
190 bd
[b
].liveout
[i
] = 0u;
191 bd
[b
].livein
[i
] = ~0u;
198 * Walk the set of instructions in the block, marking which entries in the acp
199 * are killed by the block.
202 fs_copy_prop_dataflow::run()
209 /* Update liveout for all blocks. */
210 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
211 if (cfg
->blocks
[b
]->parents
.is_empty())
214 for (int i
= 0; i
< bitset_words
; i
++) {
215 const BITSET_WORD old_liveout
= bd
[b
].liveout
[i
];
218 bd
[b
].copy
[i
] | (bd
[b
].livein
[i
] & ~bd
[b
].kill
[i
]);
220 if (old_liveout
!= bd
[b
].liveout
[i
])
225 /* Update livein for all blocks. If a copy is live out of all parent
226 * blocks, it's live coming in to this block.
228 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
229 if (cfg
->blocks
[b
]->parents
.is_empty())
232 for (int i
= 0; i
< bitset_words
; i
++) {
233 const BITSET_WORD old_livein
= bd
[b
].livein
[i
];
235 bd
[b
].livein
[i
] = ~0u;
236 foreach_list_typed(bblock_link
, link
, link
, &cfg
->blocks
[b
]->parents
) {
237 bblock_t
*block
= link
->block
;
238 bd
[b
].livein
[i
] &= bd
[block
->block_num
].liveout
[i
];
241 if (old_livein
!= bd
[b
].livein
[i
])
249 fs_copy_prop_dataflow::dump_block_data() const
251 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
252 bblock_t
*block
= cfg
->blocks
[b
];
253 fprintf(stderr
, "Block %d [%d, %d] (parents ", block
->block_num
,
254 block
->start_ip
, block
->end_ip
);
255 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
256 bblock_t
*parent
= link
->block
;
257 fprintf(stderr
, "%d ", parent
->block_num
);
259 fprintf(stderr
, "):\n");
260 fprintf(stderr
, " livein = 0x");
261 for (int i
= 0; i
< bitset_words
; i
++)
262 fprintf(stderr
, "%08x", bd
[b
].livein
[i
]);
263 fprintf(stderr
, ", liveout = 0x");
264 for (int i
= 0; i
< bitset_words
; i
++)
265 fprintf(stderr
, "%08x", bd
[b
].liveout
[i
]);
266 fprintf(stderr
, ",\n copy = 0x");
267 for (int i
= 0; i
< bitset_words
; i
++)
268 fprintf(stderr
, "%08x", bd
[b
].copy
[i
]);
269 fprintf(stderr
, ", kill = 0x");
270 for (int i
= 0; i
< bitset_words
; i
++)
271 fprintf(stderr
, "%08x", bd
[b
].kill
[i
]);
272 fprintf(stderr
, "\n");
277 is_logic_op(enum opcode opcode
)
279 return (opcode
== BRW_OPCODE_AND
||
280 opcode
== BRW_OPCODE_OR
||
281 opcode
== BRW_OPCODE_XOR
||
282 opcode
== BRW_OPCODE_NOT
);
286 fs_visitor::try_copy_propagate(fs_inst
*inst
, int arg
, acp_entry
*entry
)
288 if (entry
->src
.file
== IMM
)
291 if (entry
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
292 inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
)
295 /* Bail if inst is reading more than entry is writing. */
296 if ((inst
->regs_read(this, arg
) * inst
->src
[arg
].stride
*
297 type_sz(inst
->src
[arg
].type
)) > type_sz(entry
->dst
.type
))
300 if (inst
->src
[arg
].file
!= entry
->dst
.file
||
301 inst
->src
[arg
].reg
!= entry
->dst
.reg
||
302 inst
->src
[arg
].reg_offset
!= entry
->dst
.reg_offset
||
303 inst
->src
[arg
].subreg_offset
!= entry
->dst
.subreg_offset
) {
307 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
308 if (inst
->conditional_mod
&&
309 inst
->src
[arg
].type
== BRW_REGISTER_TYPE_UD
&&
313 bool has_source_modifiers
= entry
->src
.abs
|| entry
->src
.negate
;
315 if ((has_source_modifiers
|| entry
->src
.file
== UNIFORM
||
316 !entry
->src
.is_contiguous()) &&
317 !can_do_source_mods(inst
))
320 /* Bail if the result of composing both strides would exceed the
323 if (entry
->src
.stride
* inst
->src
[arg
].stride
> 4)
326 /* Bail if the result of composing both strides cannot be expressed
327 * as another stride. This avoids, for example, trying to transform
330 * MOV (8) rX<1>UD rY<0;1,0>UD
331 * FOO (8) ... rX<8;8,1>UW
335 * FOO (8) ... rY<0;1,0>UW
337 * Which would have different semantics.
339 if (entry
->src
.stride
!= 1 &&
340 (inst
->src
[arg
].stride
*
341 type_sz(inst
->src
[arg
].type
)) % type_sz(entry
->src
.type
) != 0)
344 if (has_source_modifiers
&& entry
->dst
.type
!= inst
->src
[arg
].type
)
348 if (entry
->src
.negate
) {
349 if (is_logic_op(inst
->opcode
)) {
355 inst
->src
[arg
].file
= entry
->src
.file
;
356 inst
->src
[arg
].reg
= entry
->src
.reg
;
357 inst
->src
[arg
].reg_offset
= entry
->src
.reg_offset
;
358 inst
->src
[arg
].subreg_offset
= entry
->src
.subreg_offset
;
359 inst
->src
[arg
].stride
*= entry
->src
.stride
;
361 if (!inst
->src
[arg
].abs
) {
362 inst
->src
[arg
].abs
= entry
->src
.abs
;
363 inst
->src
[arg
].negate
^= entry
->src
.negate
;
371 fs_visitor::try_constant_propagate(fs_inst
*inst
, acp_entry
*entry
)
373 bool progress
= false;
375 if (entry
->src
.file
!= IMM
)
378 for (int i
= inst
->sources
- 1; i
>= 0; i
--) {
379 if (inst
->src
[i
].file
!= entry
->dst
.file
||
380 inst
->src
[i
].reg
!= entry
->dst
.reg
||
381 inst
->src
[i
].reg_offset
!= entry
->dst
.reg_offset
||
382 inst
->src
[i
].subreg_offset
!= entry
->dst
.subreg_offset
||
383 inst
->src
[i
].type
!= entry
->dst
.type
||
384 inst
->src
[i
].stride
> 1)
387 /* Don't bother with cases that should have been taken care of by the
388 * GLSL compiler's constant folding pass.
390 if (inst
->src
[i
].negate
|| inst
->src
[i
].abs
)
393 switch (inst
->opcode
) {
395 inst
->src
[i
] = entry
->src
;
399 case BRW_OPCODE_BFI1
:
403 case BRW_OPCODE_SUBB
:
405 inst
->src
[i
] = entry
->src
;
410 case BRW_OPCODE_MACH
:
416 case BRW_OPCODE_ADDC
:
418 inst
->src
[i
] = entry
->src
;
420 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
421 /* Fit this constant in by commuting the operands.
422 * Exception: we can't do this for 32-bit integer MUL/MACH
423 * because it's asymmetric.
425 if ((inst
->opcode
== BRW_OPCODE_MUL
||
426 inst
->opcode
== BRW_OPCODE_MACH
) &&
427 (inst
->src
[1].type
== BRW_REGISTER_TYPE_D
||
428 inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
))
430 inst
->src
[0] = inst
->src
[1];
431 inst
->src
[1] = entry
->src
;
439 inst
->src
[i
] = entry
->src
;
441 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
444 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
445 if (new_cmod
!= ~0u) {
446 /* Fit this constant in by swapping the operands and
449 inst
->src
[0] = inst
->src
[1];
450 inst
->src
[1] = entry
->src
;
451 inst
->conditional_mod
= new_cmod
;
459 inst
->src
[i
] = entry
->src
;
461 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
462 inst
->src
[0] = inst
->src
[1];
463 inst
->src
[1] = entry
->src
;
465 /* If this was predicated, flipping operands means
466 * we also need to flip the predicate.
468 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
469 inst
->predicate_inverse
=
470 !inst
->predicate_inverse
;
476 case SHADER_OPCODE_RCP
:
477 /* The hardware doesn't do math on immediate values
478 * (because why are you doing that, seriously?), but
479 * the correct answer is to just constant fold it
483 if (inst
->src
[0].imm
.f
!= 0.0f
) {
484 inst
->opcode
= BRW_OPCODE_MOV
;
485 inst
->src
[0] = entry
->src
;
486 inst
->src
[0].imm
.f
= 1.0f
/ inst
->src
[0].imm
.f
;
491 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
492 inst
->src
[i
] = entry
->src
;
505 can_propagate_from(fs_inst
*inst
)
507 return (inst
->opcode
== BRW_OPCODE_MOV
&&
508 inst
->dst
.file
== GRF
&&
509 ((inst
->src
[0].file
== GRF
&&
510 (inst
->src
[0].reg
!= inst
->dst
.reg
||
511 inst
->src
[0].reg_offset
!= inst
->dst
.reg_offset
)) ||
512 inst
->src
[0].file
== UNIFORM
||
513 inst
->src
[0].file
== IMM
) &&
514 inst
->src
[0].type
== inst
->dst
.type
&&
516 !inst
->is_partial_write());
519 /* Walks a basic block and does copy propagation on it using the acp
523 fs_visitor::opt_copy_propagate_local(void *copy_prop_ctx
, bblock_t
*block
,
526 bool progress
= false;
528 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
529 inst
!= block
->end
->next
;
530 inst
= (fs_inst
*)inst
->next
) {
532 /* Try propagating into this instruction. */
533 for (int i
= 0; i
< inst
->sources
; i
++) {
534 if (inst
->src
[i
].file
!= GRF
)
537 foreach_list(entry_node
, &acp
[inst
->src
[i
].reg
% ACP_HASH_SIZE
]) {
538 acp_entry
*entry
= (acp_entry
*)entry_node
;
540 if (try_constant_propagate(inst
, entry
))
543 if (try_copy_propagate(inst
, i
, entry
))
548 /* kill the destination from the ACP */
549 if (inst
->dst
.file
== GRF
) {
550 foreach_list_safe(entry_node
, &acp
[inst
->dst
.reg
% ACP_HASH_SIZE
]) {
551 acp_entry
*entry
= (acp_entry
*)entry_node
;
553 if (inst
->overwrites_reg(entry
->dst
)) {
558 /* Oops, we only have the chaining hash based on the destination, not
559 * the source, so walk across the entire table.
561 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
562 foreach_list_safe(entry_node
, &acp
[i
]) {
563 acp_entry
*entry
= (acp_entry
*)entry_node
;
564 if (inst
->overwrites_reg(entry
->src
))
570 /* If this instruction's source could potentially be folded into the
571 * operand of another instruction, add it to the ACP.
573 if (can_propagate_from(inst
)) {
574 acp_entry
*entry
= ralloc(copy_prop_ctx
, acp_entry
);
575 entry
->dst
= inst
->dst
;
576 entry
->src
= inst
->src
[0];
577 entry
->opcode
= inst
->opcode
;
578 acp
[entry
->dst
.reg
% ACP_HASH_SIZE
].push_tail(entry
);
579 } else if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
&&
580 inst
->dst
.file
== GRF
) {
581 for (int i
= 0; i
< inst
->sources
; i
++) {
582 if (inst
->src
[i
].file
== GRF
) {
583 acp_entry
*entry
= ralloc(copy_prop_ctx
, acp_entry
);
584 entry
->dst
= inst
->dst
;
585 entry
->dst
.reg_offset
= i
;
586 entry
->src
= inst
->src
[i
];
587 entry
->opcode
= inst
->opcode
;
588 if (!entry
->dst
.equals(inst
->src
[i
])) {
589 acp
[entry
->dst
.reg
% ACP_HASH_SIZE
].push_tail(entry
);
602 fs_visitor::opt_copy_propagate()
604 bool progress
= false;
605 void *copy_prop_ctx
= ralloc_context(NULL
);
606 cfg_t
cfg(&instructions
);
607 exec_list
*out_acp
[cfg
.num_blocks
];
608 for (int i
= 0; i
< cfg
.num_blocks
; i
++)
609 out_acp
[i
] = new exec_list
[ACP_HASH_SIZE
];
611 /* First, walk through each block doing local copy propagation and getting
612 * the set of copies available at the end of the block.
614 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
615 bblock_t
*block
= cfg
.blocks
[b
];
617 progress
= opt_copy_propagate_local(copy_prop_ctx
, block
,
618 out_acp
[b
]) || progress
;
621 /* Do dataflow analysis for those available copies. */
622 fs_copy_prop_dataflow
dataflow(copy_prop_ctx
, &cfg
, out_acp
);
624 /* Next, re-run local copy propagation, this time with the set of copies
625 * provided by the dataflow analysis available at the start of a block.
627 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
628 bblock_t
*block
= cfg
.blocks
[b
];
629 exec_list in_acp
[ACP_HASH_SIZE
];
631 for (int i
= 0; i
< dataflow
.num_acp
; i
++) {
632 if (BITSET_TEST(dataflow
.bd
[b
].livein
, i
)) {
633 struct acp_entry
*entry
= dataflow
.acp
[i
];
634 in_acp
[entry
->dst
.reg
% ACP_HASH_SIZE
].push_tail(entry
);
638 progress
= opt_copy_propagate_local(copy_prop_ctx
, block
, in_acp
) || progress
;
641 for (int i
= 0; i
< cfg
.num_blocks
; i
++)
642 delete [] out_acp
[i
];
643 ralloc_free(copy_prop_ctx
);
646 invalidate_live_intervals();