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