2c9119d9ccf4c72d9533b139b993ae7b173935b6
[mesa.git] / src / gallium / drivers / vc4 / vc4_qir.c
1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "util/u_memory.h"
25 #include "util/ralloc.h"
26
27 #include "vc4_qir.h"
28 #include "vc4_qpu.h"
29
30 struct qir_op_info {
31 const char *name;
32 uint8_t ndst, nsrc;
33 bool has_side_effects;
34 };
35
36 static const struct qir_op_info qir_op_info[] = {
37 [QOP_MOV] = { "mov", 1, 1 },
38 [QOP_FMOV] = { "fmov", 1, 1 },
39 [QOP_MMOV] = { "mmov", 1, 1 },
40 [QOP_FADD] = { "fadd", 1, 2 },
41 [QOP_FSUB] = { "fsub", 1, 2 },
42 [QOP_FMUL] = { "fmul", 1, 2 },
43 [QOP_MUL24] = { "mul24", 1, 2 },
44 [QOP_V8MULD] = {"v8muld", 1, 2 },
45 [QOP_V8MIN] = {"v8min", 1, 2 },
46 [QOP_V8MAX] = {"v8max", 1, 2 },
47 [QOP_V8ADDS] = {"v8adds", 1, 2 },
48 [QOP_V8SUBS] = {"v8subs", 1, 2 },
49 [QOP_FMIN] = { "fmin", 1, 2 },
50 [QOP_FMAX] = { "fmax", 1, 2 },
51 [QOP_FMINABS] = { "fminabs", 1, 2 },
52 [QOP_FMAXABS] = { "fmaxabs", 1, 2 },
53 [QOP_FTOI] = { "ftoi", 1, 1 },
54 [QOP_ITOF] = { "itof", 1, 1 },
55 [QOP_ADD] = { "add", 1, 2 },
56 [QOP_SUB] = { "sub", 1, 2 },
57 [QOP_SHR] = { "shr", 1, 2 },
58 [QOP_ASR] = { "asr", 1, 2 },
59 [QOP_SHL] = { "shl", 1, 2 },
60 [QOP_MIN] = { "min", 1, 2 },
61 [QOP_MAX] = { "max", 1, 2 },
62 [QOP_AND] = { "and", 1, 2 },
63 [QOP_OR] = { "or", 1, 2 },
64 [QOP_XOR] = { "xor", 1, 2 },
65 [QOP_NOT] = { "not", 1, 1 },
66
67 [QOP_RCP] = { "rcp", 1, 1 },
68 [QOP_RSQ] = { "rsq", 1, 1 },
69 [QOP_EXP2] = { "exp2", 1, 1 },
70 [QOP_LOG2] = { "log2", 1, 1 },
71 [QOP_TLB_COLOR_READ] = { "tlb_color_read", 1, 0 },
72 [QOP_MS_MASK] = { "ms_mask", 0, 1, true },
73 [QOP_VARY_ADD_C] = { "vary_add_c", 1, 1 },
74
75 [QOP_FRAG_Z] = { "frag_z", 1, 0 },
76 [QOP_FRAG_W] = { "frag_w", 1, 0 },
77
78 [QOP_TEX_S] = { "tex_s", 0, 2, true },
79 [QOP_TEX_T] = { "tex_t", 0, 2, true },
80 [QOP_TEX_R] = { "tex_r", 0, 2, true },
81 [QOP_TEX_B] = { "tex_b", 0, 2, true },
82 [QOP_TEX_DIRECT] = { "tex_direct", 0, 2, true },
83 [QOP_TEX_RESULT] = { "tex_result", 1, 0, true },
84
85 [QOP_THRSW] = { "thrsw", 0, 0, true },
86
87 [QOP_LOAD_IMM] = { "load_imm", 0, 1 },
88 [QOP_LOAD_IMM_U2] = { "load_imm_u2", 0, 1 },
89 [QOP_LOAD_IMM_I2] = { "load_imm_i2", 0, 1 },
90
91 [QOP_ROT_MUL] = { "rot_mul", 0, 2 },
92
93 [QOP_BRANCH] = { "branch", 0, 0, true },
94 [QOP_UNIFORMS_RESET] = { "uniforms_reset", 0, 2, true },
95 };
96
97 static const char *
98 qir_get_op_name(enum qop qop)
99 {
100 if (qop < ARRAY_SIZE(qir_op_info) && qir_op_info[qop].name)
101 return qir_op_info[qop].name;
102 else
103 return "???";
104 }
105
106 int
107 qir_get_nsrc(struct qinst *inst)
108 {
109 assert(qir_op_info[inst->op].name);
110 return qir_op_info[inst->op].nsrc;
111 }
112
113 /**
114 * Returns whether the instruction has any side effects that must be
115 * preserved.
116 */
117 bool
118 qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
119 {
120 switch (inst->dst.file) {
121 case QFILE_TLB_Z_WRITE:
122 case QFILE_TLB_COLOR_WRITE:
123 case QFILE_TLB_COLOR_WRITE_MS:
124 case QFILE_TLB_STENCIL_SETUP:
125 return true;
126 default:
127 break;
128 }
129
130 return qir_op_info[inst->op].has_side_effects;
131 }
132
133 bool
134 qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
135 {
136 /* We can dead-code eliminate varyings, because we only tell the VS
137 * about the live ones at the end. But we have to preserve the
138 * point/line coordinates reads, because they're generated by
139 * fixed-function hardware.
140 */
141 for (int i = 0; i < qir_get_nsrc(inst); i++) {
142 if (inst->src[i].file == QFILE_VARY &&
143 c->input_slots[inst->src[i].index].slot == 0xff) {
144 return true;
145 }
146
147 if (inst->src[i].file == QFILE_VPM)
148 return true;
149 }
150
151 if (inst->dst.file == QFILE_VPM)
152 return true;
153
154 return false;
155 }
156
157 bool
158 qir_is_mul(struct qinst *inst)
159 {
160 switch (inst->op) {
161 case QOP_MMOV:
162 case QOP_FMUL:
163 case QOP_MUL24:
164 case QOP_V8MULD:
165 case QOP_V8MIN:
166 case QOP_V8MAX:
167 case QOP_V8ADDS:
168 case QOP_V8SUBS:
169 case QOP_ROT_MUL:
170 return true;
171 default:
172 return false;
173 }
174 }
175
176 bool
177 qir_is_float_input(struct qinst *inst)
178 {
179 switch (inst->op) {
180 case QOP_FMOV:
181 case QOP_FMUL:
182 case QOP_FADD:
183 case QOP_FSUB:
184 case QOP_FMIN:
185 case QOP_FMAX:
186 case QOP_FMINABS:
187 case QOP_FMAXABS:
188 case QOP_FTOI:
189 return true;
190 default:
191 return false;
192 }
193 }
194
195 bool
196 qir_is_raw_mov(struct qinst *inst)
197 {
198 return ((inst->op == QOP_MOV ||
199 inst->op == QOP_FMOV ||
200 inst->op == QOP_MMOV) &&
201 inst->cond == QPU_COND_ALWAYS &&
202 !inst->dst.pack &&
203 !inst->src[0].pack);
204 }
205
206 bool
207 qir_is_tex(struct qinst *inst)
208 {
209 return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
210 }
211
212 bool
213 qir_depends_on_flags(struct qinst *inst)
214 {
215 if (inst->op == QOP_BRANCH) {
216 return inst->cond != QPU_COND_BRANCH_ALWAYS;
217 } else {
218 return (inst->cond != QPU_COND_ALWAYS &&
219 inst->cond != QPU_COND_NEVER);
220 }
221 }
222
223 bool
224 qir_writes_r4(struct qinst *inst)
225 {
226 switch (inst->op) {
227 case QOP_TEX_RESULT:
228 case QOP_TLB_COLOR_READ:
229 case QOP_RCP:
230 case QOP_RSQ:
231 case QOP_EXP2:
232 case QOP_LOG2:
233 return true;
234 default:
235 return false;
236 }
237 }
238
239 uint8_t
240 qir_channels_written(struct qinst *inst)
241 {
242 if (qir_is_mul(inst)) {
243 switch (inst->dst.pack) {
244 case QPU_PACK_MUL_NOP:
245 case QPU_PACK_MUL_8888:
246 return 0xf;
247 case QPU_PACK_MUL_8A:
248 return 0x1;
249 case QPU_PACK_MUL_8B:
250 return 0x2;
251 case QPU_PACK_MUL_8C:
252 return 0x4;
253 case QPU_PACK_MUL_8D:
254 return 0x8;
255 }
256 } else {
257 switch (inst->dst.pack) {
258 case QPU_PACK_A_NOP:
259 case QPU_PACK_A_8888:
260 case QPU_PACK_A_8888_SAT:
261 case QPU_PACK_A_32_SAT:
262 return 0xf;
263 case QPU_PACK_A_8A:
264 case QPU_PACK_A_8A_SAT:
265 return 0x1;
266 case QPU_PACK_A_8B:
267 case QPU_PACK_A_8B_SAT:
268 return 0x2;
269 case QPU_PACK_A_8C:
270 case QPU_PACK_A_8C_SAT:
271 return 0x4;
272 case QPU_PACK_A_8D:
273 case QPU_PACK_A_8D_SAT:
274 return 0x8;
275 case QPU_PACK_A_16A:
276 case QPU_PACK_A_16A_SAT:
277 return 0x3;
278 case QPU_PACK_A_16B:
279 case QPU_PACK_A_16B_SAT:
280 return 0xc;
281 }
282 }
283 unreachable("Bad pack field");
284 }
285
286 static void
287 qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
288 {
289 static const char *files[] = {
290 [QFILE_TEMP] = "t",
291 [QFILE_VARY] = "v",
292 [QFILE_UNIF] = "u",
293 [QFILE_TLB_COLOR_WRITE] = "tlb_c",
294 [QFILE_TLB_COLOR_WRITE_MS] = "tlb_c_ms",
295 [QFILE_TLB_Z_WRITE] = "tlb_z",
296 [QFILE_TLB_STENCIL_SETUP] = "tlb_stencil",
297 [QFILE_FRAG_X] = "frag_x",
298 [QFILE_FRAG_Y] = "frag_y",
299 [QFILE_FRAG_REV_FLAG] = "frag_rev_flag",
300 [QFILE_QPU_ELEMENT] = "elem",
301 };
302
303 switch (reg.file) {
304
305 case QFILE_NULL:
306 fprintf(stderr, "null");
307 break;
308
309 case QFILE_LOAD_IMM:
310 fprintf(stderr, "0x%08x (%f)", reg.index, uif(reg.index));
311 break;
312
313 case QFILE_SMALL_IMM:
314 if ((int)reg.index >= -16 && (int)reg.index <= 15)
315 fprintf(stderr, "%d", reg.index);
316 else
317 fprintf(stderr, "%f", uif(reg.index));
318 break;
319
320 case QFILE_VPM:
321 if (write) {
322 fprintf(stderr, "vpm");
323 } else {
324 fprintf(stderr, "vpm%d.%d",
325 reg.index / 4, reg.index % 4);
326 }
327 break;
328
329 case QFILE_TLB_COLOR_WRITE:
330 case QFILE_TLB_COLOR_WRITE_MS:
331 case QFILE_TLB_Z_WRITE:
332 case QFILE_TLB_STENCIL_SETUP:
333 fprintf(stderr, "%s", files[reg.file]);
334 break;
335
336 default:
337 fprintf(stderr, "%s%d", files[reg.file], reg.index);
338 break;
339 }
340
341 if (reg.file == QFILE_UNIF &&
342 c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
343 fprintf(stderr, " (0x%08x / %f)",
344 c->uniform_data[reg.index],
345 uif(c->uniform_data[reg.index]));
346 }
347 }
348
349 void
350 qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
351 {
352 fprintf(stderr, "%s", qir_get_op_name(inst->op));
353 if (inst->op == QOP_BRANCH)
354 vc4_qpu_disasm_cond_branch(stderr, inst->cond);
355 else
356 vc4_qpu_disasm_cond(stderr, inst->cond);
357 if (inst->sf)
358 fprintf(stderr, ".sf");
359 fprintf(stderr, " ");
360
361 if (inst->op != QOP_BRANCH) {
362 qir_print_reg(c, inst->dst, true);
363 if (inst->dst.pack) {
364 if (inst->dst.pack) {
365 if (qir_is_mul(inst))
366 vc4_qpu_disasm_pack_mul(stderr, inst->dst.pack);
367 else
368 vc4_qpu_disasm_pack_a(stderr, inst->dst.pack);
369 }
370 }
371 }
372
373 for (int i = 0; i < qir_get_nsrc(inst); i++) {
374 fprintf(stderr, ", ");
375 qir_print_reg(c, inst->src[i], false);
376 vc4_qpu_disasm_unpack(stderr, inst->src[i].pack);
377 }
378 }
379
380 void
381 qir_dump(struct vc4_compile *c)
382 {
383 int ip = 0;
384 int pressure = 0;
385
386 qir_for_each_block(block, c) {
387 fprintf(stderr, "BLOCK %d:\n", block->index);
388 qir_for_each_inst(inst, block) {
389 if (c->temp_start) {
390 bool first = true;
391
392 fprintf(stderr, "%3d ", pressure);
393
394 for (int i = 0; i < c->num_temps; i++) {
395 if (c->temp_start[i] != ip)
396 continue;
397
398 if (first) {
399 first = false;
400 } else {
401 fprintf(stderr, ", ");
402 }
403 fprintf(stderr, "S%4d", i);
404 pressure++;
405 }
406
407 if (first)
408 fprintf(stderr, " ");
409 else
410 fprintf(stderr, " ");
411 }
412
413 if (c->temp_end) {
414 bool first = true;
415
416 for (int i = 0; i < c->num_temps; i++) {
417 if (c->temp_end[i] != ip)
418 continue;
419
420 if (first) {
421 first = false;
422 } else {
423 fprintf(stderr, ", ");
424 }
425 fprintf(stderr, "E%4d", i);
426 pressure--;
427 }
428
429 if (first)
430 fprintf(stderr, " ");
431 else
432 fprintf(stderr, " ");
433 }
434
435 qir_dump_inst(c, inst);
436 fprintf(stderr, "\n");
437 ip++;
438 }
439 if (block->successors[1]) {
440 fprintf(stderr, "-> BLOCK %d, %d\n",
441 block->successors[0]->index,
442 block->successors[1]->index);
443 } else if (block->successors[0]) {
444 fprintf(stderr, "-> BLOCK %d\n",
445 block->successors[0]->index);
446 }
447 }
448 }
449
450 struct qreg
451 qir_get_temp(struct vc4_compile *c)
452 {
453 struct qreg reg;
454
455 reg.file = QFILE_TEMP;
456 reg.index = c->num_temps++;
457 reg.pack = 0;
458
459 if (c->num_temps > c->defs_array_size) {
460 uint32_t old_size = c->defs_array_size;
461 c->defs_array_size = MAX2(old_size * 2, 16);
462 c->defs = reralloc(c, c->defs, struct qinst *,
463 c->defs_array_size);
464 memset(&c->defs[old_size], 0,
465 sizeof(c->defs[0]) * (c->defs_array_size - old_size));
466 }
467
468 return reg;
469 }
470
471 struct qinst *
472 qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
473 {
474 struct qinst *inst = CALLOC_STRUCT(qinst);
475
476 inst->op = op;
477 inst->dst = dst;
478 inst->src[0] = src0;
479 inst->src[1] = src1;
480 inst->cond = QPU_COND_ALWAYS;
481
482 return inst;
483 }
484
485 static void
486 qir_emit(struct vc4_compile *c, struct qinst *inst)
487 {
488 list_addtail(&inst->link, &c->cur_block->instructions);
489 }
490
491 /* Updates inst to write to a new temporary, emits it, and notes the def. */
492 struct qreg
493 qir_emit_def(struct vc4_compile *c, struct qinst *inst)
494 {
495 assert(inst->dst.file == QFILE_NULL);
496
497 inst->dst = qir_get_temp(c);
498
499 if (inst->dst.file == QFILE_TEMP)
500 c->defs[inst->dst.index] = inst;
501
502 qir_emit(c, inst);
503
504 return inst->dst;
505 }
506
507 struct qinst *
508 qir_emit_nondef(struct vc4_compile *c, struct qinst *inst)
509 {
510 if (inst->dst.file == QFILE_TEMP)
511 c->defs[inst->dst.index] = NULL;
512
513 qir_emit(c, inst);
514
515 return inst;
516 }
517
518 bool
519 qir_reg_equals(struct qreg a, struct qreg b)
520 {
521 return a.file == b.file && a.index == b.index && a.pack == b.pack;
522 }
523
524 struct qblock *
525 qir_new_block(struct vc4_compile *c)
526 {
527 struct qblock *block = rzalloc(c, struct qblock);
528
529 list_inithead(&block->instructions);
530 list_inithead(&block->qpu_inst_list);
531
532 block->predecessors = _mesa_set_create(block,
533 _mesa_hash_pointer,
534 _mesa_key_pointer_equal);
535
536 block->index = c->next_block_index++;
537
538 return block;
539 }
540
541 void
542 qir_set_emit_block(struct vc4_compile *c, struct qblock *block)
543 {
544 c->cur_block = block;
545 list_addtail(&block->link, &c->blocks);
546 }
547
548 struct qblock *
549 qir_entry_block(struct vc4_compile *c)
550 {
551 return list_first_entry(&c->blocks, struct qblock, link);
552 }
553
554 struct qblock *
555 qir_exit_block(struct vc4_compile *c)
556 {
557 return list_last_entry(&c->blocks, struct qblock, link);
558 }
559
560 void
561 qir_link_blocks(struct qblock *predecessor, struct qblock *successor)
562 {
563 _mesa_set_add(successor->predecessors, predecessor);
564 if (predecessor->successors[0]) {
565 assert(!predecessor->successors[1]);
566 predecessor->successors[1] = successor;
567 } else {
568 predecessor->successors[0] = successor;
569 }
570 }
571
572 struct vc4_compile *
573 qir_compile_init(void)
574 {
575 struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
576
577 list_inithead(&c->blocks);
578 qir_set_emit_block(c, qir_new_block(c));
579
580 c->output_position_index = -1;
581 c->output_color_index = -1;
582 c->output_point_size_index = -1;
583 c->output_sample_mask_index = -1;
584
585 c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
586 _mesa_key_pointer_equal);
587
588 return c;
589 }
590
591 void
592 qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
593 {
594 if (qinst->dst.file == QFILE_TEMP)
595 c->defs[qinst->dst.index] = NULL;
596
597 list_del(&qinst->link);
598 free(qinst);
599 }
600
601 struct qreg
602 qir_follow_movs(struct vc4_compile *c, struct qreg reg)
603 {
604 int pack = reg.pack;
605
606 while (reg.file == QFILE_TEMP &&
607 c->defs[reg.index] &&
608 (c->defs[reg.index]->op == QOP_MOV ||
609 c->defs[reg.index]->op == QOP_FMOV ||
610 c->defs[reg.index]->op == QOP_MMOV)&&
611 !c->defs[reg.index]->dst.pack &&
612 !c->defs[reg.index]->src[0].pack) {
613 reg = c->defs[reg.index]->src[0];
614 }
615
616 reg.pack = pack;
617 return reg;
618 }
619
620 void
621 qir_compile_destroy(struct vc4_compile *c)
622 {
623 qir_for_each_block(block, c) {
624 while (!list_empty(&block->instructions)) {
625 struct qinst *qinst =
626 list_first_entry(&block->instructions,
627 struct qinst, link);
628 qir_remove_instruction(c, qinst);
629 }
630 }
631
632 ralloc_free(c);
633 }
634
635 const char *
636 qir_get_stage_name(enum qstage stage)
637 {
638 static const char *names[] = {
639 [QSTAGE_FRAG] = "FS",
640 [QSTAGE_VERT] = "VS",
641 [QSTAGE_COORD] = "CS",
642 };
643
644 return names[stage];
645 }
646
647 struct qreg
648 qir_uniform(struct vc4_compile *c,
649 enum quniform_contents contents,
650 uint32_t data)
651 {
652 for (int i = 0; i < c->num_uniforms; i++) {
653 if (c->uniform_contents[i] == contents &&
654 c->uniform_data[i] == data) {
655 return qir_reg(QFILE_UNIF, i);
656 }
657 }
658
659 uint32_t uniform = c->num_uniforms++;
660
661 if (uniform >= c->uniform_array_size) {
662 c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
663 c->uniform_array_size * 2);
664
665 c->uniform_data = reralloc(c, c->uniform_data,
666 uint32_t,
667 c->uniform_array_size);
668 c->uniform_contents = reralloc(c, c->uniform_contents,
669 enum quniform_contents,
670 c->uniform_array_size);
671 }
672
673 c->uniform_contents[uniform] = contents;
674 c->uniform_data[uniform] = data;
675
676 return qir_reg(QFILE_UNIF, uniform);
677 }
678
679 void
680 qir_SF(struct vc4_compile *c, struct qreg src)
681 {
682 struct qinst *last_inst = NULL;
683
684 if (!list_empty(&c->cur_block->instructions))
685 last_inst = (struct qinst *)c->cur_block->instructions.prev;
686
687 /* We don't have any way to guess which kind of MOV is implied. */
688 assert(!src.pack);
689
690 if (src.file != QFILE_TEMP ||
691 !c->defs[src.index] ||
692 last_inst != c->defs[src.index]) {
693 last_inst = qir_MOV_dest(c, qir_reg(QFILE_NULL, 0), src);
694 last_inst = (struct qinst *)c->cur_block->instructions.prev;
695 }
696 last_inst->sf = true;
697 }
698
699 #define OPTPASS(func) \
700 do { \
701 bool stage_progress = func(c); \
702 if (stage_progress) { \
703 progress = true; \
704 if (print_opt_debug) { \
705 fprintf(stderr, \
706 "QIR opt pass %2d: %s progress\n", \
707 pass, #func); \
708 } \
709 qir_validate(c); \
710 } \
711 } while (0)
712
713 void
714 qir_optimize(struct vc4_compile *c)
715 {
716 bool print_opt_debug = false;
717 int pass = 1;
718
719 while (true) {
720 bool progress = false;
721
722 OPTPASS(qir_opt_algebraic);
723 OPTPASS(qir_opt_constant_folding);
724 OPTPASS(qir_opt_copy_propagation);
725 OPTPASS(qir_opt_peephole_sf);
726 OPTPASS(qir_opt_dead_code);
727 OPTPASS(qir_opt_small_immediates);
728 OPTPASS(qir_opt_vpm);
729
730 if (!progress)
731 break;
732
733 pass++;
734 }
735 }