nir: Remove old-school deref chain support
[mesa.git] / src / compiler / nir / nir_deref.c
1 /*
2 * Copyright © 2018 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
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27
28 void
29 nir_deref_path_init(nir_deref_path *path,
30 nir_deref_instr *deref, void *mem_ctx)
31 {
32 assert(deref != NULL);
33
34 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
35 * room for the NULL terminator.
36 */
37 static const int max_short_path_len = ARRAY_SIZE(path->_short_path) - 1;
38
39 int count = 0;
40
41 nir_deref_instr **tail = &path->_short_path[max_short_path_len];
42 nir_deref_instr **head = tail;
43
44 *tail = NULL;
45 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
46 count++;
47 if (count <= max_short_path_len)
48 *(--head) = d;
49 }
50
51 if (count <= max_short_path_len) {
52 /* If we're under max_short_path_len, just use the short path. */
53 path->path = head;
54 goto done;
55 }
56
57 #ifndef NDEBUG
58 /* Just in case someone uses short_path by accident */
59 for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++)
60 path->_short_path[i] = (void *)0xdeadbeef;
61 #endif
62
63 path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1);
64 head = tail = path->path + count;
65 *tail = NULL;
66 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d))
67 *(--head) = d;
68
69 done:
70 assert(head == path->path);
71 assert(tail == head + count);
72 assert((*head)->deref_type == nir_deref_type_var);
73 assert(*tail == NULL);
74 }
75
76 void
77 nir_deref_path_finish(nir_deref_path *path)
78 {
79 if (path->path < &path->_short_path[0] ||
80 path->path > &path->_short_path[ARRAY_SIZE(path->_short_path) - 1])
81 ralloc_free(path->path);
82 }
83
84 /**
85 * Recursively removes unused deref instructions
86 */
87 bool
88 nir_deref_instr_remove_if_unused(nir_deref_instr *instr)
89 {
90 bool progress = false;
91
92 for (nir_deref_instr *d = instr; d; d = nir_deref_instr_parent(d)) {
93 /* If anyone is using this deref, leave it alone */
94 assert(d->dest.is_ssa);
95 if (!list_empty(&d->dest.ssa.uses))
96 break;
97
98 nir_instr_remove(&d->instr);
99 progress = true;
100 }
101
102 return progress;
103 }
104
105 bool
106 nir_remove_dead_derefs_impl(nir_function_impl *impl)
107 {
108 bool progress = false;
109
110 nir_foreach_block(block, impl) {
111 nir_foreach_instr_safe(instr, block) {
112 if (instr->type == nir_instr_type_deref &&
113 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
114 progress = true;
115 }
116 }
117
118 if (progress)
119 nir_metadata_preserve(impl, nir_metadata_block_index |
120 nir_metadata_dominance);
121
122 return progress;
123 }
124
125 bool
126 nir_remove_dead_derefs(nir_shader *shader)
127 {
128 bool progress = false;
129 nir_foreach_function(function, shader) {
130 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
131 progress = true;
132 }
133
134 return progress;
135 }
136
137 void
138 nir_fixup_deref_modes(nir_shader *shader)
139 {
140 nir_foreach_function(function, shader) {
141 if (!function->impl)
142 continue;
143
144 nir_foreach_block(block, function->impl) {
145 nir_foreach_instr(instr, block) {
146 if (instr->type != nir_instr_type_deref)
147 continue;
148
149 nir_deref_instr *deref = nir_instr_as_deref(instr);
150
151 nir_variable_mode parent_mode;
152 if (deref->deref_type == nir_deref_type_var) {
153 parent_mode = deref->var->data.mode;
154 } else {
155 assert(deref->parent.is_ssa);
156 nir_deref_instr *parent =
157 nir_instr_as_deref(deref->parent.ssa->parent_instr);
158 parent_mode = parent->mode;
159 }
160
161 deref->mode = parent_mode;
162 }
163 }
164 }
165 }