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 alu_src_consume_abs(nir_alu_src
*src
)
43 alu_src_consume_negate(nir_alu_src
*src
)
45 /* If abs is set on the source, the negate goes away */
47 src
->negate
= !src
->negate
;
51 nir_lower_to_source_mods_block(nir_block
*block
,
52 nir_lower_to_source_mods_flags options
)
54 bool progress
= false;
56 nir_foreach_instr(instr
, block
) {
57 if (instr
->type
!= nir_instr_type_alu
)
60 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
62 bool lower_abs
= (nir_op_infos
[alu
->op
].num_inputs
< 3) ||
63 (options
& nir_lower_triop_abs
);
65 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
66 if (!alu
->src
[i
].src
.is_ssa
)
69 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
72 nir_alu_instr
*parent
= nir_instr_as_alu(alu
->src
[i
].src
.ssa
->parent_instr
);
74 if (parent
->dest
.saturate
)
77 switch (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].input_types
[i
])) {
79 if (!(options
& nir_lower_float_source_mods
))
81 if (parent
->op
!= nir_op_fabs
&& parent
->op
!= nir_op_fneg
)
85 if (!(options
& nir_lower_int_source_mods
))
87 if (parent
->op
!= nir_op_iabs
&& parent
->op
!= nir_op_ineg
)
94 /* We can only do a rewrite if the source we are copying is SSA.
95 * Otherwise, moving the read might invalidly reorder reads/writes
98 if (!parent
->src
[0].src
.is_ssa
)
101 if (!lower_abs
&& (parent
->op
== nir_op_fabs
||
102 parent
->op
== nir_op_iabs
))
105 nir_instr_rewrite_src(instr
, &alu
->src
[i
].src
, parent
->src
[0].src
);
107 /* Apply any modifiers that come from the parent opcode */
108 if (parent
->op
== nir_op_fneg
|| parent
->op
== nir_op_ineg
)
109 alu_src_consume_negate(&alu
->src
[i
]);
110 if (parent
->op
== nir_op_fabs
|| parent
->op
== nir_op_iabs
)
111 alu_src_consume_abs(&alu
->src
[i
]);
113 /* Apply modifiers from the parent source */
114 if (parent
->src
[0].negate
)
115 alu_src_consume_negate(&alu
->src
[i
]);
116 if (parent
->src
[0].abs
)
117 alu_src_consume_abs(&alu
->src
[i
]);
119 for (int j
= 0; j
< 4; ++j
) {
120 if (!nir_alu_instr_channel_used(alu
, i
, j
))
122 alu
->src
[i
].swizzle
[j
] = parent
->src
[0].swizzle
[alu
->src
[i
].swizzle
[j
]];
125 if (list_is_empty(&parent
->dest
.dest
.ssa
.uses
) &&
126 list_is_empty(&parent
->dest
.dest
.ssa
.if_uses
))
127 nir_instr_remove(&parent
->instr
);
132 /* We've covered sources. Now we're going to try and saturate the
133 * destination if we can.
136 if (!alu
->dest
.dest
.is_ssa
)
139 /* We can only saturate float destinations */
140 if (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].output_type
) !=
144 if (!(options
& nir_lower_float_source_mods
))
147 if (!list_is_empty(&alu
->dest
.dest
.ssa
.if_uses
))
150 bool all_children_are_sat
= true;
151 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
152 assert(child_src
->is_ssa
);
153 nir_instr
*child
= child_src
->parent_instr
;
154 if (child
->type
!= nir_instr_type_alu
) {
155 all_children_are_sat
= false;
159 nir_alu_instr
*child_alu
= nir_instr_as_alu(child
);
160 if (child_alu
->src
[0].negate
|| child_alu
->src
[0].abs
) {
161 all_children_are_sat
= false;
165 if (child_alu
->op
!= nir_op_fsat
) {
166 all_children_are_sat
= false;
171 if (!all_children_are_sat
)
174 alu
->dest
.saturate
= true;
177 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
178 assert(child_src
->is_ssa
);
179 nir_alu_instr
*child_alu
= nir_instr_as_alu(child_src
->parent_instr
);
181 child_alu
->op
= nir_op_mov
;
182 child_alu
->dest
.saturate
= false;
183 /* We could propagate the dest of our instruction to the
184 * destinations of the uses here. However, one quick round of
185 * copy propagation will clean that all up and then we don't have
195 nir_lower_to_source_mods_impl(nir_function_impl
*impl
,
196 nir_lower_to_source_mods_flags options
)
198 bool progress
= false;
200 nir_foreach_block(block
, impl
) {
201 progress
|= nir_lower_to_source_mods_block(block
, options
);
205 nir_metadata_preserve(impl
, nir_metadata_block_index
|
206 nir_metadata_dominance
);
212 nir_lower_to_source_mods(nir_shader
*shader
,
213 nir_lower_to_source_mods_flags options
)
215 bool progress
= false;
217 nir_foreach_function(function
, shader
) {
218 if (function
->impl
) {
219 progress
|= nir_lower_to_source_mods_impl(function
->impl
, options
);