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 "ConstantsContext.h"
19 #include "LeaksContext.h"
20 #include "TypesContext.h"
21 #include "llvm/LLVMContext.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"
43 struct DenseMapAPIntKeyInfo {
47 KeyTy(const APInt& V, const 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 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
94 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
95 class DebugRecVH : public CallbackVH {
96 /// Ctx - This is the LLVM Context being referenced.
99 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
100 /// this reference lives in. If this is zero, then it represents a
101 /// non-canonical entry that has no DenseMap value. This can happen due to
105 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
106 : CallbackVH(n), Ctx(ctx), Idx(idx) {}
108 MDNode *get() const {
109 return cast_or_null<MDNode>(getValPtr());
112 virtual void deleted();
113 virtual void allUsesReplacedWith(Value *VNew);
116 class LLVMContextImpl {
118 /// OwnedModules - The set of modules instantiated in this context, and which
119 /// will be automatically deleted if this context is deleted.
120 SmallPtrSet<Module*, 4> OwnedModules;
122 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
123 void *InlineAsmDiagContext;
125 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
126 DenseMapAPIntKeyInfo> IntMapTy;
127 IntMapTy IntConstants;
129 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
130 DenseMapAPFloatKeyInfo> FPMapTy;
133 StringMap<MDString*> MDStringCache;
135 FoldingSet<MDNode> MDNodeSet;
136 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
137 // aren't in the MDNodeSet, but they're still shared between objects, so no
138 // one object can destroy them. This set allows us to at least destroy them
139 // on Context destruction.
140 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
142 ConstantUniqueMap<char, char, Type, ConstantAggregateZero> AggZeroConstants;
144 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
145 ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy;
146 ArrayConstantsTy ArrayConstants;
148 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
149 StructType, ConstantStruct, true /*largekey*/> StructConstantsTy;
150 StructConstantsTy StructConstants;
152 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
153 VectorType, ConstantVector> VectorConstantsTy;
154 VectorConstantsTy VectorConstants;
156 ConstantUniqueMap<char, char, PointerType, ConstantPointerNull>
158 ConstantUniqueMap<char, char, Type, UndefValue> UndefValueConstants;
160 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
161 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
164 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
165 InlineAsm> InlineAsms;
167 ConstantInt *TheTrueVal;
168 ConstantInt *TheFalseVal;
170 LeakDetectorImpl<Value> LLVMObjects;
172 // Basic type instances.
177 const Type MetadataTy;
178 const Type X86_FP80Ty;
180 const Type PPC_FP128Ty;
181 const Type X86_MMXTy;
182 const IntegerType Int1Ty;
183 const IntegerType Int8Ty;
184 const IntegerType Int16Ty;
185 const IntegerType Int32Ty;
186 const IntegerType Int64Ty;
188 TypeMap<ArrayValType, ArrayType> ArrayTypes;
189 TypeMap<VectorValType, VectorType> VectorTypes;
190 TypeMap<PointerValType, PointerType> PointerTypes;
191 TypeMap<FunctionValType, FunctionType> FunctionTypes;
192 TypeMap<StructValType, StructType> StructTypes;
193 TypeMap<IntegerValType, IntegerType> IntegerTypes;
195 // Opaque types are not structurally uniqued, so don't use TypeMap.
196 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
197 OpaqueTypesTy OpaqueTypes;
199 /// Used as an abstract type that will never be resolved.
200 OpaqueType *const AlwaysOpaqueTy;
203 /// ValueHandles - This map keeps track of all of the value handles that are
204 /// watching a Value*. The Value::HasValueHandle bit is used to know
205 // whether or not a value has an entry in this map.
206 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
207 ValueHandlesTy ValueHandles;
209 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
210 StringMap<unsigned> CustomMDKindNames;
212 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
213 typedef SmallVector<MDPairTy, 2> MDMapTy;
215 /// MetadataStore - Collection of per-instruction metadata used in this
217 DenseMap<const Instruction *, MDMapTy> MetadataStore;
219 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
220 /// entry with no "inlined at" element.
221 DenseMap<MDNode*, int> ScopeRecordIdx;
223 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
224 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
225 /// the MDNode is RAUW'd.
226 std::vector<DebugRecVH> ScopeRecords;
228 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
229 /// scope/inlined-at pair.
230 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
232 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
233 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
235 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
237 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
238 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
240 LLVMContextImpl(LLVMContext &C);