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_has_complex_use(nir_deref_instr
*deref
)
127 nir_foreach_use(use_src
, &deref
->dest
.ssa
) {
128 nir_instr
*use_instr
= use_src
->parent_instr
;
130 switch (use_instr
->type
) {
131 case nir_instr_type_deref
: {
132 nir_deref_instr
*use_deref
= nir_instr_as_deref(use_instr
);
134 /* A var deref has no sources */
135 assert(use_deref
->deref_type
!= nir_deref_type_var
);
137 /* If a deref shows up in an array index or something like that, it's
140 if (use_src
!= &use_deref
->parent
)
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
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
)
155 if (nir_deref_instr_has_complex_use(use_deref
))
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]);
168 case nir_intrinsic_copy_deref
:
169 assert(use_src
== &use_intrin
->src
[0] ||
170 use_src
== &use_intrin
->src
[1]);
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.
181 if (use_src
== &use_intrin
->src
[0])
188 unreachable("Switch default failed");
196 nir_foreach_if_use(use
, &deref
->dest
.ssa
)
203 nir_deref_instr_ptr_as_array_stride(nir_deref_instr
*deref
)
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
;
215 unreachable("Invalid parent for ptr_as_array deref");
220 type_get_array_stride(const struct glsl_type
*elem_type
,
221 glsl_type_size_align_func size_align
)
223 unsigned elem_size
, elem_align
;
224 size_align(elem_type
, &elem_size
, &elem_align
);
225 return ALIGN_POT(elem_size
, elem_align
);
229 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
230 glsl_type_size_align_func size_align
,
233 assert(glsl_type_is_struct_or_ifc(struct_type
));
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
);
246 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
247 glsl_type_size_align_func size_align
)
250 nir_deref_path_init(&path
, deref
, NULL
);
252 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
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
,
265 unreachable("Unsupported deref type");
269 nir_deref_path_finish(&path
);
275 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
276 glsl_type_size_align_func size_align
)
279 nir_deref_path_init(&path
, deref
, NULL
);
281 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
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
,
295 offset
= nir_iadd_imm(b
, offset
, field_offset
);
297 unreachable("Unsupported deref type");
301 nir_deref_path_finish(&path
);
307 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
309 bool progress
= false;
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
)))
320 nir_metadata_preserve(impl
, nir_metadata_block_index
|
321 nir_metadata_dominance
);
327 nir_remove_dead_derefs(nir_shader
*shader
)
329 bool progress
= false;
330 nir_foreach_function(function
, shader
) {
331 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
339 nir_fixup_deref_modes(nir_shader
*shader
)
341 nir_foreach_function(function
, shader
) {
345 nir_foreach_block(block
, function
->impl
) {
346 nir_foreach_instr(instr
, block
) {
347 if (instr
->type
!= nir_instr_type_deref
)
350 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
351 if (deref
->deref_type
== nir_deref_type_cast
)
354 nir_variable_mode parent_mode
;
355 if (deref
->deref_type
== nir_deref_type_var
) {
356 parent_mode
= deref
->var
->data
.mode
;
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
;
364 deref
->mode
= parent_mode
;
371 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
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
))
378 /* In the general case, pointers can only alias if they have the same mode.
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,
388 deref_path_contains_coherent_decoration(nir_deref_path
*path
)
390 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
392 if (path
->path
[0]->var
->data
.image
.access
& ACCESS_COHERENT
)
395 for (nir_deref_instr
**p
= &path
->path
[1]; *p
; p
++) {
396 if ((*p
)->deref_type
!= nir_deref_type_struct
)
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
)
409 nir_deref_compare_result
410 nir_compare_deref_paths(nir_deref_path
*a_path
,
411 nir_deref_path
*b_path
)
413 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
414 return nir_derefs_do_not_alias
;
416 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
417 return nir_derefs_may_alias_bit
;
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.
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
;
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.
436 if (deref_path_contains_coherent_decoration(a_path
) &&
437 deref_path_contains_coherent_decoration(b_path
))
438 return nir_derefs_may_alias_bit
;
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.
444 return nir_derefs_do_not_alias
;
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
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
459 if (a_path
->path
[0] != b_path
->path
[0])
460 return nir_derefs_may_alias_bit
;
463 /* Start off assuming they fully compare. We ignore equality for now. In
464 * the end, we'll determine that by containment.
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
;
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
) {
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
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.
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
;
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
;
497 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
498 nir_deref_instr
*a_tail
= *(a_p
++);
499 nir_deref_instr
*b_tail
= *(b_p
++);
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
);
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
;
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
);
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.
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 */
529 /* They're not the same index so we can't prove anything about
532 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
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
;
546 unreachable("Invalid deref type");
550 /* If a is longer than b, then it can't contain b */
552 result
&= ~nir_derefs_a_contains_b_bit
;
554 result
&= ~nir_derefs_b_contains_a_bit
;
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
;
563 nir_deref_compare_result
564 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*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
;
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
);
579 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
581 nir_deref_path_finish(&a_path
);
582 nir_deref_path_finish(&b_path
);
587 struct rematerialize_deref_state
{
591 struct hash_table
*cache
;
594 static nir_deref_instr
*
595 rematerialize_deref_in_block(nir_deref_instr
*deref
,
596 struct rematerialize_deref_state
*state
)
598 if (deref
->instr
.block
== state
->block
)
602 state
->cache
= _mesa_pointer_hash_table_create(NULL
);
605 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
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
;
615 if (deref
->deref_type
== nir_deref_type_var
) {
616 new_deref
->var
= deref
->var
;
618 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
620 parent
= rematerialize_deref_in_block(parent
, state
);
621 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
623 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
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 */
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
);
639 case nir_deref_type_struct
:
640 new_deref
->strct
.index
= deref
->strct
.index
;
644 unreachable("Invalid deref instruction type");
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
);
657 rematerialize_deref_src(nir_src
*src
, void *_state
)
659 struct rematerialize_deref_state
*state
= _state
;
661 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
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;
676 /** Re-materialize derefs in every block
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.
683 * Derefs used as sources of phi instructions are not rematerialized.
686 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
688 struct rematerialize_deref_state state
= { 0 };
689 nir_builder_init(&state
.builder
, impl
);
691 nir_foreach_block(block
, impl
) {
694 /* Start each block with a fresh cache */
696 _mesa_hash_table_clear(state
.cache
, NULL
);
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
)))
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.
706 if (instr
->type
== nir_instr_type_phi
)
709 state
.builder
.cursor
= nir_before_instr(instr
);
710 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
714 nir_if
*following_if
= nir_block_get_following_if(block
);
716 assert(!nir_src_as_deref(following_if
->condition
));
720 _mesa_hash_table_destroy(state
.cache
, NULL
);
722 return state
.progress
;
726 is_trivial_deref_cast(nir_deref_instr
*cast
)
728 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
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
;
739 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
741 assert(is_trivial_deref_cast(cast
));
743 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
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
);
757 is_deref_ptr_as_array(nir_instr
*instr
)
759 return instr
->type
== nir_instr_type_deref
&&
760 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
764 * Remove casts that just wrap other casts.
767 opt_remove_cast_cast(nir_deref_instr
*cast
)
769 nir_deref_instr
*first_cast
= cast
;
772 nir_deref_instr
*parent
= nir_deref_instr_parent(first_cast
);
773 if (parent
== NULL
|| parent
->deref_type
!= nir_deref_type_cast
)
777 if (cast
== first_cast
)
780 nir_instr_rewrite_src(&cast
->instr
, &cast
->parent
,
781 nir_src_for_ssa(first_cast
->parent
.ssa
));
786 * Is this casting a struct to a contained struct.
787 * struct a { struct b field0 };
789 * deref_cast (structb *)ssa_5 (function_temp structb);
791 * deref_struct &ssa_5->field0 (function_temp structb);
792 * This allows subsequent copy propagation to work.
795 opt_replace_struct_wrapper_cast(nir_builder
*b
, nir_deref_instr
*cast
)
797 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
801 if (!glsl_type_is_struct(parent
->type
))
804 if (glsl_get_struct_field_offset(parent
->type
, 0) != 0)
807 if (cast
->type
!= glsl_get_struct_field(parent
->type
, 0))
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
);
817 opt_deref_cast(nir_builder
*b
, nir_deref_instr
*cast
)
821 if (opt_replace_struct_wrapper_cast(b
, cast
))
824 progress
= opt_remove_cast_cast(cast
);
825 if (!is_trivial_deref_cast(cast
))
828 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
830 assert(cast
->dest
.is_ssa
);
831 assert(cast
->parent
.is_ssa
);
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.
837 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
841 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
845 /* If uses would be a bit crazy */
846 assert(list_empty(&cast
->dest
.ssa
.if_uses
));
848 nir_deref_instr_remove_if_unused(cast
);
853 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
855 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
857 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
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.
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.
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
);
879 if (parent
->deref_type
!= nir_deref_type_array
&&
880 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
883 assert(parent
->parent
.is_ssa
);
884 assert(parent
->arr
.index
.is_ssa
);
885 assert(deref
->arr
.index
.is_ssa
);
887 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
888 deref
->arr
.index
.ssa
);
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
));
898 nir_opt_deref_impl(nir_function_impl
*impl
)
900 bool progress
= false;
903 nir_builder_init(&b
, impl
);
905 nir_foreach_block(block
, impl
) {
906 nir_foreach_instr_safe(instr
, block
) {
907 if (instr
->type
!= nir_instr_type_deref
)
910 b
.cursor
= nir_before_instr(instr
);
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
))
919 case nir_deref_type_cast
:
920 if (opt_deref_cast(&b
, deref
))
932 nir_metadata_preserve(impl
, nir_metadata_block_index
|
933 nir_metadata_dominance
);
936 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
944 nir_opt_deref(nir_shader
*shader
)
946 bool progress
= false;
948 nir_foreach_function(func
, shader
) {
949 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))