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/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) {
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 class LLVMContextImpl {
95 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
96 DenseMapAPIntKeyInfo> IntMapTy;
97 IntMapTy IntConstants;
99 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
100 DenseMapAPFloatKeyInfo> FPMapTy;
103 StringMap<MDString*> MDStringCache;
105 FoldingSet<MDNode> MDNodeSet;
107 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
109 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
110 ConstantArray, true /*largekey*/> ArrayConstantsTy;
111 ArrayConstantsTy ArrayConstants;
113 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
114 ConstantStruct, true /*largekey*/> StructConstantsTy;
115 StructConstantsTy StructConstants;
117 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
118 ConstantVector> VectorConstantsTy;
119 VectorConstantsTy VectorConstants;
121 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
123 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
125 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
126 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
128 ConstantInt *TheTrueVal;
129 ConstantInt *TheFalseVal;
131 LeakDetectorImpl<Value> LLVMObjects;
133 // Basic type instances.
138 const Type MetadataTy;
139 const Type X86_FP80Ty;
141 const Type PPC_FP128Ty;
142 const IntegerType Int1Ty;
143 const IntegerType Int8Ty;
144 const IntegerType Int16Ty;
145 const IntegerType Int32Ty;
146 const IntegerType Int64Ty;
148 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
149 // for types as they are needed. Because resolution of types must invalidate
150 // all of the abstract type descriptions, we keep them in a seperate map to
152 TypePrinting ConcreteTypeDescriptions;
153 TypePrinting AbstractTypeDescriptions;
155 TypeMap<ArrayValType, ArrayType> ArrayTypes;
156 TypeMap<VectorValType, VectorType> VectorTypes;
157 TypeMap<PointerValType, PointerType> PointerTypes;
158 TypeMap<FunctionValType, FunctionType> FunctionTypes;
159 TypeMap<StructValType, StructType> StructTypes;
160 TypeMap<IntegerValType, IntegerType> IntegerTypes;
162 /// ValueHandles - This map keeps track of all of the value handles that are
163 /// watching a Value*. The Value::HasValueHandle bit is used to know
164 // whether or not a value has an entry in this map.
165 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
166 ValueHandlesTy ValueHandles;
168 MetadataContext TheMetadata;
169 LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
170 VoidTy(C, Type::VoidTyID),
171 LabelTy(C, Type::LabelTyID),
172 FloatTy(C, Type::FloatTyID),
173 DoubleTy(C, Type::DoubleTyID),
174 MetadataTy(C, Type::MetadataTyID),
175 X86_FP80Ty(C, Type::X86_FP80TyID),
176 FP128Ty(C, Type::FP128TyID),
177 PPC_FP128Ty(C, Type::PPC_FP128TyID),
186 ExprConstants.freeConstants();
187 ArrayConstants.freeConstants();
188 StructConstants.freeConstants();
189 VectorConstants.freeConstants();
190 AggZeroConstants.freeConstants();
191 NullPtrConstants.freeConstants();
192 UndefValueConstants.freeConstants();
193 for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end();
195 if (I->second->use_empty())
198 for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end();
200 if (I->second->use_empty())