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 ([email protected])
25 *
26 */
27
28 #include "nir.h"
29
30 static bool
deref_used_for_not_store(nir_deref_instr * deref)31 deref_used_for_not_store(nir_deref_instr *deref)
32 {
33 nir_foreach_use(src, &deref->def) {
34 switch (nir_src_parent_instr(src)->type) {
35 case nir_instr_type_deref:
36 if (deref_used_for_not_store(nir_instr_as_deref(nir_src_parent_instr(src))))
37 return true;
38 break;
39
40 case nir_instr_type_intrinsic: {
41 nir_intrinsic_instr *intrin =
42 nir_instr_as_intrinsic(nir_src_parent_instr(src));
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
add_var_use_deref(nir_deref_instr * deref,struct set * live)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 /* Since these local variables don't escape the shader, writing doesn't
71 * make them live. Only keep them if they are used by some intrinsic.
72 */
73 if ((deref->var->data.mode & (nir_var_function_temp |
74 nir_var_shader_temp)) &&
75 !deref_used_for_not_store(deref))
76 return;
77
78 /*
79 * Shared memory blocks (interface type) alias each other, so be
80 * conservative in that case.
81 */
82 if ((deref->var->data.mode & nir_var_mem_shared) &&
83 !glsl_type_is_interface(deref->var->type) &&
84 !deref_used_for_not_store(deref))
85 return;
86
87 nir_variable *var = deref->var;
88 do {
89 _mesa_set_add(live, var);
90 /* Also mark the chain of variables used to initialize it. */
91 var = var->pointer_initializer;
92 } while (var);
93 }
94
95 static void
add_var_use_shader(nir_shader * shader,struct set * live,nir_variable_mode modes)96 add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes)
97 {
98 nir_foreach_function_impl(impl, shader) {
99 nir_foreach_block(block, impl) {
100 nir_foreach_instr(instr, block) {
101 if (instr->type == nir_instr_type_deref)
102 add_var_use_deref(nir_instr_as_deref(instr), live);
103 }
104 }
105 }
106 }
107
108 static void
remove_dead_var_writes(nir_shader * shader)109 remove_dead_var_writes(nir_shader *shader)
110 {
111 nir_foreach_function_impl(impl, shader) {
112 nir_foreach_block(block, impl) {
113 nir_foreach_instr_safe(instr, block) {
114 switch (instr->type) {
115 case nir_instr_type_deref: {
116 nir_deref_instr *deref = nir_instr_as_deref(instr);
117 if (deref->deref_type == nir_deref_type_cast &&
118 !nir_deref_instr_parent(deref))
119 continue;
120
121 nir_variable_mode parent_modes;
122 if (deref->deref_type == nir_deref_type_var)
123 parent_modes = deref->var->data.mode;
124 else
125 parent_modes = nir_deref_instr_parent(deref)->modes;
126
127 /* If the parent mode is 0, then it references a dead variable.
128 * Flag this deref as dead and remove it.
129 */
130 if (parent_modes == 0) {
131 deref->modes = 0;
132 nir_instr_remove(&deref->instr);
133 }
134 break;
135 }
136
137 case nir_instr_type_intrinsic: {
138 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
139 if (intrin->intrinsic != nir_intrinsic_copy_deref &&
140 intrin->intrinsic != nir_intrinsic_store_deref)
141 break;
142
143 if (nir_src_as_deref(intrin->src[0])->modes == 0)
144 nir_instr_remove(instr);
145 break;
146 }
147
148 default:
149 break; /* Nothing to do */
150 }
151 }
152 }
153 }
154 }
155
156 static bool
remove_dead_vars(struct exec_list * var_list,nir_variable_mode modes,struct set * live,const nir_remove_dead_variables_options * opts)157 remove_dead_vars(struct exec_list *var_list, nir_variable_mode modes,
158 struct set *live, const nir_remove_dead_variables_options *opts)
159 {
160 bool progress = false;
161
162 nir_foreach_variable_in_list_safe(var, var_list) {
163 if (!(var->data.mode & modes))
164 continue;
165
166 if (opts && opts->can_remove_var &&
167 !opts->can_remove_var(var, opts->can_remove_var_data))
168 continue;
169
170 struct set_entry *entry = _mesa_set_search(live, var);
171 if (entry == NULL) {
172 /* Mark this variable as dead by setting the mode to 0 */
173 var->data.mode = 0;
174 exec_node_remove(&var->node);
175 progress = true;
176 }
177 }
178
179 return progress;
180 }
181
182 bool
nir_remove_dead_variables(nir_shader * shader,nir_variable_mode modes,const nir_remove_dead_variables_options * opts)183 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
184 const nir_remove_dead_variables_options *opts)
185 {
186 bool progress = false;
187 struct set *live = _mesa_pointer_set_create(NULL);
188
189 add_var_use_shader(shader, live, modes);
190
191 if (modes & ~nir_var_function_temp) {
192 progress = remove_dead_vars(&shader->variables, modes,
193 live, opts) ||
194 progress;
195 }
196
197 if (modes & nir_var_function_temp) {
198 nir_foreach_function_impl(impl, shader) {
199 if (remove_dead_vars(&impl->locals,
200 nir_var_function_temp,
201 live, opts))
202 progress = true;
203 }
204 }
205
206 _mesa_set_destroy(live, NULL);
207
208 nir_foreach_function_impl(impl, shader) {
209 if (progress) {
210 remove_dead_var_writes(shader);
211 nir_metadata_preserve(impl, nir_metadata_control_flow);
212 } else {
213 nir_metadata_preserve(impl, nir_metadata_all);
214 }
215 }
216
217 return progress;
218 }
219