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 "ConstantsContext.h"
20 #include "LeaksContext.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Metadata.h"
24 #include "llvm/Support/ValueHandle.h"
25 #include "llvm/ADT/APFloat.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/FoldingSet.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/StringMap.h"
32 #include "llvm/ADT/Hashing.h"
43 struct DenseMapAPIntKeyInfo {
47 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {}
48 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
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);
56 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
57 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
58 static unsigned getHashValue(const KeyTy &Key) {
59 return DenseMapInfo<void*>::getHashValue(Key.type) ^
60 Key.val.getHashValue();
62 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
67 struct DenseMapAPFloatKeyInfo {
70 KeyTy(const APFloat& V) : val(V){}
71 KeyTy(const KeyTy& that) : val(that.val) {}
72 bool operator==(const KeyTy& that) const {
73 return this->val.bitwiseIsEqual(that.val);
75 bool operator!=(const KeyTy& that) const {
76 return !this->operator==(that);
79 static inline KeyTy getEmptyKey() {
80 return KeyTy(APFloat(APFloat::Bogus,1));
82 static inline KeyTy getTombstoneKey() {
83 return KeyTy(APFloat(APFloat::Bogus,2));
85 static unsigned getHashValue(const KeyTy &Key) {
86 return Key.val.getHashValue();
88 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
93 struct AnonStructTypeKeyInfo {
95 ArrayRef<Type*> ETypes;
97 KeyTy(const ArrayRef<Type*>& E, bool P) :
98 ETypes(E), isPacked(P) {}
99 KeyTy(const KeyTy& that) :
100 ETypes(that.ETypes), isPacked(that.isPacked) {}
101 KeyTy(const StructType* ST) :
102 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
103 isPacked(ST->isPacked()) {}
104 bool operator==(const KeyTy& that) const {
105 if (isPacked != that.isPacked)
107 if (ETypes != that.ETypes)
111 bool operator!=(const KeyTy& that) const {
112 return !this->operator==(that);
115 static inline StructType* getEmptyKey() {
116 return DenseMapInfo<StructType*>::getEmptyKey();
118 static inline StructType* getTombstoneKey() {
119 return DenseMapInfo<StructType*>::getTombstoneKey();
121 static unsigned getHashValue(const KeyTy& Key) {
123 Hash.add(Key.ETypes);
124 Hash.add(Key.isPacked);
125 return Hash.finish();
127 static unsigned getHashValue(const StructType *ST) {
128 return getHashValue(KeyTy(ST));
130 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
131 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
133 return LHS == KeyTy(RHS);
135 static bool isEqual(const StructType *LHS, const StructType *RHS) {
140 struct FunctionTypeKeyInfo {
142 const Type *ReturnType;
143 ArrayRef<Type*> Params;
145 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
146 ReturnType(R), Params(P), isVarArg(V) {}
147 KeyTy(const KeyTy& that) :
148 ReturnType(that.ReturnType),
150 isVarArg(that.isVarArg) {}
151 KeyTy(const FunctionType* FT) :
152 ReturnType(FT->getReturnType()),
153 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
154 isVarArg(FT->isVarArg()) {}
155 bool operator==(const KeyTy& that) const {
156 if (ReturnType != that.ReturnType)
158 if (isVarArg != that.isVarArg)
160 if (Params != that.Params)
164 bool operator!=(const KeyTy& that) const {
165 return !this->operator==(that);
168 static inline FunctionType* getEmptyKey() {
169 return DenseMapInfo<FunctionType*>::getEmptyKey();
171 static inline FunctionType* getTombstoneKey() {
172 return DenseMapInfo<FunctionType*>::getTombstoneKey();
174 static unsigned getHashValue(const KeyTy& Key) {
176 Hash.add(Key.ReturnType);
177 Hash.add(Key.Params);
178 Hash.add(Key.isVarArg);
179 return Hash.finish();
181 static unsigned getHashValue(const FunctionType *FT) {
182 return getHashValue(KeyTy(FT));
184 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
185 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
187 return LHS == KeyTy(RHS);
189 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
194 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
195 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
196 class DebugRecVH : public CallbackVH {
197 /// Ctx - This is the LLVM Context being referenced.
198 LLVMContextImpl *Ctx;
200 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
201 /// this reference lives in. If this is zero, then it represents a
202 /// non-canonical entry that has no DenseMap value. This can happen due to
206 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
207 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
209 MDNode *get() const {
210 return cast_or_null<MDNode>(getValPtr());
213 virtual void deleted();
214 virtual void allUsesReplacedWith(Value *VNew);
217 class LLVMContextImpl {
219 /// OwnedModules - The set of modules instantiated in this context, and which
220 /// will be automatically deleted if this context is deleted.
221 SmallPtrSet<Module*, 4> OwnedModules;
223 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
224 void *InlineAsmDiagContext;
226 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
227 DenseMapAPIntKeyInfo> IntMapTy;
228 IntMapTy IntConstants;
230 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
231 DenseMapAPFloatKeyInfo> FPMapTy;
234 StringMap<MDString*> MDStringCache;
236 FoldingSet<MDNode> MDNodeSet;
237 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
238 // aren't in the MDNodeSet, but they're still shared between objects, so no
239 // one object can destroy them. This set allows us to at least destroy them
240 // on Context destruction.
241 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
243 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
245 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
246 ArrayConstantsTy ArrayConstants;
248 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
249 StructConstantsTy StructConstants;
251 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
252 VectorConstantsTy VectorConstants;
254 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
256 DenseMap<Type*, UndefValue*> UVConstants;
258 StringMap<ConstantDataSequential*> CDSConstants;
261 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
262 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
265 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
266 InlineAsm> InlineAsms;
268 ConstantInt *TheTrueVal;
269 ConstantInt *TheFalseVal;
271 LeakDetectorImpl<Value> LLVMObjects;
273 // Basic type instances.
274 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
275 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
276 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
279 /// TypeAllocator - All dynamically allocated types are allocated from this.
280 /// They live forever until the context is torn down.
281 BumpPtrAllocator TypeAllocator;
283 DenseMap<unsigned, IntegerType*> IntegerTypes;
285 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
286 FunctionTypeMap FunctionTypes;
287 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
288 StructTypeMap AnonStructTypes;
289 StringMap<StructType*> NamedStructTypes;
290 unsigned NamedStructTypesUniqueID;
292 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
293 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
294 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
295 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
298 /// ValueHandles - This map keeps track of all of the value handles that are
299 /// watching a Value*. The Value::HasValueHandle bit is used to know
300 // whether or not a value has an entry in this map.
301 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
302 ValueHandlesTy ValueHandles;
304 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
305 StringMap<unsigned> CustomMDKindNames;
307 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
308 typedef SmallVector<MDPairTy, 2> MDMapTy;
310 /// MetadataStore - Collection of per-instruction metadata used in this
312 DenseMap<const Instruction *, MDMapTy> MetadataStore;
314 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
315 /// entry with no "inlined at" element.
316 DenseMap<MDNode*, int> ScopeRecordIdx;
318 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
319 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
320 /// the MDNode is RAUW'd.
321 std::vector<DebugRecVH> ScopeRecords;
323 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
324 /// scope/inlined-at pair.
325 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
327 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
328 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
330 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
332 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
333 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
335 LLVMContextImpl(LLVMContext &C);