v3d: Do uniform rematerialization spilling before dropping threadcount
[mesa.git] / src / broadcom / compiler / vir_opt_copy_propagate.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 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
38 is_copy_mov(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 inst->src[0].file != QFILE_UNIF) {
54 return false;
55 }
56
57 if (inst->qpu.alu.add.output_pack != V3D_QPU_PACK_NONE ||
58 inst->qpu.alu.mul.output_pack != V3D_QPU_PACK_NONE) {
59 return false;
60 }
61
62 if (inst->qpu.flags.ac != V3D_QPU_COND_NONE ||
63 inst->qpu.flags.mc != V3D_QPU_COND_NONE) {
64 return false;
65 }
66
67 switch (inst->src[0].file) {
68 case QFILE_MAGIC:
69 /* No copy propagating from R3/R4/R5 -- the MOVs from those
70 * are there to register allocate values produced into R3/4/5
71 * to other regs (though hopefully r3/4/5).
72 */
73 switch (inst->src[0].index) {
74 case V3D_QPU_WADDR_R3:
75 case V3D_QPU_WADDR_R4:
76 case V3D_QPU_WADDR_R5:
77 return false;
78 default:
79 break;
80 }
81 break;
82
83 case QFILE_REG:
84 switch (inst->src[0].index) {
85 case 0:
86 case 1:
87 case 2:
88 /* MOVs from rf0/1/2 are only to track the live
89 * intervals for W/centroid W/Z.
90 */
91 return false;
92 }
93 break;
94
95 default:
96 break;
97 }
98
99 return true;
100 }
101
102 static bool
103 vir_has_unpack(struct qinst *inst, int chan)
104 {
105 assert(chan == 0 || chan == 1);
106
107 if (vir_is_add(inst)) {
108 if (chan == 0)
109 return inst->qpu.alu.add.a_unpack != V3D_QPU_UNPACK_NONE;
110 else
111 return inst->qpu.alu.add.b_unpack != V3D_QPU_UNPACK_NONE;
112 } else {
113 if (chan == 0)
114 return inst->qpu.alu.mul.a_unpack != V3D_QPU_UNPACK_NONE;
115 else
116 return inst->qpu.alu.mul.b_unpack != V3D_QPU_UNPACK_NONE;
117 }
118 }
119
120 static bool
121 try_copy_prop(struct v3d_compile *c, struct qinst *inst, struct qinst **movs)
122 {
123 bool debug = false;
124 bool progress = false;
125
126 for (int i = 0; i < vir_get_nsrc(inst); i++) {
127 if (inst->src[i].file != QFILE_TEMP)
128 continue;
129
130 /* We have two ways of finding MOVs we can copy propagate
131 * from. One is if it's an SSA def: then we can reuse it from
132 * any block in the program, as long as its source is also an
133 * SSA def. Alternatively, if it's in the "movs" array
134 * tracked within the block, then we know the sources for it
135 * haven't been changed since we saw the instruction within
136 * our block.
137 */
138 struct qinst *mov = movs[inst->src[i].index];
139 if (!mov) {
140 if (!is_copy_mov(c->defs[inst->src[i].index]))
141 continue;
142 mov = c->defs[inst->src[i].index];
143
144 if (mov->src[0].file == QFILE_TEMP &&
145 !c->defs[mov->src[0].index])
146 continue;
147 }
148
149 if (vir_has_unpack(mov, 0)) {
150 /* Make sure that the meaning of the unpack
151 * would be the same between the two
152 * instructions.
153 */
154 if (v3d_qpu_unpacks_f32(&inst->qpu) !=
155 v3d_qpu_unpacks_f32(&mov->qpu) ||
156 v3d_qpu_unpacks_f16(&inst->qpu) !=
157 v3d_qpu_unpacks_f16(&mov->qpu)) {
158 continue;
159 }
160
161 /* No composing the unpacks. */
162 if (vir_has_unpack(inst, i))
163 continue;
164
165 /* these ops can't represent abs. */
166 if (mov->qpu.alu.mul.a_unpack == V3D_QPU_UNPACK_ABS) {
167 switch (inst->qpu.alu.add.op) {
168 case V3D_QPU_A_VFPACK:
169 case V3D_QPU_A_FROUND:
170 case V3D_QPU_A_FTRUNC:
171 case V3D_QPU_A_FFLOOR:
172 case V3D_QPU_A_FCEIL:
173 case V3D_QPU_A_FDX:
174 case V3D_QPU_A_FDY:
175 case V3D_QPU_A_FTOIN:
176 case V3D_QPU_A_FTOIZ:
177 case V3D_QPU_A_FTOUZ:
178 case V3D_QPU_A_FTOC:
179 continue;
180 default:
181 break;
182 }
183 }
184 }
185
186 if (debug) {
187 fprintf(stderr, "Copy propagate: ");
188 vir_dump_inst(c, inst);
189 fprintf(stderr, "\n");
190 }
191
192 inst->src[i] = mov->src[0];
193 if (vir_has_unpack(mov, 0)) {
194 enum v3d_qpu_input_unpack unpack = mov->qpu.alu.mul.a_unpack;
195
196 vir_set_unpack(inst, i, unpack);
197 }
198
199 if (debug) {
200 fprintf(stderr, "to: ");
201 vir_dump_inst(c, inst);
202 fprintf(stderr, "\n");
203 }
204
205 progress = true;
206 }
207
208 return progress;
209 }
210
211 static void
212 apply_kills(struct v3d_compile *c, struct qinst **movs, struct qinst *inst)
213 {
214 if (inst->dst.file != QFILE_TEMP)
215 return;
216
217 for (int i = 0; i < c->num_temps; i++) {
218 if (movs[i] &&
219 (movs[i]->dst.index == inst->dst.index ||
220 (movs[i]->src[0].file == QFILE_TEMP &&
221 movs[i]->src[0].index == inst->dst.index))) {
222 movs[i] = NULL;
223 }
224 }
225 }
226
227 bool
228 vir_opt_copy_propagate(struct v3d_compile *c)
229 {
230 bool progress = false;
231 struct qinst **movs;
232
233 movs = ralloc_array(c, struct qinst *, c->num_temps);
234 if (!movs)
235 return false;
236
237 vir_for_each_block(block, c) {
238 /* The MOVs array tracks only available movs within the
239 * block.
240 */
241 memset(movs, 0, sizeof(struct qinst *) * c->num_temps);
242
243 vir_for_each_inst(inst, block) {
244 progress = try_copy_prop(c, inst, movs) || progress;
245
246 apply_kills(c, movs, inst);
247
248 if (is_copy_mov(inst))
249 movs[inst->dst.index] = inst;
250 }
251 }
252
253 ralloc_free(movs);
254
255 return progress;
256 }