2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
33 #include "codegen/nv50_ir_util.h"
34 #include "codegen/nv50_ir_graph.h"
36 #include "codegen/nv50_ir_driver.h"
44 OP_UNION
, // unify a new definition and several source values
45 OP_SPLIT
, // $r0d -> { $r0, $r1 } ($r0d and $r0/$r1 will be coalesced)
46 OP_MERGE
, // opposite of split, e.g. combine 2 32 bit into a 64 bit value
47 OP_CONSTRAINT
, // copy values into consecutive registers
48 OP_MOV
, // simple copy, no modifiers allowed
51 OP_ADD
, // NOTE: add u64 + u32 is legal for targets w/o 64-bit integer adds
58 OP_SAD
, // abs(src0 - src1) + src2
69 OP_SAT
, // CLAMP(f32, 0.0, 1.0)
74 OP_SET_AND
, // dst = (src0 CMP src1) & src2
78 OP_SELP
, // dst = src2 ? src0 : src1
79 OP_SLCT
, // dst = (src2 CMP 0) ? src0 : src1
86 OP_EXP
, // exponential (base M_E)
87 OP_LOG
, // natural logarithm
100 OP_BRKPT
, // breakpoint (not related to loops)
101 OP_JOINAT
, // push control flow convergence point
105 OP_MEMBAR
, // memory barrier (mfence, lfence, sfence)
106 OP_VFETCH
, // indirection 0 in attribute space, indirection 1 is vertex base
107 OP_PFETCH
, // fetch base address of vertex src0 (immediate) [+ src1]
111 OP_EMIT
, // emit vertex
112 OP_RESTART
, // restart primitive
114 OP_TXB
, // texture bias
115 OP_TXL
, // texure lod
116 OP_TXF
, // texel fetch
117 OP_TXQ
, // texture size query
118 OP_TXD
, // texture derivatives
119 OP_TXG
, // texture gather
120 OP_TXLQ
, // texture query lod
121 OP_TEXCSAA
, // texture op for coverage sampling
122 OP_TEXPREP
, // turn cube map array into 2d array coordinates
123 OP_SULDB
, // surface load (raw)
124 OP_SULDP
, // surface load (formatted)
125 OP_SUSTB
, // surface store (raw)
126 OP_SUSTP
, // surface store (formatted)
128 OP_SUREDP
, // surface reduction (atomic op)
129 OP_SULEA
, // surface load effective address
130 OP_SUBFM
, // surface bitfield manipulation
131 OP_SUCLAMP
, // clamp surface coordinates
132 OP_SUEAU
, // surface effective address
133 OP_MADSP
, // special integer multiply-add
134 OP_TEXBAR
, // texture dependency barrier
137 OP_RDSV
, // read system value
138 OP_WRSV
, // write system value
139 OP_PIXLD
, // get info about raster object or surfaces
143 OP_POPCNT
, // bitcount(src0 & src1)
144 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
145 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
146 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
148 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
149 OP_VADD
, // byte/word vector operations
158 OP_CCTL
, // cache control
162 // various instruction-specific modifier definitions Instruction::subOp
163 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
164 #define NV50_IR_SUBOP_MUL_HIGH 1
165 #define NV50_IR_SUBOP_EMIT_RESTART 1
166 #define NV50_IR_SUBOP_LDC_IL 1
167 #define NV50_IR_SUBOP_LDC_IS 2
168 #define NV50_IR_SUBOP_LDC_ISL 3
169 #define NV50_IR_SUBOP_SHIFT_WRAP 1
170 #define NV50_IR_SUBOP_EMU_PRERET 1
171 #define NV50_IR_SUBOP_TEXBAR(n) n
172 #define NV50_IR_SUBOP_MOV_FINAL 1
173 #define NV50_IR_SUBOP_EXTBF_REV 1
174 #define NV50_IR_SUBOP_PERMT_F4E 1
175 #define NV50_IR_SUBOP_PERMT_B4E 2
176 #define NV50_IR_SUBOP_PERMT_RC8 3
177 #define NV50_IR_SUBOP_PERMT_ECL 4
178 #define NV50_IR_SUBOP_PERMT_ECR 5
179 #define NV50_IR_SUBOP_PERMT_RC16 6
180 #define NV50_IR_SUBOP_BAR_SYNC 0
181 #define NV50_IR_SUBOP_BAR_ARRIVE 1
182 #define NV50_IR_SUBOP_BAR_RED_AND 2
183 #define NV50_IR_SUBOP_BAR_RED_OR 3
184 #define NV50_IR_SUBOP_BAR_RED_POPC 4
185 #define NV50_IR_SUBOP_MEMBAR_L 1
186 #define NV50_IR_SUBOP_MEMBAR_S 2
187 #define NV50_IR_SUBOP_MEMBAR_M 3
188 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
189 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
190 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
191 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
192 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
193 #define NV50_IR_SUBOP_MEMBAR(d,s) \
194 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
195 #define NV50_IR_SUBOP_ATOM_ADD 0
196 #define NV50_IR_SUBOP_ATOM_MIN 1
197 #define NV50_IR_SUBOP_ATOM_MAX 2
198 #define NV50_IR_SUBOP_ATOM_INC 3
199 #define NV50_IR_SUBOP_ATOM_DEC 4
200 #define NV50_IR_SUBOP_ATOM_AND 5
201 #define NV50_IR_SUBOP_ATOM_OR 6
202 #define NV50_IR_SUBOP_ATOM_XOR 7
203 #define NV50_IR_SUBOP_ATOM_CAS 8
204 #define NV50_IR_SUBOP_ATOM_EXCH 9
205 #define NV50_IR_SUBOP_CCTL_IV 5
206 #define NV50_IR_SUBOP_CCTL_IVALL 6
207 #define NV50_IR_SUBOP_SUST_IGN 0
208 #define NV50_IR_SUBOP_SUST_TRAP 1
209 #define NV50_IR_SUBOP_SUST_SDCL 3
210 #define NV50_IR_SUBOP_SULD_ZERO 0
211 #define NV50_IR_SUBOP_SULD_TRAP 1
212 #define NV50_IR_SUBOP_SULD_SDCL 3
213 #define NV50_IR_SUBOP_SUBFM_3D 1
214 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
215 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
216 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
217 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
218 #define NV50_IR_SUBOP_PIXLD_COUNT 0
219 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
220 #define NV50_IR_SUBOP_PIXLD_COVERED 2
221 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
222 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
223 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
224 #define NV50_IR_SUBOP_MADSP_SD 0xffff
225 // Yes, we could represent those with DataType.
226 // Or put the type into operation and have a couple 1000 values in that enum.
227 // This will have to do for now.
228 // The bitfields are supposed to correspond to nve4 ISA.
229 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
230 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
231 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
232 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
233 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
244 TYPE_U64
, // 64 bit operations are only lowered after register allocation
256 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
259 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
287 ROUND_M
, // towards -inf
288 ROUND_Z
, // towards 0
289 ROUND_P
, // towards +inf
290 ROUND_NI
, // nearest integer
291 ROUND_MI
, // to integer towards -inf
292 ROUND_ZI
, // to integer towards 0
293 ROUND_PI
, // to integer towards +inf
298 CACHE_CA
, // cache at all levels
299 CACHE_WB
= CACHE_CA
, // cache write back
300 CACHE_CG
, // cache at global level
301 CACHE_CS
, // cache streaming
302 CACHE_CV
, // cache as volatile
303 CACHE_WT
= CACHE_CV
// cache write-through
310 FILE_PREDICATE
, // boolean predicate
311 FILE_FLAGS
, // zero/sign/carry/overflow bits
313 LAST_REGISTER_FILE
= FILE_ADDRESS
,
332 TEX_TARGET_1D_SHADOW
,
333 TEX_TARGET_2D_SHADOW
,
334 TEX_TARGET_CUBE_SHADOW
,
337 TEX_TARGET_2D_MS_ARRAY
,
338 TEX_TARGET_CUBE_ARRAY
,
339 TEX_TARGET_1D_ARRAY_SHADOW
,
340 TEX_TARGET_2D_ARRAY_SHADOW
,
342 TEX_TARGET_RECT_SHADOW
,
343 TEX_TARGET_CUBE_ARRAY_SHADOW
,
355 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
390 class CmpInstruction
;
391 class TexInstruction
;
392 class FlowInstruction
;
397 class ImmediateValue
;
402 int8_t fileIndex
; // signed, may be indirect for CONST[]
403 uint8_t size
; // this should match the Instruction type's size
404 DataType type
; // mainly for pretty printing
406 uint64_t u64
; // immediate values
416 int32_t offset
; // offset from 0 (base of address space)
417 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
425 // precedence: NOT after SAT after NEG after ABS
426 #define NV50_IR_MOD_ABS (1 << 0)
427 #define NV50_IR_MOD_NEG (1 << 1)
428 #define NV50_IR_MOD_SAT (1 << 2)
429 #define NV50_IR_MOD_NOT (1 << 3)
430 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
432 #define NV50_IR_INTERP_MODE_MASK 0x3
433 #define NV50_IR_INTERP_LINEAR (0 << 0)
434 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
435 #define NV50_IR_INTERP_FLAT (2 << 0)
436 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
437 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
438 #define NV50_IR_INTERP_DEFAULT (0 << 2)
439 #define NV50_IR_INTERP_CENTROID (1 << 2)
440 #define NV50_IR_INTERP_OFFSET (2 << 2)
441 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
443 // do we really want this to be a class ?
447 Modifier() : bits(0) { }
448 Modifier(unsigned int m
) : bits(m
) { }
449 Modifier(operation op
);
451 // @return new Modifier applying a after b (asserts if unrepresentable)
452 Modifier
operator*(const Modifier
) const;
453 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
454 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
455 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
457 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
458 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
459 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
461 operation
getOp() const;
463 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
464 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
466 inline operator bool() const { return bits
? true : false; }
468 void applyTo(ImmediateValue
&imm
) const;
470 int print(char *buf
, size_t size
) const;
479 ValueRef(Value
* = NULL
);
480 ValueRef(const ValueRef
&);
483 inline bool exists() const { return value
!= NULL
; }
486 void set(const ValueRef
&);
487 inline Value
*get() const { return value
; }
488 inline Value
*rep() const;
490 inline Instruction
*getInsn() const { return insn
; }
491 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
493 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
494 inline const ValueRef
*getIndirect(int dim
) const;
496 inline DataFile
getFile() const;
497 inline unsigned getSize() const;
499 // SSA: return eventual (traverse MOVs) literal value, if it exists
500 bool getImmediate(ImmediateValue
&) const;
504 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
507 bool usedAsPtr
; // for printing
517 ValueDef(Value
* = NULL
);
518 ValueDef(const ValueDef
&);
521 inline bool exists() const { return value
!= NULL
; }
523 inline Value
*get() const { return value
; }
524 inline Value
*rep() const;
526 bool mayReplace(const ValueRef
&);
527 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
529 inline Instruction
*getInsn() const { return insn
; }
530 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
532 inline DataFile
getFile() const;
533 inline unsigned getSize() const;
535 inline void setSSA(LValue
*);
536 inline const LValue
*preSSA() const;
539 Value
*value
; // should make this LValue * ...
540 LValue
*origin
; // pre SSA value
550 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
552 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
554 virtual bool equals(const Value
*, bool strict
= false) const;
555 virtual bool interfers(const Value
*) const;
556 virtual bool isUniform() const { return true; }
558 inline Value
*rep() const { return join
; }
560 inline Instruction
*getUniqueInsn() const;
561 inline Instruction
*getInsn() const; // use when uniqueness is certain
563 inline int refCount() { return uses
.size(); }
565 inline LValue
*asLValue();
566 inline Symbol
*asSym();
567 inline ImmediateValue
*asImm();
568 inline const Symbol
*asSym() const;
569 inline const ImmediateValue
*asImm() const;
571 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
573 static inline Value
*get(Iterator
&);
575 std::list
<ValueRef
*> uses
;
576 std::list
<ValueDef
*> defs
;
577 typedef std::list
<ValueRef
*>::iterator UseIterator
;
578 typedef std::list
<ValueRef
*>::const_iterator UseCIterator
;
579 typedef std::list
<ValueDef
*>::iterator DefIterator
;
580 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
585 // TODO: these should be in LValue:
590 class LValue
: public Value
593 LValue(Function
*, DataFile file
);
594 LValue(Function
*, LValue
*);
597 virtual bool isUniform() const;
599 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
601 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
604 unsigned compMask
: 8; // compound/component mask
605 unsigned compound
: 1; // used by RA, value involved in split/merge
607 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
608 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
611 class Symbol
: public Value
614 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
617 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
619 virtual bool equals(const Value
*that
, bool strict
) const;
621 virtual bool isUniform() const;
623 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
625 // print with indirect values
626 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
628 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
631 reg
.fileIndex
= fileIndex
;
634 inline void setOffset(int32_t offset
);
635 inline void setAddress(Symbol
*base
, int32_t offset
);
636 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
638 inline const Symbol
*getBase() const { return baseSym
; }
641 Symbol
*baseSym
; // array base for Symbols representing array elements
644 class ImmediateValue
: public Value
648 ImmediateValue(Program
*, uint32_t);
649 ImmediateValue(Program
*, float);
650 ImmediateValue(Program
*, double);
651 // NOTE: not added to program with
652 ImmediateValue(const ImmediateValue
*, DataType ty
);
653 ~ImmediateValue() { };
655 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
657 virtual bool equals(const Value
*that
, bool strict
) const;
659 // these only work if 'type' is valid (we mostly use untyped literals):
660 bool isInteger(const int ival
) const; // ival is cast to this' type
661 bool isNegative() const;
666 // for constant folding:
667 ImmediateValue
operator+(const ImmediateValue
&) const;
668 ImmediateValue
operator-(const ImmediateValue
&) const;
669 ImmediateValue
operator*(const ImmediateValue
&) const;
670 ImmediateValue
operator/(const ImmediateValue
&) const;
672 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
674 bool compare(CondCode cc
, float fval
) const;
676 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
683 Instruction(Function
*, operation
, DataType
);
684 virtual ~Instruction();
686 virtual Instruction
*clone(ClonePolicy
<Function
>&,
687 Instruction
* = NULL
) const;
689 void setDef(int i
, Value
*);
690 void setSrc(int s
, Value
*);
691 void setSrc(int s
, const ValueRef
&);
692 void swapSources(int a
, int b
);
693 void moveSources(int s
, int delta
);
694 bool setIndirect(int s
, int dim
, Value
*);
696 inline ValueRef
& src(int s
) { return srcs
[s
]; }
697 inline ValueDef
& def(int s
) { return defs
[s
]; }
698 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
699 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
701 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
702 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
703 inline Value
*getIndirect(int s
, int dim
) const;
705 inline bool defExists(unsigned d
) const
707 return d
< defs
.size() && defs
[d
].exists();
709 inline bool srcExists(unsigned s
) const
711 return s
< srcs
.size() && srcs
[s
].exists();
714 inline bool constrainedDefs() const;
716 bool setPredicate(CondCode ccode
, Value
*);
717 inline Value
*getPredicate() const;
718 bool writesPredicate() const;
719 inline bool isPredicated() const { return predSrc
>= 0; }
721 inline void setFlagsSrc(int s
, Value
*);
722 inline void setFlagsDef(int d
, Value
*);
723 inline bool usesFlags() const { return flagsSrc
>= 0; }
725 unsigned int defCount() const { return defs
.size(); };
726 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
727 unsigned int srcCount() const { return srcs
.size(); };
728 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
730 // save & remove / set indirect[0,1] and predicate source
731 void takeExtraSources(int s
, Value
*[3]);
732 void putExtraSources(int s
, Value
*[3]);
734 inline void setType(DataType type
) { dType
= sType
= type
; }
736 inline void setType(DataType dtype
, DataType stype
)
742 inline bool isPseudo() const { return op
< OP_MOV
; }
745 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
746 bool isActionEqual(const Instruction
*) const;
747 bool isResultEqual(const Instruction
*) const;
751 inline CmpInstruction
*asCmp();
752 inline TexInstruction
*asTex();
753 inline FlowInstruction
*asFlow();
754 inline const TexInstruction
*asTex() const;
755 inline const CmpInstruction
*asCmp() const;
756 inline const FlowInstruction
*asFlow() const;
762 int serial
; // CFG order
765 DataType dType
; // destination or defining type
766 DataType sType
; // source or secondary type
771 uint16_t subOp
; // quadop, 1 for mul-high, etc.
773 unsigned encSize
: 4; // encoding size in bytes
774 unsigned saturate
: 1; // to [0.0f, 1.0f]
775 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
776 unsigned fixed
: 1; // prevent dead code elimination
777 unsigned terminator
: 1; // end of basic block
778 unsigned ftz
: 1; // flush denormal to zero
779 unsigned dnz
: 1; // denormals, NaN are zero
780 unsigned ipa
: 4; // interpolation mode
782 unsigned perPatch
: 1;
783 unsigned exit
: 1; // terminate program after insn
784 unsigned mask
: 4; // for vector ops
786 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
792 uint8_t sched
; // scheduling data (NOTE: maybe move to separate storage)
797 std::deque
<ValueDef
> defs
; // no gaps !
798 std::deque
<ValueRef
> srcs
; // no gaps !
800 // instruction specific methods:
801 // (don't want to subclass, would need more constructors and memory pools)
803 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
805 unsigned int getInterpMode() const { return ipa
& 0x3; }
806 unsigned int getSampleMode() const { return ipa
& 0xc; }
823 class TexInstruction
: public Instruction
829 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
831 const char *getName() const { return descTable
[target
].name
; }
832 unsigned int getArgCount() const { return descTable
[target
].argc
; }
833 unsigned int getDim() const { return descTable
[target
].dim
; }
834 int isArray() const { return descTable
[target
].array
? 1 : 0; }
835 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
836 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
838 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
842 target
= TEX_TARGET_2D_ARRAY
;
844 target
= TEX_TARGET_2D
;
848 Target
& operator=(TexTarget targ
)
850 assert(targ
< TEX_TARGET_COUNT
);
855 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
856 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
858 enum TexTarget
getEnum() const { return target
; }
871 static const struct Desc descTable
[TEX_TARGET_COUNT
];
874 enum TexTarget target
;
878 TexInstruction(Function
*, operation
);
879 virtual ~TexInstruction();
881 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
882 Instruction
* = NULL
) const;
884 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
891 void setIndirectR(Value
*);
892 void setIndirectS(Value
*);
893 inline Value
*getIndirectR() const;
894 inline Value
*getIndirectS() const;
908 bool liveOnly
; // only execute on live pixels of a quad (optimization)
912 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
922 class CmpInstruction
: public Instruction
925 CmpInstruction(Function
*, operation
);
927 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
928 Instruction
* = NULL
) const;
930 void setCondition(CondCode cond
) { setCond
= cond
; }
931 CondCode
getCondition() const { return setCond
; }
937 class FlowInstruction
: public Instruction
940 FlowInstruction(Function
*, operation
, void *target
);
942 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
943 Instruction
* = NULL
) const;
946 unsigned allWarp
: 1;
947 unsigned absolute
: 1;
949 unsigned builtin
: 1; // true for calls to emulation code
950 unsigned indirect
: 1; // target in src(0)
962 BasicBlock(Function
*);
965 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
967 inline int getId() const { return id
; }
968 inline unsigned int getInsnCount() const { return numInsns
; }
969 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
971 bool dominatedBy(BasicBlock
*bb
);
972 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
974 // returns mask of conditional out blocks
975 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
976 unsigned int initiatesSimpleConditional() const;
979 Function
*getFunction() const { return func
; }
980 Program
*getProgram() const { return program
; }
982 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
983 Instruction
*getPhi() const { return phi
; }
984 Instruction
*getFirst() const { return phi
? phi
: entry
; }
985 Instruction
*getExit() const { return exit
; }
987 void insertHead(Instruction
*);
988 void insertTail(Instruction
*);
989 void insertBefore(Instruction
*, Instruction
*);
990 void insertAfter(Instruction
*, Instruction
*);
991 void remove(Instruction
*);
992 void permuteAdjacent(Instruction
*, Instruction
*);
994 BasicBlock
*idom() const;
996 // NOTE: currently does not rebuild the dominator tree
997 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
998 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1000 DLList
& getDF() { return df
; }
1001 DLList::Iterator
iterDF() { return df
.iterator(); }
1003 static inline BasicBlock
*get(Iterator
&);
1004 static inline BasicBlock
*get(Graph::Node
*);
1007 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1016 Instruction
*joinAt
; // for quick reference
1018 bool explicitCont
; // loop headers: true if loop contains continue stmts
1028 unsigned int numInsns
;
1034 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1040 Function(Program
*, const char *name
, uint32_t label
);
1043 static inline Function
*get(Graph::Node
*node
);
1045 inline Program
*getProgram() const { return prog
; }
1046 inline const char *getName() const { return name
; }
1047 inline int getId() const { return id
; }
1048 inline uint32_t getLabel() const { return label
; }
1051 void printLiveIntervals() const;
1052 void printCFGraph(const char *filePath
);
1054 bool setEntry(BasicBlock
*);
1055 bool setExit(BasicBlock
*);
1057 unsigned int orderInstructions(ArrayList
&);
1059 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1060 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1061 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1063 inline LValue
*getLValue(int id
);
1065 void buildLiveSets();
1066 void buildDefSets();
1067 bool convertToSSA();
1070 std::deque
<ValueDef
> ins
;
1071 std::deque
<ValueRef
> outs
;
1072 std::deque
<Value
*> clobbers
;
1075 Graph::Node
*cfgExit
;
1077 Graph::Node call
; // node in the call graph
1079 BasicBlock
**bbArray
; // BBs in emission order
1082 unsigned int loopNestingBound
;
1090 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1093 ArrayList allBBlocks
;
1095 ArrayList allLValues
;
1098 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1099 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1104 const char *const name
;
1111 CG_STAGE_SSA
, // expected directly before register allocation
1121 TYPE_TESSELLATION_CONTROL
,
1122 TYPE_TESSELLATION_EVAL
,
1128 Program(Type type
, Target
*targ
);
1133 Type
getType() const { return progType
; }
1135 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1136 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1137 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1139 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1140 bool makeFromSM4(struct nv50_ir_prog_info
*);
1141 bool convertToSSA();
1142 bool optimizeSSA(int level
);
1143 bool optimizePostRA(int level
);
1144 bool registerAllocation();
1145 bool emitBinary(struct nv50_ir_prog_info
*);
1147 const Target
*getTarget() const { return target
; }
1150 void emitSymbolTable(struct nv50_ir_prog_info
*);
1160 ArrayList allRValues
;
1164 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1168 MemoryPool mem_Instruction
;
1169 MemoryPool mem_CmpInstruction
;
1170 MemoryPool mem_TexInstruction
;
1171 MemoryPool mem_FlowInstruction
;
1172 MemoryPool mem_LValue
;
1173 MemoryPool mem_Symbol
;
1174 MemoryPool mem_ImmediateValue
;
1179 void *targetPriv
; // e.g. to carry information between passes
1181 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1183 void releaseInstruction(Instruction
*);
1184 void releaseValue(Value
*);
1187 // TODO: add const version
1191 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1192 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1195 // return false to continue with next entity on next higher level
1196 virtual bool visit(Function
*) { return true; }
1197 virtual bool visit(BasicBlock
*) { return true; }
1198 virtual bool visit(Instruction
*) { return false; }
1200 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1201 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1209 // =============================================================================
1211 #include "codegen/nv50_ir_inlines.h"
1213 } // namespace nv50_ir
1215 #endif // __NV50_IR_H__