nir: Support deref instructions in remove_dead_variables
[mesa.git] / src / compiler / nir / nir_remove_dead_variables.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
30 static bool
31 deref_used_for_not_store(nir_deref_instr *deref)
32 {
33 nir_foreach_use(src, &deref->dest.ssa) {
34 switch (src->parent_instr->type) {
35 case nir_instr_type_deref:
36 if (deref_used_for_not_store(nir_instr_as_deref(src->parent_instr)))
37 return true;
38 break;
39
40 case nir_instr_type_intrinsic: {
41 nir_intrinsic_instr *intrin =
42 nir_instr_as_intrinsic(src->parent_instr);
43 /* The first source of copy and store intrinsics is the deref to
44 * write. Don't record those.
45 */
46 if ((intrin->intrinsic != nir_intrinsic_store_deref &&
47 intrin->intrinsic != nir_intrinsic_copy_deref) ||
48 src != &intrin->src[0])
49 return true;
50 break;
51 }
52
53 default:
54 /* If it's used by any other instruction type (most likely a texture
55 * instruction), consider it used.
56 */
57 return true;
58 }
59 }
60
61 return false;
62 }
63
64 static void
65 add_var_use_deref(nir_deref_instr *deref, struct set *live)
66 {
67 if (deref->deref_type != nir_deref_type_var)
68 return;
69
70 /* If it's not a local that never escapes the shader, then any access at
71 * all means we need to keep it alive.
72 */
73 assert(deref->mode == deref->var->data.mode);
74 if (!(deref->mode & (nir_var_local | nir_var_global | nir_var_shared)) ||
75 deref_used_for_not_store(deref))
76 _mesa_set_add(live, deref->var);
77 }
78
79 static void
80 add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live,
81 nir_variable_mode modes)
82 {
83 unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
84
85 switch (instr->intrinsic) {
86 case nir_intrinsic_copy_var:
87 _mesa_set_add(live, instr->variables[1]->var);
88 /* Fall through */
89 case nir_intrinsic_store_var: {
90 /* The first source in both copy_var and store_var is the destination.
91 * If the variable is a local that never escapes the shader, then we
92 * don't mark it as live for just a store.
93 */
94 nir_variable_mode mode = instr->variables[0]->var->data.mode;
95 if (!(mode & (nir_var_local | nir_var_global | nir_var_shared)))
96 _mesa_set_add(live, instr->variables[0]->var);
97 break;
98 }
99
100 /* This pass can't be used on I/O variables after they've been lowered. */
101 case nir_intrinsic_load_input:
102 assert(!(modes & nir_var_shader_in));
103 break;
104 case nir_intrinsic_store_output:
105 assert(!(modes & nir_var_shader_out));
106 break;
107
108 default:
109 for (unsigned i = 0; i < num_vars; i++) {
110 _mesa_set_add(live, instr->variables[i]->var);
111 }
112 break;
113 }
114 }
115
116 static void
117 add_var_use_call(nir_call_instr *instr, struct set *live)
118 {
119 if (instr->return_deref != NULL) {
120 nir_variable *var = instr->return_deref->var;
121 _mesa_set_add(live, var);
122 }
123
124 for (unsigned i = 0; i < instr->num_params; i++) {
125 nir_variable *var = instr->params[i]->var;
126 _mesa_set_add(live, var);
127 }
128 }
129
130 static void
131 add_var_use_tex(nir_tex_instr *instr, struct set *live)
132 {
133 if (instr->texture != NULL) {
134 nir_variable *var = instr->texture->var;
135 _mesa_set_add(live, var);
136 }
137
138 if (instr->sampler != NULL) {
139 nir_variable *var = instr->sampler->var;
140 _mesa_set_add(live, var);
141 }
142 }
143
144 static void
145 add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes)
146 {
147 nir_foreach_function(function, shader) {
148 if (function->impl) {
149 nir_foreach_block(block, function->impl) {
150 nir_foreach_instr(instr, block) {
151 switch(instr->type) {
152 case nir_instr_type_deref:
153 add_var_use_deref(nir_instr_as_deref(instr), live);
154 break;
155
156 case nir_instr_type_intrinsic:
157 add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live,
158 modes);
159 break;
160
161 case nir_instr_type_call:
162 add_var_use_call(nir_instr_as_call(instr), live);
163 break;
164
165 case nir_instr_type_tex:
166 add_var_use_tex(nir_instr_as_tex(instr), live);
167 break;
168
169 default:
170 break;
171 }
172 }
173 }
174 }
175 }
176 }
177
178 static void
179 remove_dead_var_writes(nir_shader *shader, struct set *live)
180 {
181 nir_foreach_function(function, shader) {
182 if (!function->impl)
183 continue;
184
185 nir_foreach_block(block, function->impl) {
186 nir_foreach_instr_safe(instr, block) {
187 if (instr->type != nir_instr_type_intrinsic)
188 continue;
189
190 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
191 if (intrin->intrinsic != nir_intrinsic_copy_var &&
192 intrin->intrinsic != nir_intrinsic_store_var)
193 continue;
194
195 /* Stores to dead variables need to be removed */
196 if (intrin->variables[0]->var->data.mode == 0)
197 nir_instr_remove(instr);
198 }
199 }
200
201 nir_foreach_block(block, function->impl) {
202 nir_foreach_instr_safe(instr, block) {
203 switch (instr->type) {
204 case nir_instr_type_deref: {
205 nir_deref_instr *deref = nir_instr_as_deref(instr);
206
207 nir_variable_mode parent_mode;
208 if (deref->deref_type == nir_deref_type_var)
209 parent_mode = deref->var->data.mode;
210 else
211 parent_mode = nir_deref_instr_parent(deref)->mode;
212
213 /* If the parent mode is 0, then it references a dead variable.
214 * Flag this deref as dead and remove it.
215 */
216 if (parent_mode == 0) {
217 deref->mode = 0;
218 nir_instr_remove(&deref->instr);
219 }
220 break;
221 }
222
223 case nir_instr_type_intrinsic: {
224 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
225 if (intrin->intrinsic != nir_intrinsic_copy_deref &&
226 intrin->intrinsic != nir_intrinsic_store_deref)
227 break;
228
229 if (nir_src_as_deref(intrin->src[0])->mode == 0)
230 nir_instr_remove(instr);
231 break;
232 }
233
234 default:
235 break; /* Nothing to do */
236 }
237 }
238 }
239 }
240 }
241
242 static bool
243 remove_dead_vars(struct exec_list *var_list, struct set *live)
244 {
245 bool progress = false;
246
247 foreach_list_typed_safe(nir_variable, var, node, var_list) {
248 struct set_entry *entry = _mesa_set_search(live, var);
249 if (entry == NULL) {
250 /* Mark this variable as used by setting the mode to 0 */
251 var->data.mode = 0;
252 exec_node_remove(&var->node);
253 progress = true;
254 }
255 }
256
257 return progress;
258 }
259
260 bool
261 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
262 {
263 bool progress = false;
264 struct set *live =
265 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
266
267 add_var_use_shader(shader, live, modes);
268
269 if (modes & nir_var_uniform)
270 progress = remove_dead_vars(&shader->uniforms, live) || progress;
271
272 if (modes & nir_var_shader_in)
273 progress = remove_dead_vars(&shader->inputs, live) || progress;
274
275 if (modes & nir_var_shader_out)
276 progress = remove_dead_vars(&shader->outputs, live) || progress;
277
278 if (modes & nir_var_global)
279 progress = remove_dead_vars(&shader->globals, live) || progress;
280
281 if (modes & nir_var_system_value)
282 progress = remove_dead_vars(&shader->system_values, live) || progress;
283
284 if (modes & nir_var_shared)
285 progress = remove_dead_vars(&shader->shared, live) || progress;
286
287 if (modes & nir_var_local) {
288 nir_foreach_function(function, shader) {
289 if (function->impl) {
290 if (remove_dead_vars(&function->impl->locals, live))
291 progress = true;
292 }
293 }
294 }
295
296 if (progress) {
297 remove_dead_var_writes(shader, live);
298
299 nir_foreach_function(function, shader) {
300 if (function->impl) {
301 nir_metadata_preserve(function->impl, nir_metadata_block_index |
302 nir_metadata_dominance);
303 }
304 }
305 }
306
307 _mesa_set_destroy(live, NULL);
308 return progress;
309 }