2 * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "midgard_ops.h"
27 void mir_rewrite_index_src_single(midgard_instruction
*ins
, unsigned old
, unsigned new)
29 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
->src
); ++i
) {
30 if (ins
->src
[i
] == old
)
35 void mir_rewrite_index_dst_single(midgard_instruction
*ins
, unsigned old
, unsigned new)
41 static midgard_vector_alu_src
42 mir_get_alu_src(midgard_instruction
*ins
, unsigned idx
)
44 unsigned b
= (idx
== 0) ? ins
->alu
.src1
: ins
->alu
.src2
;
45 return vector_alu_from_unsigned(b
);
49 mir_rewrite_index_src_single_swizzle(midgard_instruction
*ins
, unsigned old
, unsigned new, unsigned *swizzle
)
51 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
->src
); ++i
) {
52 if (ins
->src
[i
] != old
) continue;
55 mir_compose_swizzle(ins
->swizzle
[i
], swizzle
, ins
->swizzle
[i
]);
60 mir_rewrite_index_src(compiler_context
*ctx
, unsigned old
, unsigned new)
62 mir_foreach_instr_global(ctx
, ins
) {
63 mir_rewrite_index_src_single(ins
, old
, new);
68 mir_rewrite_index_src_swizzle(compiler_context
*ctx
, unsigned old
, unsigned new, unsigned *swizzle
)
70 mir_foreach_instr_global(ctx
, ins
) {
71 mir_rewrite_index_src_single_swizzle(ins
, old
, new, swizzle
);
76 mir_rewrite_index_dst(compiler_context
*ctx
, unsigned old
, unsigned new)
78 mir_foreach_instr_global(ctx
, ins
) {
79 mir_rewrite_index_dst_single(ins
, old
, new);
84 mir_rewrite_index(compiler_context
*ctx
, unsigned old
, unsigned new)
86 mir_rewrite_index_src(ctx
, old
, new);
87 mir_rewrite_index_dst(ctx
, old
, new);
91 mir_use_count(compiler_context
*ctx
, unsigned value
)
93 unsigned used_count
= 0;
95 mir_foreach_instr_global(ctx
, ins
) {
96 if (mir_has_arg(ins
, value
))
103 /* Checks if a value is used only once (or totally dead), which is an important
104 * heuristic to figure out if certain optimizations are Worth It (TM) */
107 mir_single_use(compiler_context
*ctx
, unsigned value
)
109 /* We can replicate constants in places so who cares */
110 if (value
== SSA_FIXED_REGISTER(REGISTER_CONSTANT
))
113 return mir_use_count(ctx
, value
) <= 1;
117 mir_nontrivial_raw_mod(midgard_vector_alu_src src
, bool is_int
)
120 return src
.mod
== midgard_int_shift
;
126 mir_nontrivial_mod(midgard_vector_alu_src src
, bool is_int
, unsigned mask
, unsigned *swizzle
)
128 if (mir_nontrivial_raw_mod(src
, is_int
)) return true;
130 /* size-conversion */
131 if (src
.half
) return true;
133 for (unsigned c
= 0; c
< 16; ++c
) {
134 if (!(mask
& (1 << c
))) continue;
135 if (swizzle
[c
] != c
) return true;
142 mir_nontrivial_source2_mod(midgard_instruction
*ins
)
144 bool is_int
= midgard_is_integer_op(ins
->alu
.op
);
146 midgard_vector_alu_src src2
=
147 vector_alu_from_unsigned(ins
->alu
.src2
);
149 return mir_nontrivial_mod(src2
, is_int
, ins
->mask
, ins
->swizzle
[1]);
153 mir_nontrivial_source2_mod_simple(midgard_instruction
*ins
)
155 bool is_int
= midgard_is_integer_op(ins
->alu
.op
);
157 midgard_vector_alu_src src2
=
158 vector_alu_from_unsigned(ins
->alu
.src2
);
160 return mir_nontrivial_raw_mod(src2
, is_int
) || src2
.half
;
164 mir_nontrivial_outmod(midgard_instruction
*ins
)
166 bool is_int
= midgard_is_integer_op(ins
->alu
.op
);
167 unsigned mod
= ins
->alu
.outmod
;
169 /* Type conversion is a sort of outmod */
170 if (ins
->alu
.dest_override
!= midgard_dest_override_none
)
174 return mod
!= midgard_outmod_int_wrap
;
176 return mod
!= midgard_outmod_none
;
179 /* Checks if an index will be used as a special register -- basically, if we're
180 * used as the input to a non-ALU op */
183 mir_special_index(compiler_context
*ctx
, unsigned idx
)
185 mir_foreach_instr_global(ctx
, ins
) {
186 bool is_ldst
= ins
->type
== TAG_LOAD_STORE_4
;
187 bool is_tex
= ins
->type
== TAG_TEXTURE_4
;
188 bool is_writeout
= ins
->compact_branch
&& ins
->writeout
;
190 if (!(is_ldst
|| is_tex
|| is_writeout
))
193 if (mir_has_arg(ins
, idx
))
200 /* Grabs the type size. */
203 mir_typesize(midgard_instruction
*ins
)
205 if (ins
->compact_branch
)
206 return midgard_reg_mode_32
;
208 /* TODO: Type sizes for texture */
209 if (ins
->type
== TAG_TEXTURE_4
)
210 return midgard_reg_mode_32
;
212 if (ins
->type
== TAG_LOAD_STORE_4
)
213 return GET_LDST_SIZE(load_store_opcode_props
[ins
->load_store
.op
].props
);
215 if (ins
->type
== TAG_ALU_4
) {
216 midgard_reg_mode mode
= ins
->alu
.reg_mode
;
218 /* If we have an override, step down by half */
219 if (ins
->alu
.dest_override
!= midgard_dest_override_none
) {
220 assert(mode
> midgard_reg_mode_8
);
227 unreachable("Invalid instruction type");
230 /* Grabs the size of a source */
233 mir_srcsize(midgard_instruction
*ins
, unsigned i
)
235 if (ins
->type
== TAG_LOAD_STORE_4
) {
236 if (OP_HAS_ADDRESS(ins
->load_store
.op
)) {
238 return midgard_reg_mode_64
;
240 bool zext
= ins
->load_store
.arg_1
& 0x80;
241 return zext
? midgard_reg_mode_32
: midgard_reg_mode_64
;
246 /* TODO: 16-bit textures/ldst */
247 if (ins
->type
== TAG_TEXTURE_4
|| ins
->type
== TAG_LOAD_STORE_4
)
248 return midgard_reg_mode_32
;
250 /* TODO: 16-bit branches */
251 if (ins
->compact_branch
)
252 return midgard_reg_mode_32
;
255 /* TODO: 16-bit conditions, ffma */
256 return midgard_reg_mode_32
;
259 /* Default to type of the instruction */
261 midgard_reg_mode mode
= ins
->alu
.reg_mode
;
263 /* If we have a half modifier, step down by half */
265 if ((mir_get_alu_src(ins
, i
)).half
) {
266 assert(mode
> midgard_reg_mode_8
);
274 mir_mode_for_destsize(unsigned size
)
278 return midgard_reg_mode_8
;
280 return midgard_reg_mode_16
;
282 return midgard_reg_mode_32
;
284 return midgard_reg_mode_64
;
286 unreachable("Unknown destination size");
290 /* ...and the inverse */
293 mir_bytes_for_mode(midgard_reg_mode mode
)
296 case midgard_reg_mode_8
:
298 case midgard_reg_mode_16
:
300 case midgard_reg_mode_32
:
302 case midgard_reg_mode_64
:
305 unreachable("Invalid register mode");
310 mir_from_bytemask(uint16_t bytemask
, midgard_reg_mode mode
)
313 unsigned count
= mir_bytes_for_mode(mode
);
315 for (unsigned c
= 0, d
= 0; c
< 16; c
+= count
, ++d
) {
316 bool a
= (bytemask
& (1 << c
)) != 0;
318 for (unsigned q
= c
; q
< count
; ++q
)
319 assert(((bytemask
& (1 << q
)) != 0) == a
);
327 /* Rounds up a bytemask to fill a given component count. Iterate each
328 * component, and check if any bytes in the component are masked on */
331 mir_round_bytemask_up(uint16_t mask
, midgard_reg_mode mode
)
333 unsigned bytes
= mir_bytes_for_mode(mode
);
334 unsigned maxmask
= mask_of(bytes
);
335 unsigned channels
= 16 / bytes
;
337 for (unsigned c
= 0; c
< channels
; ++c
) {
338 unsigned submask
= maxmask
<< (c
* bytes
);
347 /* Grabs the per-byte mask of an instruction (as opposed to per-component) */
350 mir_bytemask(midgard_instruction
*ins
)
352 return pan_to_bytemask(mir_bytes_for_mode(mir_typesize(ins
)) * 8, ins
->mask
);
356 mir_set_bytemask(midgard_instruction
*ins
, uint16_t bytemask
)
358 ins
->mask
= mir_from_bytemask(bytemask
, mir_typesize(ins
));
361 /* Checks if we should use an upper destination override, rather than the lower
362 * one in the IR. Returns zero if no, returns the bytes to shift otherwise */
365 mir_upper_override(midgard_instruction
*ins
)
367 /* If there is no override, there is no upper override, tautology */
368 if (ins
->alu
.dest_override
== midgard_dest_override_none
)
371 /* Make sure we didn't already lower somehow */
372 assert(ins
->alu
.dest_override
== midgard_dest_override_lower
);
374 /* What is the mask in terms of currently? */
375 midgard_reg_mode type
= mir_typesize(ins
);
377 /* There are 16 bytes per vector, so there are (16/bytes)
378 * components per vector. So the magic half is half of
379 * (16/bytes), which simplifies to 8/bytes */
381 unsigned threshold
= 8 / mir_bytes_for_mode(type
);
383 /* How many components did we shift over? */
384 unsigned zeroes
= __builtin_ctz(ins
->mask
);
386 /* Did we hit the threshold? */
387 return (zeroes
>= threshold
) ? threshold
: 0;
390 /* Creates a mask of the components of a node read by an instruction, by
391 * analyzing the swizzle with respect to the instruction's mask. E.g.:
393 * fadd r0.xz, r1.yyyy, r2.zwyx
395 * will return a mask of Z/Y for r2
399 mir_bytemask_of_read_components_single(unsigned *swizzle
, unsigned inmask
, midgard_reg_mode mode
)
403 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
404 if (!(inmask
& (1 << c
))) continue;
405 cmask
|= (1 << swizzle
[c
]);
408 return pan_to_bytemask(mir_bytes_for_mode(mode
) * 8, cmask
);
412 mir_bytemask_of_read_components_index(midgard_instruction
*ins
, unsigned i
)
414 if (ins
->compact_branch
&& ins
->writeout
&& (i
== 0)) {
415 /* Non-ZS writeout uses all components */
416 if (!ins
->writeout_depth
&& !ins
->writeout_stencil
)
419 /* For ZS-writeout, if both Z and S are written we need two
420 * components, otherwise we only need one.
422 if (ins
->writeout_depth
&& ins
->writeout_stencil
)
428 /* Conditional branches read one 32-bit component = 4 bytes (TODO: multi branch??) */
429 if (ins
->compact_branch
&& ins
->branch
.conditional
&& (i
== 0))
432 /* ALU ops act componentwise so we need to pay attention to
433 * their mask. Texture/ldst does not so we don't clamp source
434 * readmasks based on the writemask */
435 unsigned qmask
= (ins
->type
== TAG_ALU_4
) ? ins
->mask
: ~0;
437 /* Handle dot products and things */
438 if (ins
->type
== TAG_ALU_4
&& !ins
->compact_branch
) {
439 unsigned props
= alu_opcode_props
[ins
->alu
.op
].props
;
441 unsigned channel_override
= GET_CHANNEL_COUNT(props
);
443 if (channel_override
)
444 qmask
= mask_of(channel_override
);
447 return mir_bytemask_of_read_components_single(ins
->swizzle
[i
], qmask
, mir_srcsize(ins
, i
));
451 mir_bytemask_of_read_components(midgard_instruction
*ins
, unsigned node
)
458 mir_foreach_src(ins
, i
) {
459 if (ins
->src
[i
] != node
) continue;
460 mask
|= mir_bytemask_of_read_components_index(ins
, i
);
466 /* Register allocation occurs after instruction scheduling, which is fine until
467 * we start needing to spill registers and therefore insert instructions into
468 * an already-scheduled program. We don't have to be terribly efficient about
469 * this, since spilling is already slow. So just semantically we need to insert
470 * the instruction into a new bundle before/after the bundle of the instruction
473 static midgard_bundle
474 mir_bundle_for_op(compiler_context
*ctx
, midgard_instruction ins
)
476 midgard_instruction
*u
= mir_upload_ins(ctx
, ins
);
478 midgard_bundle bundle
= {
480 .instruction_count
= 1,
481 .instructions
= { u
},
484 if (bundle
.tag
== TAG_ALU_4
) {
485 assert(OP_IS_MOVE(u
->alu
.op
));
488 size_t bytes_emitted
= sizeof(uint32_t) + sizeof(midgard_reg_info
) + sizeof(midgard_vector_alu
);
489 bundle
.padding
= ~(bytes_emitted
- 1) & 0xF;
490 bundle
.control
= ins
.type
| u
->unit
;
497 mir_bundle_idx_for_ins(midgard_instruction
*tag
, midgard_block
*block
)
499 midgard_bundle
*bundles
=
500 (midgard_bundle
*) block
->bundles
.data
;
502 size_t count
= (block
->bundles
.size
/ sizeof(midgard_bundle
));
504 for (unsigned i
= 0; i
< count
; ++i
) {
505 for (unsigned j
= 0; j
< bundles
[i
].instruction_count
; ++j
) {
506 if (bundles
[i
].instructions
[j
] == tag
)
511 mir_print_instruction(tag
);
512 unreachable("Instruction not scheduled in block");
516 mir_insert_instruction_before_scheduled(
517 compiler_context
*ctx
,
518 midgard_block
*block
,
519 midgard_instruction
*tag
,
520 midgard_instruction ins
)
522 unsigned before
= mir_bundle_idx_for_ins(tag
, block
);
523 size_t count
= util_dynarray_num_elements(&block
->bundles
, midgard_bundle
);
524 UNUSED
void *unused
= util_dynarray_grow(&block
->bundles
, midgard_bundle
, 1);
526 midgard_bundle
*bundles
= (midgard_bundle
*) block
->bundles
.data
;
527 memmove(bundles
+ before
+ 1, bundles
+ before
, (count
- before
) * sizeof(midgard_bundle
));
528 midgard_bundle
*before_bundle
= bundles
+ before
+ 1;
530 midgard_bundle
new = mir_bundle_for_op(ctx
, ins
);
531 memcpy(bundles
+ before
, &new, sizeof(new));
533 list_addtail(&new.instructions
[0]->link
, &before_bundle
->instructions
[0]->link
);
534 block
->quadword_count
+= midgard_tag_props
[new.tag
].size
;
538 mir_insert_instruction_after_scheduled(
539 compiler_context
*ctx
,
540 midgard_block
*block
,
541 midgard_instruction
*tag
,
542 midgard_instruction ins
)
544 /* We need to grow the bundles array to add our new bundle */
545 size_t count
= util_dynarray_num_elements(&block
->bundles
, midgard_bundle
);
546 UNUSED
void *unused
= util_dynarray_grow(&block
->bundles
, midgard_bundle
, 1);
548 /* Find the bundle that we want to insert after */
549 unsigned after
= mir_bundle_idx_for_ins(tag
, block
);
551 /* All the bundles after that one, we move ahead by one */
552 midgard_bundle
*bundles
= (midgard_bundle
*) block
->bundles
.data
;
553 memmove(bundles
+ after
+ 2, bundles
+ after
+ 1, (count
- after
- 1) * sizeof(midgard_bundle
));
554 midgard_bundle
*after_bundle
= bundles
+ after
;
556 midgard_bundle
new = mir_bundle_for_op(ctx
, ins
);
557 memcpy(bundles
+ after
+ 1, &new, sizeof(new));
558 list_add(&new.instructions
[0]->link
, &after_bundle
->instructions
[after_bundle
->instruction_count
- 1]->link
);
559 block
->quadword_count
+= midgard_tag_props
[new.tag
].size
;
562 /* Flip the first-two arguments of a (binary) op. Currently ALU
563 * only, no known uses for ldst/tex */
566 mir_flip(midgard_instruction
*ins
)
568 unsigned temp
= ins
->src
[0];
569 ins
->src
[0] = ins
->src
[1];
572 assert(ins
->type
== TAG_ALU_4
);
574 temp
= ins
->alu
.src1
;
575 ins
->alu
.src1
= ins
->alu
.src2
;
576 ins
->alu
.src2
= temp
;
578 unsigned temp_swizzle
[16];
579 memcpy(temp_swizzle
, ins
->swizzle
[0], sizeof(ins
->swizzle
[0]));
580 memcpy(ins
->swizzle
[0], ins
->swizzle
[1], sizeof(ins
->swizzle
[0]));
581 memcpy(ins
->swizzle
[1], temp_swizzle
, sizeof(ins
->swizzle
[0]));
584 /* Before squashing, calculate ctx->temp_count just by observing the MIR */
587 mir_compute_temp_count(compiler_context
*ctx
)
592 unsigned max_dest
= 0;
594 mir_foreach_instr_global(ctx
, ins
) {
595 if (ins
->dest
< SSA_FIXED_MINIMUM
)
596 max_dest
= MAX2(max_dest
, ins
->dest
+ 1);
599 ctx
->temp_count
= max_dest
;