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 unreachable
33 * code. In NIR, one way this can happen if if an if statement has a constant
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 * The other 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.:
66 remove_after_cf_node(nir_cf_node
*node
)
68 nir_cf_node
*end
= node
;
69 while (!nir_cf_node_is_last(end
))
70 end
= nir_cf_node_next(end
);
73 nir_cf_extract(&list
, nir_after_cf_node(node
), nir_after_cf_node(end
));
78 opt_constant_if(nir_if
*if_stmt
, bool condition
)
80 void *mem_ctx
= ralloc_parent(if_stmt
);
82 /* First, we need to remove any phi nodes after the if by rewriting uses to
83 * point to the correct source.
85 nir_block
*after
= nir_cf_node_as_block(nir_cf_node_next(&if_stmt
->cf_node
));
86 nir_block
*last_block
=
87 nir_cf_node_as_block(condition
? nir_if_last_then_node(if_stmt
)
88 : nir_if_last_else_node(if_stmt
));
90 nir_foreach_instr_safe(after
, instr
) {
91 if (instr
->type
!= nir_instr_type_phi
)
94 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
95 nir_ssa_def
*def
= NULL
;
96 nir_foreach_phi_src(phi
, phi_src
) {
97 if (phi_src
->pred
!= last_block
)
100 assert(phi_src
->src
.is_ssa
);
101 def
= phi_src
->src
.ssa
;
105 assert(phi
->dest
.is_ssa
);
106 nir_ssa_def_rewrite_uses(&phi
->dest
.ssa
, nir_src_for_ssa(def
), mem_ctx
);
107 nir_instr_remove(instr
);
110 /* The control flow list we're about to paste in may include a jump at the
111 * end, and in that case we have to delete the rest of the control flow
112 * list after the if since it's unreachable and the validator will balk if
116 if (!exec_list_is_empty(&last_block
->instr_list
)) {
117 nir_instr
*last_instr
= nir_block_last_instr(last_block
);
118 if (last_instr
->type
== nir_instr_type_jump
)
119 remove_after_cf_node(&if_stmt
->cf_node
);
122 /* Finally, actually paste in the then or else branch and delete the if. */
123 struct exec_list
*cf_list
= condition
? &if_stmt
->then_list
124 : &if_stmt
->else_list
;
127 nir_cf_extract(&list
, nir_before_cf_list(cf_list
),
128 nir_after_cf_list(cf_list
));
129 nir_cf_reinsert(&list
, nir_after_cf_node(&if_stmt
->cf_node
));
130 nir_cf_node_remove(&if_stmt
->cf_node
);
134 dead_cf_block(nir_block
*block
)
136 nir_if
*following_if
= nir_block_get_following_if(block
);
140 nir_const_value
*const_value
=
141 nir_src_as_const_value(following_if
->condition
);
146 opt_constant_if(following_if
, const_value
->u
[0] != 0);
151 ends_in_jump(nir_block
*block
)
153 if (exec_list_is_empty(&block
->instr_list
))
156 nir_instr
*instr
= nir_block_last_instr(block
);
157 return instr
->type
== nir_instr_type_jump
;
161 dead_cf_list(struct exec_list
*list
, bool *list_ends_in_jump
)
163 bool progress
= false;
164 *list_ends_in_jump
= false;
166 nir_cf_node
*prev
= NULL
;
168 foreach_list_typed(nir_cf_node
, cur
, node
, list
) {
170 case nir_cf_node_block
: {
171 nir_block
*block
= nir_cf_node_as_block(cur
);
172 if (dead_cf_block(block
)) {
173 /* We just deleted the if after this block, so we may have
174 * deleted the block before or after it -- which one is an
175 * implementation detail. Therefore, to recover the place we were
176 * at, we have to use the previous cf_node.
180 cur
= nir_cf_node_next(prev
);
182 cur
= exec_node_data(nir_cf_node
, exec_list_get_head(list
),
186 block
= nir_cf_node_as_block(cur
);
191 if (ends_in_jump(block
)) {
192 *list_ends_in_jump
= true;
194 if (!exec_node_is_tail_sentinel(cur
->node
.next
)) {
195 remove_after_cf_node(cur
);
203 case nir_cf_node_if
: {
204 nir_if
*if_stmt
= nir_cf_node_as_if(cur
);
205 bool then_ends_in_jump
, else_ends_in_jump
;
206 progress
|= dead_cf_list(&if_stmt
->then_list
, &then_ends_in_jump
);
207 progress
|= dead_cf_list(&if_stmt
->else_list
, &else_ends_in_jump
);
209 if (then_ends_in_jump
&& else_ends_in_jump
) {
210 *list_ends_in_jump
= true;
211 nir_block
*next
= nir_cf_node_as_block(nir_cf_node_next(cur
));
212 if (!exec_list_is_empty(&next
->instr_list
) ||
213 !exec_node_is_tail_sentinel(next
->cf_node
.node
.next
)) {
214 remove_after_cf_node(cur
);
222 case nir_cf_node_loop
: {
223 nir_loop
*loop
= nir_cf_node_as_loop(cur
);
225 progress
|= dead_cf_list(&loop
->body
, &dummy
);
231 unreachable("unknown cf node type");
241 opt_dead_cf_impl(nir_function_impl
*impl
)
244 bool progress
= dead_cf_list(&impl
->body
, &dummy
);
247 nir_metadata_preserve(impl
, nir_metadata_none
);
253 nir_opt_dead_cf(nir_shader
*shader
)
255 bool progress
= false;
257 nir_foreach_overload(shader
, overload
)
259 progress
|= opt_dead_cf_impl(overload
->impl
);