2 * Copyright © 2018 Intel Corporation
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:
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
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
25 #include "nir_builder.h"
26 #include "nir_deref.h"
27 #include "util/hash_table.h"
30 nir_deref_path_init(nir_deref_path
*path
,
31 nir_deref_instr
*deref
, void *mem_ctx
)
33 assert(deref
!= NULL
);
35 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
36 * room for the NULL terminator.
38 static const int max_short_path_len
= ARRAY_SIZE(path
->_short_path
) - 1;
42 nir_deref_instr
**tail
= &path
->_short_path
[max_short_path_len
];
43 nir_deref_instr
**head
= tail
;
46 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
48 if (count
<= max_short_path_len
)
52 if (count
<= max_short_path_len
) {
53 /* If we're under max_short_path_len, just use the short path. */
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;
64 path
->path
= ralloc_array(mem_ctx
, nir_deref_instr
*, count
+ 1);
65 head
= tail
= path
->path
+ count
;
67 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
))
71 assert(head
== path
->path
);
72 assert(tail
== head
+ count
);
73 assert(*tail
== NULL
);
77 nir_deref_path_finish(nir_deref_path
*path
)
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
);
85 * Recursively removes unused deref instructions
88 nir_deref_instr_remove_if_unused(nir_deref_instr
*instr
)
90 bool progress
= false;
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
))
98 nir_instr_remove(&d
->instr
);
106 nir_deref_instr_has_indirect(nir_deref_instr
*instr
)
108 while (instr
->deref_type
!= nir_deref_type_var
) {
109 /* Consider casts to be indirects */
110 if (instr
->deref_type
== nir_deref_type_cast
)
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
))
118 instr
= nir_deref_instr_parent(instr
);
125 nir_deref_instr_ptr_as_array_stride(nir_deref_instr
*deref
)
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
;
137 unreachable("Invalid parent for ptr_as_array deref");
142 type_get_array_stride(const struct glsl_type
*elem_type
,
143 glsl_type_size_align_func size_align
)
145 unsigned elem_size
, elem_align
;
146 size_align(elem_type
, &elem_size
, &elem_align
);
147 return ALIGN_POT(elem_size
, elem_align
);
151 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
152 glsl_type_size_align_func size_align
,
155 assert(glsl_type_is_struct_or_ifc(struct_type
));
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
);
168 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
169 glsl_type_size_align_func size_align
)
172 nir_deref_path_init(&path
, deref
, NULL
);
174 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
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
,
187 unreachable("Unsupported deref type");
191 nir_deref_path_finish(&path
);
197 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
198 glsl_type_size_align_func size_align
)
201 nir_deref_path_init(&path
, deref
, NULL
);
203 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
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
,
218 nir_iadd(b
, offset
, nir_imm_int(b
, field_offset
));
220 unreachable("Unsupported deref type");
224 nir_deref_path_finish(&path
);
230 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
232 bool progress
= false;
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
)))
243 nir_metadata_preserve(impl
, nir_metadata_block_index
|
244 nir_metadata_dominance
);
250 nir_remove_dead_derefs(nir_shader
*shader
)
252 bool progress
= false;
253 nir_foreach_function(function
, shader
) {
254 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
262 nir_fixup_deref_modes(nir_shader
*shader
)
264 nir_foreach_function(function
, shader
) {
268 nir_foreach_block(block
, function
->impl
) {
269 nir_foreach_instr(instr
, block
) {
270 if (instr
->type
!= nir_instr_type_deref
)
273 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
274 if (deref
->deref_type
== nir_deref_type_cast
)
277 nir_variable_mode parent_mode
;
278 if (deref
->deref_type
== nir_deref_type_var
) {
279 parent_mode
= deref
->var
->data
.mode
;
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
;
287 deref
->mode
= parent_mode
;
294 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
296 /* Generic pointers can alias with SSBOs */
297 if ((a
== nir_var_mem_ssbo
|| a
== nir_var_mem_global
) &&
298 (b
== nir_var_mem_ssbo
|| b
== nir_var_mem_global
))
301 /* In the general case, pointers can only alias if they have the same mode.
303 * NOTE: In future, with things like OpenCL generic pointers, this may not
304 * be true and will have to be re-evaluated. However, with graphics only,
311 deref_path_contains_coherent_decoration(nir_deref_path
*path
)
313 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
315 if (path
->path
[0]->var
->data
.image
.access
& ACCESS_COHERENT
)
318 for (nir_deref_instr
**p
= &path
->path
[1]; *p
; p
++) {
319 if ((*p
)->deref_type
!= nir_deref_type_struct
)
322 const struct glsl_type
*struct_type
= (*(p
- 1))->type
;
323 const struct glsl_struct_field
*field
=
324 glsl_get_struct_field_data(struct_type
, (*p
)->strct
.index
);
325 if (field
->memory_coherent
)
332 nir_deref_compare_result
333 nir_compare_deref_paths(nir_deref_path
*a_path
,
334 nir_deref_path
*b_path
)
336 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
337 return nir_derefs_do_not_alias
;
339 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
340 return nir_derefs_may_alias_bit
;
342 if (a_path
->path
[0]->deref_type
== nir_deref_type_var
) {
343 if (a_path
->path
[0]->var
!= b_path
->path
[0]->var
) {
344 /* Shader and function temporaries aren't backed by memory so two
345 * distinct variables never alias.
347 static const nir_variable_mode temp_var_modes
=
348 nir_var_shader_temp
| nir_var_function_temp
;
349 if ((a_path
->path
[0]->mode
& temp_var_modes
) ||
350 (b_path
->path
[0]->mode
& temp_var_modes
))
351 return nir_derefs_do_not_alias
;
353 /* If they are both declared coherent or have coherent somewhere in
354 * their path (due to a member of an interface being declared
355 * coherent), we have to assume we that we could have any kind of
356 * aliasing. Otherwise, they could still alias but the client didn't
357 * tell us and that's their fault.
359 if (deref_path_contains_coherent_decoration(a_path
) &&
360 deref_path_contains_coherent_decoration(b_path
))
361 return nir_derefs_may_alias_bit
;
363 /* If we can chase the deref all the way back to the variable and
364 * they're not the same variable and at least one is not declared
365 * coherent, we know they can't possibly alias.
367 return nir_derefs_do_not_alias
;
370 assert(a_path
->path
[0]->deref_type
== nir_deref_type_cast
);
371 /* If they're not exactly the same cast, it's hard to compare them so we
372 * just assume they alias. Comparing casts is tricky as there are lots
373 * of things such as mode, type, etc. to make sure work out; for now, we
374 * just assume nit_opt_deref will combine them and compare the deref
377 * TODO: At some point in the future, we could be clever and understand
378 * that a float[] and int[] have the same layout and aliasing structure
379 * but double[] and vec3[] do not and we could potentially be a bit
382 if (a_path
->path
[0] != b_path
->path
[0])
383 return nir_derefs_may_alias_bit
;
386 /* Start off assuming they fully compare. We ignore equality for now. In
387 * the end, we'll determine that by containment.
389 nir_deref_compare_result result
= nir_derefs_may_alias_bit
|
390 nir_derefs_a_contains_b_bit
|
391 nir_derefs_b_contains_a_bit
;
393 nir_deref_instr
**a_p
= &a_path
->path
[1];
394 nir_deref_instr
**b_p
= &b_path
->path
[1];
395 while (*a_p
!= NULL
&& *a_p
== *b_p
) {
400 /* We're at either the tail or the divergence point between the two deref
401 * paths. Look to see if either contains a ptr_as_array deref. It it
402 * does we don't know how to safely make any inferences. Hopefully,
403 * nir_opt_deref will clean most of these up and we can start inferring
406 * In theory, we could do a bit better. For instance, we could detect the
407 * case where we have exactly one ptr_as_array deref in the chain after the
408 * divergence point and it's matched in both chains and the two chains have
409 * different constant indices.
411 for (nir_deref_instr
**t_p
= a_p
; *t_p
; t_p
++) {
412 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
413 return nir_derefs_may_alias_bit
;
415 for (nir_deref_instr
**t_p
= b_p
; *t_p
; t_p
++) {
416 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
417 return nir_derefs_may_alias_bit
;
420 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
421 nir_deref_instr
*a_tail
= *(a_p
++);
422 nir_deref_instr
*b_tail
= *(b_p
++);
424 switch (a_tail
->deref_type
) {
425 case nir_deref_type_array
:
426 case nir_deref_type_array_wildcard
: {
427 assert(b_tail
->deref_type
== nir_deref_type_array
||
428 b_tail
->deref_type
== nir_deref_type_array_wildcard
);
430 if (a_tail
->deref_type
== nir_deref_type_array_wildcard
) {
431 if (b_tail
->deref_type
!= nir_deref_type_array_wildcard
)
432 result
&= ~nir_derefs_b_contains_a_bit
;
433 } else if (b_tail
->deref_type
== nir_deref_type_array_wildcard
) {
434 if (a_tail
->deref_type
!= nir_deref_type_array_wildcard
)
435 result
&= ~nir_derefs_a_contains_b_bit
;
437 assert(a_tail
->deref_type
== nir_deref_type_array
&&
438 b_tail
->deref_type
== nir_deref_type_array
);
439 assert(a_tail
->arr
.index
.is_ssa
&& b_tail
->arr
.index
.is_ssa
);
441 if (nir_src_is_const(a_tail
->arr
.index
) &&
442 nir_src_is_const(b_tail
->arr
.index
)) {
443 /* If they're both direct and have different offsets, they
444 * don't even alias much less anything else.
446 if (nir_src_as_uint(a_tail
->arr
.index
) !=
447 nir_src_as_uint(b_tail
->arr
.index
))
448 return nir_derefs_do_not_alias
;
449 } else if (a_tail
->arr
.index
.ssa
== b_tail
->arr
.index
.ssa
) {
450 /* They're the same indirect, continue on */
452 /* They're not the same index so we can't prove anything about
455 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
461 case nir_deref_type_struct
: {
462 /* If they're different struct members, they don't even alias */
463 if (a_tail
->strct
.index
!= b_tail
->strct
.index
)
464 return nir_derefs_do_not_alias
;
469 unreachable("Invalid deref type");
473 /* If a is longer than b, then it can't contain b */
475 result
&= ~nir_derefs_a_contains_b_bit
;
477 result
&= ~nir_derefs_b_contains_a_bit
;
479 /* If a contains b and b contains a they must be equal. */
480 if ((result
& nir_derefs_a_contains_b_bit
) && (result
& nir_derefs_b_contains_a_bit
))
481 result
|= nir_derefs_equal_bit
;
486 nir_deref_compare_result
487 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*b
)
490 return nir_derefs_equal_bit
| nir_derefs_may_alias_bit
|
491 nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
;
494 nir_deref_path a_path
, b_path
;
495 nir_deref_path_init(&a_path
, a
, NULL
);
496 nir_deref_path_init(&b_path
, b
, NULL
);
497 assert(a_path
.path
[0]->deref_type
== nir_deref_type_var
||
498 a_path
.path
[0]->deref_type
== nir_deref_type_cast
);
499 assert(b_path
.path
[0]->deref_type
== nir_deref_type_var
||
500 b_path
.path
[0]->deref_type
== nir_deref_type_cast
);
502 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
504 nir_deref_path_finish(&a_path
);
505 nir_deref_path_finish(&b_path
);
510 struct rematerialize_deref_state
{
514 struct hash_table
*cache
;
517 static nir_deref_instr
*
518 rematerialize_deref_in_block(nir_deref_instr
*deref
,
519 struct rematerialize_deref_state
*state
)
521 if (deref
->instr
.block
== state
->block
)
525 state
->cache
= _mesa_pointer_hash_table_create(NULL
);
528 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
532 nir_builder
*b
= &state
->builder
;
533 nir_deref_instr
*new_deref
=
534 nir_deref_instr_create(b
->shader
, deref
->deref_type
);
535 new_deref
->mode
= deref
->mode
;
536 new_deref
->type
= deref
->type
;
538 if (deref
->deref_type
== nir_deref_type_var
) {
539 new_deref
->var
= deref
->var
;
541 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
543 parent
= rematerialize_deref_in_block(parent
, state
);
544 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
546 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
550 switch (deref
->deref_type
) {
551 case nir_deref_type_var
:
552 case nir_deref_type_array_wildcard
:
553 case nir_deref_type_cast
:
554 /* Nothing more to do */
557 case nir_deref_type_array
:
558 assert(!nir_src_as_deref(deref
->arr
.index
));
559 nir_src_copy(&new_deref
->arr
.index
, &deref
->arr
.index
, new_deref
);
562 case nir_deref_type_struct
:
563 new_deref
->strct
.index
= deref
->strct
.index
;
567 unreachable("Invalid deref instruction type");
570 nir_ssa_dest_init(&new_deref
->instr
, &new_deref
->dest
,
571 deref
->dest
.ssa
.num_components
,
572 deref
->dest
.ssa
.bit_size
,
573 deref
->dest
.ssa
.name
);
574 nir_builder_instr_insert(b
, &new_deref
->instr
);
580 rematerialize_deref_src(nir_src
*src
, void *_state
)
582 struct rematerialize_deref_state
*state
= _state
;
584 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
588 nir_deref_instr
*block_deref
= rematerialize_deref_in_block(deref
, state
);
589 if (block_deref
!= deref
) {
590 nir_instr_rewrite_src(src
->parent_instr
, src
,
591 nir_src_for_ssa(&block_deref
->dest
.ssa
));
592 nir_deref_instr_remove_if_unused(deref
);
593 state
->progress
= true;
599 /** Re-materialize derefs in every block
601 * This pass re-materializes deref instructions in every block in which it is
602 * used. After this pass has been run, every use of a deref will be of a
603 * deref in the same block as the use. Also, all unused derefs will be
604 * deleted as a side-effect.
607 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
609 struct rematerialize_deref_state state
= { 0 };
610 nir_builder_init(&state
.builder
, impl
);
612 nir_foreach_block(block
, impl
) {
615 /* Start each block with a fresh cache */
617 _mesa_hash_table_clear(state
.cache
, NULL
);
619 nir_foreach_instr_safe(instr
, block
) {
620 if (instr
->type
== nir_instr_type_deref
&&
621 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
624 state
.builder
.cursor
= nir_before_instr(instr
);
625 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
629 nir_if
*following_if
= nir_block_get_following_if(block
);
631 assert(!nir_src_as_deref(following_if
->condition
));
635 _mesa_hash_table_destroy(state
.cache
, NULL
);
637 return state
.progress
;
641 is_trivial_deref_cast(nir_deref_instr
*cast
)
643 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
647 return cast
->mode
== parent
->mode
&&
648 cast
->type
== parent
->type
&&
649 cast
->dest
.ssa
.num_components
== parent
->dest
.ssa
.num_components
&&
650 cast
->dest
.ssa
.bit_size
== parent
->dest
.ssa
.bit_size
;
654 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
656 assert(is_trivial_deref_cast(cast
));
658 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
660 if (parent
->deref_type
== nir_deref_type_array
) {
661 return cast
->cast
.ptr_stride
==
662 glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
663 } else if (parent
->deref_type
== nir_deref_type_ptr_as_array
) {
664 return cast
->cast
.ptr_stride
==
665 nir_deref_instr_ptr_as_array_stride(parent
);
672 is_deref_ptr_as_array(nir_instr
*instr
)
674 return instr
->type
== nir_instr_type_deref
&&
675 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
679 opt_deref_cast(nir_deref_instr
*cast
)
681 if (!is_trivial_deref_cast(cast
))
684 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
686 assert(cast
->dest
.is_ssa
);
687 assert(cast
->parent
.is_ssa
);
689 bool progress
= false;
690 nir_foreach_use_safe(use_src
, &cast
->dest
.ssa
) {
691 /* If this isn't a trivial array cast, we can't propagate into
692 * ptr_as_array derefs.
694 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
698 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
702 /* If uses would be a bit crazy */
703 assert(list_empty(&cast
->dest
.ssa
.if_uses
));
705 nir_deref_instr_remove_if_unused(cast
);
710 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
712 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
714 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
716 if (nir_src_is_const(deref
->arr
.index
) &&
717 nir_src_as_int(deref
->arr
.index
) == 0) {
718 /* If it's a ptr_as_array deref with an index of 0, it does nothing
719 * and we can just replace its uses with its parent.
721 * The source of a ptr_as_array deref always has a deref_type of
722 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
723 * may be trivial and we may be able to get rid of that too. Any
724 * trivial cast of trivial cast cases should be handled already by
725 * opt_deref_cast() above.
727 if (parent
->deref_type
== nir_deref_type_cast
&&
728 is_trivial_deref_cast(parent
))
729 parent
= nir_deref_instr_parent(parent
);
730 nir_ssa_def_rewrite_uses(&deref
->dest
.ssa
,
731 nir_src_for_ssa(&parent
->dest
.ssa
));
732 nir_instr_remove(&deref
->instr
);
736 if (parent
->deref_type
!= nir_deref_type_array
&&
737 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
740 assert(parent
->parent
.is_ssa
);
741 assert(parent
->arr
.index
.is_ssa
);
742 assert(deref
->arr
.index
.is_ssa
);
744 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
745 deref
->arr
.index
.ssa
);
747 deref
->deref_type
= parent
->deref_type
;
748 nir_instr_rewrite_src(&deref
->instr
, &deref
->parent
, parent
->parent
);
749 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
750 nir_src_for_ssa(new_idx
));
755 nir_opt_deref_impl(nir_function_impl
*impl
)
757 bool progress
= false;
760 nir_builder_init(&b
, impl
);
762 nir_foreach_block(block
, impl
) {
763 nir_foreach_instr_safe(instr
, block
) {
764 if (instr
->type
!= nir_instr_type_deref
)
767 b
.cursor
= nir_before_instr(instr
);
769 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
770 switch (deref
->deref_type
) {
771 case nir_deref_type_ptr_as_array
:
772 if (opt_deref_ptr_as_array(&b
, deref
))
776 case nir_deref_type_cast
:
777 if (opt_deref_cast(deref
))
789 nir_metadata_preserve(impl
, nir_metadata_block_index
|
790 nir_metadata_dominance
);
793 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
801 nir_opt_deref(nir_shader
*shader
)
803 bool progress
= false;
805 nir_foreach_function(func
, shader
) {
806 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))