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"
29 nir_deref_path_init(nir_deref_path
*path
,
30 nir_deref_instr
*deref
, void *mem_ctx
)
32 assert(deref
!= NULL
);
34 /* The length of the short path is at most ARRAY_SIZE - 1 because we need
35 * room for the NULL terminator.
37 static const int max_short_path_len
= ARRAY_SIZE(path
->_short_path
) - 1;
41 nir_deref_instr
**tail
= &path
->_short_path
[max_short_path_len
];
42 nir_deref_instr
**head
= tail
;
45 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
47 if (count
<= max_short_path_len
)
51 if (count
<= max_short_path_len
) {
52 /* If we're under max_short_path_len, just use the short path. */
58 /* Just in case someone uses short_path by accident */
59 for (unsigned i
= 0; i
< ARRAY_SIZE(path
->_short_path
); i
++)
60 path
->_short_path
[i
] = (void *)0xdeadbeef;
63 path
->path
= ralloc_array(mem_ctx
, nir_deref_instr
*, count
+ 1);
64 head
= tail
= path
->path
+ count
;
66 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
))
70 assert(head
== path
->path
);
71 assert(tail
== head
+ count
);
72 assert((*head
)->deref_type
== nir_deref_type_var
);
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 !nir_src_as_const_value(instr
->arr
.index
))
117 instr
= nir_deref_instr_parent(instr
);
124 type_get_array_stride(const struct glsl_type
*elem_type
,
125 glsl_type_size_align_func size_align
)
127 unsigned elem_size
, elem_align
;
128 glsl_get_natural_size_align_bytes(elem_type
, &elem_size
, &elem_align
);
129 return ALIGN_POT(elem_size
, elem_align
);
133 struct_type_get_field_offset(const struct glsl_type
*struct_type
,
134 glsl_type_size_align_func size_align
,
137 assert(glsl_type_is_struct(struct_type
));
139 for (unsigned i
= 0; i
<= field_idx
; i
++) {
140 unsigned elem_size
, elem_align
;
141 glsl_get_natural_size_align_bytes(glsl_get_struct_field(struct_type
, i
),
142 &elem_size
, &elem_align
);
143 offset
= ALIGN_POT(offset
, elem_align
);
151 nir_deref_instr_get_const_offset(nir_deref_instr
*deref
,
152 glsl_type_size_align_func size_align
)
155 nir_deref_path_init(&path
, deref
, NULL
);
157 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
160 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
161 if ((*p
)->deref_type
== nir_deref_type_array
) {
162 offset
+= nir_src_as_const_value((*p
)->arr
.index
)->u32
[0] *
163 type_get_array_stride((*p
)->type
, size_align
);
164 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
165 /* p starts at path[1], so this is safe */
166 nir_deref_instr
*parent
= *(p
- 1);
167 offset
+= struct_type_get_field_offset(parent
->type
, size_align
,
170 unreachable("Unsupported deref type");
174 nir_deref_path_finish(&path
);
180 nir_build_deref_offset(nir_builder
*b
, nir_deref_instr
*deref
,
181 glsl_type_size_align_func size_align
)
184 nir_deref_path_init(&path
, deref
, NULL
);
186 assert(path
.path
[0]->deref_type
== nir_deref_type_var
);
188 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
189 for (nir_deref_instr
**p
= &path
.path
[1]; *p
; p
++) {
190 if ((*p
)->deref_type
== nir_deref_type_array
) {
191 nir_ssa_def
*index
= nir_ssa_for_src(b
, (*p
)->arr
.index
, 1);
192 nir_ssa_def
*stride
=
193 nir_imm_int(b
, type_get_array_stride((*p
)->type
, size_align
));
194 offset
= nir_iadd(b
, offset
, nir_imul(b
, index
, stride
));
195 } else if ((*p
)->deref_type
== nir_deref_type_struct
) {
196 /* p starts at path[1], so this is safe */
197 nir_deref_instr
*parent
= *(p
- 1);
198 unsigned field_offset
=
199 struct_type_get_field_offset(parent
->type
, size_align
,
201 nir_iadd(b
, offset
, nir_imm_int(b
, field_offset
));
203 unreachable("Unsupported deref type");
207 nir_deref_path_finish(&path
);
213 nir_remove_dead_derefs_impl(nir_function_impl
*impl
)
215 bool progress
= false;
217 nir_foreach_block(block
, impl
) {
218 nir_foreach_instr_safe(instr
, block
) {
219 if (instr
->type
== nir_instr_type_deref
&&
220 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
226 nir_metadata_preserve(impl
, nir_metadata_block_index
|
227 nir_metadata_dominance
);
233 nir_remove_dead_derefs(nir_shader
*shader
)
235 bool progress
= false;
236 nir_foreach_function(function
, shader
) {
237 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
245 nir_fixup_deref_modes(nir_shader
*shader
)
247 nir_foreach_function(function
, shader
) {
251 nir_foreach_block(block
, function
->impl
) {
252 nir_foreach_instr(instr
, block
) {
253 if (instr
->type
!= nir_instr_type_deref
)
256 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
258 nir_variable_mode parent_mode
;
259 if (deref
->deref_type
== nir_deref_type_var
) {
260 parent_mode
= deref
->var
->data
.mode
;
262 assert(deref
->parent
.is_ssa
);
263 nir_deref_instr
*parent
=
264 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
265 parent_mode
= parent
->mode
;
268 deref
->mode
= parent_mode
;