1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
18 #include "llvm/LLVMContext.h"
19 #include "AttributesImpl.h"
20 #include "ConstantsContext.h"
21 #include "LeaksContext.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Metadata.h"
25 #include "llvm/Support/ValueHandle.h"
26 #include "llvm/ADT/APFloat.h"
27 #include "llvm/ADT/APInt.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/FoldingSet.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/StringMap.h"
33 #include "llvm/ADT/Hashing.h"
44 struct DenseMapAPIntKeyInfo {
48 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
49 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
50 bool operator==(const KeyTy& that) const {
51 return type == that.type && this->val == that.val;
53 bool operator!=(const KeyTy& that) const {
54 return !this->operator==(that);
56 friend hash_code hash_value(const KeyTy &Key) {
57 return hash_combine(Key.type, Key.val);
60 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
61 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
62 static unsigned getHashValue(const KeyTy &Key) {
63 return static_cast<unsigned>(hash_value(Key));
65 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
70 struct DenseMapAPFloatKeyInfo {
73 KeyTy(const APFloat& V) : val(V){}
74 KeyTy(const KeyTy& that) : val(that.val) {}
75 bool operator==(const KeyTy& that) const {
76 return this->val.bitwiseIsEqual(that.val);
78 bool operator!=(const KeyTy& that) const {
79 return !this->operator==(that);
81 friend hash_code hash_value(const KeyTy &Key) {
82 return hash_combine(Key.val);
85 static inline KeyTy getEmptyKey() {
86 return KeyTy(APFloat(APFloat::Bogus,1));
88 static inline KeyTy getTombstoneKey() {
89 return KeyTy(APFloat(APFloat::Bogus,2));
91 static unsigned getHashValue(const KeyTy &Key) {
92 return static_cast<unsigned>(hash_value(Key));
94 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
99 struct AnonStructTypeKeyInfo {
101 ArrayRef<Type*> ETypes;
103 KeyTy(const ArrayRef<Type*>& E, bool P) :
104 ETypes(E), isPacked(P) {}
105 KeyTy(const KeyTy& that) :
106 ETypes(that.ETypes), isPacked(that.isPacked) {}
107 KeyTy(const StructType* ST) :
108 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
109 isPacked(ST->isPacked()) {}
110 bool operator==(const KeyTy& that) const {
111 if (isPacked != that.isPacked)
113 if (ETypes != that.ETypes)
117 bool operator!=(const KeyTy& that) const {
118 return !this->operator==(that);
121 static inline StructType* getEmptyKey() {
122 return DenseMapInfo<StructType*>::getEmptyKey();
124 static inline StructType* getTombstoneKey() {
125 return DenseMapInfo<StructType*>::getTombstoneKey();
127 static unsigned getHashValue(const KeyTy& Key) {
128 return hash_combine(hash_combine_range(Key.ETypes.begin(),
132 static unsigned getHashValue(const StructType *ST) {
133 return getHashValue(KeyTy(ST));
135 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
136 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
138 return LHS == KeyTy(RHS);
140 static bool isEqual(const StructType *LHS, const StructType *RHS) {
145 struct FunctionTypeKeyInfo {
147 const Type *ReturnType;
148 ArrayRef<Type*> Params;
150 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
151 ReturnType(R), Params(P), isVarArg(V) {}
152 KeyTy(const KeyTy& that) :
153 ReturnType(that.ReturnType),
155 isVarArg(that.isVarArg) {}
156 KeyTy(const FunctionType* FT) :
157 ReturnType(FT->getReturnType()),
158 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
159 isVarArg(FT->isVarArg()) {}
160 bool operator==(const KeyTy& that) const {
161 if (ReturnType != that.ReturnType)
163 if (isVarArg != that.isVarArg)
165 if (Params != that.Params)
169 bool operator!=(const KeyTy& that) const {
170 return !this->operator==(that);
173 static inline FunctionType* getEmptyKey() {
174 return DenseMapInfo<FunctionType*>::getEmptyKey();
176 static inline FunctionType* getTombstoneKey() {
177 return DenseMapInfo<FunctionType*>::getTombstoneKey();
179 static unsigned getHashValue(const KeyTy& Key) {
180 return hash_combine(Key.ReturnType,
181 hash_combine_range(Key.Params.begin(),
185 static unsigned getHashValue(const FunctionType *FT) {
186 return getHashValue(KeyTy(FT));
188 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
189 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
191 return LHS == KeyTy(RHS);
193 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
198 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
199 // shortcut to avoid comparing all operands.
200 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
201 static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
202 unsigned IDHash, FoldingSetNodeID &TempID) {
203 assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
204 // First, check if the cached hashes match. If they don't we can skip the
205 // expensive operand walk.
206 if (X.Hash != IDHash)
209 // If they match we have to compare the operands.
213 static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
214 return X.Hash; // Return cached hash.
218 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
219 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
220 class DebugRecVH : public CallbackVH {
221 /// Ctx - This is the LLVM Context being referenced.
222 LLVMContextImpl *Ctx;
224 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
225 /// this reference lives in. If this is zero, then it represents a
226 /// non-canonical entry that has no DenseMap value. This can happen due to
230 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
231 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
233 MDNode *get() const {
234 return cast_or_null<MDNode>(getValPtr());
237 virtual void deleted();
238 virtual void allUsesReplacedWith(Value *VNew);
241 class LLVMContextImpl {
243 /// OwnedModules - The set of modules instantiated in this context, and which
244 /// will be automatically deleted if this context is deleted.
245 SmallPtrSet<Module*, 4> OwnedModules;
247 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
248 void *InlineAsmDiagContext;
250 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
251 DenseMapAPIntKeyInfo> IntMapTy;
252 IntMapTy IntConstants;
254 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
255 DenseMapAPFloatKeyInfo> FPMapTy;
258 FoldingSet<AttributesImpl> AttrsSet;
260 StringMap<Value*> MDStringCache;
262 FoldingSet<MDNode> MDNodeSet;
264 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
265 // aren't in the MDNodeSet, but they're still shared between objects, so no
266 // one object can destroy them. This set allows us to at least destroy them
267 // on Context destruction.
268 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
270 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
272 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
273 ArrayConstantsTy ArrayConstants;
275 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
276 StructConstantsTy StructConstants;
278 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
279 VectorConstantsTy VectorConstants;
281 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
283 DenseMap<Type*, UndefValue*> UVConstants;
285 StringMap<ConstantDataSequential*> CDSConstants;
288 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
289 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
292 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
293 InlineAsm> InlineAsms;
295 ConstantInt *TheTrueVal;
296 ConstantInt *TheFalseVal;
298 LeakDetectorImpl<Value> LLVMObjects;
300 // Basic type instances.
301 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
302 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
303 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
306 /// TypeAllocator - All dynamically allocated types are allocated from this.
307 /// They live forever until the context is torn down.
308 BumpPtrAllocator TypeAllocator;
310 DenseMap<unsigned, IntegerType*> IntegerTypes;
312 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
313 FunctionTypeMap FunctionTypes;
314 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
315 StructTypeMap AnonStructTypes;
316 StringMap<StructType*> NamedStructTypes;
317 unsigned NamedStructTypesUniqueID;
319 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
320 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
321 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
322 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
325 /// ValueHandles - This map keeps track of all of the value handles that are
326 /// watching a Value*. The Value::HasValueHandle bit is used to know
327 // whether or not a value has an entry in this map.
328 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
329 ValueHandlesTy ValueHandles;
331 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
332 StringMap<unsigned> CustomMDKindNames;
334 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
335 typedef SmallVector<MDPairTy, 2> MDMapTy;
337 /// MetadataStore - Collection of per-instruction metadata used in this
339 DenseMap<const Instruction *, MDMapTy> MetadataStore;
341 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
342 /// entry with no "inlined at" element.
343 DenseMap<MDNode*, int> ScopeRecordIdx;
345 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
346 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
347 /// the MDNode is RAUW'd.
348 std::vector<DebugRecVH> ScopeRecords;
350 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
351 /// scope/inlined-at pair.
352 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
354 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
355 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
357 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
359 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
360 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
362 LLVMContextImpl(LLVMContext &C);