1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights
7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
9bf215546Sopenharmony_ci * 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 THE
18bf215546Sopenharmony_ci * 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
24bf215546Sopenharmony_ci#include "nir/nir.h"
25bf215546Sopenharmony_ci#include "nir/nir_builder.h"
26bf215546Sopenharmony_ci#include "nir/nir_search_helpers.h"
27bf215546Sopenharmony_ci#include "rogue_constreg.h"
28bf215546Sopenharmony_ci#include "rogue_nir.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/* TODO: optimize: if value is in const regs, replace, else, use shared regs and
31bf215546Sopenharmony_ci * notify driver they need to be populated?
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/* Replaces multiple ssa uses from load_const with a single use -> a register.
35bf215546Sopenharmony_ci */
36bf215546Sopenharmony_civoid rogue_nir_constreg(nir_shader *shader)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   nir_function_impl *impl = nir_shader_get_entrypoint(shader);
39bf215546Sopenharmony_ci   nir_builder b;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   /* Find load_const instructions. */
44bf215546Sopenharmony_ci   nir_foreach_block (block, impl) {
45bf215546Sopenharmony_ci      nir_foreach_instr_safe (instr, block) {
46bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_load_const)
47bf215546Sopenharmony_ci            continue;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci         nir_load_const_instr *load_const = nir_instr_as_load_const(instr);
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci         /* Skip values that can be pulled from constant registers. */
52bf215546Sopenharmony_ci         uint32_t value = nir_const_value_as_uint(load_const->value[0], 32);
53bf215546Sopenharmony_ci         size_t const_reg = rogue_constreg_lookup(value);
54bf215546Sopenharmony_ci         if (const_reg != ROGUE_NO_CONST_REG)
55bf215546Sopenharmony_ci            continue;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci         b.cursor = nir_after_instr(&load_const->instr);
58bf215546Sopenharmony_ci         nir_ssa_def *mov = nir_mov(&b, &load_const->def);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci         nir_foreach_use_safe (use_src, &load_const->def) {
61bf215546Sopenharmony_ci            if (use_src->parent_instr == mov->parent_instr)
62bf215546Sopenharmony_ci               continue;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci            /* Skip when used as an index for intrinsics, as we want to
65bf215546Sopenharmony_ci             * access that value directly.
66bf215546Sopenharmony_ci             */
67bf215546Sopenharmony_ci            if (use_src->parent_instr->type == nir_instr_type_intrinsic)
68bf215546Sopenharmony_ci               continue;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci            nir_instr_rewrite_src_ssa(use_src->parent_instr, use_src, mov);
71bf215546Sopenharmony_ci         }
72bf215546Sopenharmony_ci      }
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci}
75