13
13
// ===----------------------------------------------------------------------===//
14
14
15
15
#include " llvm/Transforms/Utils/CallGraphUpdater.h"
16
- #include " llvm/ADT/STLExtras.h"
17
- #include " llvm/Analysis/CallGraph.h"
18
- #include " llvm/Analysis/CallGraphSCCPass.h"
19
16
#include " llvm/IR/Constants.h"
20
17
#include " llvm/Transforms/Utils/ModuleUtils.h"
21
18
@@ -28,53 +25,33 @@ bool CallGraphUpdater::finalize() {
28
25
DeadFunctionsInComdats.end ());
29
26
}
30
27
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 ();
78
55
}
79
56
}
80
57
@@ -85,11 +62,7 @@ bool CallGraphUpdater::finalize() {
85
62
}
86
63
87
64
void 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) {
93
66
LazyCallGraph::Node &N = LCG->get (Fn);
94
67
LazyCallGraph::SCC *C = LCG->lookupSCC (N);
95
68
updateCGAndAnalysisManagerForCGSCCPass (*LCG, *C, N, *AM, *UR, *FAM);
@@ -98,9 +71,7 @@ void CallGraphUpdater::reanalyzeFunction(Function &Fn) {
98
71
99
72
void CallGraphUpdater::registerOutlinedFunction (Function &OriginalFn,
100
73
Function &NewFn) {
101
- if (CG)
102
- CG->addToCallGraph (&NewFn);
103
- else if (LCG)
74
+ if (LCG)
104
75
LCG->addSplitFunction (OriginalFn, NewFn);
105
76
}
106
77
@@ -112,59 +83,17 @@ void CallGraphUpdater::removeFunction(Function &DeadFn) {
112
83
else
113
84
DeadFunctions.push_back (&DeadFn);
114
85
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
- }
121
86
if (FAM)
122
87
FAM->clear (DeadFn, DeadFn.getName ());
123
88
}
124
89
125
90
void CallGraphUpdater::replaceFunctionWith (Function &OldFn, Function &NewFn) {
126
91
OldFn.removeDeadConstantUsers ();
127
92
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) {
138
94
// Directly substitute the functions in the call graph.
139
95
LazyCallGraph::Node &OldLCGN = LCG->get (OldFn);
140
96
SCC->getOuterRefSCC ().replaceNodeFunction (OldLCGN, NewFn);
141
97
}
142
98
removeFunction (OldFn);
143
99
}
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