nir: Switch the arguments to nir_foreach_instr
[mesa.git] / src / compiler / nir / nir_opt_peephole_select.c
1 /*
2 * Copyright © 2014 Intel Corporation
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 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_control_flow.h"
30
31 /*
32 * Implements a small peephole optimization that looks for
33 *
34 * if (cond) {
35 * <empty>
36 * } else {
37 * <empty>
38 * }
39 * phi
40 * ...
41 * phi
42 *
43 * and replaces it with a series of selects. It can also handle the case
44 * where, instead of being empty, the if may contain some move operations
45 * whose only use is one of the following phi nodes. This happens all the
46 * time when the SSA form comes from a conditional assignment with a
47 * swizzle.
48 */
49
50 static bool
51 block_check_for_allowed_instrs(nir_block *block)
52 {
53 nir_foreach_instr(instr, block) {
54 switch (instr->type) {
55 case nir_instr_type_intrinsic: {
56 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
57
58 switch (intrin->intrinsic) {
59 case nir_intrinsic_load_var:
60 switch (intrin->variables[0]->var->data.mode) {
61 case nir_var_shader_in:
62 case nir_var_uniform:
63 break;
64
65 default:
66 return false;
67 }
68 break;
69
70 default:
71 return false;
72 }
73
74 break;
75 }
76
77 case nir_instr_type_load_const:
78 break;
79
80 case nir_instr_type_alu: {
81 nir_alu_instr *mov = nir_instr_as_alu(instr);
82 switch (mov->op) {
83 case nir_op_fmov:
84 case nir_op_imov:
85 case nir_op_fneg:
86 case nir_op_ineg:
87 case nir_op_fabs:
88 case nir_op_iabs:
89 case nir_op_vec2:
90 case nir_op_vec3:
91 case nir_op_vec4:
92 /* It must be a move-like operation. */
93 break;
94 default:
95 return false;
96 }
97
98 /* Can't handle saturate */
99 if (mov->dest.saturate)
100 return false;
101
102 /* It must be SSA */
103 if (!mov->dest.dest.is_ssa)
104 return false;
105
106 /* It cannot have any if-uses */
107 if (!list_empty(&mov->dest.dest.ssa.if_uses))
108 return false;
109
110 /* The only uses of this definition must be phi's in the successor */
111 nir_foreach_use(&mov->dest.dest.ssa, use) {
112 if (use->parent_instr->type != nir_instr_type_phi ||
113 use->parent_instr->block != block->successors[0])
114 return false;
115 }
116 break;
117 }
118
119 default:
120 return false;
121 }
122 }
123
124 return true;
125 }
126
127 static bool
128 nir_opt_peephole_select_block(nir_block *block, void *mem_ctx)
129 {
130 /* If the block is empty, then it certainly doesn't have any phi nodes,
131 * so we can skip it. This also ensures that we do an early skip on the
132 * end block of the function which isn't actually attached to the CFG.
133 */
134 if (exec_list_is_empty(&block->instr_list))
135 return false;
136
137 if (nir_cf_node_is_first(&block->cf_node))
138 return false;
139
140 nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
141 if (prev_node->type != nir_cf_node_if)
142 return false;
143
144 nir_if *if_stmt = nir_cf_node_as_if(prev_node);
145 nir_cf_node *then_node = nir_if_first_then_node(if_stmt);
146 nir_cf_node *else_node = nir_if_first_else_node(if_stmt);
147
148 /* We can only have one block in each side ... */
149 if (nir_if_last_then_node(if_stmt) != then_node ||
150 nir_if_last_else_node(if_stmt) != else_node)
151 return false;
152
153 nir_block *then_block = nir_cf_node_as_block(then_node);
154 nir_block *else_block = nir_cf_node_as_block(else_node);
155
156 /* ... and those blocks must only contain "allowed" instructions. */
157 if (!block_check_for_allowed_instrs(then_block) ||
158 !block_check_for_allowed_instrs(else_block))
159 return false;
160
161 /* At this point, we know that the previous CFG node is an if-then
162 * statement containing only moves to phi nodes in this block. We can
163 * just remove that entire CF node and replace all of the phi nodes with
164 * selects.
165 */
166
167 nir_block *prev_block = nir_cf_node_as_block(nir_cf_node_prev(prev_node));
168 assert(prev_block->cf_node.type == nir_cf_node_block);
169
170 /* First, we move the remaining instructions from the blocks to the
171 * block before. We have already guaranteed that this is safe by
172 * calling block_check_for_allowed_instrs()
173 */
174 nir_foreach_instr_safe(instr, then_block) {
175 exec_node_remove(&instr->node);
176 instr->block = prev_block;
177 exec_list_push_tail(&prev_block->instr_list, &instr->node);
178 }
179
180 nir_foreach_instr_safe(instr, else_block) {
181 exec_node_remove(&instr->node);
182 instr->block = prev_block;
183 exec_list_push_tail(&prev_block->instr_list, &instr->node);
184 }
185
186 nir_foreach_instr_safe(instr, block) {
187 if (instr->type != nir_instr_type_phi)
188 break;
189
190 nir_phi_instr *phi = nir_instr_as_phi(instr);
191 nir_alu_instr *sel = nir_alu_instr_create(mem_ctx, nir_op_bcsel);
192 nir_src_copy(&sel->src[0].src, &if_stmt->condition, sel);
193 /* Splat the condition to all channels */
194 memset(sel->src[0].swizzle, 0, sizeof sel->src[0].swizzle);
195
196 assert(exec_list_length(&phi->srcs) == 2);
197 nir_foreach_phi_src(phi, src) {
198 assert(src->pred == then_block || src->pred == else_block);
199 assert(src->src.is_ssa);
200
201 unsigned idx = src->pred == then_block ? 1 : 2;
202 nir_src_copy(&sel->src[idx].src, &src->src, sel);
203 }
204
205 nir_ssa_dest_init(&sel->instr, &sel->dest.dest,
206 phi->dest.ssa.num_components,
207 phi->dest.ssa.bit_size, phi->dest.ssa.name);
208 sel->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
209
210 nir_ssa_def_rewrite_uses(&phi->dest.ssa,
211 nir_src_for_ssa(&sel->dest.dest.ssa));
212
213 nir_instr_insert_before(&phi->instr, &sel->instr);
214 nir_instr_remove(&phi->instr);
215 }
216
217 nir_cf_node_remove(&if_stmt->cf_node);
218 return true;
219 }
220
221 static bool
222 nir_opt_peephole_select_impl(nir_function_impl *impl)
223 {
224 void *mem_ctx = ralloc_parent(impl);
225 bool progress = false;
226
227 nir_foreach_block_safe(block, impl) {
228 progress |= nir_opt_peephole_select_block(block, mem_ctx);
229 }
230
231 if (progress)
232 nir_metadata_preserve(impl, nir_metadata_none);
233
234 return progress;
235 }
236
237 bool
238 nir_opt_peephole_select(nir_shader *shader)
239 {
240 bool progress = false;
241
242 nir_foreach_function(shader, function) {
243 if (function->impl)
244 progress |= nir_opt_peephole_select_impl(function->impl);
245 }
246
247 return progress;
248 }