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