IR: Introduce GenericDwarfNode
[oota-llvm.git] / lib / IR / 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_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
17
18 #include "AttributeImpl.h"
19 #include "ConstantsContext.h"
20 #include "LeaksContext.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/FoldingSet.h"
27 #include "llvm/ADT/Hashing.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include <vector>
36
37 namespace llvm {
38
39 class ConstantInt;
40 class ConstantFP;
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
44 class GCStrategy;
45 class LLVMContext;
46 class Type;
47 class Value;
48
49 struct DenseMapAPIntKeyInfo {
50   static inline APInt getEmptyKey() {
51     APInt V(nullptr, 0);
52     V.VAL = 0;
53     return V;
54   }
55   static inline APInt getTombstoneKey() {
56     APInt V(nullptr, 0);
57     V.VAL = 1;
58     return V;
59   }
60   static unsigned getHashValue(const APInt &Key) {
61     return static_cast<unsigned>(hash_value(Key));
62   }
63   static bool isEqual(const APInt &LHS, const APInt &RHS) {
64     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
65   }
66 };
67
68 struct DenseMapAPFloatKeyInfo {
69   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
70   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
71   static unsigned getHashValue(const APFloat &Key) {
72     return static_cast<unsigned>(hash_value(Key));
73   }
74   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
75     return LHS.bitwiseIsEqual(RHS);
76   }
77 };
78
79 struct AnonStructTypeKeyInfo {
80   struct KeyTy {
81     ArrayRef<Type*> ETypes;
82     bool isPacked;
83     KeyTy(const ArrayRef<Type*>& E, bool P) :
84       ETypes(E), isPacked(P) {}
85     KeyTy(const StructType *ST)
86         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
87     bool operator==(const KeyTy& that) const {
88       if (isPacked != that.isPacked)
89         return false;
90       if (ETypes != that.ETypes)
91         return false;
92       return true;
93     }
94     bool operator!=(const KeyTy& that) const {
95       return !this->operator==(that);
96     }
97   };
98   static inline StructType* getEmptyKey() {
99     return DenseMapInfo<StructType*>::getEmptyKey();
100   }
101   static inline StructType* getTombstoneKey() {
102     return DenseMapInfo<StructType*>::getTombstoneKey();
103   }
104   static unsigned getHashValue(const KeyTy& Key) {
105     return hash_combine(hash_combine_range(Key.ETypes.begin(),
106                                            Key.ETypes.end()),
107                         Key.isPacked);
108   }
109   static unsigned getHashValue(const StructType *ST) {
110     return getHashValue(KeyTy(ST));
111   }
112   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
113     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
114       return false;
115     return LHS == KeyTy(RHS);
116   }
117   static bool isEqual(const StructType *LHS, const StructType *RHS) {
118     return LHS == RHS;
119   }
120 };
121
122 struct FunctionTypeKeyInfo {
123   struct KeyTy {
124     const Type *ReturnType;
125     ArrayRef<Type*> Params;
126     bool isVarArg;
127     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
128       ReturnType(R), Params(P), isVarArg(V) {}
129     KeyTy(const FunctionType *FT)
130         : ReturnType(FT->getReturnType()), Params(FT->params()),
131           isVarArg(FT->isVarArg()) {}
132     bool operator==(const KeyTy& that) const {
133       if (ReturnType != that.ReturnType)
134         return false;
135       if (isVarArg != that.isVarArg)
136         return false;
137       if (Params != that.Params)
138         return false;
139       return true;
140     }
141     bool operator!=(const KeyTy& that) const {
142       return !this->operator==(that);
143     }
144   };
145   static inline FunctionType* getEmptyKey() {
146     return DenseMapInfo<FunctionType*>::getEmptyKey();
147   }
148   static inline FunctionType* getTombstoneKey() {
149     return DenseMapInfo<FunctionType*>::getTombstoneKey();
150   }
151   static unsigned getHashValue(const KeyTy& Key) {
152     return hash_combine(Key.ReturnType,
153                         hash_combine_range(Key.Params.begin(),
154                                            Key.Params.end()),
155                         Key.isVarArg);
156   }
157   static unsigned getHashValue(const FunctionType *FT) {
158     return getHashValue(KeyTy(FT));
159   }
160   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
161     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
162       return false;
163     return LHS == KeyTy(RHS);
164   }
165   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
166     return LHS == RHS;
167   }
168 };
169
170 /// \brief Structure for hashing arbitrary MDNode operands.
171 class MDNodeOpsKey {
172   ArrayRef<Metadata *> RawOps;
173   ArrayRef<MDOperand> Ops;
174
175   unsigned Hash;
176
177 protected:
178   MDNodeOpsKey(ArrayRef<Metadata *> Ops)
179       : RawOps(Ops), Hash(calculateHash(Ops)) {}
180
181   template <class NodeTy>
182   MDNodeOpsKey(NodeTy *N, unsigned Offset = 0)
183       : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
184
185   template <class NodeTy>
186   bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
187     if (getHash() != RHS->getHash())
188       return false;
189
190     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
191     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
192                           : compareOps(RawOps, RHS, Offset);
193   }
194
195   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
196
197 private:
198   template <class T>
199   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
200     if (Ops.size() != RHS->getNumOperands() - Offset)
201       return false;
202     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
203   }
204
205   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
206
207 public:
208   unsigned getHash() const { return Hash; }
209 };
210
211 /// \brief DenseMapInfo for MDTuple.
212 ///
213 /// Note that we don't need the is-function-local bit, since that's implicit in
214 /// the operands.
215 struct MDTupleInfo {
216   struct KeyTy : MDNodeOpsKey {
217     KeyTy(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
218     KeyTy(MDTuple *N) : MDNodeOpsKey(N) {}
219
220     bool operator==(const MDTuple *RHS) const {
221       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
222         return false;
223       return compareOps(RHS);
224     }
225
226     static unsigned calculateHash(MDTuple *N) {
227       return MDNodeOpsKey::calculateHash(N);
228     }
229   };
230   static inline MDTuple *getEmptyKey() {
231     return DenseMapInfo<MDTuple *>::getEmptyKey();
232   }
233   static inline MDTuple *getTombstoneKey() {
234     return DenseMapInfo<MDTuple *>::getTombstoneKey();
235   }
236   static unsigned getHashValue(const KeyTy &Key) { return Key.getHash(); }
237   static unsigned getHashValue(const MDTuple *U) { return U->getHash(); }
238   static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
239     return LHS == RHS;
240   }
241   static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
242     return LHS == RHS;
243   }
244 };
245
246 /// \brief DenseMapInfo for MDLocation.
247 struct MDLocationInfo {
248   struct KeyTy {
249     unsigned Line;
250     unsigned Column;
251     Metadata *Scope;
252     Metadata *InlinedAt;
253
254     KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
255         : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
256
257     KeyTy(const MDLocation *L)
258         : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
259           InlinedAt(L->getInlinedAt()) {}
260
261     bool operator==(const MDLocation *RHS) const {
262       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
263         return false;
264       return Line == RHS->getLine() && Column == RHS->getColumn() &&
265              Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
266     }
267   };
268   static inline MDLocation *getEmptyKey() {
269     return DenseMapInfo<MDLocation *>::getEmptyKey();
270   }
271   static inline MDLocation *getTombstoneKey() {
272     return DenseMapInfo<MDLocation *>::getTombstoneKey();
273   }
274   static unsigned getHashValue(const KeyTy &Key) {
275     return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
276   }
277   static unsigned getHashValue(const MDLocation *U) {
278     return getHashValue(KeyTy(U));
279   }
280   static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
281     return LHS == RHS;
282   }
283   static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
284     return LHS == RHS;
285   }
286 };
287
288 /// \brief DenseMapInfo for GenericDwarfNode.
289 struct GenericDwarfNodeInfo {
290   struct KeyTy : MDNodeOpsKey {
291     unsigned Tag;
292     MDString *Header;
293     KeyTy(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
294         : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
295     KeyTy(GenericDwarfNode *N)
296         : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
297
298     bool operator==(const GenericDwarfNode *RHS) const {
299       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
300         return false;
301       return Tag == RHS->getTag() && Header == RHS->getHeader() &&
302              compareOps(RHS, 1);
303     }
304
305     static unsigned calculateHash(GenericDwarfNode *N) {
306       return MDNodeOpsKey::calculateHash(N, 1);
307     }
308   };
309   static inline GenericDwarfNode *getEmptyKey() {
310     return DenseMapInfo<GenericDwarfNode *>::getEmptyKey();
311   }
312   static inline GenericDwarfNode *getTombstoneKey() {
313     return DenseMapInfo<GenericDwarfNode *>::getTombstoneKey();
314   }
315   static unsigned getHashValue(const KeyTy &Key) {
316     return hash_combine(Key.getHash(), Key.Tag, Key.Header);
317   }
318   static unsigned getHashValue(const GenericDwarfNode *U) {
319     return hash_combine(U->getHash(), U->getTag(), U->getHeader());
320   }
321   static bool isEqual(const KeyTy &LHS, const GenericDwarfNode *RHS) {
322     return LHS == RHS;
323   }
324   static bool isEqual(const GenericDwarfNode *LHS,
325                       const GenericDwarfNode *RHS) {
326     return LHS == RHS;
327   }
328 };
329
330 class LLVMContextImpl {
331 public:
332   /// OwnedModules - The set of modules instantiated in this context, and which
333   /// will be automatically deleted if this context is deleted.
334   SmallPtrSet<Module*, 4> OwnedModules;
335   
336   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
337   void *InlineAsmDiagContext;
338
339   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
340   void *DiagnosticContext;
341   bool RespectDiagnosticFilters;
342
343   LLVMContext::YieldCallbackTy YieldCallback;
344   void *YieldOpaqueHandle;
345
346   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
347   IntMapTy IntConstants;
348
349   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
350   FPMapTy FPConstants;
351
352   FoldingSet<AttributeImpl> AttrsSet;
353   FoldingSet<AttributeSetImpl> AttrsLists;
354   FoldingSet<AttributeSetNode> AttrsSetNodes;
355
356   StringMap<MDString> MDStringCache;
357   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
358   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
359
360   DenseSet<MDTuple *, MDTupleInfo> MDTuples;
361   DenseSet<MDLocation *, MDLocationInfo> MDLocations;
362   DenseSet<GenericDwarfNode *, GenericDwarfNodeInfo> GenericDwarfNodes;
363
364   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
365   // aren't in the MDNodeSet, but they're still shared between objects, so no
366   // one object can destroy them.  This set allows us to at least destroy them
367   // on Context destruction.
368   SmallPtrSet<MDNode *, 1> DistinctMDNodes;
369
370   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
371
372   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
373   ArrayConstantsTy ArrayConstants;
374   
375   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
376   StructConstantsTy StructConstants;
377   
378   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
379   VectorConstantsTy VectorConstants;
380   
381   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
382
383   DenseMap<Type*, UndefValue*> UVConstants;
384   
385   StringMap<ConstantDataSequential*> CDSConstants;
386
387   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
388     BlockAddresses;
389   ConstantUniqueMap<ConstantExpr> ExprConstants;
390
391   ConstantUniqueMap<InlineAsm> InlineAsms;
392
393   ConstantInt *TheTrueVal;
394   ConstantInt *TheFalseVal;
395   
396   LeakDetectorImpl<Value> LLVMObjects;
397   LeakDetectorImpl<Metadata> LLVMMDObjects;
398
399   // Basic type instances.
400   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
401   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
402   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
403
404   
405   /// TypeAllocator - All dynamically allocated types are allocated from this.
406   /// They live forever until the context is torn down.
407   BumpPtrAllocator TypeAllocator;
408   
409   DenseMap<unsigned, IntegerType*> IntegerTypes;
410
411   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
412   FunctionTypeSet FunctionTypes;
413   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
414   StructTypeSet AnonStructTypes;
415   StringMap<StructType*> NamedStructTypes;
416   unsigned NamedStructTypesUniqueID;
417     
418   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
419   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
420   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
421   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
422
423
424   /// ValueHandles - This map keeps track of all of the value handles that are
425   /// watching a Value*.  The Value::HasValueHandle bit is used to know
426   /// whether or not a value has an entry in this map.
427   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
428   ValueHandlesTy ValueHandles;
429   
430   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
431   StringMap<unsigned> CustomMDKindNames;
432
433   typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
434   typedef SmallVector<MDPairTy, 2> MDMapTy;
435
436   /// MetadataStore - Collection of per-instruction metadata used in this
437   /// context.
438   DenseMap<const Instruction *, MDMapTy> MetadataStore;
439   
440   /// DiscriminatorTable - This table maps file:line locations to an
441   /// integer representing the next DWARF path discriminator to assign to
442   /// instructions in different blocks at the same location.
443   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
444
445   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
446   /// requested in this context
447   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
448   IntrinsicIDCacheTy IntrinsicIDCache;
449
450   /// \brief Mapping from a function to its prefix data, which is stored as the
451   /// operand of an unparented ReturnInst so that the prefix data has a Use.
452   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
453   PrefixDataMapTy PrefixDataMap;
454
455   /// \brief Mapping from a function to its prologue data, which is stored as
456   /// the operand of an unparented ReturnInst so that the prologue data has a
457   /// Use.
458   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
459   PrologueDataMapTy PrologueDataMap;
460
461   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
462   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
463
464   /// An owning list of all GCStrategies which have been created
465   SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
466   /// A helper map to speedup lookups into the above list
467   StringMap<GCStrategy*> GCStrategyMap;
468
469   /// Lookup the GCStrategy object associated with the given gc name.  If one
470   /// can't be found, returns nullptr.  The lifetime of the returned objects
471   /// is dictated by the lifetime of the associated context.  No caller should
472   /// attempt to delete the returned objects.
473   GCStrategy *getGCStrategy(const StringRef Name);
474   
475   LLVMContextImpl(LLVMContext &C);
476   ~LLVMContextImpl();
477 };
478
479 }
480
481 #endif