nir/dead_cf: delete code that's unreachable due to jumps
[mesa.git] / src / glsl / nir / nir_opt_dead_cf.c
1 /*
2 * Copyright © 2014 Connor Abbott
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_control_flow.h"
30
31 /*
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
34 * condition:
35 *
36 * if (true) {
37 * ...
38 * }
39 *
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.
43 *
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
46 * something like:
47 *
48 * if (true) {
49 * ...
50 * break;
51 * }
52 * ...
53 *
54 * We also consider the case where both branches of an if end in a jump, e.g.:
55 *
56 * if (...) {
57 * break;
58 * } else {
59 * continue;
60 * }
61 * ...
62 *
63 */
64
65 static void
66 remove_after_cf_node(nir_cf_node *node)
67 {
68 nir_cf_node *end = node;
69 while (!nir_cf_node_is_last(end))
70 end = nir_cf_node_next(end);
71
72 nir_cf_list list;
73 nir_cf_extract(&list, nir_after_cf_node(node), nir_after_cf_node(end));
74 nir_cf_delete(&list);
75 }
76
77 static void
78 opt_constant_if(nir_if *if_stmt, bool condition)
79 {
80 void *mem_ctx = ralloc_parent(if_stmt);
81
82 /* First, we need to remove any phi nodes after the if by rewriting uses to
83 * point to the correct source.
84 */
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));
89
90 nir_foreach_instr_safe(after, instr) {
91 if (instr->type != nir_instr_type_phi)
92 break;
93
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)
98 continue;
99
100 assert(phi_src->src.is_ssa);
101 def = phi_src->src.ssa;
102 }
103
104 assert(def);
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);
108 }
109
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
113 * we don't.
114 */
115
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);
120 }
121
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;
125
126 nir_cf_list 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);
131 }
132
133 static bool
134 dead_cf_block(nir_block *block)
135 {
136 nir_if *following_if = nir_block_get_following_if(block);
137 if (!following_if)
138 return false;
139
140 nir_const_value *const_value =
141 nir_src_as_const_value(following_if->condition);
142
143 if (!const_value)
144 return false;
145
146 opt_constant_if(following_if, const_value->u[0] != 0);
147 return true;
148 }
149
150 static bool
151 ends_in_jump(nir_block *block)
152 {
153 if (exec_list_is_empty(&block->instr_list))
154 return false;
155
156 nir_instr *instr = nir_block_last_instr(block);
157 return instr->type == nir_instr_type_jump;
158 }
159
160 static bool
161 dead_cf_list(struct exec_list *list, bool *list_ends_in_jump)
162 {
163 bool progress = false;
164 *list_ends_in_jump = false;
165
166 nir_cf_node *prev = NULL;
167
168 foreach_list_typed(nir_cf_node, cur, node, list) {
169 switch (cur->type) {
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.
177 */
178
179 if (prev) {
180 cur = nir_cf_node_next(prev);
181 } else {
182 cur = exec_node_data(nir_cf_node, exec_list_get_head(list),
183 node);
184 }
185
186 block = nir_cf_node_as_block(cur);
187
188 progress = true;
189 }
190
191 if (ends_in_jump(block)) {
192 *list_ends_in_jump = true;
193
194 if (!exec_node_is_tail_sentinel(cur->node.next)) {
195 remove_after_cf_node(cur);
196 return true;
197 }
198 }
199
200 break;
201 }
202
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);
208
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);
215 return true;
216 }
217 }
218
219 break;
220 }
221
222 case nir_cf_node_loop: {
223 nir_loop *loop = nir_cf_node_as_loop(cur);
224 bool dummy;
225 progress |= dead_cf_list(&loop->body, &dummy);
226
227 break;
228 }
229
230 default:
231 unreachable("unknown cf node type");
232 }
233
234 prev = cur;
235 }
236
237 return progress;
238 }
239
240 static bool
241 opt_dead_cf_impl(nir_function_impl *impl)
242 {
243 bool dummy;
244 bool progress = dead_cf_list(&impl->body, &dummy);
245
246 if (progress)
247 nir_metadata_preserve(impl, nir_metadata_none);
248
249 return progress;
250 }
251
252 bool
253 nir_opt_dead_cf(nir_shader *shader)
254 {
255 bool progress = false;
256
257 nir_foreach_overload(shader, overload)
258 if (overload->impl)
259 progress |= opt_dead_cf_impl(overload->impl);
260
261 return progress;
262 }