2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
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:
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
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
30 #include "compiler/shader_enums.h"
32 #include "util/bitscan.h"
33 #include "util/list.h"
35 #include "util/u_debug.h"
37 #include "instr-a3xx.h"
39 /* low level intermediate representation of an adreno shader program */
43 struct ir3_instruction
;
49 uint16_t instrs_count
; /* expanded to account for rpt's */
50 uint16_t nops_count
; /* # of nop instructions, including nopN */
51 /* NOTE: max_reg, etc, does not include registers not touched
52 * by the shader (ie. vertex fetched via VFD_DECODE but not
55 int8_t max_reg
; /* highest GPR # used by shader */
59 /* number of sync bits: */
62 /* estimate of number of cycles stalled on (ss) */
65 uint16_t last_baryf
; /* instruction # of last varying fetch */
70 IR3_REG_CONST
= 0x001,
71 IR3_REG_IMMED
= 0x002,
73 /* high registers are used for some things in compute shaders,
74 * for example. Seems to be for things that are global to all
75 * threads in a wave, so possibly these are global/shared by
76 * all the threads in the wave?
79 IR3_REG_RELATIV
= 0x010,
81 /* Most instructions, it seems, can do float abs/neg but not
82 * integer. The CP pass needs to know what is intended (int or
83 * float) in order to do the right thing. For this reason the
84 * abs/neg flags are split out into float and int variants. In
85 * addition, .b (bitwise) operations, the negate is actually a
86 * bitwise not, so split that out into a new flag to make it
95 IR3_REG_POS_INF
= 0x1000,
96 /* (ei) flag, end-input? Set on last bary, presumably to signal
97 * that the shader needs no more input:
100 /* meta-flags, for intermediate stages of IR, ie.
101 * before register assignment is done:
103 IR3_REG_SSA
= 0x4000, /* 'instr' is ptr to assigning instr */
104 IR3_REG_ARRAY
= 0x8000,
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.
113 unsigned wrmask
: 16; /* up to vec16 */
115 /* for relative addressing, 32bits for array size is too small,
116 * but otoh we don't need to deal with disjoint sets, so instead
117 * use a simple size field (number of scalar components).
119 * Note the size field isn't important for relative const (since
120 * we don't have to do register allocation for constants).
124 bool merged
: 1; /* half-regs conflict with full regs (ie >= a6xx) */
127 * the component is in the low two bits of the reg #, so
128 * rN.x becomes: (N << 2) | x
143 /* For IR3_REG_SSA, src registers contain ptr back to assigning
146 * For IR3_REG_ARRAY, the pointer is back to the last dependent
147 * array access (although the net effect is the same, it points
148 * back to a previous instruction that we depend on).
150 struct ir3_instruction
*instr
;
154 * Stupid/simple growable array implementation:
156 #define DECLARE_ARRAY(type, name) \
157 unsigned name ## _count, name ## _sz; \
160 #define array_insert(ctx, arr, val) do { \
161 if (arr ## _count == arr ## _sz) { \
162 arr ## _sz = MAX2(2 * arr ## _sz, 16); \
163 arr = reralloc_size(ctx, arr, arr ## _sz * sizeof(arr[0])); \
165 arr[arr ##_count++] = val; \
168 struct ir3_instruction
{
169 struct ir3_block
*block
;
172 /* (sy) flag is set on first instruction, and after sample
173 * instructions (probably just on RAW hazard).
175 IR3_INSTR_SY
= 0x001,
176 /* (ss) flag is set on first instruction, and first instruction
177 * to depend on the result of "long" instructions (RAW hazard):
179 * rcp, rsq, log2, exp2, sin, cos, sqrt
181 * It seems to synchronize until all in-flight instructions are
182 * completed, for example:
185 * add.f hr2.z, (neg)hr2.z, hc0.y
186 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
189 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
191 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
192 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
193 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
195 * The last mul.f does not have (ss) set, presumably because the
196 * (ss) on the previous instruction does the job.
198 * The blob driver also seems to set it on WAR hazards, although
199 * not really clear if this is needed or just blob compiler being
200 * sloppy. So far I haven't found a case where removing the (ss)
201 * causes problems for WAR hazard, but I could just be getting
205 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
208 IR3_INSTR_SS
= 0x002,
209 /* (jp) flag is set on jump targets:
211 IR3_INSTR_JP
= 0x004,
212 IR3_INSTR_UL
= 0x008,
213 IR3_INSTR_3D
= 0x010,
218 IR3_INSTR_S2EN
= 0x200,
220 IR3_INSTR_SAT
= 0x800,
221 /* meta-flags, for intermediate stages of IR, ie.
222 * before register assignment is done:
224 IR3_INSTR_MARK
= 0x1000,
225 IR3_INSTR_UNUSED
= 0x2000,
233 struct ir3_register
**regs
;
239 struct ir3_block
*target
;
242 type_t src_type
, dst_type
;
262 int iim_val
: 3; /* for ldgb/stgb, # of components */
267 unsigned w
: 1; /* write */
268 unsigned r
: 1; /* read */
269 unsigned l
: 1; /* local */
270 unsigned g
: 1; /* global */
272 /* for meta-instructions, just used to hold extra data
273 * before instruction scheduling, etc
276 int off
; /* component/offset */
279 /* for output collects, this maps back to the entry in the
280 * ir3_shader_variant::outputs table.
286 unsigned input_offset
;
289 /* maps back to entry in ir3_shader_variant::inputs table: */
291 /* for sysvals, identifies the sysval type. Mostly so we can
292 * identify the special cases where a sysval should not be DCE'd
293 * (currently, just pre-fs texture fetch)
295 gl_system_value sysval
;
299 /* transient values used during various algorithms: */
301 /* The instruction depth is the max dependency distance to output.
303 * You can also think of it as the "cost", if we did any sort of
304 * optimization for register footprint. Ie. a value that is just
305 * result of moving a const to a reg would have a low cost, so to
306 * it could make sense to duplicate the instruction at various
307 * points where the result is needed to reduce register footprint.
310 /* When we get to the RA stage, we no longer need depth, but
311 * we do need instruction's position/name:
319 /* used for per-pass extra instruction data.
321 * TODO we should remove the per-pass data like this and 'use_count'
322 * and do something similar to what RA does w/ ir3_ra_instr_data..
323 * ie. use the ir3_count_instructions pass, and then use instr->ip
324 * to index into a table of pass-private data.
329 * Valid if pass calls ir3_find_ssa_uses().. see foreach_ssa_use()
333 int sun
; /* Sethi–Ullman number, used by sched */
334 int use_count
; /* currently just updated/used by cp */
336 /* Used during CP and RA stages. For collect and shader inputs/
337 * outputs where we need a sequence of consecutive registers,
338 * keep track of each src instructions left (ie 'n-1') and right
339 * (ie 'n+1') neighbor. The front-end must insert enough mov's
340 * to ensure that each instruction has at most one left and at
341 * most one right neighbor. During the copy-propagation pass,
342 * we only remove mov's when we can preserve this constraint.
343 * And during the RA stage, we use the neighbor information to
344 * allocate a block of registers in one shot.
346 * TODO: maybe just add something like:
347 * struct ir3_instruction_ref {
348 * struct ir3_instruction *instr;
352 * Or can we get away without the refcnt stuff? It seems like
353 * it should be overkill.. the problem is if, potentially after
354 * already eliminating some mov's, if you have a single mov that
355 * needs to be grouped with it's neighbors in two different
356 * places (ex. shader output and a collect).
359 struct ir3_instruction
*left
, *right
;
360 uint16_t left_cnt
, right_cnt
;
363 /* an instruction can reference at most one address register amongst
364 * it's src/dst registers. Beyond that, you need to insert mov's.
366 * NOTE: do not write this directly, use ir3_instr_set_address()
368 struct ir3_instruction
*address
;
370 /* Tracking for additional dependent instructions. Used to handle
371 * barriers, WAR hazards for arrays/SSBOs/etc.
373 DECLARE_ARRAY(struct ir3_instruction
*, deps
);
376 * From PoV of instruction scheduling, not execution (ie. ignores global/
377 * local distinction):
378 * shared image atomic SSBO everything
379 * barrier()/ - R/W R/W R/W R/W X
380 * groupMemoryBarrier()
381 * memoryBarrier() - R/W R/W
382 * (but only images declared coherent?)
383 * memoryBarrierAtomic() - R/W
384 * memoryBarrierBuffer() - R/W
385 * memoryBarrierImage() - R/W
386 * memoryBarrierShared() - R/W
388 * TODO I think for SSBO/image/shared, in cases where we can determine
389 * which variable is accessed, we don't need to care about accesses to
390 * different variables (unless declared coherent??)
393 IR3_BARRIER_EVERYTHING
= 1 << 0,
394 IR3_BARRIER_SHARED_R
= 1 << 1,
395 IR3_BARRIER_SHARED_W
= 1 << 2,
396 IR3_BARRIER_IMAGE_R
= 1 << 3,
397 IR3_BARRIER_IMAGE_W
= 1 << 4,
398 IR3_BARRIER_BUFFER_R
= 1 << 5,
399 IR3_BARRIER_BUFFER_W
= 1 << 6,
400 IR3_BARRIER_ARRAY_R
= 1 << 7,
401 IR3_BARRIER_ARRAY_W
= 1 << 8,
402 } barrier_class
, barrier_conflict
;
404 /* Entry in ir3_block's instruction list: */
405 struct list_head node
;
411 // TODO only computerator/assembler:
415 static inline struct ir3_instruction
*
416 ir3_neighbor_first(struct ir3_instruction
*instr
)
419 while (instr
->cp
.left
) {
420 instr
= instr
->cp
.left
;
421 if (++cnt
> 0xffff) {
429 static inline int ir3_neighbor_count(struct ir3_instruction
*instr
)
433 debug_assert(!instr
->cp
.left
);
435 while (instr
->cp
.right
) {
437 instr
= instr
->cp
.right
;
448 struct ir3_compiler
*compiler
;
449 gl_shader_stage type
;
451 DECLARE_ARRAY(struct ir3_instruction
*, inputs
);
452 DECLARE_ARRAY(struct ir3_instruction
*, outputs
);
454 /* Track bary.f (and ldlv) instructions.. this is needed in
455 * scheduling to ensure that all varying fetches happen before
456 * any potential kill instructions. The hw gets grumpy if all
457 * threads in a group are killed before the last bary.f gets
458 * a chance to signal end of input (ei).
460 DECLARE_ARRAY(struct ir3_instruction
*, baryfs
);
462 /* Track all indirect instructions (read and write). To avoid
463 * deadlock scenario where an address register gets scheduled,
464 * but other dependent src instructions cannot be scheduled due
465 * to dependency on a *different* address register value, the
466 * scheduler needs to ensure that all dependencies other than
467 * the instruction other than the address register are scheduled
468 * before the one that writes the address register. Having a
469 * convenient list of instructions that reference some address
470 * register simplifies this.
472 DECLARE_ARRAY(struct ir3_instruction
*, indirects
);
474 /* and same for instructions that consume predicate register: */
475 DECLARE_ARRAY(struct ir3_instruction
*, predicates
);
477 /* Track texture sample instructions which need texture state
478 * patched in (for astc-srgb workaround):
480 DECLARE_ARRAY(struct ir3_instruction
*, astc_srgb
);
482 /* List of blocks: */
483 struct list_head block_list
;
485 /* List of ir3_array's: */
486 struct list_head array_list
;
488 unsigned max_sun
; /* max Sethi–Ullman number */
491 unsigned block_count
, instr_count
;
496 struct list_head node
;
500 struct nir_register
*r
;
502 /* To avoid array write's from getting DCE'd, keep track of the
503 * most recent write. Any array access depends on the most
504 * recent write. This way, nothing depends on writes after the
505 * last read. But all the writes that happen before that have
506 * something depending on them
508 struct ir3_instruction
*last_write
;
510 /* extra stuff used in RA pass: */
511 unsigned base
; /* base vreg name */
512 unsigned reg
; /* base physical reg */
513 uint16_t start_ip
, end_ip
;
515 /* Indicates if half-precision */
519 struct ir3_array
* ir3_lookup_array(struct ir3
*ir
, unsigned id
);
522 struct list_head node
;
525 const struct nir_block
*nblock
;
527 struct list_head instr_list
; /* list of ir3_instruction */
529 /* each block has either one or two successors.. in case of
530 * two successors, 'condition' decides which one to follow.
531 * A block preceding an if/else has two successors.
533 struct ir3_instruction
*condition
;
534 struct ir3_block
*successors
[2];
536 struct set
*predecessors
; /* set of ir3_block */
538 uint16_t start_ip
, end_ip
;
540 /* Track instructions which do not write a register but other-
541 * wise must not be discarded (such as kill, stg, etc)
543 DECLARE_ARRAY(struct ir3_instruction
*, keeps
);
545 /* used for per-pass extra block data. Mainly used right
546 * now in RA step to track livein/liveout.
555 static inline uint32_t
556 block_id(struct ir3_block
*block
)
559 return block
->serialno
;
561 return (uint32_t)(unsigned long)block
;
565 struct ir3
* ir3_create(struct ir3_compiler
*compiler
, gl_shader_stage type
);
566 void ir3_destroy(struct ir3
*shader
);
567 void * ir3_assemble(struct ir3
*shader
,
568 struct ir3_info
*info
, uint32_t gpu_id
);
569 void * ir3_alloc(struct ir3
*shader
, int sz
);
571 struct ir3_block
* ir3_block_create(struct ir3
*shader
);
573 struct ir3_instruction
* ir3_instr_create(struct ir3_block
*block
, opc_t opc
);
574 struct ir3_instruction
* ir3_instr_create2(struct ir3_block
*block
,
575 opc_t opc
, int nreg
);
576 struct ir3_instruction
* ir3_instr_clone(struct ir3_instruction
*instr
);
577 void ir3_instr_add_dep(struct ir3_instruction
*instr
, struct ir3_instruction
*dep
);
578 const char *ir3_instr_name(struct ir3_instruction
*instr
);
580 struct ir3_register
* ir3_reg_create(struct ir3_instruction
*instr
,
582 struct ir3_register
* ir3_reg_clone(struct ir3
*shader
,
583 struct ir3_register
*reg
);
585 void ir3_instr_set_address(struct ir3_instruction
*instr
,
586 struct ir3_instruction
*addr
);
588 static inline bool ir3_instr_check_mark(struct ir3_instruction
*instr
)
590 if (instr
->flags
& IR3_INSTR_MARK
)
591 return true; /* already visited */
592 instr
->flags
|= IR3_INSTR_MARK
;
596 void ir3_block_clear_mark(struct ir3_block
*block
);
597 void ir3_clear_mark(struct ir3
*shader
);
599 unsigned ir3_count_instructions(struct ir3
*ir
);
601 void ir3_find_ssa_uses(struct ir3
*ir
, void *mem_ctx
);
603 #include "util/set.h"
604 #define foreach_ssa_use(__use, __instr) \
605 for (struct ir3_instruction *__use = (void *)~0; \
606 __use && (__instr)->uses; __use = NULL) \
607 set_foreach ((__instr)->uses, __entry) \
608 if ((__use = (void *)__entry->key))
610 #define MAX_ARRAYS 16
618 static inline uint32_t regid(int num
, int comp
)
620 return (num
<< 2) | (comp
& 0x3);
623 static inline uint32_t reg_num(struct ir3_register
*reg
)
625 return reg
->num
>> 2;
628 static inline uint32_t reg_comp(struct ir3_register
*reg
)
630 return reg
->num
& 0x3;
633 #define INVALID_REG regid(63, 0)
634 #define VALIDREG(r) ((r) != INVALID_REG)
635 #define CONDREG(r, val) COND(VALIDREG(r), (val))
637 static inline bool is_flow(struct ir3_instruction
*instr
)
639 return (opc_cat(instr
->opc
) == 0);
642 static inline bool is_kill(struct ir3_instruction
*instr
)
644 return instr
->opc
== OPC_KILL
;
647 static inline bool is_nop(struct ir3_instruction
*instr
)
649 return instr
->opc
== OPC_NOP
;
652 static inline bool is_same_type_reg(struct ir3_register
*reg1
,
653 struct ir3_register
*reg2
)
655 unsigned type_reg1
= (reg1
->flags
& (IR3_REG_HIGH
| IR3_REG_HALF
));
656 unsigned type_reg2
= (reg2
->flags
& (IR3_REG_HIGH
| IR3_REG_HALF
));
658 if (type_reg1
^ type_reg2
)
664 /* Is it a non-transformative (ie. not type changing) mov? This can
665 * also include absneg.s/absneg.f, which for the most part can be
666 * treated as a mov (single src argument).
668 static inline bool is_same_type_mov(struct ir3_instruction
*instr
)
670 struct ir3_register
*dst
;
672 switch (instr
->opc
) {
674 if (instr
->cat1
.src_type
!= instr
->cat1
.dst_type
)
676 /* If the type of dest reg and src reg are different,
677 * it shouldn't be considered as same type mov
679 if (!is_same_type_reg(instr
->regs
[0], instr
->regs
[1]))
684 if (instr
->flags
& IR3_INSTR_SAT
)
686 /* If the type of dest reg and src reg are different,
687 * it shouldn't be considered as same type mov
689 if (!is_same_type_reg(instr
->regs
[0], instr
->regs
[1]))
696 dst
= instr
->regs
[0];
698 /* mov's that write to a0.x or p0.x are special: */
699 if (dst
->num
== regid(REG_P0
, 0))
701 if (dst
->num
== regid(REG_A0
, 0))
704 if (dst
->flags
& (IR3_REG_RELATIV
| IR3_REG_ARRAY
))
710 /* A move from const, which changes size but not type, can also be
711 * folded into dest instruction in some cases.
713 static inline bool is_const_mov(struct ir3_instruction
*instr
)
715 if (instr
->opc
!= OPC_MOV
)
718 if (!(instr
->regs
[1]->flags
& IR3_REG_CONST
))
721 type_t src_type
= instr
->cat1
.src_type
;
722 type_t dst_type
= instr
->cat1
.dst_type
;
724 return (type_float(src_type
) && type_float(dst_type
)) ||
725 (type_uint(src_type
) && type_uint(dst_type
)) ||
726 (type_sint(src_type
) && type_sint(dst_type
));
729 static inline bool is_alu(struct ir3_instruction
*instr
)
731 return (1 <= opc_cat(instr
->opc
)) && (opc_cat(instr
->opc
) <= 3);
734 static inline bool is_sfu(struct ir3_instruction
*instr
)
736 return (opc_cat(instr
->opc
) == 4);
739 static inline bool is_tex(struct ir3_instruction
*instr
)
741 return (opc_cat(instr
->opc
) == 5);
744 static inline bool is_tex_or_prefetch(struct ir3_instruction
*instr
)
746 return is_tex(instr
) || (instr
->opc
== OPC_META_TEX_PREFETCH
);
749 static inline bool is_mem(struct ir3_instruction
*instr
)
751 return (opc_cat(instr
->opc
) == 6);
754 static inline bool is_barrier(struct ir3_instruction
*instr
)
756 return (opc_cat(instr
->opc
) == 7);
760 is_half(struct ir3_instruction
*instr
)
762 return !!(instr
->regs
[0]->flags
& IR3_REG_HALF
);
766 is_high(struct ir3_instruction
*instr
)
768 return !!(instr
->regs
[0]->flags
& IR3_REG_HIGH
);
772 is_store(struct ir3_instruction
*instr
)
774 /* these instructions, the "destination" register is
775 * actually a source, the address to store to.
777 switch (instr
->opc
) {
792 static inline bool is_load(struct ir3_instruction
*instr
)
794 switch (instr
->opc
) {
804 /* probably some others too.. */
811 static inline bool is_input(struct ir3_instruction
*instr
)
813 /* in some cases, ldlv is used to fetch varying without
814 * interpolation.. fortunately inloc is the first src
815 * register in either case
817 switch (instr
->opc
) {
826 static inline bool is_bool(struct ir3_instruction
*instr
)
828 switch (instr
->opc
) {
838 static inline bool is_meta(struct ir3_instruction
*instr
)
840 return (opc_cat(instr
->opc
) == -1);
843 static inline unsigned dest_regs(struct ir3_instruction
*instr
)
845 if ((instr
->regs_count
== 0) || is_store(instr
) || is_flow(instr
))
848 return util_last_bit(instr
->regs
[0]->wrmask
);
851 static inline bool writes_addr(struct ir3_instruction
*instr
)
853 if (instr
->regs_count
> 0) {
854 struct ir3_register
*dst
= instr
->regs
[0];
855 return reg_num(dst
) == REG_A0
;
860 static inline bool writes_pred(struct ir3_instruction
*instr
)
862 if (instr
->regs_count
> 0) {
863 struct ir3_register
*dst
= instr
->regs
[0];
864 return reg_num(dst
) == REG_P0
;
869 /* returns defining instruction for reg */
870 /* TODO better name */
871 static inline struct ir3_instruction
*ssa(struct ir3_register
*reg
)
873 if (reg
->flags
& (IR3_REG_SSA
| IR3_REG_ARRAY
)) {
879 static inline bool conflicts(struct ir3_instruction
*a
,
880 struct ir3_instruction
*b
)
882 return (a
&& b
) && (a
!= b
);
885 static inline bool reg_gpr(struct ir3_register
*r
)
887 if (r
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
889 if ((reg_num(r
) == REG_A0
) || (reg_num(r
) == REG_P0
))
894 static inline type_t
half_type(type_t type
)
897 case TYPE_F32
: return TYPE_F16
;
898 case TYPE_U32
: return TYPE_U16
;
899 case TYPE_S32
: return TYPE_S16
;
910 /* some cat2 instructions (ie. those which are not float) can embed an
913 static inline bool ir3_cat2_int(opc_t opc
)
953 static inline bool ir3_cat2_float(opc_t opc
)
976 static inline bool ir3_cat3_float(opc_t opc
)
989 /* map cat2 instruction to valid abs/neg flags: */
990 static inline unsigned ir3_cat2_absneg(opc_t opc
)
1007 return IR3_REG_FABS
| IR3_REG_FNEG
;
1028 return IR3_REG_SABS
| IR3_REG_SNEG
;
1042 return IR3_REG_BNOT
;
1049 /* map cat3 instructions to valid abs/neg flags: */
1050 static inline unsigned ir3_cat3_absneg(opc_t opc
)
1057 return IR3_REG_FNEG
;
1069 /* neg *may* work on 3rd src.. */
1079 #define MASK(n) ((1 << (n)) - 1)
1081 /* iterator for an instructions's sources (reg), also returns src #: */
1082 #define foreach_src_n(__srcreg, __n, __instr) \
1083 if ((__instr)->regs_count) \
1084 for (unsigned __cnt = (__instr)->regs_count - 1, __n = 0; __n < __cnt; __n++) \
1085 if ((__srcreg = (__instr)->regs[__n + 1]))
1087 /* iterator for an instructions's sources (reg): */
1088 #define foreach_src(__srcreg, __instr) \
1089 foreach_src_n(__srcreg, __i, __instr)
1091 static inline unsigned __ssa_src_cnt(struct ir3_instruction
*instr
)
1093 unsigned cnt
= instr
->regs_count
+ instr
->deps_count
;
1099 static inline struct ir3_instruction
* __ssa_src_n(struct ir3_instruction
*instr
, unsigned n
)
1101 if (n
== (instr
->regs_count
+ instr
->deps_count
))
1102 return instr
->address
;
1103 if (n
>= instr
->regs_count
)
1104 return instr
->deps
[n
- instr
->regs_count
];
1105 return ssa(instr
->regs
[n
]);
1108 static inline bool __is_false_dep(struct ir3_instruction
*instr
, unsigned n
)
1110 if (n
== (instr
->regs_count
+ instr
->deps_count
))
1112 if (n
>= instr
->regs_count
)
1117 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
1119 /* iterator for an instruction's SSA sources (instr), also returns src #: */
1120 #define foreach_ssa_src_n(__srcinst, __n, __instr) \
1121 for (unsigned __cnt = __ssa_src_cnt(__instr), __n = 0; __n < __cnt; __n++) \
1122 if ((__srcinst = __ssa_src_n(__instr, __n)))
1124 /* iterator for an instruction's SSA sources (instr): */
1125 #define foreach_ssa_src(__srcinst, __instr) \
1126 foreach_ssa_src_n(__srcinst, __i, __instr)
1128 /* iterators for shader inputs: */
1129 #define foreach_input_n(__ininstr, __cnt, __ir) \
1130 for (unsigned __cnt = 0; __cnt < (__ir)->inputs_count; __cnt++) \
1131 if ((__ininstr = (__ir)->inputs[__cnt]))
1132 #define foreach_input(__ininstr, __ir) \
1133 foreach_input_n(__ininstr, __i, __ir)
1135 /* iterators for shader outputs: */
1136 #define foreach_output_n(__outinstr, __cnt, __ir) \
1137 for (unsigned __cnt = 0; __cnt < (__ir)->outputs_count; __cnt++) \
1138 if ((__outinstr = (__ir)->outputs[__cnt]))
1139 #define foreach_output(__outinstr, __ir) \
1140 foreach_output_n(__outinstr, __i, __ir)
1142 /* iterators for instructions: */
1143 #define foreach_instr(__instr, __list) \
1144 list_for_each_entry(struct ir3_instruction, __instr, __list, node)
1145 #define foreach_instr_rev(__instr, __list) \
1146 list_for_each_entry_rev(struct ir3_instruction, __instr, __list, node)
1147 #define foreach_instr_safe(__instr, __list) \
1148 list_for_each_entry_safe(struct ir3_instruction, __instr, __list, node)
1150 /* iterators for blocks: */
1151 #define foreach_block(__block, __list) \
1152 list_for_each_entry(struct ir3_block, __block, __list, node)
1153 #define foreach_block_safe(__block, __list) \
1154 list_for_each_entry_safe(struct ir3_block, __block, __list, node)
1156 /* iterators for arrays: */
1157 #define foreach_array(__array, __list) \
1158 list_for_each_entry(struct ir3_array, __array, __list, node)
1161 void ir3_print(struct ir3
*ir
);
1162 void ir3_print_instr(struct ir3_instruction
*instr
);
1164 /* delay calculation: */
1165 int ir3_delayslots(struct ir3_instruction
*assigner
,
1166 struct ir3_instruction
*consumer
, unsigned n
, bool soft
);
1167 unsigned ir3_delay_calc(struct ir3_block
*block
, struct ir3_instruction
*instr
,
1168 bool soft
, bool pred
);
1169 void ir3_remove_nops(struct ir3
*ir
);
1171 /* depth calculation: */
1172 struct ir3_shader_variant
;
1173 void ir3_insert_by_depth(struct ir3_instruction
*instr
, struct list_head
*list
);
1174 void ir3_depth(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1176 /* fp16 conversion folding */
1177 void ir3_cf(struct ir3
*ir
);
1179 /* copy-propagate: */
1180 void ir3_cp(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1182 /* group neighbors and insert mov's to resolve conflicts: */
1183 void ir3_group(struct ir3
*ir
);
1185 /* Sethi–Ullman numbering: */
1186 void ir3_sun(struct ir3
*ir
);
1189 void ir3_sched_add_deps(struct ir3
*ir
);
1190 int ir3_sched(struct ir3
*ir
);
1193 int ir3_postsched(struct ir3_context
*ctx
);
1195 bool ir3_a6xx_fixup_atomic_dests(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1197 /* register assignment: */
1198 struct ir3_ra_reg_set
* ir3_ra_alloc_reg_set(struct ir3_compiler
*compiler
);
1199 int ir3_ra(struct ir3_shader_variant
*v
, struct ir3_instruction
**precolor
, unsigned nprecolor
);
1202 void ir3_legalize(struct ir3
*ir
, struct ir3_shader_variant
*so
, int *max_bary
);
1205 ir3_has_latency_to_hide(struct ir3
*ir
)
1207 /* VS/GS/TCS/TESS co-exist with frag shader invocations, but we don't
1208 * know the nature of the fragment shader. Just assume it will have
1211 if (ir
->type
!= MESA_SHADER_FRAGMENT
)
1214 foreach_block (block
, &ir
->block_list
) {
1215 foreach_instr (instr
, &block
->instr_list
) {
1216 if (is_tex_or_prefetch(instr
))
1219 if (is_load(instr
)) {
1220 switch (instr
->opc
) {
1235 /* ************************************************************************* */
1236 /* instruction helpers */
1238 /* creates SSA src of correct type (ie. half vs full precision) */
1239 static inline struct ir3_register
* __ssa_src(struct ir3_instruction
*instr
,
1240 struct ir3_instruction
*src
, unsigned flags
)
1242 struct ir3_register
*reg
;
1243 if (src
->regs
[0]->flags
& IR3_REG_HALF
)
1244 flags
|= IR3_REG_HALF
;
1245 reg
= ir3_reg_create(instr
, 0, IR3_REG_SSA
| flags
);
1247 reg
->wrmask
= src
->regs
[0]->wrmask
;
1251 static inline struct ir3_register
* __ssa_dst(struct ir3_instruction
*instr
)
1253 struct ir3_register
*reg
= ir3_reg_create(instr
, 0, 0);
1254 reg
->flags
|= IR3_REG_SSA
;
1258 static inline struct ir3_instruction
*
1259 create_immed_typed(struct ir3_block
*block
, uint32_t val
, type_t type
)
1261 struct ir3_instruction
*mov
;
1262 unsigned flags
= (type_size(type
) < 32) ? IR3_REG_HALF
: 0;
1264 mov
= ir3_instr_create(block
, OPC_MOV
);
1265 mov
->cat1
.src_type
= type
;
1266 mov
->cat1
.dst_type
= type
;
1267 __ssa_dst(mov
)->flags
|= flags
;
1268 ir3_reg_create(mov
, 0, IR3_REG_IMMED
| flags
)->uim_val
= val
;
1273 static inline struct ir3_instruction
*
1274 create_immed(struct ir3_block
*block
, uint32_t val
)
1276 return create_immed_typed(block
, val
, TYPE_U32
);
1279 static inline struct ir3_instruction
*
1280 create_uniform_typed(struct ir3_block
*block
, unsigned n
, type_t type
)
1282 struct ir3_instruction
*mov
;
1283 unsigned flags
= (type_size(type
) < 32) ? IR3_REG_HALF
: 0;
1285 mov
= ir3_instr_create(block
, OPC_MOV
);
1286 mov
->cat1
.src_type
= type
;
1287 mov
->cat1
.dst_type
= type
;
1288 __ssa_dst(mov
)->flags
|= flags
;
1289 ir3_reg_create(mov
, n
, IR3_REG_CONST
| flags
);
1294 static inline struct ir3_instruction
*
1295 create_uniform(struct ir3_block
*block
, unsigned n
)
1297 return create_uniform_typed(block
, n
, TYPE_F32
);
1300 static inline struct ir3_instruction
*
1301 create_uniform_indirect(struct ir3_block
*block
, int n
,
1302 struct ir3_instruction
*address
)
1304 struct ir3_instruction
*mov
;
1306 mov
= ir3_instr_create(block
, OPC_MOV
);
1307 mov
->cat1
.src_type
= TYPE_U32
;
1308 mov
->cat1
.dst_type
= TYPE_U32
;
1310 ir3_reg_create(mov
, 0, IR3_REG_CONST
| IR3_REG_RELATIV
)->array
.offset
= n
;
1312 ir3_instr_set_address(mov
, address
);
1317 static inline struct ir3_instruction
*
1318 ir3_MOV(struct ir3_block
*block
, struct ir3_instruction
*src
, type_t type
)
1320 struct ir3_instruction
*instr
= ir3_instr_create(block
, OPC_MOV
);
1322 if (src
->regs
[0]->flags
& IR3_REG_ARRAY
) {
1323 struct ir3_register
*src_reg
= __ssa_src(instr
, src
, IR3_REG_ARRAY
);
1324 src_reg
->array
= src
->regs
[0]->array
;
1326 __ssa_src(instr
, src
, src
->regs
[0]->flags
& IR3_REG_HIGH
);
1328 debug_assert(!(src
->regs
[0]->flags
& IR3_REG_RELATIV
));
1329 instr
->cat1
.src_type
= type
;
1330 instr
->cat1
.dst_type
= type
;
1334 static inline struct ir3_instruction
*
1335 ir3_COV(struct ir3_block
*block
, struct ir3_instruction
*src
,
1336 type_t src_type
, type_t dst_type
)
1338 struct ir3_instruction
*instr
= ir3_instr_create(block
, OPC_MOV
);
1339 unsigned dst_flags
= (type_size(dst_type
) < 32) ? IR3_REG_HALF
: 0;
1340 unsigned src_flags
= (type_size(src_type
) < 32) ? IR3_REG_HALF
: 0;
1342 debug_assert((src
->regs
[0]->flags
& IR3_REG_HALF
) == src_flags
);
1344 __ssa_dst(instr
)->flags
|= dst_flags
;
1345 __ssa_src(instr
, src
, 0);
1346 instr
->cat1
.src_type
= src_type
;
1347 instr
->cat1
.dst_type
= dst_type
;
1348 debug_assert(!(src
->regs
[0]->flags
& IR3_REG_ARRAY
));
1352 static inline struct ir3_instruction
*
1353 ir3_NOP(struct ir3_block
*block
)
1355 return ir3_instr_create(block
, OPC_NOP
);
1358 #define IR3_INSTR_0 0
1360 #define __INSTR0(flag, name, opc) \
1361 static inline struct ir3_instruction * \
1362 ir3_##name(struct ir3_block *block) \
1364 struct ir3_instruction *instr = \
1365 ir3_instr_create(block, opc); \
1366 instr->flags |= flag; \
1369 #define INSTR0F(f, name) __INSTR0(IR3_INSTR_##f, name##_##f, OPC_##name)
1370 #define INSTR0(name) __INSTR0(0, name, OPC_##name)
1372 #define __INSTR1(flag, name, opc) \
1373 static inline struct ir3_instruction * \
1374 ir3_##name(struct ir3_block *block, \
1375 struct ir3_instruction *a, unsigned aflags) \
1377 struct ir3_instruction *instr = \
1378 ir3_instr_create(block, opc); \
1380 __ssa_src(instr, a, aflags); \
1381 instr->flags |= flag; \
1384 #define INSTR1F(f, name) __INSTR1(IR3_INSTR_##f, name##_##f, OPC_##name)
1385 #define INSTR1(name) __INSTR1(0, name, OPC_##name)
1387 #define __INSTR2(flag, name, opc) \
1388 static inline struct ir3_instruction * \
1389 ir3_##name(struct ir3_block *block, \
1390 struct ir3_instruction *a, unsigned aflags, \
1391 struct ir3_instruction *b, unsigned bflags) \
1393 struct ir3_instruction *instr = \
1394 ir3_instr_create(block, opc); \
1396 __ssa_src(instr, a, aflags); \
1397 __ssa_src(instr, b, bflags); \
1398 instr->flags |= flag; \
1401 #define INSTR2F(f, name) __INSTR2(IR3_INSTR_##f, name##_##f, OPC_##name)
1402 #define INSTR2(name) __INSTR2(0, name, OPC_##name)
1404 #define __INSTR3(flag, name, opc) \
1405 static inline struct ir3_instruction * \
1406 ir3_##name(struct ir3_block *block, \
1407 struct ir3_instruction *a, unsigned aflags, \
1408 struct ir3_instruction *b, unsigned bflags, \
1409 struct ir3_instruction *c, unsigned cflags) \
1411 struct ir3_instruction *instr = \
1412 ir3_instr_create2(block, opc, 4); \
1414 __ssa_src(instr, a, aflags); \
1415 __ssa_src(instr, b, bflags); \
1416 __ssa_src(instr, c, cflags); \
1417 instr->flags |= flag; \
1420 #define INSTR3F(f, name) __INSTR3(IR3_INSTR_##f, name##_##f, OPC_##name)
1421 #define INSTR3(name) __INSTR3(0, name, OPC_##name)
1423 #define __INSTR4(flag, name, opc) \
1424 static inline struct ir3_instruction * \
1425 ir3_##name(struct ir3_block *block, \
1426 struct ir3_instruction *a, unsigned aflags, \
1427 struct ir3_instruction *b, unsigned bflags, \
1428 struct ir3_instruction *c, unsigned cflags, \
1429 struct ir3_instruction *d, unsigned dflags) \
1431 struct ir3_instruction *instr = \
1432 ir3_instr_create2(block, opc, 5); \
1434 __ssa_src(instr, a, aflags); \
1435 __ssa_src(instr, b, bflags); \
1436 __ssa_src(instr, c, cflags); \
1437 __ssa_src(instr, d, dflags); \
1438 instr->flags |= flag; \
1441 #define INSTR4F(f, name) __INSTR4(IR3_INSTR_##f, name##_##f, OPC_##name)
1442 #define INSTR4(name) __INSTR4(0, name, OPC_##name)
1444 /* cat0 instructions: */
1455 /* cat2 instructions, most 2 src but some 1 src: */
1503 /* cat3 instructions: */
1521 /* cat4 instructions: */
1533 /* cat5 instructions: */
1542 static inline struct ir3_instruction
*
1543 ir3_SAM(struct ir3_block
*block
, opc_t opc
, type_t type
,
1544 unsigned wrmask
, unsigned flags
, struct ir3_instruction
*samp_tex
,
1545 struct ir3_instruction
*src0
, struct ir3_instruction
*src1
)
1547 struct ir3_instruction
*sam
;
1549 sam
= ir3_instr_create(block
, opc
);
1550 sam
->flags
|= flags
| IR3_INSTR_S2EN
;
1551 __ssa_dst(sam
)->wrmask
= wrmask
;
1552 __ssa_src(sam
, samp_tex
, IR3_REG_HALF
);
1554 __ssa_src(sam
, src0
, 0)->wrmask
= (1 << (src0
->regs_count
- 1)) - 1;
1557 __ssa_src(sam
, src1
, 0)->wrmask
=(1 << (src1
->regs_count
- 1)) - 1;
1559 sam
->cat5
.type
= type
;
1564 /* cat6 instructions: */
1579 INSTR2(ATOMIC_CMPXCHG
)
1588 INSTR3F(G
, ATOMIC_ADD
)
1589 INSTR3F(G
, ATOMIC_SUB
)
1590 INSTR3F(G
, ATOMIC_XCHG
)
1591 INSTR3F(G
, ATOMIC_INC
)
1592 INSTR3F(G
, ATOMIC_DEC
)
1593 INSTR3F(G
, ATOMIC_CMPXCHG
)
1594 INSTR3F(G
, ATOMIC_MIN
)
1595 INSTR3F(G
, ATOMIC_MAX
)
1596 INSTR3F(G
, ATOMIC_AND
)
1597 INSTR3F(G
, ATOMIC_OR
)
1598 INSTR3F(G
, ATOMIC_XOR
)
1603 INSTR4F(G
, ATOMIC_ADD
)
1604 INSTR4F(G
, ATOMIC_SUB
)
1605 INSTR4F(G
, ATOMIC_XCHG
)
1606 INSTR4F(G
, ATOMIC_INC
)
1607 INSTR4F(G
, ATOMIC_DEC
)
1608 INSTR4F(G
, ATOMIC_CMPXCHG
)
1609 INSTR4F(G
, ATOMIC_MIN
)
1610 INSTR4F(G
, ATOMIC_MAX
)
1611 INSTR4F(G
, ATOMIC_AND
)
1612 INSTR4F(G
, ATOMIC_OR
)
1613 INSTR4F(G
, ATOMIC_XOR
)
1618 /* cat7 instructions: */
1622 /* meta instructions: */
1623 INSTR0(META_TEX_PREFETCH
);
1625 /* ************************************************************************* */
1626 /* split this out or find some helper to use.. like main/bitset.h.. */
1629 #include "util/bitset.h"
1633 typedef BITSET_DECLARE(regmask_t
, 2 * MAX_REG
);
1636 __regmask_get(regmask_t
*regmask
, struct ir3_register
*reg
, unsigned n
)
1639 /* a6xx+ case, with merged register file, we track things in terms
1640 * of half-precision registers, with a full precisions register
1641 * using two half-precision slots:
1643 if (reg
->flags
& IR3_REG_HALF
) {
1644 return BITSET_TEST(*regmask
, n
);
1647 return BITSET_TEST(*regmask
, n
) || BITSET_TEST(*regmask
, n
+1);
1650 /* pre a6xx case, with separate register file for half and full
1653 if (reg
->flags
& IR3_REG_HALF
)
1655 return BITSET_TEST(*regmask
, n
);
1660 __regmask_set(regmask_t
*regmask
, struct ir3_register
*reg
, unsigned n
)
1663 /* a6xx+ case, with merged register file, we track things in terms
1664 * of half-precision registers, with a full precisions register
1665 * using two half-precision slots:
1667 if (reg
->flags
& IR3_REG_HALF
) {
1668 BITSET_SET(*regmask
, n
);
1671 BITSET_SET(*regmask
, n
);
1672 BITSET_SET(*regmask
, n
+1);
1675 /* pre a6xx case, with separate register file for half and full
1678 if (reg
->flags
& IR3_REG_HALF
)
1680 BITSET_SET(*regmask
, n
);
1684 static inline void regmask_init(regmask_t
*regmask
)
1686 memset(regmask
, 0, sizeof(*regmask
));
1689 static inline void regmask_set(regmask_t
*regmask
, struct ir3_register
*reg
)
1691 if (reg
->flags
& IR3_REG_RELATIV
) {
1692 for (unsigned i
= 0; i
< reg
->size
; i
++)
1693 __regmask_set(regmask
, reg
, reg
->array
.offset
+ i
);
1695 for (unsigned mask
= reg
->wrmask
, n
= reg
->num
; mask
; mask
>>= 1, n
++)
1697 __regmask_set(regmask
, reg
, n
);
1701 static inline void regmask_or(regmask_t
*dst
, regmask_t
*a
, regmask_t
*b
)
1704 for (i
= 0; i
< ARRAY_SIZE(*dst
); i
++)
1705 (*dst
)[i
] = (*a
)[i
] | (*b
)[i
];
1708 static inline bool regmask_get(regmask_t
*regmask
,
1709 struct ir3_register
*reg
)
1711 if (reg
->flags
& IR3_REG_RELATIV
) {
1712 for (unsigned i
= 0; i
< reg
->size
; i
++)
1713 if (__regmask_get(regmask
, reg
, reg
->array
.offset
+ i
))
1716 for (unsigned mask
= reg
->wrmask
, n
= reg
->num
; mask
; mask
>>= 1, n
++)
1718 if (__regmask_get(regmask
, reg
, n
))
1724 /* ************************************************************************* */