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