nv50/ir: fix off-by-ones in CSE and nvc0 insnCanLoad
[mesa.git] / src / gallium / drivers / nv50 / codegen / nv50_ir.h
1 /*
2 * Copyright 2011 Christoph Bumiller
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef __NV50_IR_H__
24 #define __NV50_IR_H__
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <deque>
30 #include <list>
31 #include <vector>
32
33 #include "nv50_ir_util.h"
34 #include "nv50_ir_graph.h"
35
36 #include "nv50_ir_driver.h"
37
38 namespace nv50_ir {
39
40 enum operation
41 {
42 OP_NOP = 0,
43 OP_PHI,
44 OP_UNION, // unify a new definition and several source values
45 OP_SPLIT, // $r0d -> { $r0, $r1 } ($r0d and $r0/$r1 will be coalesced)
46 OP_MERGE, // opposite of split, e.g. combine 2 32 bit into a 64 bit value
47 OP_CONSTRAINT, // copy values into consecutive registers
48 OP_MOV, // simple copy, no modifiers allowed
49 OP_LOAD,
50 OP_STORE,
51 OP_ADD,
52 OP_SUB,
53 OP_MUL,
54 OP_DIV,
55 OP_MOD,
56 OP_MAD,
57 OP_FMA,
58 OP_SAD, // abs(src0 - src1) + src2
59 OP_ABS,
60 OP_NEG,
61 OP_NOT,
62 OP_AND,
63 OP_OR,
64 OP_XOR,
65 OP_SHL,
66 OP_SHR,
67 OP_MAX,
68 OP_MIN,
69 OP_SAT, // CLAMP(f32, 0.0, 1.0)
70 OP_CEIL,
71 OP_FLOOR,
72 OP_TRUNC,
73 OP_CVT,
74 OP_SET_AND, // dst = (src0 CMP src1) & src2
75 OP_SET_OR,
76 OP_SET_XOR,
77 OP_SET,
78 OP_SELP, // dst = src2 ? src0 : src1
79 OP_SLCT, // dst = (src2 CMP 0) ? src0 : src1
80 OP_RCP,
81 OP_RSQ,
82 OP_LG2,
83 OP_SIN,
84 OP_COS,
85 OP_EX2,
86 OP_EXP, // exponential (base M_E)
87 OP_LOG, // natural logarithm
88 OP_PRESIN,
89 OP_PREEX2,
90 OP_SQRT,
91 OP_POW,
92 OP_BRA,
93 OP_CALL,
94 OP_RET,
95 OP_CONT,
96 OP_BREAK,
97 OP_PRERET,
98 OP_PRECONT,
99 OP_PREBREAK,
100 OP_BRKPT, // breakpoint (not related to loops)
101 OP_JOINAT, // push control flow convergence point
102 OP_JOIN, // converge
103 OP_DISCARD,
104 OP_EXIT,
105 OP_MEMBAR,
106 OP_VFETCH, // indirection 0 in attribute space, indirection 1 is vertex base
107 OP_PFETCH, // fetch base address of vertex src0 (immediate) [+ src1]
108 OP_EXPORT,
109 OP_LINTERP,
110 OP_PINTERP,
111 OP_EMIT, // emit vertex
112 OP_RESTART, // restart primitive
113 OP_TEX,
114 OP_TXB, // texture bias
115 OP_TXL, // texure lod
116 OP_TXF, // texel fetch
117 OP_TXQ, // texture size query
118 OP_TXD, // texture derivatives
119 OP_TXG, // texture gather
120 OP_TEXCSAA,
121 OP_SULD, // surface load
122 OP_SUST, // surface store
123 OP_DFDX,
124 OP_DFDY,
125 OP_RDSV, // read system value
126 OP_WRSV, // write system value
127 OP_PIXLD,
128 OP_QUADOP,
129 OP_QUADON,
130 OP_QUADPOP,
131 OP_POPCNT, // bitcount(src0 & src1)
132 OP_INSBF, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
133 OP_EXTBF,
134 OP_LAST
135 };
136
137 #define NV50_IR_SUBOP_MUL_HIGH 1
138 #define NV50_IR_SUBOP_EMIT_RESTART 1
139 #define NV50_IR_SUBOP_LDC_IL 1
140 #define NV50_IR_SUBOP_LDC_IS 2
141 #define NV50_IR_SUBOP_LDC_ISL 3
142 #define NV50_IR_SUBOP_SHIFT_WRAP 1
143
144 enum DataType
145 {
146 TYPE_NONE,
147 TYPE_U8,
148 TYPE_S8,
149 TYPE_U16,
150 TYPE_S16,
151 TYPE_U32,
152 TYPE_S32,
153 TYPE_U64, // 64 bit operations are only lowered after register allocation
154 TYPE_S64,
155 TYPE_F16,
156 TYPE_F32,
157 TYPE_F64,
158 TYPE_B96,
159 TYPE_B128
160 };
161
162 enum CondCode
163 {
164 CC_FL = 0,
165 CC_NEVER = CC_FL, // when used with FILE_FLAGS
166 CC_LT = 1,
167 CC_EQ = 2,
168 CC_NOT_P = CC_EQ, // when used with FILE_PREDICATE
169 CC_LE = 3,
170 CC_GT = 4,
171 CC_NE = 5,
172 CC_P = CC_NE,
173 CC_GE = 6,
174 CC_TR = 7,
175 CC_ALWAYS = CC_TR,
176 CC_U = 8,
177 CC_LTU = 9,
178 CC_EQU = 10,
179 CC_LEU = 11,
180 CC_GTU = 12,
181 CC_NEU = 13,
182 CC_GEU = 14,
183 CC_NO = 0x10,
184 CC_NC = 0x11,
185 CC_NS = 0x12,
186 CC_NA = 0x13,
187 CC_A = 0x14,
188 CC_S = 0x15,
189 CC_C = 0x16,
190 CC_O = 0x17
191 };
192
193 enum RoundMode
194 {
195 ROUND_N, // nearest
196 ROUND_M, // towards -inf
197 ROUND_Z, // towards 0
198 ROUND_P, // towards +inf
199 ROUND_NI, // nearest integer
200 ROUND_MI, // to integer towards -inf
201 ROUND_ZI, // to integer towards 0
202 ROUND_PI, // to integer towards +inf
203 };
204
205 enum CacheMode
206 {
207 CACHE_CA, // cache at all levels
208 CACHE_WB = CACHE_CA, // cache write back
209 CACHE_CG, // cache at global level
210 CACHE_CS, // cache streaming
211 CACHE_CV, // cache as volatile
212 CACHE_WT = CACHE_CV // cache write-through
213 };
214
215 enum DataFile
216 {
217 FILE_NULL = 0,
218 FILE_GPR,
219 FILE_PREDICATE, // boolean predicate
220 FILE_FLAGS, // zero/sign/carry/overflow bits
221 FILE_ADDRESS,
222 LAST_REGISTER_FILE = FILE_ADDRESS,
223 FILE_IMMEDIATE,
224 FILE_MEMORY_CONST,
225 FILE_SHADER_INPUT,
226 FILE_SHADER_OUTPUT,
227 FILE_MEMORY_GLOBAL,
228 FILE_MEMORY_SHARED,
229 FILE_MEMORY_LOCAL,
230 FILE_SYSTEM_VALUE,
231 DATA_FILE_COUNT
232 };
233
234 enum TexTarget
235 {
236 TEX_TARGET_1D,
237 TEX_TARGET_2D,
238 TEX_TARGET_2D_MS,
239 TEX_TARGET_3D,
240 TEX_TARGET_CUBE,
241 TEX_TARGET_1D_SHADOW,
242 TEX_TARGET_2D_SHADOW,
243 TEX_TARGET_CUBE_SHADOW,
244 TEX_TARGET_1D_ARRAY,
245 TEX_TARGET_2D_ARRAY,
246 TEX_TARGET_2D_MS_ARRAY,
247 TEX_TARGET_CUBE_ARRAY,
248 TEX_TARGET_1D_ARRAY_SHADOW,
249 TEX_TARGET_2D_ARRAY_SHADOW,
250 TEX_TARGET_RECT,
251 TEX_TARGET_RECT_SHADOW,
252 TEX_TARGET_CUBE_ARRAY_SHADOW,
253 TEX_TARGET_BUFFER,
254 TEX_TARGET_COUNT
255 };
256
257 enum SVSemantic
258 {
259 SV_POSITION, // WPOS
260 SV_VERTEX_ID,
261 SV_INSTANCE_ID,
262 SV_INVOCATION_ID,
263 SV_PRIMITIVE_ID,
264 SV_VERTEX_COUNT, // gl_PatchVerticesIn
265 SV_LAYER,
266 SV_VIEWPORT_INDEX,
267 SV_YDIR,
268 SV_FACE,
269 SV_POINT_SIZE,
270 SV_POINT_COORD,
271 SV_CLIP_DISTANCE,
272 SV_SAMPLE_INDEX,
273 SV_TESS_FACTOR,
274 SV_TESS_COORD,
275 SV_TID,
276 SV_CTAID,
277 SV_NTID,
278 SV_GRIDID,
279 SV_NCTAID,
280 SV_LANEID,
281 SV_PHYSID,
282 SV_NPHYSID,
283 SV_CLOCK,
284 SV_LBASE,
285 SV_SBASE,
286 SV_UNDEFINED,
287 SV_LAST
288 };
289
290 class Program;
291 class Function;
292 class BasicBlock;
293
294 class Target;
295
296 class Instruction;
297 class CmpInstruction;
298 class TexInstruction;
299 class FlowInstruction;
300
301 class Value;
302 class LValue;
303 class Symbol;
304 class ImmediateValue;
305
306 struct Storage
307 {
308 DataFile file;
309 int8_t fileIndex; // signed, may be indirect for CONST[]
310 uint8_t size; // this should match the Instruction type's size
311 DataType type; // mainly for pretty printing
312 union {
313 uint64_t u64; // immediate values
314 uint32_t u32;
315 uint16_t u16;
316 uint8_t u8;
317 int64_t s64;
318 int32_t s32;
319 int16_t s16;
320 int8_t s8;
321 float f32;
322 double f64;
323 int32_t offset; // offset from 0 (base of address space)
324 int32_t id; // register id (< 0 if virtual/unassigned, in units <= 4)
325 struct {
326 SVSemantic sv;
327 int index;
328 } sv;
329 } data;
330 };
331
332 // precedence: NOT after SAT after NEG after ABS
333 #define NV50_IR_MOD_ABS (1 << 0)
334 #define NV50_IR_MOD_NEG (1 << 1)
335 #define NV50_IR_MOD_SAT (1 << 2)
336 #define NV50_IR_MOD_NOT (1 << 3)
337 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
338
339 #define NV50_IR_INTERP_MODE_MASK 0x3
340 #define NV50_IR_INTERP_LINEAR (0 << 0)
341 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
342 #define NV50_IR_INTERP_FLAT (2 << 0)
343 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
344 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
345 #define NV50_IR_INTERP_DEFAULT (0 << 2)
346 #define NV50_IR_INTERP_CENTROID (1 << 2)
347 #define NV50_IR_INTERP_OFFSET (2 << 2)
348 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
349
350 // do we really want this to be a class ?
351 class Modifier
352 {
353 public:
354 Modifier() : bits(0) { }
355 Modifier(unsigned int m) : bits(m) { }
356 Modifier(operation op);
357
358 // @return new Modifier applying a after b (asserts if unrepresentable)
359 Modifier operator*(const Modifier) const;
360 Modifier operator*=(const Modifier m) { *this = *this * m; return *this; }
361 Modifier operator==(const Modifier m) const { return m.bits == bits; }
362 Modifier operator!=(const Modifier m) const { return m.bits != bits; }
363
364 inline Modifier operator&(const Modifier m) const { return bits & m.bits; }
365 inline Modifier operator|(const Modifier m) const { return bits | m.bits; }
366 inline Modifier operator^(const Modifier m) const { return bits ^ m.bits; }
367
368 operation getOp() const;
369
370 inline int neg() const { return (bits & NV50_IR_MOD_NEG) ? 1 : 0; }
371 inline int abs() const { return (bits & NV50_IR_MOD_ABS) ? 1 : 0; }
372
373 inline operator bool() const { return bits ? true : false; }
374
375 void applyTo(ImmediateValue &imm) const;
376
377 int print(char *buf, size_t size) const;
378
379 private:
380 uint8_t bits;
381 };
382
383 class ValueRef
384 {
385 public:
386 ValueRef(Value * = NULL);
387 ValueRef(const ValueRef&);
388 ~ValueRef();
389
390 inline bool exists() const { return value != NULL; }
391
392 void set(Value *);
393 void set(const ValueRef&);
394 inline Value *get() const { return value; }
395 inline Value *rep() const;
396
397 inline Instruction *getInsn() const { return insn; }
398 inline void setInsn(Instruction *inst) { insn = inst; }
399
400 inline bool isIndirect(int dim) const { return indirect[dim] >= 0; }
401 inline const ValueRef *getIndirect(int dim) const;
402
403 inline DataFile getFile() const;
404 inline unsigned getSize() const;
405
406 // SSA: return eventual (traverse MOVs) literal value, if it exists
407 bool getImmediate(ImmediateValue&) const;
408
409 public:
410 Modifier mod;
411 int8_t indirect[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
412 uint8_t swizzle;
413
414 bool usedAsPtr; // for printing
415
416 private:
417 Value *value;
418 Instruction *insn;
419 };
420
421 class ValueDef
422 {
423 public:
424 ValueDef(Value * = NULL);
425 ValueDef(const ValueDef&);
426 ~ValueDef();
427
428 inline bool exists() const { return value != NULL; }
429
430 inline Value *get() const { return value; }
431 inline Value *rep() const;
432 void set(Value *);
433 bool mayReplace(const ValueRef &);
434 void replace(const ValueRef &, bool doSet); // replace all uses of the old value
435
436 inline Instruction *getInsn() const { return insn; }
437 inline void setInsn(Instruction *inst) { insn = inst; }
438
439 inline DataFile getFile() const;
440 inline unsigned getSize() const;
441
442 inline void setSSA(LValue *);
443 inline const LValue *preSSA() const;
444
445 private:
446 Value *value; // should make this LValue * ...
447 LValue *origin; // pre SSA value
448 Instruction *insn;
449 };
450
451 class Value
452 {
453 public:
454 Value();
455 virtual ~Value() { }
456
457 virtual Value *clone(ClonePolicy<Function>&) const = 0;
458
459 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const = 0;
460
461 virtual bool equals(const Value *, bool strict = false) const;
462 virtual bool interfers(const Value *) const;
463 virtual bool isUniform() const { return true; }
464
465 inline Value *rep() const { return join; }
466
467 inline Instruction *getUniqueInsn() const;
468 inline Instruction *getInsn() const; // use when uniqueness is certain
469
470 inline int refCount() { return uses.size(); }
471
472 inline LValue *asLValue();
473 inline Symbol *asSym();
474 inline ImmediateValue *asImm();
475 inline const Symbol *asSym() const;
476 inline const ImmediateValue *asImm() const;
477
478 inline bool inFile(DataFile f) { return reg.file == f; }
479
480 static inline Value *get(Iterator&);
481
482 std::list<ValueRef *> uses;
483 std::list<ValueDef *> defs;
484 typedef std::list<ValueRef *>::iterator UseIterator;
485 typedef std::list<ValueRef *>::const_iterator UseCIterator;
486 typedef std::list<ValueDef *>::iterator DefIterator;
487 typedef std::list<ValueDef *>::const_iterator DefCIterator;
488
489 int id;
490 Storage reg;
491
492 // TODO: these should be in LValue:
493 Interval livei;
494 Value *join;
495 };
496
497 class LValue : public Value
498 {
499 public:
500 LValue(Function *, DataFile file);
501 LValue(Function *, LValue *);
502 ~LValue() { }
503
504 virtual bool isUniform() const;
505
506 virtual LValue *clone(ClonePolicy<Function>&) const;
507
508 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
509
510 public:
511 unsigned compMask : 8; // compound/component mask
512 unsigned compound : 1; // used by RA, value involved in split/merge
513 unsigned ssa : 1;
514 unsigned fixedReg : 1; // set & used by RA, earlier just use (id < 0)
515 unsigned noSpill : 1; // do not spill (e.g. if spill temporary already)
516 };
517
518 class Symbol : public Value
519 {
520 public:
521 Symbol(Program *, DataFile file = FILE_MEMORY_CONST, ubyte fileIdx = 0);
522 ~Symbol() { }
523
524 virtual Symbol *clone(ClonePolicy<Function>&) const;
525
526 virtual bool equals(const Value *that, bool strict) const;
527
528 virtual bool isUniform() const;
529
530 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
531
532 // print with indirect values
533 int print(char *, size_t, Value *, Value *, DataType ty = TYPE_NONE) const;
534
535 inline void setFile(DataFile file, ubyte fileIndex = 0)
536 {
537 reg.file = file;
538 reg.fileIndex = fileIndex;
539 }
540
541 inline void setOffset(int32_t offset);
542 inline void setAddress(Symbol *base, int32_t offset);
543 inline void setSV(SVSemantic sv, uint32_t idx = 0);
544
545 inline const Symbol *getBase() const { return baseSym; }
546
547 private:
548 Symbol *baseSym; // array base for Symbols representing array elements
549 };
550
551 class ImmediateValue : public Value
552 {
553 public:
554 ImmediateValue() { }
555 ImmediateValue(Program *, uint32_t);
556 ImmediateValue(Program *, float);
557 ImmediateValue(Program *, double);
558 // NOTE: not added to program with
559 ImmediateValue(const ImmediateValue *, DataType ty);
560 ~ImmediateValue() { };
561
562 virtual ImmediateValue *clone(ClonePolicy<Function>&) const;
563
564 virtual bool equals(const Value *that, bool strict) const;
565
566 // these only work if 'type' is valid (we mostly use untyped literals):
567 bool isInteger(const int ival) const; // ival is cast to this' type
568 bool isNegative() const;
569 bool isPow2() const;
570
571 void applyLog2();
572
573 // for constant folding:
574 ImmediateValue operator+(const ImmediateValue&) const;
575 ImmediateValue operator-(const ImmediateValue&) const;
576 ImmediateValue operator*(const ImmediateValue&) const;
577 ImmediateValue operator/(const ImmediateValue&) const;
578
579 ImmediateValue& operator=(const ImmediateValue&); // only sets value !
580
581 bool compare(CondCode cc, float fval) const;
582
583 virtual int print(char *, size_t, DataType ty = TYPE_NONE) const;
584 };
585
586 class Instruction
587 {
588 public:
589 Instruction();
590 Instruction(Function *, operation, DataType);
591 virtual ~Instruction();
592
593 virtual Instruction *clone(ClonePolicy<Function>&,
594 Instruction * = NULL) const;
595
596 void setDef(int i, Value *);
597 void setSrc(int s, Value *);
598 void setSrc(int s, const ValueRef&);
599 void swapSources(int a, int b);
600 bool setIndirect(int s, int dim, Value *);
601
602 inline ValueRef& src(int s) { return srcs[s]; }
603 inline ValueDef& def(int s) { return defs[s]; }
604 inline const ValueRef& src(int s) const { return srcs[s]; }
605 inline const ValueDef& def(int s) const { return defs[s]; }
606
607 inline Value *getDef(int d) const { return defs[d].get(); }
608 inline Value *getSrc(int s) const { return srcs[s].get(); }
609 inline Value *getIndirect(int s, int dim) const;
610
611 inline bool defExists(unsigned d) const
612 {
613 return d < defs.size() && defs[d].exists();
614 }
615 inline bool srcExists(unsigned s) const
616 {
617 return s < srcs.size() && srcs[s].exists();
618 }
619
620 inline bool constrainedDefs() const;
621
622 bool setPredicate(CondCode ccode, Value *);
623 inline Value *getPredicate() const;
624 bool writesPredicate() const;
625 inline bool isPredicated() const { return predSrc >= 0; }
626
627 inline void setFlagsSrc(int s, Value *);
628 inline void setFlagsDef(int d, Value *);
629
630 unsigned int defCount() const { return defs.size(); };
631 unsigned int defCount(unsigned int mask, bool singleFile = false) const;
632 unsigned int srcCount() const { return srcs.size(); };
633 unsigned int srcCount(unsigned int mask, bool singleFile = false) const;
634
635 // save & remove / set indirect[0,1] and predicate source
636 void takeExtraSources(int s, Value *[3]);
637 void putExtraSources(int s, Value *[3]);
638
639 inline void setType(DataType type) { dType = sType = type; }
640
641 inline void setType(DataType dtype, DataType stype)
642 {
643 dType = dtype;
644 sType = stype;
645 }
646
647 inline bool isPseudo() const { return op < OP_MOV; }
648 bool isDead() const;
649 bool isNop() const;
650 bool isCommutationLegal(const Instruction *) const; // must be adjacent !
651 bool isActionEqual(const Instruction *) const;
652 bool isResultEqual(const Instruction *) const;
653
654 void print() const;
655
656 inline CmpInstruction *asCmp();
657 inline TexInstruction *asTex();
658 inline FlowInstruction *asFlow();
659 inline const TexInstruction *asTex() const;
660 inline const CmpInstruction *asCmp() const;
661 inline const FlowInstruction *asFlow() const;
662
663 public:
664 Instruction *next;
665 Instruction *prev;
666 int id;
667 int serial; // CFG order
668
669 operation op;
670 DataType dType; // destination or defining type
671 DataType sType; // source or secondary type
672 CondCode cc;
673 RoundMode rnd;
674 CacheMode cache;
675
676 uint8_t subOp; // quadop, 1 for mul-high, etc.
677
678 unsigned encSize : 4; // encoding size in bytes
679 unsigned saturate : 1; // to [0.0f, 1.0f]
680 unsigned join : 1; // converge control flow (use OP_JOIN until end)
681 unsigned fixed : 1; // prevent dead code elimination
682 unsigned terminator : 1; // end of basic block
683 unsigned atomic : 1;
684 unsigned ftz : 1; // flush denormal to zero
685 unsigned dnz : 1; // denormals, NaN are zero
686 unsigned ipa : 4; // interpolation mode
687 unsigned lanes : 4;
688 unsigned perPatch : 1;
689 unsigned exit : 1; // terminate program after insn
690
691 int8_t postFactor; // MUL/DIV(if < 0) by 1 << postFactor
692
693 int8_t predSrc;
694 int8_t flagsDef;
695 int8_t flagsSrc;
696
697 BasicBlock *bb;
698
699 protected:
700 std::deque<ValueDef> defs; // no gaps !
701 std::deque<ValueRef> srcs; // no gaps !
702
703 // instruction specific methods:
704 // (don't want to subclass, would need more constructors and memory pools)
705 public:
706 inline void setInterpolate(unsigned int mode) { ipa = mode; }
707
708 unsigned int getInterpMode() const { return ipa & 0x3; }
709 unsigned int getSampleMode() const { return ipa & 0xc; }
710
711 private:
712 void init();
713 };
714
715 enum TexQuery
716 {
717 TXQ_DIMS,
718 TXQ_TYPE,
719 TXQ_SAMPLE_POSITION,
720 TXQ_FILTER,
721 TXQ_LOD,
722 TXQ_WRAP,
723 TXQ_BORDER_COLOUR
724 };
725
726 class TexInstruction : public Instruction
727 {
728 public:
729 class Target
730 {
731 public:
732 Target(TexTarget targ = TEX_TARGET_2D) : target(targ) { }
733
734 const char *getName() const { return descTable[target].name; }
735 unsigned int getArgCount() const { return descTable[target].argc; }
736 unsigned int getDim() const { return descTable[target].dim; }
737 int isArray() const { return descTable[target].array ? 1 : 0; }
738 int isCube() const { return descTable[target].cube ? 1 : 0; }
739 int isShadow() const { return descTable[target].shadow ? 1 : 0; }
740
741 Target& operator=(TexTarget targ)
742 {
743 assert(targ < TEX_TARGET_COUNT);
744 return *this;
745 }
746
747 inline bool operator==(TexTarget targ) const { return target == targ; }
748
749 private:
750 struct Desc
751 {
752 char name[19];
753 uint8_t dim;
754 uint8_t argc;
755 bool array;
756 bool cube;
757 bool shadow;
758 };
759
760 static const struct Desc descTable[TEX_TARGET_COUNT];
761
762 private:
763 enum TexTarget target;
764 };
765
766 public:
767 TexInstruction(Function *, operation);
768 virtual ~TexInstruction();
769
770 virtual TexInstruction *clone(ClonePolicy<Function>&,
771 Instruction * = NULL) const;
772
773 inline void setTexture(Target targ, uint8_t r, uint8_t s)
774 {
775 tex.r = r;
776 tex.s = s;
777 tex.target = targ;
778 }
779
780 inline Value *getIndirectR() const;
781 inline Value *getIndirectS() const;
782
783 public:
784 struct {
785 Target target;
786
787 uint8_t r;
788 int8_t rIndirectSrc;
789 uint8_t s;
790 int8_t sIndirectSrc;
791
792 uint8_t mask;
793 uint8_t gatherComp;
794
795 bool liveOnly; // only execute on live pixels of a quad (optimization)
796 bool levelZero;
797 bool derivAll;
798
799 int8_t useOffsets; // 0, 1, or 4 for textureGatherOffsets
800 int8_t offset[4][3];
801
802 enum TexQuery query;
803 } tex;
804
805 ValueRef dPdx[3];
806 ValueRef dPdy[3];
807 };
808
809 class CmpInstruction : public Instruction
810 {
811 public:
812 CmpInstruction(Function *, operation);
813
814 virtual CmpInstruction *clone(ClonePolicy<Function>&,
815 Instruction * = NULL) const;
816
817 void setCondition(CondCode cond) { setCond = cond; }
818 CondCode getCondition() const { return setCond; }
819
820 public:
821 CondCode setCond;
822 };
823
824 class FlowInstruction : public Instruction
825 {
826 public:
827 FlowInstruction(Function *, operation, void *target);
828
829 virtual FlowInstruction *clone(ClonePolicy<Function>&,
830 Instruction * = NULL) const;
831
832 public:
833 unsigned allWarp : 1;
834 unsigned absolute : 1;
835 unsigned limit : 1;
836 unsigned builtin : 1; // true for calls to emulation code
837
838 union {
839 BasicBlock *bb;
840 int builtin;
841 Function *fn;
842 } target;
843 };
844
845 class BasicBlock
846 {
847 public:
848 BasicBlock(Function *);
849 ~BasicBlock();
850
851 BasicBlock *clone(ClonePolicy<Function>&) const;
852
853 inline int getId() const { return id; }
854 inline unsigned int getInsnCount() const { return numInsns; }
855 inline bool isTerminated() const { return exit && exit->terminator; }
856
857 bool dominatedBy(BasicBlock *bb);
858 inline bool reachableBy(BasicBlock *by, BasicBlock *term);
859
860 // returns mask of conditional out blocks
861 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
862 unsigned int initiatesSimpleConditional() const;
863
864 public:
865 Function *getFunction() const { return func; }
866 Program *getProgram() const { return program; }
867
868 Instruction *getEntry() const { return entry; } // first non-phi instruction
869 Instruction *getPhi() const { return phi; }
870 Instruction *getFirst() const { return phi ? phi : entry; }
871 Instruction *getExit() const { return exit; }
872
873 void insertHead(Instruction *);
874 void insertTail(Instruction *);
875 void insertBefore(Instruction *, Instruction *);
876 void insertAfter(Instruction *, Instruction *);
877 void remove(Instruction *);
878 void permuteAdjacent(Instruction *, Instruction *);
879
880 BasicBlock *idom() const;
881
882 // NOTE: currently does not rebuild the dominator tree
883 BasicBlock *splitBefore(Instruction *, bool attach = true);
884 BasicBlock *splitAfter(Instruction *, bool attach = true);
885
886 DLList& getDF() { return df; }
887 DLList::Iterator iterDF() { return df.iterator(); }
888
889 static inline BasicBlock *get(Iterator&);
890 static inline BasicBlock *get(Graph::Node *);
891
892 public:
893 Graph::Node cfg; // first edge is branch *taken* (the ELSE branch)
894 Graph::Node dom;
895
896 BitSet liveSet;
897 BitSet defSet;
898
899 uint32_t binPos;
900 uint32_t binSize;
901
902 Instruction *joinAt; // for quick reference
903
904 bool explicitCont; // loop headers: true if loop contains continue stmts
905
906 private:
907 int id;
908 DLList df;
909
910 Instruction *phi;
911 Instruction *entry;
912 Instruction *exit;
913
914 unsigned int numInsns;
915
916 private:
917 Function *func;
918 Program *program;
919
920 void splitCommon(Instruction *, BasicBlock *, bool attach);
921 };
922
923 class Function
924 {
925 public:
926 Function(Program *, const char *name, uint32_t label);
927 ~Function();
928
929 static inline Function *get(Graph::Node *node);
930
931 inline Program *getProgram() const { return prog; }
932 inline const char *getName() const { return name; }
933 inline int getId() const { return id; }
934 inline uint32_t getLabel() const { return label; }
935
936 void print();
937 void printLiveIntervals() const;
938 void printCFGraph(const char *filePath);
939
940 bool setEntry(BasicBlock *);
941 bool setExit(BasicBlock *);
942
943 unsigned int orderInstructions(ArrayList&);
944
945 inline void add(BasicBlock *bb, int& id) { allBBlocks.insert(bb, id); }
946 inline void add(Instruction *insn, int& id) { allInsns.insert(insn, id); }
947 inline void add(LValue *lval, int& id) { allLValues.insert(lval, id); }
948
949 inline LValue *getLValue(int id);
950
951 void buildLiveSets();
952 void buildDefSets();
953 bool convertToSSA();
954
955 public:
956 std::deque<ValueDef> ins;
957 std::deque<ValueRef> outs;
958 std::deque<Value *> clobbers;
959
960 Graph cfg;
961 Graph::Node *cfgExit;
962 Graph *domTree;
963 Graph::Node call; // node in the call graph
964
965 BasicBlock **bbArray; // BBs in emission order
966 int bbCount;
967
968 unsigned int loopNestingBound;
969 int regClobberMax;
970
971 uint32_t binPos;
972 uint32_t binSize;
973
974 Value *stackPtr;
975
976 uint32_t tlsBase; // base address for l[] space (if no stack pointer is used)
977 uint32_t tlsSize;
978
979 ArrayList allBBlocks;
980 ArrayList allInsns;
981 ArrayList allLValues;
982
983 private:
984 void buildLiveSetsPreSSA(BasicBlock *, const int sequence);
985 void buildDefSetsPreSSA(BasicBlock *bb, const int seq);
986
987 private:
988 uint32_t label;
989 int id;
990 const char *const name;
991 Program *prog;
992 };
993
994 enum CGStage
995 {
996 CG_STAGE_PRE_SSA,
997 CG_STAGE_SSA, // expected directly before register allocation
998 CG_STAGE_POST_RA
999 };
1000
1001 class Program
1002 {
1003 public:
1004 enum Type
1005 {
1006 TYPE_VERTEX,
1007 TYPE_TESSELLATION_CONTROL,
1008 TYPE_TESSELLATION_EVAL,
1009 TYPE_GEOMETRY,
1010 TYPE_FRAGMENT,
1011 TYPE_COMPUTE
1012 };
1013
1014 Program(Type type, Target *targ);
1015 ~Program();
1016
1017 void print();
1018
1019 Type getType() const { return progType; }
1020
1021 inline void add(Function *fn, int& id) { allFuncs.insert(fn, id); }
1022 inline void del(Function *fn, int& id) { allFuncs.remove(id); }
1023 inline void add(Value *rval, int& id) { allRValues.insert(rval, id); }
1024
1025 bool makeFromTGSI(struct nv50_ir_prog_info *);
1026 bool makeFromSM4(struct nv50_ir_prog_info *);
1027 bool convertToSSA();
1028 bool optimizeSSA(int level);
1029 bool optimizePostRA(int level);
1030 bool registerAllocation();
1031 bool emitBinary(struct nv50_ir_prog_info *);
1032
1033 const Target *getTarget() const { return target; }
1034
1035 private:
1036 void emitSymbolTable(struct nv50_ir_prog_info *);
1037
1038 Type progType;
1039 Target *target;
1040
1041 public:
1042 Function *main;
1043 Graph calls;
1044
1045 ArrayList allFuncs;
1046 ArrayList allRValues;
1047
1048 uint32_t *code;
1049 uint32_t binSize;
1050 uint32_t tlsSize; // size required for FILE_MEMORY_LOCAL
1051
1052 int maxGPR;
1053
1054 MemoryPool mem_Instruction;
1055 MemoryPool mem_CmpInstruction;
1056 MemoryPool mem_TexInstruction;
1057 MemoryPool mem_FlowInstruction;
1058 MemoryPool mem_LValue;
1059 MemoryPool mem_Symbol;
1060 MemoryPool mem_ImmediateValue;
1061
1062 uint32_t dbgFlags;
1063
1064 void releaseInstruction(Instruction *);
1065 void releaseValue(Value *);
1066 };
1067
1068 // TODO: add const version
1069 class Pass
1070 {
1071 public:
1072 bool run(Program *, bool ordered = false, bool skipPhi = false);
1073 bool run(Function *, bool ordered = false, bool skipPhi = false);
1074
1075 private:
1076 // return false to continue with next entity on next higher level
1077 virtual bool visit(Function *) { return true; }
1078 virtual bool visit(BasicBlock *) { return true; }
1079 virtual bool visit(Instruction *) { return false; }
1080
1081 bool doRun(Program *, bool ordered, bool skipPhi);
1082 bool doRun(Function *, bool ordered, bool skipPhi);
1083
1084 protected:
1085 bool err;
1086 Function *func;
1087 Program *prog;
1088 };
1089
1090 // =============================================================================
1091
1092 #include "nv50_ir_inlines.h"
1093
1094 } // namespace nv50_ir
1095
1096 #endif // __NV50_IR_H__