Skip to content

Commit

Permalink
[RS4GC] Clean up attribute removal (NFC)
Browse files Browse the repository at this point in the history
It is not necessary to explicitly check which attributes are
present, and only add those to the builder. We can simply list
all attributes that need to be stripped and remove them
unconditionally. This also allows us to use some nicer APIs that
don't require mucking about with attribute list indices.
  • Loading branch information
nikic committed Dec 22, 2021
1 parent e8e8bfe commit 9f24f01
Showing 1 changed file with 18 additions and 33 deletions.
51 changes: 18 additions & 33 deletions llvm/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp
Expand Up @@ -1359,16 +1359,6 @@ static constexpr Attribute::AttrKind FnAttrsToStrip[] =
Attribute::InaccessibleMemOrArgMemOnly,
Attribute::NoSync, Attribute::NoFree};

// List of all parameter and return attributes which must be stripped when
// lowering from the abstract machine model. Note that we list attributes
// here which aren't valid as return attributes, that is okay. There are
// also some additional attributes with arguments which are handled
// explicitly and are not in this list.
static constexpr Attribute::AttrKind ParamAttrsToStrip[] =
{Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly,
Attribute::NoAlias, Attribute::NoFree};


// Create new attribute set containing only attributes which can be transferred
// from original call to the safepoint.
static AttributeList legalizeCallAttributes(LLVMContext &Ctx,
Expand Down Expand Up @@ -2650,24 +2640,19 @@ static bool insertParsePoints(Function &F, DominatorTree &DT,
return !Records.empty();
}

// Handles both return values and arguments for Functions and calls.
template <typename AttrHolder>
static void RemoveNonValidAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH,
unsigned Index) {
// List of all parameter and return attributes which must be stripped when
// lowering from the abstract machine model. Note that we list attributes
// here which aren't valid as return attributes, that is okay.
static AttrBuilder getParamAndReturnAttributesToRemove() {
AttrBuilder R;
AttributeSet AS = AH.getAttributes().getAttributes(Index);
if (AS.getDereferenceableBytes())
R.addAttribute(Attribute::get(Ctx, Attribute::Dereferenceable,
AS.getDereferenceableBytes()));
if (AS.getDereferenceableOrNullBytes())
R.addAttribute(Attribute::get(Ctx, Attribute::DereferenceableOrNull,
AS.getDereferenceableOrNullBytes()));
for (auto Attr : ParamAttrsToStrip)
if (AS.hasAttribute(Attr))
R.addAttribute(Attr);

if (!R.empty())
AH.setAttributes(AH.getAttributes().removeAttributesAtIndex(Ctx, Index, R));
R.addDereferenceableAttr(1);
R.addDereferenceableOrNullAttr(1);
R.addAttribute(Attribute::ReadNone);
R.addAttribute(Attribute::ReadOnly);
R.addAttribute(Attribute::WriteOnly);
R.addAttribute(Attribute::NoAlias);
R.addAttribute(Attribute::NoFree);
return R;
}

static void stripNonValidAttributesFromPrototype(Function &F) {
Expand All @@ -2683,13 +2668,13 @@ static void stripNonValidAttributesFromPrototype(Function &F) {
return;
}

AttrBuilder R = getParamAndReturnAttributesToRemove();
for (Argument &A : F.args())
if (isa<PointerType>(A.getType()))
RemoveNonValidAttrAtIndex(Ctx, F,
A.getArgNo() + AttributeList::FirstArgIndex);
F.removeParamAttrs(A.getArgNo(), R);

if (isa<PointerType>(F.getReturnType()))
RemoveNonValidAttrAtIndex(Ctx, F, AttributeList::ReturnIndex);
F.removeRetAttrs(R);

for (auto Attr : FnAttrsToStrip)
F.removeFnAttr(Attr);
Expand Down Expand Up @@ -2757,13 +2742,13 @@ static void stripNonValidDataFromBody(Function &F) {

stripInvalidMetadataFromInstruction(I);

AttrBuilder R = getParamAndReturnAttributesToRemove();
if (auto *Call = dyn_cast<CallBase>(&I)) {
for (int i = 0, e = Call->arg_size(); i != e; i++)
if (isa<PointerType>(Call->getArgOperand(i)->getType()))
RemoveNonValidAttrAtIndex(Ctx, *Call,
i + AttributeList::FirstArgIndex);
Call->removeParamAttrs(i, R);
if (isa<PointerType>(Call->getType()))
RemoveNonValidAttrAtIndex(Ctx, *Call, AttributeList::ReturnIndex);
Call->removeRetAttrs(R);
}
}

Expand Down

0 comments on commit 9f24f01

Please sign in to comment.