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
, void *state
)
39 nir_foreach_instr(block
, instr
) {
40 if (instr
->type
!= nir_instr_type_alu
)
43 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
45 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
46 if (!alu
->src
[i
].src
.is_ssa
)
49 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
52 nir_alu_instr
*parent
= nir_instr_as_alu(alu
->src
[i
].src
.ssa
->parent_instr
);
54 if (parent
->dest
.saturate
)
57 switch (nir_op_infos
[alu
->op
].input_types
[i
]) {
59 if (parent
->op
!= nir_op_fmov
)
63 if (parent
->op
!= nir_op_imov
)
70 nir_instr_rewrite_src(instr
, &alu
->src
[i
].src
, parent
->src
[0].src
);
71 if (alu
->src
[i
].abs
) {
72 /* abs trumps both neg and abs, do nothing */
74 alu
->src
[i
].negate
= (alu
->src
[i
].negate
!= parent
->src
[0].negate
);
75 alu
->src
[i
].abs
|= parent
->src
[0].abs
;
78 for (int j
= 0; j
< 4; ++j
) {
79 if (!nir_alu_instr_channel_used(alu
, i
, j
))
81 alu
->src
[i
].swizzle
[j
] = parent
->src
[0].swizzle
[alu
->src
[i
].swizzle
[j
]];
84 if (parent
->dest
.dest
.ssa
.uses
->entries
== 0 &&
85 parent
->dest
.dest
.ssa
.if_uses
->entries
== 0)
86 nir_instr_remove(&parent
->instr
);
91 alu
->op
= nir_op_fmov
;
92 alu
->dest
.saturate
= true;
95 alu
->op
= nir_op_imov
;
96 alu
->src
[0].negate
= !alu
->src
[0].negate
;
99 alu
->op
= nir_op_fmov
;
100 alu
->src
[0].negate
= !alu
->src
[0].negate
;
103 alu
->op
= nir_op_imov
;
104 alu
->src
[0].abs
= true;
105 alu
->src
[0].negate
= false;
108 alu
->op
= nir_op_fmov
;
109 alu
->src
[0].abs
= true;
110 alu
->src
[0].negate
= false;
116 /* We've covered sources. Now we're going to try and saturate the
117 * destination if we can.
120 if (!alu
->dest
.dest
.is_ssa
)
123 if (alu
->dest
.dest
.ssa
.if_uses
->entries
!= 0)
126 bool all_children_are_sat
= true;
127 struct set_entry
*entry
;
128 set_foreach(alu
->dest
.dest
.ssa
.uses
, entry
) {
129 const nir_instr
*child
= entry
->key
;
130 if (child
->type
!= nir_instr_type_alu
) {
131 all_children_are_sat
= false;
135 nir_alu_instr
*child_alu
= nir_instr_as_alu(child
);
136 if (child_alu
->src
[0].negate
|| child_alu
->src
[0].abs
) {
137 all_children_are_sat
= false;
141 if (child_alu
->op
!= nir_op_fsat
&&
142 !(child_alu
->op
== nir_op_fmov
&& child_alu
->dest
.saturate
)) {
143 all_children_are_sat
= false;
148 if (!all_children_are_sat
)
151 alu
->dest
.saturate
= true;
153 set_foreach(alu
->dest
.dest
.ssa
.uses
, entry
) {
154 nir_alu_instr
*child_alu
= nir_instr_as_alu((nir_instr
*)entry
->key
);
155 child_alu
->op
= nir_op_fmov
;
156 child_alu
->dest
.saturate
= false;
157 /* We could propagate the dest of our instruction to the
158 * destinations of the uses here. However, one quick round of
159 * copy propagation will clean that all up and then we don't have
169 nir_lower_to_source_mods_impl(nir_function_impl
*impl
)
171 nir_foreach_block(impl
, nir_lower_to_source_mods_block
, NULL
);
175 nir_lower_to_source_mods(nir_shader
*shader
)
177 nir_foreach_overload(shader
, overload
) {
179 nir_lower_to_source_mods_impl(overload
->impl
);