2019-11-30 03:11:24 +08:00
|
|
|
//===- 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/Transforms/Utils/ModuleUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
bool CallGraphUpdater::finalize() {
|
|
|
|
if (!DeadFunctionsInComdats.empty()) {
|
|
|
|
filterDeadComdatFunctions(*DeadFunctionsInComdats.front()->getParent(),
|
|
|
|
DeadFunctionsInComdats);
|
|
|
|
DeadFunctions.append(DeadFunctionsInComdats.begin(),
|
|
|
|
DeadFunctionsInComdats.end());
|
|
|
|
}
|
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
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);
|
2019-11-30 03:11:24 +08:00
|
|
|
DeadFn->replaceAllUsesWith(UndefValue::get(DeadFn->getType()));
|
2020-04-13 00:52:06 +08:00
|
|
|
}
|
2019-11-30 03:11:24 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
// 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);
|
2019-11-30 03:11:24 +08:00
|
|
|
}
|
2020-04-13 00:52:06 +08:00
|
|
|
} 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(UndefValue::get(DeadFn->getType()));
|
2019-11-30 03:11:24 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
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();
|
2019-11-30 03:11:24 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
FunctionAnalysisManager &FAM =
|
|
|
|
AM->getResult<FunctionAnalysisManagerCGSCCProxy>(*DeadSCC, *LCG)
|
|
|
|
.getManager();
|
2019-11-30 03:11:24 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
FAM.clear(*DeadFn, DeadFn->getName());
|
|
|
|
AM->clear(*DeadSCC, DeadSCC->getName());
|
|
|
|
LCG->removeDeadFunction(*DeadFn);
|
2020-01-26 16:51:57 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
// Mark the relevant parts of the call graph as invalid so we don't
|
|
|
|
// visit them.
|
|
|
|
UR->InvalidatedSCCs.insert(DeadSCC);
|
|
|
|
UR->InvalidatedRefSCCs.insert(&DeadRC);
|
|
|
|
}
|
2019-11-30 03:11:24 +08:00
|
|
|
|
2020-04-13 00:52:06 +08:00
|
|
|
// The function is now really dead and de-attached from everything.
|
|
|
|
DeadFn->eraseFromParent();
|
2019-11-30 03:11:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-01-15 02:27:20 +08:00
|
|
|
updateCGAndAnalysisManagerForCGSCCPass(*LCG, *C, N, *AM, *UR, *FAM);
|
2019-11-30 03:11:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[CGSCC][Coroutine][NewPM] Properly support function splitting/outlining
Previously when trying to support CoroSplit's function splitting, we
added in a hack that simply added the new function's node into the
original function's SCC (https://reviews.llvm.org/D87798). This is
incorrect since it might be in its own SCC.
Now, more similar to the previous design, we have callers explicitly
notify the LazyCallGraph that a function has been split out from another
one.
In order to properly support CoroSplit, there are two ways functions can
be split out.
One is the normal expected "outlining" of one function into a new one.
The new function may only contain references to other functions that the
original did. The original function must reference the new function. The
new function may reference the original function, which can result in
the new function being in the same SCC as the original function. The
weird case is when the original function indirectly references the new
function, but the new function directly calls the original function,
resulting in the new SCC being a parent of the original function's SCC.
This form of function splitting works with CoroSplit's Switch ABI.
The second way of splitting is more specific to CoroSplit. CoroSplit's
Retcon and Async ABIs split the original function into multiple
functions that all reference each other and are referenced by the
original function. In order to keep the LazyCallGraph in a valid state,
all new functions must be processed together, else some nodes won't be
populated. To keep things simple, this only supports the case where all
new edges are ref edges, and every new function references every other
new function. There can be a reference back from any new function to the
original function, putting all functions in the same RefSCC.
This also adds asserts that all nodes in a (Ref)SCC can reach all other
nodes to prevent future incorrect hacks.
The original hacks in https://reviews.llvm.org/D87798 are no longer
necessary since all new functions should have been registered before
calling updateCGAndAnalysisManagerForPass.
This fixes all coroutine tests when opt's -enable-new-pm is true by
default. This also fixes PR48190, which was likely due to the previous
hack breaking SCC invariants.
Reviewed By: rnk
Differential Revision: https://reviews.llvm.org/D93828
2020-12-27 02:25:34 +08:00
|
|
|
void CallGraphUpdater::registerOutlinedFunction(Function &OriginalFn,
|
|
|
|
Function &NewFn) {
|
2019-11-30 03:11:24 +08:00
|
|
|
if (CG)
|
|
|
|
CG->addToCallGraph(&NewFn);
|
[CGSCC][Coroutine][NewPM] Properly support function splitting/outlining
Previously when trying to support CoroSplit's function splitting, we
added in a hack that simply added the new function's node into the
original function's SCC (https://reviews.llvm.org/D87798). This is
incorrect since it might be in its own SCC.
Now, more similar to the previous design, we have callers explicitly
notify the LazyCallGraph that a function has been split out from another
one.
In order to properly support CoroSplit, there are two ways functions can
be split out.
One is the normal expected "outlining" of one function into a new one.
The new function may only contain references to other functions that the
original did. The original function must reference the new function. The
new function may reference the original function, which can result in
the new function being in the same SCC as the original function. The
weird case is when the original function indirectly references the new
function, but the new function directly calls the original function,
resulting in the new SCC being a parent of the original function's SCC.
This form of function splitting works with CoroSplit's Switch ABI.
The second way of splitting is more specific to CoroSplit. CoroSplit's
Retcon and Async ABIs split the original function into multiple
functions that all reference each other and are referenced by the
original function. In order to keep the LazyCallGraph in a valid state,
all new functions must be processed together, else some nodes won't be
populated. To keep things simple, this only supports the case where all
new edges are ref edges, and every new function references every other
new function. There can be a reference back from any new function to the
original function, putting all functions in the same RefSCC.
This also adds asserts that all nodes in a (Ref)SCC can reach all other
nodes to prevent future incorrect hacks.
The original hacks in https://reviews.llvm.org/D87798 are no longer
necessary since all new functions should have been registered before
calling updateCGAndAnalysisManagerForPass.
This fixes all coroutine tests when opt's -enable-new-pm is true by
default. This also fixes PR48190, which was likely due to the previous
hack breaking SCC invariants.
Reviewed By: rnk
Differential Revision: https://reviews.llvm.org/D93828
2020-12-27 02:25:34 +08:00
|
|
|
else if (LCG)
|
|
|
|
LCG->addSplitFunction(OriginalFn, NewFn);
|
2019-11-30 03:11:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraphUpdater::removeFunction(Function &DeadFn) {
|
|
|
|
DeadFn.deleteBody();
|
|
|
|
DeadFn.setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
if (DeadFn.hasComdat())
|
|
|
|
DeadFunctionsInComdats.push_back(&DeadFn);
|
|
|
|
else
|
|
|
|
DeadFunctions.push_back(&DeadFn);
|
[CallGraphUpdater] Remove nodes from their SCC (old PM)
Summary:
We can and should remove deleted nodes from their respective SCCs. We
did not do this before and this was a potential problem even though I
couldn't locally trigger an issue. Since the `DeleteNode` would assert
if the node was not in the SCC, we know we only remove nodes from their
SCC and only once (when run on all the Attributor tests).
Reviewers: lebedev.ri, hfinkel, fhahn, probinson, wristow, loladiro, sstefan1, uenoku
Subscribers: hiraditya, bollu, uenoku, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77855
2020-04-10 04:43:31 +08:00
|
|
|
|
|
|
|
// For the old call graph we remove the function from the SCC right away.
|
2020-05-05 09:58:59 +08:00
|
|
|
if (CG && !ReplacedFunctions.count(&DeadFn)) {
|
|
|
|
CallGraphNode *DeadCGN = (*CG)[&DeadFn];
|
|
|
|
DeadCGN->removeAllCalledFunctions();
|
|
|
|
CGSCC->DeleteNode(DeadCGN);
|
|
|
|
}
|
2019-11-30 03:11:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallGraphUpdater::replaceFunctionWith(Function &OldFn, Function &NewFn) {
|
2020-03-23 14:09:49 +08:00
|
|
|
OldFn.removeDeadConstantUsers();
|
2019-11-30 03:11:24 +08:00
|
|
|
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);
|
[CallGraphUpdater] Update the ExternalCallingNode for node replacements
Summary:
While it is uncommon that the ExternalCallingNode needs to be updated,
it can happen. It is uncommon because most functions listed as callees
have external linkage, modifying them is usually not allowed. That said,
there are also internal functions that have, or better had, their
"address taken" at construction time. We conservatively assume various
uses cause the address "to be taken". Furthermore, the user might have
become dead at some point. As a consequence, transformations, e.g., the
Attributor, might be able to replace a function that is listed
as callee of the ExternalCallingNode.
Since there is no function corresponding to the ExternalCallingNode, we
did just remove the node from the callee list if we replaced it (so
far). Now it would be preferable to replace it if needed and remove it
otherwise. However, removing the node has implications on the CGSCC
iteration. Locally, that caused some other nodes to be never visited
but it is for sure possible other (bad) side effects can occur. As it
seems conservatively safe to keep the new node in the callee list we
will do that for now.
Reviewers: lebedev.ri, hfinkel, fhahn, probinson, wristow, loladiro, sstefan1, uenoku
Subscribers: hiraditya, bollu, uenoku, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77854
2020-04-10 04:21:24 +08:00
|
|
|
CG->ReplaceExternalCallEdge(OldCGN, NewCGN);
|
2019-11-30 03:11:24 +08:00
|
|
|
|
|
|
|
// 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) {
|
2020-06-30 07:11:16 +08:00
|
|
|
return CR.first && *CR.first == &OldCS;
|
2019-11-30 03:11:24 +08:00
|
|
|
}))
|
|
|
|
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);
|
|
|
|
}
|