• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (cwabbott0@gmail.com)
25  *
26  */
27 
28 #include "nir.h"
29 
30 static void
add_var_use_intrinsic(nir_intrinsic_instr * instr,struct set * live)31 add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live)
32 {
33    unsigned num_vars = nir_intrinsic_infos[instr->intrinsic].num_variables;
34 
35    switch (instr->intrinsic) {
36    case nir_intrinsic_copy_var:
37       _mesa_set_add(live, instr->variables[1]->var);
38       /* Fall through */
39    case nir_intrinsic_store_var: {
40       /* The first source in both copy_var and store_var is the destination.
41        * If the variable is a local that never escapes the shader, then we
42        * don't mark it as live for just a store.
43        */
44       nir_variable_mode mode = instr->variables[0]->var->data.mode;
45       if (!(mode & (nir_var_local | nir_var_global | nir_var_shared)))
46          _mesa_set_add(live, instr->variables[0]->var);
47       break;
48    }
49 
50    default:
51       for (unsigned i = 0; i < num_vars; i++) {
52          _mesa_set_add(live, instr->variables[i]->var);
53       }
54       break;
55    }
56 }
57 
58 static void
add_var_use_call(nir_call_instr * instr,struct set * live)59 add_var_use_call(nir_call_instr *instr, struct set *live)
60 {
61    if (instr->return_deref != NULL) {
62       nir_variable *var = instr->return_deref->var;
63       _mesa_set_add(live, var);
64    }
65 
66    for (unsigned i = 0; i < instr->num_params; i++) {
67       nir_variable *var = instr->params[i]->var;
68       _mesa_set_add(live, var);
69    }
70 }
71 
72 static void
add_var_use_tex(nir_tex_instr * instr,struct set * live)73 add_var_use_tex(nir_tex_instr *instr, struct set *live)
74 {
75    if (instr->texture != NULL) {
76       nir_variable *var = instr->texture->var;
77       _mesa_set_add(live, var);
78    }
79 
80    if (instr->sampler != NULL) {
81       nir_variable *var = instr->sampler->var;
82       _mesa_set_add(live, var);
83    }
84 }
85 
86 static void
add_var_use_shader(nir_shader * shader,struct set * live)87 add_var_use_shader(nir_shader *shader, struct set *live)
88 {
89    nir_foreach_function(function, shader) {
90       if (function->impl) {
91          nir_foreach_block(block, function->impl) {
92             nir_foreach_instr(instr, block) {
93                switch(instr->type) {
94                case nir_instr_type_intrinsic:
95                   add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live);
96                   break;
97 
98                case nir_instr_type_call:
99                   add_var_use_call(nir_instr_as_call(instr), live);
100                   break;
101 
102                case nir_instr_type_tex:
103                   add_var_use_tex(nir_instr_as_tex(instr), live);
104                   break;
105 
106                default:
107                   break;
108                }
109             }
110          }
111       }
112    }
113 }
114 
115 static void
remove_dead_var_writes(nir_shader * shader,struct set * live)116 remove_dead_var_writes(nir_shader *shader, struct set *live)
117 {
118    nir_foreach_function(function, shader) {
119       if (!function->impl)
120          continue;
121 
122       nir_foreach_block(block, function->impl) {
123          nir_foreach_instr_safe(instr, block) {
124             if (instr->type != nir_instr_type_intrinsic)
125                continue;
126 
127             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
128             if (intrin->intrinsic != nir_intrinsic_copy_var &&
129                 intrin->intrinsic != nir_intrinsic_store_var)
130                continue;
131 
132             /* Stores to dead variables need to be removed */
133             if (intrin->variables[0]->var->data.mode == 0)
134                nir_instr_remove(instr);
135          }
136       }
137    }
138 }
139 
140 static bool
remove_dead_vars(struct exec_list * var_list,struct set * live)141 remove_dead_vars(struct exec_list *var_list, struct set *live)
142 {
143    bool progress = false;
144 
145    foreach_list_typed_safe(nir_variable, var, node, var_list) {
146       struct set_entry *entry = _mesa_set_search(live, var);
147       if (entry == NULL) {
148          /* Mark this variable as used by setting the mode to 0 */
149          var->data.mode = 0;
150          exec_node_remove(&var->node);
151          progress = true;
152       }
153    }
154 
155    return progress;
156 }
157 
158 bool
nir_remove_dead_variables(nir_shader * shader,nir_variable_mode modes)159 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
160 {
161    bool progress = false;
162    struct set *live =
163       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
164 
165    add_var_use_shader(shader, live);
166 
167    if (modes & nir_var_uniform)
168       progress = remove_dead_vars(&shader->uniforms, live) || progress;
169 
170    if (modes & nir_var_shader_in)
171       progress = remove_dead_vars(&shader->inputs, live) || progress;
172 
173    if (modes & nir_var_shader_out)
174       progress = remove_dead_vars(&shader->outputs, live) || progress;
175 
176    if (modes & nir_var_global)
177       progress = remove_dead_vars(&shader->globals, live) || progress;
178 
179    if (modes & nir_var_system_value)
180       progress = remove_dead_vars(&shader->system_values, live) || progress;
181 
182    if (modes & nir_var_shared)
183       progress = remove_dead_vars(&shader->shared, live) || progress;
184 
185    if (modes & nir_var_local) {
186       nir_foreach_function(function, shader) {
187          if (function->impl) {
188             if (remove_dead_vars(&function->impl->locals, live))
189                progress = true;
190          }
191       }
192    }
193 
194    if (progress) {
195       remove_dead_var_writes(shader, live);
196 
197       nir_foreach_function(function, shader) {
198          if (function->impl) {
199             nir_metadata_preserve(function->impl, nir_metadata_block_index |
200                                                   nir_metadata_dominance);
201          }
202       }
203    }
204 
205    _mesa_set_destroy(live, NULL);
206    return progress;
207 }
208