[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / IR / LLVMContextImpl.h
blobbd38c86996c97fc5187e69e93cea0f34692804bf
1 //===- LLVMContextImpl.h - The LLVMContextImpl opaque class -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares LLVMContextImpl, the opaque implementation
10 // of LLVMContext.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
15 #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
17 #include "AttributeImpl.h"
18 #include "ConstantsContext.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/DenseMapInfo.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/ADT/Hashing.h"
27 #include "llvm/ADT/Optional.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringMap.h"
32 #include "llvm/BinaryFormat/Dwarf.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DebugInfoMetadata.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/LLVMRemarkStreamer.h"
38 #include "llvm/IR/Metadata.h"
39 #include "llvm/IR/TrackingMDRef.h"
40 #include "llvm/Support/Allocator.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/StringSaver.h"
43 #include "llvm/Support/YAMLTraits.h"
44 #include <algorithm>
45 #include <cassert>
46 #include <cstddef>
47 #include <cstdint>
48 #include <memory>
49 #include <string>
50 #include <utility>
51 #include <vector>
53 namespace llvm {
55 class StringRef;
56 class Type;
57 class Value;
58 class ValueHandleBase;
60 using DenseMapAPIntKeyInfo = DenseMapInfo<APInt>;
62 struct DenseMapAPFloatKeyInfo {
63 static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); }
64 static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus(), 2); }
66 static unsigned getHashValue(const APFloat &Key) {
67 return static_cast<unsigned>(hash_value(Key));
70 static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
71 return LHS.bitwiseIsEqual(RHS);
75 struct AnonStructTypeKeyInfo {
76 struct KeyTy {
77 ArrayRef<Type*> ETypes;
78 bool isPacked;
80 KeyTy(const ArrayRef<Type*>& E, bool P) :
81 ETypes(E), isPacked(P) {}
83 KeyTy(const StructType *ST)
84 : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
86 bool operator==(const KeyTy& that) const {
87 if (isPacked != that.isPacked)
88 return false;
89 if (ETypes != that.ETypes)
90 return false;
91 return true;
93 bool operator!=(const KeyTy& that) const {
94 return !this->operator==(that);
98 static inline StructType* getEmptyKey() {
99 return DenseMapInfo<StructType*>::getEmptyKey();
102 static inline StructType* getTombstoneKey() {
103 return DenseMapInfo<StructType*>::getTombstoneKey();
106 static unsigned getHashValue(const KeyTy& Key) {
107 return hash_combine(hash_combine_range(Key.ETypes.begin(),
108 Key.ETypes.end()),
109 Key.isPacked);
112 static unsigned getHashValue(const StructType *ST) {
113 return getHashValue(KeyTy(ST));
116 static bool isEqual(const KeyTy& LHS, const StructType *RHS) {
117 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
118 return false;
119 return LHS == KeyTy(RHS);
122 static bool isEqual(const StructType *LHS, const StructType *RHS) {
123 return LHS == RHS;
127 struct FunctionTypeKeyInfo {
128 struct KeyTy {
129 const Type *ReturnType;
130 ArrayRef<Type*> Params;
131 bool isVarArg;
133 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) :
134 ReturnType(R), Params(P), isVarArg(V) {}
135 KeyTy(const FunctionType *FT)
136 : ReturnType(FT->getReturnType()), Params(FT->params()),
137 isVarArg(FT->isVarArg()) {}
139 bool operator==(const KeyTy& that) const {
140 if (ReturnType != that.ReturnType)
141 return false;
142 if (isVarArg != that.isVarArg)
143 return false;
144 if (Params != that.Params)
145 return false;
146 return true;
148 bool operator!=(const KeyTy& that) const {
149 return !this->operator==(that);
153 static inline FunctionType* getEmptyKey() {
154 return DenseMapInfo<FunctionType*>::getEmptyKey();
157 static inline FunctionType* getTombstoneKey() {
158 return DenseMapInfo<FunctionType*>::getTombstoneKey();
161 static unsigned getHashValue(const KeyTy& Key) {
162 return hash_combine(Key.ReturnType,
163 hash_combine_range(Key.Params.begin(),
164 Key.Params.end()),
165 Key.isVarArg);
168 static unsigned getHashValue(const FunctionType *FT) {
169 return getHashValue(KeyTy(FT));
172 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) {
173 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
174 return false;
175 return LHS == KeyTy(RHS);
178 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
179 return LHS == RHS;
183 /// Structure for hashing arbitrary MDNode operands.
184 class MDNodeOpsKey {
185 ArrayRef<Metadata *> RawOps;
186 ArrayRef<MDOperand> Ops;
187 unsigned Hash;
189 protected:
190 MDNodeOpsKey(ArrayRef<Metadata *> Ops)
191 : RawOps(Ops), Hash(calculateHash(Ops)) {}
193 template <class NodeTy>
194 MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
195 : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
197 template <class NodeTy>
198 bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
199 if (getHash() != RHS->getHash())
200 return false;
202 assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
203 return RawOps.empty() ? compareOps(Ops, RHS, Offset)
204 : compareOps(RawOps, RHS, Offset);
207 static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
209 private:
210 template <class T>
211 static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
212 if (Ops.size() != RHS->getNumOperands() - Offset)
213 return false;
214 return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
217 static unsigned calculateHash(ArrayRef<Metadata *> Ops);
219 public:
220 unsigned getHash() const { return Hash; }
223 template <class NodeTy> struct MDNodeKeyImpl;
225 /// Configuration point for MDNodeInfo::isEqual().
226 template <class NodeTy> struct MDNodeSubsetEqualImpl {
227 using KeyTy = MDNodeKeyImpl<NodeTy>;
229 static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) {
230 return false;
233 static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) {
234 return false;
238 /// DenseMapInfo for MDTuple.
240 /// Note that we don't need the is-function-local bit, since that's implicit in
241 /// the operands.
242 template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
243 MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
244 MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
246 bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
248 unsigned getHashValue() const { return getHash(); }
250 static unsigned calculateHash(MDTuple *N) {
251 return MDNodeOpsKey::calculateHash(N);
255 /// DenseMapInfo for DILocation.
256 template <> struct MDNodeKeyImpl<DILocation> {
257 unsigned Line;
258 unsigned Column;
259 Metadata *Scope;
260 Metadata *InlinedAt;
261 bool ImplicitCode;
263 MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
264 Metadata *InlinedAt, bool ImplicitCode)
265 : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt),
266 ImplicitCode(ImplicitCode) {}
267 MDNodeKeyImpl(const DILocation *L)
268 : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
269 InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {}
271 bool isKeyOf(const DILocation *RHS) const {
272 return Line == RHS->getLine() && Column == RHS->getColumn() &&
273 Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() &&
274 ImplicitCode == RHS->isImplicitCode();
277 unsigned getHashValue() const {
278 return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode);
282 /// DenseMapInfo for GenericDINode.
283 template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
284 unsigned Tag;
285 MDString *Header;
287 MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
288 : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
289 MDNodeKeyImpl(const GenericDINode *N)
290 : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {}
292 bool isKeyOf(const GenericDINode *RHS) const {
293 return Tag == RHS->getTag() && Header == RHS->getRawHeader() &&
294 compareOps(RHS, 1);
297 unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
299 static unsigned calculateHash(GenericDINode *N) {
300 return MDNodeOpsKey::calculateHash(N, 1);
304 template <> struct MDNodeKeyImpl<DISubrange> {
305 Metadata *CountNode;
306 Metadata *LowerBound;
307 Metadata *UpperBound;
308 Metadata *Stride;
310 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound,
311 Metadata *Stride)
312 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound),
313 Stride(Stride) {}
314 MDNodeKeyImpl(const DISubrange *N)
315 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()),
316 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {}
318 bool isKeyOf(const DISubrange *RHS) const {
319 auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool {
320 if (Node1 == Node2)
321 return true;
323 ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1);
324 ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2);
325 if (MD1 && MD2) {
326 ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue());
327 ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue());
328 if (CV1->getSExtValue() == CV2->getSExtValue())
329 return true;
331 return false;
334 return BoundsEqual(CountNode, RHS->getRawCountNode()) &&
335 BoundsEqual(LowerBound, RHS->getRawLowerBound()) &&
336 BoundsEqual(UpperBound, RHS->getRawUpperBound()) &&
337 BoundsEqual(Stride, RHS->getRawStride());
340 unsigned getHashValue() const {
341 if (CountNode)
342 if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode))
343 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(),
344 LowerBound, UpperBound, Stride);
345 return hash_combine(CountNode, LowerBound, UpperBound, Stride);
349 template <> struct MDNodeKeyImpl<DIGenericSubrange> {
350 Metadata *CountNode;
351 Metadata *LowerBound;
352 Metadata *UpperBound;
353 Metadata *Stride;
355 MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound,
356 Metadata *Stride)
357 : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound),
358 Stride(Stride) {}
359 MDNodeKeyImpl(const DIGenericSubrange *N)
360 : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()),
361 UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {}
363 bool isKeyOf(const DIGenericSubrange *RHS) const {
364 return (CountNode == RHS->getRawCountNode()) &&
365 (LowerBound == RHS->getRawLowerBound()) &&
366 (UpperBound == RHS->getRawUpperBound()) &&
367 (Stride == RHS->getRawStride());
370 unsigned getHashValue() const {
371 auto *MD = dyn_cast_or_null<ConstantAsMetadata>(CountNode);
372 if (CountNode && MD)
373 return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(),
374 LowerBound, UpperBound, Stride);
375 return hash_combine(CountNode, LowerBound, UpperBound, Stride);
379 template <> struct MDNodeKeyImpl<DIEnumerator> {
380 APInt Value;
381 MDString *Name;
382 bool IsUnsigned;
384 MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name)
385 : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {}
386 MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name)
387 : Value(APInt(64, Value, !IsUnsigned)), Name(Name),
388 IsUnsigned(IsUnsigned) {}
389 MDNodeKeyImpl(const DIEnumerator *N)
390 : Value(N->getValue()), Name(N->getRawName()),
391 IsUnsigned(N->isUnsigned()) {}
393 bool isKeyOf(const DIEnumerator *RHS) const {
394 return APInt::isSameValue(Value, RHS->getValue()) &&
395 IsUnsigned == RHS->isUnsigned() && Name == RHS->getRawName();
398 unsigned getHashValue() const { return hash_combine(Value, Name); }
401 template <> struct MDNodeKeyImpl<DIBasicType> {
402 unsigned Tag;
403 MDString *Name;
404 uint64_t SizeInBits;
405 uint32_t AlignInBits;
406 unsigned Encoding;
407 unsigned Flags;
409 MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
410 uint32_t AlignInBits, unsigned Encoding, unsigned Flags)
411 : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
412 Encoding(Encoding), Flags(Flags) {}
413 MDNodeKeyImpl(const DIBasicType *N)
414 : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
415 AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()), Flags(N->getFlags()) {}
417 bool isKeyOf(const DIBasicType *RHS) const {
418 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
419 SizeInBits == RHS->getSizeInBits() &&
420 AlignInBits == RHS->getAlignInBits() &&
421 Encoding == RHS->getEncoding() &&
422 Flags == RHS->getFlags();
425 unsigned getHashValue() const {
426 return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
430 template <> struct MDNodeKeyImpl<DIStringType> {
431 unsigned Tag;
432 MDString *Name;
433 Metadata *StringLength;
434 Metadata *StringLengthExp;
435 uint64_t SizeInBits;
436 uint32_t AlignInBits;
437 unsigned Encoding;
439 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *StringLength,
440 Metadata *StringLengthExp, uint64_t SizeInBits,
441 uint32_t AlignInBits, unsigned Encoding)
442 : Tag(Tag), Name(Name), StringLength(StringLength),
443 StringLengthExp(StringLengthExp), SizeInBits(SizeInBits),
444 AlignInBits(AlignInBits), Encoding(Encoding) {}
445 MDNodeKeyImpl(const DIStringType *N)
446 : Tag(N->getTag()), Name(N->getRawName()),
447 StringLength(N->getRawStringLength()),
448 StringLengthExp(N->getRawStringLengthExp()),
449 SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()),
450 Encoding(N->getEncoding()) {}
452 bool isKeyOf(const DIStringType *RHS) const {
453 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
454 SizeInBits == RHS->getSizeInBits() &&
455 AlignInBits == RHS->getAlignInBits() &&
456 Encoding == RHS->getEncoding();
458 unsigned getHashValue() const { return hash_combine(Tag, Name, Encoding); }
461 template <> struct MDNodeKeyImpl<DIDerivedType> {
462 unsigned Tag;
463 MDString *Name;
464 Metadata *File;
465 unsigned Line;
466 Metadata *Scope;
467 Metadata *BaseType;
468 uint64_t SizeInBits;
469 uint64_t OffsetInBits;
470 uint32_t AlignInBits;
471 Optional<unsigned> DWARFAddressSpace;
472 unsigned Flags;
473 Metadata *ExtraData;
474 Metadata *Annotations;
476 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
477 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
478 uint32_t AlignInBits, uint64_t OffsetInBits,
479 Optional<unsigned> DWARFAddressSpace, unsigned Flags,
480 Metadata *ExtraData, Metadata *Annotations)
481 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
482 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
483 AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace),
484 Flags(Flags), ExtraData(ExtraData), Annotations(Annotations) {}
485 MDNodeKeyImpl(const DIDerivedType *N)
486 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
487 Line(N->getLine()), Scope(N->getRawScope()),
488 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
489 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
490 DWARFAddressSpace(N->getDWARFAddressSpace()), Flags(N->getFlags()),
491 ExtraData(N->getRawExtraData()), Annotations(N->getRawAnnotations()) {}
493 bool isKeyOf(const DIDerivedType *RHS) const {
494 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
495 File == RHS->getRawFile() && Line == RHS->getLine() &&
496 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
497 SizeInBits == RHS->getSizeInBits() &&
498 AlignInBits == RHS->getAlignInBits() &&
499 OffsetInBits == RHS->getOffsetInBits() &&
500 DWARFAddressSpace == RHS->getDWARFAddressSpace() &&
501 Flags == RHS->getFlags() && ExtraData == RHS->getRawExtraData() &&
502 Annotations == RHS->getRawAnnotations();
505 unsigned getHashValue() const {
506 // If this is a member inside an ODR type, only hash the type and the name.
507 // Otherwise the hash will be stronger than
508 // MDNodeSubsetEqualImpl::isODRMember().
509 if (Tag == dwarf::DW_TAG_member && Name)
510 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
511 if (CT->getRawIdentifier())
512 return hash_combine(Name, Scope);
514 // Intentionally computes the hash on a subset of the operands for
515 // performance reason. The subset has to be significant enough to avoid
516 // collision "most of the time". There is no correctness issue in case of
517 // collision because of the full check above.
518 return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags);
522 template <> struct MDNodeSubsetEqualImpl<DIDerivedType> {
523 using KeyTy = MDNodeKeyImpl<DIDerivedType>;
525 static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) {
526 return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS);
529 static bool isSubsetEqual(const DIDerivedType *LHS, const DIDerivedType *RHS) {
530 return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(),
531 RHS);
534 /// Subprograms compare equal if they declare the same function in an ODR
535 /// type.
536 static bool isODRMember(unsigned Tag, const Metadata *Scope,
537 const MDString *Name, const DIDerivedType *RHS) {
538 // Check whether the LHS is eligible.
539 if (Tag != dwarf::DW_TAG_member || !Name)
540 return false;
542 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
543 if (!CT || !CT->getRawIdentifier())
544 return false;
546 // Compare to the RHS.
547 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
548 Scope == RHS->getRawScope();
552 template <> struct MDNodeKeyImpl<DICompositeType> {
553 unsigned Tag;
554 MDString *Name;
555 Metadata *File;
556 unsigned Line;
557 Metadata *Scope;
558 Metadata *BaseType;
559 uint64_t SizeInBits;
560 uint64_t OffsetInBits;
561 uint32_t AlignInBits;
562 unsigned Flags;
563 Metadata *Elements;
564 unsigned RuntimeLang;
565 Metadata *VTableHolder;
566 Metadata *TemplateParams;
567 MDString *Identifier;
568 Metadata *Discriminator;
569 Metadata *DataLocation;
570 Metadata *Associated;
571 Metadata *Allocated;
572 Metadata *Rank;
573 Metadata *Annotations;
575 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
576 Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
577 uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
578 Metadata *Elements, unsigned RuntimeLang,
579 Metadata *VTableHolder, Metadata *TemplateParams,
580 MDString *Identifier, Metadata *Discriminator,
581 Metadata *DataLocation, Metadata *Associated,
582 Metadata *Allocated, Metadata *Rank, Metadata *Annotations)
583 : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
584 BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
585 AlignInBits(AlignInBits), Flags(Flags), Elements(Elements),
586 RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
587 TemplateParams(TemplateParams), Identifier(Identifier),
588 Discriminator(Discriminator), DataLocation(DataLocation),
589 Associated(Associated), Allocated(Allocated), Rank(Rank),
590 Annotations(Annotations) {}
591 MDNodeKeyImpl(const DICompositeType *N)
592 : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
593 Line(N->getLine()), Scope(N->getRawScope()),
594 BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
595 OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
596 Flags(N->getFlags()), Elements(N->getRawElements()),
597 RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
598 TemplateParams(N->getRawTemplateParams()),
599 Identifier(N->getRawIdentifier()),
600 Discriminator(N->getRawDiscriminator()),
601 DataLocation(N->getRawDataLocation()),
602 Associated(N->getRawAssociated()), Allocated(N->getRawAllocated()),
603 Rank(N->getRawRank()), Annotations(N->getRawAnnotations()) {}
605 bool isKeyOf(const DICompositeType *RHS) const {
606 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
607 File == RHS->getRawFile() && Line == RHS->getLine() &&
608 Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
609 SizeInBits == RHS->getSizeInBits() &&
610 AlignInBits == RHS->getAlignInBits() &&
611 OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
612 Elements == RHS->getRawElements() &&
613 RuntimeLang == RHS->getRuntimeLang() &&
614 VTableHolder == RHS->getRawVTableHolder() &&
615 TemplateParams == RHS->getRawTemplateParams() &&
616 Identifier == RHS->getRawIdentifier() &&
617 Discriminator == RHS->getRawDiscriminator() &&
618 DataLocation == RHS->getRawDataLocation() &&
619 Associated == RHS->getRawAssociated() &&
620 Allocated == RHS->getRawAllocated() && Rank == RHS->getRawRank() &&
621 Annotations == RHS->getRawAnnotations();
624 unsigned getHashValue() const {
625 // Intentionally computes the hash on a subset of the operands for
626 // performance reason. The subset has to be significant enough to avoid
627 // collision "most of the time". There is no correctness issue in case of
628 // collision because of the full check above.
629 return hash_combine(Name, File, Line, BaseType, Scope, Elements,
630 TemplateParams, Annotations);
634 template <> struct MDNodeKeyImpl<DISubroutineType> {
635 unsigned Flags;
636 uint8_t CC;
637 Metadata *TypeArray;
639 MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray)
640 : Flags(Flags), CC(CC), TypeArray(TypeArray) {}
641 MDNodeKeyImpl(const DISubroutineType *N)
642 : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {}
644 bool isKeyOf(const DISubroutineType *RHS) const {
645 return Flags == RHS->getFlags() && CC == RHS->getCC() &&
646 TypeArray == RHS->getRawTypeArray();
649 unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); }
652 template <> struct MDNodeKeyImpl<DIFile> {
653 MDString *Filename;
654 MDString *Directory;
655 Optional<DIFile::ChecksumInfo<MDString *>> Checksum;
656 Optional<MDString *> Source;
658 MDNodeKeyImpl(MDString *Filename, MDString *Directory,
659 Optional<DIFile::ChecksumInfo<MDString *>> Checksum,
660 Optional<MDString *> Source)
661 : Filename(Filename), Directory(Directory), Checksum(Checksum),
662 Source(Source) {}
663 MDNodeKeyImpl(const DIFile *N)
664 : Filename(N->getRawFilename()), Directory(N->getRawDirectory()),
665 Checksum(N->getRawChecksum()), Source(N->getRawSource()) {}
667 bool isKeyOf(const DIFile *RHS) const {
668 return Filename == RHS->getRawFilename() &&
669 Directory == RHS->getRawDirectory() &&
670 Checksum == RHS->getRawChecksum() &&
671 Source == RHS->getRawSource();
674 unsigned getHashValue() const {
675 return hash_combine(
676 Filename, Directory, Checksum ? Checksum->Kind : 0,
677 Checksum ? Checksum->Value : nullptr, Source.getValueOr(nullptr));
681 template <> struct MDNodeKeyImpl<DISubprogram> {
682 Metadata *Scope;
683 MDString *Name;
684 MDString *LinkageName;
685 Metadata *File;
686 unsigned Line;
687 Metadata *Type;
688 unsigned ScopeLine;
689 Metadata *ContainingType;
690 unsigned VirtualIndex;
691 int ThisAdjustment;
692 unsigned Flags;
693 unsigned SPFlags;
694 Metadata *Unit;
695 Metadata *TemplateParams;
696 Metadata *Declaration;
697 Metadata *RetainedNodes;
698 Metadata *ThrownTypes;
700 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
701 Metadata *File, unsigned Line, Metadata *Type,
702 unsigned ScopeLine, Metadata *ContainingType,
703 unsigned VirtualIndex, int ThisAdjustment, unsigned Flags,
704 unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams,
705 Metadata *Declaration, Metadata *RetainedNodes,
706 Metadata *ThrownTypes)
707 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
708 Line(Line), Type(Type), ScopeLine(ScopeLine),
709 ContainingType(ContainingType), VirtualIndex(VirtualIndex),
710 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags),
711 Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration),
712 RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes) {}
713 MDNodeKeyImpl(const DISubprogram *N)
714 : Scope(N->getRawScope()), Name(N->getRawName()),
715 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
716 Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()),
717 ContainingType(N->getRawContainingType()),
718 VirtualIndex(N->getVirtualIndex()),
719 ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
720 SPFlags(N->getSPFlags()), Unit(N->getRawUnit()),
721 TemplateParams(N->getRawTemplateParams()),
722 Declaration(N->getRawDeclaration()),
723 RetainedNodes(N->getRawRetainedNodes()),
724 ThrownTypes(N->getRawThrownTypes()) {}
726 bool isKeyOf(const DISubprogram *RHS) const {
727 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
728 LinkageName == RHS->getRawLinkageName() &&
729 File == RHS->getRawFile() && Line == RHS->getLine() &&
730 Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() &&
731 ContainingType == RHS->getRawContainingType() &&
732 VirtualIndex == RHS->getVirtualIndex() &&
733 ThisAdjustment == RHS->getThisAdjustment() &&
734 Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() &&
735 Unit == RHS->getUnit() &&
736 TemplateParams == RHS->getRawTemplateParams() &&
737 Declaration == RHS->getRawDeclaration() &&
738 RetainedNodes == RHS->getRawRetainedNodes() &&
739 ThrownTypes == RHS->getRawThrownTypes();
742 bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; }
744 unsigned getHashValue() const {
745 // If this is a declaration inside an ODR type, only hash the type and the
746 // name. Otherwise the hash will be stronger than
747 // MDNodeSubsetEqualImpl::isDeclarationOfODRMember().
748 if (!isDefinition() && LinkageName)
749 if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
750 if (CT->getRawIdentifier())
751 return hash_combine(LinkageName, Scope);
753 // Intentionally computes the hash on a subset of the operands for
754 // performance reason. The subset has to be significant enough to avoid
755 // collision "most of the time". There is no correctness issue in case of
756 // collision because of the full check above.
757 return hash_combine(Name, Scope, File, Type, Line);
761 template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
762 using KeyTy = MDNodeKeyImpl<DISubprogram>;
764 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
765 return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope,
766 LHS.LinkageName, LHS.TemplateParams, RHS);
769 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
770 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
771 LHS->getRawLinkageName(),
772 LHS->getRawTemplateParams(), RHS);
775 /// Subprograms compare equal if they declare the same function in an ODR
776 /// type.
777 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
778 const MDString *LinkageName,
779 const Metadata *TemplateParams,
780 const DISubprogram *RHS) {
781 // Check whether the LHS is eligible.
782 if (IsDefinition || !Scope || !LinkageName)
783 return false;
785 auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
786 if (!CT || !CT->getRawIdentifier())
787 return false;
789 // Compare to the RHS.
790 // FIXME: We need to compare template parameters here to avoid incorrect
791 // collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a
792 // ODR-DISubprogram has a non-ODR template parameter (i.e., a
793 // DICompositeType that does not have an identifier). Eventually we should
794 // decouple ODR logic from uniquing logic.
795 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
796 LinkageName == RHS->getRawLinkageName() &&
797 TemplateParams == RHS->getRawTemplateParams();
801 template <> struct MDNodeKeyImpl<DILexicalBlock> {
802 Metadata *Scope;
803 Metadata *File;
804 unsigned Line;
805 unsigned Column;
807 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
808 : Scope(Scope), File(File), Line(Line), Column(Column) {}
809 MDNodeKeyImpl(const DILexicalBlock *N)
810 : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
811 Column(N->getColumn()) {}
813 bool isKeyOf(const DILexicalBlock *RHS) const {
814 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
815 Line == RHS->getLine() && Column == RHS->getColumn();
818 unsigned getHashValue() const {
819 return hash_combine(Scope, File, Line, Column);
823 template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
824 Metadata *Scope;
825 Metadata *File;
826 unsigned Discriminator;
828 MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
829 : Scope(Scope), File(File), Discriminator(Discriminator) {}
830 MDNodeKeyImpl(const DILexicalBlockFile *N)
831 : Scope(N->getRawScope()), File(N->getRawFile()),
832 Discriminator(N->getDiscriminator()) {}
834 bool isKeyOf(const DILexicalBlockFile *RHS) const {
835 return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
836 Discriminator == RHS->getDiscriminator();
839 unsigned getHashValue() const {
840 return hash_combine(Scope, File, Discriminator);
844 template <> struct MDNodeKeyImpl<DINamespace> {
845 Metadata *Scope;
846 MDString *Name;
847 bool ExportSymbols;
849 MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols)
850 : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {}
851 MDNodeKeyImpl(const DINamespace *N)
852 : Scope(N->getRawScope()), Name(N->getRawName()),
853 ExportSymbols(N->getExportSymbols()) {}
855 bool isKeyOf(const DINamespace *RHS) const {
856 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
857 ExportSymbols == RHS->getExportSymbols();
860 unsigned getHashValue() const {
861 return hash_combine(Scope, Name);
865 template <> struct MDNodeKeyImpl<DICommonBlock> {
866 Metadata *Scope;
867 Metadata *Decl;
868 MDString *Name;
869 Metadata *File;
870 unsigned LineNo;
872 MDNodeKeyImpl(Metadata *Scope, Metadata *Decl, MDString *Name,
873 Metadata *File, unsigned LineNo)
874 : Scope(Scope), Decl(Decl), Name(Name), File(File), LineNo(LineNo) {}
875 MDNodeKeyImpl(const DICommonBlock *N)
876 : Scope(N->getRawScope()), Decl(N->getRawDecl()), Name(N->getRawName()),
877 File(N->getRawFile()), LineNo(N->getLineNo()) {}
879 bool isKeyOf(const DICommonBlock *RHS) const {
880 return Scope == RHS->getRawScope() && Decl == RHS->getRawDecl() &&
881 Name == RHS->getRawName() && File == RHS->getRawFile() &&
882 LineNo == RHS->getLineNo();
885 unsigned getHashValue() const {
886 return hash_combine(Scope, Decl, Name, File, LineNo);
890 template <> struct MDNodeKeyImpl<DIModule> {
891 Metadata *File;
892 Metadata *Scope;
893 MDString *Name;
894 MDString *ConfigurationMacros;
895 MDString *IncludePath;
896 MDString *APINotesFile;
897 unsigned LineNo;
898 bool IsDecl;
900 MDNodeKeyImpl(Metadata *File, Metadata *Scope, MDString *Name,
901 MDString *ConfigurationMacros, MDString *IncludePath,
902 MDString *APINotesFile, unsigned LineNo, bool IsDecl)
903 : File(File), Scope(Scope), Name(Name),
904 ConfigurationMacros(ConfigurationMacros), IncludePath(IncludePath),
905 APINotesFile(APINotesFile), LineNo(LineNo), IsDecl(IsDecl) {}
906 MDNodeKeyImpl(const DIModule *N)
907 : File(N->getRawFile()), Scope(N->getRawScope()), Name(N->getRawName()),
908 ConfigurationMacros(N->getRawConfigurationMacros()),
909 IncludePath(N->getRawIncludePath()),
910 APINotesFile(N->getRawAPINotesFile()), LineNo(N->getLineNo()),
911 IsDecl(N->getIsDecl()) {}
913 bool isKeyOf(const DIModule *RHS) const {
914 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
915 ConfigurationMacros == RHS->getRawConfigurationMacros() &&
916 IncludePath == RHS->getRawIncludePath() &&
917 APINotesFile == RHS->getRawAPINotesFile() &&
918 File == RHS->getRawFile() && LineNo == RHS->getLineNo() &&
919 IsDecl == RHS->getIsDecl();
922 unsigned getHashValue() const {
923 return hash_combine(Scope, Name, ConfigurationMacros, IncludePath);
927 template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
928 MDString *Name;
929 Metadata *Type;
930 bool IsDefault;
932 MDNodeKeyImpl(MDString *Name, Metadata *Type, bool IsDefault)
933 : Name(Name), Type(Type), IsDefault(IsDefault) {}
934 MDNodeKeyImpl(const DITemplateTypeParameter *N)
935 : Name(N->getRawName()), Type(N->getRawType()),
936 IsDefault(N->isDefault()) {}
938 bool isKeyOf(const DITemplateTypeParameter *RHS) const {
939 return Name == RHS->getRawName() && Type == RHS->getRawType() &&
940 IsDefault == RHS->isDefault();
943 unsigned getHashValue() const { return hash_combine(Name, Type, IsDefault); }
946 template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
947 unsigned Tag;
948 MDString *Name;
949 Metadata *Type;
950 bool IsDefault;
951 Metadata *Value;
953 MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, bool IsDefault,
954 Metadata *Value)
955 : Tag(Tag), Name(Name), Type(Type), IsDefault(IsDefault), Value(Value) {}
956 MDNodeKeyImpl(const DITemplateValueParameter *N)
957 : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()),
958 IsDefault(N->isDefault()), Value(N->getValue()) {}
960 bool isKeyOf(const DITemplateValueParameter *RHS) const {
961 return Tag == RHS->getTag() && Name == RHS->getRawName() &&
962 Type == RHS->getRawType() && IsDefault == RHS->isDefault() &&
963 Value == RHS->getValue();
966 unsigned getHashValue() const {
967 return hash_combine(Tag, Name, Type, IsDefault, Value);
971 template <> struct MDNodeKeyImpl<DIGlobalVariable> {
972 Metadata *Scope;
973 MDString *Name;
974 MDString *LinkageName;
975 Metadata *File;
976 unsigned Line;
977 Metadata *Type;
978 bool IsLocalToUnit;
979 bool IsDefinition;
980 Metadata *StaticDataMemberDeclaration;
981 Metadata *TemplateParams;
982 uint32_t AlignInBits;
984 MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
985 Metadata *File, unsigned Line, Metadata *Type,
986 bool IsLocalToUnit, bool IsDefinition,
987 Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
988 uint32_t AlignInBits)
989 : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
990 Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
991 IsDefinition(IsDefinition),
992 StaticDataMemberDeclaration(StaticDataMemberDeclaration),
993 TemplateParams(TemplateParams), AlignInBits(AlignInBits) {}
994 MDNodeKeyImpl(const DIGlobalVariable *N)
995 : Scope(N->getRawScope()), Name(N->getRawName()),
996 LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
997 Line(N->getLine()), Type(N->getRawType()),
998 IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
999 StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()),
1000 TemplateParams(N->getRawTemplateParams()),
1001 AlignInBits(N->getAlignInBits()) {}
1003 bool isKeyOf(const DIGlobalVariable *RHS) const {
1004 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
1005 LinkageName == RHS->getRawLinkageName() &&
1006 File == RHS->getRawFile() && Line == RHS->getLine() &&
1007 Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
1008 IsDefinition == RHS->isDefinition() &&
1009 StaticDataMemberDeclaration ==
1010 RHS->getRawStaticDataMemberDeclaration() &&
1011 TemplateParams == RHS->getRawTemplateParams() &&
1012 AlignInBits == RHS->getAlignInBits();
1015 unsigned getHashValue() const {
1016 // We do not use AlignInBits in hashing function here on purpose:
1017 // in most cases this param for local variable is zero (for function param
1018 // it is always zero). This leads to lots of hash collisions and errors on
1019 // cases with lots of similar variables.
1020 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
1021 // generated IR is random for each run and test fails with Align included.
1022 // TODO: make hashing work fine with such situations
1023 return hash_combine(Scope, Name, LinkageName, File, Line, Type,
1024 IsLocalToUnit, IsDefinition, /* AlignInBits, */
1025 StaticDataMemberDeclaration);
1029 template <> struct MDNodeKeyImpl<DILocalVariable> {
1030 Metadata *Scope;
1031 MDString *Name;
1032 Metadata *File;
1033 unsigned Line;
1034 Metadata *Type;
1035 unsigned Arg;
1036 unsigned Flags;
1037 uint32_t AlignInBits;
1039 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line,
1040 Metadata *Type, unsigned Arg, unsigned Flags,
1041 uint32_t AlignInBits)
1042 : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
1043 Flags(Flags), AlignInBits(AlignInBits) {}
1044 MDNodeKeyImpl(const DILocalVariable *N)
1045 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
1046 Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
1047 Flags(N->getFlags()), AlignInBits(N->getAlignInBits()) {}
1049 bool isKeyOf(const DILocalVariable *RHS) const {
1050 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
1051 File == RHS->getRawFile() && Line == RHS->getLine() &&
1052 Type == RHS->getRawType() && Arg == RHS->getArg() &&
1053 Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits();
1056 unsigned getHashValue() const {
1057 // We do not use AlignInBits in hashing function here on purpose:
1058 // in most cases this param for local variable is zero (for function param
1059 // it is always zero). This leads to lots of hash collisions and errors on
1060 // cases with lots of similar variables.
1061 // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
1062 // generated IR is random for each run and test fails with Align included.
1063 // TODO: make hashing work fine with such situations
1064 return hash_combine(Scope, Name, File, Line, Type, Arg, Flags);
1068 template <> struct MDNodeKeyImpl<DILabel> {
1069 Metadata *Scope;
1070 MDString *Name;
1071 Metadata *File;
1072 unsigned Line;
1074 MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line)
1075 : Scope(Scope), Name(Name), File(File), Line(Line) {}
1076 MDNodeKeyImpl(const DILabel *N)
1077 : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
1078 Line(N->getLine()) {}
1080 bool isKeyOf(const DILabel *RHS) const {
1081 return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
1082 File == RHS->getRawFile() && Line == RHS->getLine();
1085 /// Using name and line to get hash value. It should already be mostly unique.
1086 unsigned getHashValue() const {
1087 return hash_combine(Scope, Name, Line);
1091 template <> struct MDNodeKeyImpl<DIExpression> {
1092 ArrayRef<uint64_t> Elements;
1094 MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
1095 MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
1097 bool isKeyOf(const DIExpression *RHS) const {
1098 return Elements == RHS->getElements();
1101 unsigned getHashValue() const {
1102 return hash_combine_range(Elements.begin(), Elements.end());
1106 template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> {
1107 Metadata *Variable;
1108 Metadata *Expression;
1110 MDNodeKeyImpl(Metadata *Variable, Metadata *Expression)
1111 : Variable(Variable), Expression(Expression) {}
1112 MDNodeKeyImpl(const DIGlobalVariableExpression *N)
1113 : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {}
1115 bool isKeyOf(const DIGlobalVariableExpression *RHS) const {
1116 return Variable == RHS->getRawVariable() &&
1117 Expression == RHS->getRawExpression();
1120 unsigned getHashValue() const { return hash_combine(Variable, Expression); }
1123 template <> struct MDNodeKeyImpl<DIObjCProperty> {
1124 MDString *Name;
1125 Metadata *File;
1126 unsigned Line;
1127 MDString *GetterName;
1128 MDString *SetterName;
1129 unsigned Attributes;
1130 Metadata *Type;
1132 MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line,
1133 MDString *GetterName, MDString *SetterName, unsigned Attributes,
1134 Metadata *Type)
1135 : Name(Name), File(File), Line(Line), GetterName(GetterName),
1136 SetterName(SetterName), Attributes(Attributes), Type(Type) {}
1137 MDNodeKeyImpl(const DIObjCProperty *N)
1138 : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()),
1139 GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()),
1140 Attributes(N->getAttributes()), Type(N->getRawType()) {}
1142 bool isKeyOf(const DIObjCProperty *RHS) const {
1143 return Name == RHS->getRawName() && File == RHS->getRawFile() &&
1144 Line == RHS->getLine() && GetterName == RHS->getRawGetterName() &&
1145 SetterName == RHS->getRawSetterName() &&
1146 Attributes == RHS->getAttributes() && Type == RHS->getRawType();
1149 unsigned getHashValue() const {
1150 return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
1151 Type);
1155 template <> struct MDNodeKeyImpl<DIImportedEntity> {
1156 unsigned Tag;
1157 Metadata *Scope;
1158 Metadata *Entity;
1159 Metadata *File;
1160 unsigned Line;
1161 MDString *Name;
1163 MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File,
1164 unsigned Line, MDString *Name)
1165 : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line),
1166 Name(Name) {}
1167 MDNodeKeyImpl(const DIImportedEntity *N)
1168 : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
1169 File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()) {}
1171 bool isKeyOf(const DIImportedEntity *RHS) const {
1172 return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
1173 Entity == RHS->getRawEntity() && File == RHS->getFile() &&
1174 Line == RHS->getLine() && Name == RHS->getRawName();
1177 unsigned getHashValue() const {
1178 return hash_combine(Tag, Scope, Entity, File, Line, Name);
1182 template <> struct MDNodeKeyImpl<DIMacro> {
1183 unsigned MIType;
1184 unsigned Line;
1185 MDString *Name;
1186 MDString *Value;
1188 MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value)
1189 : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
1190 MDNodeKeyImpl(const DIMacro *N)
1191 : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()),
1192 Value(N->getRawValue()) {}
1194 bool isKeyOf(const DIMacro *RHS) const {
1195 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
1196 Name == RHS->getRawName() && Value == RHS->getRawValue();
1199 unsigned getHashValue() const {
1200 return hash_combine(MIType, Line, Name, Value);
1204 template <> struct MDNodeKeyImpl<DIMacroFile> {
1205 unsigned MIType;
1206 unsigned Line;
1207 Metadata *File;
1208 Metadata *Elements;
1210 MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
1211 Metadata *Elements)
1212 : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
1213 MDNodeKeyImpl(const DIMacroFile *N)
1214 : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
1215 Elements(N->getRawElements()) {}
1217 bool isKeyOf(const DIMacroFile *RHS) const {
1218 return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
1219 File == RHS->getRawFile() && Elements == RHS->getRawElements();
1222 unsigned getHashValue() const {
1223 return hash_combine(MIType, Line, File, Elements);
1227 template <> struct MDNodeKeyImpl<DIArgList> {
1228 ArrayRef<ValueAsMetadata *> Args;
1230 MDNodeKeyImpl(ArrayRef<ValueAsMetadata *> Args) : Args(Args) {}
1231 MDNodeKeyImpl(const DIArgList *N) : Args(N->getArgs()) {}
1233 bool isKeyOf(const DIArgList *RHS) const { return Args == RHS->getArgs(); }
1235 unsigned getHashValue() const {
1236 return hash_combine_range(Args.begin(), Args.end());
1240 /// DenseMapInfo for MDNode subclasses.
1241 template <class NodeTy> struct MDNodeInfo {
1242 using KeyTy = MDNodeKeyImpl<NodeTy>;
1243 using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>;
1245 static inline NodeTy *getEmptyKey() {
1246 return DenseMapInfo<NodeTy *>::getEmptyKey();
1249 static inline NodeTy *getTombstoneKey() {
1250 return DenseMapInfo<NodeTy *>::getTombstoneKey();
1253 static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
1255 static unsigned getHashValue(const NodeTy *N) {
1256 return KeyTy(N).getHashValue();
1259 static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
1260 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1261 return false;
1262 return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS);
1265 static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
1266 if (LHS == RHS)
1267 return true;
1268 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1269 return false;
1270 return SubsetEqualTy::isSubsetEqual(LHS, RHS);
1274 #define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>;
1275 #include "llvm/IR/Metadata.def"
1277 /// Multimap-like storage for metadata attachments.
1278 class MDAttachments {
1279 public:
1280 struct Attachment {
1281 unsigned MDKind;
1282 TrackingMDNodeRef Node;
1285 private:
1286 SmallVector<Attachment, 1> Attachments;
1288 public:
1289 bool empty() const { return Attachments.empty(); }
1290 size_t size() const { return Attachments.size(); }
1292 /// Returns the first attachment with the given ID or nullptr if no such
1293 /// attachment exists.
1294 MDNode *lookup(unsigned ID) const;
1296 /// Appends all attachments with the given ID to \c Result in insertion order.
1297 /// If the global has no attachments with the given ID, or if ID is invalid,
1298 /// leaves Result unchanged.
1299 void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const;
1301 /// Appends all attachments for the global to \c Result, sorting by attachment
1302 /// ID. Attachments with the same ID appear in insertion order. This function
1303 /// does \em not clear \c Result.
1304 void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
1306 /// Set an attachment to a particular node.
1308 /// Set the \c ID attachment to \c MD, replacing the current attachments at \c
1309 /// ID (if anyway).
1310 void set(unsigned ID, MDNode *MD);
1312 /// Adds an attachment to a particular node.
1313 void insert(unsigned ID, MDNode &MD);
1315 /// Remove attachments with the given ID.
1317 /// Remove the attachments at \c ID, if any.
1318 bool erase(unsigned ID);
1320 /// Erase matching attachments.
1322 /// Erases all attachments matching the \c shouldRemove predicate.
1323 template <class PredTy> void remove_if(PredTy shouldRemove) {
1324 llvm::erase_if(Attachments, shouldRemove);
1328 class LLVMContextImpl {
1329 public:
1330 /// OwnedModules - The set of modules instantiated in this context, and which
1331 /// will be automatically deleted if this context is deleted.
1332 SmallPtrSet<Module*, 4> OwnedModules;
1334 /// The main remark streamer used by all the other streamers (e.g. IR, MIR,
1335 /// frontends, etc.). This should only be used by the specific streamers, and
1336 /// never directly.
1337 std::unique_ptr<remarks::RemarkStreamer> MainRemarkStreamer;
1339 std::unique_ptr<DiagnosticHandler> DiagHandler;
1340 bool RespectDiagnosticFilters = false;
1341 bool DiagnosticsHotnessRequested = false;
1342 /// The minimum hotness value a diagnostic needs in order to be included in
1343 /// optimization diagnostics.
1345 /// The threshold is an Optional value, which maps to one of the 3 states:
1346 /// 1). 0 => threshold disabled. All emarks will be printed.
1347 /// 2). positive int => manual threshold by user. Remarks with hotness exceed
1348 /// threshold will be printed.
1349 /// 3). None => 'auto' threshold by user. The actual value is not
1350 /// available at command line, but will be synced with
1351 /// hotness threhold from profile summary during
1352 /// compilation.
1354 /// State 1 and 2 are considered as terminal states. State transition is
1355 /// only allowed from 3 to 2, when the threshold is first synced with profile
1356 /// summary. This ensures that the threshold is set only once and stays
1357 /// constant.
1359 /// If threshold option is not specified, it is disabled (0) by default.
1360 Optional<uint64_t> DiagnosticsHotnessThreshold = 0;
1362 /// The specialized remark streamer used by LLVM's OptimizationRemarkEmitter.
1363 std::unique_ptr<LLVMRemarkStreamer> LLVMRS;
1365 LLVMContext::YieldCallbackTy YieldCallback = nullptr;
1366 void *YieldOpaqueHandle = nullptr;
1368 using IntMapTy =
1369 DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>;
1370 IntMapTy IntConstants;
1372 using FPMapTy =
1373 DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>;
1374 FPMapTy FPConstants;
1376 FoldingSet<AttributeImpl> AttrsSet;
1377 FoldingSet<AttributeListImpl> AttrsLists;
1378 FoldingSet<AttributeSetNode> AttrsSetNodes;
1380 StringMap<MDString, BumpPtrAllocator> MDStringCache;
1381 DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
1382 DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
1384 DenseMap<const Value*, ValueName*> ValueNames;
1386 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
1387 DenseSet<CLASS *, CLASS##Info> CLASS##s;
1388 #include "llvm/IR/Metadata.def"
1390 // Optional map for looking up composite types by identifier.
1391 Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap;
1393 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
1394 // aren't in the MDNodeSet, but they're still shared between objects, so no
1395 // one object can destroy them. Keep track of them here so we can delete
1396 // them on context teardown.
1397 std::vector<MDNode *> DistinctMDNodes;
1399 DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants;
1401 using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>;
1402 ArrayConstantsTy ArrayConstants;
1404 using StructConstantsTy = ConstantUniqueMap<ConstantStruct>;
1405 StructConstantsTy StructConstants;
1407 using VectorConstantsTy = ConstantUniqueMap<ConstantVector>;
1408 VectorConstantsTy VectorConstants;
1410 DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants;
1412 DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants;
1414 DenseMap<Type *, std::unique_ptr<PoisonValue>> PVConstants;
1416 StringMap<std::unique_ptr<ConstantDataSequential>> CDSConstants;
1418 DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
1419 BlockAddresses;
1421 DenseMap<const GlobalValue *, DSOLocalEquivalent *> DSOLocalEquivalents;
1423 ConstantUniqueMap<ConstantExpr> ExprConstants;
1425 ConstantUniqueMap<InlineAsm> InlineAsms;
1427 ConstantInt *TheTrueVal = nullptr;
1428 ConstantInt *TheFalseVal = nullptr;
1430 std::unique_ptr<ConstantTokenNone> TheNoneToken;
1432 // Basic type instances.
1433 Type VoidTy, LabelTy, HalfTy, BFloatTy, FloatTy, DoubleTy, MetadataTy,
1434 TokenTy;
1435 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy, X86_AMXTy;
1436 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
1438 BumpPtrAllocator Alloc;
1439 UniqueStringSaver Saver{Alloc};
1441 DenseMap<unsigned, IntegerType*> IntegerTypes;
1443 using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>;
1444 FunctionTypeSet FunctionTypes;
1445 using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>;
1446 StructTypeSet AnonStructTypes;
1447 StringMap<StructType*> NamedStructTypes;
1448 unsigned NamedStructTypesUniqueID = 0;
1450 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes;
1451 DenseMap<std::pair<Type *, ElementCount>, VectorType*> VectorTypes;
1452 // TODO: clean up the following after we no longer support non-opaque pointer
1453 // types.
1454 bool ForceOpaquePointers;
1455 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0
1456 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes;
1458 /// ValueHandles - This map keeps track of all of the value handles that are
1459 /// watching a Value*. The Value::HasValueHandle bit is used to know
1460 /// whether or not a value has an entry in this map.
1461 using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>;
1462 ValueHandlesTy ValueHandles;
1464 /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
1465 StringMap<unsigned> CustomMDKindNames;
1467 /// Collection of metadata used in this context.
1468 DenseMap<const Value *, MDAttachments> ValueMetadata;
1470 /// Collection of per-GlobalObject sections used in this context.
1471 DenseMap<const GlobalObject *, StringRef> GlobalObjectSections;
1473 /// Collection of per-GlobalValue partitions used in this context.
1474 DenseMap<const GlobalValue *, StringRef> GlobalValuePartitions;
1476 /// DiscriminatorTable - This table maps file:line locations to an
1477 /// integer representing the next DWARF path discriminator to assign to
1478 /// instructions in different blocks at the same location.
1479 DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
1481 /// A set of interned tags for operand bundles. The StringMap maps
1482 /// bundle tags to their IDs.
1484 /// \see LLVMContext::getOperandBundleTagID
1485 StringMap<uint32_t> BundleTagCache;
1487 StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
1488 void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
1489 uint32_t getOperandBundleTagID(StringRef Tag) const;
1491 /// A set of interned synchronization scopes. The StringMap maps
1492 /// synchronization scope names to their respective synchronization scope IDs.
1493 StringMap<SyncScope::ID> SSC;
1495 /// getOrInsertSyncScopeID - Maps synchronization scope name to
1496 /// synchronization scope ID. Every synchronization scope registered with
1497 /// LLVMContext has unique ID except pre-defined ones.
1498 SyncScope::ID getOrInsertSyncScopeID(StringRef SSN);
1500 /// getSyncScopeNames - Populates client supplied SmallVector with
1501 /// synchronization scope names registered with LLVMContext. Synchronization
1502 /// scope names are ordered by increasing synchronization scope IDs.
1503 void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const;
1505 /// Maintain the GC name for each function.
1507 /// This saves allocating an additional word in Function for programs which
1508 /// do not use GC (i.e., most programs) at the cost of increased overhead for
1509 /// clients which do use GC.
1510 DenseMap<const Function*, std::string> GCNames;
1512 /// Flag to indicate if Value (other than GlobalValue) retains their name or
1513 /// not.
1514 bool DiscardValueNames = false;
1516 LLVMContextImpl(LLVMContext &C);
1517 ~LLVMContextImpl();
1519 /// Destroy the ConstantArrays if they are not used.
1520 void dropTriviallyDeadConstantArrays();
1522 mutable OptPassGate *OPG = nullptr;
1524 /// Access the object which can disable optional passes and individual
1525 /// optimizations at compile time.
1526 OptPassGate &getOptPassGate() const;
1528 /// Set the object which can disable optional passes and individual
1529 /// optimizations at compile time.
1531 /// The lifetime of the object must be guaranteed to extend as long as the
1532 /// LLVMContext is used by compilation.
1533 void setOptPassGate(OptPassGate&);
1536 } // end namespace llvm
1538 #endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H