8c5df7be6726aec2f7cb9a6556a67fc0197e1d5d
[mesa.git] / src / glsl / nir / nir_lower_locals_to_regs.c
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 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29
30 struct locals_to_regs_state {
31 void *mem_ctx;
32 nir_function_impl *impl;
33
34 /* A hash table mapping derefs to registers */
35 struct hash_table *regs_table;
36 };
37
38 /* The following two functions implement a hash and equality check for
39 * variable dreferences. When the hash or equality function encounters an
40 * array, it ignores the offset and whether it is direct or indirect
41 * entirely.
42 */
43 static uint32_t
44 hash_deref(const void *void_deref)
45 {
46 uint32_t hash = _mesa_fnv32_1a_offset_bias;
47
48 const nir_deref_var *deref_var = void_deref;
49 hash = _mesa_fnv32_1a_accumulate(hash, deref_var->var);
50
51 for (const nir_deref *deref = deref_var->deref.child;
52 deref; deref = deref->child) {
53 if (deref->deref_type == nir_deref_type_struct) {
54 const nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
55 hash = _mesa_fnv32_1a_accumulate(hash, deref_struct->index);
56 }
57 }
58
59 return hash;
60 }
61
62 static bool
63 derefs_equal(const void *void_a, const void *void_b)
64 {
65 const nir_deref_var *a_var = void_a;
66 const nir_deref_var *b_var = void_b;
67
68 if (a_var->var != b_var->var)
69 return false;
70
71 for (const nir_deref *a = a_var->deref.child, *b = b_var->deref.child;
72 a != NULL; a = a->child, b = b->child) {
73 if (a->deref_type != b->deref_type)
74 return false;
75
76 if (a->deref_type == nir_deref_type_struct) {
77 if (nir_deref_as_struct(a)->index != nir_deref_as_struct(b)->index)
78 return false;
79 }
80 /* Do nothing for arrays. They're all the same. */
81
82 assert((a->child == NULL) == (b->child == NULL));
83 if((a->child == NULL) != (b->child == NULL))
84 return false;
85 }
86
87 return true;
88 }
89
90 static nir_register *
91 get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
92 {
93 uint32_t hash = hash_deref(deref);
94
95 struct hash_entry *entry =
96 _mesa_hash_table_search_pre_hashed(state->regs_table, hash, deref);
97 if (entry)
98 return entry->data;
99
100 unsigned array_size = 1;
101 nir_deref *tail = &deref->deref;
102 while (tail->child) {
103 if (tail->child->deref_type == nir_deref_type_array) {
104 /* Multiply by the parent's type. */
105 if (glsl_type_is_matrix(tail->type)) {
106 array_size *= glsl_get_matrix_columns(tail->type);
107 } else {
108 assert(glsl_get_length(tail->type) > 0);
109 array_size *= glsl_get_length(tail->type);
110 }
111 }
112 tail = tail->child;
113 }
114
115 assert(glsl_type_is_vector(tail->type) || glsl_type_is_scalar(tail->type));
116
117 nir_register *reg = nir_local_reg_create(state->impl);
118 reg->num_components = glsl_get_vector_elements(tail->type);
119 reg->num_array_elems = array_size > 1 ? array_size : 0;
120
121 _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
122
123 return reg;
124 }
125
126 static nir_src
127 get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
128 struct locals_to_regs_state *state)
129 {
130 nir_src src;
131
132 src.is_ssa = false;
133 src.reg.reg = get_reg_for_deref(deref, state);
134 src.reg.base_offset = 0;
135 src.reg.indirect = NULL;
136
137 nir_deref *tail = &deref->deref;
138 while (tail->child != NULL) {
139 const struct glsl_type *parent_type = tail->type;
140 tail = tail->child;
141
142 if (tail->deref_type != nir_deref_type_array)
143 continue;
144
145 nir_deref_array *deref_array = nir_deref_as_array(tail);
146
147 src.reg.base_offset *= glsl_get_length(parent_type);
148 src.reg.base_offset += deref_array->base_offset;
149
150 if (src.reg.indirect) {
151 nir_load_const_instr *load_const =
152 nir_load_const_instr_create(state->mem_ctx, 1);
153 load_const->value.u[0] = glsl_get_length(parent_type);
154 nir_instr_insert_before(instr, &load_const->instr);
155
156 nir_alu_instr *mul = nir_alu_instr_create(state->mem_ctx, nir_op_imul);
157 mul->src[0].src = *src.reg.indirect;
158 mul->src[1].src.is_ssa = true;
159 mul->src[1].src.ssa = &load_const->def;
160 mul->dest.write_mask = 1;
161 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
162 nir_instr_insert_before(instr, &mul->instr);
163
164 src.reg.indirect->is_ssa = true;
165 src.reg.indirect->ssa = &mul->dest.dest.ssa;
166 }
167
168 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
169 if (src.reg.indirect == NULL) {
170 src.reg.indirect = ralloc(state->mem_ctx, nir_src);
171 nir_src_copy(src.reg.indirect, &deref_array->indirect,
172 state->mem_ctx);
173 } else {
174 nir_alu_instr *add = nir_alu_instr_create(state->mem_ctx,
175 nir_op_iadd);
176 add->src[0].src = *src.reg.indirect;
177 nir_src_copy(&add->src[1].src, &deref_array->indirect,
178 state->mem_ctx);
179 add->dest.write_mask = 1;
180 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
181 nir_instr_insert_before(instr, &add->instr);
182
183 src.reg.indirect->is_ssa = true;
184 src.reg.indirect->ssa = &add->dest.dest.ssa;
185 }
186 }
187 }
188
189 return src;
190 }
191
192 static bool
193 lower_locals_to_regs_block(nir_block *block, void *void_state)
194 {
195 struct locals_to_regs_state *state = void_state;
196
197 nir_foreach_instr_safe(block, instr) {
198 if (instr->type != nir_instr_type_intrinsic)
199 continue;
200
201 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
202
203 switch (intrin->intrinsic) {
204 case nir_intrinsic_load_var: {
205 if (intrin->variables[0]->var->data.mode != nir_var_local)
206 continue;
207
208 nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
209 mov->src[0].src = get_deref_reg_src(intrin->variables[0],
210 &intrin->instr, state);
211 mov->dest.write_mask = (1 << intrin->num_components) - 1;
212 if (intrin->dest.is_ssa) {
213 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
214 intrin->num_components, NULL);
215 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
216 nir_src_for_ssa(&mov->dest.dest.ssa),
217 state->mem_ctx);
218 } else {
219 nir_dest_copy(&mov->dest.dest, &intrin->dest, state->mem_ctx);
220 }
221 nir_instr_insert_before(&intrin->instr, &mov->instr);
222
223 nir_instr_remove(&intrin->instr);
224 break;
225 }
226
227 case nir_intrinsic_store_var: {
228 if (intrin->variables[0]->var->data.mode != nir_var_local)
229 continue;
230
231 nir_src reg_src = get_deref_reg_src(intrin->variables[0],
232 &intrin->instr, state);
233
234 nir_alu_instr *mov = nir_alu_instr_create(state->mem_ctx, nir_op_imov);
235 nir_src_copy(&mov->src[0].src, &intrin->src[0], state->mem_ctx);
236 mov->dest.write_mask = (1 << intrin->num_components) - 1;
237 mov->dest.dest.is_ssa = false;
238 mov->dest.dest.reg.reg = reg_src.reg.reg;
239 mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
240 mov->dest.dest.reg.indirect = reg_src.reg.indirect;
241
242 nir_instr_insert_before(&intrin->instr, &mov->instr);
243
244 nir_instr_remove(&intrin->instr);
245 break;
246 }
247
248 case nir_intrinsic_copy_var:
249 unreachable("There should be no copies whatsoever at this point");
250 break;
251
252 default:
253 continue;
254 }
255 }
256
257 return true;
258 }
259
260 static void
261 nir_lower_locals_to_regs_impl(nir_function_impl *impl)
262 {
263 struct locals_to_regs_state state;
264
265 state.mem_ctx = ralloc_parent(impl);
266 state.impl = impl;
267 state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
268
269 nir_foreach_block(impl, lower_locals_to_regs_block, &state);
270
271 nir_metadata_preserve(impl, nir_metadata_block_index |
272 nir_metadata_dominance);
273
274 _mesa_hash_table_destroy(state.regs_table, NULL);
275 }
276
277 void
278 nir_lower_locals_to_regs(nir_shader *shader)
279 {
280 nir_foreach_overload(shader, overload) {
281 if (overload->impl)
282 nir_lower_locals_to_regs_impl(overload->impl);
283 }
284 }