1 //===----------------- LLVMContextImpl.h - Implementation ------*- 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 "TypesContext.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/System/Mutex.h"
24 #include "llvm/System/RWMutex.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/ADT/APFloat.h"
27 #include "llvm/ADT/APInt.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/FoldingSet.h"
30 #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) {
65 static bool isPod() { return false; }
68 struct DenseMapAPFloatKeyInfo {
71 KeyTy(const APFloat& V) : val(V){}
72 KeyTy(const KeyTy& that) : val(that.val) {}
73 bool operator==(const KeyTy& that) const {
74 return this->val.bitwiseIsEqual(that.val);
76 bool operator!=(const KeyTy& that) const {
77 return !this->operator==(that);
80 static inline KeyTy getEmptyKey() {
81 return KeyTy(APFloat(APFloat::Bogus,1));
83 static inline KeyTy getTombstoneKey() {
84 return KeyTy(APFloat(APFloat::Bogus,2));
86 static unsigned getHashValue(const KeyTy &Key) {
87 return Key.val.getHashValue();
89 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
92 static bool isPod() { return false; }
95 class LLVMContextImpl {
97 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 type maps.
138 sys::SmartMutex<true> TypeMapLock;
140 // Recursive lock used for guarding access to AbstractTypeUsers.
141 // NOTE: The true template parameter means this will no-op when we're not in
142 // multithreaded mode.
143 sys::SmartMutex<true> AbstractTypeUsersLock;
145 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
146 // for types as they are needed. Because resolution of types must invalidate
147 // all of the abstract type descriptions, we keep them in a seperate map to
149 TypePrinting ConcreteTypeDescriptions;
150 TypePrinting AbstractTypeDescriptions;
152 TypeMap<ArrayValType, ArrayType> ArrayTypes;
153 TypeMap<VectorValType, VectorType> VectorTypes;
154 TypeMap<PointerValType, PointerType> PointerTypes;
155 TypeMap<FunctionValType, FunctionType> FunctionTypes;
156 TypeMap<StructValType, StructType> StructTypes;
157 TypeMap<IntegerValType, IntegerType> IntegerTypes;
162 const Type *DoubleTy;
163 const Type *MetadataTy;
164 const Type *X86_FP80Ty;
166 const Type *PPC_FP128Ty;
168 const IntegerType *Int1Ty;
169 const IntegerType *Int8Ty;
170 const IntegerType *Int16Ty;
171 const IntegerType *Int32Ty;
172 const IntegerType *Int64Ty;
174 /// ValueHandles - This map keeps track of all of the value handles that are
175 /// watching a Value*. The Value::HasValueHandle bit is used to know
176 // whether or not a value has an entry in this map.
177 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
178 ValueHandlesTy ValueHandles;
180 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
181 VoidTy(new Type(C, Type::VoidTyID)),
182 LabelTy(new Type(C, Type::LabelTyID)),
183 FloatTy(new Type(C, Type::FloatTyID)),
184 DoubleTy(new Type(C, Type::DoubleTyID)),
185 MetadataTy(new Type(C, Type::MetadataTyID)),
186 X86_FP80Ty(new Type(C, Type::X86_FP80TyID)),
187 FP128Ty(new Type(C, Type::FP128TyID)),
188 PPC_FP128Ty(new Type(C, Type::PPC_FP128TyID)),
189 Int1Ty(new IntegerType(C, 1)),
190 Int8Ty(new IntegerType(C, 8)),
191 Int16Ty(new IntegerType(C, 16)),
192 Int32Ty(new IntegerType(C, 32)),
193 Int64Ty(new IntegerType(C, 64)) { }
196 // In principle, we should delete the member types here. However,
197 // this causes destruction order issues with the types in the TypeMaps.
198 // For now, just leak this, which is at least not a regression from the
199 // previous behavior, though still undesirable.