100
100
#include " llvm/IR/User.h"
101
101
#include " llvm/IR/Value.h"
102
102
#include " llvm/SandboxIR/Tracker.h"
103
+ #include " llvm/SandboxIR/Type.h"
103
104
#include " llvm/SandboxIR/Use.h"
104
105
#include " llvm/Support/raw_ostream.h"
105
106
#include < iterator>
@@ -382,7 +383,7 @@ class Value {
382
383
return Cnt == Num;
383
384
}
384
385
385
- Type *getType () const { return Val-> getType (); }
386
+ Type *getType () const ;
386
387
387
388
Context &getContext () const { return Ctx; }
388
389
@@ -570,8 +571,7 @@ class ConstantInt : public Constant {
570
571
public:
571
572
// / If Ty is a vector type, return a Constant with a splat of the given
572
573
// / value. Otherwise return a ConstantInt for the given value.
573
- static ConstantInt *get (Type *Ty, uint64_t V, Context &Ctx,
574
- bool IsSigned = false );
574
+ static ConstantInt *get (Type *Ty, uint64_t V, bool IsSigned = false );
575
575
576
576
// TODO: Implement missing functions.
577
577
@@ -1019,10 +1019,7 @@ class ExtractElementInst final
1019
1019
Value *getIndexOperand () { return getOperand (1 ); }
1020
1020
const Value *getVectorOperand () const { return getOperand (0 ); }
1021
1021
const Value *getIndexOperand () const { return getOperand (1 ); }
1022
-
1023
- VectorType *getVectorOperandType () const {
1024
- return cast<VectorType>(getVectorOperand ()->getType ());
1025
- }
1022
+ VectorType *getVectorOperandType () const ;
1026
1023
};
1027
1024
1028
1025
class ShuffleVectorInst final
@@ -1067,9 +1064,7 @@ class ShuffleVectorInst final
1067
1064
}
1068
1065
1069
1066
// / Overload to return most specific vector type.
1070
- VectorType *getType () const {
1071
- return cast<llvm::ShuffleVectorInst>(Val)->getType ();
1072
- }
1067
+ VectorType *getType () const ;
1073
1068
1074
1069
// / Return the shuffle mask value of this instruction for the given element
1075
1070
// / index. Return PoisonMaskElem if the element is undef.
@@ -1095,7 +1090,7 @@ class ShuffleVectorInst final
1095
1090
Constant *getShuffleMaskForBitcode () const ;
1096
1091
1097
1092
static Constant *convertShuffleMaskForBitcode (ArrayRef<int > Mask,
1098
- Type *ResultTy, Context &Ctx );
1093
+ Type *ResultTy);
1099
1094
1100
1095
void setShuffleMask (ArrayRef<int > Mask);
1101
1096
@@ -1779,9 +1774,7 @@ class CallBase : public SingleLLVMInstructionImpl<llvm::CallBase> {
1779
1774
Opc == Instruction::ClassID::CallBr;
1780
1775
}
1781
1776
1782
- FunctionType *getFunctionType () const {
1783
- return cast<llvm::CallBase>(Val)->getFunctionType ();
1784
- }
1777
+ FunctionType *getFunctionType () const ;
1785
1778
1786
1779
op_iterator data_operands_begin () { return op_begin (); }
1787
1780
const_op_iterator data_operands_begin () const {
@@ -2197,12 +2190,8 @@ class GetElementPtrInst final
2197
2190
return From->getSubclassID () == ClassID::GetElementPtr;
2198
2191
}
2199
2192
2200
- Type *getSourceElementType () const {
2201
- return cast<llvm::GetElementPtrInst>(Val)->getSourceElementType ();
2202
- }
2203
- Type *getResultElementType () const {
2204
- return cast<llvm::GetElementPtrInst>(Val)->getResultElementType ();
2205
- }
2193
+ Type *getSourceElementType () const ;
2194
+ Type *getResultElementType () const ;
2206
2195
unsigned getAddressSpace () const {
2207
2196
return cast<llvm::GetElementPtrInst>(Val)->getAddressSpace ();
2208
2197
}
@@ -2226,9 +2215,7 @@ class GetElementPtrInst final
2226
2215
static unsigned getPointerOperandIndex () {
2227
2216
return llvm::GetElementPtrInst::getPointerOperandIndex ();
2228
2217
}
2229
- Type *getPointerOperandType () const {
2230
- return cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType ();
2231
- }
2218
+ Type *getPointerOperandType () const ;
2232
2219
unsigned getPointerAddressSpace () const {
2233
2220
return cast<llvm::GetElementPtrInst>(Val)->getPointerAddressSpace ();
2234
2221
}
@@ -2775,9 +2762,7 @@ class AllocaInst final : public UnaryInstruction {
2775
2762
return const_cast <AllocaInst *>(this )->getArraySize ();
2776
2763
}
2777
2764
// / Overload to return most specific pointer type.
2778
- PointerType *getType () const {
2779
- return cast<llvm::AllocaInst>(Val)->getType ();
2780
- }
2765
+ PointerType *getType () const ;
2781
2766
// / Return the address space for the allocation.
2782
2767
unsigned getAddressSpace () const {
2783
2768
return cast<llvm::AllocaInst>(Val)->getAddressSpace ();
@@ -2793,9 +2778,7 @@ class AllocaInst final : public UnaryInstruction {
2793
2778
return cast<llvm::AllocaInst>(Val)->getAllocationSizeInBits (DL);
2794
2779
}
2795
2780
// / Return the type that is being allocated by the instruction.
2796
- Type *getAllocatedType () const {
2797
- return cast<llvm::AllocaInst>(Val)->getAllocatedType ();
2798
- }
2781
+ Type *getAllocatedType () const ;
2799
2782
// / for use only in special circumstances that need to generically
2800
2783
// / transform a whole instruction (eg: IR linking and vectorization).
2801
2784
void setAllocatedType (Type *Ty);
@@ -2877,8 +2860,8 @@ class CastInst : public UnaryInstruction {
2877
2860
const Twine &Name = " " );
2878
2861
// / For isa/dyn_cast.
2879
2862
static bool classof (const Value *From);
2880
- Type *getSrcTy () const { return cast<llvm::CastInst>(Val)-> getSrcTy (); }
2881
- Type *getDestTy () const { return cast<llvm::CastInst>(Val)-> getDestTy (); }
2863
+ Type *getSrcTy () const ;
2864
+ Type *getDestTy () const ;
2882
2865
};
2883
2866
2884
2867
// / Instruction that can have a nneg flag (zext/uitofp).
@@ -3058,13 +3041,25 @@ class OpaqueInst : public SingleLLVMInstructionImpl<llvm::Instruction> {
3058
3041
class Context {
3059
3042
protected:
3060
3043
LLVMContext &LLVMCtx;
3044
+ friend class Type ; // For LLVMCtx.
3045
+ friend class PointerType ; // For LLVMCtx.
3061
3046
Tracker IRTracker;
3062
3047
3063
3048
// / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
3064
3049
// / SandboxIR objects.
3065
3050
DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
3066
3051
LLVMValueToValueMap;
3067
3052
3053
+ // / Type has a protected destructor to prohibit the user from managing the
3054
+ // / lifetime of the Type objects. Context is friend of Type, and this custom
3055
+ // / deleter can destroy Type.
3056
+ struct TypeDeleter {
3057
+ void operator ()(Type *Ty) { delete Ty; }
3058
+ };
3059
+ // / Maps LLVM Type to the corresonding sandboxir::Type. Owns all Sandbox IR
3060
+ // / Type objects.
3061
+ DenseMap<llvm::Type *, std::unique_ptr<Type, TypeDeleter>> LLVMTypeToTypeMap;
3062
+
3068
3063
// / Remove \p V from the maps and returns the unique_ptr.
3069
3064
std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
3070
3065
// / Remove \p SBV from all SandboxIR maps and stop owning it. This effectively
@@ -3099,7 +3094,6 @@ class Context {
3099
3094
// / Create a sandboxir::BasicBlock for an existing LLVM IR \p BB. This will
3100
3095
// / also create all contents of the block.
3101
3096
BasicBlock *createBasicBlock (llvm::BasicBlock *BB);
3102
-
3103
3097
friend class BasicBlock ; // For getOrCreateValue().
3104
3098
3105
3099
IRBuilder<ConstantFolder> LLVMIRBuilder;
@@ -3187,6 +3181,15 @@ class Context {
3187
3181
const sandboxir::Value *getValue (const llvm::Value *V) const {
3188
3182
return getValue (const_cast <llvm::Value *>(V));
3189
3183
}
3184
+
3185
+ Type *getType (llvm::Type *LLVMTy) {
3186
+ auto Pair = LLVMTypeToTypeMap.insert ({LLVMTy, nullptr });
3187
+ auto It = Pair.first ;
3188
+ if (Pair.second )
3189
+ It->second = std::unique_ptr<Type, TypeDeleter>(new Type (LLVMTy, *this ));
3190
+ return It->second .get ();
3191
+ }
3192
+
3190
3193
// / Create a sandboxir::Function for an existing LLVM IR \p F, including all
3191
3194
// / blocks and instructions.
3192
3195
// / This is the main API function for creating Sandbox IR.
@@ -3233,9 +3236,7 @@ class Function : public Constant {
3233
3236
LLVMBBToBB BBGetter (Ctx);
3234
3237
return iterator (cast<llvm::Function>(Val)->end (), BBGetter);
3235
3238
}
3236
- FunctionType *getFunctionType () const {
3237
- return cast<llvm::Function>(Val)->getFunctionType ();
3238
- }
3239
+ FunctionType *getFunctionType () const ;
3239
3240
3240
3241
#ifndef NDEBUG
3241
3242
void verify () const final {
0 commit comments