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