vc4: Split register class setup for physical files from accumulators.
[mesa.git] / src / gallium / drivers / vc4 / vc4_register_allocate.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 #include "util/ralloc.h"
25 #include "util/register_allocate.h"
26 #include "vc4_context.h"
27 #include "vc4_qir.h"
28 #include "vc4_qpu.h"
29
30 #define QPU_R(file, index) { QPU_MUX_##file, index }
31
32 static const struct qpu_reg vc4_regs[] = {
33 { QPU_MUX_R0, 0},
34 { QPU_MUX_R1, 0},
35 { QPU_MUX_R2, 0},
36 { QPU_MUX_R3, 0},
37 { QPU_MUX_R4, 0},
38 QPU_R(A, 0),
39 QPU_R(B, 0),
40 QPU_R(A, 1),
41 QPU_R(B, 1),
42 QPU_R(A, 2),
43 QPU_R(B, 2),
44 QPU_R(A, 3),
45 QPU_R(B, 3),
46 QPU_R(A, 4),
47 QPU_R(B, 4),
48 QPU_R(A, 5),
49 QPU_R(B, 5),
50 QPU_R(A, 6),
51 QPU_R(B, 6),
52 QPU_R(A, 7),
53 QPU_R(B, 7),
54 QPU_R(A, 8),
55 QPU_R(B, 8),
56 QPU_R(A, 9),
57 QPU_R(B, 9),
58 QPU_R(A, 10),
59 QPU_R(B, 10),
60 QPU_R(A, 11),
61 QPU_R(B, 11),
62 QPU_R(A, 12),
63 QPU_R(B, 12),
64 QPU_R(A, 13),
65 QPU_R(B, 13),
66 QPU_R(A, 14),
67 QPU_R(B, 14),
68 QPU_R(A, 15),
69 QPU_R(B, 15),
70 QPU_R(A, 16),
71 QPU_R(B, 16),
72 QPU_R(A, 17),
73 QPU_R(B, 17),
74 QPU_R(A, 18),
75 QPU_R(B, 18),
76 QPU_R(A, 19),
77 QPU_R(B, 19),
78 QPU_R(A, 20),
79 QPU_R(B, 20),
80 QPU_R(A, 21),
81 QPU_R(B, 21),
82 QPU_R(A, 22),
83 QPU_R(B, 22),
84 QPU_R(A, 23),
85 QPU_R(B, 23),
86 QPU_R(A, 24),
87 QPU_R(B, 24),
88 QPU_R(A, 25),
89 QPU_R(B, 25),
90 QPU_R(A, 26),
91 QPU_R(B, 26),
92 QPU_R(A, 27),
93 QPU_R(B, 27),
94 QPU_R(A, 28),
95 QPU_R(B, 28),
96 QPU_R(A, 29),
97 QPU_R(B, 29),
98 QPU_R(A, 30),
99 QPU_R(B, 30),
100 QPU_R(A, 31),
101 QPU_R(B, 31),
102 };
103 #define ACC_INDEX 0
104 #define AB_INDEX (ACC_INDEX + 5)
105
106 static void
107 vc4_alloc_reg_set(struct vc4_context *vc4)
108 {
109 assert(vc4_regs[AB_INDEX].addr == 0);
110 assert(vc4_regs[AB_INDEX + 1].addr == 0);
111 STATIC_ASSERT(ARRAY_SIZE(vc4_regs) == AB_INDEX + 64);
112
113 if (vc4->regs)
114 return;
115
116 vc4->regs = ra_alloc_reg_set(vc4, ARRAY_SIZE(vc4_regs), true);
117
118 vc4->reg_class_any = ra_alloc_reg_class(vc4->regs);
119 vc4->reg_class_a_or_b_or_acc = ra_alloc_reg_class(vc4->regs);
120 vc4->reg_class_r4_or_a = ra_alloc_reg_class(vc4->regs);
121 vc4->reg_class_a = ra_alloc_reg_class(vc4->regs);
122 vc4->reg_class_r0_r3 = ra_alloc_reg_class(vc4->regs);
123
124 /* r0-r3 */
125 for (uint32_t i = ACC_INDEX; i < ACC_INDEX + 4; i++) {
126 ra_class_add_reg(vc4->regs, vc4->reg_class_r0_r3, i);
127 ra_class_add_reg(vc4->regs, vc4->reg_class_a_or_b_or_acc, i);
128 }
129
130 /* R4 gets a special class because it can't be written as a general
131 * purpose register. (it's TMU_NOSWAP as a write address).
132 */
133 ra_class_add_reg(vc4->regs, vc4->reg_class_r4_or_a, ACC_INDEX + 4);
134
135 /* A/B */
136 for (uint32_t i = AB_INDEX; i < AB_INDEX + 64; i ++) {
137 /* Reserve ra31/rb31 for spilling fixup_raddr_conflict() in
138 * vc4_qpu_emit.c
139 */
140 if (vc4_regs[i].addr == 31)
141 continue;
142
143 ra_class_add_reg(vc4->regs, vc4->reg_class_any, i);
144 ra_class_add_reg(vc4->regs, vc4->reg_class_a_or_b_or_acc, i);
145
146 /* A only */
147 if (((i - AB_INDEX) & 1) == 0) {
148 ra_class_add_reg(vc4->regs, vc4->reg_class_a, i);
149 ra_class_add_reg(vc4->regs, vc4->reg_class_r4_or_a, i);
150 }
151 }
152
153 ra_set_finalize(vc4->regs, NULL);
154 }
155
156 struct node_to_temp_map {
157 uint32_t temp;
158 uint32_t priority;
159 };
160
161 static int
162 node_to_temp_priority(const void *in_a, const void *in_b)
163 {
164 const struct node_to_temp_map *a = in_a;
165 const struct node_to_temp_map *b = in_b;
166
167 return a->priority - b->priority;
168 }
169
170 #define CLASS_BIT_A (1 << 0)
171 #define CLASS_BIT_B (1 << 1)
172 #define CLASS_BIT_R4 (1 << 2)
173 #define CLASS_BIT_R0_R3 (1 << 4)
174
175 /**
176 * Returns a mapping from QFILE_TEMP indices to struct qpu_regs.
177 *
178 * The return value should be freed by the caller.
179 */
180 struct qpu_reg *
181 vc4_register_allocate(struct vc4_context *vc4, struct vc4_compile *c)
182 {
183 struct node_to_temp_map map[c->num_temps];
184 uint32_t temp_to_node[c->num_temps];
185 uint8_t class_bits[c->num_temps];
186 struct qpu_reg *temp_registers = calloc(c->num_temps,
187 sizeof(*temp_registers));
188
189 /* If things aren't ever written (undefined values), just read from
190 * r0.
191 */
192 for (uint32_t i = 0; i < c->num_temps; i++)
193 temp_registers[i] = qpu_rn(0);
194
195 vc4_alloc_reg_set(vc4);
196
197 struct ra_graph *g = ra_alloc_interference_graph(vc4->regs,
198 c->num_temps);
199
200 /* Compute the live ranges so we can figure out interference. */
201 qir_calculate_live_intervals(c);
202
203 for (uint32_t i = 0; i < c->num_temps; i++) {
204 map[i].temp = i;
205 map[i].priority = c->temp_end[i] - c->temp_start[i];
206 }
207 qsort(map, c->num_temps, sizeof(map[0]), node_to_temp_priority);
208 for (uint32_t i = 0; i < c->num_temps; i++) {
209 temp_to_node[map[i].temp] = i;
210 }
211
212 /* Figure out our register classes and preallocated registers. We
213 * start with any temp being able to be in any file, then instructions
214 * incrementally remove bits that the temp definitely can't be in.
215 */
216 memset(class_bits,
217 CLASS_BIT_A | CLASS_BIT_B | CLASS_BIT_R4 | CLASS_BIT_R0_R3,
218 sizeof(class_bits));
219
220 int ip = 0;
221 qir_for_each_inst_inorder(inst, c) {
222 if (qir_writes_r4(inst)) {
223 /* This instruction writes r4 (and optionally moves
224 * its result to a temp), so nothing else can be
225 * stored in r4 across it.
226 */
227 for (int i = 0; i < c->num_temps; i++) {
228 if (c->temp_start[i] < ip && c->temp_end[i] > ip)
229 class_bits[i] &= ~CLASS_BIT_R4;
230 }
231 } else {
232 /* R4 can't be written as a general purpose
233 * register. (it's TMU_NOSWAP as a write address).
234 */
235 if (inst->dst.file == QFILE_TEMP)
236 class_bits[inst->dst.index] &= ~CLASS_BIT_R4;
237 }
238
239 switch (inst->op) {
240 case QOP_FRAG_Z:
241 ra_set_node_reg(g, temp_to_node[inst->dst.index],
242 AB_INDEX + QPU_R_FRAG_PAYLOAD_ZW * 2 + 1);
243 break;
244
245 case QOP_FRAG_W:
246 ra_set_node_reg(g, temp_to_node[inst->dst.index],
247 AB_INDEX + QPU_R_FRAG_PAYLOAD_ZW * 2);
248 break;
249
250 case QOP_ROT_MUL:
251 assert(inst->src[0].file == QFILE_TEMP);
252 class_bits[inst->src[0].index] &= CLASS_BIT_R0_R3;
253 break;
254
255 default:
256 break;
257 }
258
259 if (inst->dst.pack && !qir_is_mul(inst)) {
260 /* The non-MUL pack flags require an A-file dst
261 * register.
262 */
263 class_bits[inst->dst.index] &= CLASS_BIT_A;
264 }
265
266 /* Apply restrictions for src unpacks. The integer unpacks
267 * can only be done from regfile A, while float unpacks can be
268 * either A or R4.
269 */
270 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
271 if (inst->src[i].file == QFILE_TEMP &&
272 inst->src[i].pack) {
273 if (qir_is_float_input(inst)) {
274 class_bits[inst->src[i].index] &=
275 CLASS_BIT_A | CLASS_BIT_R4;
276 } else {
277 class_bits[inst->src[i].index] &=
278 CLASS_BIT_A;
279 }
280 }
281 }
282
283 ip++;
284 }
285
286 for (uint32_t i = 0; i < c->num_temps; i++) {
287 int node = temp_to_node[i];
288
289 switch (class_bits[i]) {
290 case CLASS_BIT_A | CLASS_BIT_B | CLASS_BIT_R4 | CLASS_BIT_R0_R3:
291 ra_set_node_class(g, node, vc4->reg_class_any);
292 break;
293 case CLASS_BIT_A | CLASS_BIT_B | CLASS_BIT_R0_R3:
294 ra_set_node_class(g, node, vc4->reg_class_a_or_b_or_acc);
295 break;
296 case CLASS_BIT_A | CLASS_BIT_R4:
297 ra_set_node_class(g, node, vc4->reg_class_r4_or_a);
298 break;
299 case CLASS_BIT_A:
300 ra_set_node_class(g, node, vc4->reg_class_a);
301 break;
302 case CLASS_BIT_R0_R3:
303 ra_set_node_class(g, node, vc4->reg_class_r0_r3);
304 break;
305 default:
306 fprintf(stderr, "temp %d: bad class bits: 0x%x\n",
307 i, class_bits[i]);
308 abort();
309 break;
310 }
311 }
312
313 for (uint32_t i = 0; i < c->num_temps; i++) {
314 for (uint32_t j = i + 1; j < c->num_temps; j++) {
315 if (!(c->temp_start[i] >= c->temp_end[j] ||
316 c->temp_start[j] >= c->temp_end[i])) {
317 ra_add_node_interference(g,
318 temp_to_node[i],
319 temp_to_node[j]);
320 }
321 }
322 }
323
324 bool ok = ra_allocate(g);
325 if (!ok) {
326 fprintf(stderr, "Failed to register allocate:\n");
327 qir_dump(c);
328 c->failed = true;
329 return NULL;
330 }
331
332 for (uint32_t i = 0; i < c->num_temps; i++) {
333 temp_registers[i] = vc4_regs[ra_get_node_reg(g, temp_to_node[i])];
334
335 /* If the value's never used, just write to the NOP register
336 * for clarity in debug output.
337 */
338 if (c->temp_start[i] == c->temp_end[i])
339 temp_registers[i] = qpu_ra(QPU_W_NOP);
340 }
341
342 ralloc_free(g);
343
344 return temp_registers;
345 }