1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2019 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors (Collabora):
24bf215546Sopenharmony_ci *   Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "compiler.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/* When we're 'squeezing down' the values in the IR, we maintain a hash
30bf215546Sopenharmony_ci * as such */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic unsigned
33bf215546Sopenharmony_cifind_or_allocate_temp(compiler_context *ctx, struct hash_table_u64 *map,
34bf215546Sopenharmony_ci                unsigned hash)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci        if (hash >= SSA_FIXED_MINIMUM)
37bf215546Sopenharmony_ci                return hash;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci        unsigned temp = (uintptr_t) _mesa_hash_table_u64_search(
40bf215546Sopenharmony_ci                                map, hash + 1);
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci        if (temp)
43bf215546Sopenharmony_ci                return temp - 1;
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci        /* If no temp is find, allocate one */
46bf215546Sopenharmony_ci        temp = ctx->temp_count++;
47bf215546Sopenharmony_ci        ctx->max_hash = MAX2(ctx->max_hash, hash);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci        _mesa_hash_table_u64_insert(map,
50bf215546Sopenharmony_ci                                    hash + 1, (void *) ((uintptr_t) temp + 1));
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci        return temp;
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci/* Reassigns numbering to get rid of gaps in the indices and to prioritize
56bf215546Sopenharmony_ci * smaller register classes */
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_civoid
59bf215546Sopenharmony_cimir_squeeze_index(compiler_context *ctx)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci        struct hash_table_u64 *map = _mesa_hash_table_u64_create(NULL);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci        /* Reset */
64bf215546Sopenharmony_ci        ctx->temp_count = 0;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci        /* We need to prioritize texture registers on older GPUs so we don't
67bf215546Sopenharmony_ci         * fail RA trying to assign to work registers r0/r1 when a work
68bf215546Sopenharmony_ci         * register is already there */
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci        mir_foreach_instr_global(ctx, ins) {
71bf215546Sopenharmony_ci                if (ins->type == TAG_TEXTURE_4)
72bf215546Sopenharmony_ci                        ins->dest = find_or_allocate_temp(ctx, map, ins->dest);
73bf215546Sopenharmony_ci        }
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci        mir_foreach_instr_global(ctx, ins) {
76bf215546Sopenharmony_ci                if (ins->type != TAG_TEXTURE_4)
77bf215546Sopenharmony_ci                        ins->dest = find_or_allocate_temp(ctx, map, ins->dest);
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci                for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i)
80bf215546Sopenharmony_ci                        ins->src[i] = find_or_allocate_temp(ctx, map, ins->src[i]);
81bf215546Sopenharmony_ci        }
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci        ctx->blend_input = find_or_allocate_temp(ctx, map, ctx->blend_input);
84bf215546Sopenharmony_ci        ctx->blend_src1 = find_or_allocate_temp(ctx, map, ctx->blend_src1);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci        _mesa_hash_table_u64_destroy(map);
87bf215546Sopenharmony_ci}
88