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 Muchnik's Advanced Compiler Design and Implementation, section
35 #define ACP_HASH_SIZE 16
40 namespace { /* avoid conflict with opt_copy_propagation_elements */
41 struct acp_entry
: public exec_node
{
48 * Which entries in the fs_copy_prop_dataflow acp table are live at the
49 * start of this block. This is the useful output of the analysis, since
50 * it lets us plug those into the local copy propagation on the second
56 * Which entries in the fs_copy_prop_dataflow acp table are live at the end
57 * of this block. This is done in initial setup from the per-block acps
58 * returned by the first local copy prop pass.
63 * Which entries in the fs_copy_prop_dataflow acp table are killed over the
64 * course of this block.
69 class fs_copy_prop_dataflow
72 fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
73 exec_list
*out_acp
[ACP_HASH_SIZE
]);
84 struct block_data
*bd
;
86 } /* anonymous namespace */
88 fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx
, cfg_t
*cfg
,
89 exec_list
*out_acp
[ACP_HASH_SIZE
])
90 : mem_ctx(mem_ctx
), cfg(cfg
)
92 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
95 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
96 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
97 foreach_list(entry_node
, &out_acp
[b
][i
]) {
103 acp
= rzalloc_array(mem_ctx
, struct acp_entry
*, num_acp
);
106 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
107 bd
[b
].livein
= rzalloc_array(bd
, bool, num_acp
);
108 bd
[b
].liveout
= rzalloc_array(bd
, bool, num_acp
);
109 bd
[b
].kill
= rzalloc_array(bd
, bool, num_acp
);
111 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
112 foreach_list(entry_node
, &out_acp
[b
][i
]) {
113 acp_entry
*entry
= (acp_entry
*)entry_node
;
115 acp
[next_acp
] = entry
;
116 bd
[b
].liveout
[next_acp
] = true;
122 assert(next_acp
== num_acp
);
129 * Walk the set of instructions in the block, marking which entries in the acp
130 * are killed by the block.
133 fs_copy_prop_dataflow::setup_kills()
135 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
136 bblock_t
*block
= cfg
->blocks
[b
];
138 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
139 inst
!= block
->end
->next
;
140 inst
= (fs_inst
*)inst
->next
) {
141 if (inst
->dst
.file
!= GRF
)
144 for (int i
= 0; i
< num_acp
; i
++) {
145 if (inst
->overwrites_reg(acp
[i
]->dst
) ||
146 inst
->overwrites_reg(acp
[i
]->src
)) {
147 bd
[b
].kill
[i
] = true;
155 * Walk the set of instructions in the block, marking which entries in the acp
156 * are killed by the block.
159 fs_copy_prop_dataflow::run()
166 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
167 for (int i
= 0; i
< num_acp
; i
++) {
168 if (!bd
[b
].liveout
[i
]) {
170 if (bd
[b
].livein
[i
] && !bd
[b
].kill
[i
]) {
171 bd
[b
].liveout
[i
] = true;
176 if (!bd
[b
].livein
[i
]) {
177 /* Update livein: if it's live at the end of all parents, it's
181 foreach_list(block_node
, &cfg
->blocks
[b
]->parents
) {
182 bblock_link
*link
= (bblock_link
*)block_node
;
183 bblock_t
*block
= link
->block
;
184 if (!bd
[block
->block_num
].liveout
[i
]) {
190 bd
[b
].livein
[i
] = true;
200 fs_visitor::try_copy_propagate(fs_inst
*inst
, int arg
, acp_entry
*entry
)
202 if (entry
->src
.file
== IMM
)
205 if (inst
->src
[arg
].file
!= entry
->dst
.file
||
206 inst
->src
[arg
].reg
!= entry
->dst
.reg
||
207 inst
->src
[arg
].reg_offset
!= entry
->dst
.reg_offset
) {
211 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
212 if (inst
->conditional_mod
&&
213 inst
->src
[arg
].type
== BRW_REGISTER_TYPE_UD
&&
217 bool has_source_modifiers
= entry
->src
.abs
|| entry
->src
.negate
;
219 if ((has_source_modifiers
|| entry
->src
.file
== UNIFORM
||
220 entry
->src
.smear
!= -1) && !can_do_source_mods(inst
))
223 inst
->src
[arg
].file
= entry
->src
.file
;
224 inst
->src
[arg
].reg
= entry
->src
.reg
;
225 inst
->src
[arg
].reg_offset
= entry
->src
.reg_offset
;
226 if (entry
->src
.smear
!= -1)
227 inst
->src
[arg
].smear
= entry
->src
.smear
;
229 if (!inst
->src
[arg
].abs
) {
230 inst
->src
[arg
].abs
= entry
->src
.abs
;
231 inst
->src
[arg
].negate
^= entry
->src
.negate
;
239 fs_visitor::try_constant_propagate(fs_inst
*inst
, acp_entry
*entry
)
241 bool progress
= false;
243 if (entry
->src
.file
!= IMM
)
246 for (int i
= 2; i
>= 0; i
--) {
247 if (inst
->src
[i
].file
!= entry
->dst
.file
||
248 inst
->src
[i
].reg
!= entry
->dst
.reg
||
249 inst
->src
[i
].reg_offset
!= entry
->dst
.reg_offset
)
252 /* Don't bother with cases that should have been taken care of by the
253 * GLSL compiler's constant folding pass.
255 if (inst
->src
[i
].negate
|| inst
->src
[i
].abs
)
258 switch (inst
->opcode
) {
260 inst
->src
[i
] = entry
->src
;
267 inst
->src
[i
] = entry
->src
;
269 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
270 /* Fit this constant in by commuting the operands.
271 * Exception: we can't do this for 32-bit integer MUL
272 * because it's asymmetric.
274 if (inst
->opcode
== BRW_OPCODE_MUL
&&
275 (inst
->src
[1].type
== BRW_REGISTER_TYPE_D
||
276 inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
))
278 inst
->src
[0] = inst
->src
[1];
279 inst
->src
[1] = entry
->src
;
287 inst
->src
[i
] = entry
->src
;
289 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
292 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
293 if (new_cmod
!= ~0u) {
294 /* Fit this constant in by swapping the operands and
297 inst
->src
[0] = inst
->src
[1];
298 inst
->src
[1] = entry
->src
;
299 inst
->conditional_mod
= new_cmod
;
307 inst
->src
[i
] = entry
->src
;
309 } else if (i
== 0 && inst
->src
[1].file
!= IMM
) {
310 inst
->src
[0] = inst
->src
[1];
311 inst
->src
[1] = entry
->src
;
313 /* If this was predicated, flipping operands means
314 * we also need to flip the predicate.
316 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
317 inst
->predicate_inverse
=
318 !inst
->predicate_inverse
;
324 case SHADER_OPCODE_RCP
:
325 /* The hardware doesn't do math on immediate values
326 * (because why are you doing that, seriously?), but
327 * the correct answer is to just constant fold it
331 if (inst
->src
[0].imm
.f
!= 0.0f
) {
332 inst
->opcode
= BRW_OPCODE_MOV
;
333 inst
->src
[0] = entry
->src
;
334 inst
->src
[0].imm
.f
= 1.0f
/ inst
->src
[0].imm
.f
;
339 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
340 inst
->src
[i
] = entry
->src
;
351 /* Walks a basic block and does copy propagation on it using the acp
355 fs_visitor::opt_copy_propagate_local(void *mem_ctx
, bblock_t
*block
,
358 bool progress
= false;
360 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
361 inst
!= block
->end
->next
;
362 inst
= (fs_inst
*)inst
->next
) {
364 /* Try propagating into this instruction. */
365 for (int i
= 0; i
< 3; i
++) {
366 if (inst
->src
[i
].file
!= GRF
)
369 foreach_list(entry_node
, &acp
[inst
->src
[i
].reg
% ACP_HASH_SIZE
]) {
370 acp_entry
*entry
= (acp_entry
*)entry_node
;
372 if (try_constant_propagate(inst
, entry
))
375 if (try_copy_propagate(inst
, i
, entry
))
380 /* kill the destination from the ACP */
381 if (inst
->dst
.file
== GRF
) {
382 foreach_list_safe(entry_node
, &acp
[inst
->dst
.reg
% ACP_HASH_SIZE
]) {
383 acp_entry
*entry
= (acp_entry
*)entry_node
;
385 if (inst
->overwrites_reg(entry
->dst
)) {
390 /* Oops, we only have the chaining hash based on the destination, not
391 * the source, so walk across the entire table.
393 for (int i
= 0; i
< ACP_HASH_SIZE
; i
++) {
394 foreach_list_safe(entry_node
, &acp
[i
]) {
395 acp_entry
*entry
= (acp_entry
*)entry_node
;
396 if (inst
->overwrites_reg(entry
->src
))
402 /* If this instruction's source could potentially be folded into the
403 * operand of another instruction, add it to the ACP.
405 if (inst
->opcode
== BRW_OPCODE_MOV
&&
406 inst
->dst
.file
== GRF
&&
407 ((inst
->src
[0].file
== GRF
&&
408 (inst
->src
[0].reg
!= inst
->dst
.reg
||
409 inst
->src
[0].reg_offset
!= inst
->dst
.reg_offset
)) ||
410 inst
->src
[0].file
== UNIFORM
||
411 inst
->src
[0].file
== IMM
) &&
412 inst
->src
[0].type
== inst
->dst
.type
&&
415 !inst
->force_uncompressed
&&
416 !inst
->force_sechalf
) {
417 acp_entry
*entry
= ralloc(mem_ctx
, acp_entry
);
418 entry
->dst
= inst
->dst
;
419 entry
->src
= inst
->src
[0];
420 acp
[entry
->dst
.reg
% ACP_HASH_SIZE
].push_tail(entry
);
428 fs_visitor::opt_copy_propagate()
430 bool progress
= false;
431 void *mem_ctx
= ralloc_context(this->mem_ctx
);
433 exec_list
*out_acp
[cfg
.num_blocks
];
434 for (int i
= 0; i
< cfg
.num_blocks
; i
++)
435 out_acp
[i
] = new exec_list
[ACP_HASH_SIZE
];
437 /* First, walk through each block doing local copy propagation and getting
438 * the set of copies available at the end of the block.
440 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
441 bblock_t
*block
= cfg
.blocks
[b
];
443 progress
= opt_copy_propagate_local(mem_ctx
, block
,
444 out_acp
[b
]) || progress
;
447 /* Do dataflow analysis for those available copies. */
448 fs_copy_prop_dataflow
dataflow(mem_ctx
, &cfg
, out_acp
);
450 /* Next, re-run local copy propagation, this time with the set of copies
451 * provided by the dataflow analysis available at the start of a block.
453 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
454 bblock_t
*block
= cfg
.blocks
[b
];
455 exec_list in_acp
[ACP_HASH_SIZE
];
457 for (int i
= 0; i
< dataflow
.num_acp
; i
++) {
458 if (dataflow
.bd
[b
].livein
[i
]) {
459 struct acp_entry
*entry
= dataflow
.acp
[i
];
460 in_acp
[entry
->dst
.reg
% ACP_HASH_SIZE
].push_tail(entry
);
464 progress
= opt_copy_propagate_local(mem_ctx
, block
, in_acp
) || progress
;
467 for (int i
= 0; i
< cfg
.num_blocks
; i
++)
468 delete [] out_acp
[i
];
469 ralloc_free(mem_ctx
);
472 live_intervals_valid
= false;