[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
This commit is contained in:
Reid Kleckner 2017-04-28 21:48:28 +00:00
parent e8dea1bc56
commit 608c8b63b3
12 changed files with 51 additions and 93 deletions

View File

@ -346,29 +346,21 @@ static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
Module &M) { Module &M) {
for (auto &F : M) { for (auto &F : M) {
auto &Ctx = F.getContext(); auto &Ctx = F.getContext();
AttributeList Attrs = F.getAttributes(), NewAttrs; AttributeList Attrs = F.getAttributes();
AttrBuilder NewAttrs;
if (!CPU.empty()) if (!CPU.empty())
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, NewAttrs.addAttribute("target-cpu", CPU);
"target-cpu", CPU);
if (!Features.empty()) if (!Features.empty())
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, NewAttrs.addAttribute("target-features", Features);
"target-features", Features);
if (DisableFPElim.getNumOccurrences() > 0) if (DisableFPElim.getNumOccurrences() > 0)
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, NewAttrs.addAttribute("no-frame-pointer-elim",
"no-frame-pointer-elim", DisableFPElim ? "true" : "false");
DisableFPElim ? "true" : "false");
if (DisableTailCalls.getNumOccurrences() > 0) if (DisableTailCalls.getNumOccurrences() > 0)
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, NewAttrs.addAttribute("disable-tail-calls",
"disable-tail-calls", toStringRef(DisableTailCalls));
toStringRef(DisableTailCalls));
if (StackRealign) if (StackRealign)
NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, NewAttrs.addAttribute("stackrealign");
"stackrealign");
if (TrapFuncName.getNumOccurrences() > 0) if (TrapFuncName.getNumOccurrences() > 0)
for (auto &B : F) for (auto &B : F)
@ -382,8 +374,8 @@ static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
Attribute::get(Ctx, "trap-func-name", TrapFuncName)); Attribute::get(Ctx, "trap-func-name", TrapFuncName));
// Let NewAttrs override Attrs. // Let NewAttrs override Attrs.
NewAttrs = Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs); F.setAttributes(
F.setAttributes(NewAttrs); Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
} }
} }

View File

@ -353,9 +353,6 @@ public:
/// \brief Add attributes to the attribute set at the given index. Because /// \brief Add attributes to the attribute set at the given index. Because
/// attribute sets are immutable, this returns a new set. /// attribute sets are immutable, this returns a new set.
AttributeList addAttributes(LLVMContext &C, unsigned Index,
AttributeList Attrs) const;
AttributeList addAttributes(LLVMContext &C, unsigned Index, AttributeList addAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &B) const; const AttrBuilder &B) const;
@ -375,13 +372,7 @@ public:
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Because attribute lists are immutable, this returns the
/// new list. /// new list.
AttributeList removeAttributes(LLVMContext &C, unsigned Index, AttributeList removeAttributes(LLVMContext &C, unsigned Index,
AttributeList Attrs) const; const AttrBuilder &AttrsToRemove) const;
/// \brief Remove the specified attributes at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the
/// new list.
AttributeList removeAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &Attrs) const;
/// \brief Remove all attributes at the specified index from this /// \brief Remove all attributes at the specified index from this
/// attribute list. Because attribute lists are immutable, this returns the /// attribute list. Because attribute lists are immutable, this returns the

View File

@ -279,7 +279,7 @@ public:
void addAttribute(unsigned i, Attribute Attr); void addAttribute(unsigned i, Attribute Attr);
/// @brief adds the attributes to the list of attributes. /// @brief adds the attributes to the list of attributes.
void addAttributes(unsigned i, AttributeList Attrs); void addAttributes(unsigned i, const AttrBuilder &Attrs);
/// @brief removes the attribute from the list of attributes. /// @brief removes the attribute from the list of attributes.
void removeAttribute(unsigned i, Attribute::AttrKind Kind); void removeAttribute(unsigned i, Attribute::AttrKind Kind);
@ -288,7 +288,7 @@ public:
void removeAttribute(unsigned i, StringRef Kind); void removeAttribute(unsigned i, StringRef Kind);
/// @brief removes the attributes from the list of attributes. /// @brief removes the attributes from the list of attributes.
void removeAttributes(unsigned i, AttributeList Attrs); void removeAttributes(unsigned i, const AttrBuilder &Attrs);
/// @brief check if an attributes is in the list of attributes. /// @brief check if an attributes is in the list of attributes.
bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const { bool hasAttribute(unsigned i, Attribute::AttrKind Kind) const {

View File

@ -83,8 +83,8 @@ void CallLowering::setArgFlags(CallLowering::ArgInfo &Arg, unsigned OpIdx,
// For ByVal, alignment should be passed from FE. BE will guess if // For ByVal, alignment should be passed from FE. BE will guess if
// this info is not there but there are cases it cannot get right. // this info is not there but there are cases it cannot get right.
unsigned FrameAlign; unsigned FrameAlign;
if (FuncInfo.getParamAlignment(OpIdx - 1)) if (FuncInfo.getParamAlignment(OpIdx - 2))
FrameAlign = FuncInfo.getParamAlignment(OpIdx - 1); FrameAlign = FuncInfo.getParamAlignment(OpIdx - 2);
else else
FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL); FrameAlign = getTLI()->getByValTypeAlignment(ElementTy, DL);
Arg.Flags.setByValAlign(FrameAlign); Arg.Flags.setByValAlign(FrameAlign);

View File

@ -936,7 +936,9 @@ AttributeList AttributeList::get(LLVMContext &C,
AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const { Attribute::AttrKind Kind) const {
if (hasAttribute(Index, Kind)) return *this; if (hasAttribute(Index, Kind)) return *this;
return addAttributes(C, Index, AttributeList::get(C, Index, Kind)); AttrBuilder B;
B.addAttribute(Kind);
return addAttributes(C, Index, B);
} }
AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index, AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
@ -944,7 +946,7 @@ AttributeList AttributeList::addAttribute(LLVMContext &C, unsigned Index,
StringRef Value) const { StringRef Value) const {
AttrBuilder B; AttrBuilder B;
B.addAttribute(Kind, Value); B.addAttribute(Kind, Value);
return addAttributes(C, Index, AttributeList::get(C, Index, B)); return addAttributes(C, Index, B);
} }
AttributeList AttributeList::addAttribute(LLVMContext &C, AttributeList AttributeList::addAttribute(LLVMContext &C,
@ -977,14 +979,6 @@ AttributeList AttributeList::addAttribute(LLVMContext &C,
return get(C, AttrVec); return get(C, AttrVec);
} }
AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
AttributeList Attrs) const {
if (!pImpl) return Attrs;
if (!Attrs.pImpl) return *this;
return addAttributes(C, Index, Attrs.getAttributes(Index));
}
AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index, AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
const AttrBuilder &B) const { const AttrBuilder &B) const {
if (!B.hasAttributes()) if (!B.hasAttributes())
@ -1034,18 +1028,17 @@ AttributeList AttributeList::addAttributes(LLVMContext &C, unsigned Index,
AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
Attribute::AttrKind Kind) const { Attribute::AttrKind Kind) const {
if (!hasAttribute(Index, Kind)) return *this; if (!hasAttribute(Index, Kind)) return *this;
return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); AttrBuilder B;
B.addAttribute(Kind);
return removeAttributes(C, Index, B);
} }
AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index, AttributeList AttributeList::removeAttribute(LLVMContext &C, unsigned Index,
StringRef Kind) const { StringRef Kind) const {
if (!hasAttribute(Index, Kind)) return *this; if (!hasAttribute(Index, Kind)) return *this;
return removeAttributes(C, Index, AttributeList::get(C, Index, Kind)); AttrBuilder B;
} B.addAttribute(Kind);
return removeAttributes(C, Index, B);
AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
AttributeList Attrs) const {
return removeAttributes(C, Index, AttrBuilder(Attrs.getAttributes(Index)));
} }
AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index, AttributeList AttributeList::removeAttributes(LLVMContext &C, unsigned Index,
@ -1103,7 +1096,7 @@ AttributeList AttributeList::addDereferenceableAttr(LLVMContext &C,
uint64_t Bytes) const { uint64_t Bytes) const {
AttrBuilder B; AttrBuilder B;
B.addDereferenceableAttr(Bytes); B.addDereferenceableAttr(Bytes);
return addAttributes(C, Index, AttributeList::get(C, Index, B)); return addAttributes(C, Index, B);
} }
AttributeList AttributeList
@ -1111,7 +1104,7 @@ AttributeList::addDereferenceableOrNullAttr(LLVMContext &C, unsigned Index,
uint64_t Bytes) const { uint64_t Bytes) const {
AttrBuilder B; AttrBuilder B;
B.addDereferenceableOrNullAttr(Bytes); B.addDereferenceableOrNullAttr(Bytes);
return addAttributes(C, Index, AttributeList::get(C, Index, B)); return addAttributes(C, Index, B);
} }
AttributeList AttributeList
@ -1120,7 +1113,7 @@ AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,
const Optional<unsigned> &NumElemsArg) { const Optional<unsigned> &NumElemsArg) {
AttrBuilder B; AttrBuilder B;
B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
return addAttributes(C, Index, AttributeList::get(C, Index, B)); return addAttributes(C, Index, B);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -1610,12 +1603,10 @@ static void adjustCallerSSPLevel(Function &Caller, const Function &Callee) {
// If upgrading the SSP attribute, clear out the old SSP Attributes first. // If upgrading the SSP attribute, clear out the old SSP Attributes first.
// Having multiple SSP attributes doesn't actually hurt, but it adds useless // Having multiple SSP attributes doesn't actually hurt, but it adds useless
// clutter to the IR. // clutter to the IR.
AttrBuilder B; AttrBuilder OldSSPAttr;
B.addAttribute(Attribute::StackProtect) OldSSPAttr.addAttribute(Attribute::StackProtect)
.addAttribute(Attribute::StackProtectStrong) .addAttribute(Attribute::StackProtectStrong)
.addAttribute(Attribute::StackProtectReq); .addAttribute(Attribute::StackProtectReq);
AttributeList OldSSPAttr =
AttributeList::get(Caller.getContext(), AttributeList::FunctionIndex, B);
if (Callee.hasFnAttribute(Attribute::StackProtectReq)) { if (Callee.hasFnAttribute(Attribute::StackProtectReq)) {
Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr); Caller.removeAttributes(AttributeList::FunctionIndex, OldSSPAttr);

View File

@ -328,7 +328,7 @@ void Function::addAttribute(unsigned i, Attribute Attr) {
setAttributes(PAL); setAttributes(PAL);
} }
void Function::addAttributes(unsigned i, AttributeList Attrs) { void Function::addAttributes(unsigned i, const AttrBuilder &Attrs) {
AttributeList PAL = getAttributes(); AttributeList PAL = getAttributes();
PAL = PAL.addAttributes(getContext(), i, Attrs); PAL = PAL.addAttributes(getContext(), i, Attrs);
setAttributes(PAL); setAttributes(PAL);
@ -346,7 +346,7 @@ void Function::removeAttribute(unsigned i, StringRef Kind) {
setAttributes(PAL); setAttributes(PAL);
} }
void Function::removeAttributes(unsigned i, AttributeList Attrs) { void Function::removeAttributes(unsigned i, const AttrBuilder &Attrs) {
AttributeList PAL = getAttributes(); AttributeList PAL = getAttributes();
PAL = PAL.removeAttributes(getContext(), i, Attrs); PAL = PAL.removeAttributes(getContext(), i, Attrs);
setAttributes(PAL); setAttributes(PAL);

View File

@ -490,15 +490,14 @@ static void createFPFnStub(Function *F, Module *M, FPParamVariant PV,
// remove the use-soft-float attribute // remove the use-soft-float attribute
// //
static void removeUseSoftFloat(Function &F) { static void removeUseSoftFloat(Function &F) {
AttributeList A; AttrBuilder B;
DEBUG(errs() << "removing -use-soft-float\n"); DEBUG(errs() << "removing -use-soft-float\n");
A = A.addAttribute(F.getContext(), AttributeList::FunctionIndex, B.addAttribute("use-soft-float", "false");
"use-soft-float", "false"); F.removeAttributes(AttributeList::FunctionIndex, B);
F.removeAttributes(AttributeList::FunctionIndex, A);
if (F.hasFnAttribute("use-soft-float")) { if (F.hasFnAttribute("use-soft-float")) {
DEBUG(errs() << "still has -use-soft-float\n"); DEBUG(errs() << "still has -use-soft-float\n");
} }
F.addAttributes(AttributeList::FunctionIndex, A); F.addAttributes(AttributeList::FunctionIndex, B);
} }

View File

@ -245,9 +245,7 @@ static Function *createClone(Function &F, Twine Suffix, coro::Shape &Shape,
// Remove old return attributes. // Remove old return attributes.
NewF->removeAttributes( NewF->removeAttributes(
AttributeList::ReturnIndex, AttributeList::ReturnIndex,
AttributeList::get( AttributeFuncs::typeIncompatible(NewF->getReturnType()));
NewF->getContext(), AttributeList::ReturnIndex,
AttributeFuncs::typeIncompatible(NewF->getReturnType())));
// Make AllocaSpillBlock the new entry block. // Make AllocaSpillBlock the new entry block.
auto *SwitchBB = cast<BasicBlock>(VMap[ResumeEntry]); auto *SwitchBB = cast<BasicBlock>(VMap[ResumeEntry]);

View File

@ -254,7 +254,7 @@ class DataFlowSanitizer : public ModulePass {
MDNode *ColdCallWeights; MDNode *ColdCallWeights;
DFSanABIList ABIList; DFSanABIList ABIList;
DenseMap<Value *, Function *> UnwrappedFnMap; DenseMap<Value *, Function *> UnwrappedFnMap;
AttributeList ReadOnlyNoneAttrs; AttrBuilder ReadOnlyNoneAttrs;
bool DFSanRuntimeShadowMask; bool DFSanRuntimeShadowMask;
Value *getShadowAddress(Value *Addr, Instruction *Pos); Value *getShadowAddress(Value *Addr, Instruction *Pos);
@ -544,16 +544,12 @@ DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
NewF->copyAttributesFrom(F); NewF->copyAttributesFrom(F);
NewF->removeAttributes( NewF->removeAttributes(
AttributeList::ReturnIndex, AttributeList::ReturnIndex,
AttributeList::get( AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
F->getContext(), AttributeList::ReturnIndex,
AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF); BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
if (F->isVarArg()) { if (F->isVarArg()) {
NewF->removeAttributes( NewF->removeAttributes(AttributeList::FunctionIndex,
AttributeList::FunctionIndex, AttrBuilder().addAttribute("split-stack"));
AttributeList().addAttribute(*Ctx, AttributeList::FunctionIndex,
"split-stack"));
CallInst::Create(DFSanVarargWrapperFn, CallInst::Create(DFSanVarargWrapperFn,
IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "", IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
BB); BB);
@ -698,9 +694,8 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
} }
} }
AttrBuilder B; ReadOnlyNoneAttrs.addAttribute(Attribute::ReadOnly)
B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); .addAttribute(Attribute::ReadNone);
ReadOnlyNoneAttrs = AttributeList::get(*Ctx, AttributeList::FunctionIndex, B);
// First, change the ABI of every function in the module. ABI-listed // First, change the ABI of every function in the module. ABI-listed
// functions keep their original ABI and get a wrapper function. // functions keep their original ABI and get a wrapper function.
@ -722,9 +717,7 @@ bool DataFlowSanitizer::runOnModule(Module &M) {
NewF->copyAttributesFrom(&F); NewF->copyAttributesFrom(&F);
NewF->removeAttributes( NewF->removeAttributes(
AttributeList::ReturnIndex, AttributeList::ReturnIndex,
AttributeList::get( AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
NewF->getContext(), AttributeList::ReturnIndex,
AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
for (Function::arg_iterator FArg = F.arg_begin(), for (Function::arg_iterator FArg = F.arg_begin(),
NewFArg = NewF->arg_begin(), NewFArg = NewF->arg_begin(),
FArgEnd = F.arg_end(); FArgEnd = F.arg_end();

View File

@ -2607,10 +2607,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
AttrBuilder B; AttrBuilder B;
B.addAttribute(Attribute::ReadOnly) B.addAttribute(Attribute::ReadOnly)
.addAttribute(Attribute::ReadNone); .addAttribute(Attribute::ReadNone);
Func->removeAttributes(AttributeList::FunctionIndex, Func->removeAttributes(AttributeList::FunctionIndex, B);
AttributeList::get(Func->getContext(),
AttributeList::FunctionIndex,
B));
} }
maybeMarkSanitizerLibraryCallNoBuiltin(Call, TLI); maybeMarkSanitizerLibraryCallNoBuiltin(Call, TLI);
@ -3659,9 +3656,7 @@ bool MemorySanitizer::runOnFunction(Function &F) {
AttrBuilder B; AttrBuilder B;
B.addAttribute(Attribute::ReadOnly) B.addAttribute(Attribute::ReadOnly)
.addAttribute(Attribute::ReadNone); .addAttribute(Attribute::ReadNone);
F.removeAttributes( F.removeAttributes(AttributeList::FunctionIndex, B);
AttributeList::FunctionIndex,
AttributeList::get(F.getContext(), AttributeList::FunctionIndex, B));
return Visitor.runOnFunction(); return Visitor.runOnFunction();
} }

View File

@ -2290,8 +2290,7 @@ static void RemoveNonValidAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH,
R.addAttribute(Attribute::NoAlias); R.addAttribute(Attribute::NoAlias);
if (!R.empty()) if (!R.empty())
AH.setAttributes(AH.getAttributes().removeAttributes( AH.setAttributes(AH.getAttributes().removeAttributes(Ctx, Index, R));
Ctx, Index, AttributeList::get(Ctx, Index, R)));
} }
void void

View File

@ -45,7 +45,7 @@ TEST(Attributes, Ordering) {
AttributeList::get(C, 1, Attribute::SExt)}; AttributeList::get(C, 1, Attribute::SExt)};
AttributeList SetA = AttributeList::get(C, ASs); AttributeList SetA = AttributeList::get(C, ASs);
AttributeList SetB = SetA.removeAttributes(C, 1, ASs[1]); AttributeList SetB = SetA.removeAttributes(C, 1, ASs[1].getAttributes(1));
EXPECT_NE(SetA, SetB); EXPECT_NE(SetA, SetB);
} }