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