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/System/Mutex.h"
26 #include "llvm/System/RWMutex.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/ADT/APFloat.h"
29 #include "llvm/ADT/APInt.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/FoldingSet.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) {
67 static bool isPod() { return false; }
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);
82 static inline KeyTy getEmptyKey() {
83 return KeyTy(APFloat(APFloat::Bogus,1));
85 static inline KeyTy getTombstoneKey() {
86 return KeyTy(APFloat(APFloat::Bogus,2));
88 static unsigned getHashValue(const KeyTy &Key) {
89 return Key.val.getHashValue();
91 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
94 static bool isPod() { return false; }
97 class LLVMContextImpl {
99 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
100 DenseMapAPIntKeyInfo> IntMapTy;
101 IntMapTy IntConstants;
103 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
104 DenseMapAPFloatKeyInfo> FPMapTy;
107 StringMap<MDString*> MDStringCache;
109 FoldingSet<MDNode> MDNodeSet;
111 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
113 typedef ValueMap<std::vector<Constant*>, ArrayType,
114 ConstantArray, true /*largekey*/> ArrayConstantsTy;
115 ArrayConstantsTy ArrayConstants;
117 typedef ValueMap<std::vector<Constant*>, StructType,
118 ConstantStruct, true /*largekey*/> StructConstantsTy;
119 StructConstantsTy StructConstants;
121 typedef ValueMap<std::vector<Constant*>, VectorType,
122 ConstantVector> VectorConstantsTy;
123 VectorConstantsTy VectorConstants;
125 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
127 ValueMap<char, Type, UndefValue> UndefValueConstants;
129 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
131 ConstantInt *TheTrueVal;
132 ConstantInt *TheFalseVal;
134 // Lock used for guarding access to the leak detector
135 sys::SmartMutex<true> LLVMObjectsLock;
136 LeakDetectorImpl<Value> LLVMObjects;
138 // Lock used for guarding access to the type maps.
139 sys::SmartMutex<true> TypeMapLock;
141 // Recursive lock used for guarding access to AbstractTypeUsers.
142 // NOTE: The true template parameter means this will no-op when we're not in
143 // multithreaded mode.
144 sys::SmartMutex<true> AbstractTypeUsersLock;
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<IntegerValType, IntegerType> IntegerTypes;
175 /// ValueHandles - This map keeps track of all of the value handles that are
176 /// watching a Value*. The Value::HasValueHandle bit is used to know
177 // whether or not a value has an entry in this map.
178 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
179 ValueHandlesTy ValueHandles;
181 MetadataContext TheMetadata;
182 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
183 VoidTy(C, Type::VoidTyID),
184 LabelTy(C, Type::LabelTyID),
185 FloatTy(C, Type::FloatTyID),
186 DoubleTy(C, Type::DoubleTyID),
187 MetadataTy(C, Type::MetadataTyID),
188 X86_FP80Ty(C, Type::X86_FP80TyID),
189 FP128Ty(C, Type::FP128TyID),
190 PPC_FP128Ty(C, Type::PPC_FP128TyID),
199 ExprConstants.freeConstants();
200 ArrayConstants.freeConstants();
201 StructConstants.freeConstants();
202 VectorConstants.freeConstants();
203 AggZeroConstants.freeConstants();
204 NullPtrConstants.freeConstants();
205 UndefValueConstants.freeConstants();
206 for (FoldingSet<MDNode>::iterator I = MDNodeSet.begin(),
207 E = MDNodeSet.end(); I != E; ++I)
208 I->dropAllReferences();
209 for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end();
211 if (I->second->use_empty())
214 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
216 if (I->second->use_empty())