98
98
#include " llvm/IR/User.h"
99
99
#include " llvm/IR/Value.h"
100
100
#include " llvm/SandboxIR/Tracker.h"
101
+ #include " llvm/SandboxIR/Type.h"
101
102
#include " llvm/SandboxIR/Use.h"
102
103
#include " llvm/Support/raw_ostream.h"
103
104
#include < iterator>
@@ -378,7 +379,7 @@ class Value {
378
379
return Cnt == Num;
379
380
}
380
381
381
- Type *getType () const { return Val-> getType (); }
382
+ Type *getType () const ;
382
383
383
384
Context &getContext () const { return Ctx; }
384
385
@@ -566,8 +567,7 @@ class ConstantInt : public Constant {
566
567
public:
567
568
// / If Ty is a vector type, return a Constant with a splat of the given
568
569
// / value. Otherwise return a ConstantInt for the given value.
569
- static ConstantInt *get (Type *Ty, uint64_t V, Context &Ctx,
570
- bool IsSigned = false );
570
+ static ConstantInt *get (Type *Ty, uint64_t V, bool IsSigned = false );
571
571
572
572
// TODO: Implement missing functions.
573
573
@@ -1014,10 +1014,7 @@ class ExtractElementInst final
1014
1014
Value *getIndexOperand () { return getOperand (1 ); }
1015
1015
const Value *getVectorOperand () const { return getOperand (0 ); }
1016
1016
const Value *getIndexOperand () const { return getOperand (1 ); }
1017
-
1018
- VectorType *getVectorOperandType () const {
1019
- return cast<VectorType>(getVectorOperand ()->getType ());
1020
- }
1017
+ VectorType *getVectorOperandType () const ;
1021
1018
};
1022
1019
1023
1020
class ShuffleVectorInst final
@@ -1062,9 +1059,7 @@ class ShuffleVectorInst final
1062
1059
}
1063
1060
1064
1061
// / Overload to return most specific vector type.
1065
- VectorType *getType () const {
1066
- return cast<llvm::ShuffleVectorInst>(Val)->getType ();
1067
- }
1062
+ VectorType *getType () const ;
1068
1063
1069
1064
// / Return the shuffle mask value of this instruction for the given element
1070
1065
// / index. Return PoisonMaskElem if the element is undef.
@@ -1090,7 +1085,7 @@ class ShuffleVectorInst final
1090
1085
Constant *getShuffleMaskForBitcode () const ;
1091
1086
1092
1087
static Constant *convertShuffleMaskForBitcode (ArrayRef<int > Mask,
1093
- Type *ResultTy, Context &Ctx );
1088
+ Type *ResultTy);
1094
1089
1095
1090
void setShuffleMask (ArrayRef<int > Mask);
1096
1091
@@ -1713,9 +1708,7 @@ class CallBase : public SingleLLVMInstructionImpl<llvm::CallBase> {
1713
1708
Opc == Instruction::ClassID::CallBr;
1714
1709
}
1715
1710
1716
- FunctionType *getFunctionType () const {
1717
- return cast<llvm::CallBase>(Val)->getFunctionType ();
1718
- }
1711
+ FunctionType *getFunctionType () const ;
1719
1712
1720
1713
op_iterator data_operands_begin () { return op_begin (); }
1721
1714
const_op_iterator data_operands_begin () const {
@@ -2131,12 +2124,8 @@ class GetElementPtrInst final
2131
2124
return From->getSubclassID () == ClassID::GetElementPtr;
2132
2125
}
2133
2126
2134
- Type *getSourceElementType () const {
2135
- return cast<llvm::GetElementPtrInst>(Val)->getSourceElementType ();
2136
- }
2137
- Type *getResultElementType () const {
2138
- return cast<llvm::GetElementPtrInst>(Val)->getResultElementType ();
2139
- }
2127
+ Type *getSourceElementType () const ;
2128
+ Type *getResultElementType () const ;
2140
2129
unsigned getAddressSpace () const {
2141
2130
return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace ();
2142
2131
}
@@ -2160,9 +2149,7 @@ class GetElementPtrInst final
2160
2149
static unsigned getPointerOperandIndex () {
2161
2150
return llvm::GetElementPtrInst::getPointerOperandIndex ();
2162
2151
}
2163
- Type *getPointerOperandType () const {
2164
- return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType ();
2165
- }
2152
+ Type *getPointerOperandType () const ;
2166
2153
unsigned getPointerAddressSpace () const {
2167
2154
return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace ();
2168
2155
}
@@ -2709,9 +2696,7 @@ class AllocaInst final : public UnaryInstruction {
2709
2696
return const_cast <AllocaInst *>(this )->getArraySize ();
2710
2697
}
2711
2698
// / Overload to return most specific pointer type.
2712
- PointerType *getType () const {
2713
- return cast<llvm::AllocaInst>(Val)->getType ();
2714
- }
2699
+ PointerType *getType () const ;
2715
2700
// / Return the address space for the allocation.
2716
2701
unsigned getAddressSpace () const {
2717
2702
return cast<llvm::AllocaInst>(Val)->getAddressSpace ();
@@ -2727,9 +2712,7 @@ class AllocaInst final : public UnaryInstruction {
2727
2712
return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits (DL);
2728
2713
}
2729
2714
// / Return the type that is being allocated by the instruction.
2730
- Type *getAllocatedType () const {
2731
- return cast<llvm::AllocaInst>(Val)->getAllocatedType ();
2732
- }
2715
+ Type *getAllocatedType () const ;
2733
2716
// / for use only in special circumstances that need to generically
2734
2717
// / transform a whole instruction (eg: IR linking and vectorization).
2735
2718
void setAllocatedType (Type *Ty);
@@ -2811,8 +2794,8 @@ class CastInst : public UnaryInstruction {
2811
2794
const Twine &Name = " " );
2812
2795
// / For isa/dyn_cast.
2813
2796
static bool classof (const Value *From);
2814
- Type *getSrcTy () const { return cast<llvm::CastInst>(Val)-> getSrcTy (); }
2815
- Type *getDestTy () const { return cast<llvm::CastInst>(Val)-> getDestTy (); }
2797
+ Type *getSrcTy () const ;
2798
+ Type *getDestTy () const ;
2816
2799
};
2817
2800
2818
2801
// / Instruction that can have a nneg flag (zext/uitofp).
@@ -2992,13 +2975,25 @@ class OpaqueInst : public SingleLLVMInstructionImpl<llvm::Instruction> {
2992
2975
class Context {
2993
2976
protected:
2994
2977
LLVMContext &LLVMCtx;
2978
+ friend class Type ; // For LLVMCtx.
2979
+ friend class PointerType ; // For LLVMCtx.
2995
2980
Tracker IRTracker;
2996
2981
2997
2982
// / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
2998
2983
// / SandboxIR objects.
2999
2984
DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
3000
2985
LLVMValueToValueMap;
3001
2986
2987
+ // / Type has a protected destructor to prohibit the user from managing the
2988
+ // / lifetime of the Type objects. Context is friend of Type, and this custom
2989
+ // / deleter can destroy Type.
2990
+ struct TypeDeleter {
2991
+ void operator ()(Type *Ty) { delete Ty; }
2992
+ };
2993
+ // / Maps LLVM Type to the corresonding sandboxir::Type. Owns all Sandbox IR
2994
+ // / Type objects.
2995
+ DenseMap<llvm::Type *, std::unique_ptr<Type, TypeDeleter>> LLVMTypeToTypeMap;
2996
+
3002
2997
// / Remove \p V from the maps and returns the unique_ptr.
3003
2998
std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
3004
2999
// / Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
@@ -3033,7 +3028,6 @@ class Context {
3033
3028
// / Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
3034
3029
// / also create all contents of the block.
3035
3030
BasicBlock *createBasicBlock (llvm::BasicBlock *BB);
3036
-
3037
3031
friend class BasicBlock ; // For getOrCreateValue().
3038
3032
3039
3033
IRBuilder<ConstantFolder> LLVMIRBuilder;
@@ -3119,6 +3113,15 @@ class Context {
3119
3113
const sandboxir::Value *getValue (const llvm::Value *V) const {
3120
3114
return getValue (const_cast <llvm::Value *>(V));
3121
3115
}
3116
+
3117
+ Type *getType (llvm::Type *LLVMTy) {
3118
+ auto Pair = LLVMTypeToTypeMap.insert ({LLVMTy, nullptr });
3119
+ auto It = Pair.first ;
3120
+ if (Pair.second )
3121
+ It->second = std::unique_ptr<Type, TypeDeleter>(new Type (LLVMTy, *this ));
3122
+ return It->second .get ();
3123
+ }
3124
+
3122
3125
// / Create a sandboxir::Function for an existing LLVM IR \p F, including all
3123
3126
// / blocks and instructions.
3124
3127
// / This is the main API function for creating Sandbox IR.
@@ -3165,9 +3168,7 @@ class Function : public Constant {
3165
3168
LLVMBBToBB BBGetter (Ctx);
3166
3169
return iterator (cast<llvm::Function>(Val)->end (), BBGetter);
3167
3170
}
3168
- FunctionType *getFunctionType () const {
3169
- return cast<llvm::Function>(Val)->getFunctionType ();
3170
- }
3171
+ FunctionType *getFunctionType () const ;
3171
3172
3172
3173
#ifndef NDEBUG
3173
3174
void verify () const final {
0 commit comments