Skip to content

Commit bf36e90

Browse files
author
Jessica Paquette
committed
[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
1 parent f29e627 commit bf36e90

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ class CallLowering {
213213
ISD::ArgFlagsTy getAttributesForArgIdx(const CallBase &Call,
214214
unsigned ArgIdx) const;
215215

216+
/// Adds flags to \p Flags based off of the attributes in \p Attrs.
217+
/// \p OpIdx is the index in \p Attrs to add flags from.
218+
void addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags,
219+
const AttributeList &Attrs,
220+
unsigned OpIdx) const;
221+
216222
template <typename FuncInfoTy>
217223
void setArgFlags(ArgInfo &Arg, unsigned OpIdx, const DataLayout &DL,
218224
const FuncInfoTy &FuncInfo) const;

llvm/lib/CodeGen/GlobalISel/CallLowering.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,51 @@ using namespace llvm;
3030

3131
void CallLowering::anchor() {}
3232

33-
ISD::ArgFlagsTy CallLowering::getAttributesForArgIdx(const CallBase &Call,
34-
unsigned ArgIdx) const {
35-
ISD::ArgFlagsTy Flags;
36-
if (Call.paramHasAttr(ArgIdx, Attribute::SExt))
33+
/// Helper function which updates \p Flags when \p AttrFn returns true.
34+
static void
35+
addFlagsUsingAttrFn(ISD::ArgFlagsTy &Flags,
36+
const std::function<bool(Attribute::AttrKind)> &AttrFn) {
37+
if (AttrFn(Attribute::SExt))
3738
Flags.setSExt();
38-
if (Call.paramHasAttr(ArgIdx, Attribute::ZExt))
39+
if (AttrFn(Attribute::ZExt))
3940
Flags.setZExt();
40-
if (Call.paramHasAttr(ArgIdx, Attribute::InReg))
41+
if (AttrFn(Attribute::InReg))
4142
Flags.setInReg();
42-
if (Call.paramHasAttr(ArgIdx, Attribute::StructRet))
43+
if (AttrFn(Attribute::StructRet))
4344
Flags.setSRet();
44-
if (Call.paramHasAttr(ArgIdx, Attribute::Nest))
45+
if (AttrFn(Attribute::Nest))
4546
Flags.setNest();
46-
if (Call.paramHasAttr(ArgIdx, Attribute::ByVal))
47+
if (AttrFn(Attribute::ByVal))
4748
Flags.setByVal();
48-
if (Call.paramHasAttr(ArgIdx, Attribute::Preallocated))
49+
if (AttrFn(Attribute::Preallocated))
4950
Flags.setPreallocated();
50-
if (Call.paramHasAttr(ArgIdx, Attribute::InAlloca))
51+
if (AttrFn(Attribute::InAlloca))
5152
Flags.setInAlloca();
52-
if (Call.paramHasAttr(ArgIdx, Attribute::Returned))
53+
if (AttrFn(Attribute::Returned))
5354
Flags.setReturned();
54-
if (Call.paramHasAttr(ArgIdx, Attribute::SwiftSelf))
55+
if (AttrFn(Attribute::SwiftSelf))
5556
Flags.setSwiftSelf();
56-
if (Call.paramHasAttr(ArgIdx, Attribute::SwiftError))
57+
if (AttrFn(Attribute::SwiftError))
5758
Flags.setSwiftError();
59+
}
60+
61+
ISD::ArgFlagsTy CallLowering::getAttributesForArgIdx(const CallBase &Call,
62+
unsigned ArgIdx) const {
63+
ISD::ArgFlagsTy Flags;
64+
addFlagsUsingAttrFn(Flags, [&Call, &ArgIdx](Attribute::AttrKind Attr) {
65+
return Call.paramHasAttr(ArgIdx, Attr);
66+
});
5867
return Flags;
5968
}
6069

70+
void CallLowering::addArgFlagsFromAttributes(ISD::ArgFlagsTy &Flags,
71+
const AttributeList &Attrs,
72+
unsigned OpIdx) const {
73+
addFlagsUsingAttrFn(Flags, [&Attrs, &OpIdx](Attribute::AttrKind Attr) {
74+
return Attrs.hasAttribute(OpIdx, Attr);
75+
});
76+
}
77+
6178
bool CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, const CallBase &CB,
6279
ArrayRef<Register> ResRegs,
6380
ArrayRef<ArrayRef<Register>> ArgRegs,
@@ -118,24 +135,7 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
118135
const FuncInfoTy &FuncInfo) const {
119136
auto &Flags = Arg.Flags[0];
120137
const AttributeList &Attrs = FuncInfo.getAttributes();
121-
if (Attrs.hasAttribute(OpIdx, Attribute::ZExt))
122-
Flags.setZExt();
123-
if (Attrs.hasAttribute(OpIdx, Attribute::SExt))
124-
Flags.setSExt();
125-
if (Attrs.hasAttribute(OpIdx, Attribute::InReg))
126-
Flags.setInReg();
127-
if (Attrs.hasAttribute(OpIdx, Attribute::StructRet))
128-
Flags.setSRet();
129-
if (Attrs.hasAttribute(OpIdx, Attribute::SwiftSelf))
130-
Flags.setSwiftSelf();
131-
if (Attrs.hasAttribute(OpIdx, Attribute::SwiftError))
132-
Flags.setSwiftError();
133-
if (Attrs.hasAttribute(OpIdx, Attribute::ByVal))
134-
Flags.setByVal();
135-
if (Attrs.hasAttribute(OpIdx, Attribute::Preallocated))
136-
Flags.setPreallocated();
137-
if (Attrs.hasAttribute(OpIdx, Attribute::InAlloca))
138-
Flags.setInAlloca();
138+
addArgFlagsFromAttributes(Flags, Attrs, OpIdx);
139139

140140
if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated()) {
141141
Type *ElementTy = cast<PointerType>(Arg.Ty)->getElementType();
@@ -152,8 +152,6 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
152152
FrameAlign = Align(getTLI()->getByValTypeAlignment(ElementTy, DL));
153153
Flags.setByValAlign(FrameAlign);
154154
}
155-
if (Attrs.hasAttribute(OpIdx, Attribute::Nest))
156-
Flags.setNest();
157155
Flags.setOrigAlign(DL.getABITypeAlign(Arg.Ty));
158156
}
159157

0 commit comments

Comments
 (0)