1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- 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 "LeaksContext.h"
20 #include "TypesContext.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Metadata.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"
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;
106 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
107 // aren't in the MDNodeSet, but they're still shared between objects, so no
108 // one object can destroy them. This set allows us to at least destroy them
109 // on Context destruction.
110 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
112 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
114 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
115 ConstantArray, true /*largekey*/> ArrayConstantsTy;
116 ArrayConstantsTy ArrayConstants;
118 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
119 ConstantStruct, true /*largekey*/> StructConstantsTy;
120 StructConstantsTy StructConstants;
122 typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
124 UnionConstantsTy UnionConstants;
126 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
127 ConstantVector> VectorConstantsTy;
128 VectorConstantsTy VectorConstants;
130 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
132 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
134 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
135 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
137 ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
139 ConstantInt *TheTrueVal;
140 ConstantInt *TheFalseVal;
142 LeakDetectorImpl<Value> LLVMObjects;
144 // Basic type instances.
149 const Type MetadataTy;
150 const Type X86_FP80Ty;
152 const Type PPC_FP128Ty;
153 const IntegerType Int1Ty;
154 const IntegerType Int8Ty;
155 const IntegerType Int16Ty;
156 const IntegerType Int32Ty;
157 const IntegerType Int64Ty;
159 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
160 // for types as they are needed. Because resolution of types must invalidate
161 // all of the abstract type descriptions, we keep them in a seperate map to
163 TypePrinting ConcreteTypeDescriptions;
164 TypePrinting AbstractTypeDescriptions;
166 TypeMap<ArrayValType, ArrayType> ArrayTypes;
167 TypeMap<VectorValType, VectorType> VectorTypes;
168 TypeMap<PointerValType, PointerType> PointerTypes;
169 TypeMap<FunctionValType, FunctionType> FunctionTypes;
170 TypeMap<StructValType, StructType> StructTypes;
171 TypeMap<UnionValType, UnionType> UnionTypes;
172 TypeMap<IntegerValType, IntegerType> IntegerTypes;
174 // Opaque types are not structurally uniqued, so don't use TypeMap.
175 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
176 OpaqueTypesTy OpaqueTypes;
178 /// Used as an abstract type that will never be resolved.
179 OpaqueType *const AlwaysOpaqueTy;
182 /// ValueHandles - This map keeps track of all of the value handles that are
183 /// watching a Value*. The Value::HasValueHandle bit is used to know
184 // whether or not a value has an entry in this map.
185 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
186 ValueHandlesTy ValueHandles;
188 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
189 StringMap<unsigned> CustomMDKindNames;
191 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
192 typedef SmallVector<MDPairTy, 2> MDMapTy;
194 /// MetadataStore - Collection of per-instruction metadata used in this
196 DenseMap<const Instruction *, MDMapTy> MetadataStore;
198 LLVMContextImpl(LLVMContext &C);