1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class --------------===//
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/Metadata.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/Support/ValueHandle.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/APInt.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"
45 struct DenseMapAPIntKeyInfo {
49 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
50 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
51 bool operator==(const KeyTy& that) const {
52 return type == that.type && this->val == that.val;
54 bool operator!=(const KeyTy& that) const {
55 return !this->operator==(that);
58 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
59 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
60 static unsigned getHashValue(const KeyTy &Key) {
61 return DenseMapInfo<void*>::getHashValue(Key.type) ^
62 Key.val.getHashValue();
64 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
69 struct DenseMapAPFloatKeyInfo {
72 KeyTy(const APFloat& V) : val(V){}
73 KeyTy(const KeyTy& that) : val(that.val) {}
74 bool operator==(const KeyTy& that) const {
75 return this->val.bitwiseIsEqual(that.val);
77 bool operator!=(const KeyTy& that) const {
78 return !this->operator==(that);
81 static inline KeyTy getEmptyKey() {
82 return KeyTy(APFloat(APFloat::Bogus,1));
84 static inline KeyTy getTombstoneKey() {
85 return KeyTy(APFloat(APFloat::Bogus,2));
87 static unsigned getHashValue(const KeyTy &Key) {
88 return Key.val.getHashValue();
90 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
95 class LLVMContextImpl {
97 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
98 DenseMapAPIntKeyInfo> IntMapTy;
99 IntMapTy IntConstants;
101 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
102 DenseMapAPFloatKeyInfo> FPMapTy;
105 StringMap<MDString*> MDStringCache;
107 FoldingSet<MDNode> MDNodeSet;
108 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
109 // aren't in the MDNodeSet, but they're still shared between objects, so no
110 // one object can destroy them. This set allows us to at least destroy them
111 // on Context destruction.
112 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
114 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
116 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
117 ConstantArray, true /*largekey*/> ArrayConstantsTy;
118 ArrayConstantsTy ArrayConstants;
120 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
121 ConstantStruct, true /*largekey*/> StructConstantsTy;
122 StructConstantsTy StructConstants;
124 typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
126 UnionConstantsTy UnionConstants;
128 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
129 ConstantVector> VectorConstantsTy;
130 VectorConstantsTy VectorConstants;
132 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
134 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
136 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
137 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
139 ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
141 ConstantInt *TheTrueVal;
142 ConstantInt *TheFalseVal;
144 LeakDetectorImpl<Value> LLVMObjects;
146 // Basic type instances.
151 const Type MetadataTy;
152 const Type X86_FP80Ty;
154 const Type PPC_FP128Ty;
155 const IntegerType Int1Ty;
156 const IntegerType Int8Ty;
157 const IntegerType Int16Ty;
158 const IntegerType Int32Ty;
159 const IntegerType Int64Ty;
161 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
162 // for types as they are needed. Because resolution of types must invalidate
163 // all of the abstract type descriptions, we keep them in a seperate map to
165 TypePrinting ConcreteTypeDescriptions;
166 TypePrinting AbstractTypeDescriptions;
168 TypeMap<ArrayValType, ArrayType> ArrayTypes;
169 TypeMap<VectorValType, VectorType> VectorTypes;
170 TypeMap<PointerValType, PointerType> PointerTypes;
171 TypeMap<FunctionValType, FunctionType> FunctionTypes;
172 TypeMap<StructValType, StructType> StructTypes;
173 TypeMap<UnionValType, UnionType> UnionTypes;
174 TypeMap<IntegerValType, IntegerType> IntegerTypes;
176 // Opaque types are not structurally uniqued, so don't use TypeMap.
177 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
178 OpaqueTypesTy OpaqueTypes;
180 /// Used as an abstract type that will never be resolved.
181 OpaqueType *const AlwaysOpaqueTy;
184 /// ValueHandles - This map keeps track of all of the value handles that are
185 /// watching a Value*. The Value::HasValueHandle bit is used to know
186 // whether or not a value has an entry in this map.
187 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
188 ValueHandlesTy ValueHandles;
190 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
191 StringMap<unsigned> CustomMDKindNames;
193 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
194 typedef SmallVector<MDPairTy, 2> MDMapTy;
196 /// MetadataStore - Collection of per-instruction metadata used in this
198 DenseMap<const Instruction *, MDMapTy> MetadataStore;
201 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
202 VoidTy(C, Type::VoidTyID),
203 LabelTy(C, Type::LabelTyID),
204 FloatTy(C, Type::FloatTyID),
205 DoubleTy(C, Type::DoubleTyID),
206 MetadataTy(C, Type::MetadataTyID),
207 X86_FP80Ty(C, Type::X86_FP80TyID),
208 FP128Ty(C, Type::FP128TyID),
209 PPC_FP128Ty(C, Type::PPC_FP128TyID),
215 AlwaysOpaqueTy(new OpaqueType(C)) {
216 // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
217 AlwaysOpaqueTy->addRef();
218 OpaqueTypes.insert(AlwaysOpaqueTy);
222 ExprConstants.freeConstants();
223 ArrayConstants.freeConstants();
224 StructConstants.freeConstants();
225 VectorConstants.freeConstants();
226 AggZeroConstants.freeConstants();
227 NullPtrConstants.freeConstants();
228 UndefValueConstants.freeConstants();
229 InlineAsms.freeConstants();
230 for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end();
232 if (I->second->use_empty())
235 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
237 if (I->second->use_empty())
240 AlwaysOpaqueTy->dropRef();
241 for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
243 (*I)->AbstractTypeUsers.clear();
246 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
247 // and the NonUniquedMDNodes sets, so copy the values out first.
248 SmallVector<MDNode*, 8> MDNodes;
249 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
250 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
252 MDNodes.push_back(&*I);
254 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
255 for (SmallVector<MDNode*, 8>::iterator I = MDNodes.begin(),
256 E = MDNodes.end(); I != E; ++I) {
259 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
260 "Destroying all MDNodes didn't empty the Context's sets.");
261 // Destroy MDStrings.
262 for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
263 E = MDStringCache.end(); I != E; ++I) {