nir/lower_tex: add support to clamp texture coords
[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
33 * unreachable/dead code. In NIR, one way this can happen if if an if
34 * statement has a constant 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 * 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
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 * Finally, we also handle removing useless loops, i.e. loops with no side
64 * effects and without any definitions that are used elsewhere. This case is a
65 * little different from the first two in that the code is actually run (it
66 * just never does anything), but there are similar issues with needing to
67 * be careful with restarting after deleting the cf_node (see dead_cf_list())
68 * so this is a convenient place to remove them.
69 */
70
71 static void
72 remove_after_cf_node(nir_cf_node *node)
73 {
74 nir_cf_node *end = node;
75 while (!nir_cf_node_is_last(end))
76 end = nir_cf_node_next(end);
77
78 nir_cf_list list;
79 nir_cf_extract(&list, nir_after_cf_node(node), nir_after_cf_node(end));
80 nir_cf_delete(&list);
81 }
82
83 static void
84 opt_constant_if(nir_if *if_stmt, bool condition)
85 {
86 /* First, we need to remove any phi nodes after the if by rewriting uses to
87 * point to the correct source.
88 */
89 nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node));
90 nir_block *last_block =
91 nir_cf_node_as_block(condition ? nir_if_last_then_node(if_stmt)
92 : nir_if_last_else_node(if_stmt));
93
94 nir_foreach_instr_safe(after, instr) {
95 if (instr->type != nir_instr_type_phi)
96 break;
97
98 nir_phi_instr *phi = nir_instr_as_phi(instr);
99 nir_ssa_def *def = NULL;
100 nir_foreach_phi_src(phi, phi_src) {
101 if (phi_src->pred != last_block)
102 continue;
103
104 assert(phi_src->src.is_ssa);
105 def = phi_src->src.ssa;
106 }
107
108 assert(def);
109 assert(phi->dest.is_ssa);
110 nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_src_for_ssa(def));
111 nir_instr_remove(instr);
112 }
113
114 /* The control flow list we're about to paste in may include a jump at the
115 * end, and in that case we have to delete the rest of the control flow
116 * list after the if since it's unreachable and the validator will balk if
117 * we don't.
118 */
119
120 if (!exec_list_is_empty(&last_block->instr_list)) {
121 nir_instr *last_instr = nir_block_last_instr(last_block);
122 if (last_instr->type == nir_instr_type_jump)
123 remove_after_cf_node(&if_stmt->cf_node);
124 }
125
126 /* Finally, actually paste in the then or else branch and delete the if. */
127 struct exec_list *cf_list = condition ? &if_stmt->then_list
128 : &if_stmt->else_list;
129
130 nir_cf_list list;
131 nir_cf_extract(&list, nir_before_cf_list(cf_list),
132 nir_after_cf_list(cf_list));
133 nir_cf_reinsert(&list, nir_after_cf_node(&if_stmt->cf_node));
134 nir_cf_node_remove(&if_stmt->cf_node);
135 }
136
137 static bool
138 block_has_no_side_effects(nir_block *block, void *state)
139 {
140 (void) state;
141
142 nir_foreach_instr(block, instr) {
143 if (instr->type == nir_instr_type_call)
144 return false;
145
146 /* Return instructions can cause us to skip over other side-effecting
147 * instructions after the loop, so consider them to have side effects
148 * here.
149 */
150
151 if (instr->type == nir_instr_type_jump &&
152 nir_instr_as_jump(instr)->type == nir_jump_return)
153 return false;
154
155 if (instr->type != nir_instr_type_intrinsic)
156 continue;
157
158 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
159 if (!nir_intrinsic_infos[intrin->intrinsic].flags &
160 NIR_INTRINSIC_CAN_ELIMINATE)
161 return false;
162 }
163
164 return true;
165 }
166
167 static bool
168 def_not_live_out(nir_ssa_def *def, void *state)
169 {
170 nir_block *after = state;
171
172 return !BITSET_TEST(after->live_in, def->live_index);
173 }
174
175 /*
176 * Test if a loop is dead. A loop is dead if:
177 *
178 * 1) It has no side effects (i.e. intrinsics which could possibly affect the
179 * state of the program aside from producing an SSA value, indicated by a lack
180 * of NIR_INTRINSIC_CAN_ELIMINATE).
181 *
182 * 2) It has no phi nodes after it, since those indicate values inside the
183 * loop being used after the loop.
184 *
185 * 3) If there are no phi nodes after the loop, then the only way a value
186 * defined inside the loop can be used outside the loop is if its definition
187 * dominates the block after the loop. If none of the definitions that
188 * dominate the loop exit are used outside the loop, then the loop is dead
189 * and it can be deleted.
190 */
191
192 static bool
193 loop_is_dead(nir_loop *loop)
194 {
195 nir_block *before = nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node));
196 nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node));
197
198 if (!exec_list_is_empty(&after->instr_list) &&
199 nir_block_first_instr(after)->type == nir_instr_type_phi)
200 return false;
201
202 if (!nir_foreach_block_in_cf_node(&loop->cf_node, block_has_no_side_effects,
203 NULL))
204 return false;
205
206 for (nir_block *cur = after->imm_dom; cur != before; cur = cur->imm_dom) {
207 nir_foreach_instr(cur, instr) {
208 if (!nir_foreach_ssa_def(instr, def_not_live_out, after))
209 return false;
210 }
211 }
212
213 return true;
214 }
215
216 static bool
217 dead_cf_block(nir_block *block)
218 {
219 nir_if *following_if = nir_block_get_following_if(block);
220 if (following_if) {
221 nir_const_value *const_value =
222 nir_src_as_const_value(following_if->condition);
223
224 if (!const_value)
225 return false;
226
227 opt_constant_if(following_if, const_value->u[0] != 0);
228 return true;
229 }
230
231 nir_loop *following_loop = nir_block_get_following_loop(block);
232 if (!following_loop)
233 return false;
234
235 if (!loop_is_dead(following_loop))
236 return false;
237
238 nir_cf_node_remove(&following_loop->cf_node);
239 return true;
240 }
241
242 static bool
243 ends_in_jump(nir_block *block)
244 {
245 if (exec_list_is_empty(&block->instr_list))
246 return false;
247
248 nir_instr *instr = nir_block_last_instr(block);
249 return instr->type == nir_instr_type_jump;
250 }
251
252 static bool
253 dead_cf_list(struct exec_list *list, bool *list_ends_in_jump)
254 {
255 bool progress = false;
256 *list_ends_in_jump = false;
257
258 nir_cf_node *prev = NULL;
259
260 foreach_list_typed(nir_cf_node, cur, node, list) {
261 switch (cur->type) {
262 case nir_cf_node_block: {
263 nir_block *block = nir_cf_node_as_block(cur);
264 if (dead_cf_block(block)) {
265 /* We just deleted the if or loop after this block, so we may have
266 * deleted the block before or after it -- which one is an
267 * implementation detail. Therefore, to recover the place we were
268 * at, we have to use the previous cf_node.
269 */
270
271 if (prev) {
272 cur = nir_cf_node_next(prev);
273 } else {
274 cur = exec_node_data(nir_cf_node, exec_list_get_head(list),
275 node);
276 }
277
278 block = nir_cf_node_as_block(cur);
279
280 progress = true;
281 }
282
283 if (ends_in_jump(block)) {
284 *list_ends_in_jump = true;
285
286 if (!exec_node_is_tail_sentinel(cur->node.next)) {
287 remove_after_cf_node(cur);
288 return true;
289 }
290 }
291
292 break;
293 }
294
295 case nir_cf_node_if: {
296 nir_if *if_stmt = nir_cf_node_as_if(cur);
297 bool then_ends_in_jump, else_ends_in_jump;
298 progress |= dead_cf_list(&if_stmt->then_list, &then_ends_in_jump);
299 progress |= dead_cf_list(&if_stmt->else_list, &else_ends_in_jump);
300
301 if (then_ends_in_jump && else_ends_in_jump) {
302 *list_ends_in_jump = true;
303 nir_block *next = nir_cf_node_as_block(nir_cf_node_next(cur));
304 if (!exec_list_is_empty(&next->instr_list) ||
305 !exec_node_is_tail_sentinel(next->cf_node.node.next)) {
306 remove_after_cf_node(cur);
307 return true;
308 }
309 }
310
311 break;
312 }
313
314 case nir_cf_node_loop: {
315 nir_loop *loop = nir_cf_node_as_loop(cur);
316 bool dummy;
317 progress |= dead_cf_list(&loop->body, &dummy);
318
319 break;
320 }
321
322 default:
323 unreachable("unknown cf node type");
324 }
325
326 prev = cur;
327 }
328
329 return progress;
330 }
331
332 static bool
333 opt_dead_cf_impl(nir_function_impl *impl)
334 {
335 nir_metadata_require(impl, nir_metadata_live_variables |
336 nir_metadata_dominance);
337
338 bool dummy;
339 bool progress = dead_cf_list(&impl->body, &dummy);
340
341 if (progress)
342 nir_metadata_preserve(impl, nir_metadata_none);
343
344 return progress;
345 }
346
347 bool
348 nir_opt_dead_cf(nir_shader *shader)
349 {
350 bool progress = false;
351
352 nir_foreach_overload(shader, overload)
353 if (overload->impl)
354 progress |= opt_dead_cf_impl(overload->impl);
355
356 return progress;
357 }