Android: fix build break from nir/glsl move to compiler/
[mesa.git] / src / gallium / drivers / vc4 / vc4_qir.c
index aa1f40b1162abb16b5de6d4b5ff3738b06b28e4a..f9eb0e151c5a46307266e876749dd11c4dfcdd8f 100644 (file)
@@ -22,7 +22,7 @@
  */
 
 #include "util/u_memory.h"
-#include "util/u_simple_list.h"
+#include "util/ralloc.h"
 
 #include "vc4_qir.h"
 #include "vc4_qpu.h"
@@ -31,53 +31,65 @@ struct qir_op_info {
         const char *name;
         uint8_t ndst, nsrc;
         bool has_side_effects;
+        bool multi_instruction;
 };
 
 static const struct qir_op_info qir_op_info[] = {
         [QOP_MOV] = { "mov", 1, 1 },
+        [QOP_FMOV] = { "fmov", 1, 1 },
+        [QOP_MMOV] = { "mmov", 1, 1 },
         [QOP_FADD] = { "fadd", 1, 2 },
         [QOP_FSUB] = { "fsub", 1, 2 },
         [QOP_FMUL] = { "fmul", 1, 2 },
+        [QOP_MUL24] = { "mul24", 1, 2 },
+        [QOP_V8MULD] = {"v8muld", 1, 2 },
+        [QOP_V8MIN] = {"v8min", 1, 2 },
+        [QOP_V8MAX] = {"v8max", 1, 2 },
+        [QOP_V8ADDS] = {"v8adds", 1, 2 },
+        [QOP_V8SUBS] = {"v8subs", 1, 2 },
         [QOP_FMIN] = { "fmin", 1, 2 },
         [QOP_FMAX] = { "fmax", 1, 2 },
         [QOP_FMINABS] = { "fminabs", 1, 2 },
         [QOP_FMAXABS] = { "fmaxabs", 1, 2 },
-
-        [QOP_SEQ] = { "seq", 1, 2 },
-        [QOP_SNE] = { "sne", 1, 2 },
-        [QOP_SGE] = { "sge", 1, 2 },
-        [QOP_SLT] = { "slt", 1, 2 },
-        [QOP_CMP] = { "cmp", 1, 3 },
-
         [QOP_FTOI] = { "ftoi", 1, 1 },
         [QOP_ITOF] = { "itof", 1, 1 },
-        [QOP_RCP] = { "rcp", 1, 1 },
-        [QOP_RSQ] = { "rsq", 1, 1 },
-        [QOP_EXP2] = { "exp2", 1, 2 },
-        [QOP_LOG2] = { "log2", 1, 2 },
-        [QOP_PACK_COLORS] = { "pack_colors", 1, 4 },
-        [QOP_PACK_SCALED] = { "pack_scaled", 1, 2 },
-        [QOP_VPM_WRITE] = { "vpm_write", 0, 1, true },
-        [QOP_VPM_READ] = { "vpm_read", 0, 1, true },
-        [QOP_TLB_PASSTHROUGH_Z_WRITE] = { "tlb_passthrough_z", 0, 0, true },
+        [QOP_ADD] = { "add", 1, 2 },
+        [QOP_SUB] = { "sub", 1, 2 },
+        [QOP_SHR] = { "shr", 1, 2 },
+        [QOP_ASR] = { "asr", 1, 2 },
+        [QOP_SHL] = { "shl", 1, 2 },
+        [QOP_MIN] = { "min", 1, 2 },
+        [QOP_MAX] = { "max", 1, 2 },
+        [QOP_AND] = { "and", 1, 2 },
+        [QOP_OR] = { "or", 1, 2 },
+        [QOP_XOR] = { "xor", 1, 2 },
+        [QOP_NOT] = { "not", 1, 1 },
+
+        [QOP_RCP] = { "rcp", 1, 1, false, true },
+        [QOP_RSQ] = { "rsq", 1, 1, false, true },
+        [QOP_EXP2] = { "exp2", 1, 2, false, true },
+        [QOP_LOG2] = { "log2", 1, 2, false, true },
+        [QOP_TLB_DISCARD_SETUP] = { "discard", 0, 1, true },
+        [QOP_TLB_STENCIL_SETUP] = { "tlb_stencil_setup", 0, 1, true },
+        [QOP_TLB_Z_WRITE] = { "tlb_z", 0, 1, true },
         [QOP_TLB_COLOR_WRITE] = { "tlb_color", 0, 1, true },
-        [QOP_TLB_COLOR_READ] = { "tlb_color_read", 0, 0, true },
+        [QOP_TLB_COLOR_WRITE_MS] = { "tlb_color_ms", 0, 1, true },
+        [QOP_TLB_COLOR_READ] = { "tlb_color_read", 1, 0 },
+        [QOP_MS_MASK] = { "ms_mask", 0, 1, true },
         [QOP_VARY_ADD_C] = { "vary_add_c", 1, 1 },
 
         [QOP_FRAG_X] = { "frag_x", 1, 0 },
         [QOP_FRAG_Y] = { "frag_y", 1, 0 },
         [QOP_FRAG_Z] = { "frag_z", 1, 0 },
-        [QOP_FRAG_RCP_W] = { "frag_rcp_w", 1, 0 },
+        [QOP_FRAG_W] = { "frag_w", 1, 0 },
+        [QOP_FRAG_REV_FLAG] = { "frag_rev_flag", 1, 0 },
 
         [QOP_TEX_S] = { "tex_s", 0, 2 },
         [QOP_TEX_T] = { "tex_t", 0, 2 },
         [QOP_TEX_R] = { "tex_r", 0, 2 },
         [QOP_TEX_B] = { "tex_b", 0, 2 },
-        [QOP_TEX_RESULT] = { "tex_result", 0, 0 },
-        [QOP_R4_UNPACK_A] = { "r4_unpack_a", 1, 0 },
-        [QOP_R4_UNPACK_B] = { "r4_unpack_b", 1, 0 },
-        [QOP_R4_UNPACK_C] = { "r4_unpack_c", 1, 0 },
-        [QOP_R4_UNPACK_D] = { "r4_unpack_d", 1, 0 },
+        [QOP_TEX_DIRECT] = { "tex_direct", 0, 2 },
+        [QOP_TEX_RESULT] = { "tex_result", 1, 0, true },
 };
 
 static const char *
@@ -98,64 +110,218 @@ qir_get_op_nsrc(enum qop qop)
                 abort();
 }
 
+/**
+ * Returns whether the instruction has any side effects that must be
+ * preserved.
+ */
 bool
-qir_has_side_effects(struct qinst *inst)
+qir_has_side_effects(struct vc4_compile *c, struct qinst *inst)
 {
+        return qir_op_info[inst->op].has_side_effects;
+}
+
+bool
+qir_has_side_effect_reads(struct vc4_compile *c, struct qinst *inst)
+{
+        /* We can dead-code eliminate varyings, because we only tell the VS
+         * about the live ones at the end.  But we have to preserve the
+         * point/line coordinates reads, because they're generated by
+         * fixed-function hardware.
+         */
         for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
-                if (inst->src[i].file == QFILE_VARY ||
-                    inst->src[i].file == QFILE_UNIF)
+                if (inst->src[i].file == QFILE_VARY &&
+                    c->input_slots[inst->src[i].index].slot == 0xff) {
+                        return true;
+                }
+
+                if (inst->src[i].file == QFILE_VPM)
                         return true;
         }
 
-        return qir_op_info[inst->op].has_side_effects;
+        if (inst->dst.file == QFILE_VPM)
+                return true;
+
+        return false;
+}
+
+bool
+qir_is_multi_instruction(struct qinst *inst)
+{
+        return qir_op_info[inst->op].multi_instruction;
+}
+
+bool
+qir_is_mul(struct qinst *inst)
+{
+        switch (inst->op) {
+        case QOP_MMOV:
+        case QOP_FMUL:
+        case QOP_MUL24:
+        case QOP_V8MULD:
+        case QOP_V8MIN:
+        case QOP_V8MAX:
+        case QOP_V8ADDS:
+        case QOP_V8SUBS:
+                return true;
+        default:
+                return false;
+        }
+}
+
+bool
+qir_is_float_input(struct qinst *inst)
+{
+        switch (inst->op) {
+        case QOP_FMOV:
+        case QOP_FMUL:
+        case QOP_FADD:
+        case QOP_FSUB:
+        case QOP_FMIN:
+        case QOP_FMAX:
+        case QOP_FMINABS:
+        case QOP_FMAXABS:
+        case QOP_FTOI:
+                return true;
+        default:
+                return false;
+        }
+}
+
+bool
+qir_is_raw_mov(struct qinst *inst)
+{
+        return ((inst->op == QOP_MOV ||
+                 inst->op == QOP_FMOV ||
+                 inst->op == QOP_MMOV) &&
+                inst->cond == QPU_COND_ALWAYS &&
+                !inst->dst.pack &&
+                !inst->src[0].pack);
+}
+
+bool
+qir_is_tex(struct qinst *inst)
+{
+        return inst->op >= QOP_TEX_S && inst->op <= QOP_TEX_DIRECT;
+}
+
+bool
+qir_depends_on_flags(struct qinst *inst)
+{
+        return (inst->cond != QPU_COND_ALWAYS &&
+                inst->cond != QPU_COND_NEVER);
+}
+
+bool
+qir_writes_r4(struct qinst *inst)
+{
+        switch (inst->op) {
+        case QOP_TEX_RESULT:
+        case QOP_TLB_COLOR_READ:
+        case QOP_RCP:
+        case QOP_RSQ:
+        case QOP_EXP2:
+        case QOP_LOG2:
+                return true;
+        default:
+                return false;
+        }
 }
 
 static void
-qir_print_reg(struct qreg reg)
+qir_print_reg(struct vc4_compile *c, struct qreg reg, bool write)
 {
-        const char *files[] = {
+        static const char *files[] = {
                 [QFILE_TEMP] = "t",
                 [QFILE_VARY] = "v",
                 [QFILE_UNIF] = "u",
         };
 
-        if (reg.file == QFILE_NULL)
+        if (reg.file == QFILE_NULL) {
                 fprintf(stderr, "null");
-        else
+        } else if (reg.file == QFILE_SMALL_IMM) {
+                if ((int)reg.index >= -16 && (int)reg.index <= 15)
+                        fprintf(stderr, "%d", reg.index);
+                else
+                        fprintf(stderr, "%f", uif(reg.index));
+        } else if (reg.file == QFILE_VPM) {
+                if (write) {
+                        fprintf(stderr, "vpm");
+                } else {
+                        fprintf(stderr, "vpm%d.%d",
+                                reg.index / 4, reg.index % 4);
+                }
+        } else {
                 fprintf(stderr, "%s%d", files[reg.file], reg.index);
+        }
+
+        if (reg.file == QFILE_UNIF &&
+            c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {
+                fprintf(stderr, " (0x%08x / %f)",
+                        c->uniform_data[reg.index],
+                        uif(c->uniform_data[reg.index]));
+        }
 }
 
 void
-qir_dump_inst(struct qinst *inst)
+qir_dump_inst(struct vc4_compile *c, struct qinst *inst)
 {
-        fprintf(stderr, "%s ", qir_get_op_name(inst->op));
-
-        qir_print_reg(inst->dst);
+        static const char *conditions[] = {
+                [QPU_COND_ALWAYS] = "",
+                [QPU_COND_NEVER] = ".never",
+                [QPU_COND_ZS] = ".zs",
+                [QPU_COND_ZC] = ".zc",
+                [QPU_COND_NS] = ".ns",
+                [QPU_COND_NC] = ".nc",
+                [QPU_COND_CS] = ".cs",
+                [QPU_COND_CC] = ".cc",
+        };
+        fprintf(stderr, "%s%s%s ",
+                qir_get_op_name(inst->op),
+                conditions[inst->cond],
+                inst->sf ? ".sf" : "");
+
+        qir_print_reg(c, inst->dst, true);
+        if (inst->dst.pack) {
+                if (inst->dst.pack) {
+                        if (qir_is_mul(inst))
+                                vc4_qpu_disasm_pack_mul(stderr, inst->dst.pack);
+                        else
+                                vc4_qpu_disasm_pack_a(stderr, inst->dst.pack);
+                }
+        }
         for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
                 fprintf(stderr, ", ");
-                qir_print_reg(inst->src[i]);
+                qir_print_reg(c, inst->src[i], false);
+                vc4_qpu_disasm_unpack(stderr, inst->src[i].pack);
         }
 }
 
 void
-qir_dump(struct qcompile *c)
+qir_dump(struct vc4_compile *c)
 {
-        struct simple_node *node;
-
-        foreach(node, &c->instructions) {
-                struct qinst *inst = (struct qinst *)node;
-                qir_dump_inst(inst);
+        list_for_each_entry(struct qinst, inst, &c->instructions, link) {
+                qir_dump_inst(c, inst);
                 fprintf(stderr, "\n");
         }
 }
 
 struct qreg
-qir_get_temp(struct qcompile *c)
+qir_get_temp(struct vc4_compile *c)
 {
         struct qreg reg;
 
         reg.file = QFILE_TEMP;
         reg.index = c->num_temps++;
+        reg.pack = 0;
+
+        if (c->num_temps > c->defs_array_size) {
+                uint32_t old_size = c->defs_array_size;
+                c->defs_array_size = MAX2(old_size * 2, 16);
+                c->defs = reralloc(c, c->defs, struct qinst *,
+                                   c->defs_array_size);
+                memset(&c->defs[old_size], 0,
+                       sizeof(c->defs[0]) * (c->defs_array_size - old_size));
+        }
 
         return reg;
 }
@@ -170,6 +336,7 @@ qir_inst(enum qop op, struct qreg dst, struct qreg src0, struct qreg src1)
         inst->src = calloc(2, sizeof(inst->src[0]));
         inst->src[0] = src0;
         inst->src[1] = src1;
+        inst->cond = QPU_COND_ALWAYS;
 
         return inst;
 }
@@ -195,9 +362,12 @@ qir_inst4(enum qop op, struct qreg dst,
 }
 
 void
-qir_emit(struct qcompile *c, struct qinst *inst)
+qir_emit(struct vc4_compile *c, struct qinst *inst)
 {
-        insert_at_tail(&c->instructions, &inst->link);
+        if (inst->dst.file == QFILE_TEMP)
+                c->defs[inst->dst.index] = inst;
+
+        qir_emit_nodef(c, inst);
 }
 
 bool
@@ -206,20 +376,64 @@ qir_reg_equals(struct qreg a, struct qreg b)
         return a.file == b.file && a.index == b.index;
 }
 
-struct qcompile *
+struct vc4_compile *
 qir_compile_init(void)
 {
-        struct qcompile *c = CALLOC_STRUCT(qcompile);
+        struct vc4_compile *c = rzalloc(NULL, struct vc4_compile);
+
+        list_inithead(&c->instructions);
 
-        make_empty_list(&c->instructions);
+        c->output_position_index = -1;
+        c->output_color_index = -1;
+        c->output_point_size_index = -1;
+        c->output_sample_mask_index = -1;
+
+        c->def_ht = _mesa_hash_table_create(c, _mesa_hash_pointer,
+                                            _mesa_key_pointer_equal);
 
         return c;
 }
 
 void
-qir_compile_destroy(struct qcompile *c)
+qir_remove_instruction(struct vc4_compile *c, struct qinst *qinst)
+{
+        if (qinst->dst.file == QFILE_TEMP)
+                c->defs[qinst->dst.index] = NULL;
+
+        list_del(&qinst->link);
+        free(qinst->src);
+        free(qinst);
+}
+
+struct qreg
+qir_follow_movs(struct vc4_compile *c, struct qreg reg)
+{
+        int pack = reg.pack;
+
+        while (reg.file == QFILE_TEMP &&
+               c->defs[reg.index] &&
+               (c->defs[reg.index]->op == QOP_MOV ||
+                c->defs[reg.index]->op == QOP_FMOV ||
+                c->defs[reg.index]->op == QOP_MMOV)&&
+               !c->defs[reg.index]->dst.pack &&
+               !c->defs[reg.index]->src[0].pack) {
+                reg = c->defs[reg.index]->src[0];
+        }
+
+        reg.pack = pack;
+        return reg;
+}
+
+void
+qir_compile_destroy(struct vc4_compile *c)
 {
-        free(c);
+        while (!list_empty(&c->instructions)) {
+                struct qinst *qinst =
+                        (struct qinst *)c->instructions.next;
+                qir_remove_instruction(c, qinst);
+        }
+
+        ralloc_free(c);
 }
 
 const char *
@@ -234,6 +448,56 @@ qir_get_stage_name(enum qstage stage)
         return names[stage];
 }
 
+struct qreg
+qir_uniform(struct vc4_compile *c,
+            enum quniform_contents contents,
+            uint32_t data)
+{
+        for (int i = 0; i < c->num_uniforms; i++) {
+                if (c->uniform_contents[i] == contents &&
+                    c->uniform_data[i] == data) {
+                        return (struct qreg) { QFILE_UNIF, i };
+                }
+        }
+
+        uint32_t uniform = c->num_uniforms++;
+        struct qreg u = { QFILE_UNIF, uniform };
+
+        if (uniform >= c->uniform_array_size) {
+                c->uniform_array_size = MAX2(MAX2(16, uniform + 1),
+                                             c->uniform_array_size * 2);
+
+                c->uniform_data = reralloc(c, c->uniform_data,
+                                           uint32_t,
+                                           c->uniform_array_size);
+                c->uniform_contents = reralloc(c, c->uniform_contents,
+                                               enum quniform_contents,
+                                               c->uniform_array_size);
+        }
+
+        c->uniform_contents[uniform] = contents;
+        c->uniform_data[uniform] = data;
+
+        return u;
+}
+
+void
+qir_SF(struct vc4_compile *c, struct qreg src)
+{
+        struct qinst *last_inst = NULL;
+        if (!list_empty(&c->instructions))
+                last_inst = (struct qinst *)c->instructions.prev;
+
+        if (src.file != QFILE_TEMP ||
+            !c->defs[src.index] ||
+            last_inst != c->defs[src.index] ||
+            qir_is_multi_instruction(last_inst)) {
+                src = qir_MOV(c, src);
+                last_inst = (struct qinst *)c->instructions.prev;
+        }
+        last_inst->sf = true;
+}
+
 #define OPTPASS(func)                                                   \
         do {                                                            \
                 bool stage_progress = func(c);                          \
@@ -248,7 +512,7 @@ qir_get_stage_name(enum qstage stage)
         } while (0)
 
 void
-qir_optimize(struct qcompile *c)
+qir_optimize(struct vc4_compile *c)
 {
         bool print_opt_debug = false;
         int pass = 1;
@@ -257,8 +521,12 @@ qir_optimize(struct qcompile *c)
                 bool progress = false;
 
                 OPTPASS(qir_opt_algebraic);
+                OPTPASS(qir_opt_cse);
+                OPTPASS(qir_opt_constant_folding);
                 OPTPASS(qir_opt_copy_propagation);
                 OPTPASS(qir_opt_dead_code);
+                OPTPASS(qir_opt_small_immediates);
+                OPTPASS(qir_opt_vpm_writes);
 
                 if (!progress)
                         break;