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"
41 class DiagnosticInfoOptimizationRemark;
42 class DiagnosticInfoOptimizationRemarkMissed;
43 class DiagnosticInfoOptimizationRemarkAnalysis;
49 struct DenseMapAPIntKeyInfo {
50 static inline APInt getEmptyKey() {
55 static inline APInt getTombstoneKey() {
60 static unsigned getHashValue(const APInt &Key) {
61 return static_cast<unsigned>(hash_value(Key));
63 static bool isEqual(const APInt &LHS, const APInt &RHS) {
64 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
68 struct DenseMapAPFloatKeyInfo {
69 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); }
70 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); }
71 static unsigned getHashValue(const APFloat &Key) {
72 return static_cast<unsigned>(hash_value(Key));
74 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
75 return LHS.bitwiseIsEqual(RHS);
79 struct AnonStructTypeKeyInfo {
81 ArrayRef<Type*> ETypes;
83 KeyTy(const ArrayRef<Type*>& E, bool P) :
84 ETypes(E), isPacked(P) {}
85 KeyTy(const StructType *ST)
86 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
87 bool operator==(const KeyTy& that) const {
88 if (isPacked != that.isPacked)
90 if (ETypes != that.ETypes)
94 bool operator!=(const KeyTy& that) const {
95 return !this->operator==(that);
98 static inline StructType* getEmptyKey() {
99 return DenseMapInfo<StructType*>::getEmptyKey();
101 static inline StructType* getTombstoneKey() {
102 return DenseMapInfo<StructType*>::getTombstoneKey();
104 static unsigned getHashValue(const KeyTy& Key) {
105 return hash_combine(hash_combine_range(Key.ETypes.begin(),
109 static unsigned getHashValue(const StructType *ST) {
110 return getHashValue(KeyTy(ST));
112 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
113 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
115 return LHS == KeyTy(RHS);
117 static bool isEqual(const StructType *LHS, const StructType *RHS) {
122 struct FunctionTypeKeyInfo {
124 const Type *ReturnType;
125 ArrayRef<Type*> Params;
127 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
128 ReturnType(R), Params(P), isVarArg(V) {}
129 KeyTy(const FunctionType *FT)
130 : ReturnType(FT->getReturnType()), Params(FT->params()),
131 isVarArg(FT->isVarArg()) {}
132 bool operator==(const KeyTy& that) const {
133 if (ReturnType != that.ReturnType)
135 if (isVarArg != that.isVarArg)
137 if (Params != that.Params)
141 bool operator!=(const KeyTy& that) const {
142 return !this->operator==(that);
145 static inline FunctionType* getEmptyKey() {
146 return DenseMapInfo<FunctionType*>::getEmptyKey();
148 static inline FunctionType* getTombstoneKey() {
149 return DenseMapInfo<FunctionType*>::getTombstoneKey();
151 static unsigned getHashValue(const KeyTy& Key) {
152 return hash_combine(Key.ReturnType,
153 hash_combine_range(Key.Params.begin(),
157 static unsigned getHashValue(const FunctionType *FT) {
158 return getHashValue(KeyTy(FT));
160 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
161 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
163 return LHS == KeyTy(RHS);
165 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
170 /// \brief Structure for hashing arbitrary MDNode operands.
172 ArrayRef<Metadata *> RawOps;
173 ArrayRef<MDOperand> Ops;
178 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
179 : RawOps(Ops), Hash(calculateHash(Ops)) {}
181 template <class NodeTy>
182 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
183 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
185 template <class NodeTy>
186 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
187 if (getHash() != RHS->getHash())
190 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
191 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
192 : compareOps(RawOps, RHS, Offset);
195 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
199 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
200 if (Ops.size() != RHS->getNumOperands() - Offset)
202 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
205 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
208 unsigned getHash() const { return Hash; }
211 template <class NodeTy> struct MDNodeKeyImpl;
212 template <class NodeTy> struct MDNodeInfo;
214 /// \brief DenseMapInfo for MDTuple.
216 /// Note that we don't need the is-function-local bit, since that's implicit in
218 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
219 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
220 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
222 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
224 unsigned getHashValue() const { return getHash(); }
226 static unsigned calculateHash(MDTuple *N) {
227 return MDNodeOpsKey::calculateHash(N);
231 /// \brief DenseMapInfo for DILocation.
232 template <> struct MDNodeKeyImpl<DILocation> {
238 MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
240 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {}
242 MDNodeKeyImpl(const DILocation *L)
243 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
244 InlinedAt(L->getRawInlinedAt()) {}
246 bool isKeyOf(const DILocation *RHS) const {
247 return Line == RHS->getLine() && Column == RHS->getColumn() &&
248 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt();
250 unsigned getHashValue() const {
251 return hash_combine(Line, Column, Scope, InlinedAt);
255 /// \brief DenseMapInfo for GenericDINode.
256 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
259 MDNodeKeyImpl(unsigned Tag, StringRef Header, ArrayRef<Metadata *> DwarfOps)
260 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
261 MDNodeKeyImpl(const GenericDINode *N)
262 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {}
264 bool isKeyOf(const GenericDINode *RHS) const {
265 return Tag == RHS->getTag() && Header == RHS->getHeader() &&
269 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
271 static unsigned calculateHash(GenericDINode *N) {
272 return MDNodeOpsKey::calculateHash(N, 1);
276 template <> struct MDNodeKeyImpl<DISubrange> {
280 MDNodeKeyImpl(int64_t Count, int64_t LowerBound)
281 : Count(Count), LowerBound(LowerBound) {}
282 MDNodeKeyImpl(const DISubrange *N)
283 : Count(N->getCount()), LowerBound(N->getLowerBound()) {}
285 bool isKeyOf(const DISubrange *RHS) const {
286 return Count == RHS->getCount() && LowerBound == RHS->getLowerBound();
288 unsigned getHashValue() const { return hash_combine(Count, LowerBound); }
291 template <> struct MDNodeKeyImpl<DIEnumerator> {
295 MDNodeKeyImpl(int64_t Value, StringRef Name) : Value(Value), Name(Name) {}
296 MDNodeKeyImpl(const DIEnumerator *N)
297 : Value(N->getValue()), Name(N->getName()) {}
299 bool isKeyOf(const DIEnumerator *RHS) const {
300 return Value == RHS->getValue() && Name == RHS->getName();
302 unsigned getHashValue() const { return hash_combine(Value, Name); }
305 template <> struct MDNodeKeyImpl<DIBasicType> {
309 uint64_t AlignInBits;
312 MDNodeKeyImpl(unsigned Tag, StringRef Name, uint64_t SizeInBits,
313 uint64_t AlignInBits, unsigned Encoding)
314 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
315 Encoding(Encoding) {}
316 MDNodeKeyImpl(const DIBasicType *N)
317 : Tag(N->getTag()), Name(N->getName()), SizeInBits(N->getSizeInBits()),
318 AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {}
320 bool isKeyOf(const DIBasicType *RHS) const {
321 return Tag == RHS->getTag() && Name == RHS->getName() &&
322 SizeInBits == RHS->getSizeInBits() &&
323 AlignInBits == RHS->getAlignInBits() &&
324 Encoding == RHS->getEncoding();
326 unsigned getHashValue() const {
327 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
331 template <> struct MDNodeKeyImpl<DIDerivedType> {
339 uint64_t AlignInBits;
340 uint64_t OffsetInBits;
344 MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
345 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
346 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
348 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
349 BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
350 OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {}
351 MDNodeKeyImpl(const DIDerivedType *N)
352 : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()),
353 Line(N->getLine()), Scope(N->getRawScope()),
354 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
355 AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
356 Flags(N->getFlags()), ExtraData(N->getRawExtraData()) {}
358 bool isKeyOf(const DIDerivedType *RHS) const {
359 return Tag == RHS->getTag() && Name == RHS->getName() &&
360 File == RHS->getRawFile() && Line == RHS->getLine() &&
361 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
362 SizeInBits == RHS->getSizeInBits() &&
363 AlignInBits == RHS->getAlignInBits() &&
364 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
365 ExtraData == RHS->getRawExtraData();
367 unsigned getHashValue() const {
368 return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
369 AlignInBits, OffsetInBits, Flags, ExtraData);
373 template <> struct MDNodeKeyImpl<DICompositeType> {
381 uint64_t AlignInBits;
382 uint64_t OffsetInBits;
385 unsigned RuntimeLang;
386 Metadata *VTableHolder;
387 Metadata *TemplateParams;
388 StringRef Identifier;
390 MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line,
391 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
392 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
393 Metadata *Elements, unsigned RuntimeLang,
394 Metadata *VTableHolder, Metadata *TemplateParams,
395 StringRef Identifier)
396 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
397 BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
398 OffsetInBits(OffsetInBits), Flags(Flags), Elements(Elements),
399 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
400 TemplateParams(TemplateParams), Identifier(Identifier) {}
401 MDNodeKeyImpl(const DICompositeType *N)
402 : Tag(N->getTag()), Name(N->getName()), File(N->getRawFile()),
403 Line(N->getLine()), Scope(N->getRawScope()),
404 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
405 AlignInBits(N->getAlignInBits()), OffsetInBits(N->getOffsetInBits()),
406 Flags(N->getFlags()), Elements(N->getRawElements()),
407 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
408 TemplateParams(N->getRawTemplateParams()),
409 Identifier(N->getIdentifier()) {}
411 bool isKeyOf(const DICompositeType *RHS) const {
412 return Tag == RHS->getTag() && Name == RHS->getName() &&
413 File == RHS->getRawFile() && Line == RHS->getLine() &&
414 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
415 SizeInBits == RHS->getSizeInBits() &&
416 AlignInBits == RHS->getAlignInBits() &&
417 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
418 Elements == RHS->getRawElements() &&
419 RuntimeLang == RHS->getRuntimeLang() &&
420 VTableHolder == RHS->getRawVTableHolder() &&
421 TemplateParams == RHS->getRawTemplateParams() &&
422 Identifier == RHS->getIdentifier();
424 unsigned getHashValue() const {
425 return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits,
426 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
427 VTableHolder, TemplateParams, Identifier);
431 template <> struct MDNodeKeyImpl<DISubroutineType> {
435 MDNodeKeyImpl(int64_t Flags, Metadata *TypeArray)
436 : Flags(Flags), TypeArray(TypeArray) {}
437 MDNodeKeyImpl(const DISubroutineType *N)
438 : Flags(N->getFlags()), TypeArray(N->getRawTypeArray()) {}
440 bool isKeyOf(const DISubroutineType *RHS) const {
441 return Flags == RHS->getFlags() && TypeArray == RHS->getRawTypeArray();
443 unsigned getHashValue() const { return hash_combine(Flags, TypeArray); }
446 template <> struct MDNodeKeyImpl<DIFile> {
450 MDNodeKeyImpl(StringRef Filename, StringRef Directory)
451 : Filename(Filename), Directory(Directory) {}
452 MDNodeKeyImpl(const DIFile *N)
453 : Filename(N->getFilename()), Directory(N->getDirectory()) {}
455 bool isKeyOf(const DIFile *RHS) const {
456 return Filename == RHS->getFilename() && Directory == RHS->getDirectory();
458 unsigned getHashValue() const { return hash_combine(Filename, Directory); }
461 template <> struct MDNodeKeyImpl<DISubprogram> {
464 StringRef LinkageName;
471 Metadata *ContainingType;
473 unsigned VirtualIndex;
477 Metadata *TemplateParams;
478 Metadata *Declaration;
481 MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
482 Metadata *File, unsigned Line, Metadata *Type,
483 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
484 Metadata *ContainingType, unsigned Virtuality,
485 unsigned VirtualIndex, unsigned Flags, bool IsOptimized,
486 Metadata *Function, Metadata *TemplateParams,
487 Metadata *Declaration, Metadata *Variables)
488 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
489 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
490 IsDefinition(IsDefinition), ScopeLine(ScopeLine),
491 ContainingType(ContainingType), Virtuality(Virtuality),
492 VirtualIndex(VirtualIndex), Flags(Flags), IsOptimized(IsOptimized),
493 Function(Function), TemplateParams(TemplateParams),
494 Declaration(Declaration), Variables(Variables) {}
495 MDNodeKeyImpl(const DISubprogram *N)
496 : Scope(N->getRawScope()), Name(N->getName()),
497 LinkageName(N->getLinkageName()), File(N->getRawFile()),
498 Line(N->getLine()), Type(N->getRawType()),
499 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
500 ScopeLine(N->getScopeLine()), ContainingType(N->getRawContainingType()),
501 Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()),
502 Flags(N->getFlags()), IsOptimized(N->isOptimized()),
503 Function(N->getRawFunction()),
504 TemplateParams(N->getRawTemplateParams()),
505 Declaration(N->getRawDeclaration()), Variables(N->getRawVariables()) {}
507 bool isKeyOf(const DISubprogram *RHS) const {
508 return Scope == RHS->getRawScope() && Name == RHS->getName() &&
509 LinkageName == RHS->getLinkageName() && File == RHS->getRawFile() &&
510 Line == RHS->getLine() && Type == RHS->getRawType() &&
511 IsLocalToUnit == RHS->isLocalToUnit() &&
512 IsDefinition == RHS->isDefinition() &&
513 ScopeLine == RHS->getScopeLine() &&
514 ContainingType == RHS->getRawContainingType() &&
515 Virtuality == RHS->getVirtuality() &&
516 VirtualIndex == RHS->getVirtualIndex() && Flags == RHS->getFlags() &&
517 IsOptimized == RHS->isOptimized() &&
518 Function == RHS->getRawFunction() &&
519 TemplateParams == RHS->getRawTemplateParams() &&
520 Declaration == RHS->getRawDeclaration() &&
521 Variables == RHS->getRawVariables();
523 unsigned getHashValue() const {
524 return hash_combine(Scope, Name, LinkageName, File, Line, Type,
525 IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
526 Virtuality, VirtualIndex, Flags, IsOptimized, Function,
527 TemplateParams, Declaration, Variables);
531 template <> struct MDNodeKeyImpl<DILexicalBlock> {
537 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
538 : Scope(Scope), File(File), Line(Line), Column(Column) {}
539 MDNodeKeyImpl(const DILexicalBlock *N)
540 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
541 Column(N->getColumn()) {}
543 bool isKeyOf(const DILexicalBlock *RHS) const {
544 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
545 Line == RHS->getLine() && Column == RHS->getColumn();
547 unsigned getHashValue() const {
548 return hash_combine(Scope, File, Line, Column);
552 template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
555 unsigned Discriminator;
557 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
558 : Scope(Scope), File(File), Discriminator(Discriminator) {}
559 MDNodeKeyImpl(const DILexicalBlockFile *N)
560 : Scope(N->getRawScope()), File(N->getRawFile()),
561 Discriminator(N->getDiscriminator()) {}
563 bool isKeyOf(const DILexicalBlockFile *RHS) const {
564 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
565 Discriminator == RHS->getDiscriminator();
567 unsigned getHashValue() const {
568 return hash_combine(Scope, File, Discriminator);
572 template <> struct MDNodeKeyImpl<DINamespace> {
578 MDNodeKeyImpl(Metadata *Scope, Metadata *File, StringRef Name, unsigned Line)
579 : Scope(Scope), File(File), Name(Name), Line(Line) {}
580 MDNodeKeyImpl(const DINamespace *N)
581 : Scope(N->getRawScope()), File(N->getRawFile()), Name(N->getName()),
582 Line(N->getLine()) {}
584 bool isKeyOf(const DINamespace *RHS) const {
585 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
586 Name == RHS->getName() && Line == RHS->getLine();
588 unsigned getHashValue() const {
589 return hash_combine(Scope, File, Name, Line);
593 template <> struct MDNodeKeyImpl<DIModule> {
596 StringRef ConfigurationMacros;
597 StringRef IncludePath;
599 MDNodeKeyImpl(Metadata *Scope, StringRef Name,
600 StringRef ConfigurationMacros,
601 StringRef IncludePath,
603 : Scope(Scope), Name(Name), ConfigurationMacros(ConfigurationMacros),
604 IncludePath(IncludePath), ISysRoot(ISysRoot) {}
605 MDNodeKeyImpl(const DIModule *N)
606 : Scope(N->getRawScope()), Name(N->getName()),
607 ConfigurationMacros(N->getConfigurationMacros()),
608 IncludePath(N->getIncludePath()), ISysRoot(N->getISysRoot()) {}
610 bool isKeyOf(const DIModule *RHS) const {
611 return Scope == RHS->getRawScope() && Name == RHS->getName() &&
612 ConfigurationMacros == RHS->getConfigurationMacros() &&
613 IncludePath == RHS->getIncludePath() &&
614 ISysRoot == RHS->getISysRoot();
616 unsigned getHashValue() const {
617 return hash_combine(Scope, Name,
618 ConfigurationMacros, IncludePath, ISysRoot);
622 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
626 MDNodeKeyImpl(StringRef Name, Metadata *Type) : Name(Name), Type(Type) {}
627 MDNodeKeyImpl(const DITemplateTypeParameter *N)
628 : Name(N->getName()), Type(N->getRawType()) {}
630 bool isKeyOf(const DITemplateTypeParameter *RHS) const {
631 return Name == RHS->getName() && Type == RHS->getRawType();
633 unsigned getHashValue() const { return hash_combine(Name, Type); }
636 template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
642 MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *Type, Metadata *Value)
643 : Tag(Tag), Name(Name), Type(Type), Value(Value) {}
644 MDNodeKeyImpl(const DITemplateValueParameter *N)
645 : Tag(N->getTag()), Name(N->getName()), Type(N->getRawType()),
646 Value(N->getValue()) {}
648 bool isKeyOf(const DITemplateValueParameter *RHS) const {
649 return Tag == RHS->getTag() && Name == RHS->getName() &&
650 Type == RHS->getRawType() && Value == RHS->getValue();
652 unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); }
655 template <> struct MDNodeKeyImpl<DIGlobalVariable> {
658 StringRef LinkageName;
665 Metadata *StaticDataMemberDeclaration;
667 MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName,
668 Metadata *File, unsigned Line, Metadata *Type,
669 bool IsLocalToUnit, bool IsDefinition, Metadata *Variable,
670 Metadata *StaticDataMemberDeclaration)
671 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
672 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
673 IsDefinition(IsDefinition), Variable(Variable),
674 StaticDataMemberDeclaration(StaticDataMemberDeclaration) {}
675 MDNodeKeyImpl(const DIGlobalVariable *N)
676 : Scope(N->getRawScope()), Name(N->getName()),
677 LinkageName(N->getLinkageName()), File(N->getRawFile()),
678 Line(N->getLine()), Type(N->getRawType()),
679 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
680 Variable(N->getRawVariable()),
681 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()) {}
683 bool isKeyOf(const DIGlobalVariable *RHS) const {
684 return Scope == RHS->getRawScope() && Name == RHS->getName() &&
685 LinkageName == RHS->getLinkageName() && File == RHS->getRawFile() &&
686 Line == RHS->getLine() && Type == RHS->getRawType() &&
687 IsLocalToUnit == RHS->isLocalToUnit() &&
688 IsDefinition == RHS->isDefinition() &&
689 Variable == RHS->getRawVariable() &&
690 StaticDataMemberDeclaration ==
691 RHS->getRawStaticDataMemberDeclaration();
693 unsigned getHashValue() const {
694 return hash_combine(Scope, Name, LinkageName, File, Line, Type,
695 IsLocalToUnit, IsDefinition, Variable,
696 StaticDataMemberDeclaration);
700 template <> struct MDNodeKeyImpl<DILocalVariable> {
709 MDNodeKeyImpl(Metadata *Scope, StringRef Name, Metadata *File, unsigned Line,
710 Metadata *Type, unsigned Arg, unsigned Flags)
711 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
713 MDNodeKeyImpl(const DILocalVariable *N)
714 : Scope(N->getRawScope()), Name(N->getName()), File(N->getRawFile()),
715 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
716 Flags(N->getFlags()) {}
718 bool isKeyOf(const DILocalVariable *RHS) const {
719 return Scope == RHS->getRawScope() && Name == RHS->getName() &&
720 File == RHS->getRawFile() && Line == RHS->getLine() &&
721 Type == RHS->getRawType() && Arg == RHS->getArg() &&
722 Flags == RHS->getFlags();
724 unsigned getHashValue() const {
725 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
729 template <> struct MDNodeKeyImpl<DIExpression> {
730 ArrayRef<uint64_t> Elements;
732 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
733 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
735 bool isKeyOf(const DIExpression *RHS) const {
736 return Elements == RHS->getElements();
738 unsigned getHashValue() const {
739 return hash_combine_range(Elements.begin(), Elements.end());
743 template <> struct MDNodeKeyImpl<DIObjCProperty> {
747 StringRef GetterName;
748 StringRef SetterName;
752 MDNodeKeyImpl(StringRef Name, Metadata *File, unsigned Line,
753 StringRef GetterName, StringRef SetterName, unsigned Attributes,
755 : Name(Name), File(File), Line(Line), GetterName(GetterName),
756 SetterName(SetterName), Attributes(Attributes), Type(Type) {}
757 MDNodeKeyImpl(const DIObjCProperty *N)
758 : Name(N->getName()), File(N->getRawFile()), Line(N->getLine()),
759 GetterName(N->getGetterName()), SetterName(N->getSetterName()),
760 Attributes(N->getAttributes()), Type(N->getRawType()) {}
762 bool isKeyOf(const DIObjCProperty *RHS) const {
763 return Name == RHS->getName() && File == RHS->getRawFile() &&
764 Line == RHS->getLine() && GetterName == RHS->getGetterName() &&
765 SetterName == RHS->getSetterName() &&
766 Attributes == RHS->getAttributes() && Type == RHS->getRawType();
768 unsigned getHashValue() const {
769 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
774 template <> struct MDNodeKeyImpl<DIImportedEntity> {
781 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line,
783 : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {}
784 MDNodeKeyImpl(const DIImportedEntity *N)
785 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
786 Line(N->getLine()), Name(N->getName()) {}
788 bool isKeyOf(const DIImportedEntity *RHS) const {
789 return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
790 Entity == RHS->getRawEntity() && Line == RHS->getLine() &&
791 Name == RHS->getName();
793 unsigned getHashValue() const {
794 return hash_combine(Tag, Scope, Entity, Line, Name);
798 /// \brief DenseMapInfo for MDNode subclasses.
799 template <class NodeTy> struct MDNodeInfo {
800 typedef MDNodeKeyImpl<NodeTy> KeyTy;
801 static inline NodeTy *getEmptyKey() {
802 return DenseMapInfo<NodeTy *>::getEmptyKey();
804 static inline NodeTy *getTombstoneKey() {
805 return DenseMapInfo<NodeTy *>::getTombstoneKey();
807 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
808 static unsigned getHashValue(const NodeTy *N) {
809 return KeyTy(N).getHashValue();
811 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
812 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
814 return LHS.isKeyOf(RHS);
816 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
821 #define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo<CLASS> CLASS##Info;
822 #include "llvm/IR/Metadata.def"
824 /// \brief Map-like storage for metadata attachments.
825 class MDAttachmentMap {
826 SmallVector<std::pair<unsigned, TrackingMDNodeRef>, 2> Attachments;
829 bool empty() const { return Attachments.empty(); }
830 size_t size() const { return Attachments.size(); }
832 /// \brief Get a particular attachment (if any).
833 MDNode *lookup(unsigned ID) const;
835 /// \brief Set an attachment to a particular node.
837 /// Set the \c ID attachment to \c MD, replacing the current attachment at \c
839 void set(unsigned ID, MDNode &MD);
841 /// \brief Remove an attachment.
843 /// Remove the attachment at \c ID, if any.
844 void erase(unsigned ID);
846 /// \brief Copy out all the attachments.
848 /// Copies all the current attachments into \c Result, sorting by attachment
849 /// ID. This function does \em not clear \c Result.
850 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
852 /// \brief Erase matching attachments.
854 /// Erases all attachments matching the \c shouldRemove predicate.
855 template <class PredTy> void remove_if(PredTy shouldRemove) {
857 std::remove_if(Attachments.begin(), Attachments.end(), shouldRemove),
862 class LLVMContextImpl {
864 /// OwnedModules - The set of modules instantiated in this context, and which
865 /// will be automatically deleted if this context is deleted.
866 SmallPtrSet<Module*, 4> OwnedModules;
868 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler;
869 void *InlineAsmDiagContext;
871 LLVMContext::DiagnosticHandlerTy DiagnosticHandler;
872 void *DiagnosticContext;
873 bool RespectDiagnosticFilters;
875 LLVMContext::YieldCallbackTy YieldCallback;
876 void *YieldOpaqueHandle;
878 typedef DenseMap<APInt, ConstantInt *, DenseMapAPIntKeyInfo> IntMapTy;
879 IntMapTy IntConstants;
881 typedef DenseMap<APFloat, ConstantFP *, DenseMapAPFloatKeyInfo> FPMapTy;
884 FoldingSet<AttributeImpl> AttrsSet;
885 FoldingSet<AttributeSetImpl> AttrsLists;
886 FoldingSet<AttributeSetNode> AttrsSetNodes;
888 StringMap<MDString> MDStringCache;
889 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
890 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
892 DenseMap<const Value*, ValueName*> ValueNames;
894 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
895 DenseSet<CLASS *, CLASS##Info> CLASS##s;
896 #include "llvm/IR/Metadata.def"
898 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
899 // aren't in the MDNodeSet, but they're still shared between objects, so no
900 // one object can destroy them. This set allows us to at least destroy them
901 // on Context destruction.
902 SmallPtrSet<MDNode *, 1> DistinctMDNodes;
904 DenseMap<Type*, ConstantAggregateZero*> CAZConstants;
906 typedef ConstantUniqueMap<ConstantArray> ArrayConstantsTy;
907 ArrayConstantsTy ArrayConstants;
909 typedef ConstantUniqueMap<ConstantStruct> StructConstantsTy;
910 StructConstantsTy StructConstants;
912 typedef ConstantUniqueMap<ConstantVector> VectorConstantsTy;
913 VectorConstantsTy VectorConstants;
915 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants;
917 DenseMap<Type*, UndefValue*> UVConstants;
919 StringMap<ConstantDataSequential*> CDSConstants;
921 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
923 ConstantUniqueMap<ConstantExpr> ExprConstants;
925 ConstantUniqueMap<InlineAsm> InlineAsms;
927 ConstantInt *TheTrueVal;
928 ConstantInt *TheFalseVal;
930 // Basic type instances.
931 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy, TokenTy;
932 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
933 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
936 /// TypeAllocator - All dynamically allocated types are allocated from this.
937 /// They live forever until the context is torn down.
938 BumpPtrAllocator TypeAllocator;
940 DenseMap<unsigned, IntegerType*> IntegerTypes;
942 typedef DenseSet<FunctionType *, FunctionTypeKeyInfo> FunctionTypeSet;
943 FunctionTypeSet FunctionTypes;
944 typedef DenseSet<StructType *, AnonStructTypeKeyInfo> StructTypeSet;
945 StructTypeSet AnonStructTypes;
946 StringMap<StructType*> NamedStructTypes;
947 unsigned NamedStructTypesUniqueID;
949 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
950 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes;
951 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
952 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
955 /// ValueHandles - This map keeps track of all of the value handles that are
956 /// watching a Value*. The Value::HasValueHandle bit is used to know
957 /// whether or not a value has an entry in this map.
958 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
959 ValueHandlesTy ValueHandles;
961 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
962 StringMap<unsigned> CustomMDKindNames;
964 /// Collection of per-instruction metadata used in this context.
965 DenseMap<const Instruction *, MDAttachmentMap> InstructionMetadata;
967 /// Collection of per-function metadata used in this context.
968 DenseMap<const Function *, MDAttachmentMap> FunctionMetadata;
970 /// DiscriminatorTable - This table maps file:line locations to an
971 /// integer representing the next DWARF path discriminator to assign to
972 /// instructions in different blocks at the same location.
973 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
975 /// \brief Mapping from a function to its prefix data, which is stored as the
976 /// operand of an unparented ReturnInst so that the prefix data has a Use.
977 typedef DenseMap<const Function *, ReturnInst *> PrefixDataMapTy;
978 PrefixDataMapTy PrefixDataMap;
980 /// \brief Mapping from a function to its prologue data, which is stored as
981 /// the operand of an unparented ReturnInst so that the prologue data has a
983 typedef DenseMap<const Function *, ReturnInst *> PrologueDataMapTy;
984 PrologueDataMapTy PrologueDataMap;
986 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx);
987 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx);
989 LLVMContextImpl(LLVMContext &C);
992 /// Destroy the ConstantArrays if they are not used.
993 void dropTriviallyDeadConstantArrays();