17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
37ec681f3Smrg * Copyright (C) 2019-2020 Collabora, Ltd.
47ec681f3Smrg *
57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
67ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
77ec681f3Smrg * to deal in the Software without restriction, including without limitation
87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
107ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice (including the next
137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
147ec681f3Smrg * Software.
157ec681f3Smrg *
167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
217ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227ec681f3Smrg * SOFTWARE.
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#include "compiler.h"
267ec681f3Smrg#include "midgard_ops.h"
277ec681f3Smrg
287ec681f3Smrgvoid mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old, unsigned new)
297ec681f3Smrg{
307ec681f3Smrg        mir_foreach_src(ins, i) {
317ec681f3Smrg                if (ins->src[i] == old)
327ec681f3Smrg                        ins->src[i] = new;
337ec681f3Smrg        }
347ec681f3Smrg}
357ec681f3Smrg
367ec681f3Smrgvoid mir_rewrite_index_dst_single(midgard_instruction *ins, unsigned old, unsigned new)
377ec681f3Smrg{
387ec681f3Smrg        if (ins->dest == old)
397ec681f3Smrg                ins->dest = new;
407ec681f3Smrg}
417ec681f3Smrg
427ec681f3Smrgstatic void
437ec681f3Smrgmir_rewrite_index_src_single_swizzle(midgard_instruction *ins, unsigned old, unsigned new, unsigned *swizzle)
447ec681f3Smrg{
457ec681f3Smrg        for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i) {
467ec681f3Smrg                if (ins->src[i] != old) continue;
477ec681f3Smrg
487ec681f3Smrg                ins->src[i] = new;
497ec681f3Smrg                mir_compose_swizzle(ins->swizzle[i], swizzle, ins->swizzle[i]);
507ec681f3Smrg        }
517ec681f3Smrg}
527ec681f3Smrg
537ec681f3Smrgvoid
547ec681f3Smrgmir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new)
557ec681f3Smrg{
567ec681f3Smrg        mir_foreach_instr_global(ctx, ins) {
577ec681f3Smrg                mir_rewrite_index_src_single(ins, old, new);
587ec681f3Smrg        }
597ec681f3Smrg}
607ec681f3Smrg
617ec681f3Smrgvoid
627ec681f3Smrgmir_rewrite_index_src_swizzle(compiler_context *ctx, unsigned old, unsigned new, unsigned *swizzle)
637ec681f3Smrg{
647ec681f3Smrg        mir_foreach_instr_global(ctx, ins) {
657ec681f3Smrg                mir_rewrite_index_src_single_swizzle(ins, old, new, swizzle);
667ec681f3Smrg        }
677ec681f3Smrg}
687ec681f3Smrg
697ec681f3Smrgvoid
707ec681f3Smrgmir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new)
717ec681f3Smrg{
727ec681f3Smrg        mir_foreach_instr_global(ctx, ins) {
737ec681f3Smrg                mir_rewrite_index_dst_single(ins, old, new);
747ec681f3Smrg        }
757ec681f3Smrg
767ec681f3Smrg        /* Implicitly written before the shader */
777ec681f3Smrg        if (ctx->blend_input == old)
787ec681f3Smrg                ctx->blend_input = new;
797ec681f3Smrg
807ec681f3Smrg        if (ctx->blend_src1 == old)
817ec681f3Smrg                ctx->blend_src1 = new;
827ec681f3Smrg}
837ec681f3Smrg
847ec681f3Smrgvoid
857ec681f3Smrgmir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new)
867ec681f3Smrg{
877ec681f3Smrg        mir_rewrite_index_src(ctx, old, new);
887ec681f3Smrg        mir_rewrite_index_dst(ctx, old, new);
897ec681f3Smrg}
907ec681f3Smrg
917ec681f3Smrgunsigned
927ec681f3Smrgmir_use_count(compiler_context *ctx, unsigned value)
937ec681f3Smrg{
947ec681f3Smrg        unsigned used_count = 0;
957ec681f3Smrg
967ec681f3Smrg        mir_foreach_instr_global(ctx, ins) {
977ec681f3Smrg                if (mir_has_arg(ins, value))
987ec681f3Smrg                        ++used_count;
997ec681f3Smrg        }
1007ec681f3Smrg
1017ec681f3Smrg        if (ctx->blend_input == value)
1027ec681f3Smrg                ++used_count;
1037ec681f3Smrg
1047ec681f3Smrg        if (ctx->blend_src1 == value)
1057ec681f3Smrg                ++used_count;
1067ec681f3Smrg
1077ec681f3Smrg        return used_count;
1087ec681f3Smrg}
1097ec681f3Smrg
1107ec681f3Smrg/* Checks if a value is used only once (or totally dead), which is an important
1117ec681f3Smrg * heuristic to figure out if certain optimizations are Worth It (TM) */
1127ec681f3Smrg
1137ec681f3Smrgbool
1147ec681f3Smrgmir_single_use(compiler_context *ctx, unsigned value)
1157ec681f3Smrg{
1167ec681f3Smrg        /* We can replicate constants in places so who cares */
1177ec681f3Smrg        if (value == SSA_FIXED_REGISTER(REGISTER_CONSTANT))
1187ec681f3Smrg                return true;
1197ec681f3Smrg
1207ec681f3Smrg        return mir_use_count(ctx, value) <= 1;
1217ec681f3Smrg}
1227ec681f3Smrg
1237ec681f3Smrgbool
1247ec681f3Smrgmir_nontrivial_mod(midgard_instruction *ins, unsigned i, bool check_swizzle)
1257ec681f3Smrg{
1267ec681f3Smrg        bool is_int = midgard_is_integer_op(ins->op);
1277ec681f3Smrg
1287ec681f3Smrg        if (is_int) {
1297ec681f3Smrg                if (ins->src_shift[i]) return true;
1307ec681f3Smrg        } else {
1317ec681f3Smrg                if (ins->src_neg[i]) return true;
1327ec681f3Smrg                if (ins->src_abs[i]) return true;
1337ec681f3Smrg        }
1347ec681f3Smrg
1357ec681f3Smrg        if (ins->dest_type != ins->src_types[i]) return true;
1367ec681f3Smrg
1377ec681f3Smrg        if (check_swizzle) {
1387ec681f3Smrg                for (unsigned c = 0; c < 16; ++c) {
1397ec681f3Smrg                        if (!(ins->mask & (1 << c))) continue;
1407ec681f3Smrg                        if (ins->swizzle[i][c] != c) return true;
1417ec681f3Smrg                }
1427ec681f3Smrg        }
1437ec681f3Smrg
1447ec681f3Smrg        return false;
1457ec681f3Smrg}
1467ec681f3Smrg
1477ec681f3Smrgbool
1487ec681f3Smrgmir_nontrivial_outmod(midgard_instruction *ins)
1497ec681f3Smrg{
1507ec681f3Smrg        bool is_int = midgard_is_integer_op(ins->op);
1517ec681f3Smrg        unsigned mod = ins->outmod;
1527ec681f3Smrg
1537ec681f3Smrg        if (ins->dest_type != ins->src_types[1])
1547ec681f3Smrg                return true;
1557ec681f3Smrg
1567ec681f3Smrg        if (is_int)
1577ec681f3Smrg                return mod != midgard_outmod_keeplo;
1587ec681f3Smrg        else
1597ec681f3Smrg                return mod != midgard_outmod_none;
1607ec681f3Smrg}
1617ec681f3Smrg
1627ec681f3Smrg/* 128 / sz = exp2(log2(128 / sz))
1637ec681f3Smrg *          = exp2(log2(128) - log2(sz))
1647ec681f3Smrg *          = exp2(7 - log2(sz))
1657ec681f3Smrg *          = 1 << (7 - log2(sz))
1667ec681f3Smrg */
1677ec681f3Smrg
1687ec681f3Smrgstatic unsigned
1697ec681f3Smrgmir_components_for_bits(unsigned bits)
1707ec681f3Smrg{
1717ec681f3Smrg        return 1 << (7 - util_logbase2(bits));
1727ec681f3Smrg}
1737ec681f3Smrg
1747ec681f3Smrgunsigned
1757ec681f3Smrgmir_components_for_type(nir_alu_type T)
1767ec681f3Smrg{
1777ec681f3Smrg        unsigned sz = nir_alu_type_get_type_size(T);
1787ec681f3Smrg        return mir_components_for_bits(sz);
1797ec681f3Smrg}
1807ec681f3Smrg
1817ec681f3Smrguint16_t
1827ec681f3Smrgmir_from_bytemask(uint16_t bytemask, unsigned bits)
1837ec681f3Smrg{
1847ec681f3Smrg        unsigned value = 0;
1857ec681f3Smrg        unsigned count = bits / 8;
1867ec681f3Smrg
1877ec681f3Smrg        for (unsigned c = 0, d = 0; c < 16; c += count, ++d) {
1887ec681f3Smrg                bool a = (bytemask & (1 << c)) != 0;
1897ec681f3Smrg
1907ec681f3Smrg                for (unsigned q = c; q < count; ++q)
1917ec681f3Smrg                        assert(((bytemask & (1 << q)) != 0) == a);
1927ec681f3Smrg
1937ec681f3Smrg                value |= (a << d);
1947ec681f3Smrg        }
1957ec681f3Smrg
1967ec681f3Smrg        return value;
1977ec681f3Smrg}
1987ec681f3Smrg
1997ec681f3Smrg/* Rounds up a bytemask to fill a given component count. Iterate each
2007ec681f3Smrg * component, and check if any bytes in the component are masked on */
2017ec681f3Smrg
2027ec681f3Smrguint16_t
2037ec681f3Smrgmir_round_bytemask_up(uint16_t mask, unsigned bits)
2047ec681f3Smrg{
2057ec681f3Smrg        unsigned bytes = bits / 8;
2067ec681f3Smrg        unsigned maxmask = mask_of(bytes);
2077ec681f3Smrg        unsigned channels = mir_components_for_bits(bits);
2087ec681f3Smrg
2097ec681f3Smrg        for (unsigned c = 0; c < channels; ++c) {
2107ec681f3Smrg                unsigned submask = maxmask << (c * bytes);
2117ec681f3Smrg
2127ec681f3Smrg                if (mask & submask)
2137ec681f3Smrg                        mask |= submask;
2147ec681f3Smrg        }
2157ec681f3Smrg
2167ec681f3Smrg        return mask;
2177ec681f3Smrg}
2187ec681f3Smrg
2197ec681f3Smrg/* Grabs the per-byte mask of an instruction (as opposed to per-component) */
2207ec681f3Smrg
2217ec681f3Smrguint16_t
2227ec681f3Smrgmir_bytemask(midgard_instruction *ins)
2237ec681f3Smrg{
2247ec681f3Smrg        unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
2257ec681f3Smrg        return pan_to_bytemask(type_size, ins->mask);
2267ec681f3Smrg}
2277ec681f3Smrg
2287ec681f3Smrgvoid
2297ec681f3Smrgmir_set_bytemask(midgard_instruction *ins, uint16_t bytemask)
2307ec681f3Smrg{
2317ec681f3Smrg        unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
2327ec681f3Smrg        ins->mask = mir_from_bytemask(bytemask, type_size);
2337ec681f3Smrg}
2347ec681f3Smrg
2357ec681f3Smrg/* Checks if we should use an upper destination override, rather than the lower
2367ec681f3Smrg * one in the IR. Returns zero if no, returns the bytes to shift otherwise */
2377ec681f3Smrg
2387ec681f3Smrgsigned
2397ec681f3Smrgmir_upper_override(midgard_instruction *ins, unsigned inst_size)
2407ec681f3Smrg{
2417ec681f3Smrg        unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
2427ec681f3Smrg
2437ec681f3Smrg        /* If the sizes are the same, there's nothing to override */
2447ec681f3Smrg        if (type_size == inst_size)
2457ec681f3Smrg                return -1;
2467ec681f3Smrg
2477ec681f3Smrg        /* There are 16 bytes per vector, so there are (16/bytes)
2487ec681f3Smrg         * components per vector. So the magic half is half of
2497ec681f3Smrg         * (16/bytes), which simplifies to 8/bytes = 8 / (bits / 8) = 64 / bits
2507ec681f3Smrg         * */
2517ec681f3Smrg
2527ec681f3Smrg        unsigned threshold = mir_components_for_bits(type_size) >> 1;
2537ec681f3Smrg
2547ec681f3Smrg        /* How many components did we shift over? */
2557ec681f3Smrg        unsigned zeroes = __builtin_ctz(ins->mask);
2567ec681f3Smrg
2577ec681f3Smrg        /* Did we hit the threshold? */
2587ec681f3Smrg        return (zeroes >= threshold) ? threshold : 0;
2597ec681f3Smrg}
2607ec681f3Smrg
2617ec681f3Smrg/* Creates a mask of the components of a node read by an instruction, by
2627ec681f3Smrg * analyzing the swizzle with respect to the instruction's mask. E.g.:
2637ec681f3Smrg *
2647ec681f3Smrg *  fadd r0.xz, r1.yyyy, r2.zwyx
2657ec681f3Smrg *
2667ec681f3Smrg * will return a mask of Z/Y for r2
2677ec681f3Smrg */
2687ec681f3Smrg
2697ec681f3Smrgstatic uint16_t
2707ec681f3Smrgmir_bytemask_of_read_components_single(unsigned *swizzle, unsigned inmask, unsigned bits)
2717ec681f3Smrg{
2727ec681f3Smrg        unsigned cmask = 0;
2737ec681f3Smrg
2747ec681f3Smrg        for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
2757ec681f3Smrg                if (!(inmask & (1 << c))) continue;
2767ec681f3Smrg                cmask |= (1 << swizzle[c]);
2777ec681f3Smrg        }
2787ec681f3Smrg
2797ec681f3Smrg        return pan_to_bytemask(bits, cmask);
2807ec681f3Smrg}
2817ec681f3Smrg
2827ec681f3Smrguint16_t
2837ec681f3Smrgmir_bytemask_of_read_components_index(midgard_instruction *ins, unsigned i)
2847ec681f3Smrg{
2857ec681f3Smrg        /* Conditional branches read one 32-bit component = 4 bytes (TODO: multi branch??) */
2867ec681f3Smrg        if (ins->compact_branch && ins->branch.conditional && (i == 0))
2877ec681f3Smrg                return 0xF;
2887ec681f3Smrg
2897ec681f3Smrg        /* ALU ops act componentwise so we need to pay attention to
2907ec681f3Smrg         * their mask. Texture/ldst does not so we don't clamp source
2917ec681f3Smrg         * readmasks based on the writemask */
2927ec681f3Smrg        unsigned qmask = ~0;
2937ec681f3Smrg
2947ec681f3Smrg        /* Handle dot products and things */
2957ec681f3Smrg        if (ins->type == TAG_ALU_4 && !ins->compact_branch) {
2967ec681f3Smrg                unsigned props = alu_opcode_props[ins->op].props;
2977ec681f3Smrg
2987ec681f3Smrg                unsigned channel_override = GET_CHANNEL_COUNT(props);
2997ec681f3Smrg
3007ec681f3Smrg                if (channel_override)
3017ec681f3Smrg                        qmask = mask_of(channel_override);
3027ec681f3Smrg                else
3037ec681f3Smrg                        qmask = ins->mask;
3047ec681f3Smrg        }
3057ec681f3Smrg
3067ec681f3Smrg        return mir_bytemask_of_read_components_single(ins->swizzle[i], qmask,
3077ec681f3Smrg                nir_alu_type_get_type_size(ins->src_types[i]));
3087ec681f3Smrg}
3097ec681f3Smrg
3107ec681f3Smrguint16_t
3117ec681f3Smrgmir_bytemask_of_read_components(midgard_instruction *ins, unsigned node)
3127ec681f3Smrg{
3137ec681f3Smrg        uint16_t mask = 0;
3147ec681f3Smrg
3157ec681f3Smrg        if (node == ~0)
3167ec681f3Smrg                return 0;
3177ec681f3Smrg
3187ec681f3Smrg        mir_foreach_src(ins, i) {
3197ec681f3Smrg                if (ins->src[i] != node) continue;
3207ec681f3Smrg                mask |= mir_bytemask_of_read_components_index(ins, i);
3217ec681f3Smrg        }
3227ec681f3Smrg
3237ec681f3Smrg        return mask;
3247ec681f3Smrg}
3257ec681f3Smrg
3267ec681f3Smrg/* Register allocation occurs after instruction scheduling, which is fine until
3277ec681f3Smrg * we start needing to spill registers and therefore insert instructions into
3287ec681f3Smrg * an already-scheduled program. We don't have to be terribly efficient about
3297ec681f3Smrg * this, since spilling is already slow. So just semantically we need to insert
3307ec681f3Smrg * the instruction into a new bundle before/after the bundle of the instruction
3317ec681f3Smrg * in question */
3327ec681f3Smrg
3337ec681f3Smrgstatic midgard_bundle
3347ec681f3Smrgmir_bundle_for_op(compiler_context *ctx, midgard_instruction ins)
3357ec681f3Smrg{
3367ec681f3Smrg        midgard_instruction *u = mir_upload_ins(ctx, ins);
3377ec681f3Smrg
3387ec681f3Smrg        midgard_bundle bundle = {
3397ec681f3Smrg                .tag = ins.type,
3407ec681f3Smrg                .instruction_count = 1,
3417ec681f3Smrg                .instructions = { u },
3427ec681f3Smrg        };
3437ec681f3Smrg
3447ec681f3Smrg        if (bundle.tag == TAG_ALU_4) {
3457ec681f3Smrg                assert(OP_IS_MOVE(u->op));
3467ec681f3Smrg                u->unit = UNIT_VMUL;
3477ec681f3Smrg
3487ec681f3Smrg                size_t bytes_emitted = sizeof(uint32_t) + sizeof(midgard_reg_info) + sizeof(midgard_vector_alu);
3497ec681f3Smrg                bundle.padding = ~(bytes_emitted - 1) & 0xF;
3507ec681f3Smrg                bundle.control = ins.type | u->unit;
3517ec681f3Smrg        }
3527ec681f3Smrg
3537ec681f3Smrg        return bundle;
3547ec681f3Smrg}
3557ec681f3Smrg
3567ec681f3Smrgstatic unsigned
3577ec681f3Smrgmir_bundle_idx_for_ins(midgard_instruction *tag, midgard_block *block)
3587ec681f3Smrg{
3597ec681f3Smrg        midgard_bundle *bundles =
3607ec681f3Smrg                (midgard_bundle *) block->bundles.data;
3617ec681f3Smrg
3627ec681f3Smrg        size_t count = (block->bundles.size / sizeof(midgard_bundle));
3637ec681f3Smrg
3647ec681f3Smrg        for (unsigned i = 0; i < count; ++i) {
3657ec681f3Smrg                for (unsigned j = 0; j < bundles[i].instruction_count; ++j) {
3667ec681f3Smrg                        if (bundles[i].instructions[j] == tag)
3677ec681f3Smrg                                return i;
3687ec681f3Smrg                }
3697ec681f3Smrg        }
3707ec681f3Smrg
3717ec681f3Smrg        mir_print_instruction(tag);
3727ec681f3Smrg        unreachable("Instruction not scheduled in block");
3737ec681f3Smrg}
3747ec681f3Smrg
3757ec681f3Smrgvoid
3767ec681f3Smrgmir_insert_instruction_before_scheduled(
3777ec681f3Smrg        compiler_context *ctx,
3787ec681f3Smrg        midgard_block *block,
3797ec681f3Smrg        midgard_instruction *tag,
3807ec681f3Smrg        midgard_instruction ins)
3817ec681f3Smrg{
3827ec681f3Smrg        unsigned before = mir_bundle_idx_for_ins(tag, block);
3837ec681f3Smrg        size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
3847ec681f3Smrg        UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
3857ec681f3Smrg
3867ec681f3Smrg        midgard_bundle *bundles = (midgard_bundle *) block->bundles.data;
3877ec681f3Smrg        memmove(bundles + before + 1, bundles + before, (count - before) * sizeof(midgard_bundle));
3887ec681f3Smrg        midgard_bundle *before_bundle = bundles + before + 1;
3897ec681f3Smrg
3907ec681f3Smrg        midgard_bundle new = mir_bundle_for_op(ctx, ins);
3917ec681f3Smrg        memcpy(bundles + before, &new, sizeof(new));
3927ec681f3Smrg
3937ec681f3Smrg        list_addtail(&new.instructions[0]->link, &before_bundle->instructions[0]->link);
3947ec681f3Smrg        block->quadword_count += midgard_tag_props[new.tag].size;
3957ec681f3Smrg}
3967ec681f3Smrg
3977ec681f3Smrgvoid
3987ec681f3Smrgmir_insert_instruction_after_scheduled(
3997ec681f3Smrg        compiler_context *ctx,
4007ec681f3Smrg        midgard_block *block,
4017ec681f3Smrg        midgard_instruction *tag,
4027ec681f3Smrg        midgard_instruction ins)
4037ec681f3Smrg{
4047ec681f3Smrg        /* We need to grow the bundles array to add our new bundle */
4057ec681f3Smrg        size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
4067ec681f3Smrg        UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
4077ec681f3Smrg
4087ec681f3Smrg        /* Find the bundle that we want to insert after */
4097ec681f3Smrg        unsigned after = mir_bundle_idx_for_ins(tag, block);
4107ec681f3Smrg
4117ec681f3Smrg        /* All the bundles after that one, we move ahead by one */
4127ec681f3Smrg        midgard_bundle *bundles = (midgard_bundle *) block->bundles.data;
4137ec681f3Smrg        memmove(bundles + after + 2, bundles + after + 1, (count - after - 1) * sizeof(midgard_bundle));
4147ec681f3Smrg        midgard_bundle *after_bundle = bundles + after;
4157ec681f3Smrg
4167ec681f3Smrg        midgard_bundle new = mir_bundle_for_op(ctx, ins);
4177ec681f3Smrg        memcpy(bundles + after + 1, &new, sizeof(new));
4187ec681f3Smrg        list_add(&new.instructions[0]->link, &after_bundle->instructions[after_bundle->instruction_count - 1]->link);
4197ec681f3Smrg        block->quadword_count += midgard_tag_props[new.tag].size;
4207ec681f3Smrg}
4217ec681f3Smrg
4227ec681f3Smrg/* Flip the first-two arguments of a (binary) op. Currently ALU
4237ec681f3Smrg * only, no known uses for ldst/tex */
4247ec681f3Smrg
4257ec681f3Smrgvoid
4267ec681f3Smrgmir_flip(midgard_instruction *ins)
4277ec681f3Smrg{
4287ec681f3Smrg        unsigned temp = ins->src[0];
4297ec681f3Smrg        ins->src[0] = ins->src[1];
4307ec681f3Smrg        ins->src[1] = temp;
4317ec681f3Smrg
4327ec681f3Smrg        assert(ins->type == TAG_ALU_4);
4337ec681f3Smrg
4347ec681f3Smrg        temp = ins->src_types[0];
4357ec681f3Smrg        ins->src_types[0] = ins->src_types[1];
4367ec681f3Smrg        ins->src_types[1] = temp;
4377ec681f3Smrg
4387ec681f3Smrg        temp = ins->src_abs[0];
4397ec681f3Smrg        ins->src_abs[0] = ins->src_abs[1];
4407ec681f3Smrg        ins->src_abs[1] = temp;
4417ec681f3Smrg
4427ec681f3Smrg        temp = ins->src_neg[0];
4437ec681f3Smrg        ins->src_neg[0] = ins->src_neg[1];
4447ec681f3Smrg        ins->src_neg[1] = temp;
4457ec681f3Smrg
4467ec681f3Smrg        temp = ins->src_invert[0];
4477ec681f3Smrg        ins->src_invert[0] = ins->src_invert[1];
4487ec681f3Smrg        ins->src_invert[1] = temp;
4497ec681f3Smrg
4507ec681f3Smrg        unsigned temp_swizzle[16];
4517ec681f3Smrg        memcpy(temp_swizzle, ins->swizzle[0], sizeof(ins->swizzle[0]));
4527ec681f3Smrg        memcpy(ins->swizzle[0], ins->swizzle[1], sizeof(ins->swizzle[0]));
4537ec681f3Smrg        memcpy(ins->swizzle[1], temp_swizzle, sizeof(ins->swizzle[0]));
4547ec681f3Smrg}
4557ec681f3Smrg
4567ec681f3Smrg/* Before squashing, calculate ctx->temp_count just by observing the MIR */
4577ec681f3Smrg
4587ec681f3Smrgvoid
4597ec681f3Smrgmir_compute_temp_count(compiler_context *ctx)
4607ec681f3Smrg{
4617ec681f3Smrg        if (ctx->temp_count)
4627ec681f3Smrg                return;
4637ec681f3Smrg
4647ec681f3Smrg        unsigned max_dest = 0;
4657ec681f3Smrg
4667ec681f3Smrg        mir_foreach_instr_global(ctx, ins) {
4677ec681f3Smrg                if (ins->dest < SSA_FIXED_MINIMUM)
4687ec681f3Smrg                        max_dest = MAX2(max_dest, ins->dest + 1);
4697ec681f3Smrg        }
4707ec681f3Smrg
4717ec681f3Smrg        ctx->temp_count = max_dest;
4727ec681f3Smrg}
473