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 * Jason Ekstrand (jason@jlekstrand.net)
31 * This pass lowers the neg, abs, and sat operations to source modifiers on
32 * ALU operations to make things nicer for the backend. It's just much
33 * easier to not have them when we're doing optimizations.
37 nir_lower_to_source_mods_block(nir_block
*block
,
38 nir_lower_to_source_mods_flags options
)
40 bool progress
= false;
42 nir_foreach_instr(instr
, block
) {
43 if (instr
->type
!= nir_instr_type_alu
)
46 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
48 bool lower_abs
= (nir_op_infos
[alu
->op
].num_inputs
< 3) ||
49 (options
& nir_lower_triop_abs
);
51 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
52 if (!alu
->src
[i
].src
.is_ssa
)
55 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
58 nir_alu_instr
*parent
= nir_instr_as_alu(alu
->src
[i
].src
.ssa
->parent_instr
);
60 if (parent
->dest
.saturate
)
63 switch (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].input_types
[i
])) {
65 if (!(options
& nir_lower_float_source_mods
))
67 if (parent
->op
!= nir_op_fmov
)
71 if (!(options
& nir_lower_int_source_mods
))
73 if (parent
->op
!= nir_op_imov
)
80 /* We can only do a rewrite if the source we are copying is SSA.
81 * Otherwise, moving the read might invalidly reorder reads/writes
84 if (!parent
->src
[0].src
.is_ssa
)
87 if (!lower_abs
&& parent
->src
[0].abs
)
90 nir_instr_rewrite_src(instr
, &alu
->src
[i
].src
, parent
->src
[0].src
);
91 if (alu
->src
[i
].abs
) {
92 /* abs trumps both neg and abs, do nothing */
94 alu
->src
[i
].negate
= (alu
->src
[i
].negate
!= parent
->src
[0].negate
);
95 alu
->src
[i
].abs
|= parent
->src
[0].abs
;
98 for (int j
= 0; j
< 4; ++j
) {
99 if (!nir_alu_instr_channel_used(alu
, i
, j
))
101 alu
->src
[i
].swizzle
[j
] = parent
->src
[0].swizzle
[alu
->src
[i
].swizzle
[j
]];
104 if (list_empty(&parent
->dest
.dest
.ssa
.uses
) &&
105 list_empty(&parent
->dest
.dest
.ssa
.if_uses
))
106 nir_instr_remove(&parent
->instr
);
111 if (options
& nir_lower_float_source_mods
) {
114 alu
->op
= nir_op_fmov
;
115 alu
->dest
.saturate
= true;
118 alu
->op
= nir_op_fmov
;
119 alu
->src
[0].negate
= !alu
->src
[0].negate
;
122 alu
->op
= nir_op_fmov
;
123 alu
->src
[0].abs
= true;
124 alu
->src
[0].negate
= false;
131 if (options
& nir_lower_int_source_mods
) {
134 alu
->op
= nir_op_imov
;
135 alu
->src
[0].negate
= !alu
->src
[0].negate
;
138 alu
->op
= nir_op_imov
;
139 alu
->src
[0].abs
= true;
140 alu
->src
[0].negate
= false;
146 /* We've covered sources. Now we're going to try and saturate the
147 * destination if we can.
150 if (!alu
->dest
.dest
.is_ssa
)
153 /* We can only saturate float destinations */
154 if (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].output_type
) !=
158 if (!(options
& nir_lower_float_source_mods
))
161 if (!list_empty(&alu
->dest
.dest
.ssa
.if_uses
))
164 bool all_children_are_sat
= true;
165 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
166 assert(child_src
->is_ssa
);
167 nir_instr
*child
= child_src
->parent_instr
;
168 if (child
->type
!= nir_instr_type_alu
) {
169 all_children_are_sat
= false;
173 nir_alu_instr
*child_alu
= nir_instr_as_alu(child
);
174 if (child_alu
->src
[0].negate
|| child_alu
->src
[0].abs
) {
175 all_children_are_sat
= false;
179 if (child_alu
->op
!= nir_op_fsat
&&
180 !(child_alu
->op
== nir_op_fmov
&& child_alu
->dest
.saturate
)) {
181 all_children_are_sat
= false;
186 if (!all_children_are_sat
)
189 alu
->dest
.saturate
= true;
192 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
193 assert(child_src
->is_ssa
);
194 nir_alu_instr
*child_alu
= nir_instr_as_alu(child_src
->parent_instr
);
196 child_alu
->op
= nir_op_fmov
;
197 child_alu
->dest
.saturate
= false;
198 /* We could propagate the dest of our instruction to the
199 * destinations of the uses here. However, one quick round of
200 * copy propagation will clean that all up and then we don't have
210 nir_lower_to_source_mods_impl(nir_function_impl
*impl
,
211 nir_lower_to_source_mods_flags options
)
213 bool progress
= false;
215 nir_foreach_block(block
, impl
) {
216 progress
|= nir_lower_to_source_mods_block(block
, options
);
220 nir_metadata_preserve(impl
, nir_metadata_block_index
|
221 nir_metadata_dominance
);
227 nir_lower_to_source_mods(nir_shader
*shader
,
228 nir_lower_to_source_mods_flags options
)
230 bool progress
= false;
232 nir_foreach_function(function
, shader
) {
233 if (function
->impl
) {
234 progress
|= nir_lower_to_source_mods_impl(function
->impl
, options
);