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