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
)
33 unsigned num_vars
= nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
34 for (unsigned i
= 0; i
< num_vars
; i
++) {
35 nir_variable
*var
= instr
->variables
[i
]->var
;
36 _mesa_set_add(live
, var
);
41 add_var_use_call(nir_call_instr
*instr
, struct set
*live
)
43 if (instr
->return_deref
!= NULL
) {
44 nir_variable
*var
= instr
->return_deref
->var
;
45 _mesa_set_add(live
, var
);
48 for (unsigned i
= 0; i
< instr
->num_params
; i
++) {
49 nir_variable
*var
= instr
->params
[i
]->var
;
50 _mesa_set_add(live
, var
);
55 add_var_use_tex(nir_tex_instr
*instr
, struct set
*live
)
57 if (instr
->sampler
!= NULL
) {
58 nir_variable
*var
= instr
->sampler
->var
;
59 _mesa_set_add(live
, var
);
64 add_var_use_block(nir_block
*block
, void *state
)
66 struct set
*live
= state
;
68 nir_foreach_instr(block
, instr
) {
70 case nir_instr_type_intrinsic
:
71 add_var_use_intrinsic(nir_instr_as_intrinsic(instr
), live
);
74 case nir_instr_type_call
:
75 add_var_use_call(nir_instr_as_call(instr
), live
);
78 case nir_instr_type_tex
:
79 add_var_use_tex(nir_instr_as_tex(instr
), live
);
91 add_var_use_shader(nir_shader
*shader
, struct set
*live
)
93 nir_foreach_overload(shader
, overload
) {
95 nir_foreach_block(overload
->impl
, add_var_use_block
, live
);
101 remove_dead_vars(struct exec_list
*var_list
, struct set
*live
)
103 bool progress
= false;
105 foreach_list_typed_safe(nir_variable
, var
, node
, var_list
) {
106 struct set_entry
*entry
= _mesa_set_search(live
, var
);
108 exec_node_remove(&var
->node
);
118 nir_remove_dead_variables(nir_shader
*shader
)
120 bool progress
= false;
122 _mesa_set_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
124 add_var_use_shader(shader
, live
);
126 progress
= remove_dead_vars(&shader
->globals
, live
) || progress
;
128 nir_foreach_overload(shader
, overload
) {
130 progress
= remove_dead_vars(&overload
->impl
->locals
, live
) || progress
;
133 _mesa_set_destroy(live
, NULL
);