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 qinst
**defs
, 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 qinst
**defs
, struct qreg reg
)
81 reg
= qir_follow_movs(defs
, reg
);
82 return is_constant_value(c
, defs
, reg
, 0);
86 is_1f(struct vc4_compile
*c
, struct qinst
**defs
, struct qreg reg
)
88 reg
= qir_follow_movs(defs
, reg
);
89 return is_constant_value(c
, defs
, 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 add_replace_zero(struct vc4_compile
*c
,
108 if (!is_zero(c
, defs
, inst
->src
[arg
]))
110 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
115 fmul_replace_zero(struct vc4_compile
*c
,
120 if (!is_zero(c
, defs
, inst
->src
[arg
]))
122 replace_with_mov(c
, inst
, inst
->src
[arg
]);
127 fmul_replace_one(struct vc4_compile
*c
,
132 if (!is_1f(c
, defs
, inst
->src
[arg
]))
134 replace_with_mov(c
, inst
, inst
->src
[1 - arg
]);
139 qir_opt_algebraic(struct vc4_compile
*c
)
141 bool progress
= false;
142 struct simple_node
*node
;
143 struct qinst
*defs
[c
->num_temps
];
145 foreach(node
, &c
->instructions
) {
146 struct qinst
*inst
= (struct qinst
*)node
;
148 if (inst
->dst
.file
== QFILE_TEMP
)
149 defs
[inst
->dst
.index
] = inst
;
153 /* SF just looks at the sign bit, or whether all the
154 * bits are 0. This is preserved across an itof
157 if (inst
->src
[0].file
== QFILE_TEMP
&&
158 defs
[inst
->src
[0].index
]->op
== QOP_ITOF
) {
161 defs
[inst
->src
[0].index
]->src
[0];
172 if (qir_reg_equals(inst
->src
[0], inst
->src
[1])) {
173 /* Turn "dst = (sf == x) ? a : a)" into
176 replace_with_mov(c
, inst
, inst
->src
[1]);
181 if (is_zero(c
, defs
, inst
->src
[1])) {
182 /* Replace references to a 0 uniform value
183 * with the SEL_X_0 equivalent.
186 inst
->op
-= (QOP_SEL_X_Y_ZS
- QOP_SEL_X_0_ZS
);
187 inst
->src
[1] = c
->undef
;
193 if (is_zero(c
, defs
, inst
->src
[0])) {
194 /* Replace references to a 0 uniform value
195 * with the SEL_X_0 equivalent, flipping the
196 * condition being evaluated since the operand
200 inst
->op
-= QOP_SEL_X_Y_ZS
;
202 inst
->op
+= QOP_SEL_X_0_ZS
;
203 inst
->src
[0] = inst
->src
[1];
204 inst
->src
[1] = c
->undef
;
214 if (is_zero(c
, defs
, inst
->src
[1])) {
215 replace_with_mov(c
, inst
, inst
->src
[0]);
220 if (add_replace_zero(c
, defs
, inst
, 0) ||
221 add_replace_zero(c
, defs
, inst
, 1)) {
228 if (add_replace_zero(c
, defs
, inst
, 0) ||
229 add_replace_zero(c
, defs
, inst
, 1)) {
234 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
235 if (inst
->src
[1].file
== QFILE_TEMP
&&
236 defs
[inst
->src
[1].index
]->op
== QOP_FSUB
) {
237 struct qinst
*fsub
= defs
[inst
->src
[1].index
];
238 if (is_zero(c
, defs
, fsub
->src
[0])) {
241 inst
->src
[1] = fsub
->src
[1];
248 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
249 if (inst
->src
[0].file
== QFILE_TEMP
&&
250 defs
[inst
->src
[0].index
]->op
== QOP_FSUB
) {
251 struct qinst
*fsub
= defs
[inst
->src
[0].index
];
252 if (is_zero(c
, defs
, fsub
->src
[0])) {
255 inst
->src
[0] = inst
->src
[1];
256 inst
->src
[1] = fsub
->src
[1];
265 if (fmul_replace_zero(c
, defs
, inst
, 0) ||
266 fmul_replace_zero(c
, defs
, inst
, 1) ||
267 fmul_replace_one(c
, defs
, inst
, 0) ||
268 fmul_replace_one(c
, defs
, inst
, 1)) {