9898#include " llvm/IR/User.h"
9999#include " llvm/IR/Value.h"
100100#include " llvm/SandboxIR/Tracker.h"
101+ #include " llvm/SandboxIR/Type.h"
101102#include " llvm/SandboxIR/Use.h"
102103#include " llvm/Support/raw_ostream.h"
103104#include < iterator>
@@ -378,7 +379,7 @@ class Value {
378379 return Cnt == Num;
379380 }
380381
381- Type *getType () const { return Val-> getType (); }
382+ Type *getType () const ;
382383
383384 Context &getContext () const { return Ctx; }
384385
@@ -566,8 +567,7 @@ class ConstantInt : public Constant {
566567public:
567568 // / If Ty is a vector type, return a Constant with a splat of the given
568569 // / 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 );
571571
572572 // TODO: Implement missing functions.
573573
@@ -1014,10 +1014,7 @@ class ExtractElementInst final
10141014 Value *getIndexOperand () { return getOperand (1 ); }
10151015 const Value *getVectorOperand () const { return getOperand (0 ); }
10161016 const Value *getIndexOperand () const { return getOperand (1 ); }
1017-
1018- VectorType *getVectorOperandType () const {
1019- return cast<VectorType>(getVectorOperand ()->getType ());
1020- }
1017+ VectorType *getVectorOperandType () const ;
10211018};
10221019
10231020class ShuffleVectorInst final
@@ -1062,9 +1059,7 @@ class ShuffleVectorInst final
10621059 }
10631060
10641061 // / Overload to return most specific vector type.
1065- VectorType *getType () const {
1066- return cast<llvm::ShuffleVectorInst>(Val)->getType ();
1067- }
1062+ VectorType *getType () const ;
10681063
10691064 // / Return the shuffle mask value of this instruction for the given element
10701065 // / index. Return PoisonMaskElem if the element is undef.
@@ -1090,7 +1085,7 @@ class ShuffleVectorInst final
10901085 Constant *getShuffleMaskForBitcode () const ;
10911086
10921087 static Constant *convertShuffleMaskForBitcode (ArrayRef<int > Mask,
1093- Type *ResultTy, Context &Ctx );
1088+ Type *ResultTy);
10941089
10951090 void setShuffleMask (ArrayRef<int > Mask);
10961091
@@ -1713,9 +1708,7 @@ class CallBase : public SingleLLVMInstructionImpl<llvm::CallBase> {
17131708 Opc == Instruction::ClassID::CallBr;
17141709 }
17151710
1716- FunctionType *getFunctionType () const {
1717- return cast<llvm::CallBase>(Val)->getFunctionType ();
1718- }
1711+ FunctionType *getFunctionType () const ;
17191712
17201713 op_iterator data_operands_begin () { return op_begin (); }
17211714 const_op_iterator data_operands_begin () const {
@@ -2131,12 +2124,8 @@ class GetElementPtrInst final
21312124 return From->getSubclassID () == ClassID::GetElementPtr;
21322125 }
21332126
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 ;
21402129 unsigned getAddressSpace () const {
21412130 return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace ();
21422131 }
@@ -2160,9 +2149,7 @@ class GetElementPtrInst final
21602149 static unsigned getPointerOperandIndex () {
21612150 return llvm::GetElementPtrInst::getPointerOperandIndex ();
21622151 }
2163- Type *getPointerOperandType () const {
2164- return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType ();
2165- }
2152+ Type *getPointerOperandType () const ;
21662153 unsigned getPointerAddressSpace () const {
21672154 return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace ();
21682155 }
@@ -2709,9 +2696,7 @@ class AllocaInst final : public UnaryInstruction {
27092696 return const_cast <AllocaInst *>(this )->getArraySize ();
27102697 }
27112698 // / Overload to return most specific pointer type.
2712- PointerType *getType () const {
2713- return cast<llvm::AllocaInst>(Val)->getType ();
2714- }
2699+ PointerType *getType () const ;
27152700 // / Return the address space for the allocation.
27162701 unsigned getAddressSpace () const {
27172702 return cast<llvm::AllocaInst>(Val)->getAddressSpace ();
@@ -2727,9 +2712,7 @@ class AllocaInst final : public UnaryInstruction {
27272712 return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits (DL);
27282713 }
27292714 // / 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 ;
27332716 // / for use only in special circumstances that need to generically
27342717 // / transform a whole instruction (eg: IR linking and vectorization).
27352718 void setAllocatedType (Type *Ty);
@@ -2811,8 +2794,8 @@ class CastInst : public UnaryInstruction {
28112794 const Twine &Name = " " );
28122795 // / For isa/dyn_cast.
28132796 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 ;
28162799};
28172800
28182801// / Instruction that can have a nneg flag (zext/uitofp).
@@ -2992,13 +2975,25 @@ class OpaqueInst : public SingleLLVMInstructionImpl<llvm::Instruction> {
29922975class Context {
29932976protected:
29942977 LLVMContext &LLVMCtx;
2978+ friend class Type ; // For LLVMCtx.
2979+ friend class PointerType ; // For LLVMCtx.
29952980 Tracker IRTracker;
29962981
29972982 // / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
29982983 // / SandboxIR objects.
29992984 DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
30002985 LLVMValueToValueMap;
30012986
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+
30022997 // / Remove \p V from the maps and returns the unique_ptr.
30032998 std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
30042999 // / Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
@@ -3033,7 +3028,6 @@ class Context {
30333028 // / Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
30343029 // / also create all contents of the block.
30353030 BasicBlock *createBasicBlock (llvm::BasicBlock *BB);
3036-
30373031 friend class BasicBlock ; // For getOrCreateValue().
30383032
30393033 IRBuilder<ConstantFolder> LLVMIRBuilder;
@@ -3119,6 +3113,15 @@ class Context {
31193113 const sandboxir::Value *getValue (const llvm::Value *V) const {
31203114 return getValue (const_cast <llvm::Value *>(V));
31213115 }
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+
31223125 // / Create a sandboxir::Function for an existing LLVM IR \p F, including all
31233126 // / blocks and instructions.
31243127 // / This is the main API function for creating Sandbox IR.
@@ -3165,9 +3168,7 @@ class Function : public Constant {
31653168 LLVMBBToBB BBGetter (Ctx);
31663169 return iterator (cast<llvm::Function>(Val)->end (), BBGetter);
31673170 }
3168- FunctionType *getFunctionType () const {
3169- return cast<llvm::Function>(Val)->getFunctionType ();
3170- }
3171+ FunctionType *getFunctionType () const ;
31713172
31723173#ifndef NDEBUG
31733174 void verify () const final {
0 commit comments