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/GraphWriter.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "lcg"
22 
23 static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
24                     DenseMap<Function *, int> &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({&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 LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
44     : G(&G), F(F), DFSNumber(0), LowLink(0) {
45   DEBUG(dbgs() << "  Adding functions called by '" << F.getName()
46                << "' to the graph.\n");
47 
48   SmallVector<Constant *, 16> Worklist;
49   SmallPtrSet<Function *, 4> Callees;
50   SmallPtrSet<Constant *, 16> Visited;
51 
52   // Find all the potential call graph edges in this function. We track both
53   // actual call edges and indirect references to functions. The direct calls
54   // are trivially added, but to accumulate the latter we walk the instructions
55   // and add every operand which is a constant to the worklist to process
56   // afterward.
57   for (BasicBlock &BB : F)
58     for (Instruction &I : BB) {
59       if (auto CS = CallSite(&I))
60         if (Function *Callee = CS.getCalledFunction())
61           if (Callees.insert(Callee).second) {
62             Visited.insert(Callee);
63             addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
64           }
65 
66       for (Value *Op : I.operand_values())
67         if (Constant *C = dyn_cast<Constant>(Op))
68           if (Visited.insert(C).second)
69             Worklist.push_back(C);
70     }
71 
72   // We've collected all the constant (and thus potentially function or
73   // function containing) operands to all of the instructions in the function.
74   // Process them (recursively) collecting every function found.
75   visitReferences(Worklist, Visited, [&](Function &F) {
76     addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref);
77   });
78 }
79 
80 void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
81   if (Node *N = G->lookup(Target))
82     return insertEdgeInternal(*N, EK);
83 
84   EdgeIndexMap.insert({&Target, Edges.size()});
85   Edges.emplace_back(Target, EK);
86 }
87 
88 void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
89   EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
90   Edges.emplace_back(TargetN, EK);
91 }
92 
93 void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
94   Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
95 }
96 
97 void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
98   auto IndexMapI = EdgeIndexMap.find(&Target);
99   assert(IndexMapI != EdgeIndexMap.end() &&
100          "Target not in the edge set for this caller?");
101 
102   Edges[IndexMapI->second] = Edge();
103   EdgeIndexMap.erase(IndexMapI);
104 }
105 
106 void LazyCallGraph::Node::dump() const {
107   dbgs() << *this << '\n';
108 }
109 
110 LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
111   DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
112                << "\n");
113   for (Function &F : M)
114     if (!F.isDeclaration() && !F.hasLocalLinkage())
115       if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
116         DEBUG(dbgs() << "  Adding '" << F.getName()
117                      << "' to entry set of the graph.\n");
118         EntryEdges.emplace_back(F, Edge::Ref);
119       }
120 
121   // Now add entry nodes for functions reachable via initializers to globals.
122   SmallVector<Constant *, 16> Worklist;
123   SmallPtrSet<Constant *, 16> Visited;
124   for (GlobalVariable &GV : M.globals())
125     if (GV.hasInitializer())
126       if (Visited.insert(GV.getInitializer()).second)
127         Worklist.push_back(GV.getInitializer());
128 
129   DEBUG(dbgs() << "  Adding functions referenced by global initializers to the "
130                   "entry set.\n");
131   visitReferences(Worklist, Visited, [&](Function &F) {
132     addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref);
133   });
134 
135   for (const Edge &E : EntryEdges)
136     RefSCCEntryNodes.push_back(&E.getFunction());
137 }
138 
139 LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
140     : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
141       EntryEdges(std::move(G.EntryEdges)),
142       EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
143       SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
144       DFSStack(std::move(G.DFSStack)),
145       RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
146       NextDFSNumber(G.NextDFSNumber) {
147   updateGraphPtrs();
148 }
149 
150 LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
151   BPA = std::move(G.BPA);
152   NodeMap = std::move(G.NodeMap);
153   EntryEdges = std::move(G.EntryEdges);
154   EntryIndexMap = std::move(G.EntryIndexMap);
155   SCCBPA = std::move(G.SCCBPA);
156   SCCMap = std::move(G.SCCMap);
157   LeafRefSCCs = std::move(G.LeafRefSCCs);
158   DFSStack = std::move(G.DFSStack);
159   RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
160   NextDFSNumber = G.NextDFSNumber;
161   updateGraphPtrs();
162   return *this;
163 }
164 
165 void LazyCallGraph::SCC::dump() const {
166   dbgs() << *this << '\n';
167 }
168 
169 #ifndef NDEBUG
170 void LazyCallGraph::SCC::verify() {
171   assert(OuterRefSCC && "Can't have a null RefSCC!");
172   assert(!Nodes.empty() && "Can't have an empty SCC!");
173 
174   for (Node *N : Nodes) {
175     assert(N && "Can't have a null node!");
176     assert(OuterRefSCC->G->lookupSCC(*N) == this &&
177            "Node does not map to this SCC!");
178     assert(N->DFSNumber == -1 &&
179            "Must set DFS numbers to -1 when adding a node to an SCC!");
180     assert(N->LowLink == -1 &&
181            "Must set low link to -1 when adding a node to an SCC!");
182     for (Edge &E : *N)
183       assert(E.getNode() && "Can't have an edge to a raw function!");
184   }
185 }
186 #endif
187 
188 LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
189 
190 void LazyCallGraph::RefSCC::dump() const {
191   dbgs() << *this << '\n';
192 }
193 
194 #ifndef NDEBUG
195 void LazyCallGraph::RefSCC::verify() {
196   assert(G && "Can't have a null graph!");
197   assert(!SCCs.empty() && "Can't have an empty SCC!");
198 
199   // Verify basic properties of the SCCs.
200   SmallPtrSet<SCC *, 4> SCCSet;
201   for (SCC *C : SCCs) {
202     assert(C && "Can't have a null SCC!");
203     C->verify();
204     assert(&C->getOuterRefSCC() == this &&
205            "SCC doesn't think it is inside this RefSCC!");
206     bool Inserted = SCCSet.insert(C).second;
207     assert(Inserted && "Found a duplicate SCC!");
208   }
209 
210   // Check that our indices map correctly.
211   for (auto &SCCIndexPair : SCCIndices) {
212     SCC *C = SCCIndexPair.first;
213     int i = SCCIndexPair.second;
214     assert(C && "Can't have a null SCC in the indices!");
215     assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
216     assert(SCCs[i] == C && "Index doesn't point to SCC!");
217   }
218 
219   // Check that the SCCs are in fact in post-order.
220   for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
221     SCC &SourceSCC = *SCCs[i];
222     for (Node &N : SourceSCC)
223       for (Edge &E : N) {
224         if (!E.isCall())
225           continue;
226         SCC &TargetSCC = *G->lookupSCC(*E.getNode());
227         if (&TargetSCC.getOuterRefSCC() == this) {
228           assert(SCCIndices.find(&TargetSCC)->second <= i &&
229                  "Edge between SCCs violates post-order relationship.");
230           continue;
231         }
232         assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
233                "Edge to a RefSCC missing us in its parent set.");
234       }
235   }
236 }
237 #endif
238 
239 bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
240   // Walk up the parents of this SCC and verify that we eventually find C.
241   SmallVector<const RefSCC *, 4> AncestorWorklist;
242   AncestorWorklist.push_back(this);
243   do {
244     const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
245     if (AncestorC->isChildOf(C))
246       return true;
247     for (const RefSCC *ParentC : AncestorC->Parents)
248       AncestorWorklist.push_back(ParentC);
249   } while (!AncestorWorklist.empty());
250 
251   return false;
252 }
253 
254 SmallVector<LazyCallGraph::SCC *, 1>
255 LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
256   assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
257 
258   SmallVector<SCC *, 1> DeletedSCCs;
259 
260   SCC &SourceSCC = *G->lookupSCC(SourceN);
261   SCC &TargetSCC = *G->lookupSCC(TargetN);
262 
263   // If the two nodes are already part of the same SCC, we're also done as
264   // we've just added more connectivity.
265   if (&SourceSCC == &TargetSCC) {
266     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
267 #ifndef NDEBUG
268     // Check that the RefSCC is still valid.
269     verify();
270 #endif
271     return DeletedSCCs;
272   }
273 
274   // At this point we leverage the postorder list of SCCs to detect when the
275   // insertion of an edge changes the SCC structure in any way.
276   //
277   // First and foremost, we can eliminate the need for any changes when the
278   // edge is toward the beginning of the postorder sequence because all edges
279   // flow in that direction already. Thus adding a new one cannot form a cycle.
280   int SourceIdx = SCCIndices[&SourceSCC];
281   int TargetIdx = SCCIndices[&TargetSCC];
282   if (TargetIdx < SourceIdx) {
283     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
284 #ifndef NDEBUG
285     // Check that the RefSCC is still valid.
286     verify();
287 #endif
288     return DeletedSCCs;
289   }
290 
291   // When we do have an edge from an earlier SCC to a later SCC in the
292   // postorder sequence, all of the SCCs which may be impacted are in the
293   // closed range of those two within the postorder sequence. The algorithm to
294   // restore the state is as follows:
295   //
296   // 1) Starting from the source SCC, construct a set of SCCs which reach the
297   //    source SCC consisting of just the source SCC. Then scan toward the
298   //    target SCC in postorder and for each SCC, if it has an edge to an SCC
299   //    in the set, add it to the set. Otherwise, the source SCC is not
300   //    a successor, move it in the postorder sequence to immediately before
301   //    the source SCC, shifting the source SCC and all SCCs in the set one
302   //    position toward the target SCC. Stop scanning after processing the
303   //    target SCC.
304   // 2) If the source SCC is now past the target SCC in the postorder sequence,
305   //    and thus the new edge will flow toward the start, we are done.
306   // 3) Otherwise, starting from the target SCC, walk all edges which reach an
307   //    SCC between the source and the target, and add them to the set of
308   //    connected SCCs, then recurse through them. Once a complete set of the
309   //    SCCs the target connects to is known, hoist the remaining SCCs between
310   //    the source and the target to be above the target. Note that there is no
311   //    need to process the source SCC, it is already known to connect.
312   // 4) At this point, all of the SCCs in the closed range between the source
313   //    SCC and the target SCC in the postorder sequence are connected,
314   //    including the target SCC and the source SCC. Inserting the edge from
315   //    the source SCC to the target SCC will form a cycle out of precisely
316   //    these SCCs. Thus we can merge all of the SCCs in this closed range into
317   //    a single SCC.
318   //
319   // This process has various important properties:
320   // - Only mutates the SCCs when adding the edge actually changes the SCC
321   //   structure.
322   // - Never mutates SCCs which are unaffected by the change.
323   // - Updates the postorder sequence to correctly satisfy the postorder
324   //   constraint after the edge is inserted.
325   // - Only reorders SCCs in the closed postorder sequence from the source to
326   //   the target, so easy to bound how much has changed even in the ordering.
327   // - Big-O is the number of edges in the closed postorder range of SCCs from
328   //   source to target.
329 
330   assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
331   SmallPtrSet<SCC *, 4> ConnectedSet;
332 
333   // Compute the SCCs which (transitively) reach the source.
334   ConnectedSet.insert(&SourceSCC);
335   auto IsConnected = [&](SCC &C) {
336     for (Node &N : C)
337       for (Edge &E : N.calls()) {
338         assert(E.getNode() && "Must have formed a node within an SCC!");
339         if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
340           return true;
341       }
342 
343     return false;
344   };
345 
346   for (SCC *C :
347        make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
348     if (IsConnected(*C))
349       ConnectedSet.insert(C);
350 
351   // Partition the SCCs in this part of the port-order sequence so only SCCs
352   // connecting to the source remain between it and the target. This is
353   // a benign partition as it preserves postorder.
354   auto SourceI = std::stable_partition(
355       SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
356       [&ConnectedSet](SCC *C) { return !ConnectedSet.count(C); });
357   for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
358     SCCIndices.find(SCCs[i])->second = i;
359 
360   // If the target doesn't connect to the source, then we've corrected the
361   // post-order and there are no cycles formed.
362   if (!ConnectedSet.count(&TargetSCC)) {
363     assert(SourceI > (SCCs.begin() + SourceIdx) &&
364            "Must have moved the source to fix the post-order.");
365     assert(*std::prev(SourceI) == &TargetSCC &&
366            "Last SCC to move should have bene the target.");
367     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
368 #ifndef NDEBUG
369     verify();
370 #endif
371     return DeletedSCCs;
372   }
373 
374   assert(SCCs[TargetIdx] == &TargetSCC &&
375          "Should not have moved target if connected!");
376   SourceIdx = SourceI - SCCs.begin();
377 
378 #ifndef NDEBUG
379   // Check that the RefSCC is still valid.
380   verify();
381 #endif
382 
383   // See whether there are any remaining intervening SCCs between the source
384   // and target. If so we need to make sure they all are reachable form the
385   // target.
386   if (SourceIdx + 1 < TargetIdx) {
387     // Use a normal worklist to find which SCCs the target connects to. We still
388     // bound the search based on the range in the postorder list we care about,
389     // but because this is forward connectivity we just "recurse" through the
390     // edges.
391     ConnectedSet.clear();
392     ConnectedSet.insert(&TargetSCC);
393     SmallVector<SCC *, 4> Worklist;
394     Worklist.push_back(&TargetSCC);
395     do {
396       SCC &C = *Worklist.pop_back_val();
397       for (Node &N : C)
398         for (Edge &E : N) {
399           assert(E.getNode() && "Must have formed a node within an SCC!");
400           if (!E.isCall())
401             continue;
402           SCC &EdgeC = *G->lookupSCC(*E.getNode());
403           if (&EdgeC.getOuterRefSCC() != this)
404             // Not in this RefSCC...
405             continue;
406           if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
407             // Not in the postorder sequence between source and target.
408             continue;
409 
410           if (ConnectedSet.insert(&EdgeC).second)
411             Worklist.push_back(&EdgeC);
412         }
413     } while (!Worklist.empty());
414 
415     // Partition SCCs so that only SCCs reached from the target remain between
416     // the source and the target. This preserves postorder.
417     auto TargetI = std::stable_partition(
418         SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
419         [&ConnectedSet](SCC *C) { return ConnectedSet.count(C); });
420     for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
421       SCCIndices.find(SCCs[i])->second = i;
422     TargetIdx = std::prev(TargetI) - SCCs.begin();
423     assert(SCCs[TargetIdx] == &TargetSCC &&
424            "Should always end with the target!");
425 
426 #ifndef NDEBUG
427     // Check that the RefSCC is still valid.
428     verify();
429 #endif
430   }
431 
432   // At this point, we know that connecting source to target forms a cycle
433   // because target connects back to source, and we know that all of the SCCs
434   // between the source and target in the postorder sequence participate in that
435   // cycle. This means that we need to merge all of these SCCs into a single
436   // result SCC.
437   //
438   // NB: We merge into the target because all of these functions were already
439   // reachable from the target, meaning any SCC-wide properties deduced about it
440   // other than the set of functions within it will not have changed.
441   auto MergeRange =
442       make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
443   for (SCC *C : MergeRange) {
444     assert(C != &TargetSCC &&
445            "We merge *into* the target and shouldn't process it here!");
446     SCCIndices.erase(C);
447     TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
448     for (Node *N : C->Nodes)
449       G->SCCMap[N] = &TargetSCC;
450     C->clear();
451     DeletedSCCs.push_back(C);
452   }
453 
454   // Erase the merged SCCs from the list and update the indices of the
455   // remaining SCCs.
456   int IndexOffset = MergeRange.end() - MergeRange.begin();
457   auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
458   for (SCC *C : make_range(EraseEnd, SCCs.end()))
459     SCCIndices[C] -= IndexOffset;
460 
461   // Now that the SCC structure is finalized, flip the kind to call.
462   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
463 
464 #ifndef NDEBUG
465   // And we're done! Verify in debug builds that the RefSCC is coherent.
466   verify();
467 #endif
468   return DeletedSCCs;
469 }
470 
471 iterator_range<LazyCallGraph::RefSCC::iterator>
472 LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
473   assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
474 
475   SCC &SourceSCC = *G->lookupSCC(SourceN);
476   SCC &TargetSCC = *G->lookupSCC(TargetN);
477 
478   assert(&SourceSCC.getOuterRefSCC() == this &&
479          "Source must be in this RefSCC.");
480   assert(&TargetSCC.getOuterRefSCC() == this &&
481          "Target must be in this RefSCC.");
482 
483   // Set the edge kind.
484   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
485 
486   // If this call edge is just connecting two separate SCCs within this RefSCC,
487   // there is nothing to do.
488   if (&SourceSCC != &TargetSCC) {
489 #ifndef NDEBUG
490     // Check that the RefSCC is still valid.
491     verify();
492 #endif
493     return make_range(SCCs.end(), SCCs.end());
494   }
495 
496   // Otherwise we are removing a call edge from a single SCC. This may break
497   // the cycle. In order to compute the new set of SCCs, we need to do a small
498   // DFS over the nodes within the SCC to form any sub-cycles that remain as
499   // distinct SCCs and compute a postorder over the resulting SCCs.
500   //
501   // However, we specially handle the target node. The target node is known to
502   // reach all other nodes in the original SCC by definition. This means that
503   // we want the old SCC to be replaced with an SCC contaning that node as it
504   // will be the root of whatever SCC DAG results from the DFS. Assumptions
505   // about an SCC such as the set of functions called will continue to hold,
506   // etc.
507 
508   SCC &OldSCC = TargetSCC;
509   SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
510   SmallVector<Node *, 16> PendingSCCStack;
511   SmallVector<SCC *, 4> NewSCCs;
512 
513   // Prepare the nodes for a fresh DFS.
514   SmallVector<Node *, 16> Worklist;
515   Worklist.swap(OldSCC.Nodes);
516   for (Node *N : Worklist) {
517     N->DFSNumber = N->LowLink = 0;
518     G->SCCMap.erase(N);
519   }
520 
521   // Force the target node to be in the old SCC. This also enables us to take
522   // a very significant short-cut in the standard Tarjan walk to re-form SCCs
523   // below: whenever we build an edge that reaches the target node, we know
524   // that the target node eventually connects back to all other nodes in our
525   // walk. As a consequence, we can detect and handle participants in that
526   // cycle without walking all the edges that form this connection, and instead
527   // by relying on the fundamental guarantee coming into this operation (all
528   // nodes are reachable from the target due to previously forming an SCC).
529   TargetN.DFSNumber = TargetN.LowLink = -1;
530   OldSCC.Nodes.push_back(&TargetN);
531   G->SCCMap[&TargetN] = &OldSCC;
532 
533   // Scan down the stack and DFS across the call edges.
534   for (Node *RootN : Worklist) {
535     assert(DFSStack.empty() &&
536            "Cannot begin a new root with a non-empty DFS stack!");
537     assert(PendingSCCStack.empty() &&
538            "Cannot begin a new root with pending nodes for an SCC!");
539 
540     // Skip any nodes we've already reached in the DFS.
541     if (RootN->DFSNumber != 0) {
542       assert(RootN->DFSNumber == -1 &&
543              "Shouldn't have any mid-DFS root nodes!");
544       continue;
545     }
546 
547     RootN->DFSNumber = RootN->LowLink = 1;
548     int NextDFSNumber = 2;
549 
550     DFSStack.push_back({RootN, RootN->call_begin()});
551     do {
552       Node *N;
553       call_edge_iterator I;
554       std::tie(N, I) = DFSStack.pop_back_val();
555       auto E = N->call_end();
556       while (I != E) {
557         Node &ChildN = *I->getNode();
558         if (ChildN.DFSNumber == 0) {
559           // We haven't yet visited this child, so descend, pushing the current
560           // node onto the stack.
561           DFSStack.push_back({N, I});
562 
563           assert(!G->SCCMap.count(&ChildN) &&
564                  "Found a node with 0 DFS number but already in an SCC!");
565           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
566           N = &ChildN;
567           I = N->call_begin();
568           E = N->call_end();
569           continue;
570         }
571 
572         // Check for the child already being part of some component.
573         if (ChildN.DFSNumber == -1) {
574           if (G->lookupSCC(ChildN) == &OldSCC) {
575             // If the child is part of the old SCC, we know that it can reach
576             // every other node, so we have formed a cycle. Pull the entire DFS
577             // and pending stacks into it. See the comment above about setting
578             // up the old SCC for why we do this.
579             int OldSize = OldSCC.size();
580             OldSCC.Nodes.push_back(N);
581             OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
582             PendingSCCStack.clear();
583             while (!DFSStack.empty())
584               OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
585             for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
586               N.DFSNumber = N.LowLink = -1;
587               G->SCCMap[&N] = &OldSCC;
588             }
589             N = nullptr;
590             break;
591           }
592 
593           // If the child has already been added to some child component, it
594           // couldn't impact the low-link of this parent because it isn't
595           // connected, and thus its low-link isn't relevant so skip it.
596           ++I;
597           continue;
598         }
599 
600         // Track the lowest linked child as the lowest link for this node.
601         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
602         if (ChildN.LowLink < N->LowLink)
603           N->LowLink = ChildN.LowLink;
604 
605         // Move to the next edge.
606         ++I;
607       }
608       if (!N)
609         // Cleared the DFS early, start another round.
610         break;
611 
612       // We've finished processing N and its descendents, put it on our pending
613       // SCC stack to eventually get merged into an SCC of nodes.
614       PendingSCCStack.push_back(N);
615 
616       // If this node is linked to some lower entry, continue walking up the
617       // stack.
618       if (N->LowLink != N->DFSNumber)
619         continue;
620 
621       // Otherwise, we've completed an SCC. Append it to our post order list of
622       // SCCs.
623       int RootDFSNumber = N->DFSNumber;
624       // Find the range of the node stack by walking down until we pass the
625       // root DFS number.
626       auto SCCNodes = make_range(
627           PendingSCCStack.rbegin(),
628           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
629             return N->DFSNumber < RootDFSNumber;
630           }));
631 
632       // Form a new SCC out of these nodes and then clear them off our pending
633       // stack.
634       NewSCCs.push_back(G->createSCC(*this, SCCNodes));
635       for (Node &N : *NewSCCs.back()) {
636         N.DFSNumber = N.LowLink = -1;
637         G->SCCMap[&N] = NewSCCs.back();
638       }
639       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
640     } while (!DFSStack.empty());
641   }
642 
643   // Insert the remaining SCCs before the old one. The old SCC can reach all
644   // other SCCs we form because it contains the target node of the removed edge
645   // of the old SCC. This means that we will have edges into all of the new
646   // SCCs, which means the old one must come last for postorder.
647   int OldIdx = SCCIndices[&OldSCC];
648   SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
649 
650   // Update the mapping from SCC* to index to use the new SCC*s, and remove the
651   // old SCC from the mapping.
652   for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
653     SCCIndices[SCCs[Idx]] = Idx;
654 
655 #ifndef NDEBUG
656   // We're done. Check the validity on our way out.
657   verify();
658 #endif
659 
660   return make_range(SCCs.begin() + OldIdx,
661                     SCCs.begin() + OldIdx + NewSCCs.size());
662 }
663 
664 void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
665                                                      Node &TargetN) {
666   assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
667 
668   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
669   assert(G->lookupRefSCC(TargetN) != this &&
670          "Target must not be in this RefSCC.");
671   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
672          "Target must be a descendant of the Source.");
673 
674   // Edges between RefSCCs are the same regardless of call or ref, so we can
675   // just flip the edge here.
676   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
677 
678 #ifndef NDEBUG
679   // Check that the RefSCC is still valid.
680   verify();
681 #endif
682 }
683 
684 void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
685                                                     Node &TargetN) {
686   assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
687 
688   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
689   assert(G->lookupRefSCC(TargetN) != this &&
690          "Target must not be in this RefSCC.");
691   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
692          "Target must be a descendant of the Source.");
693 
694   // Edges between RefSCCs are the same regardless of call or ref, so we can
695   // just flip the edge here.
696   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
697 
698 #ifndef NDEBUG
699   // Check that the RefSCC is still valid.
700   verify();
701 #endif
702 }
703 
704 void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
705                                                   Node &TargetN) {
706   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
707   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
708 
709   SourceN.insertEdgeInternal(TargetN, Edge::Ref);
710 
711 #ifndef NDEBUG
712   // Check that the RefSCC is still valid.
713   verify();
714 #endif
715 }
716 
717 void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
718                                                Edge::Kind EK) {
719   // First insert it into the caller.
720   SourceN.insertEdgeInternal(TargetN, EK);
721 
722   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
723 
724   RefSCC &TargetC = *G->lookupRefSCC(TargetN);
725   assert(&TargetC != this && "Target must not be in this RefSCC.");
726   assert(TargetC.isDescendantOf(*this) &&
727          "Target must be a descendant of the Source.");
728 
729   // The only change required is to add this SCC to the parent set of the
730   // callee.
731   TargetC.Parents.insert(this);
732 
733 #ifndef NDEBUG
734   // Check that the RefSCC is still valid.
735   verify();
736 #endif
737 }
738 
739 SmallVector<LazyCallGraph::RefSCC *, 1>
740 LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
741   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this SCC.");
742 
743   // We store the RefSCCs found to be connected in postorder so that we can use
744   // that when merging. We also return this to the caller to allow them to
745   // invalidate information pertaining to these RefSCCs.
746   SmallVector<RefSCC *, 1> Connected;
747 
748   RefSCC &SourceC = *G->lookupRefSCC(SourceN);
749   assert(&SourceC != this && "Source must not be in this SCC.");
750   assert(SourceC.isDescendantOf(*this) &&
751          "Source must be a descendant of the Target.");
752 
753   // The algorithm we use for merging SCCs based on the cycle introduced here
754   // is to walk the RefSCC inverted DAG formed by the parent sets. The inverse
755   // graph has the same cycle properties as the actual DAG of the RefSCCs, and
756   // when forming RefSCCs lazily by a DFS, the bottom of the graph won't exist
757   // in many cases which should prune the search space.
758   //
759   // FIXME: We can get this pruning behavior even after the incremental RefSCC
760   // formation by leaving behind (conservative) DFS numberings in the nodes,
761   // and pruning the search with them. These would need to be cleverly updated
762   // during the removal of intra-SCC edges, but could be preserved
763   // conservatively.
764   //
765   // FIXME: This operation currently creates ordering stability problems
766   // because we don't use stably ordered containers for the parent SCCs.
767 
768   // The set of RefSCCs that are connected to the parent, and thus will
769   // participate in the merged connected component.
770   SmallPtrSet<RefSCC *, 8> ConnectedSet;
771   ConnectedSet.insert(this);
772 
773   // We build up a DFS stack of the parents chains.
774   SmallVector<std::pair<RefSCC *, parent_iterator>, 8> DFSStack;
775   SmallPtrSet<RefSCC *, 8> Visited;
776   int ConnectedDepth = -1;
777   DFSStack.push_back({&SourceC, SourceC.parent_begin()});
778   do {
779     auto DFSPair = DFSStack.pop_back_val();
780     RefSCC *C = DFSPair.first;
781     parent_iterator I = DFSPair.second;
782     auto E = C->parent_end();
783 
784     while (I != E) {
785       RefSCC &Parent = *I++;
786 
787       // If we have already processed this parent SCC, skip it, and remember
788       // whether it was connected so we don't have to check the rest of the
789       // stack. This also handles when we reach a child of the 'this' SCC (the
790       // callee) which terminates the search.
791       if (ConnectedSet.count(&Parent)) {
792         assert(ConnectedDepth < (int)DFSStack.size() &&
793                "Cannot have a connected depth greater than the DFS depth!");
794         ConnectedDepth = DFSStack.size();
795         continue;
796       }
797       if (Visited.count(&Parent))
798         continue;
799 
800       // We fully explore the depth-first space, adding nodes to the connected
801       // set only as we pop them off, so "recurse" by rotating to the parent.
802       DFSStack.push_back({C, I});
803       C = &Parent;
804       I = C->parent_begin();
805       E = C->parent_end();
806     }
807 
808     // If we've found a connection anywhere below this point on the stack (and
809     // thus up the parent graph from the caller), the current node needs to be
810     // added to the connected set now that we've processed all of its parents.
811     if ((int)DFSStack.size() == ConnectedDepth) {
812       --ConnectedDepth; // We're finished with this connection.
813       bool Inserted = ConnectedSet.insert(C).second;
814       (void)Inserted;
815       assert(Inserted && "Cannot insert a refSCC multiple times!");
816       Connected.push_back(C);
817     } else {
818       // Otherwise remember that its parents don't ever connect.
819       assert(ConnectedDepth < (int)DFSStack.size() &&
820              "Cannot have a connected depth greater than the DFS depth!");
821       Visited.insert(C);
822     }
823   } while (!DFSStack.empty());
824 
825   // Now that we have identified all of the SCCs which need to be merged into
826   // a connected set with the inserted edge, merge all of them into this SCC.
827   // We walk the newly connected RefSCCs in the reverse postorder of the parent
828   // DAG walk above and merge in each of their SCC postorder lists. This
829   // ensures a merged postorder SCC list.
830   SmallVector<SCC *, 16> MergedSCCs;
831   int SCCIndex = 0;
832   for (RefSCC *C : reverse(Connected)) {
833     assert(C != this &&
834            "This RefSCC should terminate the DFS without being reached.");
835 
836     // Merge the parents which aren't part of the merge into the our parents.
837     for (RefSCC *ParentC : C->Parents)
838       if (!ConnectedSet.count(ParentC))
839         Parents.insert(ParentC);
840     C->Parents.clear();
841 
842     // Walk the inner SCCs to update their up-pointer and walk all the edges to
843     // update any parent sets.
844     // FIXME: We should try to find a way to avoid this (rather expensive) edge
845     // walk by updating the parent sets in some other manner.
846     for (SCC &InnerC : *C) {
847       InnerC.OuterRefSCC = this;
848       SCCIndices[&InnerC] = SCCIndex++;
849       for (Node &N : InnerC) {
850         G->SCCMap[&N] = &InnerC;
851         for (Edge &E : N) {
852           assert(E.getNode() &&
853                  "Cannot have a null node within a visited SCC!");
854           RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
855           if (ConnectedSet.count(&ChildRC))
856             continue;
857           ChildRC.Parents.erase(C);
858           ChildRC.Parents.insert(this);
859         }
860       }
861     }
862 
863     // Now merge in the SCCs. We can actually move here so try to reuse storage
864     // the first time through.
865     if (MergedSCCs.empty())
866       MergedSCCs = std::move(C->SCCs);
867     else
868       MergedSCCs.append(C->SCCs.begin(), C->SCCs.end());
869     C->SCCs.clear();
870   }
871 
872   // Finally append our original SCCs to the merged list and move it into
873   // place.
874   for (SCC &InnerC : *this)
875     SCCIndices[&InnerC] = SCCIndex++;
876   MergedSCCs.append(SCCs.begin(), SCCs.end());
877   SCCs = std::move(MergedSCCs);
878 
879   // At this point we have a merged RefSCC with a post-order SCCs list, just
880   // connect the nodes to form the new edge.
881   SourceN.insertEdgeInternal(TargetN, Edge::Ref);
882 
883 #ifndef NDEBUG
884   // Check that the RefSCC is still valid.
885   verify();
886 #endif
887 
888   // We return the list of SCCs which were merged so that callers can
889   // invalidate any data they have associated with those SCCs. Note that these
890   // SCCs are no longer in an interesting state (they are totally empty) but
891   // the pointers will remain stable for the life of the graph itself.
892   return Connected;
893 }
894 
895 void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
896   assert(G->lookupRefSCC(SourceN) == this &&
897          "The source must be a member of this RefSCC.");
898 
899   RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
900   assert(&TargetRC != this && "The target must not be a member of this RefSCC");
901 
902   assert(!is_contained(G->LeafRefSCCs, this) &&
903          "Cannot have a leaf RefSCC source.");
904 
905   // First remove it from the node.
906   SourceN.removeEdgeInternal(TargetN.getFunction());
907 
908   bool HasOtherEdgeToChildRC = false;
909   bool HasOtherChildRC = false;
910   for (SCC *InnerC : SCCs) {
911     for (Node &N : *InnerC) {
912       for (Edge &E : N) {
913         assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
914         RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
915         if (&OtherChildRC == &TargetRC) {
916           HasOtherEdgeToChildRC = true;
917           break;
918         }
919         if (&OtherChildRC != this)
920           HasOtherChildRC = true;
921       }
922       if (HasOtherEdgeToChildRC)
923         break;
924     }
925     if (HasOtherEdgeToChildRC)
926       break;
927   }
928   // Because the SCCs form a DAG, deleting such an edge cannot change the set
929   // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
930   // the source SCC no longer connected to the target SCC. If so, we need to
931   // update the target SCC's map of its parents.
932   if (!HasOtherEdgeToChildRC) {
933     bool Removed = TargetRC.Parents.erase(this);
934     (void)Removed;
935     assert(Removed &&
936            "Did not find the source SCC in the target SCC's parent list!");
937 
938     // It may orphan an SCC if it is the last edge reaching it, but that does
939     // not violate any invariants of the graph.
940     if (TargetRC.Parents.empty())
941       DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
942                    << " -> " << TargetN.getFunction().getName()
943                    << " edge orphaned the callee's SCC!\n");
944 
945     // It may make the Source SCC a leaf SCC.
946     if (!HasOtherChildRC)
947       G->LeafRefSCCs.push_back(this);
948   }
949 }
950 
951 SmallVector<LazyCallGraph::RefSCC *, 1>
952 LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
953   assert(!SourceN[TargetN].isCall() &&
954          "Cannot remove a call edge, it must first be made a ref edge");
955 
956   // First remove the actual edge.
957   SourceN.removeEdgeInternal(TargetN.getFunction());
958 
959   // We return a list of the resulting *new* RefSCCs in post-order.
960   SmallVector<RefSCC *, 1> Result;
961 
962   // Direct recursion doesn't impact the SCC graph at all.
963   if (&SourceN == &TargetN)
964     return Result;
965 
966   // We build somewhat synthetic new RefSCCs by providing a postorder mapping
967   // for each inner SCC. We also store these associated with *nodes* rather
968   // than SCCs because this saves a round-trip through the node->SCC map and in
969   // the common case, SCCs are small. We will verify that we always give the
970   // same number to every node in the SCC such that these are equivalent.
971   const int RootPostOrderNumber = 0;
972   int PostOrderNumber = RootPostOrderNumber + 1;
973   SmallDenseMap<Node *, int> PostOrderMapping;
974 
975   // Every node in the target SCC can already reach every node in this RefSCC
976   // (by definition). It is the only node we know will stay inside this RefSCC.
977   // Everything which transitively reaches Target will also remain in the
978   // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
979   // back to the root post order number.
980   //
981   // This also enables us to take a very significant short-cut in the standard
982   // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
983   // references the target node, we know that the target node eventually
984   // references all other nodes in our walk. As a consequence, we can detect
985   // and handle participants in that cycle without walking all the edges that
986   // form the connections, and instead by relying on the fundamental guarantee
987   // coming into this operation.
988   SCC &TargetC = *G->lookupSCC(TargetN);
989   for (Node &N : TargetC)
990     PostOrderMapping[&N] = RootPostOrderNumber;
991 
992   // Reset all the other nodes to prepare for a DFS over them, and add them to
993   // our worklist.
994   SmallVector<Node *, 8> Worklist;
995   for (SCC *C : SCCs) {
996     if (C == &TargetC)
997       continue;
998 
999     for (Node &N : *C)
1000       N.DFSNumber = N.LowLink = 0;
1001 
1002     Worklist.append(C->Nodes.begin(), C->Nodes.end());
1003   }
1004 
1005   auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1006     N.DFSNumber = N.LowLink = -1;
1007     PostOrderMapping[&N] = Number;
1008   };
1009 
1010   SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1011   SmallVector<Node *, 4> PendingRefSCCStack;
1012   do {
1013     assert(DFSStack.empty() &&
1014            "Cannot begin a new root with a non-empty DFS stack!");
1015     assert(PendingRefSCCStack.empty() &&
1016            "Cannot begin a new root with pending nodes for an SCC!");
1017 
1018     Node *RootN = Worklist.pop_back_val();
1019     // Skip any nodes we've already reached in the DFS.
1020     if (RootN->DFSNumber != 0) {
1021       assert(RootN->DFSNumber == -1 &&
1022              "Shouldn't have any mid-DFS root nodes!");
1023       continue;
1024     }
1025 
1026     RootN->DFSNumber = RootN->LowLink = 1;
1027     int NextDFSNumber = 2;
1028 
1029     DFSStack.push_back({RootN, RootN->begin()});
1030     do {
1031       Node *N;
1032       edge_iterator I;
1033       std::tie(N, I) = DFSStack.pop_back_val();
1034       auto E = N->end();
1035 
1036       assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1037                                   "before processing a node.");
1038 
1039       while (I != E) {
1040         Node &ChildN = I->getNode(*G);
1041         if (ChildN.DFSNumber == 0) {
1042           // Mark that we should start at this child when next this node is the
1043           // top of the stack. We don't start at the next child to ensure this
1044           // child's lowlink is reflected.
1045           DFSStack.push_back({N, I});
1046 
1047           // Continue, resetting to the child node.
1048           ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1049           N = &ChildN;
1050           I = ChildN.begin();
1051           E = ChildN.end();
1052           continue;
1053         }
1054         if (ChildN.DFSNumber == -1) {
1055           // Check if this edge's target node connects to the deleted edge's
1056           // target node. If so, we know that every node connected will end up
1057           // in this RefSCC, so collapse the entire current stack into the root
1058           // slot in our SCC numbering. See above for the motivation of
1059           // optimizing the target connected nodes in this way.
1060           auto PostOrderI = PostOrderMapping.find(&ChildN);
1061           if (PostOrderI != PostOrderMapping.end() &&
1062               PostOrderI->second == RootPostOrderNumber) {
1063             MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1064             while (!PendingRefSCCStack.empty())
1065               MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1066                                    RootPostOrderNumber);
1067             while (!DFSStack.empty())
1068               MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1069                                    RootPostOrderNumber);
1070             // Ensure we break all the way out of the enclosing loop.
1071             N = nullptr;
1072             break;
1073           }
1074 
1075           // If this child isn't currently in this RefSCC, no need to process
1076           // it.
1077           // However, we do need to remove this RefSCC from its RefSCC's parent
1078           // set.
1079           RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1080           ChildRC.Parents.erase(this);
1081           ++I;
1082           continue;
1083         }
1084 
1085         // Track the lowest link of the children, if any are still in the stack.
1086         // Any child not on the stack will have a LowLink of -1.
1087         assert(ChildN.LowLink != 0 &&
1088                "Low-link must not be zero with a non-zero DFS number.");
1089         if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1090           N->LowLink = ChildN.LowLink;
1091         ++I;
1092       }
1093       if (!N)
1094         // We short-circuited this node.
1095         break;
1096 
1097       // We've finished processing N and its descendents, put it on our pending
1098       // stack to eventually get merged into a RefSCC.
1099       PendingRefSCCStack.push_back(N);
1100 
1101       // If this node is linked to some lower entry, continue walking up the
1102       // stack.
1103       if (N->LowLink != N->DFSNumber) {
1104         assert(!DFSStack.empty() &&
1105                "We never found a viable root for a RefSCC to pop off!");
1106         continue;
1107       }
1108 
1109       // Otherwise, form a new RefSCC from the top of the pending node stack.
1110       int RootDFSNumber = N->DFSNumber;
1111       // Find the range of the node stack by walking down until we pass the
1112       // root DFS number.
1113       auto RefSCCNodes = make_range(
1114           PendingRefSCCStack.rbegin(),
1115           find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1116             return N->DFSNumber < RootDFSNumber;
1117           }));
1118 
1119       // Mark the postorder number for these nodes and clear them off the
1120       // stack. We'll use the postorder number to pull them into RefSCCs at the
1121       // end. FIXME: Fuse with the loop above.
1122       int RefSCCNumber = PostOrderNumber++;
1123       for (Node *N : RefSCCNodes)
1124         MarkNodeForSCCNumber(*N, RefSCCNumber);
1125 
1126       PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1127                                PendingRefSCCStack.end());
1128     } while (!DFSStack.empty());
1129 
1130     assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
1131     assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
1132   } while (!Worklist.empty());
1133 
1134   // We now have a post-order numbering for RefSCCs and a mapping from each
1135   // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1136   // (re-using this RefSCC node for the root) and build a radix-sort style map
1137   // from postorder number to the RefSCC. We then append SCCs to each of these
1138   // RefSCCs in the order they occured in the original SCCs container.
1139   for (int i = 1; i < PostOrderNumber; ++i)
1140     Result.push_back(G->createRefSCC(*G));
1141 
1142   for (SCC *C : SCCs) {
1143     auto PostOrderI = PostOrderMapping.find(&*C->begin());
1144     assert(PostOrderI != PostOrderMapping.end() &&
1145            "Cannot have missing mappings for nodes!");
1146     int SCCNumber = PostOrderI->second;
1147 #ifndef NDEBUG
1148     for (Node &N : *C)
1149       assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1150              "Cannot have different numbers for nodes in the same SCC!");
1151 #endif
1152     if (SCCNumber == 0)
1153       // The root node is handled separately by removing the SCCs.
1154       continue;
1155 
1156     RefSCC &RC = *Result[SCCNumber - 1];
1157     int SCCIndex = RC.SCCs.size();
1158     RC.SCCs.push_back(C);
1159     SCCIndices[C] = SCCIndex;
1160     C->OuterRefSCC = &RC;
1161   }
1162 
1163   // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1164   // a leaf and connect it to the rest of the graph's parents lists. This is
1165   // really wasteful. We should instead do this during the DFS to avoid yet
1166   // another edge walk.
1167   for (RefSCC *RC : Result)
1168     G->connectRefSCC(*RC);
1169 
1170   // Now erase all but the root's SCCs.
1171   SCCs.erase(remove_if(SCCs,
1172                        [&](SCC *C) {
1173                          return PostOrderMapping.lookup(&*C->begin()) !=
1174                                 RootPostOrderNumber;
1175                        }),
1176              SCCs.end());
1177   SCCIndices.clear();
1178   for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1179     SCCIndices[SCCs[i]] = i;
1180 
1181 #ifndef NDEBUG
1182   // Now we need to reconnect the current (root) SCC to the graph. We do this
1183   // manually because we can special case our leaf handling and detect errors.
1184   bool IsLeaf = true;
1185 #endif
1186   for (SCC *C : SCCs)
1187     for (Node &N : *C) {
1188       for (Edge &E : N) {
1189         assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1190         RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1191         if (&ChildRC == this)
1192           continue;
1193         ChildRC.Parents.insert(this);
1194 #ifndef NDEBUG
1195         IsLeaf = false;
1196 #endif
1197       }
1198     }
1199 #ifndef NDEBUG
1200   if (!Result.empty())
1201     assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1202                       "SCCs by removing this edge.");
1203   if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
1204     assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1205                       "SCCs before we removed this edge.");
1206 #endif
1207   // If this SCC stopped being a leaf through this edge removal, remove it from
1208   // the leaf SCC list. Note that this DTRT in the case where this was never
1209   // a leaf.
1210   // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1211   // entire list if this RefSCC wasn't a leaf before the edge removal.
1212   if (!Result.empty())
1213     G->LeafRefSCCs.erase(
1214         std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1215         G->LeafRefSCCs.end());
1216 
1217   // Return the new list of SCCs.
1218   return Result;
1219 }
1220 
1221 void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
1222   assert(SCCMap.empty() && DFSStack.empty() &&
1223          "This method cannot be called after SCCs have been formed!");
1224 
1225   return SourceN.insertEdgeInternal(Target, EK);
1226 }
1227 
1228 void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
1229   assert(SCCMap.empty() && DFSStack.empty() &&
1230          "This method cannot be called after SCCs have been formed!");
1231 
1232   return SourceN.removeEdgeInternal(Target);
1233 }
1234 
1235 LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1236   return *new (MappedN = BPA.Allocate()) Node(*this, F);
1237 }
1238 
1239 void LazyCallGraph::updateGraphPtrs() {
1240   // Process all nodes updating the graph pointers.
1241   {
1242     SmallVector<Node *, 16> Worklist;
1243     for (Edge &E : EntryEdges)
1244       if (Node *EntryN = E.getNode())
1245         Worklist.push_back(EntryN);
1246 
1247     while (!Worklist.empty()) {
1248       Node *N = Worklist.pop_back_val();
1249       N->G = this;
1250       for (Edge &E : N->Edges)
1251         if (Node *TargetN = E.getNode())
1252           Worklist.push_back(TargetN);
1253     }
1254   }
1255 
1256   // Process all SCCs updating the graph pointers.
1257   {
1258     SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
1259 
1260     while (!Worklist.empty()) {
1261       RefSCC &C = *Worklist.pop_back_val();
1262       C.G = this;
1263       for (RefSCC &ParentC : C.parents())
1264         Worklist.push_back(&ParentC);
1265     }
1266   }
1267 }
1268 
1269 /// Build the internal SCCs for a RefSCC from a sequence of nodes.
1270 ///
1271 /// Appends the SCCs to the provided vector and updates the map with their
1272 /// indices. Both the vector and map must be empty when passed into this
1273 /// routine.
1274 void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1275   assert(RC.SCCs.empty() && "Already built SCCs!");
1276   assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
1277 
1278   for (Node *N : Nodes) {
1279     assert(N->LowLink >= (*Nodes.begin())->LowLink &&
1280            "We cannot have a low link in an SCC lower than its root on the "
1281            "stack!");
1282 
1283     // This node will go into the next RefSCC, clear out its DFS and low link
1284     // as we scan.
1285     N->DFSNumber = N->LowLink = 0;
1286   }
1287 
1288   // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1289   // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1290   // internal storage as we won't need it for the outer graph's DFS any longer.
1291 
1292   SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1293   SmallVector<Node *, 16> PendingSCCStack;
1294 
1295   // Scan down the stack and DFS across the call edges.
1296   for (Node *RootN : Nodes) {
1297     assert(DFSStack.empty() &&
1298            "Cannot begin a new root with a non-empty DFS stack!");
1299     assert(PendingSCCStack.empty() &&
1300            "Cannot begin a new root with pending nodes for an SCC!");
1301 
1302     // Skip any nodes we've already reached in the DFS.
1303     if (RootN->DFSNumber != 0) {
1304       assert(RootN->DFSNumber == -1 &&
1305              "Shouldn't have any mid-DFS root nodes!");
1306       continue;
1307     }
1308 
1309     RootN->DFSNumber = RootN->LowLink = 1;
1310     int NextDFSNumber = 2;
1311 
1312     DFSStack.push_back({RootN, RootN->call_begin()});
1313     do {
1314       Node *N;
1315       call_edge_iterator I;
1316       std::tie(N, I) = DFSStack.pop_back_val();
1317       auto E = N->call_end();
1318       while (I != E) {
1319         Node &ChildN = *I->getNode();
1320         if (ChildN.DFSNumber == 0) {
1321           // We haven't yet visited this child, so descend, pushing the current
1322           // node onto the stack.
1323           DFSStack.push_back({N, I});
1324 
1325           assert(!lookupSCC(ChildN) &&
1326                  "Found a node with 0 DFS number but already in an SCC!");
1327           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1328           N = &ChildN;
1329           I = N->call_begin();
1330           E = N->call_end();
1331           continue;
1332         }
1333 
1334         // If the child has already been added to some child component, it
1335         // couldn't impact the low-link of this parent because it isn't
1336         // connected, and thus its low-link isn't relevant so skip it.
1337         if (ChildN.DFSNumber == -1) {
1338           ++I;
1339           continue;
1340         }
1341 
1342         // Track the lowest linked child as the lowest link for this node.
1343         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1344         if (ChildN.LowLink < N->LowLink)
1345           N->LowLink = ChildN.LowLink;
1346 
1347         // Move to the next edge.
1348         ++I;
1349       }
1350 
1351       // We've finished processing N and its descendents, put it on our pending
1352       // SCC stack to eventually get merged into an SCC of nodes.
1353       PendingSCCStack.push_back(N);
1354 
1355       // If this node is linked to some lower entry, continue walking up the
1356       // stack.
1357       if (N->LowLink != N->DFSNumber)
1358         continue;
1359 
1360       // Otherwise, we've completed an SCC. Append it to our post order list of
1361       // SCCs.
1362       int RootDFSNumber = N->DFSNumber;
1363       // Find the range of the node stack by walking down until we pass the
1364       // root DFS number.
1365       auto SCCNodes = make_range(
1366           PendingSCCStack.rbegin(),
1367           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1368             return N->DFSNumber < RootDFSNumber;
1369           }));
1370       // Form a new SCC out of these nodes and then clear them off our pending
1371       // stack.
1372       RC.SCCs.push_back(createSCC(RC, SCCNodes));
1373       for (Node &N : *RC.SCCs.back()) {
1374         N.DFSNumber = N.LowLink = -1;
1375         SCCMap[&N] = RC.SCCs.back();
1376       }
1377       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1378     } while (!DFSStack.empty());
1379   }
1380 
1381   // Wire up the SCC indices.
1382   for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1383     RC.SCCIndices[RC.SCCs[i]] = i;
1384 }
1385 
1386 // FIXME: We should move callers of this to embed the parent linking and leaf
1387 // tracking into their DFS in order to remove a full walk of all edges.
1388 void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1389   // Walk all edges in the RefSCC (this remains linear as we only do this once
1390   // when we build the RefSCC) to connect it to the parent sets of its
1391   // children.
1392   bool IsLeaf = true;
1393   for (SCC &C : RC)
1394     for (Node &N : C)
1395       for (Edge &E : N) {
1396         assert(E.getNode() &&
1397                "Cannot have a missing node in a visited part of the graph!");
1398         RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1399         if (&ChildRC == &RC)
1400           continue;
1401         ChildRC.Parents.insert(&RC);
1402         IsLeaf = false;
1403       }
1404 
1405   // For the SCCs where we fine no child SCCs, add them to the leaf list.
1406   if (IsLeaf)
1407     LeafRefSCCs.push_back(&RC);
1408 }
1409 
1410 LazyCallGraph::RefSCC *LazyCallGraph::getNextRefSCCInPostOrder() {
1411   if (DFSStack.empty()) {
1412     Node *N;
1413     do {
1414       // If we've handled all candidate entry nodes to the SCC forest, we're
1415       // done.
1416       if (RefSCCEntryNodes.empty())
1417         return nullptr;
1418 
1419       N = &get(*RefSCCEntryNodes.pop_back_val());
1420     } while (N->DFSNumber != 0);
1421 
1422     // Found a new root, begin the DFS here.
1423     N->LowLink = N->DFSNumber = 1;
1424     NextDFSNumber = 2;
1425     DFSStack.push_back({N, N->begin()});
1426   }
1427 
1428   for (;;) {
1429     Node *N;
1430     edge_iterator I;
1431     std::tie(N, I) = DFSStack.pop_back_val();
1432 
1433     assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1434                                "before placing a node onto the stack.");
1435 
1436     auto E = N->end();
1437     while (I != E) {
1438       Node &ChildN = I->getNode(*this);
1439       if (ChildN.DFSNumber == 0) {
1440         // We haven't yet visited this child, so descend, pushing the current
1441         // node onto the stack.
1442         DFSStack.push_back({N, N->begin()});
1443 
1444         assert(!SCCMap.count(&ChildN) &&
1445                "Found a node with 0 DFS number but already in an SCC!");
1446         ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1447         N = &ChildN;
1448         I = N->begin();
1449         E = N->end();
1450         continue;
1451       }
1452 
1453       // If the child has already been added to some child component, it
1454       // couldn't impact the low-link of this parent because it isn't
1455       // connected, and thus its low-link isn't relevant so skip it.
1456       if (ChildN.DFSNumber == -1) {
1457         ++I;
1458         continue;
1459       }
1460 
1461       // Track the lowest linked child as the lowest link for this node.
1462       assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1463       if (ChildN.LowLink < N->LowLink)
1464         N->LowLink = ChildN.LowLink;
1465 
1466       // Move to the next edge.
1467       ++I;
1468     }
1469 
1470     // We've finished processing N and its descendents, put it on our pending
1471     // SCC stack to eventually get merged into an SCC of nodes.
1472     PendingRefSCCStack.push_back(N);
1473 
1474     // If this node is linked to some lower entry, continue walking up the
1475     // stack.
1476     if (N->LowLink != N->DFSNumber) {
1477       assert(!DFSStack.empty() &&
1478              "We never found a viable root for an SCC to pop off!");
1479       continue;
1480     }
1481 
1482     // Otherwise, form a new RefSCC from the top of the pending node stack.
1483     int RootDFSNumber = N->DFSNumber;
1484     // Find the range of the node stack by walking down until we pass the
1485     // root DFS number.
1486     auto RefSCCNodes = node_stack_range(
1487         PendingRefSCCStack.rbegin(),
1488         find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1489           return N->DFSNumber < RootDFSNumber;
1490         }));
1491     // Form a new RefSCC out of these nodes and then clear them off our pending
1492     // stack.
1493     RefSCC *NewRC = createRefSCC(*this);
1494     buildSCCs(*NewRC, RefSCCNodes);
1495     connectRefSCC(*NewRC);
1496     PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1497                              PendingRefSCCStack.end());
1498 
1499     // We return the new node here. This essentially suspends the DFS walk
1500     // until another RefSCC is requested.
1501     return NewRC;
1502   }
1503 }
1504 
1505 char LazyCallGraphAnalysis::PassID;
1506 
1507 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1508 
1509 static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
1510   OS << "  Edges in function: " << N.getFunction().getName() << "\n";
1511   for (const LazyCallGraph::Edge &E : N)
1512     OS << "    " << (E.isCall() ? "call" : "ref ") << " -> "
1513        << E.getFunction().getName() << "\n";
1514 
1515   OS << "\n";
1516 }
1517 
1518 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1519   ptrdiff_t Size = std::distance(C.begin(), C.end());
1520   OS << "    SCC with " << Size << " functions:\n";
1521 
1522   for (LazyCallGraph::Node &N : C)
1523     OS << "      " << N.getFunction().getName() << "\n";
1524 }
1525 
1526 static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1527   ptrdiff_t Size = std::distance(C.begin(), C.end());
1528   OS << "  RefSCC with " << Size << " call SCCs:\n";
1529 
1530   for (LazyCallGraph::SCC &InnerC : C)
1531     printSCC(OS, InnerC);
1532 
1533   OS << "\n";
1534 }
1535 
1536 PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
1537                                                 ModuleAnalysisManager &AM) {
1538   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1539 
1540   OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1541      << "\n\n";
1542 
1543   for (Function &F : M)
1544     printNode(OS, G.get(F));
1545 
1546   for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1547     printRefSCC(OS, C);
1548 
1549   return PreservedAnalyses::all();
1550 }
1551 
1552 LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1553     : OS(OS) {}
1554 
1555 static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1556   std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1557 
1558   for (const LazyCallGraph::Edge &E : N) {
1559     OS << "  " << Name << " -> \""
1560        << DOT::EscapeString(E.getFunction().getName()) << "\"";
1561     if (!E.isCall()) // It is a ref edge.
1562       OS << " [style=dashed,label=\"ref\"]";
1563     OS << ";\n";
1564   }
1565 
1566   OS << "\n";
1567 }
1568 
1569 PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1570                                                    ModuleAnalysisManager &AM) {
1571   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1572 
1573   OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1574 
1575   for (Function &F : M)
1576     printNodeDOT(OS, G.get(F));
1577 
1578   OS << "}\n";
1579 
1580   return PreservedAnalyses::all();
1581 }
1582