2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "util/u_memory.h"
25 #include "util/ralloc.h"
33 bool has_side_effects
;
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 },
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 },
75 [QOP_FRAG_Z
] = { "frag_z", 1, 0 },
76 [QOP_FRAG_W
] = { "frag_w", 1, 0 },
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 },
85 [QOP_LOAD_IMM
] = { "load_imm", 0, 1 },
86 [QOP_LOAD_IMM_U2
] = { "load_imm_u2", 0, 1 },
87 [QOP_LOAD_IMM_I2
] = { "load_imm_i2", 0, 1 },
89 [QOP_ROT_MUL
] = { "rot_mul", 0, 2 },
91 [QOP_BRANCH
] = { "branch", 0, 0, true },
92 [QOP_UNIFORMS_RESET
] = { "uniforms_reset", 0, 2, true },
96 qir_get_op_name(enum qop qop
)
98 if (qop
< ARRAY_SIZE(qir_op_info
) && qir_op_info
[qop
].name
)
99 return qir_op_info
[qop
].name
;
105 qir_get_op_nsrc(enum qop qop
)
107 if (qop
< ARRAY_SIZE(qir_op_info
) && qir_op_info
[qop
].name
)
108 return qir_op_info
[qop
].nsrc
;
114 * Returns whether the instruction has any side effects that must be
118 qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
)
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
:
130 return qir_op_info
[inst
->op
].has_side_effects
;
134 qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
)
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.
141 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
142 if (inst
->src
[i
].file
== QFILE_VARY
&&
143 c
->input_slots
[inst
->src
[i
].index
].slot
== 0xff) {
147 if (inst
->src
[i
].file
== QFILE_VPM
)
151 if (inst
->dst
.file
== QFILE_VPM
)
158 qir_is_mul(struct qinst
*inst
)
177 qir_is_float_input(struct qinst
*inst
)
196 qir_is_raw_mov(struct qinst
*inst
)
198 return ((inst
->op
== QOP_MOV
||
199 inst
->op
== QOP_FMOV
||
200 inst
->op
== QOP_MMOV
) &&
201 inst
->cond
== QPU_COND_ALWAYS
&&
207 qir_is_tex(struct qinst
*inst
)
209 return inst
->op
>= QOP_TEX_S
&& inst
->op
<= QOP_TEX_DIRECT
;
213 qir_depends_on_flags(struct qinst
*inst
)
215 if (inst
->op
== QOP_BRANCH
) {
216 return inst
->cond
!= QPU_COND_BRANCH_ALWAYS
;
218 return (inst
->cond
!= QPU_COND_ALWAYS
&&
219 inst
->cond
!= QPU_COND_NEVER
);
224 qir_writes_r4(struct qinst
*inst
)
228 case QOP_TLB_COLOR_READ
:
240 qir_channels_written(struct qinst
*inst
)
242 if (qir_is_mul(inst
)) {
243 switch (inst
->dst
.pack
) {
244 case QPU_PACK_MUL_NOP
:
245 case QPU_PACK_MUL_8888
:
247 case QPU_PACK_MUL_8A
:
249 case QPU_PACK_MUL_8B
:
251 case QPU_PACK_MUL_8C
:
253 case QPU_PACK_MUL_8D
:
257 switch (inst
->dst
.pack
) {
259 case QPU_PACK_A_8888
:
260 case QPU_PACK_A_8888_SAT
:
261 case QPU_PACK_A_32_SAT
:
264 case QPU_PACK_A_8A_SAT
:
267 case QPU_PACK_A_8B_SAT
:
270 case QPU_PACK_A_8C_SAT
:
273 case QPU_PACK_A_8D_SAT
:
276 case QPU_PACK_A_16A_SAT
:
279 case QPU_PACK_A_16B_SAT
:
283 unreachable("Bad pack field");
287 qir_print_reg(struct vc4_compile
*c
, struct qreg reg
, bool write
)
289 static const char *files
[] = {
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",
306 fprintf(stderr
, "null");
310 fprintf(stderr
, "0x%08x (%f)", reg
.index
, uif(reg
.index
));
313 case QFILE_SMALL_IMM
:
314 if ((int)reg
.index
>= -16 && (int)reg
.index
<= 15)
315 fprintf(stderr
, "%d", reg
.index
);
317 fprintf(stderr
, "%f", uif(reg
.index
));
322 fprintf(stderr
, "vpm");
324 fprintf(stderr
, "vpm%d.%d",
325 reg
.index
/ 4, reg
.index
% 4);
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
]);
337 fprintf(stderr
, "%s%d", files
[reg
.file
], reg
.index
);
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
]));
350 qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
)
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
);
356 vc4_qpu_disasm_cond(stderr
, inst
->cond
);
358 fprintf(stderr
, ".sf");
359 fprintf(stderr
, " ");
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
);
368 vc4_qpu_disasm_pack_a(stderr
, inst
->dst
.pack
);
373 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
374 fprintf(stderr
, ", ");
375 qir_print_reg(c
, inst
->src
[i
], false);
376 vc4_qpu_disasm_unpack(stderr
, inst
->src
[i
].pack
);
381 qir_dump(struct vc4_compile
*c
)
385 qir_for_each_block(block
, c
) {
386 fprintf(stderr
, "BLOCK %d:\n", block
->index
);
387 qir_for_each_inst(inst
, block
) {
391 for (int i
= 0; i
< c
->num_temps
; i
++) {
392 if (c
->temp_start
[i
] != ip
)
398 fprintf(stderr
, ", ");
400 fprintf(stderr
, "S%4d", i
);
404 fprintf(stderr
, " ");
406 fprintf(stderr
, " ");
412 for (int i
= 0; i
< c
->num_temps
; i
++) {
413 if (c
->temp_end
[i
] != ip
)
419 fprintf(stderr
, ", ");
421 fprintf(stderr
, "E%4d", i
);
425 fprintf(stderr
, " ");
427 fprintf(stderr
, " ");
430 qir_dump_inst(c
, inst
);
431 fprintf(stderr
, "\n");
434 if (block
->successors
[1]) {
435 fprintf(stderr
, "-> BLOCK %d, %d\n",
436 block
->successors
[0]->index
,
437 block
->successors
[1]->index
);
438 } else if (block
->successors
[0]) {
439 fprintf(stderr
, "-> BLOCK %d\n",
440 block
->successors
[0]->index
);
446 qir_get_temp(struct vc4_compile
*c
)
450 reg
.file
= QFILE_TEMP
;
451 reg
.index
= c
->num_temps
++;
454 if (c
->num_temps
> c
->defs_array_size
) {
455 uint32_t old_size
= c
->defs_array_size
;
456 c
->defs_array_size
= MAX2(old_size
* 2, 16);
457 c
->defs
= reralloc(c
, c
->defs
, struct qinst
*,
459 memset(&c
->defs
[old_size
], 0,
460 sizeof(c
->defs
[0]) * (c
->defs_array_size
- old_size
));
467 qir_inst(enum qop op
, struct qreg dst
, struct qreg src0
, struct qreg src1
)
469 struct qinst
*inst
= CALLOC_STRUCT(qinst
);
473 inst
->src
= calloc(2, sizeof(inst
->src
[0]));
476 inst
->cond
= QPU_COND_ALWAYS
;
482 qir_inst4(enum qop op
, struct qreg dst
,
488 struct qinst
*inst
= CALLOC_STRUCT(qinst
);
492 inst
->src
= calloc(4, sizeof(*inst
->src
));
502 qir_emit(struct vc4_compile
*c
, struct qinst
*inst
)
504 list_addtail(&inst
->link
, &c
->cur_block
->instructions
);
507 /* Updates inst to write to a new temporary, emits it, and notes the def. */
509 qir_emit_def(struct vc4_compile
*c
, struct qinst
*inst
)
511 assert(inst
->dst
.file
== QFILE_NULL
);
513 inst
->dst
= qir_get_temp(c
);
515 if (inst
->dst
.file
== QFILE_TEMP
)
516 c
->defs
[inst
->dst
.index
] = inst
;
524 qir_emit_nondef(struct vc4_compile
*c
, struct qinst
*inst
)
526 if (inst
->dst
.file
== QFILE_TEMP
)
527 c
->defs
[inst
->dst
.index
] = NULL
;
535 qir_reg_equals(struct qreg a
, struct qreg b
)
537 return a
.file
== b
.file
&& a
.index
== b
.index
&& a
.pack
== b
.pack
;
541 qir_new_block(struct vc4_compile
*c
)
543 struct qblock
*block
= rzalloc(c
, struct qblock
);
545 list_inithead(&block
->instructions
);
546 list_inithead(&block
->qpu_inst_list
);
548 block
->predecessors
= _mesa_set_create(block
,
550 _mesa_key_pointer_equal
);
552 block
->index
= c
->next_block_index
++;
558 qir_set_emit_block(struct vc4_compile
*c
, struct qblock
*block
)
560 c
->cur_block
= block
;
561 list_addtail(&block
->link
, &c
->blocks
);
565 qir_entry_block(struct vc4_compile
*c
)
567 return list_first_entry(&c
->blocks
, struct qblock
, link
);
571 qir_exit_block(struct vc4_compile
*c
)
573 return list_last_entry(&c
->blocks
, struct qblock
, link
);
577 qir_link_blocks(struct qblock
*predecessor
, struct qblock
*successor
)
579 _mesa_set_add(successor
->predecessors
, predecessor
);
580 if (predecessor
->successors
[0]) {
581 assert(!predecessor
->successors
[1]);
582 predecessor
->successors
[1] = successor
;
584 predecessor
->successors
[0] = successor
;
589 qir_compile_init(void)
591 struct vc4_compile
*c
= rzalloc(NULL
, struct vc4_compile
);
593 list_inithead(&c
->blocks
);
594 qir_set_emit_block(c
, qir_new_block(c
));
596 c
->output_position_index
= -1;
597 c
->output_color_index
= -1;
598 c
->output_point_size_index
= -1;
599 c
->output_sample_mask_index
= -1;
601 c
->def_ht
= _mesa_hash_table_create(c
, _mesa_hash_pointer
,
602 _mesa_key_pointer_equal
);
608 qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
)
610 if (qinst
->dst
.file
== QFILE_TEMP
)
611 c
->defs
[qinst
->dst
.index
] = NULL
;
613 list_del(&qinst
->link
);
619 qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
)
623 while (reg
.file
== QFILE_TEMP
&&
624 c
->defs
[reg
.index
] &&
625 (c
->defs
[reg
.index
]->op
== QOP_MOV
||
626 c
->defs
[reg
.index
]->op
== QOP_FMOV
||
627 c
->defs
[reg
.index
]->op
== QOP_MMOV
)&&
628 !c
->defs
[reg
.index
]->dst
.pack
&&
629 !c
->defs
[reg
.index
]->src
[0].pack
) {
630 reg
= c
->defs
[reg
.index
]->src
[0];
638 qir_compile_destroy(struct vc4_compile
*c
)
640 qir_for_each_block(block
, c
) {
641 while (!list_empty(&block
->instructions
)) {
642 struct qinst
*qinst
=
643 list_first_entry(&block
->instructions
,
645 qir_remove_instruction(c
, qinst
);
653 qir_get_stage_name(enum qstage stage
)
655 static const char *names
[] = {
656 [QSTAGE_FRAG
] = "FS",
657 [QSTAGE_VERT
] = "VS",
658 [QSTAGE_COORD
] = "CS",
665 qir_uniform(struct vc4_compile
*c
,
666 enum quniform_contents contents
,
669 for (int i
= 0; i
< c
->num_uniforms
; i
++) {
670 if (c
->uniform_contents
[i
] == contents
&&
671 c
->uniform_data
[i
] == data
) {
672 return qir_reg(QFILE_UNIF
, i
);
676 uint32_t uniform
= c
->num_uniforms
++;
678 if (uniform
>= c
->uniform_array_size
) {
679 c
->uniform_array_size
= MAX2(MAX2(16, uniform
+ 1),
680 c
->uniform_array_size
* 2);
682 c
->uniform_data
= reralloc(c
, c
->uniform_data
,
684 c
->uniform_array_size
);
685 c
->uniform_contents
= reralloc(c
, c
->uniform_contents
,
686 enum quniform_contents
,
687 c
->uniform_array_size
);
690 c
->uniform_contents
[uniform
] = contents
;
691 c
->uniform_data
[uniform
] = data
;
693 return qir_reg(QFILE_UNIF
, uniform
);
697 qir_SF(struct vc4_compile
*c
, struct qreg src
)
699 struct qinst
*last_inst
= NULL
;
701 if (!list_empty(&c
->cur_block
->instructions
))
702 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
704 /* We don't have any way to guess which kind of MOV is implied. */
707 if (src
.file
!= QFILE_TEMP
||
708 !c
->defs
[src
.index
] ||
709 last_inst
!= c
->defs
[src
.index
]) {
710 last_inst
= qir_MOV_dest(c
, qir_reg(QFILE_NULL
, 0), src
);
711 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
713 last_inst
->sf
= true;
716 #define OPTPASS(func) \
718 bool stage_progress = func(c); \
719 if (stage_progress) { \
721 if (print_opt_debug) { \
723 "QIR opt pass %2d: %s progress\n", \
731 qir_optimize(struct vc4_compile
*c
)
733 bool print_opt_debug
= false;
737 bool progress
= false;
739 OPTPASS(qir_opt_algebraic
);
740 OPTPASS(qir_opt_constant_folding
);
741 OPTPASS(qir_opt_copy_propagation
);
742 OPTPASS(qir_opt_peephole_sf
);
743 OPTPASS(qir_opt_dead_code
);
744 OPTPASS(qir_opt_small_immediates
);
745 OPTPASS(qir_opt_vpm
);