vc4: Factor out the turn-it-into-a-mov in opt_algebraic.
[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
37 static bool debug;
38
39 static void
40 dump_from(struct vc4_compile *c, struct qinst *inst)
41 {
42 if (!debug)
43 return;
44
45 fprintf(stderr, "optimizing: ");
46 qir_dump_inst(c, inst);
47 fprintf(stderr, "\n");
48 }
49
50 static void
51 dump_to(struct vc4_compile *c, struct qinst *inst)
52 {
53 if (!debug)
54 return;
55
56 fprintf(stderr, "to: ");
57 qir_dump_inst(c, inst);
58 fprintf(stderr, "\n");
59 }
60
61 static struct qreg
62 follow_movs(struct qinst **defs, struct qreg reg)
63 {
64 while (reg.file == QFILE_TEMP && defs[reg.index]->op == QOP_MOV)
65 reg = defs[reg.index]->src[0];
66
67 return reg;
68 }
69
70 static bool
71 is_zero(struct vc4_compile *c, struct qinst **defs, struct qreg reg)
72 {
73 reg = follow_movs(defs, reg);
74
75 return (reg.file == QFILE_UNIF &&
76 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT &&
77 c->uniform_data[reg.index] == 0);
78 }
79
80 static void
81 replace_with_mov(struct vc4_compile *c, struct qinst *inst, struct qreg arg)
82 {
83 dump_from(c, inst);
84 inst->op = QOP_MOV;
85 inst->src[0] = arg;
86 inst->src[1] = c->undef;
87 dump_to(c, inst);
88 }
89
90 bool
91 qir_opt_algebraic(struct vc4_compile *c)
92 {
93 bool progress = false;
94 struct simple_node *node;
95 struct qinst *defs[c->num_temps];
96
97 foreach(node, &c->instructions) {
98 struct qinst *inst = (struct qinst *)node;
99
100 if (inst->dst.file == QFILE_TEMP)
101 defs[inst->dst.index] = inst;
102
103 switch (inst->op) {
104 case QOP_SF:
105 /* SF just looks at the sign bit, or whether all the
106 * bits are 0. This is preserved across an itof
107 * transformation.
108 */
109 if (inst->src[0].file == QFILE_TEMP &&
110 defs[inst->src[0].index]->op == QOP_ITOF) {
111 dump_from(c, inst);
112 inst->src[0] =
113 defs[inst->src[0].index]->src[0];
114 progress = true;
115 dump_to(c, inst);
116 break;
117 }
118 break;
119
120 case QOP_SEL_X_Y_ZS:
121 case QOP_SEL_X_Y_ZC:
122 case QOP_SEL_X_Y_NS:
123 case QOP_SEL_X_Y_NC:
124 if (qir_reg_equals(inst->src[0], inst->src[1])) {
125 /* Turn "dst = (sf == x) ? a : a)" into
126 * "dst = a"
127 */
128 replace_with_mov(c, inst, inst->src[1]);
129 progress = true;
130 } else if (is_zero(c, defs, inst->src[1])) {
131 /* Replace references to a 0 uniform value
132 * with the SEL_X_0 equivalent.
133 */
134 dump_from(c, inst);
135 inst->op -= (QOP_SEL_X_Y_ZS - QOP_SEL_X_0_ZS);
136 inst->src[1] = c->undef;
137 progress = true;
138 dump_to(c, inst);
139 }
140 break;
141
142 case QOP_FSUB:
143 case QOP_SUB:
144 if (is_zero(c, defs, inst->src[1])) {
145 replace_with_mov(c, inst, inst->src[0]);
146 }
147 break;
148
149 case QOP_FADD:
150 /* FADD(a, FSUB(0, b)) -> FSUB(a, b) */
151 if (inst->src[1].file == QFILE_TEMP &&
152 defs[inst->src[1].index]->op == QOP_FSUB) {
153 struct qinst *fsub = defs[inst->src[1].index];
154 if (is_zero(c, defs, fsub->src[0])) {
155 dump_from(c, inst);
156 inst->op = QOP_FSUB;
157 inst->src[1] = fsub->src[1];
158 progress = true;
159 dump_to(c, inst);
160 break;
161 }
162 }
163
164 /* FADD(FSUB(0, b), a) -> FSUB(a, b) */
165 if (inst->src[0].file == QFILE_TEMP &&
166 defs[inst->src[0].index]->op == QOP_FSUB) {
167 struct qinst *fsub = defs[inst->src[0].index];
168 if (is_zero(c, defs, fsub->src[0])) {
169 dump_from(c, inst);
170 inst->op = QOP_FSUB;
171 inst->src[0] = inst->src[1];
172 inst->src[1] = fsub->src[1];
173 dump_to(c, inst);
174 progress = true;
175 break;
176 }
177 }
178 break;
179
180 default:
181 break;
182 }
183 }
184
185 return progress;
186 }