nir/peephole_select: Rename are_all_move_to_phi and use a switch
[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
30 /*
31 * Implements a small peephole optimization that looks for
32 *
33 * if (cond) {
34 * <empty>
35 * } else {
36 * <empty>
37 * }
38 * phi
39 * ...
40 * phi
41 *
42 * and replaces it with a series of selects. It can also handle the case
43 * where, instead of being empty, the if may contain some move operations
44 * whose only use is one of the following phi nodes. This happens all the
45 * time when the SSA form comes from a conditional assignment with a
46 * swizzle.
47 */
48
49 struct peephole_select_state {
50 void *mem_ctx;
51 bool progress;
52 };
53
54 static bool
55 block_check_for_allowed_instrs(nir_block *block)
56 {
57 nir_foreach_instr(block, instr) {
58 switch (instr->type) {
59 case nir_instr_type_alu: {
60 /* It must be a move operation */
61 nir_alu_instr *mov = nir_instr_as_alu(instr);
62 if (mov->op != nir_op_fmov && mov->op != nir_op_imov)
63 return false;
64
65 /* Can't handle saturate */
66 if (mov->dest.saturate)
67 return false;
68
69 /* It must be SSA */
70 if (!mov->dest.dest.is_ssa)
71 return false;
72
73 /* It cannot have any if-uses */
74 if (mov->dest.dest.ssa.if_uses->entries != 0)
75 return false;
76
77 /* The only uses of this definition must be phi's in the successor */
78 struct set_entry *entry;
79 set_foreach(mov->dest.dest.ssa.uses, entry) {
80 const nir_instr *dest_instr = entry->key;
81 if (dest_instr->type != nir_instr_type_phi ||
82 dest_instr->block != block->successors[0])
83 return false;
84 }
85 break;
86 }
87
88 default:
89 return false;
90 }
91 }
92
93 return true;
94 }
95
96 static bool
97 nir_opt_peephole_select_block(nir_block *block, void *void_state)
98 {
99 struct peephole_select_state *state = void_state;
100
101 /* If the block is empty, then it certainly doesn't have any phi nodes,
102 * so we can skip it. This also ensures that we do an early skip on the
103 * end block of the function which isn't actually attached to the CFG.
104 */
105 if (exec_list_is_empty(&block->instr_list))
106 return true;
107
108 if (nir_cf_node_is_first(&block->cf_node))
109 return true;
110
111 nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
112 if (prev_node->type != nir_cf_node_if)
113 return true;
114
115 nir_if *if_stmt = nir_cf_node_as_if(prev_node);
116 nir_cf_node *then_node = nir_if_first_then_node(if_stmt);
117 nir_cf_node *else_node = nir_if_first_else_node(if_stmt);
118
119 /* We can only have one block in each side ... */
120 if (nir_if_last_then_node(if_stmt) != then_node ||
121 nir_if_last_else_node(if_stmt) != else_node)
122 return true;
123
124 nir_block *then_block = nir_cf_node_as_block(then_node);
125 nir_block *else_block = nir_cf_node_as_block(else_node);
126
127 /* ... and those blocks must only contain "allowed" instructions. */
128 if (!block_check_for_allowed_instrs(then_block) ||
129 !block_check_for_allowed_instrs(else_block))
130 return true;
131
132 /* At this point, we know that the previous CFG node is an if-then
133 * statement containing only moves to phi nodes in this block. We can
134 * just remove that entire CF node and replace all of the phi nodes with
135 * selects.
136 */
137
138 nir_foreach_instr_safe(block, instr) {
139 if (instr->type != nir_instr_type_phi)
140 break;
141
142 nir_phi_instr *phi = nir_instr_as_phi(instr);
143 nir_alu_instr *sel = nir_alu_instr_create(state->mem_ctx, nir_op_bcsel);
144 nir_src_copy(&sel->src[0].src, &if_stmt->condition, state->mem_ctx);
145 /* Splat the condition to all channels */
146 memset(sel->src[0].swizzle, 0, sizeof sel->src[0].swizzle);
147
148 assert(exec_list_length(&phi->srcs) == 2);
149 nir_foreach_phi_src(phi, src) {
150 assert(src->pred == then_block || src->pred == else_block);
151 assert(src->src.is_ssa);
152
153 unsigned idx = src->pred == then_block ? 1 : 2;
154
155 if (src->src.ssa->parent_instr->block == src->pred) {
156 /* We already know that this instruction must be a move with
157 * this phi's in this block as its only users.
158 */
159 nir_alu_instr *mov = nir_instr_as_alu(src->src.ssa->parent_instr);
160 assert(mov->instr.type == nir_instr_type_alu);
161 assert(mov->op == nir_op_fmov || mov->op == nir_op_imov);
162
163 nir_alu_src_copy(&sel->src[idx], &mov->src[0], state->mem_ctx);
164 } else {
165 nir_src_copy(&sel->src[idx].src, &src->src, state->mem_ctx);
166 }
167 }
168
169 nir_ssa_dest_init(&sel->instr, &sel->dest.dest,
170 phi->dest.ssa.num_components, phi->dest.ssa.name);
171 sel->dest.write_mask = (1 << phi->dest.ssa.num_components) - 1;
172
173 nir_ssa_def_rewrite_uses(&phi->dest.ssa,
174 nir_src_for_ssa(&sel->dest.dest.ssa),
175 state->mem_ctx);
176
177 nir_instr_insert_before(&phi->instr, &sel->instr);
178 nir_instr_remove(&phi->instr);
179 }
180
181 nir_cf_node_remove(&if_stmt->cf_node);
182 state->progress = true;
183
184 return true;
185 }
186
187 static bool
188 nir_opt_peephole_select_impl(nir_function_impl *impl)
189 {
190 struct peephole_select_state state;
191
192 state.mem_ctx = ralloc_parent(impl);
193 state.progress = false;
194
195 nir_foreach_block(impl, nir_opt_peephole_select_block, &state);
196
197 if (state.progress)
198 nir_metadata_preserve(impl, nir_metadata_none);
199
200 return state.progress;
201 }
202
203 bool
204 nir_opt_peephole_select(nir_shader *shader)
205 {
206 bool progress = false;
207
208 nir_foreach_overload(shader, overload) {
209 if (overload->impl)
210 progress |= nir_opt_peephole_select_impl(overload->impl);
211 }
212
213 return progress;
214 }