dfeaa24959087987fc883135fa5131027bd2ef56
[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_function_temp | nir_var_shader_temp | nir_var_mem_shared)) ||
75 deref_used_for_not_store(deref))
76 _mesa_set_add(live, deref->var);
77 }
78
79 static void
80 add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes)
81 {
82 nir_foreach_function(function, shader) {
83 if (function->impl) {
84 nir_foreach_block(block, function->impl) {
85 nir_foreach_instr(instr, block) {
86 if (instr->type == nir_instr_type_deref)
87 add_var_use_deref(nir_instr_as_deref(instr), live);
88 }
89 }
90 }
91 }
92 }
93
94 static void
95 remove_dead_var_writes(nir_shader *shader, struct set *live)
96 {
97 nir_foreach_function(function, shader) {
98 if (!function->impl)
99 continue;
100
101 nir_foreach_block(block, function->impl) {
102 nir_foreach_instr_safe(instr, block) {
103 switch (instr->type) {
104 case nir_instr_type_deref: {
105 nir_deref_instr *deref = nir_instr_as_deref(instr);
106 if (deref->deref_type == nir_deref_type_cast &&
107 !nir_deref_instr_parent(deref))
108 continue;
109
110 nir_variable_mode parent_mode;
111 if (deref->deref_type == nir_deref_type_var)
112 parent_mode = deref->var->data.mode;
113 else
114 parent_mode = nir_deref_instr_parent(deref)->mode;
115
116 /* If the parent mode is 0, then it references a dead variable.
117 * Flag this deref as dead and remove it.
118 */
119 if (parent_mode == 0) {
120 deref->mode = 0;
121 nir_instr_remove(&deref->instr);
122 }
123 break;
124 }
125
126 case nir_instr_type_intrinsic: {
127 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
128 if (intrin->intrinsic != nir_intrinsic_copy_deref &&
129 intrin->intrinsic != nir_intrinsic_store_deref)
130 break;
131
132 if (nir_src_as_deref(intrin->src[0])->mode == 0)
133 nir_instr_remove(instr);
134 break;
135 }
136
137 default:
138 break; /* Nothing to do */
139 }
140 }
141 }
142 }
143 }
144
145 static bool
146 remove_dead_vars(struct exec_list *var_list, nir_variable_mode modes,
147 struct set *live, bool (*can_remove_var)(nir_variable *var))
148 {
149 bool progress = false;
150
151 nir_foreach_variable_safe(var, var_list) {
152 if (!(var->data.mode & modes))
153 continue;
154
155 if (can_remove_var && !can_remove_var(var))
156 continue;
157
158 struct set_entry *entry = _mesa_set_search(live, var);
159 if (entry == NULL) {
160 /* Mark this variable as used by setting the mode to 0 */
161 var->data.mode = 0;
162 exec_node_remove(&var->node);
163 progress = true;
164 }
165 }
166
167 return progress;
168 }
169
170 bool
171 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
172 bool (*can_remove_var)(nir_variable *var))
173 {
174 bool progress = false;
175 struct set *live = _mesa_pointer_set_create(NULL);
176
177 add_var_use_shader(shader, live, modes);
178
179 if (modes & nir_var_uniform) {
180 progress = remove_dead_vars(&shader->uniforms, modes, live, can_remove_var) ||
181 progress;
182 }
183
184 if (modes & nir_var_shader_in) {
185 progress = remove_dead_vars(&shader->inputs, modes, live, can_remove_var) ||
186 progress;
187 }
188
189 if (modes & nir_var_shader_out) {
190 progress = remove_dead_vars(&shader->outputs, modes, live, can_remove_var) ||
191 progress;
192 }
193
194 if (modes & nir_var_shader_temp) {
195 progress = remove_dead_vars(&shader->globals, modes, live, can_remove_var) ||
196 progress;
197 }
198
199 if (modes & nir_var_system_value) {
200 progress = remove_dead_vars(&shader->system_values, modes, live,
201 can_remove_var) || progress;
202 }
203
204 if (modes & nir_var_mem_shared) {
205 progress = remove_dead_vars(&shader->shared, modes, live, can_remove_var) ||
206 progress;
207 }
208
209 if (modes & nir_var_function_temp) {
210 nir_foreach_function(function, shader) {
211 if (function->impl) {
212 if (remove_dead_vars(&function->impl->locals,
213 nir_var_function_temp,
214 live, can_remove_var))
215 progress = true;
216 }
217 }
218 }
219
220 nir_foreach_function(function, shader) {
221 if (!function->impl)
222 continue;
223
224 if (progress) {
225 remove_dead_var_writes(shader, live);
226 nir_metadata_preserve(function->impl, nir_metadata_block_index |
227 nir_metadata_dominance);
228 } else {
229 nir_metadata_preserve(function->impl, nir_metadata_all);
230 }
231 }
232
233 _mesa_set_destroy(live, NULL);
234 return progress;
235 }