2 * Copyright 2011 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
33 #include "codegen/nv50_ir_util.h"
34 #include "codegen/nv50_ir_graph.h"
36 #include "codegen/nv50_ir_driver.h"
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
51 OP_ADD
, // NOTE: add u64 + u32 is legal for targets w/o 64-bit integer adds
58 OP_SAD
, // abs(src0 - src1) + src2
69 OP_SAT
, // CLAMP(f32, 0.0, 1.0)
74 OP_SET_AND
, // dst = (src0 CMP src1) & src2
78 OP_SELP
, // dst = src2 ? src0 : src1
79 OP_SLCT
, // dst = (src2 CMP 0) ? src0 : src1
86 OP_EXP
, // exponential (base M_E)
87 OP_LOG
, // natural logarithm
100 OP_BRKPT
, // breakpoint (not related to loops)
101 OP_JOINAT
, // push control flow convergence point
105 OP_MEMBAR
, // memory barrier (mfence, lfence, sfence)
106 OP_VFETCH
, // indirection 0 in attribute space, indirection 1 is vertex base
107 OP_PFETCH
, // fetch base address of vertex src0 (immediate) [+ src1]
111 OP_EMIT
, // emit vertex
112 OP_RESTART
, // restart primitive
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
, // texture op for coverage sampling
121 OP_TEXPREP
, // turn cube map array into 2d array coordinates
122 OP_SULDB
, // surface load (raw)
123 OP_SULDP
, // surface load (formatted)
124 OP_SUSTB
, // surface store (raw)
125 OP_SUSTP
, // surface store (formatted)
127 OP_SUREDP
, // surface reduction (atomic op)
128 OP_SULEA
, // surface load effective address
129 OP_SUBFM
, // surface bitfield manipulation
130 OP_SUCLAMP
, // clamp surface coordinates
131 OP_SUEAU
, // surface effective address
132 OP_MADSP
, // special integer multiply-add
133 OP_TEXBAR
, // texture dependency barrier
136 OP_RDSV
, // read system value
137 OP_WRSV
, // write system value
141 OP_POPCNT
, // bitcount(src0 & src1)
142 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
143 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
144 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
146 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
147 OP_VADD
, // byte/word vector operations
156 OP_CCTL
, // cache control
160 // various instruction-specific modifier definitions Instruction::subOp
161 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
162 #define NV50_IR_SUBOP_MUL_HIGH 1
163 #define NV50_IR_SUBOP_EMIT_RESTART 1
164 #define NV50_IR_SUBOP_LDC_IL 1
165 #define NV50_IR_SUBOP_LDC_IS 2
166 #define NV50_IR_SUBOP_LDC_ISL 3
167 #define NV50_IR_SUBOP_SHIFT_WRAP 1
168 #define NV50_IR_SUBOP_EMU_PRERET 1
169 #define NV50_IR_SUBOP_TEXBAR(n) n
170 #define NV50_IR_SUBOP_MOV_FINAL 1
171 #define NV50_IR_SUBOP_EXTBF_REV 1
172 #define NV50_IR_SUBOP_PERMT_F4E 1
173 #define NV50_IR_SUBOP_PERMT_B4E 2
174 #define NV50_IR_SUBOP_PERMT_RC8 3
175 #define NV50_IR_SUBOP_PERMT_ECL 4
176 #define NV50_IR_SUBOP_PERMT_ECR 5
177 #define NV50_IR_SUBOP_PERMT_RC16 6
178 #define NV50_IR_SUBOP_BAR_SYNC 0
179 #define NV50_IR_SUBOP_BAR_ARRIVE 1
180 #define NV50_IR_SUBOP_BAR_RED_AND 2
181 #define NV50_IR_SUBOP_BAR_RED_OR 3
182 #define NV50_IR_SUBOP_BAR_RED_POPC 4
183 #define NV50_IR_SUBOP_MEMBAR_L 1
184 #define NV50_IR_SUBOP_MEMBAR_S 2
185 #define NV50_IR_SUBOP_MEMBAR_M 3
186 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
187 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
188 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
189 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
190 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
191 #define NV50_IR_SUBOP_MEMBAR(d,s) \
192 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
193 #define NV50_IR_SUBOP_ATOM_ADD 0
194 #define NV50_IR_SUBOP_ATOM_MIN 1
195 #define NV50_IR_SUBOP_ATOM_MAX 2
196 #define NV50_IR_SUBOP_ATOM_INC 3
197 #define NV50_IR_SUBOP_ATOM_DEC 4
198 #define NV50_IR_SUBOP_ATOM_AND 5
199 #define NV50_IR_SUBOP_ATOM_OR 6
200 #define NV50_IR_SUBOP_ATOM_XOR 7
201 #define NV50_IR_SUBOP_ATOM_CAS 8
202 #define NV50_IR_SUBOP_ATOM_EXCH 9
203 #define NV50_IR_SUBOP_CCTL_IV 5
204 #define NV50_IR_SUBOP_CCTL_IVALL 6
205 #define NV50_IR_SUBOP_SUST_IGN 0
206 #define NV50_IR_SUBOP_SUST_TRAP 1
207 #define NV50_IR_SUBOP_SUST_SDCL 3
208 #define NV50_IR_SUBOP_SULD_ZERO 0
209 #define NV50_IR_SUBOP_SULD_TRAP 1
210 #define NV50_IR_SUBOP_SULD_SDCL 3
211 #define NV50_IR_SUBOP_SUBFM_3D 1
212 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
213 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
214 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
215 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
216 #define NV50_IR_SUBOP_MADSP_SD 0xffff
217 // Yes, we could represent those with DataType.
218 // Or put the type into operation and have a couple 1000 values in that enum.
219 // This will have to do for now.
220 // The bitfields are supposed to correspond to nve4 ISA.
221 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
222 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
223 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
224 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
225 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
236 TYPE_U64
, // 64 bit operations are only lowered after register allocation
248 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
251 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
279 ROUND_M
, // towards -inf
280 ROUND_Z
, // towards 0
281 ROUND_P
, // towards +inf
282 ROUND_NI
, // nearest integer
283 ROUND_MI
, // to integer towards -inf
284 ROUND_ZI
, // to integer towards 0
285 ROUND_PI
, // to integer towards +inf
290 CACHE_CA
, // cache at all levels
291 CACHE_WB
= CACHE_CA
, // cache write back
292 CACHE_CG
, // cache at global level
293 CACHE_CS
, // cache streaming
294 CACHE_CV
, // cache as volatile
295 CACHE_WT
= CACHE_CV
// cache write-through
302 FILE_PREDICATE
, // boolean predicate
303 FILE_FLAGS
, // zero/sign/carry/overflow bits
305 LAST_REGISTER_FILE
= FILE_ADDRESS
,
324 TEX_TARGET_1D_SHADOW
,
325 TEX_TARGET_2D_SHADOW
,
326 TEX_TARGET_CUBE_SHADOW
,
329 TEX_TARGET_2D_MS_ARRAY
,
330 TEX_TARGET_CUBE_ARRAY
,
331 TEX_TARGET_1D_ARRAY_SHADOW
,
332 TEX_TARGET_2D_ARRAY_SHADOW
,
334 TEX_TARGET_RECT_SHADOW
,
335 TEX_TARGET_CUBE_ARRAY_SHADOW
,
347 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
380 class CmpInstruction
;
381 class TexInstruction
;
382 class FlowInstruction
;
387 class ImmediateValue
;
392 int8_t fileIndex
; // signed, may be indirect for CONST[]
393 uint8_t size
; // this should match the Instruction type's size
394 DataType type
; // mainly for pretty printing
396 uint64_t u64
; // immediate values
406 int32_t offset
; // offset from 0 (base of address space)
407 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
415 // precedence: NOT after SAT after NEG after ABS
416 #define NV50_IR_MOD_ABS (1 << 0)
417 #define NV50_IR_MOD_NEG (1 << 1)
418 #define NV50_IR_MOD_SAT (1 << 2)
419 #define NV50_IR_MOD_NOT (1 << 3)
420 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
422 #define NV50_IR_INTERP_MODE_MASK 0x3
423 #define NV50_IR_INTERP_LINEAR (0 << 0)
424 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
425 #define NV50_IR_INTERP_FLAT (2 << 0)
426 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
427 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
428 #define NV50_IR_INTERP_DEFAULT (0 << 2)
429 #define NV50_IR_INTERP_CENTROID (1 << 2)
430 #define NV50_IR_INTERP_OFFSET (2 << 2)
431 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
433 // do we really want this to be a class ?
437 Modifier() : bits(0) { }
438 Modifier(unsigned int m
) : bits(m
) { }
439 Modifier(operation op
);
441 // @return new Modifier applying a after b (asserts if unrepresentable)
442 Modifier
operator*(const Modifier
) const;
443 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
444 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
445 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
447 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
448 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
449 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
451 operation
getOp() const;
453 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
454 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
456 inline operator bool() const { return bits
? true : false; }
458 void applyTo(ImmediateValue
&imm
) const;
460 int print(char *buf
, size_t size
) const;
469 ValueRef(Value
* = NULL
);
470 ValueRef(const ValueRef
&);
473 inline bool exists() const { return value
!= NULL
; }
476 void set(const ValueRef
&);
477 inline Value
*get() const { return value
; }
478 inline Value
*rep() const;
480 inline Instruction
*getInsn() const { return insn
; }
481 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
483 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
484 inline const ValueRef
*getIndirect(int dim
) const;
486 inline DataFile
getFile() const;
487 inline unsigned getSize() const;
489 // SSA: return eventual (traverse MOVs) literal value, if it exists
490 bool getImmediate(ImmediateValue
&) const;
494 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
497 bool usedAsPtr
; // for printing
507 ValueDef(Value
* = NULL
);
508 ValueDef(const ValueDef
&);
511 inline bool exists() const { return value
!= NULL
; }
513 inline Value
*get() const { return value
; }
514 inline Value
*rep() const;
516 bool mayReplace(const ValueRef
&);
517 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
519 inline Instruction
*getInsn() const { return insn
; }
520 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
522 inline DataFile
getFile() const;
523 inline unsigned getSize() const;
525 inline void setSSA(LValue
*);
526 inline const LValue
*preSSA() const;
529 Value
*value
; // should make this LValue * ...
530 LValue
*origin
; // pre SSA value
540 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
542 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
544 virtual bool equals(const Value
*, bool strict
= false) const;
545 virtual bool interfers(const Value
*) const;
546 virtual bool isUniform() const { return true; }
548 inline Value
*rep() const { return join
; }
550 inline Instruction
*getUniqueInsn() const;
551 inline Instruction
*getInsn() const; // use when uniqueness is certain
553 inline int refCount() { return uses
.size(); }
555 inline LValue
*asLValue();
556 inline Symbol
*asSym();
557 inline ImmediateValue
*asImm();
558 inline const Symbol
*asSym() const;
559 inline const ImmediateValue
*asImm() const;
561 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
563 static inline Value
*get(Iterator
&);
565 std::list
<ValueRef
*> uses
;
566 std::list
<ValueDef
*> defs
;
567 typedef std::list
<ValueRef
*>::iterator UseIterator
;
568 typedef std::list
<ValueRef
*>::const_iterator UseCIterator
;
569 typedef std::list
<ValueDef
*>::iterator DefIterator
;
570 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
575 // TODO: these should be in LValue:
580 class LValue
: public Value
583 LValue(Function
*, DataFile file
);
584 LValue(Function
*, LValue
*);
587 virtual bool isUniform() const;
589 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
591 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
594 unsigned compMask
: 8; // compound/component mask
595 unsigned compound
: 1; // used by RA, value involved in split/merge
597 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
598 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
601 class Symbol
: public Value
604 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
607 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
609 virtual bool equals(const Value
*that
, bool strict
) const;
611 virtual bool isUniform() const;
613 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
615 // print with indirect values
616 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
618 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
621 reg
.fileIndex
= fileIndex
;
624 inline void setOffset(int32_t offset
);
625 inline void setAddress(Symbol
*base
, int32_t offset
);
626 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
628 inline const Symbol
*getBase() const { return baseSym
; }
631 Symbol
*baseSym
; // array base for Symbols representing array elements
634 class ImmediateValue
: public Value
638 ImmediateValue(Program
*, uint32_t);
639 ImmediateValue(Program
*, float);
640 ImmediateValue(Program
*, double);
641 // NOTE: not added to program with
642 ImmediateValue(const ImmediateValue
*, DataType ty
);
643 ~ImmediateValue() { };
645 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
647 virtual bool equals(const Value
*that
, bool strict
) const;
649 // these only work if 'type' is valid (we mostly use untyped literals):
650 bool isInteger(const int ival
) const; // ival is cast to this' type
651 bool isNegative() const;
656 // for constant folding:
657 ImmediateValue
operator+(const ImmediateValue
&) const;
658 ImmediateValue
operator-(const ImmediateValue
&) const;
659 ImmediateValue
operator*(const ImmediateValue
&) const;
660 ImmediateValue
operator/(const ImmediateValue
&) const;
662 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
664 bool compare(CondCode cc
, float fval
) const;
666 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
673 Instruction(Function
*, operation
, DataType
);
674 virtual ~Instruction();
676 virtual Instruction
*clone(ClonePolicy
<Function
>&,
677 Instruction
* = NULL
) const;
679 void setDef(int i
, Value
*);
680 void setSrc(int s
, Value
*);
681 void setSrc(int s
, const ValueRef
&);
682 void swapSources(int a
, int b
);
683 void moveSources(int s
, int delta
);
684 bool setIndirect(int s
, int dim
, Value
*);
686 inline ValueRef
& src(int s
) { return srcs
[s
]; }
687 inline ValueDef
& def(int s
) { return defs
[s
]; }
688 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
689 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
691 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
692 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
693 inline Value
*getIndirect(int s
, int dim
) const;
695 inline bool defExists(unsigned d
) const
697 return d
< defs
.size() && defs
[d
].exists();
699 inline bool srcExists(unsigned s
) const
701 return s
< srcs
.size() && srcs
[s
].exists();
704 inline bool constrainedDefs() const;
706 bool setPredicate(CondCode ccode
, Value
*);
707 inline Value
*getPredicate() const;
708 bool writesPredicate() const;
709 inline bool isPredicated() const { return predSrc
>= 0; }
711 inline void setFlagsSrc(int s
, Value
*);
712 inline void setFlagsDef(int d
, Value
*);
713 inline bool usesFlags() const { return flagsSrc
>= 0; }
715 unsigned int defCount() const { return defs
.size(); };
716 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
717 unsigned int srcCount() const { return srcs
.size(); };
718 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
720 // save & remove / set indirect[0,1] and predicate source
721 void takeExtraSources(int s
, Value
*[3]);
722 void putExtraSources(int s
, Value
*[3]);
724 inline void setType(DataType type
) { dType
= sType
= type
; }
726 inline void setType(DataType dtype
, DataType stype
)
732 inline bool isPseudo() const { return op
< OP_MOV
; }
735 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
736 bool isActionEqual(const Instruction
*) const;
737 bool isResultEqual(const Instruction
*) const;
741 inline CmpInstruction
*asCmp();
742 inline TexInstruction
*asTex();
743 inline FlowInstruction
*asFlow();
744 inline const TexInstruction
*asTex() const;
745 inline const CmpInstruction
*asCmp() const;
746 inline const FlowInstruction
*asFlow() const;
752 int serial
; // CFG order
755 DataType dType
; // destination or defining type
756 DataType sType
; // source or secondary type
761 uint16_t subOp
; // quadop, 1 for mul-high, etc.
763 unsigned encSize
: 4; // encoding size in bytes
764 unsigned saturate
: 1; // to [0.0f, 1.0f]
765 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
766 unsigned fixed
: 1; // prevent dead code elimination
767 unsigned terminator
: 1; // end of basic block
768 unsigned ftz
: 1; // flush denormal to zero
769 unsigned dnz
: 1; // denormals, NaN are zero
770 unsigned ipa
: 4; // interpolation mode
772 unsigned perPatch
: 1;
773 unsigned exit
: 1; // terminate program after insn
774 unsigned mask
: 4; // for vector ops
776 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
782 uint8_t sched
; // scheduling data (NOTE: maybe move to separate storage)
787 std::deque
<ValueDef
> defs
; // no gaps !
788 std::deque
<ValueRef
> srcs
; // no gaps !
790 // instruction specific methods:
791 // (don't want to subclass, would need more constructors and memory pools)
793 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
795 unsigned int getInterpMode() const { return ipa
& 0x3; }
796 unsigned int getSampleMode() const { return ipa
& 0xc; }
813 class TexInstruction
: public Instruction
819 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
821 const char *getName() const { return descTable
[target
].name
; }
822 unsigned int getArgCount() const { return descTable
[target
].argc
; }
823 unsigned int getDim() const { return descTable
[target
].dim
; }
824 int isArray() const { return descTable
[target
].array
? 1 : 0; }
825 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
826 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
828 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
830 Target
& operator=(TexTarget targ
)
832 assert(targ
< TEX_TARGET_COUNT
);
837 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
838 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
840 enum TexTarget
getEnum() const { return target
; }
853 static const struct Desc descTable
[TEX_TARGET_COUNT
];
856 enum TexTarget target
;
860 TexInstruction(Function
*, operation
);
861 virtual ~TexInstruction();
863 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
864 Instruction
* = NULL
) const;
866 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
873 void setIndirectR(Value
*);
874 void setIndirectS(Value
*);
875 inline Value
*getIndirectR() const;
876 inline Value
*getIndirectS() const;
890 bool liveOnly
; // only execute on live pixels of a quad (optimization)
894 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
904 class CmpInstruction
: public Instruction
907 CmpInstruction(Function
*, operation
);
909 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
910 Instruction
* = NULL
) const;
912 void setCondition(CondCode cond
) { setCond
= cond
; }
913 CondCode
getCondition() const { return setCond
; }
919 class FlowInstruction
: public Instruction
922 FlowInstruction(Function
*, operation
, void *target
);
924 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
925 Instruction
* = NULL
) const;
928 unsigned allWarp
: 1;
929 unsigned absolute
: 1;
931 unsigned builtin
: 1; // true for calls to emulation code
932 unsigned indirect
: 1; // target in src(0)
944 BasicBlock(Function
*);
947 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
949 inline int getId() const { return id
; }
950 inline unsigned int getInsnCount() const { return numInsns
; }
951 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
953 bool dominatedBy(BasicBlock
*bb
);
954 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
956 // returns mask of conditional out blocks
957 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
958 unsigned int initiatesSimpleConditional() const;
961 Function
*getFunction() const { return func
; }
962 Program
*getProgram() const { return program
; }
964 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
965 Instruction
*getPhi() const { return phi
; }
966 Instruction
*getFirst() const { return phi
? phi
: entry
; }
967 Instruction
*getExit() const { return exit
; }
969 void insertHead(Instruction
*);
970 void insertTail(Instruction
*);
971 void insertBefore(Instruction
*, Instruction
*);
972 void insertAfter(Instruction
*, Instruction
*);
973 void remove(Instruction
*);
974 void permuteAdjacent(Instruction
*, Instruction
*);
976 BasicBlock
*idom() const;
978 // NOTE: currently does not rebuild the dominator tree
979 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
980 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
982 DLList
& getDF() { return df
; }
983 DLList::Iterator
iterDF() { return df
.iterator(); }
985 static inline BasicBlock
*get(Iterator
&);
986 static inline BasicBlock
*get(Graph::Node
*);
989 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
998 Instruction
*joinAt
; // for quick reference
1000 bool explicitCont
; // loop headers: true if loop contains continue stmts
1010 unsigned int numInsns
;
1016 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1022 Function(Program
*, const char *name
, uint32_t label
);
1025 static inline Function
*get(Graph::Node
*node
);
1027 inline Program
*getProgram() const { return prog
; }
1028 inline const char *getName() const { return name
; }
1029 inline int getId() const { return id
; }
1030 inline uint32_t getLabel() const { return label
; }
1033 void printLiveIntervals() const;
1034 void printCFGraph(const char *filePath
);
1036 bool setEntry(BasicBlock
*);
1037 bool setExit(BasicBlock
*);
1039 unsigned int orderInstructions(ArrayList
&);
1041 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1042 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1043 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1045 inline LValue
*getLValue(int id
);
1047 void buildLiveSets();
1048 void buildDefSets();
1049 bool convertToSSA();
1052 std::deque
<ValueDef
> ins
;
1053 std::deque
<ValueRef
> outs
;
1054 std::deque
<Value
*> clobbers
;
1057 Graph::Node
*cfgExit
;
1059 Graph::Node call
; // node in the call graph
1061 BasicBlock
**bbArray
; // BBs in emission order
1064 unsigned int loopNestingBound
;
1072 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1075 ArrayList allBBlocks
;
1077 ArrayList allLValues
;
1080 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1081 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1086 const char *const name
;
1093 CG_STAGE_SSA
, // expected directly before register allocation
1103 TYPE_TESSELLATION_CONTROL
,
1104 TYPE_TESSELLATION_EVAL
,
1110 Program(Type type
, Target
*targ
);
1115 Type
getType() const { return progType
; }
1117 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1118 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1119 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1121 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1122 bool makeFromSM4(struct nv50_ir_prog_info
*);
1123 bool convertToSSA();
1124 bool optimizeSSA(int level
);
1125 bool optimizePostRA(int level
);
1126 bool registerAllocation();
1127 bool emitBinary(struct nv50_ir_prog_info
*);
1129 const Target
*getTarget() const { return target
; }
1132 void emitSymbolTable(struct nv50_ir_prog_info
*);
1142 ArrayList allRValues
;
1146 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1150 MemoryPool mem_Instruction
;
1151 MemoryPool mem_CmpInstruction
;
1152 MemoryPool mem_TexInstruction
;
1153 MemoryPool mem_FlowInstruction
;
1154 MemoryPool mem_LValue
;
1155 MemoryPool mem_Symbol
;
1156 MemoryPool mem_ImmediateValue
;
1161 void *targetPriv
; // e.g. to carry information between passes
1163 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1165 void releaseInstruction(Instruction
*);
1166 void releaseValue(Value
*);
1169 // TODO: add const version
1173 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1174 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1177 // return false to continue with next entity on next higher level
1178 virtual bool visit(Function
*) { return true; }
1179 virtual bool visit(BasicBlock
*) { return true; }
1180 virtual bool visit(Instruction
*) { return false; }
1182 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1183 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1191 // =============================================================================
1193 #include "codegen/nv50_ir_inlines.h"
1195 } // namespace nv50_ir
1197 #endif // __NV50_IR_H__