cdb9105a751fac4f9a685dcb8ee369915e490745
[mesa.git] / 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 #include "util/hash_table.h"
28
29 void
30 nir_deref_path_init(nir_deref_path *path,
31 nir_deref_instr *deref, void *mem_ctx)
32 {
33 assert(deref != NULL);
34
35 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
36 * room for the NULL terminator.
37 */
38 static const int max_short_path_len = ARRAY_SIZE(path->_short_path) - 1;
39
40 int count = 0;
41
42 nir_deref_instr **tail = &path->_short_path[max_short_path_len];
43 nir_deref_instr **head = tail;
44
45 *tail = NULL;
46 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
47 count++;
48 if (count <= max_short_path_len)
49 *(--head) = d;
50 }
51
52 if (count <= max_short_path_len) {
53 /* If we're under max_short_path_len, just use the short path. */
54 path->path = head;
55 goto done;
56 }
57
58 #ifndef NDEBUG
59 /* Just in case someone uses short_path by accident */
60 for (unsigned i = 0; i < ARRAY_SIZE(path->_short_path); i++)
61 path->_short_path[i] = (void *)0xdeadbeef;
62 #endif
63
64 path->path = ralloc_array(mem_ctx, nir_deref_instr *, count + 1);
65 head = tail = path->path + count;
66 *tail = NULL;
67 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d))
68 *(--head) = d;
69
70 done:
71 assert(head == path->path);
72 assert(tail == head + count);
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_deref_instr_has_indirect(nir_deref_instr *instr)
107 {
108 while (instr->deref_type != nir_deref_type_var) {
109 /* Consider casts to be indirects */
110 if (instr->deref_type == nir_deref_type_cast)
111 return true;
112
113 if ((instr->deref_type == nir_deref_type_array ||
114 instr->deref_type == nir_deref_type_ptr_as_array) &&
115 !nir_src_is_const(instr->arr.index))
116 return true;
117
118 instr = nir_deref_instr_parent(instr);
119 }
120
121 return false;
122 }
123
124 bool
125 nir_deref_instr_has_complex_use(nir_deref_instr *deref)
126 {
127 nir_foreach_use(use_src, &deref->dest.ssa) {
128 nir_instr *use_instr = use_src->parent_instr;
129
130 switch (use_instr->type) {
131 case nir_instr_type_deref: {
132 nir_deref_instr *use_deref = nir_instr_as_deref(use_instr);
133
134 /* A var deref has no sources */
135 assert(use_deref->deref_type != nir_deref_type_var);
136
137 /* If a deref shows up in an array index or something like that, it's
138 * a complex use.
139 */
140 if (use_src != &use_deref->parent)
141 return true;
142
143 /* Anything that isn't a basic struct or array deref is considered to
144 * be a "complex" use. In particular, we don't allow ptr_as_array
145 * because we assume that opt_deref will turn any non-complex
146 * ptr_as_array derefs into regular array derefs eventually so passes
147 * which only want to handle simple derefs will pick them up in a
148 * later pass.
149 */
150 if (use_deref->deref_type != nir_deref_type_struct &&
151 use_deref->deref_type != nir_deref_type_array_wildcard &&
152 use_deref->deref_type != nir_deref_type_array)
153 return true;
154
155 if (nir_deref_instr_has_complex_use(use_deref))
156 return true;
157
158 continue;
159 }
160
161 case nir_instr_type_intrinsic: {
162 nir_intrinsic_instr *use_intrin = nir_instr_as_intrinsic(use_instr);
163 switch (use_intrin->intrinsic) {
164 case nir_intrinsic_load_deref:
165 assert(use_src == &use_intrin->src[0]);
166 continue;
167
168 case nir_intrinsic_copy_deref:
169 assert(use_src == &use_intrin->src[0] ||
170 use_src == &use_intrin->src[1]);
171 continue;
172
173 case nir_intrinsic_store_deref:
174 /* A use in src[1] of a store means we're taking that pointer and
175 * writing it to a variable. Because we have no idea who will
176 * read that variable and what they will do with the pointer, it's
177 * considered a "complex" use. A use in src[0], on the other
178 * hand, is a simple use because we're just going to dereference
179 * it and write a value there.
180 */
181 if (use_src == &use_intrin->src[0])
182 continue;
183 return true;
184
185 default:
186 return true;
187 }
188 unreachable("Switch default failed");
189 }
190
191 default:
192 return true;
193 }
194 }
195
196 nir_foreach_if_use(use, &deref->dest.ssa)
197 return true;
198
199 return false;
200 }
201
202 unsigned
203 nir_deref_instr_ptr_as_array_stride(nir_deref_instr *deref)
204 {
205 assert(deref->deref_type == nir_deref_type_ptr_as_array);
206 nir_deref_instr *parent = nir_deref_instr_parent(deref);
207 switch (parent->deref_type) {
208 case nir_deref_type_array:
209 return glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
210 case nir_deref_type_ptr_as_array:
211 return nir_deref_instr_ptr_as_array_stride(parent);
212 case nir_deref_type_cast:
213 return parent->cast.ptr_stride;
214 default:
215 unreachable("Invalid parent for ptr_as_array deref");
216 }
217 }
218
219 static unsigned
220 type_get_array_stride(const struct glsl_type *elem_type,
221 glsl_type_size_align_func size_align)
222 {
223 unsigned elem_size, elem_align;
224 size_align(elem_type, &elem_size, &elem_align);
225 return ALIGN_POT(elem_size, elem_align);
226 }
227
228 static unsigned
229 struct_type_get_field_offset(const struct glsl_type *struct_type,
230 glsl_type_size_align_func size_align,
231 unsigned field_idx)
232 {
233 assert(glsl_type_is_struct_or_ifc(struct_type));
234 unsigned offset = 0;
235 for (unsigned i = 0; i <= field_idx; i++) {
236 unsigned elem_size, elem_align;
237 size_align(glsl_get_struct_field(struct_type, i), &elem_size, &elem_align);
238 offset = ALIGN_POT(offset, elem_align);
239 if (i < field_idx)
240 offset += elem_size;
241 }
242 return offset;
243 }
244
245 unsigned
246 nir_deref_instr_get_const_offset(nir_deref_instr *deref,
247 glsl_type_size_align_func size_align)
248 {
249 nir_deref_path path;
250 nir_deref_path_init(&path, deref, NULL);
251
252 assert(path.path[0]->deref_type == nir_deref_type_var);
253
254 unsigned offset = 0;
255 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
256 if ((*p)->deref_type == nir_deref_type_array) {
257 offset += nir_src_as_uint((*p)->arr.index) *
258 type_get_array_stride((*p)->type, size_align);
259 } else if ((*p)->deref_type == nir_deref_type_struct) {
260 /* p starts at path[1], so this is safe */
261 nir_deref_instr *parent = *(p - 1);
262 offset += struct_type_get_field_offset(parent->type, size_align,
263 (*p)->strct.index);
264 } else {
265 unreachable("Unsupported deref type");
266 }
267 }
268
269 nir_deref_path_finish(&path);
270
271 return offset;
272 }
273
274 nir_ssa_def *
275 nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
276 glsl_type_size_align_func size_align)
277 {
278 nir_deref_path path;
279 nir_deref_path_init(&path, deref, NULL);
280
281 assert(path.path[0]->deref_type == nir_deref_type_var);
282
283 nir_ssa_def *offset = nir_imm_int(b, 0);
284 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
285 if ((*p)->deref_type == nir_deref_type_array) {
286 nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1);
287 int stride = type_get_array_stride((*p)->type, size_align);
288 offset = nir_iadd(b, offset, nir_imul_imm(b, index, stride));
289 } else if ((*p)->deref_type == nir_deref_type_struct) {
290 /* p starts at path[1], so this is safe */
291 nir_deref_instr *parent = *(p - 1);
292 unsigned field_offset =
293 struct_type_get_field_offset(parent->type, size_align,
294 (*p)->strct.index);
295 offset = nir_iadd_imm(b, offset, field_offset);
296 } else {
297 unreachable("Unsupported deref type");
298 }
299 }
300
301 nir_deref_path_finish(&path);
302
303 return offset;
304 }
305
306 bool
307 nir_remove_dead_derefs_impl(nir_function_impl *impl)
308 {
309 bool progress = false;
310
311 nir_foreach_block(block, impl) {
312 nir_foreach_instr_safe(instr, block) {
313 if (instr->type == nir_instr_type_deref &&
314 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
315 progress = true;
316 }
317 }
318
319 if (progress)
320 nir_metadata_preserve(impl, nir_metadata_block_index |
321 nir_metadata_dominance);
322
323 return progress;
324 }
325
326 bool
327 nir_remove_dead_derefs(nir_shader *shader)
328 {
329 bool progress = false;
330 nir_foreach_function(function, shader) {
331 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
332 progress = true;
333 }
334
335 return progress;
336 }
337
338 void
339 nir_fixup_deref_modes(nir_shader *shader)
340 {
341 nir_foreach_function(function, shader) {
342 if (!function->impl)
343 continue;
344
345 nir_foreach_block(block, function->impl) {
346 nir_foreach_instr(instr, block) {
347 if (instr->type != nir_instr_type_deref)
348 continue;
349
350 nir_deref_instr *deref = nir_instr_as_deref(instr);
351 if (deref->deref_type == nir_deref_type_cast)
352 continue;
353
354 nir_variable_mode parent_mode;
355 if (deref->deref_type == nir_deref_type_var) {
356 parent_mode = deref->var->data.mode;
357 } else {
358 assert(deref->parent.is_ssa);
359 nir_deref_instr *parent =
360 nir_instr_as_deref(deref->parent.ssa->parent_instr);
361 parent_mode = parent->mode;
362 }
363
364 deref->mode = parent_mode;
365 }
366 }
367 }
368 }
369
370 static bool
371 modes_may_alias(nir_variable_mode a, nir_variable_mode b)
372 {
373 /* Generic pointers can alias with SSBOs */
374 if ((a == nir_var_mem_ssbo || a == nir_var_mem_global) &&
375 (b == nir_var_mem_ssbo || b == nir_var_mem_global))
376 return true;
377
378 /* In the general case, pointers can only alias if they have the same mode.
379 *
380 * NOTE: In future, with things like OpenCL generic pointers, this may not
381 * be true and will have to be re-evaluated. However, with graphics only,
382 * it should be safe.
383 */
384 return a == b;
385 }
386
387 static bool
388 deref_path_contains_coherent_decoration(nir_deref_path *path)
389 {
390 assert(path->path[0]->deref_type == nir_deref_type_var);
391
392 if (path->path[0]->var->data.image.access & ACCESS_COHERENT)
393 return true;
394
395 for (nir_deref_instr **p = &path->path[1]; *p; p++) {
396 if ((*p)->deref_type != nir_deref_type_struct)
397 continue;
398
399 const struct glsl_type *struct_type = (*(p - 1))->type;
400 const struct glsl_struct_field *field =
401 glsl_get_struct_field_data(struct_type, (*p)->strct.index);
402 if (field->memory_coherent)
403 return true;
404 }
405
406 return false;
407 }
408
409 nir_deref_compare_result
410 nir_compare_deref_paths(nir_deref_path *a_path,
411 nir_deref_path *b_path)
412 {
413 if (!modes_may_alias(b_path->path[0]->mode, a_path->path[0]->mode))
414 return nir_derefs_do_not_alias;
415
416 if (a_path->path[0]->deref_type != b_path->path[0]->deref_type)
417 return nir_derefs_may_alias_bit;
418
419 if (a_path->path[0]->deref_type == nir_deref_type_var) {
420 if (a_path->path[0]->var != b_path->path[0]->var) {
421 /* Shader and function temporaries aren't backed by memory so two
422 * distinct variables never alias.
423 */
424 static const nir_variable_mode temp_var_modes =
425 nir_var_shader_temp | nir_var_function_temp;
426 if ((a_path->path[0]->mode & temp_var_modes) ||
427 (b_path->path[0]->mode & temp_var_modes))
428 return nir_derefs_do_not_alias;
429
430 /* If they are both declared coherent or have coherent somewhere in
431 * their path (due to a member of an interface being declared
432 * coherent), we have to assume we that we could have any kind of
433 * aliasing. Otherwise, they could still alias but the client didn't
434 * tell us and that's their fault.
435 */
436 if (deref_path_contains_coherent_decoration(a_path) &&
437 deref_path_contains_coherent_decoration(b_path))
438 return nir_derefs_may_alias_bit;
439
440 /* If we can chase the deref all the way back to the variable and
441 * they're not the same variable and at least one is not declared
442 * coherent, we know they can't possibly alias.
443 */
444 return nir_derefs_do_not_alias;
445 }
446 } else {
447 assert(a_path->path[0]->deref_type == nir_deref_type_cast);
448 /* If they're not exactly the same cast, it's hard to compare them so we
449 * just assume they alias. Comparing casts is tricky as there are lots
450 * of things such as mode, type, etc. to make sure work out; for now, we
451 * just assume nit_opt_deref will combine them and compare the deref
452 * instructions.
453 *
454 * TODO: At some point in the future, we could be clever and understand
455 * that a float[] and int[] have the same layout and aliasing structure
456 * but double[] and vec3[] do not and we could potentially be a bit
457 * smarter here.
458 */
459 if (a_path->path[0] != b_path->path[0])
460 return nir_derefs_may_alias_bit;
461 }
462
463 /* Start off assuming they fully compare. We ignore equality for now. In
464 * the end, we'll determine that by containment.
465 */
466 nir_deref_compare_result result = nir_derefs_may_alias_bit |
467 nir_derefs_a_contains_b_bit |
468 nir_derefs_b_contains_a_bit;
469
470 nir_deref_instr **a_p = &a_path->path[1];
471 nir_deref_instr **b_p = &b_path->path[1];
472 while (*a_p != NULL && *a_p == *b_p) {
473 a_p++;
474 b_p++;
475 }
476
477 /* We're at either the tail or the divergence point between the two deref
478 * paths. Look to see if either contains a ptr_as_array deref. It it
479 * does we don't know how to safely make any inferences. Hopefully,
480 * nir_opt_deref will clean most of these up and we can start inferring
481 * things again.
482 *
483 * In theory, we could do a bit better. For instance, we could detect the
484 * case where we have exactly one ptr_as_array deref in the chain after the
485 * divergence point and it's matched in both chains and the two chains have
486 * different constant indices.
487 */
488 for (nir_deref_instr **t_p = a_p; *t_p; t_p++) {
489 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
490 return nir_derefs_may_alias_bit;
491 }
492 for (nir_deref_instr **t_p = b_p; *t_p; t_p++) {
493 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
494 return nir_derefs_may_alias_bit;
495 }
496
497 while (*a_p != NULL && *b_p != NULL) {
498 nir_deref_instr *a_tail = *(a_p++);
499 nir_deref_instr *b_tail = *(b_p++);
500
501 switch (a_tail->deref_type) {
502 case nir_deref_type_array:
503 case nir_deref_type_array_wildcard: {
504 assert(b_tail->deref_type == nir_deref_type_array ||
505 b_tail->deref_type == nir_deref_type_array_wildcard);
506
507 if (a_tail->deref_type == nir_deref_type_array_wildcard) {
508 if (b_tail->deref_type != nir_deref_type_array_wildcard)
509 result &= ~nir_derefs_b_contains_a_bit;
510 } else if (b_tail->deref_type == nir_deref_type_array_wildcard) {
511 if (a_tail->deref_type != nir_deref_type_array_wildcard)
512 result &= ~nir_derefs_a_contains_b_bit;
513 } else {
514 assert(a_tail->deref_type == nir_deref_type_array &&
515 b_tail->deref_type == nir_deref_type_array);
516 assert(a_tail->arr.index.is_ssa && b_tail->arr.index.is_ssa);
517
518 if (nir_src_is_const(a_tail->arr.index) &&
519 nir_src_is_const(b_tail->arr.index)) {
520 /* If they're both direct and have different offsets, they
521 * don't even alias much less anything else.
522 */
523 if (nir_src_as_uint(a_tail->arr.index) !=
524 nir_src_as_uint(b_tail->arr.index))
525 return nir_derefs_do_not_alias;
526 } else if (a_tail->arr.index.ssa == b_tail->arr.index.ssa) {
527 /* They're the same indirect, continue on */
528 } else {
529 /* They're not the same index so we can't prove anything about
530 * containment.
531 */
532 result &= ~(nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit);
533 }
534 }
535 break;
536 }
537
538 case nir_deref_type_struct: {
539 /* If they're different struct members, they don't even alias */
540 if (a_tail->strct.index != b_tail->strct.index)
541 return nir_derefs_do_not_alias;
542 break;
543 }
544
545 default:
546 unreachable("Invalid deref type");
547 }
548 }
549
550 /* If a is longer than b, then it can't contain b */
551 if (*a_p != NULL)
552 result &= ~nir_derefs_a_contains_b_bit;
553 if (*b_p != NULL)
554 result &= ~nir_derefs_b_contains_a_bit;
555
556 /* If a contains b and b contains a they must be equal. */
557 if ((result & nir_derefs_a_contains_b_bit) && (result & nir_derefs_b_contains_a_bit))
558 result |= nir_derefs_equal_bit;
559
560 return result;
561 }
562
563 nir_deref_compare_result
564 nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
565 {
566 if (a == b) {
567 return nir_derefs_equal_bit | nir_derefs_may_alias_bit |
568 nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit;
569 }
570
571 nir_deref_path a_path, b_path;
572 nir_deref_path_init(&a_path, a, NULL);
573 nir_deref_path_init(&b_path, b, NULL);
574 assert(a_path.path[0]->deref_type == nir_deref_type_var ||
575 a_path.path[0]->deref_type == nir_deref_type_cast);
576 assert(b_path.path[0]->deref_type == nir_deref_type_var ||
577 b_path.path[0]->deref_type == nir_deref_type_cast);
578
579 nir_deref_compare_result result = nir_compare_deref_paths(&a_path, &b_path);
580
581 nir_deref_path_finish(&a_path);
582 nir_deref_path_finish(&b_path);
583
584 return result;
585 }
586
587 struct rematerialize_deref_state {
588 bool progress;
589 nir_builder builder;
590 nir_block *block;
591 struct hash_table *cache;
592 };
593
594 static nir_deref_instr *
595 rematerialize_deref_in_block(nir_deref_instr *deref,
596 struct rematerialize_deref_state *state)
597 {
598 if (deref->instr.block == state->block)
599 return deref;
600
601 if (!state->cache) {
602 state->cache = _mesa_pointer_hash_table_create(NULL);
603 }
604
605 struct hash_entry *cached = _mesa_hash_table_search(state->cache, deref);
606 if (cached)
607 return cached->data;
608
609 nir_builder *b = &state->builder;
610 nir_deref_instr *new_deref =
611 nir_deref_instr_create(b->shader, deref->deref_type);
612 new_deref->mode = deref->mode;
613 new_deref->type = deref->type;
614
615 if (deref->deref_type == nir_deref_type_var) {
616 new_deref->var = deref->var;
617 } else {
618 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
619 if (parent) {
620 parent = rematerialize_deref_in_block(parent, state);
621 new_deref->parent = nir_src_for_ssa(&parent->dest.ssa);
622 } else {
623 nir_src_copy(&new_deref->parent, &deref->parent, new_deref);
624 }
625 }
626
627 switch (deref->deref_type) {
628 case nir_deref_type_var:
629 case nir_deref_type_array_wildcard:
630 case nir_deref_type_cast:
631 /* Nothing more to do */
632 break;
633
634 case nir_deref_type_array:
635 assert(!nir_src_as_deref(deref->arr.index));
636 nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref);
637 break;
638
639 case nir_deref_type_struct:
640 new_deref->strct.index = deref->strct.index;
641 break;
642
643 default:
644 unreachable("Invalid deref instruction type");
645 }
646
647 nir_ssa_dest_init(&new_deref->instr, &new_deref->dest,
648 deref->dest.ssa.num_components,
649 deref->dest.ssa.bit_size,
650 deref->dest.ssa.name);
651 nir_builder_instr_insert(b, &new_deref->instr);
652
653 return new_deref;
654 }
655
656 static bool
657 rematerialize_deref_src(nir_src *src, void *_state)
658 {
659 struct rematerialize_deref_state *state = _state;
660
661 nir_deref_instr *deref = nir_src_as_deref(*src);
662 if (!deref)
663 return true;
664
665 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
666 if (block_deref != deref) {
667 nir_instr_rewrite_src(src->parent_instr, src,
668 nir_src_for_ssa(&block_deref->dest.ssa));
669 nir_deref_instr_remove_if_unused(deref);
670 state->progress = true;
671 }
672
673 return true;
674 }
675
676 /** Re-materialize derefs in every block
677 *
678 * This pass re-materializes deref instructions in every block in which it is
679 * used. After this pass has been run, every use of a deref will be of a
680 * deref in the same block as the use. Also, all unused derefs will be
681 * deleted as a side-effect.
682 *
683 * Derefs used as sources of phi instructions are not rematerialized.
684 */
685 bool
686 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
687 {
688 struct rematerialize_deref_state state = { 0 };
689 nir_builder_init(&state.builder, impl);
690
691 nir_foreach_block(block, impl) {
692 state.block = block;
693
694 /* Start each block with a fresh cache */
695 if (state.cache)
696 _mesa_hash_table_clear(state.cache, NULL);
697
698 nir_foreach_instr_safe(instr, block) {
699 if (instr->type == nir_instr_type_deref &&
700 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
701 continue;
702
703 /* If a deref is used in a phi, we can't rematerialize it, as the new
704 * derefs would appear before the phi, which is not valid.
705 */
706 if (instr->type == nir_instr_type_phi)
707 continue;
708
709 state.builder.cursor = nir_before_instr(instr);
710 nir_foreach_src(instr, rematerialize_deref_src, &state);
711 }
712
713 #ifndef NDEBUG
714 nir_if *following_if = nir_block_get_following_if(block);
715 if (following_if)
716 assert(!nir_src_as_deref(following_if->condition));
717 #endif
718 }
719
720 _mesa_hash_table_destroy(state.cache, NULL);
721
722 return state.progress;
723 }
724
725 static bool
726 is_trivial_deref_cast(nir_deref_instr *cast)
727 {
728 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
729 if (!parent)
730 return false;
731
732 return cast->mode == parent->mode &&
733 cast->type == parent->type &&
734 cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
735 cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
736 }
737
738 static bool
739 is_trivial_array_deref_cast(nir_deref_instr *cast)
740 {
741 assert(is_trivial_deref_cast(cast));
742
743 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
744
745 if (parent->deref_type == nir_deref_type_array) {
746 return cast->cast.ptr_stride ==
747 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
748 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
749 return cast->cast.ptr_stride ==
750 nir_deref_instr_ptr_as_array_stride(parent);
751 } else {
752 return false;
753 }
754 }
755
756 static bool
757 is_deref_ptr_as_array(nir_instr *instr)
758 {
759 return instr->type == nir_instr_type_deref &&
760 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
761 }
762
763 /**
764 * Remove casts that just wrap other casts.
765 */
766 static bool
767 opt_remove_cast_cast(nir_deref_instr *cast)
768 {
769 nir_deref_instr *first_cast = cast;
770
771 while (true) {
772 nir_deref_instr *parent = nir_deref_instr_parent(first_cast);
773 if (parent == NULL || parent->deref_type != nir_deref_type_cast)
774 break;
775 first_cast = parent;
776 }
777 if (cast == first_cast)
778 return false;
779
780 nir_instr_rewrite_src(&cast->instr, &cast->parent,
781 nir_src_for_ssa(first_cast->parent.ssa));
782 return true;
783 }
784
785 /**
786 * Is this casting a struct to a contained struct.
787 * struct a { struct b field0 };
788 * ssa_5 is structa;
789 * deref_cast (structb *)ssa_5 (function_temp structb);
790 * converts to
791 * deref_struct &ssa_5->field0 (function_temp structb);
792 * This allows subsequent copy propagation to work.
793 */
794 static bool
795 opt_replace_struct_wrapper_cast(nir_builder *b, nir_deref_instr *cast)
796 {
797 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
798 if (!parent)
799 return false;
800
801 if (!glsl_type_is_struct(parent->type))
802 return false;
803
804 if (glsl_get_struct_field_offset(parent->type, 0) != 0)
805 return false;
806
807 if (cast->type != glsl_get_struct_field(parent->type, 0))
808 return false;
809
810 nir_deref_instr *replace = nir_build_deref_struct(b, parent, 0);
811 nir_ssa_def_rewrite_uses(&cast->dest.ssa, nir_src_for_ssa(&replace->dest.ssa));
812 nir_deref_instr_remove_if_unused(cast);
813 return true;
814 }
815
816 static bool
817 opt_deref_cast(nir_builder *b, nir_deref_instr *cast)
818 {
819 bool progress;
820
821 if (opt_replace_struct_wrapper_cast(b, cast))
822 return true;
823
824 progress = opt_remove_cast_cast(cast);
825 if (!is_trivial_deref_cast(cast))
826 return progress;
827
828 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
829
830 assert(cast->dest.is_ssa);
831 assert(cast->parent.is_ssa);
832
833 nir_foreach_use_safe(use_src, &cast->dest.ssa) {
834 /* If this isn't a trivial array cast, we can't propagate into
835 * ptr_as_array derefs.
836 */
837 if (is_deref_ptr_as_array(use_src->parent_instr) &&
838 !trivial_array_cast)
839 continue;
840
841 nir_instr_rewrite_src(use_src->parent_instr, use_src, cast->parent);
842 progress = true;
843 }
844
845 /* If uses would be a bit crazy */
846 assert(list_empty(&cast->dest.ssa.if_uses));
847
848 nir_deref_instr_remove_if_unused(cast);
849 return progress;
850 }
851
852 static bool
853 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
854 {
855 assert(deref->deref_type == nir_deref_type_ptr_as_array);
856
857 nir_deref_instr *parent = nir_deref_instr_parent(deref);
858
859 if (nir_src_is_const(deref->arr.index) &&
860 nir_src_as_int(deref->arr.index) == 0) {
861 /* If it's a ptr_as_array deref with an index of 0, it does nothing
862 * and we can just replace its uses with its parent.
863 *
864 * The source of a ptr_as_array deref always has a deref_type of
865 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
866 * may be trivial and we may be able to get rid of that too. Any
867 * trivial cast of trivial cast cases should be handled already by
868 * opt_deref_cast() above.
869 */
870 if (parent->deref_type == nir_deref_type_cast &&
871 is_trivial_deref_cast(parent))
872 parent = nir_deref_instr_parent(parent);
873 nir_ssa_def_rewrite_uses(&deref->dest.ssa,
874 nir_src_for_ssa(&parent->dest.ssa));
875 nir_instr_remove(&deref->instr);
876 return true;
877 }
878
879 if (parent->deref_type != nir_deref_type_array &&
880 parent->deref_type != nir_deref_type_ptr_as_array)
881 return false;
882
883 assert(parent->parent.is_ssa);
884 assert(parent->arr.index.is_ssa);
885 assert(deref->arr.index.is_ssa);
886
887 nir_ssa_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
888 deref->arr.index.ssa);
889
890 deref->deref_type = parent->deref_type;
891 nir_instr_rewrite_src(&deref->instr, &deref->parent, parent->parent);
892 nir_instr_rewrite_src(&deref->instr, &deref->arr.index,
893 nir_src_for_ssa(new_idx));
894 return true;
895 }
896
897 bool
898 nir_opt_deref_impl(nir_function_impl *impl)
899 {
900 bool progress = false;
901
902 nir_builder b;
903 nir_builder_init(&b, impl);
904
905 nir_foreach_block(block, impl) {
906 nir_foreach_instr_safe(instr, block) {
907 if (instr->type != nir_instr_type_deref)
908 continue;
909
910 b.cursor = nir_before_instr(instr);
911
912 nir_deref_instr *deref = nir_instr_as_deref(instr);
913 switch (deref->deref_type) {
914 case nir_deref_type_ptr_as_array:
915 if (opt_deref_ptr_as_array(&b, deref))
916 progress = true;
917 break;
918
919 case nir_deref_type_cast:
920 if (opt_deref_cast(&b, deref))
921 progress = true;
922 break;
923
924 default:
925 /* Do nothing */
926 break;
927 }
928 }
929 }
930
931 if (progress) {
932 nir_metadata_preserve(impl, nir_metadata_block_index |
933 nir_metadata_dominance);
934 } else {
935 #ifndef NDEBUG
936 impl->valid_metadata &= ~nir_metadata_not_properly_reset;
937 #endif
938 }
939
940 return progress;
941 }
942
943 bool
944 nir_opt_deref(nir_shader *shader)
945 {
946 bool progress = false;
947
948 nir_foreach_function(func, shader) {
949 if (func->impl && nir_opt_deref_impl(func->impl))
950 progress = true;
951 }
952
953 return progress;
954 }