47
nir_lower_to_source_mods_block(nir_block *block,
48
nir_lower_to_source_mods_flags options)
48
nir_lower_to_source_mods_instr(nir_builder *b, nir_instr *instr,
51
nir_lower_to_source_mods_flags options =
52
*((nir_lower_to_source_mods_flags*) data);
50
54
bool progress = false;
52
nir_foreach_instr(instr, block) {
53
if (instr->type != nir_instr_type_alu)
56
nir_alu_instr *alu = nir_instr_as_alu(instr);
58
bool lower_abs = (nir_op_infos[alu->op].num_inputs < 3) ||
59
(options & nir_lower_triop_abs);
61
for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
62
if (!alu->src[i].src.is_ssa)
65
if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
68
nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
70
if (parent->dest.saturate)
73
switch (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i])) {
75
if (!(options & nir_lower_float_source_mods))
77
if (!(parent->op == nir_op_fabs && (options & nir_lower_fabs_source_mods)) &&
78
!(parent->op == nir_op_fneg && (options & nir_lower_fneg_source_mods))) {
83
if (!(options & nir_lower_int_source_mods))
85
if (parent->op != nir_op_iabs && parent->op != nir_op_ineg)
92
if (nir_src_bit_size(alu->src[i].src) == 64 &&
93
!(options & nir_lower_64bit_source_mods)) {
97
/* We can only do a rewrite if the source we are copying is SSA.
98
* Otherwise, moving the read might invalidly reorder reads/writes
101
if (!parent->src[0].src.is_ssa)
104
if (!lower_abs && (parent->op == nir_op_fabs ||
105
parent->op == nir_op_iabs ||
109
nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
111
/* Apply any modifiers that come from the parent opcode */
112
if (parent->op == nir_op_fneg || parent->op == nir_op_ineg)
113
alu_src_consume_negate(&alu->src[i]);
114
if (parent->op == nir_op_fabs || parent->op == nir_op_iabs)
115
alu_src_consume_abs(&alu->src[i]);
117
/* Apply modifiers from the parent source */
118
if (parent->src[0].negate)
119
alu_src_consume_negate(&alu->src[i]);
120
if (parent->src[0].abs)
121
alu_src_consume_abs(&alu->src[i]);
123
for (int j = 0; j < 4; ++j) {
124
if (!nir_alu_instr_channel_used(alu, i, j))
126
alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
129
if (nir_ssa_def_is_unused(&parent->dest.dest.ssa))
130
nir_instr_remove(&parent->instr);
56
if (instr->type != nir_instr_type_alu)
59
nir_alu_instr *alu = nir_instr_as_alu(instr);
61
bool lower_abs = (nir_op_infos[alu->op].num_inputs < 3) ||
62
(options & nir_lower_triop_abs);
64
for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
65
if (!alu->src[i].src.is_ssa)
68
if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
71
nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
73
if (parent->dest.saturate)
76
if (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i]) != nir_type_float)
79
if (!(parent->op == nir_op_fabs && (options & nir_lower_fabs_source_mods)) &&
80
!(parent->op == nir_op_fneg && (options & nir_lower_fneg_source_mods))) {
135
/* We've covered sources. Now we're going to try and saturate the
136
* destination if we can.
84
if (nir_src_bit_size(alu->src[i].src) == 64)
87
/* We can only do a rewrite if the source we are copying is SSA.
88
* Otherwise, moving the read might invalidly reorder reads/writes
139
if (!alu->dest.dest.is_ssa)
142
if (nir_dest_bit_size(alu->dest.dest) == 64 &&
143
!(options & nir_lower_64bit_source_mods)) {
147
/* We can only saturate float destinations */
148
if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
152
if (!(options & nir_lower_float_source_mods))
155
bool all_children_are_sat = true;
156
nir_foreach_use_including_if(child_src, &alu->dest.dest.ssa) {
157
if (child_src->is_if) {
158
all_children_are_sat = false;
162
assert(child_src->is_ssa);
163
nir_instr *child = child_src->parent_instr;
164
if (child->type != nir_instr_type_alu) {
165
all_children_are_sat = false;
169
nir_alu_instr *child_alu = nir_instr_as_alu(child);
170
if (child_alu->src[0].negate || child_alu->src[0].abs) {
171
all_children_are_sat = false;
175
if (child_alu->op != nir_op_fsat) {
176
all_children_are_sat = false;
181
if (!all_children_are_sat)
184
alu->dest.saturate = true;
91
if (!parent->src[0].src.is_ssa)
94
if (!lower_abs && (parent->op == nir_op_fabs || parent->src[0].abs))
97
nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
99
/* Apply any modifiers that come from the parent opcode */
100
if (parent->op == nir_op_fneg)
101
alu_src_consume_negate(&alu->src[i]);
102
if (parent->op == nir_op_fabs)
103
alu_src_consume_abs(&alu->src[i]);
105
/* Apply modifiers from the parent source */
106
if (parent->src[0].negate)
107
alu_src_consume_negate(&alu->src[i]);
108
if (parent->src[0].abs)
109
alu_src_consume_abs(&alu->src[i]);
111
for (int j = 0; j < 4; ++j) {
112
if (!nir_alu_instr_channel_used(alu, i, j))
114
alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
117
if (nir_ssa_def_is_unused(&parent->dest.dest.ssa))
118
nir_instr_remove(&parent->instr);
187
nir_foreach_use(child_src, &alu->dest.dest.ssa) {
188
assert(child_src->is_ssa);
189
nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
191
child_alu->op = nir_op_mov;
192
child_alu->dest.saturate = false;
193
/* We could propagate the dest of our instruction to the
194
* destinations of the uses here. However, one quick round of
195
* copy propagation will clean that all up and then we don't have
205
nir_lower_to_source_mods_impl(nir_function_impl *impl,
206
nir_lower_to_source_mods_flags options)
208
bool progress = false;
210
nir_foreach_block(block, impl) {
211
progress |= nir_lower_to_source_mods_block(block, options);
215
nir_metadata_preserve(impl, nir_metadata_block_index |
216
nir_metadata_dominance);
123
/* We've covered sources. Now we're going to try and saturate the
124
* destination if we can.
127
if (!alu->dest.dest.is_ssa)
130
if (nir_dest_bit_size(alu->dest.dest) == 64)
133
/* We can only saturate float destinations */
134
if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
138
bool all_children_are_sat = true;
139
nir_foreach_use_including_if(child_src, &alu->dest.dest.ssa) {
140
if (child_src->is_if) {
141
all_children_are_sat = false;
145
assert(child_src->is_ssa);
146
nir_instr *child = child_src->parent_instr;
147
if (child->type != nir_instr_type_alu) {
148
all_children_are_sat = false;
152
nir_alu_instr *child_alu = nir_instr_as_alu(child);
153
if (child_alu->src[0].negate || child_alu->src[0].abs) {
154
all_children_are_sat = false;
158
if (child_alu->op != nir_op_fsat) {
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_mov;
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