nir_remove_dead_variables.c revision 58fe5c57cd424889fd0a655cc4c19569ad7ba960
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
30static void
31add_var_use_intrinsic(nir_intrinsic_instr *instr, struct set *live)
32{
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);
37   }
38}
39
40static void
41add_var_use_call(nir_call_instr *instr, struct set *live)
42{
43   if (instr->return_deref != NULL) {
44      nir_variable *var = instr->return_deref->var;
45      _mesa_set_add(live, var);
46   }
47
48   for (unsigned i = 0; i < instr->num_params; i++) {
49      nir_variable *var = instr->params[i]->var;
50      _mesa_set_add(live, var);
51   }
52}
53
54static void
55add_var_use_tex(nir_tex_instr *instr, struct set *live)
56{
57   if (instr->texture != NULL) {
58      nir_variable *var = instr->texture->var;
59      _mesa_set_add(live, var);
60   }
61
62   if (instr->sampler != NULL) {
63      nir_variable *var = instr->sampler->var;
64      _mesa_set_add(live, var);
65   }
66}
67
68static void
69add_var_use_shader(nir_shader *shader, struct set *live)
70{
71   nir_foreach_function(function, shader) {
72      if (function->impl) {
73         nir_foreach_block(block, function->impl) {
74            nir_foreach_instr(instr, block) {
75               switch(instr->type) {
76               case nir_instr_type_intrinsic:
77                  add_var_use_intrinsic(nir_instr_as_intrinsic(instr), live);
78                  break;
79
80               case nir_instr_type_call:
81                  add_var_use_call(nir_instr_as_call(instr), live);
82                  break;
83
84               case nir_instr_type_tex:
85                  add_var_use_tex(nir_instr_as_tex(instr), live);
86                  break;
87
88               default:
89                  break;
90               }
91            }
92         }
93      }
94   }
95}
96
97static bool
98remove_dead_vars(struct exec_list *var_list, struct set *live)
99{
100   bool progress = false;
101
102   foreach_list_typed_safe(nir_variable, var, node, var_list) {
103      struct set_entry *entry = _mesa_set_search(live, var);
104      if (entry == NULL) {
105         exec_node_remove(&var->node);
106         ralloc_free(var);
107         progress = true;
108      }
109   }
110
111   return progress;
112}
113
114bool
115nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
116{
117   bool progress = false;
118   struct set *live =
119      _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
120
121   add_var_use_shader(shader, live);
122
123   if (modes & nir_var_uniform)
124      progress = remove_dead_vars(&shader->uniforms, live) || progress;
125
126   if (modes & nir_var_shader_in)
127      progress = remove_dead_vars(&shader->inputs, live) || progress;
128
129   if (modes & nir_var_shader_out)
130      progress = remove_dead_vars(&shader->outputs, live) || progress;
131
132   if (modes & nir_var_global)
133      progress = remove_dead_vars(&shader->globals, live) || progress;
134
135   if (modes & nir_var_system_value)
136      progress = remove_dead_vars(&shader->system_values, live) || progress;
137
138   if (modes & nir_var_shared)
139      progress = remove_dead_vars(&shader->shared, live) || progress;
140
141   if (modes & nir_var_local) {
142      nir_foreach_function(function, shader) {
143         if (function->impl) {
144            if (remove_dead_vars(&function->impl->locals, live)) {
145               nir_metadata_preserve(function->impl, nir_metadata_block_index |
146                                                     nir_metadata_dominance |
147                                                     nir_metadata_live_ssa_defs);
148               progress = true;
149            }
150         }
151      }
152   }
153
154   _mesa_set_destroy(live, NULL);
155   return progress;
156}
157