b8ce377ff6b73ea94e3348a5fb656a18600890f0
[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_FMIN:
149 if (is_1f(c, inst->src[1]) &&
150 inst->src[0].pack >= QPU_UNPACK_8D_REP &&
151 inst->src[0].pack <= QPU_UNPACK_8D) {
152 replace_with_mov(c, inst, inst->src[0]);
153 progress = true;
154 }
155 break;
156
157 case QOP_FMAX:
158 if (is_zero(c, inst->src[1]) &&
159 inst->src[0].pack >= QPU_UNPACK_8D_REP &&
160 inst->src[0].pack <= QPU_UNPACK_8D) {
161 replace_with_mov(c, inst, inst->src[0]);
162 progress = true;
163 }
164 break;
165
166 case QOP_FSUB:
167 case QOP_SUB:
168 if (is_zero(c, inst->src[1])) {
169 replace_with_mov(c, inst, inst->src[0]);
170 progress = true;
171 }
172 break;
173
174 case QOP_ADD:
175 if (replace_x_0_with_x(c, inst, 0) ||
176 replace_x_0_with_x(c, inst, 1)) {
177 progress = true;
178 break;
179 }
180 break;
181
182 case QOP_FADD:
183 if (replace_x_0_with_x(c, inst, 0) ||
184 replace_x_0_with_x(c, inst, 1)) {
185 progress = true;
186 break;
187 }
188
189 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
190 if (inst->src[1].file == QFILE_TEMP &&
191 c->defs[inst->src[1].index] &&
192 c->defs[inst->src[1].index]->op == QOP_FSUB) {
193 struct qinst *fsub = c->defs[inst->src[1].index];
194 if (is_zero(c, fsub->src[0])) {
195 dump_from(c, inst);
196 inst->op = QOP_FSUB;
197 inst->src[1] = fsub->src[1];
198 progress = true;
199 dump_to(c, inst);
200 break;
201 }
202 }
203
204 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
205 if (inst->src[0].file == QFILE_TEMP &&
206 c->defs[inst->src[0].index] &&
207 c->defs[inst->src[0].index]->op == QOP_FSUB) {
208 struct qinst *fsub = c->defs[inst->src[0].index];
209 if (is_zero(c, fsub->src[0])) {
210 dump_from(c, inst);
211 inst->op = QOP_FSUB;
212 inst->src[0] = inst->src[1];
213 inst->src[1] = fsub->src[1];
214 dump_to(c, inst);
215 progress = true;
216 break;
217 }
218 }
219 break;
220
221 case QOP_FMUL:
222 if (!inst->dst.pack &&
223 (replace_x_0_with_0(c, inst, 0) ||
224 replace_x_0_with_0(c, inst, 1) ||
225 fmul_replace_one(c, inst, 0) ||
226 fmul_replace_one(c, inst, 1))) {
227 progress = true;
228 break;
229 }
230 break;
231
232 case QOP_MUL24:
233 if (!inst->dst.pack &&
234 (replace_x_0_with_0(c, inst, 0) ||
235 replace_x_0_with_0(c, inst, 1))) {
236 progress = true;
237 break;
238 }
239 break;
240
241 case QOP_AND:
242 if (replace_x_0_with_0(c, inst, 0) ||
243 replace_x_0_with_0(c, inst, 1)) {
244 progress = true;
245 break;
246 }
247
248 if (is_constant_value(c, inst->src[0], ~0)) {
249 replace_with_mov(c, inst, inst->src[1]);
250 progress = true;
251 break;
252 }
253 if (is_constant_value(c, inst->src[1], ~0)) {
254 replace_with_mov(c, inst, inst->src[0]);
255 progress = true;
256 break;
257 }
258 break;
259
260 case QOP_OR:
261 if (replace_x_0_with_x(c, inst, 0) ||
262 replace_x_0_with_x(c, inst, 1)) {
263 progress = true;
264 break;
265 }
266 break;
267
268 case QOP_RCP:
269 if (is_1f(c, inst->src[0])) {
270 replace_with_mov(c, inst, inst->src[0]);
271 progress = true;
272 break;
273 }
274 break;
275
276 default:
277 break;
278 }
279 }
280
281 return progress;
282 }