From 7c4fc4e5ae4c8145974397da295587e77c770f62 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 31 Oct 2014 23:58:04 +0000 Subject: [PATCH] IR: MDNode => Value: Add Instruction::getMDNode() Add `Instruction::getMDNode()` that casts to `MDNode` before changing `Instruction::getMetadata()` to return `Value`. This avoids adding `cast_or_null` boiler-plate throughout the code. Part of PR21433. llvm-svn: 221023 --- llvm/include/llvm/IR/Instruction.h | 20 ++++++++++++++++++++ llvm/lib/IR/Metadata.cpp | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h index 4baacba78ed9..ac042ff7ba99 100644 --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -153,6 +153,24 @@ public: return getMetadataImpl(Kind); } + /// Get the the metadata as an MDNode. + /// + /// \pre Any KindID metadata is implemented using \a MDNode. + MDNode *getMDNode(unsigned KindID) const { + if (!hasMetadata()) + return nullptr; + return getMDNodeImpl(KindID); + } + + /// Get the the metadata as an MDNode. + /// + /// \pre Any KindID metadata is implemented using \a MDNode. + MDNode *getMDNode(StringRef Kind) const { + if (!hasMetadata()) + return nullptr; + return getMDNodeImpl(Kind); + } + /// getAllMetadata - Get all metadata attached to this Instruction. The first /// element of each pair returned is the KindID, the second element is the /// metadata value. This list is returned sorted by the KindID. @@ -273,6 +291,8 @@ private: // These are all implemented in Metadata.cpp. MDNode *getMetadataImpl(unsigned KindID) const; MDNode *getMetadataImpl(StringRef Kind) const; + MDNode *getMDNodeImpl(unsigned KindID) const; + MDNode *getMDNodeImpl(StringRef Kind) const; void getAllMetadataImpl(SmallVectorImpl > &)const; void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl > &) const; diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 3d869e87c4d3..60cd2fc9061b 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -609,6 +609,14 @@ MDNode *Instruction::getMetadataImpl(StringRef Kind) const { return getMetadataImpl(getContext().getMDKindID(Kind)); } +MDNode *Instruction::getMDNodeImpl(unsigned KindID) const { + return getMetadataImpl(KindID); +} + +MDNode *Instruction::getMDNodeImpl(StringRef Kind) const { + return getMetadataImpl(Kind); +} + void Instruction::dropUnknownMetadata(ArrayRef KnownIDs) { SmallSet KnownSet; KnownSet.insert(KnownIDs.begin(), KnownIDs.end());