freedreno/ir3: bit of cleanup
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3.h
1 /*
2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
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
24 #ifndef IR3_H_
25 #define IR3_H_
26
27 #include <stdint.h>
28 #include <stdbool.h>
29
30 #include "util/u_debug.h"
31
32 #include "instr-a3xx.h"
33 #include "disasm.h" /* TODO move 'enum shader_t' somewhere else.. */
34
35 /* low level intermediate representation of an adreno shader program */
36
37 struct ir3;
38 struct ir3_instruction;
39 struct ir3_block;
40
41 struct ir3_info {
42 uint16_t sizedwords;
43 uint16_t instrs_count; /* expanded to account for rpt's */
44 /* NOTE: max_reg, etc, does not include registers not touched
45 * by the shader (ie. vertex fetched via VFD_DECODE but not
46 * touched by shader)
47 */
48 int8_t max_reg; /* highest GPR # used by shader */
49 int8_t max_half_reg;
50 int16_t max_const;
51 };
52
53 struct ir3_register {
54 enum {
55 IR3_REG_CONST = 0x001,
56 IR3_REG_IMMED = 0x002,
57 IR3_REG_HALF = 0x004,
58 IR3_REG_RELATIV= 0x008,
59 IR3_REG_R = 0x010,
60 IR3_REG_NEGATE = 0x020,
61 IR3_REG_ABS = 0x040,
62 IR3_REG_EVEN = 0x080,
63 IR3_REG_POS_INF= 0x100,
64 /* (ei) flag, end-input? Set on last bary, presumably to signal
65 * that the shader needs no more input:
66 */
67 IR3_REG_EI = 0x200,
68 /* meta-flags, for intermediate stages of IR, ie.
69 * before register assignment is done:
70 */
71 IR3_REG_SSA = 0x1000, /* 'instr' is ptr to assigning instr */
72 IR3_REG_IA = 0x2000, /* meta-input dst is "assigned" */
73 IR3_REG_ADDR = 0x4000, /* register is a0.x */
74 } flags;
75 union {
76 /* normal registers:
77 * the component is in the low two bits of the reg #, so
78 * rN.x becomes: (N << 2) | x
79 */
80 int num;
81 /* immediate: */
82 int iim_val;
83 float fim_val;
84 /* relative: */
85 int offset;
86 };
87
88 /* for IR3_REG_SSA, src registers contain ptr back to
89 * assigning instruction.
90 */
91 struct ir3_instruction *instr;
92
93 union {
94 /* used for cat5 instructions, but also for internal/IR level
95 * tracking of what registers are read/written by an instruction.
96 * wrmask may be a bad name since it is used to represent both
97 * src and dst that touch multiple adjacent registers.
98 */
99 unsigned wrmask;
100 /* for relative addressing, 32bits for array size is too small,
101 * but otoh we don't need to deal with disjoint sets, so instead
102 * use a simple size field (number of scalar components).
103 */
104 unsigned size;
105 };
106 };
107
108 struct ir3_instruction {
109 struct ir3_block *block;
110 int category;
111 opc_t opc;
112 enum {
113 /* (sy) flag is set on first instruction, and after sample
114 * instructions (probably just on RAW hazard).
115 */
116 IR3_INSTR_SY = 0x001,
117 /* (ss) flag is set on first instruction, and first instruction
118 * to depend on the result of "long" instructions (RAW hazard):
119 *
120 * rcp, rsq, log2, exp2, sin, cos, sqrt
121 *
122 * It seems to synchronize until all in-flight instructions are
123 * completed, for example:
124 *
125 * rsq hr1.w, hr1.w
126 * add.f hr2.z, (neg)hr2.z, hc0.y
127 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
128 * rsq hr2.x, hr2.x
129 * (rpt1)nop
130 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
131 * nop
132 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
133 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
134 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
135 *
136 * The last mul.f does not have (ss) set, presumably because the
137 * (ss) on the previous instruction does the job.
138 *
139 * The blob driver also seems to set it on WAR hazards, although
140 * not really clear if this is needed or just blob compiler being
141 * sloppy. So far I haven't found a case where removing the (ss)
142 * causes problems for WAR hazard, but I could just be getting
143 * lucky:
144 *
145 * rcp r1.y, r3.y
146 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
147 *
148 */
149 IR3_INSTR_SS = 0x002,
150 /* (jp) flag is set on jump targets:
151 */
152 IR3_INSTR_JP = 0x004,
153 IR3_INSTR_UL = 0x008,
154 IR3_INSTR_3D = 0x010,
155 IR3_INSTR_A = 0x020,
156 IR3_INSTR_O = 0x040,
157 IR3_INSTR_P = 0x080,
158 IR3_INSTR_S = 0x100,
159 IR3_INSTR_S2EN = 0x200,
160 /* meta-flags, for intermediate stages of IR, ie.
161 * before register assignment is done:
162 */
163 IR3_INSTR_MARK = 0x1000,
164 } flags;
165 int repeat;
166 #ifdef DEBUG
167 unsigned regs_max;
168 #endif
169 unsigned regs_count;
170 struct ir3_register **regs;
171 union {
172 struct {
173 char inv;
174 char comp;
175 int immed;
176 } cat0;
177 struct {
178 type_t src_type, dst_type;
179 } cat1;
180 struct {
181 enum {
182 IR3_COND_LT = 0,
183 IR3_COND_LE = 1,
184 IR3_COND_GT = 2,
185 IR3_COND_GE = 3,
186 IR3_COND_EQ = 4,
187 IR3_COND_NE = 5,
188 } condition;
189 } cat2;
190 struct {
191 unsigned samp, tex;
192 type_t type;
193 } cat5;
194 struct {
195 type_t type;
196 int offset;
197 int iim_val;
198 } cat6;
199 /* for meta-instructions, just used to hold extra data
200 * before instruction scheduling, etc
201 */
202 struct {
203 int off; /* component/offset */
204 } fo;
205 struct {
206 int aid;
207 } fi;
208 struct {
209 struct ir3_block *if_block, *else_block;
210 } flow;
211 struct {
212 struct ir3_block *block;
213 } inout;
214
215 /* XXX keep this as big as all other union members! */
216 uint32_t info[3];
217 };
218
219 /* transient values used during various algorithms: */
220 union {
221 /* The instruction depth is the max dependency distance to output.
222 *
223 * You can also think of it as the "cost", if we did any sort of
224 * optimization for register footprint. Ie. a value that is just
225 * result of moving a const to a reg would have a low cost, so to
226 * it could make sense to duplicate the instruction at various
227 * points where the result is needed to reduce register footprint.
228 *
229 * DEPTH_UNUSED used to mark unused instructions after depth
230 * calculation pass.
231 */
232 #define DEPTH_UNUSED ~0
233 unsigned depth;
234 };
235
236 /* Used during CP and RA stages. For fanin and shader inputs/
237 * outputs where we need a sequence of consecutive registers,
238 * keep track of each src instructions left (ie 'n-1') and right
239 * (ie 'n+1') neighbor. The front-end must insert enough mov's
240 * to ensure that each instruction has at most one left and at
241 * most one right neighbor. During the copy-propagation pass,
242 * we only remove mov's when we can preserve this constraint.
243 * And during the RA stage, we use the neighbor information to
244 * allocate a block of registers in one shot.
245 *
246 * TODO: maybe just add something like:
247 * struct ir3_instruction_ref {
248 * struct ir3_instruction *instr;
249 * unsigned cnt;
250 * }
251 *
252 * Or can we get away without the refcnt stuff? It seems like
253 * it should be overkill.. the problem is if, potentially after
254 * already eliminating some mov's, if you have a single mov that
255 * needs to be grouped with it's neighbors in two different
256 * places (ex. shader output and a fanin).
257 */
258 struct {
259 struct ir3_instruction *left, *right;
260 uint16_t left_cnt, right_cnt;
261 } cp;
262
263 /* an instruction can reference at most one address register amongst
264 * it's src/dst registers. Beyond that, you need to insert mov's.
265 */
266 struct ir3_instruction *address;
267
268 /* in case of a instruction with relative dst instruction, we need to
269 * capture the dependency on the fanin for the previous values of
270 * the array elements. Since we don't know at compile time actually
271 * which array elements are written, this serves to preserve the
272 * unconditional write to array elements prior to the conditional
273 * write.
274 *
275 * TODO only cat1 can do indirect write.. we could maybe move this
276 * into instr->cat1.fanin (but would require the frontend to insert
277 * the extra mov)
278 */
279 struct ir3_instruction *fanin;
280
281 struct ir3_instruction *next;
282 #ifdef DEBUG
283 uint32_t serialno;
284 #endif
285 };
286
287 static inline struct ir3_instruction *
288 ir3_neighbor_first(struct ir3_instruction *instr)
289 {
290 while (instr->cp.left)
291 instr = instr->cp.left;
292 return instr;
293 }
294
295 static inline int ir3_neighbor_count(struct ir3_instruction *instr)
296 {
297 int num = 1;
298
299 debug_assert(!instr->cp.left);
300
301 while (instr->cp.right) {
302 num++;
303 instr = instr->cp.right;
304 }
305
306 return num;
307 }
308
309 struct ir3_heap_chunk;
310
311 struct ir3 {
312 unsigned instrs_count, instrs_sz;
313 struct ir3_instruction **instrs;
314
315 /* Track bary.f (and ldlv) instructions.. this is needed in
316 * scheduling to ensure that all varying fetches happen before
317 * any potential kill instructions. The hw gets grumpy if all
318 * threads in a group are killed before the last bary.f gets
319 * a chance to signal end of input (ei).
320 */
321 unsigned baryfs_count, baryfs_sz;
322 struct ir3_instruction **baryfs;
323
324 struct ir3_block *block;
325 unsigned heap_idx;
326 struct ir3_heap_chunk *chunk;
327 };
328
329 struct ir3_block {
330 struct ir3 *shader;
331 unsigned ntemporaries, ninputs, noutputs;
332 /* maps TGSI_FILE_TEMPORARY index back to the assigning instruction: */
333 struct ir3_instruction **temporaries;
334 struct ir3_instruction **inputs;
335 struct ir3_instruction **outputs;
336 /* only a single address register: */
337 struct ir3_instruction *address;
338 struct ir3_block *parent;
339 struct ir3_instruction *head;
340 };
341
342 struct ir3 * ir3_create(void);
343 void ir3_destroy(struct ir3 *shader);
344 void * ir3_assemble(struct ir3 *shader,
345 struct ir3_info *info, uint32_t gpu_id);
346 void * ir3_alloc(struct ir3 *shader, int sz);
347
348 struct ir3_block * ir3_block_create(struct ir3 *shader,
349 unsigned ntmp, unsigned nin, unsigned nout);
350
351 struct ir3_instruction * ir3_instr_create(struct ir3_block *block,
352 int category, opc_t opc);
353 struct ir3_instruction * ir3_instr_create2(struct ir3_block *block,
354 int category, opc_t opc, int nreg);
355 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr);
356 const char *ir3_instr_name(struct ir3_instruction *instr);
357
358 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
359 int num, int flags);
360
361
362 static inline bool ir3_instr_check_mark(struct ir3_instruction *instr)
363 {
364 if (instr->flags & IR3_INSTR_MARK)
365 return true; /* already visited */
366 instr->flags |= IR3_INSTR_MARK;
367 return false;
368 }
369
370 static inline void ir3_clear_mark(struct ir3 *shader)
371 {
372 /* TODO would be nice to drop the instruction array.. for
373 * new compiler, _clear_mark() is all we use it for, and
374 * we could probably manage a linked list instead..
375 *
376 * Also, we'll probably want to mark instructions within
377 * a block, so tracking the list of instrs globally is
378 * unlikely to be what we want.
379 */
380 unsigned i;
381 for (i = 0; i < shader->instrs_count; i++) {
382 struct ir3_instruction *instr = shader->instrs[i];
383 instr->flags &= ~IR3_INSTR_MARK;
384 }
385 }
386
387 static inline int ir3_instr_regno(struct ir3_instruction *instr,
388 struct ir3_register *reg)
389 {
390 unsigned i;
391 for (i = 0; i < instr->regs_count; i++)
392 if (reg == instr->regs[i])
393 return i;
394 return -1;
395 }
396
397
398 #define MAX_ARRAYS 16
399
400 /* comp:
401 * 0 - x
402 * 1 - y
403 * 2 - z
404 * 3 - w
405 */
406 static inline uint32_t regid(int num, int comp)
407 {
408 return (num << 2) | (comp & 0x3);
409 }
410
411 static inline uint32_t reg_num(struct ir3_register *reg)
412 {
413 return reg->num >> 2;
414 }
415
416 static inline uint32_t reg_comp(struct ir3_register *reg)
417 {
418 return reg->num & 0x3;
419 }
420
421 static inline bool is_flow(struct ir3_instruction *instr)
422 {
423 return (instr->category == 0);
424 }
425
426 static inline bool is_kill(struct ir3_instruction *instr)
427 {
428 return is_flow(instr) && (instr->opc == OPC_KILL);
429 }
430
431 static inline bool is_nop(struct ir3_instruction *instr)
432 {
433 return is_flow(instr) && (instr->opc == OPC_NOP);
434 }
435
436 static inline bool is_alu(struct ir3_instruction *instr)
437 {
438 return (1 <= instr->category) && (instr->category <= 3);
439 }
440
441 static inline bool is_sfu(struct ir3_instruction *instr)
442 {
443 return (instr->category == 4);
444 }
445
446 static inline bool is_tex(struct ir3_instruction *instr)
447 {
448 return (instr->category == 5);
449 }
450
451 static inline bool is_mem(struct ir3_instruction *instr)
452 {
453 return (instr->category == 6);
454 }
455
456 static inline bool is_input(struct ir3_instruction *instr)
457 {
458 /* in some cases, ldlv is used to fetch varying without
459 * interpolation.. fortunately inloc is the first src
460 * register in either case
461 */
462 if (is_mem(instr) && (instr->opc == OPC_LDLV))
463 return true;
464 return (instr->category == 2) && (instr->opc == OPC_BARY_F);
465 }
466
467 static inline bool is_meta(struct ir3_instruction *instr)
468 {
469 /* TODO how should we count PHI (and maybe fan-in/out) which
470 * might actually contribute some instructions to the final
471 * result?
472 */
473 return (instr->category == -1);
474 }
475
476 static inline bool writes_addr(struct ir3_instruction *instr)
477 {
478 if (instr->regs_count > 0) {
479 struct ir3_register *dst = instr->regs[0];
480 return !!(dst->flags & IR3_REG_ADDR);
481 }
482 return false;
483 }
484
485 static inline bool writes_pred(struct ir3_instruction *instr)
486 {
487 if (instr->regs_count > 0) {
488 struct ir3_register *dst = instr->regs[0];
489 return reg_num(dst) == REG_P0;
490 }
491 return false;
492 }
493
494 /* returns defining instruction for reg */
495 /* TODO better name */
496 static inline struct ir3_instruction *ssa(struct ir3_register *reg)
497 {
498 if (reg->flags & IR3_REG_SSA)
499 return reg->instr;
500 return NULL;
501 }
502
503 static inline bool reg_gpr(struct ir3_register *r)
504 {
505 if (r->flags & (IR3_REG_CONST | IR3_REG_IMMED | IR3_REG_ADDR))
506 return false;
507 if ((reg_num(r) == REG_A0) || (reg_num(r) == REG_P0))
508 return false;
509 return true;
510 }
511
512 #define array_insert(arr, val) do { \
513 if (arr ## _count == arr ## _sz) { \
514 arr ## _sz = MAX2(2 * arr ## _sz, 16); \
515 arr = realloc(arr, arr ## _sz * sizeof(arr[0])); \
516 } \
517 arr[arr ##_count++] = val; \
518 } while (0)
519
520 /* iterator for an instructions's sources (reg), also returns src #: */
521 #define foreach_src_n(__srcreg, __n, __instr) \
522 if ((__instr)->regs_count) \
523 for (unsigned __cnt = (__instr)->regs_count - 1, __n = 0; __n < __cnt; __n++) \
524 if ((__srcreg = (__instr)->regs[__n + 1]))
525
526 /* iterator for an instructions's sources (reg): */
527 #define foreach_src(__srcreg, __instr) \
528 foreach_src_n(__srcreg, __i, __instr)
529
530 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
531 {
532 if (instr->fanin)
533 return instr->regs_count + 2;
534 if (instr->address)
535 return instr->regs_count + 1;
536 return instr->regs_count;
537 }
538
539 static inline struct ir3_instruction * __ssa_src_n(struct ir3_instruction *instr, unsigned n)
540 {
541 if (n == (instr->regs_count + 1))
542 return instr->fanin;
543 if (n == (instr->regs_count + 0))
544 return instr->address;
545 return ssa(instr->regs[n]);
546 }
547
548 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
549
550 /* iterator for an instruction's SSA sources (instr), also returns src #: */
551 #define foreach_ssa_src_n(__srcinst, __n, __instr) \
552 if ((__instr)->regs_count) \
553 for (unsigned __cnt = __ssa_src_cnt(__instr) - 1, __n = 0; __n < __cnt; __n++) \
554 if ((__srcinst = __ssa_src_n(__instr, __n + 1)))
555
556 /* iterator for an instruction's SSA sources (instr): */
557 #define foreach_ssa_src(__srcinst, __instr) \
558 foreach_ssa_src_n(__srcinst, __i, __instr)
559
560
561 /* dump: */
562 #include <stdio.h>
563 void ir3_dump(struct ir3 *shader, const char *name,
564 struct ir3_block *block /* XXX maybe 'block' ptr should move to ir3? */,
565 FILE *f);
566 void ir3_dump_instr_single(struct ir3_instruction *instr);
567 void ir3_dump_instr_list(struct ir3_instruction *instr);
568
569 /* flatten if/else: */
570 int ir3_block_flatten(struct ir3_block *block);
571
572 /* depth calculation: */
573 int ir3_delayslots(struct ir3_instruction *assigner,
574 struct ir3_instruction *consumer, unsigned n);
575 void ir3_block_depth(struct ir3_block *block);
576
577 /* copy-propagate: */
578 void ir3_block_cp(struct ir3_block *block);
579
580 /* group neightbors and insert mov's to resolve conflicts: */
581 void ir3_block_group(struct ir3_block *block);
582
583 /* scheduling: */
584 int ir3_block_sched(struct ir3_block *block);
585
586 /* register assignment: */
587 int ir3_block_ra(struct ir3_block *block, enum shader_t type,
588 bool frag_coord, bool frag_face);
589
590 /* legalize: */
591 void ir3_block_legalize(struct ir3_block *block,
592 bool *has_samp, int *max_bary);
593
594
595 /* ************************************************************************* */
596 /* split this out or find some helper to use.. like main/bitset.h.. */
597
598 #include <string.h>
599
600 #define MAX_REG 256
601
602 typedef uint8_t regmask_t[2 * MAX_REG / 8];
603
604 static inline unsigned regmask_idx(struct ir3_register *reg)
605 {
606 unsigned num = reg->num;
607 debug_assert(num < MAX_REG);
608 if (reg->flags & IR3_REG_HALF)
609 num += MAX_REG;
610 return num;
611 }
612
613 static inline void regmask_init(regmask_t *regmask)
614 {
615 memset(regmask, 0, sizeof(*regmask));
616 }
617
618 static inline void regmask_set(regmask_t *regmask, struct ir3_register *reg)
619 {
620 unsigned idx = regmask_idx(reg);
621 if (reg->flags & IR3_REG_RELATIV) {
622 unsigned i;
623 for (i = 0; i < reg->size; i++, idx++)
624 (*regmask)[idx / 8] |= 1 << (idx % 8);
625 } else {
626 unsigned mask;
627 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
628 if (mask & 1)
629 (*regmask)[idx / 8] |= 1 << (idx % 8);
630 }
631 }
632
633 static inline void regmask_or(regmask_t *dst, regmask_t *a, regmask_t *b)
634 {
635 unsigned i;
636 for (i = 0; i < ARRAY_SIZE(*dst); i++)
637 (*dst)[i] = (*a)[i] | (*b)[i];
638 }
639
640 /* set bits in a if not set in b, conceptually:
641 * a |= (reg & ~b)
642 */
643 static inline void regmask_set_if_not(regmask_t *a,
644 struct ir3_register *reg, regmask_t *b)
645 {
646 unsigned idx = regmask_idx(reg);
647 if (reg->flags & IR3_REG_RELATIV) {
648 unsigned i;
649 for (i = 0; i < reg->size; i++, idx++)
650 if (!((*b)[idx / 8] & (1 << (idx % 8))))
651 (*a)[idx / 8] |= 1 << (idx % 8);
652 } else {
653 unsigned mask;
654 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
655 if (mask & 1)
656 if (!((*b)[idx / 8] & (1 << (idx % 8))))
657 (*a)[idx / 8] |= 1 << (idx % 8);
658 }
659 }
660
661 static inline bool regmask_get(regmask_t *regmask,
662 struct ir3_register *reg)
663 {
664 unsigned idx = regmask_idx(reg);
665 if (reg->flags & IR3_REG_RELATIV) {
666 unsigned i;
667 for (i = 0; i < reg->size; i++, idx++)
668 if ((*regmask)[idx / 8] & (1 << (idx % 8)))
669 return true;
670 } else {
671 unsigned mask;
672 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
673 if (mask & 1)
674 if ((*regmask)[idx / 8] & (1 << (idx % 8)))
675 return true;
676 }
677 return false;
678 }
679
680 /* ************************************************************************* */
681
682 #endif /* IR3_H_ */