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