Skip to content

Commit

Permalink
[CallSite removal][MemCpyOptimizer] Replace CallSite with CallBase. NFC
Browse files Browse the repository at this point in the history
There are also some adjustments to use MaybeAlign in here due
to CallBase::getParamAlignment() being deprecated. It would
be cleaner if getOrEnforceKnownAlignment was migrated
to Align/MaybeAlign.

Differential Revision: https://reviews.llvm.org/D78345
  • Loading branch information
topperc committed Apr 17, 2020
1 parent f715eda commit b91f78d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 176 deletions.
27 changes: 5 additions & 22 deletions llvm/include/llvm/Transforms/IPO/Attributor.h
Expand Up @@ -217,23 +217,6 @@ struct IRPosition {
return IRPosition(const_cast<CallBase &>(CB), Kind(ArgNo));
}

/// Create a position describing the function scope of \p ICS.
static const IRPosition callsite_function(ImmutableCallSite ICS) {
return IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction()));
}

/// Create a position describing the returned value of \p ICS.
static const IRPosition callsite_returned(ImmutableCallSite ICS) {
return IRPosition::callsite_returned(cast<CallBase>(*ICS.getInstruction()));
}

/// Create a position describing the argument of \p ICS at position \p ArgNo.
static const IRPosition callsite_argument(ImmutableCallSite ICS,
unsigned ArgNo) {
return IRPosition::callsite_argument(cast<CallBase>(*ICS.getInstruction()),
ArgNo);
}

/// Create a position describing the argument of \p ACS at position \p ArgNo.
static const IRPosition callsite_argument(AbstractCallSite ACS,
unsigned ArgNo) {
Expand Down Expand Up @@ -418,18 +401,18 @@ struct IRPosition {
return;

AttributeList AttrList;
CallSite CS = CallSite(&getAnchorValue());
if (CS)
AttrList = CS.getAttributes();
auto *CB = dyn_cast<CallBase>(&getAnchorValue());
if (CB)
AttrList = CB->getAttributes();
else
AttrList = getAssociatedFunction()->getAttributes();

LLVMContext &Ctx = getAnchorValue().getContext();
for (Attribute::AttrKind AK : AKs)
AttrList = AttrList.removeAttribute(Ctx, getAttrIdx(), AK);

if (CS)
CS.setAttributes(AttrList);
if (CB)
CB->setAttributes(AttrList);
else
getAssociatedFunction()->setAttributes(AttrList);
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/include/llvm/Transforms/Scalar/MemCpyOptimizer.h
Expand Up @@ -16,7 +16,6 @@

#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/PassManager.h"
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -66,7 +65,7 @@ class MemCpyOptPass : public PassInfoMixin<MemCpyOptPass> {
bool processMemCpyMemCpyDependence(MemCpyInst *M, MemCpyInst *MDep);
bool processMemSetMemCpyDependence(MemCpyInst *M, MemSetInst *MDep);
bool performMemCpyToMemSetOptzn(MemCpyInst *M, MemSetInst *MDep);
bool processByValArgument(CallSite CS, unsigned ArgNo);
bool processByValArgument(CallBase &CB, unsigned ArgNo);
Instruction *tryMergingIntoMemset(Instruction *I, Value *StartPtr,
Value *ByteVal);

Expand Down
91 changes: 45 additions & 46 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Expand Up @@ -228,7 +228,7 @@ IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
case IRPosition::IRP_CALL_SITE:
case IRPosition::IRP_CALL_SITE_RETURNED:
case IRPosition::IRP_CALL_SITE_ARGUMENT:
Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes();
break;
}

Expand All @@ -253,7 +253,7 @@ IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
case IRPosition::IRP_CALL_SITE:
case IRPosition::IRP_CALL_SITE_RETURNED:
case IRPosition::IRP_CALL_SITE_ARGUMENT:
CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs);
break;
case IRPosition::IRP_INVALID:
case IRPosition::IRP_FLOAT:
Expand All @@ -269,7 +269,7 @@ const IRPosition IRPosition::TombstoneKey(256);
SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
IRPositions.emplace_back(IRP);

ImmutableCallSite ICS(&IRP.getAnchorValue());
const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
switch (IRP.getPositionKind()) {
case IRPosition::IRP_INVALID:
case IRPosition::IRP_FLOAT:
Expand All @@ -280,41 +280,40 @@ SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
return;
case IRPosition::IRP_CALL_SITE:
assert(ICS && "Expected call site!");
assert(CB && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!ICS.hasOperandBundles())
if (const Function *Callee = ICS.getCalledFunction())
if (!CB->hasOperandBundles())
if (const Function *Callee = CB->getCalledFunction())
IRPositions.emplace_back(IRPosition::function(*Callee));
return;
case IRPosition::IRP_CALL_SITE_RETURNED:
assert(ICS && "Expected call site!");
assert(CB && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!ICS.hasOperandBundles()) {
if (const Function *Callee = ICS.getCalledFunction()) {
if (!CB->hasOperandBundles()) {
if (const Function *Callee = CB->getCalledFunction()) {
IRPositions.emplace_back(IRPosition::returned(*Callee));
IRPositions.emplace_back(IRPosition::function(*Callee));
for (const Argument &Arg : Callee->args())
if (Arg.hasReturnedAttr()) {
IRPositions.emplace_back(
IRPosition::callsite_argument(ICS, Arg.getArgNo()));
IRPosition::callsite_argument(*CB, Arg.getArgNo()));
IRPositions.emplace_back(
IRPosition::value(*ICS.getArgOperand(Arg.getArgNo())));
IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
IRPositions.emplace_back(IRPosition::argument(Arg));
}
}
}
IRPositions.emplace_back(
IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
IRPositions.emplace_back(IRPosition::callsite_function(*CB));
return;
case IRPosition::IRP_CALL_SITE_ARGUMENT: {
int ArgNo = IRP.getArgNo();
assert(ICS && ArgNo >= 0 && "Expected call site!");
assert(CB && ArgNo >= 0 && "Expected call site!");
// TODO: We need to look at the operand bundles similar to the redirection
// in CallBase.
if (!ICS.hasOperandBundles()) {
const Function *Callee = ICS.getCalledFunction();
if (!CB->hasOperandBundles()) {
const Function *Callee = CB->getCalledFunction();
if (Callee && Callee->arg_size() > unsigned(ArgNo))
IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
if (Callee)
Expand Down Expand Up @@ -369,8 +368,8 @@ bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
return false;

AttributeList AttrList;
if (ImmutableCallSite ICS = ImmutableCallSite(&getAnchorValue()))
AttrList = ICS.getAttributes();
if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
AttrList = CB->getAttributes();
else
AttrList = getAssociatedFunction()->getAttributes();

Expand Down Expand Up @@ -510,12 +509,12 @@ bool Attributor::isAssumedDead(const Use &U,
return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
CheckBBLivenessOnly, DepClass);

if (CallSite CS = CallSite(UserI)) {
if (auto *CB = dyn_cast<CallBase>(UserI)) {
// For call site argument uses we can check if the argument is
// unused/dead.
if (CS.isArgOperand(&U)) {
if (CB->isArgOperand(&U)) {
const IRPosition &CSArgPos =
IRPosition::callsite_argument(CS, CS.getArgumentNo(&U));
IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
CheckBBLivenessOnly, DepClass);
}
Expand Down Expand Up @@ -1617,8 +1616,8 @@ void InformationCache::initializeInformationCache(const Function &CF,
// Note: There are no concrete attributes now so this is initially empty.
switch (I.getOpcode()) {
default:
assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
"New call site/base instruction type needs to be known in the "
assert(!isa<CallBase>(&I) &&
"New call base instruction type needs to be known in the "
"Attributor.");
break;
case Instruction::Call:
Expand Down Expand Up @@ -1687,8 +1686,8 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) {
InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
if (!isModulePass() && !FI.CalledViaMustTail) {
for (const Use &U : F.uses())
if (ImmutableCallSite ICS = ImmutableCallSite(U.getUser()))
if (ICS.isCallee(&U) && ICS.isMustTailCall())
if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
if (CB->isCallee(&U) && CB->isMustTailCall())
FI.CalledViaMustTail = true;
}

Expand Down Expand Up @@ -1800,14 +1799,14 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) {
}

auto CallSitePred = [&](Instruction &I) -> bool {
CallSite CS(&I);
IRPosition CSRetPos = IRPosition::callsite_returned(CS);
auto *CB = dyn_cast<CallBase>(&I);
IRPosition CBRetPos = IRPosition::callsite_returned(*CB);

// Call sites might be dead if they do not have side effects and no live
// users. The return value might be dead if there are no live users.
getOrCreateAAFor<AAIsDead>(CSRetPos);
getOrCreateAAFor<AAIsDead>(CBRetPos);

Function *Callee = CS.getCalledFunction();
Function *Callee = CB->getCalledFunction();
// TODO: Even if the callee is not known now we might be able to simplify
// the call/callee.
if (!Callee)
Expand All @@ -1819,46 +1818,46 @@ void Attributor::identifyDefaultAbstractAttributes(Function &F) {
!Callee->hasMetadata(LLVMContext::MD_callback))
return true;

if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) {
if (!Callee->getReturnType()->isVoidTy() && !CB->use_empty()) {

IRPosition CSRetPos = IRPosition::callsite_returned(CS);
IRPosition CBRetPos = IRPosition::callsite_returned(*CB);

// Call site return integer values might be limited by a constant range.
if (Callee->getReturnType()->isIntegerTy())
getOrCreateAAFor<AAValueConstantRange>(CSRetPos);
getOrCreateAAFor<AAValueConstantRange>(CBRetPos);
}

for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) {
for (int i = 0, e = CB->getNumArgOperands(); i < e; i++) {

IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
IRPosition CBArgPos = IRPosition::callsite_argument(*CB, i);

// Every call site argument might be dead.
getOrCreateAAFor<AAIsDead>(CSArgPos);
getOrCreateAAFor<AAIsDead>(CBArgPos);

// Call site argument might be simplified.
getOrCreateAAFor<AAValueSimplify>(CSArgPos);
getOrCreateAAFor<AAValueSimplify>(CBArgPos);

if (!CS.getArgument(i)->getType()->isPointerTy())
if (!CB->getArgOperand(i)->getType()->isPointerTy())
continue;

// Call site argument attribute "non-null".
getOrCreateAAFor<AANonNull>(CSArgPos);
getOrCreateAAFor<AANonNull>(CBArgPos);

// Call site argument attribute "no-alias".
getOrCreateAAFor<AANoAlias>(CSArgPos);
getOrCreateAAFor<AANoAlias>(CBArgPos);

// Call site argument attribute "dereferenceable".
getOrCreateAAFor<AADereferenceable>(CSArgPos);
getOrCreateAAFor<AADereferenceable>(CBArgPos);

// Call site argument attribute "align".
getOrCreateAAFor<AAAlign>(CSArgPos);
getOrCreateAAFor<AAAlign>(CBArgPos);

// Call site argument attribute
// "readnone/readonly/writeonly/..."
getOrCreateAAFor<AAMemoryBehavior>(CSArgPos);
getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);

// Call site argument attribute "nofree".
getOrCreateAAFor<AANoFree>(CSArgPos);
getOrCreateAAFor<AANoFree>(CBArgPos);
}
return true;
};
Expand Down Expand Up @@ -1983,9 +1982,9 @@ static bool runAttributorOnFunctions(InformationCache &InfoCache,
// do it eagerly.
if (F->hasLocalLinkage()) {
if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
ImmutableCallSite ICS(U.getUser());
return ICS && ICS.isCallee(&U) &&
Functions.count(const_cast<Function *>(ICS.getCaller()));
const auto *CB = dyn_cast<CallBase>(U.getUser());
return CB && CB->isCallee(&U) &&
Functions.count(const_cast<Function *>(CB->getCaller()));
}))
continue;
}
Expand Down

0 comments on commit b91f78d

Please sign in to comment.