1313// ===----------------------------------------------------------------------===//
1414
1515#include " llvm/Transforms/Utils/CallGraphUpdater.h"
16- #include " llvm/ADT/STLExtras.h"
17- #include " llvm/Analysis/CallGraph.h"
18- #include " llvm/Analysis/CallGraphSCCPass.h"
1916#include " llvm/IR/Constants.h"
2017#include " llvm/Transforms/Utils/ModuleUtils.h"
2118
@@ -28,53 +25,33 @@ bool CallGraphUpdater::finalize() {
2825 DeadFunctionsInComdats.end ());
2926 }
3027
31- if (CG) {
32- // First remove all references, e.g., outgoing via called functions. This is
33- // necessary as we can delete functions that have circular references.
34- for (Function *DeadFn : DeadFunctions) {
35- DeadFn->removeDeadConstantUsers ();
36- CallGraphNode *DeadCGN = (*CG)[DeadFn];
37- DeadCGN->removeAllCalledFunctions ();
38- CG->getExternalCallingNode ()->removeAnyCallEdgeTo (DeadCGN);
39- DeadFn->replaceAllUsesWith (PoisonValue::get (DeadFn->getType ()));
40- }
41-
42- // Then remove the node and function from the module.
43- for (Function *DeadFn : DeadFunctions) {
44- CallGraphNode *DeadCGN = CG->getOrInsertFunction (DeadFn);
45- assert (DeadCGN->getNumReferences () == 0 &&
46- " References should have been handled by now" );
47- delete CG->removeFunctionFromModule (DeadCGN);
48- }
49- } else {
50- // This is the code path for the new lazy call graph and for the case were
51- // no call graph was provided.
52- for (Function *DeadFn : DeadFunctions) {
53- DeadFn->removeDeadConstantUsers ();
54- DeadFn->replaceAllUsesWith (PoisonValue::get (DeadFn->getType ()));
55-
56- if (LCG && !ReplacedFunctions.count (DeadFn)) {
57- // Taken mostly from the inliner:
58- LazyCallGraph::Node &N = LCG->get (*DeadFn);
59- auto *DeadSCC = LCG->lookupSCC (N);
60- assert (DeadSCC && DeadSCC->size () == 1 &&
61- &DeadSCC->begin ()->getFunction () == DeadFn);
62-
63- FAM->clear (*DeadFn, DeadFn->getName ());
64- AM->clear (*DeadSCC, DeadSCC->getName ());
65- LCG->markDeadFunction (*DeadFn);
66-
67- // Mark the relevant parts of the call graph as invalid so we don't
68- // visit them.
69- UR->InvalidatedSCCs .insert (LCG->lookupSCC (N));
70- UR->DeadFunctions .push_back (DeadFn);
71- } else {
72- // The CGSCC infrastructure batch deletes functions at the end of the
73- // call graph walk, so only erase the function if we're not using that
74- // infrastructure.
75- // The function is now really dead and de-attached from everything.
76- DeadFn->eraseFromParent ();
77- }
28+ // This is the code path for the new lazy call graph and for the case were
29+ // no call graph was provided.
30+ for (Function *DeadFn : DeadFunctions) {
31+ DeadFn->removeDeadConstantUsers ();
32+ DeadFn->replaceAllUsesWith (PoisonValue::get (DeadFn->getType ()));
33+
34+ if (LCG && !ReplacedFunctions.count (DeadFn)) {
35+ // Taken mostly from the inliner:
36+ LazyCallGraph::Node &N = LCG->get (*DeadFn);
37+ auto *DeadSCC = LCG->lookupSCC (N);
38+ assert (DeadSCC && DeadSCC->size () == 1 &&
39+ &DeadSCC->begin ()->getFunction () == DeadFn);
40+
41+ FAM->clear (*DeadFn, DeadFn->getName ());
42+ AM->clear (*DeadSCC, DeadSCC->getName ());
43+ LCG->markDeadFunction (*DeadFn);
44+
45+ // Mark the relevant parts of the call graph as invalid so we don't
46+ // visit them.
47+ UR->InvalidatedSCCs .insert (LCG->lookupSCC (N));
48+ UR->DeadFunctions .push_back (DeadFn);
49+ } else {
50+ // The CGSCC infrastructure batch deletes functions at the end of the
51+ // call graph walk, so only erase the function if we're not using that
52+ // infrastructure.
53+ // The function is now really dead and de-attached from everything.
54+ DeadFn->eraseFromParent ();
7855 }
7956 }
8057
@@ -85,11 +62,7 @@ bool CallGraphUpdater::finalize() {
8562}
8663
8764void CallGraphUpdater::reanalyzeFunction (Function &Fn) {
88- if (CG) {
89- CallGraphNode *OldCGN = CG->getOrInsertFunction (&Fn);
90- OldCGN->removeAllCalledFunctions ();
91- CG->populateCallGraphNode (OldCGN);
92- } else if (LCG) {
65+ if (LCG) {
9366 LazyCallGraph::Node &N = LCG->get (Fn);
9467 LazyCallGraph::SCC *C = LCG->lookupSCC (N);
9568 updateCGAndAnalysisManagerForCGSCCPass (*LCG, *C, N, *AM, *UR, *FAM);
@@ -98,9 +71,7 @@ void CallGraphUpdater::reanalyzeFunction(Function &Fn) {
9871
9972void CallGraphUpdater::registerOutlinedFunction (Function &OriginalFn,
10073 Function &NewFn) {
101- if (CG)
102- CG->addToCallGraph (&NewFn);
103- else if (LCG)
74+ if (LCG)
10475 LCG->addSplitFunction (OriginalFn, NewFn);
10576}
10677
@@ -112,59 +83,17 @@ void CallGraphUpdater::removeFunction(Function &DeadFn) {
11283 else
11384 DeadFunctions.push_back (&DeadFn);
11485
115- // For the old call graph we remove the function from the SCC right away.
116- if (CG && !ReplacedFunctions.count (&DeadFn)) {
117- CallGraphNode *DeadCGN = (*CG)[&DeadFn];
118- DeadCGN->removeAllCalledFunctions ();
119- CGSCC->DeleteNode (DeadCGN);
120- }
12186 if (FAM)
12287 FAM->clear (DeadFn, DeadFn.getName ());
12388}
12489
12590void CallGraphUpdater::replaceFunctionWith (Function &OldFn, Function &NewFn) {
12691 OldFn.removeDeadConstantUsers ();
12792 ReplacedFunctions.insert (&OldFn);
128- if (CG) {
129- // Update the call graph for the newly promoted function.
130- CallGraphNode *OldCGN = (*CG)[&OldFn];
131- CallGraphNode *NewCGN = CG->getOrInsertFunction (&NewFn);
132- NewCGN->stealCalledFunctionsFrom (OldCGN);
133- CG->ReplaceExternalCallEdge (OldCGN, NewCGN);
134-
135- // And update the SCC we're iterating as well.
136- CGSCC->ReplaceNode (OldCGN, NewCGN);
137- } else if (LCG) {
93+ if (LCG) {
13894 // Directly substitute the functions in the call graph.
13995 LazyCallGraph::Node &OldLCGN = LCG->get (OldFn);
14096 SCC->getOuterRefSCC ().replaceNodeFunction (OldLCGN, NewFn);
14197 }
14298 removeFunction (OldFn);
14399}
144-
145- bool CallGraphUpdater::replaceCallSite (CallBase &OldCS, CallBase &NewCS) {
146- // This is only necessary in the (old) CG.
147- if (!CG)
148- return true ;
149-
150- Function *Caller = OldCS.getCaller ();
151- CallGraphNode *NewCalleeNode =
152- CG->getOrInsertFunction (NewCS.getCalledFunction ());
153- CallGraphNode *CallerNode = (*CG)[Caller];
154- if (llvm::none_of (*CallerNode, [&OldCS](const CallGraphNode::CallRecord &CR) {
155- return CR.first && *CR.first == &OldCS;
156- }))
157- return false ;
158- CallerNode->replaceCallEdge (OldCS, NewCS, NewCalleeNode);
159- return true ;
160- }
161-
162- void CallGraphUpdater::removeCallSite (CallBase &CS) {
163- // This is only necessary in the (old) CG.
164- if (!CG)
165- return ;
166-
167- Function *Caller = CS.getCaller ();
168- CallGraphNode *CallerNode = (*CG)[Caller];
169- CallerNode->removeCallEdgeFor (CS);
170- }
0 commit comments