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
71 OP_SAT
, // CLAMP(f32, 0.0, 1.0)
76 OP_SET_AND
, // dst = (src0 CMP src1) & src2
80 OP_SELP
, // dst = src2 ? src0 : src1
81 OP_SLCT
, // dst = (src2 CMP 0) ? src0 : src1
88 OP_EXP
, // exponential (base M_E)
89 OP_LOG
, // natural logarithm
102 OP_BRKPT
, // breakpoint (not related to loops)
103 OP_JOINAT
, // push control flow convergence point
107 OP_MEMBAR
, // memory barrier (mfence, lfence, sfence)
108 OP_VFETCH
, // indirection 0 in attribute space, indirection 1 is vertex base
109 OP_PFETCH
, // fetch base address of vertex src0 (immediate) [+ src1]
110 OP_AFETCH
, // fetch base address of shader input (a[%r1+0x10])
114 OP_EMIT
, // emit vertex
115 OP_RESTART
, // restart primitive
117 OP_TXB
, // texture bias
118 OP_TXL
, // texure lod
119 OP_TXF
, // texel fetch
120 OP_TXQ
, // texture size query
121 OP_TXD
, // texture derivatives
122 OP_TXG
, // texture gather
123 OP_TXLQ
, // texture query lod
124 OP_TEXCSAA
, // texture op for coverage sampling
125 OP_TEXPREP
, // turn cube map array into 2d array coordinates
126 OP_SULDB
, // surface load (raw)
127 OP_SULDP
, // surface load (formatted)
128 OP_SUSTB
, // surface store (raw)
129 OP_SUSTP
, // surface store (formatted)
131 OP_SUREDP
, // surface reduction (atomic op)
132 OP_SULEA
, // surface load effective address
133 OP_SUBFM
, // surface bitfield manipulation
134 OP_SUCLAMP
, // clamp surface coordinates
135 OP_SUEAU
, // surface effective address
136 OP_SUQ
, // surface query
137 OP_MADSP
, // special integer multiply-add
138 OP_TEXBAR
, // texture dependency barrier
141 OP_RDSV
, // read system value
142 OP_WRSV
, // write system value
143 OP_PIXLD
, // get info about raster object or surfaces
147 OP_POPCNT
, // bitcount(src0 & src1)
148 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
149 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
150 OP_BFIND
, // find highest/lowest set bit
151 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
153 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
154 OP_VADD
, // byte/word vector operations
163 OP_CCTL
, // cache control
164 OP_SHFL
, // warp shuffle
166 OP_BUFQ
, // buffer query
170 // various instruction-specific modifier definitions Instruction::subOp
171 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
172 #define NV50_IR_SUBOP_MUL_HIGH 1
173 #define NV50_IR_SUBOP_EMIT_RESTART 1
174 #define NV50_IR_SUBOP_LDC_IL 1
175 #define NV50_IR_SUBOP_LDC_IS 2
176 #define NV50_IR_SUBOP_LDC_ISL 3
177 #define NV50_IR_SUBOP_SHIFT_WRAP 1
178 #define NV50_IR_SUBOP_EMU_PRERET 1
179 #define NV50_IR_SUBOP_TEXBAR(n) n
180 #define NV50_IR_SUBOP_MOV_FINAL 1
181 #define NV50_IR_SUBOP_EXTBF_REV 1
182 #define NV50_IR_SUBOP_BFIND_SAMT 1
183 #define NV50_IR_SUBOP_RCPRSQ_64H 1
184 #define NV50_IR_SUBOP_PERMT_F4E 1
185 #define NV50_IR_SUBOP_PERMT_B4E 2
186 #define NV50_IR_SUBOP_PERMT_RC8 3
187 #define NV50_IR_SUBOP_PERMT_ECL 4
188 #define NV50_IR_SUBOP_PERMT_ECR 5
189 #define NV50_IR_SUBOP_PERMT_RC16 6
190 #define NV50_IR_SUBOP_BAR_SYNC 0
191 #define NV50_IR_SUBOP_BAR_ARRIVE 1
192 #define NV50_IR_SUBOP_BAR_RED_AND 2
193 #define NV50_IR_SUBOP_BAR_RED_OR 3
194 #define NV50_IR_SUBOP_BAR_RED_POPC 4
195 #define NV50_IR_SUBOP_MEMBAR_L 1
196 #define NV50_IR_SUBOP_MEMBAR_S 2
197 #define NV50_IR_SUBOP_MEMBAR_M 3
198 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
199 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
200 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
201 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
202 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
203 #define NV50_IR_SUBOP_MEMBAR(d,s) \
204 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
205 #define NV50_IR_SUBOP_ATOM_ADD 0
206 #define NV50_IR_SUBOP_ATOM_MIN 1
207 #define NV50_IR_SUBOP_ATOM_MAX 2
208 #define NV50_IR_SUBOP_ATOM_INC 3
209 #define NV50_IR_SUBOP_ATOM_DEC 4
210 #define NV50_IR_SUBOP_ATOM_AND 5
211 #define NV50_IR_SUBOP_ATOM_OR 6
212 #define NV50_IR_SUBOP_ATOM_XOR 7
213 #define NV50_IR_SUBOP_ATOM_CAS 8
214 #define NV50_IR_SUBOP_ATOM_EXCH 9
215 #define NV50_IR_SUBOP_CCTL_IV 5
216 #define NV50_IR_SUBOP_CCTL_IVALL 6
217 #define NV50_IR_SUBOP_SUST_IGN 0
218 #define NV50_IR_SUBOP_SUST_TRAP 1
219 #define NV50_IR_SUBOP_SUST_SDCL 3
220 #define NV50_IR_SUBOP_SULD_ZERO 0
221 #define NV50_IR_SUBOP_SULD_TRAP 1
222 #define NV50_IR_SUBOP_SULD_SDCL 3
223 #define NV50_IR_SUBOP_SUBFM_3D 1
224 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
225 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
226 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
227 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
228 #define NV50_IR_SUBOP_PIXLD_COUNT 0
229 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
230 #define NV50_IR_SUBOP_PIXLD_COVERED 2
231 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
232 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
233 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
234 #define NV50_IR_SUBOP_SHFL_IDX 0
235 #define NV50_IR_SUBOP_SHFL_UP 1
236 #define NV50_IR_SUBOP_SHFL_DOWN 2
237 #define NV50_IR_SUBOP_SHFL_BFLY 3
238 #define NV50_IR_SUBOP_LOAD_LOCKED 1
239 #define NV50_IR_SUBOP_STORE_UNLOCKED 2
240 #define NV50_IR_SUBOP_MADSP_SD 0xffff
241 // Yes, we could represent those with DataType.
242 // Or put the type into operation and have a couple 1000 values in that enum.
243 // This will have to do for now.
244 // The bitfields are supposed to correspond to nve4 ISA.
245 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
246 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
247 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
248 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
249 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
250 #define NV50_IR_SUBOP_VOTE_ALL 0
251 #define NV50_IR_SUBOP_VOTE_ANY 1
252 #define NV50_IR_SUBOP_VOTE_UNI 2
263 TYPE_U64
, // 64 bit operations are only lowered after register allocation
275 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
278 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
306 ROUND_M
, // towards -inf
307 ROUND_Z
, // towards 0
308 ROUND_P
, // towards +inf
309 ROUND_NI
, // nearest integer
310 ROUND_MI
, // to integer towards -inf
311 ROUND_ZI
, // to integer towards 0
312 ROUND_PI
, // to integer towards +inf
317 CACHE_CA
, // cache at all levels
318 CACHE_WB
= CACHE_CA
, // cache write back
319 CACHE_CG
, // cache at global level
320 CACHE_CS
, // cache streaming
321 CACHE_CV
, // cache as volatile
322 CACHE_WT
= CACHE_CV
// cache write-through
329 FILE_PREDICATE
, // boolean predicate
330 FILE_FLAGS
, // zero/sign/carry/overflow bits
332 LAST_REGISTER_FILE
= FILE_ADDRESS
,
352 TEX_TARGET_1D_SHADOW
,
353 TEX_TARGET_2D_SHADOW
,
354 TEX_TARGET_CUBE_SHADOW
,
357 TEX_TARGET_2D_MS_ARRAY
,
358 TEX_TARGET_CUBE_ARRAY
,
359 TEX_TARGET_1D_ARRAY_SHADOW
,
360 TEX_TARGET_2D_ARRAY_SHADOW
,
362 TEX_TARGET_RECT_SHADOW
,
363 TEX_TARGET_CUBE_ARRAY_SHADOW
,
436 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
479 class CmpInstruction
;
480 class TexInstruction
;
481 class FlowInstruction
;
486 class ImmediateValue
;
491 int8_t fileIndex
; // signed, may be indirect for CONST[]
492 uint8_t size
; // this should match the Instruction type's size
493 DataType type
; // mainly for pretty printing
495 uint64_t u64
; // immediate values
505 int32_t offset
; // offset from 0 (base of address space)
506 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
514 // precedence: NOT after SAT after NEG after ABS
515 #define NV50_IR_MOD_ABS (1 << 0)
516 #define NV50_IR_MOD_NEG (1 << 1)
517 #define NV50_IR_MOD_SAT (1 << 2)
518 #define NV50_IR_MOD_NOT (1 << 3)
519 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
521 #define NV50_IR_INTERP_MODE_MASK 0x3
522 #define NV50_IR_INTERP_LINEAR (0 << 0)
523 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
524 #define NV50_IR_INTERP_FLAT (2 << 0)
525 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
526 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
527 #define NV50_IR_INTERP_DEFAULT (0 << 2)
528 #define NV50_IR_INTERP_CENTROID (1 << 2)
529 #define NV50_IR_INTERP_OFFSET (2 << 2)
530 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
532 // do we really want this to be a class ?
536 Modifier() : bits(0) { }
537 Modifier(unsigned int m
) : bits(m
) { }
538 Modifier(operation op
);
540 // @return new Modifier applying a after b (asserts if unrepresentable)
541 Modifier
operator*(const Modifier
) const;
542 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
543 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
544 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
546 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
547 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
548 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
550 operation
getOp() const;
552 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
553 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
555 inline operator bool() const { return bits
? true : false; }
557 void applyTo(ImmediateValue
&imm
) const;
559 int print(char *buf
, size_t size
) const;
568 ValueRef(Value
* = NULL
);
569 ValueRef(const ValueRef
&);
572 inline bool exists() const { return value
!= NULL
; }
575 void set(const ValueRef
&);
576 inline Value
*get() const { return value
; }
577 inline Value
*rep() const;
579 inline Instruction
*getInsn() const { return insn
; }
580 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
582 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
583 inline const ValueRef
*getIndirect(int dim
) const;
585 inline DataFile
getFile() const;
586 inline unsigned getSize() const;
588 // SSA: return eventual (traverse MOVs) literal value, if it exists
589 bool getImmediate(ImmediateValue
&) const;
593 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
596 bool usedAsPtr
; // for printing
606 ValueDef(Value
* = NULL
);
607 ValueDef(const ValueDef
&);
610 inline bool exists() const { return value
!= NULL
; }
612 inline Value
*get() const { return value
; }
613 inline Value
*rep() const;
615 bool mayReplace(const ValueRef
&);
616 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
618 inline Instruction
*getInsn() const { return insn
; }
619 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
621 inline DataFile
getFile() const;
622 inline unsigned getSize() const;
624 inline void setSSA(LValue
*);
625 inline const LValue
*preSSA() const;
628 Value
*value
; // should make this LValue * ...
629 LValue
*origin
; // pre SSA value
639 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
641 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
643 virtual bool equals(const Value
*, bool strict
= false) const;
644 virtual bool interfers(const Value
*) const;
645 virtual bool isUniform() const { return true; }
647 inline Value
*rep() const { return join
; }
649 inline Instruction
*getUniqueInsn() const;
650 inline Instruction
*getInsn() const; // use when uniqueness is certain
652 inline int refCount() { return uses
.size(); }
654 inline LValue
*asLValue();
655 inline Symbol
*asSym();
656 inline ImmediateValue
*asImm();
657 inline const Symbol
*asSym() const;
658 inline const ImmediateValue
*asImm() const;
660 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
662 static inline Value
*get(Iterator
&);
664 unordered_set
<ValueRef
*> uses
;
665 std::list
<ValueDef
*> defs
;
666 typedef unordered_set
<ValueRef
*>::iterator UseIterator
;
667 typedef unordered_set
<ValueRef
*>::const_iterator UseCIterator
;
668 typedef std::list
<ValueDef
*>::iterator DefIterator
;
669 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
674 // TODO: these should be in LValue:
679 class LValue
: public Value
682 LValue(Function
*, DataFile file
);
683 LValue(Function
*, LValue
*);
686 virtual bool isUniform() const;
688 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
690 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
693 unsigned compMask
: 8; // compound/component mask
694 unsigned compound
: 1; // used by RA, value involved in split/merge
696 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
697 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
700 class Symbol
: public Value
703 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
706 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
708 virtual bool equals(const Value
*that
, bool strict
) const;
710 virtual bool isUniform() const;
712 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
714 // print with indirect values
715 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
717 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
720 reg
.fileIndex
= fileIndex
;
723 inline void setOffset(int32_t offset
);
724 inline void setAddress(Symbol
*base
, int32_t offset
);
725 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
727 inline const Symbol
*getBase() const { return baseSym
; }
730 Symbol
*baseSym
; // array base for Symbols representing array elements
733 class ImmediateValue
: public Value
737 ImmediateValue(Program
*, uint32_t);
738 ImmediateValue(Program
*, float);
739 ImmediateValue(Program
*, double);
740 // NOTE: not added to program with
741 ImmediateValue(const ImmediateValue
*, DataType ty
);
742 ~ImmediateValue() { };
744 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
746 virtual bool equals(const Value
*that
, bool strict
) const;
748 // these only work if 'type' is valid (we mostly use untyped literals):
749 bool isInteger(const int ival
) const; // ival is cast to this' type
750 bool isNegative() const;
755 // for constant folding:
756 ImmediateValue
operator+(const ImmediateValue
&) const;
757 ImmediateValue
operator-(const ImmediateValue
&) const;
758 ImmediateValue
operator*(const ImmediateValue
&) const;
759 ImmediateValue
operator/(const ImmediateValue
&) const;
761 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
763 bool compare(CondCode cc
, float fval
) const;
765 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
772 Instruction(Function
*, operation
, DataType
);
773 virtual ~Instruction();
775 virtual Instruction
*clone(ClonePolicy
<Function
>&,
776 Instruction
* = NULL
) const;
778 void setDef(int i
, Value
*);
779 void setSrc(int s
, Value
*);
780 void setSrc(int s
, const ValueRef
&);
781 void swapSources(int a
, int b
);
782 void moveSources(int s
, int delta
);
783 bool setIndirect(int s
, int dim
, Value
*);
785 inline ValueRef
& src(int s
) { return srcs
[s
]; }
786 inline ValueDef
& def(int s
) { return defs
[s
]; }
787 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
788 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
790 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
791 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
792 inline Value
*getIndirect(int s
, int dim
) const;
794 inline bool defExists(unsigned d
) const
796 return d
< defs
.size() && defs
[d
].exists();
798 inline bool srcExists(unsigned s
) const
800 return s
< srcs
.size() && srcs
[s
].exists();
803 inline bool constrainedDefs() const;
805 bool setPredicate(CondCode ccode
, Value
*);
806 inline Value
*getPredicate() const;
807 bool writesPredicate() const;
808 inline bool isPredicated() const { return predSrc
>= 0; }
810 inline void setFlagsSrc(int s
, Value
*);
811 inline void setFlagsDef(int d
, Value
*);
812 inline bool usesFlags() const { return flagsSrc
>= 0; }
814 unsigned int defCount() const { return defs
.size(); };
815 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
816 unsigned int srcCount() const { return srcs
.size(); };
817 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
819 // save & remove / set indirect[0,1] and predicate source
820 void takeExtraSources(int s
, Value
*[3]);
821 void putExtraSources(int s
, Value
*[3]);
823 inline void setType(DataType type
) { dType
= sType
= type
; }
825 inline void setType(DataType dtype
, DataType stype
)
831 inline bool isPseudo() const { return op
< OP_MOV
; }
834 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
835 bool isActionEqual(const Instruction
*) const;
836 bool isResultEqual(const Instruction
*) const;
838 // check whether the defs interfere with srcs and defs of another instruction
839 bool canCommuteDefDef(const Instruction
*) const;
840 bool canCommuteDefSrc(const Instruction
*) const;
844 inline CmpInstruction
*asCmp();
845 inline TexInstruction
*asTex();
846 inline FlowInstruction
*asFlow();
847 inline const TexInstruction
*asTex() const;
848 inline const CmpInstruction
*asCmp() const;
849 inline const FlowInstruction
*asFlow() const;
855 int serial
; // CFG order
858 DataType dType
; // destination or defining type
859 DataType sType
; // source or secondary type
864 uint16_t subOp
; // quadop, 1 for mul-high, etc.
866 unsigned encSize
: 4; // encoding size in bytes
867 unsigned saturate
: 1; // to [0.0f, 1.0f]
868 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
869 unsigned fixed
: 1; // prevent dead code elimination
870 unsigned terminator
: 1; // end of basic block
871 unsigned ftz
: 1; // flush denormal to zero
872 unsigned dnz
: 1; // denormals, NaN are zero
873 unsigned ipa
: 4; // interpolation mode
875 unsigned perPatch
: 1;
876 unsigned exit
: 1; // terminate program after insn
877 unsigned mask
: 4; // for vector ops
879 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
885 uint32_t sched
; // scheduling data (NOTE: maybe move to separate storage)
890 std::deque
<ValueDef
> defs
; // no gaps !
891 std::deque
<ValueRef
> srcs
; // no gaps !
893 // instruction specific methods:
894 // (don't want to subclass, would need more constructors and memory pools)
896 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
898 unsigned int getInterpMode() const { return ipa
& 0x3; }
899 unsigned int getSampleMode() const { return ipa
& 0xc; }
907 TXQ_DIMS
, /* x, y, z, levels */
908 TXQ_TYPE
, /* ?, ?, samples, ? */
916 class TexInstruction
: public Instruction
922 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
924 const char *getName() const { return descTable
[target
].name
; }
925 unsigned int getArgCount() const { return descTable
[target
].argc
; }
926 unsigned int getDim() const { return descTable
[target
].dim
; }
927 int isArray() const { return descTable
[target
].array
? 1 : 0; }
928 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
929 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
931 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
935 target
= TEX_TARGET_2D_ARRAY
;
937 target
= TEX_TARGET_2D
;
941 Target
& operator=(TexTarget targ
)
943 assert(targ
< TEX_TARGET_COUNT
);
948 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
949 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
951 enum TexTarget
getEnum() const { return target
; }
964 static const struct Desc descTable
[TEX_TARGET_COUNT
];
967 enum TexTarget target
;
980 static const struct ImgFormatDesc formatTable
[IMG_FORMAT_COUNT
];
983 TexInstruction(Function
*, operation
);
984 virtual ~TexInstruction();
986 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
987 Instruction
* = NULL
) const;
989 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
996 void setIndirectR(Value
*);
997 void setIndirectS(Value
*);
998 inline Value
*getIndirectR() const;
999 inline Value
*getIndirectS() const;
1007 int8_t rIndirectSrc
;
1008 int8_t sIndirectSrc
;
1013 bool liveOnly
; // only execute on live pixels of a quad (optimization)
1017 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
1018 int8_t offset
[3]; // only used on nv50
1020 enum TexQuery query
;
1021 const struct ImgFormatDesc
*format
;
1026 ValueRef offset
[4][3];
1029 class CmpInstruction
: public Instruction
1032 CmpInstruction(Function
*, operation
);
1034 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
1035 Instruction
* = NULL
) const;
1037 void setCondition(CondCode cond
) { setCond
= cond
; }
1038 CondCode
getCondition() const { return setCond
; }
1044 class FlowInstruction
: public Instruction
1047 FlowInstruction(Function
*, operation
, void *target
);
1049 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
1050 Instruction
* = NULL
) const;
1053 unsigned allWarp
: 1;
1054 unsigned absolute
: 1;
1056 unsigned builtin
: 1; // true for calls to emulation code
1057 unsigned indirect
: 1; // target in src(0)
1069 BasicBlock(Function
*);
1072 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
1074 inline int getId() const { return id
; }
1075 inline unsigned int getInsnCount() const { return numInsns
; }
1076 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
1078 bool dominatedBy(BasicBlock
*bb
);
1079 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
1081 // returns mask of conditional out blocks
1082 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1083 unsigned int initiatesSimpleConditional() const;
1086 Function
*getFunction() const { return func
; }
1087 Program
*getProgram() const { return program
; }
1089 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
1090 Instruction
*getPhi() const { return phi
; }
1091 Instruction
*getFirst() const { return phi
? phi
: entry
; }
1092 Instruction
*getExit() const { return exit
; }
1094 void insertHead(Instruction
*);
1095 void insertTail(Instruction
*);
1096 void insertBefore(Instruction
*, Instruction
*);
1097 void insertAfter(Instruction
*, Instruction
*);
1098 void remove(Instruction
*);
1099 void permuteAdjacent(Instruction
*, Instruction
*);
1101 BasicBlock
*idom() const;
1103 // NOTE: currently does not rebuild the dominator tree
1104 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1105 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1107 DLList
& getDF() { return df
; }
1108 DLList::Iterator
iterDF() { return df
.iterator(); }
1110 static inline BasicBlock
*get(Iterator
&);
1111 static inline BasicBlock
*get(Graph::Node
*);
1114 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1123 Instruction
*joinAt
; // for quick reference
1125 bool explicitCont
; // loop headers: true if loop contains continue stmts
1135 unsigned int numInsns
;
1141 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1147 Function(Program
*, const char *name
, uint32_t label
);
1150 static inline Function
*get(Graph::Node
*node
);
1152 inline Program
*getProgram() const { return prog
; }
1153 inline const char *getName() const { return name
; }
1154 inline int getId() const { return id
; }
1155 inline uint32_t getLabel() const { return label
; }
1158 void printLiveIntervals() const;
1159 void printCFGraph(const char *filePath
);
1161 bool setEntry(BasicBlock
*);
1162 bool setExit(BasicBlock
*);
1164 unsigned int orderInstructions(ArrayList
&);
1166 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1167 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1168 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1170 inline LValue
*getLValue(int id
);
1172 void buildLiveSets();
1173 void buildDefSets();
1174 bool convertToSSA();
1177 std::deque
<ValueDef
> ins
;
1178 std::deque
<ValueRef
> outs
;
1179 std::deque
<Value
*> clobbers
;
1182 Graph::Node
*cfgExit
;
1184 Graph::Node call
; // node in the call graph
1186 BasicBlock
**bbArray
; // BBs in emission order
1189 unsigned int loopNestingBound
;
1197 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1200 ArrayList allBBlocks
;
1202 ArrayList allLValues
;
1205 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1206 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1211 const char *const name
;
1218 CG_STAGE_SSA
, // expected directly before register allocation
1228 TYPE_TESSELLATION_CONTROL
,
1229 TYPE_TESSELLATION_EVAL
,
1235 Program(Type type
, Target
*targ
);
1240 Type
getType() const { return progType
; }
1242 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1243 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1244 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1246 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1247 bool makeFromSM4(struct nv50_ir_prog_info
*);
1248 bool convertToSSA();
1249 bool optimizeSSA(int level
);
1250 bool optimizePostRA(int level
);
1251 bool registerAllocation();
1252 bool emitBinary(struct nv50_ir_prog_info
*);
1254 const Target
*getTarget() const { return target
; }
1257 void emitSymbolTable(struct nv50_ir_prog_info
*);
1267 ArrayList allRValues
;
1271 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1275 MemoryPool mem_Instruction
;
1276 MemoryPool mem_CmpInstruction
;
1277 MemoryPool mem_TexInstruction
;
1278 MemoryPool mem_FlowInstruction
;
1279 MemoryPool mem_LValue
;
1280 MemoryPool mem_Symbol
;
1281 MemoryPool mem_ImmediateValue
;
1286 void *targetPriv
; // e.g. to carry information between passes
1288 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1290 void releaseInstruction(Instruction
*);
1291 void releaseValue(Value
*);
1294 // TODO: add const version
1298 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1299 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1302 // return false to continue with next entity on next higher level
1303 virtual bool visit(Function
*) { return true; }
1304 virtual bool visit(BasicBlock
*) { return true; }
1305 virtual bool visit(Instruction
*) { return false; }
1307 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1308 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1316 // =============================================================================
1318 #include "codegen/nv50_ir_inlines.h"
1320 } // namespace nv50_ir
1322 #endif // __NV50_IR_H__