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 v3d_opt_copy_propagation.c
26 *
27 * This implements simple copy propagation for VIR without control flow.
28 *
29 * For each temp, it keeps a qreg of which source it was MOVed from, if it
30 * was. If we see that used later, we can just reuse the source value, since
31 * we know we don't have control flow, and we have SSA for our values so
32 * there's no killing to worry about.
33 */
34
35 #include "v3d_compiler.h"
36
37 static bool
is_copy_mov(const struct v3d_device_info * devinfo,struct qinst * inst)38 is_copy_mov(const struct v3d_device_info *devinfo, struct qinst *inst)
39 {
40 if (!inst)
41 return false;
42
43 if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||
44 (inst->qpu.alu.mul.op != V3D_QPU_M_FMOV &&
45 inst->qpu.alu.mul.op != V3D_QPU_M_MOV)) {
46 return false;
47 }
48
49 if (inst->dst.file != QFILE_TEMP)
50 return false;
51
52 if (inst->src[0].file != QFILE_TEMP)
53 return false;
54
55 if (inst->qpu.alu.add.output_pack != V3D_QPU_PACK_NONE ||
56 inst->qpu.alu.mul.output_pack != V3D_QPU_PACK_NONE) {
57 return false;
58 }
59
60 if (inst->qpu.flags.ac != V3D_QPU_COND_NONE ||
61 inst->qpu.flags.mc != V3D_QPU_COND_NONE) {
62 return false;
63 }
64
65 if (devinfo->ver == 42) {
66 switch (inst->src[0].file) {
67 case QFILE_MAGIC:
68 /* No copy propagating from R3/R4/R5 -- the MOVs from
69 * those are there to register allocate values produced
70 * into R3/4/5 to other regs (though hopefully r3/4/5).
71 */
72 switch (inst->src[0].index) {
73 case V3D_QPU_WADDR_R3:
74 case V3D_QPU_WADDR_R4:
75 case V3D_QPU_WADDR_R5:
76 return false;
77 default:
78 break;
79 }
80 break;
81
82 case QFILE_REG:
83 switch (inst->src[0].index) {
84 case 0:
85 case 1:
86 case 2:
87 /* MOVs from rf0/1/2 are only to track the live
88 * intervals for W/centroid W/Z.
89 */
90 return false;
91 }
92 break;
93
94 default:
95 break;
96 }
97 } else {
98 assert(devinfo->ver >= 71);
99 switch (inst->src[0].file) {
100 case QFILE_REG:
101 switch (inst->src[0].index) {
102 /* MOVs from rf1/2/3 are only to track the live
103 * intervals for W/centroid W/Z.
104 *
105 * Note: rf0 can be implicitly written by ldvary
106 * (no temp involved), so it is not an SSA value and
107 * could clash with writes to other temps that are
108 * also allocated to rf0. In theory, that would mean
109 * that we can't copy propagate from it, but we handle
110 * this at register allocation time, preventing temps
111 * from being allocated to rf0 while the rf0 value from
112 * ldvary is still live.
113 */
114 case 1:
115 case 2:
116 case 3:
117 return false;
118 }
119 break;
120
121 default:
122 break;
123 }
124 }
125
126 return true;
127 }
128
129 static bool
vir_has_unpack(struct qinst * inst,int chan)130 vir_has_unpack(struct qinst *inst, int chan)
131 {
132 assert(chan == 0 || chan == 1);
133
134 if (vir_is_add(inst)) {
135 if (chan == 0)
136 return inst->qpu.alu.add.a.unpack != V3D_QPU_UNPACK_NONE;
137 else
138 return inst->qpu.alu.add.b.unpack != V3D_QPU_UNPACK_NONE;
139 } else {
140 if (chan == 0)
141 return inst->qpu.alu.mul.a.unpack != V3D_QPU_UNPACK_NONE;
142 else
143 return inst->qpu.alu.mul.b.unpack != V3D_QPU_UNPACK_NONE;
144 }
145 }
146
147 static bool
try_copy_prop(struct v3d_compile * c,struct qinst * inst,struct qinst ** movs)148 try_copy_prop(struct v3d_compile *c, struct qinst *inst, struct qinst **movs)
149 {
150 bool debug = false;
151 bool progress = false;
152
153 for (int i = 0; i < vir_get_nsrc(inst); i++) {
154 if (inst->src[i].file != QFILE_TEMP)
155 continue;
156
157 /* We have two ways of finding MOVs we can copy propagate
158 * from. One is if it's an SSA def: then we can reuse it from
159 * any block in the program, as long as its source is also an
160 * SSA def. Alternatively, if it's in the "movs" array
161 * tracked within the block, then we know the sources for it
162 * haven't been changed since we saw the instruction within
163 * our block.
164 */
165 struct qinst *mov = movs[inst->src[i].index];
166 if (!mov) {
167 if (!is_copy_mov(c->devinfo, c->defs[inst->src[i].index]))
168 continue;
169 mov = c->defs[inst->src[i].index];
170
171 if (mov->src[0].file == QFILE_TEMP &&
172 !c->defs[mov->src[0].index])
173 continue;
174 }
175
176 if (vir_has_unpack(mov, 0)) {
177 /* Make sure that the meaning of the unpack
178 * would be the same between the two
179 * instructions.
180 */
181 if (v3d_qpu_unpacks_f32(&inst->qpu) !=
182 v3d_qpu_unpacks_f32(&mov->qpu) ||
183 v3d_qpu_unpacks_f16(&inst->qpu) !=
184 v3d_qpu_unpacks_f16(&mov->qpu)) {
185 continue;
186 }
187
188 /* No composing the unpacks. */
189 if (vir_has_unpack(inst, i))
190 continue;
191
192 /* these ops can't represent abs. */
193 if (mov->qpu.alu.mul.a.unpack == V3D_QPU_UNPACK_ABS) {
194 switch (inst->qpu.alu.add.op) {
195 case V3D_QPU_A_VFPACK:
196 case V3D_QPU_A_FROUND:
197 case V3D_QPU_A_FTRUNC:
198 case V3D_QPU_A_FFLOOR:
199 case V3D_QPU_A_FCEIL:
200 case V3D_QPU_A_FDX:
201 case V3D_QPU_A_FDY:
202 case V3D_QPU_A_FTOIN:
203 case V3D_QPU_A_FTOIZ:
204 case V3D_QPU_A_FTOUZ:
205 case V3D_QPU_A_FTOC:
206 continue;
207 default:
208 break;
209 }
210 }
211
212 /* These are only available with FMOV */
213 if (mov->qpu.alu.mul.a.unpack >= V3D71_QPU_UNPACK_SAT &&
214 mov->qpu.alu.mul.a.unpack <= V3D71_QPU_UNPACK_MAX0 &&
215 inst->qpu.alu.mul.op != V3D_QPU_M_FMOV) {
216 assert(c->devinfo->ver >= 71);
217 return false;
218 }
219 }
220
221 if (debug) {
222 fprintf(stderr, "Copy propagate: ");
223 vir_dump_inst(c, inst);
224 fprintf(stderr, "\n");
225 }
226
227 inst->src[i] = mov->src[0];
228 if (vir_has_unpack(mov, 0)) {
229 enum v3d_qpu_input_unpack unpack = mov->qpu.alu.mul.a.unpack;
230
231 vir_set_unpack(inst, i, unpack);
232 }
233
234 if (debug) {
235 fprintf(stderr, "to: ");
236 vir_dump_inst(c, inst);
237 fprintf(stderr, "\n");
238 }
239
240 progress = true;
241 }
242
243 return progress;
244 }
245
246 static void
apply_kills(struct v3d_compile * c,struct qinst ** movs,struct qinst * inst)247 apply_kills(struct v3d_compile *c, struct qinst **movs, struct qinst *inst)
248 {
249 if (inst->dst.file != QFILE_TEMP)
250 return;
251
252 for (int i = 0; i < c->num_temps; i++) {
253 if (movs[i] &&
254 (movs[i]->dst.index == inst->dst.index ||
255 (movs[i]->src[0].file == QFILE_TEMP &&
256 movs[i]->src[0].index == inst->dst.index))) {
257 movs[i] = NULL;
258 }
259 }
260 }
261
262 bool
vir_opt_copy_propagate(struct v3d_compile * c)263 vir_opt_copy_propagate(struct v3d_compile *c)
264 {
265 bool progress = false;
266 struct qinst **movs;
267
268 movs = ralloc_array(c, struct qinst *, c->num_temps);
269 if (!movs)
270 return false;
271
272 vir_for_each_block(block, c) {
273 /* The MOVs array tracks only available movs within the
274 * block.
275 */
276 memset(movs, 0, sizeof(struct qinst *) * c->num_temps);
277
278 c->cur_block = block;
279 vir_for_each_inst(inst, block) {
280
281 progress = try_copy_prop(c, inst, movs) || progress;
282
283 apply_kills(c, movs, inst);
284
285 if (is_copy_mov(c->devinfo, inst))
286 movs[inst->dst.index] = inst;
287 }
288 }
289
290 ralloc_free(movs);
291
292 return progress;
293 }
294