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/unordered_set.h"
34 #include "codegen/nv50_ir_util.h"
35 #include "codegen/nv50_ir_graph.h"
37 #include "codegen/nv50_ir_driver.h"
45 OP_UNION
, // unify a new definition and several source values
46 OP_SPLIT
, // $r0d -> { $r0, $r1 } ($r0d and $r0/$r1 will be coalesced)
47 OP_MERGE
, // opposite of split, e.g. combine 2 32 bit into a 64 bit value
48 OP_CONSTRAINT
, // copy values into consecutive registers
49 OP_MOV
, // simple copy, no modifiers allowed
52 OP_ADD
, // NOTE: add u64 + u32 is legal for targets w/o 64-bit integer adds
59 OP_SAD
, // abs(src0 - src1) + src2
70 OP_SAT
, // CLAMP(f32, 0.0, 1.0)
75 OP_SET_AND
, // dst = (src0 CMP src1) & src2
79 OP_SELP
, // dst = src2 ? src0 : src1
80 OP_SLCT
, // dst = (src2 CMP 0) ? src0 : src1
87 OP_EXP
, // exponential (base M_E)
88 OP_LOG
, // natural logarithm
101 OP_BRKPT
, // breakpoint (not related to loops)
102 OP_JOINAT
, // push control flow convergence point
106 OP_MEMBAR
, // memory barrier (mfence, lfence, sfence)
107 OP_VFETCH
, // indirection 0 in attribute space, indirection 1 is vertex base
108 OP_PFETCH
, // fetch base address of vertex src0 (immediate) [+ src1]
109 OP_AFETCH
, // fetch base address of shader input (a[%r1+0x10])
113 OP_EMIT
, // emit vertex
114 OP_RESTART
, // restart primitive
116 OP_TXB
, // texture bias
117 OP_TXL
, // texure lod
118 OP_TXF
, // texel fetch
119 OP_TXQ
, // texture size query
120 OP_TXD
, // texture derivatives
121 OP_TXG
, // texture gather
122 OP_TXLQ
, // texture query lod
123 OP_TEXCSAA
, // texture op for coverage sampling
124 OP_TEXPREP
, // turn cube map array into 2d array coordinates
125 OP_SULDB
, // surface load (raw)
126 OP_SULDP
, // surface load (formatted)
127 OP_SUSTB
, // surface store (raw)
128 OP_SUSTP
, // surface store (formatted)
130 OP_SUREDP
, // surface reduction (atomic op)
131 OP_SULEA
, // surface load effective address
132 OP_SUBFM
, // surface bitfield manipulation
133 OP_SUCLAMP
, // clamp surface coordinates
134 OP_SUEAU
, // surface effective address
135 OP_SUQ
, // surface query
136 OP_MADSP
, // special integer multiply-add
137 OP_TEXBAR
, // texture dependency barrier
140 OP_RDSV
, // read system value
141 OP_WRSV
, // write system value
142 OP_PIXLD
, // get info about raster object or surfaces
146 OP_POPCNT
, // bitcount(src0 & src1)
147 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
148 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
149 OP_BFIND
, // find highest/lowest set bit
150 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
152 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
153 OP_VADD
, // byte/word vector operations
162 OP_CCTL
, // cache control
163 OP_SHFL
, // warp shuffle
165 OP_BUFQ
, // buffer query
169 // various instruction-specific modifier definitions Instruction::subOp
170 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
171 #define NV50_IR_SUBOP_MUL_HIGH 1
172 #define NV50_IR_SUBOP_EMIT_RESTART 1
173 #define NV50_IR_SUBOP_LDC_IL 1
174 #define NV50_IR_SUBOP_LDC_IS 2
175 #define NV50_IR_SUBOP_LDC_ISL 3
176 #define NV50_IR_SUBOP_SHIFT_WRAP 1
177 #define NV50_IR_SUBOP_EMU_PRERET 1
178 #define NV50_IR_SUBOP_TEXBAR(n) n
179 #define NV50_IR_SUBOP_MOV_FINAL 1
180 #define NV50_IR_SUBOP_EXTBF_REV 1
181 #define NV50_IR_SUBOP_BFIND_SAMT 1
182 #define NV50_IR_SUBOP_RCPRSQ_64H 1
183 #define NV50_IR_SUBOP_PERMT_F4E 1
184 #define NV50_IR_SUBOP_PERMT_B4E 2
185 #define NV50_IR_SUBOP_PERMT_RC8 3
186 #define NV50_IR_SUBOP_PERMT_ECL 4
187 #define NV50_IR_SUBOP_PERMT_ECR 5
188 #define NV50_IR_SUBOP_PERMT_RC16 6
189 #define NV50_IR_SUBOP_BAR_SYNC 0
190 #define NV50_IR_SUBOP_BAR_ARRIVE 1
191 #define NV50_IR_SUBOP_BAR_RED_AND 2
192 #define NV50_IR_SUBOP_BAR_RED_OR 3
193 #define NV50_IR_SUBOP_BAR_RED_POPC 4
194 #define NV50_IR_SUBOP_MEMBAR_L 1
195 #define NV50_IR_SUBOP_MEMBAR_S 2
196 #define NV50_IR_SUBOP_MEMBAR_M 3
197 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
198 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
199 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
200 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
201 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
202 #define NV50_IR_SUBOP_MEMBAR(d,s) \
203 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
204 #define NV50_IR_SUBOP_ATOM_ADD 0
205 #define NV50_IR_SUBOP_ATOM_MIN 1
206 #define NV50_IR_SUBOP_ATOM_MAX 2
207 #define NV50_IR_SUBOP_ATOM_INC 3
208 #define NV50_IR_SUBOP_ATOM_DEC 4
209 #define NV50_IR_SUBOP_ATOM_AND 5
210 #define NV50_IR_SUBOP_ATOM_OR 6
211 #define NV50_IR_SUBOP_ATOM_XOR 7
212 #define NV50_IR_SUBOP_ATOM_CAS 8
213 #define NV50_IR_SUBOP_ATOM_EXCH 9
214 #define NV50_IR_SUBOP_CCTL_IV 5
215 #define NV50_IR_SUBOP_CCTL_IVALL 6
216 #define NV50_IR_SUBOP_SUST_IGN 0
217 #define NV50_IR_SUBOP_SUST_TRAP 1
218 #define NV50_IR_SUBOP_SUST_SDCL 3
219 #define NV50_IR_SUBOP_SULD_ZERO 0
220 #define NV50_IR_SUBOP_SULD_TRAP 1
221 #define NV50_IR_SUBOP_SULD_SDCL 3
222 #define NV50_IR_SUBOP_SUBFM_3D 1
223 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
224 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
225 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
226 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
227 #define NV50_IR_SUBOP_PIXLD_COUNT 0
228 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
229 #define NV50_IR_SUBOP_PIXLD_COVERED 2
230 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
231 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
232 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
233 #define NV50_IR_SUBOP_SHFL_IDX 0
234 #define NV50_IR_SUBOP_SHFL_UP 1
235 #define NV50_IR_SUBOP_SHFL_DOWN 2
236 #define NV50_IR_SUBOP_SHFL_BFLY 3
237 #define NV50_IR_SUBOP_LOAD_LOCKED 1
238 #define NV50_IR_SUBOP_STORE_UNLOCKED 2
239 #define NV50_IR_SUBOP_MADSP_SD 0xffff
240 // Yes, we could represent those with DataType.
241 // Or put the type into operation and have a couple 1000 values in that enum.
242 // This will have to do for now.
243 // The bitfields are supposed to correspond to nve4 ISA.
244 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
245 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
246 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
247 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
248 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
249 #define NV50_IR_SUBOP_VOTE_ALL 0
250 #define NV50_IR_SUBOP_VOTE_ANY 1
251 #define NV50_IR_SUBOP_VOTE_UNI 2
262 TYPE_U64
, // 64 bit operations are only lowered after register allocation
274 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
277 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
305 ROUND_M
, // towards -inf
306 ROUND_Z
, // towards 0
307 ROUND_P
, // towards +inf
308 ROUND_NI
, // nearest integer
309 ROUND_MI
, // to integer towards -inf
310 ROUND_ZI
, // to integer towards 0
311 ROUND_PI
, // to integer towards +inf
316 CACHE_CA
, // cache at all levels
317 CACHE_WB
= CACHE_CA
, // cache write back
318 CACHE_CG
, // cache at global level
319 CACHE_CS
, // cache streaming
320 CACHE_CV
, // cache as volatile
321 CACHE_WT
= CACHE_CV
// cache write-through
328 FILE_PREDICATE
, // boolean predicate
329 FILE_FLAGS
, // zero/sign/carry/overflow bits
331 LAST_REGISTER_FILE
= FILE_ADDRESS
,
351 TEX_TARGET_1D_SHADOW
,
352 TEX_TARGET_2D_SHADOW
,
353 TEX_TARGET_CUBE_SHADOW
,
356 TEX_TARGET_2D_MS_ARRAY
,
357 TEX_TARGET_CUBE_ARRAY
,
358 TEX_TARGET_1D_ARRAY_SHADOW
,
359 TEX_TARGET_2D_ARRAY_SHADOW
,
361 TEX_TARGET_RECT_SHADOW
,
362 TEX_TARGET_CUBE_ARRAY_SHADOW
,
433 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
475 class CmpInstruction
;
476 class TexInstruction
;
477 class FlowInstruction
;
482 class ImmediateValue
;
487 int8_t fileIndex
; // signed, may be indirect for CONST[]
488 uint8_t size
; // this should match the Instruction type's size
489 DataType type
; // mainly for pretty printing
491 uint64_t u64
; // immediate values
501 int32_t offset
; // offset from 0 (base of address space)
502 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
510 // precedence: NOT after SAT after NEG after ABS
511 #define NV50_IR_MOD_ABS (1 << 0)
512 #define NV50_IR_MOD_NEG (1 << 1)
513 #define NV50_IR_MOD_SAT (1 << 2)
514 #define NV50_IR_MOD_NOT (1 << 3)
515 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
517 #define NV50_IR_INTERP_MODE_MASK 0x3
518 #define NV50_IR_INTERP_LINEAR (0 << 0)
519 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
520 #define NV50_IR_INTERP_FLAT (2 << 0)
521 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
522 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
523 #define NV50_IR_INTERP_DEFAULT (0 << 2)
524 #define NV50_IR_INTERP_CENTROID (1 << 2)
525 #define NV50_IR_INTERP_OFFSET (2 << 2)
526 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
528 // do we really want this to be a class ?
532 Modifier() : bits(0) { }
533 Modifier(unsigned int m
) : bits(m
) { }
534 Modifier(operation op
);
536 // @return new Modifier applying a after b (asserts if unrepresentable)
537 Modifier
operator*(const Modifier
) const;
538 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
539 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
540 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
542 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
543 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
544 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
546 operation
getOp() const;
548 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
549 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
551 inline operator bool() const { return bits
? true : false; }
553 void applyTo(ImmediateValue
&imm
) const;
555 int print(char *buf
, size_t size
) const;
564 ValueRef(Value
* = NULL
);
565 ValueRef(const ValueRef
&);
568 inline bool exists() const { return value
!= NULL
; }
571 void set(const ValueRef
&);
572 inline Value
*get() const { return value
; }
573 inline Value
*rep() const;
575 inline Instruction
*getInsn() const { return insn
; }
576 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
578 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
579 inline const ValueRef
*getIndirect(int dim
) const;
581 inline DataFile
getFile() const;
582 inline unsigned getSize() const;
584 // SSA: return eventual (traverse MOVs) literal value, if it exists
585 bool getImmediate(ImmediateValue
&) const;
589 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
592 bool usedAsPtr
; // for printing
602 ValueDef(Value
* = NULL
);
603 ValueDef(const ValueDef
&);
606 inline bool exists() const { return value
!= NULL
; }
608 inline Value
*get() const { return value
; }
609 inline Value
*rep() const;
611 bool mayReplace(const ValueRef
&);
612 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
614 inline Instruction
*getInsn() const { return insn
; }
615 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
617 inline DataFile
getFile() const;
618 inline unsigned getSize() const;
620 inline void setSSA(LValue
*);
621 inline const LValue
*preSSA() const;
624 Value
*value
; // should make this LValue * ...
625 LValue
*origin
; // pre SSA value
635 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
637 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
639 virtual bool equals(const Value
*, bool strict
= false) const;
640 virtual bool interfers(const Value
*) const;
641 virtual bool isUniform() const { return true; }
643 inline Value
*rep() const { return join
; }
645 inline Instruction
*getUniqueInsn() const;
646 inline Instruction
*getInsn() const; // use when uniqueness is certain
648 inline int refCount() { return uses
.size(); }
650 inline LValue
*asLValue();
651 inline Symbol
*asSym();
652 inline ImmediateValue
*asImm();
653 inline const Symbol
*asSym() const;
654 inline const ImmediateValue
*asImm() const;
656 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
658 static inline Value
*get(Iterator
&);
660 unordered_set
<ValueRef
*> uses
;
661 std::list
<ValueDef
*> defs
;
662 typedef unordered_set
<ValueRef
*>::iterator UseIterator
;
663 typedef unordered_set
<ValueRef
*>::const_iterator UseCIterator
;
664 typedef std::list
<ValueDef
*>::iterator DefIterator
;
665 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
670 // TODO: these should be in LValue:
675 class LValue
: public Value
678 LValue(Function
*, DataFile file
);
679 LValue(Function
*, LValue
*);
682 virtual bool isUniform() const;
684 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
686 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
689 unsigned compMask
: 8; // compound/component mask
690 unsigned compound
: 1; // used by RA, value involved in split/merge
692 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
693 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
696 class Symbol
: public Value
699 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
702 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
704 virtual bool equals(const Value
*that
, bool strict
) const;
706 virtual bool isUniform() const;
708 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
710 // print with indirect values
711 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
713 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
716 reg
.fileIndex
= fileIndex
;
719 inline void setOffset(int32_t offset
);
720 inline void setAddress(Symbol
*base
, int32_t offset
);
721 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
723 inline const Symbol
*getBase() const { return baseSym
; }
726 Symbol
*baseSym
; // array base for Symbols representing array elements
729 class ImmediateValue
: public Value
733 ImmediateValue(Program
*, uint32_t);
734 ImmediateValue(Program
*, float);
735 ImmediateValue(Program
*, double);
736 // NOTE: not added to program with
737 ImmediateValue(const ImmediateValue
*, DataType ty
);
738 ~ImmediateValue() { };
740 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
742 virtual bool equals(const Value
*that
, bool strict
) const;
744 // these only work if 'type' is valid (we mostly use untyped literals):
745 bool isInteger(const int ival
) const; // ival is cast to this' type
746 bool isNegative() const;
751 // for constant folding:
752 ImmediateValue
operator+(const ImmediateValue
&) const;
753 ImmediateValue
operator-(const ImmediateValue
&) const;
754 ImmediateValue
operator*(const ImmediateValue
&) const;
755 ImmediateValue
operator/(const ImmediateValue
&) const;
757 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
759 bool compare(CondCode cc
, float fval
) const;
761 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
768 Instruction(Function
*, operation
, DataType
);
769 virtual ~Instruction();
771 virtual Instruction
*clone(ClonePolicy
<Function
>&,
772 Instruction
* = NULL
) const;
774 void setDef(int i
, Value
*);
775 void setSrc(int s
, Value
*);
776 void setSrc(int s
, const ValueRef
&);
777 void swapSources(int a
, int b
);
778 void moveSources(int s
, int delta
);
779 bool setIndirect(int s
, int dim
, Value
*);
781 inline ValueRef
& src(int s
) { return srcs
[s
]; }
782 inline ValueDef
& def(int s
) { return defs
[s
]; }
783 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
784 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
786 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
787 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
788 inline Value
*getIndirect(int s
, int dim
) const;
790 inline bool defExists(unsigned d
) const
792 return d
< defs
.size() && defs
[d
].exists();
794 inline bool srcExists(unsigned s
) const
796 return s
< srcs
.size() && srcs
[s
].exists();
799 inline bool constrainedDefs() const;
801 bool setPredicate(CondCode ccode
, Value
*);
802 inline Value
*getPredicate() const;
803 bool writesPredicate() const;
804 inline bool isPredicated() const { return predSrc
>= 0; }
806 inline void setFlagsSrc(int s
, Value
*);
807 inline void setFlagsDef(int d
, Value
*);
808 inline bool usesFlags() const { return flagsSrc
>= 0; }
810 unsigned int defCount() const { return defs
.size(); };
811 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
812 unsigned int srcCount() const { return srcs
.size(); };
813 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
815 // save & remove / set indirect[0,1] and predicate source
816 void takeExtraSources(int s
, Value
*[3]);
817 void putExtraSources(int s
, Value
*[3]);
819 inline void setType(DataType type
) { dType
= sType
= type
; }
821 inline void setType(DataType dtype
, DataType stype
)
827 inline bool isPseudo() const { return op
< OP_MOV
; }
830 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
831 bool isActionEqual(const Instruction
*) const;
832 bool isResultEqual(const Instruction
*) const;
836 inline CmpInstruction
*asCmp();
837 inline TexInstruction
*asTex();
838 inline FlowInstruction
*asFlow();
839 inline const TexInstruction
*asTex() const;
840 inline const CmpInstruction
*asCmp() const;
841 inline const FlowInstruction
*asFlow() const;
847 int serial
; // CFG order
850 DataType dType
; // destination or defining type
851 DataType sType
; // source or secondary type
856 uint16_t subOp
; // quadop, 1 for mul-high, etc.
858 unsigned encSize
: 4; // encoding size in bytes
859 unsigned saturate
: 1; // to [0.0f, 1.0f]
860 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
861 unsigned fixed
: 1; // prevent dead code elimination
862 unsigned terminator
: 1; // end of basic block
863 unsigned ftz
: 1; // flush denormal to zero
864 unsigned dnz
: 1; // denormals, NaN are zero
865 unsigned ipa
: 4; // interpolation mode
867 unsigned perPatch
: 1;
868 unsigned exit
: 1; // terminate program after insn
869 unsigned mask
: 4; // for vector ops
871 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
877 uint32_t sched
; // scheduling data (NOTE: maybe move to separate storage)
882 std::deque
<ValueDef
> defs
; // no gaps !
883 std::deque
<ValueRef
> srcs
; // no gaps !
885 // instruction specific methods:
886 // (don't want to subclass, would need more constructors and memory pools)
888 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
890 unsigned int getInterpMode() const { return ipa
& 0x3; }
891 unsigned int getSampleMode() const { return ipa
& 0xc; }
899 TXQ_DIMS
, /* x, y, z, levels */
900 TXQ_TYPE
, /* ?, ?, samples, ? */
908 class TexInstruction
: public Instruction
914 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
916 const char *getName() const { return descTable
[target
].name
; }
917 unsigned int getArgCount() const { return descTable
[target
].argc
; }
918 unsigned int getDim() const { return descTable
[target
].dim
; }
919 int isArray() const { return descTable
[target
].array
? 1 : 0; }
920 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
921 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
923 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
927 target
= TEX_TARGET_2D_ARRAY
;
929 target
= TEX_TARGET_2D
;
933 Target
& operator=(TexTarget targ
)
935 assert(targ
< TEX_TARGET_COUNT
);
940 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
941 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
943 enum TexTarget
getEnum() const { return target
; }
956 static const struct Desc descTable
[TEX_TARGET_COUNT
];
959 enum TexTarget target
;
971 static const struct ImgFormatDesc formatTable
[IMG_FORMAT_COUNT
];
974 TexInstruction(Function
*, operation
);
975 virtual ~TexInstruction();
977 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
978 Instruction
* = NULL
) const;
980 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
987 void setIndirectR(Value
*);
988 void setIndirectS(Value
*);
989 inline Value
*getIndirectR() const;
990 inline Value
*getIndirectS() const;
1004 bool liveOnly
; // only execute on live pixels of a quad (optimization)
1008 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
1009 int8_t offset
[3]; // only used on nv50
1011 enum TexQuery query
;
1012 const struct ImgFormatDesc
*format
;
1017 ValueRef offset
[4][3];
1020 class CmpInstruction
: public Instruction
1023 CmpInstruction(Function
*, operation
);
1025 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
1026 Instruction
* = NULL
) const;
1028 void setCondition(CondCode cond
) { setCond
= cond
; }
1029 CondCode
getCondition() const { return setCond
; }
1035 class FlowInstruction
: public Instruction
1038 FlowInstruction(Function
*, operation
, void *target
);
1040 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
1041 Instruction
* = NULL
) const;
1044 unsigned allWarp
: 1;
1045 unsigned absolute
: 1;
1047 unsigned builtin
: 1; // true for calls to emulation code
1048 unsigned indirect
: 1; // target in src(0)
1060 BasicBlock(Function
*);
1063 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
1065 inline int getId() const { return id
; }
1066 inline unsigned int getInsnCount() const { return numInsns
; }
1067 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
1069 bool dominatedBy(BasicBlock
*bb
);
1070 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
1072 // returns mask of conditional out blocks
1073 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1074 unsigned int initiatesSimpleConditional() const;
1077 Function
*getFunction() const { return func
; }
1078 Program
*getProgram() const { return program
; }
1080 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
1081 Instruction
*getPhi() const { return phi
; }
1082 Instruction
*getFirst() const { return phi
? phi
: entry
; }
1083 Instruction
*getExit() const { return exit
; }
1085 void insertHead(Instruction
*);
1086 void insertTail(Instruction
*);
1087 void insertBefore(Instruction
*, Instruction
*);
1088 void insertAfter(Instruction
*, Instruction
*);
1089 void remove(Instruction
*);
1090 void permuteAdjacent(Instruction
*, Instruction
*);
1092 BasicBlock
*idom() const;
1094 // NOTE: currently does not rebuild the dominator tree
1095 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1096 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1098 DLList
& getDF() { return df
; }
1099 DLList::Iterator
iterDF() { return df
.iterator(); }
1101 static inline BasicBlock
*get(Iterator
&);
1102 static inline BasicBlock
*get(Graph::Node
*);
1105 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1114 Instruction
*joinAt
; // for quick reference
1116 bool explicitCont
; // loop headers: true if loop contains continue stmts
1126 unsigned int numInsns
;
1132 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1138 Function(Program
*, const char *name
, uint32_t label
);
1141 static inline Function
*get(Graph::Node
*node
);
1143 inline Program
*getProgram() const { return prog
; }
1144 inline const char *getName() const { return name
; }
1145 inline int getId() const { return id
; }
1146 inline uint32_t getLabel() const { return label
; }
1149 void printLiveIntervals() const;
1150 void printCFGraph(const char *filePath
);
1152 bool setEntry(BasicBlock
*);
1153 bool setExit(BasicBlock
*);
1155 unsigned int orderInstructions(ArrayList
&);
1157 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1158 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1159 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1161 inline LValue
*getLValue(int id
);
1163 void buildLiveSets();
1164 void buildDefSets();
1165 bool convertToSSA();
1168 std::deque
<ValueDef
> ins
;
1169 std::deque
<ValueRef
> outs
;
1170 std::deque
<Value
*> clobbers
;
1173 Graph::Node
*cfgExit
;
1175 Graph::Node call
; // node in the call graph
1177 BasicBlock
**bbArray
; // BBs in emission order
1180 unsigned int loopNestingBound
;
1188 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1191 ArrayList allBBlocks
;
1193 ArrayList allLValues
;
1196 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1197 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1202 const char *const name
;
1209 CG_STAGE_SSA
, // expected directly before register allocation
1219 TYPE_TESSELLATION_CONTROL
,
1220 TYPE_TESSELLATION_EVAL
,
1226 Program(Type type
, Target
*targ
);
1231 Type
getType() const { return progType
; }
1233 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1234 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1235 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1237 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1238 bool makeFromSM4(struct nv50_ir_prog_info
*);
1239 bool convertToSSA();
1240 bool optimizeSSA(int level
);
1241 bool optimizePostRA(int level
);
1242 bool registerAllocation();
1243 bool emitBinary(struct nv50_ir_prog_info
*);
1245 const Target
*getTarget() const { return target
; }
1248 void emitSymbolTable(struct nv50_ir_prog_info
*);
1258 ArrayList allRValues
;
1262 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1266 MemoryPool mem_Instruction
;
1267 MemoryPool mem_CmpInstruction
;
1268 MemoryPool mem_TexInstruction
;
1269 MemoryPool mem_FlowInstruction
;
1270 MemoryPool mem_LValue
;
1271 MemoryPool mem_Symbol
;
1272 MemoryPool mem_ImmediateValue
;
1277 void *targetPriv
; // e.g. to carry information between passes
1279 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1281 void releaseInstruction(Instruction
*);
1282 void releaseValue(Value
*);
1285 // TODO: add const version
1289 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1290 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1293 // return false to continue with next entity on next higher level
1294 virtual bool visit(Function
*) { return true; }
1295 virtual bool visit(BasicBlock
*) { return true; }
1296 virtual bool visit(Instruction
*) { return false; }
1298 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1299 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1307 // =============================================================================
1309 #include "codegen/nv50_ir_inlines.h"
1311 } // namespace nv50_ir
1313 #endif // __NV50_IR_H__