41dddd9387f80adc3671cc07b83b8ff2f893b7e2
[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 * or call 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_tex(nir_tex_instr *instr, struct set *live)
118 {
119 if (instr->texture != NULL) {
120 nir_variable *var = instr->texture->var;
121 _mesa_set_add(live, var);
122 }
123
124 if (instr->sampler != NULL) {
125 nir_variable *var = instr->sampler->var;
126 _mesa_set_add(live, var);
127 }
128 }
129
130 static void
131 add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes)
132 {
133 nir_foreach_function(function, shader) {
134 if (function->impl) {
135 nir_foreach_block(block, function->impl) {
136 nir_foreach_instr(instr, block) {
137 switch(instr->type) {
138 case nir_instr_type_deref:
139 add_var_use_deref(nir_instr_as_deref(instr), live);
140 break;
141
142 case nir_instr_type_intrinsic:
143 add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live,
144 modes);
145 break;
146
147 case nir_instr_type_tex:
148 add_var_use_tex(nir_instr_as_tex(instr), live);
149 break;
150
151 default:
152 break;
153 }
154 }
155 }
156 }
157 }
158 }
159
160 static void
161 remove_dead_var_writes(nir_shader *shader, struct set *live)
162 {
163 nir_foreach_function(function, shader) {
164 if (!function->impl)
165 continue;
166
167 nir_foreach_block(block, function->impl) {
168 nir_foreach_instr_safe(instr, block) {
169 if (instr->type != nir_instr_type_intrinsic)
170 continue;
171
172 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
173 if (intrin->intrinsic != nir_intrinsic_copy_var &&
174 intrin->intrinsic != nir_intrinsic_store_var)
175 continue;
176
177 /* Stores to dead variables need to be removed */
178 if (intrin->variables[0]->var->data.mode == 0)
179 nir_instr_remove(instr);
180 }
181 }
182
183 nir_foreach_block(block, function->impl) {
184 nir_foreach_instr_safe(instr, block) {
185 switch (instr->type) {
186 case nir_instr_type_deref: {
187 nir_deref_instr *deref = nir_instr_as_deref(instr);
188
189 nir_variable_mode parent_mode;
190 if (deref->deref_type == nir_deref_type_var)
191 parent_mode = deref->var->data.mode;
192 else
193 parent_mode = nir_deref_instr_parent(deref)->mode;
194
195 /* If the parent mode is 0, then it references a dead variable.
196 * Flag this deref as dead and remove it.
197 */
198 if (parent_mode == 0) {
199 deref->mode = 0;
200 nir_instr_remove(&deref->instr);
201 }
202 break;
203 }
204
205 case nir_instr_type_intrinsic: {
206 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
207 if (intrin->intrinsic != nir_intrinsic_copy_deref &&
208 intrin->intrinsic != nir_intrinsic_store_deref)
209 break;
210
211 if (nir_src_as_deref(intrin->src[0])->mode == 0)
212 nir_instr_remove(instr);
213 break;
214 }
215
216 default:
217 break; /* Nothing to do */
218 }
219 }
220 }
221 }
222 }
223
224 static bool
225 remove_dead_vars(struct exec_list *var_list, struct set *live)
226 {
227 bool progress = false;
228
229 foreach_list_typed_safe(nir_variable, var, node, var_list) {
230 struct set_entry *entry = _mesa_set_search(live, var);
231 if (entry == NULL) {
232 /* Mark this variable as used by setting the mode to 0 */
233 var->data.mode = 0;
234 exec_node_remove(&var->node);
235 progress = true;
236 }
237 }
238
239 return progress;
240 }
241
242 bool
243 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
244 {
245 bool progress = false;
246 struct set *live =
247 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
248
249 add_var_use_shader(shader, live, modes);
250
251 if (modes & nir_var_uniform)
252 progress = remove_dead_vars(&shader->uniforms, live) || progress;
253
254 if (modes & nir_var_shader_in)
255 progress = remove_dead_vars(&shader->inputs, live) || progress;
256
257 if (modes & nir_var_shader_out)
258 progress = remove_dead_vars(&shader->outputs, live) || progress;
259
260 if (modes & nir_var_global)
261 progress = remove_dead_vars(&shader->globals, live) || progress;
262
263 if (modes & nir_var_system_value)
264 progress = remove_dead_vars(&shader->system_values, live) || progress;
265
266 if (modes & nir_var_shared)
267 progress = remove_dead_vars(&shader->shared, live) || progress;
268
269 if (modes & nir_var_local) {
270 nir_foreach_function(function, shader) {
271 if (function->impl) {
272 if (remove_dead_vars(&function->impl->locals, live))
273 progress = true;
274 }
275 }
276 }
277
278 if (progress) {
279 remove_dead_var_writes(shader, live);
280
281 nir_foreach_function(function, shader) {
282 if (function->impl) {
283 nir_metadata_preserve(function->impl, nir_metadata_block_index |
284 nir_metadata_dominance);
285 }
286 }
287 }
288
289 _mesa_set_destroy(live, NULL);
290 return progress;
291 }