aea2b9dbe876aed6854177146b039bd5132d3772
[mesa.git] / src / gallium / drivers / vc4 / vc4_opt_algebraic.c
1 /*
2 * Copyright © 2014 Broadcom
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
24 /**
25 * @file vc4_opt_algebraic.c
26 *
27 * This is the optimization pass for miscellaneous changes to instructions
28 * where we can simplify the operation by some knowledge about the specific
29 * operations.
30 *
31 * Mostly this will be a matter of turning things into MOVs so that they can
32 * later be copy-propagated out.
33 */
34
35 #include "vc4_qir.h"
36 #include "util/u_math.h"
37
38 static bool debug;
39
40 static void
41 dump_from(struct vc4_compile *c, struct qinst *inst)
42 {
43 if (!debug)
44 return;
45
46 fprintf(stderr, "optimizing: ");
47 qir_dump_inst(c, inst);
48 fprintf(stderr, "\n");
49 }
50
51 static void
52 dump_to(struct vc4_compile *c, struct qinst *inst)
53 {
54 if (!debug)
55 return;
56
57 fprintf(stderr, "to: ");
58 qir_dump_inst(c, inst);
59 fprintf(stderr, "\n");
60 }
61
62 static bool
63 is_constant_value(struct vc4_compile *c, struct qreg reg,
64 uint32_t val)
65 {
66 if (reg.file == QFILE_UNIF &&
67 !reg.pack &&
68 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT &&
69 c->uniform_data[reg.index] == val) {
70 return true;
71 }
72
73 if (reg.file == QFILE_SMALL_IMM && reg.index == val)
74 return true;
75
76 return false;
77 }
78
79 static bool
80 is_zero(struct vc4_compile *c, struct qreg reg)
81 {
82 reg = qir_follow_movs(c, reg);
83 return is_constant_value(c, reg, 0);
84 }
85
86 static bool
87 is_1f(struct vc4_compile *c, struct qreg reg)
88 {
89 reg = qir_follow_movs(c, reg);
90 return is_constant_value(c, reg, fui(1.0));
91 }
92
93 static void
94 replace_with_mov(struct vc4_compile *c, struct qinst *inst, struct qreg arg)
95 {
96 dump_from(c, inst);
97 if (qir_is_mul(inst))
98 inst->op = QOP_MMOV;
99 else if (qir_is_float_input(inst))
100 inst->op = QOP_FMOV;
101 else
102 inst->op = QOP_MOV;
103 inst->src[0] = arg;
104 inst->src[1] = c->undef;
105 dump_to(c, inst);
106 }
107
108 static bool
109 replace_x_0_with_x(struct vc4_compile *c,
110 struct qinst *inst,
111 int arg)
112 {
113 if (!is_zero(c, inst->src[arg]))
114 return false;
115 replace_with_mov(c, inst, inst->src[1 - arg]);
116 return true;
117 }
118
119 static bool
120 replace_x_0_with_0(struct vc4_compile *c,
121 struct qinst *inst,
122 int arg)
123 {
124 if (!is_zero(c, inst->src[arg]))
125 return false;
126 replace_with_mov(c, inst, inst->src[arg]);
127 return true;
128 }
129
130 static bool
131 fmul_replace_one(struct vc4_compile *c,
132 struct qinst *inst,
133 int arg)
134 {
135 if (!is_1f(c, inst->src[arg]))
136 return false;
137 replace_with_mov(c, inst, inst->src[1 - arg]);
138 return true;
139 }
140
141 bool
142 qir_opt_algebraic(struct vc4_compile *c)
143 {
144 bool progress = false;
145
146 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
147 switch (inst->op) {
148 case QOP_SEL_X_Y_ZS:
149 case QOP_SEL_X_Y_ZC:
150 case QOP_SEL_X_Y_NS:
151 case QOP_SEL_X_Y_NC:
152 case QOP_SEL_X_Y_CS:
153 case QOP_SEL_X_Y_CC:
154 if (is_zero(c, inst->src[1])) {
155 /* Replace references to a 0 uniform value
156 * with the SEL_X_0 equivalent.
157 */
158 dump_from(c, inst);
159 inst->op -= (QOP_SEL_X_Y_ZS - QOP_SEL_X_0_ZS);
160 inst->src[1] = c->undef;
161 progress = true;
162 dump_to(c, inst);
163 break;
164 }
165
166 if (is_zero(c, inst->src[0])) {
167 /* Replace references to a 0 uniform value
168 * with the SEL_X_0 equivalent, flipping the
169 * condition being evaluated since the operand
170 * order is flipped.
171 */
172 dump_from(c, inst);
173 inst->op -= QOP_SEL_X_Y_ZS;
174 inst->op ^= 1;
175 inst->op += QOP_SEL_X_0_ZS;
176 inst->src[0] = inst->src[1];
177 inst->src[1] = c->undef;
178 progress = true;
179 dump_to(c, inst);
180 break;
181 }
182
183 break;
184
185 case QOP_FMIN:
186 if (is_1f(c, inst->src[1]) &&
187 inst->src[0].pack >= QPU_UNPACK_8D_REP &&
188 inst->src[0].pack <= QPU_UNPACK_8D) {
189 replace_with_mov(c, inst, inst->src[0]);
190 progress = true;
191 }
192 break;
193
194 case QOP_FMAX:
195 if (is_zero(c, inst->src[1]) &&
196 inst->src[0].pack >= QPU_UNPACK_8D_REP &&
197 inst->src[0].pack <= QPU_UNPACK_8D) {
198 replace_with_mov(c, inst, inst->src[0]);
199 progress = true;
200 }
201 break;
202
203 case QOP_FSUB:
204 case QOP_SUB:
205 if (is_zero(c, inst->src[1])) {
206 replace_with_mov(c, inst, inst->src[0]);
207 progress = true;
208 }
209 break;
210
211 case QOP_ADD:
212 if (replace_x_0_with_x(c, inst, 0) ||
213 replace_x_0_with_x(c, inst, 1)) {
214 progress = true;
215 break;
216 }
217 break;
218
219 case QOP_FADD:
220 if (replace_x_0_with_x(c, inst, 0) ||
221 replace_x_0_with_x(c, inst, 1)) {
222 progress = true;
223 break;
224 }
225
226 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
227 if (inst->src[1].file == QFILE_TEMP &&
228 c->defs[inst->src[1].index] &&
229 c->defs[inst->src[1].index]->op == QOP_FSUB) {
230 struct qinst *fsub = c->defs[inst->src[1].index];
231 if (is_zero(c, fsub->src[0])) {
232 dump_from(c, inst);
233 inst->op = QOP_FSUB;
234 inst->src[1] = fsub->src[1];
235 progress = true;
236 dump_to(c, inst);
237 break;
238 }
239 }
240
241 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
242 if (inst->src[0].file == QFILE_TEMP &&
243 c->defs[inst->src[0].index] &&
244 c->defs[inst->src[0].index]->op == QOP_FSUB) {
245 struct qinst *fsub = c->defs[inst->src[0].index];
246 if (is_zero(c, fsub->src[0])) {
247 dump_from(c, inst);
248 inst->op = QOP_FSUB;
249 inst->src[0] = inst->src[1];
250 inst->src[1] = fsub->src[1];
251 dump_to(c, inst);
252 progress = true;
253 break;
254 }
255 }
256 break;
257
258 case QOP_FMUL:
259 if (!inst->dst.pack &&
260 (replace_x_0_with_0(c, inst, 0) ||
261 replace_x_0_with_0(c, inst, 1) ||
262 fmul_replace_one(c, inst, 0) ||
263 fmul_replace_one(c, inst, 1))) {
264 progress = true;
265 break;
266 }
267 break;
268
269 case QOP_MUL24:
270 if (!inst->dst.pack &&
271 (replace_x_0_with_0(c, inst, 0) ||
272 replace_x_0_with_0(c, inst, 1))) {
273 progress = true;
274 break;
275 }
276 break;
277
278 case QOP_AND:
279 if (replace_x_0_with_0(c, inst, 0) ||
280 replace_x_0_with_0(c, inst, 1)) {
281 progress = true;
282 break;
283 }
284
285 if (is_constant_value(c, inst->src[0], ~0)) {
286 replace_with_mov(c, inst, inst->src[1]);
287 progress = true;
288 break;
289 }
290 if (is_constant_value(c, inst->src[1], ~0)) {
291 replace_with_mov(c, inst, inst->src[0]);
292 progress = true;
293 break;
294 }
295 break;
296
297 case QOP_OR:
298 if (replace_x_0_with_x(c, inst, 0) ||
299 replace_x_0_with_x(c, inst, 1)) {
300 progress = true;
301 break;
302 }
303 break;
304
305 case QOP_RCP:
306 if (is_1f(c, inst->src[0])) {
307 replace_with_mov(c, inst, inst->src[0]);
308 progress = true;
309 break;
310 }
311 break;
312
313 default:
314 break;
315 }
316 }
317
318 return progress;
319 }