forked from OSchip/llvm-project
[GlobalISel][CallLowering] NFC: Unify flag-setting from CallBase + AttributeList
It's annoying to have to maintain multiple, nearly identical chains of if statements which all set the same attributes. Add a helper function, `addFlagsUsingAttrFn` which performs the attribute setting. Then, use wrappers for that function in `lowerCall` and `setArgFlags`. (Note that the flag-setting code in `setArgFlags` was missing the returned attribute. There's no selection for this yet, so no test. It's an example of the kind of thing this lets us avoid, though.) Differential Revision: https://reviews.llvm.org/D86159
This commit is contained in:
parent
f29e6277ad
commit
bf36e90295
|
@ -213,6 +213,12 @@ protected:
|
|||
ISD::ArgFlagsTy getAttributesForArgIdx(const CallBase &Call,
|
||||
unsigned ArgIdx) const;
|
||||
|
||||
/// Adds flags to \p Flags based off of the attributes in \p Attrs.
|
||||
/// \p OpIdx is the index in \p Attrs to add flags from.
|
||||
void addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags,
|
||||
const AttributeList &Attrs,
|
||||
unsigned OpIdx) const;
|
||||
|
||||
template <typename FuncInfoTy>
|
||||
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL,
|
||||
const FuncInfoTy &FuncInfo) const;
|
||||
|
|
|
@ -30,34 +30,51 @@ using namespace llvm;
|
|||
|
||||
void CallLowering::anchor() {}
|
||||
|
||||
/// Helper function which updates \p Flags when \p AttrFn returns true.
|
||||
static void
|
||||
addFlagsUsingAttrFn(ISD::ArgFlagsTy &Flags,
|
||||
const std::function<bool(Attribute::AttrKind)> &AttrFn) {
|
||||
if (AttrFn(Attribute::SExt))
|
||||
Flags.setSExt();
|
||||
if (AttrFn(Attribute::ZExt))
|
||||
Flags.setZExt();
|
||||
if (AttrFn(Attribute::InReg))
|
||||
Flags.setInReg();
|
||||
if (AttrFn(Attribute::StructRet))
|
||||
Flags.setSRet();
|
||||
if (AttrFn(Attribute::Nest))
|
||||
Flags.setNest();
|
||||
if (AttrFn(Attribute::ByVal))
|
||||
Flags.setByVal();
|
||||
if (AttrFn(Attribute::Preallocated))
|
||||
Flags.setPreallocated();
|
||||
if (AttrFn(Attribute::InAlloca))
|
||||
Flags.setInAlloca();
|
||||
if (AttrFn(Attribute::Returned))
|
||||
Flags.setReturned();
|
||||
if (AttrFn(Attribute::SwiftSelf))
|
||||
Flags.setSwiftSelf();
|
||||
if (AttrFn(Attribute::SwiftError))
|
||||
Flags.setSwiftError();
|
||||
}
|
||||
|
||||
ISD::ArgFlagsTy CallLowering::getAttributesForArgIdx(const CallBase &Call,
|
||||
unsigned ArgIdx) const {
|
||||
ISD::ArgFlagsTy Flags;
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::SExt))
|
||||
Flags.setSExt();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::ZExt))
|
||||
Flags.setZExt();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::InReg))
|
||||
Flags.setInReg();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::StructRet))
|
||||
Flags.setSRet();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::Nest))
|
||||
Flags.setNest();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::ByVal))
|
||||
Flags.setByVal();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::Preallocated))
|
||||
Flags.setPreallocated();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::InAlloca))
|
||||
Flags.setInAlloca();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::Returned))
|
||||
Flags.setReturned();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::SwiftSelf))
|
||||
Flags.setSwiftSelf();
|
||||
if (Call.paramHasAttr(ArgIdx, Attribute::SwiftError))
|
||||
Flags.setSwiftError();
|
||||
addFlagsUsingAttrFn(Flags, [&Call, &ArgIdx](Attribute::AttrKind Attr) {
|
||||
return Call.paramHasAttr(ArgIdx, Attr);
|
||||
});
|
||||
return Flags;
|
||||
}
|
||||
|
||||
void CallLowering::addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags,
|
||||
const AttributeList &Attrs,
|
||||
unsigned OpIdx) const {
|
||||
addFlagsUsingAttrFn(Flags, [&Attrs, &OpIdx](Attribute::AttrKind Attr) {
|
||||
return Attrs.hasAttribute(OpIdx, Attr);
|
||||
});
|
||||
}
|
||||
|
||||
bool CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, const CallBase &CB,
|
||||
ArrayRef<Register> ResRegs,
|
||||
ArrayRef<ArrayRef<Register>> ArgRegs,
|
||||
|
@ -118,24 +135,7 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
|
|||
const FuncInfoTy &FuncInfo) const {
|
||||
auto &Flags = Arg.Flags[0];
|
||||
const AttributeList &Attrs = FuncInfo.getAttributes();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
|
||||
Flags.setZExt();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
|
||||
Flags.setSExt();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
|
||||
Flags.setInReg();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
|
||||
Flags.setSRet();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
|
||||
Flags.setSwiftSelf();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
|
||||
Flags.setSwiftError();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
|
||||
Flags.setByVal();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::Preallocated))
|
||||
Flags.setPreallocated();
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
|
||||
Flags.setInAlloca();
|
||||
addArgFlagsFromAttributes(Flags, Attrs, OpIdx);
|
||||
|
||||
if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated()) {
|
||||
Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
|
||||
|
@ -152,8 +152,6 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
|
|||
FrameAlign = Align(getTLI()->getByValTypeAlignment(ElementTy, DL));
|
||||
Flags.setByValAlign(FrameAlign);
|
||||
}
|
||||
if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
|
||||
Flags.setNest();
|
||||
Flags.setOrigAlign(DL.getABITypeAlign(Arg.Ty));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue