In some modules, e.g. Kotlin-generated IR, we end up with a huge RefSCC and the call graph updates done as a result of the inliner take a long time. This is due to RefSCC::removeInternalRefEdges() getting called many times, each time removing one function from the RefSCC, but each call to removeInternalRefEdges() is proportional to the size of the RefSCC. There are two places that call removeInternalRefEdges(), in updateCGAndAnalysisManagerForPass() and LazyCallGraph::removeDeadFunction(). 1) Since LazyCallGraph can deal with spurious (edges that exist in the graph but not in the IR) ref edges, we can simply not call removeInternalRefEdges() in updateCGAndAnalysisManagerForPass(). 2) LazyCallGraph::removeDeadFunction() still ends up taking the brunt of compile time with the above change for the original reason. So instead we batch all the dead function removals so we can call removeInternalRefEdges() just once. This requires some changes to callers of removeDeadFunction() to not actually erase the function from the module, but defer it to when we batch delete dead functions at the end of the CGSCC run, leaving the function body as "unreachable" in the meantime. We still need to ensure that call edges are accurate. I had also tried deleting dead functions after visiting a RefSCC, but deleting them all at once at the end was simpler. Many test changes are due to not performing unnecessary revisits of an SCC (the CGSCC infrastructure deems ref edge refinements as unimportant when it comes to revisiting SCCs, although that seems to not be consistently true given these changes) because we don't remove some ref edges. Specifically for devirt-invalidated.ll this seems to expose an inlining order issue with the inliner. Probably unimportant for this type of intentionally weird call graph. Compile time: https://llvm-compile-time-tracker.com/compare.php?from=6f2c61071c274a1b5e212e6ad4114641ec7c7fc3&to=b08c90d05e290dd065755ea776ceaf1420680224&stat=instructions:u
177 lines
6.2 KiB
C++
177 lines
6.2 KiB
C++
//===- CallGraphUpdater.cpp - A (lazy) call graph update helper -----------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
/// \file
|
|
///
|
|
/// This file provides interfaces used to manipulate a call graph, regardless
|
|
/// if it is a "old style" CallGraph or an "new style" LazyCallGraph.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/Utils/CallGraphUpdater.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
#include "llvm/Analysis/CallGraphSCCPass.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
|
|
|
using namespace llvm;
|
|
|
|
bool CallGraphUpdater::finalize() {
|
|
if (!DeadFunctionsInComdats.empty()) {
|
|
filterDeadComdatFunctions(DeadFunctionsInComdats);
|
|
DeadFunctions.append(DeadFunctionsInComdats.begin(),
|
|
DeadFunctionsInComdats.end());
|
|
}
|
|
|
|
if (CG) {
|
|
// First remove all references, e.g., outgoing via called functions. This is
|
|
// necessary as we can delete functions that have circular references.
|
|
for (Function *DeadFn : DeadFunctions) {
|
|
DeadFn->removeDeadConstantUsers();
|
|
CallGraphNode *DeadCGN = (*CG)[DeadFn];
|
|
DeadCGN->removeAllCalledFunctions();
|
|
CG->getExternalCallingNode()->removeAnyCallEdgeTo(DeadCGN);
|
|
DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));
|
|
}
|
|
|
|
// Then remove the node and function from the module.
|
|
for (Function *DeadFn : DeadFunctions) {
|
|
CallGraphNode *DeadCGN = CG->getOrInsertFunction(DeadFn);
|
|
assert(DeadCGN->getNumReferences() == 0 &&
|
|
"References should have been handled by now");
|
|
delete CG->removeFunctionFromModule(DeadCGN);
|
|
}
|
|
} else {
|
|
// This is the code path for the new lazy call graph and for the case were
|
|
// no call graph was provided.
|
|
for (Function *DeadFn : DeadFunctions) {
|
|
DeadFn->removeDeadConstantUsers();
|
|
DeadFn->replaceAllUsesWith(PoisonValue::get(DeadFn->getType()));
|
|
|
|
if (LCG && !ReplacedFunctions.count(DeadFn)) {
|
|
// Taken mostly from the inliner:
|
|
LazyCallGraph::Node &N = LCG->get(*DeadFn);
|
|
auto *DeadSCC = LCG->lookupSCC(N);
|
|
assert(DeadSCC && DeadSCC->size() == 1 &&
|
|
&DeadSCC->begin()->getFunction() == DeadFn);
|
|
auto &DeadRC = DeadSCC->getOuterRefSCC();
|
|
|
|
FunctionAnalysisManager &FAM =
|
|
AM->getResult<FunctionAnalysisManagerCGSCCProxy>(*DeadSCC, *LCG)
|
|
.getManager();
|
|
|
|
FAM.clear(*DeadFn, DeadFn->getName());
|
|
AM->clear(*DeadSCC, DeadSCC->getName());
|
|
LCG->markDeadFunction(*DeadFn);
|
|
|
|
// Mark the relevant parts of the call graph as invalid so we don't
|
|
// visit them.
|
|
UR->InvalidatedSCCs.insert(DeadSCC);
|
|
UR->InvalidatedRefSCCs.insert(&DeadRC);
|
|
UR->DeadFunctions.push_back(DeadFn);
|
|
} else {
|
|
// The CGSCC infrastructure batch deletes functions at the end of the
|
|
// call graph walk, so only erase the function if we're not using that
|
|
// infrastructure.
|
|
// The function is now really dead and de-attached from everything.
|
|
DeadFn->eraseFromParent();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Changed = !DeadFunctions.empty();
|
|
DeadFunctionsInComdats.clear();
|
|
DeadFunctions.clear();
|
|
return Changed;
|
|
}
|
|
|
|
void CallGraphUpdater::reanalyzeFunction(Function &Fn) {
|
|
if (CG) {
|
|
CallGraphNode *OldCGN = CG->getOrInsertFunction(&Fn);
|
|
OldCGN->removeAllCalledFunctions();
|
|
CG->populateCallGraphNode(OldCGN);
|
|
} else if (LCG) {
|
|
LazyCallGraph::Node &N = LCG->get(Fn);
|
|
LazyCallGraph::SCC *C = LCG->lookupSCC(N);
|
|
updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
|
|
}
|
|
}
|
|
|
|
void CallGraphUpdater::registerOutlinedFunction(Function &OriginalFn,
|
|
Function &NewFn) {
|
|
if (CG)
|
|
CG->addToCallGraph(&NewFn);
|
|
else if (LCG)
|
|
LCG->addSplitFunction(OriginalFn, NewFn);
|
|
}
|
|
|
|
void CallGraphUpdater::removeFunction(Function &DeadFn) {
|
|
DeadFn.deleteBody();
|
|
DeadFn.setLinkage(GlobalValue::ExternalLinkage);
|
|
if (DeadFn.hasComdat())
|
|
DeadFunctionsInComdats.push_back(&DeadFn);
|
|
else
|
|
DeadFunctions.push_back(&DeadFn);
|
|
|
|
// For the old call graph we remove the function from the SCC right away.
|
|
if (CG && !ReplacedFunctions.count(&DeadFn)) {
|
|
CallGraphNode *DeadCGN = (*CG)[&DeadFn];
|
|
DeadCGN->removeAllCalledFunctions();
|
|
CGSCC->DeleteNode(DeadCGN);
|
|
}
|
|
if (FAM)
|
|
FAM->clear(DeadFn, DeadFn.getName());
|
|
}
|
|
|
|
void CallGraphUpdater::replaceFunctionWith(Function &OldFn, Function &NewFn) {
|
|
OldFn.removeDeadConstantUsers();
|
|
ReplacedFunctions.insert(&OldFn);
|
|
if (CG) {
|
|
// Update the call graph for the newly promoted function.
|
|
CallGraphNode *OldCGN = (*CG)[&OldFn];
|
|
CallGraphNode *NewCGN = CG->getOrInsertFunction(&NewFn);
|
|
NewCGN->stealCalledFunctionsFrom(OldCGN);
|
|
CG->ReplaceExternalCallEdge(OldCGN, NewCGN);
|
|
|
|
// And update the SCC we're iterating as well.
|
|
CGSCC->ReplaceNode(OldCGN, NewCGN);
|
|
} else if (LCG) {
|
|
// Directly substitute the functions in the call graph.
|
|
LazyCallGraph::Node &OldLCGN = LCG->get(OldFn);
|
|
SCC->getOuterRefSCC().replaceNodeFunction(OldLCGN, NewFn);
|
|
}
|
|
removeFunction(OldFn);
|
|
}
|
|
|
|
bool CallGraphUpdater::replaceCallSite(CallBase &OldCS, CallBase &NewCS) {
|
|
// This is only necessary in the (old) CG.
|
|
if (!CG)
|
|
return true;
|
|
|
|
Function *Caller = OldCS.getCaller();
|
|
CallGraphNode *NewCalleeNode =
|
|
CG->getOrInsertFunction(NewCS.getCalledFunction());
|
|
CallGraphNode *CallerNode = (*CG)[Caller];
|
|
if (llvm::none_of(*CallerNode, [&OldCS](const CallGraphNode::CallRecord &CR) {
|
|
return CR.first && *CR.first == &OldCS;
|
|
}))
|
|
return false;
|
|
CallerNode->replaceCallEdge(OldCS, NewCS, NewCalleeNode);
|
|
return true;
|
|
}
|
|
|
|
void CallGraphUpdater::removeCallSite(CallBase &CS) {
|
|
// This is only necessary in the (old) CG.
|
|
if (!CG)
|
|
return;
|
|
|
|
Function *Caller = CS.getCaller();
|
|
CallGraphNode *CallerNode = (*CG)[Caller];
|
|
CallerNode->removeCallEdgeFor(CS);
|
|
}
|