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
&&
67 c
->uniform_contents
[reg
.index
] == QUNIFORM_CONSTANT
&&
68 c
->uniform_data
[reg
.index
] == val
) {
72 if (reg
.file
== QFILE_SMALL_IMM
&& reg
.index
== val
)
79 is_zero(struct vc4_compile
*c
, struct qreg reg
)
81 reg
= qir_follow_movs(c
, reg
);
82 return is_constant_value(c
, reg
, 0);
86 is_1f(struct vc4_compile
*c
, struct qreg reg
)
88 reg
= qir_follow_movs(c
, reg
);
89 return is_constant_value(c
, reg
, fui(1.0));
93 replace_with_mov(struct vc4_compile
*c
, struct qinst
*inst
, struct qreg arg
)
98 inst
->src
[1] = c
->undef
;
103 replace_x_0_with_x(struct vc4_compile
*c
,
107 if (!is_zero(c
, inst
->src
[arg
]))
109 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
114 replace_x_0_with_0(struct vc4_compile
*c
,
118 if (!is_zero(c
, inst
->src
[arg
]))
120 replace_with_mov(c
, inst
, inst
->src
[arg
]);
125 fmul_replace_one(struct vc4_compile
*c
,
129 if (!is_1f(c
, inst
->src
[arg
]))
131 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
136 qir_opt_algebraic(struct vc4_compile
*c
)
138 bool progress
= false;
140 list_for_each_entry(struct qinst
, inst
, &c
->instructions
, link
) {
146 if (is_zero(c
, inst
->src
[1])) {
147 /* Replace references to a 0 uniform value
148 * with the SEL_X_0 equivalent.
151 inst
->op
-= (QOP_SEL_X_Y_ZS
- QOP_SEL_X_0_ZS
);
152 inst
->src
[1] = c
->undef
;
158 if (is_zero(c
, inst
->src
[0])) {
159 /* Replace references to a 0 uniform value
160 * with the SEL_X_0 equivalent, flipping the
161 * condition being evaluated since the operand
165 inst
->op
-= QOP_SEL_X_Y_ZS
;
167 inst
->op
+= QOP_SEL_X_0_ZS
;
168 inst
->src
[0] = inst
->src
[1];
169 inst
->src
[1] = c
->undef
;
179 if (is_zero(c
, inst
->src
[1])) {
180 replace_with_mov(c
, inst
, inst
->src
[0]);
185 if (replace_x_0_with_x(c
, inst
, 0) ||
186 replace_x_0_with_x(c
, inst
, 1)) {
193 if (replace_x_0_with_x(c
, inst
, 0) ||
194 replace_x_0_with_x(c
, inst
, 1)) {
199 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
200 if (inst
->src
[1].file
== QFILE_TEMP
&&
201 c
->defs
[inst
->src
[1].index
] &&
202 c
->defs
[inst
->src
[1].index
]->op
== QOP_FSUB
) {
203 struct qinst
*fsub
= c
->defs
[inst
->src
[1].index
];
204 if (is_zero(c
, fsub
->src
[0])) {
207 inst
->src
[1] = fsub
->src
[1];
214 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
215 if (inst
->src
[0].file
== QFILE_TEMP
&&
216 c
->defs
[inst
->src
[0].index
] &&
217 c
->defs
[inst
->src
[0].index
]->op
== QOP_FSUB
) {
218 struct qinst
*fsub
= c
->defs
[inst
->src
[0].index
];
219 if (is_zero(c
, fsub
->src
[0])) {
222 inst
->src
[0] = inst
->src
[1];
223 inst
->src
[1] = fsub
->src
[1];
232 if (!inst
->dst
.pack
&&
233 (replace_x_0_with_0(c
, inst
, 0) ||
234 replace_x_0_with_0(c
, inst
, 1) ||
235 fmul_replace_one(c
, inst
, 0) ||
236 fmul_replace_one(c
, inst
, 1))) {
243 if (!inst
->dst
.pack
&&
244 (replace_x_0_with_0(c
, inst
, 0) ||
245 replace_x_0_with_0(c
, inst
, 1))) {
252 if (replace_x_0_with_0(c
, inst
, 0) ||
253 replace_x_0_with_0(c
, inst
, 1)) {
258 if (is_constant_value(c
, inst
->src
[0], ~0)) {
259 replace_with_mov(c
, inst
, inst
->src
[1]);
263 if (is_constant_value(c
, inst
->src
[1], ~0)) {
264 replace_with_mov(c
, inst
, inst
->src
[0]);
271 if (replace_x_0_with_x(c
, inst
, 0) ||
272 replace_x_0_with_x(c
, inst
, 1)) {