switch UndefValue and ConstantPointerNull over to DenseMap's for uniquing.
[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/OwningPtr.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 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 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
94 /// up to date as MDNodes mutate.  This class is implemented in DebugLoc.cpp.
95 class DebugRecVH : public CallbackVH {
96   /// Ctx - This is the LLVM Context being referenced.
97   LLVMContextImpl *Ctx;
98   
99   /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
100   /// this reference lives in.  If this is zero, then it represents a
101   /// non-canonical entry that has no DenseMap value.  This can happen due to
102   /// RAUW.
103   int Idx;
104 public:
105   DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
106     : CallbackVH(n), Ctx(ctx), Idx(idx) {}
107   
108   MDNode *get() const {
109     return cast_or_null<MDNode>(getValPtr());
110   }
111   
112   virtual void deleted();
113   virtual void allUsesReplacedWith(Value *VNew);
114 };
115   
116 class LLVMContextImpl {
117 public:
118   /// OwnedModules - The set of modules instantiated in this context, and which
119   /// will be automatically deleted if this context is deleted.
120   SmallPtrSet<Module*, 4> OwnedModules;
121   
122   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
123   void *InlineAsmDiagContext;
124   
125   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
126                          DenseMapAPIntKeyInfo> IntMapTy;
127   IntMapTy IntConstants;
128   
129   typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 
130                          DenseMapAPFloatKeyInfo> FPMapTy;
131   FPMapTy FPConstants;
132   
133   StringMap<MDString*> MDStringCache;
134   
135   FoldingSet<MDNode> MDNodeSet;
136   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
137   // aren't in the MDNodeSet, but they're still shared between objects, so no
138   // one object can destroy them.  This set allows us to at least destroy them
139   // on Context destruction.
140   SmallPtrSet<MDNode*, 1> NonUniquedMDNodes;
141   
142   typedef DenseMap<Type*, OwningPtr<ConstantAggregateZero> > CAZMapTy;
143   CAZMapTy CAZConstants;
144
145   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
146     ArrayType, ConstantArray, true /*largekey*/> ArrayConstantsTy;
147   ArrayConstantsTy ArrayConstants;
148   
149   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
150     StructType, ConstantStruct, true /*largekey*/> StructConstantsTy;
151   StructConstantsTy StructConstants;
152   
153   typedef ConstantUniqueMap<std::vector<Constant*>, ArrayRef<Constant*>,
154                             VectorType, ConstantVector> VectorConstantsTy;
155   VectorConstantsTy VectorConstants;
156   
157   typedef DenseMap<PointerType*, OwningPtr<ConstantPointerNull> > CPNMapTy;
158   CPNMapTy CPNConstants;
159
160   typedef DenseMap<Type*, OwningPtr<UndefValue> > UVMapTy;
161   UVMapTy UVConstants;
162   
163   DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses;
164   ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr>
165     ExprConstants;
166
167   ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType,
168                     InlineAsm> InlineAsms;
169   
170   ConstantInt *TheTrueVal;
171   ConstantInt *TheFalseVal;
172   
173   LeakDetectorImpl<Value> LLVMObjects;
174   
175   // Basic type instances.
176   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
177   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
178   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
179
180   
181   /// TypeAllocator - All dynamically allocated types are allocated from this.
182   /// They live forever until the context is torn down.
183   BumpPtrAllocator TypeAllocator;
184   
185   DenseMap<unsigned, IntegerType*> IntegerTypes;
186   
187   // TODO: Optimize FunctionTypes/AnonStructTypes!
188   std::map<std::vector<Type*>, FunctionType*> FunctionTypes;
189   std::map<std::vector<Type*>, StructType*> AnonStructTypes;
190   StringMap<StructType*> NamedStructTypes;
191   unsigned NamedStructTypesUniqueID;
192     
193   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
194   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
195   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
196   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
197
198
199   /// ValueHandles - This map keeps track of all of the value handles that are
200   /// watching a Value*.  The Value::HasValueHandle bit is used to know
201   // whether or not a value has an entry in this map.
202   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
203   ValueHandlesTy ValueHandles;
204   
205   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
206   StringMap<unsigned> CustomMDKindNames;
207   
208   typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
209   typedef SmallVector<MDPairTy, 2> MDMapTy;
210
211   /// MetadataStore - Collection of per-instruction metadata used in this
212   /// context.
213   DenseMap<const Instruction *, MDMapTy> MetadataStore;
214   
215   /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
216   /// entry with no "inlined at" element.
217   DenseMap<MDNode*, int> ScopeRecordIdx;
218   
219   /// ScopeRecords - These are the actual mdnodes (in a value handle) for an
220   /// index.  The ValueHandle ensures that ScopeRecordIdx stays up to date if
221   /// the MDNode is RAUW'd.
222   std::vector<DebugRecVH> ScopeRecords;
223   
224   /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
225   /// scope/inlined-at pair.
226   DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;
227   
228   /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
229   /// for an index.  The ValueHandle ensures that ScopeINlinedAtIdx stays up
230   /// to date.
231   std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;
232   
233   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
234   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
235   
236   LLVMContextImpl(LLVMContext &C);
237   ~LLVMContextImpl();
238 };
239
240 }
241
242 #endif