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_BFIND
, // find highest/lowest set bit
147 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
149 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
150 OP_VADD
, // byte/word vector operations
159 OP_CCTL
, // cache control
163 // various instruction-specific modifier definitions Instruction::subOp
164 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
165 #define NV50_IR_SUBOP_MUL_HIGH 1
166 #define NV50_IR_SUBOP_EMIT_RESTART 1
167 #define NV50_IR_SUBOP_LDC_IL 1
168 #define NV50_IR_SUBOP_LDC_IS 2
169 #define NV50_IR_SUBOP_LDC_ISL 3
170 #define NV50_IR_SUBOP_SHIFT_WRAP 1
171 #define NV50_IR_SUBOP_EMU_PRERET 1
172 #define NV50_IR_SUBOP_TEXBAR(n) n
173 #define NV50_IR_SUBOP_MOV_FINAL 1
174 #define NV50_IR_SUBOP_EXTBF_REV 1
175 #define NV50_IR_SUBOP_BFIND_SAMT 1
176 #define NV50_IR_SUBOP_PERMT_F4E 1
177 #define NV50_IR_SUBOP_PERMT_B4E 2
178 #define NV50_IR_SUBOP_PERMT_RC8 3
179 #define NV50_IR_SUBOP_PERMT_ECL 4
180 #define NV50_IR_SUBOP_PERMT_ECR 5
181 #define NV50_IR_SUBOP_PERMT_RC16 6
182 #define NV50_IR_SUBOP_BAR_SYNC 0
183 #define NV50_IR_SUBOP_BAR_ARRIVE 1
184 #define NV50_IR_SUBOP_BAR_RED_AND 2
185 #define NV50_IR_SUBOP_BAR_RED_OR 3
186 #define NV50_IR_SUBOP_BAR_RED_POPC 4
187 #define NV50_IR_SUBOP_MEMBAR_L 1
188 #define NV50_IR_SUBOP_MEMBAR_S 2
189 #define NV50_IR_SUBOP_MEMBAR_M 3
190 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
191 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
192 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
193 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
194 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
195 #define NV50_IR_SUBOP_MEMBAR(d,s) \
196 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
197 #define NV50_IR_SUBOP_ATOM_ADD 0
198 #define NV50_IR_SUBOP_ATOM_MIN 1
199 #define NV50_IR_SUBOP_ATOM_MAX 2
200 #define NV50_IR_SUBOP_ATOM_INC 3
201 #define NV50_IR_SUBOP_ATOM_DEC 4
202 #define NV50_IR_SUBOP_ATOM_AND 5
203 #define NV50_IR_SUBOP_ATOM_OR 6
204 #define NV50_IR_SUBOP_ATOM_XOR 7
205 #define NV50_IR_SUBOP_ATOM_CAS 8
206 #define NV50_IR_SUBOP_ATOM_EXCH 9
207 #define NV50_IR_SUBOP_CCTL_IV 5
208 #define NV50_IR_SUBOP_CCTL_IVALL 6
209 #define NV50_IR_SUBOP_SUST_IGN 0
210 #define NV50_IR_SUBOP_SUST_TRAP 1
211 #define NV50_IR_SUBOP_SUST_SDCL 3
212 #define NV50_IR_SUBOP_SULD_ZERO 0
213 #define NV50_IR_SUBOP_SULD_TRAP 1
214 #define NV50_IR_SUBOP_SULD_SDCL 3
215 #define NV50_IR_SUBOP_SUBFM_3D 1
216 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
217 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
218 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
219 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
220 #define NV50_IR_SUBOP_PIXLD_COUNT 0
221 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
222 #define NV50_IR_SUBOP_PIXLD_COVERED 2
223 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
224 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
225 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
226 #define NV50_IR_SUBOP_MADSP_SD 0xffff
227 // Yes, we could represent those with DataType.
228 // Or put the type into operation and have a couple 1000 values in that enum.
229 // This will have to do for now.
230 // The bitfields are supposed to correspond to nve4 ISA.
231 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
232 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
233 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
234 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
235 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
246 TYPE_U64
, // 64 bit operations are only lowered after register allocation
258 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
261 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
289 ROUND_M
, // towards -inf
290 ROUND_Z
, // towards 0
291 ROUND_P
, // towards +inf
292 ROUND_NI
, // nearest integer
293 ROUND_MI
, // to integer towards -inf
294 ROUND_ZI
, // to integer towards 0
295 ROUND_PI
, // to integer towards +inf
300 CACHE_CA
, // cache at all levels
301 CACHE_WB
= CACHE_CA
, // cache write back
302 CACHE_CG
, // cache at global level
303 CACHE_CS
, // cache streaming
304 CACHE_CV
, // cache as volatile
305 CACHE_WT
= CACHE_CV
// cache write-through
312 FILE_PREDICATE
, // boolean predicate
313 FILE_FLAGS
, // zero/sign/carry/overflow bits
315 LAST_REGISTER_FILE
= FILE_ADDRESS
,
334 TEX_TARGET_1D_SHADOW
,
335 TEX_TARGET_2D_SHADOW
,
336 TEX_TARGET_CUBE_SHADOW
,
339 TEX_TARGET_2D_MS_ARRAY
,
340 TEX_TARGET_CUBE_ARRAY
,
341 TEX_TARGET_1D_ARRAY_SHADOW
,
342 TEX_TARGET_2D_ARRAY_SHADOW
,
344 TEX_TARGET_RECT_SHADOW
,
345 TEX_TARGET_CUBE_ARRAY_SHADOW
,
357 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
393 class CmpInstruction
;
394 class TexInstruction
;
395 class FlowInstruction
;
400 class ImmediateValue
;
405 int8_t fileIndex
; // signed, may be indirect for CONST[]
406 uint8_t size
; // this should match the Instruction type's size
407 DataType type
; // mainly for pretty printing
409 uint64_t u64
; // immediate values
419 int32_t offset
; // offset from 0 (base of address space)
420 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
428 // precedence: NOT after SAT after NEG after ABS
429 #define NV50_IR_MOD_ABS (1 << 0)
430 #define NV50_IR_MOD_NEG (1 << 1)
431 #define NV50_IR_MOD_SAT (1 << 2)
432 #define NV50_IR_MOD_NOT (1 << 3)
433 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
435 #define NV50_IR_INTERP_MODE_MASK 0x3
436 #define NV50_IR_INTERP_LINEAR (0 << 0)
437 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
438 #define NV50_IR_INTERP_FLAT (2 << 0)
439 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
440 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
441 #define NV50_IR_INTERP_DEFAULT (0 << 2)
442 #define NV50_IR_INTERP_CENTROID (1 << 2)
443 #define NV50_IR_INTERP_OFFSET (2 << 2)
444 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
446 // do we really want this to be a class ?
450 Modifier() : bits(0) { }
451 Modifier(unsigned int m
) : bits(m
) { }
452 Modifier(operation op
);
454 // @return new Modifier applying a after b (asserts if unrepresentable)
455 Modifier
operator*(const Modifier
) const;
456 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
457 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
458 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
460 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
461 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
462 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
464 operation
getOp() const;
466 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
467 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
469 inline operator bool() const { return bits
? true : false; }
471 void applyTo(ImmediateValue
&imm
) const;
473 int print(char *buf
, size_t size
) const;
482 ValueRef(Value
* = NULL
);
483 ValueRef(const ValueRef
&);
486 inline bool exists() const { return value
!= NULL
; }
489 void set(const ValueRef
&);
490 inline Value
*get() const { return value
; }
491 inline Value
*rep() const;
493 inline Instruction
*getInsn() const { return insn
; }
494 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
496 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
497 inline const ValueRef
*getIndirect(int dim
) const;
499 inline DataFile
getFile() const;
500 inline unsigned getSize() const;
502 // SSA: return eventual (traverse MOVs) literal value, if it exists
503 bool getImmediate(ImmediateValue
&) const;
507 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
510 bool usedAsPtr
; // for printing
520 ValueDef(Value
* = NULL
);
521 ValueDef(const ValueDef
&);
524 inline bool exists() const { return value
!= NULL
; }
526 inline Value
*get() const { return value
; }
527 inline Value
*rep() const;
529 bool mayReplace(const ValueRef
&);
530 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
532 inline Instruction
*getInsn() const { return insn
; }
533 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
535 inline DataFile
getFile() const;
536 inline unsigned getSize() const;
538 inline void setSSA(LValue
*);
539 inline const LValue
*preSSA() const;
542 Value
*value
; // should make this LValue * ...
543 LValue
*origin
; // pre SSA value
553 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
555 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
557 virtual bool equals(const Value
*, bool strict
= false) const;
558 virtual bool interfers(const Value
*) const;
559 virtual bool isUniform() const { return true; }
561 inline Value
*rep() const { return join
; }
563 inline Instruction
*getUniqueInsn() const;
564 inline Instruction
*getInsn() const; // use when uniqueness is certain
566 inline int refCount() { return uses
.size(); }
568 inline LValue
*asLValue();
569 inline Symbol
*asSym();
570 inline ImmediateValue
*asImm();
571 inline const Symbol
*asSym() const;
572 inline const ImmediateValue
*asImm() const;
574 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
576 static inline Value
*get(Iterator
&);
578 std::list
<ValueRef
*> uses
;
579 std::list
<ValueDef
*> defs
;
580 typedef std::list
<ValueRef
*>::iterator UseIterator
;
581 typedef std::list
<ValueRef
*>::const_iterator UseCIterator
;
582 typedef std::list
<ValueDef
*>::iterator DefIterator
;
583 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
588 // TODO: these should be in LValue:
593 class LValue
: public Value
596 LValue(Function
*, DataFile file
);
597 LValue(Function
*, LValue
*);
600 virtual bool isUniform() const;
602 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
604 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
607 unsigned compMask
: 8; // compound/component mask
608 unsigned compound
: 1; // used by RA, value involved in split/merge
610 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
611 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
614 class Symbol
: public Value
617 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
620 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
622 virtual bool equals(const Value
*that
, bool strict
) const;
624 virtual bool isUniform() const;
626 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
628 // print with indirect values
629 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
631 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
634 reg
.fileIndex
= fileIndex
;
637 inline void setOffset(int32_t offset
);
638 inline void setAddress(Symbol
*base
, int32_t offset
);
639 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
641 inline const Symbol
*getBase() const { return baseSym
; }
644 Symbol
*baseSym
; // array base for Symbols representing array elements
647 class ImmediateValue
: public Value
651 ImmediateValue(Program
*, uint32_t);
652 ImmediateValue(Program
*, float);
653 ImmediateValue(Program
*, double);
654 // NOTE: not added to program with
655 ImmediateValue(const ImmediateValue
*, DataType ty
);
656 ~ImmediateValue() { };
658 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
660 virtual bool equals(const Value
*that
, bool strict
) const;
662 // these only work if 'type' is valid (we mostly use untyped literals):
663 bool isInteger(const int ival
) const; // ival is cast to this' type
664 bool isNegative() const;
669 // for constant folding:
670 ImmediateValue
operator+(const ImmediateValue
&) const;
671 ImmediateValue
operator-(const ImmediateValue
&) const;
672 ImmediateValue
operator*(const ImmediateValue
&) const;
673 ImmediateValue
operator/(const ImmediateValue
&) const;
675 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
677 bool compare(CondCode cc
, float fval
) const;
679 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
686 Instruction(Function
*, operation
, DataType
);
687 virtual ~Instruction();
689 virtual Instruction
*clone(ClonePolicy
<Function
>&,
690 Instruction
* = NULL
) const;
692 void setDef(int i
, Value
*);
693 void setSrc(int s
, Value
*);
694 void setSrc(int s
, const ValueRef
&);
695 void swapSources(int a
, int b
);
696 void moveSources(int s
, int delta
);
697 bool setIndirect(int s
, int dim
, Value
*);
699 inline ValueRef
& src(int s
) { return srcs
[s
]; }
700 inline ValueDef
& def(int s
) { return defs
[s
]; }
701 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
702 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
704 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
705 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
706 inline Value
*getIndirect(int s
, int dim
) const;
708 inline bool defExists(unsigned d
) const
710 return d
< defs
.size() && defs
[d
].exists();
712 inline bool srcExists(unsigned s
) const
714 return s
< srcs
.size() && srcs
[s
].exists();
717 inline bool constrainedDefs() const;
719 bool setPredicate(CondCode ccode
, Value
*);
720 inline Value
*getPredicate() const;
721 bool writesPredicate() const;
722 inline bool isPredicated() const { return predSrc
>= 0; }
724 inline void setFlagsSrc(int s
, Value
*);
725 inline void setFlagsDef(int d
, Value
*);
726 inline bool usesFlags() const { return flagsSrc
>= 0; }
728 unsigned int defCount() const { return defs
.size(); };
729 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
730 unsigned int srcCount() const { return srcs
.size(); };
731 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
733 // save & remove / set indirect[0,1] and predicate source
734 void takeExtraSources(int s
, Value
*[3]);
735 void putExtraSources(int s
, Value
*[3]);
737 inline void setType(DataType type
) { dType
= sType
= type
; }
739 inline void setType(DataType dtype
, DataType stype
)
745 inline bool isPseudo() const { return op
< OP_MOV
; }
748 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
749 bool isActionEqual(const Instruction
*) const;
750 bool isResultEqual(const Instruction
*) const;
754 inline CmpInstruction
*asCmp();
755 inline TexInstruction
*asTex();
756 inline FlowInstruction
*asFlow();
757 inline const TexInstruction
*asTex() const;
758 inline const CmpInstruction
*asCmp() const;
759 inline const FlowInstruction
*asFlow() const;
765 int serial
; // CFG order
768 DataType dType
; // destination or defining type
769 DataType sType
; // source or secondary type
774 uint16_t subOp
; // quadop, 1 for mul-high, etc.
776 unsigned encSize
: 4; // encoding size in bytes
777 unsigned saturate
: 1; // to [0.0f, 1.0f]
778 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
779 unsigned fixed
: 1; // prevent dead code elimination
780 unsigned terminator
: 1; // end of basic block
781 unsigned ftz
: 1; // flush denormal to zero
782 unsigned dnz
: 1; // denormals, NaN are zero
783 unsigned ipa
: 4; // interpolation mode
785 unsigned perPatch
: 1;
786 unsigned exit
: 1; // terminate program after insn
787 unsigned mask
: 4; // for vector ops
789 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
795 uint8_t sched
; // scheduling data (NOTE: maybe move to separate storage)
800 std::deque
<ValueDef
> defs
; // no gaps !
801 std::deque
<ValueRef
> srcs
; // no gaps !
803 // instruction specific methods:
804 // (don't want to subclass, would need more constructors and memory pools)
806 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
808 unsigned int getInterpMode() const { return ipa
& 0x3; }
809 unsigned int getSampleMode() const { return ipa
& 0xc; }
826 class TexInstruction
: public Instruction
832 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
834 const char *getName() const { return descTable
[target
].name
; }
835 unsigned int getArgCount() const { return descTable
[target
].argc
; }
836 unsigned int getDim() const { return descTable
[target
].dim
; }
837 int isArray() const { return descTable
[target
].array
? 1 : 0; }
838 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
839 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
841 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
845 target
= TEX_TARGET_2D_ARRAY
;
847 target
= TEX_TARGET_2D
;
851 Target
& operator=(TexTarget targ
)
853 assert(targ
< TEX_TARGET_COUNT
);
858 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
859 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
861 enum TexTarget
getEnum() const { return target
; }
874 static const struct Desc descTable
[TEX_TARGET_COUNT
];
877 enum TexTarget target
;
881 TexInstruction(Function
*, operation
);
882 virtual ~TexInstruction();
884 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
885 Instruction
* = NULL
) const;
887 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
894 void setIndirectR(Value
*);
895 void setIndirectS(Value
*);
896 inline Value
*getIndirectR() const;
897 inline Value
*getIndirectS() const;
911 bool liveOnly
; // only execute on live pixels of a quad (optimization)
915 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
925 class CmpInstruction
: public Instruction
928 CmpInstruction(Function
*, operation
);
930 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
931 Instruction
* = NULL
) const;
933 void setCondition(CondCode cond
) { setCond
= cond
; }
934 CondCode
getCondition() const { return setCond
; }
940 class FlowInstruction
: public Instruction
943 FlowInstruction(Function
*, operation
, void *target
);
945 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
946 Instruction
* = NULL
) const;
949 unsigned allWarp
: 1;
950 unsigned absolute
: 1;
952 unsigned builtin
: 1; // true for calls to emulation code
953 unsigned indirect
: 1; // target in src(0)
965 BasicBlock(Function
*);
968 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
970 inline int getId() const { return id
; }
971 inline unsigned int getInsnCount() const { return numInsns
; }
972 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
974 bool dominatedBy(BasicBlock
*bb
);
975 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
977 // returns mask of conditional out blocks
978 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
979 unsigned int initiatesSimpleConditional() const;
982 Function
*getFunction() const { return func
; }
983 Program
*getProgram() const { return program
; }
985 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
986 Instruction
*getPhi() const { return phi
; }
987 Instruction
*getFirst() const { return phi
? phi
: entry
; }
988 Instruction
*getExit() const { return exit
; }
990 void insertHead(Instruction
*);
991 void insertTail(Instruction
*);
992 void insertBefore(Instruction
*, Instruction
*);
993 void insertAfter(Instruction
*, Instruction
*);
994 void remove(Instruction
*);
995 void permuteAdjacent(Instruction
*, Instruction
*);
997 BasicBlock
*idom() const;
999 // NOTE: currently does not rebuild the dominator tree
1000 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1001 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1003 DLList
& getDF() { return df
; }
1004 DLList::Iterator
iterDF() { return df
.iterator(); }
1006 static inline BasicBlock
*get(Iterator
&);
1007 static inline BasicBlock
*get(Graph::Node
*);
1010 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1019 Instruction
*joinAt
; // for quick reference
1021 bool explicitCont
; // loop headers: true if loop contains continue stmts
1031 unsigned int numInsns
;
1037 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1043 Function(Program
*, const char *name
, uint32_t label
);
1046 static inline Function
*get(Graph::Node
*node
);
1048 inline Program
*getProgram() const { return prog
; }
1049 inline const char *getName() const { return name
; }
1050 inline int getId() const { return id
; }
1051 inline uint32_t getLabel() const { return label
; }
1054 void printLiveIntervals() const;
1055 void printCFGraph(const char *filePath
);
1057 bool setEntry(BasicBlock
*);
1058 bool setExit(BasicBlock
*);
1060 unsigned int orderInstructions(ArrayList
&);
1062 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1063 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1064 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1066 inline LValue
*getLValue(int id
);
1068 void buildLiveSets();
1069 void buildDefSets();
1070 bool convertToSSA();
1073 std::deque
<ValueDef
> ins
;
1074 std::deque
<ValueRef
> outs
;
1075 std::deque
<Value
*> clobbers
;
1078 Graph::Node
*cfgExit
;
1080 Graph::Node call
; // node in the call graph
1082 BasicBlock
**bbArray
; // BBs in emission order
1085 unsigned int loopNestingBound
;
1093 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1096 ArrayList allBBlocks
;
1098 ArrayList allLValues
;
1101 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1102 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1107 const char *const name
;
1114 CG_STAGE_SSA
, // expected directly before register allocation
1124 TYPE_TESSELLATION_CONTROL
,
1125 TYPE_TESSELLATION_EVAL
,
1131 Program(Type type
, Target
*targ
);
1136 Type
getType() const { return progType
; }
1138 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1139 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1140 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1142 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1143 bool makeFromSM4(struct nv50_ir_prog_info
*);
1144 bool convertToSSA();
1145 bool optimizeSSA(int level
);
1146 bool optimizePostRA(int level
);
1147 bool registerAllocation();
1148 bool emitBinary(struct nv50_ir_prog_info
*);
1150 const Target
*getTarget() const { return target
; }
1153 void emitSymbolTable(struct nv50_ir_prog_info
*);
1163 ArrayList allRValues
;
1167 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1171 MemoryPool mem_Instruction
;
1172 MemoryPool mem_CmpInstruction
;
1173 MemoryPool mem_TexInstruction
;
1174 MemoryPool mem_FlowInstruction
;
1175 MemoryPool mem_LValue
;
1176 MemoryPool mem_Symbol
;
1177 MemoryPool mem_ImmediateValue
;
1182 void *targetPriv
; // e.g. to carry information between passes
1184 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1186 void releaseInstruction(Instruction
*);
1187 void releaseValue(Value
*);
1190 // TODO: add const version
1194 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1195 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1198 // return false to continue with next entity on next higher level
1199 virtual bool visit(Function
*) { return true; }
1200 virtual bool visit(BasicBlock
*) { return true; }
1201 virtual bool visit(Instruction
*) { return false; }
1203 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1204 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1212 // =============================================================================
1214 #include "codegen/nv50_ir_inlines.h"
1216 } // namespace nv50_ir
1218 #endif // __NV50_IR_H__