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 add_var_use_intrinsic(nir_intrinsic_instr
*instr
, struct set
*live
,
32 nir_variable_mode modes
)
34 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
36 switch (instr
->intrinsic
) {
37 case nir_intrinsic_copy_var
:
38 _mesa_set_add(live
, instr
->variables
[1]->var
);
40 case nir_intrinsic_store_var
: {
41 /* The first source in both copy_var and store_var is the destination.
42 * If the variable is a local that never escapes the shader, then we
43 * don't mark it as live for just a store.
45 nir_variable_mode mode
= instr
->variables
[0]->var
->data
.mode
;
46 if (!(mode
& (nir_var_local
| nir_var_global
| nir_var_shared
)))
47 _mesa_set_add(live
, instr
->variables
[0]->var
);
51 /* This pass can't be used on I/O variables after they've been lowered. */
52 case nir_intrinsic_load_input
:
53 assert(!(modes
& nir_var_shader_in
));
55 case nir_intrinsic_store_output
:
56 assert(!(modes
& nir_var_shader_out
));
60 for (unsigned i
= 0; i
< num_vars
; i
++) {
61 _mesa_set_add(live
, instr
->variables
[i
]->var
);
68 add_var_use_call(nir_call_instr
*instr
, struct set
*live
)
70 if (instr
->return_deref
!= NULL
) {
71 nir_variable
*var
= instr
->return_deref
->var
;
72 _mesa_set_add(live
, var
);
75 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
76 nir_variable
*var
= instr
->params
[i
]->var
;
77 _mesa_set_add(live
, var
);
82 add_var_use_tex(nir_tex_instr
*instr
, struct set
*live
)
84 if (instr
->texture
!= NULL
) {
85 nir_variable
*var
= instr
->texture
->var
;
86 _mesa_set_add(live
, var
);
89 if (instr
->sampler
!= NULL
) {
90 nir_variable
*var
= instr
->sampler
->var
;
91 _mesa_set_add(live
, var
);
96 add_var_use_shader(nir_shader
*shader
, struct set
*live
, nir_variable_mode modes
)
98 nir_foreach_function(function
, shader
) {
100 nir_foreach_block(block
, function
->impl
) {
101 nir_foreach_instr(instr
, block
) {
102 switch(instr
->type
) {
103 case nir_instr_type_intrinsic
:
104 add_var_use_intrinsic(nir_instr_as_intrinsic(instr
), live
,
108 case nir_instr_type_call
:
109 add_var_use_call(nir_instr_as_call(instr
), live
);
112 case nir_instr_type_tex
:
113 add_var_use_tex(nir_instr_as_tex(instr
), live
);
126 remove_dead_var_writes(nir_shader
*shader
, struct set
*live
)
128 nir_foreach_function(function
, shader
) {
132 nir_foreach_block(block
, function
->impl
) {
133 nir_foreach_instr_safe(instr
, block
) {
134 if (instr
->type
!= nir_instr_type_intrinsic
)
137 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
138 if (intrin
->intrinsic
!= nir_intrinsic_copy_var
&&
139 intrin
->intrinsic
!= nir_intrinsic_store_var
)
142 /* Stores to dead variables need to be removed */
143 if (intrin
->variables
[0]->var
->data
.mode
== 0)
144 nir_instr_remove(instr
);
151 remove_dead_vars(struct exec_list
*var_list
, struct set
*live
)
153 bool progress
= false;
155 foreach_list_typed_safe(nir_variable
, var
, node
, var_list
) {
156 struct set_entry
*entry
= _mesa_set_search(live
, var
);
158 /* Mark this variable as used by setting the mode to 0 */
160 exec_node_remove(&var
->node
);
169 nir_remove_dead_variables(nir_shader
*shader
, nir_variable_mode modes
)
171 bool progress
= false;
173 _mesa_set_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
175 add_var_use_shader(shader
, live
, modes
);
177 if (modes
& nir_var_uniform
)
178 progress
= remove_dead_vars(&shader
->uniforms
, live
) || progress
;
180 if (modes
& nir_var_shader_in
)
181 progress
= remove_dead_vars(&shader
->inputs
, live
) || progress
;
183 if (modes
& nir_var_shader_out
)
184 progress
= remove_dead_vars(&shader
->outputs
, live
) || progress
;
186 if (modes
& nir_var_global
)
187 progress
= remove_dead_vars(&shader
->globals
, live
) || progress
;
189 if (modes
& nir_var_system_value
)
190 progress
= remove_dead_vars(&shader
->system_values
, live
) || progress
;
192 if (modes
& nir_var_shared
)
193 progress
= remove_dead_vars(&shader
->shared
, live
) || progress
;
195 if (modes
& nir_var_local
) {
196 nir_foreach_function(function
, shader
) {
197 if (function
->impl
) {
198 if (remove_dead_vars(&function
->impl
->locals
, live
))
205 remove_dead_var_writes(shader
, live
);
207 nir_foreach_function(function
, shader
) {
208 if (function
->impl
) {
209 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
210 nir_metadata_dominance
);
215 _mesa_set_destroy(live
, NULL
);