2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
31 deref_used_for_not_store(nir_deref_instr
*deref
)
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
)))
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.
46 if ((intrin
->intrinsic
!= nir_intrinsic_store_deref
&&
47 intrin
->intrinsic
!= nir_intrinsic_copy_deref
) ||
48 src
!= &intrin
->src
[0])
54 /* If it's used by any other instruction type (most likely a texture
55 * or call instruction), consider it used.
65 add_var_use_deref(nir_deref_instr
*deref
, struct set
*live
)
67 if (deref
->deref_type
!= nir_deref_type_var
)
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.
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
);
80 add_var_use_intrinsic(nir_intrinsic_instr
*instr
, struct set
*live
,
81 nir_variable_mode modes
)
83 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
85 switch (instr
->intrinsic
) {
86 case nir_intrinsic_copy_var
:
87 _mesa_set_add(live
, instr
->variables
[1]->var
);
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.
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
);
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
));
104 case nir_intrinsic_store_output
:
105 assert(!(modes
& nir_var_shader_out
));
109 for (unsigned i
= 0; i
< num_vars
; i
++) {
110 _mesa_set_add(live
, instr
->variables
[i
]->var
);
117 add_var_use_tex(nir_tex_instr
*instr
, struct set
*live
)
119 if (instr
->texture
!= NULL
) {
120 nir_variable
*var
= instr
->texture
->var
;
121 _mesa_set_add(live
, var
);
124 if (instr
->sampler
!= NULL
) {
125 nir_variable
*var
= instr
->sampler
->var
;
126 _mesa_set_add(live
, var
);
131 add_var_use_shader(nir_shader
*shader
, struct set
*live
, nir_variable_mode modes
)
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
);
142 case nir_instr_type_intrinsic
:
143 add_var_use_intrinsic(nir_instr_as_intrinsic(instr
), live
,
147 case nir_instr_type_tex
:
148 add_var_use_tex(nir_instr_as_tex(instr
), live
);
161 remove_dead_var_writes(nir_shader
*shader
, struct set
*live
)
163 nir_foreach_function(function
, shader
) {
167 nir_foreach_block(block
, function
->impl
) {
168 nir_foreach_instr_safe(instr
, block
) {
169 if (instr
->type
!= nir_instr_type_intrinsic
)
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
)
177 /* Stores to dead variables need to be removed */
178 if (intrin
->variables
[0]->var
->data
.mode
== 0)
179 nir_instr_remove(instr
);
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
);
189 nir_variable_mode parent_mode
;
190 if (deref
->deref_type
== nir_deref_type_var
)
191 parent_mode
= deref
->var
->data
.mode
;
193 parent_mode
= nir_deref_instr_parent(deref
)->mode
;
195 /* If the parent mode is 0, then it references a dead variable.
196 * Flag this deref as dead and remove it.
198 if (parent_mode
== 0) {
200 nir_instr_remove(&deref
->instr
);
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
)
211 if (nir_src_as_deref(intrin
->src
[0])->mode
== 0)
212 nir_instr_remove(instr
);
217 break; /* Nothing to do */
225 remove_dead_vars(struct exec_list
*var_list
, struct set
*live
)
227 bool progress
= false;
229 foreach_list_typed_safe(nir_variable
, var
, node
, var_list
) {
230 struct set_entry
*entry
= _mesa_set_search(live
, var
);
232 /* Mark this variable as used by setting the mode to 0 */
234 exec_node_remove(&var
->node
);
243 nir_remove_dead_variables(nir_shader
*shader
, nir_variable_mode modes
)
245 bool progress
= false;
247 _mesa_set_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
249 add_var_use_shader(shader
, live
, modes
);
251 if (modes
& nir_var_uniform
)
252 progress
= remove_dead_vars(&shader
->uniforms
, live
) || progress
;
254 if (modes
& nir_var_shader_in
)
255 progress
= remove_dead_vars(&shader
->inputs
, live
) || progress
;
257 if (modes
& nir_var_shader_out
)
258 progress
= remove_dead_vars(&shader
->outputs
, live
) || progress
;
260 if (modes
& nir_var_global
)
261 progress
= remove_dead_vars(&shader
->globals
, live
) || progress
;
263 if (modes
& nir_var_system_value
)
264 progress
= remove_dead_vars(&shader
->system_values
, live
) || progress
;
266 if (modes
& nir_var_shared
)
267 progress
= remove_dead_vars(&shader
->shared
, live
) || progress
;
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
))
279 remove_dead_var_writes(shader
, live
);
281 nir_foreach_function(function
, shader
) {
282 if (function
->impl
) {
283 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
284 nir_metadata_dominance
);
289 _mesa_set_destroy(live
, NULL
);