Stop leaking MDStrings.
[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/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"
33 #include <vector>
34
35 namespace llvm {
36
37 class ConstantInt;
38 class ConstantFP;
39 class MDString;
40 class MDNode;
41 class LLVMContext;
42 class Type;
43 class Value;
44
45 struct DenseMapAPIntKeyInfo {
46   struct KeyTy {
47     APInt val;
48     const Type* type;
49     KeyTy(const APInt& V, const Type* Ty) : val(V), type(Ty) {}
50     KeyTy(const KeyTy& that) : val(that.val), type(that.type) {}
51     bool operator==(const KeyTy& that) const {
52       return type == that.type && this->val == that.val;
53     }
54     bool operator!=(const KeyTy& that) const {
55       return !this->operator==(that);
56     }
57   };
58   static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); }
59   static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); }
60   static unsigned getHashValue(const KeyTy &Key) {
61     return DenseMapInfo<void*>::getHashValue(Key.type) ^ 
62       Key.val.getHashValue();
63   }
64   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
65     return LHS == RHS;
66   }
67 };
68
69 struct DenseMapAPFloatKeyInfo {
70   struct KeyTy {
71     APFloat val;
72     KeyTy(const APFloat& V) : val(V){}
73     KeyTy(const KeyTy& that) : val(that.val) {}
74     bool operator==(const KeyTy& that) const {
75       return this->val.bitwiseIsEqual(that.val);
76     }
77     bool operator!=(const KeyTy& that) const {
78       return !this->operator==(that);
79     }
80   };
81   static inline KeyTy getEmptyKey() { 
82     return KeyTy(APFloat(APFloat::Bogus,1));
83   }
84   static inline KeyTy getTombstoneKey() { 
85     return KeyTy(APFloat(APFloat::Bogus,2)); 
86   }
87   static unsigned getHashValue(const KeyTy &Key) {
88     return Key.val.getHashValue();
89   }
90   static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
91     return LHS == RHS;
92   }
93 };
94
95 class LLVMContextImpl {
96 public:
97   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
98                          DenseMapAPIntKeyInfo> IntMapTy;
99   IntMapTy IntConstants;
100   
101   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
102                          DenseMapAPFloatKeyInfo> FPMapTy;
103   FPMapTy FPConstants;
104   
105   StringMap<MDString*> MDStringCache;
106   
107   FoldingSet<MDNode> MDNodeSet;
108   
109   ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
110
111   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
112     ConstantArray, true /*largekey*/> ArrayConstantsTy;
113   ArrayConstantsTy ArrayConstants;
114   
115   typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
116     ConstantStruct, true /*largekey*/> StructConstantsTy;
117   StructConstantsTy StructConstants;
118   
119   typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
120       UnionConstantsTy;
121   UnionConstantsTy UnionConstants;
122   
123   typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
124                             ConstantVector> VectorConstantsTy;
125   VectorConstantsTy VectorConstants;
126   
127   ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
128   
129   ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
130   
131   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
132   ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
133   
134   ConstantInt *TheTrueVal;
135   ConstantInt *TheFalseVal;
136   
137   LeakDetectorImpl<Value> LLVMObjects;
138   
139   // Basic type instances.
140   const Type VoidTy;
141   const Type LabelTy;
142   const Type FloatTy;
143   const Type DoubleTy;
144   const Type MetadataTy;
145   const Type X86_FP80Ty;
146   const Type FP128Ty;
147   const Type PPC_FP128Ty;
148   const IntegerType Int1Ty;
149   const IntegerType Int8Ty;
150   const IntegerType Int16Ty;
151   const IntegerType Int32Ty;
152   const IntegerType Int64Ty;
153
154   // Concrete/Abstract TypeDescriptions - We lazily calculate type descriptions
155   // for types as they are needed.  Because resolution of types must invalidate
156   // all of the abstract type descriptions, we keep them in a seperate map to 
157   // make this easy.
158   TypePrinting ConcreteTypeDescriptions;
159   TypePrinting AbstractTypeDescriptions;
160   
161   TypeMap<ArrayValType, ArrayType> ArrayTypes;
162   TypeMap<VectorValType, VectorType> VectorTypes;
163   TypeMap<PointerValType, PointerType> PointerTypes;
164   TypeMap<FunctionValType, FunctionType> FunctionTypes;
165   TypeMap<StructValType, StructType> StructTypes;
166   TypeMap<UnionValType, UnionType> UnionTypes;
167   TypeMap<IntegerValType, IntegerType> IntegerTypes;
168
169   // Opaque types are not structurally uniqued, so don't use TypeMap.
170   typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
171   OpaqueTypesTy OpaqueTypes;
172
173   /// Used as an abstract type that will never be resolved.
174   OpaqueType *const AlwaysOpaqueTy;
175
176
177   /// ValueHandles - This map keeps track of all of the value handles that are
178   /// watching a Value*.  The Value::HasValueHandle bit is used to know
179   // whether or not a value has an entry in this map.
180   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
181   ValueHandlesTy ValueHandles;
182   
183   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
184   StringMap<unsigned> CustomMDKindNames;
185   
186   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
187   typedef SmallVector<MDPairTy, 2> MDMapTy;
188
189   /// MetadataStore - Collection of per-instruction metadata used in this
190   /// context.
191   DenseMap<const Instruction *, MDMapTy> MetadataStore;
192   
193   
194   LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
195     VoidTy(C, Type::VoidTyID),
196     LabelTy(C, Type::LabelTyID),
197     FloatTy(C, Type::FloatTyID),
198     DoubleTy(C, Type::DoubleTyID),
199     MetadataTy(C, Type::MetadataTyID),
200     X86_FP80Ty(C, Type::X86_FP80TyID),
201     FP128Ty(C, Type::FP128TyID),
202     PPC_FP128Ty(C, Type::PPC_FP128TyID),
203     Int1Ty(C, 1),
204     Int8Ty(C, 8),
205     Int16Ty(C, 16),
206     Int32Ty(C, 32),
207     Int64Ty(C, 64),
208     AlwaysOpaqueTy(new OpaqueType(C)) {
209     // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
210     AlwaysOpaqueTy->addRef();
211     OpaqueTypes.insert(AlwaysOpaqueTy);
212   }
213
214   ~LLVMContextImpl() {
215     ExprConstants.freeConstants();
216     ArrayConstants.freeConstants();
217     StructConstants.freeConstants();
218     VectorConstants.freeConstants();
219     AggZeroConstants.freeConstants();
220     NullPtrConstants.freeConstants();
221     UndefValueConstants.freeConstants();
222     for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
223          I != E; ++I) {
224       if (I->second->use_empty())
225         delete I->second;
226     }
227     for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
228          I != E; ++I) {
229       if (I->second->use_empty())
230         delete I->second;
231     }
232     AlwaysOpaqueTy->dropRef();
233     for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
234         I != E; ++I) {
235       (*I)->AbstractTypeUsers.clear();
236       delete *I;
237     }
238     // Destroy MDNode operands first.
239     for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
240          I != E;) {
241       MDNode *N = &(*I);
242       ++I;
243       N->replaceAllOperandsWithNull();
244     }
245     while (!MDNodeSet.empty()) {
246       MDNode *N = &(*MDNodeSet.begin());
247       N->destroy();
248     }
249     // Destroy MDStrings.
250     for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
251            E = MDStringCache.end(); I != E; ++I) {
252       delete I->second;
253     }
254   }
255 };
256
257 }
258
259 #endif