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_MADSP
, // special integer multiply-add
136 OP_TEXBAR
, // texture dependency barrier
139 OP_RDSV
, // read system value
140 OP_WRSV
, // write system value
141 OP_PIXLD
, // get info about raster object or surfaces
145 OP_POPCNT
, // bitcount(src0 & src1)
146 OP_INSBF
, // insert first src1[8:15] bits of src0 into src2 at src1[0:7]
147 OP_EXTBF
, // place bits [K,K+N) of src0 into dst, src1 = 0xNNKK
148 OP_BFIND
, // find highest/lowest set bit
149 OP_PERMT
, // dst = bytes from src2,src0 selected by src1 (nvc0's src order)
151 OP_BAR
, // execution barrier, sources = { id, thread count, predicate }
152 OP_VADD
, // byte/word vector operations
161 OP_CCTL
, // cache control
162 OP_SHFL
, // warp shuffle
166 // various instruction-specific modifier definitions Instruction::subOp
167 // MOV_FINAL marks a MOV originating from an EXPORT (used for placing TEXBARs)
168 #define NV50_IR_SUBOP_MUL_HIGH 1
169 #define NV50_IR_SUBOP_EMIT_RESTART 1
170 #define NV50_IR_SUBOP_LDC_IL 1
171 #define NV50_IR_SUBOP_LDC_IS 2
172 #define NV50_IR_SUBOP_LDC_ISL 3
173 #define NV50_IR_SUBOP_SHIFT_WRAP 1
174 #define NV50_IR_SUBOP_EMU_PRERET 1
175 #define NV50_IR_SUBOP_TEXBAR(n) n
176 #define NV50_IR_SUBOP_MOV_FINAL 1
177 #define NV50_IR_SUBOP_EXTBF_REV 1
178 #define NV50_IR_SUBOP_BFIND_SAMT 1
179 #define NV50_IR_SUBOP_RCPRSQ_64H 1
180 #define NV50_IR_SUBOP_PERMT_F4E 1
181 #define NV50_IR_SUBOP_PERMT_B4E 2
182 #define NV50_IR_SUBOP_PERMT_RC8 3
183 #define NV50_IR_SUBOP_PERMT_ECL 4
184 #define NV50_IR_SUBOP_PERMT_ECR 5
185 #define NV50_IR_SUBOP_PERMT_RC16 6
186 #define NV50_IR_SUBOP_BAR_SYNC 0
187 #define NV50_IR_SUBOP_BAR_ARRIVE 1
188 #define NV50_IR_SUBOP_BAR_RED_AND 2
189 #define NV50_IR_SUBOP_BAR_RED_OR 3
190 #define NV50_IR_SUBOP_BAR_RED_POPC 4
191 #define NV50_IR_SUBOP_MEMBAR_L 1
192 #define NV50_IR_SUBOP_MEMBAR_S 2
193 #define NV50_IR_SUBOP_MEMBAR_M 3
194 #define NV50_IR_SUBOP_MEMBAR_CTA (0 << 2)
195 #define NV50_IR_SUBOP_MEMBAR_GL (1 << 2)
196 #define NV50_IR_SUBOP_MEMBAR_SYS (2 << 2)
197 #define NV50_IR_SUBOP_MEMBAR_DIR(m) ((m) & 0x3)
198 #define NV50_IR_SUBOP_MEMBAR_SCOPE(m) ((m) & ~0x3)
199 #define NV50_IR_SUBOP_MEMBAR(d,s) \
200 (NV50_IR_SUBOP_MEMBAR_##d | NV50_IR_SUBOP_MEMBAR_##s)
201 #define NV50_IR_SUBOP_ATOM_ADD 0
202 #define NV50_IR_SUBOP_ATOM_MIN 1
203 #define NV50_IR_SUBOP_ATOM_MAX 2
204 #define NV50_IR_SUBOP_ATOM_INC 3
205 #define NV50_IR_SUBOP_ATOM_DEC 4
206 #define NV50_IR_SUBOP_ATOM_AND 5
207 #define NV50_IR_SUBOP_ATOM_OR 6
208 #define NV50_IR_SUBOP_ATOM_XOR 7
209 #define NV50_IR_SUBOP_ATOM_CAS 8
210 #define NV50_IR_SUBOP_ATOM_EXCH 9
211 #define NV50_IR_SUBOP_CCTL_IV 5
212 #define NV50_IR_SUBOP_CCTL_IVALL 6
213 #define NV50_IR_SUBOP_SUST_IGN 0
214 #define NV50_IR_SUBOP_SUST_TRAP 1
215 #define NV50_IR_SUBOP_SUST_SDCL 3
216 #define NV50_IR_SUBOP_SULD_ZERO 0
217 #define NV50_IR_SUBOP_SULD_TRAP 1
218 #define NV50_IR_SUBOP_SULD_SDCL 3
219 #define NV50_IR_SUBOP_SUBFM_3D 1
220 #define NV50_IR_SUBOP_SUCLAMP_2D 0x10
221 #define NV50_IR_SUBOP_SUCLAMP_SD(r, d) (( 0 + (r)) | ((d == 2) ? 0x10 : 0))
222 #define NV50_IR_SUBOP_SUCLAMP_PL(r, d) (( 5 + (r)) | ((d == 2) ? 0x10 : 0))
223 #define NV50_IR_SUBOP_SUCLAMP_BL(r, d) ((10 + (r)) | ((d == 2) ? 0x10 : 0))
224 #define NV50_IR_SUBOP_PIXLD_COUNT 0
225 #define NV50_IR_SUBOP_PIXLD_COVMASK 1
226 #define NV50_IR_SUBOP_PIXLD_COVERED 2
227 #define NV50_IR_SUBOP_PIXLD_OFFSET 3
228 #define NV50_IR_SUBOP_PIXLD_CENT_OFFSET 4
229 #define NV50_IR_SUBOP_PIXLD_SAMPLEID 5
230 #define NV50_IR_SUBOP_SHFL_IDX 0
231 #define NV50_IR_SUBOP_SHFL_UP 1
232 #define NV50_IR_SUBOP_SHFL_DOWN 2
233 #define NV50_IR_SUBOP_SHFL_BFLY 3
234 #define NV50_IR_SUBOP_MADSP_SD 0xffff
235 // Yes, we could represent those with DataType.
236 // Or put the type into operation and have a couple 1000 values in that enum.
237 // This will have to do for now.
238 // The bitfields are supposed to correspond to nve4 ISA.
239 #define NV50_IR_SUBOP_MADSP(a,b,c) (((c) << 8) | ((b) << 4) | (a))
240 #define NV50_IR_SUBOP_V1(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x0000)
241 #define NV50_IR_SUBOP_V2(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x4000)
242 #define NV50_IR_SUBOP_V4(d,a,b) (((d) << 10) | ((b) << 5) | (a) | 0x8000)
243 #define NV50_IR_SUBOP_Vn(n) ((n) >> 14)
254 TYPE_U64
, // 64 bit operations are only lowered after register allocation
266 CC_NEVER
= CC_FL
, // when used with FILE_FLAGS
269 CC_NOT_P
= CC_EQ
, // when used with FILE_PREDICATE
297 ROUND_M
, // towards -inf
298 ROUND_Z
, // towards 0
299 ROUND_P
, // towards +inf
300 ROUND_NI
, // nearest integer
301 ROUND_MI
, // to integer towards -inf
302 ROUND_ZI
, // to integer towards 0
303 ROUND_PI
, // to integer towards +inf
308 CACHE_CA
, // cache at all levels
309 CACHE_WB
= CACHE_CA
, // cache write back
310 CACHE_CG
, // cache at global level
311 CACHE_CS
, // cache streaming
312 CACHE_CV
, // cache as volatile
313 CACHE_WT
= CACHE_CV
// cache write-through
320 FILE_PREDICATE
, // boolean predicate
321 FILE_FLAGS
, // zero/sign/carry/overflow bits
323 LAST_REGISTER_FILE
= FILE_ADDRESS
,
342 TEX_TARGET_1D_SHADOW
,
343 TEX_TARGET_2D_SHADOW
,
344 TEX_TARGET_CUBE_SHADOW
,
347 TEX_TARGET_2D_MS_ARRAY
,
348 TEX_TARGET_CUBE_ARRAY
,
349 TEX_TARGET_1D_ARRAY_SHADOW
,
350 TEX_TARGET_2D_ARRAY_SHADOW
,
352 TEX_TARGET_RECT_SHADOW
,
353 TEX_TARGET_CUBE_ARRAY_SHADOW
,
365 SV_VERTEX_COUNT
, // gl_PatchVerticesIn
407 class CmpInstruction
;
408 class TexInstruction
;
409 class FlowInstruction
;
414 class ImmediateValue
;
419 int8_t fileIndex
; // signed, may be indirect for CONST[]
420 uint8_t size
; // this should match the Instruction type's size
421 DataType type
; // mainly for pretty printing
423 uint64_t u64
; // immediate values
433 int32_t offset
; // offset from 0 (base of address space)
434 int32_t id
; // register id (< 0 if virtual/unassigned, in units <= 4)
442 // precedence: NOT after SAT after NEG after ABS
443 #define NV50_IR_MOD_ABS (1 << 0)
444 #define NV50_IR_MOD_NEG (1 << 1)
445 #define NV50_IR_MOD_SAT (1 << 2)
446 #define NV50_IR_MOD_NOT (1 << 3)
447 #define NV50_IR_MOD_NEG_ABS (NV50_IR_MOD_NEG | NV50_IR_MOD_ABS)
449 #define NV50_IR_INTERP_MODE_MASK 0x3
450 #define NV50_IR_INTERP_LINEAR (0 << 0)
451 #define NV50_IR_INTERP_PERSPECTIVE (1 << 0)
452 #define NV50_IR_INTERP_FLAT (2 << 0)
453 #define NV50_IR_INTERP_SC (3 << 0) // what exactly is that ?
454 #define NV50_IR_INTERP_SAMPLE_MASK 0xc
455 #define NV50_IR_INTERP_DEFAULT (0 << 2)
456 #define NV50_IR_INTERP_CENTROID (1 << 2)
457 #define NV50_IR_INTERP_OFFSET (2 << 2)
458 #define NV50_IR_INTERP_SAMPLEID (3 << 2)
460 // do we really want this to be a class ?
464 Modifier() : bits(0) { }
465 Modifier(unsigned int m
) : bits(m
) { }
466 Modifier(operation op
);
468 // @return new Modifier applying a after b (asserts if unrepresentable)
469 Modifier
operator*(const Modifier
) const;
470 Modifier
operator*=(const Modifier m
) { *this = *this * m
; return *this; }
471 Modifier
operator==(const Modifier m
) const { return m
.bits
== bits
; }
472 Modifier
operator!=(const Modifier m
) const { return m
.bits
!= bits
; }
474 inline Modifier
operator&(const Modifier m
) const { return bits
& m
.bits
; }
475 inline Modifier
operator|(const Modifier m
) const { return bits
| m
.bits
; }
476 inline Modifier
operator^(const Modifier m
) const { return bits
^ m
.bits
; }
478 operation
getOp() const;
480 inline int neg() const { return (bits
& NV50_IR_MOD_NEG
) ? 1 : 0; }
481 inline int abs() const { return (bits
& NV50_IR_MOD_ABS
) ? 1 : 0; }
483 inline operator bool() const { return bits
? true : false; }
485 void applyTo(ImmediateValue
&imm
) const;
487 int print(char *buf
, size_t size
) const;
496 ValueRef(Value
* = NULL
);
497 ValueRef(const ValueRef
&);
500 inline bool exists() const { return value
!= NULL
; }
503 void set(const ValueRef
&);
504 inline Value
*get() const { return value
; }
505 inline Value
*rep() const;
507 inline Instruction
*getInsn() const { return insn
; }
508 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
510 inline bool isIndirect(int dim
) const { return indirect
[dim
] >= 0; }
511 inline const ValueRef
*getIndirect(int dim
) const;
513 inline DataFile
getFile() const;
514 inline unsigned getSize() const;
516 // SSA: return eventual (traverse MOVs) literal value, if it exists
517 bool getImmediate(ImmediateValue
&) const;
521 int8_t indirect
[2]; // >= 0 if relative to lvalue in insn->src(indirect[i])
524 bool usedAsPtr
; // for printing
534 ValueDef(Value
* = NULL
);
535 ValueDef(const ValueDef
&);
538 inline bool exists() const { return value
!= NULL
; }
540 inline Value
*get() const { return value
; }
541 inline Value
*rep() const;
543 bool mayReplace(const ValueRef
&);
544 void replace(const ValueRef
&, bool doSet
); // replace all uses of the old value
546 inline Instruction
*getInsn() const { return insn
; }
547 inline void setInsn(Instruction
*inst
) { insn
= inst
; }
549 inline DataFile
getFile() const;
550 inline unsigned getSize() const;
552 inline void setSSA(LValue
*);
553 inline const LValue
*preSSA() const;
556 Value
*value
; // should make this LValue * ...
557 LValue
*origin
; // pre SSA value
567 virtual Value
*clone(ClonePolicy
<Function
>&) const = 0;
569 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const = 0;
571 virtual bool equals(const Value
*, bool strict
= false) const;
572 virtual bool interfers(const Value
*) const;
573 virtual bool isUniform() const { return true; }
575 inline Value
*rep() const { return join
; }
577 inline Instruction
*getUniqueInsn() const;
578 inline Instruction
*getInsn() const; // use when uniqueness is certain
580 inline int refCount() { return uses
.size(); }
582 inline LValue
*asLValue();
583 inline Symbol
*asSym();
584 inline ImmediateValue
*asImm();
585 inline const Symbol
*asSym() const;
586 inline const ImmediateValue
*asImm() const;
588 inline bool inFile(DataFile f
) { return reg
.file
== f
; }
590 static inline Value
*get(Iterator
&);
592 unordered_set
<ValueRef
*> uses
;
593 std::list
<ValueDef
*> defs
;
594 typedef unordered_set
<ValueRef
*>::iterator UseIterator
;
595 typedef unordered_set
<ValueRef
*>::const_iterator UseCIterator
;
596 typedef std::list
<ValueDef
*>::iterator DefIterator
;
597 typedef std::list
<ValueDef
*>::const_iterator DefCIterator
;
602 // TODO: these should be in LValue:
607 class LValue
: public Value
610 LValue(Function
*, DataFile file
);
611 LValue(Function
*, LValue
*);
614 virtual bool isUniform() const;
616 virtual LValue
*clone(ClonePolicy
<Function
>&) const;
618 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
621 unsigned compMask
: 8; // compound/component mask
622 unsigned compound
: 1; // used by RA, value involved in split/merge
624 unsigned fixedReg
: 1; // set & used by RA, earlier just use (id < 0)
625 unsigned noSpill
: 1; // do not spill (e.g. if spill temporary already)
628 class Symbol
: public Value
631 Symbol(Program
*, DataFile file
= FILE_MEMORY_CONST
, ubyte fileIdx
= 0);
634 virtual Symbol
*clone(ClonePolicy
<Function
>&) const;
636 virtual bool equals(const Value
*that
, bool strict
) const;
638 virtual bool isUniform() const;
640 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
642 // print with indirect values
643 int print(char *, size_t, Value
*, Value
*, DataType ty
= TYPE_NONE
) const;
645 inline void setFile(DataFile file
, ubyte fileIndex
= 0)
648 reg
.fileIndex
= fileIndex
;
651 inline void setOffset(int32_t offset
);
652 inline void setAddress(Symbol
*base
, int32_t offset
);
653 inline void setSV(SVSemantic sv
, uint32_t idx
= 0);
655 inline const Symbol
*getBase() const { return baseSym
; }
658 Symbol
*baseSym
; // array base for Symbols representing array elements
661 class ImmediateValue
: public Value
665 ImmediateValue(Program
*, uint32_t);
666 ImmediateValue(Program
*, float);
667 ImmediateValue(Program
*, double);
668 // NOTE: not added to program with
669 ImmediateValue(const ImmediateValue
*, DataType ty
);
670 ~ImmediateValue() { };
672 virtual ImmediateValue
*clone(ClonePolicy
<Function
>&) const;
674 virtual bool equals(const Value
*that
, bool strict
) const;
676 // these only work if 'type' is valid (we mostly use untyped literals):
677 bool isInteger(const int ival
) const; // ival is cast to this' type
678 bool isNegative() const;
683 // for constant folding:
684 ImmediateValue
operator+(const ImmediateValue
&) const;
685 ImmediateValue
operator-(const ImmediateValue
&) const;
686 ImmediateValue
operator*(const ImmediateValue
&) const;
687 ImmediateValue
operator/(const ImmediateValue
&) const;
689 ImmediateValue
& operator=(const ImmediateValue
&); // only sets value !
691 bool compare(CondCode cc
, float fval
) const;
693 virtual int print(char *, size_t, DataType ty
= TYPE_NONE
) const;
700 Instruction(Function
*, operation
, DataType
);
701 virtual ~Instruction();
703 virtual Instruction
*clone(ClonePolicy
<Function
>&,
704 Instruction
* = NULL
) const;
706 void setDef(int i
, Value
*);
707 void setSrc(int s
, Value
*);
708 void setSrc(int s
, const ValueRef
&);
709 void swapSources(int a
, int b
);
710 void moveSources(int s
, int delta
);
711 bool setIndirect(int s
, int dim
, Value
*);
713 inline ValueRef
& src(int s
) { return srcs
[s
]; }
714 inline ValueDef
& def(int s
) { return defs
[s
]; }
715 inline const ValueRef
& src(int s
) const { return srcs
[s
]; }
716 inline const ValueDef
& def(int s
) const { return defs
[s
]; }
718 inline Value
*getDef(int d
) const { return defs
[d
].get(); }
719 inline Value
*getSrc(int s
) const { return srcs
[s
].get(); }
720 inline Value
*getIndirect(int s
, int dim
) const;
722 inline bool defExists(unsigned d
) const
724 return d
< defs
.size() && defs
[d
].exists();
726 inline bool srcExists(unsigned s
) const
728 return s
< srcs
.size() && srcs
[s
].exists();
731 inline bool constrainedDefs() const;
733 bool setPredicate(CondCode ccode
, Value
*);
734 inline Value
*getPredicate() const;
735 bool writesPredicate() const;
736 inline bool isPredicated() const { return predSrc
>= 0; }
738 inline void setFlagsSrc(int s
, Value
*);
739 inline void setFlagsDef(int d
, Value
*);
740 inline bool usesFlags() const { return flagsSrc
>= 0; }
742 unsigned int defCount() const { return defs
.size(); };
743 unsigned int defCount(unsigned int mask
, bool singleFile
= false) const;
744 unsigned int srcCount() const { return srcs
.size(); };
745 unsigned int srcCount(unsigned int mask
, bool singleFile
= false) const;
747 // save & remove / set indirect[0,1] and predicate source
748 void takeExtraSources(int s
, Value
*[3]);
749 void putExtraSources(int s
, Value
*[3]);
751 inline void setType(DataType type
) { dType
= sType
= type
; }
753 inline void setType(DataType dtype
, DataType stype
)
759 inline bool isPseudo() const { return op
< OP_MOV
; }
762 bool isCommutationLegal(const Instruction
*) const; // must be adjacent !
763 bool isActionEqual(const Instruction
*) const;
764 bool isResultEqual(const Instruction
*) const;
768 inline CmpInstruction
*asCmp();
769 inline TexInstruction
*asTex();
770 inline FlowInstruction
*asFlow();
771 inline const TexInstruction
*asTex() const;
772 inline const CmpInstruction
*asCmp() const;
773 inline const FlowInstruction
*asFlow() const;
779 int serial
; // CFG order
782 DataType dType
; // destination or defining type
783 DataType sType
; // source or secondary type
788 uint16_t subOp
; // quadop, 1 for mul-high, etc.
790 unsigned encSize
: 4; // encoding size in bytes
791 unsigned saturate
: 1; // to [0.0f, 1.0f]
792 unsigned join
: 1; // converge control flow (use OP_JOIN until end)
793 unsigned fixed
: 1; // prevent dead code elimination
794 unsigned terminator
: 1; // end of basic block
795 unsigned ftz
: 1; // flush denormal to zero
796 unsigned dnz
: 1; // denormals, NaN are zero
797 unsigned ipa
: 4; // interpolation mode
799 unsigned perPatch
: 1;
800 unsigned exit
: 1; // terminate program after insn
801 unsigned mask
: 4; // for vector ops
803 int8_t postFactor
; // MUL/DIV(if < 0) by 1 << postFactor
809 uint32_t sched
; // scheduling data (NOTE: maybe move to separate storage)
814 std::deque
<ValueDef
> defs
; // no gaps !
815 std::deque
<ValueRef
> srcs
; // no gaps !
817 // instruction specific methods:
818 // (don't want to subclass, would need more constructors and memory pools)
820 inline void setInterpolate(unsigned int mode
) { ipa
= mode
; }
822 unsigned int getInterpMode() const { return ipa
& 0x3; }
823 unsigned int getSampleMode() const { return ipa
& 0xc; }
831 TXQ_DIMS
, /* x, y, z, levels */
832 TXQ_TYPE
, /* ?, ?, samples, ? */
840 class TexInstruction
: public Instruction
846 Target(TexTarget targ
= TEX_TARGET_2D
) : target(targ
) { }
848 const char *getName() const { return descTable
[target
].name
; }
849 unsigned int getArgCount() const { return descTable
[target
].argc
; }
850 unsigned int getDim() const { return descTable
[target
].dim
; }
851 int isArray() const { return descTable
[target
].array
? 1 : 0; }
852 int isCube() const { return descTable
[target
].cube
? 1 : 0; }
853 int isShadow() const { return descTable
[target
].shadow
? 1 : 0; }
855 return target
== TEX_TARGET_2D_MS
|| target
== TEX_TARGET_2D_MS_ARRAY
; }
859 target
= TEX_TARGET_2D_ARRAY
;
861 target
= TEX_TARGET_2D
;
865 Target
& operator=(TexTarget targ
)
867 assert(targ
< TEX_TARGET_COUNT
);
872 inline bool operator==(TexTarget targ
) const { return target
== targ
; }
873 inline bool operator!=(TexTarget targ
) const { return target
!= targ
; }
875 enum TexTarget
getEnum() const { return target
; }
888 static const struct Desc descTable
[TEX_TARGET_COUNT
];
891 enum TexTarget target
;
895 TexInstruction(Function
*, operation
);
896 virtual ~TexInstruction();
898 virtual TexInstruction
*clone(ClonePolicy
<Function
>&,
899 Instruction
* = NULL
) const;
901 inline void setTexture(Target targ
, uint8_t r
, uint8_t s
)
908 void setIndirectR(Value
*);
909 void setIndirectS(Value
*);
910 inline Value
*getIndirectR() const;
911 inline Value
*getIndirectS() const;
925 bool liveOnly
; // only execute on live pixels of a quad (optimization)
929 int8_t useOffsets
; // 0, 1, or 4 for textureGatherOffsets
930 int8_t offset
[3]; // only used on nv50
937 ValueRef offset
[4][3];
940 class CmpInstruction
: public Instruction
943 CmpInstruction(Function
*, operation
);
945 virtual CmpInstruction
*clone(ClonePolicy
<Function
>&,
946 Instruction
* = NULL
) const;
948 void setCondition(CondCode cond
) { setCond
= cond
; }
949 CondCode
getCondition() const { return setCond
; }
955 class FlowInstruction
: public Instruction
958 FlowInstruction(Function
*, operation
, void *target
);
960 virtual FlowInstruction
*clone(ClonePolicy
<Function
>&,
961 Instruction
* = NULL
) const;
964 unsigned allWarp
: 1;
965 unsigned absolute
: 1;
967 unsigned builtin
: 1; // true for calls to emulation code
968 unsigned indirect
: 1; // target in src(0)
980 BasicBlock(Function
*);
983 BasicBlock
*clone(ClonePolicy
<Function
>&) const;
985 inline int getId() const { return id
; }
986 inline unsigned int getInsnCount() const { return numInsns
; }
987 inline bool isTerminated() const { return exit
&& exit
->terminator
; }
989 bool dominatedBy(BasicBlock
*bb
);
990 inline bool reachableBy(const BasicBlock
*by
, const BasicBlock
*term
);
992 // returns mask of conditional out blocks
993 // e.g. 3 for IF { .. } ELSE { .. } ENDIF, 1 for IF { .. } ENDIF
994 unsigned int initiatesSimpleConditional() const;
997 Function
*getFunction() const { return func
; }
998 Program
*getProgram() const { return program
; }
1000 Instruction
*getEntry() const { return entry
; } // first non-phi instruction
1001 Instruction
*getPhi() const { return phi
; }
1002 Instruction
*getFirst() const { return phi
? phi
: entry
; }
1003 Instruction
*getExit() const { return exit
; }
1005 void insertHead(Instruction
*);
1006 void insertTail(Instruction
*);
1007 void insertBefore(Instruction
*, Instruction
*);
1008 void insertAfter(Instruction
*, Instruction
*);
1009 void remove(Instruction
*);
1010 void permuteAdjacent(Instruction
*, Instruction
*);
1012 BasicBlock
*idom() const;
1014 // NOTE: currently does not rebuild the dominator tree
1015 BasicBlock
*splitBefore(Instruction
*, bool attach
= true);
1016 BasicBlock
*splitAfter(Instruction
*, bool attach
= true);
1018 DLList
& getDF() { return df
; }
1019 DLList::Iterator
iterDF() { return df
.iterator(); }
1021 static inline BasicBlock
*get(Iterator
&);
1022 static inline BasicBlock
*get(Graph::Node
*);
1025 Graph::Node cfg
; // first edge is branch *taken* (the ELSE branch)
1034 Instruction
*joinAt
; // for quick reference
1036 bool explicitCont
; // loop headers: true if loop contains continue stmts
1046 unsigned int numInsns
;
1052 void splitCommon(Instruction
*, BasicBlock
*, bool attach
);
1058 Function(Program
*, const char *name
, uint32_t label
);
1061 static inline Function
*get(Graph::Node
*node
);
1063 inline Program
*getProgram() const { return prog
; }
1064 inline const char *getName() const { return name
; }
1065 inline int getId() const { return id
; }
1066 inline uint32_t getLabel() const { return label
; }
1069 void printLiveIntervals() const;
1070 void printCFGraph(const char *filePath
);
1072 bool setEntry(BasicBlock
*);
1073 bool setExit(BasicBlock
*);
1075 unsigned int orderInstructions(ArrayList
&);
1077 inline void add(BasicBlock
*bb
, int& id
) { allBBlocks
.insert(bb
, id
); }
1078 inline void add(Instruction
*insn
, int& id
) { allInsns
.insert(insn
, id
); }
1079 inline void add(LValue
*lval
, int& id
) { allLValues
.insert(lval
, id
); }
1081 inline LValue
*getLValue(int id
);
1083 void buildLiveSets();
1084 void buildDefSets();
1085 bool convertToSSA();
1088 std::deque
<ValueDef
> ins
;
1089 std::deque
<ValueRef
> outs
;
1090 std::deque
<Value
*> clobbers
;
1093 Graph::Node
*cfgExit
;
1095 Graph::Node call
; // node in the call graph
1097 BasicBlock
**bbArray
; // BBs in emission order
1100 unsigned int loopNestingBound
;
1108 uint32_t tlsBase
; // base address for l[] space (if no stack pointer is used)
1111 ArrayList allBBlocks
;
1113 ArrayList allLValues
;
1116 void buildLiveSetsPreSSA(BasicBlock
*, const int sequence
);
1117 void buildDefSetsPreSSA(BasicBlock
*bb
, const int seq
);
1122 const char *const name
;
1129 CG_STAGE_SSA
, // expected directly before register allocation
1139 TYPE_TESSELLATION_CONTROL
,
1140 TYPE_TESSELLATION_EVAL
,
1146 Program(Type type
, Target
*targ
);
1151 Type
getType() const { return progType
; }
1153 inline void add(Function
*fn
, int& id
) { allFuncs
.insert(fn
, id
); }
1154 inline void del(Function
*fn
, int& id
) { allFuncs
.remove(id
); }
1155 inline void add(Value
*rval
, int& id
) { allRValues
.insert(rval
, id
); }
1157 bool makeFromTGSI(struct nv50_ir_prog_info
*);
1158 bool makeFromSM4(struct nv50_ir_prog_info
*);
1159 bool convertToSSA();
1160 bool optimizeSSA(int level
);
1161 bool optimizePostRA(int level
);
1162 bool registerAllocation();
1163 bool emitBinary(struct nv50_ir_prog_info
*);
1165 const Target
*getTarget() const { return target
; }
1168 void emitSymbolTable(struct nv50_ir_prog_info
*);
1178 ArrayList allRValues
;
1182 uint32_t tlsSize
; // size required for FILE_MEMORY_LOCAL
1186 MemoryPool mem_Instruction
;
1187 MemoryPool mem_CmpInstruction
;
1188 MemoryPool mem_TexInstruction
;
1189 MemoryPool mem_FlowInstruction
;
1190 MemoryPool mem_LValue
;
1191 MemoryPool mem_Symbol
;
1192 MemoryPool mem_ImmediateValue
;
1197 void *targetPriv
; // e.g. to carry information between passes
1199 const struct nv50_ir_prog_info
*driver
; // for driver configuration
1201 void releaseInstruction(Instruction
*);
1202 void releaseValue(Value
*);
1205 // TODO: add const version
1209 bool run(Program
*, bool ordered
= false, bool skipPhi
= false);
1210 bool run(Function
*, bool ordered
= false, bool skipPhi
= false);
1213 // return false to continue with next entity on next higher level
1214 virtual bool visit(Function
*) { return true; }
1215 virtual bool visit(BasicBlock
*) { return true; }
1216 virtual bool visit(Instruction
*) { return false; }
1218 bool doRun(Program
*, bool ordered
, bool skipPhi
);
1219 bool doRun(Function
*, bool ordered
, bool skipPhi
);
1227 // =============================================================================
1229 #include "codegen/nv50_ir_inlines.h"
1231 } // namespace nv50_ir
1233 #endif // __NV50_IR_H__