Skip to content

Commit

Permalink
[IR] Make getParamAttributes take argument numbers, not ArgNo+1
Browse files Browse the repository at this point in the history
Add hasParamAttribute() and use it instead of hasAttribute(ArgNo+1,
Kind) everywhere.

The fact that the AttributeList index for an argument is ArgNo+1 should
be a hidden implementation detail.

NFC

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300272 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rnk committed Apr 13, 2017
1 parent c67ef6a commit 1c35def
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 76 deletions.
8 changes: 6 additions & 2 deletions include/llvm/IR/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ class AttributeList {
/// \brief The attributes for the specified index are returned.
AttributeSet getAttributes(unsigned Index) const;

/// \brief The attributes for the specified index are returned.
AttributeSet getParamAttributes(unsigned Index) const;
/// \brief The attributes for the argument or parameter at the given index are
/// returned.
AttributeSet getParamAttributes(unsigned ArgNo) const;

/// \brief The attributes for the ret value are returned.
AttributeSet getRetAttributes() const;
Expand All @@ -444,6 +445,9 @@ class AttributeList {
/// may be faster.
bool hasFnAttribute(StringRef Kind) const;

/// \brief Equivalent to hasAttribute(ArgNo + 1, Kind).
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;

/// \brief Return true if the specified attribute is set for at least one
/// parameter or for the return value. If Index is not nullptr, the index
/// of a parameter with the specified attribute is provided.
Expand Down
5 changes: 5 additions & 0 deletions include/llvm/IR/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ class Function : public GlobalObject, public ilist_node<Function> {
return getAttributes().hasAttribute(i, Kind);
}

/// @brief check if an attributes is in the list of attributes.
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const {
return getAttributes().hasParamAttribute(ArgNo, Kind);
}

Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
return AttributeSets.getAttribute(i, Kind);
}
Expand Down
9 changes: 7 additions & 2 deletions lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ AttributeList::addAllocSizeAttr(LLVMContext &C, unsigned Index,

LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }

AttributeSet AttributeList::getParamAttributes(unsigned Index) const {
return getAttributes(Index);
AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
return getAttributes(ArgNo + 1);
}

AttributeSet AttributeList::getRetAttributes() const {
Expand Down Expand Up @@ -1203,6 +1203,11 @@ bool AttributeList::hasFnAttribute(StringRef Kind) const {
return hasAttribute(AttributeList::FunctionIndex, Kind);
}

bool AttributeList::hasParamAttribute(unsigned ArgNo,
Attribute::AttrKind Kind) const {
return hasAttribute(ArgNo + 1, Kind);
}

bool AttributeList::hasAttrSomewhere(Attribute::AttrKind Attr,
unsigned *Index) const {
if (!pImpl) return false;
Expand Down
22 changes: 9 additions & 13 deletions lib/IR/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ void Argument::setParent(Function *parent) {

bool Argument::hasNonNullAttr() const {
if (!getType()->isPointerTy()) return false;
if (getParent()->getAttributes().
hasAttribute(getArgNo()+1, Attribute::NonNull))
if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
return true;
else if (getDereferenceableBytes() > 0 &&
getType()->getPointerAddressSpace() == 0)
Expand All @@ -64,13 +63,11 @@ bool Argument::hasByValAttr() const {
}

bool Argument::hasSwiftSelfAttr() const {
return getParent()->getAttributes().
hasAttribute(getArgNo()+1, Attribute::SwiftSelf);
return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftSelf);
}

bool Argument::hasSwiftErrorAttr() const {
return getParent()->getAttributes().
hasAttribute(getArgNo()+1, Attribute::SwiftError);
return getParent()->hasParamAttribute(getArgNo(), Attribute::SwiftError);
}

bool Argument::hasInAllocaAttr() const {
Expand All @@ -81,8 +78,8 @@ bool Argument::hasInAllocaAttr() const {
bool Argument::hasByValOrInAllocaAttr() const {
if (!getType()->isPointerTy()) return false;
AttributeList Attrs = getParent()->getAttributes();
return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) ||
Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca);
return Attrs.hasParamAttribute(getArgNo(), Attribute::ByVal) ||
Attrs.hasParamAttribute(getArgNo(), Attribute::InAlloca);
}

unsigned Argument::getParamAlignment() const {
Expand Down Expand Up @@ -136,10 +133,9 @@ bool Argument::hasSExtAttr() const {
}

bool Argument::onlyReadsMemory() const {
return getParent()->getAttributes().
hasAttribute(getArgNo()+1, Attribute::ReadOnly) ||
getParent()->getAttributes().
hasAttribute(getArgNo()+1, Attribute::ReadNone);
AttributeList Attrs = getParent()->getAttributes();
return Attrs.hasParamAttribute(getArgNo(), Attribute::ReadOnly) ||
Attrs.hasParamAttribute(getArgNo(), Attribute::ReadNone);
}

void Argument::addAttr(AttributeList AS) {
Expand All @@ -161,7 +157,7 @@ void Argument::removeAttr(AttributeList AS) {
}

bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
return getParent()->hasAttribute(getArgNo() + 1, Kind);
return getParent()->hasParamAttribute(getArgNo(), Kind);
}

//===----------------------------------------------------------------------===//
Expand Down
6 changes: 3 additions & 3 deletions lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@ void Verifier::visitFunction(const Function &F) {
}

// Check that swifterror argument is only used by loads and stores.
if (Attrs.hasAttribute(i+1, Attribute::SwiftError)) {
if (Attrs.hasParamAttribute(i, Attribute::SwiftError)) {
verifySwiftErrorValue(&Arg);
}
++i;
Expand Down Expand Up @@ -2763,10 +2763,10 @@ static AttrBuilder getParameterABIAttributes(int I, AttributeList Attrs) {
Attribute::SwiftError};
AttrBuilder Copy;
for (auto AK : ABIAttrs) {
if (Attrs.hasAttribute(I + 1, AK))
if (Attrs.hasParamAttribute(I, AK))
Copy.addAttribute(AK);
}
if (Attrs.hasAttribute(I + 1, Attribute::Alignment))
if (Attrs.hasParamAttribute(I, Attribute::Alignment))
Copy.addAlignmentAttr(Attrs.getParamAlignment(I + 1));
return Copy;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
not dereference that pointer argument, even though it may read or write
the memory that the pointer points to if accessed through other pointers.
*/
if (F->getAttributes().hasAttribute(ArgNo + 1, Attribute::NoAlias) &&
(F->getAttributes().hasAttribute(ArgNo + 1, Attribute::ReadNone) ||
F->getAttributes().hasAttribute(ArgNo + 1, Attribute::ReadOnly))) {
if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
(F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ static bool isArgPassedInSGPR(const Argument *A) {
return true;

// For non-compute shaders, SGPR inputs are marked with either inreg or byval.
if (F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::InReg) ||
F->getAttributes().hasAttribute(A->getArgNo() + 1, Attribute::ByVal))
if (F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal))
return true;

// Everything else is in VGPRs.
Expand Down
2 changes: 1 addition & 1 deletion lib/Target/NVPTX/NVPTXAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
}
}

if (!PAL.hasAttribute(paramIndex + 1, Attribute::ByVal)) {
if (!PAL.hasParamAttribute(paramIndex, Attribute::ByVal)) {
if (Ty->isAggregateType() || Ty->isVectorTy()) {
// Just print .param .align <a> .b8 .param[size];
// <a> = PAL.getparamalignment
Expand Down
2 changes: 1 addition & 1 deletion lib/Target/NVPTX/NVPTXISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ SDValue NVPTXTargetLowering::LowerFormalArguments(
// to newly created nodes. The SDNodes for params have to
// appear in the same order as their order of appearance
// in the original function. "idx+1" holds that order.
if (!PAL.hasAttribute(i + 1, Attribute::ByVal)) {
if (!PAL.hasParamAttribute(i, Attribute::ByVal)) {
bool aggregateIsPacked = false;
if (StructType *STy = dyn_cast<StructType>(Ty))
aggregateIsPacked = STy->isPacked();
Expand Down
24 changes: 12 additions & 12 deletions lib/Target/WebAssembly/WebAssemblyFastISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,11 @@ bool WebAssemblyFastISel::fastLowerArguments() {
unsigned i = 0;
for (auto const &Arg : F->args()) {
const AttributeList &Attrs = F->getAttributes();
if (Attrs.hasAttribute(i+1, Attribute::ByVal) ||
Attrs.hasAttribute(i+1, Attribute::SwiftSelf) ||
Attrs.hasAttribute(i+1, Attribute::SwiftError) ||
Attrs.hasAttribute(i+1, Attribute::InAlloca) ||
Attrs.hasAttribute(i+1, Attribute::Nest))
if (Attrs.hasParamAttribute(i, Attribute::ByVal) ||
Attrs.hasParamAttribute(i, Attribute::SwiftSelf) ||
Attrs.hasParamAttribute(i, Attribute::SwiftError) ||
Attrs.hasParamAttribute(i, Attribute::InAlloca) ||
Attrs.hasParamAttribute(i, Attribute::Nest))
return false;

Type *ArgTy = Arg.getType();
Expand Down Expand Up @@ -747,18 +747,18 @@ bool WebAssemblyFastISel::selectCall(const Instruction *I) {
return false;

const AttributeList &Attrs = Call->getAttributes();
if (Attrs.hasAttribute(i+1, Attribute::ByVal) ||
Attrs.hasAttribute(i+1, Attribute::SwiftSelf) ||
Attrs.hasAttribute(i+1, Attribute::SwiftError) ||
Attrs.hasAttribute(i+1, Attribute::InAlloca) ||
Attrs.hasAttribute(i+1, Attribute::Nest))
if (Attrs.hasParamAttribute(i, Attribute::ByVal) ||
Attrs.hasParamAttribute(i, Attribute::SwiftSelf) ||
Attrs.hasParamAttribute(i, Attribute::SwiftError) ||
Attrs.hasParamAttribute(i, Attribute::InAlloca) ||
Attrs.hasParamAttribute(i, Attribute::Nest))
return false;

unsigned Reg;

if (Attrs.hasAttribute(i+1, Attribute::SExt))
if (Attrs.hasParamAttribute(i, Attribute::SExt))
Reg = getRegForSignedValue(V);
else if (Attrs.hasAttribute(i+1, Attribute::ZExt))
else if (Attrs.hasParamAttribute(i, Attribute::ZExt))
Reg = getRegForUnsignedValue(V);
else
Reg = getRegForValue(V);
Expand Down
3 changes: 1 addition & 2 deletions lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,8 @@ Value *WebAssemblyLowerEmscriptenEHSjLj::wrapInvoke(CallOrInvoke *CI) {
// No attributes for the callee pointer.
ArgAttributes.push_back(AttributeSet());
// Copy the argument attributes from the original
for (unsigned i = 1, e = CI->getNumArgOperands(); i <= e; ++i) {
for (unsigned i = 0, e = CI->getNumArgOperands(); i < e; ++i)
ArgAttributes.push_back(InvokeAL.getParamAttributes(i));
}

// Reconstruct the AttributesList based on the vector we constructed.
AttributeList NewCallAL =
Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/IPO/ArgumentPromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ doPromotion(Function *F, SmallPtrSetImpl<Argument *> &ArgsToPromote,
AttributeList PAL = F->getAttributes();

// First, determine the new argument list
unsigned ArgIndex = 1;
unsigned ArgIndex = 0;
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
++I, ++ArgIndex) {
if (ByValArgsToTransform.count(&*I)) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Transforms/IPO/DeadArgumentElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
if (LiveValues.erase(Arg)) {
Params.push_back(I->getType());
ArgAlive[i] = true;
ArgAttrVec.push_back(PAL.getParamAttributes(i + 1));
HasLiveReturnedArg |= PAL.hasAttribute(i + 1, Attribute::Returned);
ArgAttrVec.push_back(PAL.getParamAttributes(i));
HasLiveReturnedArg |= PAL.hasParamAttribute(i, Attribute::Returned);
} else {
++NumArgumentsEliminated;
DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument " << i
Expand Down Expand Up @@ -836,7 +836,7 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
if (ArgAlive[i]) {
Args.push_back(*I);
// Get original parameter attributes, but skip return attributes.
AttributeSet Attrs = CallPAL.getParamAttributes(i + 1);
AttributeSet Attrs = CallPAL.getParamAttributes(i);
if (NRetTy != RetTy && Attrs.hasAttribute(Attribute::Returned)) {
// If the return type has changed, then get rid of 'returned' on the
// call site. The alternative is to make all 'returned' attributes on
Expand All @@ -855,7 +855,7 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
// Push any varargs arguments on the list. Don't forget their attributes.
for (CallSite::arg_iterator E = CS.arg_end(); I != E; ++I, ++i) {
Args.push_back(*I);
ArgAttrVec.push_back(CallPAL.getParamAttributes(i + 1));
ArgAttrVec.push_back(CallPAL.getParamAttributes(i));
}

// Reconstruct the AttributesList based on the vector we constructed.
Expand Down
22 changes: 9 additions & 13 deletions lib/Transforms/IPO/MergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,15 @@ void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
// Transferring other attributes may help other optimizations, but that
// should be done uniformly and not in this ad-hoc way.
auto &Context = New->getContext();
auto NewFuncAttrs = New->getAttributes();
auto CallSiteAttrs = CS.getAttributes();

CallSiteAttrs = CallSiteAttrs.addAttributes(
Context, AttributeList::ReturnIndex, NewFuncAttrs.getRetAttributes());

for (unsigned argIdx = 0; argIdx < CS.arg_size(); argIdx++) {
AttributeSet Attrs = NewFuncAttrs.getParamAttributes(argIdx);
if (Attrs.hasAttributes())
CallSiteAttrs = CallSiteAttrs.addAttributes(Context, argIdx, Attrs);
}

CS.setAttributes(CallSiteAttrs);
auto NewPAL = New->getAttributes();
SmallVector<AttributeSet, 4> NewArgAttrs;
for (unsigned argIdx = 0; argIdx < CS.arg_size(); argIdx++)
NewArgAttrs.push_back(NewPAL.getParamAttributes(argIdx));
// Don't transfer attributes from the function to the callee. Function
// attributes typically aren't relevant to the calling convention or ABI.
CS.setAttributes(AttributeList::get(Context, /*FnAttrs=*/AttributeSet(),
NewPAL.getRetAttributes(),
NewArgAttrs));

remove(CS.getInstruction()->getParent()->getParent());
U->set(BitcastNew);
Expand Down
Loading

0 comments on commit 1c35def

Please sign in to comment.