45df2397ddc3e02a97072a65d8c13b95f482c33b
[mesa.git] / src / freedreno / ir3 / ir3_ra.h
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
18 * THE 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 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #ifndef IR3_RA_H_
28 #define IR3_RA_H_
29
30 #include "util/bitset.h"
31
32
33 static const unsigned class_sizes[] = {
34 1, 2, 3, 4,
35 4 + 4, /* txd + 1d/2d */
36 4 + 6, /* txd + 3d */
37 };
38 #define class_count ARRAY_SIZE(class_sizes)
39
40 static const unsigned half_class_sizes[] = {
41 1, 2, 3, 4,
42 };
43 #define half_class_count ARRAY_SIZE(half_class_sizes)
44
45 /* seems to just be used for compute shaders? Seems like vec1 and vec3
46 * are sufficient (for now?)
47 */
48 static const unsigned high_class_sizes[] = {
49 1, 3,
50 };
51 #define high_class_count ARRAY_SIZE(high_class_sizes)
52
53 #define total_class_count (class_count + half_class_count + high_class_count)
54
55 /* Below a0.x are normal regs. RA doesn't need to assign a0.x/p0.x. */
56 #define NUM_REGS (4 * 48) /* r0 to r47 */
57 #define NUM_HIGH_REGS (4 * 8) /* r48 to r55 */
58 #define FIRST_HIGH_REG (4 * 48)
59 /* Number of virtual regs in a given class: */
60
61 static inline unsigned CLASS_REGS(unsigned i)
62 {
63 assert(i < class_count);
64
65 return (NUM_REGS - (class_sizes[i] - 1));
66 }
67
68 static inline unsigned HALF_CLASS_REGS(unsigned i)
69 {
70 assert(i < half_class_count);
71
72 return (NUM_REGS - (half_class_sizes[i] - 1));
73 }
74
75 static inline unsigned HIGH_CLASS_REGS(unsigned i)
76 {
77 assert(i < high_class_count);
78
79 return (NUM_HIGH_REGS - (high_class_sizes[i] - 1));
80 }
81
82 #define HALF_OFFSET (class_count)
83 #define HIGH_OFFSET (class_count + half_class_count)
84
85 /* register-set, created one time, used for all shaders: */
86 struct ir3_ra_reg_set {
87 struct ra_regs *regs;
88 unsigned int classes[class_count];
89 unsigned int half_classes[half_class_count];
90 unsigned int high_classes[high_class_count];
91
92 /* The virtual register space flattens out all the classes,
93 * starting with full, followed by half and then high, ie:
94 *
95 * scalar full (starting at zero)
96 * vec2 full
97 * vec3 full
98 * ...
99 * vecN full
100 * scalar half (starting at first_half_reg)
101 * vec2 half
102 * ...
103 * vecN half
104 * scalar high (starting at first_high_reg)
105 * ...
106 * vecN high
107 *
108 */
109 unsigned first_half_reg, first_high_reg;
110
111 /* maps flat virtual register space to base gpr: */
112 uint16_t *ra_reg_to_gpr;
113 /* maps cls,gpr to flat virtual register space: */
114 uint16_t **gpr_to_ra_reg;
115 };
116
117 /* additional block-data (per-block) */
118 struct ir3_ra_block_data {
119 BITSET_WORD *def; /* variables defined before used in block */
120 BITSET_WORD *use; /* variables used before defined in block */
121 BITSET_WORD *livein; /* which defs reach entry point of block */
122 BITSET_WORD *liveout; /* which defs reach exit point of block */
123 };
124
125 /* additional instruction-data (per-instruction) */
126 struct ir3_ra_instr_data {
127 /* cached instruction 'definer' info: */
128 struct ir3_instruction *defn;
129 int off, sz, cls;
130 };
131
132 /* register-assign context, per-shader */
133 struct ir3_ra_ctx {
134 struct ir3_shader_variant *v;
135 struct ir3 *ir;
136
137 struct ir3_ra_reg_set *set;
138 struct ra_graph *g;
139
140 /* Are we in the scalar assignment pass? In this pass, all larger-
141 * than-vec1 vales have already been assigned and pre-colored, so
142 * we only consider scalar values.
143 */
144 bool scalar_pass;
145
146 unsigned alloc_count;
147 /* one per class, plus one slot for arrays: */
148 unsigned class_alloc_count[total_class_count + 1];
149 unsigned class_base[total_class_count + 1];
150 unsigned instr_cnt;
151 unsigned *def, *use; /* def/use table */
152 struct ir3_ra_instr_data *instrd;
153
154 /* Mapping vreg name back to instruction, used select reg callback: */
155 struct hash_table *name_to_instr;
156
157 /* Tracking for select_reg callback */
158 unsigned start_search_reg;
159 unsigned max_target;
160
161 /* Temporary buffer for def/use iterators
162 *
163 * The worst case should probably be an array w/ relative access (ie.
164 * all elements are def'd or use'd), and that can't be larger than
165 * the number of registers.
166 *
167 * NOTE we could declare this on the stack if needed, but I don't
168 * think there is a need for nested iterators.
169 */
170 unsigned namebuf[NUM_REGS];
171 unsigned namecnt, nameidx;
172 };
173
174 static inline int
175 ra_name(struct ir3_ra_ctx *ctx, struct ir3_ra_instr_data *id)
176 {
177 unsigned name;
178 debug_assert(id->cls >= 0);
179 debug_assert(id->cls < total_class_count); /* we shouldn't get arrays here.. */
180 name = ctx->class_base[id->cls] + id->defn->name;
181 debug_assert(name < ctx->alloc_count);
182 return name;
183 }
184
185 /* Get the scalar name of the n'th component of an instruction dst: */
186 static inline int
187 scalar_name(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr, unsigned n)
188 {
189 if (ctx->scalar_pass) {
190 if (instr->opc == OPC_META_SPLIT) {
191 debug_assert(n == 0); /* split results in a scalar */
192 struct ir3_instruction *src = instr->regs[1]->instr;
193 return scalar_name(ctx, src, instr->split.off);
194 } else if (instr->opc == OPC_META_COLLECT) {
195 debug_assert(n < (instr->regs_count + 1));
196 struct ir3_instruction *src = instr->regs[n + 1]->instr;
197 return scalar_name(ctx, src, 0);
198 }
199 } else {
200 debug_assert(n == 0);
201 }
202
203 return ra_name(ctx, &ctx->instrd[instr->ip]) + n;
204 }
205
206 static inline bool
207 writes_gpr(struct ir3_instruction *instr)
208 {
209 if (dest_regs(instr) == 0)
210 return false;
211 /* is dest a normal temp register: */
212 struct ir3_register *reg = instr->regs[0];
213 debug_assert(!(reg->flags & (IR3_REG_CONST | IR3_REG_IMMED)));
214 if ((reg_num(reg) == REG_A0) ||
215 (reg->num == regid(REG_P0, 0)))
216 return false;
217 return true;
218 }
219
220 #define NO_NAME ~0
221
222 /*
223 * Iterators to iterate the vreg names of an instructions def's and use's
224 */
225
226 static inline unsigned
227 __ra_name_cnt(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr)
228 {
229 if (!instr)
230 return 0;
231
232 /* Filter special cases, ie. writes to a0.x or p0.x, or non-ssa: */
233 if (!writes_gpr(instr) || (instr->regs[0]->flags & IR3_REG_ARRAY))
234 return 0;
235
236 /* in scalar pass, we aren't considering virtual register classes, ie.
237 * if an instruction writes a vec2, then it defines two different scalar
238 * register names.
239 */
240 if (ctx->scalar_pass)
241 return dest_regs(instr);
242
243 return 1;
244 }
245
246 #define foreach_name_n(__name, __n, __ctx, __instr) \
247 for (unsigned __cnt = __ra_name_cnt(__ctx, __instr), __n = 0, __name; \
248 (__n < __cnt) && ({__name = scalar_name(__ctx, __instr, __n); 1;}); __n++)
249
250 #define foreach_name(__name, __ctx, __instr) \
251 foreach_name_n(__name, __n, __ctx, __instr)
252
253 static inline unsigned
254 __ra_itr_pop(struct ir3_ra_ctx *ctx)
255 {
256 if (ctx->nameidx < ctx->namecnt)
257 return ctx->namebuf[ctx->nameidx++];
258 return NO_NAME;
259 }
260
261 static inline void
262 __ra_itr_push(struct ir3_ra_ctx *ctx, unsigned name)
263 {
264 assert(ctx->namecnt < ARRAY_SIZE(ctx->namebuf));
265 ctx->namebuf[ctx->namecnt++] = name;
266 }
267
268 static inline unsigned
269 __ra_init_def_itr(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr)
270 {
271 /* nested use is not supported: */
272 assert(ctx->namecnt == ctx->nameidx);
273
274 ctx->namecnt = ctx->nameidx = 0;
275
276 if (!writes_gpr(instr))
277 return NO_NAME;
278
279 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
280 struct ir3_register *dst = instr->regs[0];
281
282 if (dst->flags & IR3_REG_ARRAY) {
283 struct ir3_array *arr = ir3_lookup_array(ctx->ir, dst->array.id);
284
285 /* indirect write is treated like a write to all array
286 * elements, since we don't know which one is actually
287 * written:
288 */
289 if (dst->flags & IR3_REG_RELATIV) {
290 for (unsigned i = 0; i < arr->length; i++) {
291 __ra_itr_push(ctx, arr->base + i);
292 }
293 } else {
294 __ra_itr_push(ctx, arr->base + dst->array.offset);
295 debug_assert(dst->array.offset < arr->length);
296 }
297 } else if (id->defn == instr) {
298 foreach_name_n (name, i, ctx, instr) {
299 /* tex instructions actually have a wrmask, and
300 * don't touch masked out components. We can't do
301 * anything useful about that in the first pass,
302 * but in the scalar pass we can realize these
303 * registers are available:
304 */
305 if (ctx->scalar_pass && is_tex_or_prefetch(instr) &&
306 !(instr->regs[0]->wrmask & (1 << i)))
307 continue;
308 __ra_itr_push(ctx, name);
309 }
310 }
311
312 return __ra_itr_pop(ctx);
313 }
314
315 static inline unsigned
316 __ra_init_use_itr(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr)
317 {
318 /* nested use is not supported: */
319 assert(ctx->namecnt == ctx->nameidx);
320
321 ctx->namecnt = ctx->nameidx = 0;
322
323 struct ir3_register *reg;
324 foreach_src (reg, instr) {
325 if (reg->flags & IR3_REG_ARRAY) {
326 struct ir3_array *arr =
327 ir3_lookup_array(ctx->ir, reg->array.id);
328
329 /* indirect read is treated like a read from all array
330 * elements, since we don't know which one is actually
331 * read:
332 */
333 if (reg->flags & IR3_REG_RELATIV) {
334 for (unsigned i = 0; i < arr->length; i++) {
335 __ra_itr_push(ctx, arr->base + i);
336 }
337 } else {
338 __ra_itr_push(ctx, arr->base + reg->array.offset);
339 debug_assert(reg->array.offset < arr->length);
340 }
341 } else {
342 foreach_name_n (name, i, ctx, reg->instr) {
343 /* split takes a src w/ wrmask potentially greater
344 * than 0x1, but it really only cares about a single
345 * component. This shows up in splits coming out of
346 * a tex instruction w/ wrmask=.z, for example.
347 */
348 if (ctx->scalar_pass && (instr->opc == OPC_META_SPLIT) &&
349 !(i == instr->split.off))
350 continue;
351 __ra_itr_push(ctx, name);
352 }
353 }
354 }
355
356 return __ra_itr_pop(ctx);
357 }
358
359 #define foreach_def(__name, __ctx, __instr) \
360 for (unsigned __name = __ra_init_def_itr(__ctx, __instr); \
361 __name != NO_NAME; __name = __ra_itr_pop(__ctx))
362
363 #define foreach_use(__name, __ctx, __instr) \
364 for (unsigned __name = __ra_init_use_itr(__ctx, __instr); \
365 __name != NO_NAME; __name = __ra_itr_pop(__ctx))
366
367 int ra_size_to_class(unsigned sz, bool half, bool high);
368 int ra_class_to_size(unsigned class, bool *half, bool *high);
369
370 #endif /* IR3_RA_H_ */