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/Assembly/Writer.h"
25 #include "llvm/Support/ValueHandle.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/SmallPtrSet.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) {
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) {
94 class LLVMContextImpl {
96 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*,
97 DenseMapAPIntKeyInfo> IntMapTy;
98 IntMapTy IntConstants;
100 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*,
101 DenseMapAPFloatKeyInfo> FPMapTy;
104 StringMap<MDString*> MDStringCache;
106 FoldingSet<MDNode> MDNodeSet;
107 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
108 // aren't in the MDNodeSet, but they're still shared between objects, so no
109 // one object can destroy them. This set allows us to at least destroy them
110 // on Context destruction.
111 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
113 ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
115 typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
116 ConstantArray, true /*largekey*/> ArrayConstantsTy;
117 ArrayConstantsTy ArrayConstants;
119 typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
120 ConstantStruct, true /*largekey*/> StructConstantsTy;
121 StructConstantsTy StructConstants;
123 typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
125 UnionConstantsTy UnionConstants;
127 typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
128 ConstantVector> VectorConstantsTy;
129 VectorConstantsTy VectorConstants;
131 ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
133 ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
135 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
136 ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
138 ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
140 ConstantInt *TheTrueVal;
141 ConstantInt *TheFalseVal;
143 LeakDetectorImpl<Value> LLVMObjects;
145 // Basic type instances.
150 const Type MetadataTy;
151 const Type X86_FP80Ty;
153 const Type PPC_FP128Ty;
154 const IntegerType Int1Ty;
155 const IntegerType Int8Ty;
156 const IntegerType Int16Ty;
157 const IntegerType Int32Ty;
158 const IntegerType Int64Ty;
160 // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
161 // for types as they are needed. Because resolution of types must invalidate
162 // all of the abstract type descriptions, we keep them in a seperate map to
164 TypePrinting ConcreteTypeDescriptions;
165 TypePrinting AbstractTypeDescriptions;
167 TypeMap<ArrayValType, ArrayType> ArrayTypes;
168 TypeMap<VectorValType, VectorType> VectorTypes;
169 TypeMap<PointerValType, PointerType> PointerTypes;
170 TypeMap<FunctionValType, FunctionType> FunctionTypes;
171 TypeMap<StructValType, StructType> StructTypes;
172 TypeMap<UnionValType, UnionType> UnionTypes;
173 TypeMap<IntegerValType, IntegerType> IntegerTypes;
175 // Opaque types are not structurally uniqued, so don't use TypeMap.
176 typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
177 OpaqueTypesTy OpaqueTypes;
179 /// Used as an abstract type that will never be resolved.
180 OpaqueType *const AlwaysOpaqueTy;
183 /// ValueHandles - This map keeps track of all of the value handles that are
184 /// watching a Value*. The Value::HasValueHandle bit is used to know
185 // whether or not a value has an entry in this map.
186 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
187 ValueHandlesTy ValueHandles;
189 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
190 StringMap<unsigned> CustomMDKindNames;
192 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
193 typedef SmallVector<MDPairTy, 2> MDMapTy;
195 /// MetadataStore - Collection of per-instruction metadata used in this
197 DenseMap<const Instruction *, MDMapTy> MetadataStore;
199 LLVMContextImpl(LLVMContext &C);