2 * Copyright © 2014 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
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include <main/imports.h>
32 * SSA-based copy propagation
35 static bool is_move(nir_alu_instr
*instr
)
37 if (instr
->op
!= nir_op_fmov
&&
38 instr
->op
!= nir_op_imov
)
41 if (instr
->dest
.saturate
)
44 /* we handle modifiers in a separate pass */
46 if (instr
->src
[0].abs
|| instr
->src
[0].negate
)
49 if (!instr
->src
[0].src
.is_ssa
)
56 static bool is_vec(nir_alu_instr
*instr
)
58 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
59 if (!instr
->src
[i
].src
.is_ssa
)
62 /* we handle modifiers in a separate pass */
63 if (instr
->src
[i
].abs
|| instr
->src
[i
].negate
)
67 return instr
->op
== nir_op_vec2
||
68 instr
->op
== nir_op_vec3
||
69 instr
->op
== nir_op_vec4
;
73 is_swizzleless_move(nir_alu_instr
*instr
)
76 for (unsigned i
= 0; i
< 4; i
++) {
77 if (!((instr
->dest
.write_mask
>> i
) & 1))
79 if (instr
->src
[0].swizzle
[i
] != i
)
83 } else if (is_vec(instr
)) {
84 nir_ssa_def
*def
= NULL
;
85 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
86 if (instr
->src
[i
].swizzle
[0] != i
)
90 def
= instr
->src
[i
].src
.ssa
;
91 } else if (instr
->src
[i
].src
.ssa
!= def
) {
102 copy_prop_src(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
,
103 unsigned num_components
)
106 if (src
->reg
.indirect
)
107 return copy_prop_src(src
->reg
.indirect
, parent_instr
, parent_if
, 1);
111 nir_instr
*src_instr
= src
->ssa
->parent_instr
;
112 nir_ssa_def
*copy_def
;
113 if (src_instr
->type
== nir_instr_type_alu
) {
114 nir_alu_instr
*alu_instr
= nir_instr_as_alu(src_instr
);
115 if (!is_swizzleless_move(alu_instr
))
118 if (alu_instr
->src
[0].src
.ssa
->num_components
!= num_components
)
121 copy_def
= alu_instr
->src
[0].src
.ssa
;
127 nir_instr_rewrite_src(parent_instr
, src
, nir_src_for_ssa(copy_def
));
129 assert(src
== &parent_if
->condition
);
130 nir_if_rewrite_condition(parent_if
, nir_src_for_ssa(copy_def
));
137 copy_prop_alu_src(nir_alu_instr
*parent_alu_instr
, unsigned index
)
139 nir_alu_src
*src
= &parent_alu_instr
->src
[index
];
140 if (!src
->src
.is_ssa
) {
141 if (src
->src
.reg
.indirect
)
142 return copy_prop_src(src
->src
.reg
.indirect
, &parent_alu_instr
->instr
,
147 nir_instr
*src_instr
= src
->src
.ssa
->parent_instr
;
148 if (src_instr
->type
!= nir_instr_type_alu
)
151 nir_alu_instr
*alu_instr
= nir_instr_as_alu(src_instr
);
152 if (!is_move(alu_instr
) && !is_vec(alu_instr
))
156 unsigned new_swizzle
[4] = {0, 0, 0, 0};
158 if (alu_instr
->op
== nir_op_fmov
||
159 alu_instr
->op
== nir_op_imov
) {
160 for (unsigned i
= 0; i
< 4; i
++)
161 new_swizzle
[i
] = alu_instr
->src
[0].swizzle
[src
->swizzle
[i
]];
162 def
= alu_instr
->src
[0].src
.ssa
;
166 for (unsigned i
= 0; i
< 4; i
++) {
167 if (!nir_alu_instr_channel_used(parent_alu_instr
, index
, i
))
170 nir_ssa_def
*new_def
= alu_instr
->src
[src
->swizzle
[i
]].src
.ssa
;
177 new_swizzle
[i
] = alu_instr
->src
[src
->swizzle
[i
]].swizzle
[0];
181 for (unsigned i
= 0; i
< 4; i
++)
182 src
->swizzle
[i
] = new_swizzle
[i
];
184 nir_instr_rewrite_src(&parent_alu_instr
->instr
, &src
->src
,
185 nir_src_for_ssa(def
));
191 copy_prop_dest(nir_dest
*dest
, nir_instr
*instr
)
193 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
194 return copy_prop_src(dest
->reg
.indirect
, instr
, NULL
, 1);
200 copy_prop_instr(nir_instr
*instr
)
202 bool progress
= false;
203 switch (instr
->type
) {
204 case nir_instr_type_alu
: {
205 nir_alu_instr
*alu_instr
= nir_instr_as_alu(instr
);
207 for (unsigned i
= 0; i
< nir_op_infos
[alu_instr
->op
].num_inputs
; i
++)
208 while (copy_prop_alu_src(alu_instr
, i
))
211 while (copy_prop_dest(&alu_instr
->dest
.dest
, instr
))
217 case nir_instr_type_deref
: {
218 nir_deref_instr
*deref
= nir_instr_as_deref(instr
);
220 if (deref
->deref_type
!= nir_deref_type_var
) {
221 assert(deref
->dest
.is_ssa
);
222 const unsigned comps
= deref
->dest
.ssa
.num_components
;
223 while (copy_prop_src(&deref
->parent
, instr
, NULL
, comps
))
227 if (deref
->deref_type
== nir_deref_type_array
||
228 deref
->deref_type
== nir_deref_type_ptr_as_array
) {
229 while (copy_prop_src(&deref
->arr
.index
, instr
, NULL
, 1))
236 case nir_instr_type_tex
: {
237 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
238 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
239 unsigned num_components
= nir_tex_instr_src_size(tex
, i
);
240 while (copy_prop_src(&tex
->src
[i
].src
, instr
, NULL
, num_components
))
244 while (copy_prop_dest(&tex
->dest
, instr
))
250 case nir_instr_type_intrinsic
: {
251 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
253 i
< nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
; i
++) {
254 unsigned num_components
= nir_intrinsic_src_components(intrin
, i
);
256 while (copy_prop_src(&intrin
->src
[i
], instr
, NULL
, num_components
))
260 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
) {
261 while (copy_prop_dest(&intrin
->dest
, instr
))
268 case nir_instr_type_phi
: {
269 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
270 assert(phi
->dest
.is_ssa
);
271 unsigned num_components
= phi
->dest
.ssa
.num_components
;
272 nir_foreach_phi_src(src
, phi
) {
273 while (copy_prop_src(&src
->src
, instr
, NULL
, num_components
))
286 copy_prop_if(nir_if
*if_stmt
)
288 return copy_prop_src(&if_stmt
->condition
, NULL
, if_stmt
, 1);
292 nir_copy_prop_impl(nir_function_impl
*impl
)
294 bool progress
= false;
296 nir_foreach_block(block
, impl
) {
297 nir_foreach_instr(instr
, block
) {
298 if (copy_prop_instr(instr
))
302 nir_if
*if_stmt
= nir_block_get_following_if(block
);
303 if (if_stmt
&& copy_prop_if(if_stmt
))
308 nir_metadata_preserve(impl
, nir_metadata_block_index
|
309 nir_metadata_dominance
);
316 nir_copy_prop(nir_shader
*shader
)
318 bool progress
= false;
320 nir_foreach_function(function
, shader
) {
321 if (function
->impl
&& nir_copy_prop_impl(function
->impl
))