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_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 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
220 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
221 switch (parent
->deref_type
) {
222 case nir_deref_type_array
:
223 return glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
224 case nir_deref_type_ptr_as_array
:
225 return nir_deref_instr_ptr_as_array_stride(parent
);
226 case nir_deref_type_cast
:
227 return parent
->cast
.ptr_stride
;
229 unreachable("Invalid parent for ptr_as_array deref");
234 type_get_array_stride(const struct glsl_type
*elem_type
,
235 glsl_type_size_align_func size_align
)
237 unsigned elem_size
, elem_align
;
238 size_align(elem_type
, &elem_size
, &elem_align
);
239 return ALIGN_POT(elem_size
, elem_align
);
243 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
244 glsl_type_size_align_func size_align
,
247 assert(glsl_type_is_struct_or_ifc(struct_type
));
249 for (unsigned i
= 0; i
<= field_idx
; i
++) {
250 unsigned elem_size
, elem_align
;
251 size_align(glsl_get_struct_field(struct_type
, i
), &elem_size
, &elem_align
);
252 offset
= ALIGN_POT(offset
, elem_align
);
260 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
261 glsl_type_size_align_func size_align
)
264 nir_deref_path_init(&path
, deref
, NULL
);
266 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
269 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
270 if ((*p
)->deref_type
== nir_deref_type_array
) {
271 offset
+= nir_src_as_uint((*p
)->arr
.index
) *
272 type_get_array_stride((*p
)->type
, size_align
);
273 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
274 /* p starts at path[1], so this is safe */
275 nir_deref_instr
*parent
= *(p
- 1);
276 offset
+= struct_type_get_field_offset(parent
->type
, size_align
,
279 unreachable("Unsupported deref type");
283 nir_deref_path_finish(&path
);
289 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
290 glsl_type_size_align_func size_align
)
293 nir_deref_path_init(&path
, deref
, NULL
);
295 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
297 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
298 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
299 if ((*p
)->deref_type
== nir_deref_type_array
) {
300 nir_ssa_def
*index
= nir_ssa_for_src(b
, (*p
)->arr
.index
, 1);
301 int stride
= type_get_array_stride((*p
)->type
, size_align
);
302 offset
= nir_iadd(b
, offset
, nir_imul_imm(b
, index
, stride
));
303 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
304 /* p starts at path[1], so this is safe */
305 nir_deref_instr
*parent
= *(p
- 1);
306 unsigned field_offset
=
307 struct_type_get_field_offset(parent
->type
, size_align
,
309 offset
= nir_iadd_imm(b
, offset
, field_offset
);
311 unreachable("Unsupported deref type");
315 nir_deref_path_finish(&path
);
321 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
323 bool progress
= false;
325 nir_foreach_block(block
, impl
) {
326 nir_foreach_instr_safe(instr
, block
) {
327 if (instr
->type
== nir_instr_type_deref
&&
328 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
334 nir_metadata_preserve(impl
, nir_metadata_block_index
|
335 nir_metadata_dominance
);
341 nir_remove_dead_derefs(nir_shader
*shader
)
343 bool progress
= false;
344 nir_foreach_function(function
, shader
) {
345 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
353 nir_fixup_deref_modes(nir_shader
*shader
)
355 nir_foreach_function(function
, shader
) {
359 nir_foreach_block(block
, function
->impl
) {
360 nir_foreach_instr(instr
, block
) {
361 if (instr
->type
!= nir_instr_type_deref
)
364 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
365 if (deref
->deref_type
== nir_deref_type_cast
)
368 nir_variable_mode parent_mode
;
369 if (deref
->deref_type
== nir_deref_type_var
) {
370 parent_mode
= deref
->var
->data
.mode
;
372 assert(deref
->parent
.is_ssa
);
373 nir_deref_instr
*parent
=
374 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
375 parent_mode
= parent
->mode
;
378 deref
->mode
= parent_mode
;
385 modes_may_alias(nir_variable_mode a
, nir_variable_mode b
)
387 /* Generic pointers can alias with SSBOs */
388 if ((a
== nir_var_mem_ssbo
|| a
== nir_var_mem_global
) &&
389 (b
== nir_var_mem_ssbo
|| b
== nir_var_mem_global
))
392 /* In the general case, pointers can only alias if they have the same mode.
394 * NOTE: In future, with things like OpenCL generic pointers, this may not
395 * be true and will have to be re-evaluated. However, with graphics only,
402 deref_path_contains_coherent_decoration(nir_deref_path
*path
)
404 assert(path
->path
[0]->deref_type
== nir_deref_type_var
);
406 if (path
->path
[0]->var
->data
.image
.access
& ACCESS_COHERENT
)
409 for (nir_deref_instr
**p
= &path
->path
[1]; *p
; p
++) {
410 if ((*p
)->deref_type
!= nir_deref_type_struct
)
413 const struct glsl_type
*struct_type
= (*(p
- 1))->type
;
414 const struct glsl_struct_field
*field
=
415 glsl_get_struct_field_data(struct_type
, (*p
)->strct
.index
);
416 if (field
->memory_coherent
)
423 nir_deref_compare_result
424 nir_compare_deref_paths(nir_deref_path
*a_path
,
425 nir_deref_path
*b_path
)
427 if (!modes_may_alias(b_path
->path
[0]->mode
, a_path
->path
[0]->mode
))
428 return nir_derefs_do_not_alias
;
430 if (a_path
->path
[0]->deref_type
!= b_path
->path
[0]->deref_type
)
431 return nir_derefs_may_alias_bit
;
433 if (a_path
->path
[0]->deref_type
== nir_deref_type_var
) {
434 if (a_path
->path
[0]->var
!= b_path
->path
[0]->var
) {
435 /* Shader and function temporaries aren't backed by memory so two
436 * distinct variables never alias.
438 static const nir_variable_mode temp_var_modes
=
439 nir_var_shader_temp
| nir_var_function_temp
;
440 if ((a_path
->path
[0]->mode
& temp_var_modes
) ||
441 (b_path
->path
[0]->mode
& temp_var_modes
))
442 return nir_derefs_do_not_alias
;
444 /* If they are both declared coherent or have coherent somewhere in
445 * their path (due to a member of an interface being declared
446 * coherent), we have to assume we that we could have any kind of
447 * aliasing. Otherwise, they could still alias but the client didn't
448 * tell us and that's their fault.
450 if (deref_path_contains_coherent_decoration(a_path
) &&
451 deref_path_contains_coherent_decoration(b_path
))
452 return nir_derefs_may_alias_bit
;
454 /* If we can chase the deref all the way back to the variable and
455 * they're not the same variable and at least one is not declared
456 * coherent, we know they can't possibly alias.
458 return nir_derefs_do_not_alias
;
461 assert(a_path
->path
[0]->deref_type
== nir_deref_type_cast
);
462 /* If they're not exactly the same cast, it's hard to compare them so we
463 * just assume they alias. Comparing casts is tricky as there are lots
464 * of things such as mode, type, etc. to make sure work out; for now, we
465 * just assume nit_opt_deref will combine them and compare the deref
468 * TODO: At some point in the future, we could be clever and understand
469 * that a float[] and int[] have the same layout and aliasing structure
470 * but double[] and vec3[] do not and we could potentially be a bit
473 if (a_path
->path
[0] != b_path
->path
[0])
474 return nir_derefs_may_alias_bit
;
477 /* Start off assuming they fully compare. We ignore equality for now. In
478 * the end, we'll determine that by containment.
480 nir_deref_compare_result result
= nir_derefs_may_alias_bit
|
481 nir_derefs_a_contains_b_bit
|
482 nir_derefs_b_contains_a_bit
;
484 nir_deref_instr
**a_p
= &a_path
->path
[1];
485 nir_deref_instr
**b_p
= &b_path
->path
[1];
486 while (*a_p
!= NULL
&& *a_p
== *b_p
) {
491 /* We're at either the tail or the divergence point between the two deref
492 * paths. Look to see if either contains a ptr_as_array deref. It it
493 * does we don't know how to safely make any inferences. Hopefully,
494 * nir_opt_deref will clean most of these up and we can start inferring
497 * In theory, we could do a bit better. For instance, we could detect the
498 * case where we have exactly one ptr_as_array deref in the chain after the
499 * divergence point and it's matched in both chains and the two chains have
500 * different constant indices.
502 for (nir_deref_instr
**t_p
= a_p
; *t_p
; t_p
++) {
503 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
504 return nir_derefs_may_alias_bit
;
506 for (nir_deref_instr
**t_p
= b_p
; *t_p
; t_p
++) {
507 if ((*t_p
)->deref_type
== nir_deref_type_ptr_as_array
)
508 return nir_derefs_may_alias_bit
;
511 while (*a_p
!= NULL
&& *b_p
!= NULL
) {
512 nir_deref_instr
*a_tail
= *(a_p
++);
513 nir_deref_instr
*b_tail
= *(b_p
++);
515 switch (a_tail
->deref_type
) {
516 case nir_deref_type_array
:
517 case nir_deref_type_array_wildcard
: {
518 assert(b_tail
->deref_type
== nir_deref_type_array
||
519 b_tail
->deref_type
== nir_deref_type_array_wildcard
);
521 if (a_tail
->deref_type
== nir_deref_type_array_wildcard
) {
522 if (b_tail
->deref_type
!= nir_deref_type_array_wildcard
)
523 result
&= ~nir_derefs_b_contains_a_bit
;
524 } else if (b_tail
->deref_type
== nir_deref_type_array_wildcard
) {
525 if (a_tail
->deref_type
!= nir_deref_type_array_wildcard
)
526 result
&= ~nir_derefs_a_contains_b_bit
;
528 assert(a_tail
->deref_type
== nir_deref_type_array
&&
529 b_tail
->deref_type
== nir_deref_type_array
);
530 assert(a_tail
->arr
.index
.is_ssa
&& b_tail
->arr
.index
.is_ssa
);
532 if (nir_src_is_const(a_tail
->arr
.index
) &&
533 nir_src_is_const(b_tail
->arr
.index
)) {
534 /* If they're both direct and have different offsets, they
535 * don't even alias much less anything else.
537 if (nir_src_as_uint(a_tail
->arr
.index
) !=
538 nir_src_as_uint(b_tail
->arr
.index
))
539 return nir_derefs_do_not_alias
;
540 } else if (a_tail
->arr
.index
.ssa
== b_tail
->arr
.index
.ssa
) {
541 /* They're the same indirect, continue on */
543 /* They're not the same index so we can't prove anything about
546 result
&= ~(nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
);
552 case nir_deref_type_struct
: {
553 /* If they're different struct members, they don't even alias */
554 if (a_tail
->strct
.index
!= b_tail
->strct
.index
)
555 return nir_derefs_do_not_alias
;
560 unreachable("Invalid deref type");
564 /* If a is longer than b, then it can't contain b */
566 result
&= ~nir_derefs_a_contains_b_bit
;
568 result
&= ~nir_derefs_b_contains_a_bit
;
570 /* If a contains b and b contains a they must be equal. */
571 if ((result
& nir_derefs_a_contains_b_bit
) && (result
& nir_derefs_b_contains_a_bit
))
572 result
|= nir_derefs_equal_bit
;
577 nir_deref_compare_result
578 nir_compare_derefs(nir_deref_instr
*a
, nir_deref_instr
*b
)
581 return nir_derefs_equal_bit
| nir_derefs_may_alias_bit
|
582 nir_derefs_a_contains_b_bit
| nir_derefs_b_contains_a_bit
;
585 nir_deref_path a_path
, b_path
;
586 nir_deref_path_init(&a_path
, a
, NULL
);
587 nir_deref_path_init(&b_path
, b
, NULL
);
588 assert(a_path
.path
[0]->deref_type
== nir_deref_type_var
||
589 a_path
.path
[0]->deref_type
== nir_deref_type_cast
);
590 assert(b_path
.path
[0]->deref_type
== nir_deref_type_var
||
591 b_path
.path
[0]->deref_type
== nir_deref_type_cast
);
593 nir_deref_compare_result result
= nir_compare_deref_paths(&a_path
, &b_path
);
595 nir_deref_path_finish(&a_path
);
596 nir_deref_path_finish(&b_path
);
601 struct rematerialize_deref_state
{
605 struct hash_table
*cache
;
608 static nir_deref_instr
*
609 rematerialize_deref_in_block(nir_deref_instr
*deref
,
610 struct rematerialize_deref_state
*state
)
612 if (deref
->instr
.block
== state
->block
)
616 state
->cache
= _mesa_pointer_hash_table_create(NULL
);
619 struct hash_entry
*cached
= _mesa_hash_table_search(state
->cache
, deref
);
623 nir_builder
*b
= &state
->builder
;
624 nir_deref_instr
*new_deref
=
625 nir_deref_instr_create(b
->shader
, deref
->deref_type
);
626 new_deref
->mode
= deref
->mode
;
627 new_deref
->type
= deref
->type
;
629 if (deref
->deref_type
== nir_deref_type_var
) {
630 new_deref
->var
= deref
->var
;
632 nir_deref_instr
*parent
= nir_src_as_deref(deref
->parent
);
634 parent
= rematerialize_deref_in_block(parent
, state
);
635 new_deref
->parent
= nir_src_for_ssa(&parent
->dest
.ssa
);
637 nir_src_copy(&new_deref
->parent
, &deref
->parent
, new_deref
);
641 switch (deref
->deref_type
) {
642 case nir_deref_type_var
:
643 case nir_deref_type_array_wildcard
:
644 case nir_deref_type_cast
:
645 /* Nothing more to do */
648 case nir_deref_type_array
:
649 assert(!nir_src_as_deref(deref
->arr
.index
));
650 nir_src_copy(&new_deref
->arr
.index
, &deref
->arr
.index
, new_deref
);
653 case nir_deref_type_struct
:
654 new_deref
->strct
.index
= deref
->strct
.index
;
658 unreachable("Invalid deref instruction type");
661 nir_ssa_dest_init(&new_deref
->instr
, &new_deref
->dest
,
662 deref
->dest
.ssa
.num_components
,
663 deref
->dest
.ssa
.bit_size
,
664 deref
->dest
.ssa
.name
);
665 nir_builder_instr_insert(b
, &new_deref
->instr
);
671 rematerialize_deref_src(nir_src
*src
, void *_state
)
673 struct rematerialize_deref_state
*state
= _state
;
675 nir_deref_instr
*deref
= nir_src_as_deref(*src
);
679 nir_deref_instr
*block_deref
= rematerialize_deref_in_block(deref
, state
);
680 if (block_deref
!= deref
) {
681 nir_instr_rewrite_src(src
->parent_instr
, src
,
682 nir_src_for_ssa(&block_deref
->dest
.ssa
));
683 nir_deref_instr_remove_if_unused(deref
);
684 state
->progress
= true;
690 /** Re-materialize derefs in every block
692 * This pass re-materializes deref instructions in every block in which it is
693 * used. After this pass has been run, every use of a deref will be of a
694 * deref in the same block as the use. Also, all unused derefs will be
695 * deleted as a side-effect.
697 * Derefs used as sources of phi instructions are not rematerialized.
700 nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl
*impl
)
702 struct rematerialize_deref_state state
= { 0 };
703 nir_builder_init(&state
.builder
, impl
);
705 nir_foreach_block(block
, impl
) {
708 /* Start each block with a fresh cache */
710 _mesa_hash_table_clear(state
.cache
, NULL
);
712 nir_foreach_instr_safe(instr
, block
) {
713 if (instr
->type
== nir_instr_type_deref
&&
714 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
717 /* If a deref is used in a phi, we can't rematerialize it, as the new
718 * derefs would appear before the phi, which is not valid.
720 if (instr
->type
== nir_instr_type_phi
)
723 state
.builder
.cursor
= nir_before_instr(instr
);
724 nir_foreach_src(instr
, rematerialize_deref_src
, &state
);
728 nir_if
*following_if
= nir_block_get_following_if(block
);
730 assert(!nir_src_as_deref(following_if
->condition
));
734 _mesa_hash_table_destroy(state
.cache
, NULL
);
736 return state
.progress
;
740 is_trivial_deref_cast(nir_deref_instr
*cast
)
742 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
746 return cast
->mode
== parent
->mode
&&
747 cast
->type
== parent
->type
&&
748 cast
->dest
.ssa
.num_components
== parent
->dest
.ssa
.num_components
&&
749 cast
->dest
.ssa
.bit_size
== parent
->dest
.ssa
.bit_size
;
753 is_trivial_array_deref_cast(nir_deref_instr
*cast
)
755 assert(is_trivial_deref_cast(cast
));
757 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
759 if (parent
->deref_type
== nir_deref_type_array
) {
760 return cast
->cast
.ptr_stride
==
761 glsl_get_explicit_stride(nir_deref_instr_parent(parent
)->type
);
762 } else if (parent
->deref_type
== nir_deref_type_ptr_as_array
) {
763 return cast
->cast
.ptr_stride
==
764 nir_deref_instr_ptr_as_array_stride(parent
);
771 is_deref_ptr_as_array(nir_instr
*instr
)
773 return instr
->type
== nir_instr_type_deref
&&
774 nir_instr_as_deref(instr
)->deref_type
== nir_deref_type_ptr_as_array
;
778 * Remove casts that just wrap other casts.
781 opt_remove_cast_cast(nir_deref_instr
*cast
)
783 nir_deref_instr
*first_cast
= cast
;
786 nir_deref_instr
*parent
= nir_deref_instr_parent(first_cast
);
787 if (parent
== NULL
|| parent
->deref_type
!= nir_deref_type_cast
)
791 if (cast
== first_cast
)
794 nir_instr_rewrite_src(&cast
->instr
, &cast
->parent
,
795 nir_src_for_ssa(first_cast
->parent
.ssa
));
800 * Is this casting a struct to a contained struct.
801 * struct a { struct b field0 };
803 * deref_cast (structb *)ssa_5 (function_temp structb);
805 * deref_struct &ssa_5->field0 (function_temp structb);
806 * This allows subsequent copy propagation to work.
809 opt_replace_struct_wrapper_cast(nir_builder
*b
, nir_deref_instr
*cast
)
811 nir_deref_instr
*parent
= nir_src_as_deref(cast
->parent
);
815 if (!glsl_type_is_struct(parent
->type
))
818 if (glsl_get_struct_field_offset(parent
->type
, 0) != 0)
821 if (cast
->type
!= glsl_get_struct_field(parent
->type
, 0))
824 nir_deref_instr
*replace
= nir_build_deref_struct(b
, parent
, 0);
825 nir_ssa_def_rewrite_uses(&cast
->dest
.ssa
, nir_src_for_ssa(&replace
->dest
.ssa
));
826 nir_deref_instr_remove_if_unused(cast
);
831 opt_deref_cast(nir_builder
*b
, nir_deref_instr
*cast
)
835 if (opt_replace_struct_wrapper_cast(b
, cast
))
838 progress
= opt_remove_cast_cast(cast
);
839 if (!is_trivial_deref_cast(cast
))
842 bool trivial_array_cast
= is_trivial_array_deref_cast(cast
);
844 assert(cast
->dest
.is_ssa
);
845 assert(cast
->parent
.is_ssa
);
847 nir_foreach_use_safe(use_src
, &cast
->dest
.ssa
) {
848 /* If this isn't a trivial array cast, we can't propagate into
849 * ptr_as_array derefs.
851 if (is_deref_ptr_as_array(use_src
->parent_instr
) &&
855 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, cast
->parent
);
859 /* If uses would be a bit crazy */
860 assert(list_empty(&cast
->dest
.ssa
.if_uses
));
862 nir_deref_instr_remove_if_unused(cast
);
867 opt_deref_ptr_as_array(nir_builder
*b
, nir_deref_instr
*deref
)
869 assert(deref
->deref_type
== nir_deref_type_ptr_as_array
);
871 nir_deref_instr
*parent
= nir_deref_instr_parent(deref
);
873 if (nir_src_is_const(deref
->arr
.index
) &&
874 nir_src_as_int(deref
->arr
.index
) == 0) {
875 /* If it's a ptr_as_array deref with an index of 0, it does nothing
876 * and we can just replace its uses with its parent.
878 * The source of a ptr_as_array deref always has a deref_type of
879 * nir_deref_type_array or nir_deref_type_cast. If it's a cast, it
880 * may be trivial and we may be able to get rid of that too. Any
881 * trivial cast of trivial cast cases should be handled already by
882 * opt_deref_cast() above.
884 if (parent
->deref_type
== nir_deref_type_cast
&&
885 is_trivial_deref_cast(parent
))
886 parent
= nir_deref_instr_parent(parent
);
887 nir_ssa_def_rewrite_uses(&deref
->dest
.ssa
,
888 nir_src_for_ssa(&parent
->dest
.ssa
));
889 nir_instr_remove(&deref
->instr
);
893 if (parent
->deref_type
!= nir_deref_type_array
&&
894 parent
->deref_type
!= nir_deref_type_ptr_as_array
)
897 assert(parent
->parent
.is_ssa
);
898 assert(parent
->arr
.index
.is_ssa
);
899 assert(deref
->arr
.index
.is_ssa
);
901 nir_ssa_def
*new_idx
= nir_iadd(b
, parent
->arr
.index
.ssa
,
902 deref
->arr
.index
.ssa
);
904 deref
->deref_type
= parent
->deref_type
;
905 nir_instr_rewrite_src(&deref
->instr
, &deref
->parent
, parent
->parent
);
906 nir_instr_rewrite_src(&deref
->instr
, &deref
->arr
.index
,
907 nir_src_for_ssa(new_idx
));
912 nir_opt_deref_impl(nir_function_impl
*impl
)
914 bool progress
= false;
917 nir_builder_init(&b
, impl
);
919 nir_foreach_block(block
, impl
) {
920 nir_foreach_instr_safe(instr
, block
) {
921 if (instr
->type
!= nir_instr_type_deref
)
924 b
.cursor
= nir_before_instr(instr
);
926 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
927 switch (deref
->deref_type
) {
928 case nir_deref_type_ptr_as_array
:
929 if (opt_deref_ptr_as_array(&b
, deref
))
933 case nir_deref_type_cast
:
934 if (opt_deref_cast(&b
, deref
))
946 nir_metadata_preserve(impl
, nir_metadata_block_index
|
947 nir_metadata_dominance
);
950 impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;
958 nir_opt_deref(nir_shader
*shader
)
960 bool progress
= false;
962 nir_foreach_function(func
, shader
) {
963 if (func
->impl
&& nir_opt_deref_impl(func
->impl
))