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 *)(uintptr_t)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_is_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_is_known_out_of_bounds(nir_deref_instr
*instr
)
127 for (; instr
; instr
= nir_deref_instr_parent(instr
)) {
128 if (instr
->deref_type
== nir_deref_type_array
&&
129 nir_src_is_const(instr
->arr
.index
) &&
130 nir_src_as_uint(instr
->arr
.index
) >=
131 glsl_get_length(nir_deref_instr_parent(instr
)->type
))
139 nir_deref_instr_has_complex_use(nir_deref_instr
*deref
)
141 nir_foreach_use(use_src
, &deref
->dest
.ssa
) {
142 nir_instr
*use_instr
= use_src
->parent_instr
;
144 switch (use_instr
->type
) {
145 case nir_instr_type_deref
: {
146 nir_deref_instr
*use_deref
= nir_instr_as_deref(use_instr
);
148 /* A var deref has no sources */
149 assert(use_deref
->deref_type
!= nir_deref_type_var
);
151 /* If a deref shows up in an array index or something like that, it's
154 if (use_src
!= &use_deref
->parent
)
157 /* Anything that isn't a basic struct or array deref is considered to
158 * be a "complex" use. In particular, we don't allow ptr_as_array
159 * because we assume that opt_deref will turn any non-complex
160 * ptr_as_array derefs into regular array derefs eventually so passes
161 * which only want to handle simple derefs will pick them up in a
164 if (use_deref
->deref_type
!= nir_deref_type_struct
&&
165 use_deref
->deref_type
!= nir_deref_type_array_wildcard
&&
166 use_deref
->deref_type
!= nir_deref_type_array
)
169 if (nir_deref_instr_has_complex_use(use_deref
))
175 case nir_instr_type_intrinsic
: {
176 nir_intrinsic_instr
*use_intrin
= nir_instr_as_intrinsic(use_instr
);
177 switch (use_intrin
->intrinsic
) {
178 case nir_intrinsic_load_deref
:
179 assert(use_src
== &use_intrin
->src
[0]);
182 case nir_intrinsic_copy_deref
:
183 assert(use_src
== &use_intrin
->src
[0] ||
184 use_src
== &use_intrin
->src
[1]);
187 case nir_intrinsic_store_deref
:
188 /* A use in src[1] of a store means we're taking that pointer and
189 * writing it to a variable. Because we have no idea who will
190 * read that variable and what they will do with the pointer, it's
191 * considered a "complex" use. A use in src[0], on the other
192 * hand, is a simple use because we're just going to dereference
193 * it and write a value there.
195 if (use_src
== &use_intrin
->src
[0])
202 unreachable("Switch default failed");
210 nir_foreach_if_use(use
, &deref
->dest
.ssa
)
217 nir_deref_instr_ptr_as_array_stride(nir_deref_instr
*deref
)
219 switch (deref
->deref_type
) {
220 case nir_deref_type_array
:
221 return glsl_get_explicit_stride(nir_deref_instr_parent(deref
)->type
);
222 case nir_deref_type_ptr_as_array
:
223 return nir_deref_instr_ptr_as_array_stride(nir_deref_instr_parent(deref
));
224 case nir_deref_type_cast
:
225 return deref
->cast
.ptr_stride
;
232 type_get_array_stride(const struct glsl_type
*elem_type
,
233 glsl_type_size_align_func size_align
)
235 unsigned elem_size
, elem_align
;
236 size_align(elem_type
, &elem_size
, &elem_align
);
237 return ALIGN_POT(elem_size
, elem_align
);
241 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
242 glsl_type_size_align_func size_align
,
245 assert(glsl_type_is_struct_or_ifc(struct_type
));
247 for (unsigned i
= 0; i
<= field_idx
; i
++) {
248 unsigned elem_size
, elem_align
;
249 size_align(glsl_get_struct_field(struct_type
, i
), &elem_size
, &elem_align
);
250 offset
= ALIGN_POT(offset
, elem_align
);
258 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
259 glsl_type_size_align_func size_align
)
262 nir_deref_path_init(&path
, deref
, NULL
);
264 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
267 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
268 if ((*p
)->deref_type
== nir_deref_type_array
) {
269 offset
+= nir_src_as_uint((*p
)->arr
.index
) *
270 type_get_array_stride((*p
)->type
, size_align
);
271 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
272 /* p starts at path[1], so this is safe */
273 nir_deref_instr
*parent
= *(p
- 1);
274 offset
+= struct_type_get_field_offset(parent
->type
, size_align
,
277 unreachable("Unsupported deref type");
281 nir_deref_path_finish(&path
);
287 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
288 glsl_type_size_align_func size_align
)
291 nir_deref_path_init(&path
, deref
, NULL
);
293 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
295 nir_ssa_def
*offset
= nir_imm_intN_t(b
, 0, deref
->dest
.ssa
.bit_size
);
296 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
297 if ((*p
)->deref_type
== nir_deref_type_array
) {
298 nir_ssa_def
*index
= nir_ssa_for_src(b
, (*p
)->arr
.index
, 1);
299 int stride
= type_get_array_stride((*p
)->type
, size_align
);
300 offset
= nir_iadd(b
, offset
, nir_amul_imm(b
, index
, stride
));
301 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
302 /* p starts at path[1], so this is safe */
303 nir_deref_instr
*parent
= *(p
- 1);
304 unsigned field_offset
=
305 struct_type_get_field_offset(parent
->type
, size_align
,
307 offset
= nir_iadd_imm(b
, offset
, field_offset
);
309 unreachable("Unsupported deref type");
313 nir_deref_path_finish(&path
);
319 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
321 bool progress
= false;
323 nir_foreach_block(block
, impl
) {
324 nir_foreach_instr_safe(instr
, block
) {
325 if (instr
->type
== nir_instr_type_deref
&&
326 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
332 nir_metadata_preserve(impl
, nir_metadata_block_index
|
333 nir_metadata_dominance
);
339 nir_remove_dead_derefs(nir_shader
*shader
)
341 bool progress
= false;
342 nir_foreach_function(function
, shader
) {
343 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
351 nir_fixup_deref_modes(nir_shader
*shader
)
353 nir_foreach_function(function
, shader
) {
357 nir_foreach_block(block
, function
->impl
) {
358 nir_foreach_instr(instr
, block
) {
359 if (instr
->type
!= nir_instr_type_deref
)
362 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
363 if (deref
->deref_type
== nir_deref_type_cast
)
366 nir_variable_mode parent_mode
;
367 if (deref
->deref_type
== nir_deref_type_var
) {
368 parent_mode
= deref
->var
->data
.mode
;
370 assert(deref
->parent
.is_ssa
);
371 nir_deref_instr
*parent
=
372 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
373 parent_mode
= parent
->mode
;
376 deref
->mode
= parent_mode
;
383 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
385 /* Generic pointers can alias with SSBOs */
386 if ((a
== nir_var_mem_ssbo
|| a
== nir_var_mem_global
) &&
387 (b
== nir_var_mem_ssbo
|| b
== nir_var_mem_global
))
390 /* In the general case, pointers can only alias if they have the same mode.
392 * NOTE: In future, with things like OpenCL generic pointers, this may not
393 * be true and will have to be re-evaluated. However, with graphics only,
400 deref_path_contains_coherent_decoration(nir_deref_path
*path
)
402 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
404 if (path
->path
[0]->var
->data
.access
& ACCESS_COHERENT
)
407 for (nir_deref_instr
**p
= &path
->path
[1]; *p
; p
++) {
408 if ((*p
)->deref_type
!= nir_deref_type_struct
)
411 const struct glsl_type
*struct_type
= (*(p
- 1))->type
;
412 const struct glsl_struct_field
*field
=
413 glsl_get_struct_field_data(struct_type
, (*p
)->strct
.index
);
414 if (field
->memory_coherent
)
421 nir_deref_compare_result
422 nir_compare_deref_paths(nir_deref_path
*a_path
,
423 nir_deref_path
*b_path
)
425 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
426 return nir_derefs_do_not_alias
;
428 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
429 return nir_derefs_may_alias_bit
;
431 if (a_path
->path
[0]->deref_type
== nir_deref_type_var
) {
432 if (a_path
->path
[0]->var
!= b_path
->path
[0]->var
) {
433 /* Shader and function temporaries aren't backed by memory so two
434 * distinct variables never alias.
436 static const nir_variable_mode temp_var_modes
=
437 nir_var_shader_temp
| nir_var_function_temp
;
438 if ((a_path
->path
[0]->mode
& temp_var_modes
) ||
439 (b_path
->path
[0]->mode
& temp_var_modes
))
440 return nir_derefs_do_not_alias
;
442 /* If they are both declared coherent or have coherent somewhere in
443 * their path (due to a member of an interface being declared
444 * coherent), we have to assume we that we could have any kind of
445 * aliasing. Otherwise, they could still alias but the client didn't
446 * tell us and that's their fault.
448 if (deref_path_contains_coherent_decoration(a_path
) &&
449 deref_path_contains_coherent_decoration(b_path
))
450 return nir_derefs_may_alias_bit
;
452 /* If we can chase the deref all the way back to the variable and
453 * they're not the same variable and at least one is not declared
454 * coherent, we know they can't possibly alias.
456 return nir_derefs_do_not_alias
;
459 assert(a_path
->path
[0]->deref_type
== nir_deref_type_cast
);
460 /* If they're not exactly the same cast, it's hard to compare them so we
461 * just assume they alias. Comparing casts is tricky as there are lots
462 * of things such as mode, type, etc. to make sure work out; for now, we
463 * just assume nit_opt_deref will combine them and compare the deref
466 * TODO: At some point in the future, we could be clever and understand
467 * that a float[] and int[] have the same layout and aliasing structure
468 * but double[] and vec3[] do not and we could potentially be a bit
471 if (a_path
->path
[0] != b_path
->path
[0])
472 return nir_derefs_may_alias_bit
;
475 /* Start off assuming they fully compare. We ignore equality for now. In
476 * the end, we'll determine that by containment.
478 nir_deref_compare_result result
= nir_derefs_may_alias_bit
|
479 nir_derefs_a_contains_b_bit
|
480 nir_derefs_b_contains_a_bit
;
482 nir_deref_instr
**a_p
= &a_path
->path
[1];
483 nir_deref_instr
**b_p
= &b_path
->path
[1];
484 while (*a_p
!= NULL
&& *a_p
== *b_p
) {
489 /* We're at either the tail or the divergence point between the two deref
490 * paths. Look to see if either contains a ptr_as_array deref. It it
491 * does we don't know how to safely make any inferences. Hopefully,
492 * nir_opt_deref will clean most of these up and we can start inferring
495 * In theory, we could do a bit better. For instance, we could detect the
496 * case where we have exactly one ptr_as_array deref in the chain after the
497 * divergence point and it's matched in both chains and the two chains have
498 * different constant indices.
500 for (nir_deref_instr
**t_p
= a_p
; *t_p
; t_p
++) {
501 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
502 return nir_derefs_may_alias_bit
;
504 for (nir_deref_instr
**t_p
= b_p
; *t_p
; t_p
++) {
505 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
506 return nir_derefs_may_alias_bit
;
509 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
510 nir_deref_instr
*a_tail
= *(a_p
++);
511 nir_deref_instr
*b_tail
= *(b_p
++);
513 switch (a_tail
->deref_type
) {
514 case nir_deref_type_array
:
515 case nir_deref_type_array_wildcard
: {
516 assert(b_tail
->deref_type
== nir_deref_type_array
||
517 b_tail
->deref_type
== nir_deref_type_array_wildcard
);
519 if (a_tail
->deref_type
== nir_deref_type_array_wildcard
) {
520 if (b_tail
->deref_type
!= nir_deref_type_array_wildcard
)
521 result
&= ~nir_derefs_b_contains_a_bit
;
522 } else if (b_tail
->deref_type
== nir_deref_type_array_wildcard
) {
523 if (a_tail
->deref_type
!= nir_deref_type_array_wildcard
)
524 result
&= ~nir_derefs_a_contains_b_bit
;
526 assert(a_tail
->deref_type
== nir_deref_type_array
&&
527 b_tail
->deref_type
== nir_deref_type_array
);
528 assert(a_tail
->arr
.index
.is_ssa
&& b_tail
->arr
.index
.is_ssa
);
530 if (nir_src_is_const(a_tail
->arr
.index
) &&
531 nir_src_is_const(b_tail
->arr
.index
)) {
532 /* If they're both direct and have different offsets, they
533 * don't even alias much less anything else.
535 if (nir_src_as_uint(a_tail
->arr
.index
) !=
536 nir_src_as_uint(b_tail
->arr
.index
))
537 return nir_derefs_do_not_alias
;
538 } else if (a_tail
->arr
.index
.ssa
== b_tail
->arr
.index
.ssa
) {
539 /* They're the same indirect, continue on */
541 /* They're not the same index so we can't prove anything about
544 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
550 case nir_deref_type_struct
: {
551 /* If they're different struct members, they don't even alias */
552 if (a_tail
->strct
.index
!= b_tail
->strct
.index
)
553 return nir_derefs_do_not_alias
;
558 unreachable("Invalid deref type");
562 /* If a is longer than b, then it can't contain b */
564 result
&= ~nir_derefs_a_contains_b_bit
;
566 result
&= ~nir_derefs_b_contains_a_bit
;
568 /* If a contains b and b contains a they must be equal. */
569 if ((result
& nir_derefs_a_contains_b_bit
) && (result
& nir_derefs_b_contains_a_bit
))
570 result
|= nir_derefs_equal_bit
;
575 nir_deref_compare_result
576 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*b
)
579 return nir_derefs_equal_bit
| nir_derefs_may_alias_bit
|
580 nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
;
583 nir_deref_path a_path
, b_path
;
584 nir_deref_path_init(&a_path
, a
, NULL
);
585 nir_deref_path_init(&b_path
, b
, NULL
);
586 assert(a_path
.path
[0]->deref_type
== nir_deref_type_var
||
587 a_path
.path
[0]->deref_type
== nir_deref_type_cast
);
588 assert(b_path
.path
[0]->deref_type
== nir_deref_type_var
||
589 b_path
.path
[0]->deref_type
== nir_deref_type_cast
);
591 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
593 nir_deref_path_finish(&a_path
);
594 nir_deref_path_finish(&b_path
);
599 struct rematerialize_deref_state
{
603 struct hash_table
*cache
;
606 static nir_deref_instr
*
607 rematerialize_deref_in_block(nir_deref_instr
*deref
,
608 struct rematerialize_deref_state
*state
)
610 if (deref
->instr
.block
== state
->block
)
614 state
->cache
= _mesa_pointer_hash_table_create(NULL
);
617 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
621 nir_builder
*b
= &state
->builder
;
622 nir_deref_instr
*new_deref
=
623 nir_deref_instr_create(b
->shader
, deref
->deref_type
);
624 new_deref
->mode
= deref
->mode
;
625 new_deref
->type
= deref
->type
;
627 if (deref
->deref_type
== nir_deref_type_var
) {
628 new_deref
->var
= deref
->var
;
630 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
632 parent
= rematerialize_deref_in_block(parent
, state
);
633 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
635 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
639 switch (deref
->deref_type
) {
640 case nir_deref_type_var
:
641 case nir_deref_type_array_wildcard
:
642 case nir_deref_type_cast
:
643 /* Nothing more to do */
646 case nir_deref_type_array
:
647 assert(!nir_src_as_deref(deref
->arr
.index
));
648 nir_src_copy(&new_deref
->arr
.index
, &deref
->arr
.index
, new_deref
);
651 case nir_deref_type_struct
:
652 new_deref
->strct
.index
= deref
->strct
.index
;
656 unreachable("Invalid deref instruction type");
659 nir_ssa_dest_init(&new_deref
->instr
, &new_deref
->dest
,
660 deref
->dest
.ssa
.num_components
,
661 deref
->dest
.ssa
.bit_size
,
662 deref
->dest
.ssa
.name
);
663 nir_builder_instr_insert(b
, &new_deref
->instr
);
669 rematerialize_deref_src(nir_src
*src
, void *_state
)
671 struct rematerialize_deref_state
*state
= _state
;
673 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
677 nir_deref_instr
*block_deref
= rematerialize_deref_in_block(deref
, state
);
678 if (block_deref
!= deref
) {
679 nir_instr_rewrite_src(src
->parent_instr
, src
,
680 nir_src_for_ssa(&block_deref
->dest
.ssa
));
681 nir_deref_instr_remove_if_unused(deref
);
682 state
->progress
= true;
688 /** Re-materialize derefs in every block
690 * This pass re-materializes deref instructions in every block in which it is
691 * used. After this pass has been run, every use of a deref will be of a
692 * deref in the same block as the use. Also, all unused derefs will be
693 * deleted as a side-effect.
695 * Derefs used as sources of phi instructions are not rematerialized.
698 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
700 struct rematerialize_deref_state state
= { 0 };
701 nir_builder_init(&state
.builder
, impl
);
703 nir_foreach_block(block
, impl
) {
706 /* Start each block with a fresh cache */
708 _mesa_hash_table_clear(state
.cache
, NULL
);
710 nir_foreach_instr_safe(instr
, block
) {
711 if (instr
->type
== nir_instr_type_deref
&&
712 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
715 /* If a deref is used in a phi, we can't rematerialize it, as the new
716 * derefs would appear before the phi, which is not valid.
718 if (instr
->type
== nir_instr_type_phi
)
721 state
.builder
.cursor
= nir_before_instr(instr
);
722 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
726 nir_if
*following_if
= nir_block_get_following_if(block
);
728 assert(!nir_src_as_deref(following_if
->condition
));
732 _mesa_hash_table_destroy(state
.cache
, NULL
);
734 return state
.progress
;
738 is_trivial_deref_cast(nir_deref_instr
*cast
)
740 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
744 return cast
->mode
== parent
->mode
&&
745 cast
->type
== parent
->type
&&
746 cast
->dest
.ssa
.num_components
== parent
->dest
.ssa
.num_components
&&
747 cast
->dest
.ssa
.bit_size
== parent
->dest
.ssa
.bit_size
;
751 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
753 assert(is_trivial_deref_cast(cast
));
755 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
757 if (parent
->deref_type
== nir_deref_type_array
) {
758 return cast
->cast
.ptr_stride
==
759 glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
760 } else if (parent
->deref_type
== nir_deref_type_ptr_as_array
) {
761 return cast
->cast
.ptr_stride
==
762 nir_deref_instr_ptr_as_array_stride(parent
);
769 is_deref_ptr_as_array(nir_instr
*instr
)
771 return instr
->type
== nir_instr_type_deref
&&
772 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
776 * Remove casts that just wrap other casts.
779 opt_remove_cast_cast(nir_deref_instr
*cast
)
781 nir_deref_instr
*first_cast
= cast
;
784 nir_deref_instr
*parent
= nir_deref_instr_parent(first_cast
);
785 if (parent
== NULL
|| parent
->deref_type
!= nir_deref_type_cast
)
789 if (cast
== first_cast
)
792 nir_instr_rewrite_src(&cast
->instr
, &cast
->parent
,
793 nir_src_for_ssa(first_cast
->parent
.ssa
));
798 * Is this casting a struct to a contained struct.
799 * struct a { struct b field0 };
801 * deref_cast (structb *)ssa_5 (function_temp structb);
803 * deref_struct &ssa_5->field0 (function_temp structb);
804 * This allows subsequent copy propagation to work.
807 opt_replace_struct_wrapper_cast(nir_builder
*b
, nir_deref_instr
*cast
)
809 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
813 if (!glsl_type_is_struct(parent
->type
))
816 if (glsl_get_struct_field_offset(parent
->type
, 0) != 0)
819 if (cast
->type
!= glsl_get_struct_field(parent
->type
, 0))
822 nir_deref_instr
*replace
= nir_build_deref_struct(b
, parent
, 0);
823 nir_ssa_def_rewrite_uses(&cast
->dest
.ssa
, nir_src_for_ssa(&replace
->dest
.ssa
));
824 nir_deref_instr_remove_if_unused(cast
);
829 opt_deref_cast(nir_builder
*b
, nir_deref_instr
*cast
)
833 if (opt_replace_struct_wrapper_cast(b
, cast
))
836 progress
= opt_remove_cast_cast(cast
);
837 if (!is_trivial_deref_cast(cast
))
840 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
842 assert(cast
->dest
.is_ssa
);
843 assert(cast
->parent
.is_ssa
);
845 nir_foreach_use_safe(use_src
, &cast
->dest
.ssa
) {
846 /* If this isn't a trivial array cast, we can't propagate into
847 * ptr_as_array derefs.
849 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
853 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
857 /* If uses would be a bit crazy */
858 assert(list_is_empty(&cast
->dest
.ssa
.if_uses
));
860 nir_deref_instr_remove_if_unused(cast
);
865 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
867 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
869 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
871 if (nir_src_is_const(deref
->arr
.index
) &&
872 nir_src_as_int(deref
->arr
.index
) == 0) {
873 /* If it's a ptr_as_array deref with an index of 0, it does nothing
874 * and we can just replace its uses with its parent.
876 * The source of a ptr_as_array deref always has a deref_type of
877 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
878 * may be trivial and we may be able to get rid of that too. Any
879 * trivial cast of trivial cast cases should be handled already by
880 * opt_deref_cast() above.
882 if (parent
->deref_type
== nir_deref_type_cast
&&
883 is_trivial_deref_cast(parent
))
884 parent
= nir_deref_instr_parent(parent
);
885 nir_ssa_def_rewrite_uses(&deref
->dest
.ssa
,
886 nir_src_for_ssa(&parent
->dest
.ssa
));
887 nir_instr_remove(&deref
->instr
);
891 if (parent
->deref_type
!= nir_deref_type_array
&&
892 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
895 assert(parent
->parent
.is_ssa
);
896 assert(parent
->arr
.index
.is_ssa
);
897 assert(deref
->arr
.index
.is_ssa
);
899 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
900 deref
->arr
.index
.ssa
);
902 deref
->deref_type
= parent
->deref_type
;
903 nir_instr_rewrite_src(&deref
->instr
, &deref
->parent
, parent
->parent
);
904 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
905 nir_src_for_ssa(new_idx
));
910 nir_opt_deref_impl(nir_function_impl
*impl
)
912 bool progress
= false;
915 nir_builder_init(&b
, impl
);
917 nir_foreach_block(block
, impl
) {
918 nir_foreach_instr_safe(instr
, block
) {
919 if (instr
->type
!= nir_instr_type_deref
)
922 b
.cursor
= nir_before_instr(instr
);
924 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
925 switch (deref
->deref_type
) {
926 case nir_deref_type_ptr_as_array
:
927 if (opt_deref_ptr_as_array(&b
, deref
))
931 case nir_deref_type_cast
:
932 if (opt_deref_cast(&b
, deref
))
944 nir_metadata_preserve(impl
, nir_metadata_block_index
|
945 nir_metadata_dominance
);
948 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
956 nir_opt_deref(nir_shader
*shader
)
958 bool progress
= false;
960 nir_foreach_function(func
, shader
) {
961 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))