PR1210: make uniquing of struct and function types more efficient by
[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 "llvm/LLVMContext.h"
19 #include "ConstantsContext.h"
20 #include "LeaksContext.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Metadata.h"
24 #include "llvm/Support/ValueHandle.h"
25 #include "llvm/ADT/APFloat.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/ArrayRef.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 "llvm/ADT/Hashing.h"
33 #include <vector>
34
35 namespace llvm {
36
37 class ConstantInt;
38 class ConstantFP;
39 class LLVMContext;
40 class Type;
41 class Value;
42
43 struct DenseMapAPIntKeyInfo {
44   struct KeyTy {
45     APInt val;
46     Type* type;
47     KeyTy(const APInt& V, 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 struct AnonStructTypeKeyInfo {
94   struct KeyTy {
95     ArrayRef<Type*> ETypes;
96     bool isPacked;
97     KeyTy(const ArrayRef<Type*>& E, bool P) :
98       ETypes(E), isPacked(P) {}
99     KeyTy(const KeyTy& that) :
100       ETypes(that.ETypes), isPacked(that.isPacked) {}
101     KeyTy(const StructType* ST) :
102       ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())),
103       isPacked(ST->isPacked()) {}
104     bool operator==(const KeyTy& that) const {
105       if (isPacked != that.isPacked)
106         return false;
107       if (ETypes != that.ETypes)
108         return false;
109       return true;
110     }
111     bool operator!=(const KeyTy& that) const {
112       return !this->operator==(that);
113     }
114   };
115   static inline StructType* getEmptyKey() {
116     return DenseMapInfo<StructType*>::getEmptyKey();
117   }
118   static inline StructType* getTombstoneKey() {
119     return DenseMapInfo<StructType*>::getTombstoneKey();
120   }
121   static unsigned getHashValue(const KeyTy& Key) {
122     GeneralHash Hash;
123     Hash.add(Key.ETypes);
124     Hash.add(Key.isPacked);
125     return Hash.finish();
126   }
127   static unsigned getHashValue(const StructType *ST) {
128     return getHashValue(KeyTy(ST));
129   }
130   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
131     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
132       return false;
133     return LHS == KeyTy(RHS);
134   }
135   static bool isEqual(const StructType *LHS, const StructType *RHS) {
136     return LHS == RHS;
137   }
138 };
139
140 struct FunctionTypeKeyInfo {
141   struct KeyTy {
142     const Type *ReturnType;
143     ArrayRef<Type*> Params;
144     bool isVarArg;
145     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
146       ReturnType(R), Params(P), isVarArg(V) {}
147     KeyTy(const KeyTy& that) :
148       ReturnType(that.ReturnType),
149       Params(that.Params),
150       isVarArg(that.isVarArg) {}
151     KeyTy(const FunctionType* FT) :
152       ReturnType(FT->getReturnType()),
153       Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())),
154       isVarArg(FT->isVarArg()) {}
155     bool operator==(const KeyTy& that) const {
156       if (ReturnType != that.ReturnType)
157         return false;
158       if (isVarArg != that.isVarArg)
159         return false;
160       if (Params != that.Params)
161         return false;
162       return true;
163     }
164     bool operator!=(const KeyTy& that) const {
165       return !this->operator==(that);
166     }
167   };
168   static inline FunctionType* getEmptyKey() {
169     return DenseMapInfo<FunctionType*>::getEmptyKey();
170   }
171   static inline FunctionType* getTombstoneKey() {
172     return DenseMapInfo<FunctionType*>::getTombstoneKey();
173   }
174   static unsigned getHashValue(const KeyTy& Key) {
175     GeneralHash Hash;
176     Hash.add(Key.ReturnType);
177     Hash.add(Key.Params);
178     Hash.add(Key.isVarArg);
179     return Hash.finish();
180   }
181   static unsigned getHashValue(const FunctionType *FT) {
182     return getHashValue(KeyTy(FT));
183   }
184   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
185     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
186       return false;
187     return LHS == KeyTy(RHS);
188   }
189   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
190     return LHS == RHS;
191   }
192 };
193
194 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
195 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
196 class DebugRecVH : public CallbackVH {
197   /// Ctx - This is the LLVM Context being referenced.
198   LLVMContextImpl *Ctx;
199   
200   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
201   /// this reference lives in.  If this is zero, then it represents a
202   /// non-canonical entry that has no DenseMap value.  This can happen due to
203   /// RAUW.
204   int Idx;
205 public:
206   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
207     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
208   
209   MDNode *get() const {
210     return cast_or_null<MDNode>(getValPtr());
211   }
212   
213   virtual void deleted();
214   virtual void allUsesReplacedWith(Value *VNew);
215 };
216   
217 class LLVMContextImpl {
218 public:
219   /// OwnedModules - The set of modules instantiated in this context, and which
220   /// will be automatically deleted if this context is deleted.
221   SmallPtrSet<Module*, 4> OwnedModules;
222   
223   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
224   void *InlineAsmDiagContext;
225   
226   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
227                          DenseMapAPIntKeyInfo> IntMapTy;
228   IntMapTy IntConstants;
229   
230   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
231                          DenseMapAPFloatKeyInfo> FPMapTy;
232   FPMapTy FPConstants;
233   
234   StringMap<MDString*> MDStringCache;
235   
236   FoldingSet<MDNode> MDNodeSet;
237   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
238   // aren't in the MDNodeSet, but they're still shared between objects, so no
239   // one object can destroy them.  This set allows us to at least destroy them
240   // on Context destruction.
241   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
242   
243   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
244
245   typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy;
246   ArrayConstantsTy ArrayConstants;
247   
248   typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy;
249   StructConstantsTy StructConstants;
250   
251   typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy;
252   VectorConstantsTy VectorConstants;
253   
254   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
255
256   DenseMap<Type*, UndefValue*> UVConstants;
257   
258   StringMap<ConstantDataSequential*> CDSConstants;
259
260   
261   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
262   ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
263     ExprConstants;
264
265   ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
266                     InlineAsm> InlineAsms;
267   
268   ConstantInt *TheTrueVal;
269   ConstantInt *TheFalseVal;
270   
271   LeakDetectorImpl<Value> LLVMObjects;
272   
273   // Basic type instances.
274   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
275   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
276   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
277
278   
279   /// TypeAllocator - All dynamically allocated types are allocated from this.
280   /// They live forever until the context is torn down.
281   BumpPtrAllocator TypeAllocator;
282   
283   DenseMap<unsigned, IntegerType*> IntegerTypes;
284   
285   typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap;
286   FunctionTypeMap FunctionTypes;
287   typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap;
288   StructTypeMap AnonStructTypes;
289   StringMap<StructType*> NamedStructTypes;
290   unsigned NamedStructTypesUniqueID;
291     
292   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
293   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
294   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
295   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
296
297
298   /// ValueHandles - This map keeps track of all of the value handles that are
299   /// watching a Value*.  The Value::HasValueHandle bit is used to know
300   // whether or not a value has an entry in this map.
301   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
302   ValueHandlesTy ValueHandles;
303   
304   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
305   StringMap<unsigned> CustomMDKindNames;
306   
307   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
308   typedef SmallVector<MDPairTy, 2> MDMapTy;
309
310   /// MetadataStore - Collection of per-instruction metadata used in this
311   /// context.
312   DenseMap<const Instruction *, MDMapTy> MetadataStore;
313   
314   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
315   /// entry with no "inlined at" element.
316   DenseMap<MDNode*, int> ScopeRecordIdx;
317   
318   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
319   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
320   /// the MDNode is RAUW'd.
321   std::vector<DebugRecVH> ScopeRecords;
322   
323   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
324   /// scope/inlined-at pair.
325   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
326   
327   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
328   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
329   /// to date.
330   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
331   
332   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
333   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
334   
335   LLVMContextImpl(LLVMContext &C);
336   ~LLVMContextImpl();
337 };
338
339 }
340
341 #endif