2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019 Collabora, Ltd.
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:
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
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
26 #include "midgard_ops.h"
27 #include "util/register_allocate.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
31 /* For work registers, we can subdivide in various ways. So we create
32 * classes for the various sizes and conflict accordingly, keeping in
33 * mind that physical registers are divided along 128-bit boundaries.
34 * The important part is that 128-bit boundaries are not crossed.
36 * For each 128-bit register, we can subdivide to 32-bits 10 ways
43 * For each 64-bit register, we can subdivide similarly to 16-bit
44 * (TODO: half-float RA, not that we support fp16 yet)
47 #define WORK_STRIDE 10
49 /* We have overlapping register classes for special registers, handled via
55 /* Prepacked masks/swizzles for virtual register types */
56 static unsigned reg_type_to_mask
[WORK_STRIDE
] = {
58 0x7, 0x7 << 1, /* xyz */
59 0x3, 0x3 << 1, 0x3 << 2, /* xy */
60 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
63 static unsigned reg_type_to_swizzle
[WORK_STRIDE
] = {
64 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
66 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
67 SWIZZLE(COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
, COMPONENT_W
),
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
),
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
),
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 */
89 compose_writemask(unsigned mask
, struct phys_reg reg
)
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 */
94 unsigned shift
= __builtin_ctz(reg
.mask
);
95 return ((reg
.mask
>> shift
) & mask
) << shift
;
99 compose_swizzle(unsigned swizzle
, unsigned mask
,
100 struct phys_reg reg
, struct phys_reg dst
)
102 unsigned out
= pan_compose_swizzle(swizzle
, reg
.swizzle
);
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 */
111 unsigned rep
= out
& 0x3;
112 unsigned shift
= __builtin_ctz(dst
.mask
) - __builtin_ctz(mask
);
113 unsigned shifted
= out
<< (2*shift
);
115 /* ..but we fill in the gaps so it appears to replicate */
117 for (unsigned s
= 0; s
< shift
; ++s
)
118 shifted
|= rep
<< (2*s
);
123 /* Helper to return the default phys_reg for a given register */
125 static struct phys_reg
126 default_phys_reg(int reg
)
128 struct phys_reg r
= {
130 .mask
= 0xF, /* xyzw */
131 .swizzle
= 0xE4 /* xyzw */
137 /* Determine which physical register, swizzle, and mask a virtual
138 * register corresponds to */
140 static struct phys_reg
141 index_to_reg(compiler_context
*ctx
, struct ra_graph
*g
, int reg
)
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
);
149 /* Special cases aside, we pick the underlying register */
150 int virt
= ra_get_node_reg(g
, reg
);
152 /* Divide out the register and classification */
153 int phys
= virt
/ WORK_STRIDE
;
154 int type
= virt
% WORK_STRIDE
;
156 /* Apply shadow registers */
158 if (phys
>= SHADOW_R28
&& phys
<= SHADOW_R29
)
159 phys
+= 28 - SHADOW_R28
;
161 struct phys_reg r
= {
163 .mask
= reg_type_to_mask
[type
],
164 .swizzle
= reg_type_to_swizzle
[type
]
167 /* Report that we actually use this register, and return it */
170 ctx
->work_registers
= MAX2(ctx
->work_registers
, phys
);
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 */
181 add_shadow_conflicts (struct ra_regs
*regs
, unsigned base
, unsigned shadow
)
183 for (unsigned a
= 0; a
< WORK_STRIDE
; ++a
) {
184 unsigned reg_a
= (WORK_STRIDE
* base
) + a
;
186 for (unsigned b
= 0; b
< WORK_STRIDE
; ++b
) {
187 unsigned reg_b
= (WORK_STRIDE
* shadow
) + b
;
189 ra_add_reg_conflict(regs
, reg_a
, reg_b
);
190 ra_add_reg_conflict(regs
, reg_b
, reg_a
);
195 static struct ra_regs
*
196 create_register_set(unsigned work_count
, unsigned *classes
)
198 int virtual_count
= 32 * WORK_STRIDE
;
200 /* First, initialize the RA */
201 struct ra_regs
*regs
= ra_alloc_reg_set(NULL
, virtual_count
, true);
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
);
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
;
214 /* Special register classes have other register counts */
216 (c
== REG_CLASS_WORK
) ? work_count
: 2;
219 (c
== REG_CLASS_LDST
) ? 26 :
220 (c
== REG_CLASS_TEXR
) ? 28 :
221 (c
== REG_CLASS_TEXW
) ? SHADOW_R28
:
224 /* Add the full set of work registers */
225 for (unsigned i
= first_reg
; i
< (first_reg
+ count
); ++i
) {
226 int base
= WORK_STRIDE
* i
;
228 /* Build a full set of subdivisions */
229 ra_class_add_reg(regs
, work_vec4
, base
);
230 ra_class_add_reg(regs
, work_vec3
, base
+ 1);
231 ra_class_add_reg(regs
, work_vec3
, base
+ 2);
232 ra_class_add_reg(regs
, work_vec2
, base
+ 3);
233 ra_class_add_reg(regs
, work_vec2
, base
+ 4);
234 ra_class_add_reg(regs
, work_vec2
, base
+ 5);
235 ra_class_add_reg(regs
, work_vec1
, base
+ 6);
236 ra_class_add_reg(regs
, work_vec1
, base
+ 7);
237 ra_class_add_reg(regs
, work_vec1
, base
+ 8);
238 ra_class_add_reg(regs
, work_vec1
, base
+ 9);
240 for (unsigned a
= 0; a
< 10; ++a
) {
241 unsigned mask1
= reg_type_to_mask
[a
];
243 for (unsigned b
= 0; b
< 10; ++b
) {
244 unsigned mask2
= reg_type_to_mask
[b
];
247 ra_add_reg_conflict(regs
,
255 /* We have duplicate classes */
256 add_shadow_conflicts(regs
, 28, SHADOW_R28
);
257 add_shadow_conflicts(regs
, 29, SHADOW_R29
);
259 /* We're done setting up */
260 ra_set_finalize(regs
, NULL
);
265 /* This routine gets a precomputed register set off the screen if it's able, or
266 * otherwise it computes one on the fly */
268 static struct ra_regs
*
269 get_register_set(struct midgard_screen
*screen
, unsigned work_count
, unsigned **classes
)
272 assert(work_count
>= 8);
273 assert(work_count
<= 16);
276 unsigned index
= work_count
- 8;
278 /* Find the reg set */
279 struct ra_regs
*cached
= screen
->regs
[index
];
282 assert(screen
->reg_classes
[index
]);
283 *classes
= screen
->reg_classes
[index
];
287 /* Otherwise, create one */
288 struct ra_regs
*created
= create_register_set(work_count
, screen
->reg_classes
[index
]);
290 /* Cache it and use it */
291 screen
->regs
[index
] = created
;
293 *classes
= screen
->reg_classes
[index
];
297 /* Assign a (special) class, ensuring that it is compatible with whatever class
301 set_class(unsigned *classes
, unsigned node
, unsigned class)
303 /* Check that we're even a node */
304 if ((node
< 0) || (node
>= SSA_FIXED_MINIMUM
))
307 /* First 4 are work, next 4 are load/store.. */
308 unsigned current_class
= classes
[node
] >> 2;
311 if (class == current_class
)
314 /* If we're changing, we haven't assigned a special class */
315 assert(current_class
== REG_CLASS_WORK
);
317 classes
[node
] &= 0x3;
318 classes
[node
] |= (class << 2);
322 force_vec4(unsigned *classes
, unsigned node
)
324 if ((node
< 0) || (node
>= SSA_FIXED_MINIMUM
))
328 classes
[node
] |= 0x3;
331 /* Special register classes impose special constraints on who can read their
332 * values, so check that */
335 check_read_class(unsigned *classes
, unsigned tag
, unsigned node
)
337 /* Non-nodes are implicitly ok */
338 if ((node
< 0) || (node
>= SSA_FIXED_MINIMUM
))
341 unsigned current_class
= classes
[node
] >> 2;
343 switch (current_class
) {
345 return (tag
== TAG_LOAD_STORE_4
);
347 return (tag
== TAG_TEXTURE_4
);
349 return (tag
!= TAG_LOAD_STORE_4
);
351 return (tag
== TAG_ALU_4
);
353 unreachable("Invalid class");
358 check_write_class(unsigned *classes
, unsigned tag
, unsigned node
)
360 /* Non-nodes are implicitly ok */
361 if ((node
< 0) || (node
>= SSA_FIXED_MINIMUM
))
364 unsigned current_class
= classes
[node
] >> 2;
366 switch (current_class
) {
370 return (tag
== TAG_TEXTURE_4
);
373 return (tag
== TAG_ALU_4
) || (tag
== TAG_LOAD_STORE_4
);
375 unreachable("Invalid class");
379 /* Prepass before RA to ensure special class restrictions are met. The idea is
380 * to create a bit field of types of instructions that read a particular index.
381 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
384 mark_node_class (unsigned *bitfield
, unsigned node
)
386 if ((node
>= 0) && (node
< SSA_FIXED_MINIMUM
))
387 BITSET_SET(bitfield
, node
);
391 mir_lower_special_reads(compiler_context
*ctx
)
393 size_t sz
= BITSET_WORDS(ctx
->temp_count
) * sizeof(BITSET_WORD
);
395 /* Bitfields for the various types of registers we could have */
397 unsigned *alur
= calloc(sz
, 1);
398 unsigned *aluw
= calloc(sz
, 1);
399 unsigned *ldst
= calloc(sz
, 1);
400 unsigned *texr
= calloc(sz
, 1);
401 unsigned *texw
= calloc(sz
, 1);
403 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
405 mir_foreach_instr_global(ctx
, ins
) {
408 mark_node_class(aluw
, ins
->ssa_args
.dest
);
409 mark_node_class(alur
, ins
->ssa_args
.src
[0]);
410 mark_node_class(alur
, ins
->ssa_args
.src
[1]);
413 case TAG_LOAD_STORE_4
:
414 mark_node_class(ldst
, ins
->ssa_args
.src
[0]);
415 mark_node_class(ldst
, ins
->ssa_args
.src
[1]);
416 mark_node_class(ldst
, ins
->ssa_args
.src
[2]);
420 mark_node_class(texr
, ins
->ssa_args
.src
[0]);
421 mark_node_class(texr
, ins
->ssa_args
.src
[1]);
422 mark_node_class(texr
, ins
->ssa_args
.src
[2]);
423 mark_node_class(texw
, ins
->ssa_args
.dest
);
428 /* Pass #2 is lowering now that we've analyzed all the classes.
429 * Conceptually, if an index is only marked for a single type of use,
430 * there is nothing to lower. If it is marked for different uses, we
431 * split up based on the number of types of uses. To do so, we divide
432 * into N distinct classes of use (where N>1 by definition), emit N-1
433 * moves from the index to copies of the index, and finally rewrite N-1
434 * of the types of uses to use the corresponding move */
436 unsigned spill_idx
= ctx
->temp_count
;
438 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
439 bool is_alur
= BITSET_TEST(alur
, i
);
440 bool is_aluw
= BITSET_TEST(aluw
, i
);
441 bool is_ldst
= BITSET_TEST(ldst
, i
);
442 bool is_texr
= BITSET_TEST(texr
, i
);
443 bool is_texw
= BITSET_TEST(texw
, i
);
445 /* Analyse to check how many distinct uses there are. ALU ops
446 * (alur) can read the results of the texture pipeline (texw)
447 * but not ldst or texr. Load/store ops (ldst) cannot read
448 * anything but load/store inputs. Texture pipeline cannot read
449 * anything but texture inputs. TODO: Simplify. */
452 (is_alur
&& (is_ldst
|| is_texr
)) ||
453 (is_ldst
&& (is_alur
|| is_texr
|| is_texw
)) ||
454 (is_texr
&& (is_alur
|| is_ldst
|| is_texw
)) ||
455 (is_texw
&& (is_aluw
|| is_ldst
|| is_texr
));
460 /* Use the index as-is as the work copy. Emit copies for
463 unsigned classes
[] = { TAG_LOAD_STORE_4
, TAG_TEXTURE_4
, TAG_TEXTURE_4
};
464 bool collisions
[] = { is_ldst
, is_texr
, is_texw
&& is_aluw
};
466 for (unsigned j
= 0; j
< ARRAY_SIZE(collisions
); ++j
) {
467 if (!collisions
[j
]) continue;
469 /* When the hazard is from reading, we move and rewrite
470 * sources (typical case). When it's from writing, we
471 * flip the move and rewrite destinations (obscure,
472 * only from control flow -- impossible in SSA) */
474 bool hazard_write
= (j
== 2);
476 unsigned idx
= spill_idx
++;
478 midgard_instruction m
= hazard_write
?
479 v_mov(idx
, blank_alu_src
, i
) :
480 v_mov(i
, blank_alu_src
, idx
);
482 /* Insert move before each read/write, depending on the
483 * hazard we're trying to account for */
485 mir_foreach_instr_global_safe(ctx
, pre_use
) {
486 if (pre_use
->type
!= classes
[j
])
490 if (pre_use
->ssa_args
.dest
!= i
)
493 if (!mir_has_arg(pre_use
, i
))
498 midgard_instruction
*use
= mir_next_op(pre_use
);
500 mir_insert_instruction_before(use
, m
);
501 mir_rewrite_index_dst_single(pre_use
, i
, idx
);
504 m
= v_mov(i
, blank_alu_src
, idx
);
505 m
.mask
= mir_mask_of_read_components(pre_use
, i
);
506 mir_insert_instruction_before(pre_use
, m
);
507 mir_rewrite_index_src_single(pre_use
, i
, idx
);
520 /* Routines for liveness analysis */
523 liveness_gen(uint8_t *live
, unsigned node
, unsigned max
, unsigned mask
)
525 if ((node
< 0) || (node
>= max
))
532 liveness_kill(uint8_t *live
, unsigned node
, unsigned max
, unsigned mask
)
534 if ((node
< 0) || (node
>= max
))
540 /* Updates live_in for a single instruction */
543 liveness_ins_update(uint8_t *live
, midgard_instruction
*ins
, unsigned max
)
545 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
547 liveness_kill(live
, ins
->ssa_args
.dest
, max
, ins
->mask
);
549 mir_foreach_src(ins
, src
) {
550 unsigned node
= ins
->ssa_args
.src
[src
];
551 unsigned mask
= mir_mask_of_read_components(ins
, node
);
553 liveness_gen(live
, node
, max
, mask
);
557 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
560 liveness_block_live_out(compiler_context
*ctx
, midgard_block
*blk
)
562 mir_foreach_successor(blk
, succ
) {
563 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
564 blk
->live_out
[i
] |= succ
->live_in
[i
];
568 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
569 * we compute live_out from live_in. The intrablock pass is linear-time. It
570 * returns whether progress was made. */
573 liveness_block_update(compiler_context
*ctx
, midgard_block
*blk
)
575 bool progress
= false;
577 liveness_block_live_out(ctx
, blk
);
579 uint8_t *live
= mem_dup(blk
->live_out
, ctx
->temp_count
);
581 mir_foreach_instr_in_block_rev(blk
, ins
)
582 liveness_ins_update(live
, ins
, ctx
->temp_count
);
584 /* To figure out progress, diff live_in */
586 for (unsigned i
= 0; (i
< ctx
->temp_count
) && !progress
; ++i
)
587 progress
|= (blk
->live_in
[i
] != live
[i
]);
595 /* Globally, liveness analysis uses a fixed-point algorithm based on a
596 * worklist. We initialize a work list with the exit block. We iterate the work
597 * list to compute live_in from live_out for each block on the work list,
598 * adding the predecessors of the block to the work list if we made progress.
602 mir_compute_liveness(
603 compiler_context
*ctx
,
606 /* List of midgard_block */
607 struct set
*work_list
;
609 work_list
= _mesa_set_create(ctx
,
611 _mesa_key_pointer_equal
);
615 mir_foreach_block(ctx
, block
) {
616 block
->live_in
= calloc(ctx
->temp_count
, 1);
617 block
->live_out
= calloc(ctx
->temp_count
, 1);
620 /* Initialize the work list with the exit block */
621 struct set_entry
*cur
;
623 midgard_block
*exit
= mir_exit_block(ctx
);
624 cur
= _mesa_set_add(work_list
, exit
);
626 /* Iterate the work list */
629 /* Pop off a block */
630 midgard_block
*blk
= (struct midgard_block
*) cur
->key
;
631 _mesa_set_remove(work_list
, cur
);
633 /* Update its liveness information */
634 bool progress
= liveness_block_update(ctx
, blk
);
636 /* If we made progress, we need to process the predecessors */
638 if (progress
|| (blk
== exit
)) {
639 mir_foreach_predecessor(blk
, pred
)
640 _mesa_set_add(work_list
, pred
);
642 } while((cur
= _mesa_set_next_entry(work_list
, NULL
)) != NULL
);
644 /* Now that every block has live_in/live_out computed, we can determine
645 * interference by walking each block linearly. Take live_out at the
646 * end of each block and walk the block backwards. */
648 mir_foreach_block(ctx
, blk
) {
649 uint8_t *live
= calloc(ctx
->temp_count
, 1);
651 mir_foreach_successor(blk
, succ
) {
652 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
653 live
[i
] |= succ
->live_in
[i
];
656 mir_foreach_instr_in_block_rev(blk
, ins
) {
657 /* Mark all registers live after the instruction as
658 * interfering with the destination */
660 unsigned dest
= ins
->ssa_args
.dest
;
662 if (dest
>= 0 && dest
< ctx
->temp_count
) {
663 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
665 ra_add_node_interference(g
, dest
, i
);
669 liveness_ins_update(live
, ins
, ctx
->temp_count
);
675 mir_foreach_block(ctx
, blk
) {
681 /* This routine performs the actual register allocation. It should be succeeded
682 * by install_registers */
685 allocate_registers(compiler_context
*ctx
, bool *spilled
)
687 /* The number of vec4 work registers available depends on when the
688 * uniforms start, so compute that first */
689 int work_count
= 16 - MAX2((ctx
->uniform_cutoff
- 8), 0);
690 unsigned *classes
= NULL
;
691 struct ra_regs
*regs
= get_register_set(ctx
->screen
, work_count
, &classes
);
693 assert(regs
!= NULL
);
694 assert(classes
!= NULL
);
696 /* No register allocation to do with no SSA */
698 if (!ctx
->temp_count
)
701 /* Let's actually do register allocation */
702 int nodes
= ctx
->temp_count
;
703 struct ra_graph
*g
= ra_alloc_interference_graph(regs
, nodes
);
705 /* Register class (as known to the Mesa register allocator) is actually
706 * the product of both semantic class (work, load/store, texture..) and
707 * size (vec2/vec3..). First, we'll go through and determine the
708 * minimum size needed to hold values */
710 unsigned *found_class
= calloc(sizeof(unsigned), ctx
->temp_count
);
712 mir_foreach_instr_global(ctx
, ins
) {
713 if (ins
->ssa_args
.dest
< 0) continue;
714 if (ins
->ssa_args
.dest
>= SSA_FIXED_MINIMUM
) continue;
716 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
717 int class = util_logbase2(ins
->mask
);
719 /* Use the largest class if there's ambiguity, this
720 * handles partial writes */
722 int dest
= ins
->ssa_args
.dest
;
723 found_class
[dest
] = MAX2(found_class
[dest
], class);
726 /* Next, we'll determine semantic class. We default to zero (work).
727 * But, if we're used with a special operation, that will force us to a
728 * particular class. Each node must be assigned to exactly one class; a
729 * prepass before RA should have lowered what-would-have-been
730 * multiclass nodes into a series of moves to break it up into multiple
733 mir_foreach_instr_global(ctx
, ins
) {
734 /* Check if this operation imposes any classes */
736 if (ins
->type
== TAG_LOAD_STORE_4
) {
737 bool force_vec4_only
= OP_IS_VEC4_ONLY(ins
->load_store
.op
);
739 set_class(found_class
, ins
->ssa_args
.src
[0], REG_CLASS_LDST
);
740 set_class(found_class
, ins
->ssa_args
.src
[1], REG_CLASS_LDST
);
741 set_class(found_class
, ins
->ssa_args
.src
[2], REG_CLASS_LDST
);
743 if (force_vec4_only
) {
744 force_vec4(found_class
, ins
->ssa_args
.dest
);
745 force_vec4(found_class
, ins
->ssa_args
.src
[0]);
746 force_vec4(found_class
, ins
->ssa_args
.src
[1]);
747 force_vec4(found_class
, ins
->ssa_args
.src
[2]);
749 } else if (ins
->type
== TAG_TEXTURE_4
) {
750 set_class(found_class
, ins
->ssa_args
.dest
, REG_CLASS_TEXW
);
751 set_class(found_class
, ins
->ssa_args
.src
[0], REG_CLASS_TEXR
);
752 set_class(found_class
, ins
->ssa_args
.src
[1], REG_CLASS_TEXR
);
753 set_class(found_class
, ins
->ssa_args
.src
[2], REG_CLASS_TEXR
);
757 /* Check that the semantics of the class are respected */
758 mir_foreach_instr_global(ctx
, ins
) {
759 assert(check_write_class(found_class
, ins
->type
, ins
->ssa_args
.dest
));
760 assert(check_read_class(found_class
, ins
->type
, ins
->ssa_args
.src
[0]));
761 assert(check_read_class(found_class
, ins
->type
, ins
->ssa_args
.src
[1]));
762 assert(check_read_class(found_class
, ins
->type
, ins
->ssa_args
.src
[2]));
765 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
766 unsigned class = found_class
[i
];
767 ra_set_node_class(g
, i
, classes
[class]);
770 mir_compute_liveness(ctx
, g
);
772 if (!ra_allocate(g
)) {
778 /* Whether we were successful or not, report the graph so we can
779 * compute spill nodes */
784 /* Once registers have been decided via register allocation
785 * (allocate_registers), we need to rewrite the MIR to use registers instead of
789 install_registers_instr(
790 compiler_context
*ctx
,
792 midgard_instruction
*ins
)
794 ssa_args args
= ins
->ssa_args
;
798 struct phys_reg src1
= index_to_reg(ctx
, g
, args
.src
[0]);
799 struct phys_reg src2
= index_to_reg(ctx
, g
, args
.src
[1]);
800 struct phys_reg dest
= index_to_reg(ctx
, g
, args
.dest
);
802 unsigned uncomposed_mask
= ins
->mask
;
803 ins
->mask
= compose_writemask(uncomposed_mask
, dest
);
805 /* Adjust the dest mask if necessary. Mostly this is a no-op
806 * but it matters for dot products */
807 dest
.mask
= effective_writemask(&ins
->alu
, ins
->mask
);
809 midgard_vector_alu_src mod1
=
810 vector_alu_from_unsigned(ins
->alu
.src1
);
811 mod1
.swizzle
= compose_swizzle(mod1
.swizzle
, uncomposed_mask
, src1
, dest
);
812 ins
->alu
.src1
= vector_alu_srco_unsigned(mod1
);
814 ins
->registers
.src1_reg
= src1
.reg
;
816 ins
->registers
.src2_imm
= args
.inline_constant
;
818 if (args
.inline_constant
) {
819 /* Encode inline 16-bit constant. See disassembler for
820 * where the algorithm is from */
822 ins
->registers
.src2_reg
= ins
->inline_constant
>> 11;
824 int lower_11
= ins
->inline_constant
& ((1 << 12) - 1);
825 uint16_t imm
= ((lower_11
>> 8) & 0x7) |
826 ((lower_11
& 0xFF) << 3);
828 ins
->alu
.src2
= imm
<< 2;
830 midgard_vector_alu_src mod2
=
831 vector_alu_from_unsigned(ins
->alu
.src2
);
832 mod2
.swizzle
= compose_swizzle(
833 mod2
.swizzle
, uncomposed_mask
, src2
, dest
);
834 ins
->alu
.src2
= vector_alu_srco_unsigned(mod2
);
836 ins
->registers
.src2_reg
= src2
.reg
;
839 ins
->registers
.out_reg
= dest
.reg
;
843 case TAG_LOAD_STORE_4
: {
844 /* Which physical register we read off depends on
845 * whether we are loading or storing -- think about the
846 * logical dataflow */
848 bool encodes_src
= OP_IS_STORE(ins
->load_store
.op
);
851 struct phys_reg src
= index_to_reg(ctx
, g
, args
.src
[0]);
852 assert(src
.reg
== 26 || src
.reg
== 27);
854 ins
->load_store
.reg
= src
.reg
- 26;
856 unsigned shift
= __builtin_ctz(src
.mask
);
857 unsigned adjusted_mask
= src
.mask
>> shift
;
858 assert(((adjusted_mask
+ 1) & adjusted_mask
) == 0);
860 unsigned new_swizzle
= 0;
861 for (unsigned q
= 0; q
< 4; ++q
) {
862 unsigned c
= (ins
->load_store
.swizzle
>> (2*q
)) & 3;
863 new_swizzle
|= (c
+ shift
) << (2*q
);
866 ins
->load_store
.swizzle
= compose_swizzle(
867 new_swizzle
, src
.mask
,
868 default_phys_reg(0), src
);
870 unsigned r
= encodes_src
?
871 args
.src
[0] : args
.dest
;
873 struct phys_reg src
= index_to_reg(ctx
, g
, r
);
875 ins
->load_store
.reg
= src
.reg
;
877 ins
->load_store
.swizzle
= compose_swizzle(
878 ins
->load_store
.swizzle
, 0xF,
879 default_phys_reg(0), src
);
881 ins
->mask
= compose_writemask(
885 /* We also follow up by actual arguments */
888 encodes_src
? args
.src
[1] : args
.src
[0];
891 encodes_src
? args
.src
[2] : args
.src
[1];
894 struct phys_reg src
= index_to_reg(ctx
, g
, src2
);
895 unsigned component
= __builtin_ctz(src
.mask
);
896 ins
->load_store
.arg_1
|= midgard_ldst_reg(src
.reg
, component
);
900 struct phys_reg src
= index_to_reg(ctx
, g
, src3
);
901 unsigned component
= __builtin_ctz(src
.mask
);
902 ins
->load_store
.arg_2
|= midgard_ldst_reg(src
.reg
, component
);
908 case TAG_TEXTURE_4
: {
909 /* Grab RA results */
910 struct phys_reg dest
= index_to_reg(ctx
, g
, args
.dest
);
911 struct phys_reg coord
= index_to_reg(ctx
, g
, args
.src
[0]);
912 struct phys_reg lod
= index_to_reg(ctx
, g
, args
.src
[1]);
914 assert(dest
.reg
== 28 || dest
.reg
== 29);
915 assert(coord
.reg
== 28 || coord
.reg
== 29);
917 /* First, install the texture coordinate */
918 ins
->texture
.in_reg_full
= 1;
919 ins
->texture
.in_reg_upper
= 0;
920 ins
->texture
.in_reg_select
= coord
.reg
- 28;
921 ins
->texture
.in_reg_swizzle
=
922 compose_swizzle(ins
->texture
.in_reg_swizzle
, 0xF, coord
, dest
);
924 /* Next, install the destination */
925 ins
->texture
.out_full
= 1;
926 ins
->texture
.out_upper
= 0;
927 ins
->texture
.out_reg_select
= dest
.reg
- 28;
928 ins
->texture
.swizzle
=
929 compose_swizzle(ins
->texture
.swizzle
, dest
.mask
, dest
, dest
);
931 compose_writemask(ins
->mask
, dest
);
933 /* If there is a register LOD/bias, use it */
934 if (args
.src
[1] > -1) {
935 midgard_tex_register_select sel
= {
938 .component
= lod
.swizzle
& 3,
942 memcpy(&packed
, &sel
, sizeof(packed
));
943 ins
->texture
.bias
= packed
;
955 install_registers(compiler_context
*ctx
, struct ra_graph
*g
)
957 mir_foreach_block(ctx
, block
) {
958 mir_foreach_instr_in_block(block
, ins
) {
959 install_registers_instr(ctx
, g
, ins
);