freedreno/ir3: introduce ir3_compiler object
[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 #include "util/list.h"
32
33 #include "instr-a3xx.h"
34 #include "disasm.h" /* TODO move 'enum shader_t' somewhere else.. */
35
36 /* low level intermediate representation of an adreno shader program */
37
38 struct ir3_compiler;
39 struct ir3;
40 struct ir3_instruction;
41 struct ir3_block;
42
43 struct ir3_info {
44 uint16_t sizedwords;
45 uint16_t instrs_count; /* expanded to account for rpt's */
46 /* NOTE: max_reg, etc, does not include registers not touched
47 * by the shader (ie. vertex fetched via VFD_DECODE but not
48 * touched by shader)
49 */
50 int8_t max_reg; /* highest GPR # used by shader */
51 int8_t max_half_reg;
52 int16_t max_const;
53 };
54
55 struct ir3_register {
56 enum {
57 IR3_REG_CONST = 0x001,
58 IR3_REG_IMMED = 0x002,
59 IR3_REG_HALF = 0x004,
60 IR3_REG_RELATIV= 0x008,
61 IR3_REG_R = 0x010,
62 /* Most instructions, it seems, can do float abs/neg but not
63 * integer. The CP pass needs to know what is intended (int or
64 * float) in order to do the right thing. For this reason the
65 * abs/neg flags are split out into float and int variants. In
66 * addition, .b (bitwise) operations, the negate is actually a
67 * bitwise not, so split that out into a new flag to make it
68 * more clear.
69 */
70 IR3_REG_FNEG = 0x020,
71 IR3_REG_FABS = 0x040,
72 IR3_REG_SNEG = 0x080,
73 IR3_REG_SABS = 0x100,
74 IR3_REG_BNOT = 0x200,
75 IR3_REG_EVEN = 0x400,
76 IR3_REG_POS_INF= 0x800,
77 /* (ei) flag, end-input? Set on last bary, presumably to signal
78 * that the shader needs no more input:
79 */
80 IR3_REG_EI = 0x1000,
81 /* meta-flags, for intermediate stages of IR, ie.
82 * before register assignment is done:
83 */
84 IR3_REG_SSA = 0x2000, /* 'instr' is ptr to assigning instr */
85 IR3_REG_IA = 0x4000, /* meta-input dst is "assigned" */
86 IR3_REG_ADDR = 0x8000, /* register is a0.x */
87 } flags;
88 union {
89 /* normal registers:
90 * the component is in the low two bits of the reg #, so
91 * rN.x becomes: (N << 2) | x
92 */
93 int num;
94 /* immediate: */
95 int32_t iim_val;
96 uint32_t uim_val;
97 float fim_val;
98 /* relative: */
99 int offset;
100 };
101
102 /* for IR3_REG_SSA, src registers contain ptr back to
103 * assigning instruction.
104 */
105 struct ir3_instruction *instr;
106
107 union {
108 /* used for cat5 instructions, but also for internal/IR level
109 * tracking of what registers are read/written by an instruction.
110 * wrmask may be a bad name since it is used to represent both
111 * src and dst that touch multiple adjacent registers.
112 */
113 unsigned wrmask;
114 /* for relative addressing, 32bits for array size is too small,
115 * but otoh we don't need to deal with disjoint sets, so instead
116 * use a simple size field (number of scalar components).
117 */
118 unsigned size;
119 };
120 };
121
122 struct ir3_instruction {
123 struct ir3_block *block;
124 int category;
125 opc_t opc;
126 enum {
127 /* (sy) flag is set on first instruction, and after sample
128 * instructions (probably just on RAW hazard).
129 */
130 IR3_INSTR_SY = 0x001,
131 /* (ss) flag is set on first instruction, and first instruction
132 * to depend on the result of "long" instructions (RAW hazard):
133 *
134 * rcp, rsq, log2, exp2, sin, cos, sqrt
135 *
136 * It seems to synchronize until all in-flight instructions are
137 * completed, for example:
138 *
139 * rsq hr1.w, hr1.w
140 * add.f hr2.z, (neg)hr2.z, hc0.y
141 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
142 * rsq hr2.x, hr2.x
143 * (rpt1)nop
144 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
145 * nop
146 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
147 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
148 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
149 *
150 * The last mul.f does not have (ss) set, presumably because the
151 * (ss) on the previous instruction does the job.
152 *
153 * The blob driver also seems to set it on WAR hazards, although
154 * not really clear if this is needed or just blob compiler being
155 * sloppy. So far I haven't found a case where removing the (ss)
156 * causes problems for WAR hazard, but I could just be getting
157 * lucky:
158 *
159 * rcp r1.y, r3.y
160 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
161 *
162 */
163 IR3_INSTR_SS = 0x002,
164 /* (jp) flag is set on jump targets:
165 */
166 IR3_INSTR_JP = 0x004,
167 IR3_INSTR_UL = 0x008,
168 IR3_INSTR_3D = 0x010,
169 IR3_INSTR_A = 0x020,
170 IR3_INSTR_O = 0x040,
171 IR3_INSTR_P = 0x080,
172 IR3_INSTR_S = 0x100,
173 IR3_INSTR_S2EN = 0x200,
174 /* meta-flags, for intermediate stages of IR, ie.
175 * before register assignment is done:
176 */
177 IR3_INSTR_MARK = 0x1000,
178 } flags;
179 int repeat;
180 #ifdef DEBUG
181 unsigned regs_max;
182 #endif
183 unsigned regs_count;
184 struct ir3_register **regs;
185 union {
186 struct {
187 char inv;
188 char comp;
189 int immed;
190 } cat0;
191 struct {
192 type_t src_type, dst_type;
193 } cat1;
194 struct {
195 enum {
196 IR3_COND_LT = 0,
197 IR3_COND_LE = 1,
198 IR3_COND_GT = 2,
199 IR3_COND_GE = 3,
200 IR3_COND_EQ = 4,
201 IR3_COND_NE = 5,
202 } condition;
203 } cat2;
204 struct {
205 unsigned samp, tex;
206 type_t type;
207 } cat5;
208 struct {
209 type_t type;
210 int offset;
211 int iim_val;
212 } cat6;
213 /* for meta-instructions, just used to hold extra data
214 * before instruction scheduling, etc
215 */
216 struct {
217 int off; /* component/offset */
218 } fo;
219 struct {
220 int aid;
221 } fi;
222 struct {
223 struct ir3_block *if_block, *else_block;
224 } flow;
225 struct {
226 struct ir3_block *block;
227 } inout;
228
229 /* XXX keep this as big as all other union members! */
230 uint32_t info[3];
231 };
232
233 /* transient values used during various algorithms: */
234 union {
235 /* The instruction depth is the max dependency distance to output.
236 *
237 * You can also think of it as the "cost", if we did any sort of
238 * optimization for register footprint. Ie. a value that is just
239 * result of moving a const to a reg would have a low cost, so to
240 * it could make sense to duplicate the instruction at various
241 * points where the result is needed to reduce register footprint.
242 *
243 * DEPTH_UNUSED used to mark unused instructions after depth
244 * calculation pass.
245 */
246 #define DEPTH_UNUSED ~0
247 unsigned depth;
248 };
249
250 /* Used during CP and RA stages. For fanin and shader inputs/
251 * outputs where we need a sequence of consecutive registers,
252 * keep track of each src instructions left (ie 'n-1') and right
253 * (ie 'n+1') neighbor. The front-end must insert enough mov's
254 * to ensure that each instruction has at most one left and at
255 * most one right neighbor. During the copy-propagation pass,
256 * we only remove mov's when we can preserve this constraint.
257 * And during the RA stage, we use the neighbor information to
258 * allocate a block of registers in one shot.
259 *
260 * TODO: maybe just add something like:
261 * struct ir3_instruction_ref {
262 * struct ir3_instruction *instr;
263 * unsigned cnt;
264 * }
265 *
266 * Or can we get away without the refcnt stuff? It seems like
267 * it should be overkill.. the problem is if, potentially after
268 * already eliminating some mov's, if you have a single mov that
269 * needs to be grouped with it's neighbors in two different
270 * places (ex. shader output and a fanin).
271 */
272 struct {
273 struct ir3_instruction *left, *right;
274 uint16_t left_cnt, right_cnt;
275 } cp;
276
277 /* an instruction can reference at most one address register amongst
278 * it's src/dst registers. Beyond that, you need to insert mov's.
279 */
280 struct ir3_instruction *address;
281
282 /* in case of a instruction with relative dst instruction, we need to
283 * capture the dependency on the fanin for the previous values of
284 * the array elements. Since we don't know at compile time actually
285 * which array elements are written, this serves to preserve the
286 * unconditional write to array elements prior to the conditional
287 * write.
288 *
289 * TODO only cat1 can do indirect write.. we could maybe move this
290 * into instr->cat1.fanin (but would require the frontend to insert
291 * the extra mov)
292 */
293 struct ir3_instruction *fanin;
294
295 /* Entry in ir3_block's instruction list: */
296 struct list_head node;
297
298 #ifdef DEBUG
299 uint32_t serialno;
300 #endif
301 };
302
303 static inline struct ir3_instruction *
304 ir3_neighbor_first(struct ir3_instruction *instr)
305 {
306 while (instr->cp.left)
307 instr = instr->cp.left;
308 return instr;
309 }
310
311 static inline int ir3_neighbor_count(struct ir3_instruction *instr)
312 {
313 int num = 1;
314
315 debug_assert(!instr->cp.left);
316
317 while (instr->cp.right) {
318 num++;
319 instr = instr->cp.right;
320 }
321
322 return num;
323 }
324
325 struct ir3_heap_chunk;
326
327 struct ir3 {
328 struct ir3_compiler *compiler;
329
330 /* Track bary.f (and ldlv) instructions.. this is needed in
331 * scheduling to ensure that all varying fetches happen before
332 * any potential kill instructions. The hw gets grumpy if all
333 * threads in a group are killed before the last bary.f gets
334 * a chance to signal end of input (ei).
335 */
336 unsigned baryfs_count, baryfs_sz;
337 struct ir3_instruction **baryfs;
338
339 /* Track all indirect instructions (read and write). To avoid
340 * deadlock scenario where an address register gets scheduled,
341 * but other dependent src instructions cannot be scheduled due
342 * to dependency on a *different* address register value, the
343 * scheduler needs to ensure that all dependencies other than
344 * the instruction other than the address register are scheduled
345 * before the one that writes the address register. Having a
346 * convenient list of instructions that reference some address
347 * register simplifies this.
348 */
349 unsigned indirects_count, indirects_sz;
350 struct ir3_instruction **indirects;
351 /* and same for instructions that consume predicate register: */
352 unsigned predicates_count, predicates_sz;
353 struct ir3_instruction **predicates;
354
355 struct ir3_block *block;
356 unsigned heap_idx;
357 struct ir3_heap_chunk *chunk;
358 };
359
360 struct ir3_block {
361 struct ir3 *shader;
362 unsigned ntemporaries, ninputs, noutputs;
363 /* maps TGSI_FILE_TEMPORARY index back to the assigning instruction: */
364 struct ir3_instruction **temporaries;
365 struct ir3_instruction **inputs;
366 struct ir3_instruction **outputs;
367 /* only a single address register: */
368 struct ir3_instruction *address;
369 struct list_head instr_list;
370 };
371
372 struct ir3 * ir3_create(struct ir3_compiler *compiler);
373 void ir3_destroy(struct ir3 *shader);
374 void * ir3_assemble(struct ir3 *shader,
375 struct ir3_info *info, uint32_t gpu_id);
376 void * ir3_alloc(struct ir3 *shader, int sz);
377
378 struct ir3_block * ir3_block_create(struct ir3 *shader,
379 unsigned ntmp, unsigned nin, unsigned nout);
380
381 struct ir3_instruction * ir3_instr_create(struct ir3_block *block,
382 int category, opc_t opc);
383 struct ir3_instruction * ir3_instr_create2(struct ir3_block *block,
384 int category, opc_t opc, int nreg);
385 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr);
386 const char *ir3_instr_name(struct ir3_instruction *instr);
387
388 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
389 int num, int flags);
390
391
392 static inline bool ir3_instr_check_mark(struct ir3_instruction *instr)
393 {
394 if (instr->flags & IR3_INSTR_MARK)
395 return true; /* already visited */
396 instr->flags |= IR3_INSTR_MARK;
397 return false;
398 }
399
400 static inline void ir3_clear_mark(struct ir3 *shader)
401 {
402 /* TODO would be nice to drop the instruction array.. for
403 * new compiler, _clear_mark() is all we use it for, and
404 * we could probably manage a linked list instead..
405 *
406 * Also, we'll probably want to mark instructions within
407 * a block, so tracking the list of instrs globally is
408 * unlikely to be what we want.
409 */
410 list_for_each_entry (struct ir3_instruction, instr, &shader->block->instr_list, node)
411 instr->flags &= ~IR3_INSTR_MARK;
412 }
413
414 static inline int ir3_instr_regno(struct ir3_instruction *instr,
415 struct ir3_register *reg)
416 {
417 unsigned i;
418 for (i = 0; i < instr->regs_count; i++)
419 if (reg == instr->regs[i])
420 return i;
421 return -1;
422 }
423
424
425 #define MAX_ARRAYS 16
426
427 /* comp:
428 * 0 - x
429 * 1 - y
430 * 2 - z
431 * 3 - w
432 */
433 static inline uint32_t regid(int num, int comp)
434 {
435 return (num << 2) | (comp & 0x3);
436 }
437
438 static inline uint32_t reg_num(struct ir3_register *reg)
439 {
440 return reg->num >> 2;
441 }
442
443 static inline uint32_t reg_comp(struct ir3_register *reg)
444 {
445 return reg->num & 0x3;
446 }
447
448 static inline bool is_flow(struct ir3_instruction *instr)
449 {
450 return (instr->category == 0);
451 }
452
453 static inline bool is_kill(struct ir3_instruction *instr)
454 {
455 return is_flow(instr) && (instr->opc == OPC_KILL);
456 }
457
458 static inline bool is_nop(struct ir3_instruction *instr)
459 {
460 return is_flow(instr) && (instr->opc == OPC_NOP);
461 }
462
463 /* Is it a non-transformative (ie. not type changing) mov? This can
464 * also include absneg.s/absneg.f, which for the most part can be
465 * treated as a mov (single src argument).
466 */
467 static inline bool is_same_type_mov(struct ir3_instruction *instr)
468 {
469 struct ir3_register *dst = instr->regs[0];
470
471 /* mov's that write to a0.x or p0.x are special: */
472 if (dst->num == regid(REG_P0, 0))
473 return false;
474 if (dst->num == regid(REG_A0, 0))
475 return false;
476
477 if ((instr->category == 1) &&
478 (instr->cat1.src_type == instr->cat1.dst_type))
479 return true;
480 if ((instr->category == 2) && ((instr->opc == OPC_ABSNEG_F) ||
481 (instr->opc == OPC_ABSNEG_S)))
482 return true;
483 return false;
484 }
485
486 static inline bool is_alu(struct ir3_instruction *instr)
487 {
488 return (1 <= instr->category) && (instr->category <= 3);
489 }
490
491 static inline bool is_sfu(struct ir3_instruction *instr)
492 {
493 return (instr->category == 4);
494 }
495
496 static inline bool is_tex(struct ir3_instruction *instr)
497 {
498 return (instr->category == 5);
499 }
500
501 static inline bool is_mem(struct ir3_instruction *instr)
502 {
503 return (instr->category == 6);
504 }
505
506 static inline bool is_input(struct ir3_instruction *instr)
507 {
508 /* in some cases, ldlv is used to fetch varying without
509 * interpolation.. fortunately inloc is the first src
510 * register in either case
511 */
512 if (is_mem(instr) && (instr->opc == OPC_LDLV))
513 return true;
514 return (instr->category == 2) && (instr->opc == OPC_BARY_F);
515 }
516
517 static inline bool is_meta(struct ir3_instruction *instr)
518 {
519 /* TODO how should we count PHI (and maybe fan-in/out) which
520 * might actually contribute some instructions to the final
521 * result?
522 */
523 return (instr->category == -1);
524 }
525
526 static inline bool writes_addr(struct ir3_instruction *instr)
527 {
528 if (instr->regs_count > 0) {
529 struct ir3_register *dst = instr->regs[0];
530 return !!(dst->flags & IR3_REG_ADDR);
531 }
532 return false;
533 }
534
535 static inline bool writes_pred(struct ir3_instruction *instr)
536 {
537 if (instr->regs_count > 0) {
538 struct ir3_register *dst = instr->regs[0];
539 return reg_num(dst) == REG_P0;
540 }
541 return false;
542 }
543
544 /* returns defining instruction for reg */
545 /* TODO better name */
546 static inline struct ir3_instruction *ssa(struct ir3_register *reg)
547 {
548 if (reg->flags & IR3_REG_SSA)
549 return reg->instr;
550 return NULL;
551 }
552
553 static inline bool conflicts(struct ir3_instruction *a,
554 struct ir3_instruction *b)
555 {
556 return (a && b) && (a != b);
557 }
558
559 static inline bool reg_gpr(struct ir3_register *r)
560 {
561 if (r->flags & (IR3_REG_CONST | IR3_REG_IMMED | IR3_REG_ADDR))
562 return false;
563 if ((reg_num(r) == REG_A0) || (reg_num(r) == REG_P0))
564 return false;
565 return true;
566 }
567
568 /* some cat2 instructions (ie. those which are not float) can embed an
569 * immediate:
570 */
571 static inline bool ir3_cat2_int(opc_t opc)
572 {
573 switch (opc) {
574 case OPC_ADD_U:
575 case OPC_ADD_S:
576 case OPC_SUB_U:
577 case OPC_SUB_S:
578 case OPC_CMPS_U:
579 case OPC_CMPS_S:
580 case OPC_MIN_U:
581 case OPC_MIN_S:
582 case OPC_MAX_U:
583 case OPC_MAX_S:
584 case OPC_CMPV_U:
585 case OPC_CMPV_S:
586 case OPC_MUL_U:
587 case OPC_MUL_S:
588 case OPC_MULL_U:
589 case OPC_CLZ_S:
590 case OPC_ABSNEG_S:
591 case OPC_AND_B:
592 case OPC_OR_B:
593 case OPC_NOT_B:
594 case OPC_XOR_B:
595 case OPC_BFREV_B:
596 case OPC_CLZ_B:
597 case OPC_SHL_B:
598 case OPC_SHR_B:
599 case OPC_ASHR_B:
600 case OPC_MGEN_B:
601 case OPC_GETBIT_B:
602 case OPC_CBITS_B:
603 case OPC_BARY_F:
604 return true;
605
606 default:
607 return false;
608 }
609 }
610
611
612 /* map cat2 instruction to valid abs/neg flags: */
613 static inline unsigned ir3_cat2_absneg(opc_t opc)
614 {
615 switch (opc) {
616 case OPC_ADD_F:
617 case OPC_MIN_F:
618 case OPC_MAX_F:
619 case OPC_MUL_F:
620 case OPC_SIGN_F:
621 case OPC_CMPS_F:
622 case OPC_ABSNEG_F:
623 case OPC_CMPV_F:
624 case OPC_FLOOR_F:
625 case OPC_CEIL_F:
626 case OPC_RNDNE_F:
627 case OPC_RNDAZ_F:
628 case OPC_TRUNC_F:
629 case OPC_BARY_F:
630 return IR3_REG_FABS | IR3_REG_FNEG;
631
632 case OPC_ADD_U:
633 case OPC_ADD_S:
634 case OPC_SUB_U:
635 case OPC_SUB_S:
636 case OPC_CMPS_U:
637 case OPC_CMPS_S:
638 case OPC_MIN_U:
639 case OPC_MIN_S:
640 case OPC_MAX_U:
641 case OPC_MAX_S:
642 case OPC_CMPV_U:
643 case OPC_CMPV_S:
644 case OPC_MUL_U:
645 case OPC_MUL_S:
646 case OPC_MULL_U:
647 case OPC_CLZ_S:
648 return 0;
649
650 case OPC_ABSNEG_S:
651 return IR3_REG_SABS | IR3_REG_SNEG;
652
653 case OPC_AND_B:
654 case OPC_OR_B:
655 case OPC_NOT_B:
656 case OPC_XOR_B:
657 case OPC_BFREV_B:
658 case OPC_CLZ_B:
659 case OPC_SHL_B:
660 case OPC_SHR_B:
661 case OPC_ASHR_B:
662 case OPC_MGEN_B:
663 case OPC_GETBIT_B:
664 case OPC_CBITS_B:
665 return IR3_REG_BNOT;
666
667 default:
668 return 0;
669 }
670 }
671
672 /* map cat3 instructions to valid abs/neg flags: */
673 static inline unsigned ir3_cat3_absneg(opc_t opc)
674 {
675 switch (opc) {
676 case OPC_MAD_F16:
677 case OPC_MAD_F32:
678 case OPC_SEL_F16:
679 case OPC_SEL_F32:
680 return IR3_REG_FNEG;
681
682 case OPC_MAD_U16:
683 case OPC_MADSH_U16:
684 case OPC_MAD_S16:
685 case OPC_MADSH_M16:
686 case OPC_MAD_U24:
687 case OPC_MAD_S24:
688 case OPC_SEL_S16:
689 case OPC_SEL_S32:
690 case OPC_SAD_S16:
691 case OPC_SAD_S32:
692 /* neg *may* work on 3rd src.. */
693
694 case OPC_SEL_B16:
695 case OPC_SEL_B32:
696
697 default:
698 return 0;
699 }
700 }
701
702 #define array_insert(arr, val) do { \
703 if (arr ## _count == arr ## _sz) { \
704 arr ## _sz = MAX2(2 * arr ## _sz, 16); \
705 arr = realloc(arr, arr ## _sz * sizeof(arr[0])); \
706 } \
707 arr[arr ##_count++] = val; \
708 } while (0)
709
710 /* iterator for an instructions's sources (reg), also returns src #: */
711 #define foreach_src_n(__srcreg, __n, __instr) \
712 if ((__instr)->regs_count) \
713 for (unsigned __cnt = (__instr)->regs_count - 1, __n = 0; __n < __cnt; __n++) \
714 if ((__srcreg = (__instr)->regs[__n + 1]))
715
716 /* iterator for an instructions's sources (reg): */
717 #define foreach_src(__srcreg, __instr) \
718 foreach_src_n(__srcreg, __i, __instr)
719
720 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
721 {
722 if (instr->fanin)
723 return instr->regs_count + 2;
724 if (instr->address)
725 return instr->regs_count + 1;
726 return instr->regs_count;
727 }
728
729 static inline struct ir3_instruction * __ssa_src_n(struct ir3_instruction *instr, unsigned n)
730 {
731 if (n == (instr->regs_count + 1))
732 return instr->fanin;
733 if (n == (instr->regs_count + 0))
734 return instr->address;
735 return ssa(instr->regs[n]);
736 }
737
738 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
739
740 /* iterator for an instruction's SSA sources (instr), also returns src #: */
741 #define foreach_ssa_src_n(__srcinst, __n, __instr) \
742 if ((__instr)->regs_count) \
743 for (unsigned __cnt = __ssa_src_cnt(__instr) - 1, __n = 0; __n < __cnt; __n++) \
744 if ((__srcinst = __ssa_src_n(__instr, __n + 1)))
745
746 /* iterator for an instruction's SSA sources (instr): */
747 #define foreach_ssa_src(__srcinst, __instr) \
748 foreach_ssa_src_n(__srcinst, __i, __instr)
749
750
751 /* dump: */
752 void ir3_print(struct ir3 *ir);
753 void ir3_print_instr(struct ir3_instruction *instr);
754
755 /* flatten if/else: */
756 int ir3_block_flatten(struct ir3_block *block);
757
758 /* depth calculation: */
759 int ir3_delayslots(struct ir3_instruction *assigner,
760 struct ir3_instruction *consumer, unsigned n);
761 void ir3_insert_by_depth(struct ir3_instruction *instr, struct list_head *list);
762 void ir3_block_depth(struct ir3_block *block);
763
764 /* copy-propagate: */
765 void ir3_block_cp(struct ir3_block *block);
766
767 /* group neighbors and insert mov's to resolve conflicts: */
768 void ir3_block_group(struct ir3_block *block);
769
770 /* scheduling: */
771 int ir3_block_sched(struct ir3_block *block);
772
773 /* register assignment: */
774 int ir3_block_ra(struct ir3_block *block, enum shader_t type,
775 bool frag_coord, bool frag_face);
776
777 /* legalize: */
778 void ir3_block_legalize(struct ir3_block *block,
779 bool *has_samp, int *max_bary);
780
781 /* ************************************************************************* */
782 /* instruction helpers */
783
784 static inline struct ir3_instruction *
785 ir3_MOV(struct ir3_block *block, struct ir3_instruction *src, type_t type)
786 {
787 struct ir3_instruction *instr =
788 ir3_instr_create(block, 1, 0);
789 ir3_reg_create(instr, 0, 0); /* dst */
790 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
791 instr->cat1.src_type = type;
792 instr->cat1.dst_type = type;
793 return instr;
794 }
795
796 static inline struct ir3_instruction *
797 ir3_COV(struct ir3_block *block, struct ir3_instruction *src,
798 type_t src_type, type_t dst_type)
799 {
800 struct ir3_instruction *instr =
801 ir3_instr_create(block, 1, 0);
802 ir3_reg_create(instr, 0, 0); /* dst */
803 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
804 instr->cat1.src_type = src_type;
805 instr->cat1.dst_type = dst_type;
806 return instr;
807 }
808
809 static inline struct ir3_instruction *
810 ir3_NOP(struct ir3_block *block)
811 {
812 return ir3_instr_create(block, 0, OPC_NOP);
813 }
814
815 #define INSTR1(CAT, name) \
816 static inline struct ir3_instruction * \
817 ir3_##name(struct ir3_block *block, \
818 struct ir3_instruction *a, unsigned aflags) \
819 { \
820 struct ir3_instruction *instr = \
821 ir3_instr_create(block, CAT, OPC_##name); \
822 ir3_reg_create(instr, 0, 0); /* dst */ \
823 ir3_reg_create(instr, 0, IR3_REG_SSA | aflags)->instr = a; \
824 return instr; \
825 }
826
827 #define INSTR2(CAT, name) \
828 static inline struct ir3_instruction * \
829 ir3_##name(struct ir3_block *block, \
830 struct ir3_instruction *a, unsigned aflags, \
831 struct ir3_instruction *b, unsigned bflags) \
832 { \
833 struct ir3_instruction *instr = \
834 ir3_instr_create(block, CAT, OPC_##name); \
835 ir3_reg_create(instr, 0, 0); /* dst */ \
836 ir3_reg_create(instr, 0, IR3_REG_SSA | aflags)->instr = a; \
837 ir3_reg_create(instr, 0, IR3_REG_SSA | bflags)->instr = b; \
838 return instr; \
839 }
840
841 #define INSTR3(CAT, name) \
842 static inline struct ir3_instruction * \
843 ir3_##name(struct ir3_block *block, \
844 struct ir3_instruction *a, unsigned aflags, \
845 struct ir3_instruction *b, unsigned bflags, \
846 struct ir3_instruction *c, unsigned cflags) \
847 { \
848 struct ir3_instruction *instr = \
849 ir3_instr_create(block, CAT, OPC_##name); \
850 ir3_reg_create(instr, 0, 0); /* dst */ \
851 ir3_reg_create(instr, 0, IR3_REG_SSA | aflags)->instr = a; \
852 ir3_reg_create(instr, 0, IR3_REG_SSA | bflags)->instr = b; \
853 ir3_reg_create(instr, 0, IR3_REG_SSA | cflags)->instr = c; \
854 return instr; \
855 }
856
857 /* cat0 instructions: */
858 INSTR1(0, KILL);
859
860 /* cat2 instructions, most 2 src but some 1 src: */
861 INSTR2(2, ADD_F)
862 INSTR2(2, MIN_F)
863 INSTR2(2, MAX_F)
864 INSTR2(2, MUL_F)
865 INSTR1(2, SIGN_F)
866 INSTR2(2, CMPS_F)
867 INSTR1(2, ABSNEG_F)
868 INSTR2(2, CMPV_F)
869 INSTR1(2, FLOOR_F)
870 INSTR1(2, CEIL_F)
871 INSTR1(2, RNDNE_F)
872 INSTR1(2, RNDAZ_F)
873 INSTR1(2, TRUNC_F)
874 INSTR2(2, ADD_U)
875 INSTR2(2, ADD_S)
876 INSTR2(2, SUB_U)
877 INSTR2(2, SUB_S)
878 INSTR2(2, CMPS_U)
879 INSTR2(2, CMPS_S)
880 INSTR2(2, MIN_U)
881 INSTR2(2, MIN_S)
882 INSTR2(2, MAX_U)
883 INSTR2(2, MAX_S)
884 INSTR1(2, ABSNEG_S)
885 INSTR2(2, AND_B)
886 INSTR2(2, OR_B)
887 INSTR1(2, NOT_B)
888 INSTR2(2, XOR_B)
889 INSTR2(2, CMPV_U)
890 INSTR2(2, CMPV_S)
891 INSTR2(2, MUL_U)
892 INSTR2(2, MUL_S)
893 INSTR2(2, MULL_U)
894 INSTR1(2, BFREV_B)
895 INSTR1(2, CLZ_S)
896 INSTR1(2, CLZ_B)
897 INSTR2(2, SHL_B)
898 INSTR2(2, SHR_B)
899 INSTR2(2, ASHR_B)
900 INSTR2(2, BARY_F)
901 INSTR2(2, MGEN_B)
902 INSTR2(2, GETBIT_B)
903 INSTR1(2, SETRM)
904 INSTR1(2, CBITS_B)
905 INSTR2(2, SHB)
906 INSTR2(2, MSAD)
907
908 /* cat3 instructions: */
909 INSTR3(3, MAD_U16)
910 INSTR3(3, MADSH_U16)
911 INSTR3(3, MAD_S16)
912 INSTR3(3, MADSH_M16)
913 INSTR3(3, MAD_U24)
914 INSTR3(3, MAD_S24)
915 INSTR3(3, MAD_F16)
916 INSTR3(3, MAD_F32)
917 INSTR3(3, SEL_B16)
918 INSTR3(3, SEL_B32)
919 INSTR3(3, SEL_S16)
920 INSTR3(3, SEL_S32)
921 INSTR3(3, SEL_F16)
922 INSTR3(3, SEL_F32)
923 INSTR3(3, SAD_S16)
924 INSTR3(3, SAD_S32)
925
926 /* cat4 instructions: */
927 INSTR1(4, RCP)
928 INSTR1(4, RSQ)
929 INSTR1(4, LOG2)
930 INSTR1(4, EXP2)
931 INSTR1(4, SIN)
932 INSTR1(4, COS)
933 INSTR1(4, SQRT)
934
935 /* cat5 instructions: */
936 INSTR1(5, DSX)
937 INSTR1(5, DSY)
938
939 static inline struct ir3_instruction *
940 ir3_SAM(struct ir3_block *block, opc_t opc, type_t type,
941 unsigned wrmask, unsigned flags, unsigned samp, unsigned tex,
942 struct ir3_instruction *src0, struct ir3_instruction *src1)
943 {
944 struct ir3_instruction *sam;
945 struct ir3_register *reg;
946
947 sam = ir3_instr_create(block, 5, opc);
948 sam->flags |= flags;
949 ir3_reg_create(sam, 0, 0)->wrmask = wrmask;
950 if (src0) {
951 reg = ir3_reg_create(sam, 0, IR3_REG_SSA);
952 reg->wrmask = (1 << (src0->regs_count - 1)) - 1;
953 reg->instr = src0;
954 }
955 if (src1) {
956 reg = ir3_reg_create(sam, 0, IR3_REG_SSA);
957 reg->instr = src1;
958 reg->wrmask = (1 << (src1->regs_count - 1)) - 1;
959 }
960 sam->cat5.samp = samp;
961 sam->cat5.tex = tex;
962 sam->cat5.type = type;
963
964 return sam;
965 }
966
967 /* cat6 instructions: */
968 INSTR2(6, LDLV)
969 INSTR2(6, LDG)
970
971 /* ************************************************************************* */
972 /* split this out or find some helper to use.. like main/bitset.h.. */
973
974 #include <string.h>
975
976 #define MAX_REG 256
977
978 typedef uint8_t regmask_t[2 * MAX_REG / 8];
979
980 static inline unsigned regmask_idx(struct ir3_register *reg)
981 {
982 unsigned num = reg->num;
983 debug_assert(num < MAX_REG);
984 if (reg->flags & IR3_REG_HALF)
985 num += MAX_REG;
986 return num;
987 }
988
989 static inline void regmask_init(regmask_t *regmask)
990 {
991 memset(regmask, 0, sizeof(*regmask));
992 }
993
994 static inline void regmask_set(regmask_t *regmask, struct ir3_register *reg)
995 {
996 unsigned idx = regmask_idx(reg);
997 if (reg->flags & IR3_REG_RELATIV) {
998 unsigned i;
999 for (i = 0; i < reg->size; i++, idx++)
1000 (*regmask)[idx / 8] |= 1 << (idx % 8);
1001 } else {
1002 unsigned mask;
1003 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
1004 if (mask & 1)
1005 (*regmask)[idx / 8] |= 1 << (idx % 8);
1006 }
1007 }
1008
1009 static inline void regmask_or(regmask_t *dst, regmask_t *a, regmask_t *b)
1010 {
1011 unsigned i;
1012 for (i = 0; i < ARRAY_SIZE(*dst); i++)
1013 (*dst)[i] = (*a)[i] | (*b)[i];
1014 }
1015
1016 /* set bits in a if not set in b, conceptually:
1017 * a |= (reg & ~b)
1018 */
1019 static inline void regmask_set_if_not(regmask_t *a,
1020 struct ir3_register *reg, regmask_t *b)
1021 {
1022 unsigned idx = regmask_idx(reg);
1023 if (reg->flags & IR3_REG_RELATIV) {
1024 unsigned i;
1025 for (i = 0; i < reg->size; i++, idx++)
1026 if (!((*b)[idx / 8] & (1 << (idx % 8))))
1027 (*a)[idx / 8] |= 1 << (idx % 8);
1028 } else {
1029 unsigned mask;
1030 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
1031 if (mask & 1)
1032 if (!((*b)[idx / 8] & (1 << (idx % 8))))
1033 (*a)[idx / 8] |= 1 << (idx % 8);
1034 }
1035 }
1036
1037 static inline bool regmask_get(regmask_t *regmask,
1038 struct ir3_register *reg)
1039 {
1040 unsigned idx = regmask_idx(reg);
1041 if (reg->flags & IR3_REG_RELATIV) {
1042 unsigned i;
1043 for (i = 0; i < reg->size; i++, idx++)
1044 if ((*regmask)[idx / 8] & (1 << (idx % 8)))
1045 return true;
1046 } else {
1047 unsigned mask;
1048 for (mask = reg->wrmask; mask; mask >>= 1, idx++)
1049 if (mask & 1)
1050 if ((*regmask)[idx / 8] & (1 << (idx % 8)))
1051 return true;
1052 }
1053 return false;
1054 }
1055
1056 /* ************************************************************************* */
1057
1058 #endif /* IR3_H_ */