src/compiler: use new hash table and set creation helpers
[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_pointer_hash_table_create(NULL);
478 }
479
480 struct hash_entry *cached = _mesa_hash_table_search(state->cache, deref);
481 if (cached)
482 return cached->data;
483
484 nir_builder *b = &state->builder;
485 nir_deref_instr *new_deref =
486 nir_deref_instr_create(b->shader, deref->deref_type);
487 new_deref->mode = deref->mode;
488 new_deref->type = deref->type;
489
490 if (deref->deref_type == nir_deref_type_var) {
491 new_deref->var = deref->var;
492 } else {
493 nir_deref_instr *parent = nir_src_as_deref(deref->parent);
494 if (parent) {
495 parent = rematerialize_deref_in_block(parent, state);
496 new_deref->parent = nir_src_for_ssa(&parent->dest.ssa);
497 } else {
498 nir_src_copy(&new_deref->parent, &deref->parent, new_deref);
499 }
500 }
501
502 switch (deref->deref_type) {
503 case nir_deref_type_var:
504 case nir_deref_type_array_wildcard:
505 case nir_deref_type_cast:
506 /* Nothing more to do */
507 break;
508
509 case nir_deref_type_array:
510 assert(!nir_src_as_deref(deref->arr.index));
511 nir_src_copy(&new_deref->arr.index, &deref->arr.index, new_deref);
512 break;
513
514 case nir_deref_type_struct:
515 new_deref->strct.index = deref->strct.index;
516 break;
517
518 default:
519 unreachable("Invalid deref instruction type");
520 }
521
522 nir_ssa_dest_init(&new_deref->instr, &new_deref->dest,
523 deref->dest.ssa.num_components,
524 deref->dest.ssa.bit_size,
525 deref->dest.ssa.name);
526 nir_builder_instr_insert(b, &new_deref->instr);
527
528 return new_deref;
529 }
530
531 static bool
532 rematerialize_deref_src(nir_src *src, void *_state)
533 {
534 struct rematerialize_deref_state *state = _state;
535
536 nir_deref_instr *deref = nir_src_as_deref(*src);
537 if (!deref)
538 return true;
539
540 nir_deref_instr *block_deref = rematerialize_deref_in_block(deref, state);
541 if (block_deref != deref) {
542 nir_instr_rewrite_src(src->parent_instr, src,
543 nir_src_for_ssa(&block_deref->dest.ssa));
544 nir_deref_instr_remove_if_unused(deref);
545 state->progress = true;
546 }
547
548 return true;
549 }
550
551 /** Re-materialize derefs in every block
552 *
553 * This pass re-materializes deref instructions in every block in which it is
554 * used. After this pass has been run, every use of a deref will be of a
555 * deref in the same block as the use. Also, all unused derefs will be
556 * deleted as a side-effect.
557 */
558 bool
559 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl)
560 {
561 struct rematerialize_deref_state state = { 0 };
562 nir_builder_init(&state.builder, impl);
563
564 nir_foreach_block(block, impl) {
565 state.block = block;
566
567 /* Start each block with a fresh cache */
568 if (state.cache)
569 _mesa_hash_table_clear(state.cache, NULL);
570
571 nir_foreach_instr_safe(instr, block) {
572 if (instr->type == nir_instr_type_deref) {
573 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr));
574 continue;
575 }
576
577 state.builder.cursor = nir_before_instr(instr);
578 nir_foreach_src(instr, rematerialize_deref_src, &state);
579 }
580
581 #ifndef NDEBUG
582 nir_if *following_if = nir_block_get_following_if(block);
583 if (following_if)
584 assert(!nir_src_as_deref(following_if->condition));
585 #endif
586 }
587
588 _mesa_hash_table_destroy(state.cache, NULL);
589
590 return state.progress;
591 }
592
593 static bool
594 is_trivial_deref_cast(nir_deref_instr *cast)
595 {
596 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
597 if (!parent)
598 return false;
599
600 return cast->mode == parent->mode &&
601 cast->type == parent->type &&
602 cast->dest.ssa.num_components == parent->dest.ssa.num_components &&
603 cast->dest.ssa.bit_size == parent->dest.ssa.bit_size;
604 }
605
606 static bool
607 is_trivial_array_deref_cast(nir_deref_instr *cast)
608 {
609 assert(is_trivial_deref_cast(cast));
610
611 nir_deref_instr *parent = nir_src_as_deref(cast->parent);
612
613 if (parent->deref_type == nir_deref_type_array) {
614 return cast->cast.ptr_stride ==
615 glsl_get_explicit_stride(nir_deref_instr_parent(parent)->type);
616 } else if (parent->deref_type == nir_deref_type_ptr_as_array) {
617 return cast->cast.ptr_stride ==
618 nir_deref_instr_ptr_as_array_stride(parent);
619 } else {
620 return false;
621 }
622 }
623
624 static bool
625 is_deref_ptr_as_array(nir_instr *instr)
626 {
627 return instr->type == nir_instr_type_deref &&
628 nir_instr_as_deref(instr)->deref_type == nir_deref_type_ptr_as_array;
629 }
630
631 static bool
632 opt_deref_cast(nir_deref_instr *cast)
633 {
634 if (!is_trivial_deref_cast(cast))
635 return false;
636
637 bool trivial_array_cast = is_trivial_array_deref_cast(cast);
638
639 assert(cast->dest.is_ssa);
640 assert(cast->parent.is_ssa);
641
642 bool progress = false;
643 nir_foreach_use_safe(use_src, &cast->dest.ssa) {
644 /* If this isn't a trivial array cast, we can't propagate into
645 * ptr_as_array derefs.
646 */
647 if (is_deref_ptr_as_array(use_src->parent_instr) &&
648 !trivial_array_cast)
649 continue;
650
651 nir_instr_rewrite_src(use_src->parent_instr, use_src, cast->parent);
652 progress = true;
653 }
654
655 /* If uses would be a bit crazy */
656 assert(list_empty(&cast->dest.ssa.if_uses));
657
658 nir_deref_instr_remove_if_unused(cast);
659 return progress;
660 }
661
662 static bool
663 opt_deref_ptr_as_array(nir_builder *b, nir_deref_instr *deref)
664 {
665 assert(deref->deref_type == nir_deref_type_ptr_as_array);
666
667 nir_deref_instr *parent = nir_deref_instr_parent(deref);
668 if (parent->deref_type != nir_deref_type_array &&
669 parent->deref_type != nir_deref_type_ptr_as_array)
670 return false;
671
672 assert(parent->parent.is_ssa);
673 assert(parent->arr.index.is_ssa);
674 assert(deref->arr.index.is_ssa);
675
676 nir_ssa_def *new_idx = nir_iadd(b, parent->arr.index.ssa,
677 deref->arr.index.ssa);
678
679 deref->deref_type = parent->deref_type;
680 nir_instr_rewrite_src(&deref->instr, &deref->parent, parent->parent);
681 nir_instr_rewrite_src(&deref->instr, &deref->arr.index,
682 nir_src_for_ssa(new_idx));
683 return true;
684 }
685
686 static bool
687 nir_opt_deref_impl(nir_function_impl *impl)
688 {
689 bool progress = false;
690
691 nir_builder b;
692 nir_builder_init(&b, impl);
693
694 nir_foreach_block(block, impl) {
695 nir_foreach_instr_safe(instr, block) {
696 if (instr->type != nir_instr_type_deref)
697 continue;
698
699 b.cursor = nir_before_instr(instr);
700
701 nir_deref_instr *deref = nir_instr_as_deref(instr);
702 switch (deref->deref_type) {
703 case nir_deref_type_ptr_as_array:
704 if (opt_deref_ptr_as_array(&b, deref))
705 progress = true;
706 break;
707
708 case nir_deref_type_cast:
709 if (opt_deref_cast(deref))
710 progress = true;
711 break;
712
713 default:
714 /* Do nothing */
715 break;
716 }
717 }
718 }
719
720 if (progress) {
721 nir_metadata_preserve(impl, nir_metadata_block_index |
722 nir_metadata_dominance);
723 } else {
724 #ifndef NDEBUG
725 impl->valid_metadata &= ~nir_metadata_not_properly_reset;
726 #endif
727 }
728
729 return progress;
730 }
731
732 bool
733 nir_opt_deref(nir_shader *shader)
734 {
735 bool progress = false;
736
737 nir_foreach_function(func, shader) {
738 if (func->impl && nir_opt_deref_impl(func->impl))
739 progress = true;
740 }
741
742 return progress;
743 }