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/ScopeExit.h"
12 #include "llvm/ADT/Sequence.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/IR/CallSite.h"
16 #include "llvm/IR/InstVisitor.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/GraphWriter.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "lcg"
25 
26 static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
27                     DenseMap<Function *, int> &EdgeIndexMap, Function &F,
28                     LazyCallGraph::Edge::Kind EK) {
29   if (!EdgeIndexMap.insert({&F, Edges.size()}).second)
30     return;
31 
32   DEBUG(dbgs() << "    Added callable function: " << F.getName() << "\n");
33   Edges.emplace_back(LazyCallGraph::Edge(F, EK));
34 }
35 
36 LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
37     : G(&G), F(F), DFSNumber(0), LowLink(0) {
38   DEBUG(dbgs() << "  Adding functions called by '" << F.getName()
39                << "' to the graph.\n");
40 
41   SmallVector<Constant *, 16> Worklist;
42   SmallPtrSet<Function *, 4> Callees;
43   SmallPtrSet<Constant *, 16> Visited;
44 
45   // Find all the potential call graph edges in this function. We track both
46   // actual call edges and indirect references to functions. The direct calls
47   // are trivially added, but to accumulate the latter we walk the instructions
48   // and add every operand which is a constant to the worklist to process
49   // afterward.
50   //
51   // Note that we consider *any* function with a definition to be a viable
52   // edge. Even if the function's definition is subject to replacement by
53   // some other module (say, a weak definition) there may still be
54   // optimizations which essentially speculate based on the definition and
55   // a way to check that the specific definition is in fact the one being
56   // used. For example, this could be done by moving the weak definition to
57   // a strong (internal) definition and making the weak definition be an
58   // alias. Then a test of the address of the weak function against the new
59   // strong definition's address would be an effective way to determine the
60   // safety of optimizing a direct call edge.
61   for (BasicBlock &BB : F)
62     for (Instruction &I : BB) {
63       if (auto CS = CallSite(&I))
64         if (Function *Callee = CS.getCalledFunction())
65           if (!Callee->isDeclaration())
66             if (Callees.insert(Callee).second) {
67               Visited.insert(Callee);
68               addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
69             }
70 
71       for (Value *Op : I.operand_values())
72         if (Constant *C = dyn_cast<Constant>(Op))
73           if (Visited.insert(C).second)
74             Worklist.push_back(C);
75     }
76 
77   // We've collected all the constant (and thus potentially function or
78   // function containing) operands to all of the instructions in the function.
79   // Process them (recursively) collecting every function found.
80   visitReferences(Worklist, Visited, [&](Function &F) {
81     addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref);
82   });
83 }
84 
85 void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
86   if (Node *N = G->lookup(Target))
87     return insertEdgeInternal(*N, EK);
88 
89   EdgeIndexMap.insert({&Target, Edges.size()});
90   Edges.emplace_back(Target, EK);
91 }
92 
93 void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
94   EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
95   Edges.emplace_back(TargetN, EK);
96 }
97 
98 void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
99   Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
100 }
101 
102 void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
103   auto IndexMapI = EdgeIndexMap.find(&Target);
104   assert(IndexMapI != EdgeIndexMap.end() &&
105          "Target not in the edge set for this caller?");
106 
107   Edges[IndexMapI->second] = Edge();
108   EdgeIndexMap.erase(IndexMapI);
109 }
110 
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112 LLVM_DUMP_METHOD void LazyCallGraph::Node::dump() const {
113   dbgs() << *this << '\n';
114 }
115 #endif
116 
117 LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
118   DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
119                << "\n");
120   for (Function &F : M)
121     if (!F.isDeclaration() && !F.hasLocalLinkage())
122       if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
123         DEBUG(dbgs() << "  Adding '" << F.getName()
124                      << "' to entry set of the graph.\n");
125         EntryEdges.emplace_back(F, Edge::Ref);
126       }
127 
128   // Now add entry nodes for functions reachable via initializers to globals.
129   SmallVector<Constant *, 16> Worklist;
130   SmallPtrSet<Constant *, 16> Visited;
131   for (GlobalVariable &GV : M.globals())
132     if (GV.hasInitializer())
133       if (Visited.insert(GV.getInitializer()).second)
134         Worklist.push_back(GV.getInitializer());
135 
136   DEBUG(dbgs() << "  Adding functions referenced by global initializers to the "
137                   "entry set.\n");
138   visitReferences(Worklist, Visited, [&](Function &F) {
139     addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref);
140   });
141 
142   for (const Edge &E : EntryEdges)
143     RefSCCEntryNodes.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)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
151       DFSStack(std::move(G.DFSStack)),
152       RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
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   LeafRefSCCs = std::move(G.LeafRefSCCs);
165   DFSStack = std::move(G.DFSStack);
166   RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
167   NextDFSNumber = G.NextDFSNumber;
168   updateGraphPtrs();
169   return *this;
170 }
171 
172 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
173 LLVM_DUMP_METHOD void LazyCallGraph::SCC::dump() const {
174   dbgs() << *this << '\n';
175 }
176 #endif
177 
178 #ifndef NDEBUG
179 void LazyCallGraph::SCC::verify() {
180   assert(OuterRefSCC && "Can't have a null RefSCC!");
181   assert(!Nodes.empty() && "Can't have an empty SCC!");
182 
183   for (Node *N : Nodes) {
184     assert(N && "Can't have a null node!");
185     assert(OuterRefSCC->G->lookupSCC(*N) == this &&
186            "Node does not map to this SCC!");
187     assert(N->DFSNumber == -1 &&
188            "Must set DFS numbers to -1 when adding a node to an SCC!");
189     assert(N->LowLink == -1 &&
190            "Must set low link to -1 when adding a node to an SCC!");
191     for (Edge &E : *N)
192       assert(E.getNode() && "Can't have an edge to a raw function!");
193   }
194 }
195 #endif
196 
197 bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
198   if (this == &C)
199     return false;
200 
201   for (Node &N : *this)
202     for (Edge &E : N.calls())
203       if (Node *CalleeN = E.getNode())
204         if (OuterRefSCC->G->lookupSCC(*CalleeN) == &C)
205           return true;
206 
207   // No edges found.
208   return false;
209 }
210 
211 bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
212   if (this == &TargetC)
213     return false;
214 
215   LazyCallGraph &G = *OuterRefSCC->G;
216 
217   // Start with this SCC.
218   SmallPtrSet<const SCC *, 16> Visited = {this};
219   SmallVector<const SCC *, 16> Worklist = {this};
220 
221   // Walk down the graph until we run out of edges or find a path to TargetC.
222   do {
223     const SCC &C = *Worklist.pop_back_val();
224     for (Node &N : C)
225       for (Edge &E : N.calls()) {
226         Node *CalleeN = E.getNode();
227         if (!CalleeN)
228           continue;
229         SCC *CalleeC = G.lookupSCC(*CalleeN);
230         if (!CalleeC)
231           continue;
232 
233         // If the callee's SCC is the TargetC, we're done.
234         if (CalleeC == &TargetC)
235           return true;
236 
237         // If this is the first time we've reached this SCC, put it on the
238         // worklist to recurse through.
239         if (Visited.insert(CalleeC).second)
240           Worklist.push_back(CalleeC);
241       }
242   } while (!Worklist.empty());
243 
244   // No paths found.
245   return false;
246 }
247 
248 LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
249 
250 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
251 LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
252   dbgs() << *this << '\n';
253 }
254 #endif
255 
256 #ifndef NDEBUG
257 void LazyCallGraph::RefSCC::verify() {
258   assert(G && "Can't have a null graph!");
259   assert(!SCCs.empty() && "Can't have an empty SCC!");
260 
261   // Verify basic properties of the SCCs.
262   SmallPtrSet<SCC *, 4> SCCSet;
263   for (SCC *C : SCCs) {
264     assert(C && "Can't have a null SCC!");
265     C->verify();
266     assert(&C->getOuterRefSCC() == this &&
267            "SCC doesn't think it is inside this RefSCC!");
268     bool Inserted = SCCSet.insert(C).second;
269     assert(Inserted && "Found a duplicate SCC!");
270     auto IndexIt = SCCIndices.find(C);
271     assert(IndexIt != SCCIndices.end() &&
272            "Found an SCC that doesn't have an index!");
273   }
274 
275   // Check that our indices map correctly.
276   for (auto &SCCIndexPair : SCCIndices) {
277     SCC *C = SCCIndexPair.first;
278     int i = SCCIndexPair.second;
279     assert(C && "Can't have a null SCC in the indices!");
280     assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
281     assert(SCCs[i] == C && "Index doesn't point to SCC!");
282   }
283 
284   // Check that the SCCs are in fact in post-order.
285   for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
286     SCC &SourceSCC = *SCCs[i];
287     for (Node &N : SourceSCC)
288       for (Edge &E : N) {
289         if (!E.isCall())
290           continue;
291         SCC &TargetSCC = *G->lookupSCC(*E.getNode());
292         if (&TargetSCC.getOuterRefSCC() == this) {
293           assert(SCCIndices.find(&TargetSCC)->second <= i &&
294                  "Edge between SCCs violates post-order relationship.");
295           continue;
296         }
297         assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
298                "Edge to a RefSCC missing us in its parent set.");
299       }
300   }
301 
302   // Check that our parents are actually parents.
303   for (RefSCC *ParentRC : Parents) {
304     assert(ParentRC != this && "Cannot be our own parent!");
305     auto HasConnectingEdge = [&] {
306       for (SCC &C : *ParentRC)
307         for (Node &N : C)
308           for (Edge &E : N)
309             if (G->lookupRefSCC(*E.getNode()) == this)
310               return true;
311       return false;
312     };
313     assert(HasConnectingEdge() && "No edge connects the parent to us!");
314   }
315 }
316 #endif
317 
318 bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
319   // Walk up the parents of this SCC and verify that we eventually find C.
320   SmallVector<const RefSCC *, 4> AncestorWorklist;
321   AncestorWorklist.push_back(this);
322   do {
323     const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
324     if (AncestorC->isChildOf(C))
325       return true;
326     for (const RefSCC *ParentC : AncestorC->Parents)
327       AncestorWorklist.push_back(ParentC);
328   } while (!AncestorWorklist.empty());
329 
330   return false;
331 }
332 
333 /// Generic helper that updates a postorder sequence of SCCs for a potentially
334 /// cycle-introducing edge insertion.
335 ///
336 /// A postorder sequence of SCCs of a directed graph has one fundamental
337 /// property: all deges in the DAG of SCCs point "up" the sequence. That is,
338 /// all edges in the SCC DAG point to prior SCCs in the sequence.
339 ///
340 /// This routine both updates a postorder sequence and uses that sequence to
341 /// compute the set of SCCs connected into a cycle. It should only be called to
342 /// insert a "downward" edge which will require changing the sequence to
343 /// restore it to a postorder.
344 ///
345 /// When inserting an edge from an earlier SCC to a later SCC in some postorder
346 /// sequence, all of the SCCs which may be impacted are in the closed range of
347 /// those two within the postorder sequence. The algorithm used here to restore
348 /// the state is as follows:
349 ///
350 /// 1) Starting from the source SCC, construct a set of SCCs which reach the
351 ///    source SCC consisting of just the source SCC. Then scan toward the
352 ///    target SCC in postorder and for each SCC, if it has an edge to an SCC
353 ///    in the set, add it to the set. Otherwise, the source SCC is not
354 ///    a successor, move it in the postorder sequence to immediately before
355 ///    the source SCC, shifting the source SCC and all SCCs in the set one
356 ///    position toward the target SCC. Stop scanning after processing the
357 ///    target SCC.
358 /// 2) If the source SCC is now past the target SCC in the postorder sequence,
359 ///    and thus the new edge will flow toward the start, we are done.
360 /// 3) Otherwise, starting from the target SCC, walk all edges which reach an
361 ///    SCC between the source and the target, and add them to the set of
362 ///    connected SCCs, then recurse through them. Once a complete set of the
363 ///    SCCs the target connects to is known, hoist the remaining SCCs between
364 ///    the source and the target to be above the target. Note that there is no
365 ///    need to process the source SCC, it is already known to connect.
366 /// 4) At this point, all of the SCCs in the closed range between the source
367 ///    SCC and the target SCC in the postorder sequence are connected,
368 ///    including the target SCC and the source SCC. Inserting the edge from
369 ///    the source SCC to the target SCC will form a cycle out of precisely
370 ///    these SCCs. Thus we can merge all of the SCCs in this closed range into
371 ///    a single SCC.
372 ///
373 /// This process has various important properties:
374 /// - Only mutates the SCCs when adding the edge actually changes the SCC
375 ///   structure.
376 /// - Never mutates SCCs which are unaffected by the change.
377 /// - Updates the postorder sequence to correctly satisfy the postorder
378 ///   constraint after the edge is inserted.
379 /// - Only reorders SCCs in the closed postorder sequence from the source to
380 ///   the target, so easy to bound how much has changed even in the ordering.
381 /// - Big-O is the number of edges in the closed postorder range of SCCs from
382 ///   source to target.
383 ///
384 /// This helper routine, in addition to updating the postorder sequence itself
385 /// will also update a map from SCCs to indices within that sequecne.
386 ///
387 /// The sequence and the map must operate on pointers to the SCC type.
388 ///
389 /// Two callbacks must be provided. The first computes the subset of SCCs in
390 /// the postorder closed range from the source to the target which connect to
391 /// the source SCC via some (transitive) set of edges. The second computes the
392 /// subset of the same range which the target SCC connects to via some
393 /// (transitive) set of edges. Both callbacks should populate the set argument
394 /// provided.
395 template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
396           typename ComputeSourceConnectedSetCallableT,
397           typename ComputeTargetConnectedSetCallableT>
398 static iterator_range<typename PostorderSequenceT::iterator>
399 updatePostorderSequenceForEdgeInsertion(
400     SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
401     SCCIndexMapT &SCCIndices,
402     ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
403     ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
404   int SourceIdx = SCCIndices[&SourceSCC];
405   int TargetIdx = SCCIndices[&TargetSCC];
406   assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
407 
408   SmallPtrSet<SCCT *, 4> ConnectedSet;
409 
410   // Compute the SCCs which (transitively) reach the source.
411   ComputeSourceConnectedSet(ConnectedSet);
412 
413   // Partition the SCCs in this part of the port-order sequence so only SCCs
414   // connecting to the source remain between it and the target. This is
415   // a benign partition as it preserves postorder.
416   auto SourceI = std::stable_partition(
417       SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
418       [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
419   for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
420     SCCIndices.find(SCCs[i])->second = i;
421 
422   // If the target doesn't connect to the source, then we've corrected the
423   // post-order and there are no cycles formed.
424   if (!ConnectedSet.count(&TargetSCC)) {
425     assert(SourceI > (SCCs.begin() + SourceIdx) &&
426            "Must have moved the source to fix the post-order.");
427     assert(*std::prev(SourceI) == &TargetSCC &&
428            "Last SCC to move should have bene the target.");
429 
430     // Return an empty range at the target SCC indicating there is nothing to
431     // merge.
432     return make_range(std::prev(SourceI), std::prev(SourceI));
433   }
434 
435   assert(SCCs[TargetIdx] == &TargetSCC &&
436          "Should not have moved target if connected!");
437   SourceIdx = SourceI - SCCs.begin();
438   assert(SCCs[SourceIdx] == &SourceSCC &&
439          "Bad updated index computation for the source SCC!");
440 
441 
442   // See whether there are any remaining intervening SCCs between the source
443   // and target. If so we need to make sure they all are reachable form the
444   // target.
445   if (SourceIdx + 1 < TargetIdx) {
446     ConnectedSet.clear();
447     ComputeTargetConnectedSet(ConnectedSet);
448 
449     // Partition SCCs so that only SCCs reached from the target remain between
450     // the source and the target. This preserves postorder.
451     auto TargetI = std::stable_partition(
452         SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
453         [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
454     for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
455       SCCIndices.find(SCCs[i])->second = i;
456     TargetIdx = std::prev(TargetI) - SCCs.begin();
457     assert(SCCs[TargetIdx] == &TargetSCC &&
458            "Should always end with the target!");
459   }
460 
461   // At this point, we know that connecting source to target forms a cycle
462   // because target connects back to source, and we know that all of the SCCs
463   // between the source and target in the postorder sequence participate in that
464   // cycle.
465   return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
466 }
467 
468 SmallVector<LazyCallGraph::SCC *, 1>
469 LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
470   assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
471   SmallVector<SCC *, 1> DeletedSCCs;
472 
473 #ifndef NDEBUG
474   // In a debug build, verify the RefSCC is valid to start with and when this
475   // routine finishes.
476   verify();
477   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
478 #endif
479 
480   SCC &SourceSCC = *G->lookupSCC(SourceN);
481   SCC &TargetSCC = *G->lookupSCC(TargetN);
482 
483   // If the two nodes are already part of the same SCC, we're also done as
484   // we've just added more connectivity.
485   if (&SourceSCC == &TargetSCC) {
486     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
487     return DeletedSCCs;
488   }
489 
490   // At this point we leverage the postorder list of SCCs to detect when the
491   // insertion of an edge changes the SCC structure in any way.
492   //
493   // First and foremost, we can eliminate the need for any changes when the
494   // edge is toward the beginning of the postorder sequence because all edges
495   // flow in that direction already. Thus adding a new one cannot form a cycle.
496   int SourceIdx = SCCIndices[&SourceSCC];
497   int TargetIdx = SCCIndices[&TargetSCC];
498   if (TargetIdx < SourceIdx) {
499     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
500     return DeletedSCCs;
501   }
502 
503   // Compute the SCCs which (transitively) reach the source.
504   auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
505 #ifndef NDEBUG
506     // Check that the RefSCC is still valid before computing this as the
507     // results will be nonsensical of we've broken its invariants.
508     verify();
509 #endif
510     ConnectedSet.insert(&SourceSCC);
511     auto IsConnected = [&](SCC &C) {
512       for (Node &N : C)
513         for (Edge &E : N.calls()) {
514           assert(E.getNode() && "Must have formed a node within an SCC!");
515           if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
516             return true;
517         }
518 
519       return false;
520     };
521 
522     for (SCC *C :
523          make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
524       if (IsConnected(*C))
525         ConnectedSet.insert(C);
526   };
527 
528   // Use a normal worklist to find which SCCs the target connects to. We still
529   // bound the search based on the range in the postorder list we care about,
530   // but because this is forward connectivity we just "recurse" through the
531   // edges.
532   auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
533 #ifndef NDEBUG
534     // Check that the RefSCC is still valid before computing this as the
535     // results will be nonsensical of we've broken its invariants.
536     verify();
537 #endif
538     ConnectedSet.insert(&TargetSCC);
539     SmallVector<SCC *, 4> Worklist;
540     Worklist.push_back(&TargetSCC);
541     do {
542       SCC &C = *Worklist.pop_back_val();
543       for (Node &N : C)
544         for (Edge &E : N) {
545           assert(E.getNode() && "Must have formed a node within an SCC!");
546           if (!E.isCall())
547             continue;
548           SCC &EdgeC = *G->lookupSCC(*E.getNode());
549           if (&EdgeC.getOuterRefSCC() != this)
550             // Not in this RefSCC...
551             continue;
552           if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
553             // Not in the postorder sequence between source and target.
554             continue;
555 
556           if (ConnectedSet.insert(&EdgeC).second)
557             Worklist.push_back(&EdgeC);
558         }
559     } while (!Worklist.empty());
560   };
561 
562   // Use a generic helper to update the postorder sequence of SCCs and return
563   // a range of any SCCs connected into a cycle by inserting this edge. This
564   // routine will also take care of updating the indices into the postorder
565   // sequence.
566   auto MergeRange = updatePostorderSequenceForEdgeInsertion(
567       SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
568       ComputeTargetConnectedSet);
569 
570   // If the merge range is empty, then adding the edge didn't actually form any
571   // new cycles. We're done.
572   if (MergeRange.begin() == MergeRange.end()) {
573     // Now that the SCC structure is finalized, flip the kind to call.
574     SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
575     return DeletedSCCs;
576   }
577 
578 #ifndef NDEBUG
579   // Before merging, check that the RefSCC remains valid after all the
580   // postorder updates.
581   verify();
582 #endif
583 
584   // Otherwise we need to merge all of the SCCs in the cycle into a single
585   // result SCC.
586   //
587   // NB: We merge into the target because all of these functions were already
588   // reachable from the target, meaning any SCC-wide properties deduced about it
589   // other than the set of functions within it will not have changed.
590   for (SCC *C : MergeRange) {
591     assert(C != &TargetSCC &&
592            "We merge *into* the target and shouldn't process it here!");
593     SCCIndices.erase(C);
594     TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
595     for (Node *N : C->Nodes)
596       G->SCCMap[N] = &TargetSCC;
597     C->clear();
598     DeletedSCCs.push_back(C);
599   }
600 
601   // Erase the merged SCCs from the list and update the indices of the
602   // remaining SCCs.
603   int IndexOffset = MergeRange.end() - MergeRange.begin();
604   auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
605   for (SCC *C : make_range(EraseEnd, SCCs.end()))
606     SCCIndices[C] -= IndexOffset;
607 
608   // Now that the SCC structure is finalized, flip the kind to call.
609   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
610 
611   // And we're done!
612   return DeletedSCCs;
613 }
614 
615 void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
616                                                            Node &TargetN) {
617   assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
618 
619 #ifndef NDEBUG
620   // In a debug build, verify the RefSCC is valid to start with and when this
621   // routine finishes.
622   verify();
623   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
624 #endif
625 
626   assert(G->lookupRefSCC(SourceN) == this &&
627          "Source must be in this RefSCC.");
628   assert(G->lookupRefSCC(TargetN) == this &&
629          "Target must be in this RefSCC.");
630   assert(G->lookupSCC(SourceN) != G->lookupSCC(TargetN) &&
631          "Source and Target must be in separate SCCs for this to be trivial!");
632 
633   // Set the edge kind.
634   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
635 }
636 
637 iterator_range<LazyCallGraph::RefSCC::iterator>
638 LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
639   assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
640 
641 #ifndef NDEBUG
642   // In a debug build, verify the RefSCC is valid to start with and when this
643   // routine finishes.
644   verify();
645   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
646 #endif
647 
648   assert(G->lookupRefSCC(SourceN) == this &&
649          "Source must be in this RefSCC.");
650   assert(G->lookupRefSCC(TargetN) == this &&
651          "Target must be in this RefSCC.");
652 
653   SCC &TargetSCC = *G->lookupSCC(TargetN);
654   assert(G->lookupSCC(SourceN) == &TargetSCC && "Source and Target must be in "
655                                                 "the same SCC to require the "
656                                                 "full CG update.");
657 
658   // Set the edge kind.
659   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
660 
661   // Otherwise we are removing a call edge from a single SCC. This may break
662   // the cycle. In order to compute the new set of SCCs, we need to do a small
663   // DFS over the nodes within the SCC to form any sub-cycles that remain as
664   // distinct SCCs and compute a postorder over the resulting SCCs.
665   //
666   // However, we specially handle the target node. The target node is known to
667   // reach all other nodes in the original SCC by definition. This means that
668   // we want the old SCC to be replaced with an SCC contaning that node as it
669   // will be the root of whatever SCC DAG results from the DFS. Assumptions
670   // about an SCC such as the set of functions called will continue to hold,
671   // etc.
672 
673   SCC &OldSCC = TargetSCC;
674   SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
675   SmallVector<Node *, 16> PendingSCCStack;
676   SmallVector<SCC *, 4> NewSCCs;
677 
678   // Prepare the nodes for a fresh DFS.
679   SmallVector<Node *, 16> Worklist;
680   Worklist.swap(OldSCC.Nodes);
681   for (Node *N : Worklist) {
682     N->DFSNumber = N->LowLink = 0;
683     G->SCCMap.erase(N);
684   }
685 
686   // Force the target node to be in the old SCC. This also enables us to take
687   // a very significant short-cut in the standard Tarjan walk to re-form SCCs
688   // below: whenever we build an edge that reaches the target node, we know
689   // that the target node eventually connects back to all other nodes in our
690   // walk. As a consequence, we can detect and handle participants in that
691   // cycle without walking all the edges that form this connection, and instead
692   // by relying on the fundamental guarantee coming into this operation (all
693   // nodes are reachable from the target due to previously forming an SCC).
694   TargetN.DFSNumber = TargetN.LowLink = -1;
695   OldSCC.Nodes.push_back(&TargetN);
696   G->SCCMap[&TargetN] = &OldSCC;
697 
698   // Scan down the stack and DFS across the call edges.
699   for (Node *RootN : Worklist) {
700     assert(DFSStack.empty() &&
701            "Cannot begin a new root with a non-empty DFS stack!");
702     assert(PendingSCCStack.empty() &&
703            "Cannot begin a new root with pending nodes for an SCC!");
704 
705     // Skip any nodes we've already reached in the DFS.
706     if (RootN->DFSNumber != 0) {
707       assert(RootN->DFSNumber == -1 &&
708              "Shouldn't have any mid-DFS root nodes!");
709       continue;
710     }
711 
712     RootN->DFSNumber = RootN->LowLink = 1;
713     int NextDFSNumber = 2;
714 
715     DFSStack.push_back({RootN, RootN->call_begin()});
716     do {
717       Node *N;
718       call_edge_iterator I;
719       std::tie(N, I) = DFSStack.pop_back_val();
720       auto E = N->call_end();
721       while (I != E) {
722         Node &ChildN = *I->getNode();
723         if (ChildN.DFSNumber == 0) {
724           // We haven't yet visited this child, so descend, pushing the current
725           // node onto the stack.
726           DFSStack.push_back({N, I});
727 
728           assert(!G->SCCMap.count(&ChildN) &&
729                  "Found a node with 0 DFS number but already in an SCC!");
730           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
731           N = &ChildN;
732           I = N->call_begin();
733           E = N->call_end();
734           continue;
735         }
736 
737         // Check for the child already being part of some component.
738         if (ChildN.DFSNumber == -1) {
739           if (G->lookupSCC(ChildN) == &OldSCC) {
740             // If the child is part of the old SCC, we know that it can reach
741             // every other node, so we have formed a cycle. Pull the entire DFS
742             // and pending stacks into it. See the comment above about setting
743             // up the old SCC for why we do this.
744             int OldSize = OldSCC.size();
745             OldSCC.Nodes.push_back(N);
746             OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
747             PendingSCCStack.clear();
748             while (!DFSStack.empty())
749               OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
750             for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
751               N.DFSNumber = N.LowLink = -1;
752               G->SCCMap[&N] = &OldSCC;
753             }
754             N = nullptr;
755             break;
756           }
757 
758           // If the child has already been added to some child component, it
759           // couldn't impact the low-link of this parent because it isn't
760           // connected, and thus its low-link isn't relevant so skip it.
761           ++I;
762           continue;
763         }
764 
765         // Track the lowest linked child as the lowest link for this node.
766         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
767         if (ChildN.LowLink < N->LowLink)
768           N->LowLink = ChildN.LowLink;
769 
770         // Move to the next edge.
771         ++I;
772       }
773       if (!N)
774         // Cleared the DFS early, start another round.
775         break;
776 
777       // We've finished processing N and its descendents, put it on our pending
778       // SCC stack to eventually get merged into an SCC of nodes.
779       PendingSCCStack.push_back(N);
780 
781       // If this node is linked to some lower entry, continue walking up the
782       // stack.
783       if (N->LowLink != N->DFSNumber)
784         continue;
785 
786       // Otherwise, we've completed an SCC. Append it to our post order list of
787       // SCCs.
788       int RootDFSNumber = N->DFSNumber;
789       // Find the range of the node stack by walking down until we pass the
790       // root DFS number.
791       auto SCCNodes = make_range(
792           PendingSCCStack.rbegin(),
793           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
794             return N->DFSNumber < RootDFSNumber;
795           }));
796 
797       // Form a new SCC out of these nodes and then clear them off our pending
798       // stack.
799       NewSCCs.push_back(G->createSCC(*this, SCCNodes));
800       for (Node &N : *NewSCCs.back()) {
801         N.DFSNumber = N.LowLink = -1;
802         G->SCCMap[&N] = NewSCCs.back();
803       }
804       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
805     } while (!DFSStack.empty());
806   }
807 
808   // Insert the remaining SCCs before the old one. The old SCC can reach all
809   // other SCCs we form because it contains the target node of the removed edge
810   // of the old SCC. This means that we will have edges into all of the new
811   // SCCs, which means the old one must come last for postorder.
812   int OldIdx = SCCIndices[&OldSCC];
813   SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
814 
815   // Update the mapping from SCC* to index to use the new SCC*s, and remove the
816   // old SCC from the mapping.
817   for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
818     SCCIndices[SCCs[Idx]] = Idx;
819 
820   return make_range(SCCs.begin() + OldIdx,
821                     SCCs.begin() + OldIdx + NewSCCs.size());
822 }
823 
824 void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
825                                                      Node &TargetN) {
826   assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
827 
828   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
829   assert(G->lookupRefSCC(TargetN) != this &&
830          "Target must not be in this RefSCC.");
831   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
832          "Target must be a descendant of the Source.");
833 
834   // Edges between RefSCCs are the same regardless of call or ref, so we can
835   // just flip the edge here.
836   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
837 
838 #ifndef NDEBUG
839   // Check that the RefSCC is still valid.
840   verify();
841 #endif
842 }
843 
844 void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
845                                                     Node &TargetN) {
846   assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
847 
848   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
849   assert(G->lookupRefSCC(TargetN) != this &&
850          "Target must not be in this RefSCC.");
851   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
852          "Target must be a descendant of the Source.");
853 
854   // Edges between RefSCCs are the same regardless of call or ref, so we can
855   // just flip the edge here.
856   SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
857 
858 #ifndef NDEBUG
859   // Check that the RefSCC is still valid.
860   verify();
861 #endif
862 }
863 
864 void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
865                                                   Node &TargetN) {
866   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
867   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
868 
869   SourceN.insertEdgeInternal(TargetN, Edge::Ref);
870 
871 #ifndef NDEBUG
872   // Check that the RefSCC is still valid.
873   verify();
874 #endif
875 }
876 
877 void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
878                                                Edge::Kind EK) {
879   // First insert it into the caller.
880   SourceN.insertEdgeInternal(TargetN, EK);
881 
882   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
883 
884   RefSCC &TargetC = *G->lookupRefSCC(TargetN);
885   assert(&TargetC != this && "Target must not be in this RefSCC.");
886   assert(TargetC.isDescendantOf(*this) &&
887          "Target must be a descendant of the Source.");
888 
889   // The only change required is to add this SCC to the parent set of the
890   // callee.
891   TargetC.Parents.insert(this);
892 
893 #ifndef NDEBUG
894   // Check that the RefSCC is still valid.
895   verify();
896 #endif
897 }
898 
899 SmallVector<LazyCallGraph::RefSCC *, 1>
900 LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
901   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
902   RefSCC &SourceC = *G->lookupRefSCC(SourceN);
903   assert(&SourceC != this && "Source must not be in this RefSCC.");
904   assert(SourceC.isDescendantOf(*this) &&
905          "Source must be a descendant of the Target.");
906 
907   SmallVector<RefSCC *, 1> DeletedRefSCCs;
908 
909 #ifndef NDEBUG
910   // In a debug build, verify the RefSCC is valid to start with and when this
911   // routine finishes.
912   verify();
913   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
914 #endif
915 
916   int SourceIdx = G->RefSCCIndices[&SourceC];
917   int TargetIdx = G->RefSCCIndices[this];
918   assert(SourceIdx < TargetIdx &&
919          "Postorder list doesn't see edge as incoming!");
920 
921   // Compute the RefSCCs which (transitively) reach the source. We do this by
922   // working backwards from the source using the parent set in each RefSCC,
923   // skipping any RefSCCs that don't fall in the postorder range. This has the
924   // advantage of walking the sparser parent edge (in high fan-out graphs) but
925   // more importantly this removes examining all forward edges in all RefSCCs
926   // within the postorder range which aren't in fact connected. Only connected
927   // RefSCCs (and their edges) are visited here.
928   auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
929     Set.insert(&SourceC);
930     SmallVector<RefSCC *, 4> Worklist;
931     Worklist.push_back(&SourceC);
932     do {
933       RefSCC &RC = *Worklist.pop_back_val();
934       for (RefSCC &ParentRC : RC.parents()) {
935         // Skip any RefSCCs outside the range of source to target in the
936         // postorder sequence.
937         int ParentIdx = G->getRefSCCIndex(ParentRC);
938         assert(ParentIdx > SourceIdx && "Parent cannot precede source in postorder!");
939         if (ParentIdx > TargetIdx)
940           continue;
941         if (Set.insert(&ParentRC).second)
942           // First edge connecting to this parent, add it to our worklist.
943           Worklist.push_back(&ParentRC);
944       }
945     } while (!Worklist.empty());
946   };
947 
948   // Use a normal worklist to find which SCCs the target connects to. We still
949   // bound the search based on the range in the postorder list we care about,
950   // but because this is forward connectivity we just "recurse" through the
951   // edges.
952   auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
953     Set.insert(this);
954     SmallVector<RefSCC *, 4> Worklist;
955     Worklist.push_back(this);
956     do {
957       RefSCC &RC = *Worklist.pop_back_val();
958       for (SCC &C : RC)
959         for (Node &N : C)
960           for (Edge &E : N) {
961             assert(E.getNode() && "Must have formed a node!");
962             RefSCC &EdgeRC = *G->lookupRefSCC(*E.getNode());
963             if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
964               // Not in the postorder sequence between source and target.
965               continue;
966 
967             if (Set.insert(&EdgeRC).second)
968               Worklist.push_back(&EdgeRC);
969           }
970     } while (!Worklist.empty());
971   };
972 
973   // Use a generic helper to update the postorder sequence of RefSCCs and return
974   // a range of any RefSCCs connected into a cycle by inserting this edge. This
975   // routine will also take care of updating the indices into the postorder
976   // sequence.
977   iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
978       updatePostorderSequenceForEdgeInsertion(
979           SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
980           ComputeSourceConnectedSet, ComputeTargetConnectedSet);
981 
982   // Build a set so we can do fast tests for whether a RefSCC will end up as
983   // part of the merged RefSCC.
984   SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end());
985 
986   // This RefSCC will always be part of that set, so just insert it here.
987   MergeSet.insert(this);
988 
989   // Now that we have identified all of the SCCs which need to be merged into
990   // a connected set with the inserted edge, merge all of them into this SCC.
991   SmallVector<SCC *, 16> MergedSCCs;
992   int SCCIndex = 0;
993   for (RefSCC *RC : MergeRange) {
994     assert(RC != this && "We're merging into the target RefSCC, so it "
995                          "shouldn't be in the range.");
996 
997     // Merge the parents which aren't part of the merge into the our parents.
998     for (RefSCC *ParentRC : RC->Parents)
999       if (!MergeSet.count(ParentRC))
1000         Parents.insert(ParentRC);
1001     RC->Parents.clear();
1002 
1003     // Walk the inner SCCs to update their up-pointer and walk all the edges to
1004     // update any parent sets.
1005     // FIXME: We should try to find a way to avoid this (rather expensive) edge
1006     // walk by updating the parent sets in some other manner.
1007     for (SCC &InnerC : *RC) {
1008       InnerC.OuterRefSCC = this;
1009       SCCIndices[&InnerC] = SCCIndex++;
1010       for (Node &N : InnerC) {
1011         G->SCCMap[&N] = &InnerC;
1012         for (Edge &E : N) {
1013           assert(E.getNode() &&
1014                  "Cannot have a null node within a visited SCC!");
1015           RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1016           if (MergeSet.count(&ChildRC))
1017             continue;
1018           ChildRC.Parents.erase(RC);
1019           ChildRC.Parents.insert(this);
1020         }
1021       }
1022     }
1023 
1024     // Now merge in the SCCs. We can actually move here so try to reuse storage
1025     // the first time through.
1026     if (MergedSCCs.empty())
1027       MergedSCCs = std::move(RC->SCCs);
1028     else
1029       MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
1030     RC->SCCs.clear();
1031     DeletedRefSCCs.push_back(RC);
1032   }
1033 
1034   // Append our original SCCs to the merged list and move it into place.
1035   for (SCC &InnerC : *this)
1036     SCCIndices[&InnerC] = SCCIndex++;
1037   MergedSCCs.append(SCCs.begin(), SCCs.end());
1038   SCCs = std::move(MergedSCCs);
1039 
1040   // Remove the merged away RefSCCs from the post order sequence.
1041   for (RefSCC *RC : MergeRange)
1042     G->RefSCCIndices.erase(RC);
1043   int IndexOffset = MergeRange.end() - MergeRange.begin();
1044   auto EraseEnd =
1045       G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
1046   for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
1047     G->RefSCCIndices[RC] -= IndexOffset;
1048 
1049   // At this point we have a merged RefSCC with a post-order SCCs list, just
1050   // connect the nodes to form the new edge.
1051   SourceN.insertEdgeInternal(TargetN, Edge::Ref);
1052 
1053   // We return the list of SCCs which were merged so that callers can
1054   // invalidate any data they have associated with those SCCs. Note that these
1055   // SCCs are no longer in an interesting state (they are totally empty) but
1056   // the pointers will remain stable for the life of the graph itself.
1057   return DeletedRefSCCs;
1058 }
1059 
1060 void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
1061   assert(G->lookupRefSCC(SourceN) == this &&
1062          "The source must be a member of this RefSCC.");
1063 
1064   RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1065   assert(&TargetRC != this && "The target must not be a member of this RefSCC");
1066 
1067   assert(!is_contained(G->LeafRefSCCs, this) &&
1068          "Cannot have a leaf RefSCC source.");
1069 
1070 #ifndef NDEBUG
1071   // In a debug build, verify the RefSCC is valid to start with and when this
1072   // routine finishes.
1073   verify();
1074   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1075 #endif
1076 
1077   // First remove it from the node.
1078   SourceN.removeEdgeInternal(TargetN.getFunction());
1079 
1080   bool HasOtherEdgeToChildRC = false;
1081   bool HasOtherChildRC = false;
1082   for (SCC *InnerC : SCCs) {
1083     for (Node &N : *InnerC) {
1084       for (Edge &E : N) {
1085         assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1086         RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
1087         if (&OtherChildRC == &TargetRC) {
1088           HasOtherEdgeToChildRC = true;
1089           break;
1090         }
1091         if (&OtherChildRC != this)
1092           HasOtherChildRC = true;
1093       }
1094       if (HasOtherEdgeToChildRC)
1095         break;
1096     }
1097     if (HasOtherEdgeToChildRC)
1098       break;
1099   }
1100   // Because the SCCs form a DAG, deleting such an edge cannot change the set
1101   // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
1102   // the source SCC no longer connected to the target SCC. If so, we need to
1103   // update the target SCC's map of its parents.
1104   if (!HasOtherEdgeToChildRC) {
1105     bool Removed = TargetRC.Parents.erase(this);
1106     (void)Removed;
1107     assert(Removed &&
1108            "Did not find the source SCC in the target SCC's parent list!");
1109 
1110     // It may orphan an SCC if it is the last edge reaching it, but that does
1111     // not violate any invariants of the graph.
1112     if (TargetRC.Parents.empty())
1113       DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
1114                    << " -> " << TargetN.getFunction().getName()
1115                    << " edge orphaned the callee's SCC!\n");
1116 
1117     // It may make the Source SCC a leaf SCC.
1118     if (!HasOtherChildRC)
1119       G->LeafRefSCCs.push_back(this);
1120   }
1121 }
1122 
1123 SmallVector<LazyCallGraph::RefSCC *, 1>
1124 LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
1125   assert(!SourceN[TargetN].isCall() &&
1126          "Cannot remove a call edge, it must first be made a ref edge");
1127 
1128 #ifndef NDEBUG
1129   // In a debug build, verify the RefSCC is valid to start with and when this
1130   // routine finishes.
1131   verify();
1132   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1133 #endif
1134 
1135   // First remove the actual edge.
1136   SourceN.removeEdgeInternal(TargetN.getFunction());
1137 
1138   // We return a list of the resulting *new* RefSCCs in post-order.
1139   SmallVector<RefSCC *, 1> Result;
1140 
1141   // Direct recursion doesn't impact the SCC graph at all.
1142   if (&SourceN == &TargetN)
1143     return Result;
1144 
1145   // If this ref edge is within an SCC then there are sufficient other edges to
1146   // form a cycle without this edge so removing it is a no-op.
1147   SCC &SourceC = *G->lookupSCC(SourceN);
1148   SCC &TargetC = *G->lookupSCC(TargetN);
1149   if (&SourceC == &TargetC)
1150     return Result;
1151 
1152   // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1153   // for each inner SCC. We also store these associated with *nodes* rather
1154   // than SCCs because this saves a round-trip through the node->SCC map and in
1155   // the common case, SCCs are small. We will verify that we always give the
1156   // same number to every node in the SCC such that these are equivalent.
1157   const int RootPostOrderNumber = 0;
1158   int PostOrderNumber = RootPostOrderNumber + 1;
1159   SmallDenseMap<Node *, int> PostOrderMapping;
1160 
1161   // Every node in the target SCC can already reach every node in this RefSCC
1162   // (by definition). It is the only node we know will stay inside this RefSCC.
1163   // Everything which transitively reaches Target will also remain in the
1164   // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
1165   // back to the root post order number.
1166   //
1167   // This also enables us to take a very significant short-cut in the standard
1168   // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
1169   // references the target node, we know that the target node eventually
1170   // references all other nodes in our walk. As a consequence, we can detect
1171   // and handle participants in that cycle without walking all the edges that
1172   // form the connections, and instead by relying on the fundamental guarantee
1173   // coming into this operation.
1174   for (Node &N : TargetC)
1175     PostOrderMapping[&N] = RootPostOrderNumber;
1176 
1177   // Reset all the other nodes to prepare for a DFS over them, and add them to
1178   // our worklist.
1179   SmallVector<Node *, 8> Worklist;
1180   for (SCC *C : SCCs) {
1181     if (C == &TargetC)
1182       continue;
1183 
1184     for (Node &N : *C)
1185       N.DFSNumber = N.LowLink = 0;
1186 
1187     Worklist.append(C->Nodes.begin(), C->Nodes.end());
1188   }
1189 
1190   auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1191     N.DFSNumber = N.LowLink = -1;
1192     PostOrderMapping[&N] = Number;
1193   };
1194 
1195   SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1196   SmallVector<Node *, 4> PendingRefSCCStack;
1197   do {
1198     assert(DFSStack.empty() &&
1199            "Cannot begin a new root with a non-empty DFS stack!");
1200     assert(PendingRefSCCStack.empty() &&
1201            "Cannot begin a new root with pending nodes for an SCC!");
1202 
1203     Node *RootN = Worklist.pop_back_val();
1204     // Skip any nodes we've already reached in the DFS.
1205     if (RootN->DFSNumber != 0) {
1206       assert(RootN->DFSNumber == -1 &&
1207              "Shouldn't have any mid-DFS root nodes!");
1208       continue;
1209     }
1210 
1211     RootN->DFSNumber = RootN->LowLink = 1;
1212     int NextDFSNumber = 2;
1213 
1214     DFSStack.push_back({RootN, RootN->begin()});
1215     do {
1216       Node *N;
1217       edge_iterator I;
1218       std::tie(N, I) = DFSStack.pop_back_val();
1219       auto E = N->end();
1220 
1221       assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1222                                   "before processing a node.");
1223 
1224       while (I != E) {
1225         Node &ChildN = I->getNode(*G);
1226         if (ChildN.DFSNumber == 0) {
1227           // Mark that we should start at this child when next this node is the
1228           // top of the stack. We don't start at the next child to ensure this
1229           // child's lowlink is reflected.
1230           DFSStack.push_back({N, I});
1231 
1232           // Continue, resetting to the child node.
1233           ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1234           N = &ChildN;
1235           I = ChildN.begin();
1236           E = ChildN.end();
1237           continue;
1238         }
1239         if (ChildN.DFSNumber == -1) {
1240           // Check if this edge's target node connects to the deleted edge's
1241           // target node. If so, we know that every node connected will end up
1242           // in this RefSCC, so collapse the entire current stack into the root
1243           // slot in our SCC numbering. See above for the motivation of
1244           // optimizing the target connected nodes in this way.
1245           auto PostOrderI = PostOrderMapping.find(&ChildN);
1246           if (PostOrderI != PostOrderMapping.end() &&
1247               PostOrderI->second == RootPostOrderNumber) {
1248             MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1249             while (!PendingRefSCCStack.empty())
1250               MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1251                                    RootPostOrderNumber);
1252             while (!DFSStack.empty())
1253               MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1254                                    RootPostOrderNumber);
1255             // Ensure we break all the way out of the enclosing loop.
1256             N = nullptr;
1257             break;
1258           }
1259 
1260           // If this child isn't currently in this RefSCC, no need to process
1261           // it. However, we do need to remove this RefSCC from its RefSCC's
1262           // parent set.
1263           RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1264           ChildRC.Parents.erase(this);
1265           ++I;
1266           continue;
1267         }
1268 
1269         // Track the lowest link of the children, if any are still in the stack.
1270         // Any child not on the stack will have a LowLink of -1.
1271         assert(ChildN.LowLink != 0 &&
1272                "Low-link must not be zero with a non-zero DFS number.");
1273         if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1274           N->LowLink = ChildN.LowLink;
1275         ++I;
1276       }
1277       if (!N)
1278         // We short-circuited this node.
1279         break;
1280 
1281       // We've finished processing N and its descendents, put it on our pending
1282       // stack to eventually get merged into a RefSCC.
1283       PendingRefSCCStack.push_back(N);
1284 
1285       // If this node is linked to some lower entry, continue walking up the
1286       // stack.
1287       if (N->LowLink != N->DFSNumber) {
1288         assert(!DFSStack.empty() &&
1289                "We never found a viable root for a RefSCC to pop off!");
1290         continue;
1291       }
1292 
1293       // Otherwise, form a new RefSCC from the top of the pending node stack.
1294       int RootDFSNumber = N->DFSNumber;
1295       // Find the range of the node stack by walking down until we pass the
1296       // root DFS number.
1297       auto RefSCCNodes = make_range(
1298           PendingRefSCCStack.rbegin(),
1299           find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1300             return N->DFSNumber < RootDFSNumber;
1301           }));
1302 
1303       // Mark the postorder number for these nodes and clear them off the
1304       // stack. We'll use the postorder number to pull them into RefSCCs at the
1305       // end. FIXME: Fuse with the loop above.
1306       int RefSCCNumber = PostOrderNumber++;
1307       for (Node *N : RefSCCNodes)
1308         MarkNodeForSCCNumber(*N, RefSCCNumber);
1309 
1310       PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1311                                PendingRefSCCStack.end());
1312     } while (!DFSStack.empty());
1313 
1314     assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
1315     assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
1316   } while (!Worklist.empty());
1317 
1318   // We now have a post-order numbering for RefSCCs and a mapping from each
1319   // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1320   // (re-using this RefSCC node for the root) and build a radix-sort style map
1321   // from postorder number to the RefSCC. We then append SCCs to each of these
1322   // RefSCCs in the order they occured in the original SCCs container.
1323   for (int i = 1; i < PostOrderNumber; ++i)
1324     Result.push_back(G->createRefSCC(*G));
1325 
1326   // Insert the resulting postorder sequence into the global graph postorder
1327   // sequence before the current RefSCC in that sequence. The idea being that
1328   // this RefSCC is the target of the reference edge removed, and thus has
1329   // a direct or indirect edge to every other RefSCC formed and so must be at
1330   // the end of any postorder traversal.
1331   //
1332   // FIXME: It'd be nice to change the APIs so that we returned an iterator
1333   // range over the global postorder sequence and generally use that sequence
1334   // rather than building a separate result vector here.
1335   if (!Result.empty()) {
1336     int Idx = G->getRefSCCIndex(*this);
1337     G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx,
1338                                Result.begin(), Result.end());
1339     for (int i : seq<int>(Idx, G->PostOrderRefSCCs.size()))
1340       G->RefSCCIndices[G->PostOrderRefSCCs[i]] = i;
1341     assert(G->PostOrderRefSCCs[G->getRefSCCIndex(*this)] == this &&
1342            "Failed to update this RefSCC's index after insertion!");
1343   }
1344 
1345   for (SCC *C : SCCs) {
1346     auto PostOrderI = PostOrderMapping.find(&*C->begin());
1347     assert(PostOrderI != PostOrderMapping.end() &&
1348            "Cannot have missing mappings for nodes!");
1349     int SCCNumber = PostOrderI->second;
1350 #ifndef NDEBUG
1351     for (Node &N : *C)
1352       assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1353              "Cannot have different numbers for nodes in the same SCC!");
1354 #endif
1355     if (SCCNumber == 0)
1356       // The root node is handled separately by removing the SCCs.
1357       continue;
1358 
1359     RefSCC &RC = *Result[SCCNumber - 1];
1360     int SCCIndex = RC.SCCs.size();
1361     RC.SCCs.push_back(C);
1362     RC.SCCIndices[C] = SCCIndex;
1363     C->OuterRefSCC = &RC;
1364   }
1365 
1366   // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1367   // a leaf and connect it to the rest of the graph's parents lists. This is
1368   // really wasteful. We should instead do this during the DFS to avoid yet
1369   // another edge walk.
1370   for (RefSCC *RC : Result)
1371     G->connectRefSCC(*RC);
1372 
1373   // Now erase all but the root's SCCs.
1374   SCCs.erase(remove_if(SCCs,
1375                        [&](SCC *C) {
1376                          return PostOrderMapping.lookup(&*C->begin()) !=
1377                                 RootPostOrderNumber;
1378                        }),
1379              SCCs.end());
1380   SCCIndices.clear();
1381   for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1382     SCCIndices[SCCs[i]] = i;
1383 
1384 #ifndef NDEBUG
1385   // Now we need to reconnect the current (root) SCC to the graph. We do this
1386   // manually because we can special case our leaf handling and detect errors.
1387   bool IsLeaf = true;
1388 #endif
1389   for (SCC *C : SCCs)
1390     for (Node &N : *C) {
1391       for (Edge &E : N) {
1392         assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1393         RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1394         if (&ChildRC == this)
1395           continue;
1396         ChildRC.Parents.insert(this);
1397 #ifndef NDEBUG
1398         IsLeaf = false;
1399 #endif
1400       }
1401     }
1402 #ifndef NDEBUG
1403   if (!Result.empty())
1404     assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1405                       "SCCs by removing this edge.");
1406   if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
1407     assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1408                       "SCCs before we removed this edge.");
1409 #endif
1410   // And connect both this RefSCC and all the new ones to the correct parents.
1411   // The easiest way to do this is just to re-analyze the old parent set.
1412   SmallVector<RefSCC *, 4> OldParents(Parents.begin(), Parents.end());
1413   Parents.clear();
1414   for (RefSCC *ParentRC : OldParents)
1415     for (SCC &ParentC : *ParentRC)
1416       for (Node &ParentN : ParentC)
1417         for (Edge &E : ParentN) {
1418           assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1419           RefSCC &RC = *G->lookupRefSCC(*E.getNode());
1420           if (&RC != ParentRC)
1421             RC.Parents.insert(ParentRC);
1422         }
1423 
1424   // If this SCC stopped being a leaf through this edge removal, remove it from
1425   // the leaf SCC list. Note that this DTRT in the case where this was never
1426   // a leaf.
1427   // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1428   // entire list if this RefSCC wasn't a leaf before the edge removal.
1429   if (!Result.empty())
1430     G->LeafRefSCCs.erase(
1431         std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1432         G->LeafRefSCCs.end());
1433 
1434 #ifndef NDEBUG
1435   // Verify all of the new RefSCCs.
1436   for (RefSCC *RC : Result)
1437     RC->verify();
1438 #endif
1439 
1440   // Return the new list of SCCs.
1441   return Result;
1442 }
1443 
1444 void LazyCallGraph::RefSCC::handleTrivialEdgeInsertion(Node &SourceN,
1445                                                        Node &TargetN) {
1446   // The only trivial case that requires any graph updates is when we add new
1447   // ref edge and may connect different RefSCCs along that path. This is only
1448   // because of the parents set. Every other part of the graph remains constant
1449   // after this edge insertion.
1450   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
1451   RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1452   if (&TargetRC == this) {
1453 
1454     return;
1455   }
1456 
1457   assert(TargetRC.isDescendantOf(*this) &&
1458          "Target must be a descendant of the Source.");
1459   // The only change required is to add this RefSCC to the parent set of the
1460   // target. This is a set and so idempotent if the edge already existed.
1461   TargetRC.Parents.insert(this);
1462 }
1463 
1464 void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
1465                                                   Node &TargetN) {
1466 #ifndef NDEBUG
1467   // Check that the RefSCC is still valid when we finish.
1468   auto ExitVerifier = make_scope_exit([this] { verify(); });
1469 
1470   // Check that we aren't breaking some invariants of the SCC graph.
1471   SCC &SourceC = *G->lookupSCC(SourceN);
1472   SCC &TargetC = *G->lookupSCC(TargetN);
1473   if (&SourceC != &TargetC)
1474     assert(SourceC.isAncestorOf(TargetC) &&
1475            "Call edge is not trivial in the SCC graph!");
1476 #endif
1477   // First insert it into the source or find the existing edge.
1478   auto InsertResult = SourceN.EdgeIndexMap.insert(
1479       {&TargetN.getFunction(), SourceN.Edges.size()});
1480   if (!InsertResult.second) {
1481     // Already an edge, just update it.
1482     Edge &E = SourceN.Edges[InsertResult.first->second];
1483     if (E.isCall())
1484       return; // Nothing to do!
1485     E.setKind(Edge::Call);
1486   } else {
1487     // Create the new edge.
1488     SourceN.Edges.emplace_back(TargetN, Edge::Call);
1489   }
1490 
1491   // Now that we have the edge, handle the graph fallout.
1492   handleTrivialEdgeInsertion(SourceN, TargetN);
1493 }
1494 
1495 void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
1496 #ifndef NDEBUG
1497   // Check that the RefSCC is still valid when we finish.
1498   auto ExitVerifier = make_scope_exit([this] { verify(); });
1499 
1500   // Check that we aren't breaking some invariants of the RefSCC graph.
1501   RefSCC &SourceRC = *G->lookupRefSCC(SourceN);
1502   RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1503   if (&SourceRC != &TargetRC)
1504     assert(SourceRC.isAncestorOf(TargetRC) &&
1505            "Ref edge is not trivial in the RefSCC graph!");
1506 #endif
1507   // First insert it into the source or find the existing edge.
1508   auto InsertResult = SourceN.EdgeIndexMap.insert(
1509       {&TargetN.getFunction(), SourceN.Edges.size()});
1510   if (!InsertResult.second)
1511     // Already an edge, we're done.
1512     return;
1513 
1514   // Create the new edge.
1515   SourceN.Edges.emplace_back(TargetN, Edge::Ref);
1516 
1517   // Now that we have the edge, handle the graph fallout.
1518   handleTrivialEdgeInsertion(SourceN, TargetN);
1519 }
1520 
1521 void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
1522   assert(SCCMap.empty() && DFSStack.empty() &&
1523          "This method cannot be called after SCCs have been formed!");
1524 
1525   return SourceN.insertEdgeInternal(Target, EK);
1526 }
1527 
1528 void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
1529   assert(SCCMap.empty() && DFSStack.empty() &&
1530          "This method cannot be called after SCCs have been formed!");
1531 
1532   return SourceN.removeEdgeInternal(Target);
1533 }
1534 
1535 void LazyCallGraph::removeDeadFunction(Function &F) {
1536   // FIXME: This is unnecessarily restrictive. We should be able to remove
1537   // functions which recursively call themselves.
1538   assert(F.use_empty() &&
1539          "This routine should only be called on trivially dead functions!");
1540 
1541   auto EII = EntryIndexMap.find(&F);
1542   if (EII != EntryIndexMap.end()) {
1543     EntryEdges[EII->second] = Edge();
1544     EntryIndexMap.erase(EII);
1545   }
1546 
1547   // It's safe to just remove un-visited functions from the RefSCC entry list.
1548   // FIXME: This is a linear operation which could become hot and benefit from
1549   // an index map.
1550   auto RENI = find(RefSCCEntryNodes, &F);
1551   if (RENI != RefSCCEntryNodes.end())
1552     RefSCCEntryNodes.erase(RENI);
1553 
1554   auto NI = NodeMap.find(&F);
1555   if (NI == NodeMap.end())
1556     // Not in the graph at all!
1557     return;
1558 
1559   Node &N = *NI->second;
1560   NodeMap.erase(NI);
1561 
1562   if (SCCMap.empty() && DFSStack.empty()) {
1563     // No SCC walk has begun, so removing this is fine and there is nothing
1564     // else necessary at this point but clearing out the node.
1565     N.clear();
1566     return;
1567   }
1568 
1569   // Check that we aren't going to break the DFS walk.
1570   assert(all_of(DFSStack,
1571                 [&N](const std::pair<Node *, edge_iterator> &Element) {
1572                   return Element.first != &N;
1573                 }) &&
1574          "Tried to remove a function currently in the DFS stack!");
1575   assert(find(PendingRefSCCStack, &N) == PendingRefSCCStack.end() &&
1576          "Tried to remove a function currently pending to add to a RefSCC!");
1577 
1578   // Cannot remove a function which has yet to be visited in the DFS walk, so
1579   // if we have a node at all then we must have an SCC and RefSCC.
1580   auto CI = SCCMap.find(&N);
1581   assert(CI != SCCMap.end() &&
1582          "Tried to remove a node without an SCC after DFS walk started!");
1583   SCC &C = *CI->second;
1584   SCCMap.erase(CI);
1585   RefSCC &RC = C.getOuterRefSCC();
1586 
1587   // This node must be the only member of its SCC as it has no callers, and
1588   // that SCC must be the only member of a RefSCC as it has no references.
1589   // Validate these properties first.
1590   assert(C.size() == 1 && "Dead functions must be in a singular SCC");
1591   assert(RC.size() == 1 && "Dead functions must be in a singular RefSCC");
1592   assert(RC.Parents.empty() && "Cannot have parents of a dead RefSCC!");
1593 
1594   // Now remove this RefSCC from any parents sets and the leaf list.
1595   for (Edge &E : N)
1596     if (Node *TargetN = E.getNode())
1597       if (RefSCC *TargetRC = lookupRefSCC(*TargetN))
1598         TargetRC->Parents.erase(&RC);
1599   // FIXME: This is a linear operation which could become hot and benefit from
1600   // an index map.
1601   auto LRI = find(LeafRefSCCs, &RC);
1602   if (LRI != LeafRefSCCs.end())
1603     LeafRefSCCs.erase(LRI);
1604 
1605   auto RCIndexI = RefSCCIndices.find(&RC);
1606   int RCIndex = RCIndexI->second;
1607   PostOrderRefSCCs.erase(PostOrderRefSCCs.begin() + RCIndex);
1608   RefSCCIndices.erase(RCIndexI);
1609   for (int i = RCIndex, Size = PostOrderRefSCCs.size(); i < Size; ++i)
1610     RefSCCIndices[PostOrderRefSCCs[i]] = i;
1611 
1612   // Finally clear out all the data structures from the node down through the
1613   // components.
1614   N.clear();
1615   C.clear();
1616   RC.clear();
1617 
1618   // Nothing to delete as all the objects are allocated in stable bump pointer
1619   // allocators.
1620 }
1621 
1622 LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1623   return *new (MappedN = BPA.Allocate()) Node(*this, F);
1624 }
1625 
1626 void LazyCallGraph::updateGraphPtrs() {
1627   // Process all nodes updating the graph pointers.
1628   {
1629     SmallVector<Node *, 16> Worklist;
1630     for (Edge &E : EntryEdges)
1631       if (Node *EntryN = E.getNode())
1632         Worklist.push_back(EntryN);
1633 
1634     while (!Worklist.empty()) {
1635       Node *N = Worklist.pop_back_val();
1636       N->G = this;
1637       for (Edge &E : N->Edges)
1638         if (Node *TargetN = E.getNode())
1639           Worklist.push_back(TargetN);
1640     }
1641   }
1642 
1643   // Process all SCCs updating the graph pointers.
1644   {
1645     SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
1646 
1647     while (!Worklist.empty()) {
1648       RefSCC &C = *Worklist.pop_back_val();
1649       C.G = this;
1650       for (RefSCC &ParentC : C.parents())
1651         Worklist.push_back(&ParentC);
1652     }
1653   }
1654 }
1655 
1656 /// Build the internal SCCs for a RefSCC from a sequence of nodes.
1657 ///
1658 /// Appends the SCCs to the provided vector and updates the map with their
1659 /// indices. Both the vector and map must be empty when passed into this
1660 /// routine.
1661 void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1662   assert(RC.SCCs.empty() && "Already built SCCs!");
1663   assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
1664 
1665   for (Node *N : Nodes) {
1666     assert(N->LowLink >= (*Nodes.begin())->LowLink &&
1667            "We cannot have a low link in an SCC lower than its root on the "
1668            "stack!");
1669 
1670     // This node will go into the next RefSCC, clear out its DFS and low link
1671     // as we scan.
1672     N->DFSNumber = N->LowLink = 0;
1673   }
1674 
1675   // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1676   // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1677   // internal storage as we won't need it for the outer graph's DFS any longer.
1678 
1679   SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1680   SmallVector<Node *, 16> PendingSCCStack;
1681 
1682   // Scan down the stack and DFS across the call edges.
1683   for (Node *RootN : Nodes) {
1684     assert(DFSStack.empty() &&
1685            "Cannot begin a new root with a non-empty DFS stack!");
1686     assert(PendingSCCStack.empty() &&
1687            "Cannot begin a new root with pending nodes for an SCC!");
1688 
1689     // Skip any nodes we've already reached in the DFS.
1690     if (RootN->DFSNumber != 0) {
1691       assert(RootN->DFSNumber == -1 &&
1692              "Shouldn't have any mid-DFS root nodes!");
1693       continue;
1694     }
1695 
1696     RootN->DFSNumber = RootN->LowLink = 1;
1697     int NextDFSNumber = 2;
1698 
1699     DFSStack.push_back({RootN, RootN->call_begin()});
1700     do {
1701       Node *N;
1702       call_edge_iterator I;
1703       std::tie(N, I) = DFSStack.pop_back_val();
1704       auto E = N->call_end();
1705       while (I != E) {
1706         Node &ChildN = *I->getNode();
1707         if (ChildN.DFSNumber == 0) {
1708           // We haven't yet visited this child, so descend, pushing the current
1709           // node onto the stack.
1710           DFSStack.push_back({N, I});
1711 
1712           assert(!lookupSCC(ChildN) &&
1713                  "Found a node with 0 DFS number but already in an SCC!");
1714           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1715           N = &ChildN;
1716           I = N->call_begin();
1717           E = N->call_end();
1718           continue;
1719         }
1720 
1721         // If the child has already been added to some child component, it
1722         // couldn't impact the low-link of this parent because it isn't
1723         // connected, and thus its low-link isn't relevant so skip it.
1724         if (ChildN.DFSNumber == -1) {
1725           ++I;
1726           continue;
1727         }
1728 
1729         // Track the lowest linked child as the lowest link for this node.
1730         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1731         if (ChildN.LowLink < N->LowLink)
1732           N->LowLink = ChildN.LowLink;
1733 
1734         // Move to the next edge.
1735         ++I;
1736       }
1737 
1738       // We've finished processing N and its descendents, put it on our pending
1739       // SCC stack to eventually get merged into an SCC of nodes.
1740       PendingSCCStack.push_back(N);
1741 
1742       // If this node is linked to some lower entry, continue walking up the
1743       // stack.
1744       if (N->LowLink != N->DFSNumber)
1745         continue;
1746 
1747       // Otherwise, we've completed an SCC. Append it to our post order list of
1748       // SCCs.
1749       int RootDFSNumber = N->DFSNumber;
1750       // Find the range of the node stack by walking down until we pass the
1751       // root DFS number.
1752       auto SCCNodes = make_range(
1753           PendingSCCStack.rbegin(),
1754           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1755             return N->DFSNumber < RootDFSNumber;
1756           }));
1757       // Form a new SCC out of these nodes and then clear them off our pending
1758       // stack.
1759       RC.SCCs.push_back(createSCC(RC, SCCNodes));
1760       for (Node &N : *RC.SCCs.back()) {
1761         N.DFSNumber = N.LowLink = -1;
1762         SCCMap[&N] = RC.SCCs.back();
1763       }
1764       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1765     } while (!DFSStack.empty());
1766   }
1767 
1768   // Wire up the SCC indices.
1769   for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1770     RC.SCCIndices[RC.SCCs[i]] = i;
1771 }
1772 
1773 // FIXME: We should move callers of this to embed the parent linking and leaf
1774 // tracking into their DFS in order to remove a full walk of all edges.
1775 void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1776   // Walk all edges in the RefSCC (this remains linear as we only do this once
1777   // when we build the RefSCC) to connect it to the parent sets of its
1778   // children.
1779   bool IsLeaf = true;
1780   for (SCC &C : RC)
1781     for (Node &N : C)
1782       for (Edge &E : N) {
1783         assert(E.getNode() &&
1784                "Cannot have a missing node in a visited part of the graph!");
1785         RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1786         if (&ChildRC == &RC)
1787           continue;
1788         ChildRC.Parents.insert(&RC);
1789         IsLeaf = false;
1790       }
1791 
1792   // For the SCCs where we find no child SCCs, add them to the leaf list.
1793   if (IsLeaf)
1794     LeafRefSCCs.push_back(&RC);
1795 }
1796 
1797 bool LazyCallGraph::buildNextRefSCCInPostOrder() {
1798   if (DFSStack.empty()) {
1799     Node *N;
1800     do {
1801       // If we've handled all candidate entry nodes to the SCC forest, we're
1802       // done.
1803       if (RefSCCEntryNodes.empty())
1804         return false;
1805 
1806       N = &get(*RefSCCEntryNodes.pop_back_val());
1807     } while (N->DFSNumber != 0);
1808 
1809     // Found a new root, begin the DFS here.
1810     N->LowLink = N->DFSNumber = 1;
1811     NextDFSNumber = 2;
1812     DFSStack.push_back({N, N->begin()});
1813   }
1814 
1815   for (;;) {
1816     Node *N;
1817     edge_iterator I;
1818     std::tie(N, I) = DFSStack.pop_back_val();
1819 
1820     assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1821                                "before placing a node onto the stack.");
1822 
1823     auto E = N->end();
1824     while (I != E) {
1825       Node &ChildN = I->getNode(*this);
1826       if (ChildN.DFSNumber == 0) {
1827         // We haven't yet visited this child, so descend, pushing the current
1828         // node onto the stack.
1829         DFSStack.push_back({N, N->begin()});
1830 
1831         assert(!SCCMap.count(&ChildN) &&
1832                "Found a node with 0 DFS number but already in an SCC!");
1833         ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1834         N = &ChildN;
1835         I = N->begin();
1836         E = N->end();
1837         continue;
1838       }
1839 
1840       // If the child has already been added to some child component, it
1841       // couldn't impact the low-link of this parent because it isn't
1842       // connected, and thus its low-link isn't relevant so skip it.
1843       if (ChildN.DFSNumber == -1) {
1844         ++I;
1845         continue;
1846       }
1847 
1848       // Track the lowest linked child as the lowest link for this node.
1849       assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1850       if (ChildN.LowLink < N->LowLink)
1851         N->LowLink = ChildN.LowLink;
1852 
1853       // Move to the next edge.
1854       ++I;
1855     }
1856 
1857     // We've finished processing N and its descendents, put it on our pending
1858     // SCC stack to eventually get merged into an SCC of nodes.
1859     PendingRefSCCStack.push_back(N);
1860 
1861     // If this node is linked to some lower entry, continue walking up the
1862     // stack.
1863     if (N->LowLink != N->DFSNumber) {
1864       assert(!DFSStack.empty() &&
1865              "We never found a viable root for an SCC to pop off!");
1866       continue;
1867     }
1868 
1869     // Otherwise, form a new RefSCC from the top of the pending node stack.
1870     int RootDFSNumber = N->DFSNumber;
1871     // Find the range of the node stack by walking down until we pass the
1872     // root DFS number.
1873     auto RefSCCNodes = node_stack_range(
1874         PendingRefSCCStack.rbegin(),
1875         find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1876           return N->DFSNumber < RootDFSNumber;
1877         }));
1878     // Form a new RefSCC out of these nodes and then clear them off our pending
1879     // stack.
1880     RefSCC *NewRC = createRefSCC(*this);
1881     buildSCCs(*NewRC, RefSCCNodes);
1882     connectRefSCC(*NewRC);
1883     PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1884                              PendingRefSCCStack.end());
1885 
1886     // Push the new node into the postorder list and return true indicating we
1887     // successfully grew the postorder sequence by one.
1888     bool Inserted =
1889         RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second;
1890     (void)Inserted;
1891     assert(Inserted && "Cannot already have this RefSCC in the index map!");
1892     PostOrderRefSCCs.push_back(NewRC);
1893     return true;
1894   }
1895 }
1896 
1897 AnalysisKey LazyCallGraphAnalysis::Key;
1898 
1899 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1900 
1901 static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
1902   OS << "  Edges in function: " << N.getFunction().getName() << "\n";
1903   for (const LazyCallGraph::Edge &E : N)
1904     OS << "    " << (E.isCall() ? "call" : "ref ") << " -> "
1905        << E.getFunction().getName() << "\n";
1906 
1907   OS << "\n";
1908 }
1909 
1910 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1911   ptrdiff_t Size = std::distance(C.begin(), C.end());
1912   OS << "    SCC with " << Size << " functions:\n";
1913 
1914   for (LazyCallGraph::Node &N : C)
1915     OS << "      " << N.getFunction().getName() << "\n";
1916 }
1917 
1918 static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1919   ptrdiff_t Size = std::distance(C.begin(), C.end());
1920   OS << "  RefSCC with " << Size << " call SCCs:\n";
1921 
1922   for (LazyCallGraph::SCC &InnerC : C)
1923     printSCC(OS, InnerC);
1924 
1925   OS << "\n";
1926 }
1927 
1928 PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
1929                                                 ModuleAnalysisManager &AM) {
1930   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1931 
1932   OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1933      << "\n\n";
1934 
1935   for (Function &F : M)
1936     printNode(OS, G.get(F));
1937 
1938   for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1939     printRefSCC(OS, C);
1940 
1941   return PreservedAnalyses::all();
1942 }
1943 
1944 LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1945     : OS(OS) {}
1946 
1947 static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1948   std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1949 
1950   for (const LazyCallGraph::Edge &E : N) {
1951     OS << "  " << Name << " -> \""
1952        << DOT::EscapeString(E.getFunction().getName()) << "\"";
1953     if (!E.isCall()) // It is a ref edge.
1954       OS << " [style=dashed,label=\"ref\"]";
1955     OS << ";\n";
1956   }
1957 
1958   OS << "\n";
1959 }
1960 
1961 PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1962                                                    ModuleAnalysisManager &AM) {
1963   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1964 
1965   OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1966 
1967   for (Function &F : M)
1968     printNodeDOT(OS, G.get(F));
1969 
1970   OS << "}\n";
1971 
1972   return PreservedAnalyses::all();
1973 }
1974