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