Skip to content

Commit 608c8b6

Browse files
committed
[IR] Make add/remove Attributes use AttrBuilder instead of AttributeList
This change cleans up call sites and avoids creating temporary AttributeList objects. NFC llvm-svn: 301697
1 parent e8dea1b commit 608c8b6

File tree

12 files changed

+51
-93
lines changed

12 files changed

+51
-93
lines changed

llvm/include/llvm/CodeGen/CommandFlags.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -346,29 +346,21 @@ static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
346346
Module &M) {
347347
for (auto &F : M) {
348348
auto &Ctx = F.getContext();
349-
AttributeList Attrs = F.getAttributes(), NewAttrs;
349+
AttributeList Attrs = F.getAttributes();
350+
AttrBuilder NewAttrs;
350351

351352
if (!CPU.empty())
352-
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex,
353-
"target-cpu", CPU);
354-
353+
NewAttrs.addAttribute("target-cpu", CPU);
355354
if (!Features.empty())
356-
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex,
357-
"target-features", Features);
358-
355+
NewAttrs.addAttribute("target-features", Features);
359356
if (DisableFPElim.getNumOccurrences() > 0)
360-
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex,
361-
"no-frame-pointer-elim",
362-
DisableFPElim ? "true" : "false");
363-
357+
NewAttrs.addAttribute("no-frame-pointer-elim",
358+
DisableFPElim ? "true" : "false");
364359
if (DisableTailCalls.getNumOccurrences() > 0)
365-
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex,
366-
"disable-tail-calls",
367-
toStringRef(DisableTailCalls));
368-
360+
NewAttrs.addAttribute("disable-tail-calls",
361+
toStringRef(DisableTailCalls));
369362
if (StackRealign)
370-
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex,
371-
"stackrealign");
363+
NewAttrs.addAttribute("stackrealign");
372364

373365
if (TrapFuncName.getNumOccurrences() > 0)
374366
for (auto &B : F)
@@ -382,8 +374,8 @@ static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
382374
Attribute::get(Ctx, "trap-func-name", TrapFuncName));
383375

384376
// Let NewAttrs override Attrs.
385-
NewAttrs = Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs);
386-
F.setAttributes(NewAttrs);
377+
F.setAttributes(
378+
Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
387379
}
388380
}
389381

llvm/include/llvm/IR/Attributes.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,6 @@ class AttributeList {
353353

354354
/// \brief Add attributes to the attribute set at the given index. Because
355355
/// attribute sets are immutable, this returns a new set.
356-
AttributeList addAttributes(LLVMContext &C, unsigned Index,
357-
AttributeList Attrs) const;
358-
359356
AttributeList addAttributes(LLVMContext &C, unsigned Index,
360357
const AttrBuilder &B) const;
361358

@@ -375,13 +372,7 @@ class AttributeList {
375372
/// attribute list. Because attribute lists are immutable, this returns the
376373
/// new list.
377374
AttributeList removeAttributes(LLVMContext &C, unsigned Index,
378-
AttributeList Attrs) const;
379-
380-
/// \brief Remove the specified attributes at the specified index from this
381-
/// attribute list. Because attribute lists are immutable, this returns the
382-
/// new list.
383-
AttributeList removeAttributes(LLVMContext &C, unsigned Index,
384-
const AttrBuilder &Attrs) const;
375+
const AttrBuilder &AttrsToRemove) const;
385376

386377
/// \brief Remove all attributes at the specified index from this
387378
/// attribute list. Because attribute lists are immutable, this returns the

llvm/include/llvm/IR/Function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Function : public GlobalObject, public ilist_node<Function> {
279279
void addAttribute(unsigned i, Attribute Attr);
280280

281281
/// @brief adds the attributes to the list of attributes.
282-
void addAttributes(unsigned i, AttributeList Attrs);
282+
void addAttributes(unsigned i, const AttrBuilder &Attrs);
283283

284284
/// @brief removes the attribute from the list of attributes.
285285
void removeAttribute(unsigned i, Attribute::AttrKind Kind);
@@ -288,7 +288,7 @@ class Function : public GlobalObject, public ilist_node<Function> {
288288
void removeAttribute(unsigned i, StringRef Kind);
289289

290290
/// @brief removes the attributes from the list of attributes.
291-
void removeAttributes(unsigned i, AttributeList Attrs);
291+
void removeAttributes(unsigned i, const AttrBuilder &Attrs);
292292

293293
/// @brief check if an attributes is in the list of attributes.
294294
bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {

llvm/lib/CodeGen/GlobalISel/CallLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
8383
// For ByVal, alignment should be passed from FE. BE will guess if
8484
// this info is not there but there are cases it cannot get right.
8585
unsigned FrameAlign;
86-
if (FuncInfo.getParamAlignment(OpIdx - 1))
87-
FrameAlign = FuncInfo.getParamAlignment(OpIdx - 1);
86+
if (FuncInfo.getParamAlignment(OpIdx - 2))
87+
FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
8888
else
8989
FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
9090
Arg.Flags.setByValAlign(FrameAlign);

llvm/lib/IR/Attributes.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,17 @@ AttributeList AttributeList::get(LLVMContext &C,
936936
AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
937937
Attribute::AttrKind Kind) const {
938938
if (hasAttribute(Index, Kind)) return *this;
939-
return addAttributes(C, Index, AttributeList::get(C, Index, Kind));
939+
AttrBuilder B;
940+
B.addAttribute(Kind);
941+
return addAttributes(C, Index, B);
940942
}
941943

942944
AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
943945
StringRef Kind,
944946
StringRef Value) const {
945947
AttrBuilder B;
946948
B.addAttribute(Kind, Value);
947-
return addAttributes(C, Index, AttributeList::get(C, Index, B));
949+
return addAttributes(C, Index, B);
948950
}
949951

950952
AttributeList AttributeList::addAttribute(LLVMContext &C,
@@ -977,14 +979,6 @@ AttributeList AttributeList::addAttribute(LLVMContext &C,
977979
return get(C, AttrVec);
978980
}
979981

980-
AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
981-
AttributeList Attrs) const {
982-
if (!pImpl) return Attrs;
983-
if (!Attrs.pImpl) return *this;
984-
985-
return addAttributes(C, Index, Attrs.getAttributes(Index));
986-
}
987-
988982
AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
989983
const AttrBuilder &B) const {
990984
if (!B.hasAttributes())
@@ -1034,18 +1028,17 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
10341028
AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
10351029
Attribute::AttrKind Kind) const {
10361030
if (!hasAttribute(Index, Kind)) return *this;
1037-
return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
1031+
AttrBuilder B;
1032+
B.addAttribute(Kind);
1033+
return removeAttributes(C, Index, B);
10381034
}
10391035

10401036
AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
10411037
StringRef Kind) const {
10421038
if (!hasAttribute(Index, Kind)) return *this;
1043-
return removeAttributes(C, Index, AttributeList::get(C, Index, Kind));
1044-
}
1045-
1046-
AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
1047-
AttributeList Attrs) const {
1048-
return removeAttributes(C, Index, AttrBuilder(Attrs.getAttributes(Index)));
1039+
AttrBuilder B;
1040+
B.addAttribute(Kind);
1041+
return removeAttributes(C, Index, B);
10491042
}
10501043

10511044
AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
@@ -1103,15 +1096,15 @@ AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
11031096
uint64_t Bytes) const {
11041097
AttrBuilder B;
11051098
B.addDereferenceableAttr(Bytes);
1106-
return addAttributes(C, Index, AttributeList::get(C, Index, B));
1099+
return addAttributes(C, Index, B);
11071100
}
11081101

11091102
AttributeList
11101103
AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
11111104
uint64_t Bytes) const {
11121105
AttrBuilder B;
11131106
B.addDereferenceableOrNullAttr(Bytes);
1114-
return addAttributes(C, Index, AttributeList::get(C, Index, B));
1107+
return addAttributes(C, Index, B);
11151108
}
11161109

11171110
AttributeList
@@ -1120,7 +1113,7 @@ AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
11201113
const Optional<unsigned> &NumElemsArg) {
11211114
AttrBuilder B;
11221115
B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1123-
return addAttributes(C, Index, AttributeList::get(C, Index, B));
1116+
return addAttributes(C, Index, B);
11241117
}
11251118

11261119
//===----------------------------------------------------------------------===//
@@ -1610,12 +1603,10 @@ static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
16101603
// If upgrading the SSP attribute, clear out the old SSP Attributes first.
16111604
// Having multiple SSP attributes doesn't actually hurt, but it adds useless
16121605
// clutter to the IR.
1613-
AttrBuilder B;
1614-
B.addAttribute(Attribute::StackProtect)
1615-
.addAttribute(Attribute::StackProtectStrong)
1616-
.addAttribute(Attribute::StackProtectReq);
1617-
AttributeList OldSSPAttr =
1618-
AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
1606+
AttrBuilder OldSSPAttr;
1607+
OldSSPAttr.addAttribute(Attribute::StackProtect)
1608+
.addAttribute(Attribute::StackProtectStrong)
1609+
.addAttribute(Attribute::StackProtectReq);
16191610

16201611
if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
16211612
Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);

llvm/lib/IR/Function.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void Function::addAttribute(unsigned i, Attribute Attr) {
328328
setAttributes(PAL);
329329
}
330330

331-
void Function::addAttributes(unsigned i, AttributeList Attrs) {
331+
void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
332332
AttributeList PAL = getAttributes();
333333
PAL = PAL.addAttributes(getContext(), i, Attrs);
334334
setAttributes(PAL);
@@ -346,7 +346,7 @@ void Function::removeAttribute(unsigned i, StringRef Kind) {
346346
setAttributes(PAL);
347347
}
348348

349-
void Function::removeAttributes(unsigned i, AttributeList Attrs) {
349+
void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
350350
AttributeList PAL = getAttributes();
351351
PAL = PAL.removeAttributes(getContext(), i, Attrs);
352352
setAttributes(PAL);

llvm/lib/Target/Mips/Mips16HardFloat.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,14 @@ static void createFPFnStub(Function *F, Module *M, FPParamVariant PV,
490490
// remove the use-soft-float attribute
491491
//
492492
static void removeUseSoftFloat(Function &F) {
493-
AttributeList A;
493+
AttrBuilder B;
494494
DEBUG(errs() << "removing -use-soft-float\n");
495-
A = A.addAttribute(F.getContext(), AttributeList::FunctionIndex,
496-
"use-soft-float", "false");
497-
F.removeAttributes(AttributeList::FunctionIndex, A);
495+
B.addAttribute("use-soft-float", "false");
496+
F.removeAttributes(AttributeList::FunctionIndex, B);
498497
if (F.hasFnAttribute("use-soft-float")) {
499498
DEBUG(errs() << "still has -use-soft-float\n");
500499
}
501-
F.addAttributes(AttributeList::FunctionIndex, A);
500+
F.addAttributes(AttributeList::FunctionIndex, B);
502501
}
503502

504503

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,7 @@ static Function *createClone(Function &F, Twine Suffix, coro::Shape &Shape,
245245
// Remove old return attributes.
246246
NewF->removeAttributes(
247247
AttributeList::ReturnIndex,
248-
AttributeList::get(
249-
NewF->getContext(), AttributeList::ReturnIndex,
250-
AttributeFuncs::typeIncompatible(NewF->getReturnType())));
248+
AttributeFuncs::typeIncompatible(NewF->getReturnType()));
251249

252250
// Make AllocaSpillBlock the new entry block.
253251
auto *SwitchBB = cast<BasicBlock>(VMap[ResumeEntry]);

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class DataFlowSanitizer : public ModulePass {
254254
MDNode *ColdCallWeights;
255255
DFSanABIList ABIList;
256256
DenseMap<Value *, Function *> UnwrappedFnMap;
257-
AttributeList ReadOnlyNoneAttrs;
257+
AttrBuilder ReadOnlyNoneAttrs;
258258
bool DFSanRuntimeShadowMask;
259259

260260
Value *getShadowAddress(Value *Addr, Instruction *Pos);
@@ -544,16 +544,12 @@ DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
544544
NewF->copyAttributesFrom(F);
545545
NewF->removeAttributes(
546546
AttributeList::ReturnIndex,
547-
AttributeList::get(
548-
F->getContext(), AttributeList::ReturnIndex,
549-
AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
547+
AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
550548

551549
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
552550
if (F->isVarArg()) {
553-
NewF->removeAttributes(
554-
AttributeList::FunctionIndex,
555-
AttributeList().addAttribute(*Ctx, AttributeList::FunctionIndex,
556-
"split-stack"));
551+
NewF->removeAttributes(AttributeList::FunctionIndex,
552+
AttrBuilder().addAttribute("split-stack"));
557553
CallInst::Create(DFSanVarargWrapperFn,
558554
IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
559555
BB);
@@ -698,9 +694,8 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
698694
}
699695
}
700696

701-
AttrBuilder B;
702-
B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
703-
ReadOnlyNoneAttrs = AttributeList::get(*Ctx, AttributeList::FunctionIndex, B);
697+
ReadOnlyNoneAttrs.addAttribute(Attribute::ReadOnly)
698+
.addAttribute(Attribute::ReadNone);
704699

705700
// First, change the ABI of every function in the module. ABI-listed
706701
// functions keep their original ABI and get a wrapper function.
@@ -722,9 +717,7 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
722717
NewF->copyAttributesFrom(&F);
723718
NewF->removeAttributes(
724719
AttributeList::ReturnIndex,
725-
AttributeList::get(
726-
NewF->getContext(), AttributeList::ReturnIndex,
727-
AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
720+
AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
728721
for (Function::arg_iterator FArg = F.arg_begin(),
729722
NewFArg = NewF->arg_begin(),
730723
FArgEnd = F.arg_end();

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,10 +2607,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
26072607
AttrBuilder B;
26082608
B.addAttribute(Attribute::ReadOnly)
26092609
.addAttribute(Attribute::ReadNone);
2610-
Func->removeAttributes(AttributeList::FunctionIndex,
2611-
AttributeList::get(Func->getContext(),
2612-
AttributeList::FunctionIndex,
2613-
B));
2610+
Func->removeAttributes(AttributeList::FunctionIndex, B);
26142611
}
26152612

26162613
maybeMarkSanitizerLibraryCallNoBuiltin(Call, TLI);
@@ -3659,9 +3656,7 @@ bool MemorySanitizer::runOnFunction(Function &F) {
36593656
AttrBuilder B;
36603657
B.addAttribute(Attribute::ReadOnly)
36613658
.addAttribute(Attribute::ReadNone);
3662-
F.removeAttributes(
3663-
AttributeList::FunctionIndex,
3664-
AttributeList::get(F.getContext(), AttributeList::FunctionIndex, B));
3659+
F.removeAttributes(AttributeList::FunctionIndex, B);
36653660

36663661
return Visitor.runOnFunction();
36673662
}

0 commit comments

Comments
 (0)