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
)
39 bool progress
= false;
41 nir_foreach_instr(instr
, block
) {
42 if (instr
->type
!= nir_instr_type_alu
)
45 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
47 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
48 if (!alu
->src
[i
].src
.is_ssa
)
51 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
54 nir_alu_instr
*parent
= nir_instr_as_alu(alu
->src
[i
].src
.ssa
->parent_instr
);
56 if (parent
->dest
.saturate
)
59 switch (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].input_types
[i
])) {
61 if (parent
->op
!= nir_op_fmov
)
65 if (parent
->op
!= nir_op_imov
)
72 /* We can only do a rewrite if the source we are copying is SSA.
73 * Otherwise, moving the read might invalidly reorder reads/writes
76 if (!parent
->src
[0].src
.is_ssa
)
79 nir_instr_rewrite_src(instr
, &alu
->src
[i
].src
, parent
->src
[0].src
);
80 if (alu
->src
[i
].abs
) {
81 /* abs trumps both neg and abs, do nothing */
83 alu
->src
[i
].negate
= (alu
->src
[i
].negate
!= parent
->src
[0].negate
);
84 alu
->src
[i
].abs
|= parent
->src
[0].abs
;
87 for (int j
= 0; j
< 4; ++j
) {
88 if (!nir_alu_instr_channel_used(alu
, i
, j
))
90 alu
->src
[i
].swizzle
[j
] = parent
->src
[0].swizzle
[alu
->src
[i
].swizzle
[j
]];
93 if (list_empty(&parent
->dest
.dest
.ssa
.uses
) &&
94 list_empty(&parent
->dest
.dest
.ssa
.if_uses
))
95 nir_instr_remove(&parent
->instr
);
102 alu
->op
= nir_op_fmov
;
103 alu
->dest
.saturate
= true;
106 alu
->op
= nir_op_imov
;
107 alu
->src
[0].negate
= !alu
->src
[0].negate
;
110 alu
->op
= nir_op_fmov
;
111 alu
->src
[0].negate
= !alu
->src
[0].negate
;
114 alu
->op
= nir_op_imov
;
115 alu
->src
[0].abs
= true;
116 alu
->src
[0].negate
= false;
119 alu
->op
= nir_op_fmov
;
120 alu
->src
[0].abs
= true;
121 alu
->src
[0].negate
= false;
127 /* We've covered sources. Now we're going to try and saturate the
128 * destination if we can.
131 if (!alu
->dest
.dest
.is_ssa
)
134 /* We can only saturate float destinations */
135 if (nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].output_type
) !=
139 if (!list_empty(&alu
->dest
.dest
.ssa
.if_uses
))
142 bool all_children_are_sat
= true;
143 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
144 assert(child_src
->is_ssa
);
145 nir_instr
*child
= child_src
->parent_instr
;
146 if (child
->type
!= nir_instr_type_alu
) {
147 all_children_are_sat
= false;
151 nir_alu_instr
*child_alu
= nir_instr_as_alu(child
);
152 if (child_alu
->src
[0].negate
|| child_alu
->src
[0].abs
) {
153 all_children_are_sat
= false;
157 if (child_alu
->op
!= nir_op_fsat
&&
158 !(child_alu
->op
== nir_op_fmov
&& child_alu
->dest
.saturate
)) {
159 all_children_are_sat
= false;
164 if (!all_children_are_sat
)
167 alu
->dest
.saturate
= true;
170 nir_foreach_use(child_src
, &alu
->dest
.dest
.ssa
) {
171 assert(child_src
->is_ssa
);
172 nir_alu_instr
*child_alu
= nir_instr_as_alu(child_src
->parent_instr
);
174 child_alu
->op
= nir_op_fmov
;
175 child_alu
->dest
.saturate
= false;
176 /* We could propagate the dest of our instruction to the
177 * destinations of the uses here. However, one quick round of
178 * copy propagation will clean that all up and then we don't have
188 nir_lower_to_source_mods_impl(nir_function_impl
*impl
)
190 bool progress
= false;
192 nir_foreach_block(block
, impl
) {
193 progress
|= nir_lower_to_source_mods_block(block
);
197 nir_metadata_preserve(impl
, nir_metadata_block_index
|
198 nir_metadata_dominance
);
204 nir_lower_to_source_mods(nir_shader
*shader
)
206 bool progress
= false;
208 nir_foreach_function(function
, shader
) {
209 if (function
->impl
) {
210 progress
|= nir_lower_to_source_mods_impl(function
->impl
);