Debian Codegraph @

src/imagination/pco/pco_const_imms.c

raw
1 /*
2 * Copyright © 2024 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 /**
8 * \file pco_const_imms.c
9 *
10 * \brief PCO constant immediates lowering pass.
11 */
12
13 #include "pco.h"
14 #include "pco_builder.h"
15 #include "pco_internal.h"
16 #include "util/macros.h"
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21
22 /** Mapping of constant register values and their indices. */
23 struct const_reg_def {
24 uint32_t val;
25 uint8_t idx;
26 bool flr : 1;
27 bool neg : 1;
28 };
29
30 /** Constant register values (sorted for bsearch). */
31 static const struct const_reg_def const_reg_defs[] = {
32 { 0x00000000, 0, false, false }, /* */
33 { 0x00000001, 1, false, false }, /* */
34 { 0x00000002, 2, false, false }, /* */
35 { 0x00000003, 3, false, false }, /* */
36 { 0x00000004, 4, false, false }, /* */
37 { 0x00000005, 5, false, false }, /* */
38 { 0x00000006, 6, false, false }, /* */
39 { 0x00000007, 7, false, false }, /* */
40 { 0x00000008, 8, false, false }, /* */
41 { 0x00000009, 9, false, false }, /* */
42 { 0x0000000a, 10, false, false }, /* */
43 { 0x0000000b, 11, false, false }, /* */
44 { 0x0000000c, 12, false, false }, /* */
45 { 0x0000000d, 13, false, false }, /* */
46 { 0x0000000e, 14, false, false }, /* */
47 { 0x0000000f, 15, false, false }, /* */
48 { 0x00000010, 16, false, false }, /* */
49 { 0x00000011, 17, false, false }, /* */
50 { 0x00000012, 18, false, false }, /* */
51 { 0x00000013, 19, false, false }, /* */
52 { 0x00000014, 20, false, false }, /* */
53 { 0x00000015, 21, false, false }, /* */
54 { 0x00000016, 22, false, false }, /* */
55 { 0x00000017, 23, false, false }, /* */
56 { 0x00000018, 24, false, false }, /* */
57 { 0x00000019, 25, false, false }, /* */
58 { 0x0000001a, 26, false, false }, /* */
59 { 0x0000001b, 27, false, false }, /* */
60 { 0x0000001c, 28, false, false }, /* */
61 { 0x0000001d, 29, false, false }, /* */
62 { 0x0000001e, 30, false, false }, /* */
63 { 0x0000001f, 31, false, false }, /* */
64 { 0x0000007f, 147, false, false }, /* */
65 { 0x37800000, 134, false, false }, /* */
66 { 0x38000000, 135, false, false }, /* */
67 { 0x38800000, 88, false, false }, /* */
68 { 0x39000000, 87, false, false }, /* */
69 { 0x39800000, 86, false, false }, /* */
70 { 0x3a000000, 85, false, false }, /* */
71 { 0x3a800000, 84, false, false }, /* */
72 { 0x3b000000, 83, false, false }, /* */
73 { 0x3b4d2e1c, 136, false, false }, /* */
74 { 0x3b800000, 82, false, false }, /* */
75 { 0x3c000000, 81, false, false }, /* */
76 { 0x3c800000, 80, false, false }, /* */
77 { 0x3d000000, 79, false, false }, /* */
78 { 0x3d25aee6, 156, false, false }, /* */
79 { 0x3d6147ae, 140, false, false }, /* */
80 { 0x3d800000, 78, false, false }, /* */
81 { 0x3d9e8391, 157, false, false }, /* */
82 { 0x3e000000, 77, false, false }, /* */
83 { 0x3e2aaaab, 153, false, false }, /* */
84 { 0x3e800000, 76, false, false }, /* */
85 { 0x3e9a209b, 145, false, false }, /* */
86 { 0x3ea2f983, 128, false, false }, /* */
87 { 0x3eaaaaab, 152, false, false }, /* */
88 { 0x3ebc5ab2, 90, false, false }, /* */
89 { 0x3ed55555, 138, false, false }, /* */
90 { 0x3f000000, 75, false, false }, /* */
91 { 0x3f22f983, 129, false, false }, /* */
92 { 0x3f317218, 146, false, false }, /* */
93 { 0x3f3504f3, 92, false, false }, /* */
94 { 0x3f490fdb, 93, false, false }, /* */
95 { 0x3f72a76f, 158, false, false }, /* */
96 { 0x3f800000, 64, false, false }, /* */
97 { 0x3f860a92, 151, false, false }, /* */
98 { 0x3f870a3d, 139, false, false }, /* */
99 { 0x3fa2f983, 130, false, false }, /* */
100 { 0x3fb504f3, 91, false, false }, /* */
101 { 0x3fb8aa3b, 155, false, false }, /* */
102 { 0x3fc90fdb, 94, false, false }, /* */
103 { 0x40000000, 65, false, false }, /* */
104 { 0x4019999a, 159, false, false }, /* */
105 { 0x402df854, 89, false, false }, /* */
106 { 0x40490fdb, 95, false, false }, /* */
107 { 0x40549a78, 154, false, false }, /* */
108 { 0x40800000, 66, false, false }, /* */
109 { 0x40c90fdb, 131, false, false }, /* */
110 { 0x41000000, 67, false, false }, /* */
111 { 0x41490fdb, 132, false, false }, /* */
112 { 0x414eb852, 137, false, false }, /* */
113 { 0x41800000, 68, false, false }, /* */
114 { 0x41c90fdb, 133, false, false }, /* */
115 { 0x42000000, 69, false, false }, /* */
116 { 0x42800000, 70, false, false }, /* */
117 { 0x43000000, 71, false, false }, /* */
118 { 0x43800000, 72, false, false }, /* */
119 { 0x44000000, 73, false, false }, /* */
120 { 0x44800000, 74, false, false }, /* */
121 { 0x4b000000, 149, false, false }, /* */
122 { 0x4b800000, 150, false, false }, /* */
123 { 0x7f7fffff, 148, false, false }, /* */
124 { 0x7f800000, 142, false, false }, /* */
125 { 0x7fff7fff, 144, false, false }, /* */
126 { 0x80000000, 141, false, false }, /* */
127 { 0xffffffff, 143, false, false }, /* */
128 };
129
130 /**
131 * \brief Comparison function for bsearch() to support rogue_const_reg_def.
132 *
133 * \param[in] lhs The left hand side of the comparison.
134 * \param[in] rhs The right hand side of the comparison.
135 * \return 0 if (lhs == rhs), -1 if (lhs < rhs), 1 if (lhs > rhs).
136 */
137 static int constreg_cmp(const void *lhs, const void *rhs)
138 {
139 const struct const_reg_def *l = lhs;
140 const struct const_reg_def *r = rhs;
141
142 if (l->val < r->val)
143 return -1;
144 else if (l->val > r->val)
145 return 1;
146
147 return 0;
148 }
149
150 /**
151 * \brief Looks up an immediate in constant registers.
152 *
153 * \param[in] imm The immediate to lookup.
154 * \return A pointer to the constant register definition,
155 * or NULL if no constant register was found.
156 */
157 inline static const struct const_reg_def *constreg_lookup(uint32_t imm)
158 {
159 return bsearch(&(struct const_reg_def){ .val = imm },
160 const_reg_defs,
161 ARRAY_SIZE(const_reg_defs),
162 sizeof(*const_reg_defs),
163 constreg_cmp);
164 }
165
166 /**
167 * \brief Converts immediates into constant register lookups where possible.
168 *
169 * \param[in,out] shader PCO shader.
170 * \return True if the pass made progress.
171 */
172 bool pco_const_imms(pco_shader *shader)
173 {
174 bool progress = false;
175
176 pco_foreach_func_in_shader (func, shader) {
177 pco_foreach_instr_in_func_safe (instr, func) {
178 if (instr->op != PCO_OP_MOVI32)
179 continue;
180
181 const struct const_reg_def *const_reg_def =
182 constreg_lookup(pco_ref_get_imm(instr->src[0]));
183
184 if (!const_reg_def)
185 continue;
186
187 pco_builder b =
188 pco_builder_create(func, pco_cursor_before_instr(instr));
189
190 pco_ref dest = instr->dest[0];
191 pco_ref const_reg =
192 pco_ref_hwreg(const_reg_def->idx, PCO_REG_CLASS_CONST);
193
194 if (!const_reg_def->flr && !const_reg_def->neg) {
195 pco_mov(&b, dest, const_reg);
196 } else if (!const_reg_def->flr && const_reg_def->neg) {
197 pco_fneg(&b, dest, const_reg);
198 } else if (const_reg_def->flr && !const_reg_def->neg) {
199 pco_fflr(&b, dest, const_reg);
200 } else {
201 /* TODO: use floor and neg mods when support for > 1 is added. */
202 const_reg = pco_ref_flr(const_reg);
203 const_reg = pco_ref_neg(const_reg);
204
205 pco_fadd(&b, dest, const_reg, pco_zero);
206 }
207
208 pco_instr_delete(instr);
209 progress = true;
210 }
211 }
212
213 return progress;
214 }