From 607c8a9d1481312acb421425ac8c8df56a0c9012 Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Wed, 5 Jun 2019 20:37:47 +0000 Subject: [PATCH] IR: make getParamByValType Just Work. NFC. Most parts of LLVM don't care whether the byval type is derived from an explicit Attribute or from the parameter's pointee type, so it makes sense for the main access function to just return the right value. The very few users who do care (only BitcodeReader so far) can find out how it's specified by accessing the Attribute directly. llvm-svn: 362642 --- llvm/include/llvm/IR/Argument.h | 2 ++ llvm/include/llvm/IR/Function.h | 10 ++++++++-- llvm/include/llvm/IR/InstrTypes.h | 5 +++-- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 3 ++- llvm/lib/Bitcode/Writer/ValueEnumerator.cpp | 2 +- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp | 3 +-- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 4 +++- llvm/lib/IR/Function.cpp | 4 ++++ 8 files changed, 24 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/IR/Argument.h b/llvm/include/llvm/IR/Argument.h index 952fbcd..5f514b9 100644 --- a/llvm/include/llvm/IR/Argument.h +++ b/llvm/include/llvm/IR/Argument.h @@ -124,6 +124,8 @@ public: /// Check if an argument has a given attribute. bool hasAttribute(Attribute::AttrKind Kind) const; + Attribute getAttribute(Attribute::AttrKind Kind) const; + /// Method for support type inquiry through isa, cast, and dyn_cast. static bool classof(const Value *V) { return V->getValueID() == ArgumentVal; diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h index 896c218..b93541c 100644 --- a/llvm/include/llvm/IR/Function.h +++ b/llvm/include/llvm/IR/Function.h @@ -401,6 +401,11 @@ public: return getAttributes().hasParamAttribute(ArgNo, Kind); } + /// gets the specified attribute from the list of attributes. + Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const { + return getAttributes().getParamAttr(ArgNo, Kind); + } + /// gets the attribute from the list of attributes. Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const { return AttributeSets.getAttribute(i, Kind); @@ -431,9 +436,10 @@ public: return AttributeSets.getParamAlignment(ArgNo); } - /// Extract the byval type for a parameter (nullptr=unknown). + /// Extract the byval type for a parameter. Type *getParamByValType(unsigned ArgNo) const { - return AttributeSets.getParamByValType(ArgNo); + Type *Ty = AttributeSets.getParamByValType(ArgNo); + return Ty ? Ty : (arg_begin() + ArgNo)->getType()->getPointerElementType(); } /// Extract the number of dereferenceable bytes for a call or diff --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h index 6ce7681..237929f 100644 --- a/llvm/include/llvm/IR/InstrTypes.h +++ b/llvm/include/llvm/IR/InstrTypes.h @@ -1560,9 +1560,10 @@ public: return Attrs.getParamAlignment(ArgNo); } - /// Extract the byval type for a call or parameter (nullptr=unknown). + /// Extract the byval type for a call or parameter. Type *getParamByValType(unsigned ArgNo) const { - return Attrs.getParamByValType(ArgNo); + Type *Ty = Attrs.getParamByValType(ArgNo); + return Ty ? Ty : getArgOperand(ArgNo)->getType()->getPointerElementType(); } /// Extract the number of dereferenceable bytes for a call or diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 9f562ba..c33fc56 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -3049,7 +3049,8 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef Record) { // pointee type. There should be no opaque pointers where the byval type is // implicit. for (auto &Arg : Func->args()) { - if (Arg.hasByValAttr() && !Arg.getParamByValType()) { + if (Arg.hasByValAttr() && + !Arg.getAttribute(Attribute::ByVal).getValueAsType()) { Arg.removeAttr(Attribute::ByVal); Arg.addAttr(Attribute::getWithByValType( Context, Arg.getType()->getPointerElementType())); diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp index 143570f..f59c906 100644 --- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -951,7 +951,7 @@ void ValueEnumerator::incorporateFunction(const Function &F) { // Adding function arguments to the value table. for (const auto &I : F.args()) { EnumerateValue(&I); - if (I.hasAttribute(Attribute::ByVal) && I.getParamByValType()) + if (I.hasAttribute(Attribute::ByVal)) EnumerateType(I.getParamByValType()); } FirstFuncConstantID = Values.size(); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 4f7257d..07d6ac8 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -9584,8 +9584,7 @@ void SelectionDAGISel::LowerArguments(const Function &F) { // For ByVal, size and alignment should be passed from FE. BE will // guess if this info is not there but there are cases it cannot get // right. - unsigned FrameSize = DL.getTypeAllocSize( - Arg.getParamByValType() ? Arg.getParamByValType() : ElementTy); + unsigned FrameSize = DL.getTypeAllocSize(Arg.getParamByValType()); Flags.setByValSize(FrameSize); unsigned FrameAlign; diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp index 6e6917b..c2123db 100644 --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -112,7 +112,9 @@ void TargetLoweringBase::ArgListEntry::setAttributes(const CallBase *Call, IsSwiftSelf = Call->paramHasAttr(ArgIdx, Attribute::SwiftSelf); IsSwiftError = Call->paramHasAttr(ArgIdx, Attribute::SwiftError); Alignment = Call->getParamAlignment(ArgIdx); - ByValType = Call->getParamByValType(ArgIdx); + ByValType = nullptr; + if (Call->paramHasAttr(ArgIdx, Attribute::ByVal)) + ByValType = Call->getParamByValType(ArgIdx); } /// Generate a libcall taking the given operands as arguments and returning a diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index a4a78ca..c88fd1a 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -194,6 +194,10 @@ bool Argument::hasAttribute(Attribute::AttrKind Kind) const { return getParent()->hasParamAttribute(getArgNo(), Kind); } +Attribute Argument::getAttribute(Attribute::AttrKind Kind) const { + return getParent()->getParamAttribute(getArgNo(), Kind); +} + //===----------------------------------------------------------------------===// // Helper Methods in Function //===----------------------------------------------------------------------===// -- 2.7.4