17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2020 Collabora Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg * Authors (Collabora):
247ec681f3Smrg *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
257ec681f3Smrg */
267ec681f3Smrg
277ec681f3Smrg#include "pan_ir.h"
287ec681f3Smrg#include "util/macros.h"
297ec681f3Smrg
307ec681f3Smrg/* Converts a per-component mask to a byte mask */
317ec681f3Smrg
327ec681f3Smrguint16_t
337ec681f3Smrgpan_to_bytemask(unsigned bytes, unsigned mask)
347ec681f3Smrg{
357ec681f3Smrg        switch (bytes) {
367ec681f3Smrg        case 0:
377ec681f3Smrg                assert(mask == 0);
387ec681f3Smrg                return 0;
397ec681f3Smrg
407ec681f3Smrg        case 8:
417ec681f3Smrg                return mask;
427ec681f3Smrg
437ec681f3Smrg        case 16: {
447ec681f3Smrg                unsigned space =
457ec681f3Smrg                        (mask & 0x1) |
467ec681f3Smrg                        ((mask & 0x2) << (2 - 1)) |
477ec681f3Smrg                        ((mask & 0x4) << (4 - 2)) |
487ec681f3Smrg                        ((mask & 0x8) << (6 - 3)) |
497ec681f3Smrg                        ((mask & 0x10) << (8 - 4)) |
507ec681f3Smrg                        ((mask & 0x20) << (10 - 5)) |
517ec681f3Smrg                        ((mask & 0x40) << (12 - 6)) |
527ec681f3Smrg                        ((mask & 0x80) << (14 - 7));
537ec681f3Smrg
547ec681f3Smrg                return space | (space << 1);
557ec681f3Smrg        }
567ec681f3Smrg
577ec681f3Smrg        case 32: {
587ec681f3Smrg                unsigned space =
597ec681f3Smrg                        (mask & 0x1) |
607ec681f3Smrg                        ((mask & 0x2) << (4 - 1)) |
617ec681f3Smrg                        ((mask & 0x4) << (8 - 2)) |
627ec681f3Smrg                        ((mask & 0x8) << (12 - 3));
637ec681f3Smrg
647ec681f3Smrg                return space | (space << 1) | (space << 2) | (space << 3);
657ec681f3Smrg        }
667ec681f3Smrg
677ec681f3Smrg        case 64: {
687ec681f3Smrg                unsigned A = (mask & 0x1) ? 0xFF : 0x00;
697ec681f3Smrg                unsigned B = (mask & 0x2) ? 0xFF : 0x00;
707ec681f3Smrg                return A | (B << 8);
717ec681f3Smrg        }
727ec681f3Smrg
737ec681f3Smrg        default:
747ec681f3Smrg                unreachable("Invalid register mode");
757ec681f3Smrg        }
767ec681f3Smrg}
777ec681f3Smrg
787ec681f3Smrgvoid
797ec681f3Smrgpan_block_add_successor(pan_block *block, pan_block *successor)
807ec681f3Smrg{
817ec681f3Smrg        assert(block);
827ec681f3Smrg        assert(successor);
837ec681f3Smrg
847ec681f3Smrg        /* Cull impossible edges */
857ec681f3Smrg        if (block->unconditional_jumps)
867ec681f3Smrg                return;
877ec681f3Smrg
887ec681f3Smrg        for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
897ec681f3Smrg                if (block->successors[i]) {
907ec681f3Smrg                       if (block->successors[i] == successor)
917ec681f3Smrg                               return;
927ec681f3Smrg                       else
937ec681f3Smrg                               continue;
947ec681f3Smrg                }
957ec681f3Smrg
967ec681f3Smrg                block->successors[i] = successor;
977ec681f3Smrg                _mesa_set_add(successor->predecessors, block);
987ec681f3Smrg                return;
997ec681f3Smrg        }
1007ec681f3Smrg
1017ec681f3Smrg        unreachable("Too many successors");
1027ec681f3Smrg}
1037ec681f3Smrg
1047ec681f3Smrg/* Prints a NIR ALU type in Bifrost-style ".f32" ".i8" etc */
1057ec681f3Smrg
1067ec681f3Smrgvoid
1077ec681f3Smrgpan_print_alu_type(nir_alu_type t, FILE *fp)
1087ec681f3Smrg{
1097ec681f3Smrg        unsigned size = nir_alu_type_get_type_size(t);
1107ec681f3Smrg        nir_alu_type base = nir_alu_type_get_base_type(t);
1117ec681f3Smrg
1127ec681f3Smrg        switch (base) {
1137ec681f3Smrg        case nir_type_int:
1147ec681f3Smrg                fprintf(fp, ".i");
1157ec681f3Smrg                break;
1167ec681f3Smrg        case nir_type_uint:
1177ec681f3Smrg                fprintf(fp, ".u");
1187ec681f3Smrg                break;
1197ec681f3Smrg        case nir_type_bool:
1207ec681f3Smrg                fprintf(fp, ".b");
1217ec681f3Smrg                break;
1227ec681f3Smrg        case nir_type_float:
1237ec681f3Smrg                fprintf(fp, ".f");
1247ec681f3Smrg                break;
1257ec681f3Smrg        default:
1267ec681f3Smrg                fprintf(fp, ".unknown");
1277ec681f3Smrg                break;
1287ec681f3Smrg        }
1297ec681f3Smrg
1307ec681f3Smrg        fprintf(fp, "%u", size);
1317ec681f3Smrg}
1327ec681f3Smrg
1337ec681f3Smrg/* Could optimize with a better data structure if anyone cares, TODO: profile */
1347ec681f3Smrg
1357ec681f3Smrgunsigned
1367ec681f3Smrgpan_lookup_pushed_ubo(struct panfrost_ubo_push *push, unsigned ubo, unsigned offs)
1377ec681f3Smrg{
1387ec681f3Smrg        struct panfrost_ubo_word word = {
1397ec681f3Smrg                .ubo = ubo,
1407ec681f3Smrg                .offset = offs
1417ec681f3Smrg        };
1427ec681f3Smrg
1437ec681f3Smrg        for (unsigned i = 0; i < push->count; ++i) {
1447ec681f3Smrg                if (memcmp(push->words + i, &word, sizeof(word)) == 0)
1457ec681f3Smrg                        return i;
1467ec681f3Smrg        }
1477ec681f3Smrg
1487ec681f3Smrg        unreachable("UBO not pushed");
1497ec681f3Smrg
1507ec681f3Smrg}
151