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