MDNodes that refer to an instruction are local to a function; in that case, explicitl...
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.h
1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class --------------===//
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/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"
31 #include <vector>
32
33 namespace llvm {
34
35 class ConstantInt;
36 class ConstantFP;
37 class MDString;
38 class MDNode;
39 class LLVMContext;
40 class Type;
41 class Value;
42
43 struct DenseMapAPIntKeyInfo {
44   struct KeyTy {
45     APInt val;
46     const Type* type;
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;
51     }
52     bool operator!=(const KeyTy& that) const {
53       return !this->operator==(that);
54     }
55   };
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();
61   }
62   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
63     return LHS == RHS;
64   }
65 };
66
67 struct DenseMapAPFloatKeyInfo {
68   struct KeyTy {
69     APFloat val;
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);
74     }
75     bool operator!=(const KeyTy& that) const {
76       return !this->operator==(that);
77     }
78   };
79   static inline KeyTy getEmptyKey() { 
80     return KeyTy(APFloat(APFloat::Bogus,1));
81   }
82   static inline KeyTy getTombstoneKey() { 
83     return KeyTy(APFloat(APFloat::Bogus,2)); 
84   }
85   static unsigned getHashValue(const KeyTy &Key) {
86     return Key.val.getHashValue();
87   }
88   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
89     return LHS == RHS;
90   }
91 };
92
93 class LLVMContextImpl {
94 public:
95   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
96                          DenseMapAPIntKeyInfo> IntMapTy;
97   IntMapTy IntConstants;
98   
99   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
100                          DenseMapAPFloatKeyInfo> FPMapTy;
101   FPMapTy FPConstants;
102   
103   StringMap<MDString*> MDStringCache;
104   
105   FoldingSet<MDNode> MDNodeSet;
106   
107   ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
108
109   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
110     ConstantArray, true /*largekey*/> ArrayConstantsTy;
111   ArrayConstantsTy ArrayConstants;
112   
113   typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
114     ConstantStruct, true /*largekey*/> StructConstantsTy;
115   StructConstantsTy StructConstants;
116   
117   typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
118                             ConstantVector> VectorConstantsTy;
119   VectorConstantsTy VectorConstants;
120   
121   ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
122   
123   ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
124   
125   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
126   ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
127   
128   ConstantInt *TheTrueVal;
129   ConstantInt *TheFalseVal;
130   
131   LeakDetectorImpl<Value> LLVMObjects;
132   
133   // Basic type instances.
134   const Type VoidTy;
135   const Type LabelTy;
136   const Type FloatTy;
137   const Type DoubleTy;
138   const Type MetadataTy;
139   const Type X86_FP80Ty;
140   const Type FP128Ty;
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;
147
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 
151   // make this easy.
152   TypePrinting ConcreteTypeDescriptions;
153   TypePrinting AbstractTypeDescriptions;
154   
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;
161
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;
167   
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),
178     Int1Ty(C, 1),
179     Int8Ty(C, 8),
180     Int16Ty(C, 16),
181     Int32Ty(C, 32),
182     Int64Ty(C, 64) { }
183
184   ~LLVMContextImpl()
185   {
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(); 
194          I != E; ++I) {
195       if (I->second->use_empty())
196         delete I->second;
197     }
198     for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
199          I != E; ++I) {
200       if (I->second->use_empty())
201         delete I->second;
202     }
203     MDNodeSet.clear();
204   }
205 };
206
207 }
208
209 #endif