util/hash_set: Rework the API to know about hashing
[mesa.git] / src / glsl / nir / nir_opt_copy_propagate.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 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include <main/imports.h>
30
31 /**
32 * SSA-based copy propagation
33 */
34
35 static bool is_move(nir_alu_instr *instr)
36 {
37 if (instr->op != nir_op_fmov &&
38 instr->op != nir_op_imov)
39 return false;
40
41 if (instr->dest.saturate)
42 return false;
43
44 /* we handle modifiers in a separate pass */
45
46 if (instr->src[0].abs || instr->src[0].negate)
47 return false;
48
49 if (!instr->src[0].src.is_ssa)
50 return false;
51
52 return true;
53
54 }
55
56 static bool
57 is_swizzleless_move(nir_alu_instr *instr)
58 {
59 if (!is_move(instr))
60 return false;
61
62 for (unsigned i = 0; i < 4; i++) {
63 if (!((instr->dest.write_mask >> i) & 1))
64 break;
65 if (instr->src[0].swizzle[i] != i)
66 return false;
67 }
68
69 return true;
70 }
71
72 static bool is_vec(nir_alu_instr *instr)
73 {
74 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
75 if (!instr->src[i].src.is_ssa)
76 return false;
77
78 return instr->op == nir_op_vec2 ||
79 instr->op == nir_op_vec3 ||
80 instr->op == nir_op_vec4;
81 }
82
83 typedef struct {
84 nir_ssa_def *def;
85 bool found;
86 } search_def_state;
87
88 static bool
89 search_def(nir_src *src, void *_state)
90 {
91 search_def_state *state = (search_def_state *) _state;
92
93 if (src->is_ssa && src->ssa == state->def)
94 state->found = true;
95
96 return true;
97 }
98
99 static void
100 rewrite_src_instr(nir_src *src, nir_ssa_def *new_def, nir_instr *parent_instr)
101 {
102 nir_ssa_def *old_def = src->ssa;
103
104 src->ssa = new_def;
105
106 /*
107 * The instruction could still use the old definition in one of its other
108 * sources, so only remove the instruction from the uses if there are no
109 * more uses left.
110 */
111
112 search_def_state search_state;
113 search_state.def = old_def;
114 search_state.found = false;
115 nir_foreach_src(parent_instr, search_def, &search_state);
116 if (!search_state.found) {
117 struct set_entry *entry = _mesa_set_search(old_def->uses, parent_instr);
118 assert(entry);
119 _mesa_set_remove(old_def->uses, entry);
120 }
121
122 _mesa_set_add(new_def->uses, parent_instr);
123 }
124
125 static void
126 rewrite_src_if(nir_if *if_stmt, nir_ssa_def *new_def)
127 {
128 nir_ssa_def *old_def = if_stmt->condition.ssa;
129
130 if_stmt->condition.ssa = new_def;
131
132 struct set_entry *entry = _mesa_set_search(old_def->if_uses, if_stmt);
133 assert(entry);
134 _mesa_set_remove(old_def->if_uses, entry);
135
136 _mesa_set_add(new_def->if_uses, if_stmt);
137 }
138
139 static bool
140 copy_prop_src(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
141 {
142 if (!src->is_ssa) {
143 if (src->reg.indirect)
144 return copy_prop_src(src, parent_instr, parent_if);
145 return false;
146 }
147
148 nir_instr *src_instr = src->ssa->parent_instr;
149 if (src_instr->type != nir_instr_type_alu)
150 return false;
151
152 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
153 if (!is_swizzleless_move(alu_instr))
154 return false;
155
156 /* Don't let copy propagation land us with a phi that has more
157 * components in its source than it has in its destination. That badly
158 * messes up out-of-ssa.
159 */
160 if (parent_instr && parent_instr->type == nir_instr_type_phi) {
161 nir_phi_instr *phi = nir_instr_as_phi(parent_instr);
162 assert(phi->dest.is_ssa);
163 if (phi->dest.ssa.num_components !=
164 alu_instr->src[0].src.ssa->num_components)
165 return false;
166 }
167
168 if (parent_instr)
169 rewrite_src_instr(src, alu_instr->src[0].src.ssa, parent_instr);
170 else
171 rewrite_src_if(parent_if, alu_instr->src[0].src.ssa);
172
173 return true;
174 }
175
176 static bool
177 copy_prop_alu_src(nir_alu_instr *parent_alu_instr, unsigned index)
178 {
179 nir_alu_src *src = &parent_alu_instr->src[index];
180 if (!src->src.is_ssa) {
181 if (src->src.reg.indirect)
182 return copy_prop_src(src->src.reg.indirect, &parent_alu_instr->instr,
183 NULL);
184 return false;
185 }
186
187 nir_instr *src_instr = src->src.ssa->parent_instr;
188 if (src_instr->type != nir_instr_type_alu)
189 return false;
190
191 nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
192 if (!is_move(alu_instr) && !is_vec(alu_instr))
193 return false;
194
195 nir_ssa_def *def;
196 unsigned new_swizzle[4] = {0, 0, 0, 0};
197
198 if (alu_instr->op == nir_op_fmov ||
199 alu_instr->op == nir_op_imov) {
200 for (unsigned i = 0; i < 4; i++)
201 new_swizzle[i] = alu_instr->src[0].swizzle[src->swizzle[i]];
202 def = alu_instr->src[0].src.ssa;
203 } else {
204 def = NULL;
205
206 for (unsigned i = 0; i < 4; i++) {
207 if (!nir_alu_instr_channel_used(parent_alu_instr, index, i))
208 continue;
209
210 nir_ssa_def *new_def = alu_instr->src[src->swizzle[i]].src.ssa;
211 if (def == NULL)
212 def = new_def;
213 else {
214 if (def != new_def)
215 return false;
216 }
217 new_swizzle[i] = alu_instr->src[src->swizzle[i]].swizzle[0];
218 }
219 }
220
221 for (unsigned i = 0; i < 4; i++)
222 src->swizzle[i] = new_swizzle[i];
223
224 rewrite_src_instr(&src->src, def, &parent_alu_instr->instr);
225
226 return true;
227 }
228
229 typedef struct {
230 nir_instr *parent_instr;
231 bool progress;
232 } copy_prop_state;
233
234 static bool
235 copy_prop_src_cb(nir_src *src, void *_state)
236 {
237 copy_prop_state *state = (copy_prop_state *) _state;
238 while (copy_prop_src(src, state->parent_instr, NULL))
239 state->progress = true;
240
241 return true;
242 }
243
244 static bool
245 copy_prop_instr(nir_instr *instr)
246 {
247 if (instr->type == nir_instr_type_alu) {
248 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
249 bool progress = false;
250
251 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++)
252 while (copy_prop_alu_src(alu_instr, i))
253 progress = true;
254
255 if (!alu_instr->dest.dest.is_ssa && alu_instr->dest.dest.reg.indirect)
256 while (copy_prop_src(alu_instr->dest.dest.reg.indirect, instr, NULL))
257 progress = true;
258
259 return progress;
260 }
261
262 copy_prop_state state;
263 state.parent_instr = instr;
264 state.progress = false;
265 nir_foreach_src(instr, copy_prop_src_cb, &state);
266
267 return state.progress;
268 }
269
270 static bool
271 copy_prop_if(nir_if *if_stmt)
272 {
273 return copy_prop_src(&if_stmt->condition, NULL, if_stmt);
274 }
275
276 static bool
277 copy_prop_block(nir_block *block, void *_state)
278 {
279 bool *progress = (bool *) _state;
280
281 nir_foreach_instr(block, instr) {
282 if (copy_prop_instr(instr))
283 *progress = true;
284 }
285
286 if (block->cf_node.node.next != NULL && /* check that we aren't the end node */
287 !nir_cf_node_is_last(&block->cf_node) &&
288 nir_cf_node_next(&block->cf_node)->type == nir_cf_node_if) {
289 nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_next(&block->cf_node));
290 if (copy_prop_if(if_stmt))
291 *progress = true;
292 }
293
294 return true;
295 }
296
297 bool
298 nir_copy_prop_impl(nir_function_impl *impl)
299 {
300 bool progress = false;
301
302 nir_foreach_block(impl, copy_prop_block, &progress);
303 return progress;
304 }
305
306 bool
307 nir_copy_prop(nir_shader *shader)
308 {
309 bool progress = false;
310
311 nir_foreach_overload(shader, overload) {
312 if (overload->impl && nir_copy_prop_impl(overload->impl))
313 progress = true;
314 }
315
316 return progress;
317 }