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