10b57cec5SDimitry Andric //===- LLVMContextImpl.h - The LLVMContextImpl opaque class -----*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file declares LLVMContextImpl, the opaque implementation 100b57cec5SDimitry Andric // of LLVMContext. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H 150b57cec5SDimitry Andric #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "AttributeImpl.h" 180b57cec5SDimitry Andric #include "ConstantsContext.h" 190b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h" 200b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 210b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 220b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 230b57cec5SDimitry Andric #include "llvm/ADT/DenseMapInfo.h" 240b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 250b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 260b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h" 270b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 280b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 290b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 300b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 310b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h" 320b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 330b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 340b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 350b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 360b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 375ffd83dbSDimitry Andric #include "llvm/IR/LLVMRemarkStreamer.h" 380b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 390b57cec5SDimitry Andric #include "llvm/IR/TrackingMDRef.h" 400b57cec5SDimitry Andric #include "llvm/Support/Allocator.h" 410b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 420b57cec5SDimitry Andric #include "llvm/Support/StringSaver.h" 430b57cec5SDimitry Andric #include "llvm/Support/YAMLTraits.h" 440b57cec5SDimitry Andric #include <algorithm> 450b57cec5SDimitry Andric #include <cassert> 460b57cec5SDimitry Andric #include <cstddef> 470b57cec5SDimitry Andric #include <cstdint> 480b57cec5SDimitry Andric #include <memory> 490b57cec5SDimitry Andric #include <string> 500b57cec5SDimitry Andric #include <utility> 510b57cec5SDimitry Andric #include <vector> 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric namespace llvm { 540b57cec5SDimitry Andric 555ffd83dbSDimitry Andric class StringRef; 560b57cec5SDimitry Andric class Type; 570b57cec5SDimitry Andric class Value; 580b57cec5SDimitry Andric class ValueHandleBase; 590b57cec5SDimitry Andric 60e8d8bef9SDimitry Andric using DenseMapAPIntKeyInfo = DenseMapInfo<APInt>; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric struct DenseMapAPFloatKeyInfo { 630b57cec5SDimitry Andric static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); } 64*349cc55cSDimitry Andric static inline APFloat getTombstoneKey() { 65*349cc55cSDimitry Andric return APFloat(APFloat::Bogus(), 2); 66*349cc55cSDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric static unsigned getHashValue(const APFloat &Key) { 690b57cec5SDimitry Andric return static_cast<unsigned>(hash_value(Key)); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric static bool isEqual(const APFloat &LHS, const APFloat &RHS) { 730b57cec5SDimitry Andric return LHS.bitwiseIsEqual(RHS); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric }; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric struct AnonStructTypeKeyInfo { 780b57cec5SDimitry Andric struct KeyTy { 790b57cec5SDimitry Andric ArrayRef<Type *> ETypes; 800b57cec5SDimitry Andric bool isPacked; 810b57cec5SDimitry Andric 82*349cc55cSDimitry Andric KeyTy(const ArrayRef<Type *> &E, bool P) : ETypes(E), isPacked(P) {} 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric KeyTy(const StructType *ST) 850b57cec5SDimitry Andric : ETypes(ST->elements()), isPacked(ST->isPacked()) {} 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric bool operator==(const KeyTy &that) const { 880b57cec5SDimitry Andric if (isPacked != that.isPacked) 890b57cec5SDimitry Andric return false; 900b57cec5SDimitry Andric if (ETypes != that.ETypes) 910b57cec5SDimitry Andric return false; 920b57cec5SDimitry Andric return true; 930b57cec5SDimitry Andric } 94*349cc55cSDimitry Andric bool operator!=(const KeyTy &that) const { return !this->operator==(that); } 950b57cec5SDimitry Andric }; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric static inline StructType *getEmptyKey() { 980b57cec5SDimitry Andric return DenseMapInfo<StructType *>::getEmptyKey(); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric static inline StructType *getTombstoneKey() { 1020b57cec5SDimitry Andric return DenseMapInfo<StructType *>::getTombstoneKey(); 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric static unsigned getHashValue(const KeyTy &Key) { 106*349cc55cSDimitry Andric return hash_combine( 107*349cc55cSDimitry Andric hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()), Key.isPacked); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric static unsigned getHashValue(const StructType *ST) { 1110b57cec5SDimitry Andric return getHashValue(KeyTy(ST)); 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric static bool isEqual(const KeyTy &LHS, const StructType *RHS) { 1150b57cec5SDimitry Andric if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1160b57cec5SDimitry Andric return false; 1170b57cec5SDimitry Andric return LHS == KeyTy(RHS); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric static bool isEqual(const StructType *LHS, const StructType *RHS) { 1210b57cec5SDimitry Andric return LHS == RHS; 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric }; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric struct FunctionTypeKeyInfo { 1260b57cec5SDimitry Andric struct KeyTy { 1270b57cec5SDimitry Andric const Type *ReturnType; 1280b57cec5SDimitry Andric ArrayRef<Type *> Params; 1290b57cec5SDimitry Andric bool isVarArg; 1300b57cec5SDimitry Andric 131*349cc55cSDimitry Andric KeyTy(const Type *R, const ArrayRef<Type *> &P, bool V) 132*349cc55cSDimitry Andric : ReturnType(R), Params(P), isVarArg(V) {} 1330b57cec5SDimitry Andric KeyTy(const FunctionType *FT) 1340b57cec5SDimitry Andric : ReturnType(FT->getReturnType()), Params(FT->params()), 1350b57cec5SDimitry Andric isVarArg(FT->isVarArg()) {} 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric bool operator==(const KeyTy &that) const { 1380b57cec5SDimitry Andric if (ReturnType != that.ReturnType) 1390b57cec5SDimitry Andric return false; 1400b57cec5SDimitry Andric if (isVarArg != that.isVarArg) 1410b57cec5SDimitry Andric return false; 1420b57cec5SDimitry Andric if (Params != that.Params) 1430b57cec5SDimitry Andric return false; 1440b57cec5SDimitry Andric return true; 1450b57cec5SDimitry Andric } 146*349cc55cSDimitry Andric bool operator!=(const KeyTy &that) const { return !this->operator==(that); } 1470b57cec5SDimitry Andric }; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric static inline FunctionType *getEmptyKey() { 1500b57cec5SDimitry Andric return DenseMapInfo<FunctionType *>::getEmptyKey(); 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric static inline FunctionType *getTombstoneKey() { 1540b57cec5SDimitry Andric return DenseMapInfo<FunctionType *>::getTombstoneKey(); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric static unsigned getHashValue(const KeyTy &Key) { 158*349cc55cSDimitry Andric return hash_combine( 159*349cc55cSDimitry Andric Key.ReturnType, 160*349cc55cSDimitry Andric hash_combine_range(Key.Params.begin(), Key.Params.end()), Key.isVarArg); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric static unsigned getHashValue(const FunctionType *FT) { 1640b57cec5SDimitry Andric return getHashValue(KeyTy(FT)); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric static bool isEqual(const KeyTy &LHS, const FunctionType *RHS) { 1680b57cec5SDimitry Andric if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 1690b57cec5SDimitry Andric return false; 1700b57cec5SDimitry Andric return LHS == KeyTy(RHS); 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 1740b57cec5SDimitry Andric return LHS == RHS; 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric }; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric /// Structure for hashing arbitrary MDNode operands. 1790b57cec5SDimitry Andric class MDNodeOpsKey { 1800b57cec5SDimitry Andric ArrayRef<Metadata *> RawOps; 1810b57cec5SDimitry Andric ArrayRef<MDOperand> Ops; 1820b57cec5SDimitry Andric unsigned Hash; 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric protected: 1850b57cec5SDimitry Andric MDNodeOpsKey(ArrayRef<Metadata *> Ops) 1860b57cec5SDimitry Andric : RawOps(Ops), Hash(calculateHash(Ops)) {} 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric template <class NodeTy> 1890b57cec5SDimitry Andric MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0) 1900b57cec5SDimitry Andric : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {} 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric template <class NodeTy> 1930b57cec5SDimitry Andric bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const { 1940b57cec5SDimitry Andric if (getHash() != RHS->getHash()) 1950b57cec5SDimitry Andric return false; 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); 1980b57cec5SDimitry Andric return RawOps.empty() ? compareOps(Ops, RHS, Offset) 1990b57cec5SDimitry Andric : compareOps(RawOps, RHS, Offset); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric static unsigned calculateHash(MDNode *N, unsigned Offset = 0); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric private: 2050b57cec5SDimitry Andric template <class T> 2060b57cec5SDimitry Andric static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) { 2070b57cec5SDimitry Andric if (Ops.size() != RHS->getNumOperands() - Offset) 2080b57cec5SDimitry Andric return false; 2090b57cec5SDimitry Andric return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric static unsigned calculateHash(ArrayRef<Metadata *> Ops); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric public: 2150b57cec5SDimitry Andric unsigned getHash() const { return Hash; } 2160b57cec5SDimitry Andric }; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric template <class NodeTy> struct MDNodeKeyImpl; 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric /// Configuration point for MDNodeInfo::isEqual(). 2210b57cec5SDimitry Andric template <class NodeTy> struct MDNodeSubsetEqualImpl { 2220b57cec5SDimitry Andric using KeyTy = MDNodeKeyImpl<NodeTy>; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) { 2250b57cec5SDimitry Andric return false; 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) { 2290b57cec5SDimitry Andric return false; 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric }; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric /// DenseMapInfo for MDTuple. 2340b57cec5SDimitry Andric /// 2350b57cec5SDimitry Andric /// Note that we don't need the is-function-local bit, since that's implicit in 2360b57cec5SDimitry Andric /// the operands. 2370b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey { 2380b57cec5SDimitry Andric MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {} 2390b57cec5SDimitry Andric MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {} 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric unsigned getHashValue() const { return getHash(); } 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric static unsigned calculateHash(MDTuple *N) { 2460b57cec5SDimitry Andric return MDNodeOpsKey::calculateHash(N); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric }; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric /// DenseMapInfo for DILocation. 2510b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DILocation> { 2520b57cec5SDimitry Andric unsigned Line; 2530b57cec5SDimitry Andric unsigned Column; 2540b57cec5SDimitry Andric Metadata *Scope; 2550b57cec5SDimitry Andric Metadata *InlinedAt; 2560b57cec5SDimitry Andric bool ImplicitCode; 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope, 2590b57cec5SDimitry Andric Metadata *InlinedAt, bool ImplicitCode) 2600b57cec5SDimitry Andric : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt), 2610b57cec5SDimitry Andric ImplicitCode(ImplicitCode) {} 2620b57cec5SDimitry Andric MDNodeKeyImpl(const DILocation *L) 2630b57cec5SDimitry Andric : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()), 2640b57cec5SDimitry Andric InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {} 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric bool isKeyOf(const DILocation *RHS) const { 2670b57cec5SDimitry Andric return Line == RHS->getLine() && Column == RHS->getColumn() && 2680b57cec5SDimitry Andric Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() && 2690b57cec5SDimitry Andric ImplicitCode == RHS->isImplicitCode(); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric unsigned getHashValue() const { 2730b57cec5SDimitry Andric return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric }; 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric /// DenseMapInfo for GenericDINode. 2780b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey { 2790b57cec5SDimitry Andric unsigned Tag; 2800b57cec5SDimitry Andric MDString *Header; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps) 2830b57cec5SDimitry Andric : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {} 2840b57cec5SDimitry Andric MDNodeKeyImpl(const GenericDINode *N) 2850b57cec5SDimitry Andric : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {} 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric bool isKeyOf(const GenericDINode *RHS) const { 2880b57cec5SDimitry Andric return Tag == RHS->getTag() && Header == RHS->getRawHeader() && 2890b57cec5SDimitry Andric compareOps(RHS, 1); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric static unsigned calculateHash(GenericDINode *N) { 2950b57cec5SDimitry Andric return MDNodeOpsKey::calculateHash(N, 1); 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric }; 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DISubrange> { 3000b57cec5SDimitry Andric Metadata *CountNode; 3015ffd83dbSDimitry Andric Metadata *LowerBound; 3025ffd83dbSDimitry Andric Metadata *UpperBound; 3035ffd83dbSDimitry Andric Metadata *Stride; 3040b57cec5SDimitry Andric 3055ffd83dbSDimitry Andric MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 3065ffd83dbSDimitry Andric Metadata *Stride) 3075ffd83dbSDimitry Andric : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 3085ffd83dbSDimitry Andric Stride(Stride) {} 3090b57cec5SDimitry Andric MDNodeKeyImpl(const DISubrange *N) 3105ffd83dbSDimitry Andric : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 3115ffd83dbSDimitry Andric UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric bool isKeyOf(const DISubrange *RHS) const { 3145ffd83dbSDimitry Andric auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool { 3155ffd83dbSDimitry Andric if (Node1 == Node2) 3160b57cec5SDimitry Andric return true; 3170b57cec5SDimitry Andric 3185ffd83dbSDimitry Andric ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1); 3195ffd83dbSDimitry Andric ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2); 3205ffd83dbSDimitry Andric if (MD1 && MD2) { 3215ffd83dbSDimitry Andric ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue()); 3225ffd83dbSDimitry Andric ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue()); 3235ffd83dbSDimitry Andric if (CV1->getSExtValue() == CV2->getSExtValue()) 3245ffd83dbSDimitry Andric return true; 3255ffd83dbSDimitry Andric } 3265ffd83dbSDimitry Andric return false; 3275ffd83dbSDimitry Andric }; 3285ffd83dbSDimitry Andric 3295ffd83dbSDimitry Andric return BoundsEqual(CountNode, RHS->getRawCountNode()) && 3305ffd83dbSDimitry Andric BoundsEqual(LowerBound, RHS->getRawLowerBound()) && 3315ffd83dbSDimitry Andric BoundsEqual(UpperBound, RHS->getRawUpperBound()) && 3325ffd83dbSDimitry Andric BoundsEqual(Stride, RHS->getRawStride()); 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric unsigned getHashValue() const { 3365ffd83dbSDimitry Andric if (CountNode) 3370b57cec5SDimitry Andric if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode)) 3380b57cec5SDimitry Andric return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 3395ffd83dbSDimitry Andric LowerBound, UpperBound, Stride); 3405ffd83dbSDimitry Andric return hash_combine(CountNode, LowerBound, UpperBound, Stride); 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric }; 3430b57cec5SDimitry Andric 344e8d8bef9SDimitry Andric template <> struct MDNodeKeyImpl<DIGenericSubrange> { 345e8d8bef9SDimitry Andric Metadata *CountNode; 346e8d8bef9SDimitry Andric Metadata *LowerBound; 347e8d8bef9SDimitry Andric Metadata *UpperBound; 348e8d8bef9SDimitry Andric Metadata *Stride; 349e8d8bef9SDimitry Andric 350e8d8bef9SDimitry Andric MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound, 351e8d8bef9SDimitry Andric Metadata *Stride) 352e8d8bef9SDimitry Andric : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound), 353e8d8bef9SDimitry Andric Stride(Stride) {} 354e8d8bef9SDimitry Andric MDNodeKeyImpl(const DIGenericSubrange *N) 355e8d8bef9SDimitry Andric : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()), 356e8d8bef9SDimitry Andric UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {} 357e8d8bef9SDimitry Andric 358e8d8bef9SDimitry Andric bool isKeyOf(const DIGenericSubrange *RHS) const { 359e8d8bef9SDimitry Andric return (CountNode == RHS->getRawCountNode()) && 360e8d8bef9SDimitry Andric (LowerBound == RHS->getRawLowerBound()) && 361e8d8bef9SDimitry Andric (UpperBound == RHS->getRawUpperBound()) && 362e8d8bef9SDimitry Andric (Stride == RHS->getRawStride()); 363e8d8bef9SDimitry Andric } 364e8d8bef9SDimitry Andric 365e8d8bef9SDimitry Andric unsigned getHashValue() const { 366e8d8bef9SDimitry Andric auto *MD = dyn_cast_or_null<ConstantAsMetadata>(CountNode); 367e8d8bef9SDimitry Andric if (CountNode && MD) 368e8d8bef9SDimitry Andric return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(), 369e8d8bef9SDimitry Andric LowerBound, UpperBound, Stride); 370e8d8bef9SDimitry Andric return hash_combine(CountNode, LowerBound, UpperBound, Stride); 371e8d8bef9SDimitry Andric } 372e8d8bef9SDimitry Andric }; 373e8d8bef9SDimitry Andric 3740b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIEnumerator> { 3755ffd83dbSDimitry Andric APInt Value; 3760b57cec5SDimitry Andric MDString *Name; 3770b57cec5SDimitry Andric bool IsUnsigned; 3780b57cec5SDimitry Andric 3795ffd83dbSDimitry Andric MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name) 3800b57cec5SDimitry Andric : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {} 3815ffd83dbSDimitry Andric MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name) 3825ffd83dbSDimitry Andric : Value(APInt(64, Value, !IsUnsigned)), Name(Name), 3835ffd83dbSDimitry Andric IsUnsigned(IsUnsigned) {} 3840b57cec5SDimitry Andric MDNodeKeyImpl(const DIEnumerator *N) 3850b57cec5SDimitry Andric : Value(N->getValue()), Name(N->getRawName()), 3860b57cec5SDimitry Andric IsUnsigned(N->isUnsigned()) {} 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric bool isKeyOf(const DIEnumerator *RHS) const { 3895ffd83dbSDimitry Andric return APInt::isSameValue(Value, RHS->getValue()) && 3905ffd83dbSDimitry Andric IsUnsigned == RHS->isUnsigned() && Name == RHS->getRawName(); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric unsigned getHashValue() const { return hash_combine(Value, Name); } 3940b57cec5SDimitry Andric }; 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIBasicType> { 3970b57cec5SDimitry Andric unsigned Tag; 3980b57cec5SDimitry Andric MDString *Name; 3990b57cec5SDimitry Andric uint64_t SizeInBits; 4000b57cec5SDimitry Andric uint32_t AlignInBits; 4010b57cec5SDimitry Andric unsigned Encoding; 4020b57cec5SDimitry Andric unsigned Flags; 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits, 4050b57cec5SDimitry Andric uint32_t AlignInBits, unsigned Encoding, unsigned Flags) 4060b57cec5SDimitry Andric : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits), 4070b57cec5SDimitry Andric Encoding(Encoding), Flags(Flags) {} 4080b57cec5SDimitry Andric MDNodeKeyImpl(const DIBasicType *N) 4090b57cec5SDimitry Andric : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()), 410*349cc55cSDimitry Andric AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()), 411*349cc55cSDimitry Andric Flags(N->getFlags()) {} 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric bool isKeyOf(const DIBasicType *RHS) const { 4140b57cec5SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 4150b57cec5SDimitry Andric SizeInBits == RHS->getSizeInBits() && 4160b57cec5SDimitry Andric AlignInBits == RHS->getAlignInBits() && 417*349cc55cSDimitry Andric Encoding == RHS->getEncoding() && Flags == RHS->getFlags(); 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric unsigned getHashValue() const { 4210b57cec5SDimitry Andric return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric }; 4240b57cec5SDimitry Andric 425e8d8bef9SDimitry Andric template <> struct MDNodeKeyImpl<DIStringType> { 426e8d8bef9SDimitry Andric unsigned Tag; 427e8d8bef9SDimitry Andric MDString *Name; 428e8d8bef9SDimitry Andric Metadata *StringLength; 429e8d8bef9SDimitry Andric Metadata *StringLengthExp; 430e8d8bef9SDimitry Andric uint64_t SizeInBits; 431e8d8bef9SDimitry Andric uint32_t AlignInBits; 432e8d8bef9SDimitry Andric unsigned Encoding; 433e8d8bef9SDimitry Andric 434e8d8bef9SDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *StringLength, 435e8d8bef9SDimitry Andric Metadata *StringLengthExp, uint64_t SizeInBits, 436e8d8bef9SDimitry Andric uint32_t AlignInBits, unsigned Encoding) 437e8d8bef9SDimitry Andric : Tag(Tag), Name(Name), StringLength(StringLength), 438e8d8bef9SDimitry Andric StringLengthExp(StringLengthExp), SizeInBits(SizeInBits), 439e8d8bef9SDimitry Andric AlignInBits(AlignInBits), Encoding(Encoding) {} 440e8d8bef9SDimitry Andric MDNodeKeyImpl(const DIStringType *N) 441e8d8bef9SDimitry Andric : Tag(N->getTag()), Name(N->getRawName()), 442e8d8bef9SDimitry Andric StringLength(N->getRawStringLength()), 443e8d8bef9SDimitry Andric StringLengthExp(N->getRawStringLengthExp()), 444e8d8bef9SDimitry Andric SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), 445e8d8bef9SDimitry Andric Encoding(N->getEncoding()) {} 446e8d8bef9SDimitry Andric 447e8d8bef9SDimitry Andric bool isKeyOf(const DIStringType *RHS) const { 448e8d8bef9SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 449e8d8bef9SDimitry Andric SizeInBits == RHS->getSizeInBits() && 450e8d8bef9SDimitry Andric AlignInBits == RHS->getAlignInBits() && 451e8d8bef9SDimitry Andric Encoding == RHS->getEncoding(); 452e8d8bef9SDimitry Andric } 453e8d8bef9SDimitry Andric unsigned getHashValue() const { return hash_combine(Tag, Name, Encoding); } 454e8d8bef9SDimitry Andric }; 455e8d8bef9SDimitry Andric 4560b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIDerivedType> { 4570b57cec5SDimitry Andric unsigned Tag; 4580b57cec5SDimitry Andric MDString *Name; 4590b57cec5SDimitry Andric Metadata *File; 4600b57cec5SDimitry Andric unsigned Line; 4610b57cec5SDimitry Andric Metadata *Scope; 4620b57cec5SDimitry Andric Metadata *BaseType; 4630b57cec5SDimitry Andric uint64_t SizeInBits; 4640b57cec5SDimitry Andric uint64_t OffsetInBits; 4650b57cec5SDimitry Andric uint32_t AlignInBits; 4660b57cec5SDimitry Andric Optional<unsigned> DWARFAddressSpace; 4670b57cec5SDimitry Andric unsigned Flags; 4680b57cec5SDimitry Andric Metadata *ExtraData; 469*349cc55cSDimitry Andric Metadata *Annotations; 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 4720b57cec5SDimitry Andric Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 4730b57cec5SDimitry Andric uint32_t AlignInBits, uint64_t OffsetInBits, 4740b57cec5SDimitry Andric Optional<unsigned> DWARFAddressSpace, unsigned Flags, 475*349cc55cSDimitry Andric Metadata *ExtraData, Metadata *Annotations) 4760b57cec5SDimitry Andric : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 4770b57cec5SDimitry Andric BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 4780b57cec5SDimitry Andric AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace), 479*349cc55cSDimitry Andric Flags(Flags), ExtraData(ExtraData), Annotations(Annotations) {} 4800b57cec5SDimitry Andric MDNodeKeyImpl(const DIDerivedType *N) 4810b57cec5SDimitry Andric : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 4820b57cec5SDimitry Andric Line(N->getLine()), Scope(N->getRawScope()), 4830b57cec5SDimitry Andric BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), 4840b57cec5SDimitry Andric OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()), 4850b57cec5SDimitry Andric DWARFAddressSpace(N->getDWARFAddressSpace()), Flags(N->getFlags()), 486*349cc55cSDimitry Andric ExtraData(N->getRawExtraData()), Annotations(N->getRawAnnotations()) {} 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric bool isKeyOf(const DIDerivedType *RHS) const { 4890b57cec5SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 4900b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine() && 4910b57cec5SDimitry Andric Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 4920b57cec5SDimitry Andric SizeInBits == RHS->getSizeInBits() && 4930b57cec5SDimitry Andric AlignInBits == RHS->getAlignInBits() && 4940b57cec5SDimitry Andric OffsetInBits == RHS->getOffsetInBits() && 4950b57cec5SDimitry Andric DWARFAddressSpace == RHS->getDWARFAddressSpace() && 496*349cc55cSDimitry Andric Flags == RHS->getFlags() && ExtraData == RHS->getRawExtraData() && 497*349cc55cSDimitry Andric Annotations == RHS->getRawAnnotations(); 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric unsigned getHashValue() const { 5010b57cec5SDimitry Andric // If this is a member inside an ODR type, only hash the type and the name. 5020b57cec5SDimitry Andric // Otherwise the hash will be stronger than 5030b57cec5SDimitry Andric // MDNodeSubsetEqualImpl::isODRMember(). 5040b57cec5SDimitry Andric if (Tag == dwarf::DW_TAG_member && Name) 5050b57cec5SDimitry Andric if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 5060b57cec5SDimitry Andric if (CT->getRawIdentifier()) 5070b57cec5SDimitry Andric return hash_combine(Name, Scope); 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric // Intentionally computes the hash on a subset of the operands for 5100b57cec5SDimitry Andric // performance reason. The subset has to be significant enough to avoid 5110b57cec5SDimitry Andric // collision "most of the time". There is no correctness issue in case of 5120b57cec5SDimitry Andric // collision because of the full check above. 5130b57cec5SDimitry Andric return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags); 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric }; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric template <> struct MDNodeSubsetEqualImpl<DIDerivedType> { 5180b57cec5SDimitry Andric using KeyTy = MDNodeKeyImpl<DIDerivedType>; 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) { 5210b57cec5SDimitry Andric return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS); 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 524*349cc55cSDimitry Andric static bool isSubsetEqual(const DIDerivedType *LHS, 525*349cc55cSDimitry Andric const DIDerivedType *RHS) { 5260b57cec5SDimitry Andric return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(), 5270b57cec5SDimitry Andric RHS); 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric /// Subprograms compare equal if they declare the same function in an ODR 5310b57cec5SDimitry Andric /// type. 5320b57cec5SDimitry Andric static bool isODRMember(unsigned Tag, const Metadata *Scope, 5330b57cec5SDimitry Andric const MDString *Name, const DIDerivedType *RHS) { 5340b57cec5SDimitry Andric // Check whether the LHS is eligible. 5350b57cec5SDimitry Andric if (Tag != dwarf::DW_TAG_member || !Name) 5360b57cec5SDimitry Andric return false; 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 5390b57cec5SDimitry Andric if (!CT || !CT->getRawIdentifier()) 5400b57cec5SDimitry Andric return false; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Compare to the RHS. 5430b57cec5SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 5440b57cec5SDimitry Andric Scope == RHS->getRawScope(); 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric }; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DICompositeType> { 5490b57cec5SDimitry Andric unsigned Tag; 5500b57cec5SDimitry Andric MDString *Name; 5510b57cec5SDimitry Andric Metadata *File; 5520b57cec5SDimitry Andric unsigned Line; 5530b57cec5SDimitry Andric Metadata *Scope; 5540b57cec5SDimitry Andric Metadata *BaseType; 5550b57cec5SDimitry Andric uint64_t SizeInBits; 5560b57cec5SDimitry Andric uint64_t OffsetInBits; 5570b57cec5SDimitry Andric uint32_t AlignInBits; 5580b57cec5SDimitry Andric unsigned Flags; 5590b57cec5SDimitry Andric Metadata *Elements; 5600b57cec5SDimitry Andric unsigned RuntimeLang; 5610b57cec5SDimitry Andric Metadata *VTableHolder; 5620b57cec5SDimitry Andric Metadata *TemplateParams; 5630b57cec5SDimitry Andric MDString *Identifier; 5640b57cec5SDimitry Andric Metadata *Discriminator; 5655ffd83dbSDimitry Andric Metadata *DataLocation; 566e8d8bef9SDimitry Andric Metadata *Associated; 567e8d8bef9SDimitry Andric Metadata *Allocated; 568e8d8bef9SDimitry Andric Metadata *Rank; 569*349cc55cSDimitry Andric Metadata *Annotations; 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line, 5720b57cec5SDimitry Andric Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 5730b57cec5SDimitry Andric uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, 5740b57cec5SDimitry Andric Metadata *Elements, unsigned RuntimeLang, 5750b57cec5SDimitry Andric Metadata *VTableHolder, Metadata *TemplateParams, 5765ffd83dbSDimitry Andric MDString *Identifier, Metadata *Discriminator, 577e8d8bef9SDimitry Andric Metadata *DataLocation, Metadata *Associated, 578*349cc55cSDimitry Andric Metadata *Allocated, Metadata *Rank, Metadata *Annotations) 5790b57cec5SDimitry Andric : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), 5800b57cec5SDimitry Andric BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits), 5810b57cec5SDimitry Andric AlignInBits(AlignInBits), Flags(Flags), Elements(Elements), 5820b57cec5SDimitry Andric RuntimeLang(RuntimeLang), VTableHolder(VTableHolder), 5830b57cec5SDimitry Andric TemplateParams(TemplateParams), Identifier(Identifier), 584e8d8bef9SDimitry Andric Discriminator(Discriminator), DataLocation(DataLocation), 585*349cc55cSDimitry Andric Associated(Associated), Allocated(Allocated), Rank(Rank), 586*349cc55cSDimitry Andric Annotations(Annotations) {} 5870b57cec5SDimitry Andric MDNodeKeyImpl(const DICompositeType *N) 5880b57cec5SDimitry Andric : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()), 5890b57cec5SDimitry Andric Line(N->getLine()), Scope(N->getRawScope()), 5900b57cec5SDimitry Andric BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()), 5910b57cec5SDimitry Andric OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()), 5920b57cec5SDimitry Andric Flags(N->getFlags()), Elements(N->getRawElements()), 5930b57cec5SDimitry Andric RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()), 5940b57cec5SDimitry Andric TemplateParams(N->getRawTemplateParams()), 5950b57cec5SDimitry Andric Identifier(N->getRawIdentifier()), 5965ffd83dbSDimitry Andric Discriminator(N->getRawDiscriminator()), 597e8d8bef9SDimitry Andric DataLocation(N->getRawDataLocation()), 598e8d8bef9SDimitry Andric Associated(N->getRawAssociated()), Allocated(N->getRawAllocated()), 599*349cc55cSDimitry Andric Rank(N->getRawRank()), Annotations(N->getRawAnnotations()) {} 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric bool isKeyOf(const DICompositeType *RHS) const { 6020b57cec5SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 6030b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine() && 6040b57cec5SDimitry Andric Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() && 6050b57cec5SDimitry Andric SizeInBits == RHS->getSizeInBits() && 6060b57cec5SDimitry Andric AlignInBits == RHS->getAlignInBits() && 6070b57cec5SDimitry Andric OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && 6080b57cec5SDimitry Andric Elements == RHS->getRawElements() && 6090b57cec5SDimitry Andric RuntimeLang == RHS->getRuntimeLang() && 6100b57cec5SDimitry Andric VTableHolder == RHS->getRawVTableHolder() && 6110b57cec5SDimitry Andric TemplateParams == RHS->getRawTemplateParams() && 6120b57cec5SDimitry Andric Identifier == RHS->getRawIdentifier() && 6135ffd83dbSDimitry Andric Discriminator == RHS->getRawDiscriminator() && 614e8d8bef9SDimitry Andric DataLocation == RHS->getRawDataLocation() && 615e8d8bef9SDimitry Andric Associated == RHS->getRawAssociated() && 616*349cc55cSDimitry Andric Allocated == RHS->getRawAllocated() && Rank == RHS->getRawRank() && 617*349cc55cSDimitry Andric Annotations == RHS->getRawAnnotations(); 6180b57cec5SDimitry Andric } 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric unsigned getHashValue() const { 6210b57cec5SDimitry Andric // Intentionally computes the hash on a subset of the operands for 6220b57cec5SDimitry Andric // performance reason. The subset has to be significant enough to avoid 6230b57cec5SDimitry Andric // collision "most of the time". There is no correctness issue in case of 6240b57cec5SDimitry Andric // collision because of the full check above. 6250b57cec5SDimitry Andric return hash_combine(Name, File, Line, BaseType, Scope, Elements, 626*349cc55cSDimitry Andric TemplateParams, Annotations); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric }; 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DISubroutineType> { 6310b57cec5SDimitry Andric unsigned Flags; 6320b57cec5SDimitry Andric uint8_t CC; 6330b57cec5SDimitry Andric Metadata *TypeArray; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray) 6360b57cec5SDimitry Andric : Flags(Flags), CC(CC), TypeArray(TypeArray) {} 6370b57cec5SDimitry Andric MDNodeKeyImpl(const DISubroutineType *N) 6380b57cec5SDimitry Andric : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {} 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric bool isKeyOf(const DISubroutineType *RHS) const { 6410b57cec5SDimitry Andric return Flags == RHS->getFlags() && CC == RHS->getCC() && 6420b57cec5SDimitry Andric TypeArray == RHS->getRawTypeArray(); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 6450b57cec5SDimitry Andric unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); } 6460b57cec5SDimitry Andric }; 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIFile> { 6490b57cec5SDimitry Andric MDString *Filename; 6500b57cec5SDimitry Andric MDString *Directory; 6510b57cec5SDimitry Andric Optional<DIFile::ChecksumInfo<MDString *>> Checksum; 6520b57cec5SDimitry Andric Optional<MDString *> Source; 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric MDNodeKeyImpl(MDString *Filename, MDString *Directory, 6550b57cec5SDimitry Andric Optional<DIFile::ChecksumInfo<MDString *>> Checksum, 6560b57cec5SDimitry Andric Optional<MDString *> Source) 6570b57cec5SDimitry Andric : Filename(Filename), Directory(Directory), Checksum(Checksum), 6580b57cec5SDimitry Andric Source(Source) {} 6590b57cec5SDimitry Andric MDNodeKeyImpl(const DIFile *N) 6600b57cec5SDimitry Andric : Filename(N->getRawFilename()), Directory(N->getRawDirectory()), 6610b57cec5SDimitry Andric Checksum(N->getRawChecksum()), Source(N->getRawSource()) {} 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric bool isKeyOf(const DIFile *RHS) const { 6640b57cec5SDimitry Andric return Filename == RHS->getRawFilename() && 6650b57cec5SDimitry Andric Directory == RHS->getRawDirectory() && 666*349cc55cSDimitry Andric Checksum == RHS->getRawChecksum() && Source == RHS->getRawSource(); 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric unsigned getHashValue() const { 670*349cc55cSDimitry Andric return hash_combine(Filename, Directory, Checksum ? Checksum->Kind : 0, 671*349cc55cSDimitry Andric Checksum ? Checksum->Value : nullptr, 672*349cc55cSDimitry Andric Source.getValueOr(nullptr)); 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric }; 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DISubprogram> { 6770b57cec5SDimitry Andric Metadata *Scope; 6780b57cec5SDimitry Andric MDString *Name; 6790b57cec5SDimitry Andric MDString *LinkageName; 6800b57cec5SDimitry Andric Metadata *File; 6810b57cec5SDimitry Andric unsigned Line; 6820b57cec5SDimitry Andric Metadata *Type; 6830b57cec5SDimitry Andric unsigned ScopeLine; 6840b57cec5SDimitry Andric Metadata *ContainingType; 6850b57cec5SDimitry Andric unsigned VirtualIndex; 6860b57cec5SDimitry Andric int ThisAdjustment; 6870b57cec5SDimitry Andric unsigned Flags; 6880b57cec5SDimitry Andric unsigned SPFlags; 6890b57cec5SDimitry Andric Metadata *Unit; 6900b57cec5SDimitry Andric Metadata *TemplateParams; 6910b57cec5SDimitry Andric Metadata *Declaration; 6920b57cec5SDimitry Andric Metadata *RetainedNodes; 6930b57cec5SDimitry Andric Metadata *ThrownTypes; 694*349cc55cSDimitry Andric Metadata *Annotations; 6950b57cec5SDimitry Andric 6960b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 6970b57cec5SDimitry Andric Metadata *File, unsigned Line, Metadata *Type, 6980b57cec5SDimitry Andric unsigned ScopeLine, Metadata *ContainingType, 6990b57cec5SDimitry Andric unsigned VirtualIndex, int ThisAdjustment, unsigned Flags, 7000b57cec5SDimitry Andric unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams, 7010b57cec5SDimitry Andric Metadata *Declaration, Metadata *RetainedNodes, 702*349cc55cSDimitry Andric Metadata *ThrownTypes, Metadata *Annotations) 7030b57cec5SDimitry Andric : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 7040b57cec5SDimitry Andric Line(Line), Type(Type), ScopeLine(ScopeLine), 7050b57cec5SDimitry Andric ContainingType(ContainingType), VirtualIndex(VirtualIndex), 7060b57cec5SDimitry Andric ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags), 7070b57cec5SDimitry Andric Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration), 708*349cc55cSDimitry Andric RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes), 709*349cc55cSDimitry Andric Annotations(Annotations) {} 7100b57cec5SDimitry Andric MDNodeKeyImpl(const DISubprogram *N) 7110b57cec5SDimitry Andric : Scope(N->getRawScope()), Name(N->getRawName()), 7120b57cec5SDimitry Andric LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 7130b57cec5SDimitry Andric Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()), 7140b57cec5SDimitry Andric ContainingType(N->getRawContainingType()), 7150b57cec5SDimitry Andric VirtualIndex(N->getVirtualIndex()), 7160b57cec5SDimitry Andric ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()), 7170b57cec5SDimitry Andric SPFlags(N->getSPFlags()), Unit(N->getRawUnit()), 7180b57cec5SDimitry Andric TemplateParams(N->getRawTemplateParams()), 7190b57cec5SDimitry Andric Declaration(N->getRawDeclaration()), 7200b57cec5SDimitry Andric RetainedNodes(N->getRawRetainedNodes()), 721*349cc55cSDimitry Andric ThrownTypes(N->getRawThrownTypes()), 722*349cc55cSDimitry Andric Annotations(N->getRawAnnotations()) {} 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric bool isKeyOf(const DISubprogram *RHS) const { 7250b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 7260b57cec5SDimitry Andric LinkageName == RHS->getRawLinkageName() && 7270b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine() && 7280b57cec5SDimitry Andric Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() && 7290b57cec5SDimitry Andric ContainingType == RHS->getRawContainingType() && 7300b57cec5SDimitry Andric VirtualIndex == RHS->getVirtualIndex() && 7310b57cec5SDimitry Andric ThisAdjustment == RHS->getThisAdjustment() && 7320b57cec5SDimitry Andric Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() && 7330b57cec5SDimitry Andric Unit == RHS->getUnit() && 7340b57cec5SDimitry Andric TemplateParams == RHS->getRawTemplateParams() && 7350b57cec5SDimitry Andric Declaration == RHS->getRawDeclaration() && 7360b57cec5SDimitry Andric RetainedNodes == RHS->getRawRetainedNodes() && 737*349cc55cSDimitry Andric ThrownTypes == RHS->getRawThrownTypes() && 738*349cc55cSDimitry Andric Annotations == RHS->getRawAnnotations(); 7390b57cec5SDimitry Andric } 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric unsigned getHashValue() const { 7440b57cec5SDimitry Andric // If this is a declaration inside an ODR type, only hash the type and the 7450b57cec5SDimitry Andric // name. Otherwise the hash will be stronger than 7460b57cec5SDimitry Andric // MDNodeSubsetEqualImpl::isDeclarationOfODRMember(). 7470b57cec5SDimitry Andric if (!isDefinition() && LinkageName) 7480b57cec5SDimitry Andric if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope)) 7490b57cec5SDimitry Andric if (CT->getRawIdentifier()) 7500b57cec5SDimitry Andric return hash_combine(LinkageName, Scope); 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric // Intentionally computes the hash on a subset of the operands for 7530b57cec5SDimitry Andric // performance reason. The subset has to be significant enough to avoid 7540b57cec5SDimitry Andric // collision "most of the time". There is no correctness issue in case of 7550b57cec5SDimitry Andric // collision because of the full check above. 7560b57cec5SDimitry Andric return hash_combine(Name, Scope, File, Type, Line); 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric }; 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric template <> struct MDNodeSubsetEqualImpl<DISubprogram> { 7610b57cec5SDimitry Andric using KeyTy = MDNodeKeyImpl<DISubprogram>; 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) { 7640b57cec5SDimitry Andric return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope, 7650b57cec5SDimitry Andric LHS.LinkageName, LHS.TemplateParams, RHS); 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) { 7690b57cec5SDimitry Andric return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(), 7700b57cec5SDimitry Andric LHS->getRawLinkageName(), 7710b57cec5SDimitry Andric LHS->getRawTemplateParams(), RHS); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric /// Subprograms compare equal if they declare the same function in an ODR 7750b57cec5SDimitry Andric /// type. 7760b57cec5SDimitry Andric static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope, 7770b57cec5SDimitry Andric const MDString *LinkageName, 7780b57cec5SDimitry Andric const Metadata *TemplateParams, 7790b57cec5SDimitry Andric const DISubprogram *RHS) { 7800b57cec5SDimitry Andric // Check whether the LHS is eligible. 7810b57cec5SDimitry Andric if (IsDefinition || !Scope || !LinkageName) 7820b57cec5SDimitry Andric return false; 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric auto *CT = dyn_cast_or_null<DICompositeType>(Scope); 7850b57cec5SDimitry Andric if (!CT || !CT->getRawIdentifier()) 7860b57cec5SDimitry Andric return false; 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric // Compare to the RHS. 7890b57cec5SDimitry Andric // FIXME: We need to compare template parameters here to avoid incorrect 790fe6060f1SDimitry Andric // collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a 791fe6060f1SDimitry Andric // ODR-DISubprogram has a non-ODR template parameter (i.e., a 792fe6060f1SDimitry Andric // DICompositeType that does not have an identifier). Eventually we should 793fe6060f1SDimitry Andric // decouple ODR logic from uniquing logic. 7940b57cec5SDimitry Andric return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() && 7950b57cec5SDimitry Andric LinkageName == RHS->getRawLinkageName() && 7960b57cec5SDimitry Andric TemplateParams == RHS->getRawTemplateParams(); 7970b57cec5SDimitry Andric } 7980b57cec5SDimitry Andric }; 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DILexicalBlock> { 8010b57cec5SDimitry Andric Metadata *Scope; 8020b57cec5SDimitry Andric Metadata *File; 8030b57cec5SDimitry Andric unsigned Line; 8040b57cec5SDimitry Andric unsigned Column; 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column) 8070b57cec5SDimitry Andric : Scope(Scope), File(File), Line(Line), Column(Column) {} 8080b57cec5SDimitry Andric MDNodeKeyImpl(const DILexicalBlock *N) 8090b57cec5SDimitry Andric : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()), 8100b57cec5SDimitry Andric Column(N->getColumn()) {} 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric bool isKeyOf(const DILexicalBlock *RHS) const { 8130b57cec5SDimitry Andric return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 8140b57cec5SDimitry Andric Line == RHS->getLine() && Column == RHS->getColumn(); 8150b57cec5SDimitry Andric } 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric unsigned getHashValue() const { 8180b57cec5SDimitry Andric return hash_combine(Scope, File, Line, Column); 8190b57cec5SDimitry Andric } 8200b57cec5SDimitry Andric }; 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DILexicalBlockFile> { 8230b57cec5SDimitry Andric Metadata *Scope; 8240b57cec5SDimitry Andric Metadata *File; 8250b57cec5SDimitry Andric unsigned Discriminator; 8260b57cec5SDimitry Andric 8270b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator) 8280b57cec5SDimitry Andric : Scope(Scope), File(File), Discriminator(Discriminator) {} 8290b57cec5SDimitry Andric MDNodeKeyImpl(const DILexicalBlockFile *N) 8300b57cec5SDimitry Andric : Scope(N->getRawScope()), File(N->getRawFile()), 8310b57cec5SDimitry Andric Discriminator(N->getDiscriminator()) {} 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric bool isKeyOf(const DILexicalBlockFile *RHS) const { 8340b57cec5SDimitry Andric return Scope == RHS->getRawScope() && File == RHS->getRawFile() && 8350b57cec5SDimitry Andric Discriminator == RHS->getDiscriminator(); 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric unsigned getHashValue() const { 8390b57cec5SDimitry Andric return hash_combine(Scope, File, Discriminator); 8400b57cec5SDimitry Andric } 8410b57cec5SDimitry Andric }; 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DINamespace> { 8440b57cec5SDimitry Andric Metadata *Scope; 8450b57cec5SDimitry Andric MDString *Name; 8460b57cec5SDimitry Andric bool ExportSymbols; 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols) 8490b57cec5SDimitry Andric : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {} 8500b57cec5SDimitry Andric MDNodeKeyImpl(const DINamespace *N) 8510b57cec5SDimitry Andric : Scope(N->getRawScope()), Name(N->getRawName()), 8520b57cec5SDimitry Andric ExportSymbols(N->getExportSymbols()) {} 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric bool isKeyOf(const DINamespace *RHS) const { 8550b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 8560b57cec5SDimitry Andric ExportSymbols == RHS->getExportSymbols(); 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 859*349cc55cSDimitry Andric unsigned getHashValue() const { return hash_combine(Scope, Name); } 8600b57cec5SDimitry Andric }; 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DICommonBlock> { 8630b57cec5SDimitry Andric Metadata *Scope; 8640b57cec5SDimitry Andric Metadata *Decl; 8650b57cec5SDimitry Andric MDString *Name; 8660b57cec5SDimitry Andric Metadata *File; 8670b57cec5SDimitry Andric unsigned LineNo; 8680b57cec5SDimitry Andric 869*349cc55cSDimitry Andric MDNodeKeyImpl(Metadata *Scope, Metadata *Decl, MDString *Name, Metadata *File, 870*349cc55cSDimitry Andric unsigned LineNo) 8710b57cec5SDimitry Andric : Scope(Scope), Decl(Decl), Name(Name), File(File), LineNo(LineNo) {} 8720b57cec5SDimitry Andric MDNodeKeyImpl(const DICommonBlock *N) 8730b57cec5SDimitry Andric : Scope(N->getRawScope()), Decl(N->getRawDecl()), Name(N->getRawName()), 8740b57cec5SDimitry Andric File(N->getRawFile()), LineNo(N->getLineNo()) {} 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric bool isKeyOf(const DICommonBlock *RHS) const { 8770b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Decl == RHS->getRawDecl() && 8780b57cec5SDimitry Andric Name == RHS->getRawName() && File == RHS->getRawFile() && 8790b57cec5SDimitry Andric LineNo == RHS->getLineNo(); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric unsigned getHashValue() const { 8830b57cec5SDimitry Andric return hash_combine(Scope, Decl, Name, File, LineNo); 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric }; 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIModule> { 8885ffd83dbSDimitry Andric Metadata *File; 8890b57cec5SDimitry Andric Metadata *Scope; 8900b57cec5SDimitry Andric MDString *Name; 8910b57cec5SDimitry Andric MDString *ConfigurationMacros; 8920b57cec5SDimitry Andric MDString *IncludePath; 8935ffd83dbSDimitry Andric MDString *APINotesFile; 8945ffd83dbSDimitry Andric unsigned LineNo; 895e8d8bef9SDimitry Andric bool IsDecl; 8960b57cec5SDimitry Andric 8975ffd83dbSDimitry Andric MDNodeKeyImpl(Metadata *File, Metadata *Scope, MDString *Name, 8985ffd83dbSDimitry Andric MDString *ConfigurationMacros, MDString *IncludePath, 899e8d8bef9SDimitry Andric MDString *APINotesFile, unsigned LineNo, bool IsDecl) 9005ffd83dbSDimitry Andric : File(File), Scope(Scope), Name(Name), 9015ffd83dbSDimitry Andric ConfigurationMacros(ConfigurationMacros), IncludePath(IncludePath), 902e8d8bef9SDimitry Andric APINotesFile(APINotesFile), LineNo(LineNo), IsDecl(IsDecl) {} 9030b57cec5SDimitry Andric MDNodeKeyImpl(const DIModule *N) 9045ffd83dbSDimitry Andric : File(N->getRawFile()), Scope(N->getRawScope()), Name(N->getRawName()), 9050b57cec5SDimitry Andric ConfigurationMacros(N->getRawConfigurationMacros()), 9065ffd83dbSDimitry Andric IncludePath(N->getRawIncludePath()), 907e8d8bef9SDimitry Andric APINotesFile(N->getRawAPINotesFile()), LineNo(N->getLineNo()), 908e8d8bef9SDimitry Andric IsDecl(N->getIsDecl()) {} 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric bool isKeyOf(const DIModule *RHS) const { 9110b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 9120b57cec5SDimitry Andric ConfigurationMacros == RHS->getRawConfigurationMacros() && 9130b57cec5SDimitry Andric IncludePath == RHS->getRawIncludePath() && 9145ffd83dbSDimitry Andric APINotesFile == RHS->getRawAPINotesFile() && 915e8d8bef9SDimitry Andric File == RHS->getRawFile() && LineNo == RHS->getLineNo() && 916e8d8bef9SDimitry Andric IsDecl == RHS->getIsDecl(); 9170b57cec5SDimitry Andric } 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric unsigned getHashValue() const { 9205ffd83dbSDimitry Andric return hash_combine(Scope, Name, ConfigurationMacros, IncludePath); 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric }; 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DITemplateTypeParameter> { 9250b57cec5SDimitry Andric MDString *Name; 9260b57cec5SDimitry Andric Metadata *Type; 9275ffd83dbSDimitry Andric bool IsDefault; 9280b57cec5SDimitry Andric 9295ffd83dbSDimitry Andric MDNodeKeyImpl(MDString *Name, Metadata *Type, bool IsDefault) 9305ffd83dbSDimitry Andric : Name(Name), Type(Type), IsDefault(IsDefault) {} 9310b57cec5SDimitry Andric MDNodeKeyImpl(const DITemplateTypeParameter *N) 9325ffd83dbSDimitry Andric : Name(N->getRawName()), Type(N->getRawType()), 9335ffd83dbSDimitry Andric IsDefault(N->isDefault()) {} 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric bool isKeyOf(const DITemplateTypeParameter *RHS) const { 9365ffd83dbSDimitry Andric return Name == RHS->getRawName() && Type == RHS->getRawType() && 9375ffd83dbSDimitry Andric IsDefault == RHS->isDefault(); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric 9405ffd83dbSDimitry Andric unsigned getHashValue() const { return hash_combine(Name, Type, IsDefault); } 9410b57cec5SDimitry Andric }; 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DITemplateValueParameter> { 9440b57cec5SDimitry Andric unsigned Tag; 9450b57cec5SDimitry Andric MDString *Name; 9460b57cec5SDimitry Andric Metadata *Type; 9475ffd83dbSDimitry Andric bool IsDefault; 9480b57cec5SDimitry Andric Metadata *Value; 9490b57cec5SDimitry Andric 9505ffd83dbSDimitry Andric MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, bool IsDefault, 9515ffd83dbSDimitry Andric Metadata *Value) 9525ffd83dbSDimitry Andric : Tag(Tag), Name(Name), Type(Type), IsDefault(IsDefault), Value(Value) {} 9530b57cec5SDimitry Andric MDNodeKeyImpl(const DITemplateValueParameter *N) 9540b57cec5SDimitry Andric : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()), 9555ffd83dbSDimitry Andric IsDefault(N->isDefault()), Value(N->getValue()) {} 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric bool isKeyOf(const DITemplateValueParameter *RHS) const { 9580b57cec5SDimitry Andric return Tag == RHS->getTag() && Name == RHS->getRawName() && 9595ffd83dbSDimitry Andric Type == RHS->getRawType() && IsDefault == RHS->isDefault() && 9605ffd83dbSDimitry Andric Value == RHS->getValue(); 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 9635ffd83dbSDimitry Andric unsigned getHashValue() const { 9645ffd83dbSDimitry Andric return hash_combine(Tag, Name, Type, IsDefault, Value); 9655ffd83dbSDimitry Andric } 9660b57cec5SDimitry Andric }; 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIGlobalVariable> { 9690b57cec5SDimitry Andric Metadata *Scope; 9700b57cec5SDimitry Andric MDString *Name; 9710b57cec5SDimitry Andric MDString *LinkageName; 9720b57cec5SDimitry Andric Metadata *File; 9730b57cec5SDimitry Andric unsigned Line; 9740b57cec5SDimitry Andric Metadata *Type; 9750b57cec5SDimitry Andric bool IsLocalToUnit; 9760b57cec5SDimitry Andric bool IsDefinition; 9770b57cec5SDimitry Andric Metadata *StaticDataMemberDeclaration; 9780b57cec5SDimitry Andric Metadata *TemplateParams; 9790b57cec5SDimitry Andric uint32_t AlignInBits; 980*349cc55cSDimitry Andric Metadata *Annotations; 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName, 9830b57cec5SDimitry Andric Metadata *File, unsigned Line, Metadata *Type, 9840b57cec5SDimitry Andric bool IsLocalToUnit, bool IsDefinition, 9850b57cec5SDimitry Andric Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams, 986*349cc55cSDimitry Andric uint32_t AlignInBits, Metadata *Annotations) 9870b57cec5SDimitry Andric : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), 9880b57cec5SDimitry Andric Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit), 9890b57cec5SDimitry Andric IsDefinition(IsDefinition), 9900b57cec5SDimitry Andric StaticDataMemberDeclaration(StaticDataMemberDeclaration), 991*349cc55cSDimitry Andric TemplateParams(TemplateParams), AlignInBits(AlignInBits), 992*349cc55cSDimitry Andric Annotations(Annotations) {} 9930b57cec5SDimitry Andric MDNodeKeyImpl(const DIGlobalVariable *N) 9940b57cec5SDimitry Andric : Scope(N->getRawScope()), Name(N->getRawName()), 9950b57cec5SDimitry Andric LinkageName(N->getRawLinkageName()), File(N->getRawFile()), 9960b57cec5SDimitry Andric Line(N->getLine()), Type(N->getRawType()), 9970b57cec5SDimitry Andric IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), 9980b57cec5SDimitry Andric StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()), 9990b57cec5SDimitry Andric TemplateParams(N->getRawTemplateParams()), 1000*349cc55cSDimitry Andric AlignInBits(N->getAlignInBits()), Annotations(N->getRawAnnotations()) {} 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric bool isKeyOf(const DIGlobalVariable *RHS) const { 10030b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 10040b57cec5SDimitry Andric LinkageName == RHS->getRawLinkageName() && 10050b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine() && 10060b57cec5SDimitry Andric Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() && 10070b57cec5SDimitry Andric IsDefinition == RHS->isDefinition() && 10080b57cec5SDimitry Andric StaticDataMemberDeclaration == 10090b57cec5SDimitry Andric RHS->getRawStaticDataMemberDeclaration() && 10100b57cec5SDimitry Andric TemplateParams == RHS->getRawTemplateParams() && 1011*349cc55cSDimitry Andric AlignInBits == RHS->getAlignInBits() && 1012*349cc55cSDimitry Andric Annotations == RHS->getRawAnnotations(); 10130b57cec5SDimitry Andric } 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric unsigned getHashValue() const { 10160b57cec5SDimitry Andric // We do not use AlignInBits in hashing function here on purpose: 10170b57cec5SDimitry Andric // in most cases this param for local variable is zero (for function param 10180b57cec5SDimitry Andric // it is always zero). This leads to lots of hash collisions and errors on 10190b57cec5SDimitry Andric // cases with lots of similar variables. 10200b57cec5SDimitry Andric // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 10210b57cec5SDimitry Andric // generated IR is random for each run and test fails with Align included. 10220b57cec5SDimitry Andric // TODO: make hashing work fine with such situations 10230b57cec5SDimitry Andric return hash_combine(Scope, Name, LinkageName, File, Line, Type, 10240b57cec5SDimitry Andric IsLocalToUnit, IsDefinition, /* AlignInBits, */ 1025*349cc55cSDimitry Andric StaticDataMemberDeclaration, Annotations); 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric }; 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DILocalVariable> { 10300b57cec5SDimitry Andric Metadata *Scope; 10310b57cec5SDimitry Andric MDString *Name; 10320b57cec5SDimitry Andric Metadata *File; 10330b57cec5SDimitry Andric unsigned Line; 10340b57cec5SDimitry Andric Metadata *Type; 10350b57cec5SDimitry Andric unsigned Arg; 10360b57cec5SDimitry Andric unsigned Flags; 10370b57cec5SDimitry Andric uint32_t AlignInBits; 1038*349cc55cSDimitry Andric Metadata *Annotations; 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line, 10410b57cec5SDimitry Andric Metadata *Type, unsigned Arg, unsigned Flags, 1042*349cc55cSDimitry Andric uint32_t AlignInBits, Metadata *Annotations) 10430b57cec5SDimitry Andric : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg), 1044*349cc55cSDimitry Andric Flags(Flags), AlignInBits(AlignInBits), Annotations(Annotations) {} 10450b57cec5SDimitry Andric MDNodeKeyImpl(const DILocalVariable *N) 10460b57cec5SDimitry Andric : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 10470b57cec5SDimitry Andric Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()), 1048*349cc55cSDimitry Andric Flags(N->getFlags()), AlignInBits(N->getAlignInBits()), 1049*349cc55cSDimitry Andric Annotations(N->getRawAnnotations()) {} 10500b57cec5SDimitry Andric 10510b57cec5SDimitry Andric bool isKeyOf(const DILocalVariable *RHS) const { 10520b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 10530b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine() && 10540b57cec5SDimitry Andric Type == RHS->getRawType() && Arg == RHS->getArg() && 1055*349cc55cSDimitry Andric Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits() && 1056*349cc55cSDimitry Andric Annotations == RHS->getRawAnnotations(); 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric unsigned getHashValue() const { 10600b57cec5SDimitry Andric // We do not use AlignInBits in hashing function here on purpose: 10610b57cec5SDimitry Andric // in most cases this param for local variable is zero (for function param 10620b57cec5SDimitry Andric // it is always zero). This leads to lots of hash collisions and errors on 10630b57cec5SDimitry Andric // cases with lots of similar variables. 10640b57cec5SDimitry Andric // clang/test/CodeGen/debug-info-257-args.c is an example of this problem, 10650b57cec5SDimitry Andric // generated IR is random for each run and test fails with Align included. 10660b57cec5SDimitry Andric // TODO: make hashing work fine with such situations 1067*349cc55cSDimitry Andric return hash_combine(Scope, Name, File, Line, Type, Arg, Flags, Annotations); 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric }; 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DILabel> { 10720b57cec5SDimitry Andric Metadata *Scope; 10730b57cec5SDimitry Andric MDString *Name; 10740b57cec5SDimitry Andric Metadata *File; 10750b57cec5SDimitry Andric unsigned Line; 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line) 10780b57cec5SDimitry Andric : Scope(Scope), Name(Name), File(File), Line(Line) {} 10790b57cec5SDimitry Andric MDNodeKeyImpl(const DILabel *N) 10800b57cec5SDimitry Andric : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()), 10810b57cec5SDimitry Andric Line(N->getLine()) {} 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric bool isKeyOf(const DILabel *RHS) const { 10840b57cec5SDimitry Andric return Scope == RHS->getRawScope() && Name == RHS->getRawName() && 10850b57cec5SDimitry Andric File == RHS->getRawFile() && Line == RHS->getLine(); 10860b57cec5SDimitry Andric } 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric /// Using name and line to get hash value. It should already be mostly unique. 1089*349cc55cSDimitry Andric unsigned getHashValue() const { return hash_combine(Scope, Name, Line); } 10900b57cec5SDimitry Andric }; 10910b57cec5SDimitry Andric 10920b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIExpression> { 10930b57cec5SDimitry Andric ArrayRef<uint64_t> Elements; 10940b57cec5SDimitry Andric 10950b57cec5SDimitry Andric MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {} 10960b57cec5SDimitry Andric MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {} 10970b57cec5SDimitry Andric 10980b57cec5SDimitry Andric bool isKeyOf(const DIExpression *RHS) const { 10990b57cec5SDimitry Andric return Elements == RHS->getElements(); 11000b57cec5SDimitry Andric } 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric unsigned getHashValue() const { 11030b57cec5SDimitry Andric return hash_combine_range(Elements.begin(), Elements.end()); 11040b57cec5SDimitry Andric } 11050b57cec5SDimitry Andric }; 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> { 11080b57cec5SDimitry Andric Metadata *Variable; 11090b57cec5SDimitry Andric Metadata *Expression; 11100b57cec5SDimitry Andric 11110b57cec5SDimitry Andric MDNodeKeyImpl(Metadata *Variable, Metadata *Expression) 11120b57cec5SDimitry Andric : Variable(Variable), Expression(Expression) {} 11130b57cec5SDimitry Andric MDNodeKeyImpl(const DIGlobalVariableExpression *N) 11140b57cec5SDimitry Andric : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {} 11150b57cec5SDimitry Andric 11160b57cec5SDimitry Andric bool isKeyOf(const DIGlobalVariableExpression *RHS) const { 11170b57cec5SDimitry Andric return Variable == RHS->getRawVariable() && 11180b57cec5SDimitry Andric Expression == RHS->getRawExpression(); 11190b57cec5SDimitry Andric } 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andric unsigned getHashValue() const { return hash_combine(Variable, Expression); } 11220b57cec5SDimitry Andric }; 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIObjCProperty> { 11250b57cec5SDimitry Andric MDString *Name; 11260b57cec5SDimitry Andric Metadata *File; 11270b57cec5SDimitry Andric unsigned Line; 11280b57cec5SDimitry Andric MDString *GetterName; 11290b57cec5SDimitry Andric MDString *SetterName; 11300b57cec5SDimitry Andric unsigned Attributes; 11310b57cec5SDimitry Andric Metadata *Type; 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line, 11340b57cec5SDimitry Andric MDString *GetterName, MDString *SetterName, unsigned Attributes, 11350b57cec5SDimitry Andric Metadata *Type) 11360b57cec5SDimitry Andric : Name(Name), File(File), Line(Line), GetterName(GetterName), 11370b57cec5SDimitry Andric SetterName(SetterName), Attributes(Attributes), Type(Type) {} 11380b57cec5SDimitry Andric MDNodeKeyImpl(const DIObjCProperty *N) 11390b57cec5SDimitry Andric : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()), 11400b57cec5SDimitry Andric GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()), 11410b57cec5SDimitry Andric Attributes(N->getAttributes()), Type(N->getRawType()) {} 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric bool isKeyOf(const DIObjCProperty *RHS) const { 11440b57cec5SDimitry Andric return Name == RHS->getRawName() && File == RHS->getRawFile() && 11450b57cec5SDimitry Andric Line == RHS->getLine() && GetterName == RHS->getRawGetterName() && 11460b57cec5SDimitry Andric SetterName == RHS->getRawSetterName() && 11470b57cec5SDimitry Andric Attributes == RHS->getAttributes() && Type == RHS->getRawType(); 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric unsigned getHashValue() const { 11510b57cec5SDimitry Andric return hash_combine(Name, File, Line, GetterName, SetterName, Attributes, 11520b57cec5SDimitry Andric Type); 11530b57cec5SDimitry Andric } 11540b57cec5SDimitry Andric }; 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIImportedEntity> { 11570b57cec5SDimitry Andric unsigned Tag; 11580b57cec5SDimitry Andric Metadata *Scope; 11590b57cec5SDimitry Andric Metadata *Entity; 11600b57cec5SDimitry Andric Metadata *File; 11610b57cec5SDimitry Andric unsigned Line; 11620b57cec5SDimitry Andric MDString *Name; 1163*349cc55cSDimitry Andric Metadata *Elements; 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File, 1166*349cc55cSDimitry Andric unsigned Line, MDString *Name, Metadata *Elements) 11670b57cec5SDimitry Andric : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line), 1168*349cc55cSDimitry Andric Name(Name), Elements(Elements) {} 11690b57cec5SDimitry Andric MDNodeKeyImpl(const DIImportedEntity *N) 11700b57cec5SDimitry Andric : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()), 1171*349cc55cSDimitry Andric File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()), 1172*349cc55cSDimitry Andric Elements(N->getRawElements()) {} 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric bool isKeyOf(const DIImportedEntity *RHS) const { 11750b57cec5SDimitry Andric return Tag == RHS->getTag() && Scope == RHS->getRawScope() && 11760b57cec5SDimitry Andric Entity == RHS->getRawEntity() && File == RHS->getFile() && 1177*349cc55cSDimitry Andric Line == RHS->getLine() && Name == RHS->getRawName() && 1178*349cc55cSDimitry Andric Elements == RHS->getRawElements(); 11790b57cec5SDimitry Andric } 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric unsigned getHashValue() const { 1182*349cc55cSDimitry Andric return hash_combine(Tag, Scope, Entity, File, Line, Name, Elements); 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric }; 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIMacro> { 11870b57cec5SDimitry Andric unsigned MIType; 11880b57cec5SDimitry Andric unsigned Line; 11890b57cec5SDimitry Andric MDString *Name; 11900b57cec5SDimitry Andric MDString *Value; 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value) 11930b57cec5SDimitry Andric : MIType(MIType), Line(Line), Name(Name), Value(Value) {} 11940b57cec5SDimitry Andric MDNodeKeyImpl(const DIMacro *N) 11950b57cec5SDimitry Andric : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()), 11960b57cec5SDimitry Andric Value(N->getRawValue()) {} 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric bool isKeyOf(const DIMacro *RHS) const { 11990b57cec5SDimitry Andric return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 12000b57cec5SDimitry Andric Name == RHS->getRawName() && Value == RHS->getRawValue(); 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric unsigned getHashValue() const { 12040b57cec5SDimitry Andric return hash_combine(MIType, Line, Name, Value); 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric }; 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric template <> struct MDNodeKeyImpl<DIMacroFile> { 12090b57cec5SDimitry Andric unsigned MIType; 12100b57cec5SDimitry Andric unsigned Line; 12110b57cec5SDimitry Andric Metadata *File; 12120b57cec5SDimitry Andric Metadata *Elements; 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File, 12150b57cec5SDimitry Andric Metadata *Elements) 12160b57cec5SDimitry Andric : MIType(MIType), Line(Line), File(File), Elements(Elements) {} 12170b57cec5SDimitry Andric MDNodeKeyImpl(const DIMacroFile *N) 12180b57cec5SDimitry Andric : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()), 12190b57cec5SDimitry Andric Elements(N->getRawElements()) {} 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric bool isKeyOf(const DIMacroFile *RHS) const { 12220b57cec5SDimitry Andric return MIType == RHS->getMacinfoType() && Line == RHS->getLine() && 12230b57cec5SDimitry Andric File == RHS->getRawFile() && Elements == RHS->getRawElements(); 12240b57cec5SDimitry Andric } 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric unsigned getHashValue() const { 12270b57cec5SDimitry Andric return hash_combine(MIType, Line, File, Elements); 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric }; 12300b57cec5SDimitry Andric 1231fe6060f1SDimitry Andric template <> struct MDNodeKeyImpl<DIArgList> { 1232fe6060f1SDimitry Andric ArrayRef<ValueAsMetadata *> Args; 1233fe6060f1SDimitry Andric 1234fe6060f1SDimitry Andric MDNodeKeyImpl(ArrayRef<ValueAsMetadata *> Args) : Args(Args) {} 1235fe6060f1SDimitry Andric MDNodeKeyImpl(const DIArgList *N) : Args(N->getArgs()) {} 1236fe6060f1SDimitry Andric 1237fe6060f1SDimitry Andric bool isKeyOf(const DIArgList *RHS) const { return Args == RHS->getArgs(); } 1238fe6060f1SDimitry Andric 1239fe6060f1SDimitry Andric unsigned getHashValue() const { 1240fe6060f1SDimitry Andric return hash_combine_range(Args.begin(), Args.end()); 1241fe6060f1SDimitry Andric } 1242fe6060f1SDimitry Andric }; 1243fe6060f1SDimitry Andric 12440b57cec5SDimitry Andric /// DenseMapInfo for MDNode subclasses. 12450b57cec5SDimitry Andric template <class NodeTy> struct MDNodeInfo { 12460b57cec5SDimitry Andric using KeyTy = MDNodeKeyImpl<NodeTy>; 12470b57cec5SDimitry Andric using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>; 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric static inline NodeTy *getEmptyKey() { 12500b57cec5SDimitry Andric return DenseMapInfo<NodeTy *>::getEmptyKey(); 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric static inline NodeTy *getTombstoneKey() { 12540b57cec5SDimitry Andric return DenseMapInfo<NodeTy *>::getTombstoneKey(); 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); } 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric static unsigned getHashValue(const NodeTy *N) { 12600b57cec5SDimitry Andric return KeyTy(N).getHashValue(); 12610b57cec5SDimitry Andric } 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) { 12640b57cec5SDimitry Andric if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 12650b57cec5SDimitry Andric return false; 12660b57cec5SDimitry Andric return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS); 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) { 12700b57cec5SDimitry Andric if (LHS == RHS) 12710b57cec5SDimitry Andric return true; 12720b57cec5SDimitry Andric if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 12730b57cec5SDimitry Andric return false; 12740b57cec5SDimitry Andric return SubsetEqualTy::isSubsetEqual(LHS, RHS); 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric }; 12770b57cec5SDimitry Andric 12780b57cec5SDimitry Andric #define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>; 12790b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 12800b57cec5SDimitry Andric 1281e8d8bef9SDimitry Andric /// Multimap-like storage for metadata attachments. 1282e8d8bef9SDimitry Andric class MDAttachments { 1283e8d8bef9SDimitry Andric public: 1284e8d8bef9SDimitry Andric struct Attachment { 1285e8d8bef9SDimitry Andric unsigned MDKind; 1286e8d8bef9SDimitry Andric TrackingMDNodeRef Node; 1287e8d8bef9SDimitry Andric }; 1288e8d8bef9SDimitry Andric 1289e8d8bef9SDimitry Andric private: 1290e8d8bef9SDimitry Andric SmallVector<Attachment, 1> Attachments; 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric public: 12930b57cec5SDimitry Andric bool empty() const { return Attachments.empty(); } 12940b57cec5SDimitry Andric size_t size() const { return Attachments.size(); } 12950b57cec5SDimitry Andric 1296e8d8bef9SDimitry Andric /// Returns the first attachment with the given ID or nullptr if no such 1297e8d8bef9SDimitry Andric /// attachment exists. 12980b57cec5SDimitry Andric MDNode *lookup(unsigned ID) const; 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric /// Appends all attachments with the given ID to \c Result in insertion order. 13010b57cec5SDimitry Andric /// If the global has no attachments with the given ID, or if ID is invalid, 13020b57cec5SDimitry Andric /// leaves Result unchanged. 13030b57cec5SDimitry Andric void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const; 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric /// Appends all attachments for the global to \c Result, sorting by attachment 13060b57cec5SDimitry Andric /// ID. Attachments with the same ID appear in insertion order. This function 13070b57cec5SDimitry Andric /// does \em not clear \c Result. 13080b57cec5SDimitry Andric void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const; 1309e8d8bef9SDimitry Andric 1310e8d8bef9SDimitry Andric /// Set an attachment to a particular node. 1311e8d8bef9SDimitry Andric /// 1312e8d8bef9SDimitry Andric /// Set the \c ID attachment to \c MD, replacing the current attachments at \c 1313e8d8bef9SDimitry Andric /// ID (if anyway). 1314e8d8bef9SDimitry Andric void set(unsigned ID, MDNode *MD); 1315e8d8bef9SDimitry Andric 1316e8d8bef9SDimitry Andric /// Adds an attachment to a particular node. 1317e8d8bef9SDimitry Andric void insert(unsigned ID, MDNode &MD); 1318e8d8bef9SDimitry Andric 1319e8d8bef9SDimitry Andric /// Remove attachments with the given ID. 1320e8d8bef9SDimitry Andric /// 1321e8d8bef9SDimitry Andric /// Remove the attachments at \c ID, if any. 1322e8d8bef9SDimitry Andric bool erase(unsigned ID); 1323e8d8bef9SDimitry Andric 1324e8d8bef9SDimitry Andric /// Erase matching attachments. 1325e8d8bef9SDimitry Andric /// 1326e8d8bef9SDimitry Andric /// Erases all attachments matching the \c shouldRemove predicate. 1327e8d8bef9SDimitry Andric template <class PredTy> void remove_if(PredTy shouldRemove) { 1328e8d8bef9SDimitry Andric llvm::erase_if(Attachments, shouldRemove); 1329e8d8bef9SDimitry Andric } 13300b57cec5SDimitry Andric }; 13310b57cec5SDimitry Andric 13320b57cec5SDimitry Andric class LLVMContextImpl { 13330b57cec5SDimitry Andric public: 13340b57cec5SDimitry Andric /// OwnedModules - The set of modules instantiated in this context, and which 13350b57cec5SDimitry Andric /// will be automatically deleted if this context is deleted. 13360b57cec5SDimitry Andric SmallPtrSet<Module *, 4> OwnedModules; 13370b57cec5SDimitry Andric 13385ffd83dbSDimitry Andric /// The main remark streamer used by all the other streamers (e.g. IR, MIR, 13395ffd83dbSDimitry Andric /// frontends, etc.). This should only be used by the specific streamers, and 13405ffd83dbSDimitry Andric /// never directly. 13415ffd83dbSDimitry Andric std::unique_ptr<remarks::RemarkStreamer> MainRemarkStreamer; 13425ffd83dbSDimitry Andric 13430b57cec5SDimitry Andric std::unique_ptr<DiagnosticHandler> DiagHandler; 13440b57cec5SDimitry Andric bool RespectDiagnosticFilters = false; 13450b57cec5SDimitry Andric bool DiagnosticsHotnessRequested = false; 1346e8d8bef9SDimitry Andric /// The minimum hotness value a diagnostic needs in order to be included in 1347e8d8bef9SDimitry Andric /// optimization diagnostics. 1348e8d8bef9SDimitry Andric /// 1349e8d8bef9SDimitry Andric /// The threshold is an Optional value, which maps to one of the 3 states: 1350e8d8bef9SDimitry Andric /// 1). 0 => threshold disabled. All emarks will be printed. 1351e8d8bef9SDimitry Andric /// 2). positive int => manual threshold by user. Remarks with hotness exceed 1352e8d8bef9SDimitry Andric /// threshold will be printed. 1353e8d8bef9SDimitry Andric /// 3). None => 'auto' threshold by user. The actual value is not 1354e8d8bef9SDimitry Andric /// available at command line, but will be synced with 1355e8d8bef9SDimitry Andric /// hotness threhold from profile summary during 1356e8d8bef9SDimitry Andric /// compilation. 1357e8d8bef9SDimitry Andric /// 1358e8d8bef9SDimitry Andric /// State 1 and 2 are considered as terminal states. State transition is 1359e8d8bef9SDimitry Andric /// only allowed from 3 to 2, when the threshold is first synced with profile 1360e8d8bef9SDimitry Andric /// summary. This ensures that the threshold is set only once and stays 1361e8d8bef9SDimitry Andric /// constant. 1362e8d8bef9SDimitry Andric /// 1363e8d8bef9SDimitry Andric /// If threshold option is not specified, it is disabled (0) by default. 1364e8d8bef9SDimitry Andric Optional<uint64_t> DiagnosticsHotnessThreshold = 0; 1365e8d8bef9SDimitry Andric 13665ffd83dbSDimitry Andric /// The specialized remark streamer used by LLVM's OptimizationRemarkEmitter. 13675ffd83dbSDimitry Andric std::unique_ptr<LLVMRemarkStreamer> LLVMRS; 13680b57cec5SDimitry Andric 13690b57cec5SDimitry Andric LLVMContext::YieldCallbackTy YieldCallback = nullptr; 13700b57cec5SDimitry Andric void *YieldOpaqueHandle = nullptr; 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric using IntMapTy = 13730b57cec5SDimitry Andric DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>; 13740b57cec5SDimitry Andric IntMapTy IntConstants; 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andric using FPMapTy = 13770b57cec5SDimitry Andric DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>; 13780b57cec5SDimitry Andric FPMapTy FPConstants; 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric FoldingSet<AttributeImpl> AttrsSet; 13810b57cec5SDimitry Andric FoldingSet<AttributeListImpl> AttrsLists; 13820b57cec5SDimitry Andric FoldingSet<AttributeSetNode> AttrsSetNodes; 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric StringMap<MDString, BumpPtrAllocator> MDStringCache; 13850b57cec5SDimitry Andric DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata; 13860b57cec5SDimitry Andric DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues; 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric DenseMap<const Value *, ValueName *> ValueNames; 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 13910b57cec5SDimitry Andric DenseSet<CLASS *, CLASS##Info> CLASS##s; 13920b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric // Optional map for looking up composite types by identifier. 13950b57cec5SDimitry Andric Optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap; 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 13980b57cec5SDimitry Andric // aren't in the MDNodeSet, but they're still shared between objects, so no 13990b57cec5SDimitry Andric // one object can destroy them. Keep track of them here so we can delete 14000b57cec5SDimitry Andric // them on context teardown. 14010b57cec5SDimitry Andric std::vector<MDNode *> DistinctMDNodes; 14020b57cec5SDimitry Andric 14030b57cec5SDimitry Andric DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants; 14040b57cec5SDimitry Andric 14050b57cec5SDimitry Andric using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>; 14060b57cec5SDimitry Andric ArrayConstantsTy ArrayConstants; 14070b57cec5SDimitry Andric 14080b57cec5SDimitry Andric using StructConstantsTy = ConstantUniqueMap<ConstantStruct>; 14090b57cec5SDimitry Andric StructConstantsTy StructConstants; 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric using VectorConstantsTy = ConstantUniqueMap<ConstantVector>; 14120b57cec5SDimitry Andric VectorConstantsTy VectorConstants; 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andric DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants; 14150b57cec5SDimitry Andric 14160b57cec5SDimitry Andric DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants; 14170b57cec5SDimitry Andric 1418e8d8bef9SDimitry Andric DenseMap<Type *, std::unique_ptr<PoisonValue>> PVConstants; 1419e8d8bef9SDimitry Andric 1420e8d8bef9SDimitry Andric StringMap<std::unique_ptr<ConstantDataSequential>> CDSConstants; 14210b57cec5SDimitry Andric 14220b57cec5SDimitry Andric DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *> 14230b57cec5SDimitry Andric BlockAddresses; 1424e8d8bef9SDimitry Andric 1425e8d8bef9SDimitry Andric DenseMap<const GlobalValue *, DSOLocalEquivalent *> DSOLocalEquivalents; 1426e8d8bef9SDimitry Andric 14270b57cec5SDimitry Andric ConstantUniqueMap<ConstantExpr> ExprConstants; 14280b57cec5SDimitry Andric 14290b57cec5SDimitry Andric ConstantUniqueMap<InlineAsm> InlineAsms; 14300b57cec5SDimitry Andric 14310b57cec5SDimitry Andric ConstantInt *TheTrueVal = nullptr; 14320b57cec5SDimitry Andric ConstantInt *TheFalseVal = nullptr; 14330b57cec5SDimitry Andric 14340b57cec5SDimitry Andric std::unique_ptr<ConstantTokenNone> TheNoneToken; 14350b57cec5SDimitry Andric 14360b57cec5SDimitry Andric // Basic type instances. 14375ffd83dbSDimitry Andric Type VoidTy, LabelTy, HalfTy, BFloatTy, FloatTy, DoubleTy, MetadataTy, 14385ffd83dbSDimitry Andric TokenTy; 1439e8d8bef9SDimitry Andric Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy, X86_AMXTy; 14400b57cec5SDimitry Andric IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty; 14410b57cec5SDimitry Andric 14420b57cec5SDimitry Andric BumpPtrAllocator Alloc; 14430b57cec5SDimitry Andric UniqueStringSaver Saver{Alloc}; 14440b57cec5SDimitry Andric 14450b57cec5SDimitry Andric DenseMap<unsigned, IntegerType *> IntegerTypes; 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>; 14480b57cec5SDimitry Andric FunctionTypeSet FunctionTypes; 14490b57cec5SDimitry Andric using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>; 14500b57cec5SDimitry Andric StructTypeSet AnonStructTypes; 14510b57cec5SDimitry Andric StringMap<StructType *> NamedStructTypes; 14520b57cec5SDimitry Andric unsigned NamedStructTypesUniqueID = 0; 14530b57cec5SDimitry Andric 14540b57cec5SDimitry Andric DenseMap<std::pair<Type *, uint64_t>, ArrayType *> ArrayTypes; 14550b57cec5SDimitry Andric DenseMap<std::pair<Type *, ElementCount>, VectorType *> VectorTypes; 14560b57cec5SDimitry Andric DenseMap<Type *, PointerType *> PointerTypes; // Pointers in AddrSpace = 0 14570b57cec5SDimitry Andric DenseMap<std::pair<Type *, unsigned>, PointerType *> ASPointerTypes; 14580b57cec5SDimitry Andric 14590b57cec5SDimitry Andric /// ValueHandles - This map keeps track of all of the value handles that are 14600b57cec5SDimitry Andric /// watching a Value*. The Value::HasValueHandle bit is used to know 14610b57cec5SDimitry Andric /// whether or not a value has an entry in this map. 14620b57cec5SDimitry Andric using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>; 14630b57cec5SDimitry Andric ValueHandlesTy ValueHandles; 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 14660b57cec5SDimitry Andric StringMap<unsigned> CustomMDKindNames; 14670b57cec5SDimitry Andric 1468e8d8bef9SDimitry Andric /// Collection of metadata used in this context. 1469e8d8bef9SDimitry Andric DenseMap<const Value *, MDAttachments> ValueMetadata; 14700b57cec5SDimitry Andric 14710b57cec5SDimitry Andric /// Collection of per-GlobalObject sections used in this context. 14720b57cec5SDimitry Andric DenseMap<const GlobalObject *, StringRef> GlobalObjectSections; 14730b57cec5SDimitry Andric 14740b57cec5SDimitry Andric /// Collection of per-GlobalValue partitions used in this context. 14750b57cec5SDimitry Andric DenseMap<const GlobalValue *, StringRef> GlobalValuePartitions; 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric /// DiscriminatorTable - This table maps file:line locations to an 14780b57cec5SDimitry Andric /// integer representing the next DWARF path discriminator to assign to 14790b57cec5SDimitry Andric /// instructions in different blocks at the same location. 14800b57cec5SDimitry Andric DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable; 14810b57cec5SDimitry Andric 14820b57cec5SDimitry Andric /// A set of interned tags for operand bundles. The StringMap maps 14830b57cec5SDimitry Andric /// bundle tags to their IDs. 14840b57cec5SDimitry Andric /// 14850b57cec5SDimitry Andric /// \see LLVMContext::getOperandBundleTagID 14860b57cec5SDimitry Andric StringMap<uint32_t> BundleTagCache; 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag); 14890b57cec5SDimitry Andric void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const; 14900b57cec5SDimitry Andric uint32_t getOperandBundleTagID(StringRef Tag) const; 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric /// A set of interned synchronization scopes. The StringMap maps 14930b57cec5SDimitry Andric /// synchronization scope names to their respective synchronization scope IDs. 14940b57cec5SDimitry Andric StringMap<SyncScope::ID> SSC; 14950b57cec5SDimitry Andric 14960b57cec5SDimitry Andric /// getOrInsertSyncScopeID - Maps synchronization scope name to 14970b57cec5SDimitry Andric /// synchronization scope ID. Every synchronization scope registered with 14980b57cec5SDimitry Andric /// LLVMContext has unique ID except pre-defined ones. 14990b57cec5SDimitry Andric SyncScope::ID getOrInsertSyncScopeID(StringRef SSN); 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric /// getSyncScopeNames - Populates client supplied SmallVector with 15020b57cec5SDimitry Andric /// synchronization scope names registered with LLVMContext. Synchronization 15030b57cec5SDimitry Andric /// scope names are ordered by increasing synchronization scope IDs. 15040b57cec5SDimitry Andric void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const; 15050b57cec5SDimitry Andric 15060b57cec5SDimitry Andric /// Maintain the GC name for each function. 15070b57cec5SDimitry Andric /// 15080b57cec5SDimitry Andric /// This saves allocating an additional word in Function for programs which 15090b57cec5SDimitry Andric /// do not use GC (i.e., most programs) at the cost of increased overhead for 15100b57cec5SDimitry Andric /// clients which do use GC. 15110b57cec5SDimitry Andric DenseMap<const Function *, std::string> GCNames; 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric /// Flag to indicate if Value (other than GlobalValue) retains their name or 15140b57cec5SDimitry Andric /// not. 15150b57cec5SDimitry Andric bool DiscardValueNames = false; 15160b57cec5SDimitry Andric 15170b57cec5SDimitry Andric LLVMContextImpl(LLVMContext &C); 15180b57cec5SDimitry Andric ~LLVMContextImpl(); 15190b57cec5SDimitry Andric 15200b57cec5SDimitry Andric /// Destroy the ConstantArrays if they are not used. 15210b57cec5SDimitry Andric void dropTriviallyDeadConstantArrays(); 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andric mutable OptPassGate *OPG = nullptr; 15240b57cec5SDimitry Andric 15250b57cec5SDimitry Andric /// Access the object which can disable optional passes and individual 15260b57cec5SDimitry Andric /// optimizations at compile time. 15270b57cec5SDimitry Andric OptPassGate &getOptPassGate() const; 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric /// Set the object which can disable optional passes and individual 15300b57cec5SDimitry Andric /// optimizations at compile time. 15310b57cec5SDimitry Andric /// 15320b57cec5SDimitry Andric /// The lifetime of the object must be guaranteed to extend as long as the 15330b57cec5SDimitry Andric /// LLVMContext is used by compilation. 15340b57cec5SDimitry Andric void setOptPassGate(OptPassGate &); 1535*349cc55cSDimitry Andric 1536*349cc55cSDimitry Andric // TODO: clean up the following after we no longer support non-opaque pointer 1537*349cc55cSDimitry Andric // types. 1538*349cc55cSDimitry Andric bool getOpaquePointers(); 1539*349cc55cSDimitry Andric void setOpaquePointers(bool OP); 1540*349cc55cSDimitry Andric 1541*349cc55cSDimitry Andric private: 1542*349cc55cSDimitry Andric Optional<bool> OpaquePointers; 15430b57cec5SDimitry Andric }; 15440b57cec5SDimitry Andric 15450b57cec5SDimitry Andric } // end namespace llvm 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric #endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H 1548