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 uint16_t last_baryf
; /* instruction # of last varying fetch */
67 IR3_REG_CONST
= 0x001,
68 IR3_REG_IMMED
= 0x002,
70 /* high registers are used for some things in compute shaders,
71 * for example. Seems to be for things that are global to all
72 * threads in a wave, so possibly these are global/shared by
73 * all the threads in the wave?
76 IR3_REG_RELATIV
= 0x010,
78 /* Most instructions, it seems, can do float abs/neg but not
79 * integer. The CP pass needs to know what is intended (int or
80 * float) in order to do the right thing. For this reason the
81 * abs/neg flags are split out into float and int variants. In
82 * addition, .b (bitwise) operations, the negate is actually a
83 * bitwise not, so split that out into a new flag to make it
92 IR3_REG_POS_INF
= 0x1000,
93 /* (ei) flag, end-input? Set on last bary, presumably to signal
94 * that the shader needs no more input:
97 /* meta-flags, for intermediate stages of IR, ie.
98 * before register assignment is done:
100 IR3_REG_SSA
= 0x4000, /* 'instr' is ptr to assigning instr */
101 IR3_REG_ARRAY
= 0x8000,
105 /* used for cat5 instructions, but also for internal/IR level
106 * tracking of what registers are read/written by an instruction.
107 * wrmask may be a bad name since it is used to represent both
108 * src and dst that touch multiple adjacent registers.
110 unsigned wrmask
: 16; /* up to vec16 */
112 /* for relative addressing, 32bits for array size is too small,
113 * but otoh we don't need to deal with disjoint sets, so instead
114 * use a simple size field (number of scalar components).
116 * Note the size field isn't important for relative const (since
117 * we don't have to do register allocation for constants).
121 bool merged
: 1; /* half-regs conflict with full regs (ie >= a6xx) */
124 * the component is in the low two bits of the reg #, so
125 * rN.x becomes: (N << 2) | x
140 /* For IR3_REG_SSA, src registers contain ptr back to assigning
143 * For IR3_REG_ARRAY, the pointer is back to the last dependent
144 * array access (although the net effect is the same, it points
145 * back to a previous instruction that we depend on).
147 struct ir3_instruction
*instr
;
151 * Stupid/simple growable array implementation:
153 #define DECLARE_ARRAY(type, name) \
154 unsigned name ## _count, name ## _sz; \
157 #define array_insert(ctx, arr, val) do { \
158 if (arr ## _count == arr ## _sz) { \
159 arr ## _sz = MAX2(2 * arr ## _sz, 16); \
160 arr = reralloc_size(ctx, arr, arr ## _sz * sizeof(arr[0])); \
162 arr[arr ##_count++] = val; \
165 struct ir3_instruction
{
166 struct ir3_block
*block
;
169 /* (sy) flag is set on first instruction, and after sample
170 * instructions (probably just on RAW hazard).
172 IR3_INSTR_SY
= 0x001,
173 /* (ss) flag is set on first instruction, and first instruction
174 * to depend on the result of "long" instructions (RAW hazard):
176 * rcp, rsq, log2, exp2, sin, cos, sqrt
178 * It seems to synchronize until all in-flight instructions are
179 * completed, for example:
182 * add.f hr2.z, (neg)hr2.z, hc0.y
183 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
186 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
188 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
189 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
190 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
192 * The last mul.f does not have (ss) set, presumably because the
193 * (ss) on the previous instruction does the job.
195 * The blob driver also seems to set it on WAR hazards, although
196 * not really clear if this is needed or just blob compiler being
197 * sloppy. So far I haven't found a case where removing the (ss)
198 * causes problems for WAR hazard, but I could just be getting
202 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
205 IR3_INSTR_SS
= 0x002,
206 /* (jp) flag is set on jump targets:
208 IR3_INSTR_JP
= 0x004,
209 IR3_INSTR_UL
= 0x008,
210 IR3_INSTR_3D
= 0x010,
215 IR3_INSTR_S2EN
= 0x200,
217 IR3_INSTR_SAT
= 0x800,
218 /* meta-flags, for intermediate stages of IR, ie.
219 * before register assignment is done:
221 IR3_INSTR_MARK
= 0x1000,
222 IR3_INSTR_UNUSED
= 0x2000,
230 struct ir3_register
**regs
;
236 struct ir3_block
*target
;
239 type_t src_type
, dst_type
;
259 int iim_val
: 3; /* for ldgb/stgb, # of components */
264 unsigned w
: 1; /* write */
265 unsigned r
: 1; /* read */
266 unsigned l
: 1; /* local */
267 unsigned g
: 1; /* global */
269 /* for meta-instructions, just used to hold extra data
270 * before instruction scheduling, etc
273 int off
; /* component/offset */
276 /* for output collects, this maps back to the entry in the
277 * ir3_shader_variant::outputs table.
283 unsigned input_offset
;
286 /* maps back to entry in ir3_shader_variant::inputs table: */
288 /* for sysvals, identifies the sysval type. Mostly so we can
289 * identify the special cases where a sysval should not be DCE'd
290 * (currently, just pre-fs texture fetch)
292 gl_system_value sysval
;
296 /* transient values used during various algorithms: */
298 /* The instruction depth is the max dependency distance to output.
300 * You can also think of it as the "cost", if we did any sort of
301 * optimization for register footprint. Ie. a value that is just
302 * result of moving a const to a reg would have a low cost, so to
303 * it could make sense to duplicate the instruction at various
304 * points where the result is needed to reduce register footprint.
307 /* When we get to the RA stage, we no longer need depth, but
308 * we do need instruction's position/name:
316 /* used for per-pass extra instruction data.
318 * TODO we should remove the per-pass data like this and 'use_count'
319 * and do something similar to what RA does w/ ir3_ra_instr_data..
320 * ie. use the ir3_count_instructions pass, and then use instr->ip
321 * to index into a table of pass-private data.
325 int sun
; /* Sethi–Ullman number, used by sched */
326 int use_count
; /* currently just updated/used by cp */
328 /* Used during CP and RA stages. For collect and shader inputs/
329 * outputs where we need a sequence of consecutive registers,
330 * keep track of each src instructions left (ie 'n-1') and right
331 * (ie 'n+1') neighbor. The front-end must insert enough mov's
332 * to ensure that each instruction has at most one left and at
333 * most one right neighbor. During the copy-propagation pass,
334 * we only remove mov's when we can preserve this constraint.
335 * And during the RA stage, we use the neighbor information to
336 * allocate a block of registers in one shot.
338 * TODO: maybe just add something like:
339 * struct ir3_instruction_ref {
340 * struct ir3_instruction *instr;
344 * Or can we get away without the refcnt stuff? It seems like
345 * it should be overkill.. the problem is if, potentially after
346 * already eliminating some mov's, if you have a single mov that
347 * needs to be grouped with it's neighbors in two different
348 * places (ex. shader output and a collect).
351 struct ir3_instruction
*left
, *right
;
352 uint16_t left_cnt
, right_cnt
;
355 /* an instruction can reference at most one address register amongst
356 * it's src/dst registers. Beyond that, you need to insert mov's.
358 * NOTE: do not write this directly, use ir3_instr_set_address()
360 struct ir3_instruction
*address
;
362 /* Tracking for additional dependent instructions. Used to handle
363 * barriers, WAR hazards for arrays/SSBOs/etc.
365 DECLARE_ARRAY(struct ir3_instruction
*, deps
);
368 * From PoV of instruction scheduling, not execution (ie. ignores global/
369 * local distinction):
370 * shared image atomic SSBO everything
371 * barrier()/ - R/W R/W R/W R/W X
372 * groupMemoryBarrier()
373 * memoryBarrier() - R/W R/W
374 * (but only images declared coherent?)
375 * memoryBarrierAtomic() - R/W
376 * memoryBarrierBuffer() - R/W
377 * memoryBarrierImage() - R/W
378 * memoryBarrierShared() - R/W
380 * TODO I think for SSBO/image/shared, in cases where we can determine
381 * which variable is accessed, we don't need to care about accesses to
382 * different variables (unless declared coherent??)
385 IR3_BARRIER_EVERYTHING
= 1 << 0,
386 IR3_BARRIER_SHARED_R
= 1 << 1,
387 IR3_BARRIER_SHARED_W
= 1 << 2,
388 IR3_BARRIER_IMAGE_R
= 1 << 3,
389 IR3_BARRIER_IMAGE_W
= 1 << 4,
390 IR3_BARRIER_BUFFER_R
= 1 << 5,
391 IR3_BARRIER_BUFFER_W
= 1 << 6,
392 IR3_BARRIER_ARRAY_R
= 1 << 7,
393 IR3_BARRIER_ARRAY_W
= 1 << 8,
394 } barrier_class
, barrier_conflict
;
396 /* Entry in ir3_block's instruction list: */
397 struct list_head node
;
404 static inline struct ir3_instruction
*
405 ir3_neighbor_first(struct ir3_instruction
*instr
)
408 while (instr
->cp
.left
) {
409 instr
= instr
->cp
.left
;
410 if (++cnt
> 0xffff) {
418 static inline int ir3_neighbor_count(struct ir3_instruction
*instr
)
422 debug_assert(!instr
->cp
.left
);
424 while (instr
->cp
.right
) {
426 instr
= instr
->cp
.right
;
437 struct ir3_compiler
*compiler
;
438 gl_shader_stage type
;
440 DECLARE_ARRAY(struct ir3_instruction
*, inputs
);
441 DECLARE_ARRAY(struct ir3_instruction
*, outputs
);
443 /* Track bary.f (and ldlv) instructions.. this is needed in
444 * scheduling to ensure that all varying fetches happen before
445 * any potential kill instructions. The hw gets grumpy if all
446 * threads in a group are killed before the last bary.f gets
447 * a chance to signal end of input (ei).
449 DECLARE_ARRAY(struct ir3_instruction
*, baryfs
);
451 /* Track all indirect instructions (read and write). To avoid
452 * deadlock scenario where an address register gets scheduled,
453 * but other dependent src instructions cannot be scheduled due
454 * to dependency on a *different* address register value, the
455 * scheduler needs to ensure that all dependencies other than
456 * the instruction other than the address register are scheduled
457 * before the one that writes the address register. Having a
458 * convenient list of instructions that reference some address
459 * register simplifies this.
461 DECLARE_ARRAY(struct ir3_instruction
*, indirects
);
463 /* and same for instructions that consume predicate register: */
464 DECLARE_ARRAY(struct ir3_instruction
*, predicates
);
466 /* Track texture sample instructions which need texture state
467 * patched in (for astc-srgb workaround):
469 DECLARE_ARRAY(struct ir3_instruction
*, astc_srgb
);
471 /* List of blocks: */
472 struct list_head block_list
;
474 /* List of ir3_array's: */
475 struct list_head array_list
;
477 unsigned max_sun
; /* max Sethi–Ullman number */
480 unsigned block_count
, instr_count
;
485 struct list_head node
;
489 struct nir_register
*r
;
491 /* To avoid array write's from getting DCE'd, keep track of the
492 * most recent write. Any array access depends on the most
493 * recent write. This way, nothing depends on writes after the
494 * last read. But all the writes that happen before that have
495 * something depending on them
497 struct ir3_instruction
*last_write
;
499 /* extra stuff used in RA pass: */
500 unsigned base
; /* base vreg name */
501 unsigned reg
; /* base physical reg */
502 uint16_t start_ip
, end_ip
;
504 /* Indicates if half-precision */
508 struct ir3_array
* ir3_lookup_array(struct ir3
*ir
, unsigned id
);
511 struct list_head node
;
514 const struct nir_block
*nblock
;
516 struct list_head instr_list
; /* list of ir3_instruction */
518 /* each block has either one or two successors.. in case of
519 * two successors, 'condition' decides which one to follow.
520 * A block preceding an if/else has two successors.
522 struct ir3_instruction
*condition
;
523 struct ir3_block
*successors
[2];
525 struct set
*predecessors
; /* set of ir3_block */
527 uint16_t start_ip
, end_ip
;
529 /* Track instructions which do not write a register but other-
530 * wise must not be discarded (such as kill, stg, etc)
532 DECLARE_ARRAY(struct ir3_instruction
*, keeps
);
534 /* used for per-pass extra block data. Mainly used right
535 * now in RA step to track livein/liveout.
544 static inline uint32_t
545 block_id(struct ir3_block
*block
)
548 return block
->serialno
;
550 return (uint32_t)(unsigned long)block
;
554 struct ir3
* ir3_create(struct ir3_compiler
*compiler
, gl_shader_stage type
);
555 void ir3_destroy(struct ir3
*shader
);
556 void * ir3_assemble(struct ir3
*shader
,
557 struct ir3_info
*info
, uint32_t gpu_id
);
558 void * ir3_alloc(struct ir3
*shader
, int sz
);
560 struct ir3_block
* ir3_block_create(struct ir3
*shader
);
562 struct ir3_instruction
* ir3_instr_create(struct ir3_block
*block
, opc_t opc
);
563 struct ir3_instruction
* ir3_instr_create2(struct ir3_block
*block
,
564 opc_t opc
, int nreg
);
565 struct ir3_instruction
* ir3_instr_clone(struct ir3_instruction
*instr
);
566 void ir3_instr_add_dep(struct ir3_instruction
*instr
, struct ir3_instruction
*dep
);
567 const char *ir3_instr_name(struct ir3_instruction
*instr
);
569 struct ir3_register
* ir3_reg_create(struct ir3_instruction
*instr
,
571 struct ir3_register
* ir3_reg_clone(struct ir3
*shader
,
572 struct ir3_register
*reg
);
574 void ir3_instr_set_address(struct ir3_instruction
*instr
,
575 struct ir3_instruction
*addr
);
577 static inline bool ir3_instr_check_mark(struct ir3_instruction
*instr
)
579 if (instr
->flags
& IR3_INSTR_MARK
)
580 return true; /* already visited */
581 instr
->flags
|= IR3_INSTR_MARK
;
585 void ir3_block_clear_mark(struct ir3_block
*block
);
586 void ir3_clear_mark(struct ir3
*shader
);
588 unsigned ir3_count_instructions(struct ir3
*ir
);
590 static inline int ir3_instr_regno(struct ir3_instruction
*instr
,
591 struct ir3_register
*reg
)
594 for (i
= 0; i
< instr
->regs_count
; i
++)
595 if (reg
== instr
->regs
[i
])
601 #define MAX_ARRAYS 16
609 static inline uint32_t regid(int num
, int comp
)
611 return (num
<< 2) | (comp
& 0x3);
614 static inline uint32_t reg_num(struct ir3_register
*reg
)
616 return reg
->num
>> 2;
619 static inline uint32_t reg_comp(struct ir3_register
*reg
)
621 return reg
->num
& 0x3;
624 #define INVALID_REG regid(63, 0)
625 #define VALIDREG(r) ((r) != INVALID_REG)
626 #define CONDREG(r, val) COND(VALIDREG(r), (val))
628 static inline bool is_flow(struct ir3_instruction
*instr
)
630 return (opc_cat(instr
->opc
) == 0);
633 static inline bool is_kill(struct ir3_instruction
*instr
)
635 return instr
->opc
== OPC_KILL
;
638 static inline bool is_nop(struct ir3_instruction
*instr
)
640 return instr
->opc
== OPC_NOP
;
643 static inline bool is_same_type_reg(struct ir3_register
*reg1
,
644 struct ir3_register
*reg2
)
646 unsigned type_reg1
= (reg1
->flags
& (IR3_REG_HIGH
| IR3_REG_HALF
));
647 unsigned type_reg2
= (reg2
->flags
& (IR3_REG_HIGH
| IR3_REG_HALF
));
649 if (type_reg1
^ type_reg2
)
655 /* Is it a non-transformative (ie. not type changing) mov? This can
656 * also include absneg.s/absneg.f, which for the most part can be
657 * treated as a mov (single src argument).
659 static inline bool is_same_type_mov(struct ir3_instruction
*instr
)
661 struct ir3_register
*dst
;
663 switch (instr
->opc
) {
665 if (instr
->cat1
.src_type
!= instr
->cat1
.dst_type
)
667 /* If the type of dest reg and src reg are different,
668 * it shouldn't be considered as same type mov
670 if (!is_same_type_reg(instr
->regs
[0], instr
->regs
[1]))
675 if (instr
->flags
& IR3_INSTR_SAT
)
677 /* If the type of dest reg and src reg are different,
678 * it shouldn't be considered as same type mov
680 if (!is_same_type_reg(instr
->regs
[0], instr
->regs
[1]))
687 dst
= instr
->regs
[0];
689 /* mov's that write to a0.x or p0.x are special: */
690 if (dst
->num
== regid(REG_P0
, 0))
692 if (dst
->num
== regid(REG_A0
, 0))
695 if (dst
->flags
& (IR3_REG_RELATIV
| IR3_REG_ARRAY
))
701 /* A move from const, which changes size but not type, can also be
702 * folded into dest instruction in some cases.
704 static inline bool is_const_mov(struct ir3_instruction
*instr
)
706 if (instr
->opc
!= OPC_MOV
)
709 if (!(instr
->regs
[1]->flags
& IR3_REG_CONST
))
712 type_t src_type
= instr
->cat1
.src_type
;
713 type_t dst_type
= instr
->cat1
.dst_type
;
715 return (type_float(src_type
) && type_float(dst_type
)) ||
716 (type_uint(src_type
) && type_uint(dst_type
)) ||
717 (type_sint(src_type
) && type_sint(dst_type
));
720 static inline bool is_alu(struct ir3_instruction
*instr
)
722 return (1 <= opc_cat(instr
->opc
)) && (opc_cat(instr
->opc
) <= 3);
725 static inline bool is_sfu(struct ir3_instruction
*instr
)
727 return (opc_cat(instr
->opc
) == 4);
730 static inline bool is_tex(struct ir3_instruction
*instr
)
732 return (opc_cat(instr
->opc
) == 5);
735 static inline bool is_tex_or_prefetch(struct ir3_instruction
*instr
)
737 return is_tex(instr
) || (instr
->opc
== OPC_META_TEX_PREFETCH
);
740 static inline bool is_mem(struct ir3_instruction
*instr
)
742 return (opc_cat(instr
->opc
) == 6);
745 static inline bool is_barrier(struct ir3_instruction
*instr
)
747 return (opc_cat(instr
->opc
) == 7);
751 is_store(struct ir3_instruction
*instr
)
753 /* these instructions, the "destination" register is
754 * actually a source, the address to store to.
756 switch (instr
->opc
) {
771 static inline bool is_load(struct ir3_instruction
*instr
)
773 switch (instr
->opc
) {
783 /* probably some others too.. */
790 static inline bool is_input(struct ir3_instruction
*instr
)
792 /* in some cases, ldlv is used to fetch varying without
793 * interpolation.. fortunately inloc is the first src
794 * register in either case
796 switch (instr
->opc
) {
805 static inline bool is_bool(struct ir3_instruction
*instr
)
807 switch (instr
->opc
) {
817 static inline bool is_meta(struct ir3_instruction
*instr
)
819 return (opc_cat(instr
->opc
) == -1);
822 static inline unsigned dest_regs(struct ir3_instruction
*instr
)
824 if ((instr
->regs_count
== 0) || is_store(instr
) || is_flow(instr
))
827 return util_last_bit(instr
->regs
[0]->wrmask
);
830 static inline bool writes_addr(struct ir3_instruction
*instr
)
832 if (instr
->regs_count
> 0) {
833 struct ir3_register
*dst
= instr
->regs
[0];
834 return reg_num(dst
) == REG_A0
;
839 static inline bool writes_pred(struct ir3_instruction
*instr
)
841 if (instr
->regs_count
> 0) {
842 struct ir3_register
*dst
= instr
->regs
[0];
843 return reg_num(dst
) == REG_P0
;
848 /* returns defining instruction for reg */
849 /* TODO better name */
850 static inline struct ir3_instruction
*ssa(struct ir3_register
*reg
)
852 if (reg
->flags
& (IR3_REG_SSA
| IR3_REG_ARRAY
)) {
858 static inline bool conflicts(struct ir3_instruction
*a
,
859 struct ir3_instruction
*b
)
861 return (a
&& b
) && (a
!= b
);
864 static inline bool reg_gpr(struct ir3_register
*r
)
866 if (r
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
868 if ((reg_num(r
) == REG_A0
) || (reg_num(r
) == REG_P0
))
873 static inline type_t
half_type(type_t type
)
876 case TYPE_F32
: return TYPE_F16
;
877 case TYPE_U32
: return TYPE_U16
;
878 case TYPE_S32
: return TYPE_S16
;
889 /* some cat2 instructions (ie. those which are not float) can embed an
892 static inline bool ir3_cat2_int(opc_t opc
)
932 static inline bool ir3_cat2_float(opc_t opc
)
955 static inline bool ir3_cat3_float(opc_t opc
)
968 /* map cat2 instruction to valid abs/neg flags: */
969 static inline unsigned ir3_cat2_absneg(opc_t opc
)
986 return IR3_REG_FABS
| IR3_REG_FNEG
;
1007 return IR3_REG_SABS
| IR3_REG_SNEG
;
1021 return IR3_REG_BNOT
;
1028 /* map cat3 instructions to valid abs/neg flags: */
1029 static inline unsigned ir3_cat3_absneg(opc_t opc
)
1036 return IR3_REG_FNEG
;
1048 /* neg *may* work on 3rd src.. */
1058 #define MASK(n) ((1 << (n)) - 1)
1060 /* iterator for an instructions's sources (reg), also returns src #: */
1061 #define foreach_src_n(__srcreg, __n, __instr) \
1062 if ((__instr)->regs_count) \
1063 for (unsigned __cnt = (__instr)->regs_count - 1, __n = 0; __n < __cnt; __n++) \
1064 if ((__srcreg = (__instr)->regs[__n + 1]))
1066 /* iterator for an instructions's sources (reg): */
1067 #define foreach_src(__srcreg, __instr) \
1068 foreach_src_n(__srcreg, __i, __instr)
1070 static inline unsigned __ssa_src_cnt(struct ir3_instruction
*instr
)
1072 unsigned cnt
= instr
->regs_count
+ instr
->deps_count
;
1078 static inline struct ir3_instruction
* __ssa_src_n(struct ir3_instruction
*instr
, unsigned n
)
1080 if (n
== (instr
->regs_count
+ instr
->deps_count
))
1081 return instr
->address
;
1082 if (n
>= instr
->regs_count
)
1083 return instr
->deps
[n
- instr
->regs_count
];
1084 return ssa(instr
->regs
[n
]);
1087 static inline bool __is_false_dep(struct ir3_instruction
*instr
, unsigned n
)
1089 if (n
== (instr
->regs_count
+ instr
->deps_count
))
1091 if (n
>= instr
->regs_count
)
1096 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
1098 /* iterator for an instruction's SSA sources (instr), also returns src #: */
1099 #define foreach_ssa_src_n(__srcinst, __n, __instr) \
1100 for (unsigned __cnt = __ssa_src_cnt(__instr), __n = 0; __n < __cnt; __n++) \
1101 if ((__srcinst = __ssa_src_n(__instr, __n)))
1103 /* iterator for an instruction's SSA sources (instr): */
1104 #define foreach_ssa_src(__srcinst, __instr) \
1105 foreach_ssa_src_n(__srcinst, __i, __instr)
1107 /* iterators for shader inputs: */
1108 #define foreach_input_n(__ininstr, __cnt, __ir) \
1109 for (unsigned __cnt = 0; __cnt < (__ir)->inputs_count; __cnt++) \
1110 if ((__ininstr = (__ir)->inputs[__cnt]))
1111 #define foreach_input(__ininstr, __ir) \
1112 foreach_input_n(__ininstr, __i, __ir)
1114 /* iterators for shader outputs: */
1115 #define foreach_output_n(__outinstr, __cnt, __ir) \
1116 for (unsigned __cnt = 0; __cnt < (__ir)->outputs_count; __cnt++) \
1117 if ((__outinstr = (__ir)->outputs[__cnt]))
1118 #define foreach_output(__outinstr, __ir) \
1119 foreach_output_n(__outinstr, __i, __ir)
1121 /* iterators for instructions: */
1122 #define foreach_instr(__instr, __list) \
1123 list_for_each_entry(struct ir3_instruction, __instr, __list, node)
1124 #define foreach_instr_rev(__instr, __list) \
1125 list_for_each_entry_rev(struct ir3_instruction, __instr, __list, node)
1126 #define foreach_instr_safe(__instr, __list) \
1127 list_for_each_entry_safe(struct ir3_instruction, __instr, __list, node)
1129 /* iterators for blocks: */
1130 #define foreach_block(__block, __list) \
1131 list_for_each_entry(struct ir3_block, __block, __list, node)
1132 #define foreach_block_safe(__block, __list) \
1133 list_for_each_entry_safe(struct ir3_block, __block, __list, node)
1135 /* iterators for arrays: */
1136 #define foreach_array(__array, __list) \
1137 list_for_each_entry(struct ir3_array, __array, __list, node)
1140 void ir3_print(struct ir3
*ir
);
1141 void ir3_print_instr(struct ir3_instruction
*instr
);
1143 /* delay calculation: */
1144 int ir3_delayslots(struct ir3_instruction
*assigner
,
1145 struct ir3_instruction
*consumer
, unsigned n
);
1146 unsigned ir3_delay_calc(struct ir3_block
*block
, struct ir3_instruction
*instr
,
1147 bool soft
, bool pred
);
1148 void ir3_remove_nops(struct ir3
*ir
);
1150 /* depth calculation: */
1151 struct ir3_shader_variant
;
1152 void ir3_insert_by_depth(struct ir3_instruction
*instr
, struct list_head
*list
);
1153 void ir3_depth(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1155 /* fp16 conversion folding */
1156 void ir3_cf(struct ir3
*ir
);
1158 /* copy-propagate: */
1159 void ir3_cp(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1161 /* group neighbors and insert mov's to resolve conflicts: */
1162 void ir3_group(struct ir3
*ir
);
1164 /* Sethi–Ullman numbering: */
1165 void ir3_sun(struct ir3
*ir
);
1168 void ir3_sched_add_deps(struct ir3
*ir
);
1169 int ir3_sched(struct ir3
*ir
);
1172 int ir3_postsched(struct ir3_context
*ctx
);
1174 bool ir3_a6xx_fixup_atomic_dests(struct ir3
*ir
, struct ir3_shader_variant
*so
);
1176 /* register assignment: */
1177 struct ir3_ra_reg_set
* ir3_ra_alloc_reg_set(struct ir3_compiler
*compiler
);
1178 int ir3_ra(struct ir3_shader_variant
*v
, struct ir3_instruction
**precolor
, unsigned nprecolor
);
1181 void ir3_legalize(struct ir3
*ir
, struct ir3_shader_variant
*so
, int *max_bary
);
1183 /* ************************************************************************* */
1184 /* instruction helpers */
1186 /* creates SSA src of correct type (ie. half vs full precision) */
1187 static inline struct ir3_register
* __ssa_src(struct ir3_instruction
*instr
,
1188 struct ir3_instruction
*src
, unsigned flags
)
1190 struct ir3_register
*reg
;
1191 if (src
->regs
[0]->flags
& IR3_REG_HALF
)
1192 flags
|= IR3_REG_HALF
;
1193 reg
= ir3_reg_create(instr
, 0, IR3_REG_SSA
| flags
);
1195 reg
->wrmask
= src
->regs
[0]->wrmask
;
1199 static inline struct ir3_register
* __ssa_dst(struct ir3_instruction
*instr
)
1201 struct ir3_register
*reg
= ir3_reg_create(instr
, 0, 0);
1202 reg
->flags
|= IR3_REG_SSA
;
1206 static inline struct ir3_instruction
*
1207 create_immed_typed(struct ir3_block
*block
, uint32_t val
, type_t type
)
1209 struct ir3_instruction
*mov
;
1210 unsigned flags
= (type_size(type
) < 32) ? IR3_REG_HALF
: 0;
1212 mov
= ir3_instr_create(block
, OPC_MOV
);
1213 mov
->cat1
.src_type
= type
;
1214 mov
->cat1
.dst_type
= type
;
1215 __ssa_dst(mov
)->flags
|= flags
;
1216 ir3_reg_create(mov
, 0, IR3_REG_IMMED
| flags
)->uim_val
= val
;
1221 static inline struct ir3_instruction
*
1222 create_immed(struct ir3_block
*block
, uint32_t val
)
1224 return create_immed_typed(block
, val
, TYPE_U32
);
1227 static inline struct ir3_instruction
*
1228 create_uniform_typed(struct ir3_block
*block
, unsigned n
, type_t type
)
1230 struct ir3_instruction
*mov
;
1231 unsigned flags
= (type_size(type
) < 32) ? IR3_REG_HALF
: 0;
1233 mov
= ir3_instr_create(block
, OPC_MOV
);
1234 mov
->cat1
.src_type
= type
;
1235 mov
->cat1
.dst_type
= type
;
1236 __ssa_dst(mov
)->flags
|= flags
;
1237 ir3_reg_create(mov
, n
, IR3_REG_CONST
| flags
);
1242 static inline struct ir3_instruction
*
1243 create_uniform(struct ir3_block
*block
, unsigned n
)
1245 return create_uniform_typed(block
, n
, TYPE_F32
);
1248 static inline struct ir3_instruction
*
1249 create_uniform_indirect(struct ir3_block
*block
, int n
,
1250 struct ir3_instruction
*address
)
1252 struct ir3_instruction
*mov
;
1254 mov
= ir3_instr_create(block
, OPC_MOV
);
1255 mov
->cat1
.src_type
= TYPE_U32
;
1256 mov
->cat1
.dst_type
= TYPE_U32
;
1258 ir3_reg_create(mov
, 0, IR3_REG_CONST
| IR3_REG_RELATIV
)->array
.offset
= n
;
1260 ir3_instr_set_address(mov
, address
);
1265 static inline struct ir3_instruction
*
1266 ir3_MOV(struct ir3_block
*block
, struct ir3_instruction
*src
, type_t type
)
1268 struct ir3_instruction
*instr
= ir3_instr_create(block
, OPC_MOV
);
1270 if (src
->regs
[0]->flags
& IR3_REG_ARRAY
) {
1271 struct ir3_register
*src_reg
= __ssa_src(instr
, src
, IR3_REG_ARRAY
);
1272 src_reg
->array
= src
->regs
[0]->array
;
1274 __ssa_src(instr
, src
, src
->regs
[0]->flags
& IR3_REG_HIGH
);
1276 debug_assert(!(src
->regs
[0]->flags
& IR3_REG_RELATIV
));
1277 instr
->cat1
.src_type
= type
;
1278 instr
->cat1
.dst_type
= type
;
1282 static inline struct ir3_instruction
*
1283 ir3_COV(struct ir3_block
*block
, struct ir3_instruction
*src
,
1284 type_t src_type
, type_t dst_type
)
1286 struct ir3_instruction
*instr
= ir3_instr_create(block
, OPC_MOV
);
1287 unsigned dst_flags
= (type_size(dst_type
) < 32) ? IR3_REG_HALF
: 0;
1288 unsigned src_flags
= (type_size(src_type
) < 32) ? IR3_REG_HALF
: 0;
1290 debug_assert((src
->regs
[0]->flags
& IR3_REG_HALF
) == src_flags
);
1292 __ssa_dst(instr
)->flags
|= dst_flags
;
1293 __ssa_src(instr
, src
, 0);
1294 instr
->cat1
.src_type
= src_type
;
1295 instr
->cat1
.dst_type
= dst_type
;
1296 debug_assert(!(src
->regs
[0]->flags
& IR3_REG_ARRAY
));
1300 static inline struct ir3_instruction
*
1301 ir3_NOP(struct ir3_block
*block
)
1303 return ir3_instr_create(block
, OPC_NOP
);
1306 #define IR3_INSTR_0 0
1308 #define __INSTR0(flag, name, opc) \
1309 static inline struct ir3_instruction * \
1310 ir3_##name(struct ir3_block *block) \
1312 struct ir3_instruction *instr = \
1313 ir3_instr_create(block, opc); \
1314 instr->flags |= flag; \
1317 #define INSTR0F(f, name) __INSTR0(IR3_INSTR_##f, name##_##f, OPC_##name)
1318 #define INSTR0(name) __INSTR0(0, name, OPC_##name)
1320 #define __INSTR1(flag, name, opc) \
1321 static inline struct ir3_instruction * \
1322 ir3_##name(struct ir3_block *block, \
1323 struct ir3_instruction *a, unsigned aflags) \
1325 struct ir3_instruction *instr = \
1326 ir3_instr_create(block, opc); \
1328 __ssa_src(instr, a, aflags); \
1329 instr->flags |= flag; \
1332 #define INSTR1F(f, name) __INSTR1(IR3_INSTR_##f, name##_##f, OPC_##name)
1333 #define INSTR1(name) __INSTR1(0, name, OPC_##name)
1335 #define __INSTR2(flag, name, opc) \
1336 static inline struct ir3_instruction * \
1337 ir3_##name(struct ir3_block *block, \
1338 struct ir3_instruction *a, unsigned aflags, \
1339 struct ir3_instruction *b, unsigned bflags) \
1341 struct ir3_instruction *instr = \
1342 ir3_instr_create(block, opc); \
1344 __ssa_src(instr, a, aflags); \
1345 __ssa_src(instr, b, bflags); \
1346 instr->flags |= flag; \
1349 #define INSTR2F(f, name) __INSTR2(IR3_INSTR_##f, name##_##f, OPC_##name)
1350 #define INSTR2(name) __INSTR2(0, name, OPC_##name)
1352 #define __INSTR3(flag, name, opc) \
1353 static inline struct ir3_instruction * \
1354 ir3_##name(struct ir3_block *block, \
1355 struct ir3_instruction *a, unsigned aflags, \
1356 struct ir3_instruction *b, unsigned bflags, \
1357 struct ir3_instruction *c, unsigned cflags) \
1359 struct ir3_instruction *instr = \
1360 ir3_instr_create2(block, opc, 4); \
1362 __ssa_src(instr, a, aflags); \
1363 __ssa_src(instr, b, bflags); \
1364 __ssa_src(instr, c, cflags); \
1365 instr->flags |= flag; \
1368 #define INSTR3F(f, name) __INSTR3(IR3_INSTR_##f, name##_##f, OPC_##name)
1369 #define INSTR3(name) __INSTR3(0, name, OPC_##name)
1371 #define __INSTR4(flag, name, opc) \
1372 static inline struct ir3_instruction * \
1373 ir3_##name(struct ir3_block *block, \
1374 struct ir3_instruction *a, unsigned aflags, \
1375 struct ir3_instruction *b, unsigned bflags, \
1376 struct ir3_instruction *c, unsigned cflags, \
1377 struct ir3_instruction *d, unsigned dflags) \
1379 struct ir3_instruction *instr = \
1380 ir3_instr_create2(block, opc, 5); \
1382 __ssa_src(instr, a, aflags); \
1383 __ssa_src(instr, b, bflags); \
1384 __ssa_src(instr, c, cflags); \
1385 __ssa_src(instr, d, dflags); \
1386 instr->flags |= flag; \
1389 #define INSTR4F(f, name) __INSTR4(IR3_INSTR_##f, name##_##f, OPC_##name)
1390 #define INSTR4(name) __INSTR4(0, name, OPC_##name)
1392 /* cat0 instructions: */
1403 /* cat2 instructions, most 2 src but some 1 src: */
1451 /* cat3 instructions: */
1469 /* cat4 instructions: */
1481 /* cat5 instructions: */
1490 static inline struct ir3_instruction
*
1491 ir3_SAM(struct ir3_block
*block
, opc_t opc
, type_t type
,
1492 unsigned wrmask
, unsigned flags
, struct ir3_instruction
*samp_tex
,
1493 struct ir3_instruction
*src0
, struct ir3_instruction
*src1
)
1495 struct ir3_instruction
*sam
;
1497 sam
= ir3_instr_create(block
, opc
);
1498 sam
->flags
|= flags
| IR3_INSTR_S2EN
;
1499 __ssa_dst(sam
)->wrmask
= wrmask
;
1500 __ssa_src(sam
, samp_tex
, IR3_REG_HALF
);
1502 __ssa_src(sam
, src0
, 0)->wrmask
= (1 << (src0
->regs_count
- 1)) - 1;
1505 __ssa_src(sam
, src1
, 0)->wrmask
=(1 << (src1
->regs_count
- 1)) - 1;
1507 sam
->cat5
.type
= type
;
1512 /* cat6 instructions: */
1527 INSTR2(ATOMIC_CMPXCHG
)
1536 INSTR3F(G
, ATOMIC_ADD
)
1537 INSTR3F(G
, ATOMIC_SUB
)
1538 INSTR3F(G
, ATOMIC_XCHG
)
1539 INSTR3F(G
, ATOMIC_INC
)
1540 INSTR3F(G
, ATOMIC_DEC
)
1541 INSTR3F(G
, ATOMIC_CMPXCHG
)
1542 INSTR3F(G
, ATOMIC_MIN
)
1543 INSTR3F(G
, ATOMIC_MAX
)
1544 INSTR3F(G
, ATOMIC_AND
)
1545 INSTR3F(G
, ATOMIC_OR
)
1546 INSTR3F(G
, ATOMIC_XOR
)
1551 INSTR4F(G
, ATOMIC_ADD
)
1552 INSTR4F(G
, ATOMIC_SUB
)
1553 INSTR4F(G
, ATOMIC_XCHG
)
1554 INSTR4F(G
, ATOMIC_INC
)
1555 INSTR4F(G
, ATOMIC_DEC
)
1556 INSTR4F(G
, ATOMIC_CMPXCHG
)
1557 INSTR4F(G
, ATOMIC_MIN
)
1558 INSTR4F(G
, ATOMIC_MAX
)
1559 INSTR4F(G
, ATOMIC_AND
)
1560 INSTR4F(G
, ATOMIC_OR
)
1561 INSTR4F(G
, ATOMIC_XOR
)
1566 /* cat7 instructions: */
1570 /* meta instructions: */
1571 INSTR0(META_TEX_PREFETCH
);
1573 /* ************************************************************************* */
1574 /* split this out or find some helper to use.. like main/bitset.h.. */
1580 typedef uint8_t regmask_t
[2 * MAX_REG
/ 8];
1582 static inline unsigned regmask_idx(struct ir3_register
*reg
)
1584 unsigned num
= (reg
->flags
& IR3_REG_RELATIV
) ? reg
->array
.offset
: reg
->num
;
1585 debug_assert(num
< MAX_REG
);
1586 if (reg
->flags
& IR3_REG_HALF
) {
1596 static inline void regmask_init(regmask_t
*regmask
)
1598 memset(regmask
, 0, sizeof(*regmask
));
1601 static inline void regmask_set(regmask_t
*regmask
, struct ir3_register
*reg
)
1603 unsigned idx
= regmask_idx(reg
);
1604 if (reg
->flags
& IR3_REG_RELATIV
) {
1606 for (i
= 0; i
< reg
->size
; i
++, idx
++)
1607 (*regmask
)[idx
/ 8] |= 1 << (idx
% 8);
1610 for (mask
= reg
->wrmask
; mask
; mask
>>= 1, idx
++)
1612 (*regmask
)[idx
/ 8] |= 1 << (idx
% 8);
1616 static inline void regmask_or(regmask_t
*dst
, regmask_t
*a
, regmask_t
*b
)
1619 for (i
= 0; i
< ARRAY_SIZE(*dst
); i
++)
1620 (*dst
)[i
] = (*a
)[i
] | (*b
)[i
];
1623 /* set bits in a if not set in b, conceptually:
1626 static inline void regmask_set_if_not(regmask_t
*a
,
1627 struct ir3_register
*reg
, regmask_t
*b
)
1629 unsigned idx
= regmask_idx(reg
);
1630 if (reg
->flags
& IR3_REG_RELATIV
) {
1632 for (i
= 0; i
< reg
->size
; i
++, idx
++)
1633 if (!((*b
)[idx
/ 8] & (1 << (idx
% 8))))
1634 (*a
)[idx
/ 8] |= 1 << (idx
% 8);
1637 for (mask
= reg
->wrmask
; mask
; mask
>>= 1, idx
++)
1639 if (!((*b
)[idx
/ 8] & (1 << (idx
% 8))))
1640 (*a
)[idx
/ 8] |= 1 << (idx
% 8);
1644 static inline bool regmask_get(regmask_t
*regmask
,
1645 struct ir3_register
*reg
)
1647 unsigned idx
= regmask_idx(reg
);
1648 if (reg
->flags
& IR3_REG_RELATIV
) {
1650 for (i
= 0; i
< reg
->size
; i
++, idx
++)
1651 if ((*regmask
)[idx
/ 8] & (1 << (idx
% 8)))
1655 for (mask
= reg
->wrmask
; mask
; mask
>>= 1, idx
++)
1657 if ((*regmask
)[idx
/ 8] & (1 << (idx
% 8)))
1663 /* ************************************************************************* */