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 "AttributeImpl.h"
19 #include "ConstantsContext.h"
20 #include "LeaksContext.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/Support/ValueHandle.h"
44 struct DenseMapAPIntKeyInfo {
48 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
49 bool operator==(const KeyTy& that) const {
50 return type == that.type && this->val == that.val;
52 bool operator!=(const KeyTy& that) const {
53 return !this->operator==(that);
55 friend hash_code hash_value(const KeyTy &Key) {
56 return hash_combine(Key.type, Key.val);
59 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
60 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
61 static unsigned getHashValue(const KeyTy &Key) {
62 return static_cast<unsigned>(hash_value(Key));
64 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
69 struct DenseMapAPFloatKeyInfo {
72 KeyTy(const APFloat& V) : val(V){}
73 bool operator==(const KeyTy& that) const {
74 return this->val.bitwiseIsEqual(that.val);
76 bool operator!=(const KeyTy& that) const {
77 return !this->operator==(that);
79 friend hash_code hash_value(const KeyTy &Key) {
80 return hash_combine(Key.val);
83 static inline KeyTy getEmptyKey() {
84 return KeyTy(APFloat(APFloat::Bogus,1));
86 static inline KeyTy getTombstoneKey() {
87 return KeyTy(APFloat(APFloat::Bogus,2));
89 static unsigned getHashValue(const KeyTy &Key) {
90 return static_cast<unsigned>(hash_value(Key));
92 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
97 struct AnonStructTypeKeyInfo {
99 ArrayRef<Type*> ETypes;
101 KeyTy(const ArrayRef<Type*>& E, bool P) :
102 ETypes(E), isPacked(P) {}
103 KeyTy(const StructType* ST) :
104 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
105 isPacked(ST->isPacked()) {}
106 bool operator==(const KeyTy& that) const {
107 if (isPacked != that.isPacked)
109 if (ETypes != that.ETypes)
113 bool operator!=(const KeyTy& that) const {
114 return !this->operator==(that);
117 static inline StructType* getEmptyKey() {
118 return DenseMapInfo<StructType*>::getEmptyKey();
120 static inline StructType* getTombstoneKey() {
121 return DenseMapInfo<StructType*>::getTombstoneKey();
123 static unsigned getHashValue(const KeyTy& Key) {
124 return hash_combine(hash_combine_range(Key.ETypes.begin(),
128 static unsigned getHashValue(const StructType *ST) {
129 return getHashValue(KeyTy(ST));
131 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
132 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
134 return LHS == KeyTy(RHS);
136 static bool isEqual(const StructType *LHS, const StructType *RHS) {
141 struct FunctionTypeKeyInfo {
143 const Type *ReturnType;
144 ArrayRef<Type*> Params;
146 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
147 ReturnType(R), Params(P), isVarArg(V) {}
148 KeyTy(const FunctionType* FT) :
149 ReturnType(FT->getReturnType()),
150 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
151 isVarArg(FT->isVarArg()) {}
152 bool operator==(const KeyTy& that) const {
153 if (ReturnType != that.ReturnType)
155 if (isVarArg != that.isVarArg)
157 if (Params != that.Params)
161 bool operator!=(const KeyTy& that) const {
162 return !this->operator==(that);
165 static inline FunctionType* getEmptyKey() {
166 return DenseMapInfo<FunctionType*>::getEmptyKey();
168 static inline FunctionType* getTombstoneKey() {
169 return DenseMapInfo<FunctionType*>::getTombstoneKey();
171 static unsigned getHashValue(const KeyTy& Key) {
172 return hash_combine(Key.ReturnType,
173 hash_combine_range(Key.Params.begin(),
177 static unsigned getHashValue(const FunctionType *FT) {
178 return getHashValue(KeyTy(FT));
180 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
181 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
183 return LHS == KeyTy(RHS);
185 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
190 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a
191 // shortcut to avoid comparing all operands.
192 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> {
193 static bool Equals(const MDNode &X, const FoldingSetNodeID &ID,
194 unsigned IDHash, FoldingSetNodeID &TempID) {
195 assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?");
196 // First, check if the cached hashes match. If they don't we can skip the
197 // expensive operand walk.
198 if (X.Hash != IDHash)
201 // If they match we have to compare the operands.
205 static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) {
206 return X.Hash; // Return cached hash.
210 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
211 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
212 class DebugRecVH : public CallbackVH {
213 /// Ctx - This is the LLVM Context being referenced.
214 LLVMContextImpl *Ctx;
216 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
217 /// this reference lives in. If this is zero, then it represents a
218 /// non-canonical entry that has no DenseMap value. This can happen due to
222 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
223 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
225 MDNode *get() const {
226 return cast_or_null<MDNode>(getValPtr());
229 virtual void deleted();
230 virtual void allUsesReplacedWith(Value *VNew);
233 class LLVMContextImpl {
235 /// OwnedModules - The set of modules instantiated in this context, and which
236 /// will be automatically deleted if this context is deleted.
237 SmallPtrSet<Module*, 4> OwnedModules;
239 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
240 void *InlineAsmDiagContext;
242 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
243 DenseMapAPIntKeyInfo> IntMapTy;
244 IntMapTy IntConstants;
246 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
247 DenseMapAPFloatKeyInfo> FPMapTy;
250 FoldingSet<AttributeImpl> AttrsSet;
251 FoldingSet<AttributeSetImpl> AttrsLists;
252 FoldingSet<AttributeSetNode> AttrsSetNodes;
254 StringMap<Value*> MDStringCache;
256 FoldingSet<MDNode> MDNodeSet;
258 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
259 // aren't in the MDNodeSet, but they're still shared between objects, so no
260 // one object can destroy them. This set allows us to at least destroy them
261 // on Context destruction.
262 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
264 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
266 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
267 ArrayConstantsTy ArrayConstants;
269 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
270 StructConstantsTy StructConstants;
272 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
273 VectorConstantsTy VectorConstants;
275 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
277 DenseMap<Type*, UndefValue*> UVConstants;
279 StringMap<ConstantDataSequential*> CDSConstants;
282 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
283 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
286 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
287 InlineAsm> InlineAsms;
289 ConstantInt *TheTrueVal;
290 ConstantInt *TheFalseVal;
292 LeakDetectorImpl<Value> LLVMObjects;
294 // Basic type instances.
295 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
296 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
297 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
300 /// TypeAllocator - All dynamically allocated types are allocated from this.
301 /// They live forever until the context is torn down.
302 BumpPtrAllocator TypeAllocator;
304 DenseMap<unsigned, IntegerType*> IntegerTypes;
306 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
307 FunctionTypeMap FunctionTypes;
308 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
309 StructTypeMap AnonStructTypes;
310 StringMap<StructType*> NamedStructTypes;
311 unsigned NamedStructTypesUniqueID;
313 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
314 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
315 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
316 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
319 /// ValueHandles - This map keeps track of all of the value handles that are
320 /// watching a Value*. The Value::HasValueHandle bit is used to know
321 /// whether or not a value has an entry in this map.
322 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
323 ValueHandlesTy ValueHandles;
325 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
326 StringMap<unsigned> CustomMDKindNames;
328 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
329 typedef SmallVector<MDPairTy, 2> MDMapTy;
331 /// MetadataStore - Collection of per-instruction metadata used in this
333 DenseMap<const Instruction *, MDMapTy> MetadataStore;
335 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
336 /// entry with no "inlined at" element.
337 DenseMap<MDNode*, int> ScopeRecordIdx;
339 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
340 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
341 /// the MDNode is RAUW'd.
342 std::vector<DebugRecVH> ScopeRecords;
344 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
345 /// scope/inlined-at pair.
346 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
348 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
349 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
351 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
353 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
354 /// requested in this context
355 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
356 IntrinsicIDCacheTy IntrinsicIDCache;
358 /// \brief Mapping from a function to its prefix data, which is stored as the
359 /// operand of an unparented ReturnInst so that the prefix data has a Use.
360 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
361 PrefixDataMapTy PrefixDataMap;
363 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
364 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
366 LLVMContextImpl(LLVMContext &C);