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 * 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_call(nir_call_instr
*instr
, struct set
*live
)
119 if (instr
->return_deref
!= NULL
) {
120 nir_variable
*var
= instr
->return_deref
->var
;
121 _mesa_set_add(live
, var
);
124 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
125 nir_variable
*var
= instr
->params
[i
]->var
;
126 _mesa_set_add(live
, var
);
131 add_var_use_tex(nir_tex_instr
*instr
, struct set
*live
)
133 if (instr
->texture
!= NULL
) {
134 nir_variable
*var
= instr
->texture
->var
;
135 _mesa_set_add(live
, var
);
138 if (instr
->sampler
!= NULL
) {
139 nir_variable
*var
= instr
->sampler
->var
;
140 _mesa_set_add(live
, var
);
145 add_var_use_shader(nir_shader
*shader
, struct set
*live
, nir_variable_mode modes
)
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
);
156 case nir_instr_type_intrinsic
:
157 add_var_use_intrinsic(nir_instr_as_intrinsic(instr
), live
,
161 case nir_instr_type_call
:
162 add_var_use_call(nir_instr_as_call(instr
), live
);
165 case nir_instr_type_tex
:
166 add_var_use_tex(nir_instr_as_tex(instr
), live
);
179 remove_dead_var_writes(nir_shader
*shader
, struct set
*live
)
181 nir_foreach_function(function
, shader
) {
185 nir_foreach_block(block
, function
->impl
) {
186 nir_foreach_instr_safe(instr
, block
) {
187 if (instr
->type
!= nir_instr_type_intrinsic
)
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
)
195 /* Stores to dead variables need to be removed */
196 if (intrin
->variables
[0]->var
->data
.mode
== 0)
197 nir_instr_remove(instr
);
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
);
207 nir_variable_mode parent_mode
;
208 if (deref
->deref_type
== nir_deref_type_var
)
209 parent_mode
= deref
->var
->data
.mode
;
211 parent_mode
= nir_deref_instr_parent(deref
)->mode
;
213 /* If the parent mode is 0, then it references a dead variable.
214 * Flag this deref as dead and remove it.
216 if (parent_mode
== 0) {
218 nir_instr_remove(&deref
->instr
);
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
)
229 if (nir_src_as_deref(intrin
->src
[0])->mode
== 0)
230 nir_instr_remove(instr
);
235 break; /* Nothing to do */
243 remove_dead_vars(struct exec_list
*var_list
, struct set
*live
)
245 bool progress
= false;
247 foreach_list_typed_safe(nir_variable
, var
, node
, var_list
) {
248 struct set_entry
*entry
= _mesa_set_search(live
, var
);
250 /* Mark this variable as used by setting the mode to 0 */
252 exec_node_remove(&var
->node
);
261 nir_remove_dead_variables(nir_shader
*shader
, nir_variable_mode modes
)
263 bool progress
= false;
265 _mesa_set_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
267 add_var_use_shader(shader
, live
, modes
);
269 if (modes
& nir_var_uniform
)
270 progress
= remove_dead_vars(&shader
->uniforms
, live
) || progress
;
272 if (modes
& nir_var_shader_in
)
273 progress
= remove_dead_vars(&shader
->inputs
, live
) || progress
;
275 if (modes
& nir_var_shader_out
)
276 progress
= remove_dead_vars(&shader
->outputs
, live
) || progress
;
278 if (modes
& nir_var_global
)
279 progress
= remove_dead_vars(&shader
->globals
, live
) || progress
;
281 if (modes
& nir_var_system_value
)
282 progress
= remove_dead_vars(&shader
->system_values
, live
) || progress
;
284 if (modes
& nir_var_shared
)
285 progress
= remove_dead_vars(&shader
->shared
, live
) || progress
;
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
))
297 remove_dead_var_writes(shader
, live
);
299 nir_foreach_function(function
, shader
) {
300 if (function
->impl
) {
301 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
302 nir_metadata_dominance
);
307 _mesa_set_destroy(live
, NULL
);