2 * Copyright © 2014 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 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_control_flow.h"
32 * Implements a small peephole optimization that looks for
43 * and replaces it with:
51 * where the SSA defs are ALU operations or other cheap instructions (not
52 * texturing, for example).
54 * If the number of ALU operations in the branches is greater than the limit
55 * parameter, then the optimization is skipped. In limit=0 mode, the SSA defs
56 * must only be MOVs which we expect to get copy-propagated away once they're
57 * out of the inner blocks.
61 block_check_for_allowed_instrs(nir_block
*block
, unsigned *count
,
62 bool alu_ok
, bool indirect_load_ok
,
63 bool expensive_alu_ok
)
65 nir_foreach_instr(instr
, block
) {
66 switch (instr
->type
) {
67 case nir_instr_type_intrinsic
: {
68 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
70 switch (intrin
->intrinsic
) {
71 case nir_intrinsic_load_deref
: {
72 nir_deref_instr
*const deref
= nir_src_as_deref(intrin
->src
[0]);
74 switch (deref
->mode
) {
75 case nir_var_shader_in
:
77 /* Don't try to remove flow control around an indirect load
78 * because that flow control may be trying to avoid invalid
81 if (!indirect_load_ok
&& nir_deref_instr_has_indirect(deref
))
92 case nir_intrinsic_load_uniform
:
104 case nir_instr_type_deref
:
105 case nir_instr_type_load_const
:
108 case nir_instr_type_alu
: {
109 nir_alu_instr
*mov
= nir_instr_as_alu(instr
);
135 if (!alu_ok
|| !expensive_alu_ok
)
142 /* It must be a move-like operation. */
149 if (!mov
->dest
.dest
.is_ssa
)
155 /* Can't handle saturate */
156 if (mov
->dest
.saturate
)
159 /* It cannot have any if-uses */
160 if (!list_empty(&mov
->dest
.dest
.ssa
.if_uses
))
163 /* The only uses of this definition must be phis in the successor */
164 nir_foreach_use(use
, &mov
->dest
.dest
.ssa
) {
165 if (use
->parent_instr
->type
!= nir_instr_type_phi
||
166 use
->parent_instr
->block
!= block
->successors
[0])
182 nir_opt_peephole_select_block(nir_block
*block
, nir_shader
*shader
,
183 unsigned limit
, bool indirect_load_ok
,
184 bool expensive_alu_ok
)
186 if (nir_cf_node_is_first(&block
->cf_node
))
189 nir_cf_node
*prev_node
= nir_cf_node_prev(&block
->cf_node
);
190 if (prev_node
->type
!= nir_cf_node_if
)
193 nir_if
*if_stmt
= nir_cf_node_as_if(prev_node
);
194 nir_block
*then_block
= nir_if_first_then_block(if_stmt
);
195 nir_block
*else_block
= nir_if_first_else_block(if_stmt
);
197 /* We can only have one block in each side ... */
198 if (nir_if_last_then_block(if_stmt
) != then_block
||
199 nir_if_last_else_block(if_stmt
) != else_block
)
202 /* ... and those blocks must only contain "allowed" instructions. */
204 if (!block_check_for_allowed_instrs(then_block
, &count
, limit
!= 0,
205 indirect_load_ok
, expensive_alu_ok
) ||
206 !block_check_for_allowed_instrs(else_block
, &count
, limit
!= 0,
207 indirect_load_ok
, expensive_alu_ok
))
213 /* At this point, we know that the previous CFG node is an if-then
214 * statement containing only moves to phi nodes in this block. We can
215 * just remove that entire CF node and replace all of the phi nodes with
219 nir_block
*prev_block
= nir_cf_node_as_block(nir_cf_node_prev(prev_node
));
221 /* First, we move the remaining instructions from the blocks to the
222 * block before. We have already guaranteed that this is safe by
223 * calling block_check_for_allowed_instrs()
225 nir_foreach_instr_safe(instr
, then_block
) {
226 exec_node_remove(&instr
->node
);
227 instr
->block
= prev_block
;
228 exec_list_push_tail(&prev_block
->instr_list
, &instr
->node
);
231 nir_foreach_instr_safe(instr
, else_block
) {
232 exec_node_remove(&instr
->node
);
233 instr
->block
= prev_block
;
234 exec_list_push_tail(&prev_block
->instr_list
, &instr
->node
);
237 nir_foreach_instr_safe(instr
, block
) {
238 if (instr
->type
!= nir_instr_type_phi
)
241 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
242 nir_alu_instr
*sel
= nir_alu_instr_create(shader
, nir_op_bcsel
);
243 nir_src_copy(&sel
->src
[0].src
, &if_stmt
->condition
, sel
);
244 /* Splat the condition to all channels */
245 memset(sel
->src
[0].swizzle
, 0, sizeof sel
->src
[0].swizzle
);
247 assert(exec_list_length(&phi
->srcs
) == 2);
248 nir_foreach_phi_src(src
, phi
) {
249 assert(src
->pred
== then_block
|| src
->pred
== else_block
);
250 assert(src
->src
.is_ssa
);
252 unsigned idx
= src
->pred
== then_block
? 1 : 2;
253 nir_src_copy(&sel
->src
[idx
].src
, &src
->src
, sel
);
256 nir_ssa_dest_init(&sel
->instr
, &sel
->dest
.dest
,
257 phi
->dest
.ssa
.num_components
,
258 phi
->dest
.ssa
.bit_size
, phi
->dest
.ssa
.name
);
259 sel
->dest
.write_mask
= (1 << phi
->dest
.ssa
.num_components
) - 1;
261 nir_ssa_def_rewrite_uses(&phi
->dest
.ssa
,
262 nir_src_for_ssa(&sel
->dest
.dest
.ssa
));
264 nir_instr_insert_before(&phi
->instr
, &sel
->instr
);
265 nir_instr_remove(&phi
->instr
);
268 nir_cf_node_remove(&if_stmt
->cf_node
);
273 nir_opt_peephole_select_impl(nir_function_impl
*impl
, unsigned limit
,
274 bool indirect_load_ok
, bool expensive_alu_ok
)
276 nir_shader
*shader
= impl
->function
->shader
;
277 bool progress
= false;
279 nir_foreach_block_safe(block
, impl
) {
280 progress
|= nir_opt_peephole_select_block(block
, shader
, limit
,
286 nir_metadata_preserve(impl
, nir_metadata_none
);
289 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
297 nir_opt_peephole_select(nir_shader
*shader
, unsigned limit
,
298 bool indirect_load_ok
, bool expensive_alu_ok
)
300 bool progress
= false;
302 nir_foreach_function(function
, shader
) {
304 progress
|= nir_opt_peephole_select_impl(function
->impl
, limit
,