2 * Copyright © 2014 Broadcom
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:
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
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
25 * @file vc4_opt_algebraic.c
27 * This is the optimization pass for miscellaneous changes to instructions
28 * where we can simplify the operation by some knowledge about the specific
31 * Mostly this will be a matter of turning things into MOVs so that they can
32 * later be copy-propagated out.
36 #include "util/u_math.h"
41 dump_from(struct vc4_compile
*c
, struct qinst
*inst
)
46 fprintf(stderr
, "optimizing: ");
47 qir_dump_inst(c
, inst
);
48 fprintf(stderr
, "\n");
52 dump_to(struct vc4_compile
*c
, struct qinst
*inst
)
57 fprintf(stderr
, "to: ");
58 qir_dump_inst(c
, inst
);
59 fprintf(stderr
, "\n");
63 is_constant_value(struct vc4_compile
*c
, struct qreg reg
,
66 if (reg
.file
== QFILE_UNIF
&&
68 c
->uniform_contents
[reg
.index
] == QUNIFORM_CONSTANT
&&
69 c
->uniform_data
[reg
.index
] == val
) {
73 if (reg
.file
== QFILE_SMALL_IMM
&& reg
.index
== val
)
80 is_zero(struct vc4_compile
*c
, struct qreg reg
)
82 reg
= qir_follow_movs(c
, reg
);
83 return is_constant_value(c
, reg
, 0);
87 is_1f(struct vc4_compile
*c
, struct qreg reg
)
89 reg
= qir_follow_movs(c
, reg
);
90 return is_constant_value(c
, reg
, fui(1.0));
94 replace_with_mov(struct vc4_compile
*c
, struct qinst
*inst
, struct qreg arg
)
99 inst
->src
[1] = c
->undef
;
104 replace_x_0_with_x(struct vc4_compile
*c
,
108 if (!is_zero(c
, inst
->src
[arg
]))
110 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
115 replace_x_0_with_0(struct vc4_compile
*c
,
119 if (!is_zero(c
, inst
->src
[arg
]))
121 replace_with_mov(c
, inst
, inst
->src
[arg
]);
126 fmul_replace_one(struct vc4_compile
*c
,
130 if (!is_1f(c
, inst
->src
[arg
]))
132 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
137 qir_opt_algebraic(struct vc4_compile
*c
)
139 bool progress
= false;
141 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
149 if (is_zero(c
, inst
->src
[1])) {
150 /* Replace references to a 0 uniform value
151 * with the SEL_X_0 equivalent.
154 inst
->op
-= (QOP_SEL_X_Y_ZS
- QOP_SEL_X_0_ZS
);
155 inst
->src
[1] = c
->undef
;
161 if (is_zero(c
, inst
->src
[0])) {
162 /* Replace references to a 0 uniform value
163 * with the SEL_X_0 equivalent, flipping the
164 * condition being evaluated since the operand
168 inst
->op
-= QOP_SEL_X_Y_ZS
;
170 inst
->op
+= QOP_SEL_X_0_ZS
;
171 inst
->src
[0] = inst
->src
[1];
172 inst
->src
[1] = c
->undef
;
182 if (is_zero(c
, inst
->src
[1])) {
183 replace_with_mov(c
, inst
, inst
->src
[0]);
189 if (replace_x_0_with_x(c
, inst
, 0) ||
190 replace_x_0_with_x(c
, inst
, 1)) {
197 if (replace_x_0_with_x(c
, inst
, 0) ||
198 replace_x_0_with_x(c
, inst
, 1)) {
203 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
204 if (inst
->src
[1].file
== QFILE_TEMP
&&
205 c
->defs
[inst
->src
[1].index
] &&
206 c
->defs
[inst
->src
[1].index
]->op
== QOP_FSUB
) {
207 struct qinst
*fsub
= c
->defs
[inst
->src
[1].index
];
208 if (is_zero(c
, fsub
->src
[0])) {
211 inst
->src
[1] = fsub
->src
[1];
218 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
219 if (inst
->src
[0].file
== QFILE_TEMP
&&
220 c
->defs
[inst
->src
[0].index
] &&
221 c
->defs
[inst
->src
[0].index
]->op
== QOP_FSUB
) {
222 struct qinst
*fsub
= c
->defs
[inst
->src
[0].index
];
223 if (is_zero(c
, fsub
->src
[0])) {
226 inst
->src
[0] = inst
->src
[1];
227 inst
->src
[1] = fsub
->src
[1];
236 if (!inst
->dst
.pack
&&
237 (replace_x_0_with_0(c
, inst
, 0) ||
238 replace_x_0_with_0(c
, inst
, 1) ||
239 fmul_replace_one(c
, inst
, 0) ||
240 fmul_replace_one(c
, inst
, 1))) {
247 if (!inst
->dst
.pack
&&
248 (replace_x_0_with_0(c
, inst
, 0) ||
249 replace_x_0_with_0(c
, inst
, 1))) {
256 if (replace_x_0_with_0(c
, inst
, 0) ||
257 replace_x_0_with_0(c
, inst
, 1)) {
262 if (is_constant_value(c
, inst
->src
[0], ~0)) {
263 replace_with_mov(c
, inst
, inst
->src
[1]);
267 if (is_constant_value(c
, inst
->src
[1], ~0)) {
268 replace_with_mov(c
, inst
, inst
->src
[0]);
275 if (replace_x_0_with_x(c
, inst
, 0) ||
276 replace_x_0_with_x(c
, inst
, 1)) {
283 if (is_1f(c
, inst
->src
[0])) {
284 replace_with_mov(c
, inst
, inst
->src
[0]);