1 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
16 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
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"
42 class DiagnosticInfoOptimizationRemark;
43 class DiagnosticInfoOptimizationRemarkMissed;
44 class DiagnosticInfoOptimizationRemarkAnalysis;
50 struct DenseMapAPIntKeyInfo {
51 static inline APInt getEmptyKey() {
56 static inline APInt getTombstoneKey() {
61 static unsigned getHashValue(const APInt &Key) {
62 return static_cast<unsigned>(hash_value(Key));
64 static bool isEqual(const APInt &LHS, const APInt &RHS) {
65 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
69 struct DenseMapAPFloatKeyInfo {
70 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
71 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
72 static unsigned getHashValue(const APFloat &Key) {
73 return static_cast<unsigned>(hash_value(Key));
75 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
76 return LHS.bitwiseIsEqual(RHS);
80 struct AnonStructTypeKeyInfo {
82 ArrayRef<Type*> ETypes;
84 KeyTy(const ArrayRef<Type*>& E, bool P) :
85 ETypes(E), isPacked(P) {}
86 KeyTy(const StructType *ST)
87 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
88 bool operator==(const KeyTy& that) const {
89 if (isPacked != that.isPacked)
91 if (ETypes != that.ETypes)
95 bool operator!=(const KeyTy& that) const {
96 return !this->operator==(that);
99 static inline StructType* getEmptyKey() {
100 return DenseMapInfo<StructType*>::getEmptyKey();
102 static inline StructType* getTombstoneKey() {
103 return DenseMapInfo<StructType*>::getTombstoneKey();
105 static unsigned getHashValue(const KeyTy& Key) {
106 return hash_combine(hash_combine_range(Key.ETypes.begin(),
110 static unsigned getHashValue(const StructType *ST) {
111 return getHashValue(KeyTy(ST));
113 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
114 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
116 return LHS == KeyTy(RHS);
118 static bool isEqual(const StructType *LHS, const StructType *RHS) {
123 struct FunctionTypeKeyInfo {
125 const Type *ReturnType;
126 ArrayRef<Type*> Params;
128 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
129 ReturnType(R), Params(P), isVarArg(V) {}
130 KeyTy(const FunctionType *FT)
131 : ReturnType(FT->getReturnType()), Params(FT->params()),
132 isVarArg(FT->isVarArg()) {}
133 bool operator==(const KeyTy& that) const {
134 if (ReturnType != that.ReturnType)
136 if (isVarArg != that.isVarArg)
138 if (Params != that.Params)
142 bool operator!=(const KeyTy& that) const {
143 return !this->operator==(that);
146 static inline FunctionType* getEmptyKey() {
147 return DenseMapInfo<FunctionType*>::getEmptyKey();
149 static inline FunctionType* getTombstoneKey() {
150 return DenseMapInfo<FunctionType*>::getTombstoneKey();
152 static unsigned getHashValue(const KeyTy& Key) {
153 return hash_combine(Key.ReturnType,
154 hash_combine_range(Key.Params.begin(),
158 static unsigned getHashValue(const FunctionType *FT) {
159 return getHashValue(KeyTy(FT));
161 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
162 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
164 return LHS == KeyTy(RHS);
166 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
171 /// \brief Structure for hashing arbitrary MDNode operands.
173 ArrayRef<Metadata *> RawOps;
174 ArrayRef<MDOperand> Ops;
179 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
180 : RawOps(Ops), Hash(calculateHash(Ops)) {}
182 template <class NodeTy>
183 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
184 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
186 template <class NodeTy>
187 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
188 if (getHash() != RHS->getHash())
191 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
192 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
193 : compareOps(RawOps, RHS, Offset);
196 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
200 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
201 if (Ops.size() != RHS->getNumOperands() - Offset)
203 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
206 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
209 unsigned getHash() const { return Hash; }
212 template <class NodeTy> struct MDNodeKeyImpl;
213 template <class NodeTy> struct MDNodeInfo;
215 /// Configuration point for MDNodeInfo::isEqual().
216 template <class NodeTy> struct MDNodeSubsetEqualImpl {
217 typedef MDNodeKeyImpl<NodeTy> KeyTy;
218 static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) {
221 static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) {
226 /// \brief DenseMapInfo for MDTuple.
228 /// Note that we don't need the is-function-local bit, since that's implicit in
230 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
231 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
232 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
234 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
236 unsigned getHashValue() const { return getHash(); }
238 static unsigned calculateHash(MDTuple *N) {
239 return MDNodeOpsKey::calculateHash(N);
243 /// \brief DenseMapInfo for DILocation.
244 template <> struct MDNodeKeyImpl<DILocation> {
250 MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
252 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
254 MDNodeKeyImpl(const DILocation *L)
255 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
256 InlinedAt(L->getRawInlinedAt()) {}
258 bool isKeyOf(const DILocation *RHS) const {
259 return Line == RHS->getLine() && Column == RHS->getColumn() &&
260 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt();
262 unsigned getHashValue() const {
263 return hash_combine(Line, Column, Scope, InlinedAt);
267 /// \brief DenseMapInfo for GenericDINode.
268 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
271 MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
272 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
273 MDNodeKeyImpl(const GenericDINode *N)
274 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {}
276 bool isKeyOf(const GenericDINode *RHS) const {
277 return Tag == RHS->getTag() && Header == RHS->getRawHeader() &&
281 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
283 static unsigned calculateHash(GenericDINode *N) {
284 return MDNodeOpsKey::calculateHash(N, 1);
288 template <> struct MDNodeKeyImpl<DISubrange> {
292 MDNodeKeyImpl(int64_t Count, int64_t LowerBound)
293 : Count(Count), LowerBound(LowerBound) {}
294 MDNodeKeyImpl(const DISubrange *N)
295 : Count(N->getCount()), LowerBound(N->getLowerBound()) {}
297 bool isKeyOf(const DISubrange *RHS) const {
298 return Count == RHS->getCount() && LowerBound == RHS->getLowerBound();
300 unsigned getHashValue() const { return hash_combine(Count, LowerBound); }
303 template <> struct MDNodeKeyImpl<DIEnumerator> {
307 MDNodeKeyImpl(int64_t Value, MDString *Name) : Value(Value), Name(Name) {}
308 MDNodeKeyImpl(const DIEnumerator *N)
309 : Value(N->getValue()), Name(N->getRawName()) {}
311 bool isKeyOf(const DIEnumerator *RHS) const {
312 return Value == RHS->getValue() && Name == RHS->getRawName();
314 unsigned getHashValue() const { return hash_combine(Value, Name); }
317 template <> struct MDNodeKeyImpl<DIBasicType> {
321 uint64_t AlignInBits;
324 MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
325 uint64_t AlignInBits, unsigned Encoding)
326 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
327 Encoding(Encoding) {}
328 MDNodeKeyImpl(const DIBasicType *N)
329 : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
330 AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
332 bool isKeyOf(const DIBasicType *RHS) const {
333 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
334 SizeInBits == RHS->getSizeInBits() &&
335 AlignInBits == RHS->getAlignInBits() &&
336 Encoding == RHS->getEncoding();
338 unsigned getHashValue() const {
339 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
343 template <> struct MDNodeKeyImpl<DIDerivedType> {
351 uint64_t AlignInBits;
352 uint64_t OffsetInBits;
356 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
357 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
358 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
360 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
361 BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
362 OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {}
363 MDNodeKeyImpl(const DIDerivedType *N)
364 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
365 Line(N->getLine()), Scope(N->getRawScope()),
366 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
367 AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
368 Flags(N->getFlags()), ExtraData(N->getRawExtraData()) {}
370 bool isKeyOf(const DIDerivedType *RHS) const {
371 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
372 File == RHS->getRawFile() && Line == RHS->getLine() &&
373 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
374 SizeInBits == RHS->getSizeInBits() &&
375 AlignInBits == RHS->getAlignInBits() &&
376 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
377 ExtraData == RHS->getRawExtraData();
379 unsigned getHashValue() const {
380 // If this is a member inside an ODR type, only hash the type and the name.
381 // Otherwise the hash will be stronger than
382 // MDNodeSubsetEqualImpl::isODRMember().
383 if (Tag == dwarf::DW_TAG_member && Name)
384 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
385 if (CT->getRawIdentifier())
386 return hash_combine(Name, Scope);
388 // Intentionally computes the hash on a subset of the operands for
389 // performance reason. The subset has to be significant enough to avoid
390 // collision "most of the time". There is no correctness issue in case of
391 // collision because of the full check above.
392 return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags);
396 template <> struct MDNodeSubsetEqualImpl<DIDerivedType> {
397 typedef MDNodeKeyImpl<DIDerivedType> KeyTy;
398 static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) {
399 return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS);
401 static bool isSubsetEqual(const DIDerivedType *LHS, const DIDerivedType *RHS) {
402 return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(),
406 /// Subprograms compare equal if they declare the same function in an ODR
408 static bool isODRMember(unsigned Tag, const Metadata *Scope,
409 const MDString *Name, const DIDerivedType *RHS) {
410 // Check whether the LHS is eligible.
411 if (Tag != dwarf::DW_TAG_member || !Name)
414 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
415 if (!CT || !CT->getRawIdentifier())
418 // Compare to the RHS.
419 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
420 Scope == RHS->getRawScope();
424 template <> struct MDNodeKeyImpl<DICompositeType> {
432 uint64_t AlignInBits;
433 uint64_t OffsetInBits;
436 unsigned RuntimeLang;
437 Metadata *VTableHolder;
438 Metadata *TemplateParams;
439 MDString *Identifier;
441 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
442 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
443 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
444 Metadata *Elements, unsigned RuntimeLang,
445 Metadata *VTableHolder, Metadata *TemplateParams,
446 MDString *Identifier)
447 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
448 BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
449 OffsetInBits(OffsetInBits), Flags(Flags), Elements(Elements),
450 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
451 TemplateParams(TemplateParams), Identifier(Identifier) {}
452 MDNodeKeyImpl(const DICompositeType *N)
453 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
454 Line(N->getLine()), Scope(N->getRawScope()),
455 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
456 AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
457 Flags(N->getFlags()), Elements(N->getRawElements()),
458 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
459 TemplateParams(N->getRawTemplateParams()),
460 Identifier(N->getRawIdentifier()) {}
462 bool isKeyOf(const DICompositeType *RHS) const {
463 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
464 File == RHS->getRawFile() && Line == RHS->getLine() &&
465 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
466 SizeInBits == RHS->getSizeInBits() &&
467 AlignInBits == RHS->getAlignInBits() &&
468 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
469 Elements == RHS->getRawElements() &&
470 RuntimeLang == RHS->getRuntimeLang() &&
471 VTableHolder == RHS->getRawVTableHolder() &&
472 TemplateParams == RHS->getRawTemplateParams() &&
473 Identifier == RHS->getRawIdentifier();
475 unsigned getHashValue() const {
476 // Intentionally computes the hash on a subset of the operands for
477 // performance reason. The subset has to be significant enough to avoid
478 // collision "most of the time". There is no correctness issue in case of
479 // collision because of the full check above.
480 return hash_combine(Name, File, Line, BaseType, Scope, Elements,
485 template <> struct MDNodeKeyImpl<DISubroutineType> {
490 MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray)
491 : Flags(Flags), CC(CC), TypeArray(TypeArray) {}
492 MDNodeKeyImpl(const DISubroutineType *N)
493 : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {}
495 bool isKeyOf(const DISubroutineType *RHS) const {
496 return Flags == RHS->getFlags() && CC == RHS->getCC() &&
497 TypeArray == RHS->getRawTypeArray();
499 unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); }
502 template <> struct MDNodeKeyImpl<DIFile> {
506 MDNodeKeyImpl(MDString *Filename, MDString *Directory)
507 : Filename(Filename), Directory(Directory) {}
508 MDNodeKeyImpl(const DIFile *N)
509 : Filename(N->getRawFilename()), Directory(N->getRawDirectory()) {}
511 bool isKeyOf(const DIFile *RHS) const {
512 return Filename == RHS->getRawFilename() &&
513 Directory == RHS->getRawDirectory();
515 unsigned getHashValue() const { return hash_combine(Filename, Directory); }
518 template <> struct MDNodeKeyImpl<DISubprogram> {
521 MDString *LinkageName;
528 Metadata *ContainingType;
530 unsigned VirtualIndex;
535 Metadata *TemplateParams;
536 Metadata *Declaration;
539 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
540 Metadata *File, unsigned Line, Metadata *Type,
541 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
542 Metadata *ContainingType, unsigned Virtuality,
543 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags,
544 bool IsOptimized, Metadata *Unit, Metadata *TemplateParams,
545 Metadata *Declaration, Metadata *Variables)
546 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
547 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
548 IsDefinition(IsDefinition), ScopeLine(ScopeLine),
549 ContainingType(ContainingType), Virtuality(Virtuality),
550 VirtualIndex(VirtualIndex), ThisAdjustment(ThisAdjustment),
551 Flags(Flags), IsOptimized(IsOptimized), Unit(Unit),
552 TemplateParams(TemplateParams), Declaration(Declaration),
553 Variables(Variables) {}
554 MDNodeKeyImpl(const DISubprogram *N)
555 : Scope(N->getRawScope()), Name(N->getRawName()),
556 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
557 Line(N->getLine()), Type(N->getRawType()),
558 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
559 ScopeLine(N->getScopeLine()), ContainingType(N->getRawContainingType()),
560 Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()),
561 ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
562 IsOptimized(N->isOptimized()), Unit(N->getRawUnit()),
563 TemplateParams(N->getRawTemplateParams()),
564 Declaration(N->getRawDeclaration()), Variables(N->getRawVariables()) {}
566 bool isKeyOf(const DISubprogram *RHS) const {
567 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
568 LinkageName == RHS->getRawLinkageName() &&
569 File == RHS->getRawFile() && Line == RHS->getLine() &&
570 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
571 IsDefinition == RHS->isDefinition() &&
572 ScopeLine == RHS->getScopeLine() &&
573 ContainingType == RHS->getRawContainingType() &&
574 Virtuality == RHS->getVirtuality() &&
575 VirtualIndex == RHS->getVirtualIndex() &&
576 ThisAdjustment == RHS->getThisAdjustment() &&
577 Flags == RHS->getFlags() && IsOptimized == RHS->isOptimized() &&
578 Unit == RHS->getUnit() &&
579 TemplateParams == RHS->getRawTemplateParams() &&
580 Declaration == RHS->getRawDeclaration() &&
581 Variables == RHS->getRawVariables();
583 unsigned getHashValue() const {
584 // If this is a declaration inside an ODR type, only hash the type and the
585 // name. Otherwise the hash will be stronger than
586 // MDNodeSubsetEqualImpl::isDeclarationOfODRMember().
587 if (!IsDefinition && LinkageName)
588 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
589 if (CT->getRawIdentifier())
590 return hash_combine(LinkageName, Scope);
592 // Intentionally computes the hash on a subset of the operands for
593 // performance reason. The subset has to be significant enough to avoid
594 // collision "most of the time". There is no correctness issue in case of
595 // collision because of the full check above.
596 return hash_combine(Name, Scope, File, Type, Line);
600 template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
601 typedef MDNodeKeyImpl<DISubprogram> KeyTy;
602 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
603 return isDeclarationOfODRMember(LHS.IsDefinition, LHS.Scope,
604 LHS.LinkageName, RHS);
606 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
607 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
608 LHS->getRawLinkageName(), RHS);
611 /// Subprograms compare equal if they declare the same function in an ODR
613 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
614 const MDString *LinkageName,
615 const DISubprogram *RHS) {
616 // Check whether the LHS is eligible.
617 if (IsDefinition || !Scope || !LinkageName)
620 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
621 if (!CT || !CT->getRawIdentifier())
624 // Compare to the RHS.
625 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
626 LinkageName == RHS->getRawLinkageName();
630 template <> struct MDNodeKeyImpl<DILexicalBlock> {
636 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
637 : Scope(Scope), File(File), Line(Line), Column(Column) {}
638 MDNodeKeyImpl(const DILexicalBlock *N)
639 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
640 Column(N->getColumn()) {}
642 bool isKeyOf(const DILexicalBlock *RHS) const {
643 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
644 Line == RHS->getLine() && Column == RHS->getColumn();
646 unsigned getHashValue() const {
647 return hash_combine(Scope, File, Line, Column);
651 template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
654 unsigned Discriminator;
656 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
657 : Scope(Scope), File(File), Discriminator(Discriminator) {}
658 MDNodeKeyImpl(const DILexicalBlockFile *N)
659 : Scope(N->getRawScope()), File(N->getRawFile()),
660 Discriminator(N->getDiscriminator()) {}
662 bool isKeyOf(const DILexicalBlockFile *RHS) const {
663 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
664 Discriminator == RHS->getDiscriminator();
666 unsigned getHashValue() const {
667 return hash_combine(Scope, File, Discriminator);
671 template <> struct MDNodeKeyImpl<DINamespace> {
677 MDNodeKeyImpl(Metadata *Scope, Metadata *File, MDString *Name, unsigned Line)
678 : Scope(Scope), File(File), Name(Name), Line(Line) {}
679 MDNodeKeyImpl(const DINamespace *N)
680 : Scope(N->getRawScope()), File(N->getRawFile()), Name(N->getRawName()),
681 Line(N->getLine()) {}
683 bool isKeyOf(const DINamespace *RHS) const {
684 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
685 Name == RHS->getRawName() && Line == RHS->getLine();
687 unsigned getHashValue() const {
688 return hash_combine(Scope, File, Name, Line);
692 template <> struct MDNodeKeyImpl<DIModule> {
695 MDString *ConfigurationMacros;
696 MDString *IncludePath;
698 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *ConfigurationMacros,
699 MDString *IncludePath, MDString *ISysRoot)
700 : Scope(Scope), Name(Name), ConfigurationMacros(ConfigurationMacros),
701 IncludePath(IncludePath), ISysRoot(ISysRoot) {}
702 MDNodeKeyImpl(const DIModule *N)
703 : Scope(N->getRawScope()), Name(N->getRawName()),
704 ConfigurationMacros(N->getRawConfigurationMacros()),
705 IncludePath(N->getRawIncludePath()), ISysRoot(N->getRawISysRoot()) {}
707 bool isKeyOf(const DIModule *RHS) const {
708 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
709 ConfigurationMacros == RHS->getRawConfigurationMacros() &&
710 IncludePath == RHS->getRawIncludePath() &&
711 ISysRoot == RHS->getRawISysRoot();
713 unsigned getHashValue() const {
714 return hash_combine(Scope, Name,
715 ConfigurationMacros, IncludePath, ISysRoot);
719 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
723 MDNodeKeyImpl(MDString *Name, Metadata *Type) : Name(Name), Type(Type) {}
724 MDNodeKeyImpl(const DITemplateTypeParameter *N)
725 : Name(N->getRawName()), Type(N->getRawType()) {}
727 bool isKeyOf(const DITemplateTypeParameter *RHS) const {
728 return Name == RHS->getRawName() && Type == RHS->getRawType();
730 unsigned getHashValue() const { return hash_combine(Name, Type); }
733 template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
739 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, Metadata *Value)
740 : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
741 MDNodeKeyImpl(const DITemplateValueParameter *N)
742 : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()),
743 Value(N->getValue()) {}
745 bool isKeyOf(const DITemplateValueParameter *RHS) const {
746 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
747 Type == RHS->getRawType() && Value == RHS->getValue();
749 unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
752 template <> struct MDNodeKeyImpl<DIGlobalVariable> {
755 MDString *LinkageName;
762 Metadata *StaticDataMemberDeclaration;
764 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
765 Metadata *File, unsigned Line, Metadata *Type,
766 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
767 Metadata *StaticDataMemberDeclaration)
768 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
769 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
770 IsDefinition(IsDefinition), Variable(Variable),
771 StaticDataMemberDeclaration(StaticDataMemberDeclaration) {}
772 MDNodeKeyImpl(const DIGlobalVariable *N)
773 : Scope(N->getRawScope()), Name(N->getRawName()),
774 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
775 Line(N->getLine()), Type(N->getRawType()),
776 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
777 Variable(N->getRawVariable()),
778 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()) {}
780 bool isKeyOf(const DIGlobalVariable *RHS) const {
781 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
782 LinkageName == RHS->getRawLinkageName() &&
783 File == RHS->getRawFile() && Line == RHS->getLine() &&
784 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
785 IsDefinition == RHS->isDefinition() &&
786 Variable == RHS->getRawVariable() &&
787 StaticDataMemberDeclaration ==
788 RHS->getRawStaticDataMemberDeclaration();
790 unsigned getHashValue() const {
791 return hash_combine(Scope, Name, LinkageName, File, Line, Type,
792 IsLocalToUnit, IsDefinition, Variable,
793 StaticDataMemberDeclaration);
797 template <> struct MDNodeKeyImpl<DILocalVariable> {
806 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line,
807 Metadata *Type, unsigned Arg, unsigned Flags)
808 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
810 MDNodeKeyImpl(const DILocalVariable *N)
811 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
812 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
813 Flags(N->getFlags()) {}
815 bool isKeyOf(const DILocalVariable *RHS) const {
816 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
817 File == RHS->getRawFile() && Line == RHS->getLine() &&
818 Type == RHS->getRawType() && Arg == RHS->getArg() &&
819 Flags == RHS->getFlags();
821 unsigned getHashValue() const {
822 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
826 template <> struct MDNodeKeyImpl<DIExpression> {
827 ArrayRef<uint64_t> Elements;
829 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
830 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
832 bool isKeyOf(const DIExpression *RHS) const {
833 return Elements == RHS->getElements();
835 unsigned getHashValue() const {
836 return hash_combine_range(Elements.begin(), Elements.end());
840 template <> struct MDNodeKeyImpl<DIObjCProperty> {
844 MDString *GetterName;
845 MDString *SetterName;
849 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line,
850 MDString *GetterName, MDString *SetterName, unsigned Attributes,
852 : Name(Name), File(File), Line(Line), GetterName(GetterName),
853 SetterName(SetterName), Attributes(Attributes), Type(Type) {}
854 MDNodeKeyImpl(const DIObjCProperty *N)
855 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()),
856 GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()),
857 Attributes(N->getAttributes()), Type(N->getRawType()) {}
859 bool isKeyOf(const DIObjCProperty *RHS) const {
860 return Name == RHS->getRawName() && File == RHS->getRawFile() &&
861 Line == RHS->getLine() && GetterName == RHS->getRawGetterName() &&
862 SetterName == RHS->getRawSetterName() &&
863 Attributes == RHS->getAttributes() && Type == RHS->getRawType();
865 unsigned getHashValue() const {
866 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
871 template <> struct MDNodeKeyImpl<DIImportedEntity> {
878 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line,
880 : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {}
881 MDNodeKeyImpl(const DIImportedEntity *N)
882 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
883 Line(N->getLine()), Name(N->getRawName()) {}
885 bool isKeyOf(const DIImportedEntity *RHS) const {
886 return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
887 Entity == RHS->getRawEntity() && Line == RHS->getLine() &&
888 Name == RHS->getRawName();
890 unsigned getHashValue() const {
891 return hash_combine(Tag, Scope, Entity, Line, Name);
895 template <> struct MDNodeKeyImpl<DIMacro> {
901 MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value)
902 : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
903 MDNodeKeyImpl(const DIMacro *N)
904 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()),
905 Value(N->getRawValue()) {}
907 bool isKeyOf(const DIMacro *RHS) const {
908 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
909 Name == RHS->getRawName() && Value == RHS->getRawValue();
911 unsigned getHashValue() const {
912 return hash_combine(MIType, Line, Name, Value);
916 template <> struct MDNodeKeyImpl<DIMacroFile> {
922 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
924 : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
925 MDNodeKeyImpl(const DIMacroFile *N)
926 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
927 Elements(N->getRawElements()) {}
929 bool isKeyOf(const DIMacroFile *RHS) const {
930 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
931 File == RHS->getRawFile() && File == RHS->getRawElements();
933 unsigned getHashValue() const {
934 return hash_combine(MIType, Line, File, Elements);
938 /// \brief DenseMapInfo for MDNode subclasses.
939 template <class NodeTy> struct MDNodeInfo {
940 typedef MDNodeKeyImpl<NodeTy> KeyTy;
941 typedef MDNodeSubsetEqualImpl<NodeTy> SubsetEqualTy;
942 static inline NodeTy *getEmptyKey() {
943 return DenseMapInfo<NodeTy *>::getEmptyKey();
945 static inline NodeTy *getTombstoneKey() {
946 return DenseMapInfo<NodeTy *>::getTombstoneKey();
948 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
949 static unsigned getHashValue(const NodeTy *N) {
950 return KeyTy(N).getHashValue();
952 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
953 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
955 return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS);
957 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
960 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
962 return SubsetEqualTy::isSubsetEqual(LHS, RHS);
966 #define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo<CLASS> CLASS##Info;
967 #include "llvm/IR/Metadata.def"
969 /// \brief Map-like storage for metadata attachments.
970 class MDAttachmentMap {
971 SmallVector<std::pair<unsigned, TrackingMDNodeRef>, 2> Attachments;
974 bool empty() const { return Attachments.empty(); }
975 size_t size() const { return Attachments.size(); }
977 /// \brief Get a particular attachment (if any).
978 MDNode *lookup(unsigned ID) const;
980 /// \brief Set an attachment to a particular node.
982 /// Set the \c ID attachment to \c MD, replacing the current attachment at \c
984 void set(unsigned ID, MDNode &MD);
986 /// \brief Remove an attachment.
988 /// Remove the attachment at \c ID, if any.
989 void erase(unsigned ID);
991 /// \brief Copy out all the attachments.
993 /// Copies all the current attachments into \c Result, sorting by attachment
994 /// ID. This function does \em not clear \c Result.
995 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
997 /// \brief Erase matching attachments.
999 /// Erases all attachments matching the \c shouldRemove predicate.
1000 template <class PredTy> void remove_if(PredTy shouldRemove) {
1002 std::remove_if(Attachments.begin(), Attachments.end(), shouldRemove),
1007 /// Multimap-like storage for metadata attachments for globals. This differs
1008 /// from MDAttachmentMap in that it allows multiple attachments per metadata
1010 class MDGlobalAttachmentMap {
1013 TrackingMDNodeRef Node;
1015 SmallVector<Attachment, 1> Attachments;
1018 bool empty() const { return Attachments.empty(); }
1020 /// Appends all attachments with the given ID to \c Result in insertion order.
1021 /// If the global has no attachments with the given ID, or if ID is invalid,
1022 /// leaves Result unchanged.
1023 void get(unsigned ID, SmallVectorImpl<MDNode *> &Result);
1025 void insert(unsigned ID, MDNode &MD);
1026 void erase(unsigned ID);
1028 /// Appends all attachments for the global to \c Result, sorting by attachment
1029 /// ID. Attachments with the same ID appear in insertion order. This function
1030 /// does \em not clear \c Result.
1031 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1034 class LLVMContextImpl {
1036 /// OwnedModules - The set of modules instantiated in this context, and which
1037 /// will be automatically deleted if this context is deleted.
1038 SmallPtrSet<Module*, 4> OwnedModules;
1040 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
1041 void *InlineAsmDiagContext;
1043 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
1044 void *DiagnosticContext;
1045 bool RespectDiagnosticFilters;
1046 bool DiagnosticHotnessRequested;
1048 LLVMContext::YieldCallbackTy YieldCallback;
1049 void *YieldOpaqueHandle;
1051 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
1052 IntMapTy IntConstants;
1054 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
1055 FPMapTy FPConstants;
1057 FoldingSet<AttributeImpl> AttrsSet;
1058 FoldingSet<AttributeSetImpl> AttrsLists;
1059 FoldingSet<AttributeSetNode> AttrsSetNodes;
1061 StringMap<MDString, BumpPtrAllocator> MDStringCache;
1062 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
1063 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
1065 DenseMap<const Value*, ValueName*> ValueNames;
1067 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
1068 DenseSet<CLASS *, CLASS##Info> CLASS##s;
1069 #include "llvm/IR/Metadata.def"
1071 // Optional map for looking up composite types by identifier.
1072 Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap;
1074 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
1075 // aren't in the MDNodeSet, but they're still shared between objects, so no
1076 // one object can destroy them. Keep track of them here so we can delete
1077 // them on context teardown.
1078 std::vector<MDNode *> DistinctMDNodes;
1080 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
1082 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
1083 ArrayConstantsTy ArrayConstants;
1085 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
1086 StructConstantsTy StructConstants;
1088 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
1089 VectorConstantsTy VectorConstants;
1091 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
1093 DenseMap<Type*, UndefValue*> UVConstants;
1095 StringMap<ConstantDataSequential*> CDSConstants;
1097 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
1099 ConstantUniqueMap<ConstantExpr> ExprConstants;
1101 ConstantUniqueMap<InlineAsm> InlineAsms;
1103 ConstantInt *TheTrueVal;
1104 ConstantInt *TheFalseVal;
1106 std::unique_ptr<ConstantTokenNone> TheNoneToken;
1108 // Basic type instances.
1109 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy, TokenTy;
1110 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
1111 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
1114 /// TypeAllocator - All dynamically allocated types are allocated from this.
1115 /// They live forever until the context is torn down.
1116 BumpPtrAllocator TypeAllocator;
1118 DenseMap<unsigned, IntegerType*> IntegerTypes;
1120 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
1121 FunctionTypeSet FunctionTypes;
1122 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
1123 StructTypeSet AnonStructTypes;
1124 StringMap<StructType*> NamedStructTypes;
1125 unsigned NamedStructTypesUniqueID;
1127 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
1128 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
1129 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
1130 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
1133 /// ValueHandles - This map keeps track of all of the value handles that are
1134 /// watching a Value*. The Value::HasValueHandle bit is used to know
1135 /// whether or not a value has an entry in this map.
1136 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
1137 ValueHandlesTy ValueHandles;
1139 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
1140 StringMap<unsigned> CustomMDKindNames;
1142 /// Collection of per-instruction metadata used in this context.
1143 DenseMap<const Instruction *, MDAttachmentMap> InstructionMetadata;
1145 /// Collection of per-GlobalObject metadata used in this context.
1146 DenseMap<const GlobalObject *, MDGlobalAttachmentMap> GlobalObjectMetadata;
1148 /// DiscriminatorTable - This table maps file:line locations to an
1149 /// integer representing the next DWARF path discriminator to assign to
1150 /// instructions in different blocks at the same location.
1151 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
1153 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
1154 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
1156 /// \brief A set of interned tags for operand bundles. The StringMap maps
1157 /// bundle tags to their IDs.
1159 /// \see LLVMContext::getOperandBundleTagID
1160 StringMap<uint32_t> BundleTagCache;
1162 StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
1163 void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
1164 uint32_t getOperandBundleTagID(StringRef Tag) const;
1166 /// Maintain the GC name for each function.
1168 /// This saves allocating an additional word in Function for programs which
1169 /// do not use GC (i.e., most programs) at the cost of increased overhead for
1170 /// clients which do use GC.
1171 DenseMap<const Function*, std::string> GCNames;
1173 /// Flag to indicate if Value (other than GlobalValue) retains their name or
1175 bool DiscardValueNames = false;
1177 LLVMContextImpl(LLVMContext &C);
1180 /// Destroy the ConstantArrays if they are not used.
1181 void dropTriviallyDeadConstantArrays();
1183 /// \brief Access the object which manages optimization bisection for failure
1185 OptBisect &getOptBisect();