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