1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef ROGUE_NIR_HELPERS_H
25 #define ROGUE_NIR_HELPERS_H
26
27 #include <assert.h>
28 #include <stddef.h>
29 #include <stdint.h>
30
31 #include "nir/nir.h"
32 #include "util/bitscan.h"
33
34 /**
35 * \file rogue_nir.c
36 *
37 * \brief Contains various NIR helper functions.
38 */
39
nir_alu_dest_regindex(const nir_alu_instr * alu)40 static inline unsigned nir_alu_dest_regindex(const nir_alu_instr *alu)
41 {
42 assert(!alu->dest.dest.is_ssa);
43
44 return alu->dest.dest.reg.reg->index;
45 }
46
nir_alu_dest_comp(const nir_alu_instr * alu)47 static inline unsigned nir_alu_dest_comp(const nir_alu_instr *alu)
48 {
49 assert(!alu->dest.dest.is_ssa);
50 assert(util_is_power_of_two_nonzero(alu->dest.write_mask));
51
52 return ffs(alu->dest.write_mask) - 1;
53 }
54
nir_alu_src_regindex(const nir_alu_instr * alu,size_t src)55 static inline unsigned nir_alu_src_regindex(const nir_alu_instr *alu,
56 size_t src)
57 {
58 assert(src < nir_op_infos[alu->op].num_inputs);
59 assert(!alu->src[src].src.is_ssa);
60
61 return alu->src[src].src.reg.reg->index;
62 }
63
nir_alu_src_const(const nir_alu_instr * alu,size_t src)64 static inline uint32_t nir_alu_src_const(const nir_alu_instr *alu, size_t src)
65 {
66 assert(src < nir_op_infos[alu->op].num_inputs);
67 assert(alu->src[src].src.is_ssa);
68
69 nir_const_value *const_value = nir_src_as_const_value(alu->src[src].src);
70
71 return nir_const_value_as_uint(*const_value, 32);
72 }
73
nir_alu_src_is_const(const nir_alu_instr * alu,size_t src)74 static inline bool nir_alu_src_is_const(const nir_alu_instr *alu, size_t src)
75 {
76 assert(src < nir_op_infos[alu->op].num_inputs);
77
78 if (!alu->src[src].src.is_ssa)
79 return false;
80
81 assert(alu->src[src].src.ssa->parent_instr);
82
83 return (alu->src[src].src.ssa->parent_instr->type ==
84 nir_instr_type_load_const);
85 }
86
nir_intr_dest_regindex(const nir_intrinsic_instr * intr)87 static inline unsigned nir_intr_dest_regindex(const nir_intrinsic_instr *intr)
88 {
89 assert(!intr->dest.is_ssa);
90
91 return intr->dest.reg.reg->index;
92 }
93
nir_intr_src_regindex(const nir_intrinsic_instr * intr,size_t src)94 static inline unsigned nir_intr_src_regindex(const nir_intrinsic_instr *intr,
95 size_t src)
96 {
97 assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs);
98 assert(!intr->src[src].is_ssa);
99
100 return intr->src[src].reg.reg->index;
101 }
102
nir_intr_src_const(const nir_intrinsic_instr * intr,size_t src)103 static inline uint32_t nir_intr_src_const(const nir_intrinsic_instr *intr,
104 size_t src)
105 {
106 assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs);
107 assert(intr->src[src].is_ssa);
108
109 nir_const_value *const_value = nir_src_as_const_value(intr->src[src]);
110
111 return nir_const_value_as_uint(*const_value, 32);
112 }
113
nir_intr_src_comp_const(const nir_intrinsic_instr * intr,size_t src,size_t comp)114 static inline uint32_t nir_intr_src_comp_const(const nir_intrinsic_instr *intr,
115 size_t src,
116 size_t comp)
117 {
118 assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs);
119 assert(intr->src[src].is_ssa);
120 assert(comp < nir_src_num_components(intr->src[src]));
121
122 return nir_src_comp_as_uint(intr->src[src], comp);
123 }
124
nir_intr_src_is_const(const nir_intrinsic_instr * intr,size_t src)125 static inline bool nir_intr_src_is_const(const nir_intrinsic_instr *intr,
126 size_t src)
127 {
128 assert(src < nir_intrinsic_infos[intr->intrinsic].num_srcs);
129
130 if (!intr->src[src].is_ssa)
131 return false;
132
133 assert(intr->src[src].ssa->parent_instr);
134
135 return (intr->src[src].ssa->parent_instr->type == nir_instr_type_load_const);
136 }
137
nir_count_variables_with_modes(const nir_shader * nir,nir_variable_mode mode)138 static inline size_t nir_count_variables_with_modes(const nir_shader *nir,
139 nir_variable_mode mode)
140 {
141 size_t count = 0;
142
143 nir_foreach_variable_with_modes (var, nir, mode)
144 ++count;
145
146 return count;
147 }
148
149 #endif /* ROGUE_NIR_HELPERS_H */
150