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