2 * Copyright © 2014 Connor Abbott
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 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_control_flow.h"
32 * This file implements an optimization that deletes statically
33 * unreachable/dead code. In NIR, one way this can happen is when an if
34 * statement has a constant condition:
40 * We delete the if statement and paste the contents of the always-executed
41 * branch into the surrounding control flow, possibly removing more code if
42 * the branch had a jump at the end.
44 * Another way is that control flow can end in a jump so that code after it
45 * never gets executed. In particular, this can happen after optimizing
54 * We also consider the case where both branches of an if end in a jump, e.g.:
63 * Finally, we also handle removing useless loops and ifs, i.e. loops and ifs
64 * with no side effects and without any definitions that are used
65 * elsewhere. This case is a little different from the first two in that the
66 * code is actually run (it just never does anything), but there are similar
67 * issues with needing to be careful with restarting after deleting the
68 * cf_node (see dead_cf_list()) so this is a convenient place to remove them.
72 remove_after_cf_node(nir_cf_node
*node
)
74 nir_cf_node
*end
= node
;
75 while (!nir_cf_node_is_last(end
))
76 end
= nir_cf_node_next(end
);
79 nir_cf_extract(&list
, nir_after_cf_node(node
), nir_after_cf_node(end
));
84 opt_constant_if(nir_if
*if_stmt
, bool condition
)
86 /* First, we need to remove any phi nodes after the if by rewriting uses to
87 * point to the correct source.
89 nir_block
*after
= nir_cf_node_as_block(nir_cf_node_next(&if_stmt
->cf_node
));
90 nir_block
*last_block
= condition
? nir_if_last_then_block(if_stmt
)
91 : nir_if_last_else_block(if_stmt
);
93 nir_foreach_instr_safe(instr
, after
) {
94 if (instr
->type
!= nir_instr_type_phi
)
97 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
98 nir_ssa_def
*def
= NULL
;
99 nir_foreach_phi_src(phi_src
, phi
) {
100 if (phi_src
->pred
!= last_block
)
103 assert(phi_src
->src
.is_ssa
);
104 def
= phi_src
->src
.ssa
;
108 assert(phi
->dest
.is_ssa
);
109 nir_ssa_def_rewrite_uses(&phi
->dest
.ssa
, nir_src_for_ssa(def
));
110 nir_instr_remove(instr
);
113 /* The control flow list we're about to paste in may include a jump at the
114 * end, and in that case we have to delete the rest of the control flow
115 * list after the if since it's unreachable and the validator will balk if
119 if (!exec_list_is_empty(&last_block
->instr_list
)) {
120 nir_instr
*last_instr
= nir_block_last_instr(last_block
);
121 if (last_instr
->type
== nir_instr_type_jump
)
122 remove_after_cf_node(&if_stmt
->cf_node
);
125 /* Finally, actually paste in the then or else branch and delete the if. */
126 struct exec_list
*cf_list
= condition
? &if_stmt
->then_list
127 : &if_stmt
->else_list
;
130 nir_cf_list_extract(&list
, cf_list
);
131 nir_cf_reinsert(&list
, nir_after_cf_node(&if_stmt
->cf_node
));
132 nir_cf_node_remove(&if_stmt
->cf_node
);
136 cf_node_has_side_effects(nir_cf_node
*node
)
138 nir_foreach_block_in_cf_node(block
, node
) {
139 bool inside_loop
= node
->type
== nir_cf_node_loop
;
140 for (nir_cf_node
*n
= &block
->cf_node
; !inside_loop
&& n
!= node
; n
= n
->parent
) {
141 if (n
->type
== nir_cf_node_loop
)
145 nir_foreach_instr(instr
, block
) {
146 if (instr
->type
== nir_instr_type_call
)
149 /* Return instructions can cause us to skip over other side-effecting
150 * instructions after the loop, so consider them to have side effects
153 * When the block is not inside a loop, break and continue might also
157 if (instr
->type
== nir_instr_type_jump
&&
158 (!inside_loop
|| nir_instr_as_jump(instr
)->type
== nir_jump_return
))
161 if (instr
->type
!= nir_instr_type_intrinsic
)
164 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
165 if (!(nir_intrinsic_infos
[intrin
->intrinsic
].flags
&
166 NIR_INTRINSIC_CAN_ELIMINATE
))
175 def_not_live_out(nir_ssa_def
*def
, void *state
)
177 nir_block
*after
= state
;
179 return !BITSET_TEST(after
->live_in
, def
->live_index
);
183 * Test if a loop node or if node is dead. Such nodes are dead if:
185 * 1) It has no side effects (i.e. intrinsics which could possibly affect the
186 * state of the program aside from producing an SSA value, indicated by a lack
187 * of NIR_INTRINSIC_CAN_ELIMINATE).
189 * 2) It has no phi instructions after it, since those indicate values inside
190 * the node being used after the node.
192 * 3) None of the values defined inside the node is used outside the node,
193 * i.e. none of the definitions that dominate the node exit are used outside.
195 * If those conditions hold, then the node is dead and can be deleted.
199 node_is_dead(nir_cf_node
*node
)
201 assert(node
->type
== nir_cf_node_loop
|| node
->type
== nir_cf_node_if
);
203 nir_block
*before
= nir_cf_node_as_block(nir_cf_node_prev(node
));
204 nir_block
*after
= nir_cf_node_as_block(nir_cf_node_next(node
));
206 if (!exec_list_is_empty(&after
->instr_list
) &&
207 nir_block_first_instr(after
)->type
== nir_instr_type_phi
)
210 if (cf_node_has_side_effects(node
))
213 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
214 nir_metadata_require(impl
, nir_metadata_live_ssa_defs
|
215 nir_metadata_dominance
);
217 for (nir_block
*cur
= after
->imm_dom
; cur
&& cur
!= before
;
218 cur
= cur
->imm_dom
) {
219 nir_foreach_instr(instr
, cur
) {
220 if (!nir_foreach_ssa_def(instr
, def_not_live_out
, after
))
229 dead_cf_block(nir_block
*block
)
231 nir_if
*following_if
= nir_block_get_following_if(block
);
233 if (node_is_dead(&following_if
->cf_node
)) {
234 nir_cf_node_remove(&following_if
->cf_node
);
238 nir_const_value
*const_value
=
239 nir_src_as_const_value(following_if
->condition
);
244 opt_constant_if(following_if
, const_value
->u32
[0] != 0);
248 nir_loop
*following_loop
= nir_block_get_following_loop(block
);
252 if (!node_is_dead(&following_loop
->cf_node
))
255 nir_cf_node_remove(&following_loop
->cf_node
);
260 ends_in_jump(nir_block
*block
)
262 if (exec_list_is_empty(&block
->instr_list
))
265 nir_instr
*instr
= nir_block_last_instr(block
);
266 return instr
->type
== nir_instr_type_jump
;
270 dead_cf_list(struct exec_list
*list
, bool *list_ends_in_jump
)
272 bool progress
= false;
273 *list_ends_in_jump
= false;
275 nir_cf_node
*prev
= NULL
;
277 foreach_list_typed(nir_cf_node
, cur
, node
, list
) {
279 case nir_cf_node_block
: {
280 nir_block
*block
= nir_cf_node_as_block(cur
);
281 if (dead_cf_block(block
)) {
282 /* We just deleted the if or loop after this block, so we may have
283 * deleted the block before or after it -- which one is an
284 * implementation detail. Therefore, to recover the place we were
285 * at, we have to use the previous cf_node.
289 cur
= nir_cf_node_next(prev
);
291 cur
= exec_node_data(nir_cf_node
, exec_list_get_head(list
),
295 block
= nir_cf_node_as_block(cur
);
300 if (ends_in_jump(block
)) {
301 *list_ends_in_jump
= true;
303 if (!exec_node_is_tail_sentinel(cur
->node
.next
)) {
304 remove_after_cf_node(cur
);
312 case nir_cf_node_if
: {
313 nir_if
*if_stmt
= nir_cf_node_as_if(cur
);
314 bool then_ends_in_jump
, else_ends_in_jump
;
315 progress
|= dead_cf_list(&if_stmt
->then_list
, &then_ends_in_jump
);
316 progress
|= dead_cf_list(&if_stmt
->else_list
, &else_ends_in_jump
);
318 if (then_ends_in_jump
&& else_ends_in_jump
) {
319 *list_ends_in_jump
= true;
320 nir_block
*next
= nir_cf_node_as_block(nir_cf_node_next(cur
));
321 if (!exec_list_is_empty(&next
->instr_list
) ||
322 !exec_node_is_tail_sentinel(next
->cf_node
.node
.next
)) {
323 remove_after_cf_node(cur
);
331 case nir_cf_node_loop
: {
332 nir_loop
*loop
= nir_cf_node_as_loop(cur
);
334 progress
|= dead_cf_list(&loop
->body
, &dummy
);
340 unreachable("unknown cf node type");
350 opt_dead_cf_impl(nir_function_impl
*impl
)
353 bool progress
= dead_cf_list(&impl
->body
, &dummy
);
356 nir_metadata_preserve(impl
, nir_metadata_none
);
362 nir_opt_dead_cf(nir_shader
*shader
)
364 bool progress
= false;
366 nir_foreach_function(function
, shader
)
368 progress
|= opt_dead_cf_impl(function
->impl
);