IR: Merge UniquableMDNode back into MDNode, NFC
[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)
183       : Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}
184
185   template <class NodeTy> bool compareOps(const NodeTy *RHS) const {
186     if (getHash() != RHS->getHash())
187       return false;
188
189     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
190     return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
191   }
192
193   static unsigned calculateHash(MDNode *N);
194
195 private:
196   template <class T>
197   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS) {
198     if (Ops.size() != RHS->getNumOperands())
199       return false;
200     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
201   }
202
203   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
204
205 public:
206   unsigned getHash() const { return Hash; }
207 };
208
209 /// \brief DenseMapInfo for MDTuple.
210 ///
211 /// Note that we don't need the is-function-local bit, since that's implicit in
212 /// the operands.
213 struct MDTupleInfo {
214   struct KeyTy : MDNodeOpsKey {
215     KeyTy(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
216     KeyTy(MDTuple *N) : MDNodeOpsKey(N) {}
217
218     bool operator==(const MDTuple *RHS) const {
219       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
220         return false;
221       return compareOps(RHS);
222     }
223
224     static unsigned calculateHash(MDTuple *N) {
225       return MDNodeOpsKey::calculateHash(N);
226     }
227   };
228   static inline MDTuple *getEmptyKey() {
229     return DenseMapInfo<MDTuple *>::getEmptyKey();
230   }
231   static inline MDTuple *getTombstoneKey() {
232     return DenseMapInfo<MDTuple *>::getTombstoneKey();
233   }
234   static unsigned getHashValue(const KeyTy &Key) { return Key.getHash(); }
235   static unsigned getHashValue(const MDTuple *U) { return U->getHash(); }
236   static bool isEqual(const KeyTy &LHS, const MDTuple *RHS) {
237     return LHS == RHS;
238   }
239   static bool isEqual(const MDTuple *LHS, const MDTuple *RHS) {
240     return LHS == RHS;
241   }
242 };
243
244 /// \brief DenseMapInfo for MDLocation.
245 struct MDLocationInfo {
246   struct KeyTy {
247     unsigned Line;
248     unsigned Column;
249     Metadata *Scope;
250     Metadata *InlinedAt;
251
252     KeyTy(unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt)
253         : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
254
255     KeyTy(const MDLocation *L)
256         : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()),
257           InlinedAt(L->getInlinedAt()) {}
258
259     bool operator==(const MDLocation *RHS) const {
260       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
261         return false;
262       return Line == RHS->getLine() && Column == RHS->getColumn() &&
263              Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt();
264     }
265   };
266   static inline MDLocation *getEmptyKey() {
267     return DenseMapInfo<MDLocation *>::getEmptyKey();
268   }
269   static inline MDLocation *getTombstoneKey() {
270     return DenseMapInfo<MDLocation *>::getTombstoneKey();
271   }
272   static unsigned getHashValue(const KeyTy &Key) {
273     return hash_combine(Key.Line, Key.Column, Key.Scope, Key.InlinedAt);
274   }
275   static unsigned getHashValue(const MDLocation *U) {
276     return getHashValue(KeyTy(U));
277   }
278   static bool isEqual(const KeyTy &LHS, const MDLocation *RHS) {
279     return LHS == RHS;
280   }
281   static bool isEqual(const MDLocation *LHS, const MDLocation *RHS) {
282     return LHS == RHS;
283   }
284 };
285
286 class LLVMContextImpl {
287 public:
288   /// OwnedModules - The set of modules instantiated in this context, and which
289   /// will be automatically deleted if this context is deleted.
290   SmallPtrSet<Module*, 4> OwnedModules;
291   
292   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
293   void *InlineAsmDiagContext;
294
295   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
296   void *DiagnosticContext;
297   bool RespectDiagnosticFilters;
298
299   LLVMContext::YieldCallbackTy YieldCallback;
300   void *YieldOpaqueHandle;
301
302   typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
303   IntMapTy IntConstants;
304
305   typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
306   FPMapTy FPConstants;
307
308   FoldingSet<AttributeImpl> AttrsSet;
309   FoldingSet<AttributeSetImpl> AttrsLists;
310   FoldingSet<AttributeSetNode> AttrsSetNodes;
311
312   StringMap<MDString> MDStringCache;
313   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
314   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
315
316   DenseSet<MDTuple *, MDTupleInfo> MDTuples;
317   DenseSet<MDLocation *, MDLocationInfo> MDLocations;
318
319   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
320   // aren't in the MDNodeSet, but they're still shared between objects, so no
321   // one object can destroy them.  This set allows us to at least destroy them
322   // on Context destruction.
323   SmallPtrSet<MDNode *, 1> DistinctMDNodes;
324
325   DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
326
327   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
328   ArrayConstantsTy ArrayConstants;
329   
330   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
331   StructConstantsTy StructConstants;
332   
333   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
334   VectorConstantsTy VectorConstants;
335   
336   DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
337
338   DenseMap<Type*, UndefValue*> UVConstants;
339   
340   StringMap<ConstantDataSequential*> CDSConstants;
341
342   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
343     BlockAddresses;
344   ConstantUniqueMap<ConstantExpr> ExprConstants;
345
346   ConstantUniqueMap<InlineAsm> InlineAsms;
347
348   ConstantInt *TheTrueVal;
349   ConstantInt *TheFalseVal;
350   
351   LeakDetectorImpl<Value> LLVMObjects;
352   LeakDetectorImpl<Metadata> LLVMMDObjects;
353
354   // Basic type instances.
355   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
356   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
357   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty;
358
359   
360   /// TypeAllocator - All dynamically allocated types are allocated from this.
361   /// They live forever until the context is torn down.
362   BumpPtrAllocator TypeAllocator;
363   
364   DenseMap<unsigned, IntegerType*> IntegerTypes;
365
366   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
367   FunctionTypeSet FunctionTypes;
368   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
369   StructTypeSet AnonStructTypes;
370   StringMap<StructType*> NamedStructTypes;
371   unsigned NamedStructTypesUniqueID;
372     
373   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
374   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
375   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
376   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
377
378
379   /// ValueHandles - This map keeps track of all of the value handles that are
380   /// watching a Value*.  The Value::HasValueHandle bit is used to know
381   /// whether or not a value has an entry in this map.
382   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
383   ValueHandlesTy ValueHandles;
384   
385   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
386   StringMap<unsigned> CustomMDKindNames;
387
388   typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
389   typedef SmallVector<MDPairTy, 2> MDMapTy;
390
391   /// MetadataStore - Collection of per-instruction metadata used in this
392   /// context.
393   DenseMap<const Instruction *, MDMapTy> MetadataStore;
394   
395   /// DiscriminatorTable - This table maps file:line locations to an
396   /// integer representing the next DWARF path discriminator to assign to
397   /// instructions in different blocks at the same location.
398   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
399
400   /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings
401   /// requested in this context
402   typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy;
403   IntrinsicIDCacheTy IntrinsicIDCache;
404
405   /// \brief Mapping from a function to its prefix data, which is stored as the
406   /// operand of an unparented ReturnInst so that the prefix data has a Use.
407   typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
408   PrefixDataMapTy PrefixDataMap;
409
410   /// \brief Mapping from a function to its prologue data, which is stored as
411   /// the operand of an unparented ReturnInst so that the prologue data has a
412   /// Use.
413   typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
414   PrologueDataMapTy PrologueDataMap;
415
416   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
417   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
418
419   /// An owning list of all GCStrategies which have been created
420   SmallVector<std::unique_ptr<GCStrategy>, 1> GCStrategyList;
421   /// A helper map to speedup lookups into the above list
422   StringMap<GCStrategy*> GCStrategyMap;
423
424   /// Lookup the GCStrategy object associated with the given gc name.  If one
425   /// can't be found, returns nullptr.  The lifetime of the returned objects
426   /// is dictated by the lifetime of the associated context.  No caller should
427   /// attempt to delete the returned objects.
428   GCStrategy *getGCStrategy(const StringRef Name);
429   
430   LLVMContextImpl(LLVMContext &C);
431   ~LLVMContextImpl();
432 };
433
434 }
435
436 #endif