nir: Unset metadata debug bit if no progress made
[mesa.git] / src / compiler / nir / nir_lower_to_source_mods.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29
30 /*
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.
34 */
35
36 static bool
37 nir_lower_to_source_mods_block(nir_block *block,
38 nir_lower_to_source_mods_flags options)
39 {
40 bool progress = false;
41
42 nir_foreach_instr(instr, block) {
43 if (instr->type != nir_instr_type_alu)
44 continue;
45
46 nir_alu_instr *alu = nir_instr_as_alu(instr);
47
48 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
49 if (!alu->src[i].src.is_ssa)
50 continue;
51
52 if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
53 continue;
54
55 nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
56
57 if (parent->dest.saturate)
58 continue;
59
60 switch (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i])) {
61 case nir_type_float:
62 if (!(options & nir_lower_float_source_mods))
63 continue;
64 if (parent->op != nir_op_fmov)
65 continue;
66 break;
67 case nir_type_int:
68 if (!(options & nir_lower_int_source_mods))
69 continue;
70 if (parent->op != nir_op_imov)
71 continue;
72 break;
73 default:
74 continue;
75 }
76
77 /* We can only do a rewrite if the source we are copying is SSA.
78 * Otherwise, moving the read might invalidly reorder reads/writes
79 * on a register.
80 */
81 if (!parent->src[0].src.is_ssa)
82 continue;
83
84 nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
85 if (alu->src[i].abs) {
86 /* abs trumps both neg and abs, do nothing */
87 } else {
88 alu->src[i].negate = (alu->src[i].negate != parent->src[0].negate);
89 alu->src[i].abs |= parent->src[0].abs;
90 }
91
92 for (int j = 0; j < 4; ++j) {
93 if (!nir_alu_instr_channel_used(alu, i, j))
94 continue;
95 alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
96 }
97
98 if (list_empty(&parent->dest.dest.ssa.uses) &&
99 list_empty(&parent->dest.dest.ssa.if_uses))
100 nir_instr_remove(&parent->instr);
101
102 progress = true;
103 }
104
105 if (options & nir_lower_float_source_mods) {
106 switch (alu->op) {
107 case nir_op_fsat:
108 alu->op = nir_op_fmov;
109 alu->dest.saturate = true;
110 break;
111 case nir_op_fneg:
112 alu->op = nir_op_fmov;
113 alu->src[0].negate = !alu->src[0].negate;
114 break;
115 case nir_op_fabs:
116 alu->op = nir_op_fmov;
117 alu->src[0].abs = true;
118 alu->src[0].negate = false;
119 break;
120 default:
121 break;
122 }
123 }
124
125 if (options & nir_lower_int_source_mods) {
126 switch (alu->op) {
127 case nir_op_ineg:
128 alu->op = nir_op_imov;
129 alu->src[0].negate = !alu->src[0].negate;
130 break;
131 case nir_op_iabs:
132 alu->op = nir_op_imov;
133 alu->src[0].abs = true;
134 alu->src[0].negate = false;
135 break;
136 default:
137 break;
138 }
139 }
140 /* We've covered sources. Now we're going to try and saturate the
141 * destination if we can.
142 */
143
144 if (!alu->dest.dest.is_ssa)
145 continue;
146
147 /* We can only saturate float destinations */
148 if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
149 nir_type_float)
150 continue;
151
152 if (!(options & nir_lower_float_source_mods))
153 continue;
154
155 if (!list_empty(&alu->dest.dest.ssa.if_uses))
156 continue;
157
158 bool all_children_are_sat = true;
159 nir_foreach_use(child_src, &alu->dest.dest.ssa) {
160 assert(child_src->is_ssa);
161 nir_instr *child = child_src->parent_instr;
162 if (child->type != nir_instr_type_alu) {
163 all_children_are_sat = false;
164 continue;
165 }
166
167 nir_alu_instr *child_alu = nir_instr_as_alu(child);
168 if (child_alu->src[0].negate || child_alu->src[0].abs) {
169 all_children_are_sat = false;
170 continue;
171 }
172
173 if (child_alu->op != nir_op_fsat &&
174 !(child_alu->op == nir_op_fmov && child_alu->dest.saturate)) {
175 all_children_are_sat = false;
176 continue;
177 }
178 }
179
180 if (!all_children_are_sat)
181 continue;
182
183 alu->dest.saturate = true;
184 progress = true;
185
186 nir_foreach_use(child_src, &alu->dest.dest.ssa) {
187 assert(child_src->is_ssa);
188 nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
189
190 child_alu->op = nir_op_fmov;
191 child_alu->dest.saturate = false;
192 /* We could propagate the dest of our instruction to the
193 * destinations of the uses here. However, one quick round of
194 * copy propagation will clean that all up and then we don't have
195 * the complexity.
196 */
197 }
198 }
199
200 return progress;
201 }
202
203 static bool
204 nir_lower_to_source_mods_impl(nir_function_impl *impl,
205 nir_lower_to_source_mods_flags options)
206 {
207 bool progress = false;
208
209 nir_foreach_block(block, impl) {
210 progress |= nir_lower_to_source_mods_block(block, options);
211 }
212
213 if (progress)
214 nir_metadata_preserve(impl, nir_metadata_block_index |
215 nir_metadata_dominance);
216
217 return progress;
218 }
219
220 bool
221 nir_lower_to_source_mods(nir_shader *shader,
222 nir_lower_to_source_mods_flags options)
223 {
224 bool progress = false;
225
226 nir_foreach_function(function, shader) {
227 if (function->impl) {
228 progress |= nir_lower_to_source_mods_impl(function->impl, options);
229 }
230 }
231
232 return progress;
233 }