nir: Make nir_deref_instr_build/get_const_offset actually use size_align.
[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 #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 unsigned
125 nir_deref_instr_ptr_as_array_stride(nir_deref_instr *deref)
126 {
127 assert(deref->deref_type == nir_deref_type_ptr_as_array);
128 nir_deref_instr *parent = nir_deref_instr_parent(deref);
129 switch (parent->deref_type) {
130 case nir_deref_type_array:
131 return glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
132 case nir_deref_type_ptr_as_array:
133 return nir_deref_instr_ptr_as_array_stride(parent);
134 case nir_deref_type_cast:
135 return parent->cast.ptr_stride;
136 default:
137 unreachable("Invalid parent for ptr_as_array deref");
138 }
139 }
140
141 static unsigned
142 type_get_array_stride(const struct glsl_type *elem_type,
143 glsl_type_size_align_func size_align)
144 {
145 unsigned elem_size, elem_align;
146 size_align(elem_type, &elem_size, &elem_align);
147 return ALIGN_POT(elem_size, elem_align);
148 }
149
150 static unsigned
151 struct_type_get_field_offset(const struct glsl_type *struct_type,
152 glsl_type_size_align_func size_align,
153 unsigned field_idx)
154 {
155 assert(glsl_type_is_struct(struct_type));
156 unsigned offset = 0;
157 for (unsigned i = 0; i <= field_idx; i++) {
158 unsigned elem_size, elem_align;
159 size_align(glsl_get_struct_field(struct_type, i), &elem_size, &elem_align);
160 offset = ALIGN_POT(offset, elem_align);
161 if (i < field_idx)
162 offset += elem_size;
163 }
164 return offset;
165 }
166
167 unsigned
168 nir_deref_instr_get_const_offset(nir_deref_instr *deref,
169 glsl_type_size_align_func size_align)
170 {
171 nir_deref_path path;
172 nir_deref_path_init(&path, deref, NULL);
173
174 assert(path.path[0]->deref_type == nir_deref_type_var);
175
176 unsigned offset = 0;
177 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
178 if ((*p)->deref_type == nir_deref_type_array) {
179 offset += nir_src_as_uint((*p)->arr.index) *
180 type_get_array_stride((*p)->type, size_align);
181 } else if ((*p)->deref_type == nir_deref_type_struct) {
182 /* p starts at path[1], so this is safe */
183 nir_deref_instr *parent = *(p - 1);
184 offset += struct_type_get_field_offset(parent->type, size_align,
185 (*p)->strct.index);
186 } else {
187 unreachable("Unsupported deref type");
188 }
189 }
190
191 nir_deref_path_finish(&path);
192
193 return offset;
194 }
195
196 nir_ssa_def *
197 nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
198 glsl_type_size_align_func size_align)
199 {
200 nir_deref_path path;
201 nir_deref_path_init(&path, deref, NULL);
202
203 assert(path.path[0]->deref_type == nir_deref_type_var);
204
205 nir_ssa_def *offset = nir_imm_int(b, 0);
206 for (nir_deref_instr **p = &path.path[1]; *p; p++) {
207 if ((*p)->deref_type == nir_deref_type_array) {
208 nir_ssa_def *index = nir_ssa_for_src(b, (*p)->arr.index, 1);
209 nir_ssa_def *stride =
210 nir_imm_int(b, type_get_array_stride((*p)->type, size_align));
211 offset = nir_iadd(b, offset, nir_imul(b, index, stride));
212 } else if ((*p)->deref_type == nir_deref_type_struct) {
213 /* p starts at path[1], so this is safe */
214 nir_deref_instr *parent = *(p - 1);
215 unsigned field_offset =
216 struct_type_get_field_offset(parent->type, size_align,
217 (*p)->strct.index);
218 nir_iadd(b, offset, nir_imm_int(b, field_offset));
219 } else {
220 unreachable("Unsupported deref type");
221 }
222 }
223
224 nir_deref_path_finish(&path);
225
226 return offset;
227 }
228
229 bool
230 nir_remove_dead_derefs_impl(nir_function_impl *impl)
231 {
232 bool progress = false;
233
234 nir_foreach_block(block, impl) {
235 nir_foreach_instr_safe(instr, block) {
236 if (instr->type == nir_instr_type_deref &&
237 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr)))
238 progress = true;
239 }
240 }
241
242 if (progress)
243 nir_metadata_preserve(impl, nir_metadata_block_index |
244 nir_metadata_dominance);
245
246 return progress;
247 }
248
249 bool
250 nir_remove_dead_derefs(nir_shader *shader)
251 {
252 bool progress = false;
253 nir_foreach_function(function, shader) {
254 if (function->impl && nir_remove_dead_derefs_impl(function->impl))
255 progress = true;
256 }
257
258 return progress;
259 }
260
261 void
262 nir_fixup_deref_modes(nir_shader *shader)
263 {
264 nir_foreach_function(function, shader) {
265 if (!function->impl)
266 continue;
267
268 nir_foreach_block(block, function->impl) {
269 nir_foreach_instr(instr, block) {
270 if (instr->type != nir_instr_type_deref)
271 continue;
272
273 nir_deref_instr *deref = nir_instr_as_deref(instr);
274 if (deref->deref_type == nir_deref_type_cast)
275 continue;
276
277 nir_variable_mode parent_mode;
278 if (deref->deref_type == nir_deref_type_var) {
279 parent_mode = deref->var->data.mode;
280 } else {
281 assert(deref->parent.is_ssa);
282 nir_deref_instr *parent =
283 nir_instr_as_deref(deref->parent.ssa->parent_instr);
284 parent_mode = parent->mode;
285 }
286
287 deref->mode = parent_mode;
288 }
289 }
290 }
291 }
292
293 static bool
294 modes_may_alias(nir_variable_mode a, nir_variable_mode b)
295 {
296 /* Two pointers can only alias if they have the same mode.
297 *
298 * NOTE: In future, with things like OpenCL generic pointers, this may not
299 * be true and will have to be re-evaluated. However, with graphics only,
300 * it should be safe.
301 */
302 return a == b;
303 }
304
305 nir_deref_compare_result
306 nir_compare_deref_paths(nir_deref_path *a_path,
307 nir_deref_path *b_path)
308 {
309 if (!modes_may_alias(b_path->path[0]->mode, a_path->path[0]->mode))
310 return nir_derefs_do_not_alias;
311
312 if (a_path->path[0]->deref_type != b_path->path[0]->deref_type)
313 return nir_derefs_may_alias_bit;
314
315 if (a_path->path[0]->deref_type == nir_deref_type_var) {
316 /* If we can chase the deref all the way back to the variable and
317 * they're not the same variable, we know they can't possibly alias.
318 */
319 if (a_path->path[0]->var != b_path->path[0]->var)
320 return nir_derefs_do_not_alias;
321 } else {
322 assert(a_path->path[0]->deref_type == nir_deref_type_cast);
323 /* If they're not exactly the same cast, it's hard to compare them so we
324 * just assume they alias. Comparing casts is tricky as there are lots
325 * of things such as mode, type, etc. to make sure work out; for now, we
326 * just assume nit_opt_deref will combine them and compare the deref
327 * instructions.
328 *
329 * TODO: At some point in the future, we could be clever and understand
330 * that a float[] and int[] have the same layout and aliasing structure
331 * but double[] and vec3[] do not and we could potentially be a bit
332 * smarter here.
333 */
334 if (a_path->path[0] != b_path->path[0])
335 return nir_derefs_may_alias_bit;
336 }
337
338 /* Start off assuming they fully compare. We ignore equality for now. In
339 * the end, we'll determine that by containment.
340 */
341 nir_deref_compare_result result = nir_derefs_may_alias_bit |
342 nir_derefs_a_contains_b_bit |
343 nir_derefs_b_contains_a_bit;
344
345 nir_deref_instr **a_p = &a_path->path[1];
346 nir_deref_instr **b_p = &b_path->path[1];
347 while (*a_p != NULL && *a_p == *b_p) {
348 a_p++;
349 b_p++;
350 }
351
352 /* We're at either the tail or the divergence point between the two deref
353 * paths. Look to see if either contains a ptr_as_array deref. It it
354 * does we don't know how to safely make any inferences. Hopefully,
355 * nir_opt_deref will clean most of these up and we can start inferring
356 * things again.
357 *
358 * In theory, we could do a bit better. For instance, we could detect the
359 * case where we have exactly one ptr_as_array deref in the chain after the
360 * divergence point and it's matched in both chains and the two chains have
361 * different constant indices.
362 */
363 for (nir_deref_instr **t_p = a_p; *t_p; t_p++) {
364 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
365 return nir_derefs_may_alias_bit;
366 }
367 for (nir_deref_instr **t_p = b_p; *t_p; t_p++) {
368 if ((*t_p)->deref_type == nir_deref_type_ptr_as_array)
369 return nir_derefs_may_alias_bit;
370 }
371
372 while (*a_p != NULL && *b_p != NULL) {
373 nir_deref_instr *a_tail = *(a_p++);
374 nir_deref_instr *b_tail = *(b_p++);
375
376 switch (a_tail->deref_type) {
377 case nir_deref_type_array:
378 case nir_deref_type_array_wildcard: {
379 assert(b_tail->deref_type == nir_deref_type_array ||
380 b_tail->deref_type == nir_deref_type_array_wildcard);
381
382 if (a_tail->deref_type == nir_deref_type_array_wildcard) {
383 if (b_tail->deref_type != nir_deref_type_array_wildcard)
384 result &= ~nir_derefs_b_contains_a_bit;
385 } else if (b_tail->deref_type == nir_deref_type_array_wildcard) {
386 if (a_tail->deref_type != nir_deref_type_array_wildcard)
387 result &= ~nir_derefs_a_contains_b_bit;
388 } else {
389 assert(a_tail->deref_type == nir_deref_type_array &&
390 b_tail->deref_type == nir_deref_type_array);
391 assert(a_tail->arr.index.is_ssa && b_tail->arr.index.is_ssa);
392
393 if (nir_src_is_const(a_tail->arr.index) &&
394 nir_src_is_const(b_tail->arr.index)) {
395 /* If they're both direct and have different offsets, they
396 * don't even alias much less anything else.
397 */
398 if (nir_src_as_uint(a_tail->arr.index) !=
399 nir_src_as_uint(b_tail->arr.index))
400 return nir_derefs_do_not_alias;
401 } else if (a_tail->arr.index.ssa == b_tail->arr.index.ssa) {
402 /* They're the same indirect, continue on */
403 } else {
404 /* They're not the same index so we can't prove anything about
405 * containment.
406 */
407 result &= ~(nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit);
408 }
409 }
410 break;
411 }
412
413 case nir_deref_type_struct: {
414 /* If they're different struct members, they don't even alias */
415 if (a_tail->strct.index != b_tail->strct.index)
416 return nir_derefs_do_not_alias;
417 break;
418 }
419
420 default:
421 unreachable("Invalid deref type");
422 }
423 }
424
425 /* If a is longer than b, then it can't contain b */
426 if (*a_p != NULL)
427 result &= ~nir_derefs_a_contains_b_bit;
428 if (*b_p != NULL)
429 result &= ~nir_derefs_b_contains_a_bit;
430
431 /* If a contains b and b contains a they must be equal. */
432 if ((result & nir_derefs_a_contains_b_bit) && (result & nir_derefs_b_contains_a_bit))
433 result |= nir_derefs_equal_bit;
434
435 return result;
436 }
437
438 nir_deref_compare_result
439 nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
440 {
441 if (a == b) {
442 return nir_derefs_equal_bit | nir_derefs_may_alias_bit |
443 nir_derefs_a_contains_b_bit | nir_derefs_b_contains_a_bit;
444 }
445
446 nir_deref_path a_path, b_path;
447 nir_deref_path_init(&a_path, a, NULL);
448 nir_deref_path_init(&b_path, b, NULL);
449 assert(a_path.path[0]->deref_type == nir_deref_type_var ||
450 a_path.path[0]->deref_type == nir_deref_type_cast);
451 assert(b_path.path[0]->deref_type == nir_deref_type_var ||
452 b_path.path[0]->deref_type == nir_deref_type_cast);
453
454 nir_deref_compare_result result = nir_compare_deref_paths(&a_path, &b_path);
455
456 nir_deref_path_finish(&a_path);
457 nir_deref_path_finish(&b_path);
458
459 return result;
460 }
461
462 struct rematerialize_deref_state {
463 bool progress;
464 nir_builder builder;
465 nir_block *block;
466 struct hash_table *cache;
467 };
468
469 static nir_deref_instr *
470 rematerialize_deref_in_block(nir_deref_instr *deref,
471 struct rematerialize_deref_state *state)
472 {
473 if (deref->instr.block == state->block)
474 return deref;
475
476 if (!state->cache) {
477 state->cache = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
478 _mesa_key_pointer_equal);
479 }
480
481 struct hash_entry *cached = _mesa_hash_table_search(state->cache, deref);
482 if (cached)
483 return cached->data;
484
485 nir_builder *b = &state->builder;
486 nir_deref_instr *new_deref =
487 nir_deref_instr_create(b->shader, deref->deref_type);
488 new_deref->mode = deref->mode;
489 new_deref->type = deref->type;
490
491 if (deref->deref_type == nir_deref_type_var) {
492 new_deref->var = deref->var;
493 } else {
494 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
495 if (parent) {
496 parent = rematerialize_deref_in_block(parent, state);
497 new_deref->parent = nir_src_for_ssa(&parent->dest.ssa);
498 } else {
499 nir_src_copy(&new_deref->parent, &deref->parent, new_deref);
500 }
501 }
502
503 switch (deref->deref_type) {
504 case nir_deref_type_var:
505 case nir_deref_type_array_wildcard:
506 case nir_deref_type_cast:
507 /* Nothing more to do */
508 break;
509
510 case nir_deref_type_array:
511 assert(!nir_src_as_deref(deref->arr.index));
512 nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref);
513 break;
514
515 case nir_deref_type_struct:
516 new_deref->strct.index = deref->strct.index;
517 break;
518
519 default:
520 unreachable("Invalid deref instruction type");
521 }
522
523 nir_ssa_dest_init(&new_deref->instr, &new_deref->dest,
524 deref->dest.ssa.num_components,
525 deref->dest.ssa.bit_size,
526 deref->dest.ssa.name);
527 nir_builder_instr_insert(b, &new_deref->instr);
528
529 return new_deref;
530 }
531
532 static bool
533 rematerialize_deref_src(nir_src *src, void *_state)
534 {
535 struct rematerialize_deref_state *state = _state;
536
537 nir_deref_instr *deref = nir_src_as_deref(*src);
538 if (!deref)
539 return true;
540
541 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
542 if (block_deref != deref) {
543 nir_instr_rewrite_src(src->parent_instr, src,
544 nir_src_for_ssa(&block_deref->dest.ssa));
545 nir_deref_instr_remove_if_unused(deref);
546 state->progress = true;
547 }
548
549 return true;
550 }
551
552 /** Re-materialize derefs in every block
553 *
554 * This pass re-materializes deref instructions in every block in which it is
555 * used. After this pass has been run, every use of a deref will be of a
556 * deref in the same block as the use. Also, all unused derefs will be
557 * deleted as a side-effect.
558 */
559 bool
560 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
561 {
562 struct rematerialize_deref_state state = { 0 };
563 nir_builder_init(&state.builder, impl);
564
565 nir_foreach_block(block, impl) {
566 state.block = block;
567
568 /* Start each block with a fresh cache */
569 if (state.cache)
570 _mesa_hash_table_clear(state.cache, NULL);
571
572 nir_foreach_instr_safe(instr, block) {
573 if (instr->type == nir_instr_type_deref) {
574 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr));
575 continue;
576 }
577
578 state.builder.cursor = nir_before_instr(instr);
579 nir_foreach_src(instr, rematerialize_deref_src, &state);
580 }
581
582 #ifndef NDEBUG
583 nir_if *following_if = nir_block_get_following_if(block);
584 if (following_if)
585 assert(!nir_src_as_deref(following_if->condition));
586 #endif
587 }
588
589 _mesa_hash_table_destroy(state.cache, NULL);
590
591 return state.progress;
592 }
593
594 static bool
595 is_trivial_deref_cast(nir_deref_instr *cast)
596 {
597 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
598 if (!parent)
599 return false;
600
601 return cast->mode == parent->mode &&
602 cast->type == parent->type &&
603 cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
604 cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
605 }
606
607 static bool
608 is_trivial_array_deref_cast(nir_deref_instr *cast)
609 {
610 assert(is_trivial_deref_cast(cast));
611
612 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
613
614 if (parent->deref_type == nir_deref_type_array) {
615 return cast->cast.ptr_stride ==
616 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
617 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
618 return cast->cast.ptr_stride ==
619 nir_deref_instr_ptr_as_array_stride(parent);
620 } else {
621 return false;
622 }
623 }
624
625 static bool
626 is_deref_ptr_as_array(nir_instr *instr)
627 {
628 return instr->type == nir_instr_type_deref &&
629 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
630 }
631
632 static bool
633 opt_deref_cast(nir_deref_instr *cast)
634 {
635 if (!is_trivial_deref_cast(cast))
636 return false;
637
638 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
639
640 assert(cast->dest.is_ssa);
641 assert(cast->parent.is_ssa);
642
643 bool progress = false;
644 nir_foreach_use_safe(use_src, &cast->dest.ssa) {
645 /* If this isn't a trivial array cast, we can't propagate into
646 * ptr_as_array derefs.
647 */
648 if (is_deref_ptr_as_array(use_src->parent_instr) &&
649 !trivial_array_cast)
650 continue;
651
652 nir_instr_rewrite_src(use_src->parent_instr, use_src, cast->parent);
653 progress = true;
654 }
655
656 /* If uses would be a bit crazy */
657 assert(list_empty(&cast->dest.ssa.if_uses));
658
659 nir_deref_instr_remove_if_unused(cast);
660 return progress;
661 }
662
663 static bool
664 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
665 {
666 assert(deref->deref_type == nir_deref_type_ptr_as_array);
667
668 nir_deref_instr *parent = nir_deref_instr_parent(deref);
669 if (parent->deref_type != nir_deref_type_array &&
670 parent->deref_type != nir_deref_type_ptr_as_array)
671 return false;
672
673 assert(parent->parent.is_ssa);
674 assert(parent->arr.index.is_ssa);
675 assert(deref->arr.index.is_ssa);
676
677 nir_ssa_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
678 deref->arr.index.ssa);
679
680 deref->deref_type = parent->deref_type;
681 nir_instr_rewrite_src(&deref->instr, &deref->parent, parent->parent);
682 nir_instr_rewrite_src(&deref->instr, &deref->arr.index,
683 nir_src_for_ssa(new_idx));
684 return true;
685 }
686
687 static bool
688 nir_opt_deref_impl(nir_function_impl *impl)
689 {
690 bool progress = false;
691
692 nir_builder b;
693 nir_builder_init(&b, impl);
694
695 nir_foreach_block(block, impl) {
696 nir_foreach_instr_safe(instr, block) {
697 if (instr->type != nir_instr_type_deref)
698 continue;
699
700 b.cursor = nir_before_instr(instr);
701
702 nir_deref_instr *deref = nir_instr_as_deref(instr);
703 switch (deref->deref_type) {
704 case nir_deref_type_ptr_as_array:
705 if (opt_deref_ptr_as_array(&b, deref))
706 progress = true;
707 break;
708
709 case nir_deref_type_cast:
710 if (opt_deref_cast(deref))
711 progress = true;
712 break;
713
714 default:
715 /* Do nothing */
716 break;
717 }
718 }
719 }
720
721 if (progress) {
722 nir_metadata_preserve(impl, nir_metadata_block_index |
723 nir_metadata_dominance);
724 }
725
726 return progress;
727 }
728
729 bool
730 nir_opt_deref(nir_shader *shader)
731 {
732 bool progress = false;
733
734 nir_foreach_function(func, shader) {
735 if (func->impl && nir_opt_deref_impl(func->impl))
736 progress = true;
737 }
738
739 return progress;
740 }