Prefer addAttr(Attribute::AttrKind) over the AttributeList overload

This should simplify the call sites, which typically want to tweak one
attribute at a time. It should also avoid creating ephemeral
AttributeLists that live forever.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300718 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Kleckner 2017-04-19 17:28:52 +00:00
parent 8ccdc59458
commit d6b4b10a39
6 changed files with 45 additions and 69 deletions

View File

@ -108,18 +108,16 @@ public:
bool hasSExtAttr() const;
/// Add attributes to an argument.
void addAttr(AttributeList AS);
void addAttrs(AttrBuilder &B);
void addAttr(Attribute::AttrKind Kind) {
addAttr(AttributeList::get(getContext(), getArgNo() + 1, Kind));
}
void addAttr(Attribute::AttrKind Kind);
void addAttr(Attribute Attr);
/// Remove attributes from an argument.
void removeAttr(AttributeList AS);
void removeAttr(Attribute::AttrKind Kind) {
removeAttr(AttributeList::get(getContext(), getArgNo() + 1, Kind));
}
void removeAttr(Attribute::AttrKind Kind);
/// Check if an argument has a given attribute.
bool hasAttribute(Attribute::AttrKind Kind) const;

View File

@ -143,27 +143,24 @@ bool LLParser::ValidateEndOfModule() {
FnAttrs.removeAttribute(Attribute::Alignment);
}
AS = AS.addAttributes(
Context, AttributeList::FunctionIndex,
AttributeList::get(Context, AttributeList::FunctionIndex, FnAttrs));
AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
AttributeSet::get(Context, FnAttrs));
Fn->setAttributes(AS);
} else if (CallInst *CI = dyn_cast<CallInst>(V)) {
AttributeList AS = CI->getAttributes();
AttrBuilder FnAttrs(AS.getFnAttributes());
AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
FnAttrs.merge(B);
AS = AS.addAttributes(
Context, AttributeList::FunctionIndex,
AttributeList::get(Context, AttributeList::FunctionIndex, FnAttrs));
AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
AttributeSet::get(Context, FnAttrs));
CI->setAttributes(AS);
} else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
AttributeList AS = II->getAttributes();
AttrBuilder FnAttrs(AS.getFnAttributes());
AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
FnAttrs.merge(B);
AS = AS.addAttributes(
Context, AttributeList::FunctionIndex,
AttributeList::get(Context, AttributeList::FunctionIndex, FnAttrs));
AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
AttributeSet::get(Context, FnAttrs));
II->setAttributes(AS);
} else {
llvm_unreachable("invalid object with forward attribute group reference");

View File

@ -1896,13 +1896,8 @@ void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx,
void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
const char *V) {
Function *Func = unwrap<Function>(Fn);
AttributeList::AttrIndex Idx =
AttributeList::AttrIndex(AttributeList::FunctionIndex);
AttrBuilder B;
B.addAttribute(A, V);
AttributeList Set = AttributeList::get(Func->getContext(), Idx, B);
Func->addAttributes(Idx, Set);
Attribute Attr = Attribute::get(Func->getContext(), A, V);
Func->addAttribute(AttributeList::FunctionIndex, Attr);
}
/*--.. Operations on parameters ............................................--*/
@ -1962,9 +1957,7 @@ LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
Argument *A = unwrap<Argument>(Arg);
AttrBuilder B;
B.addAlignmentAttr(align);
A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::getWithAlignment(A->getContext(), align));
}
/*--.. Operations on basic blocks ..........................................--*/
@ -2171,11 +2164,8 @@ void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
unsigned align) {
CallSite Call = CallSite(unwrap<Instruction>(Instr));
AttrBuilder B;
B.addAlignmentAttr(align);
Call.setAttributes(Call.getAttributes().addAttributes(
Call->getContext(), index,
AttributeList::get(Call->getContext(), index, B)));
Attribute AlignAttr = Attribute::getWithAlignment(Call->getContext(), align);
Call.addAttribute(index, AlignAttr);
}
void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx,

View File

@ -138,13 +138,18 @@ bool Argument::onlyReadsMemory() const {
Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
}
void Argument::addAttr(AttributeList AS) {
assert(AS.getNumSlots() <= 1 &&
"Trying to add more than one attribute set to an argument!");
AttrBuilder B(AS, AS.getSlotIndex(0));
getParent()->addAttributes(
getArgNo() + 1,
AttributeList::get(Parent->getContext(), getArgNo() + 1, B));
void Argument::addAttrs(AttrBuilder &B) {
AttributeList AL = getParent()->getAttributes();
AL = AL.addAttributes(Parent->getContext(), getArgNo() + 1, B);
getParent()->setAttributes(AL);
}
void Argument::addAttr(Attribute::AttrKind Kind) {
getParent()->addAttribute(getArgNo() + 1, Kind);
}
void Argument::addAttr(Attribute Attr) {
getParent()->addAttribute(getArgNo() + 1, Attr);
}
void Argument::removeAttr(AttributeList AS) {
@ -156,6 +161,10 @@ void Argument::removeAttr(AttributeList AS) {
AttributeList::get(Parent->getContext(), getArgNo() + 1, B));
}
void Argument::removeAttr(Attribute::AttrKind Kind) {
getParent()->removeAttribute(getArgNo() + 1, Kind);
}
bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
return getParent()->hasParamAttribute(getArgNo(), Kind);
}

View File

@ -222,15 +222,11 @@ static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
MadeChange = true;
// Clear out any existing attributes.
AttrBuilder B;
B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
F->removeAttributes(
AttributeList::FunctionIndex,
AttributeList::get(F->getContext(), AttributeList::FunctionIndex, B));
F->removeFnAttr(Attribute::ReadOnly);
F->removeFnAttr(Attribute::ReadNone);
// Add in the new attribute.
F->addAttribute(AttributeList::FunctionIndex,
ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
if (ReadsMemory)
++NumReadOnly;
@ -495,9 +491,6 @@ determinePointerReadAttrs(Argument *A,
static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
bool Changed = false;
AttrBuilder B;
B.addAttribute(Attribute::Returned);
// Check each function in turn, determining if an argument is always returned.
for (Function *F : SCCNodes) {
// We can infer and propagate function attributes only when we know that the
@ -535,7 +528,7 @@ static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
if (Value *RetArg = FindRetArg()) {
auto *A = cast<Argument>(RetArg);
A->addAttr(AttributeList::get(F->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::Returned);
++NumReturned;
Changed = true;
}
@ -593,9 +586,6 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
ArgumentGraph AG;
AttrBuilder B;
B.addAttribute(Attribute::NoCapture);
// Check each function in turn, determining which pointer arguments are not
// captured.
for (Function *F : SCCNodes) {
@ -614,7 +604,7 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
++A) {
if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
A->addAttr(AttributeList::get(F->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::NoCapture);
++NumNoCapture;
Changed = true;
}
@ -633,8 +623,7 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
if (!Tracker.Captured) {
if (Tracker.Uses.empty()) {
// If it's trivially not captured, mark it nocapture now.
A->addAttr(
AttributeList::get(F->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::NoCapture);
++NumNoCapture;
Changed = true;
} else {
@ -660,9 +649,7 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Self.insert(&*A);
Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
if (R != Attribute::None) {
AttrBuilder B;
B.addAttribute(R);
A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
A->addAttr(R);
Changed = true;
R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
}
@ -687,7 +674,7 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
if (ArgumentSCC[0]->Uses.size() == 1 &&
ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Argument *A = ArgumentSCC[0]->Definition;
A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::NoCapture);
++NumNoCapture;
Changed = true;
}
@ -729,7 +716,7 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Argument *A = ArgumentSCC[i]->Definition;
A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
A->addAttr(Attribute::NoCapture);
++NumNoCapture;
Changed = true;
}
@ -760,15 +747,12 @@ static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
}
if (ReadAttr != Attribute::None) {
AttrBuilder B, R;
B.addAttribute(ReadAttr);
R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Argument *A = ArgumentSCC[i]->Definition;
// Clear out existing readonly/readnone attributes
A->removeAttr(
AttributeList::get(A->getContext(), A->getArgNo() + 1, R));
A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B));
A->removeAttr(Attribute::ReadOnly);
A->removeAttr(Attribute::ReadNone);
A->addAttr(ReadAttr);
ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
Changed = true;
}

View File

@ -162,10 +162,8 @@ TEST_F(CloneInstruction, Attributes) {
Function *F2 = Function::Create(FT1, Function::ExternalLinkage);
Attribute::AttrKind AK[] = { Attribute::NoCapture };
AttributeList AS = AttributeList::get(context, 0, AK);
Argument *A = &*F1->arg_begin();
A->addAttr(AS);
A->addAttr(Attribute::NoCapture);
SmallVector<ReturnInst*, 4> Returns;
ValueToValueMapTy VMap;