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 bool
deref_used_for_not_store(nir_deref_instr * deref)31 deref_used_for_not_store(nir_deref_instr *deref)
32 {
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)))
37             return true;
38          break;
39 
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.
45           */
46          if ((intrin->intrinsic != nir_intrinsic_store_deref &&
47               intrin->intrinsic != nir_intrinsic_copy_deref) ||
48              src != &intrin->src[0])
49             return true;
50          break;
51       }
52 
53       default:
54          /* If it's used by any other instruction type (most likely a texture
55           * or call instruction), consider it used.
56           */
57          return true;
58       }
59    }
60 
61    return false;
62 }
63 
64 static void
add_var_use_deref(nir_deref_instr * deref,struct set * live)65 add_var_use_deref(nir_deref_instr *deref, struct set *live)
66 {
67    if (deref->deref_type != nir_deref_type_var)
68       return;
69 
70    /* Since these local variables don't escape the shader, writing doesn't
71     * make them live.  Only keep them if they are used by some intrinsic.
72     */
73    if ((deref->var->data.mode & (nir_var_function_temp |
74                                  nir_var_shader_temp |
75                                  nir_var_mem_shared)) &&
76        !deref_used_for_not_store(deref))
77       return;
78 
79    nir_variable *var = deref->var;
80    do {
81       _mesa_set_add(live, var);
82       /* Also mark the chain of variables used to initialize it. */
83       var = var->pointer_initializer;
84    } while (var);
85 }
86 
87 static void
add_var_use_shader(nir_shader * shader,struct set * live,nir_variable_mode modes)88 add_var_use_shader(nir_shader *shader, struct set *live, nir_variable_mode modes)
89 {
90    nir_foreach_function(function, shader) {
91       if (function->impl) {
92          nir_foreach_block(block, function->impl) {
93             nir_foreach_instr(instr, block) {
94                if (instr->type == nir_instr_type_deref)
95                   add_var_use_deref(nir_instr_as_deref(instr), live);
96             }
97          }
98       }
99    }
100 }
101 
102 static void
remove_dead_var_writes(nir_shader * shader,struct set * live)103 remove_dead_var_writes(nir_shader *shader, struct set *live)
104 {
105    nir_foreach_function(function, shader) {
106       if (!function->impl)
107          continue;
108 
109       nir_foreach_block(block, function->impl) {
110          nir_foreach_instr_safe(instr, block) {
111             switch (instr->type) {
112             case nir_instr_type_deref: {
113                nir_deref_instr *deref = nir_instr_as_deref(instr);
114                if (deref->deref_type == nir_deref_type_cast &&
115                    !nir_deref_instr_parent(deref))
116                   continue;
117 
118                nir_variable_mode parent_modes;
119                if (deref->deref_type == nir_deref_type_var)
120                   parent_modes = deref->var->data.mode;
121                else
122                   parent_modes = nir_deref_instr_parent(deref)->modes;
123 
124                /* If the parent mode is 0, then it references a dead variable.
125                 * Flag this deref as dead and remove it.
126                 */
127                if (parent_modes == 0) {
128                   deref->modes = 0;
129                   nir_instr_remove(&deref->instr);
130                }
131                break;
132             }
133 
134             case nir_instr_type_intrinsic: {
135                nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
136                if (intrin->intrinsic != nir_intrinsic_copy_deref &&
137                    intrin->intrinsic != nir_intrinsic_store_deref)
138                   break;
139 
140                if (nir_src_as_deref(intrin->src[0])->modes == 0)
141                   nir_instr_remove(instr);
142                break;
143             }
144 
145             default:
146                break; /* Nothing to do */
147             }
148          }
149       }
150    }
151 }
152 
153 static bool
remove_dead_vars(struct exec_list * var_list,nir_variable_mode modes,struct set * live,const nir_remove_dead_variables_options * opts)154 remove_dead_vars(struct exec_list *var_list, nir_variable_mode modes,
155                  struct set *live, const nir_remove_dead_variables_options *opts)
156 {
157    bool progress = false;
158 
159    nir_foreach_variable_in_list_safe(var, var_list) {
160       if (!(var->data.mode & modes))
161          continue;
162 
163       if (opts && opts->can_remove_var &&
164           !opts->can_remove_var(var, opts->can_remove_var_data))
165          continue;
166 
167       struct set_entry *entry = _mesa_set_search(live, var);
168       if (entry == NULL) {
169          /* Mark this variable as used by setting the mode to 0 */
170          var->data.mode = 0;
171          exec_node_remove(&var->node);
172          progress = true;
173       }
174    }
175 
176    return progress;
177 }
178 
179 bool
nir_remove_dead_variables(nir_shader * shader,nir_variable_mode modes,const nir_remove_dead_variables_options * opts)180 nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
181                           const nir_remove_dead_variables_options *opts)
182 {
183    bool progress = false;
184    struct set *live = _mesa_pointer_set_create(NULL);
185 
186    add_var_use_shader(shader, live, modes);
187 
188    if (modes & ~nir_var_function_temp) {
189       progress = remove_dead_vars(&shader->variables, modes,
190                                   live, opts) || progress;
191    }
192 
193    if (modes & nir_var_function_temp) {
194       nir_foreach_function(function, shader) {
195          if (function->impl) {
196             if (remove_dead_vars(&function->impl->locals,
197                                  nir_var_function_temp,
198                                  live, opts))
199                progress = true;
200          }
201       }
202    }
203 
204    nir_foreach_function(function, shader) {
205       if (!function->impl)
206          continue;
207 
208       if (progress) {
209          remove_dead_var_writes(shader, live);
210          nir_metadata_preserve(function->impl, nir_metadata_block_index |
211                                                nir_metadata_dominance);
212       } else {
213          nir_metadata_preserve(function->impl, nir_metadata_all);
214       }
215    }
216 
217    _mesa_set_destroy(live, NULL);
218    return progress;
219 }
220