102
102
#include " llvm/IR/User.h"
103
103
#include " llvm/IR/Value.h"
104
104
#include " llvm/SandboxIR/Tracker.h"
105
+ #include " llvm/SandboxIR/Type.h"
105
106
#include " llvm/SandboxIR/Use.h"
106
107
#include " llvm/Support/raw_ostream.h"
107
108
#include < iterator>
@@ -386,7 +387,7 @@ class Value {
386
387
return Cnt == Num;
387
388
}
388
389
389
- Type *getType () const { return Val-> getType (); }
390
+ Type *getType () const ;
390
391
391
392
Context &getContext () const { return Ctx; }
392
393
@@ -574,8 +575,7 @@ class ConstantInt : public Constant {
574
575
public:
575
576
// / If Ty is a vector type, return a Constant with a splat of the given
576
577
// / value. Otherwise return a ConstantInt for the given value.
577
- static ConstantInt *get (Type *Ty, uint64_t V, Context &Ctx,
578
- bool IsSigned = false );
578
+ static ConstantInt *get (Type *Ty, uint64_t V, bool IsSigned = false );
579
579
580
580
// TODO: Implement missing functions.
581
581
@@ -1024,10 +1024,7 @@ class ExtractElementInst final
1024
1024
Value *getIndexOperand () { return getOperand (1 ); }
1025
1025
const Value *getVectorOperand () const { return getOperand (0 ); }
1026
1026
const Value *getIndexOperand () const { return getOperand (1 ); }
1027
-
1028
- VectorType *getVectorOperandType () const {
1029
- return cast<VectorType>(getVectorOperand ()->getType ());
1030
- }
1027
+ VectorType *getVectorOperandType () const ;
1031
1028
};
1032
1029
1033
1030
class ShuffleVectorInst final
@@ -1072,9 +1069,7 @@ class ShuffleVectorInst final
1072
1069
}
1073
1070
1074
1071
// / Overload to return most specific vector type.
1075
- VectorType *getType () const {
1076
- return cast<llvm::ShuffleVectorInst>(Val)->getType ();
1077
- }
1072
+ VectorType *getType () const ;
1078
1073
1079
1074
// / Return the shuffle mask value of this instruction for the given element
1080
1075
// / index. Return PoisonMaskElem if the element is undef.
@@ -1100,7 +1095,7 @@ class ShuffleVectorInst final
1100
1095
Constant *getShuffleMaskForBitcode () const ;
1101
1096
1102
1097
static Constant *convertShuffleMaskForBitcode (ArrayRef<int > Mask,
1103
- Type *ResultTy, Context &Ctx );
1098
+ Type *ResultTy);
1104
1099
1105
1100
void setShuffleMask (ArrayRef<int > Mask);
1106
1101
@@ -1646,9 +1641,7 @@ class ExtractValueInst : public UnaryInstruction {
1646
1641
// / with an extractvalue instruction with the specified parameters.
1647
1642
// /
1648
1643
// / Null is returned if the indices are invalid for the specified type.
1649
- static Type *getIndexedType (Type *Agg, ArrayRef<unsigned > Idxs) {
1650
- return llvm::ExtractValueInst::getIndexedType (Agg, Idxs);
1651
- }
1644
+ static Type *getIndexedType (Type *Agg, ArrayRef<unsigned > Idxs);
1652
1645
1653
1646
using idx_iterator = llvm::ExtractValueInst::idx_iterator;
1654
1647
@@ -1843,9 +1836,7 @@ class CallBase : public SingleLLVMInstructionImpl<llvm::CallBase> {
1843
1836
Opc == Instruction::ClassID::CallBr;
1844
1837
}
1845
1838
1846
- FunctionType *getFunctionType () const {
1847
- return cast<llvm::CallBase>(Val)->getFunctionType ();
1848
- }
1839
+ FunctionType *getFunctionType () const ;
1849
1840
1850
1841
op_iterator data_operands_begin () { return op_begin (); }
1851
1842
const_op_iterator data_operands_begin () const {
@@ -2261,12 +2252,8 @@ class GetElementPtrInst final
2261
2252
return From->getSubclassID () == ClassID::GetElementPtr;
2262
2253
}
2263
2254
2264
- Type *getSourceElementType () const {
2265
- return cast<llvm::GetElementPtrInst>(Val)->getSourceElementType ();
2266
- }
2267
- Type *getResultElementType () const {
2268
- return cast<llvm::GetElementPtrInst>(Val)->getResultElementType ();
2269
- }
2255
+ Type *getSourceElementType () const ;
2256
+ Type *getResultElementType () const ;
2270
2257
unsigned getAddressSpace () const {
2271
2258
return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace ();
2272
2259
}
@@ -2290,9 +2277,7 @@ class GetElementPtrInst final
2290
2277
static unsigned getPointerOperandIndex () {
2291
2278
return llvm::GetElementPtrInst::getPointerOperandIndex ();
2292
2279
}
2293
- Type *getPointerOperandType () const {
2294
- return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType ();
2295
- }
2280
+ Type *getPointerOperandType () const ;
2296
2281
unsigned getPointerAddressSpace () const {
2297
2282
return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace ();
2298
2283
}
@@ -2843,9 +2828,7 @@ class AllocaInst final : public UnaryInstruction {
2843
2828
return const_cast <AllocaInst *>(this )->getArraySize ();
2844
2829
}
2845
2830
// / Overload to return most specific pointer type.
2846
- PointerType *getType () const {
2847
- return cast<llvm::AllocaInst>(Val)->getType ();
2848
- }
2831
+ PointerType *getType () const ;
2849
2832
// / Return the address space for the allocation.
2850
2833
unsigned getAddressSpace () const {
2851
2834
return cast<llvm::AllocaInst>(Val)->getAddressSpace ();
@@ -2861,9 +2844,7 @@ class AllocaInst final : public UnaryInstruction {
2861
2844
return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits (DL);
2862
2845
}
2863
2846
// / Return the type that is being allocated by the instruction.
2864
- Type *getAllocatedType () const {
2865
- return cast<llvm::AllocaInst>(Val)->getAllocatedType ();
2866
- }
2847
+ Type *getAllocatedType () const ;
2867
2848
// / for use only in special circumstances that need to generically
2868
2849
// / transform a whole instruction (eg: IR linking and vectorization).
2869
2850
void setAllocatedType (Type *Ty);
@@ -2945,8 +2926,8 @@ class CastInst : public UnaryInstruction {
2945
2926
const Twine &Name = " " );
2946
2927
// / For isa/dyn_cast.
2947
2928
static bool classof (const Value *From);
2948
- Type *getSrcTy () const { return cast<llvm::CastInst>(Val)-> getSrcTy (); }
2949
- Type *getDestTy () const { return cast<llvm::CastInst>(Val)-> getDestTy (); }
2929
+ Type *getSrcTy () const ;
2930
+ Type *getDestTy () const ;
2950
2931
};
2951
2932
2952
2933
// / Instruction that can have a nneg flag (zext/uitofp).
@@ -3126,13 +3107,25 @@ class OpaqueInst : public SingleLLVMInstructionImpl<llvm::Instruction> {
3126
3107
class Context {
3127
3108
protected:
3128
3109
LLVMContext &LLVMCtx;
3110
+ friend class Type ; // For LLVMCtx.
3111
+ friend class PointerType ; // For LLVMCtx.
3129
3112
Tracker IRTracker;
3130
3113
3131
3114
// / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
3132
3115
// / SandboxIR objects.
3133
3116
DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
3134
3117
LLVMValueToValueMap;
3135
3118
3119
+ // / Type has a protected destructor to prohibit the user from managing the
3120
+ // / lifetime of the Type objects. Context is friend of Type, and this custom
3121
+ // / deleter can destroy Type.
3122
+ struct TypeDeleter {
3123
+ void operator ()(Type *Ty) { delete Ty; }
3124
+ };
3125
+ // / Maps LLVM Type to the corresonding sandboxir::Type. Owns all Sandbox IR
3126
+ // / Type objects.
3127
+ DenseMap<llvm::Type *, std::unique_ptr<Type, TypeDeleter>> LLVMTypeToTypeMap;
3128
+
3136
3129
// / Remove \p V from the maps and returns the unique_ptr.
3137
3130
std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
3138
3131
// / Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
@@ -3167,7 +3160,6 @@ class Context {
3167
3160
// / Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
3168
3161
// / also create all contents of the block.
3169
3162
BasicBlock *createBasicBlock (llvm::BasicBlock *BB);
3170
-
3171
3163
friend class BasicBlock ; // For getOrCreateValue().
3172
3164
3173
3165
IRBuilder<ConstantFolder> LLVMIRBuilder;
@@ -3257,6 +3249,17 @@ class Context {
3257
3249
const sandboxir::Value *getValue (const llvm::Value *V) const {
3258
3250
return getValue (const_cast <llvm::Value *>(V));
3259
3251
}
3252
+
3253
+ Type *getType (llvm::Type *LLVMTy) {
3254
+ if (LLVMTy == nullptr )
3255
+ return nullptr ;
3256
+ auto Pair = LLVMTypeToTypeMap.insert ({LLVMTy, nullptr });
3257
+ auto It = Pair.first ;
3258
+ if (Pair.second )
3259
+ It->second = std::unique_ptr<Type, TypeDeleter>(new Type (LLVMTy, *this ));
3260
+ return It->second .get ();
3261
+ }
3262
+
3260
3263
// / Create a sandboxir::Function for an existing LLVM IR \p F, including all
3261
3264
// / blocks and instructions.
3262
3265
// / This is the main API function for creating Sandbox IR.
@@ -3303,9 +3306,7 @@ class Function : public Constant {
3303
3306
LLVMBBToBB BBGetter (Ctx);
3304
3307
return iterator (cast<llvm::Function>(Val)->end (), BBGetter);
3305
3308
}
3306
- FunctionType *getFunctionType () const {
3307
- return cast<llvm::Function>(Val)->getFunctionType ();
3308
- }
3309
+ FunctionType *getFunctionType () const ;
3309
3310
3310
3311
#ifndef NDEBUG
3311
3312
void verify () const final {
0 commit comments