ec526fb3d18f4e8dbe3fd3428e6891f508fd96a2
[mesa.git] / 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 struct qreg
63 follow_movs(struct qinst **defs, struct qreg reg)
64 {
65 while (reg.file == QFILE_TEMP && defs[reg.index]->op == QOP_MOV)
66 reg = defs[reg.index]->src[0];
67
68 return reg;
69 }
70
71 static bool
72 is_zero(struct vc4_compile *c, struct qinst **defs, struct qreg reg)
73 {
74 reg = follow_movs(defs, reg);
75
76 return (reg.file == QFILE_UNIF &&
77 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT &&
78 c->uniform_data[reg.index] == 0);
79 }
80
81 static bool
82 is_1f(struct vc4_compile *c, struct qinst **defs, struct qreg reg)
83 {
84 reg = follow_movs(defs, reg);
85
86 return (reg.file == QFILE_UNIF &&
87 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT &&
88 c->uniform_data[reg.index] == fui(1.0));
89 }
90
91 static void
92 replace_with_mov(struct vc4_compile *c, struct qinst *inst, struct qreg arg)
93 {
94 dump_from(c, inst);
95 inst->op = QOP_MOV;
96 inst->src[0] = arg;
97 inst->src[1] = c->undef;
98 dump_to(c, inst);
99 }
100
101 static bool
102 add_replace_zero(struct vc4_compile *c,
103 struct qinst **defs,
104 struct qinst *inst,
105 int arg)
106 {
107 if (!is_zero(c, defs, inst->src[arg]))
108 return false;
109 replace_with_mov(c, inst, inst->src[1 - arg]);
110 return true;
111 }
112
113 static bool
114 fmul_replace_zero(struct vc4_compile *c,
115 struct qinst **defs,
116 struct qinst *inst,
117 int arg)
118 {
119 if (!is_zero(c, defs, inst->src[arg]))
120 return false;
121 replace_with_mov(c, inst, inst->src[arg]);
122 return true;
123 }
124
125 static bool
126 fmul_replace_one(struct vc4_compile *c,
127 struct qinst **defs,
128 struct qinst *inst,
129 int arg)
130 {
131 if (!is_1f(c, defs, inst->src[arg]))
132 return false;
133 replace_with_mov(c, inst, inst->src[1 - arg]);
134 return true;
135 }
136
137 bool
138 qir_opt_algebraic(struct vc4_compile *c)
139 {
140 bool progress = false;
141 struct simple_node *node;
142 struct qinst *defs[c->num_temps];
143
144 foreach(node, &c->instructions) {
145 struct qinst *inst = (struct qinst *)node;
146
147 if (inst->dst.file == QFILE_TEMP)
148 defs[inst->dst.index] = inst;
149
150 switch (inst->op) {
151 case QOP_SF:
152 /* SF just looks at the sign bit, or whether all the
153 * bits are 0. This is preserved across an itof
154 * transformation.
155 */
156 if (inst->src[0].file == QFILE_TEMP &&
157 defs[inst->src[0].index]->op == QOP_ITOF) {
158 dump_from(c, inst);
159 inst->src[0] =
160 defs[inst->src[0].index]->src[0];
161 progress = true;
162 dump_to(c, inst);
163 break;
164 }
165 break;
166
167 case QOP_SEL_X_Y_ZS:
168 case QOP_SEL_X_Y_ZC:
169 case QOP_SEL_X_Y_NS:
170 case QOP_SEL_X_Y_NC:
171 if (qir_reg_equals(inst->src[0], inst->src[1])) {
172 /* Turn "dst = (sf == x) ? a : a)" into
173 * "dst = a"
174 */
175 replace_with_mov(c, inst, inst->src[1]);
176 progress = true;
177 break;
178 }
179
180 if (is_zero(c, defs, inst->src[1])) {
181 /* Replace references to a 0 uniform value
182 * with the SEL_X_0 equivalent.
183 */
184 dump_from(c, inst);
185 inst->op -= (QOP_SEL_X_Y_ZS - QOP_SEL_X_0_ZS);
186 inst->src[1] = c->undef;
187 progress = true;
188 dump_to(c, inst);
189 break;
190 }
191
192 if (is_zero(c, defs, inst->src[0])) {
193 /* Replace references to a 0 uniform value
194 * with the SEL_X_0 equivalent, flipping the
195 * condition being evaluated since the operand
196 * order is flipped.
197 */
198 dump_from(c, inst);
199 inst->op -= QOP_SEL_X_Y_ZS;
200 inst->op ^= 1;
201 inst->op += QOP_SEL_X_0_ZS;
202 inst->src[0] = inst->src[1];
203 inst->src[1] = c->undef;
204 progress = true;
205 dump_to(c, inst);
206 break;
207 }
208
209 break;
210
211 case QOP_FSUB:
212 case QOP_SUB:
213 if (is_zero(c, defs, inst->src[1])) {
214 replace_with_mov(c, inst, inst->src[0]);
215 }
216 break;
217
218 case QOP_ADD:
219 if (add_replace_zero(c, defs, inst, 0) ||
220 add_replace_zero(c, defs, inst, 1)) {
221 progress = true;
222 break;
223 }
224 break;
225
226 case QOP_FADD:
227 if (add_replace_zero(c, defs, inst, 0) ||
228 add_replace_zero(c, defs, inst, 1)) {
229 progress = true;
230 break;
231 }
232
233 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
234 if (inst->src[1].file == QFILE_TEMP &&
235 defs[inst->src[1].index]->op == QOP_FSUB) {
236 struct qinst *fsub = defs[inst->src[1].index];
237 if (is_zero(c, defs, fsub->src[0])) {
238 dump_from(c, inst);
239 inst->op = QOP_FSUB;
240 inst->src[1] = fsub->src[1];
241 progress = true;
242 dump_to(c, inst);
243 break;
244 }
245 }
246
247 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
248 if (inst->src[0].file == QFILE_TEMP &&
249 defs[inst->src[0].index]->op == QOP_FSUB) {
250 struct qinst *fsub = defs[inst->src[0].index];
251 if (is_zero(c, defs, fsub->src[0])) {
252 dump_from(c, inst);
253 inst->op = QOP_FSUB;
254 inst->src[0] = inst->src[1];
255 inst->src[1] = fsub->src[1];
256 dump_to(c, inst);
257 progress = true;
258 break;
259 }
260 }
261 break;
262
263 case QOP_FMUL:
264 if (fmul_replace_zero(c, defs, inst, 0) ||
265 fmul_replace_zero(c, defs, inst, 1) ||
266 fmul_replace_one(c, defs, inst, 0) ||
267 fmul_replace_one(c, defs, inst, 1)) {
268 progress = true;
269 break;
270 }
271 break;
272
273 default:
274 break;
275 }
276 }
277
278 return progress;
279 }