1 //===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Analysis/LazyCallGraph.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/IR/CallSite.h"
13 #include "llvm/IR/InstVisitor.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/PassManager.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "lcg"
22 
23 static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
24                     DenseMap<Function *, size_t> &EdgeIndexMap, Function &F,
25                     LazyCallGraph::Edge::Kind EK) {
26   // Note that we consider *any* function with a definition to be a viable
27   // edge. Even if the function's definition is subject to replacement by
28   // some other module (say, a weak definition) there may still be
29   // optimizations which essentially speculate based on the definition and
30   // a way to check that the specific definition is in fact the one being
31   // used. For example, this could be done by moving the weak definition to
32   // a strong (internal) definition and making the weak definition be an
33   // alias. Then a test of the address of the weak function against the new
34   // strong definition's address would be an effective way to determine the
35   // safety of optimizing a direct call edge.
36   if (!F.isDeclaration() &&
37       EdgeIndexMap.insert(std::make_pair(&F, Edges.size())).second) {
38     DEBUG(dbgs() << "    Added callable function: " << F.getName() << "\n");
39     Edges.emplace_back(LazyCallGraph::Edge(F, EK));
40   }
41 }
42 
43 static void findReferences(
44                       SmallVectorImpl<Constant *> &Worklist,
45                       SmallPtrSetImpl<Constant *> &Visited,
46                       SmallVectorImpl<LazyCallGraph::Edge> &Edges,
47                       DenseMap<Function *, size_t> &EdgeIndexMap) {
48   while (!Worklist.empty()) {
49     Constant *C = Worklist.pop_back_val();
50 
51     if (Function *F = dyn_cast<Function>(C)) {
52       addEdge(Edges, EdgeIndexMap, *F, LazyCallGraph::Edge::Ref);
53       continue;
54     }
55 
56     for (Value *Op : C->operand_values())
57       if (Visited.insert(cast<Constant>(Op)).second)
58         Worklist.push_back(cast<Constant>(Op));
59   }
60 }
61 
62 LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
63     : G(&G), F(F), DFSNumber(0), LowLink(0) {
64   DEBUG(dbgs() << "  Adding functions called by '" << F.getName()
65                << "' to the graph.\n");
66 
67   SmallVector<Constant *, 16> Worklist;
68   SmallPtrSet<Function *, 4> Callees;
69   SmallPtrSet<Constant *, 16> Visited;
70 
71   // Find all the potential call graph edges in this function. We track both
72   // actual call edges and indirect references to functions. The direct calls
73   // are trivially added, but to accumulate the latter we walk the instructions
74   // and add every operand which is a constant to the worklist to process
75   // afterward.
76   for (BasicBlock &BB : F)
77     for (Instruction &I : BB) {
78       if (auto CS = CallSite(&I))
79         if (Function *Callee = CS.getCalledFunction())
80           if (Callees.insert(Callee).second) {
81             Visited.insert(Callee);
82             addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
83           }
84 
85       for (Value *Op : I.operand_values())
86         if (Constant *C = dyn_cast<Constant>(Op))
87           if (Visited.insert(C).second)
88             Worklist.push_back(C);
89     }
90 
91   // We've collected all the constant (and thus potentially function or
92   // function containing) operands to all of the instructions in the function.
93   // Process them (recursively) collecting every function found.
94   findReferences(Worklist, Visited, Edges, EdgeIndexMap);
95 }
96 
97 void LazyCallGraph::Node::insertEdgeInternal(Function &Child, Edge::Kind EK) {
98   if (Node *N = G->lookup(Child))
99     return insertEdgeInternal(*N, EK);
100 
101   EdgeIndexMap.insert(std::make_pair(&Child, Edges.size()));
102   Edges.emplace_back(Child, EK);
103 }
104 
105 void LazyCallGraph::Node::insertEdgeInternal(Node &ChildN, Edge::Kind EK) {
106   EdgeIndexMap.insert(std::make_pair(&ChildN.getFunction(), Edges.size()));
107   Edges.emplace_back(ChildN, EK);
108 }
109 
110 void LazyCallGraph::Node::removeEdgeInternal(Function &Child) {
111   auto IndexMapI = EdgeIndexMap.find(&Child);
112   assert(IndexMapI != EdgeIndexMap.end() &&
113          "Child not in the edge set for this caller?");
114 
115   Edges[IndexMapI->second] = Edge();
116   EdgeIndexMap.erase(IndexMapI);
117 }
118 
119 LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
120   DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
121                << "\n");
122   for (Function &F : M)
123     if (!F.isDeclaration() && !F.hasLocalLinkage())
124       if (EntryIndexMap.insert(std::make_pair(&F, EntryEdges.size())).second) {
125         DEBUG(dbgs() << "  Adding '" << F.getName()
126                      << "' to entry set of the graph.\n");
127         EntryEdges.emplace_back(F, Edge::Ref);
128       }
129 
130   // Now add entry nodes for functions reachable via initializers to globals.
131   SmallVector<Constant *, 16> Worklist;
132   SmallPtrSet<Constant *, 16> Visited;
133   for (GlobalVariable &GV : M.globals())
134     if (GV.hasInitializer())
135       if (Visited.insert(GV.getInitializer()).second)
136         Worklist.push_back(GV.getInitializer());
137 
138   DEBUG(dbgs() << "  Adding functions referenced by global initializers to the "
139                   "entry set.\n");
140   findReferences(Worklist, Visited, EntryEdges, EntryIndexMap);
141 
142   for (const Edge &E : EntryEdges)
143     SCCEntryNodes.push_back(&E.getFunction());
144 }
145 
146 LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
147     : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
148       EntryEdges(std::move(G.EntryEdges)),
149       EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
150       SCCMap(std::move(G.SCCMap)), LeafSCCs(std::move(G.LeafSCCs)),
151       DFSStack(std::move(G.DFSStack)),
152       SCCEntryNodes(std::move(G.SCCEntryNodes)),
153       NextDFSNumber(G.NextDFSNumber) {
154   updateGraphPtrs();
155 }
156 
157 LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
158   BPA = std::move(G.BPA);
159   NodeMap = std::move(G.NodeMap);
160   EntryEdges = std::move(G.EntryEdges);
161   EntryIndexMap = std::move(G.EntryIndexMap);
162   SCCBPA = std::move(G.SCCBPA);
163   SCCMap = std::move(G.SCCMap);
164   LeafSCCs = std::move(G.LeafSCCs);
165   DFSStack = std::move(G.DFSStack);
166   SCCEntryNodes = std::move(G.SCCEntryNodes);
167   NextDFSNumber = G.NextDFSNumber;
168   updateGraphPtrs();
169   return *this;
170 }
171 
172 void LazyCallGraph::SCC::insert(Node &N) {
173   N.DFSNumber = N.LowLink = -1;
174   Nodes.push_back(&N);
175   G->SCCMap[&N] = this;
176 }
177 
178 bool LazyCallGraph::SCC::isDescendantOf(const SCC &C) const {
179   // Walk up the parents of this SCC and verify that we eventually find C.
180   SmallVector<const SCC *, 4> AncestorWorklist;
181   AncestorWorklist.push_back(this);
182   do {
183     const SCC *AncestorC = AncestorWorklist.pop_back_val();
184     if (AncestorC->isChildOf(C))
185       return true;
186     for (const SCC *ParentC : AncestorC->ParentSCCs)
187       AncestorWorklist.push_back(ParentC);
188   } while (!AncestorWorklist.empty());
189 
190   return false;
191 }
192 
193 void LazyCallGraph::SCC::insertIntraSCCEdge(Node &ParentN, Node &ChildN,
194                                             Edge::Kind EK) {
195   // First insert it into the caller.
196   ParentN.insertEdgeInternal(ChildN, EK);
197 
198   assert(G->SCCMap.lookup(&ParentN) == this && "Parent must be in this SCC.");
199   assert(G->SCCMap.lookup(&ChildN) == this && "Child must be in this SCC.");
200 
201   // Nothing changes about this SCC or any other.
202 }
203 
204 void LazyCallGraph::SCC::insertOutgoingEdge(Node &ParentN, Node &ChildN,
205                                             Edge::Kind EK) {
206   // First insert it into the caller.
207   ParentN.insertEdgeInternal(ChildN, EK);
208 
209   assert(G->SCCMap.lookup(&ParentN) == this && "Parent must be in this SCC.");
210 
211   SCC &ChildC = *G->SCCMap.lookup(&ChildN);
212   assert(&ChildC != this && "Child must not be in this SCC.");
213   assert(ChildC.isDescendantOf(*this) &&
214          "Child must be a descendant of the Parent.");
215 
216   // The only change required is to add this SCC to the parent set of the
217   // callee.
218   ChildC.ParentSCCs.insert(this);
219 }
220 
221 SmallVector<LazyCallGraph::SCC *, 1>
222 LazyCallGraph::SCC::insertIncomingEdge(Node &ParentN, Node &ChildN,
223                                        Edge::Kind EK) {
224   // First insert it into the caller.
225   ParentN.insertEdgeInternal(ChildN, EK);
226 
227   assert(G->SCCMap.lookup(&ChildN) == this && "Child must be in this SCC.");
228 
229   SCC &ParentC = *G->SCCMap.lookup(&ParentN);
230   assert(&ParentC != this && "Parent must not be in this SCC.");
231   assert(ParentC.isDescendantOf(*this) &&
232          "Parent must be a descendant of the Child.");
233 
234   // The algorithm we use for merging SCCs based on the cycle introduced here
235   // is to walk the SCC inverted DAG formed by the parent SCC sets. The inverse
236   // graph has the same cycle properties as the actual DAG of the SCCs, and
237   // when forming SCCs lazily by a DFS, the bottom of the graph won't exist in
238   // many cases which should prune the search space.
239   //
240   // FIXME: We can get this pruning behavior even after the incremental SCC
241   // formation by leaving behind (conservative) DFS numberings in the nodes,
242   // and pruning the search with them. These would need to be cleverly updated
243   // during the removal of intra-SCC edges, but could be preserved
244   // conservatively.
245 
246   // The set of SCCs that are connected to the caller, and thus will
247   // participate in the merged connected component.
248   SmallPtrSet<SCC *, 8> ConnectedSCCs;
249   ConnectedSCCs.insert(this);
250   ConnectedSCCs.insert(&ParentC);
251 
252   // We build up a DFS stack of the parents chains.
253   SmallVector<std::pair<SCC *, SCC::parent_iterator>, 8> DFSSCCs;
254   SmallPtrSet<SCC *, 8> VisitedSCCs;
255   int ConnectedDepth = -1;
256   SCC *C = this;
257   parent_iterator I = parent_begin(), E = parent_end();
258   for (;;) {
259     while (I != E) {
260       SCC &ParentSCC = *I++;
261 
262       // If we have already processed this parent SCC, skip it, and remember
263       // whether it was connected so we don't have to check the rest of the
264       // stack. This also handles when we reach a child of the 'this' SCC (the
265       // callee) which terminates the search.
266       if (ConnectedSCCs.count(&ParentSCC)) {
267         ConnectedDepth = std::max<int>(ConnectedDepth, DFSSCCs.size());
268         continue;
269       }
270       if (VisitedSCCs.count(&ParentSCC))
271         continue;
272 
273       // We fully explore the depth-first space, adding nodes to the connected
274       // set only as we pop them off, so "recurse" by rotating to the parent.
275       DFSSCCs.push_back(std::make_pair(C, I));
276       C = &ParentSCC;
277       I = ParentSCC.parent_begin();
278       E = ParentSCC.parent_end();
279     }
280 
281     // If we've found a connection anywhere below this point on the stack (and
282     // thus up the parent graph from the caller), the current node needs to be
283     // added to the connected set now that we've processed all of its parents.
284     if ((int)DFSSCCs.size() == ConnectedDepth) {
285       --ConnectedDepth; // We're finished with this connection.
286       ConnectedSCCs.insert(C);
287     } else {
288       // Otherwise remember that its parents don't ever connect.
289       assert(ConnectedDepth < (int)DFSSCCs.size() &&
290              "Cannot have a connected depth greater than the DFS depth!");
291       VisitedSCCs.insert(C);
292     }
293 
294     if (DFSSCCs.empty())
295       break; // We've walked all the parents of the caller transitively.
296 
297     // Pop off the prior node and position to unwind the depth first recursion.
298     std::tie(C, I) = DFSSCCs.pop_back_val();
299     E = C->parent_end();
300   }
301 
302   // Now that we have identified all of the SCCs which need to be merged into
303   // a connected set with the inserted edge, merge all of them into this SCC.
304   // FIXME: This operation currently creates ordering stability problems
305   // because we don't use stably ordered containers for the parent SCCs or the
306   // connected SCCs.
307   unsigned NewNodeBeginIdx = Nodes.size();
308   for (SCC *C : ConnectedSCCs) {
309     if (C == this)
310       continue;
311     for (SCC *ParentC : C->ParentSCCs)
312       if (!ConnectedSCCs.count(ParentC))
313         ParentSCCs.insert(ParentC);
314     C->ParentSCCs.clear();
315 
316     for (Node *N : *C) {
317       for (Edge &E : *N) {
318         assert(E.getNode() && "Cannot have a null node within a visited SCC!");
319         SCC &ChildC = *G->SCCMap.lookup(E.getNode());
320         if (&ChildC != C)
321           ChildC.ParentSCCs.erase(C);
322       }
323       G->SCCMap[N] = this;
324       Nodes.push_back(N);
325     }
326     C->Nodes.clear();
327   }
328   for (auto I = Nodes.begin() + NewNodeBeginIdx, E = Nodes.end(); I != E; ++I)
329     for (Edge &E : **I) {
330       assert(E.getNode() && "Cannot have a null node within a visited SCC!");
331       SCC &ChildC = *G->SCCMap.lookup(E.getNode());
332       if (&ChildC != this)
333         ChildC.ParentSCCs.insert(this);
334     }
335 
336   // We return the list of SCCs which were merged so that callers can
337   // invalidate any data they have associated with those SCCs. Note that these
338   // SCCs are no longer in an interesting state (they are totally empty) but
339   // the pointers will remain stable for the life of the graph itself.
340   return SmallVector<SCC *, 1>(ConnectedSCCs.begin(), ConnectedSCCs.end());
341 }
342 
343 void LazyCallGraph::SCC::removeInterSCCEdge(Node &ParentN, Node &ChildN) {
344   // First remove it from the node.
345   ParentN.removeEdgeInternal(ChildN.getFunction());
346 
347   assert(G->SCCMap.lookup(&ParentN) == this &&
348          "The caller must be a member of this SCC.");
349 
350   SCC &ChildC = *G->SCCMap.lookup(&ChildN);
351   assert(&ChildC != this &&
352          "This API only supports the rmoval of inter-SCC edges.");
353 
354   assert(std::find(G->LeafSCCs.begin(), G->LeafSCCs.end(), this) ==
355              G->LeafSCCs.end() &&
356          "Cannot have a leaf SCC caller with a different SCC callee.");
357 
358   bool HasOtherEdgeToChildC = false;
359   bool HasOtherChildC = false;
360   for (Node *N : *this) {
361     for (Edge &E : *N) {
362       assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
363       SCC &OtherChildC = *G->SCCMap.lookup(E.getNode());
364       if (&OtherChildC == &ChildC) {
365         HasOtherEdgeToChildC = true;
366         break;
367       }
368       if (&OtherChildC != this)
369         HasOtherChildC = true;
370     }
371     if (HasOtherEdgeToChildC)
372       break;
373   }
374   // Because the SCCs form a DAG, deleting such an edge cannot change the set
375   // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
376   // the parent SCC no longer connected to the child SCC. If so, we need to
377   // update the child SCC's map of its parents.
378   if (!HasOtherEdgeToChildC) {
379     bool Removed = ChildC.ParentSCCs.erase(this);
380     (void)Removed;
381     assert(Removed &&
382            "Did not find the parent SCC in the child SCC's parent list!");
383 
384     // It may orphan an SCC if it is the last edge reaching it, but that does
385     // not violate any invariants of the graph.
386     if (ChildC.ParentSCCs.empty())
387       DEBUG(dbgs() << "LCG: Update removing " << ParentN.getFunction().getName()
388                    << " -> " << ChildN.getFunction().getName()
389                    << " edge orphaned the callee's SCC!\n");
390   }
391 
392   // It may make the Parent SCC a leaf SCC.
393   if (!HasOtherChildC)
394     G->LeafSCCs.push_back(this);
395 }
396 
397 void LazyCallGraph::SCC::internalDFS(
398     SmallVectorImpl<std::pair<Node *, Node::edge_iterator>> &DFSStack,
399     SmallVectorImpl<Node *> &PendingSCCStack, Node *N,
400     SmallVectorImpl<SCC *> &ResultSCCs) {
401   auto I = N->begin();
402   N->LowLink = N->DFSNumber = 1;
403   int NextDFSNumber = 2;
404   for (;;) {
405     assert(N->DFSNumber != 0 && "We should always assign a DFS number "
406                                 "before processing a node.");
407 
408     // We simulate recursion by popping out of the nested loop and continuing.
409     auto E = N->end();
410     while (I != E) {
411       Node &ChildN = I->getNode(*G);
412       if (SCC *ChildSCC = G->SCCMap.lookup(&ChildN)) {
413         // Check if we have reached a node in the new (known connected) set of
414         // this SCC. If so, the entire stack is necessarily in that set and we
415         // can re-start.
416         if (ChildSCC == this) {
417           insert(*N);
418           while (!PendingSCCStack.empty())
419             insert(*PendingSCCStack.pop_back_val());
420           while (!DFSStack.empty())
421             insert(*DFSStack.pop_back_val().first);
422           return;
423         }
424 
425         // If this child isn't currently in this SCC, no need to process it.
426         // However, we do need to remove this SCC from its SCC's parent set.
427         ChildSCC->ParentSCCs.erase(this);
428         ++I;
429         continue;
430       }
431 
432       if (ChildN.DFSNumber == 0) {
433         // Mark that we should start at this child when next this node is the
434         // top of the stack. We don't start at the next child to ensure this
435         // child's lowlink is reflected.
436         DFSStack.push_back(std::make_pair(N, I));
437 
438         // Continue, resetting to the child node.
439         ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
440         N = &ChildN;
441         I = ChildN.begin();
442         E = ChildN.end();
443         continue;
444       }
445 
446       // Track the lowest link of the children, if any are still in the stack.
447       // Any child not on the stack will have a LowLink of -1.
448       assert(ChildN.LowLink != 0 &&
449              "Low-link must not be zero with a non-zero DFS number.");
450       if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
451         N->LowLink = ChildN.LowLink;
452       ++I;
453     }
454 
455     if (N->LowLink == N->DFSNumber) {
456       ResultSCCs.push_back(G->formSCC(N, PendingSCCStack));
457       if (DFSStack.empty())
458         return;
459     } else {
460       // At this point we know that N cannot ever be an SCC root. Its low-link
461       // is not its dfs-number, and we've processed all of its children. It is
462       // just sitting here waiting until some node further down the stack gets
463       // low-link == dfs-number and pops it off as well. Move it to the pending
464       // stack which is pulled into the next SCC to be formed.
465       PendingSCCStack.push_back(N);
466 
467       assert(!DFSStack.empty() && "We shouldn't have an empty stack!");
468     }
469 
470     N = DFSStack.back().first;
471     I = DFSStack.back().second;
472     DFSStack.pop_back();
473   }
474 }
475 
476 SmallVector<LazyCallGraph::SCC *, 1>
477 LazyCallGraph::SCC::removeIntraSCCEdge(Node &ParentN, Node &ChildN) {
478   // First remove it from the node.
479   ParentN.removeEdgeInternal(ChildN.getFunction());
480 
481   // We return a list of the resulting *new* SCCs in postorder.
482   SmallVector<SCC *, 1> ResultSCCs;
483 
484   // Direct recursion doesn't impact the SCC graph at all.
485   if (&ParentN == &ChildN)
486     return ResultSCCs;
487 
488   // The worklist is every node in the original SCC.
489   SmallVector<Node *, 1> Worklist;
490   Worklist.swap(Nodes);
491   for (Node *N : Worklist) {
492     // The nodes formerly in this SCC are no longer in any SCC.
493     N->DFSNumber = 0;
494     N->LowLink = 0;
495     G->SCCMap.erase(N);
496   }
497   assert(Worklist.size() > 1 && "We have to have at least two nodes to have an "
498                                 "edge between them that is within the SCC.");
499 
500   // The child can already reach every node in this SCC (by definition). It is
501   // the only node we know will stay inside this SCC. Everything which
502   // transitively reaches Child will also remain in the SCC. To model this we
503   // incrementally add any chain of nodes which reaches something in the new
504   // node set to the new node set. This short circuits one side of the Tarjan's
505   // walk.
506   insert(ChildN);
507 
508   // We're going to do a full mini-Tarjan's walk using a local stack here.
509   SmallVector<std::pair<Node *, Node::edge_iterator>, 4> DFSStack;
510   SmallVector<Node *, 4> PendingSCCStack;
511   do {
512     Node *N = Worklist.pop_back_val();
513     if (N->DFSNumber == 0)
514       internalDFS(DFSStack, PendingSCCStack, N, ResultSCCs);
515 
516     assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
517     assert(PendingSCCStack.empty() && "Didn't flush all pending SCC nodes!");
518   } while (!Worklist.empty());
519 
520   // Now we need to reconnect the current SCC to the graph.
521   bool IsLeafSCC = true;
522   for (Node *N : Nodes) {
523     for (Edge &E : *N) {
524       assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
525       SCC &ChildSCC = *G->SCCMap.lookup(E.getNode());
526       if (&ChildSCC == this)
527         continue;
528       ChildSCC.ParentSCCs.insert(this);
529       IsLeafSCC = false;
530     }
531   }
532 #ifndef NDEBUG
533   if (!ResultSCCs.empty())
534     assert(!IsLeafSCC && "This SCC cannot be a leaf as we have split out new "
535                          "SCCs by removing this edge.");
536   if (!std::any_of(G->LeafSCCs.begin(), G->LeafSCCs.end(),
537                    [&](SCC *C) { return C == this; }))
538     assert(!IsLeafSCC && "This SCC cannot be a leaf as it already had child "
539                          "SCCs before we removed this edge.");
540 #endif
541   // If this SCC stopped being a leaf through this edge removal, remove it from
542   // the leaf SCC list.
543   if (!IsLeafSCC && !ResultSCCs.empty())
544     G->LeafSCCs.erase(std::remove(G->LeafSCCs.begin(), G->LeafSCCs.end(), this),
545                       G->LeafSCCs.end());
546 
547   // Return the new list of SCCs.
548   return ResultSCCs;
549 }
550 
551 void LazyCallGraph::insertEdge(Node &ParentN, Function &Child, Edge::Kind EK) {
552   assert(SCCMap.empty() && DFSStack.empty() &&
553          "This method cannot be called after SCCs have been formed!");
554 
555   return ParentN.insertEdgeInternal(Child, EK);
556 }
557 
558 void LazyCallGraph::removeEdge(Node &ParentN, Function &Child) {
559   assert(SCCMap.empty() && DFSStack.empty() &&
560          "This method cannot be called after SCCs have been formed!");
561 
562   return ParentN.removeEdgeInternal(Child);
563 }
564 
565 LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
566   return *new (MappedN = BPA.Allocate()) Node(*this, F);
567 }
568 
569 void LazyCallGraph::updateGraphPtrs() {
570   // Process all nodes updating the graph pointers.
571   {
572     SmallVector<Node *, 16> Worklist;
573     for (Edge &E : EntryEdges)
574       if (Node *EntryN = E.getNode())
575         Worklist.push_back(EntryN);
576 
577     while (!Worklist.empty()) {
578       Node *N = Worklist.pop_back_val();
579       N->G = this;
580       for (Edge &E : N->Edges)
581         if (Node *ChildN = E.getNode())
582           Worklist.push_back(ChildN);
583     }
584   }
585 
586   // Process all SCCs updating the graph pointers.
587   {
588     SmallVector<SCC *, 16> Worklist(LeafSCCs.begin(), LeafSCCs.end());
589 
590     while (!Worklist.empty()) {
591       SCC *C = Worklist.pop_back_val();
592       C->G = this;
593       Worklist.insert(Worklist.end(), C->ParentSCCs.begin(),
594                       C->ParentSCCs.end());
595     }
596   }
597 }
598 
599 LazyCallGraph::SCC *LazyCallGraph::formSCC(Node *RootN,
600                                            SmallVectorImpl<Node *> &NodeStack) {
601   // The tail of the stack is the new SCC. Allocate the SCC and pop the stack
602   // into it.
603   SCC *NewSCC = new (SCCBPA.Allocate()) SCC(*this);
604 
605   while (!NodeStack.empty() && NodeStack.back()->DFSNumber > RootN->DFSNumber) {
606     assert(NodeStack.back()->LowLink >= RootN->LowLink &&
607            "We cannot have a low link in an SCC lower than its root on the "
608            "stack!");
609     NewSCC->insert(*NodeStack.pop_back_val());
610   }
611   NewSCC->insert(*RootN);
612 
613   // A final pass over all edges in the SCC (this remains linear as we only
614   // do this once when we build the SCC) to connect it to the parent sets of
615   // its children.
616   bool IsLeafSCC = true;
617   for (Node *SCCN : NewSCC->Nodes)
618     for (Edge &E : *SCCN) {
619       assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
620       SCC &ChildSCC = *SCCMap.lookup(E.getNode());
621       if (&ChildSCC == NewSCC)
622         continue;
623       ChildSCC.ParentSCCs.insert(NewSCC);
624       IsLeafSCC = false;
625     }
626 
627   // For the SCCs where we fine no child SCCs, add them to the leaf list.
628   if (IsLeafSCC)
629     LeafSCCs.push_back(NewSCC);
630 
631   return NewSCC;
632 }
633 
634 LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() {
635   Node *N;
636   Node::edge_iterator I;
637   if (!DFSStack.empty()) {
638     N = DFSStack.back().first;
639     I = DFSStack.back().second;
640     DFSStack.pop_back();
641   } else {
642     // If we've handled all candidate entry nodes to the SCC forest, we're done.
643     do {
644       if (SCCEntryNodes.empty())
645         return nullptr;
646 
647       N = &get(*SCCEntryNodes.pop_back_val());
648     } while (N->DFSNumber != 0);
649     I = N->begin();
650     N->LowLink = N->DFSNumber = 1;
651     NextDFSNumber = 2;
652   }
653 
654   for (;;) {
655     assert(N->DFSNumber != 0 && "We should always assign a DFS number "
656                                 "before placing a node onto the stack.");
657 
658     auto E = N->end();
659     while (I != E) {
660       Node &ChildN = I->getNode(*this);
661       if (ChildN.DFSNumber == 0) {
662         // Mark that we should start at this child when next this node is the
663         // top of the stack. We don't start at the next child to ensure this
664         // child's lowlink is reflected.
665         DFSStack.push_back(std::make_pair(N, N->begin()));
666 
667         // Recurse onto this node via a tail call.
668         assert(!SCCMap.count(&ChildN) &&
669                "Found a node with 0 DFS number but already in an SCC!");
670         ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
671         N = &ChildN;
672         I = ChildN.begin();
673         E = ChildN.end();
674         continue;
675       }
676 
677       // Track the lowest link of the children, if any are still in the stack.
678       assert(ChildN.LowLink != 0 &&
679              "Low-link must not be zero with a non-zero DFS number.");
680       if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
681         N->LowLink = ChildN.LowLink;
682       ++I;
683     }
684 
685     if (N->LowLink == N->DFSNumber)
686       // Form the new SCC out of the top of the DFS stack.
687       return formSCC(N, PendingSCCStack);
688 
689     // At this point we know that N cannot ever be an SCC root. Its low-link
690     // is not its dfs-number, and we've processed all of its children. It is
691     // just sitting here waiting until some node further down the stack gets
692     // low-link == dfs-number and pops it off as well. Move it to the pending
693     // stack which is pulled into the next SCC to be formed.
694     PendingSCCStack.push_back(N);
695 
696     assert(!DFSStack.empty() && "We never found a viable root!");
697     N = DFSStack.back().first;
698     I = DFSStack.back().second;
699     DFSStack.pop_back();
700   }
701 }
702 
703 char LazyCallGraphAnalysis::PassID;
704 
705 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
706 
707 static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N,
708                        SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) {
709   LazyCallGraph &G = N.getGraph();
710 
711   // Recurse depth first through the nodes.
712   for (LazyCallGraph::Edge &E : N) {
713     LazyCallGraph::Node &ChildN = E.getNode(G);
714     if (Printed.insert(&ChildN).second)
715       printNodes(OS, ChildN, Printed);
716   }
717 
718   OS << "  Edges in function: " << N.getFunction().getName() << "\n";
719   for (const LazyCallGraph::Edge &E : N)
720     OS << "    " << (E.isCall() ? "call" : "ref ") << " -> "
721        << E.getFunction().getName() << "\n";
722 
723   OS << "\n";
724 }
725 
726 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &SCC) {
727   ptrdiff_t SCCSize = std::distance(SCC.begin(), SCC.end());
728   OS << "  SCC with " << SCCSize << " functions:\n";
729 
730   for (LazyCallGraph::Node *N : SCC)
731     OS << "    " << N->getFunction().getName() << "\n";
732 
733   OS << "\n";
734 }
735 
736 PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
737                                                 ModuleAnalysisManager *AM) {
738   LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
739 
740   OS << "Printing the call graph for module: " << M.getModuleIdentifier()
741      << "\n\n";
742 
743   SmallPtrSet<LazyCallGraph::Node *, 16> Printed;
744   for (LazyCallGraph::Edge &E : G) {
745     LazyCallGraph::Node &N = E.getNode(G);
746     if (Printed.insert(&N).second)
747       printNodes(OS, N, Printed);
748   }
749 
750   for (LazyCallGraph::SCC &SCC : G.postorder_sccs())
751     printSCC(OS, SCC);
752 
753   return PreservedAnalyses::all();
754 }
755