034f3b99979d7624e9900e4cac35087b5a075da6
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.h
1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file declares LLVMContextImpl, the opaque implementation 
11 //  of LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LLVMCONTEXT_IMPL_H
16 #define LLVM_LLVMCONTEXT_IMPL_H
17
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"
32 #include <vector>
33
34 namespace llvm {
35
36 class ConstantInt;
37 class ConstantFP;
38 class MDString;
39 class MDNode;
40 class LLVMContext;
41 class Type;
42 class Value;
43
44 struct DenseMapAPIntKeyInfo {
45   struct KeyTy {
46     APInt val;
47     const Type* type;
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;
52     }
53     bool operator!=(const KeyTy& that) const {
54       return !this->operator==(that);
55     }
56   };
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();
62   }
63   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
64     return LHS == RHS;
65   }
66 };
67
68 struct DenseMapAPFloatKeyInfo {
69   struct KeyTy {
70     APFloat val;
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);
75     }
76     bool operator!=(const KeyTy& that) const {
77       return !this->operator==(that);
78     }
79   };
80   static inline KeyTy getEmptyKey() { 
81     return KeyTy(APFloat(APFloat::Bogus,1));
82   }
83   static inline KeyTy getTombstoneKey() { 
84     return KeyTy(APFloat(APFloat::Bogus,2)); 
85   }
86   static unsigned getHashValue(const KeyTy &Key) {
87     return Key.val.getHashValue();
88   }
89   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
90     return LHS == RHS;
91   }
92 };
93
94 class LLVMContextImpl {
95 public:
96   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
97                          DenseMapAPIntKeyInfo> IntMapTy;
98   IntMapTy IntConstants;
99   
100   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
101                          DenseMapAPFloatKeyInfo> FPMapTy;
102   FPMapTy FPConstants;
103   
104   StringMap<MDString*> MDStringCache;
105   
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;
112   
113   ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
114
115   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
116     ConstantArray, true /*largekey*/> ArrayConstantsTy;
117   ArrayConstantsTy ArrayConstants;
118   
119   typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
120     ConstantStruct, true /*largekey*/> StructConstantsTy;
121   StructConstantsTy StructConstants;
122   
123   typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
124       UnionConstantsTy;
125   UnionConstantsTy UnionConstants;
126   
127   typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
128                             ConstantVector> VectorConstantsTy;
129   VectorConstantsTy VectorConstants;
130   
131   ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
132   
133   ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
134   
135   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
136   ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
137
138   ConstantUniqueMap<InlineAsmKeyType, PointerType, InlineAsm> InlineAsms;
139   
140   ConstantInt *TheTrueVal;
141   ConstantInt *TheFalseVal;
142   
143   LeakDetectorImpl<Value> LLVMObjects;
144   
145   // Basic type instances.
146   const Type VoidTy;
147   const Type LabelTy;
148   const Type FloatTy;
149   const Type DoubleTy;
150   const Type MetadataTy;
151   const Type X86_FP80Ty;
152   const Type FP128Ty;
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;
159
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 
163   // make this easy.
164   TypePrinting ConcreteTypeDescriptions;
165   TypePrinting AbstractTypeDescriptions;
166   
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;
174
175   // Opaque types are not structurally uniqued, so don't use TypeMap.
176   typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
177   OpaqueTypesTy OpaqueTypes;
178
179   /// Used as an abstract type that will never be resolved.
180   OpaqueType *const AlwaysOpaqueTy;
181
182
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;
188   
189   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
190   StringMap<unsigned> CustomMDKindNames;
191   
192   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
193   typedef SmallVector<MDPairTy, 2> MDMapTy;
194
195   /// MetadataStore - Collection of per-instruction metadata used in this
196   /// context.
197   DenseMap<const Instruction *, MDMapTy> MetadataStore;
198   
199   LLVMContextImpl(LLVMContext &C);
200   ~LLVMContextImpl();
201 };
202
203 }
204
205 #endif