Free MDNodes when the LLVMContext is destroyed. Leak found by Valgrind.
[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   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
109   // aren't in the MDNodeSet, but they're still shared between objects, so no
110   // one object can destroy them.  This set allows us to at least destroy them
111   // on Context destruction.
112   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
113   
114   ConstantUniqueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
115
116   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayType,
117     ConstantArray, true /*largekey*/> ArrayConstantsTy;
118   ArrayConstantsTy ArrayConstants;
119   
120   typedef ConstantUniqueMap<std::vector<Constant*>, StructType,
121     ConstantStruct, true /*largekey*/> StructConstantsTy;
122   StructConstantsTy StructConstants;
123   
124   typedef ConstantUniqueMap<Constant*, UnionType, ConstantUnion>
125       UnionConstantsTy;
126   UnionConstantsTy UnionConstants;
127   
128   typedef ConstantUniqueMap<std::vector<Constant*>, VectorType,
129                             ConstantVector> VectorConstantsTy;
130   VectorConstantsTy VectorConstants;
131   
132   ConstantUniqueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
133   
134   ConstantUniqueMap<char, Type, UndefValue> UndefValueConstants;
135   
136   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
137   ConstantUniqueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
138   
139   ConstantInt *TheTrueVal;
140   ConstantInt *TheFalseVal;
141   
142   LeakDetectorImpl<Value> LLVMObjects;
143   
144   // Basic type instances.
145   const Type VoidTy;
146   const Type LabelTy;
147   const Type FloatTy;
148   const Type DoubleTy;
149   const Type MetadataTy;
150   const Type X86_FP80Ty;
151   const Type FP128Ty;
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;
158
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 
162   // make this easy.
163   TypePrinting ConcreteTypeDescriptions;
164   TypePrinting AbstractTypeDescriptions;
165   
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;
173
174   // Opaque types are not structurally uniqued, so don't use TypeMap.
175   typedef SmallPtrSet<const OpaqueType*, 8> OpaqueTypesTy;
176   OpaqueTypesTy OpaqueTypes;
177
178   /// Used as an abstract type that will never be resolved.
179   OpaqueType *const AlwaysOpaqueTy;
180
181
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;
187   
188   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
189   StringMap<unsigned> CustomMDKindNames;
190   
191   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
192   typedef SmallVector<MDPairTy, 2> MDMapTy;
193
194   /// MetadataStore - Collection of per-instruction metadata used in this
195   /// context.
196   DenseMap<const Instruction *, MDMapTy> MetadataStore;
197   
198   
199   LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
200     VoidTy(C, Type::VoidTyID),
201     LabelTy(C, Type::LabelTyID),
202     FloatTy(C, Type::FloatTyID),
203     DoubleTy(C, Type::DoubleTyID),
204     MetadataTy(C, Type::MetadataTyID),
205     X86_FP80Ty(C, Type::X86_FP80TyID),
206     FP128Ty(C, Type::FP128TyID),
207     PPC_FP128Ty(C, Type::PPC_FP128TyID),
208     Int1Ty(C, 1),
209     Int8Ty(C, 8),
210     Int16Ty(C, 16),
211     Int32Ty(C, 32),
212     Int64Ty(C, 64),
213     AlwaysOpaqueTy(new OpaqueType(C)) {
214     // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
215     AlwaysOpaqueTy->addRef();
216     OpaqueTypes.insert(AlwaysOpaqueTy);
217   }
218
219   ~LLVMContextImpl() {
220     ExprConstants.freeConstants();
221     ArrayConstants.freeConstants();
222     StructConstants.freeConstants();
223     VectorConstants.freeConstants();
224     AggZeroConstants.freeConstants();
225     NullPtrConstants.freeConstants();
226     UndefValueConstants.freeConstants();
227     for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
228          I != E; ++I) {
229       if (I->second->use_empty())
230         delete I->second;
231     }
232     for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
233          I != E; ++I) {
234       if (I->second->use_empty())
235         delete I->second;
236     }
237     AlwaysOpaqueTy->dropRef();
238     for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
239         I != E; ++I) {
240       (*I)->AbstractTypeUsers.clear();
241       delete *I;
242     }
243     // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
244     // and the NonUniquedMDNodes sets, so copy the values out first.
245     SmallVector<MDNode*, 8> MDNodes;
246     MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
247     for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
248          I != E; ++I) {
249       MDNodes.push_back(&*I);
250     }
251     MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
252     for (SmallVector<MDNode*, 8>::iterator I = MDNodes.begin(),
253            E = MDNodes.end(); I != E; ++I) {
254       (*I)->destroy();
255     }
256     assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
257            "Destroying all MDNodes didn't empty the Context's sets.");
258     // Destroy MDStrings.
259     for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
260            E = MDStringCache.end(); I != E; ++I) {
261       delete I->second;
262     }
263   }
264 };
265
266 }
267
268 #endif