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 if (qir_has_implicit_tex_uniform(inst
))
100 inst
->src
[1] = inst
->src
[qir_get_tex_uniform_src(inst
)];
102 if (qir_is_mul(inst
))
104 else if (qir_is_float_input(inst
))
112 replace_x_0_with_x(struct vc4_compile
*c
,
116 if (!is_zero(c
, inst
->src
[arg
]))
118 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
123 replace_x_0_with_0(struct vc4_compile
*c
,
127 if (!is_zero(c
, inst
->src
[arg
]))
129 replace_with_mov(c
, inst
, inst
->src
[arg
]);
134 fmul_replace_one(struct vc4_compile
*c
,
138 if (!is_1f(c
, inst
->src
[arg
]))
140 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
145 qir_opt_algebraic(struct vc4_compile
*c
)
147 bool progress
= false;
149 qir_for_each_inst_inorder(inst
, c
) {
152 if (is_1f(c
, inst
->src
[1]) &&
153 inst
->src
[0].pack
>= QPU_UNPACK_8D_REP
&&
154 inst
->src
[0].pack
<= QPU_UNPACK_8D
) {
155 replace_with_mov(c
, inst
, inst
->src
[0]);
161 if (is_zero(c
, inst
->src
[1]) &&
162 inst
->src
[0].pack
>= QPU_UNPACK_8D_REP
&&
163 inst
->src
[0].pack
<= QPU_UNPACK_8D
) {
164 replace_with_mov(c
, inst
, inst
->src
[0]);
171 if (is_zero(c
, inst
->src
[1])) {
172 replace_with_mov(c
, inst
, inst
->src
[0]);
178 /* Kernel validation requires that we use an actual
181 if (inst
->dst
.file
!= QFILE_TEX_S_DIRECT
&&
182 (replace_x_0_with_x(c
, inst
, 0) ||
183 replace_x_0_with_x(c
, inst
, 1))) {
190 if (replace_x_0_with_x(c
, inst
, 0) ||
191 replace_x_0_with_x(c
, inst
, 1)) {
196 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
197 if (inst
->src
[1].file
== QFILE_TEMP
&&
198 c
->defs
[inst
->src
[1].index
] &&
199 c
->defs
[inst
->src
[1].index
]->op
== QOP_FSUB
) {
200 struct qinst
*fsub
= c
->defs
[inst
->src
[1].index
];
201 if (is_zero(c
, fsub
->src
[0])) {
204 inst
->src
[1] = fsub
->src
[1];
211 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
212 if (inst
->src
[0].file
== QFILE_TEMP
&&
213 c
->defs
[inst
->src
[0].index
] &&
214 c
->defs
[inst
->src
[0].index
]->op
== QOP_FSUB
) {
215 struct qinst
*fsub
= c
->defs
[inst
->src
[0].index
];
216 if (is_zero(c
, fsub
->src
[0])) {
219 inst
->src
[0] = inst
->src
[1];
220 inst
->src
[1] = fsub
->src
[1];
229 if (!inst
->dst
.pack
&&
230 (replace_x_0_with_0(c
, inst
, 0) ||
231 replace_x_0_with_0(c
, inst
, 1) ||
232 fmul_replace_one(c
, inst
, 0) ||
233 fmul_replace_one(c
, inst
, 1))) {
240 if (!inst
->dst
.pack
&&
241 (replace_x_0_with_0(c
, inst
, 0) ||
242 replace_x_0_with_0(c
, inst
, 1))) {
249 if (replace_x_0_with_0(c
, inst
, 0) ||
250 replace_x_0_with_0(c
, inst
, 1)) {
255 if (is_constant_value(c
, inst
->src
[0], ~0)) {
256 replace_with_mov(c
, inst
, inst
->src
[1]);
260 if (is_constant_value(c
, inst
->src
[1], ~0)) {
261 replace_with_mov(c
, inst
, inst
->src
[0]);
268 if (replace_x_0_with_x(c
, inst
, 0) ||
269 replace_x_0_with_x(c
, inst
, 1)) {
276 if (is_1f(c
, inst
->src
[0])) {
277 replace_with_mov(c
, inst
, inst
->src
[0]);