pan/midgard: Don't special case inline_constant
[mesa.git] / src / panfrost / midgard / midgard_ra.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "compiler.h"
26 #include "midgard_ops.h"
27 #include "util/register_allocate.h"
28 #include "util/u_math.h"
29
30 /* For work registers, we can subdivide in various ways. So we create
31 * classes for the various sizes and conflict accordingly, keeping in
32 * mind that physical registers are divided along 128-bit boundaries.
33 * The important part is that 128-bit boundaries are not crossed.
34 *
35 * For each 128-bit register, we can subdivide to 32-bits 10 ways
36 *
37 * vec4: xyzw
38 * vec3: xyz, yzw
39 * vec2: xy, yz, zw,
40 * vec1: x, y, z, w
41 *
42 * For each 64-bit register, we can subdivide similarly to 16-bit
43 * (TODO: half-float RA, not that we support fp16 yet)
44 */
45
46 #define WORK_STRIDE 10
47
48 /* We have overlapping register classes for special registers, handled via
49 * shadows */
50
51 #define SHADOW_R27 17
52 #define SHADOW_R28 18
53 #define SHADOW_R29 19
54
55 /* Prepacked masks/swizzles for virtual register types */
56 static unsigned reg_type_to_mask[WORK_STRIDE] = {
57 0xF, /* xyzw */
58 0x7, 0x7 << 1, /* xyz */
59 0x3, 0x3 << 1, 0x3 << 2, /* xy */
60 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
61 };
62
63 static unsigned reg_type_to_swizzle[WORK_STRIDE] = {
64 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
65
66 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
67 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_W, COMPONENT_W),
68
69 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
70 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_Z, COMPONENT_W),
71 SWIZZLE(COMPONENT_Z, COMPONENT_W, COMPONENT_Z, COMPONENT_W),
72
73 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
74 SWIZZLE(COMPONENT_Y, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
75 SWIZZLE(COMPONENT_Z, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
76 SWIZZLE(COMPONENT_W, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
77 };
78
79 struct phys_reg {
80 unsigned reg;
81 unsigned mask;
82 unsigned swizzle;
83 };
84
85 /* Given the mask/swizzle of both the register and the original source,
86 * compose to find the actual mask/swizzle to give the hardware */
87
88 static unsigned
89 compose_writemask(unsigned mask, struct phys_reg reg)
90 {
91 /* Note: the reg mask is guaranteed to be contiguous. So we shift
92 * into the X place, compose via a simple AND, and shift back */
93
94 unsigned shift = __builtin_ctz(reg.mask);
95 return ((reg.mask >> shift) & mask) << shift;
96 }
97
98 static unsigned
99 compose_swizzle(unsigned swizzle, unsigned mask,
100 struct phys_reg reg, struct phys_reg dst)
101 {
102 unsigned out = pan_compose_swizzle(swizzle, reg.swizzle);
103
104 /* Based on the register mask, we need to adjust over. E.g if we're
105 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
106 * original first component (x). But to prevent duplicate shifting
107 * (only applies to ALU -- mask param is set to xyzw out on L/S to
108 * prevent changes), we have to account for the shift inherent to the
109 * original writemask */
110
111 unsigned rep = out & 0x3;
112 unsigned shift = __builtin_ctz(dst.mask) - __builtin_ctz(mask);
113 unsigned shifted = out << (2*shift);
114
115 /* ..but we fill in the gaps so it appears to replicate */
116
117 for (unsigned s = 0; s < shift; ++s)
118 shifted |= rep << (2*s);
119
120 return shifted;
121 }
122
123 /* Helper to return the default phys_reg for a given register */
124
125 static struct phys_reg
126 default_phys_reg(int reg)
127 {
128 struct phys_reg r = {
129 .reg = reg,
130 .mask = 0xF, /* xyzw */
131 .swizzle = 0xE4 /* xyzw */
132 };
133
134 return r;
135 }
136
137 /* Determine which physical register, swizzle, and mask a virtual
138 * register corresponds to */
139
140 static struct phys_reg
141 index_to_reg(compiler_context *ctx, struct ra_graph *g, int reg)
142 {
143 /* Check for special cases */
144 if (reg >= SSA_FIXED_MINIMUM)
145 return default_phys_reg(SSA_REG_FROM_FIXED(reg));
146 else if ((reg < 0) || !g)
147 return default_phys_reg(REGISTER_UNUSED);
148
149 /* Special cases aside, we pick the underlying register */
150 int virt = ra_get_node_reg(g, reg);
151
152 /* Divide out the register and classification */
153 int phys = virt / WORK_STRIDE;
154 int type = virt % WORK_STRIDE;
155
156 /* Apply shadow registers */
157
158 if (phys >= SHADOW_R27 && phys <= SHADOW_R29)
159 phys += 27 - SHADOW_R27;
160
161 struct phys_reg r = {
162 .reg = phys,
163 .mask = reg_type_to_mask[type],
164 .swizzle = reg_type_to_swizzle[type]
165 };
166
167 /* Report that we actually use this register, and return it */
168
169 if (phys < 16)
170 ctx->work_registers = MAX2(ctx->work_registers, phys);
171
172 return r;
173 }
174
175 /* This routine creates a register set. Should be called infrequently since
176 * it's slow and can be cached. For legibility, variables are named in terms of
177 * work registers, although it is also used to create the register set for
178 * special register allocation */
179
180 static void
181 add_shadow_conflicts (struct ra_regs *regs, unsigned base, unsigned shadow)
182 {
183 for (unsigned a = 0; a < WORK_STRIDE; ++a) {
184 unsigned reg_a = (WORK_STRIDE * base) + a;
185
186 for (unsigned b = 0; b < WORK_STRIDE; ++b) {
187 unsigned reg_b = (WORK_STRIDE * shadow) + b;
188
189 ra_add_reg_conflict(regs, reg_a, reg_b);
190 ra_add_reg_conflict(regs, reg_b, reg_a);
191 }
192 }
193 }
194
195 static struct ra_regs *
196 create_register_set(unsigned work_count, unsigned *classes)
197 {
198 int virtual_count = 32 * WORK_STRIDE;
199
200 /* First, initialize the RA */
201 struct ra_regs *regs = ra_alloc_reg_set(NULL, virtual_count, true);
202
203 for (unsigned c = 0; c < NR_REG_CLASSES; ++c) {
204 int work_vec4 = ra_alloc_reg_class(regs);
205 int work_vec3 = ra_alloc_reg_class(regs);
206 int work_vec2 = ra_alloc_reg_class(regs);
207 int work_vec1 = ra_alloc_reg_class(regs);
208
209 classes[4*c + 0] = work_vec1;
210 classes[4*c + 1] = work_vec2;
211 classes[4*c + 2] = work_vec3;
212 classes[4*c + 3] = work_vec4;
213
214 /* Special register classes have other register counts */
215 unsigned count =
216 (c == REG_CLASS_WORK) ? work_count :
217 (c == REG_CLASS_LDST27) ? 1 : 2;
218
219 /* We arbitraily pick r17 (RA unused) as the shadow for r27 */
220 unsigned first_reg =
221 (c == REG_CLASS_LDST) ? 26 :
222 (c == REG_CLASS_LDST27) ? SHADOW_R27 :
223 (c == REG_CLASS_TEXR) ? 28 :
224 (c == REG_CLASS_TEXW) ? SHADOW_R28 :
225 0;
226
227 /* Add the full set of work registers */
228 for (unsigned i = first_reg; i < (first_reg + count); ++i) {
229 int base = WORK_STRIDE * i;
230
231 /* Build a full set of subdivisions */
232 ra_class_add_reg(regs, work_vec4, base);
233 ra_class_add_reg(regs, work_vec3, base + 1);
234 ra_class_add_reg(regs, work_vec3, base + 2);
235 ra_class_add_reg(regs, work_vec2, base + 3);
236 ra_class_add_reg(regs, work_vec2, base + 4);
237 ra_class_add_reg(regs, work_vec2, base + 5);
238 ra_class_add_reg(regs, work_vec1, base + 6);
239 ra_class_add_reg(regs, work_vec1, base + 7);
240 ra_class_add_reg(regs, work_vec1, base + 8);
241 ra_class_add_reg(regs, work_vec1, base + 9);
242
243 for (unsigned a = 0; a < 10; ++a) {
244 unsigned mask1 = reg_type_to_mask[a];
245
246 for (unsigned b = 0; b < 10; ++b) {
247 unsigned mask2 = reg_type_to_mask[b];
248
249 if (mask1 & mask2)
250 ra_add_reg_conflict(regs,
251 base + a, base + b);
252 }
253 }
254 }
255 }
256
257
258 /* We have duplicate classes */
259 add_shadow_conflicts(regs, 27, SHADOW_R27);
260 add_shadow_conflicts(regs, 28, SHADOW_R28);
261 add_shadow_conflicts(regs, 29, SHADOW_R29);
262
263 /* We're done setting up */
264 ra_set_finalize(regs, NULL);
265
266 return regs;
267 }
268
269 /* This routine gets a precomputed register set off the screen if it's able, or
270 * otherwise it computes one on the fly */
271
272 static struct ra_regs *
273 get_register_set(struct midgard_screen *screen, unsigned work_count, unsigned **classes)
274 {
275 /* Bounds check */
276 assert(work_count >= 8);
277 assert(work_count <= 16);
278
279 /* Compute index */
280 unsigned index = work_count - 8;
281
282 /* Find the reg set */
283 struct ra_regs *cached = screen->regs[index];
284
285 if (cached) {
286 assert(screen->reg_classes[index]);
287 *classes = screen->reg_classes[index];
288 return cached;
289 }
290
291 /* Otherwise, create one */
292 struct ra_regs *created = create_register_set(work_count, screen->reg_classes[index]);
293
294 /* Cache it and use it */
295 screen->regs[index] = created;
296
297 *classes = screen->reg_classes[index];
298 return created;
299 }
300
301 /* Assign a (special) class, ensuring that it is compatible with whatever class
302 * was already set */
303
304 static void
305 set_class(unsigned *classes, unsigned node, unsigned class)
306 {
307 /* Check that we're even a node */
308 if ((node < 0) || (node >= SSA_FIXED_MINIMUM))
309 return;
310
311 /* First 4 are work, next 4 are load/store.. */
312 unsigned current_class = classes[node] >> 2;
313
314 /* Nothing to do */
315 if (class == current_class)
316 return;
317
318
319 if ((current_class == REG_CLASS_LDST27) && (class == REG_CLASS_LDST))
320 return;
321
322 /* If we're changing, we must not have already assigned a special class
323 */
324
325 bool compat = current_class == REG_CLASS_WORK;
326 compat |= (current_class == REG_CLASS_LDST) && (class == REG_CLASS_LDST27);
327
328 assert(compat);
329
330 classes[node] &= 0x3;
331 classes[node] |= (class << 2);
332 }
333
334 static void
335 force_vec4(unsigned *classes, unsigned node)
336 {
337 if ((node < 0) || (node >= SSA_FIXED_MINIMUM))
338 return;
339
340 /* Force vec4 = 3 */
341 classes[node] |= 0x3;
342 }
343
344 /* Special register classes impose special constraints on who can read their
345 * values, so check that */
346
347 static bool
348 check_read_class(unsigned *classes, unsigned tag, unsigned node)
349 {
350 /* Non-nodes are implicitly ok */
351 if ((node < 0) || (node >= SSA_FIXED_MINIMUM))
352 return true;
353
354 unsigned current_class = classes[node] >> 2;
355
356 switch (current_class) {
357 case REG_CLASS_LDST:
358 case REG_CLASS_LDST27:
359 return (tag == TAG_LOAD_STORE_4);
360 case REG_CLASS_TEXR:
361 return (tag == TAG_TEXTURE_4);
362 case REG_CLASS_TEXW:
363 return (tag != TAG_LOAD_STORE_4);
364 case REG_CLASS_WORK:
365 return (tag == TAG_ALU_4);
366 default:
367 unreachable("Invalid class");
368 }
369 }
370
371 static bool
372 check_write_class(unsigned *classes, unsigned tag, unsigned node)
373 {
374 /* Non-nodes are implicitly ok */
375 if ((node < 0) || (node >= SSA_FIXED_MINIMUM))
376 return true;
377
378 unsigned current_class = classes[node] >> 2;
379
380 switch (current_class) {
381 case REG_CLASS_TEXR:
382 return true;
383 case REG_CLASS_TEXW:
384 return (tag == TAG_TEXTURE_4);
385 case REG_CLASS_LDST:
386 case REG_CLASS_LDST27:
387 case REG_CLASS_WORK:
388 return (tag == TAG_ALU_4) || (tag == TAG_LOAD_STORE_4);
389 default:
390 unreachable("Invalid class");
391 }
392 }
393
394 /* Prepass before RA to ensure special class restrictions are met. The idea is
395 * to create a bit field of types of instructions that read a particular index.
396 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
397
398 static void
399 mark_node_class (unsigned *bitfield, unsigned node)
400 {
401 if ((node >= 0) && (node < SSA_FIXED_MINIMUM))
402 BITSET_SET(bitfield, node);
403 }
404
405 void
406 mir_lower_special_reads(compiler_context *ctx)
407 {
408 size_t sz = BITSET_WORDS(ctx->temp_count) * sizeof(BITSET_WORD);
409
410 /* Bitfields for the various types of registers we could have */
411
412 unsigned *alur = calloc(sz, 1);
413 unsigned *aluw = calloc(sz, 1);
414 unsigned *ldst = calloc(sz, 1);
415 unsigned *texr = calloc(sz, 1);
416 unsigned *texw = calloc(sz, 1);
417
418 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
419
420 mir_foreach_instr_global(ctx, ins) {
421 switch (ins->type) {
422 case TAG_ALU_4:
423 mark_node_class(aluw, ins->ssa_args.dest);
424 mark_node_class(alur, ins->ssa_args.src0);
425 mark_node_class(alur, ins->ssa_args.src1);
426
427 break;
428 case TAG_LOAD_STORE_4:
429 mark_node_class(ldst, ins->ssa_args.src0);
430 mark_node_class(ldst, ins->ssa_args.src1);
431 break;
432 case TAG_TEXTURE_4:
433 mark_node_class(texr, ins->ssa_args.src0);
434 mark_node_class(texr, ins->ssa_args.src1);
435 mark_node_class(texw, ins->ssa_args.dest);
436 break;
437 }
438 }
439
440 /* Pass #2 is lowering now that we've analyzed all the classes.
441 * Conceptually, if an index is only marked for a single type of use,
442 * there is nothing to lower. If it is marked for different uses, we
443 * split up based on the number of types of uses. To do so, we divide
444 * into N distinct classes of use (where N>1 by definition), emit N-1
445 * moves from the index to copies of the index, and finally rewrite N-1
446 * of the types of uses to use the corresponding move */
447
448 unsigned spill_idx = ctx->temp_count;
449
450 for (unsigned i = 0; i < ctx->temp_count; ++i) {
451 bool is_alur = BITSET_TEST(alur, i);
452 bool is_aluw = BITSET_TEST(aluw, i);
453 bool is_ldst = BITSET_TEST(ldst, i);
454 bool is_texr = BITSET_TEST(texr, i);
455 bool is_texw = BITSET_TEST(texw, i);
456
457 /* Analyse to check how many distinct uses there are. ALU ops
458 * (alur) can read the results of the texture pipeline (texw)
459 * but not ldst or texr. Load/store ops (ldst) cannot read
460 * anything but load/store inputs. Texture pipeline cannot read
461 * anything but texture inputs. TODO: Simplify. */
462
463 bool collision =
464 (is_alur && (is_ldst || is_texr)) ||
465 (is_ldst && (is_alur || is_texr || is_texw)) ||
466 (is_texr && (is_alur || is_ldst || is_texw)) ||
467 (is_texw && (is_aluw || is_ldst || is_texr));
468
469 if (!collision)
470 continue;
471
472 /* Use the index as-is as the work copy. Emit copies for
473 * special uses */
474
475 unsigned classes[] = { TAG_LOAD_STORE_4, TAG_TEXTURE_4, TAG_TEXTURE_4 };
476 bool collisions[] = { is_ldst, is_texr, is_texw && is_aluw };
477
478 for (unsigned j = 0; j < ARRAY_SIZE(collisions); ++j) {
479 if (!collisions[j]) continue;
480
481 /* When the hazard is from reading, we move and rewrite
482 * sources (typical case). When it's from writing, we
483 * flip the move and rewrite destinations (obscure,
484 * only from control flow -- impossible in SSA) */
485
486 bool hazard_write = (j == 2);
487
488 unsigned idx = spill_idx++;
489
490 midgard_instruction m = hazard_write ?
491 v_mov(idx, blank_alu_src, i) :
492 v_mov(i, blank_alu_src, idx);
493
494 /* Insert move after each write */
495 mir_foreach_instr_global_safe(ctx, pre_use) {
496 if (pre_use->ssa_args.dest != i)
497 continue;
498
499 /* If the hazard is writing, we need to
500 * specific insert moves for the contentious
501 * class. If the hazard is reading, we insert
502 * moves whenever it is written */
503
504 if (hazard_write && pre_use->type != classes[j])
505 continue;
506
507 midgard_instruction *use = mir_next_op(pre_use);
508 assert(use);
509 mir_insert_instruction_before(use, m);
510 }
511
512 /* Rewrite to use */
513 if (hazard_write)
514 mir_rewrite_index_dst_tag(ctx, i, idx, classes[j]);
515 else
516 mir_rewrite_index_src_tag(ctx, i, idx, classes[j]);
517 }
518 }
519
520 free(alur);
521 free(aluw);
522 free(ldst);
523 free(texr);
524 free(texw);
525 }
526
527 /* This routine performs the actual register allocation. It should be succeeded
528 * by install_registers */
529
530 struct ra_graph *
531 allocate_registers(compiler_context *ctx, bool *spilled)
532 {
533 /* The number of vec4 work registers available depends on when the
534 * uniforms start, so compute that first */
535 int work_count = 16 - MAX2((ctx->uniform_cutoff - 8), 0);
536 unsigned *classes = NULL;
537 struct ra_regs *regs = get_register_set(ctx->screen, work_count, &classes);
538
539 assert(regs != NULL);
540 assert(classes != NULL);
541
542 /* No register allocation to do with no SSA */
543
544 if (!ctx->temp_count)
545 return NULL;
546
547 /* Let's actually do register allocation */
548 int nodes = ctx->temp_count;
549 struct ra_graph *g = ra_alloc_interference_graph(regs, nodes);
550
551 /* Register class (as known to the Mesa register allocator) is actually
552 * the product of both semantic class (work, load/store, texture..) and
553 * size (vec2/vec3..). First, we'll go through and determine the
554 * minimum size needed to hold values */
555
556 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
557
558 mir_foreach_instr_global(ctx, ins) {
559 if (ins->ssa_args.dest < 0) continue;
560 if (ins->ssa_args.dest >= SSA_FIXED_MINIMUM) continue;
561
562 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
563 int class = util_logbase2(ins->mask);
564
565 /* Use the largest class if there's ambiguity, this
566 * handles partial writes */
567
568 int dest = ins->ssa_args.dest;
569 found_class[dest] = MAX2(found_class[dest], class);
570 }
571
572 /* Next, we'll determine semantic class. We default to zero (work).
573 * But, if we're used with a special operation, that will force us to a
574 * particular class. Each node must be assigned to exactly one class; a
575 * prepass before RA should have lowered what-would-have-been
576 * multiclass nodes into a series of moves to break it up into multiple
577 * nodes (TODO) */
578
579 mir_foreach_instr_global(ctx, ins) {
580 /* Check if this operation imposes any classes */
581
582 if (ins->type == TAG_LOAD_STORE_4) {
583 bool force_r27 = OP_IS_R27_ONLY(ins->load_store.op);
584 unsigned class = force_r27 ? REG_CLASS_LDST27 : REG_CLASS_LDST;
585
586 set_class(found_class, ins->ssa_args.src0, class);
587 set_class(found_class, ins->ssa_args.src1, class);
588
589 if (force_r27) {
590 force_vec4(found_class, ins->ssa_args.dest);
591 force_vec4(found_class, ins->ssa_args.src0);
592 force_vec4(found_class, ins->ssa_args.src1);
593 }
594 } else if (ins->type == TAG_TEXTURE_4) {
595 set_class(found_class, ins->ssa_args.dest, REG_CLASS_TEXW);
596 set_class(found_class, ins->ssa_args.src0, REG_CLASS_TEXR);
597 set_class(found_class, ins->ssa_args.src1, REG_CLASS_TEXR);
598 }
599 }
600
601 /* Check that the semantics of the class are respected */
602 mir_foreach_instr_global(ctx, ins) {
603 assert(check_write_class(found_class, ins->type, ins->ssa_args.dest));
604 assert(check_read_class(found_class, ins->type, ins->ssa_args.src0));
605 assert(check_read_class(found_class, ins->type, ins->ssa_args.src1));
606 }
607
608 for (unsigned i = 0; i < ctx->temp_count; ++i) {
609 unsigned class = found_class[i];
610 ra_set_node_class(g, i, classes[class]);
611 }
612
613 /* Determine liveness */
614
615 int *live_start = malloc(nodes * sizeof(int));
616 int *live_end = malloc(nodes * sizeof(int));
617
618 /* Initialize as non-existent */
619
620 for (int i = 0; i < nodes; ++i) {
621 live_start[i] = live_end[i] = -1;
622 }
623
624 int d = 0;
625
626 mir_foreach_block(ctx, block) {
627 mir_foreach_instr_in_block(block, ins) {
628 if (ins->ssa_args.dest < SSA_FIXED_MINIMUM) {
629 /* If this destination is not yet live, it is
630 * now since we just wrote it */
631
632 int dest = ins->ssa_args.dest;
633
634 if (dest >= 0 && live_start[dest] == -1)
635 live_start[dest] = d;
636 }
637
638 /* Since we just used a source, the source might be
639 * dead now. Scan the rest of the block for
640 * invocations, and if there are none, the source dies
641 * */
642
643 int sources[2] = {
644 ins->ssa_args.src0, ins->ssa_args.src1
645 };
646
647 for (int src = 0; src < 2; ++src) {
648 int s = sources[src];
649
650 if (s < 0) continue;
651
652 if (s >= SSA_FIXED_MINIMUM) continue;
653
654 if (!mir_is_live_after(ctx, block, ins, s)) {
655 live_end[s] = d;
656 }
657 }
658
659 ++d;
660 }
661 }
662
663 /* If a node still hasn't been killed, kill it now */
664
665 for (int i = 0; i < nodes; ++i) {
666 /* live_start == -1 most likely indicates a pinned output */
667
668 if (live_end[i] == -1)
669 live_end[i] = d;
670 }
671
672 /* Setup interference between nodes that are live at the same time */
673
674 for (int i = 0; i < nodes; ++i) {
675 for (int j = i + 1; j < nodes; ++j) {
676 bool j_overlaps_i = live_start[j] < live_end[i];
677 bool i_overlaps_j = live_end[j] < live_start[i];
678
679 if (i_overlaps_j || j_overlaps_i)
680 ra_add_node_interference(g, i, j);
681 }
682 }
683
684 /* Cleanup */
685 free(live_start);
686 free(live_end);
687
688 if (!ra_allocate(g)) {
689 *spilled = true;
690 } else {
691 *spilled = false;
692 }
693
694 /* Whether we were successful or not, report the graph so we can
695 * compute spill nodes */
696
697 return g;
698 }
699
700 /* Once registers have been decided via register allocation
701 * (allocate_registers), we need to rewrite the MIR to use registers instead of
702 * indices */
703
704 static void
705 install_registers_instr(
706 compiler_context *ctx,
707 struct ra_graph *g,
708 midgard_instruction *ins)
709 {
710 ssa_args args = ins->ssa_args;
711
712 switch (ins->type) {
713 case TAG_ALU_4: {
714 struct phys_reg src1 = index_to_reg(ctx, g, args.src0);
715 struct phys_reg src2 = index_to_reg(ctx, g, args.src1);
716 struct phys_reg dest = index_to_reg(ctx, g, args.dest);
717
718 unsigned uncomposed_mask = ins->mask;
719 ins->mask = compose_writemask(uncomposed_mask, dest);
720
721 /* Adjust the dest mask if necessary. Mostly this is a no-op
722 * but it matters for dot products */
723 dest.mask = effective_writemask(&ins->alu, ins->mask);
724
725 midgard_vector_alu_src mod1 =
726 vector_alu_from_unsigned(ins->alu.src1);
727 mod1.swizzle = compose_swizzle(mod1.swizzle, uncomposed_mask, src1, dest);
728 ins->alu.src1 = vector_alu_srco_unsigned(mod1);
729
730 ins->registers.src1_reg = src1.reg;
731
732 ins->registers.src2_imm = args.inline_constant;
733
734 if (args.inline_constant) {
735 /* Encode inline 16-bit constant. See disassembler for
736 * where the algorithm is from */
737
738 ins->registers.src2_reg = ins->inline_constant >> 11;
739
740 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
741 uint16_t imm = ((lower_11 >> 8) & 0x7) |
742 ((lower_11 & 0xFF) << 3);
743
744 ins->alu.src2 = imm << 2;
745 } else {
746 midgard_vector_alu_src mod2 =
747 vector_alu_from_unsigned(ins->alu.src2);
748 mod2.swizzle = compose_swizzle(
749 mod2.swizzle, uncomposed_mask, src2, dest);
750 ins->alu.src2 = vector_alu_srco_unsigned(mod2);
751
752 ins->registers.src2_reg = src2.reg;
753 }
754
755 ins->registers.out_reg = dest.reg;
756 break;
757 }
758
759 case TAG_LOAD_STORE_4: {
760 bool fixed = args.src0 >= SSA_FIXED_MINIMUM;
761
762 if (OP_IS_STORE_R26(ins->load_store.op) && fixed) {
763 ins->load_store.reg = SSA_REG_FROM_FIXED(args.src0);
764 } else if (OP_IS_STORE_VARY(ins->load_store.op)) {
765 struct phys_reg src = index_to_reg(ctx, g, args.src0);
766 assert(src.reg == 26 || src.reg == 27);
767
768 ins->load_store.reg = src.reg - 26;
769
770 /* TODO: swizzle/mask */
771 } else {
772 /* Which physical register we read off depends on
773 * whether we are loading or storing -- think about the
774 * logical dataflow */
775
776 bool encodes_src =
777 OP_IS_STORE(ins->load_store.op) &&
778 ins->load_store.op != midgard_op_st_cubemap_coords;
779
780 unsigned r = encodes_src ?
781 args.src0 : args.dest;
782
783 struct phys_reg src = index_to_reg(ctx, g, r);
784
785 ins->load_store.reg = src.reg;
786
787 ins->load_store.swizzle = compose_swizzle(
788 ins->load_store.swizzle, 0xF,
789 default_phys_reg(0), src);
790
791 ins->mask = compose_writemask(
792 ins->mask, src);
793 }
794
795 break;
796 }
797
798 case TAG_TEXTURE_4: {
799 /* Grab RA results */
800 struct phys_reg dest = index_to_reg(ctx, g, args.dest);
801 struct phys_reg coord = index_to_reg(ctx, g, args.src0);
802 struct phys_reg lod = index_to_reg(ctx, g, args.src1);
803
804 assert(dest.reg == 28 || dest.reg == 29);
805 assert(coord.reg == 28 || coord.reg == 29);
806
807 /* First, install the texture coordinate */
808 ins->texture.in_reg_full = 1;
809 ins->texture.in_reg_upper = 0;
810 ins->texture.in_reg_select = coord.reg - 28;
811 ins->texture.in_reg_swizzle =
812 compose_swizzle(ins->texture.in_reg_swizzle, 0xF, coord, dest);
813
814 /* Next, install the destination */
815 ins->texture.out_full = 1;
816 ins->texture.out_upper = 0;
817 ins->texture.out_reg_select = dest.reg - 28;
818 ins->texture.swizzle =
819 compose_swizzle(ins->texture.swizzle, dest.mask, dest, dest);
820 ins->mask =
821 compose_writemask(ins->mask, dest);
822
823 /* If there is a register LOD/bias, use it */
824 if (args.src1 > -1) {
825 midgard_tex_register_select sel = {
826 .select = lod.reg,
827 .full = 1,
828 .component = lod.swizzle & 3,
829 };
830
831 uint8_t packed;
832 memcpy(&packed, &sel, sizeof(packed));
833 ins->texture.bias = packed;
834 }
835
836 break;
837 }
838
839 default:
840 break;
841 }
842 }
843
844 void
845 install_registers(compiler_context *ctx, struct ra_graph *g)
846 {
847 mir_foreach_block(ctx, block) {
848 mir_foreach_instr_in_block(block, ins) {
849 install_registers_instr(ctx, g, ins);
850 }
851 }
852
853 }