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/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/System/Mutex.h"
25 #include "llvm/System/RWMutex.h"
26 #include "llvm/Assembly/Writer.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/StringMap.h"
44 struct DenseMapAPIntKeyInfo {
48 KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
49 KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
50 bool operator==(const KeyTy& that) const {
51 return type == that.type && this->val == that.val;
53 bool operator!=(const KeyTy& that) const {
54 return !this->operator==(that);
57 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
58 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
59 static unsigned getHashValue(const KeyTy &Key) {
60 return DenseMapInfo<void*>::getHashValue(Key.type) ^
61 Key.val.getHashValue();
63 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
66 static bool isPod() { return false; }
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) {
93 static bool isPod() { return false; }
96 class LLVMContextImpl {
98 sys::SmartRWMutex<true> ConstantsLock;
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 ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
111 typedef ValueMap<std::vector<Value*>, Type, MDNode, true /*largekey*/>
116 typedef ValueMap<std::vector<Constant*>, ArrayType,
117 ConstantArray, true /*largekey*/> ArrayConstantsTy;
118 ArrayConstantsTy ArrayConstants;
120 typedef ValueMap<std::vector<Constant*>, StructType,
121 ConstantStruct, true /*largekey*/> StructConstantsTy;
122 StructConstantsTy StructConstants;
124 typedef ValueMap<std::vector<Constant*>, VectorType,
125 ConstantVector> VectorConstantsTy;
126 VectorConstantsTy VectorConstants;
128 ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
130 ValueMap<char, Type, UndefValue> UndefValueConstants;
132 ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
134 ConstantInt *TheTrueVal;
135 ConstantInt *TheFalseVal;
137 // Lock used for guarding access to the leak detector
138 sys::SmartMutex<true> LLVMObjectsLock;
139 LeakDetectorImpl<Value> LLVMObjects;
141 // Lock used for guarding access to the type maps.
142 sys::SmartMutex<true> TypeMapLock;
144 // Recursive lock used for guarding access to AbstractTypeUsers.
145 // NOTE: The true template parameter means this will no-op when we're not in
146 // multithreaded mode.
147 sys::SmartMutex<true> AbstractTypeUsersLock;
149 // Basic type instances.
154 const Type MetadataTy;
155 const Type X86_FP80Ty;
157 const Type PPC_FP128Ty;
158 const IntegerType Int1Ty;
159 const IntegerType Int8Ty;
160 const IntegerType Int16Ty;
161 const IntegerType Int32Ty;
162 const IntegerType Int64Ty;
164 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
165 // for types as they are needed. Because resolution of types must invalidate
166 // all of the abstract type descriptions, we keep them in a seperate map to
168 TypePrinting ConcreteTypeDescriptions;
169 TypePrinting AbstractTypeDescriptions;
171 TypeMap<ArrayValType, ArrayType> ArrayTypes;
172 TypeMap<VectorValType, VectorType> VectorTypes;
173 TypeMap<PointerValType, PointerType> PointerTypes;
174 TypeMap<FunctionValType, FunctionType> FunctionTypes;
175 TypeMap<StructValType, StructType> StructTypes;
176 TypeMap<IntegerValType, IntegerType> IntegerTypes;
178 /// ValueHandles - This map keeps track of all of the value handles that are
179 /// watching a Value*. The Value::HasValueHandle bit is used to know
180 // whether or not a value has an entry in this map.
181 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
182 ValueHandlesTy ValueHandles;
184 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
185 VoidTy(C, Type::VoidTyID),
186 LabelTy(C, Type::LabelTyID),
187 FloatTy(C, Type::FloatTyID),
188 DoubleTy(C, Type::DoubleTyID),
189 MetadataTy(C, Type::MetadataTyID),
190 X86_FP80Ty(C, Type::X86_FP80TyID),
191 FP128Ty(C, Type::FP128TyID),
192 PPC_FP128Ty(C, Type::PPC_FP128TyID),
201 ExprConstants.freeConstants();
202 ArrayConstants.freeConstants();
203 StructConstants.freeConstants();
204 VectorConstants.freeConstants();
206 AggZeroConstants.freeConstants();
207 NullPtrConstants.freeConstants();
208 UndefValueConstants.freeConstants();
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())