]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/LLVMContextImpl.h
Merge ^/head r312309 through r312623.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/StringMap.h"
29 #include "llvm/ADT/StringSet.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DebugInfoMetadata.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/IR/Metadata.h"
35 #include "llvm/IR/ValueHandle.h"
36 #include "llvm/Support/Dwarf.h"
37 #include "llvm/Support/YAMLTraits.h"
38 #include <vector>
39
40 namespace llvm {
41
42 class ConstantInt;
43 class ConstantFP;
44 class DiagnosticInfoOptimizationRemark;
45 class DiagnosticInfoOptimizationRemarkMissed;
46 class DiagnosticInfoOptimizationRemarkAnalysis;
47 class GCStrategy;
48 class LLVMContext;
49 class Type;
50 class Value;
51
52 struct DenseMapAPIntKeyInfo {
53   static inline APInt getEmptyKey() {
54     APInt V(nullptr, 0);
55     V.VAL = 0;
56     return V;
57   }
58   static inline APInt getTombstoneKey() {
59     APInt V(nullptr, 0);
60     V.VAL = 1;
61     return V;
62   }
63   static unsigned getHashValue(const APInt &Key) {
64     return static_cast<unsigned>(hash_value(Key));
65   }
66   static bool isEqual(const APInt &LHS, const APInt &RHS) {
67     return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
68   }
69 };
70
71 struct DenseMapAPFloatKeyInfo {
72   static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); }
73   static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus(), 2); }
74   static unsigned getHashValue(const APFloat &Key) {
75     return static_cast<unsigned>(hash_value(Key));
76   }
77   static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
78     return LHS.bitwiseIsEqual(RHS);
79   }
80 };
81
82 struct AnonStructTypeKeyInfo {
83   struct KeyTy {
84     ArrayRef<Type*> ETypes;
85     bool isPacked;
86     KeyTy(const ArrayRef<Type*>& E, bool P) :
87       ETypes(E), isPacked(P) {}
88     KeyTy(const StructType *ST)
89         : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
90     bool operator==(const KeyTy& that) const {
91       if (isPacked != that.isPacked)
92         return false;
93       if (ETypes != that.ETypes)
94         return false;
95       return true;
96     }
97     bool operator!=(const KeyTy& that) const {
98       return !this->operator==(that);
99     }
100   };
101   static inline StructType* getEmptyKey() {
102     return DenseMapInfo<StructType*>::getEmptyKey();
103   }
104   static inline StructType* getTombstoneKey() {
105     return DenseMapInfo<StructType*>::getTombstoneKey();
106   }
107   static unsigned getHashValue(const KeyTy& Key) {
108     return hash_combine(hash_combine_range(Key.ETypes.begin(),
109                                            Key.ETypes.end()),
110                         Key.isPacked);
111   }
112   static unsigned getHashValue(const StructType *ST) {
113     return getHashValue(KeyTy(ST));
114   }
115   static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
116     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
117       return false;
118     return LHS == KeyTy(RHS);
119   }
120   static bool isEqual(const StructType *LHS, const StructType *RHS) {
121     return LHS == RHS;
122   }
123 };
124
125 struct FunctionTypeKeyInfo {
126   struct KeyTy {
127     const Type *ReturnType;
128     ArrayRef<Type*> Params;
129     bool isVarArg;
130     KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
131       ReturnType(R), Params(P), isVarArg(V) {}
132     KeyTy(const FunctionType *FT)
133         : ReturnType(FT->getReturnType()), Params(FT->params()),
134           isVarArg(FT->isVarArg()) {}
135     bool operator==(const KeyTy& that) const {
136       if (ReturnType != that.ReturnType)
137         return false;
138       if (isVarArg != that.isVarArg)
139         return false;
140       if (Params != that.Params)
141         return false;
142       return true;
143     }
144     bool operator!=(const KeyTy& that) const {
145       return !this->operator==(that);
146     }
147   };
148   static inline FunctionType* getEmptyKey() {
149     return DenseMapInfo<FunctionType*>::getEmptyKey();
150   }
151   static inline FunctionType* getTombstoneKey() {
152     return DenseMapInfo<FunctionType*>::getTombstoneKey();
153   }
154   static unsigned getHashValue(const KeyTy& Key) {
155     return hash_combine(Key.ReturnType,
156                         hash_combine_range(Key.Params.begin(),
157                                            Key.Params.end()),
158                         Key.isVarArg);
159   }
160   static unsigned getHashValue(const FunctionType *FT) {
161     return getHashValue(KeyTy(FT));
162   }
163   static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
164     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
165       return false;
166     return LHS == KeyTy(RHS);
167   }
168   static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
169     return LHS == RHS;
170   }
171 };
172
173 /// \brief Structure for hashing arbitrary MDNode operands.
174 class MDNodeOpsKey {
175   ArrayRef<Metadata *> RawOps;
176   ArrayRef<MDOperand> Ops;
177
178   unsigned Hash;
179
180 protected:
181   MDNodeOpsKey(ArrayRef<Metadata *> Ops)
182       : RawOps(Ops), Hash(calculateHash(Ops)) {}
183
184   template <class NodeTy>
185   MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
186       : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
187
188   template <class NodeTy>
189   bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
190     if (getHash() != RHS->getHash())
191       return false;
192
193     assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
194     return RawOps.empty() ? compareOps(Ops, RHS, Offset)
195                           : compareOps(RawOps, RHS, Offset);
196   }
197
198   static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
199
200 private:
201   template <class T>
202   static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
203     if (Ops.size() != RHS->getNumOperands() - Offset)
204       return false;
205     return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
206   }
207
208   static unsigned calculateHash(ArrayRef<Metadata *> Ops);
209
210 public:
211   unsigned getHash() const { return Hash; }
212 };
213
214 template <class NodeTy> struct MDNodeKeyImpl;
215 template <class NodeTy> struct MDNodeInfo;
216
217 /// Configuration point for MDNodeInfo::isEqual().
218 template <class NodeTy> struct MDNodeSubsetEqualImpl {
219   typedef MDNodeKeyImpl<NodeTy> KeyTy;
220   static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) {
221     return false;
222   }
223   static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) {
224     return false;
225   }
226 };
227
228 /// \brief DenseMapInfo for MDTuple.
229 ///
230 /// Note that we don't need the is-function-local bit, since that's implicit in
231 /// the operands.
232 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
233   MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
234   MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
235
236   bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
237
238   unsigned getHashValue() const { return getHash(); }
239
240   static unsigned calculateHash(MDTuple *N) {
241     return MDNodeOpsKey::calculateHash(N);
242   }
243 };
244
245 /// \brief DenseMapInfo for DILocation.
246 template <> struct MDNodeKeyImpl<DILocation> {
247   unsigned Line;
248   unsigned Column;
249   Metadata *Scope;
250   Metadata *InlinedAt;
251
252   MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
253                 Metadata *InlinedAt)
254       : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
255
256   MDNodeKeyImpl(const DILocation *L)
257       : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
258         InlinedAt(L->getRawInlinedAt()) {}
259
260   bool isKeyOf(const DILocation *RHS) const {
261     return Line == RHS->getLine() && Column == RHS->getColumn() &&
262            Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt();
263   }
264   unsigned getHashValue() const {
265     return hash_combine(Line, Column, Scope, InlinedAt);
266   }
267 };
268
269 /// \brief DenseMapInfo for GenericDINode.
270 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
271   unsigned Tag;
272   MDString *Header;
273   MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
274       : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
275   MDNodeKeyImpl(const GenericDINode *N)
276       : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {}
277
278   bool isKeyOf(const GenericDINode *RHS) const {
279     return Tag == RHS->getTag() && Header == RHS->getRawHeader() &&
280            compareOps(RHS, 1);
281   }
282
283   unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
284
285   static unsigned calculateHash(GenericDINode *N) {
286     return MDNodeOpsKey::calculateHash(N, 1);
287   }
288 };
289
290 template <> struct MDNodeKeyImpl<DISubrange> {
291   int64_t Count;
292   int64_t LowerBound;
293
294   MDNodeKeyImpl(int64_t Count, int64_t LowerBound)
295       : Count(Count), LowerBound(LowerBound) {}
296   MDNodeKeyImpl(const DISubrange *N)
297       : Count(N->getCount()), LowerBound(N->getLowerBound()) {}
298
299   bool isKeyOf(const DISubrange *RHS) const {
300     return Count == RHS->getCount() && LowerBound == RHS->getLowerBound();
301   }
302   unsigned getHashValue() const { return hash_combine(Count, LowerBound); }
303 };
304
305 template <> struct MDNodeKeyImpl<DIEnumerator> {
306   int64_t Value;
307   MDString *Name;
308
309   MDNodeKeyImpl(int64_t Value, MDString *Name) : Value(Value), Name(Name) {}
310   MDNodeKeyImpl(const DIEnumerator *N)
311       : Value(N->getValue()), Name(N->getRawName()) {}
312
313   bool isKeyOf(const DIEnumerator *RHS) const {
314     return Value == RHS->getValue() && Name == RHS->getRawName();
315   }
316   unsigned getHashValue() const { return hash_combine(Value, Name); }
317 };
318
319 template <> struct MDNodeKeyImpl<DIBasicType> {
320   unsigned Tag;
321   MDString *Name;
322   uint64_t SizeInBits;
323   uint32_t AlignInBits;
324   unsigned Encoding;
325
326   MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
327                 uint32_t AlignInBits, unsigned Encoding)
328       : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
329         Encoding(Encoding) {}
330   MDNodeKeyImpl(const DIBasicType *N)
331       : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
332         AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
333
334   bool isKeyOf(const DIBasicType *RHS) const {
335     return Tag == RHS->getTag() && Name == RHS->getRawName() &&
336            SizeInBits == RHS->getSizeInBits() &&
337            AlignInBits == RHS->getAlignInBits() &&
338            Encoding == RHS->getEncoding();
339   }
340   unsigned getHashValue() const {
341     return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
342   }
343 };
344
345 template <> struct MDNodeKeyImpl<DIDerivedType> {
346   unsigned Tag;
347   MDString *Name;
348   Metadata *File;
349   unsigned Line;
350   Metadata *Scope;
351   Metadata *BaseType;
352   uint64_t SizeInBits;
353   uint64_t OffsetInBits;
354   uint32_t AlignInBits;
355   unsigned Flags;
356   Metadata *ExtraData;
357
358   MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
359                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
360                 uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
361                 Metadata *ExtraData)
362       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
363         BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
364         AlignInBits(AlignInBits), Flags(Flags), ExtraData(ExtraData) {}
365   MDNodeKeyImpl(const DIDerivedType *N)
366       : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
367         Line(N->getLine()), Scope(N->getRawScope()),
368         BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
369         OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
370         Flags(N->getFlags()), ExtraData(N->getRawExtraData()) {}
371
372   bool isKeyOf(const DIDerivedType *RHS) const {
373     return Tag == RHS->getTag() && Name == RHS->getRawName() &&
374            File == RHS->getRawFile() && Line == RHS->getLine() &&
375            Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
376            SizeInBits == RHS->getSizeInBits() &&
377            AlignInBits == RHS->getAlignInBits() &&
378            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
379            ExtraData == RHS->getRawExtraData();
380   }
381   unsigned getHashValue() const {
382     // If this is a member inside an ODR type, only hash the type and the name.
383     // Otherwise the hash will be stronger than
384     // MDNodeSubsetEqualImpl::isODRMember().
385     if (Tag == dwarf::DW_TAG_member && Name)
386       if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
387         if (CT->getRawIdentifier())
388           return hash_combine(Name, Scope);
389
390     // Intentionally computes the hash on a subset of the operands for
391     // performance reason. The subset has to be significant enough to avoid
392     // collision "most of the time". There is no correctness issue in case of
393     // collision because of the full check above.
394     return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags);
395   }
396 };
397
398 template <> struct MDNodeSubsetEqualImpl<DIDerivedType> {
399   typedef MDNodeKeyImpl<DIDerivedType> KeyTy;
400   static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) {
401     return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS);
402   }
403   static bool isSubsetEqual(const DIDerivedType *LHS, const DIDerivedType *RHS) {
404     return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(),
405                        RHS);
406   }
407
408   /// Subprograms compare equal if they declare the same function in an ODR
409   /// type.
410   static bool isODRMember(unsigned Tag, const Metadata *Scope,
411                           const MDString *Name, const DIDerivedType *RHS) {
412     // Check whether the LHS is eligible.
413     if (Tag != dwarf::DW_TAG_member || !Name)
414       return false;
415
416     auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
417     if (!CT || !CT->getRawIdentifier())
418       return false;
419
420     // Compare to the RHS.
421     return Tag == RHS->getTag() && Name == RHS->getRawName() &&
422            Scope == RHS->getRawScope();
423   }
424 };
425
426 template <> struct MDNodeKeyImpl<DICompositeType> {
427   unsigned Tag;
428   MDString *Name;
429   Metadata *File;
430   unsigned Line;
431   Metadata *Scope;
432   Metadata *BaseType;
433   uint64_t SizeInBits;
434   uint64_t OffsetInBits;
435   uint32_t AlignInBits;
436   unsigned Flags;
437   Metadata *Elements;
438   unsigned RuntimeLang;
439   Metadata *VTableHolder;
440   Metadata *TemplateParams;
441   MDString *Identifier;
442
443   MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
444                 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
445                 uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
446                 Metadata *Elements, unsigned RuntimeLang,
447                 Metadata *VTableHolder, Metadata *TemplateParams,
448                 MDString *Identifier)
449       : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
450         BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
451         AlignInBits(AlignInBits), Flags(Flags), Elements(Elements),
452         RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
453         TemplateParams(TemplateParams), Identifier(Identifier) {}
454   MDNodeKeyImpl(const DICompositeType *N)
455       : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
456         Line(N->getLine()), Scope(N->getRawScope()),
457         BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
458         OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
459         Flags(N->getFlags()), Elements(N->getRawElements()),
460         RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
461         TemplateParams(N->getRawTemplateParams()),
462         Identifier(N->getRawIdentifier()) {}
463
464   bool isKeyOf(const DICompositeType *RHS) const {
465     return Tag == RHS->getTag() && Name == RHS->getRawName() &&
466            File == RHS->getRawFile() && Line == RHS->getLine() &&
467            Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
468            SizeInBits == RHS->getSizeInBits() &&
469            AlignInBits == RHS->getAlignInBits() &&
470            OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
471            Elements == RHS->getRawElements() &&
472            RuntimeLang == RHS->getRuntimeLang() &&
473            VTableHolder == RHS->getRawVTableHolder() &&
474            TemplateParams == RHS->getRawTemplateParams() &&
475            Identifier == RHS->getRawIdentifier();
476   }
477   unsigned getHashValue() const {
478     // Intentionally computes the hash on a subset of the operands for
479     // performance reason. The subset has to be significant enough to avoid
480     // collision "most of the time". There is no correctness issue in case of
481     // collision because of the full check above.
482     return hash_combine(Name, File, Line, BaseType, Scope, Elements,
483                         TemplateParams);
484   }
485 };
486
487 template <> struct MDNodeKeyImpl<DISubroutineType> {
488   unsigned Flags;
489   uint8_t CC;
490   Metadata *TypeArray;
491
492   MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray)
493       : Flags(Flags), CC(CC), TypeArray(TypeArray) {}
494   MDNodeKeyImpl(const DISubroutineType *N)
495       : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {}
496
497   bool isKeyOf(const DISubroutineType *RHS) const {
498     return Flags == RHS->getFlags() && CC == RHS->getCC() &&
499            TypeArray == RHS->getRawTypeArray();
500   }
501   unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); }
502 };
503
504 template <> struct MDNodeKeyImpl<DIFile> {
505   MDString *Filename;
506   MDString *Directory;
507   DIFile::ChecksumKind CSKind;
508   MDString *Checksum;
509
510   MDNodeKeyImpl(MDString *Filename, MDString *Directory,
511                 DIFile::ChecksumKind CSKind, MDString *Checksum)
512       : Filename(Filename), Directory(Directory), CSKind(CSKind),
513         Checksum(Checksum) {}
514   MDNodeKeyImpl(const DIFile *N)
515       : Filename(N->getRawFilename()), Directory(N->getRawDirectory()),
516         CSKind(N->getChecksumKind()), Checksum(N->getRawChecksum()) {}
517
518   bool isKeyOf(const DIFile *RHS) const {
519     return Filename == RHS->getRawFilename() &&
520            Directory == RHS->getRawDirectory() &&
521            CSKind == RHS->getChecksumKind() &&
522            Checksum == RHS->getRawChecksum();
523   }
524   unsigned getHashValue() const {
525     return hash_combine(Filename, Directory, CSKind, Checksum);
526   }
527 };
528
529 template <> struct MDNodeKeyImpl<DISubprogram> {
530   Metadata *Scope;
531   MDString *Name;
532   MDString *LinkageName;
533   Metadata *File;
534   unsigned Line;
535   Metadata *Type;
536   bool IsLocalToUnit;
537   bool IsDefinition;
538   unsigned ScopeLine;
539   Metadata *ContainingType;
540   unsigned Virtuality;
541   unsigned VirtualIndex;
542   int ThisAdjustment;
543   unsigned Flags;
544   bool IsOptimized;
545   Metadata *Unit;
546   Metadata *TemplateParams;
547   Metadata *Declaration;
548   Metadata *Variables;
549
550   MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
551                 Metadata *File, unsigned Line, Metadata *Type,
552                 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
553                 Metadata *ContainingType, unsigned Virtuality,
554                 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags,
555                 bool IsOptimized, Metadata *Unit, Metadata *TemplateParams,
556                 Metadata *Declaration, Metadata *Variables)
557       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
558         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
559         IsDefinition(IsDefinition), ScopeLine(ScopeLine),
560         ContainingType(ContainingType), Virtuality(Virtuality),
561         VirtualIndex(VirtualIndex), ThisAdjustment(ThisAdjustment),
562         Flags(Flags), IsOptimized(IsOptimized), Unit(Unit),
563         TemplateParams(TemplateParams), Declaration(Declaration),
564         Variables(Variables) {}
565   MDNodeKeyImpl(const DISubprogram *N)
566       : Scope(N->getRawScope()), Name(N->getRawName()),
567         LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
568         Line(N->getLine()), Type(N->getRawType()),
569         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
570         ScopeLine(N->getScopeLine()), ContainingType(N->getRawContainingType()),
571         Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()),
572         ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
573         IsOptimized(N->isOptimized()), Unit(N->getRawUnit()),
574         TemplateParams(N->getRawTemplateParams()),
575         Declaration(N->getRawDeclaration()), Variables(N->getRawVariables()) {}
576
577   bool isKeyOf(const DISubprogram *RHS) const {
578     return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
579            LinkageName == RHS->getRawLinkageName() &&
580            File == RHS->getRawFile() && Line == RHS->getLine() &&
581            Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
582            IsDefinition == RHS->isDefinition() &&
583            ScopeLine == RHS->getScopeLine() &&
584            ContainingType == RHS->getRawContainingType() &&
585            Virtuality == RHS->getVirtuality() &&
586            VirtualIndex == RHS->getVirtualIndex() &&
587            ThisAdjustment == RHS->getThisAdjustment() &&
588            Flags == RHS->getFlags() && IsOptimized == RHS->isOptimized() &&
589            Unit == RHS->getUnit() &&
590            TemplateParams == RHS->getRawTemplateParams() &&
591            Declaration == RHS->getRawDeclaration() &&
592            Variables == RHS->getRawVariables();
593   }
594   unsigned getHashValue() const {
595     // If this is a declaration inside an ODR type, only hash the type and the
596     // name.  Otherwise the hash will be stronger than
597     // MDNodeSubsetEqualImpl::isDeclarationOfODRMember().
598     if (!IsDefinition && LinkageName)
599       if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
600         if (CT->getRawIdentifier())
601           return hash_combine(LinkageName, Scope);
602
603     // Intentionally computes the hash on a subset of the operands for
604     // performance reason. The subset has to be significant enough to avoid
605     // collision "most of the time". There is no correctness issue in case of
606     // collision because of the full check above.
607     return hash_combine(Name, Scope, File, Type, Line);
608   }
609 };
610
611 template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
612   typedef MDNodeKeyImpl<DISubprogram> KeyTy;
613   static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
614     return isDeclarationOfODRMember(LHS.IsDefinition, LHS.Scope,
615                                     LHS.LinkageName, RHS);
616   }
617   static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
618     return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
619                                     LHS->getRawLinkageName(), RHS);
620   }
621
622   /// Subprograms compare equal if they declare the same function in an ODR
623   /// type.
624   static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
625                                        const MDString *LinkageName,
626                                        const DISubprogram *RHS) {
627     // Check whether the LHS is eligible.
628     if (IsDefinition || !Scope || !LinkageName)
629       return false;
630
631     auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
632     if (!CT || !CT->getRawIdentifier())
633       return false;
634
635     // Compare to the RHS.
636     return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
637            LinkageName == RHS->getRawLinkageName();
638   }
639 };
640
641 template <> struct MDNodeKeyImpl<DILexicalBlock> {
642   Metadata *Scope;
643   Metadata *File;
644   unsigned Line;
645   unsigned Column;
646
647   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
648       : Scope(Scope), File(File), Line(Line), Column(Column) {}
649   MDNodeKeyImpl(const DILexicalBlock *N)
650       : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
651         Column(N->getColumn()) {}
652
653   bool isKeyOf(const DILexicalBlock *RHS) const {
654     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
655            Line == RHS->getLine() && Column == RHS->getColumn();
656   }
657   unsigned getHashValue() const {
658     return hash_combine(Scope, File, Line, Column);
659   }
660 };
661
662 template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
663   Metadata *Scope;
664   Metadata *File;
665   unsigned Discriminator;
666
667   MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
668       : Scope(Scope), File(File), Discriminator(Discriminator) {}
669   MDNodeKeyImpl(const DILexicalBlockFile *N)
670       : Scope(N->getRawScope()), File(N->getRawFile()),
671         Discriminator(N->getDiscriminator()) {}
672
673   bool isKeyOf(const DILexicalBlockFile *RHS) const {
674     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
675            Discriminator == RHS->getDiscriminator();
676   }
677   unsigned getHashValue() const {
678     return hash_combine(Scope, File, Discriminator);
679   }
680 };
681
682 template <> struct MDNodeKeyImpl<DINamespace> {
683   Metadata *Scope;
684   Metadata *File;
685   MDString *Name;
686   unsigned Line;
687   bool ExportSymbols;
688
689   MDNodeKeyImpl(Metadata *Scope, Metadata *File, MDString *Name, unsigned Line,
690                 bool ExportSymbols)
691       : Scope(Scope), File(File), Name(Name), Line(Line),
692         ExportSymbols(ExportSymbols) {}
693   MDNodeKeyImpl(const DINamespace *N)
694       : Scope(N->getRawScope()), File(N->getRawFile()), Name(N->getRawName()),
695         Line(N->getLine()), ExportSymbols(N->getExportSymbols()) {}
696
697   bool isKeyOf(const DINamespace *RHS) const {
698     return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
699            Name == RHS->getRawName() && Line == RHS->getLine() &&
700            ExportSymbols == RHS->getExportSymbols();
701   }
702   unsigned getHashValue() const {
703     return hash_combine(Scope, File, Name, Line);
704   }
705 };
706
707 template <> struct MDNodeKeyImpl<DIModule> {
708   Metadata *Scope;
709   MDString *Name;
710   MDString *ConfigurationMacros;
711   MDString *IncludePath;
712   MDString *ISysRoot;
713   MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
714                 MDString *IncludePath, MDString *ISysRoot)
715       : Scope(Scope), Name(Name), ConfigurationMacros(ConfigurationMacros),
716         IncludePath(IncludePath), ISysRoot(ISysRoot) {}
717   MDNodeKeyImpl(const DIModule *N)
718       : Scope(N->getRawScope()), Name(N->getRawName()),
719         ConfigurationMacros(N->getRawConfigurationMacros()),
720         IncludePath(N->getRawIncludePath()), ISysRoot(N->getRawISysRoot()) {}
721
722   bool isKeyOf(const DIModule *RHS) const {
723     return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
724            ConfigurationMacros == RHS->getRawConfigurationMacros() &&
725            IncludePath == RHS->getRawIncludePath() &&
726            ISysRoot == RHS->getRawISysRoot();
727   }
728   unsigned getHashValue() const {
729     return hash_combine(Scope, Name,
730                         ConfigurationMacros, IncludePath, ISysRoot);
731   }
732 };
733
734 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
735   MDString *Name;
736   Metadata *Type;
737
738   MDNodeKeyImpl(MDString *Name, Metadata *Type) : Name(Name), Type(Type) {}
739   MDNodeKeyImpl(const DITemplateTypeParameter *N)
740       : Name(N->getRawName()), Type(N->getRawType()) {}
741
742   bool isKeyOf(const DITemplateTypeParameter *RHS) const {
743     return Name == RHS->getRawName() && Type == RHS->getRawType();
744   }
745   unsigned getHashValue() const { return hash_combine(Name, Type); }
746 };
747
748 template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
749   unsigned Tag;
750   MDString *Name;
751   Metadata *Type;
752   Metadata *Value;
753
754   MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, Metadata *Value)
755       : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
756   MDNodeKeyImpl(const DITemplateValueParameter *N)
757       : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()),
758         Value(N->getValue()) {}
759
760   bool isKeyOf(const DITemplateValueParameter *RHS) const {
761     return Tag == RHS->getTag() && Name == RHS->getRawName() &&
762            Type == RHS->getRawType() && Value == RHS->getValue();
763   }
764   unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
765 };
766
767 template <> struct MDNodeKeyImpl<DIGlobalVariable> {
768   Metadata *Scope;
769   MDString *Name;
770   MDString *LinkageName;
771   Metadata *File;
772   unsigned Line;
773   Metadata *Type;
774   bool IsLocalToUnit;
775   bool IsDefinition;
776   Metadata *StaticDataMemberDeclaration;
777   uint32_t AlignInBits;
778
779   MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
780                 Metadata *File, unsigned Line, Metadata *Type,
781                 bool IsLocalToUnit, bool IsDefinition,
782                 Metadata *StaticDataMemberDeclaration, uint32_t AlignInBits)
783       : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
784         Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
785         IsDefinition(IsDefinition),
786         StaticDataMemberDeclaration(StaticDataMemberDeclaration),
787         AlignInBits(AlignInBits) {}
788   MDNodeKeyImpl(const DIGlobalVariable *N)
789       : Scope(N->getRawScope()), Name(N->getRawName()),
790         LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
791         Line(N->getLine()), Type(N->getRawType()),
792         IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
793         StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()),
794         AlignInBits(N->getAlignInBits()) {}
795
796   bool isKeyOf(const DIGlobalVariable *RHS) const {
797     return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
798            LinkageName == RHS->getRawLinkageName() &&
799            File == RHS->getRawFile() && Line == RHS->getLine() &&
800            Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
801            IsDefinition == RHS->isDefinition() &&
802            StaticDataMemberDeclaration ==
803                RHS->getRawStaticDataMemberDeclaration() &&
804            AlignInBits == RHS->getAlignInBits();
805   }
806   unsigned getHashValue() const {
807     // We do not use AlignInBits in hashing function here on purpose:
808     // in most cases this param for local variable is zero (for function param
809     // it is always zero). This leads to lots of hash collisions and errors on
810     // cases with lots of similar variables.
811     // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
812     // generated IR is random for each run and test fails with Align included.
813     // TODO: make hashing work fine with such situations
814     return hash_combine(Scope, Name, LinkageName, File, Line, Type,
815                         IsLocalToUnit, IsDefinition, /* AlignInBits, */
816                         StaticDataMemberDeclaration);
817   }
818 };
819
820 template <> struct MDNodeKeyImpl<DILocalVariable> {
821   Metadata *Scope;
822   MDString *Name;
823   Metadata *File;
824   unsigned Line;
825   Metadata *Type;
826   unsigned Arg;
827   unsigned Flags;
828   uint32_t AlignInBits;
829
830   MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line,
831                 Metadata *Type, unsigned Arg, unsigned Flags,
832                 uint32_t AlignInBits)
833       : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
834         Flags(Flags), AlignInBits(AlignInBits) {}
835   MDNodeKeyImpl(const DILocalVariable *N)
836       : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
837         Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
838         Flags(N->getFlags()), AlignInBits(N->getAlignInBits()) {}
839
840   bool isKeyOf(const DILocalVariable *RHS) const {
841     return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
842            File == RHS->getRawFile() && Line == RHS->getLine() &&
843            Type == RHS->getRawType() && Arg == RHS->getArg() &&
844            Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits();
845   }
846   unsigned getHashValue() const {
847     // We do not use AlignInBits in hashing function here on purpose:
848     // in most cases this param for local variable is zero (for function param
849     // it is always zero). This leads to lots of hash collisions and errors on
850     // cases with lots of similar variables.
851     // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
852     // generated IR is random for each run and test fails with Align included.
853     // TODO: make hashing work fine with such situations
854     return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
855   }
856 };
857
858 template <> struct MDNodeKeyImpl<DIExpression> {
859   ArrayRef<uint64_t> Elements;
860
861   MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
862   MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
863
864   bool isKeyOf(const DIExpression *RHS) const {
865     return Elements == RHS->getElements();
866   }
867   unsigned getHashValue() const {
868     return hash_combine_range(Elements.begin(), Elements.end());
869   }
870 };
871
872 template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> {
873   Metadata *Variable;
874   Metadata *Expression;
875
876   MDNodeKeyImpl(Metadata *Variable, Metadata *Expression)
877       : Variable(Variable), Expression(Expression) {}
878   MDNodeKeyImpl(const DIGlobalVariableExpression *N)
879       : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {}
880
881   bool isKeyOf(const DIGlobalVariableExpression *RHS) const {
882     return Variable == RHS->getRawVariable() &&
883            Expression == RHS->getRawExpression();
884   }
885   unsigned getHashValue() const { return hash_combine(Variable, Expression); }
886 };
887
888 template <> struct MDNodeKeyImpl<DIObjCProperty> {
889   MDString *Name;
890   Metadata *File;
891   unsigned Line;
892   MDString *GetterName;
893   MDString *SetterName;
894   unsigned Attributes;
895   Metadata *Type;
896
897   MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line,
898                 MDString *GetterName, MDString *SetterName, unsigned Attributes,
899                 Metadata *Type)
900       : Name(Name), File(File), Line(Line), GetterName(GetterName),
901         SetterName(SetterName), Attributes(Attributes), Type(Type) {}
902   MDNodeKeyImpl(const DIObjCProperty *N)
903       : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()),
904         GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()),
905         Attributes(N->getAttributes()), Type(N->getRawType()) {}
906
907   bool isKeyOf(const DIObjCProperty *RHS) const {
908     return Name == RHS->getRawName() && File == RHS->getRawFile() &&
909            Line == RHS->getLine() && GetterName == RHS->getRawGetterName() &&
910            SetterName == RHS->getRawSetterName() &&
911            Attributes == RHS->getAttributes() && Type == RHS->getRawType();
912   }
913   unsigned getHashValue() const {
914     return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
915                         Type);
916   }
917 };
918
919 template <> struct MDNodeKeyImpl<DIImportedEntity> {
920   unsigned Tag;
921   Metadata *Scope;
922   Metadata *Entity;
923   unsigned Line;
924   MDString *Name;
925
926   MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line,
927                 MDString *Name)
928       : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {}
929   MDNodeKeyImpl(const DIImportedEntity *N)
930       : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
931         Line(N->getLine()), Name(N->getRawName()) {}
932
933   bool isKeyOf(const DIImportedEntity *RHS) const {
934     return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
935            Entity == RHS->getRawEntity() && Line == RHS->getLine() &&
936            Name == RHS->getRawName();
937   }
938   unsigned getHashValue() const {
939     return hash_combine(Tag, Scope, Entity, Line, Name);
940   }
941 };
942
943 template <> struct MDNodeKeyImpl<DIMacro> {
944   unsigned MIType;
945   unsigned Line;
946   MDString *Name;
947   MDString *Value;
948
949   MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value)
950       : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
951   MDNodeKeyImpl(const DIMacro *N)
952       : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()),
953         Value(N->getRawValue()) {}
954
955   bool isKeyOf(const DIMacro *RHS) const {
956     return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
957            Name == RHS->getRawName() && Value == RHS->getRawValue();
958   }
959   unsigned getHashValue() const {
960     return hash_combine(MIType, Line, Name, Value);
961   }
962 };
963
964 template <> struct MDNodeKeyImpl<DIMacroFile> {
965   unsigned MIType;
966   unsigned Line;
967   Metadata *File;
968   Metadata *Elements;
969
970   MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
971                 Metadata *Elements)
972       : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
973   MDNodeKeyImpl(const DIMacroFile *N)
974       : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
975         Elements(N->getRawElements()) {}
976
977   bool isKeyOf(const DIMacroFile *RHS) const {
978     return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
979            File == RHS->getRawFile() && Elements == RHS->getRawElements();
980   }
981   unsigned getHashValue() const {
982     return hash_combine(MIType, Line, File, Elements);
983   }
984 };
985
986 /// \brief DenseMapInfo for MDNode subclasses.
987 template <class NodeTy> struct MDNodeInfo {
988   typedef MDNodeKeyImpl<NodeTy> KeyTy;
989   typedef MDNodeSubsetEqualImpl<NodeTy> SubsetEqualTy;
990   static inline NodeTy *getEmptyKey() {
991     return DenseMapInfo<NodeTy *>::getEmptyKey();
992   }
993   static inline NodeTy *getTombstoneKey() {
994     return DenseMapInfo<NodeTy *>::getTombstoneKey();
995   }
996   static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
997   static unsigned getHashValue(const NodeTy *N) {
998     return KeyTy(N).getHashValue();
999   }
1000   static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
1001     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1002       return false;
1003     return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS);
1004   }
1005   static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
1006     if (LHS == RHS)
1007       return true;
1008     if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1009       return false;
1010     return SubsetEqualTy::isSubsetEqual(LHS, RHS);
1011   }
1012 };
1013
1014 #define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo<CLASS> CLASS##Info;
1015 #include "llvm/IR/Metadata.def"
1016
1017 /// \brief Map-like storage for metadata attachments.
1018 class MDAttachmentMap {
1019   SmallVector<std::pair<unsigned, TrackingMDNodeRef>, 2> Attachments;
1020
1021 public:
1022   bool empty() const { return Attachments.empty(); }
1023   size_t size() const { return Attachments.size(); }
1024
1025   /// \brief Get a particular attachment (if any).
1026   MDNode *lookup(unsigned ID) const;
1027
1028   /// \brief Set an attachment to a particular node.
1029   ///
1030   /// Set the \c ID attachment to \c MD, replacing the current attachment at \c
1031   /// ID (if anyway).
1032   void set(unsigned ID, MDNode &MD);
1033
1034   /// \brief Remove an attachment.
1035   ///
1036   /// Remove the attachment at \c ID, if any.
1037   void erase(unsigned ID);
1038
1039   /// \brief Copy out all the attachments.
1040   ///
1041   /// Copies all the current attachments into \c Result, sorting by attachment
1042   /// ID.  This function does \em not clear \c Result.
1043   void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1044
1045   /// \brief Erase matching attachments.
1046   ///
1047   /// Erases all attachments matching the \c shouldRemove predicate.
1048   template <class PredTy> void remove_if(PredTy shouldRemove) {
1049     Attachments.erase(llvm::remove_if(Attachments, shouldRemove),
1050                       Attachments.end());
1051   }
1052 };
1053
1054 /// Multimap-like storage for metadata attachments for globals. This differs
1055 /// from MDAttachmentMap in that it allows multiple attachments per metadata
1056 /// kind.
1057 class MDGlobalAttachmentMap {
1058   struct Attachment {
1059     unsigned MDKind;
1060     TrackingMDNodeRef Node;
1061   };
1062   SmallVector<Attachment, 1> Attachments;
1063
1064 public:
1065   bool empty() const { return Attachments.empty(); }
1066
1067   /// Appends all attachments with the given ID to \c Result in insertion order.
1068   /// If the global has no attachments with the given ID, or if ID is invalid,
1069   /// leaves Result unchanged.
1070   void get(unsigned ID, SmallVectorImpl<MDNode *> &Result);
1071
1072   void insert(unsigned ID, MDNode &MD);
1073   void erase(unsigned ID);
1074
1075   /// Appends all attachments for the global to \c Result, sorting by attachment
1076   /// ID. Attachments with the same ID appear in insertion order. This function
1077   /// does \em not clear \c Result.
1078   void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1079 };
1080
1081 class LLVMContextImpl {
1082 public:
1083   /// OwnedModules - The set of modules instantiated in this context, and which
1084   /// will be automatically deleted if this context is deleted.
1085   SmallPtrSet<Module*, 4> OwnedModules;
1086   
1087   LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
1088   void *InlineAsmDiagContext;
1089
1090   LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
1091   void *DiagnosticContext;
1092   bool RespectDiagnosticFilters;
1093   bool DiagnosticHotnessRequested;
1094   std::unique_ptr<yaml::Output> DiagnosticsOutputFile;
1095
1096   LLVMContext::YieldCallbackTy YieldCallback;
1097   void *YieldOpaqueHandle;
1098
1099   typedef DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>
1100       IntMapTy;
1101   IntMapTy IntConstants;
1102
1103   typedef DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>
1104       FPMapTy;
1105   FPMapTy FPConstants;
1106
1107   FoldingSet<AttributeImpl> AttrsSet;
1108   FoldingSet<AttributeSetImpl> AttrsLists;
1109   FoldingSet<AttributeSetNode> AttrsSetNodes;
1110
1111   StringMap<MDString, BumpPtrAllocator> MDStringCache;
1112   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
1113   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
1114
1115   DenseMap<const Value*, ValueName*> ValueNames;
1116
1117 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
1118   DenseSet<CLASS *, CLASS##Info> CLASS##s;
1119 #include "llvm/IR/Metadata.def"
1120
1121   // Optional map for looking up composite types by identifier.
1122   Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap;
1123
1124   // MDNodes may be uniqued or not uniqued.  When they're not uniqued, they
1125   // aren't in the MDNodeSet, but they're still shared between objects, so no
1126   // one object can destroy them.  Keep track of them here so we can delete
1127   // them on context teardown.
1128   std::vector<MDNode *> DistinctMDNodes;
1129
1130   DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants;
1131
1132   typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
1133   ArrayConstantsTy ArrayConstants;
1134   
1135   typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
1136   StructConstantsTy StructConstants;
1137   
1138   typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
1139   VectorConstantsTy VectorConstants;
1140
1141   DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants;
1142
1143   DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants;
1144
1145   StringMap<ConstantDataSequential*> CDSConstants;
1146
1147   DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
1148     BlockAddresses;
1149   ConstantUniqueMap<ConstantExpr> ExprConstants;
1150
1151   ConstantUniqueMap<InlineAsm> InlineAsms;
1152
1153   ConstantInt *TheTrueVal;
1154   ConstantInt *TheFalseVal;
1155
1156   std::unique_ptr<ConstantTokenNone> TheNoneToken;
1157
1158   // Basic type instances.
1159   Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy, TokenTy;
1160   Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
1161   IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
1162
1163   
1164   /// TypeAllocator - All dynamically allocated types are allocated from this.
1165   /// They live forever until the context is torn down.
1166   BumpPtrAllocator TypeAllocator;
1167   
1168   DenseMap<unsigned, IntegerType*> IntegerTypes;
1169
1170   typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
1171   FunctionTypeSet FunctionTypes;
1172   typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
1173   StructTypeSet AnonStructTypes;
1174   StringMap<StructType*> NamedStructTypes;
1175   unsigned NamedStructTypesUniqueID;
1176     
1177   DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
1178   DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
1179   DenseMap<Type*, PointerType*> PointerTypes;  // Pointers in AddrSpace = 0
1180   DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
1181
1182
1183   /// ValueHandles - This map keeps track of all of the value handles that are
1184   /// watching a Value*.  The Value::HasValueHandle bit is used to know
1185   /// whether or not a value has an entry in this map.
1186   typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
1187   ValueHandlesTy ValueHandles;
1188   
1189   /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
1190   StringMap<unsigned> CustomMDKindNames;
1191
1192   /// Collection of per-instruction metadata used in this context.
1193   DenseMap<const Instruction *, MDAttachmentMap> InstructionMetadata;
1194
1195   /// Collection of per-GlobalObject metadata used in this context.
1196   DenseMap<const GlobalObject *, MDGlobalAttachmentMap> GlobalObjectMetadata;
1197
1198   /// Collection of per-GlobalObject sections used in this context.
1199   DenseMap<const GlobalObject *, StringRef> GlobalObjectSections;
1200
1201   /// Stable collection of section strings.
1202   StringSet<> SectionStrings;
1203
1204   /// DiscriminatorTable - This table maps file:line locations to an
1205   /// integer representing the next DWARF path discriminator to assign to
1206   /// instructions in different blocks at the same location.
1207   DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
1208
1209   int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
1210   int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
1211
1212   /// \brief A set of interned tags for operand bundles.  The StringMap maps
1213   /// bundle tags to their IDs.
1214   ///
1215   /// \see LLVMContext::getOperandBundleTagID
1216   StringMap<uint32_t> BundleTagCache;
1217
1218   StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
1219   void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
1220   uint32_t getOperandBundleTagID(StringRef Tag) const;
1221
1222   /// Maintain the GC name for each function.
1223   ///
1224   /// This saves allocating an additional word in Function for programs which
1225   /// do not use GC (i.e., most programs) at the cost of increased overhead for
1226   /// clients which do use GC.
1227   DenseMap<const Function*, std::string> GCNames;
1228
1229   /// Flag to indicate if Value (other than GlobalValue) retains their name or
1230   /// not.
1231   bool DiscardValueNames = false;
1232
1233   LLVMContextImpl(LLVMContext &C);
1234   ~LLVMContextImpl();
1235
1236   /// Destroy the ConstantArrays if they are not used.
1237   void dropTriviallyDeadConstantArrays();
1238
1239   /// \brief Access the object which manages optimization bisection for failure
1240   /// analysis.
1241   OptBisect &getOptBisect();
1242 };
1243
1244 }
1245
1246 #endif