pan/midgard: Generalize IS_ALU and quadword_size
[mesa.git] / src / panfrost / midgard / mir.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25 #include "midgard_ops.h"
26
27 void mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old, unsigned new)
28 {
29 for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i) {
30 if (ins->src[i] == old)
31 ins->src[i] = new;
32 }
33 }
34
35 void mir_rewrite_index_dst_single(midgard_instruction *ins, unsigned old, unsigned new)
36 {
37 if (ins->dest == old)
38 ins->dest = new;
39 }
40
41 static midgard_vector_alu_src
42 mir_get_alu_src(midgard_instruction *ins, unsigned idx)
43 {
44 unsigned b = (idx == 0) ? ins->alu.src1 : ins->alu.src2;
45 return vector_alu_from_unsigned(b);
46 }
47
48 static void
49 mir_rewrite_index_src_single_swizzle(midgard_instruction *ins, unsigned old, unsigned new, unsigned *swizzle)
50 {
51 for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i) {
52 if (ins->src[i] != old) continue;
53
54 ins->src[i] = new;
55 mir_compose_swizzle(ins->swizzle[i], swizzle, ins->swizzle[i]);
56 }
57 }
58
59 void
60 mir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new)
61 {
62 mir_foreach_instr_global(ctx, ins) {
63 mir_rewrite_index_src_single(ins, old, new);
64 }
65 }
66
67 void
68 mir_rewrite_index_src_swizzle(compiler_context *ctx, unsigned old, unsigned new, unsigned *swizzle)
69 {
70 mir_foreach_instr_global(ctx, ins) {
71 mir_rewrite_index_src_single_swizzle(ins, old, new, swizzle);
72 }
73 }
74
75 void
76 mir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new)
77 {
78 mir_foreach_instr_global(ctx, ins) {
79 mir_rewrite_index_dst_single(ins, old, new);
80 }
81 }
82
83 void
84 mir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new)
85 {
86 mir_rewrite_index_src(ctx, old, new);
87 mir_rewrite_index_dst(ctx, old, new);
88 }
89
90 unsigned
91 mir_use_count(compiler_context *ctx, unsigned value)
92 {
93 unsigned used_count = 0;
94
95 mir_foreach_instr_global(ctx, ins) {
96 if (mir_has_arg(ins, value))
97 ++used_count;
98 }
99
100 return used_count;
101 }
102
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) */
105
106 bool
107 mir_single_use(compiler_context *ctx, unsigned value)
108 {
109 /* We can replicate constants in places so who cares */
110 if (value == SSA_FIXED_REGISTER(REGISTER_CONSTANT))
111 return true;
112
113 return mir_use_count(ctx, value) <= 1;
114 }
115
116 static bool
117 mir_nontrivial_raw_mod(midgard_vector_alu_src src, bool is_int)
118 {
119 if (is_int)
120 return src.mod == midgard_int_shift;
121 else
122 return src.mod;
123 }
124
125 static bool
126 mir_nontrivial_mod(midgard_vector_alu_src src, bool is_int, unsigned mask, unsigned *swizzle)
127 {
128 if (mir_nontrivial_raw_mod(src, is_int)) return true;
129
130 /* size-conversion */
131 if (src.half) return true;
132
133 for (unsigned c = 0; c < 16; ++c) {
134 if (!(mask & (1 << c))) continue;
135 if (swizzle[c] != c) return true;
136 }
137
138 return false;
139 }
140
141 bool
142 mir_nontrivial_source2_mod(midgard_instruction *ins)
143 {
144 bool is_int = midgard_is_integer_op(ins->alu.op);
145
146 midgard_vector_alu_src src2 =
147 vector_alu_from_unsigned(ins->alu.src2);
148
149 return mir_nontrivial_mod(src2, is_int, ins->mask, ins->swizzle[1]);
150 }
151
152 bool
153 mir_nontrivial_source2_mod_simple(midgard_instruction *ins)
154 {
155 bool is_int = midgard_is_integer_op(ins->alu.op);
156
157 midgard_vector_alu_src src2 =
158 vector_alu_from_unsigned(ins->alu.src2);
159
160 return mir_nontrivial_raw_mod(src2, is_int) || src2.half;
161 }
162
163 bool
164 mir_nontrivial_outmod(midgard_instruction *ins)
165 {
166 bool is_int = midgard_is_integer_op(ins->alu.op);
167 unsigned mod = ins->alu.outmod;
168
169 /* Pseudo-outmod */
170 if (ins->invert)
171 return true;
172
173 /* Type conversion is a sort of outmod */
174 if (ins->alu.dest_override != midgard_dest_override_none)
175 return true;
176
177 if (is_int)
178 return mod != midgard_outmod_int_wrap;
179 else
180 return mod != midgard_outmod_none;
181 }
182
183 /* Checks if an index will be used as a special register -- basically, if we're
184 * used as the input to a non-ALU op */
185
186 bool
187 mir_special_index(compiler_context *ctx, unsigned idx)
188 {
189 mir_foreach_instr_global(ctx, ins) {
190 bool is_ldst = ins->type == TAG_LOAD_STORE_4;
191 bool is_tex = ins->type == TAG_TEXTURE_4;
192 bool is_writeout = ins->compact_branch && ins->writeout;
193
194 if (!(is_ldst || is_tex || is_writeout))
195 continue;
196
197 if (mir_has_arg(ins, idx))
198 return true;
199 }
200
201 return false;
202 }
203
204 /* Is a node written before a given instruction? */
205
206 bool
207 mir_is_written_before(compiler_context *ctx, midgard_instruction *ins, unsigned node)
208 {
209 if (node >= SSA_FIXED_MINIMUM)
210 return true;
211
212 mir_foreach_instr_global(ctx, q) {
213 if (q == ins)
214 break;
215
216 if (q->dest == node)
217 return true;
218 }
219
220 return false;
221 }
222
223 /* Grabs the type size. */
224
225 midgard_reg_mode
226 mir_typesize(midgard_instruction *ins)
227 {
228 if (ins->compact_branch)
229 return midgard_reg_mode_32;
230
231 /* TODO: Type sizes for texture */
232 if (ins->type == TAG_TEXTURE_4)
233 return midgard_reg_mode_32;
234
235 if (ins->type == TAG_LOAD_STORE_4)
236 return GET_LDST_SIZE(load_store_opcode_props[ins->load_store.op].props);
237
238 if (ins->type == TAG_ALU_4) {
239 midgard_reg_mode mode = ins->alu.reg_mode;
240
241 /* If we have an override, step down by half */
242 if (ins->alu.dest_override != midgard_dest_override_none) {
243 assert(mode > midgard_reg_mode_8);
244 mode--;
245 }
246
247 return mode;
248 }
249
250 unreachable("Invalid instruction type");
251 }
252
253 /* Grabs the size of a source */
254
255 midgard_reg_mode
256 mir_srcsize(midgard_instruction *ins, unsigned i)
257 {
258 /* TODO: 16-bit textures/ldst */
259 if (ins->type == TAG_TEXTURE_4 || ins->type == TAG_LOAD_STORE_4)
260 return midgard_reg_mode_32;
261
262 /* TODO: 16-bit branches */
263 if (ins->compact_branch)
264 return midgard_reg_mode_32;
265
266 if (i >= 2) {
267 /* TODO: 16-bit conditions, ffma */
268 return midgard_reg_mode_32;
269 }
270
271 /* Default to type of the instruction */
272
273 midgard_reg_mode mode = ins->alu.reg_mode;
274
275 /* If we have a half modifier, step down by half */
276
277 if ((mir_get_alu_src(ins, i)).half) {
278 assert(mode > midgard_reg_mode_8);
279 mode--;
280 }
281
282 return mode;
283 }
284
285 midgard_reg_mode
286 mir_mode_for_destsize(unsigned size)
287 {
288 switch (size) {
289 case 8:
290 return midgard_reg_mode_8;
291 case 16:
292 return midgard_reg_mode_16;
293 case 32:
294 return midgard_reg_mode_32;
295 case 64:
296 return midgard_reg_mode_64;
297 default:
298 unreachable("Unknown destination size");
299 }
300 }
301
302
303 /* Converts per-component mask to a byte mask */
304
305 uint16_t
306 mir_to_bytemask(midgard_reg_mode mode, unsigned mask)
307 {
308 switch (mode) {
309 case midgard_reg_mode_8:
310 return mask;
311
312 case midgard_reg_mode_16: {
313 unsigned space =
314 (mask & 0x1) |
315 ((mask & 0x2) << (2 - 1)) |
316 ((mask & 0x4) << (4 - 2)) |
317 ((mask & 0x8) << (6 - 3)) |
318 ((mask & 0x10) << (8 - 4)) |
319 ((mask & 0x20) << (10 - 5)) |
320 ((mask & 0x40) << (12 - 6)) |
321 ((mask & 0x80) << (14 - 7));
322
323 return space | (space << 1);
324 }
325
326 case midgard_reg_mode_32: {
327 unsigned space =
328 (mask & 0x1) |
329 ((mask & 0x2) << (4 - 1)) |
330 ((mask & 0x4) << (8 - 2)) |
331 ((mask & 0x8) << (12 - 3));
332
333 return space | (space << 1) | (space << 2) | (space << 3);
334 }
335
336 case midgard_reg_mode_64: {
337 unsigned A = (mask & 0x1) ? 0xFF : 0x00;
338 unsigned B = (mask & 0x2) ? 0xFF : 0x00;
339 return A | (B << 8);
340 }
341
342 default:
343 unreachable("Invalid register mode");
344 }
345 }
346
347 /* ...and the inverse */
348
349 unsigned
350 mir_bytes_for_mode(midgard_reg_mode mode)
351 {
352 switch (mode) {
353 case midgard_reg_mode_8:
354 return 1;
355 case midgard_reg_mode_16:
356 return 2;
357 case midgard_reg_mode_32:
358 return 4;
359 case midgard_reg_mode_64:
360 return 8;
361 default:
362 unreachable("Invalid register mode");
363 }
364 }
365
366 uint16_t
367 mir_from_bytemask(uint16_t bytemask, midgard_reg_mode mode)
368 {
369 unsigned value = 0;
370 unsigned count = mir_bytes_for_mode(mode);
371
372 for (unsigned c = 0, d = 0; c < 16; c += count, ++d) {
373 bool a = (bytemask & (1 << c)) != 0;
374
375 for (unsigned q = c; q < count; ++q)
376 assert(((bytemask & (1 << q)) != 0) == a);
377
378 value |= (a << d);
379 }
380
381 return value;
382 }
383
384 /* Rounds down a bytemask to fit a given component count. Iterate each
385 * component, and check if all bytes in the component are masked on */
386
387 uint16_t
388 mir_round_bytemask_down(uint16_t mask, midgard_reg_mode mode)
389 {
390 unsigned bytes = mir_bytes_for_mode(mode);
391 unsigned maxmask = mask_of(bytes);
392 unsigned channels = 16 / bytes;
393
394 for (unsigned c = 0; c < channels; ++c) {
395 /* Get bytes in component */
396 unsigned submask = (mask >> (c * bytes)) & maxmask;
397
398 if (submask != maxmask)
399 mask &= ~(maxmask << (c * bytes));
400 }
401
402 return mask;
403 }
404
405 /* Grabs the per-byte mask of an instruction (as opposed to per-component) */
406
407 uint16_t
408 mir_bytemask(midgard_instruction *ins)
409 {
410 return mir_to_bytemask(mir_typesize(ins), ins->mask);
411 }
412
413 void
414 mir_set_bytemask(midgard_instruction *ins, uint16_t bytemask)
415 {
416 ins->mask = mir_from_bytemask(bytemask, mir_typesize(ins));
417 }
418
419 /* Checks if we should use an upper destination override, rather than the lower
420 * one in the IR. Returns zero if no, returns the bytes to shift otherwise */
421
422 unsigned
423 mir_upper_override(midgard_instruction *ins)
424 {
425 /* If there is no override, there is no upper override, tautology */
426 if (ins->alu.dest_override == midgard_dest_override_none)
427 return 0;
428
429 /* Make sure we didn't already lower somehow */
430 assert(ins->alu.dest_override == midgard_dest_override_lower);
431
432 /* What is the mask in terms of currently? */
433 midgard_reg_mode type = mir_typesize(ins);
434
435 /* There are 16 bytes per vector, so there are (16/bytes)
436 * components per vector. So the magic half is half of
437 * (16/bytes), which simplifies to 8/bytes */
438
439 unsigned threshold = 8 / mir_bytes_for_mode(type);
440
441 /* How many components did we shift over? */
442 unsigned zeroes = __builtin_ctz(ins->mask);
443
444 /* Did we hit the threshold? */
445 return (zeroes >= threshold) ? threshold : 0;
446 }
447
448 /* Creates a mask of the components of a node read by an instruction, by
449 * analyzing the swizzle with respect to the instruction's mask. E.g.:
450 *
451 * fadd r0.xz, r1.yyyy, r2.zwyx
452 *
453 * will return a mask of Z/Y for r2
454 */
455
456 static uint16_t
457 mir_bytemask_of_read_components_single(unsigned *swizzle, unsigned inmask, midgard_reg_mode mode)
458 {
459 unsigned cmask = 0;
460
461 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
462 if (!(inmask & (1 << c))) continue;
463 cmask |= (1 << swizzle[c]);
464 }
465
466 return mir_to_bytemask(mode, cmask);
467 }
468
469 uint16_t
470 mir_bytemask_of_read_components(midgard_instruction *ins, unsigned node)
471 {
472 uint16_t mask = 0;
473
474 if (node == ~0)
475 return 0;
476
477 mir_foreach_src(ins, i) {
478 if (ins->src[i] != node) continue;
479
480 /* Branch writeout uses all components */
481 if (ins->compact_branch && ins->writeout && (i == 0))
482 return 0xFFFF;
483
484 /* Conditional branches read one 32-bit component = 4 bytes (TODO: multi branch??) */
485 if (ins->compact_branch && ins->branch.conditional && (i == 0))
486 return 0xF;
487
488 /* ALU ops act componentwise so we need to pay attention to
489 * their mask. Texture/ldst does not so we don't clamp source
490 * readmasks based on the writemask */
491 unsigned qmask = (ins->type == TAG_ALU_4) ? ins->mask : ~0;
492
493 /* Handle dot products and things */
494 if (ins->type == TAG_ALU_4 && !ins->compact_branch) {
495 unsigned props = alu_opcode_props[ins->alu.op].props;
496
497 unsigned channel_override = GET_CHANNEL_COUNT(props);
498
499 if (channel_override)
500 qmask = mask_of(channel_override);
501 }
502
503 mask |= mir_bytemask_of_read_components_single(ins->swizzle[i], qmask, mir_srcsize(ins, i));
504 }
505
506 return mask;
507 }
508
509 /* Register allocation occurs after instruction scheduling, which is fine until
510 * we start needing to spill registers and therefore insert instructions into
511 * an already-scheduled program. We don't have to be terribly efficient about
512 * this, since spilling is already slow. So just semantically we need to insert
513 * the instruction into a new bundle before/after the bundle of the instruction
514 * in question */
515
516 static midgard_bundle
517 mir_bundle_for_op(compiler_context *ctx, midgard_instruction ins)
518 {
519 midgard_instruction *u = mir_upload_ins(ctx, ins);
520
521 midgard_bundle bundle = {
522 .tag = ins.type,
523 .instruction_count = 1,
524 .instructions = { u },
525 };
526
527 if (bundle.tag == TAG_ALU_4) {
528 assert(OP_IS_MOVE(u->alu.op));
529 u->unit = UNIT_VMUL;
530
531 size_t bytes_emitted = sizeof(uint32_t) + sizeof(midgard_reg_info) + sizeof(midgard_vector_alu);
532 bundle.padding = ~(bytes_emitted - 1) & 0xF;
533 bundle.control = ins.type | u->unit;
534 }
535
536 return bundle;
537 }
538
539 static unsigned
540 mir_bundle_idx_for_ins(midgard_instruction *tag, midgard_block *block)
541 {
542 midgard_bundle *bundles =
543 (midgard_bundle *) block->bundles.data;
544
545 size_t count = (block->bundles.size / sizeof(midgard_bundle));
546
547 for (unsigned i = 0; i < count; ++i) {
548 for (unsigned j = 0; j < bundles[i].instruction_count; ++j) {
549 if (bundles[i].instructions[j] == tag)
550 return i;
551 }
552 }
553
554 mir_print_instruction(tag);
555 unreachable("Instruction not scheduled in block");
556 }
557
558 void
559 mir_insert_instruction_before_scheduled(
560 compiler_context *ctx,
561 midgard_block *block,
562 midgard_instruction *tag,
563 midgard_instruction ins)
564 {
565 unsigned before = mir_bundle_idx_for_ins(tag, block);
566 size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
567 UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
568
569 midgard_bundle *bundles = (midgard_bundle *) block->bundles.data;
570 memmove(bundles + before + 1, bundles + before, (count - before) * sizeof(midgard_bundle));
571 midgard_bundle *before_bundle = bundles + before + 1;
572
573 midgard_bundle new = mir_bundle_for_op(ctx, ins);
574 memcpy(bundles + before, &new, sizeof(new));
575
576 list_addtail(&new.instructions[0]->link, &before_bundle->instructions[0]->link);
577 block->quadword_count += midgard_word_size[new.tag];
578 }
579
580 void
581 mir_insert_instruction_after_scheduled(
582 compiler_context *ctx,
583 midgard_block *block,
584 midgard_instruction *tag,
585 midgard_instruction ins)
586 {
587 /* We need to grow the bundles array to add our new bundle */
588 size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
589 UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
590
591 /* Find the bundle that we want to insert after */
592 unsigned after = mir_bundle_idx_for_ins(tag, block);
593
594 /* All the bundles after that one, we move ahead by one */
595 midgard_bundle *bundles = (midgard_bundle *) block->bundles.data;
596 memmove(bundles + after + 2, bundles + after + 1, (count - after - 1) * sizeof(midgard_bundle));
597 midgard_bundle *after_bundle = bundles + after;
598
599 midgard_bundle new = mir_bundle_for_op(ctx, ins);
600 memcpy(bundles + after + 1, &new, sizeof(new));
601 list_add(&new.instructions[0]->link, &after_bundle->instructions[after_bundle->instruction_count - 1]->link);
602 block->quadword_count += midgard_word_size[new.tag];
603 }
604
605 /* Flip the first-two arguments of a (binary) op. Currently ALU
606 * only, no known uses for ldst/tex */
607
608 void
609 mir_flip(midgard_instruction *ins)
610 {
611 unsigned temp = ins->src[0];
612 ins->src[0] = ins->src[1];
613 ins->src[1] = temp;
614
615 assert(ins->type == TAG_ALU_4);
616
617 temp = ins->alu.src1;
618 ins->alu.src1 = ins->alu.src2;
619 ins->alu.src2 = temp;
620
621 unsigned temp_swizzle[16];
622 memcpy(temp_swizzle, ins->swizzle[0], sizeof(ins->swizzle[0]));
623 memcpy(ins->swizzle[0], ins->swizzle[1], sizeof(ins->swizzle[0]));
624 memcpy(ins->swizzle[1], temp_swizzle, sizeof(ins->swizzle[0]));
625 }
626
627 /* Before squashing, calculate ctx->temp_count just by observing the MIR */
628
629 void
630 mir_compute_temp_count(compiler_context *ctx)
631 {
632 if (ctx->temp_count)
633 return;
634
635 unsigned max_dest = 0;
636
637 mir_foreach_instr_global(ctx, ins) {
638 if (ins->dest < SSA_FIXED_MINIMUM)
639 max_dest = MAX2(max_dest, ins->dest + 1);
640 }
641
642 ctx->temp_count = max_dest;
643 }