Skip to content

Commit

Permalink
[ArgPromotion] Remove all the getters and ReplaceCallSite (NFC)
Browse files Browse the repository at this point in the history
AARGetter is an abstraction over a source of the `AAResults` introduced
to support the legacy pass manager as well as the modern one. Since the
Argument Promotion pass doesn't support the legacy pass manager anymore,
the abstraction is not required and `AAResults` may be used directly.

The instance of the `FunctionAnalysisManager` is passed through the
functions to get all the required analyses just wherever they are
required and do not use the awkward getter callbacks.

The `ReplaceCallSite` parameter was required for the legacy pass manager
only and isn't used anymore, so the parameter has been eliminated.

Differential Revision: https://reviews.llvm.org/D128727
  • Loading branch information
samolisov committed Jun 29, 2022
1 parent 8958057 commit 3d9ce9e
Showing 1 changed file with 15 additions and 56 deletions.
71 changes: 15 additions & 56 deletions llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
Expand Up @@ -31,8 +31,6 @@
#include "llvm/Transforms/IPO/ArgumentPromotion.h"

#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallPtrSet.h"
Expand All @@ -41,13 +39,9 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/LazyCallGraph.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
Expand All @@ -64,19 +58,15 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/NoFolder.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -155,12 +145,10 @@ static Value *createByteGEP(IRBuilderBase &IRB, const DataLayout &DL,
/// DoPromotion - This method actually performs the promotion of the specified
/// arguments, and returns the new function. At this point, we know that it's
/// safe to do so.
static Function *doPromotion(
Function *F, function_ref<DominatorTree &(Function &F)> DTGetter,
function_ref<AssumptionCache *(Function &F)> ACGetter,
const DenseMap<Argument *, SmallVector<OffsetAndArgPart, 4>> &ArgsToPromote,
Optional<function_ref<void(CallBase &OldCS, CallBase &NewCS)>>
ReplaceCallSite) {
static Function *
doPromotion(Function *F, FunctionAnalysisManager &FAM,
const DenseMap<Argument *, SmallVector<OffsetAndArgPart, 4>>
&ArgsToPromote) {
// Start by computing a new prototype for the function, which is the same as
// the old function, but has modified arguments.
FunctionType *FTy = F->getFunctionType();
Expand Down Expand Up @@ -228,9 +216,8 @@ static Function *doPromotion(
F->getParent()->getFunctionList().insert(F->getIterator(), NF);
NF->takeName(F);

// Loop over all of the callers of the function, transforming the call sites
// to pass in the loaded pointers.
//
// Loop over all the callers of the function, transforming the call sites to
// pass in the loaded pointers.
SmallVector<Value *, 16> Args;
const DataLayout &DL = F->getParent()->getDataLayout();
while (!F->use_empty()) {
Expand Down Expand Up @@ -299,10 +286,6 @@ static Function *doPromotion(
AttributeFuncs::updateMinLegalVectorWidthAttr(*CB.getCaller(),
LargestVectorWidth);

// Update the callgraph to know that the callsite has been transformed.
if (ReplaceCallSite)
(*ReplaceCallSite)(CB, *NewCS);

if (!CB.use_empty()) {
CB.replaceAllUsesWith(NewCS);
NewCS->takeName(&CB);
Expand Down Expand Up @@ -429,7 +412,9 @@ static Function *doPromotion(
// And we are able to call the `promoteMemoryToRegister()` function.
// Our earlier checks have ensured that PromoteMemToReg() will
// succeed.
PromoteMemToReg(Allocas, DTGetter(*NF), ACGetter(*NF));
auto &DT = FAM.getResult<DominatorTreeAnalysis>(*NF);
auto &AC = FAM.getResult<AssumptionAnalysis>(*NF);
PromoteMemToReg(Allocas, DT, &AC);
}

return NF;
Expand Down Expand Up @@ -717,14 +702,8 @@ static bool areTypesABICompatible(ArrayRef<Type *> Types, const Function &F,
/// are any promotable arguments and if it is safe to promote the function (for
/// example, all callers are direct). If safe to promote some arguments, it
/// calls the DoPromotion method.
static Function *
promoteArguments(Function *F, function_ref<AAResults &(Function &F)> AARGetter,
function_ref<DominatorTree &(Function &F)> DTGetter,
function_ref<AssumptionCache *(Function &F)> ACGetter,
unsigned MaxElements,
Optional<function_ref<void(CallBase &OldCS, CallBase &NewCS)>>
ReplaceCallSite,
const TargetTransformInfo &TTI, bool IsRecursive) {
static Function *promoteArguments(Function *F, FunctionAnalysisManager &FAM,
unsigned MaxElements, bool IsRecursive) {
// Don't perform argument promotion for naked functions; otherwise we can end
// up removing parameters that are seemingly 'not used' as they are referred
// to in the assembly.
Expand Down Expand Up @@ -781,8 +760,8 @@ promoteArguments(Function *F, function_ref<AAResults &(Function &F)> AARGetter,
return nullptr;

const DataLayout &DL = F->getParent()->getDataLayout();

AAResults &AAR = AARGetter(*F);
auto &AAR = FAM.getResult<AAManager>(*F);
const auto &TTI = FAM.getResult<TargetIRAnalysis>(*F);

// Check to see which arguments are promotable. If an argument is promotable,
// add it to ArgsToPromote.
Expand Down Expand Up @@ -819,7 +798,7 @@ promoteArguments(Function *F, function_ref<AAResults &(Function &F)> AARGetter,
if (ArgsToPromote.empty())
return nullptr;

return doPromotion(F, DTGetter, ACGetter, ArgsToPromote, ReplaceCallSite);
return doPromotion(F, FAM, ArgsToPromote);
}

PreservedAnalyses ArgumentPromotionPass::run(LazyCallGraph::SCC &C,
Expand All @@ -838,27 +817,7 @@ PreservedAnalyses ArgumentPromotionPass::run(LazyCallGraph::SCC &C,
bool IsRecursive = C.size() > 1;
for (LazyCallGraph::Node &N : C) {
Function &OldF = N.getFunction();

// FIXME: This lambda must only be used with this function. We should
// skip the lambda and just get the AA results directly.
auto AARGetter = [&](Function &F) -> AAResults & {
assert(&F == &OldF && "Called with an unexpected function!");
return FAM.getResult<AAManager>(F);
};

auto DTGetter = [&](Function &F) -> DominatorTree & {
assert(&F != &OldF && "Called with the obsolete function!");
return FAM.getResult<DominatorTreeAnalysis>(F);
};

auto ACGetter = [&](Function &F) -> AssumptionCache * {
assert(&F != &OldF && "Called with the obsolete function!");
return &FAM.getResult<AssumptionAnalysis>(F);
};

const auto &TTI = FAM.getResult<TargetIRAnalysis>(OldF);
Function *NewF = promoteArguments(&OldF, AARGetter, DTGetter, ACGetter,
MaxElements, None, TTI, IsRecursive);
Function *NewF = promoteArguments(&OldF, FAM, MaxElements, IsRecursive);
if (!NewF)
continue;
LocalChange = true;
Expand Down

0 comments on commit 3d9ce9e

Please sign in to comment.