diff --git a/include/llvm/IR/InstrTypes.h b/include/llvm/IR/InstrTypes.h index 8af18e14c47..8a702e3c9c5 100644 --- a/include/llvm/IR/InstrTypes.h +++ b/include/llvm/IR/InstrTypes.h @@ -1553,7 +1553,11 @@ public: } /// Determine whether the return value has the given attribute. - bool hasRetAttr(Attribute::AttrKind Kind) const; + bool hasRetAttr(Attribute::AttrKind Kind) const { + return hasRetAttrImpl(Kind); + } + /// Determine whether the return value has the given attribute. + bool hasRetAttr(StringRef Kind) const { return hasRetAttrImpl(Kind); } /// Determine whether the argument or parameter has the given attribute. bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const; @@ -2232,6 +2236,18 @@ private: return hasFnAttrOnCalledFunction(Kind); } + + /// Determine whether the return value has the given attribute. Supports + /// Attribute::AttrKind and StringRef as \p AttrKind types. + template bool hasRetAttrImpl(AttrKind Kind) const { + if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) + return true; + + // Look at the callee, if available. + if (const Function *F = getCalledFunction()) + return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); + return false; + } }; template <> diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 74a95da7993..47bf3966bc2 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -322,16 +322,6 @@ Value *CallBase::getReturnedArgOperand() const { return nullptr; } -bool CallBase::hasRetAttr(Attribute::AttrKind Kind) const { - if (Attrs.hasAttribute(AttributeList::ReturnIndex, Kind)) - return true; - - // Look at the callee, if available. - if (const Function *F = getCalledFunction()) - return F->getAttributes().hasAttribute(AttributeList::ReturnIndex, Kind); - return false; -} - /// Determine whether the argument or parameter has the given attribute. bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const { assert(ArgNo < getNumArgOperands() && "Param index out of bounds!"); diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp index 419cddc0c37..c9d6d846cba 100644 --- a/unittests/IR/InstructionsTest.cpp +++ b/unittests/IR/InstructionsTest.cpp @@ -94,6 +94,11 @@ TEST_F(ModuleWithFunctionTest, CallInst) { EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); Idx++; } + + Call->addAttribute(llvm::AttributeList::ReturnIndex, + Attribute::get(Call->getContext(), "test-str-attr")); + EXPECT_TRUE(Call->hasRetAttr("test-str-attr")); + EXPECT_FALSE(Call->hasRetAttr("not-on-call")); } TEST_F(ModuleWithFunctionTest, InvokeInst) {