freedreno/ir3: new pre-RA scheduler
[mesa.git] / src / 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 "compiler/shader_enums.h"
31
32 #include "util/bitscan.h"
33 #include "util/list.h"
34 #include "util/set.h"
35 #include "util/u_debug.h"
36
37 #include "instr-a3xx.h"
38
39 /* low level intermediate representation of an adreno shader program */
40
41 struct ir3_compiler;
42 struct ir3;
43 struct ir3_instruction;
44 struct ir3_block;
45
46 struct ir3_info {
47 uint32_t gpu_id;
48 uint16_t sizedwords;
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
53 * touched by shader)
54 */
55 int8_t max_reg; /* highest GPR # used by shader */
56 int8_t max_half_reg;
57 int16_t max_const;
58
59 /* number of sync bits: */
60 uint16_t ss, sy;
61
62 /* estimate of number of cycles stalled on (ss) */
63 uint16_t sstall;
64
65 uint16_t last_baryf; /* instruction # of last varying fetch */
66 };
67
68 struct ir3_register {
69 enum {
70 IR3_REG_CONST = 0x001,
71 IR3_REG_IMMED = 0x002,
72 IR3_REG_HALF = 0x004,
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?
77 */
78 IR3_REG_HIGH = 0x008,
79 IR3_REG_RELATIV= 0x010,
80 IR3_REG_R = 0x020,
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
87 * more clear.
88 */
89 IR3_REG_FNEG = 0x040,
90 IR3_REG_FABS = 0x080,
91 IR3_REG_SNEG = 0x100,
92 IR3_REG_SABS = 0x200,
93 IR3_REG_BNOT = 0x400,
94 IR3_REG_EVEN = 0x800,
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:
98 */
99 IR3_REG_EI = 0x2000,
100 /* meta-flags, for intermediate stages of IR, ie.
101 * before register assignment is done:
102 */
103 IR3_REG_SSA = 0x4000, /* 'instr' is ptr to assigning instr */
104 IR3_REG_ARRAY = 0x8000,
105
106 } flags;
107
108 /* used for cat5 instructions, but also for internal/IR level
109 * tracking of what registers are read/written by an instruction.
110 * wrmask may be a bad name since it is used to represent both
111 * src and dst that touch multiple adjacent registers.
112 */
113 unsigned wrmask : 16; /* up to vec16 */
114
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).
118 *
119 * Note the size field isn't important for relative const (since
120 * we don't have to do register allocation for constants).
121 */
122 unsigned size : 15;
123
124 bool merged : 1; /* half-regs conflict with full regs (ie >= a6xx) */
125
126 /* normal registers:
127 * the component is in the low two bits of the reg #, so
128 * rN.x becomes: (N << 2) | x
129 */
130 uint16_t num;
131 union {
132 /* immediate: */
133 int32_t iim_val;
134 uint32_t uim_val;
135 float fim_val;
136 /* relative: */
137 struct {
138 uint16_t id;
139 int16_t offset;
140 } array;
141 };
142
143 /* For IR3_REG_SSA, src registers contain ptr back to assigning
144 * instruction.
145 *
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).
149 */
150 struct ir3_instruction *instr;
151 };
152
153 /*
154 * Stupid/simple growable array implementation:
155 */
156 #define DECLARE_ARRAY(type, name) \
157 unsigned name ## _count, name ## _sz; \
158 type * name;
159
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])); \
164 } \
165 arr[arr ##_count++] = val; \
166 } while (0)
167
168 struct ir3_instruction {
169 struct ir3_block *block;
170 opc_t opc;
171 enum {
172 /* (sy) flag is set on first instruction, and after sample
173 * instructions (probably just on RAW hazard).
174 */
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):
178 *
179 * rcp, rsq, log2, exp2, sin, cos, sqrt
180 *
181 * It seems to synchronize until all in-flight instructions are
182 * completed, for example:
183 *
184 * rsq hr1.w, hr1.w
185 * add.f hr2.z, (neg)hr2.z, hc0.y
186 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
187 * rsq hr2.x, hr2.x
188 * (rpt1)nop
189 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
190 * nop
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
194 *
195 * The last mul.f does not have (ss) set, presumably because the
196 * (ss) on the previous instruction does the job.
197 *
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
202 * lucky:
203 *
204 * rcp r1.y, r3.y
205 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
206 *
207 */
208 IR3_INSTR_SS = 0x002,
209 /* (jp) flag is set on jump targets:
210 */
211 IR3_INSTR_JP = 0x004,
212 IR3_INSTR_UL = 0x008,
213 IR3_INSTR_3D = 0x010,
214 IR3_INSTR_A = 0x020,
215 IR3_INSTR_O = 0x040,
216 IR3_INSTR_P = 0x080,
217 IR3_INSTR_S = 0x100,
218 IR3_INSTR_S2EN = 0x200,
219 IR3_INSTR_G = 0x400,
220 IR3_INSTR_SAT = 0x800,
221 /* (cat5/cat6) Bindless */
222 IR3_INSTR_B = 0x1000,
223 /* (cat5-only) Get some parts of the encoding from a1.x */
224 IR3_INSTR_A1EN = 0x2000,
225 /* meta-flags, for intermediate stages of IR, ie.
226 * before register assignment is done:
227 */
228 IR3_INSTR_MARK = 0x4000,
229 IR3_INSTR_UNUSED= 0x8000,
230 } flags;
231 uint8_t repeat;
232 uint8_t nop;
233 #ifdef DEBUG
234 unsigned regs_max;
235 #endif
236 unsigned regs_count;
237 struct ir3_register **regs;
238 union {
239 struct {
240 char inv;
241 char comp;
242 int immed;
243 struct ir3_block *target;
244 } cat0;
245 struct {
246 type_t src_type, dst_type;
247 } cat1;
248 struct {
249 enum {
250 IR3_COND_LT = 0,
251 IR3_COND_LE = 1,
252 IR3_COND_GT = 2,
253 IR3_COND_GE = 3,
254 IR3_COND_EQ = 4,
255 IR3_COND_NE = 5,
256 } condition;
257 } cat2;
258 struct {
259 unsigned samp, tex;
260 unsigned tex_base : 3;
261 type_t type;
262 } cat5;
263 struct {
264 type_t type;
265 int src_offset;
266 int dst_offset;
267 int iim_val : 3; /* for ldgb/stgb, # of components */
268 unsigned d : 3;
269 bool typed : 1;
270 unsigned base : 3;
271 } cat6;
272 struct {
273 unsigned w : 1; /* write */
274 unsigned r : 1; /* read */
275 unsigned l : 1; /* local */
276 unsigned g : 1; /* global */
277 } cat7;
278 /* for meta-instructions, just used to hold extra data
279 * before instruction scheduling, etc
280 */
281 struct {
282 int off; /* component/offset */
283 } split;
284 struct {
285 /* for output collects, this maps back to the entry in the
286 * ir3_shader_variant::outputs table.
287 */
288 int outidx;
289 } collect;
290 struct {
291 unsigned samp, tex;
292 unsigned input_offset;
293 unsigned samp_base : 3;
294 unsigned tex_base : 3;
295 } prefetch;
296 struct {
297 /* maps back to entry in ir3_shader_variant::inputs table: */
298 int inidx;
299 /* for sysvals, identifies the sysval type. Mostly so we can
300 * identify the special cases where a sysval should not be DCE'd
301 * (currently, just pre-fs texture fetch)
302 */
303 gl_system_value sysval;
304 } input;
305 };
306
307 /* transient values used during various algorithms: */
308 union {
309 /* The instruction depth is the max dependency distance to output.
310 *
311 * You can also think of it as the "cost", if we did any sort of
312 * optimization for register footprint. Ie. a value that is just
313 * result of moving a const to a reg would have a low cost, so to
314 * it could make sense to duplicate the instruction at various
315 * points where the result is needed to reduce register footprint.
316 */
317 int depth;
318 /* When we get to the RA stage, we no longer need depth, but
319 * we do need instruction's position/name:
320 */
321 struct {
322 uint16_t ip;
323 uint16_t name;
324 };
325 };
326
327 /* used for per-pass extra instruction data.
328 *
329 * TODO we should remove the per-pass data like this and 'use_count'
330 * and do something similar to what RA does w/ ir3_ra_instr_data..
331 * ie. use the ir3_count_instructions pass, and then use instr->ip
332 * to index into a table of pass-private data.
333 */
334 void *data;
335
336 /**
337 * Valid if pass calls ir3_find_ssa_uses().. see foreach_ssa_use()
338 */
339 struct set *uses;
340
341 int sun; /* Sethi–Ullman number, used by sched */
342 int use_count; /* currently just updated/used by cp */
343
344 /* Used during CP and RA stages. For collect and shader inputs/
345 * outputs where we need a sequence of consecutive registers,
346 * keep track of each src instructions left (ie 'n-1') and right
347 * (ie 'n+1') neighbor. The front-end must insert enough mov's
348 * to ensure that each instruction has at most one left and at
349 * most one right neighbor. During the copy-propagation pass,
350 * we only remove mov's when we can preserve this constraint.
351 * And during the RA stage, we use the neighbor information to
352 * allocate a block of registers in one shot.
353 *
354 * TODO: maybe just add something like:
355 * struct ir3_instruction_ref {
356 * struct ir3_instruction *instr;
357 * unsigned cnt;
358 * }
359 *
360 * Or can we get away without the refcnt stuff? It seems like
361 * it should be overkill.. the problem is if, potentially after
362 * already eliminating some mov's, if you have a single mov that
363 * needs to be grouped with it's neighbors in two different
364 * places (ex. shader output and a collect).
365 */
366 struct {
367 struct ir3_instruction *left, *right;
368 uint16_t left_cnt, right_cnt;
369 } cp;
370
371 /* an instruction can reference at most one address register amongst
372 * it's src/dst registers. Beyond that, you need to insert mov's.
373 *
374 * NOTE: do not write this directly, use ir3_instr_set_address()
375 */
376 struct ir3_instruction *address;
377
378 /* Tracking for additional dependent instructions. Used to handle
379 * barriers, WAR hazards for arrays/SSBOs/etc.
380 */
381 DECLARE_ARRAY(struct ir3_instruction *, deps);
382
383 /*
384 * From PoV of instruction scheduling, not execution (ie. ignores global/
385 * local distinction):
386 * shared image atomic SSBO everything
387 * barrier()/ - R/W R/W R/W R/W X
388 * groupMemoryBarrier()
389 * memoryBarrier() - R/W R/W
390 * (but only images declared coherent?)
391 * memoryBarrierAtomic() - R/W
392 * memoryBarrierBuffer() - R/W
393 * memoryBarrierImage() - R/W
394 * memoryBarrierShared() - R/W
395 *
396 * TODO I think for SSBO/image/shared, in cases where we can determine
397 * which variable is accessed, we don't need to care about accesses to
398 * different variables (unless declared coherent??)
399 */
400 enum {
401 IR3_BARRIER_EVERYTHING = 1 << 0,
402 IR3_BARRIER_SHARED_R = 1 << 1,
403 IR3_BARRIER_SHARED_W = 1 << 2,
404 IR3_BARRIER_IMAGE_R = 1 << 3,
405 IR3_BARRIER_IMAGE_W = 1 << 4,
406 IR3_BARRIER_BUFFER_R = 1 << 5,
407 IR3_BARRIER_BUFFER_W = 1 << 6,
408 IR3_BARRIER_ARRAY_R = 1 << 7,
409 IR3_BARRIER_ARRAY_W = 1 << 8,
410 } barrier_class, barrier_conflict;
411
412 /* Entry in ir3_block's instruction list: */
413 struct list_head node;
414
415 #ifdef DEBUG
416 uint32_t serialno;
417 #endif
418
419 // TODO only computerator/assembler:
420 int line;
421 };
422
423 static inline struct ir3_instruction *
424 ir3_neighbor_first(struct ir3_instruction *instr)
425 {
426 int cnt = 0;
427 while (instr->cp.left) {
428 instr = instr->cp.left;
429 if (++cnt > 0xffff) {
430 debug_assert(0);
431 break;
432 }
433 }
434 return instr;
435 }
436
437 static inline int ir3_neighbor_count(struct ir3_instruction *instr)
438 {
439 int num = 1;
440
441 debug_assert(!instr->cp.left);
442
443 while (instr->cp.right) {
444 num++;
445 instr = instr->cp.right;
446 if (num > 0xffff) {
447 debug_assert(0);
448 break;
449 }
450 }
451
452 return num;
453 }
454
455 struct ir3 {
456 struct ir3_compiler *compiler;
457 gl_shader_stage type;
458
459 DECLARE_ARRAY(struct ir3_instruction *, inputs);
460 DECLARE_ARRAY(struct ir3_instruction *, outputs);
461
462 /* Track bary.f (and ldlv) instructions.. this is needed in
463 * scheduling to ensure that all varying fetches happen before
464 * any potential kill instructions. The hw gets grumpy if all
465 * threads in a group are killed before the last bary.f gets
466 * a chance to signal end of input (ei).
467 */
468 DECLARE_ARRAY(struct ir3_instruction *, baryfs);
469
470 /* Track all indirect instructions (read and write). To avoid
471 * deadlock scenario where an address register gets scheduled,
472 * but other dependent src instructions cannot be scheduled due
473 * to dependency on a *different* address register value, the
474 * scheduler needs to ensure that all dependencies other than
475 * the instruction other than the address register are scheduled
476 * before the one that writes the address register. Having a
477 * convenient list of instructions that reference some address
478 * register simplifies this.
479 */
480 DECLARE_ARRAY(struct ir3_instruction *, a0_users);
481
482 /* same for a1.x: */
483 DECLARE_ARRAY(struct ir3_instruction *, a1_users);
484
485 /* and same for instructions that consume predicate register: */
486 DECLARE_ARRAY(struct ir3_instruction *, predicates);
487
488 /* Track texture sample instructions which need texture state
489 * patched in (for astc-srgb workaround):
490 */
491 DECLARE_ARRAY(struct ir3_instruction *, astc_srgb);
492
493 /* List of blocks: */
494 struct list_head block_list;
495
496 /* List of ir3_array's: */
497 struct list_head array_list;
498
499 unsigned max_sun; /* max Sethi–Ullman number */
500
501 #ifdef DEBUG
502 unsigned block_count, instr_count;
503 #endif
504 };
505
506 struct ir3_array {
507 struct list_head node;
508 unsigned length;
509 unsigned id;
510
511 struct nir_register *r;
512
513 /* To avoid array write's from getting DCE'd, keep track of the
514 * most recent write. Any array access depends on the most
515 * recent write. This way, nothing depends on writes after the
516 * last read. But all the writes that happen before that have
517 * something depending on them
518 */
519 struct ir3_instruction *last_write;
520
521 /* extra stuff used in RA pass: */
522 unsigned base; /* base vreg name */
523 unsigned reg; /* base physical reg */
524 uint16_t start_ip, end_ip;
525
526 /* Indicates if half-precision */
527 bool half;
528 };
529
530 struct ir3_array * ir3_lookup_array(struct ir3 *ir, unsigned id);
531
532 struct ir3_block {
533 struct list_head node;
534 struct ir3 *shader;
535
536 const struct nir_block *nblock;
537
538 struct list_head instr_list; /* list of ir3_instruction */
539
540 /* each block has either one or two successors.. in case of
541 * two successors, 'condition' decides which one to follow.
542 * A block preceding an if/else has two successors.
543 */
544 struct ir3_instruction *condition;
545 struct ir3_block *successors[2];
546
547 struct set *predecessors; /* set of ir3_block */
548
549 uint16_t start_ip, end_ip;
550
551 /* Track instructions which do not write a register but other-
552 * wise must not be discarded (such as kill, stg, etc)
553 */
554 DECLARE_ARRAY(struct ir3_instruction *, keeps);
555
556 /* used for per-pass extra block data. Mainly used right
557 * now in RA step to track livein/liveout.
558 */
559 void *data;
560
561 #ifdef DEBUG
562 uint32_t serialno;
563 #endif
564 };
565
566 static inline uint32_t
567 block_id(struct ir3_block *block)
568 {
569 #ifdef DEBUG
570 return block->serialno;
571 #else
572 return (uint32_t)(unsigned long)block;
573 #endif
574 }
575
576 struct ir3 * ir3_create(struct ir3_compiler *compiler, gl_shader_stage type);
577 void ir3_destroy(struct ir3 *shader);
578 void * ir3_assemble(struct ir3 *shader,
579 struct ir3_info *info, uint32_t gpu_id);
580 void * ir3_alloc(struct ir3 *shader, int sz);
581
582 struct ir3_block * ir3_block_create(struct ir3 *shader);
583
584 struct ir3_instruction * ir3_instr_create(struct ir3_block *block, opc_t opc);
585 struct ir3_instruction * ir3_instr_create2(struct ir3_block *block,
586 opc_t opc, int nreg);
587 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr);
588 void ir3_instr_add_dep(struct ir3_instruction *instr, struct ir3_instruction *dep);
589 const char *ir3_instr_name(struct ir3_instruction *instr);
590
591 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
592 int num, int flags);
593 struct ir3_register * ir3_reg_clone(struct ir3 *shader,
594 struct ir3_register *reg);
595
596 void ir3_instr_set_address(struct ir3_instruction *instr,
597 struct ir3_instruction *addr);
598
599 static inline bool ir3_instr_check_mark(struct ir3_instruction *instr)
600 {
601 if (instr->flags & IR3_INSTR_MARK)
602 return true; /* already visited */
603 instr->flags |= IR3_INSTR_MARK;
604 return false;
605 }
606
607 void ir3_block_clear_mark(struct ir3_block *block);
608 void ir3_clear_mark(struct ir3 *shader);
609
610 unsigned ir3_count_instructions(struct ir3 *ir);
611
612 void ir3_find_ssa_uses(struct ir3 *ir, void *mem_ctx, bool falsedeps);
613
614 #include "util/set.h"
615 #define foreach_ssa_use(__use, __instr) \
616 for (struct ir3_instruction *__use = (void *)~0; \
617 __use && (__instr)->uses; __use = NULL) \
618 set_foreach ((__instr)->uses, __entry) \
619 if ((__use = (void *)__entry->key))
620
621 #define MAX_ARRAYS 16
622
623 /* comp:
624 * 0 - x
625 * 1 - y
626 * 2 - z
627 * 3 - w
628 */
629 static inline uint32_t regid(int num, int comp)
630 {
631 return (num << 2) | (comp & 0x3);
632 }
633
634 static inline uint32_t reg_num(struct ir3_register *reg)
635 {
636 return reg->num >> 2;
637 }
638
639 static inline uint32_t reg_comp(struct ir3_register *reg)
640 {
641 return reg->num & 0x3;
642 }
643
644 #define INVALID_REG regid(63, 0)
645 #define VALIDREG(r) ((r) != INVALID_REG)
646 #define CONDREG(r, val) COND(VALIDREG(r), (val))
647
648 static inline bool is_flow(struct ir3_instruction *instr)
649 {
650 return (opc_cat(instr->opc) == 0);
651 }
652
653 static inline bool is_kill(struct ir3_instruction *instr)
654 {
655 return instr->opc == OPC_KILL;
656 }
657
658 static inline bool is_nop(struct ir3_instruction *instr)
659 {
660 return instr->opc == OPC_NOP;
661 }
662
663 static inline bool is_same_type_reg(struct ir3_register *reg1,
664 struct ir3_register *reg2)
665 {
666 unsigned type_reg1 = (reg1->flags & (IR3_REG_HIGH | IR3_REG_HALF));
667 unsigned type_reg2 = (reg2->flags & (IR3_REG_HIGH | IR3_REG_HALF));
668
669 if (type_reg1 ^ type_reg2)
670 return false;
671 else
672 return true;
673 }
674
675 /* Is it a non-transformative (ie. not type changing) mov? This can
676 * also include absneg.s/absneg.f, which for the most part can be
677 * treated as a mov (single src argument).
678 */
679 static inline bool is_same_type_mov(struct ir3_instruction *instr)
680 {
681 struct ir3_register *dst;
682
683 switch (instr->opc) {
684 case OPC_MOV:
685 if (instr->cat1.src_type != instr->cat1.dst_type)
686 return false;
687 /* If the type of dest reg and src reg are different,
688 * it shouldn't be considered as same type mov
689 */
690 if (!is_same_type_reg(instr->regs[0], instr->regs[1]))
691 return false;
692 break;
693 case OPC_ABSNEG_F:
694 case OPC_ABSNEG_S:
695 if (instr->flags & IR3_INSTR_SAT)
696 return false;
697 /* If the type of dest reg and src reg are different,
698 * it shouldn't be considered as same type mov
699 */
700 if (!is_same_type_reg(instr->regs[0], instr->regs[1]))
701 return false;
702 break;
703 default:
704 return false;
705 }
706
707 dst = instr->regs[0];
708
709 /* mov's that write to a0 or p0.x are special: */
710 if (dst->num == regid(REG_P0, 0))
711 return false;
712 if (reg_num(dst) == REG_A0)
713 return false;
714
715 if (dst->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
716 return false;
717
718 return true;
719 }
720
721 /* A move from const, which changes size but not type, can also be
722 * folded into dest instruction in some cases.
723 */
724 static inline bool is_const_mov(struct ir3_instruction *instr)
725 {
726 if (instr->opc != OPC_MOV)
727 return false;
728
729 if (!(instr->regs[1]->flags & IR3_REG_CONST))
730 return false;
731
732 type_t src_type = instr->cat1.src_type;
733 type_t dst_type = instr->cat1.dst_type;
734
735 return (type_float(src_type) && type_float(dst_type)) ||
736 (type_uint(src_type) && type_uint(dst_type)) ||
737 (type_sint(src_type) && type_sint(dst_type));
738 }
739
740 static inline bool is_alu(struct ir3_instruction *instr)
741 {
742 return (1 <= opc_cat(instr->opc)) && (opc_cat(instr->opc) <= 3);
743 }
744
745 static inline bool is_sfu(struct ir3_instruction *instr)
746 {
747 return (opc_cat(instr->opc) == 4);
748 }
749
750 static inline bool is_tex(struct ir3_instruction *instr)
751 {
752 return (opc_cat(instr->opc) == 5);
753 }
754
755 static inline bool is_tex_or_prefetch(struct ir3_instruction *instr)
756 {
757 return is_tex(instr) || (instr->opc == OPC_META_TEX_PREFETCH);
758 }
759
760 static inline bool is_mem(struct ir3_instruction *instr)
761 {
762 return (opc_cat(instr->opc) == 6);
763 }
764
765 static inline bool is_barrier(struct ir3_instruction *instr)
766 {
767 return (opc_cat(instr->opc) == 7);
768 }
769
770 static inline bool
771 is_half(struct ir3_instruction *instr)
772 {
773 return !!(instr->regs[0]->flags & IR3_REG_HALF);
774 }
775
776 static inline bool
777 is_high(struct ir3_instruction *instr)
778 {
779 return !!(instr->regs[0]->flags & IR3_REG_HIGH);
780 }
781
782 static inline bool
783 is_store(struct ir3_instruction *instr)
784 {
785 /* these instructions, the "destination" register is
786 * actually a source, the address to store to.
787 */
788 switch (instr->opc) {
789 case OPC_STG:
790 case OPC_STGB:
791 case OPC_STIB:
792 case OPC_STP:
793 case OPC_STL:
794 case OPC_STLW:
795 case OPC_L2G:
796 case OPC_G2L:
797 return true;
798 default:
799 return false;
800 }
801 }
802
803 static inline bool is_load(struct ir3_instruction *instr)
804 {
805 switch (instr->opc) {
806 case OPC_LDG:
807 case OPC_LDGB:
808 case OPC_LDIB:
809 case OPC_LDL:
810 case OPC_LDP:
811 case OPC_L2G:
812 case OPC_LDLW:
813 case OPC_LDC:
814 case OPC_LDLV:
815 /* probably some others too.. */
816 return true;
817 default:
818 return false;
819 }
820 }
821
822 static inline bool is_input(struct ir3_instruction *instr)
823 {
824 /* in some cases, ldlv is used to fetch varying without
825 * interpolation.. fortunately inloc is the first src
826 * register in either case
827 */
828 switch (instr->opc) {
829 case OPC_LDLV:
830 case OPC_BARY_F:
831 return true;
832 default:
833 return false;
834 }
835 }
836
837 static inline bool is_bool(struct ir3_instruction *instr)
838 {
839 switch (instr->opc) {
840 case OPC_CMPS_F:
841 case OPC_CMPS_S:
842 case OPC_CMPS_U:
843 return true;
844 default:
845 return false;
846 }
847 }
848
849 static inline bool is_meta(struct ir3_instruction *instr)
850 {
851 return (opc_cat(instr->opc) == -1);
852 }
853
854 static inline unsigned dest_regs(struct ir3_instruction *instr)
855 {
856 if ((instr->regs_count == 0) || is_store(instr) || is_flow(instr))
857 return 0;
858
859 return util_last_bit(instr->regs[0]->wrmask);
860 }
861
862 static inline bool writes_addr0(struct ir3_instruction *instr)
863 {
864 if (instr->regs_count > 0) {
865 struct ir3_register *dst = instr->regs[0];
866 return dst->num == regid(REG_A0, 0);
867 }
868 return false;
869 }
870
871 static inline bool writes_addr1(struct ir3_instruction *instr)
872 {
873 if (instr->regs_count > 0) {
874 struct ir3_register *dst = instr->regs[0];
875 return dst->num == regid(REG_A0, 1);
876 }
877 return false;
878 }
879
880 static inline bool writes_pred(struct ir3_instruction *instr)
881 {
882 if (instr->regs_count > 0) {
883 struct ir3_register *dst = instr->regs[0];
884 return reg_num(dst) == REG_P0;
885 }
886 return false;
887 }
888
889 /* returns defining instruction for reg */
890 /* TODO better name */
891 static inline struct ir3_instruction *ssa(struct ir3_register *reg)
892 {
893 if (reg->flags & (IR3_REG_SSA | IR3_REG_ARRAY)) {
894 return reg->instr;
895 }
896 return NULL;
897 }
898
899 static inline bool conflicts(struct ir3_instruction *a,
900 struct ir3_instruction *b)
901 {
902 return (a && b) && (a != b);
903 }
904
905 static inline bool reg_gpr(struct ir3_register *r)
906 {
907 if (r->flags & (IR3_REG_CONST | IR3_REG_IMMED))
908 return false;
909 if ((reg_num(r) == REG_A0) || (reg_num(r) == REG_P0))
910 return false;
911 return true;
912 }
913
914 static inline type_t half_type(type_t type)
915 {
916 switch (type) {
917 case TYPE_F32: return TYPE_F16;
918 case TYPE_U32: return TYPE_U16;
919 case TYPE_S32: return TYPE_S16;
920 case TYPE_F16:
921 case TYPE_U16:
922 case TYPE_S16:
923 return type;
924 default:
925 assert(0);
926 return ~0;
927 }
928 }
929
930 /* some cat2 instructions (ie. those which are not float) can embed an
931 * immediate:
932 */
933 static inline bool ir3_cat2_int(opc_t opc)
934 {
935 switch (opc) {
936 case OPC_ADD_U:
937 case OPC_ADD_S:
938 case OPC_SUB_U:
939 case OPC_SUB_S:
940 case OPC_CMPS_U:
941 case OPC_CMPS_S:
942 case OPC_MIN_U:
943 case OPC_MIN_S:
944 case OPC_MAX_U:
945 case OPC_MAX_S:
946 case OPC_CMPV_U:
947 case OPC_CMPV_S:
948 case OPC_MUL_U24:
949 case OPC_MUL_S24:
950 case OPC_MULL_U:
951 case OPC_CLZ_S:
952 case OPC_ABSNEG_S:
953 case OPC_AND_B:
954 case OPC_OR_B:
955 case OPC_NOT_B:
956 case OPC_XOR_B:
957 case OPC_BFREV_B:
958 case OPC_CLZ_B:
959 case OPC_SHL_B:
960 case OPC_SHR_B:
961 case OPC_ASHR_B:
962 case OPC_MGEN_B:
963 case OPC_GETBIT_B:
964 case OPC_CBITS_B:
965 case OPC_BARY_F:
966 return true;
967
968 default:
969 return false;
970 }
971 }
972
973 static inline bool ir3_cat2_float(opc_t opc)
974 {
975 switch (opc) {
976 case OPC_ADD_F:
977 case OPC_MIN_F:
978 case OPC_MAX_F:
979 case OPC_MUL_F:
980 case OPC_SIGN_F:
981 case OPC_CMPS_F:
982 case OPC_ABSNEG_F:
983 case OPC_CMPV_F:
984 case OPC_FLOOR_F:
985 case OPC_CEIL_F:
986 case OPC_RNDNE_F:
987 case OPC_RNDAZ_F:
988 case OPC_TRUNC_F:
989 return true;
990
991 default:
992 return false;
993 }
994 }
995
996 static inline bool ir3_cat3_float(opc_t opc)
997 {
998 switch (opc) {
999 case OPC_MAD_F16:
1000 case OPC_MAD_F32:
1001 case OPC_SEL_F16:
1002 case OPC_SEL_F32:
1003 return true;
1004 default:
1005 return false;
1006 }
1007 }
1008
1009 /* map cat2 instruction to valid abs/neg flags: */
1010 static inline unsigned ir3_cat2_absneg(opc_t opc)
1011 {
1012 switch (opc) {
1013 case OPC_ADD_F:
1014 case OPC_MIN_F:
1015 case OPC_MAX_F:
1016 case OPC_MUL_F:
1017 case OPC_SIGN_F:
1018 case OPC_CMPS_F:
1019 case OPC_ABSNEG_F:
1020 case OPC_CMPV_F:
1021 case OPC_FLOOR_F:
1022 case OPC_CEIL_F:
1023 case OPC_RNDNE_F:
1024 case OPC_RNDAZ_F:
1025 case OPC_TRUNC_F:
1026 case OPC_BARY_F:
1027 return IR3_REG_FABS | IR3_REG_FNEG;
1028
1029 case OPC_ADD_U:
1030 case OPC_ADD_S:
1031 case OPC_SUB_U:
1032 case OPC_SUB_S:
1033 case OPC_CMPS_U:
1034 case OPC_CMPS_S:
1035 case OPC_MIN_U:
1036 case OPC_MIN_S:
1037 case OPC_MAX_U:
1038 case OPC_MAX_S:
1039 case OPC_CMPV_U:
1040 case OPC_CMPV_S:
1041 case OPC_MUL_U24:
1042 case OPC_MUL_S24:
1043 case OPC_MULL_U:
1044 case OPC_CLZ_S:
1045 return 0;
1046
1047 case OPC_ABSNEG_S:
1048 return IR3_REG_SABS | IR3_REG_SNEG;
1049
1050 case OPC_AND_B:
1051 case OPC_OR_B:
1052 case OPC_NOT_B:
1053 case OPC_XOR_B:
1054 case OPC_BFREV_B:
1055 case OPC_CLZ_B:
1056 case OPC_SHL_B:
1057 case OPC_SHR_B:
1058 case OPC_ASHR_B:
1059 case OPC_MGEN_B:
1060 case OPC_GETBIT_B:
1061 case OPC_CBITS_B:
1062 return IR3_REG_BNOT;
1063
1064 default:
1065 return 0;
1066 }
1067 }
1068
1069 /* map cat3 instructions to valid abs/neg flags: */
1070 static inline unsigned ir3_cat3_absneg(opc_t opc)
1071 {
1072 switch (opc) {
1073 case OPC_MAD_F16:
1074 case OPC_MAD_F32:
1075 case OPC_SEL_F16:
1076 case OPC_SEL_F32:
1077 return IR3_REG_FNEG;
1078
1079 case OPC_MAD_U16:
1080 case OPC_MADSH_U16:
1081 case OPC_MAD_S16:
1082 case OPC_MADSH_M16:
1083 case OPC_MAD_U24:
1084 case OPC_MAD_S24:
1085 case OPC_SEL_S16:
1086 case OPC_SEL_S32:
1087 case OPC_SAD_S16:
1088 case OPC_SAD_S32:
1089 /* neg *may* work on 3rd src.. */
1090
1091 case OPC_SEL_B16:
1092 case OPC_SEL_B32:
1093
1094 default:
1095 return 0;
1096 }
1097 }
1098
1099 #define MASK(n) ((1 << (n)) - 1)
1100
1101 /* iterator for an instructions's sources (reg), also returns src #: */
1102 #define foreach_src_n(__srcreg, __n, __instr) \
1103 if ((__instr)->regs_count) \
1104 for (unsigned __cnt = (__instr)->regs_count - 1, __n = 0; __n < __cnt; __n++) \
1105 if ((__srcreg = (__instr)->regs[__n + 1]))
1106
1107 /* iterator for an instructions's sources (reg): */
1108 #define foreach_src(__srcreg, __instr) \
1109 foreach_src_n(__srcreg, __i, __instr)
1110
1111 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
1112 {
1113 unsigned cnt = instr->regs_count + instr->deps_count;
1114 if (instr->address)
1115 cnt++;
1116 return cnt;
1117 }
1118
1119 static inline struct ir3_instruction * __ssa_src_n(struct ir3_instruction *instr, unsigned n)
1120 {
1121 if (n == (instr->regs_count + instr->deps_count))
1122 return instr->address;
1123 if (n >= instr->regs_count)
1124 return instr->deps[n - instr->regs_count];
1125 return ssa(instr->regs[n]);
1126 }
1127
1128 static inline bool __is_false_dep(struct ir3_instruction *instr, unsigned n)
1129 {
1130 if (n == (instr->regs_count + instr->deps_count))
1131 return false;
1132 if (n >= instr->regs_count)
1133 return true;
1134 return false;
1135 }
1136
1137 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
1138
1139 /* iterator for an instruction's SSA sources (instr), also returns src #: */
1140 #define foreach_ssa_src_n(__srcinst, __n, __instr) \
1141 for (unsigned __cnt = __ssa_src_cnt(__instr), __n = 0; __n < __cnt; __n++) \
1142 if ((__srcinst = __ssa_src_n(__instr, __n)))
1143
1144 /* iterator for an instruction's SSA sources (instr): */
1145 #define foreach_ssa_src(__srcinst, __instr) \
1146 foreach_ssa_src_n(__srcinst, __i, __instr)
1147
1148 /* iterators for shader inputs: */
1149 #define foreach_input_n(__ininstr, __cnt, __ir) \
1150 for (unsigned __cnt = 0; __cnt < (__ir)->inputs_count; __cnt++) \
1151 if ((__ininstr = (__ir)->inputs[__cnt]))
1152 #define foreach_input(__ininstr, __ir) \
1153 foreach_input_n(__ininstr, __i, __ir)
1154
1155 /* iterators for shader outputs: */
1156 #define foreach_output_n(__outinstr, __cnt, __ir) \
1157 for (unsigned __cnt = 0; __cnt < (__ir)->outputs_count; __cnt++) \
1158 if ((__outinstr = (__ir)->outputs[__cnt]))
1159 #define foreach_output(__outinstr, __ir) \
1160 foreach_output_n(__outinstr, __i, __ir)
1161
1162 /* iterators for instructions: */
1163 #define foreach_instr(__instr, __list) \
1164 list_for_each_entry(struct ir3_instruction, __instr, __list, node)
1165 #define foreach_instr_rev(__instr, __list) \
1166 list_for_each_entry_rev(struct ir3_instruction, __instr, __list, node)
1167 #define foreach_instr_safe(__instr, __list) \
1168 list_for_each_entry_safe(struct ir3_instruction, __instr, __list, node)
1169
1170 /* iterators for blocks: */
1171 #define foreach_block(__block, __list) \
1172 list_for_each_entry(struct ir3_block, __block, __list, node)
1173 #define foreach_block_safe(__block, __list) \
1174 list_for_each_entry_safe(struct ir3_block, __block, __list, node)
1175
1176 /* iterators for arrays: */
1177 #define foreach_array(__array, __list) \
1178 list_for_each_entry(struct ir3_array, __array, __list, node)
1179
1180 /* dump: */
1181 void ir3_print(struct ir3 *ir);
1182 void ir3_print_instr(struct ir3_instruction *instr);
1183
1184 /* delay calculation: */
1185 int ir3_delayslots(struct ir3_instruction *assigner,
1186 struct ir3_instruction *consumer, unsigned n, bool soft);
1187 unsigned ir3_delay_calc(struct ir3_block *block, struct ir3_instruction *instr,
1188 bool soft, bool pred);
1189 void ir3_remove_nops(struct ir3 *ir);
1190
1191 /* depth calculation: */
1192 struct ir3_shader_variant;
1193 void ir3_depth(struct ir3 *ir, struct ir3_shader_variant *so);
1194
1195 /* fp16 conversion folding */
1196 void ir3_cf(struct ir3 *ir);
1197
1198 /* copy-propagate: */
1199 void ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so);
1200
1201 /* group neighbors and insert mov's to resolve conflicts: */
1202 void ir3_group(struct ir3 *ir);
1203
1204 /* Sethi–Ullman numbering: */
1205 void ir3_sun(struct ir3 *ir);
1206
1207 /* scheduling: */
1208 void ir3_sched_add_deps(struct ir3 *ir);
1209 int ir3_sched(struct ir3 *ir);
1210
1211 struct ir3_context;
1212 int ir3_postsched(struct ir3_context *ctx);
1213
1214 bool ir3_a6xx_fixup_atomic_dests(struct ir3 *ir, struct ir3_shader_variant *so);
1215
1216 /* register assignment: */
1217 struct ir3_ra_reg_set * ir3_ra_alloc_reg_set(struct ir3_compiler *compiler);
1218 int ir3_ra(struct ir3_shader_variant *v, struct ir3_instruction **precolor, unsigned nprecolor);
1219
1220 /* legalize: */
1221 void ir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary);
1222
1223 static inline bool
1224 ir3_has_latency_to_hide(struct ir3 *ir)
1225 {
1226 /* VS/GS/TCS/TESS co-exist with frag shader invocations, but we don't
1227 * know the nature of the fragment shader. Just assume it will have
1228 * latency to hide:
1229 */
1230 if (ir->type != MESA_SHADER_FRAGMENT)
1231 return true;
1232
1233 foreach_block (block, &ir->block_list) {
1234 foreach_instr (instr, &block->instr_list) {
1235 if (is_tex_or_prefetch(instr))
1236 return true;
1237
1238 if (is_load(instr)) {
1239 switch (instr->opc) {
1240 case OPC_LDLV:
1241 case OPC_LDL:
1242 case OPC_LDLW:
1243 break;
1244 default:
1245 return true;
1246 }
1247 }
1248 }
1249 }
1250
1251 return false;
1252 }
1253
1254 /* ************************************************************************* */
1255 /* instruction helpers */
1256
1257 /* creates SSA src of correct type (ie. half vs full precision) */
1258 static inline struct ir3_register * __ssa_src(struct ir3_instruction *instr,
1259 struct ir3_instruction *src, unsigned flags)
1260 {
1261 struct ir3_register *reg;
1262 if (src->regs[0]->flags & IR3_REG_HALF)
1263 flags |= IR3_REG_HALF;
1264 reg = ir3_reg_create(instr, 0, IR3_REG_SSA | flags);
1265 reg->instr = src;
1266 reg->wrmask = src->regs[0]->wrmask;
1267 return reg;
1268 }
1269
1270 static inline struct ir3_register * __ssa_dst(struct ir3_instruction *instr)
1271 {
1272 struct ir3_register *reg = ir3_reg_create(instr, 0, 0);
1273 reg->flags |= IR3_REG_SSA;
1274 return reg;
1275 }
1276
1277 static inline struct ir3_instruction *
1278 create_immed_typed(struct ir3_block *block, uint32_t val, type_t type)
1279 {
1280 struct ir3_instruction *mov;
1281 unsigned flags = (type_size(type) < 32) ? IR3_REG_HALF : 0;
1282
1283 mov = ir3_instr_create(block, OPC_MOV);
1284 mov->cat1.src_type = type;
1285 mov->cat1.dst_type = type;
1286 __ssa_dst(mov)->flags |= flags;
1287 ir3_reg_create(mov, 0, IR3_REG_IMMED | flags)->uim_val = val;
1288
1289 return mov;
1290 }
1291
1292 static inline struct ir3_instruction *
1293 create_immed(struct ir3_block *block, uint32_t val)
1294 {
1295 return create_immed_typed(block, val, TYPE_U32);
1296 }
1297
1298 static inline struct ir3_instruction *
1299 create_uniform_typed(struct ir3_block *block, unsigned n, type_t type)
1300 {
1301 struct ir3_instruction *mov;
1302 unsigned flags = (type_size(type) < 32) ? IR3_REG_HALF : 0;
1303
1304 mov = ir3_instr_create(block, OPC_MOV);
1305 mov->cat1.src_type = type;
1306 mov->cat1.dst_type = type;
1307 __ssa_dst(mov)->flags |= flags;
1308 ir3_reg_create(mov, n, IR3_REG_CONST | flags);
1309
1310 return mov;
1311 }
1312
1313 static inline struct ir3_instruction *
1314 create_uniform(struct ir3_block *block, unsigned n)
1315 {
1316 return create_uniform_typed(block, n, TYPE_F32);
1317 }
1318
1319 static inline struct ir3_instruction *
1320 create_uniform_indirect(struct ir3_block *block, int n,
1321 struct ir3_instruction *address)
1322 {
1323 struct ir3_instruction *mov;
1324
1325 mov = ir3_instr_create(block, OPC_MOV);
1326 mov->cat1.src_type = TYPE_U32;
1327 mov->cat1.dst_type = TYPE_U32;
1328 __ssa_dst(mov);
1329 ir3_reg_create(mov, 0, IR3_REG_CONST | IR3_REG_RELATIV)->array.offset = n;
1330
1331 ir3_instr_set_address(mov, address);
1332
1333 return mov;
1334 }
1335
1336 static inline struct ir3_instruction *
1337 ir3_MOV(struct ir3_block *block, struct ir3_instruction *src, type_t type)
1338 {
1339 struct ir3_instruction *instr = ir3_instr_create(block, OPC_MOV);
1340 __ssa_dst(instr);
1341 if (src->regs[0]->flags & IR3_REG_ARRAY) {
1342 struct ir3_register *src_reg = __ssa_src(instr, src, IR3_REG_ARRAY);
1343 src_reg->array = src->regs[0]->array;
1344 } else {
1345 __ssa_src(instr, src, src->regs[0]->flags & IR3_REG_HIGH);
1346 }
1347 debug_assert(!(src->regs[0]->flags & IR3_REG_RELATIV));
1348 instr->cat1.src_type = type;
1349 instr->cat1.dst_type = type;
1350 return instr;
1351 }
1352
1353 static inline struct ir3_instruction *
1354 ir3_COV(struct ir3_block *block, struct ir3_instruction *src,
1355 type_t src_type, type_t dst_type)
1356 {
1357 struct ir3_instruction *instr = ir3_instr_create(block, OPC_MOV);
1358 unsigned dst_flags = (type_size(dst_type) < 32) ? IR3_REG_HALF : 0;
1359 unsigned src_flags = (type_size(src_type) < 32) ? IR3_REG_HALF : 0;
1360
1361 debug_assert((src->regs[0]->flags & IR3_REG_HALF) == src_flags);
1362
1363 __ssa_dst(instr)->flags |= dst_flags;
1364 __ssa_src(instr, src, 0);
1365 instr->cat1.src_type = src_type;
1366 instr->cat1.dst_type = dst_type;
1367 debug_assert(!(src->regs[0]->flags & IR3_REG_ARRAY));
1368 return instr;
1369 }
1370
1371 static inline struct ir3_instruction *
1372 ir3_NOP(struct ir3_block *block)
1373 {
1374 return ir3_instr_create(block, OPC_NOP);
1375 }
1376
1377 #define IR3_INSTR_0 0
1378
1379 #define __INSTR0(flag, name, opc) \
1380 static inline struct ir3_instruction * \
1381 ir3_##name(struct ir3_block *block) \
1382 { \
1383 struct ir3_instruction *instr = \
1384 ir3_instr_create(block, opc); \
1385 instr->flags |= flag; \
1386 return instr; \
1387 }
1388 #define INSTR0F(f, name) __INSTR0(IR3_INSTR_##f, name##_##f, OPC_##name)
1389 #define INSTR0(name) __INSTR0(0, name, OPC_##name)
1390
1391 #define __INSTR1(flag, name, opc) \
1392 static inline struct ir3_instruction * \
1393 ir3_##name(struct ir3_block *block, \
1394 struct ir3_instruction *a, unsigned aflags) \
1395 { \
1396 struct ir3_instruction *instr = \
1397 ir3_instr_create(block, opc); \
1398 __ssa_dst(instr); \
1399 __ssa_src(instr, a, aflags); \
1400 instr->flags |= flag; \
1401 return instr; \
1402 }
1403 #define INSTR1F(f, name) __INSTR1(IR3_INSTR_##f, name##_##f, OPC_##name)
1404 #define INSTR1(name) __INSTR1(0, name, OPC_##name)
1405
1406 #define __INSTR2(flag, name, opc) \
1407 static inline struct ir3_instruction * \
1408 ir3_##name(struct ir3_block *block, \
1409 struct ir3_instruction *a, unsigned aflags, \
1410 struct ir3_instruction *b, unsigned bflags) \
1411 { \
1412 struct ir3_instruction *instr = \
1413 ir3_instr_create(block, opc); \
1414 __ssa_dst(instr); \
1415 __ssa_src(instr, a, aflags); \
1416 __ssa_src(instr, b, bflags); \
1417 instr->flags |= flag; \
1418 return instr; \
1419 }
1420 #define INSTR2F(f, name) __INSTR2(IR3_INSTR_##f, name##_##f, OPC_##name)
1421 #define INSTR2(name) __INSTR2(0, name, OPC_##name)
1422
1423 #define __INSTR3(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 { \
1430 struct ir3_instruction *instr = \
1431 ir3_instr_create2(block, opc, 4); \
1432 __ssa_dst(instr); \
1433 __ssa_src(instr, a, aflags); \
1434 __ssa_src(instr, b, bflags); \
1435 __ssa_src(instr, c, cflags); \
1436 instr->flags |= flag; \
1437 return instr; \
1438 }
1439 #define INSTR3F(f, name) __INSTR3(IR3_INSTR_##f, name##_##f, OPC_##name)
1440 #define INSTR3(name) __INSTR3(0, name, OPC_##name)
1441
1442 #define __INSTR4(flag, name, opc) \
1443 static inline struct ir3_instruction * \
1444 ir3_##name(struct ir3_block *block, \
1445 struct ir3_instruction *a, unsigned aflags, \
1446 struct ir3_instruction *b, unsigned bflags, \
1447 struct ir3_instruction *c, unsigned cflags, \
1448 struct ir3_instruction *d, unsigned dflags) \
1449 { \
1450 struct ir3_instruction *instr = \
1451 ir3_instr_create2(block, opc, 5); \
1452 __ssa_dst(instr); \
1453 __ssa_src(instr, a, aflags); \
1454 __ssa_src(instr, b, bflags); \
1455 __ssa_src(instr, c, cflags); \
1456 __ssa_src(instr, d, dflags); \
1457 instr->flags |= flag; \
1458 return instr; \
1459 }
1460 #define INSTR4F(f, name) __INSTR4(IR3_INSTR_##f, name##_##f, OPC_##name)
1461 #define INSTR4(name) __INSTR4(0, name, OPC_##name)
1462
1463 /* cat0 instructions: */
1464 INSTR1(BR)
1465 INSTR0(JUMP)
1466 INSTR1(KILL)
1467 INSTR0(END)
1468 INSTR0(CHSH)
1469 INSTR0(CHMASK)
1470 INSTR1(IF)
1471 INSTR0(ELSE)
1472 INSTR0(ENDIF)
1473
1474 /* cat2 instructions, most 2 src but some 1 src: */
1475 INSTR2(ADD_F)
1476 INSTR2(MIN_F)
1477 INSTR2(MAX_F)
1478 INSTR2(MUL_F)
1479 INSTR1(SIGN_F)
1480 INSTR2(CMPS_F)
1481 INSTR1(ABSNEG_F)
1482 INSTR2(CMPV_F)
1483 INSTR1(FLOOR_F)
1484 INSTR1(CEIL_F)
1485 INSTR1(RNDNE_F)
1486 INSTR1(RNDAZ_F)
1487 INSTR1(TRUNC_F)
1488 INSTR2(ADD_U)
1489 INSTR2(ADD_S)
1490 INSTR2(SUB_U)
1491 INSTR2(SUB_S)
1492 INSTR2(CMPS_U)
1493 INSTR2(CMPS_S)
1494 INSTR2(MIN_U)
1495 INSTR2(MIN_S)
1496 INSTR2(MAX_U)
1497 INSTR2(MAX_S)
1498 INSTR1(ABSNEG_S)
1499 INSTR2(AND_B)
1500 INSTR2(OR_B)
1501 INSTR1(NOT_B)
1502 INSTR2(XOR_B)
1503 INSTR2(CMPV_U)
1504 INSTR2(CMPV_S)
1505 INSTR2(MUL_U24)
1506 INSTR2(MUL_S24)
1507 INSTR2(MULL_U)
1508 INSTR1(BFREV_B)
1509 INSTR1(CLZ_S)
1510 INSTR1(CLZ_B)
1511 INSTR2(SHL_B)
1512 INSTR2(SHR_B)
1513 INSTR2(ASHR_B)
1514 INSTR2(BARY_F)
1515 INSTR2(MGEN_B)
1516 INSTR2(GETBIT_B)
1517 INSTR1(SETRM)
1518 INSTR1(CBITS_B)
1519 INSTR2(SHB)
1520 INSTR2(MSAD)
1521
1522 /* cat3 instructions: */
1523 INSTR3(MAD_U16)
1524 INSTR3(MADSH_U16)
1525 INSTR3(MAD_S16)
1526 INSTR3(MADSH_M16)
1527 INSTR3(MAD_U24)
1528 INSTR3(MAD_S24)
1529 INSTR3(MAD_F16)
1530 INSTR3(MAD_F32)
1531 /* NOTE: SEL_B32 checks for zero vs nonzero */
1532 INSTR3(SEL_B16)
1533 INSTR3(SEL_B32)
1534 INSTR3(SEL_S16)
1535 INSTR3(SEL_S32)
1536 INSTR3(SEL_F16)
1537 INSTR3(SEL_F32)
1538 INSTR3(SAD_S16)
1539 INSTR3(SAD_S32)
1540
1541 /* cat4 instructions: */
1542 INSTR1(RCP)
1543 INSTR1(RSQ)
1544 INSTR1(HRSQ)
1545 INSTR1(LOG2)
1546 INSTR1(HLOG2)
1547 INSTR1(EXP2)
1548 INSTR1(HEXP2)
1549 INSTR1(SIN)
1550 INSTR1(COS)
1551 INSTR1(SQRT)
1552
1553 /* cat5 instructions: */
1554 INSTR1(DSX)
1555 INSTR1(DSXPP_1)
1556 INSTR1(DSY)
1557 INSTR1(DSYPP_1)
1558 INSTR1F(3D, DSX)
1559 INSTR1F(3D, DSY)
1560 INSTR1(RGETPOS)
1561
1562 static inline struct ir3_instruction *
1563 ir3_SAM(struct ir3_block *block, opc_t opc, type_t type,
1564 unsigned wrmask, unsigned flags, struct ir3_instruction *samp_tex,
1565 struct ir3_instruction *src0, struct ir3_instruction *src1)
1566 {
1567 struct ir3_instruction *sam;
1568
1569 sam = ir3_instr_create(block, opc);
1570 sam->flags |= flags;
1571 __ssa_dst(sam)->wrmask = wrmask;
1572 if (flags & IR3_INSTR_S2EN) {
1573 __ssa_src(sam, samp_tex, IR3_REG_HALF);
1574 }
1575 if (src0) {
1576 __ssa_src(sam, src0, 0)->wrmask = (1 << (src0->regs_count - 1)) - 1;
1577 }
1578 if (src1) {
1579 __ssa_src(sam, src1, 0)->wrmask =(1 << (src1->regs_count - 1)) - 1;
1580 }
1581 sam->cat5.type = type;
1582
1583 return sam;
1584 }
1585
1586 /* cat6 instructions: */
1587 INSTR2(LDLV)
1588 INSTR3(LDG)
1589 INSTR3(LDL)
1590 INSTR3(LDLW)
1591 INSTR3(STG)
1592 INSTR3(STL)
1593 INSTR3(STLW)
1594 INSTR1(RESINFO)
1595 INSTR1(RESFMT)
1596 INSTR2(ATOMIC_ADD)
1597 INSTR2(ATOMIC_SUB)
1598 INSTR2(ATOMIC_XCHG)
1599 INSTR2(ATOMIC_INC)
1600 INSTR2(ATOMIC_DEC)
1601 INSTR2(ATOMIC_CMPXCHG)
1602 INSTR2(ATOMIC_MIN)
1603 INSTR2(ATOMIC_MAX)
1604 INSTR2(ATOMIC_AND)
1605 INSTR2(ATOMIC_OR)
1606 INSTR2(ATOMIC_XOR)
1607 INSTR2(LDC)
1608 #if GPU >= 600
1609 INSTR3(STIB);
1610 INSTR2(LDIB);
1611 INSTR3F(G, ATOMIC_ADD)
1612 INSTR3F(G, ATOMIC_SUB)
1613 INSTR3F(G, ATOMIC_XCHG)
1614 INSTR3F(G, ATOMIC_INC)
1615 INSTR3F(G, ATOMIC_DEC)
1616 INSTR3F(G, ATOMIC_CMPXCHG)
1617 INSTR3F(G, ATOMIC_MIN)
1618 INSTR3F(G, ATOMIC_MAX)
1619 INSTR3F(G, ATOMIC_AND)
1620 INSTR3F(G, ATOMIC_OR)
1621 INSTR3F(G, ATOMIC_XOR)
1622 #elif GPU >= 400
1623 INSTR3(LDGB)
1624 INSTR4(STGB)
1625 INSTR4(STIB)
1626 INSTR4F(G, ATOMIC_ADD)
1627 INSTR4F(G, ATOMIC_SUB)
1628 INSTR4F(G, ATOMIC_XCHG)
1629 INSTR4F(G, ATOMIC_INC)
1630 INSTR4F(G, ATOMIC_DEC)
1631 INSTR4F(G, ATOMIC_CMPXCHG)
1632 INSTR4F(G, ATOMIC_MIN)
1633 INSTR4F(G, ATOMIC_MAX)
1634 INSTR4F(G, ATOMIC_AND)
1635 INSTR4F(G, ATOMIC_OR)
1636 INSTR4F(G, ATOMIC_XOR)
1637 #endif
1638
1639 INSTR4F(G, STG)
1640
1641 /* cat7 instructions: */
1642 INSTR0(BAR)
1643 INSTR0(FENCE)
1644
1645 /* meta instructions: */
1646 INSTR0(META_TEX_PREFETCH);
1647
1648 /* ************************************************************************* */
1649 /* split this out or find some helper to use.. like main/bitset.h.. */
1650
1651 #include <string.h>
1652 #include "util/bitset.h"
1653
1654 #define MAX_REG 256
1655
1656 typedef BITSET_DECLARE(regmask_t, 2 * MAX_REG);
1657
1658 static inline bool
1659 __regmask_get(regmask_t *regmask, struct ir3_register *reg, unsigned n)
1660 {
1661 if (reg->merged) {
1662 /* a6xx+ case, with merged register file, we track things in terms
1663 * of half-precision registers, with a full precisions register
1664 * using two half-precision slots:
1665 */
1666 if (reg->flags & IR3_REG_HALF) {
1667 return BITSET_TEST(*regmask, n);
1668 } else {
1669 n *= 2;
1670 return BITSET_TEST(*regmask, n) || BITSET_TEST(*regmask, n+1);
1671 }
1672 } else {
1673 /* pre a6xx case, with separate register file for half and full
1674 * precision:
1675 */
1676 if (reg->flags & IR3_REG_HALF)
1677 n += MAX_REG;
1678 return BITSET_TEST(*regmask, n);
1679 }
1680 }
1681
1682 static inline void
1683 __regmask_set(regmask_t *regmask, struct ir3_register *reg, unsigned n)
1684 {
1685 if (reg->merged) {
1686 /* a6xx+ case, with merged register file, we track things in terms
1687 * of half-precision registers, with a full precisions register
1688 * using two half-precision slots:
1689 */
1690 if (reg->flags & IR3_REG_HALF) {
1691 BITSET_SET(*regmask, n);
1692 } else {
1693 n *= 2;
1694 BITSET_SET(*regmask, n);
1695 BITSET_SET(*regmask, n+1);
1696 }
1697 } else {
1698 /* pre a6xx case, with separate register file for half and full
1699 * precision:
1700 */
1701 if (reg->flags & IR3_REG_HALF)
1702 n += MAX_REG;
1703 BITSET_SET(*regmask, n);
1704 }
1705 }
1706
1707 static inline void regmask_init(regmask_t *regmask)
1708 {
1709 memset(regmask, 0, sizeof(*regmask));
1710 }
1711
1712 static inline void regmask_set(regmask_t *regmask, struct ir3_register *reg)
1713 {
1714 if (reg->flags & IR3_REG_RELATIV) {
1715 for (unsigned i = 0; i < reg->size; i++)
1716 __regmask_set(regmask, reg, reg->array.offset + i);
1717 } else {
1718 for (unsigned mask = reg->wrmask, n = reg->num; mask; mask >>= 1, n++)
1719 if (mask & 1)
1720 __regmask_set(regmask, reg, n);
1721 }
1722 }
1723
1724 static inline void regmask_or(regmask_t *dst, regmask_t *a, regmask_t *b)
1725 {
1726 unsigned i;
1727 for (i = 0; i < ARRAY_SIZE(*dst); i++)
1728 (*dst)[i] = (*a)[i] | (*b)[i];
1729 }
1730
1731 static inline bool regmask_get(regmask_t *regmask,
1732 struct ir3_register *reg)
1733 {
1734 if (reg->flags & IR3_REG_RELATIV) {
1735 for (unsigned i = 0; i < reg->size; i++)
1736 if (__regmask_get(regmask, reg, reg->array.offset + i))
1737 return true;
1738 } else {
1739 for (unsigned mask = reg->wrmask, n = reg->num; mask; mask >>= 1, n++)
1740 if (mask & 1)
1741 if (__regmask_get(regmask, reg, n))
1742 return true;
1743 }
1744 return false;
1745 }
1746
1747 /* ************************************************************************* */
1748
1749 #endif /* IR3_H_ */