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_remove_dead_derefs_impl(nir_function_impl
*impl
)
108 bool progress
= false;
110 nir_foreach_block(block
, impl
) {
111 nir_foreach_instr_safe(instr
, block
) {
112 if (instr
->type
== nir_instr_type_deref
&&
113 nir_deref_instr_remove_if_unused(nir_instr_as_deref(instr
)))
119 nir_metadata_preserve(impl
, nir_metadata_block_index
|
120 nir_metadata_dominance
);
126 nir_remove_dead_derefs(nir_shader
*shader
)
128 bool progress
= false;
129 nir_foreach_function(function
, shader
) {
130 if (function
->impl
&& nir_remove_dead_derefs_impl(function
->impl
))
138 nir_fixup_deref_modes(nir_shader
*shader
)
140 nir_foreach_function(function
, shader
) {
144 nir_foreach_block(block
, function
->impl
) {
145 nir_foreach_instr(instr
, block
) {
146 if (instr
->type
!= nir_instr_type_deref
)
149 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
151 nir_variable_mode parent_mode
;
152 if (deref
->deref_type
== nir_deref_type_var
) {
153 parent_mode
= deref
->var
->data
.mode
;
155 assert(deref
->parent
.is_ssa
);
156 nir_deref_instr
*parent
=
157 nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
);
158 parent_mode
= parent
->mode
;
161 deref
->mode
= parent_mode
;