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
61 // extended multiply-add (GM107+), does a lot of things.
62 // see envytools for detailed documentation
75 OP_SAT
, // CLAMP(f32, 0.0, 1.0)
80 OP_SET_AND
, // dst = (src0 CMP src1) & src2
84 OP_SELP
, // dst = src2 ? src0 : src1
85 OP_SLCT
, // dst = (src2 CMP 0) ? src0 : src1
92 OP_EXP
, // exponential (base M_E)
93 OP_LOG
, // natural logarithm
106 OP_BRKPT
, // breakpoint (not related to loops)
107 OP_JOINAT
, // push control flow convergence point
111 OP_MEMBAR
, // memory barrier (mfence, lfence, sfence)
112 OP_VFETCH
, // indirection 0 in attribute space, indirection 1 is vertex base
113 OP_PFETCH
, // fetch base address of vertex src0 (immediate) [+ src1]
114 OP_AFETCH
, // fetch base address of shader input (a[%r1+0x10])
118 OP_EMIT
, // emit vertex
119 OP_RESTART
, // restart primitive
121 OP_TXB
, // texture bias
122 OP_TXL
, // texure lod
123 OP_TXF
, // texel fetch
124 OP_TXQ
, // texture size query
125 OP_TXD
, // texture derivatives
126 OP_TXG
, // texture gather
127 OP_TXLQ
, // texture query lod
128 OP_TEXCSAA
, // texture op for coverage sampling
129 OP_TEXPREP
, // turn cube map array into 2d array coordinates
130 OP_SULDB
, // surface load (raw)
131 OP_SULDP
, // surface load (formatted)
132 OP_SUSTB
, // surface store (raw)
133 OP_SUSTP
, // surface store (formatted)
135 OP_SUREDP
, // surface reduction (atomic op)
136 OP_SULEA
, // surface load effective address
137 OP_SUBFM
, // surface bitfield manipulation
138 OP_SUCLAMP
, // clamp surface coordinates
139 OP_SUEAU
, // surface effective address
140 OP_SUQ
, // surface query
141 OP_MADSP
, // special integer multiply-add
142 OP_TEXBAR
, // texture dependency barrier
145 OP_RDSV
, // read system value
146 OP_WRSV
, // write system value
147 OP_PIXLD
, // get info about raster object or surfaces
151 OP_POPCNT
, // bitcount(src0 & src1)
152 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
153 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
154 OP_BFIND
, // find highest/lowest set bit
155 OP_BREV
, // bitfield reverse
156 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
158 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
159 OP_VADD
, // byte/word vector operations
168 OP_CCTL
, // cache control
169 OP_SHFL
, // warp shuffle
171 OP_BUFQ
, // buffer query
176 // various instruction-specific modifier definitions Instruction::subOp
177 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
178 #define NV50_IR_SUBOP_MUL_HIGH 1
179 #define NV50_IR_SUBOP_EMIT_RESTART 1
180 #define NV50_IR_SUBOP_LDC_IL 1
181 #define NV50_IR_SUBOP_LDC_IS 2
182 #define NV50_IR_SUBOP_LDC_ISL 3
183 #define NV50_IR_SUBOP_SHIFT_WRAP 1
184 #define NV50_IR_SUBOP_SHIFT_HIGH 2
185 #define NV50_IR_SUBOP_EMU_PRERET 1
186 #define NV50_IR_SUBOP_TEXBAR(n) n
187 #define NV50_IR_SUBOP_MOV_FINAL 1
188 #define NV50_IR_SUBOP_EXTBF_REV 1
189 #define NV50_IR_SUBOP_BFIND_SAMT 1
190 #define NV50_IR_SUBOP_RCPRSQ_64H 1
191 #define NV50_IR_SUBOP_PERMT_F4E 1
192 #define NV50_IR_SUBOP_PERMT_B4E 2
193 #define NV50_IR_SUBOP_PERMT_RC8 3
194 #define NV50_IR_SUBOP_PERMT_ECL 4
195 #define NV50_IR_SUBOP_PERMT_ECR 5
196 #define NV50_IR_SUBOP_PERMT_RC16 6
197 #define NV50_IR_SUBOP_BAR_SYNC 0
198 #define NV50_IR_SUBOP_BAR_ARRIVE 1
199 #define NV50_IR_SUBOP_BAR_RED_AND 2
200 #define NV50_IR_SUBOP_BAR_RED_OR 3
201 #define NV50_IR_SUBOP_BAR_RED_POPC 4
202 #define NV50_IR_SUBOP_MEMBAR_L 1
203 #define NV50_IR_SUBOP_MEMBAR_S 2
204 #define NV50_IR_SUBOP_MEMBAR_M 3
205 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
206 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
207 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
208 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
209 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
210 #define NV50_IR_SUBOP_MEMBAR(d,s) \
211 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
212 #define NV50_IR_SUBOP_ATOM_ADD 0
213 #define NV50_IR_SUBOP_ATOM_MIN 1
214 #define NV50_IR_SUBOP_ATOM_MAX 2
215 #define NV50_IR_SUBOP_ATOM_INC 3
216 #define NV50_IR_SUBOP_ATOM_DEC 4
217 #define NV50_IR_SUBOP_ATOM_AND 5
218 #define NV50_IR_SUBOP_ATOM_OR 6
219 #define NV50_IR_SUBOP_ATOM_XOR 7
220 #define NV50_IR_SUBOP_ATOM_CAS 8
221 #define NV50_IR_SUBOP_ATOM_EXCH 9
222 #define NV50_IR_SUBOP_CCTL_IV 5
223 #define NV50_IR_SUBOP_CCTL_IVALL 6
224 #define NV50_IR_SUBOP_SUST_IGN 0
225 #define NV50_IR_SUBOP_SUST_TRAP 1
226 #define NV50_IR_SUBOP_SUST_SDCL 3
227 #define NV50_IR_SUBOP_SULD_ZERO 0
228 #define NV50_IR_SUBOP_SULD_TRAP 1
229 #define NV50_IR_SUBOP_SULD_SDCL 3
230 #define NV50_IR_SUBOP_SUBFM_3D 1
231 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
232 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
233 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
234 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
235 #define NV50_IR_SUBOP_PIXLD_COUNT 0
236 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
237 #define NV50_IR_SUBOP_PIXLD_COVERED 2
238 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
239 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
240 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
241 #define NV50_IR_SUBOP_SHFL_IDX 0
242 #define NV50_IR_SUBOP_SHFL_UP 1
243 #define NV50_IR_SUBOP_SHFL_DOWN 2
244 #define NV50_IR_SUBOP_SHFL_BFLY 3
245 #define NV50_IR_SUBOP_LOAD_LOCKED 1
246 #define NV50_IR_SUBOP_STORE_UNLOCKED 2
247 #define NV50_IR_SUBOP_MADSP_SD 0xffff
248 // Yes, we could represent those with DataType.
249 // Or put the type into operation and have a couple 1000 values in that enum.
250 // This will have to do for now.
251 // The bitfields are supposed to correspond to nve4 ISA.
252 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
253 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
254 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
255 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
256 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
257 #define NV50_IR_SUBOP_VOTE_ALL 0
258 #define NV50_IR_SUBOP_VOTE_ANY 1
259 #define NV50_IR_SUBOP_VOTE_UNI 2
260 #define NV50_IR_SUBOP_LOP3_LUT_SRC0 0xf0
261 #define NV50_IR_SUBOP_LOP3_LUT_SRC1 0xcc
262 #define NV50_IR_SUBOP_LOP3_LUT_SRC2 0xaa
263 #define NV50_IR_SUBOP_LOP3_LUT(exp) ({ \
264 uint8_t a = NV50_IR_SUBOP_LOP3_LUT_SRC0; \
265 uint8_t b = NV50_IR_SUBOP_LOP3_LUT_SRC1; \
266 uint8_t c = NV50_IR_SUBOP_LOP3_LUT_SRC2; \
270 #define NV50_IR_SUBOP_MINMAX_LOW 1
271 #define NV50_IR_SUBOP_MINMAX_MED 2
272 #define NV50_IR_SUBOP_MINMAX_HIGH 3
274 // xmad(src0, src1, 0) << 16 + src2
275 #define NV50_IR_SUBOP_XMAD_PSL (1 << 0)
276 // (xmad(src0, src1, src2) & 0xffff) | (src1 << 16)
277 #define NV50_IR_SUBOP_XMAD_MRG (1 << 1)
278 // xmad(src0, src1, src2.lo)
279 #define NV50_IR_SUBOP_XMAD_CLO (1 << 2)
280 // xmad(src0, src1, src2.hi)
281 #define NV50_IR_SUBOP_XMAD_CHI (2 << 2)
282 // if both operands to the multiplication are non-zero, subtract 65536 for each
284 #define NV50_IR_SUBOP_XMAD_CSFU (3 << 2)
285 // xmad(src0, src1, src2) + src1 << 16
286 #define NV50_IR_SUBOP_XMAD_CBCC (4 << 2)
287 #define NV50_IR_SUBOP_XMAD_CMODE_SHIFT 2
288 #define NV50_IR_SUBOP_XMAD_CMODE_MASK (0x7 << NV50_IR_SUBOP_XMAD_CMODE_SHIFT)
290 // use the high 16 bits instead of the low 16 bits for the multiplication.
291 // if the instruction's sType is signed, sign extend the operand from 16 bits
292 // to 32 before multiplication.
293 #define NV50_IR_SUBOP_XMAD_H1_SHIFT 5
294 #define NV50_IR_SUBOP_XMAD_H1(i) (1 << (NV50_IR_SUBOP_XMAD_H1_SHIFT + (i)))
295 #define NV50_IR_SUBOP_XMAD_H1_MASK (0x3 << NV50_IR_SUBOP_XMAD_H1_SHIFT)
306 TYPE_U64
, // 64 bit operations are only lowered after register allocation
318 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
321 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
349 ROUND_M
, // towards -inf
350 ROUND_Z
, // towards 0
351 ROUND_P
, // towards +inf
352 ROUND_NI
, // nearest integer
353 ROUND_MI
, // to integer towards -inf
354 ROUND_ZI
, // to integer towards 0
355 ROUND_PI
, // to integer towards +inf
360 CACHE_CA
, // cache at all levels
361 CACHE_WB
= CACHE_CA
, // cache write back
362 CACHE_CG
, // cache at global level
363 CACHE_CS
, // cache streaming
364 CACHE_CV
, // cache as volatile
365 CACHE_WT
= CACHE_CV
// cache write-through
372 FILE_PREDICATE
, // boolean predicate
373 FILE_FLAGS
, // zero/sign/carry/overflow bits
375 LAST_REGISTER_FILE
= FILE_ADDRESS
,
395 TEX_TARGET_1D_SHADOW
,
396 TEX_TARGET_2D_SHADOW
,
397 TEX_TARGET_CUBE_SHADOW
,
400 TEX_TARGET_2D_MS_ARRAY
,
401 TEX_TARGET_CUBE_ARRAY
,
402 TEX_TARGET_1D_ARRAY_SHADOW
,
403 TEX_TARGET_2D_ARRAY_SHADOW
,
405 TEX_TARGET_RECT_SHADOW
,
406 TEX_TARGET_CUBE_ARRAY_SHADOW
,
479 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
529 class CmpInstruction
;
530 class TexInstruction
;
531 class FlowInstruction
;
536 class ImmediateValue
;
541 int8_t fileIndex
; // signed, may be indirect for CONST[]
542 uint8_t size
; // this should match the Instruction type's size
543 DataType type
; // mainly for pretty printing
545 uint64_t u64
; // immediate values
555 int32_t offset
; // offset from 0 (base of address space)
556 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
564 // precedence: NOT after SAT after NEG after ABS
565 #define NV50_IR_MOD_ABS (1 << 0)
566 #define NV50_IR_MOD_NEG (1 << 1)
567 #define NV50_IR_MOD_SAT (1 << 2)
568 #define NV50_IR_MOD_NOT (1 << 3)
569 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
571 #define NV50_IR_INTERP_MODE_MASK 0x3
572 #define NV50_IR_INTERP_LINEAR (0 << 0)
573 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
574 #define NV50_IR_INTERP_FLAT (2 << 0)
575 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
576 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
577 #define NV50_IR_INTERP_DEFAULT (0 << 2)
578 #define NV50_IR_INTERP_CENTROID (1 << 2)
579 #define NV50_IR_INTERP_OFFSET (2 << 2)
580 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
582 // do we really want this to be a class ?
586 Modifier() : bits(0) { }
587 Modifier(unsigned int m
) : bits(m
) { }
588 Modifier(operation op
);
590 // @return new Modifier applying a after b (asserts if unrepresentable)
591 Modifier
operator*(const Modifier
) const;
592 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
593 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
594 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
596 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
597 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
598 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
600 operation
getOp() const;
602 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
603 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
605 inline operator bool() const { return bits
? true : false; }
607 void applyTo(ImmediateValue
&imm
) const;
609 int print(char *buf
, size_t size
) const;
618 ValueRef(Value
* = NULL
);
619 ValueRef(const ValueRef
&);
622 inline bool exists() const { return value
!= NULL
; }
625 void set(const ValueRef
&);
626 inline Value
*get() const { return value
; }
627 inline Value
*rep() const;
629 inline Instruction
*getInsn() const { return insn
; }
630 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
632 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
633 inline const ValueRef
*getIndirect(int dim
) const;
635 inline DataFile
getFile() const;
636 inline unsigned getSize() const;
638 // SSA: return eventual (traverse MOVs) literal value, if it exists
639 bool getImmediate(ImmediateValue
&) const;
643 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
645 bool usedAsPtr
; // for printing
655 ValueDef(Value
* = NULL
);
656 ValueDef(const ValueDef
&);
659 inline bool exists() const { return value
!= NULL
; }
661 inline Value
*get() const { return value
; }
662 inline Value
*rep() const;
664 bool mayReplace(const ValueRef
&);
665 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
667 inline Instruction
*getInsn() const { return insn
; }
668 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
670 inline DataFile
getFile() const;
671 inline unsigned getSize() const;
673 inline void setSSA(LValue
*);
674 inline const LValue
*preSSA() const;
677 Value
*value
; // should make this LValue * ...
678 LValue
*origin
; // pre SSA value
688 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
690 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
692 virtual bool equals(const Value
*, bool strict
= false) const;
693 virtual bool interfers(const Value
*) const;
694 virtual bool isUniform() const { return true; }
696 inline Value
*rep() const { return join
; }
698 inline Instruction
*getUniqueInsn() const;
699 inline Instruction
*getInsn() const; // use when uniqueness is certain
701 inline int refCount() { return uses
.size(); }
703 inline LValue
*asLValue();
704 inline Symbol
*asSym();
705 inline ImmediateValue
*asImm();
706 inline const Symbol
*asSym() const;
707 inline const ImmediateValue
*asImm() const;
709 inline bool inFile(DataFile f
) const { return reg
.file
== f
; }
711 static inline Value
*get(Iterator
&);
713 unordered_set
<ValueRef
*> uses
;
714 std::list
<ValueDef
*> defs
;
715 typedef unordered_set
<ValueRef
*>::iterator UseIterator
;
716 typedef unordered_set
<ValueRef
*>::const_iterator UseCIterator
;
717 typedef std::list
<ValueDef
*>::iterator DefIterator
;
718 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
723 // TODO: these should be in LValue:
728 class LValue
: public Value
731 LValue(Function
*, DataFile file
);
732 LValue(Function
*, LValue
*);
735 virtual bool isUniform() const;
737 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
739 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
742 unsigned compMask
: 8; // compound/component mask
743 unsigned compound
: 1; // used by RA, value involved in split/merge
745 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
746 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
749 class Symbol
: public Value
752 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
755 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
757 virtual bool equals(const Value
*that
, bool strict
) const;
759 virtual bool isUniform() const;
761 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
763 // print with indirect values
764 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
766 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
769 reg
.fileIndex
= fileIndex
;
772 inline void setOffset(int32_t offset
);
773 inline void setAddress(Symbol
*base
, int32_t offset
);
774 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
776 inline const Symbol
*getBase() const { return baseSym
; }
779 Symbol
*baseSym
; // array base for Symbols representing array elements
782 class ImmediateValue
: public Value
786 ImmediateValue(Program
*, uint32_t);
787 ImmediateValue(Program
*, float);
788 ImmediateValue(Program
*, double);
789 // NOTE: not added to program with
790 ImmediateValue(const ImmediateValue
*, DataType ty
);
791 ~ImmediateValue() { };
793 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
795 virtual bool equals(const Value
*that
, bool strict
) const;
797 // these only work if 'type' is valid (we mostly use untyped literals):
798 bool isInteger(const int ival
) const; // ival is cast to this' type
799 bool isNegative() const;
804 // for constant folding:
805 ImmediateValue
operator+(const ImmediateValue
&) const;
806 ImmediateValue
operator-(const ImmediateValue
&) const;
807 ImmediateValue
operator*(const ImmediateValue
&) const;
808 ImmediateValue
operator/(const ImmediateValue
&) const;
810 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
812 bool compare(CondCode cc
, float fval
) const;
814 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
821 Instruction(Function
*, operation
, DataType
);
822 virtual ~Instruction();
824 virtual Instruction
*clone(ClonePolicy
<Function
>&,
825 Instruction
* = NULL
) const;
827 void setDef(int i
, Value
*);
828 void setSrc(int s
, Value
*);
829 void setSrc(int s
, const ValueRef
&);
830 void swapSources(int a
, int b
);
831 void moveSources(int s
, int delta
);
832 bool setIndirect(int s
, int dim
, Value
*);
834 inline ValueRef
& src(int s
) { return srcs
[s
]; }
835 inline ValueDef
& def(int s
) { return defs
[s
]; }
836 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
837 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
839 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
840 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
841 inline Value
*getIndirect(int s
, int dim
) const;
843 inline bool defExists(unsigned d
) const
845 return d
< defs
.size() && defs
[d
].exists();
847 inline bool srcExists(unsigned s
) const
849 return s
< srcs
.size() && srcs
[s
].exists();
852 inline bool constrainedDefs() const;
854 bool setPredicate(CondCode ccode
, Value
*);
855 inline Value
*getPredicate() const;
856 bool writesPredicate() const;
857 inline bool isPredicated() const { return predSrc
>= 0; }
859 inline void setFlagsSrc(int s
, Value
*);
860 inline void setFlagsDef(int d
, Value
*);
861 inline bool usesFlags() const { return flagsSrc
>= 0; }
863 unsigned int defCount() const { return defs
.size(); };
864 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
865 unsigned int srcCount() const { return srcs
.size(); };
866 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
868 // save & remove / set indirect[0,1] and predicate source
869 void takeExtraSources(int s
, Value
*[3]);
870 void putExtraSources(int s
, Value
*[3]);
872 inline void setType(DataType type
) { dType
= sType
= type
; }
874 inline void setType(DataType dtype
, DataType stype
)
880 inline bool isPseudo() const { return op
< OP_MOV
; }
883 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
884 bool isActionEqual(const Instruction
*) const;
885 bool isResultEqual(const Instruction
*) const;
887 // check whether the defs interfere with srcs and defs of another instruction
888 bool canCommuteDefDef(const Instruction
*) const;
889 bool canCommuteDefSrc(const Instruction
*) const;
893 inline CmpInstruction
*asCmp();
894 inline TexInstruction
*asTex();
895 inline FlowInstruction
*asFlow();
896 inline const TexInstruction
*asTex() const;
897 inline const CmpInstruction
*asCmp() const;
898 inline const FlowInstruction
*asFlow() const;
904 int serial
; // CFG order
907 DataType dType
; // destination or defining type
908 DataType sType
; // source or secondary type
913 uint16_t subOp
; // quadop, 1 for mul-high, etc.
915 unsigned encSize
: 5; // encoding size in bytes
916 unsigned saturate
: 1; // to [0.0f, 1.0f]
917 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
918 unsigned fixed
: 1; // prevent dead code elimination
919 unsigned terminator
: 1; // end of basic block
920 unsigned ftz
: 1; // flush denormal to zero
921 unsigned dnz
: 1; // denormals, NaN are zero
922 unsigned ipa
: 4; // interpolation mode
924 unsigned perPatch
: 1;
925 unsigned exit
: 1; // terminate program after insn
926 unsigned mask
: 4; // for vector ops
927 // prevent algebraic optimisations that aren't bit-for-bit identical
928 unsigned precise
: 1;
930 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
936 uint32_t sched
; // scheduling data (NOTE: maybe move to separate storage)
941 std::deque
<ValueDef
> defs
; // no gaps !
942 std::deque
<ValueRef
> srcs
; // no gaps !
944 // instruction specific methods:
945 // (don't want to subclass, would need more constructors and memory pools)
947 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
949 unsigned int getInterpMode() const { return ipa
& 0x3; }
950 unsigned int getSampleMode() const { return ipa
& 0xc; }
958 TXQ_DIMS
, /* x, y, z, levels */
959 TXQ_TYPE
, /* ?, ?, samples, ? */
967 class TexInstruction
: public Instruction
973 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
975 const char *getName() const { return descTable
[target
].name
; }
976 unsigned int getArgCount() const { return descTable
[target
].argc
; }
977 unsigned int getDim() const { return descTable
[target
].dim
; }
978 int isArray() const { return descTable
[target
].array
? 1 : 0; }
979 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
980 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
982 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
986 target
= TEX_TARGET_2D_ARRAY
;
988 target
= TEX_TARGET_2D
;
992 Target
& operator=(TexTarget targ
)
994 assert(targ
< TEX_TARGET_COUNT
);
999 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
1000 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
1002 enum TexTarget
getEnum() const { return target
; }
1015 static const struct Desc descTable
[TEX_TARGET_COUNT
];
1018 enum TexTarget target
;
1022 struct ImgFormatDesc
1031 static const struct ImgFormatDesc formatTable
[IMG_FORMAT_COUNT
];
1032 static const struct ImgFormatDesc
*translateImgFormat(
1033 enum pipe_format format
);
1036 TexInstruction(Function
*, operation
);
1037 virtual ~TexInstruction();
1039 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
1040 Instruction
* = NULL
) const;
1042 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
1049 void setIndirectR(Value
*);
1050 void setIndirectS(Value
*);
1051 inline Value
*getIndirectR() const;
1052 inline Value
*getIndirectS() const;
1060 int8_t rIndirectSrc
;
1061 int8_t sIndirectSrc
;
1066 bool liveOnly
; // only execute on live pixels of a quad (optimization)
1071 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
1072 int8_t offset
[3]; // only used on nv50
1074 enum TexQuery query
;
1075 const struct ImgFormatDesc
*format
;
1077 bool scalar
; // for GM107s TEXS, TLDS, TLD4S
1082 ValueRef offset
[4][3];
1085 class CmpInstruction
: public Instruction
1088 CmpInstruction(Function
*, operation
);
1090 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
1091 Instruction
* = NULL
) const;
1093 void setCondition(CondCode cond
) { setCond
= cond
; }
1094 CondCode
getCondition() const { return setCond
; }
1100 class FlowInstruction
: public Instruction
1103 FlowInstruction(Function
*, operation
, void *target
);
1105 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
1106 Instruction
* = NULL
) const;
1109 unsigned allWarp
: 1;
1110 unsigned absolute
: 1;
1112 unsigned builtin
: 1; // true for calls to emulation code
1113 unsigned indirect
: 1; // target in src(0)
1125 BasicBlock(Function
*);
1128 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
1130 inline int getId() const { return id
; }
1131 inline unsigned int getInsnCount() const { return numInsns
; }
1132 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
1134 bool dominatedBy(BasicBlock
*bb
);
1135 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
1137 // returns mask of conditional out blocks
1138 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
1139 unsigned int initiatesSimpleConditional() const;
1142 Function
*getFunction() const { return func
; }
1143 Program
*getProgram() const { return program
; }
1145 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
1146 Instruction
*getPhi() const { return phi
; }
1147 Instruction
*getFirst() const { return phi
? phi
: entry
; }
1148 Instruction
*getExit() const { return exit
; }
1150 void insertHead(Instruction
*);
1151 void insertTail(Instruction
*);
1152 void insertBefore(Instruction
*, Instruction
*);
1153 void insertAfter(Instruction
*, Instruction
*);
1154 void remove(Instruction
*);
1155 void permuteAdjacent(Instruction
*, Instruction
*);
1157 BasicBlock
*idom() const;
1159 // NOTE: currently does not rebuild the dominator tree
1160 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1161 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1163 DLList
& getDF() { return df
; }
1164 DLList::Iterator
iterDF() { return df
.iterator(); }
1166 static inline BasicBlock
*get(Iterator
&);
1167 static inline BasicBlock
*get(Graph::Node
*);
1170 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1179 Instruction
*joinAt
; // for quick reference
1181 bool explicitCont
; // loop headers: true if loop contains continue stmts
1191 unsigned int numInsns
;
1197 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1203 Function(Program
*, const char *name
, uint32_t label
);
1206 static inline Function
*get(Graph::Node
*node
);
1208 inline Program
*getProgram() const { return prog
; }
1209 inline const char *getName() const { return name
; }
1210 inline int getId() const { return id
; }
1211 inline uint32_t getLabel() const { return label
; }
1214 void printLiveIntervals() const;
1215 void printCFGraph(const char *filePath
);
1217 bool setEntry(BasicBlock
*);
1218 bool setExit(BasicBlock
*);
1220 unsigned int orderInstructions(ArrayList
&);
1222 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1223 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1224 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1226 inline LValue
*getLValue(int id
);
1228 void buildLiveSets();
1229 void buildDefSets();
1230 bool convertToSSA();
1233 std::deque
<ValueDef
> ins
;
1234 std::deque
<ValueRef
> outs
;
1235 std::deque
<Value
*> clobbers
;
1238 Graph::Node
*cfgExit
;
1240 Graph::Node call
; // node in the call graph
1242 BasicBlock
**bbArray
; // BBs in emission order
1245 unsigned int loopNestingBound
;
1253 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1256 ArrayList allBBlocks
;
1258 ArrayList allLValues
;
1261 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1262 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1267 const char *const name
;
1274 CG_STAGE_SSA
, // expected directly before register allocation
1284 TYPE_TESSELLATION_CONTROL
,
1285 TYPE_TESSELLATION_EVAL
,
1291 Program(Type type
, Target
*targ
);
1296 Type
getType() const { return progType
; }
1298 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1299 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1300 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1302 bool makeFromNIR(struct nv50_ir_prog_info
*);
1303 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1304 bool convertToSSA();
1305 bool optimizeSSA(int level
);
1306 bool optimizePostRA(int level
);
1307 bool registerAllocation();
1308 bool emitBinary(struct nv50_ir_prog_info
*);
1310 const Target
*getTarget() const { return target
; }
1313 void emitSymbolTable(struct nv50_ir_prog_info
*);
1323 ArrayList allRValues
;
1327 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1332 MemoryPool mem_Instruction
;
1333 MemoryPool mem_CmpInstruction
;
1334 MemoryPool mem_TexInstruction
;
1335 MemoryPool mem_FlowInstruction
;
1336 MemoryPool mem_LValue
;
1337 MemoryPool mem_Symbol
;
1338 MemoryPool mem_ImmediateValue
;
1343 void *targetPriv
; // e.g. to carry information between passes
1345 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1347 void releaseInstruction(Instruction
*);
1348 void releaseValue(Value
*);
1351 // TODO: add const version
1355 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1356 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1359 // return false to continue with next entity on next higher level
1360 virtual bool visit(Function
*) { return true; }
1361 virtual bool visit(BasicBlock
*) { return true; }
1362 virtual bool visit(Instruction
*) { return false; }
1364 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1365 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1373 // =============================================================================
1375 #include "codegen/nv50_ir_inlines.h"
1377 } // namespace nv50_ir
1379 #endif // __NV50_IR_H__