2 * Copyright © 2015 Broadcom
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
26 /** @file nir_opt_undef.c
28 * Handles optimization of operations involving ssa_undef. For now, we just
29 * make sure that csels between undef and some other value just give the other
30 * value (on the assumption that the condition's going to be choosing the
31 * defined value). This reduces work after if flattening when each side of
32 * the if is defining a variable.
34 * Some day, we may find some use for making other operations consuming an
35 * undef arg output undef, but I don't know of any cases currently.
39 opt_undef_alu(nir_alu_instr
*instr
)
41 if (instr
->op
!= nir_op_bcsel
&& instr
->op
!= nir_op_fcsel
)
44 assert(instr
->dest
.dest
.is_ssa
);
46 for (int i
= 1; i
<= 2; i
++) {
47 if (!instr
->src
[i
].src
.is_ssa
)
50 nir_instr
*parent
= instr
->src
[i
].src
.ssa
->parent_instr
;
51 if (parent
->type
!= nir_instr_type_ssa_undef
)
54 /* We can't just use nir_alu_src_copy, because we need the def/use
57 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[0].src
,
58 instr
->src
[i
== 1 ? 2 : 1].src
);
59 nir_alu_src_copy(&instr
->src
[0], &instr
->src
[i
== 1 ? 2 : 1],
60 ralloc_parent(instr
));
63 memset(&empty_src
, 0, sizeof(empty_src
));
64 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[1].src
, empty_src
);
65 nir_instr_rewrite_src(&instr
->instr
, &instr
->src
[2].src
, empty_src
);
66 instr
->op
= nir_op_imov
;
75 opt_undef_block(nir_block
*block
, void *data
)
77 bool *progress
= data
;
79 nir_foreach_instr_safe(block
, instr
) {
80 if (instr
->type
== nir_instr_type_alu
)
81 if (opt_undef_alu(nir_instr_as_alu(instr
)))
89 nir_opt_undef(nir_shader
*shader
)
91 bool progress
= false;
93 nir_foreach_function(shader
, function
) {
95 nir_foreach_block(function
->impl
, opt_undef_block
, &progress
);
97 nir_metadata_preserve(function
->impl
,
98 nir_metadata_block_index
|
99 nir_metadata_dominance
);