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
,
435 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
478 class CmpInstruction
;
479 class TexInstruction
;
480 class FlowInstruction
;
485 class ImmediateValue
;
490 int8_t fileIndex
; // signed, may be indirect for CONST[]
491 uint8_t size
; // this should match the Instruction type's size
492 DataType type
; // mainly for pretty printing
494 uint64_t u64
; // immediate values
504 int32_t offset
; // offset from 0 (base of address space)
505 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
513 // precedence: NOT after SAT after NEG after ABS
514 #define NV50_IR_MOD_ABS (1 << 0)
515 #define NV50_IR_MOD_NEG (1 << 1)
516 #define NV50_IR_MOD_SAT (1 << 2)
517 #define NV50_IR_MOD_NOT (1 << 3)
518 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
520 #define NV50_IR_INTERP_MODE_MASK 0x3
521 #define NV50_IR_INTERP_LINEAR (0 << 0)
522 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
523 #define NV50_IR_INTERP_FLAT (2 << 0)
524 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
525 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
526 #define NV50_IR_INTERP_DEFAULT (0 << 2)
527 #define NV50_IR_INTERP_CENTROID (1 << 2)
528 #define NV50_IR_INTERP_OFFSET (2 << 2)
529 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
531 // do we really want this to be a class ?
535 Modifier() : bits(0) { }
536 Modifier(unsigned int m
) : bits(m
) { }
537 Modifier(operation op
);
539 // @return new Modifier applying a after b (asserts if unrepresentable)
540 Modifier
operator*(const Modifier
) const;
541 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
542 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
543 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
545 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.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
; }
549 operation
getOp() const;
551 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
552 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
554 inline operator bool() const { return bits
? true : false; }
556 void applyTo(ImmediateValue
&imm
) const;
558 int print(char *buf
, size_t size
) const;
567 ValueRef(Value
* = NULL
);
568 ValueRef(const ValueRef
&);
571 inline bool exists() const { return value
!= NULL
; }
574 void set(const ValueRef
&);
575 inline Value
*get() const { return value
; }
576 inline Value
*rep() const;
578 inline Instruction
*getInsn() const { return insn
; }
579 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
581 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
582 inline const ValueRef
*getIndirect(int dim
) const;
584 inline DataFile
getFile() const;
585 inline unsigned getSize() const;
587 // SSA: return eventual (traverse MOVs) literal value, if it exists
588 bool getImmediate(ImmediateValue
&) const;
592 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
595 bool usedAsPtr
; // for printing
605 ValueDef(Value
* = NULL
);
606 ValueDef(const ValueDef
&);
609 inline bool exists() const { return value
!= NULL
; }
611 inline Value
*get() const { return value
; }
612 inline Value
*rep() const;
614 bool mayReplace(const ValueRef
&);
615 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
617 inline Instruction
*getInsn() const { return insn
; }
618 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
620 inline DataFile
getFile() const;
621 inline unsigned getSize() const;
623 inline void setSSA(LValue
*);
624 inline const LValue
*preSSA() const;
627 Value
*value
; // should make this LValue * ...
628 LValue
*origin
; // pre SSA value
638 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
640 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
642 virtual bool equals(const Value
*, bool strict
= false) const;
643 virtual bool interfers(const Value
*) const;
644 virtual bool isUniform() const { return true; }
646 inline Value
*rep() const { return join
; }
648 inline Instruction
*getUniqueInsn() const;
649 inline Instruction
*getInsn() const; // use when uniqueness is certain
651 inline int refCount() { return uses
.size(); }
653 inline LValue
*asLValue();
654 inline Symbol
*asSym();
655 inline ImmediateValue
*asImm();
656 inline const Symbol
*asSym() const;
657 inline const ImmediateValue
*asImm() const;
659 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
661 static inline Value
*get(Iterator
&);
663 unordered_set
<ValueRef
*> uses
;
664 std::list
<ValueDef
*> defs
;
665 typedef unordered_set
<ValueRef
*>::iterator UseIterator
;
666 typedef unordered_set
<ValueRef
*>::const_iterator UseCIterator
;
667 typedef std::list
<ValueDef
*>::iterator DefIterator
;
668 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
673 // TODO: these should be in LValue:
678 class LValue
: public Value
681 LValue(Function
*, DataFile file
);
682 LValue(Function
*, LValue
*);
685 virtual bool isUniform() const;
687 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
689 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
692 unsigned compMask
: 8; // compound/component mask
693 unsigned compound
: 1; // used by RA, value involved in split/merge
695 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
696 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
699 class Symbol
: public Value
702 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
705 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
707 virtual bool equals(const Value
*that
, bool strict
) const;
709 virtual bool isUniform() const;
711 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
713 // print with indirect values
714 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
716 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
719 reg
.fileIndex
= fileIndex
;
722 inline void setOffset(int32_t offset
);
723 inline void setAddress(Symbol
*base
, int32_t offset
);
724 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
726 inline const Symbol
*getBase() const { return baseSym
; }
729 Symbol
*baseSym
; // array base for Symbols representing array elements
732 class ImmediateValue
: public Value
736 ImmediateValue(Program
*, uint32_t);
737 ImmediateValue(Program
*, float);
738 ImmediateValue(Program
*, double);
739 // NOTE: not added to program with
740 ImmediateValue(const ImmediateValue
*, DataType ty
);
741 ~ImmediateValue() { };
743 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
745 virtual bool equals(const Value
*that
, bool strict
) const;
747 // these only work if 'type' is valid (we mostly use untyped literals):
748 bool isInteger(const int ival
) const; // ival is cast to this' type
749 bool isNegative() const;
754 // for constant folding:
755 ImmediateValue
operator+(const ImmediateValue
&) const;
756 ImmediateValue
operator-(const ImmediateValue
&) const;
757 ImmediateValue
operator*(const ImmediateValue
&) const;
758 ImmediateValue
operator/(const ImmediateValue
&) const;
760 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
762 bool compare(CondCode cc
, float fval
) const;
764 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
771 Instruction(Function
*, operation
, DataType
);
772 virtual ~Instruction();
774 virtual Instruction
*clone(ClonePolicy
<Function
>&,
775 Instruction
* = NULL
) const;
777 void setDef(int i
, Value
*);
778 void setSrc(int s
, Value
*);
779 void setSrc(int s
, const ValueRef
&);
780 void swapSources(int a
, int b
);
781 void moveSources(int s
, int delta
);
782 bool setIndirect(int s
, int dim
, Value
*);
784 inline ValueRef
& src(int s
) { return srcs
[s
]; }
785 inline ValueDef
& def(int s
) { return defs
[s
]; }
786 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
787 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
789 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
790 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
791 inline Value
*getIndirect(int s
, int dim
) const;
793 inline bool defExists(unsigned d
) const
795 return d
< defs
.size() && defs
[d
].exists();
797 inline bool srcExists(unsigned s
) const
799 return s
< srcs
.size() && srcs
[s
].exists();
802 inline bool constrainedDefs() const;
804 bool setPredicate(CondCode ccode
, Value
*);
805 inline Value
*getPredicate() const;
806 bool writesPredicate() const;
807 inline bool isPredicated() const { return predSrc
>= 0; }
809 inline void setFlagsSrc(int s
, Value
*);
810 inline void setFlagsDef(int d
, Value
*);
811 inline bool usesFlags() const { return flagsSrc
>= 0; }
813 unsigned int defCount() const { return defs
.size(); };
814 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
815 unsigned int srcCount() const { return srcs
.size(); };
816 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
818 // save & remove / set indirect[0,1] and predicate source
819 void takeExtraSources(int s
, Value
*[3]);
820 void putExtraSources(int s
, Value
*[3]);
822 inline void setType(DataType type
) { dType
= sType
= type
; }
824 inline void setType(DataType dtype
, DataType stype
)
830 inline bool isPseudo() const { return op
< OP_MOV
; }
833 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
834 bool isActionEqual(const Instruction
*) const;
835 bool isResultEqual(const Instruction
*) const;
839 inline CmpInstruction
*asCmp();
840 inline TexInstruction
*asTex();
841 inline FlowInstruction
*asFlow();
842 inline const TexInstruction
*asTex() const;
843 inline const CmpInstruction
*asCmp() const;
844 inline const FlowInstruction
*asFlow() const;
850 int serial
; // CFG order
853 DataType dType
; // destination or defining type
854 DataType sType
; // source or secondary type
859 uint16_t subOp
; // quadop, 1 for mul-high, etc.
861 unsigned encSize
: 4; // encoding size in bytes
862 unsigned saturate
: 1; // to [0.0f, 1.0f]
863 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
864 unsigned fixed
: 1; // prevent dead code elimination
865 unsigned terminator
: 1; // end of basic block
866 unsigned ftz
: 1; // flush denormal to zero
867 unsigned dnz
: 1; // denormals, NaN are zero
868 unsigned ipa
: 4; // interpolation mode
870 unsigned perPatch
: 1;
871 unsigned exit
: 1; // terminate program after insn
872 unsigned mask
: 4; // for vector ops
874 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
880 uint32_t sched
; // scheduling data (NOTE: maybe move to separate storage)
885 std::deque
<ValueDef
> defs
; // no gaps !
886 std::deque
<ValueRef
> srcs
; // no gaps !
888 // instruction specific methods:
889 // (don't want to subclass, would need more constructors and memory pools)
891 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
893 unsigned int getInterpMode() const { return ipa
& 0x3; }
894 unsigned int getSampleMode() const { return ipa
& 0xc; }
902 TXQ_DIMS
, /* x, y, z, levels */
903 TXQ_TYPE
, /* ?, ?, samples, ? */
911 class TexInstruction
: public Instruction
917 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
919 const char *getName() const { return descTable
[target
].name
; }
920 unsigned int getArgCount() const { return descTable
[target
].argc
; }
921 unsigned int getDim() const { return descTable
[target
].dim
; }
922 int isArray() const { return descTable
[target
].array
? 1 : 0; }
923 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
924 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
926 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
930 target
= TEX_TARGET_2D_ARRAY
;
932 target
= TEX_TARGET_2D
;
936 Target
& operator=(TexTarget targ
)
938 assert(targ
< TEX_TARGET_COUNT
);
943 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
944 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
946 enum TexTarget
getEnum() const { return target
; }
959 static const struct Desc descTable
[TEX_TARGET_COUNT
];
962 enum TexTarget target
;
975 static const struct ImgFormatDesc formatTable
[IMG_FORMAT_COUNT
];
978 TexInstruction(Function
*, operation
);
979 virtual ~TexInstruction();
981 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
982 Instruction
* = NULL
) const;
984 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
991 void setIndirectR(Value
*);
992 void setIndirectS(Value
*);
993 inline Value
*getIndirectR() const;
994 inline Value
*getIndirectS() const;
1002 int8_t rIndirectSrc
;
1003 int8_t sIndirectSrc
;
1008 bool liveOnly
; // only execute on live pixels of a quad (optimization)
1012 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
1013 int8_t offset
[3]; // only used on nv50
1015 enum TexQuery query
;
1016 const struct ImgFormatDesc
*format
;
1021 ValueRef offset
[4][3];
1024 class CmpInstruction
: public Instruction
1027 CmpInstruction(Function
*, operation
);
1029 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
1030 Instruction
* = NULL
) const;
1032 void setCondition(CondCode cond
) { setCond
= cond
; }
1033 CondCode
getCondition() const { return setCond
; }
1039 class FlowInstruction
: public Instruction
1042 FlowInstruction(Function
*, operation
, void *target
);
1044 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
1045 Instruction
* = NULL
) const;
1048 unsigned allWarp
: 1;
1049 unsigned absolute
: 1;
1051 unsigned builtin
: 1; // true for calls to emulation code
1052 unsigned indirect
: 1; // target in src(0)
1064 BasicBlock(Function
*);
1067 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
1069 inline int getId() const { return id
; }
1070 inline unsigned int getInsnCount() const { return numInsns
; }
1071 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
1073 bool dominatedBy(BasicBlock
*bb
);
1074 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
1076 // returns mask of conditional out blocks
1077 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1078 unsigned int initiatesSimpleConditional() const;
1081 Function
*getFunction() const { return func
; }
1082 Program
*getProgram() const { return program
; }
1084 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
1085 Instruction
*getPhi() const { return phi
; }
1086 Instruction
*getFirst() const { return phi
? phi
: entry
; }
1087 Instruction
*getExit() const { return exit
; }
1089 void insertHead(Instruction
*);
1090 void insertTail(Instruction
*);
1091 void insertBefore(Instruction
*, Instruction
*);
1092 void insertAfter(Instruction
*, Instruction
*);
1093 void remove(Instruction
*);
1094 void permuteAdjacent(Instruction
*, Instruction
*);
1096 BasicBlock
*idom() const;
1098 // NOTE: currently does not rebuild the dominator tree
1099 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1100 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1102 DLList
& getDF() { return df
; }
1103 DLList::Iterator
iterDF() { return df
.iterator(); }
1105 static inline BasicBlock
*get(Iterator
&);
1106 static inline BasicBlock
*get(Graph::Node
*);
1109 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1118 Instruction
*joinAt
; // for quick reference
1120 bool explicitCont
; // loop headers: true if loop contains continue stmts
1130 unsigned int numInsns
;
1136 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1142 Function(Program
*, const char *name
, uint32_t label
);
1145 static inline Function
*get(Graph::Node
*node
);
1147 inline Program
*getProgram() const { return prog
; }
1148 inline const char *getName() const { return name
; }
1149 inline int getId() const { return id
; }
1150 inline uint32_t getLabel() const { return label
; }
1153 void printLiveIntervals() const;
1154 void printCFGraph(const char *filePath
);
1156 bool setEntry(BasicBlock
*);
1157 bool setExit(BasicBlock
*);
1159 unsigned int orderInstructions(ArrayList
&);
1161 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1162 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1163 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1165 inline LValue
*getLValue(int id
);
1167 void buildLiveSets();
1168 void buildDefSets();
1169 bool convertToSSA();
1172 std::deque
<ValueDef
> ins
;
1173 std::deque
<ValueRef
> outs
;
1174 std::deque
<Value
*> clobbers
;
1177 Graph::Node
*cfgExit
;
1179 Graph::Node call
; // node in the call graph
1181 BasicBlock
**bbArray
; // BBs in emission order
1184 unsigned int loopNestingBound
;
1192 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1195 ArrayList allBBlocks
;
1197 ArrayList allLValues
;
1200 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1201 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1206 const char *const name
;
1213 CG_STAGE_SSA
, // expected directly before register allocation
1223 TYPE_TESSELLATION_CONTROL
,
1224 TYPE_TESSELLATION_EVAL
,
1230 Program(Type type
, Target
*targ
);
1235 Type
getType() const { return progType
; }
1237 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1238 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1239 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1241 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1242 bool makeFromSM4(struct nv50_ir_prog_info
*);
1243 bool convertToSSA();
1244 bool optimizeSSA(int level
);
1245 bool optimizePostRA(int level
);
1246 bool registerAllocation();
1247 bool emitBinary(struct nv50_ir_prog_info
*);
1249 const Target
*getTarget() const { return target
; }
1252 void emitSymbolTable(struct nv50_ir_prog_info
*);
1262 ArrayList allRValues
;
1266 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1270 MemoryPool mem_Instruction
;
1271 MemoryPool mem_CmpInstruction
;
1272 MemoryPool mem_TexInstruction
;
1273 MemoryPool mem_FlowInstruction
;
1274 MemoryPool mem_LValue
;
1275 MemoryPool mem_Symbol
;
1276 MemoryPool mem_ImmediateValue
;
1281 void *targetPriv
; // e.g. to carry information between passes
1283 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1285 void releaseInstruction(Instruction
*);
1286 void releaseValue(Value
*);
1289 // TODO: add const version
1293 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1294 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1297 // return false to continue with next entity on next higher level
1298 virtual bool visit(Function
*) { return true; }
1299 virtual bool visit(BasicBlock
*) { return true; }
1300 virtual bool visit(Instruction
*) { return false; }
1302 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1303 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1311 // =============================================================================
1313 #include "codegen/nv50_ir_inlines.h"
1315 } // namespace nv50_ir
1317 #endif // __NV50_IR_H__