freedreno/ir3: add a pass to collect SSA uses
[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 /* meta-flags, for intermediate stages of IR, ie.
222 * before register assignment is done:
223 */
224 IR3_INSTR_MARK = 0x1000,
225 IR3_INSTR_UNUSED= 0x2000,
226 } flags;
227 uint8_t repeat;
228 uint8_t nop;
229 #ifdef DEBUG
230 unsigned regs_max;
231 #endif
232 unsigned regs_count;
233 struct ir3_register **regs;
234 union {
235 struct {
236 char inv;
237 char comp;
238 int immed;
239 struct ir3_block *target;
240 } cat0;
241 struct {
242 type_t src_type, dst_type;
243 } cat1;
244 struct {
245 enum {
246 IR3_COND_LT = 0,
247 IR3_COND_LE = 1,
248 IR3_COND_GT = 2,
249 IR3_COND_GE = 3,
250 IR3_COND_EQ = 4,
251 IR3_COND_NE = 5,
252 } condition;
253 } cat2;
254 struct {
255 unsigned samp, tex;
256 type_t type;
257 } cat5;
258 struct {
259 type_t type;
260 int src_offset;
261 int dst_offset;
262 int iim_val : 3; /* for ldgb/stgb, # of components */
263 unsigned d : 3;
264 bool typed : 1;
265 } cat6;
266 struct {
267 unsigned w : 1; /* write */
268 unsigned r : 1; /* read */
269 unsigned l : 1; /* local */
270 unsigned g : 1; /* global */
271 } cat7;
272 /* for meta-instructions, just used to hold extra data
273 * before instruction scheduling, etc
274 */
275 struct {
276 int off; /* component/offset */
277 } split;
278 struct {
279 /* for output collects, this maps back to the entry in the
280 * ir3_shader_variant::outputs table.
281 */
282 int outidx;
283 } collect;
284 struct {
285 unsigned samp, tex;
286 unsigned input_offset;
287 } prefetch;
288 struct {
289 /* maps back to entry in ir3_shader_variant::inputs table: */
290 int inidx;
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)
294 */
295 gl_system_value sysval;
296 } input;
297 };
298
299 /* transient values used during various algorithms: */
300 union {
301 /* The instruction depth is the max dependency distance to output.
302 *
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.
308 */
309 int depth;
310 /* When we get to the RA stage, we no longer need depth, but
311 * we do need instruction's position/name:
312 */
313 struct {
314 uint16_t ip;
315 uint16_t name;
316 };
317 };
318
319 /* used for per-pass extra instruction data.
320 *
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.
325 */
326 void *data;
327
328 /**
329 * Valid if pass calls ir3_find_ssa_uses().. see foreach_ssa_use()
330 */
331 struct set *uses;
332
333 int sun; /* Sethi–Ullman number, used by sched */
334 int use_count; /* currently just updated/used by cp */
335
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.
345 *
346 * TODO: maybe just add something like:
347 * struct ir3_instruction_ref {
348 * struct ir3_instruction *instr;
349 * unsigned cnt;
350 * }
351 *
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).
357 */
358 struct {
359 struct ir3_instruction *left, *right;
360 uint16_t left_cnt, right_cnt;
361 } cp;
362
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.
365 *
366 * NOTE: do not write this directly, use ir3_instr_set_address()
367 */
368 struct ir3_instruction *address;
369
370 /* Tracking for additional dependent instructions. Used to handle
371 * barriers, WAR hazards for arrays/SSBOs/etc.
372 */
373 DECLARE_ARRAY(struct ir3_instruction *, deps);
374
375 /*
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
387 *
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??)
391 */
392 enum {
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;
403
404 /* Entry in ir3_block's instruction list: */
405 struct list_head node;
406
407 #ifdef DEBUG
408 uint32_t serialno;
409 #endif
410
411 // TODO only computerator/assembler:
412 int line;
413 };
414
415 static inline struct ir3_instruction *
416 ir3_neighbor_first(struct ir3_instruction *instr)
417 {
418 int cnt = 0;
419 while (instr->cp.left) {
420 instr = instr->cp.left;
421 if (++cnt > 0xffff) {
422 debug_assert(0);
423 break;
424 }
425 }
426 return instr;
427 }
428
429 static inline int ir3_neighbor_count(struct ir3_instruction *instr)
430 {
431 int num = 1;
432
433 debug_assert(!instr->cp.left);
434
435 while (instr->cp.right) {
436 num++;
437 instr = instr->cp.right;
438 if (num > 0xffff) {
439 debug_assert(0);
440 break;
441 }
442 }
443
444 return num;
445 }
446
447 struct ir3 {
448 struct ir3_compiler *compiler;
449 gl_shader_stage type;
450
451 DECLARE_ARRAY(struct ir3_instruction *, inputs);
452 DECLARE_ARRAY(struct ir3_instruction *, outputs);
453
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).
459 */
460 DECLARE_ARRAY(struct ir3_instruction *, baryfs);
461
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.
471 */
472 DECLARE_ARRAY(struct ir3_instruction *, indirects);
473
474 /* and same for instructions that consume predicate register: */
475 DECLARE_ARRAY(struct ir3_instruction *, predicates);
476
477 /* Track texture sample instructions which need texture state
478 * patched in (for astc-srgb workaround):
479 */
480 DECLARE_ARRAY(struct ir3_instruction *, astc_srgb);
481
482 /* List of blocks: */
483 struct list_head block_list;
484
485 /* List of ir3_array's: */
486 struct list_head array_list;
487
488 unsigned max_sun; /* max Sethi–Ullman number */
489
490 #ifdef DEBUG
491 unsigned block_count, instr_count;
492 #endif
493 };
494
495 struct ir3_array {
496 struct list_head node;
497 unsigned length;
498 unsigned id;
499
500 struct nir_register *r;
501
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
507 */
508 struct ir3_instruction *last_write;
509
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;
514
515 /* Indicates if half-precision */
516 bool half;
517 };
518
519 struct ir3_array * ir3_lookup_array(struct ir3 *ir, unsigned id);
520
521 struct ir3_block {
522 struct list_head node;
523 struct ir3 *shader;
524
525 const struct nir_block *nblock;
526
527 struct list_head instr_list; /* list of ir3_instruction */
528
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.
532 */
533 struct ir3_instruction *condition;
534 struct ir3_block *successors[2];
535
536 struct set *predecessors; /* set of ir3_block */
537
538 uint16_t start_ip, end_ip;
539
540 /* Track instructions which do not write a register but other-
541 * wise must not be discarded (such as kill, stg, etc)
542 */
543 DECLARE_ARRAY(struct ir3_instruction *, keeps);
544
545 /* used for per-pass extra block data. Mainly used right
546 * now in RA step to track livein/liveout.
547 */
548 void *data;
549
550 #ifdef DEBUG
551 uint32_t serialno;
552 #endif
553 };
554
555 static inline uint32_t
556 block_id(struct ir3_block *block)
557 {
558 #ifdef DEBUG
559 return block->serialno;
560 #else
561 return (uint32_t)(unsigned long)block;
562 #endif
563 }
564
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);
570
571 struct ir3_block * ir3_block_create(struct ir3 *shader);
572
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);
579
580 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
581 int num, int flags);
582 struct ir3_register * ir3_reg_clone(struct ir3 *shader,
583 struct ir3_register *reg);
584
585 void ir3_instr_set_address(struct ir3_instruction *instr,
586 struct ir3_instruction *addr);
587
588 static inline bool ir3_instr_check_mark(struct ir3_instruction *instr)
589 {
590 if (instr->flags & IR3_INSTR_MARK)
591 return true; /* already visited */
592 instr->flags |= IR3_INSTR_MARK;
593 return false;
594 }
595
596 void ir3_block_clear_mark(struct ir3_block *block);
597 void ir3_clear_mark(struct ir3 *shader);
598
599 unsigned ir3_count_instructions(struct ir3 *ir);
600
601 void ir3_find_ssa_uses(struct ir3 *ir, void *mem_ctx);
602
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))
609
610 #define MAX_ARRAYS 16
611
612 /* comp:
613 * 0 - x
614 * 1 - y
615 * 2 - z
616 * 3 - w
617 */
618 static inline uint32_t regid(int num, int comp)
619 {
620 return (num << 2) | (comp & 0x3);
621 }
622
623 static inline uint32_t reg_num(struct ir3_register *reg)
624 {
625 return reg->num >> 2;
626 }
627
628 static inline uint32_t reg_comp(struct ir3_register *reg)
629 {
630 return reg->num & 0x3;
631 }
632
633 #define INVALID_REG regid(63, 0)
634 #define VALIDREG(r) ((r) != INVALID_REG)
635 #define CONDREG(r, val) COND(VALIDREG(r), (val))
636
637 static inline bool is_flow(struct ir3_instruction *instr)
638 {
639 return (opc_cat(instr->opc) == 0);
640 }
641
642 static inline bool is_kill(struct ir3_instruction *instr)
643 {
644 return instr->opc == OPC_KILL;
645 }
646
647 static inline bool is_nop(struct ir3_instruction *instr)
648 {
649 return instr->opc == OPC_NOP;
650 }
651
652 static inline bool is_same_type_reg(struct ir3_register *reg1,
653 struct ir3_register *reg2)
654 {
655 unsigned type_reg1 = (reg1->flags & (IR3_REG_HIGH | IR3_REG_HALF));
656 unsigned type_reg2 = (reg2->flags & (IR3_REG_HIGH | IR3_REG_HALF));
657
658 if (type_reg1 ^ type_reg2)
659 return false;
660 else
661 return true;
662 }
663
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).
667 */
668 static inline bool is_same_type_mov(struct ir3_instruction *instr)
669 {
670 struct ir3_register *dst;
671
672 switch (instr->opc) {
673 case OPC_MOV:
674 if (instr->cat1.src_type != instr->cat1.dst_type)
675 return false;
676 /* If the type of dest reg and src reg are different,
677 * it shouldn't be considered as same type mov
678 */
679 if (!is_same_type_reg(instr->regs[0], instr->regs[1]))
680 return false;
681 break;
682 case OPC_ABSNEG_F:
683 case OPC_ABSNEG_S:
684 if (instr->flags & IR3_INSTR_SAT)
685 return false;
686 /* If the type of dest reg and src reg are different,
687 * it shouldn't be considered as same type mov
688 */
689 if (!is_same_type_reg(instr->regs[0], instr->regs[1]))
690 return false;
691 break;
692 default:
693 return false;
694 }
695
696 dst = instr->regs[0];
697
698 /* mov's that write to a0.x or p0.x are special: */
699 if (dst->num == regid(REG_P0, 0))
700 return false;
701 if (dst->num == regid(REG_A0, 0))
702 return false;
703
704 if (dst->flags & (IR3_REG_RELATIV | IR3_REG_ARRAY))
705 return false;
706
707 return true;
708 }
709
710 /* A move from const, which changes size but not type, can also be
711 * folded into dest instruction in some cases.
712 */
713 static inline bool is_const_mov(struct ir3_instruction *instr)
714 {
715 if (instr->opc != OPC_MOV)
716 return false;
717
718 if (!(instr->regs[1]->flags & IR3_REG_CONST))
719 return false;
720
721 type_t src_type = instr->cat1.src_type;
722 type_t dst_type = instr->cat1.dst_type;
723
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));
727 }
728
729 static inline bool is_alu(struct ir3_instruction *instr)
730 {
731 return (1 <= opc_cat(instr->opc)) && (opc_cat(instr->opc) <= 3);
732 }
733
734 static inline bool is_sfu(struct ir3_instruction *instr)
735 {
736 return (opc_cat(instr->opc) == 4);
737 }
738
739 static inline bool is_tex(struct ir3_instruction *instr)
740 {
741 return (opc_cat(instr->opc) == 5);
742 }
743
744 static inline bool is_tex_or_prefetch(struct ir3_instruction *instr)
745 {
746 return is_tex(instr) || (instr->opc == OPC_META_TEX_PREFETCH);
747 }
748
749 static inline bool is_mem(struct ir3_instruction *instr)
750 {
751 return (opc_cat(instr->opc) == 6);
752 }
753
754 static inline bool is_barrier(struct ir3_instruction *instr)
755 {
756 return (opc_cat(instr->opc) == 7);
757 }
758
759 static inline bool
760 is_half(struct ir3_instruction *instr)
761 {
762 return !!(instr->regs[0]->flags & IR3_REG_HALF);
763 }
764
765 static inline bool
766 is_high(struct ir3_instruction *instr)
767 {
768 return !!(instr->regs[0]->flags & IR3_REG_HIGH);
769 }
770
771 static inline bool
772 is_store(struct ir3_instruction *instr)
773 {
774 /* these instructions, the "destination" register is
775 * actually a source, the address to store to.
776 */
777 switch (instr->opc) {
778 case OPC_STG:
779 case OPC_STGB:
780 case OPC_STIB:
781 case OPC_STP:
782 case OPC_STL:
783 case OPC_STLW:
784 case OPC_L2G:
785 case OPC_G2L:
786 return true;
787 default:
788 return false;
789 }
790 }
791
792 static inline bool is_load(struct ir3_instruction *instr)
793 {
794 switch (instr->opc) {
795 case OPC_LDG:
796 case OPC_LDGB:
797 case OPC_LDIB:
798 case OPC_LDL:
799 case OPC_LDP:
800 case OPC_L2G:
801 case OPC_LDLW:
802 case OPC_LDC:
803 case OPC_LDLV:
804 /* probably some others too.. */
805 return true;
806 default:
807 return false;
808 }
809 }
810
811 static inline bool is_input(struct ir3_instruction *instr)
812 {
813 /* in some cases, ldlv is used to fetch varying without
814 * interpolation.. fortunately inloc is the first src
815 * register in either case
816 */
817 switch (instr->opc) {
818 case OPC_LDLV:
819 case OPC_BARY_F:
820 return true;
821 default:
822 return false;
823 }
824 }
825
826 static inline bool is_bool(struct ir3_instruction *instr)
827 {
828 switch (instr->opc) {
829 case OPC_CMPS_F:
830 case OPC_CMPS_S:
831 case OPC_CMPS_U:
832 return true;
833 default:
834 return false;
835 }
836 }
837
838 static inline bool is_meta(struct ir3_instruction *instr)
839 {
840 return (opc_cat(instr->opc) == -1);
841 }
842
843 static inline unsigned dest_regs(struct ir3_instruction *instr)
844 {
845 if ((instr->regs_count == 0) || is_store(instr) || is_flow(instr))
846 return 0;
847
848 return util_last_bit(instr->regs[0]->wrmask);
849 }
850
851 static inline bool writes_addr(struct ir3_instruction *instr)
852 {
853 if (instr->regs_count > 0) {
854 struct ir3_register *dst = instr->regs[0];
855 return reg_num(dst) == REG_A0;
856 }
857 return false;
858 }
859
860 static inline bool writes_pred(struct ir3_instruction *instr)
861 {
862 if (instr->regs_count > 0) {
863 struct ir3_register *dst = instr->regs[0];
864 return reg_num(dst) == REG_P0;
865 }
866 return false;
867 }
868
869 /* returns defining instruction for reg */
870 /* TODO better name */
871 static inline struct ir3_instruction *ssa(struct ir3_register *reg)
872 {
873 if (reg->flags & (IR3_REG_SSA | IR3_REG_ARRAY)) {
874 return reg->instr;
875 }
876 return NULL;
877 }
878
879 static inline bool conflicts(struct ir3_instruction *a,
880 struct ir3_instruction *b)
881 {
882 return (a && b) && (a != b);
883 }
884
885 static inline bool reg_gpr(struct ir3_register *r)
886 {
887 if (r->flags & (IR3_REG_CONST | IR3_REG_IMMED))
888 return false;
889 if ((reg_num(r) == REG_A0) || (reg_num(r) == REG_P0))
890 return false;
891 return true;
892 }
893
894 static inline type_t half_type(type_t type)
895 {
896 switch (type) {
897 case TYPE_F32: return TYPE_F16;
898 case TYPE_U32: return TYPE_U16;
899 case TYPE_S32: return TYPE_S16;
900 case TYPE_F16:
901 case TYPE_U16:
902 case TYPE_S16:
903 return type;
904 default:
905 assert(0);
906 return ~0;
907 }
908 }
909
910 /* some cat2 instructions (ie. those which are not float) can embed an
911 * immediate:
912 */
913 static inline bool ir3_cat2_int(opc_t opc)
914 {
915 switch (opc) {
916 case OPC_ADD_U:
917 case OPC_ADD_S:
918 case OPC_SUB_U:
919 case OPC_SUB_S:
920 case OPC_CMPS_U:
921 case OPC_CMPS_S:
922 case OPC_MIN_U:
923 case OPC_MIN_S:
924 case OPC_MAX_U:
925 case OPC_MAX_S:
926 case OPC_CMPV_U:
927 case OPC_CMPV_S:
928 case OPC_MUL_U24:
929 case OPC_MUL_S24:
930 case OPC_MULL_U:
931 case OPC_CLZ_S:
932 case OPC_ABSNEG_S:
933 case OPC_AND_B:
934 case OPC_OR_B:
935 case OPC_NOT_B:
936 case OPC_XOR_B:
937 case OPC_BFREV_B:
938 case OPC_CLZ_B:
939 case OPC_SHL_B:
940 case OPC_SHR_B:
941 case OPC_ASHR_B:
942 case OPC_MGEN_B:
943 case OPC_GETBIT_B:
944 case OPC_CBITS_B:
945 case OPC_BARY_F:
946 return true;
947
948 default:
949 return false;
950 }
951 }
952
953 static inline bool ir3_cat2_float(opc_t opc)
954 {
955 switch (opc) {
956 case OPC_ADD_F:
957 case OPC_MIN_F:
958 case OPC_MAX_F:
959 case OPC_MUL_F:
960 case OPC_SIGN_F:
961 case OPC_CMPS_F:
962 case OPC_ABSNEG_F:
963 case OPC_CMPV_F:
964 case OPC_FLOOR_F:
965 case OPC_CEIL_F:
966 case OPC_RNDNE_F:
967 case OPC_RNDAZ_F:
968 case OPC_TRUNC_F:
969 return true;
970
971 default:
972 return false;
973 }
974 }
975
976 static inline bool ir3_cat3_float(opc_t opc)
977 {
978 switch (opc) {
979 case OPC_MAD_F16:
980 case OPC_MAD_F32:
981 case OPC_SEL_F16:
982 case OPC_SEL_F32:
983 return true;
984 default:
985 return false;
986 }
987 }
988
989 /* map cat2 instruction to valid abs/neg flags: */
990 static inline unsigned ir3_cat2_absneg(opc_t opc)
991 {
992 switch (opc) {
993 case OPC_ADD_F:
994 case OPC_MIN_F:
995 case OPC_MAX_F:
996 case OPC_MUL_F:
997 case OPC_SIGN_F:
998 case OPC_CMPS_F:
999 case OPC_ABSNEG_F:
1000 case OPC_CMPV_F:
1001 case OPC_FLOOR_F:
1002 case OPC_CEIL_F:
1003 case OPC_RNDNE_F:
1004 case OPC_RNDAZ_F:
1005 case OPC_TRUNC_F:
1006 case OPC_BARY_F:
1007 return IR3_REG_FABS | IR3_REG_FNEG;
1008
1009 case OPC_ADD_U:
1010 case OPC_ADD_S:
1011 case OPC_SUB_U:
1012 case OPC_SUB_S:
1013 case OPC_CMPS_U:
1014 case OPC_CMPS_S:
1015 case OPC_MIN_U:
1016 case OPC_MIN_S:
1017 case OPC_MAX_U:
1018 case OPC_MAX_S:
1019 case OPC_CMPV_U:
1020 case OPC_CMPV_S:
1021 case OPC_MUL_U24:
1022 case OPC_MUL_S24:
1023 case OPC_MULL_U:
1024 case OPC_CLZ_S:
1025 return 0;
1026
1027 case OPC_ABSNEG_S:
1028 return IR3_REG_SABS | IR3_REG_SNEG;
1029
1030 case OPC_AND_B:
1031 case OPC_OR_B:
1032 case OPC_NOT_B:
1033 case OPC_XOR_B:
1034 case OPC_BFREV_B:
1035 case OPC_CLZ_B:
1036 case OPC_SHL_B:
1037 case OPC_SHR_B:
1038 case OPC_ASHR_B:
1039 case OPC_MGEN_B:
1040 case OPC_GETBIT_B:
1041 case OPC_CBITS_B:
1042 return IR3_REG_BNOT;
1043
1044 default:
1045 return 0;
1046 }
1047 }
1048
1049 /* map cat3 instructions to valid abs/neg flags: */
1050 static inline unsigned ir3_cat3_absneg(opc_t opc)
1051 {
1052 switch (opc) {
1053 case OPC_MAD_F16:
1054 case OPC_MAD_F32:
1055 case OPC_SEL_F16:
1056 case OPC_SEL_F32:
1057 return IR3_REG_FNEG;
1058
1059 case OPC_MAD_U16:
1060 case OPC_MADSH_U16:
1061 case OPC_MAD_S16:
1062 case OPC_MADSH_M16:
1063 case OPC_MAD_U24:
1064 case OPC_MAD_S24:
1065 case OPC_SEL_S16:
1066 case OPC_SEL_S32:
1067 case OPC_SAD_S16:
1068 case OPC_SAD_S32:
1069 /* neg *may* work on 3rd src.. */
1070
1071 case OPC_SEL_B16:
1072 case OPC_SEL_B32:
1073
1074 default:
1075 return 0;
1076 }
1077 }
1078
1079 #define MASK(n) ((1 << (n)) - 1)
1080
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]))
1086
1087 /* iterator for an instructions's sources (reg): */
1088 #define foreach_src(__srcreg, __instr) \
1089 foreach_src_n(__srcreg, __i, __instr)
1090
1091 static inline unsigned __ssa_src_cnt(struct ir3_instruction *instr)
1092 {
1093 unsigned cnt = instr->regs_count + instr->deps_count;
1094 if (instr->address)
1095 cnt++;
1096 return cnt;
1097 }
1098
1099 static inline struct ir3_instruction * __ssa_src_n(struct ir3_instruction *instr, unsigned n)
1100 {
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]);
1106 }
1107
1108 static inline bool __is_false_dep(struct ir3_instruction *instr, unsigned n)
1109 {
1110 if (n == (instr->regs_count + instr->deps_count))
1111 return false;
1112 if (n >= instr->regs_count)
1113 return true;
1114 return false;
1115 }
1116
1117 #define __src_cnt(__instr) ((__instr)->address ? (__instr)->regs_count : (__instr)->regs_count - 1)
1118
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)))
1123
1124 /* iterator for an instruction's SSA sources (instr): */
1125 #define foreach_ssa_src(__srcinst, __instr) \
1126 foreach_ssa_src_n(__srcinst, __i, __instr)
1127
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)
1134
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)
1141
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)
1149
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)
1155
1156 /* iterators for arrays: */
1157 #define foreach_array(__array, __list) \
1158 list_for_each_entry(struct ir3_array, __array, __list, node)
1159
1160 /* dump: */
1161 void ir3_print(struct ir3 *ir);
1162 void ir3_print_instr(struct ir3_instruction *instr);
1163
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);
1170
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);
1175
1176 /* fp16 conversion folding */
1177 void ir3_cf(struct ir3 *ir);
1178
1179 /* copy-propagate: */
1180 void ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so);
1181
1182 /* group neighbors and insert mov's to resolve conflicts: */
1183 void ir3_group(struct ir3 *ir);
1184
1185 /* Sethi–Ullman numbering: */
1186 void ir3_sun(struct ir3 *ir);
1187
1188 /* scheduling: */
1189 void ir3_sched_add_deps(struct ir3 *ir);
1190 int ir3_sched(struct ir3 *ir);
1191
1192 struct ir3_context;
1193 int ir3_postsched(struct ir3_context *ctx);
1194
1195 bool ir3_a6xx_fixup_atomic_dests(struct ir3 *ir, struct ir3_shader_variant *so);
1196
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);
1200
1201 /* legalize: */
1202 void ir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary);
1203
1204 static inline bool
1205 ir3_has_latency_to_hide(struct ir3 *ir)
1206 {
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
1209 * latency to hide:
1210 */
1211 if (ir->type != MESA_SHADER_FRAGMENT)
1212 return true;
1213
1214 foreach_block (block, &ir->block_list) {
1215 foreach_instr (instr, &block->instr_list) {
1216 if (is_tex_or_prefetch(instr))
1217 return true;
1218
1219 if (is_load(instr)) {
1220 switch (instr->opc) {
1221 case OPC_LDLV:
1222 case OPC_LDL:
1223 case OPC_LDLW:
1224 break;
1225 default:
1226 return true;
1227 }
1228 }
1229 }
1230 }
1231
1232 return false;
1233 }
1234
1235 /* ************************************************************************* */
1236 /* instruction helpers */
1237
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)
1241 {
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);
1246 reg->instr = src;
1247 reg->wrmask = src->regs[0]->wrmask;
1248 return reg;
1249 }
1250
1251 static inline struct ir3_register * __ssa_dst(struct ir3_instruction *instr)
1252 {
1253 struct ir3_register *reg = ir3_reg_create(instr, 0, 0);
1254 reg->flags |= IR3_REG_SSA;
1255 return reg;
1256 }
1257
1258 static inline struct ir3_instruction *
1259 create_immed_typed(struct ir3_block *block, uint32_t val, type_t type)
1260 {
1261 struct ir3_instruction *mov;
1262 unsigned flags = (type_size(type) < 32) ? IR3_REG_HALF : 0;
1263
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;
1269
1270 return mov;
1271 }
1272
1273 static inline struct ir3_instruction *
1274 create_immed(struct ir3_block *block, uint32_t val)
1275 {
1276 return create_immed_typed(block, val, TYPE_U32);
1277 }
1278
1279 static inline struct ir3_instruction *
1280 create_uniform_typed(struct ir3_block *block, unsigned n, type_t type)
1281 {
1282 struct ir3_instruction *mov;
1283 unsigned flags = (type_size(type) < 32) ? IR3_REG_HALF : 0;
1284
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);
1290
1291 return mov;
1292 }
1293
1294 static inline struct ir3_instruction *
1295 create_uniform(struct ir3_block *block, unsigned n)
1296 {
1297 return create_uniform_typed(block, n, TYPE_F32);
1298 }
1299
1300 static inline struct ir3_instruction *
1301 create_uniform_indirect(struct ir3_block *block, int n,
1302 struct ir3_instruction *address)
1303 {
1304 struct ir3_instruction *mov;
1305
1306 mov = ir3_instr_create(block, OPC_MOV);
1307 mov->cat1.src_type = TYPE_U32;
1308 mov->cat1.dst_type = TYPE_U32;
1309 __ssa_dst(mov);
1310 ir3_reg_create(mov, 0, IR3_REG_CONST | IR3_REG_RELATIV)->array.offset = n;
1311
1312 ir3_instr_set_address(mov, address);
1313
1314 return mov;
1315 }
1316
1317 static inline struct ir3_instruction *
1318 ir3_MOV(struct ir3_block *block, struct ir3_instruction *src, type_t type)
1319 {
1320 struct ir3_instruction *instr = ir3_instr_create(block, OPC_MOV);
1321 __ssa_dst(instr);
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;
1325 } else {
1326 __ssa_src(instr, src, src->regs[0]->flags & IR3_REG_HIGH);
1327 }
1328 debug_assert(!(src->regs[0]->flags & IR3_REG_RELATIV));
1329 instr->cat1.src_type = type;
1330 instr->cat1.dst_type = type;
1331 return instr;
1332 }
1333
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)
1337 {
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;
1341
1342 debug_assert((src->regs[0]->flags & IR3_REG_HALF) == src_flags);
1343
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));
1349 return instr;
1350 }
1351
1352 static inline struct ir3_instruction *
1353 ir3_NOP(struct ir3_block *block)
1354 {
1355 return ir3_instr_create(block, OPC_NOP);
1356 }
1357
1358 #define IR3_INSTR_0 0
1359
1360 #define __INSTR0(flag, name, opc) \
1361 static inline struct ir3_instruction * \
1362 ir3_##name(struct ir3_block *block) \
1363 { \
1364 struct ir3_instruction *instr = \
1365 ir3_instr_create(block, opc); \
1366 instr->flags |= flag; \
1367 return instr; \
1368 }
1369 #define INSTR0F(f, name) __INSTR0(IR3_INSTR_##f, name##_##f, OPC_##name)
1370 #define INSTR0(name) __INSTR0(0, name, OPC_##name)
1371
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) \
1376 { \
1377 struct ir3_instruction *instr = \
1378 ir3_instr_create(block, opc); \
1379 __ssa_dst(instr); \
1380 __ssa_src(instr, a, aflags); \
1381 instr->flags |= flag; \
1382 return instr; \
1383 }
1384 #define INSTR1F(f, name) __INSTR1(IR3_INSTR_##f, name##_##f, OPC_##name)
1385 #define INSTR1(name) __INSTR1(0, name, OPC_##name)
1386
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) \
1392 { \
1393 struct ir3_instruction *instr = \
1394 ir3_instr_create(block, opc); \
1395 __ssa_dst(instr); \
1396 __ssa_src(instr, a, aflags); \
1397 __ssa_src(instr, b, bflags); \
1398 instr->flags |= flag; \
1399 return instr; \
1400 }
1401 #define INSTR2F(f, name) __INSTR2(IR3_INSTR_##f, name##_##f, OPC_##name)
1402 #define INSTR2(name) __INSTR2(0, name, OPC_##name)
1403
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) \
1410 { \
1411 struct ir3_instruction *instr = \
1412 ir3_instr_create2(block, opc, 4); \
1413 __ssa_dst(instr); \
1414 __ssa_src(instr, a, aflags); \
1415 __ssa_src(instr, b, bflags); \
1416 __ssa_src(instr, c, cflags); \
1417 instr->flags |= flag; \
1418 return instr; \
1419 }
1420 #define INSTR3F(f, name) __INSTR3(IR3_INSTR_##f, name##_##f, OPC_##name)
1421 #define INSTR3(name) __INSTR3(0, name, OPC_##name)
1422
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) \
1430 { \
1431 struct ir3_instruction *instr = \
1432 ir3_instr_create2(block, opc, 5); \
1433 __ssa_dst(instr); \
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; \
1439 return instr; \
1440 }
1441 #define INSTR4F(f, name) __INSTR4(IR3_INSTR_##f, name##_##f, OPC_##name)
1442 #define INSTR4(name) __INSTR4(0, name, OPC_##name)
1443
1444 /* cat0 instructions: */
1445 INSTR1(BR)
1446 INSTR0(JUMP)
1447 INSTR1(KILL)
1448 INSTR0(END)
1449 INSTR0(CHSH)
1450 INSTR0(CHMASK)
1451 INSTR1(IF)
1452 INSTR0(ELSE)
1453 INSTR0(ENDIF)
1454
1455 /* cat2 instructions, most 2 src but some 1 src: */
1456 INSTR2(ADD_F)
1457 INSTR2(MIN_F)
1458 INSTR2(MAX_F)
1459 INSTR2(MUL_F)
1460 INSTR1(SIGN_F)
1461 INSTR2(CMPS_F)
1462 INSTR1(ABSNEG_F)
1463 INSTR2(CMPV_F)
1464 INSTR1(FLOOR_F)
1465 INSTR1(CEIL_F)
1466 INSTR1(RNDNE_F)
1467 INSTR1(RNDAZ_F)
1468 INSTR1(TRUNC_F)
1469 INSTR2(ADD_U)
1470 INSTR2(ADD_S)
1471 INSTR2(SUB_U)
1472 INSTR2(SUB_S)
1473 INSTR2(CMPS_U)
1474 INSTR2(CMPS_S)
1475 INSTR2(MIN_U)
1476 INSTR2(MIN_S)
1477 INSTR2(MAX_U)
1478 INSTR2(MAX_S)
1479 INSTR1(ABSNEG_S)
1480 INSTR2(AND_B)
1481 INSTR2(OR_B)
1482 INSTR1(NOT_B)
1483 INSTR2(XOR_B)
1484 INSTR2(CMPV_U)
1485 INSTR2(CMPV_S)
1486 INSTR2(MUL_U24)
1487 INSTR2(MUL_S24)
1488 INSTR2(MULL_U)
1489 INSTR1(BFREV_B)
1490 INSTR1(CLZ_S)
1491 INSTR1(CLZ_B)
1492 INSTR2(SHL_B)
1493 INSTR2(SHR_B)
1494 INSTR2(ASHR_B)
1495 INSTR2(BARY_F)
1496 INSTR2(MGEN_B)
1497 INSTR2(GETBIT_B)
1498 INSTR1(SETRM)
1499 INSTR1(CBITS_B)
1500 INSTR2(SHB)
1501 INSTR2(MSAD)
1502
1503 /* cat3 instructions: */
1504 INSTR3(MAD_U16)
1505 INSTR3(MADSH_U16)
1506 INSTR3(MAD_S16)
1507 INSTR3(MADSH_M16)
1508 INSTR3(MAD_U24)
1509 INSTR3(MAD_S24)
1510 INSTR3(MAD_F16)
1511 INSTR3(MAD_F32)
1512 INSTR3(SEL_B16)
1513 INSTR3(SEL_B32)
1514 INSTR3(SEL_S16)
1515 INSTR3(SEL_S32)
1516 INSTR3(SEL_F16)
1517 INSTR3(SEL_F32)
1518 INSTR3(SAD_S16)
1519 INSTR3(SAD_S32)
1520
1521 /* cat4 instructions: */
1522 INSTR1(RCP)
1523 INSTR1(RSQ)
1524 INSTR1(HRSQ)
1525 INSTR1(LOG2)
1526 INSTR1(HLOG2)
1527 INSTR1(EXP2)
1528 INSTR1(HEXP2)
1529 INSTR1(SIN)
1530 INSTR1(COS)
1531 INSTR1(SQRT)
1532
1533 /* cat5 instructions: */
1534 INSTR1(DSX)
1535 INSTR1(DSXPP_1)
1536 INSTR1(DSY)
1537 INSTR1(DSYPP_1)
1538 INSTR1F(3D, DSX)
1539 INSTR1F(3D, DSY)
1540 INSTR1(RGETPOS)
1541
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)
1546 {
1547 struct ir3_instruction *sam;
1548
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);
1553 if (src0) {
1554 __ssa_src(sam, src0, 0)->wrmask = (1 << (src0->regs_count - 1)) - 1;
1555 }
1556 if (src1) {
1557 __ssa_src(sam, src1, 0)->wrmask =(1 << (src1->regs_count - 1)) - 1;
1558 }
1559 sam->cat5.type = type;
1560
1561 return sam;
1562 }
1563
1564 /* cat6 instructions: */
1565 INSTR2(LDLV)
1566 INSTR3(LDG)
1567 INSTR3(LDL)
1568 INSTR3(LDLW)
1569 INSTR3(STG)
1570 INSTR3(STL)
1571 INSTR3(STLW)
1572 INSTR1(RESINFO)
1573 INSTR1(RESFMT)
1574 INSTR2(ATOMIC_ADD)
1575 INSTR2(ATOMIC_SUB)
1576 INSTR2(ATOMIC_XCHG)
1577 INSTR2(ATOMIC_INC)
1578 INSTR2(ATOMIC_DEC)
1579 INSTR2(ATOMIC_CMPXCHG)
1580 INSTR2(ATOMIC_MIN)
1581 INSTR2(ATOMIC_MAX)
1582 INSTR2(ATOMIC_AND)
1583 INSTR2(ATOMIC_OR)
1584 INSTR2(ATOMIC_XOR)
1585 #if GPU >= 600
1586 INSTR3(STIB);
1587 INSTR2(LDIB);
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)
1599 #elif GPU >= 400
1600 INSTR3(LDGB)
1601 INSTR4(STGB)
1602 INSTR4(STIB)
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)
1614 #endif
1615
1616 INSTR4F(G, STG)
1617
1618 /* cat7 instructions: */
1619 INSTR0(BAR)
1620 INSTR0(FENCE)
1621
1622 /* meta instructions: */
1623 INSTR0(META_TEX_PREFETCH);
1624
1625 /* ************************************************************************* */
1626 /* split this out or find some helper to use.. like main/bitset.h.. */
1627
1628 #include <string.h>
1629 #include "util/bitset.h"
1630
1631 #define MAX_REG 256
1632
1633 typedef BITSET_DECLARE(regmask_t, 2 * MAX_REG);
1634
1635 static inline bool
1636 __regmask_get(regmask_t *regmask, struct ir3_register *reg, unsigned n)
1637 {
1638 if (reg->merged) {
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:
1642 */
1643 if (reg->flags & IR3_REG_HALF) {
1644 return BITSET_TEST(*regmask, n);
1645 } else {
1646 n *= 2;
1647 return BITSET_TEST(*regmask, n) || BITSET_TEST(*regmask, n+1);
1648 }
1649 } else {
1650 /* pre a6xx case, with separate register file for half and full
1651 * precision:
1652 */
1653 if (reg->flags & IR3_REG_HALF)
1654 n += MAX_REG;
1655 return BITSET_TEST(*regmask, n);
1656 }
1657 }
1658
1659 static inline void
1660 __regmask_set(regmask_t *regmask, struct ir3_register *reg, unsigned n)
1661 {
1662 if (reg->merged) {
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:
1666 */
1667 if (reg->flags & IR3_REG_HALF) {
1668 BITSET_SET(*regmask, n);
1669 } else {
1670 n *= 2;
1671 BITSET_SET(*regmask, n);
1672 BITSET_SET(*regmask, n+1);
1673 }
1674 } else {
1675 /* pre a6xx case, with separate register file for half and full
1676 * precision:
1677 */
1678 if (reg->flags & IR3_REG_HALF)
1679 n += MAX_REG;
1680 BITSET_SET(*regmask, n);
1681 }
1682 }
1683
1684 static inline void regmask_init(regmask_t *regmask)
1685 {
1686 memset(regmask, 0, sizeof(*regmask));
1687 }
1688
1689 static inline void regmask_set(regmask_t *regmask, struct ir3_register *reg)
1690 {
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);
1694 } else {
1695 for (unsigned mask = reg->wrmask, n = reg->num; mask; mask >>= 1, n++)
1696 if (mask & 1)
1697 __regmask_set(regmask, reg, n);
1698 }
1699 }
1700
1701 static inline void regmask_or(regmask_t *dst, regmask_t *a, regmask_t *b)
1702 {
1703 unsigned i;
1704 for (i = 0; i < ARRAY_SIZE(*dst); i++)
1705 (*dst)[i] = (*a)[i] | (*b)[i];
1706 }
1707
1708 static inline bool regmask_get(regmask_t *regmask,
1709 struct ir3_register *reg)
1710 {
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))
1714 return true;
1715 } else {
1716 for (unsigned mask = reg->wrmask, n = reg->num; mask; mask >>= 1, n++)
1717 if (mask & 1)
1718 if (__regmask_get(regmask, reg, n))
1719 return true;
1720 }
1721 return false;
1722 }
1723
1724 /* ************************************************************************* */
1725
1726 #endif /* IR3_H_ */