xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/LazyCallGraph.cpp (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
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/Function.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/InstIterator.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 *CB = dyn_cast<CallBase>(&I))
104         if (Function *Callee = CB->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 #ifdef EXPENSIVE_CHECKS
261     // Verify that all nodes in this SCC can reach all other nodes.
262     SmallVector<Node *, 4> Worklist;
263     SmallPtrSet<Node *, 4> Visited;
264     Worklist.push_back(N);
265     while (!Worklist.empty()) {
266       Node *VisitingNode = Worklist.pop_back_val();
267       if (!Visited.insert(VisitingNode).second)
268         continue;
269       for (Edge &E : (*VisitingNode)->calls())
270         Worklist.push_back(&E.getNode());
271     }
272     for (Node *NodeToVisit : Nodes) {
273       assert(Visited.contains(NodeToVisit) &&
274              "Cannot reach all nodes within SCC");
275     }
276 #endif
277   }
278 }
279 #endif
280 
281 bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
282   if (this == &C)
283     return false;
284 
285   for (Node &N : *this)
286     for (Edge &E : N->calls())
287       if (OuterRefSCC->G->lookupSCC(E.getNode()) == &C)
288         return true;
289 
290   // No edges found.
291   return false;
292 }
293 
294 bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
295   if (this == &TargetC)
296     return false;
297 
298   LazyCallGraph &G = *OuterRefSCC->G;
299 
300   // Start with this SCC.
301   SmallPtrSet<const SCC *, 16> Visited = {this};
302   SmallVector<const SCC *, 16> Worklist = {this};
303 
304   // Walk down the graph until we run out of edges or find a path to TargetC.
305   do {
306     const SCC &C = *Worklist.pop_back_val();
307     for (Node &N : C)
308       for (Edge &E : N->calls()) {
309         SCC *CalleeC = G.lookupSCC(E.getNode());
310         if (!CalleeC)
311           continue;
312 
313         // If the callee's SCC is the TargetC, we're done.
314         if (CalleeC == &TargetC)
315           return true;
316 
317         // If this is the first time we've reached this SCC, put it on the
318         // worklist to recurse through.
319         if (Visited.insert(CalleeC).second)
320           Worklist.push_back(CalleeC);
321       }
322   } while (!Worklist.empty());
323 
324   // No paths found.
325   return false;
326 }
327 
328 LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
329 
330 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
331 LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
332   dbgs() << *this << '\n';
333 }
334 #endif
335 
336 #ifndef NDEBUG
337 void LazyCallGraph::RefSCC::verify() {
338   assert(G && "Can't have a null graph!");
339   assert(!SCCs.empty() && "Can't have an empty SCC!");
340 
341   // Verify basic properties of the SCCs.
342   SmallPtrSet<SCC *, 4> SCCSet;
343   for (SCC *C : SCCs) {
344     assert(C && "Can't have a null SCC!");
345     C->verify();
346     assert(&C->getOuterRefSCC() == this &&
347            "SCC doesn't think it is inside this RefSCC!");
348     bool Inserted = SCCSet.insert(C).second;
349     assert(Inserted && "Found a duplicate SCC!");
350     auto IndexIt = SCCIndices.find(C);
351     assert(IndexIt != SCCIndices.end() &&
352            "Found an SCC that doesn't have an index!");
353   }
354 
355   // Check that our indices map correctly.
356   for (auto &SCCIndexPair : SCCIndices) {
357     SCC *C = SCCIndexPair.first;
358     int i = SCCIndexPair.second;
359     assert(C && "Can't have a null SCC in the indices!");
360     assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
361     assert(SCCs[i] == C && "Index doesn't point to SCC!");
362   }
363 
364   // Check that the SCCs are in fact in post-order.
365   for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
366     SCC &SourceSCC = *SCCs[i];
367     for (Node &N : SourceSCC)
368       for (Edge &E : *N) {
369         if (!E.isCall())
370           continue;
371         SCC &TargetSCC = *G->lookupSCC(E.getNode());
372         if (&TargetSCC.getOuterRefSCC() == this) {
373           assert(SCCIndices.find(&TargetSCC)->second <= i &&
374                  "Edge between SCCs violates post-order relationship.");
375           continue;
376         }
377       }
378   }
379 
380 #ifdef EXPENSIVE_CHECKS
381   // Verify that all nodes in this RefSCC can reach all other nodes.
382   SmallVector<Node *> Nodes;
383   for (SCC *C : SCCs) {
384     for (Node &N : *C)
385       Nodes.push_back(&N);
386   }
387   for (Node *N : Nodes) {
388     SmallVector<Node *, 4> Worklist;
389     SmallPtrSet<Node *, 4> Visited;
390     Worklist.push_back(N);
391     while (!Worklist.empty()) {
392       Node *VisitingNode = Worklist.pop_back_val();
393       if (!Visited.insert(VisitingNode).second)
394         continue;
395       for (Edge &E : **VisitingNode)
396         Worklist.push_back(&E.getNode());
397     }
398     for (Node *NodeToVisit : Nodes) {
399       assert(Visited.contains(NodeToVisit) &&
400              "Cannot reach all nodes within RefSCC");
401     }
402   }
403 #endif
404 }
405 #endif
406 
407 bool LazyCallGraph::RefSCC::isParentOf(const RefSCC &RC) const {
408   if (&RC == this)
409     return false;
410 
411   // Search all edges to see if this is a parent.
412   for (SCC &C : *this)
413     for (Node &N : C)
414       for (Edge &E : *N)
415         if (G->lookupRefSCC(E.getNode()) == &RC)
416           return true;
417 
418   return false;
419 }
420 
421 bool LazyCallGraph::RefSCC::isAncestorOf(const RefSCC &RC) const {
422   if (&RC == this)
423     return false;
424 
425   // For each descendant of this RefSCC, see if one of its children is the
426   // argument. If not, add that descendant to the worklist and continue
427   // searching.
428   SmallVector<const RefSCC *, 4> Worklist;
429   SmallPtrSet<const RefSCC *, 4> Visited;
430   Worklist.push_back(this);
431   Visited.insert(this);
432   do {
433     const RefSCC &DescendantRC = *Worklist.pop_back_val();
434     for (SCC &C : DescendantRC)
435       for (Node &N : C)
436         for (Edge &E : *N) {
437           auto *ChildRC = G->lookupRefSCC(E.getNode());
438           if (ChildRC == &RC)
439             return true;
440           if (!ChildRC || !Visited.insert(ChildRC).second)
441             continue;
442           Worklist.push_back(ChildRC);
443         }
444   } while (!Worklist.empty());
445 
446   return false;
447 }
448 
449 /// Generic helper that updates a postorder sequence of SCCs for a potentially
450 /// cycle-introducing edge insertion.
451 ///
452 /// A postorder sequence of SCCs of a directed graph has one fundamental
453 /// property: all deges in the DAG of SCCs point "up" the sequence. That is,
454 /// all edges in the SCC DAG point to prior SCCs in the sequence.
455 ///
456 /// This routine both updates a postorder sequence and uses that sequence to
457 /// compute the set of SCCs connected into a cycle. It should only be called to
458 /// insert a "downward" edge which will require changing the sequence to
459 /// restore it to a postorder.
460 ///
461 /// When inserting an edge from an earlier SCC to a later SCC in some postorder
462 /// sequence, all of the SCCs which may be impacted are in the closed range of
463 /// those two within the postorder sequence. The algorithm used here to restore
464 /// the state is as follows:
465 ///
466 /// 1) Starting from the source SCC, construct a set of SCCs which reach the
467 ///    source SCC consisting of just the source SCC. Then scan toward the
468 ///    target SCC in postorder and for each SCC, if it has an edge to an SCC
469 ///    in the set, add it to the set. Otherwise, the source SCC is not
470 ///    a successor, move it in the postorder sequence to immediately before
471 ///    the source SCC, shifting the source SCC and all SCCs in the set one
472 ///    position toward the target SCC. Stop scanning after processing the
473 ///    target SCC.
474 /// 2) If the source SCC is now past the target SCC in the postorder sequence,
475 ///    and thus the new edge will flow toward the start, we are done.
476 /// 3) Otherwise, starting from the target SCC, walk all edges which reach an
477 ///    SCC between the source and the target, and add them to the set of
478 ///    connected SCCs, then recurse through them. Once a complete set of the
479 ///    SCCs the target connects to is known, hoist the remaining SCCs between
480 ///    the source and the target to be above the target. Note that there is no
481 ///    need to process the source SCC, it is already known to connect.
482 /// 4) At this point, all of the SCCs in the closed range between the source
483 ///    SCC and the target SCC in the postorder sequence are connected,
484 ///    including the target SCC and the source SCC. Inserting the edge from
485 ///    the source SCC to the target SCC will form a cycle out of precisely
486 ///    these SCCs. Thus we can merge all of the SCCs in this closed range into
487 ///    a single SCC.
488 ///
489 /// This process has various important properties:
490 /// - Only mutates the SCCs when adding the edge actually changes the SCC
491 ///   structure.
492 /// - Never mutates SCCs which are unaffected by the change.
493 /// - Updates the postorder sequence to correctly satisfy the postorder
494 ///   constraint after the edge is inserted.
495 /// - Only reorders SCCs in the closed postorder sequence from the source to
496 ///   the target, so easy to bound how much has changed even in the ordering.
497 /// - Big-O is the number of edges in the closed postorder range of SCCs from
498 ///   source to target.
499 ///
500 /// This helper routine, in addition to updating the postorder sequence itself
501 /// will also update a map from SCCs to indices within that sequence.
502 ///
503 /// The sequence and the map must operate on pointers to the SCC type.
504 ///
505 /// Two callbacks must be provided. The first computes the subset of SCCs in
506 /// the postorder closed range from the source to the target which connect to
507 /// the source SCC via some (transitive) set of edges. The second computes the
508 /// subset of the same range which the target SCC connects to via some
509 /// (transitive) set of edges. Both callbacks should populate the set argument
510 /// provided.
511 template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
512           typename ComputeSourceConnectedSetCallableT,
513           typename ComputeTargetConnectedSetCallableT>
514 static iterator_range<typename PostorderSequenceT::iterator>
515 updatePostorderSequenceForEdgeInsertion(
516     SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
517     SCCIndexMapT &SCCIndices,
518     ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
519     ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
520   int SourceIdx = SCCIndices[&SourceSCC];
521   int TargetIdx = SCCIndices[&TargetSCC];
522   assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
523 
524   SmallPtrSet<SCCT *, 4> ConnectedSet;
525 
526   // Compute the SCCs which (transitively) reach the source.
527   ComputeSourceConnectedSet(ConnectedSet);
528 
529   // Partition the SCCs in this part of the port-order sequence so only SCCs
530   // connecting to the source remain between it and the target. This is
531   // a benign partition as it preserves postorder.
532   auto SourceI = std::stable_partition(
533       SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
534       [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
535   for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
536     SCCIndices.find(SCCs[i])->second = i;
537 
538   // If the target doesn't connect to the source, then we've corrected the
539   // post-order and there are no cycles formed.
540   if (!ConnectedSet.count(&TargetSCC)) {
541     assert(SourceI > (SCCs.begin() + SourceIdx) &&
542            "Must have moved the source to fix the post-order.");
543     assert(*std::prev(SourceI) == &TargetSCC &&
544            "Last SCC to move should have bene the target.");
545 
546     // Return an empty range at the target SCC indicating there is nothing to
547     // merge.
548     return make_range(std::prev(SourceI), std::prev(SourceI));
549   }
550 
551   assert(SCCs[TargetIdx] == &TargetSCC &&
552          "Should not have moved target if connected!");
553   SourceIdx = SourceI - SCCs.begin();
554   assert(SCCs[SourceIdx] == &SourceSCC &&
555          "Bad updated index computation for the source SCC!");
556 
557 
558   // See whether there are any remaining intervening SCCs between the source
559   // and target. If so we need to make sure they all are reachable form the
560   // target.
561   if (SourceIdx + 1 < TargetIdx) {
562     ConnectedSet.clear();
563     ComputeTargetConnectedSet(ConnectedSet);
564 
565     // Partition SCCs so that only SCCs reached from the target remain between
566     // the source and the target. This preserves postorder.
567     auto TargetI = std::stable_partition(
568         SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
569         [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
570     for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
571       SCCIndices.find(SCCs[i])->second = i;
572     TargetIdx = std::prev(TargetI) - SCCs.begin();
573     assert(SCCs[TargetIdx] == &TargetSCC &&
574            "Should always end with the target!");
575   }
576 
577   // At this point, we know that connecting source to target forms a cycle
578   // because target connects back to source, and we know that all of the SCCs
579   // between the source and target in the postorder sequence participate in that
580   // cycle.
581   return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
582 }
583 
584 bool
585 LazyCallGraph::RefSCC::switchInternalEdgeToCall(
586     Node &SourceN, Node &TargetN,
587     function_ref<void(ArrayRef<SCC *> MergeSCCs)> MergeCB) {
588   assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
589   SmallVector<SCC *, 1> DeletedSCCs;
590 
591 #ifndef NDEBUG
592   // In a debug build, verify the RefSCC is valid to start with and when this
593   // routine finishes.
594   verify();
595   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
596 #endif
597 
598   SCC &SourceSCC = *G->lookupSCC(SourceN);
599   SCC &TargetSCC = *G->lookupSCC(TargetN);
600 
601   // If the two nodes are already part of the same SCC, we're also done as
602   // we've just added more connectivity.
603   if (&SourceSCC == &TargetSCC) {
604     SourceN->setEdgeKind(TargetN, Edge::Call);
605     return false; // No new cycle.
606   }
607 
608   // At this point we leverage the postorder list of SCCs to detect when the
609   // insertion of an edge changes the SCC structure in any way.
610   //
611   // First and foremost, we can eliminate the need for any changes when the
612   // edge is toward the beginning of the postorder sequence because all edges
613   // flow in that direction already. Thus adding a new one cannot form a cycle.
614   int SourceIdx = SCCIndices[&SourceSCC];
615   int TargetIdx = SCCIndices[&TargetSCC];
616   if (TargetIdx < SourceIdx) {
617     SourceN->setEdgeKind(TargetN, Edge::Call);
618     return false; // No new cycle.
619   }
620 
621   // Compute the SCCs which (transitively) reach the source.
622   auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
623 #ifndef NDEBUG
624     // Check that the RefSCC is still valid before computing this as the
625     // results will be nonsensical of we've broken its invariants.
626     verify();
627 #endif
628     ConnectedSet.insert(&SourceSCC);
629     auto IsConnected = [&](SCC &C) {
630       for (Node &N : C)
631         for (Edge &E : N->calls())
632           if (ConnectedSet.count(G->lookupSCC(E.getNode())))
633             return true;
634 
635       return false;
636     };
637 
638     for (SCC *C :
639          make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
640       if (IsConnected(*C))
641         ConnectedSet.insert(C);
642   };
643 
644   // Use a normal worklist to find which SCCs the target connects to. We still
645   // bound the search based on the range in the postorder list we care about,
646   // but because this is forward connectivity we just "recurse" through the
647   // edges.
648   auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
649 #ifndef NDEBUG
650     // Check that the RefSCC is still valid before computing this as the
651     // results will be nonsensical of we've broken its invariants.
652     verify();
653 #endif
654     ConnectedSet.insert(&TargetSCC);
655     SmallVector<SCC *, 4> Worklist;
656     Worklist.push_back(&TargetSCC);
657     do {
658       SCC &C = *Worklist.pop_back_val();
659       for (Node &N : C)
660         for (Edge &E : *N) {
661           if (!E.isCall())
662             continue;
663           SCC &EdgeC = *G->lookupSCC(E.getNode());
664           if (&EdgeC.getOuterRefSCC() != this)
665             // Not in this RefSCC...
666             continue;
667           if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
668             // Not in the postorder sequence between source and target.
669             continue;
670 
671           if (ConnectedSet.insert(&EdgeC).second)
672             Worklist.push_back(&EdgeC);
673         }
674     } while (!Worklist.empty());
675   };
676 
677   // Use a generic helper to update the postorder sequence of SCCs and return
678   // a range of any SCCs connected into a cycle by inserting this edge. This
679   // routine will also take care of updating the indices into the postorder
680   // sequence.
681   auto MergeRange = updatePostorderSequenceForEdgeInsertion(
682       SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
683       ComputeTargetConnectedSet);
684 
685   // Run the user's callback on the merged SCCs before we actually merge them.
686   if (MergeCB)
687     MergeCB(makeArrayRef(MergeRange.begin(), MergeRange.end()));
688 
689   // If the merge range is empty, then adding the edge didn't actually form any
690   // new cycles. We're done.
691   if (MergeRange.empty()) {
692     // Now that the SCC structure is finalized, flip the kind to call.
693     SourceN->setEdgeKind(TargetN, Edge::Call);
694     return false; // No new cycle.
695   }
696 
697 #ifndef NDEBUG
698   // Before merging, check that the RefSCC remains valid after all the
699   // postorder updates.
700   verify();
701 #endif
702 
703   // Otherwise we need to merge all of the SCCs in the cycle into a single
704   // result SCC.
705   //
706   // NB: We merge into the target because all of these functions were already
707   // reachable from the target, meaning any SCC-wide properties deduced about it
708   // other than the set of functions within it will not have changed.
709   for (SCC *C : MergeRange) {
710     assert(C != &TargetSCC &&
711            "We merge *into* the target and shouldn't process it here!");
712     SCCIndices.erase(C);
713     TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
714     for (Node *N : C->Nodes)
715       G->SCCMap[N] = &TargetSCC;
716     C->clear();
717     DeletedSCCs.push_back(C);
718   }
719 
720   // Erase the merged SCCs from the list and update the indices of the
721   // remaining SCCs.
722   int IndexOffset = MergeRange.end() - MergeRange.begin();
723   auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
724   for (SCC *C : make_range(EraseEnd, SCCs.end()))
725     SCCIndices[C] -= IndexOffset;
726 
727   // Now that the SCC structure is finalized, flip the kind to call.
728   SourceN->setEdgeKind(TargetN, Edge::Call);
729 
730   // And we're done, but we did form a new cycle.
731   return true;
732 }
733 
734 void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
735                                                            Node &TargetN) {
736   assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
737 
738 #ifndef NDEBUG
739   // In a debug build, verify the RefSCC is valid to start with and when this
740   // routine finishes.
741   verify();
742   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
743 #endif
744 
745   assert(G->lookupRefSCC(SourceN) == this &&
746          "Source must be in this RefSCC.");
747   assert(G->lookupRefSCC(TargetN) == this &&
748          "Target must be in this RefSCC.");
749   assert(G->lookupSCC(SourceN) != G->lookupSCC(TargetN) &&
750          "Source and Target must be in separate SCCs for this to be trivial!");
751 
752   // Set the edge kind.
753   SourceN->setEdgeKind(TargetN, Edge::Ref);
754 }
755 
756 iterator_range<LazyCallGraph::RefSCC::iterator>
757 LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
758   assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
759 
760 #ifndef NDEBUG
761   // In a debug build, verify the RefSCC is valid to start with and when this
762   // routine finishes.
763   verify();
764   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
765 #endif
766 
767   assert(G->lookupRefSCC(SourceN) == this &&
768          "Source must be in this RefSCC.");
769   assert(G->lookupRefSCC(TargetN) == this &&
770          "Target must be in this RefSCC.");
771 
772   SCC &TargetSCC = *G->lookupSCC(TargetN);
773   assert(G->lookupSCC(SourceN) == &TargetSCC && "Source and Target must be in "
774                                                 "the same SCC to require the "
775                                                 "full CG update.");
776 
777   // Set the edge kind.
778   SourceN->setEdgeKind(TargetN, Edge::Ref);
779 
780   // Otherwise we are removing a call edge from a single SCC. This may break
781   // the cycle. In order to compute the new set of SCCs, we need to do a small
782   // DFS over the nodes within the SCC to form any sub-cycles that remain as
783   // distinct SCCs and compute a postorder over the resulting SCCs.
784   //
785   // However, we specially handle the target node. The target node is known to
786   // reach all other nodes in the original SCC by definition. This means that
787   // we want the old SCC to be replaced with an SCC containing that node as it
788   // will be the root of whatever SCC DAG results from the DFS. Assumptions
789   // about an SCC such as the set of functions called will continue to hold,
790   // etc.
791 
792   SCC &OldSCC = TargetSCC;
793   SmallVector<std::pair<Node *, EdgeSequence::call_iterator>, 16> DFSStack;
794   SmallVector<Node *, 16> PendingSCCStack;
795   SmallVector<SCC *, 4> NewSCCs;
796 
797   // Prepare the nodes for a fresh DFS.
798   SmallVector<Node *, 16> Worklist;
799   Worklist.swap(OldSCC.Nodes);
800   for (Node *N : Worklist) {
801     N->DFSNumber = N->LowLink = 0;
802     G->SCCMap.erase(N);
803   }
804 
805   // Force the target node to be in the old SCC. This also enables us to take
806   // a very significant short-cut in the standard Tarjan walk to re-form SCCs
807   // below: whenever we build an edge that reaches the target node, we know
808   // that the target node eventually connects back to all other nodes in our
809   // walk. As a consequence, we can detect and handle participants in that
810   // cycle without walking all the edges that form this connection, and instead
811   // by relying on the fundamental guarantee coming into this operation (all
812   // nodes are reachable from the target due to previously forming an SCC).
813   TargetN.DFSNumber = TargetN.LowLink = -1;
814   OldSCC.Nodes.push_back(&TargetN);
815   G->SCCMap[&TargetN] = &OldSCC;
816 
817   // Scan down the stack and DFS across the call edges.
818   for (Node *RootN : Worklist) {
819     assert(DFSStack.empty() &&
820            "Cannot begin a new root with a non-empty DFS stack!");
821     assert(PendingSCCStack.empty() &&
822            "Cannot begin a new root with pending nodes for an SCC!");
823 
824     // Skip any nodes we've already reached in the DFS.
825     if (RootN->DFSNumber != 0) {
826       assert(RootN->DFSNumber == -1 &&
827              "Shouldn't have any mid-DFS root nodes!");
828       continue;
829     }
830 
831     RootN->DFSNumber = RootN->LowLink = 1;
832     int NextDFSNumber = 2;
833 
834     DFSStack.push_back({RootN, (*RootN)->call_begin()});
835     do {
836       Node *N;
837       EdgeSequence::call_iterator I;
838       std::tie(N, I) = DFSStack.pop_back_val();
839       auto E = (*N)->call_end();
840       while (I != E) {
841         Node &ChildN = I->getNode();
842         if (ChildN.DFSNumber == 0) {
843           // We haven't yet visited this child, so descend, pushing the current
844           // node onto the stack.
845           DFSStack.push_back({N, I});
846 
847           assert(!G->SCCMap.count(&ChildN) &&
848                  "Found a node with 0 DFS number but already in an SCC!");
849           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
850           N = &ChildN;
851           I = (*N)->call_begin();
852           E = (*N)->call_end();
853           continue;
854         }
855 
856         // Check for the child already being part of some component.
857         if (ChildN.DFSNumber == -1) {
858           if (G->lookupSCC(ChildN) == &OldSCC) {
859             // If the child is part of the old SCC, we know that it can reach
860             // every other node, so we have formed a cycle. Pull the entire DFS
861             // and pending stacks into it. See the comment above about setting
862             // up the old SCC for why we do this.
863             int OldSize = OldSCC.size();
864             OldSCC.Nodes.push_back(N);
865             OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
866             PendingSCCStack.clear();
867             while (!DFSStack.empty())
868               OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
869             for (Node &N : drop_begin(OldSCC, OldSize)) {
870               N.DFSNumber = N.LowLink = -1;
871               G->SCCMap[&N] = &OldSCC;
872             }
873             N = nullptr;
874             break;
875           }
876 
877           // If the child has already been added to some child component, it
878           // couldn't impact the low-link of this parent because it isn't
879           // connected, and thus its low-link isn't relevant so skip it.
880           ++I;
881           continue;
882         }
883 
884         // Track the lowest linked child as the lowest link for this node.
885         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
886         if (ChildN.LowLink < N->LowLink)
887           N->LowLink = ChildN.LowLink;
888 
889         // Move to the next edge.
890         ++I;
891       }
892       if (!N)
893         // Cleared the DFS early, start another round.
894         break;
895 
896       // We've finished processing N and its descendants, put it on our pending
897       // SCC stack to eventually get merged into an SCC of nodes.
898       PendingSCCStack.push_back(N);
899 
900       // If this node is linked to some lower entry, continue walking up the
901       // stack.
902       if (N->LowLink != N->DFSNumber)
903         continue;
904 
905       // Otherwise, we've completed an SCC. Append it to our post order list of
906       // SCCs.
907       int RootDFSNumber = N->DFSNumber;
908       // Find the range of the node stack by walking down until we pass the
909       // root DFS number.
910       auto SCCNodes = make_range(
911           PendingSCCStack.rbegin(),
912           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
913             return N->DFSNumber < RootDFSNumber;
914           }));
915 
916       // Form a new SCC out of these nodes and then clear them off our pending
917       // stack.
918       NewSCCs.push_back(G->createSCC(*this, SCCNodes));
919       for (Node &N : *NewSCCs.back()) {
920         N.DFSNumber = N.LowLink = -1;
921         G->SCCMap[&N] = NewSCCs.back();
922       }
923       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
924     } while (!DFSStack.empty());
925   }
926 
927   // Insert the remaining SCCs before the old one. The old SCC can reach all
928   // other SCCs we form because it contains the target node of the removed edge
929   // of the old SCC. This means that we will have edges into all of the new
930   // SCCs, which means the old one must come last for postorder.
931   int OldIdx = SCCIndices[&OldSCC];
932   SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
933 
934   // Update the mapping from SCC* to index to use the new SCC*s, and remove the
935   // old SCC from the mapping.
936   for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
937     SCCIndices[SCCs[Idx]] = Idx;
938 
939   return make_range(SCCs.begin() + OldIdx,
940                     SCCs.begin() + OldIdx + NewSCCs.size());
941 }
942 
943 void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
944                                                      Node &TargetN) {
945   assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
946 
947   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
948   assert(G->lookupRefSCC(TargetN) != this &&
949          "Target must not be in this RefSCC.");
950 #ifdef EXPENSIVE_CHECKS
951   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
952          "Target must be a descendant of the Source.");
953 #endif
954 
955   // Edges between RefSCCs are the same regardless of call or ref, so we can
956   // just flip the edge here.
957   SourceN->setEdgeKind(TargetN, Edge::Call);
958 
959 #ifndef NDEBUG
960   // Check that the RefSCC is still valid.
961   verify();
962 #endif
963 }
964 
965 void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
966                                                     Node &TargetN) {
967   assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
968 
969   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
970   assert(G->lookupRefSCC(TargetN) != this &&
971          "Target must not be in this RefSCC.");
972 #ifdef EXPENSIVE_CHECKS
973   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
974          "Target must be a descendant of the Source.");
975 #endif
976 
977   // Edges between RefSCCs are the same regardless of call or ref, so we can
978   // just flip the edge here.
979   SourceN->setEdgeKind(TargetN, Edge::Ref);
980 
981 #ifndef NDEBUG
982   // Check that the RefSCC is still valid.
983   verify();
984 #endif
985 }
986 
987 void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
988                                                   Node &TargetN) {
989   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
990   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
991 
992   SourceN->insertEdgeInternal(TargetN, Edge::Ref);
993 
994 #ifndef NDEBUG
995   // Check that the RefSCC is still valid.
996   verify();
997 #endif
998 }
999 
1000 void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
1001                                                Edge::Kind EK) {
1002   // First insert it into the caller.
1003   SourceN->insertEdgeInternal(TargetN, EK);
1004 
1005   assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
1006 
1007   assert(G->lookupRefSCC(TargetN) != this &&
1008          "Target must not be in this RefSCC.");
1009 #ifdef EXPENSIVE_CHECKS
1010   assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
1011          "Target must be a descendant of the Source.");
1012 #endif
1013 
1014 #ifndef NDEBUG
1015   // Check that the RefSCC is still valid.
1016   verify();
1017 #endif
1018 }
1019 
1020 SmallVector<LazyCallGraph::RefSCC *, 1>
1021 LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
1022   assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
1023   RefSCC &SourceC = *G->lookupRefSCC(SourceN);
1024   assert(&SourceC != this && "Source must not be in this RefSCC.");
1025 #ifdef EXPENSIVE_CHECKS
1026   assert(SourceC.isDescendantOf(*this) &&
1027          "Source must be a descendant of the Target.");
1028 #endif
1029 
1030   SmallVector<RefSCC *, 1> DeletedRefSCCs;
1031 
1032 #ifndef NDEBUG
1033   // In a debug build, verify the RefSCC is valid to start with and when this
1034   // routine finishes.
1035   verify();
1036   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1037 #endif
1038 
1039   int SourceIdx = G->RefSCCIndices[&SourceC];
1040   int TargetIdx = G->RefSCCIndices[this];
1041   assert(SourceIdx < TargetIdx &&
1042          "Postorder list doesn't see edge as incoming!");
1043 
1044   // Compute the RefSCCs which (transitively) reach the source. We do this by
1045   // working backwards from the source using the parent set in each RefSCC,
1046   // skipping any RefSCCs that don't fall in the postorder range. This has the
1047   // advantage of walking the sparser parent edge (in high fan-out graphs) but
1048   // more importantly this removes examining all forward edges in all RefSCCs
1049   // within the postorder range which aren't in fact connected. Only connected
1050   // RefSCCs (and their edges) are visited here.
1051   auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
1052     Set.insert(&SourceC);
1053     auto IsConnected = [&](RefSCC &RC) {
1054       for (SCC &C : RC)
1055         for (Node &N : C)
1056           for (Edge &E : *N)
1057             if (Set.count(G->lookupRefSCC(E.getNode())))
1058               return true;
1059 
1060       return false;
1061     };
1062 
1063     for (RefSCC *C : make_range(G->PostOrderRefSCCs.begin() + SourceIdx + 1,
1064                                 G->PostOrderRefSCCs.begin() + TargetIdx + 1))
1065       if (IsConnected(*C))
1066         Set.insert(C);
1067   };
1068 
1069   // Use a normal worklist to find which SCCs the target connects to. We still
1070   // bound the search based on the range in the postorder list we care about,
1071   // but because this is forward connectivity we just "recurse" through the
1072   // edges.
1073   auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
1074     Set.insert(this);
1075     SmallVector<RefSCC *, 4> Worklist;
1076     Worklist.push_back(this);
1077     do {
1078       RefSCC &RC = *Worklist.pop_back_val();
1079       for (SCC &C : RC)
1080         for (Node &N : C)
1081           for (Edge &E : *N) {
1082             RefSCC &EdgeRC = *G->lookupRefSCC(E.getNode());
1083             if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
1084               // Not in the postorder sequence between source and target.
1085               continue;
1086 
1087             if (Set.insert(&EdgeRC).second)
1088               Worklist.push_back(&EdgeRC);
1089           }
1090     } while (!Worklist.empty());
1091   };
1092 
1093   // Use a generic helper to update the postorder sequence of RefSCCs and return
1094   // a range of any RefSCCs connected into a cycle by inserting this edge. This
1095   // routine will also take care of updating the indices into the postorder
1096   // sequence.
1097   iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
1098       updatePostorderSequenceForEdgeInsertion(
1099           SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
1100           ComputeSourceConnectedSet, ComputeTargetConnectedSet);
1101 
1102   // Build a set so we can do fast tests for whether a RefSCC will end up as
1103   // part of the merged RefSCC.
1104   SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end());
1105 
1106   // This RefSCC will always be part of that set, so just insert it here.
1107   MergeSet.insert(this);
1108 
1109   // Now that we have identified all of the SCCs which need to be merged into
1110   // a connected set with the inserted edge, merge all of them into this SCC.
1111   SmallVector<SCC *, 16> MergedSCCs;
1112   int SCCIndex = 0;
1113   for (RefSCC *RC : MergeRange) {
1114     assert(RC != this && "We're merging into the target RefSCC, so it "
1115                          "shouldn't be in the range.");
1116 
1117     // Walk the inner SCCs to update their up-pointer and walk all the edges to
1118     // update any parent sets.
1119     // FIXME: We should try to find a way to avoid this (rather expensive) edge
1120     // walk by updating the parent sets in some other manner.
1121     for (SCC &InnerC : *RC) {
1122       InnerC.OuterRefSCC = this;
1123       SCCIndices[&InnerC] = SCCIndex++;
1124       for (Node &N : InnerC)
1125         G->SCCMap[&N] = &InnerC;
1126     }
1127 
1128     // Now merge in the SCCs. We can actually move here so try to reuse storage
1129     // the first time through.
1130     if (MergedSCCs.empty())
1131       MergedSCCs = std::move(RC->SCCs);
1132     else
1133       MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
1134     RC->SCCs.clear();
1135     DeletedRefSCCs.push_back(RC);
1136   }
1137 
1138   // Append our original SCCs to the merged list and move it into place.
1139   for (SCC &InnerC : *this)
1140     SCCIndices[&InnerC] = SCCIndex++;
1141   MergedSCCs.append(SCCs.begin(), SCCs.end());
1142   SCCs = std::move(MergedSCCs);
1143 
1144   // Remove the merged away RefSCCs from the post order sequence.
1145   for (RefSCC *RC : MergeRange)
1146     G->RefSCCIndices.erase(RC);
1147   int IndexOffset = MergeRange.end() - MergeRange.begin();
1148   auto EraseEnd =
1149       G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
1150   for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
1151     G->RefSCCIndices[RC] -= IndexOffset;
1152 
1153   // At this point we have a merged RefSCC with a post-order SCCs list, just
1154   // connect the nodes to form the new edge.
1155   SourceN->insertEdgeInternal(TargetN, Edge::Ref);
1156 
1157   // We return the list of SCCs which were merged so that callers can
1158   // invalidate any data they have associated with those SCCs. Note that these
1159   // SCCs are no longer in an interesting state (they are totally empty) but
1160   // the pointers will remain stable for the life of the graph itself.
1161   return DeletedRefSCCs;
1162 }
1163 
1164 void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
1165   assert(G->lookupRefSCC(SourceN) == this &&
1166          "The source must be a member of this RefSCC.");
1167   assert(G->lookupRefSCC(TargetN) != this &&
1168          "The target must not be a member of this RefSCC");
1169 
1170 #ifndef NDEBUG
1171   // In a debug build, verify the RefSCC is valid to start with and when this
1172   // routine finishes.
1173   verify();
1174   auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1175 #endif
1176 
1177   // First remove it from the node.
1178   bool Removed = SourceN->removeEdgeInternal(TargetN);
1179   (void)Removed;
1180   assert(Removed && "Target not in the edge set for this caller?");
1181 }
1182 
1183 SmallVector<LazyCallGraph::RefSCC *, 1>
1184 LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN,
1185                                              ArrayRef<Node *> TargetNs) {
1186   // We return a list of the resulting *new* RefSCCs in post-order.
1187   SmallVector<RefSCC *, 1> Result;
1188 
1189 #ifndef NDEBUG
1190   // In a debug build, verify the RefSCC is valid to start with and that either
1191   // we return an empty list of result RefSCCs and this RefSCC remains valid,
1192   // or we return new RefSCCs and this RefSCC is dead.
1193   verify();
1194   auto VerifyOnExit = make_scope_exit([&]() {
1195     // If we didn't replace our RefSCC with new ones, check that this one
1196     // remains valid.
1197     if (G)
1198       verify();
1199   });
1200 #endif
1201 
1202   // First remove the actual edges.
1203   for (Node *TargetN : TargetNs) {
1204     assert(!(*SourceN)[*TargetN].isCall() &&
1205            "Cannot remove a call edge, it must first be made a ref edge");
1206 
1207     bool Removed = SourceN->removeEdgeInternal(*TargetN);
1208     (void)Removed;
1209     assert(Removed && "Target not in the edge set for this caller?");
1210   }
1211 
1212   // Direct self references don't impact the ref graph at all.
1213   if (llvm::all_of(TargetNs,
1214                    [&](Node *TargetN) { return &SourceN == TargetN; }))
1215     return Result;
1216 
1217   // If all targets are in the same SCC as the source, because no call edges
1218   // were removed there is no RefSCC structure change.
1219   SCC &SourceC = *G->lookupSCC(SourceN);
1220   if (llvm::all_of(TargetNs, [&](Node *TargetN) {
1221         return G->lookupSCC(*TargetN) == &SourceC;
1222       }))
1223     return Result;
1224 
1225   // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1226   // for each inner SCC. We store these inside the low-link field of the nodes
1227   // rather than associated with SCCs because this saves a round-trip through
1228   // the node->SCC map and in the common case, SCCs are small. We will verify
1229   // that we always give the same number to every node in the SCC such that
1230   // these are equivalent.
1231   int PostOrderNumber = 0;
1232 
1233   // Reset all the other nodes to prepare for a DFS over them, and add them to
1234   // our worklist.
1235   SmallVector<Node *, 8> Worklist;
1236   for (SCC *C : SCCs) {
1237     for (Node &N : *C)
1238       N.DFSNumber = N.LowLink = 0;
1239 
1240     Worklist.append(C->Nodes.begin(), C->Nodes.end());
1241   }
1242 
1243   // Track the number of nodes in this RefSCC so that we can quickly recognize
1244   // an important special case of the edge removal not breaking the cycle of
1245   // this RefSCC.
1246   const int NumRefSCCNodes = Worklist.size();
1247 
1248   SmallVector<std::pair<Node *, EdgeSequence::iterator>, 4> DFSStack;
1249   SmallVector<Node *, 4> PendingRefSCCStack;
1250   do {
1251     assert(DFSStack.empty() &&
1252            "Cannot begin a new root with a non-empty DFS stack!");
1253     assert(PendingRefSCCStack.empty() &&
1254            "Cannot begin a new root with pending nodes for an SCC!");
1255 
1256     Node *RootN = Worklist.pop_back_val();
1257     // Skip any nodes we've already reached in the DFS.
1258     if (RootN->DFSNumber != 0) {
1259       assert(RootN->DFSNumber == -1 &&
1260              "Shouldn't have any mid-DFS root nodes!");
1261       continue;
1262     }
1263 
1264     RootN->DFSNumber = RootN->LowLink = 1;
1265     int NextDFSNumber = 2;
1266 
1267     DFSStack.push_back({RootN, (*RootN)->begin()});
1268     do {
1269       Node *N;
1270       EdgeSequence::iterator I;
1271       std::tie(N, I) = DFSStack.pop_back_val();
1272       auto E = (*N)->end();
1273 
1274       assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1275                                   "before processing a node.");
1276 
1277       while (I != E) {
1278         Node &ChildN = I->getNode();
1279         if (ChildN.DFSNumber == 0) {
1280           // Mark that we should start at this child when next this node is the
1281           // top of the stack. We don't start at the next child to ensure this
1282           // child's lowlink is reflected.
1283           DFSStack.push_back({N, I});
1284 
1285           // Continue, resetting to the child node.
1286           ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1287           N = &ChildN;
1288           I = ChildN->begin();
1289           E = ChildN->end();
1290           continue;
1291         }
1292         if (ChildN.DFSNumber == -1) {
1293           // If this child isn't currently in this RefSCC, no need to process
1294           // it.
1295           ++I;
1296           continue;
1297         }
1298 
1299         // Track the lowest link of the children, if any are still in the stack.
1300         // Any child not on the stack will have a LowLink of -1.
1301         assert(ChildN.LowLink != 0 &&
1302                "Low-link must not be zero with a non-zero DFS number.");
1303         if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1304           N->LowLink = ChildN.LowLink;
1305         ++I;
1306       }
1307 
1308       // We've finished processing N and its descendants, put it on our pending
1309       // stack to eventually get merged into a RefSCC.
1310       PendingRefSCCStack.push_back(N);
1311 
1312       // If this node is linked to some lower entry, continue walking up the
1313       // stack.
1314       if (N->LowLink != N->DFSNumber) {
1315         assert(!DFSStack.empty() &&
1316                "We never found a viable root for a RefSCC to pop off!");
1317         continue;
1318       }
1319 
1320       // Otherwise, form a new RefSCC from the top of the pending node stack.
1321       int RefSCCNumber = PostOrderNumber++;
1322       int RootDFSNumber = N->DFSNumber;
1323 
1324       // Find the range of the node stack by walking down until we pass the
1325       // root DFS number. Update the DFS numbers and low link numbers in the
1326       // process to avoid re-walking this list where possible.
1327       auto StackRI = find_if(reverse(PendingRefSCCStack), [&](Node *N) {
1328         if (N->DFSNumber < RootDFSNumber)
1329           // We've found the bottom.
1330           return true;
1331 
1332         // Update this node and keep scanning.
1333         N->DFSNumber = -1;
1334         // Save the post-order number in the lowlink field so that we can use
1335         // it to map SCCs into new RefSCCs after we finish the DFS.
1336         N->LowLink = RefSCCNumber;
1337         return false;
1338       });
1339       auto RefSCCNodes = make_range(StackRI.base(), PendingRefSCCStack.end());
1340 
1341       // If we find a cycle containing all nodes originally in this RefSCC then
1342       // the removal hasn't changed the structure at all. This is an important
1343       // special case and we can directly exit the entire routine more
1344       // efficiently as soon as we discover it.
1345       if (llvm::size(RefSCCNodes) == NumRefSCCNodes) {
1346         // Clear out the low link field as we won't need it.
1347         for (Node *N : RefSCCNodes)
1348           N->LowLink = -1;
1349         // Return the empty result immediately.
1350         return Result;
1351       }
1352 
1353       // We've already marked the nodes internally with the RefSCC number so
1354       // just clear them off the stack and continue.
1355       PendingRefSCCStack.erase(RefSCCNodes.begin(), PendingRefSCCStack.end());
1356     } while (!DFSStack.empty());
1357 
1358     assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
1359     assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
1360   } while (!Worklist.empty());
1361 
1362   assert(PostOrderNumber > 1 &&
1363          "Should never finish the DFS when the existing RefSCC remains valid!");
1364 
1365   // Otherwise we create a collection of new RefSCC nodes and build
1366   // a radix-sort style map from postorder number to these new RefSCCs. We then
1367   // append SCCs to each of these RefSCCs in the order they occurred in the
1368   // original SCCs container.
1369   for (int i = 0; i < PostOrderNumber; ++i)
1370     Result.push_back(G->createRefSCC(*G));
1371 
1372   // Insert the resulting postorder sequence into the global graph postorder
1373   // sequence before the current RefSCC in that sequence, and then remove the
1374   // current one.
1375   //
1376   // FIXME: It'd be nice to change the APIs so that we returned an iterator
1377   // range over the global postorder sequence and generally use that sequence
1378   // rather than building a separate result vector here.
1379   int Idx = G->getRefSCCIndex(*this);
1380   G->PostOrderRefSCCs.erase(G->PostOrderRefSCCs.begin() + Idx);
1381   G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx, Result.begin(),
1382                              Result.end());
1383   for (int i : seq<int>(Idx, G->PostOrderRefSCCs.size()))
1384     G->RefSCCIndices[G->PostOrderRefSCCs[i]] = i;
1385 
1386   for (SCC *C : SCCs) {
1387     // We store the SCC number in the node's low-link field above.
1388     int SCCNumber = C->begin()->LowLink;
1389     // Clear out all of the SCC's node's low-link fields now that we're done
1390     // using them as side-storage.
1391     for (Node &N : *C) {
1392       assert(N.LowLink == SCCNumber &&
1393              "Cannot have different numbers for nodes in the same SCC!");
1394       N.LowLink = -1;
1395     }
1396 
1397     RefSCC &RC = *Result[SCCNumber];
1398     int SCCIndex = RC.SCCs.size();
1399     RC.SCCs.push_back(C);
1400     RC.SCCIndices[C] = SCCIndex;
1401     C->OuterRefSCC = &RC;
1402   }
1403 
1404   // Now that we've moved things into the new RefSCCs, clear out our current
1405   // one.
1406   G = nullptr;
1407   SCCs.clear();
1408   SCCIndices.clear();
1409 
1410 #ifndef NDEBUG
1411   // Verify the new RefSCCs we've built.
1412   for (RefSCC *RC : Result)
1413     RC->verify();
1414 #endif
1415 
1416   // Return the new list of SCCs.
1417   return Result;
1418 }
1419 
1420 void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
1421                                                   Node &TargetN) {
1422 #ifndef NDEBUG
1423   // Check that the RefSCC is still valid when we finish.
1424   auto ExitVerifier = make_scope_exit([this] { verify(); });
1425 
1426 #ifdef EXPENSIVE_CHECKS
1427   // Check that we aren't breaking some invariants of the SCC graph. Note that
1428   // this is quadratic in the number of edges in the call graph!
1429   SCC &SourceC = *G->lookupSCC(SourceN);
1430   SCC &TargetC = *G->lookupSCC(TargetN);
1431   if (&SourceC != &TargetC)
1432     assert(SourceC.isAncestorOf(TargetC) &&
1433            "Call edge is not trivial in the SCC graph!");
1434 #endif // EXPENSIVE_CHECKS
1435 #endif // NDEBUG
1436 
1437   // First insert it into the source or find the existing edge.
1438   auto InsertResult =
1439       SourceN->EdgeIndexMap.insert({&TargetN, SourceN->Edges.size()});
1440   if (!InsertResult.second) {
1441     // Already an edge, just update it.
1442     Edge &E = SourceN->Edges[InsertResult.first->second];
1443     if (E.isCall())
1444       return; // Nothing to do!
1445     E.setKind(Edge::Call);
1446   } else {
1447     // Create the new edge.
1448     SourceN->Edges.emplace_back(TargetN, Edge::Call);
1449   }
1450 }
1451 
1452 void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
1453 #ifndef NDEBUG
1454   // Check that the RefSCC is still valid when we finish.
1455   auto ExitVerifier = make_scope_exit([this] { verify(); });
1456 
1457 #ifdef EXPENSIVE_CHECKS
1458   // Check that we aren't breaking some invariants of the RefSCC graph.
1459   RefSCC &SourceRC = *G->lookupRefSCC(SourceN);
1460   RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1461   if (&SourceRC != &TargetRC)
1462     assert(SourceRC.isAncestorOf(TargetRC) &&
1463            "Ref edge is not trivial in the RefSCC graph!");
1464 #endif // EXPENSIVE_CHECKS
1465 #endif // NDEBUG
1466 
1467   // First insert it into the source or find the existing edge.
1468   auto InsertResult =
1469       SourceN->EdgeIndexMap.insert({&TargetN, SourceN->Edges.size()});
1470   if (!InsertResult.second)
1471     // Already an edge, we're done.
1472     return;
1473 
1474   // Create the new edge.
1475   SourceN->Edges.emplace_back(TargetN, Edge::Ref);
1476 }
1477 
1478 void LazyCallGraph::RefSCC::replaceNodeFunction(Node &N, Function &NewF) {
1479   Function &OldF = N.getFunction();
1480 
1481 #ifndef NDEBUG
1482   // Check that the RefSCC is still valid when we finish.
1483   auto ExitVerifier = make_scope_exit([this] { verify(); });
1484 
1485   assert(G->lookupRefSCC(N) == this &&
1486          "Cannot replace the function of a node outside this RefSCC.");
1487 
1488   assert(G->NodeMap.find(&NewF) == G->NodeMap.end() &&
1489          "Must not have already walked the new function!'");
1490 
1491   // It is important that this replacement not introduce graph changes so we
1492   // insist that the caller has already removed every use of the original
1493   // function and that all uses of the new function correspond to existing
1494   // edges in the graph. The common and expected way to use this is when
1495   // replacing the function itself in the IR without changing the call graph
1496   // shape and just updating the analysis based on that.
1497   assert(&OldF != &NewF && "Cannot replace a function with itself!");
1498   assert(OldF.use_empty() &&
1499          "Must have moved all uses from the old function to the new!");
1500 #endif
1501 
1502   N.replaceFunction(NewF);
1503 
1504   // Update various call graph maps.
1505   G->NodeMap.erase(&OldF);
1506   G->NodeMap[&NewF] = &N;
1507 }
1508 
1509 void LazyCallGraph::insertEdge(Node &SourceN, Node &TargetN, Edge::Kind EK) {
1510   assert(SCCMap.empty() &&
1511          "This method cannot be called after SCCs have been formed!");
1512 
1513   return SourceN->insertEdgeInternal(TargetN, EK);
1514 }
1515 
1516 void LazyCallGraph::removeEdge(Node &SourceN, Node &TargetN) {
1517   assert(SCCMap.empty() &&
1518          "This method cannot be called after SCCs have been formed!");
1519 
1520   bool Removed = SourceN->removeEdgeInternal(TargetN);
1521   (void)Removed;
1522   assert(Removed && "Target not in the edge set for this caller?");
1523 }
1524 
1525 void LazyCallGraph::removeDeadFunction(Function &F) {
1526   // FIXME: This is unnecessarily restrictive. We should be able to remove
1527   // functions which recursively call themselves.
1528   assert(F.use_empty() &&
1529          "This routine should only be called on trivially dead functions!");
1530 
1531   // We shouldn't remove library functions as they are never really dead while
1532   // the call graph is in use -- every function definition refers to them.
1533   assert(!isLibFunction(F) &&
1534          "Must not remove lib functions from the call graph!");
1535 
1536   auto NI = NodeMap.find(&F);
1537   if (NI == NodeMap.end())
1538     // Not in the graph at all!
1539     return;
1540 
1541   Node &N = *NI->second;
1542   NodeMap.erase(NI);
1543 
1544   // Remove this from the entry edges if present.
1545   EntryEdges.removeEdgeInternal(N);
1546 
1547   if (SCCMap.empty()) {
1548     // No SCCs have been formed, so removing this is fine and there is nothing
1549     // else necessary at this point but clearing out the node.
1550     N.clear();
1551     return;
1552   }
1553 
1554   // Cannot remove a function which has yet to be visited in the DFS walk, so
1555   // if we have a node at all then we must have an SCC and RefSCC.
1556   auto CI = SCCMap.find(&N);
1557   assert(CI != SCCMap.end() &&
1558          "Tried to remove a node without an SCC after DFS walk started!");
1559   SCC &C = *CI->second;
1560   SCCMap.erase(CI);
1561   RefSCC &RC = C.getOuterRefSCC();
1562 
1563   // This node must be the only member of its SCC as it has no callers, and
1564   // that SCC must be the only member of a RefSCC as it has no references.
1565   // Validate these properties first.
1566   assert(C.size() == 1 && "Dead functions must be in a singular SCC");
1567   assert(RC.size() == 1 && "Dead functions must be in a singular RefSCC");
1568 
1569   auto RCIndexI = RefSCCIndices.find(&RC);
1570   int RCIndex = RCIndexI->second;
1571   PostOrderRefSCCs.erase(PostOrderRefSCCs.begin() + RCIndex);
1572   RefSCCIndices.erase(RCIndexI);
1573   for (int i = RCIndex, Size = PostOrderRefSCCs.size(); i < Size; ++i)
1574     RefSCCIndices[PostOrderRefSCCs[i]] = i;
1575 
1576   // Finally clear out all the data structures from the node down through the
1577   // components.
1578   N.clear();
1579   N.G = nullptr;
1580   N.F = nullptr;
1581   C.clear();
1582   RC.clear();
1583   RC.G = nullptr;
1584 
1585   // Nothing to delete as all the objects are allocated in stable bump pointer
1586   // allocators.
1587 }
1588 
1589 // Gets the Edge::Kind from one function to another by looking at the function's
1590 // instructions. Asserts if there is no edge.
1591 // Useful for determining what type of edge should exist between functions when
1592 // the edge hasn't been created yet.
1593 static LazyCallGraph::Edge::Kind getEdgeKind(Function &OriginalFunction,
1594                                              Function &NewFunction) {
1595   // In release builds, assume that if there are no direct calls to the new
1596   // function, then there is a ref edge. In debug builds, keep track of
1597   // references to assert that there is actually a ref edge if there is no call
1598   // edge.
1599 #ifndef NDEBUG
1600   SmallVector<Constant *, 16> Worklist;
1601   SmallPtrSet<Constant *, 16> Visited;
1602 #endif
1603 
1604   for (Instruction &I : instructions(OriginalFunction)) {
1605     if (auto *CB = dyn_cast<CallBase>(&I)) {
1606       if (Function *Callee = CB->getCalledFunction()) {
1607         if (Callee == &NewFunction)
1608           return LazyCallGraph::Edge::Kind::Call;
1609       }
1610     }
1611 #ifndef NDEBUG
1612     for (Value *Op : I.operand_values()) {
1613       if (Constant *C = dyn_cast<Constant>(Op)) {
1614         if (Visited.insert(C).second)
1615           Worklist.push_back(C);
1616       }
1617     }
1618 #endif
1619   }
1620 
1621 #ifndef NDEBUG
1622   bool FoundNewFunction = false;
1623   LazyCallGraph::visitReferences(Worklist, Visited, [&](Function &F) {
1624     if (&F == &NewFunction)
1625       FoundNewFunction = true;
1626   });
1627   assert(FoundNewFunction && "No edge from original function to new function");
1628 #endif
1629 
1630   return LazyCallGraph::Edge::Kind::Ref;
1631 }
1632 
1633 void LazyCallGraph::addSplitFunction(Function &OriginalFunction,
1634                                      Function &NewFunction) {
1635   assert(lookup(OriginalFunction) &&
1636          "Original function's node should already exist");
1637   Node &OriginalN = get(OriginalFunction);
1638   SCC *OriginalC = lookupSCC(OriginalN);
1639   RefSCC *OriginalRC = lookupRefSCC(OriginalN);
1640 
1641 #ifndef NDEBUG
1642   OriginalRC->verify();
1643   auto VerifyOnExit = make_scope_exit([&]() { OriginalRC->verify(); });
1644 #endif
1645 
1646   assert(!lookup(NewFunction) &&
1647          "New function's node should not already exist");
1648   Node &NewN = initNode(NewFunction);
1649 
1650   Edge::Kind EK = getEdgeKind(OriginalFunction, NewFunction);
1651 
1652   SCC *NewC = nullptr;
1653   for (Edge &E : *NewN) {
1654     Node &EN = E.getNode();
1655     if (EK == Edge::Kind::Call && E.isCall() && lookupSCC(EN) == OriginalC) {
1656       // If the edge to the new function is a call edge and there is a call edge
1657       // from the new function to any function in the original function's SCC,
1658       // it is in the same SCC (and RefSCC) as the original function.
1659       NewC = OriginalC;
1660       NewC->Nodes.push_back(&NewN);
1661       break;
1662     }
1663   }
1664 
1665   if (!NewC) {
1666     for (Edge &E : *NewN) {
1667       Node &EN = E.getNode();
1668       if (lookupRefSCC(EN) == OriginalRC) {
1669         // If there is any edge from the new function to any function in the
1670         // original function's RefSCC, it is in the same RefSCC as the original
1671         // function but a new SCC.
1672         RefSCC *NewRC = OriginalRC;
1673         NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
1674 
1675         // The new function's SCC is not the same as the original function's
1676         // SCC, since that case was handled earlier. If the edge from the
1677         // original function to the new function was a call edge, then we need
1678         // to insert the newly created function's SCC before the original
1679         // function's SCC. Otherwise either the new SCC comes after the original
1680         // function's SCC, or it doesn't matter, and in both cases we can add it
1681         // to the very end.
1682         int InsertIndex = EK == Edge::Kind::Call ? NewRC->SCCIndices[OriginalC]
1683                                                  : NewRC->SCCIndices.size();
1684         NewRC->SCCs.insert(NewRC->SCCs.begin() + InsertIndex, NewC);
1685         for (int I = InsertIndex, Size = NewRC->SCCs.size(); I < Size; ++I)
1686           NewRC->SCCIndices[NewRC->SCCs[I]] = I;
1687 
1688         break;
1689       }
1690     }
1691   }
1692 
1693   if (!NewC) {
1694     // We didn't find any edges back to the original function's RefSCC, so the
1695     // new function belongs in a new RefSCC. The new RefSCC goes before the
1696     // original function's RefSCC.
1697     RefSCC *NewRC = createRefSCC(*this);
1698     NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
1699     NewRC->SCCIndices[NewC] = 0;
1700     NewRC->SCCs.push_back(NewC);
1701     auto OriginalRCIndex = RefSCCIndices.find(OriginalRC)->second;
1702     PostOrderRefSCCs.insert(PostOrderRefSCCs.begin() + OriginalRCIndex, NewRC);
1703     for (int I = OriginalRCIndex, Size = PostOrderRefSCCs.size(); I < Size; ++I)
1704       RefSCCIndices[PostOrderRefSCCs[I]] = I;
1705   }
1706 
1707   SCCMap[&NewN] = NewC;
1708 
1709   OriginalN->insertEdgeInternal(NewN, EK);
1710 }
1711 
1712 void LazyCallGraph::addSplitRefRecursiveFunctions(
1713     Function &OriginalFunction, ArrayRef<Function *> NewFunctions) {
1714   assert(!NewFunctions.empty() && "Can't add zero functions");
1715   assert(lookup(OriginalFunction) &&
1716          "Original function's node should already exist");
1717   Node &OriginalN = get(OriginalFunction);
1718   RefSCC *OriginalRC = lookupRefSCC(OriginalN);
1719 
1720 #ifndef NDEBUG
1721   OriginalRC->verify();
1722   auto VerifyOnExit = make_scope_exit([&]() {
1723     OriginalRC->verify();
1724 #ifdef EXPENSIVE_CHECKS
1725     for (Function *NewFunction : NewFunctions)
1726       lookupRefSCC(get(*NewFunction))->verify();
1727 #endif
1728   });
1729 #endif
1730 
1731   bool ExistsRefToOriginalRefSCC = false;
1732 
1733   for (Function *NewFunction : NewFunctions) {
1734     Node &NewN = initNode(*NewFunction);
1735 
1736     OriginalN->insertEdgeInternal(NewN, Edge::Kind::Ref);
1737 
1738     // Check if there is any edge from any new function back to any function in
1739     // the original function's RefSCC.
1740     for (Edge &E : *NewN) {
1741       if (lookupRefSCC(E.getNode()) == OriginalRC) {
1742         ExistsRefToOriginalRefSCC = true;
1743         break;
1744       }
1745     }
1746   }
1747 
1748   RefSCC *NewRC;
1749   if (ExistsRefToOriginalRefSCC) {
1750     // If there is any edge from any new function to any function in the
1751     // original function's RefSCC, all new functions will be in the same RefSCC
1752     // as the original function.
1753     NewRC = OriginalRC;
1754   } else {
1755     // Otherwise the new functions are in their own RefSCC.
1756     NewRC = createRefSCC(*this);
1757     // The new RefSCC goes before the original function's RefSCC in postorder
1758     // since there are only edges from the original function's RefSCC to the new
1759     // RefSCC.
1760     auto OriginalRCIndex = RefSCCIndices.find(OriginalRC)->second;
1761     PostOrderRefSCCs.insert(PostOrderRefSCCs.begin() + OriginalRCIndex, NewRC);
1762     for (int I = OriginalRCIndex, Size = PostOrderRefSCCs.size(); I < Size; ++I)
1763       RefSCCIndices[PostOrderRefSCCs[I]] = I;
1764   }
1765 
1766   for (Function *NewFunction : NewFunctions) {
1767     Node &NewN = get(*NewFunction);
1768     // Each new function is in its own new SCC. The original function can only
1769     // have a ref edge to new functions, and no other existing functions can
1770     // have references to new functions. Each new function only has a ref edge
1771     // to the other new functions.
1772     SCC *NewC = createSCC(*NewRC, SmallVector<Node *, 1>({&NewN}));
1773     // The new SCCs are either sibling SCCs or parent SCCs to all other existing
1774     // SCCs in the RefSCC. Either way, they can go at the back of the postorder
1775     // SCC list.
1776     auto Index = NewRC->SCCIndices.size();
1777     NewRC->SCCIndices[NewC] = Index;
1778     NewRC->SCCs.push_back(NewC);
1779     SCCMap[&NewN] = NewC;
1780   }
1781 
1782 #ifndef NDEBUG
1783   for (Function *F1 : NewFunctions) {
1784     assert(getEdgeKind(OriginalFunction, *F1) == Edge::Kind::Ref &&
1785            "Expected ref edges from original function to every new function");
1786     Node &N1 = get(*F1);
1787     for (Function *F2 : NewFunctions) {
1788       if (F1 == F2)
1789         continue;
1790       Node &N2 = get(*F2);
1791       assert(!N1->lookup(N2)->isCall() &&
1792              "Edges between new functions must be ref edges");
1793     }
1794   }
1795 #endif
1796 }
1797 
1798 LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1799   return *new (MappedN = BPA.Allocate()) Node(*this, F);
1800 }
1801 
1802 void LazyCallGraph::updateGraphPtrs() {
1803   // Walk the node map to update their graph pointers. While this iterates in
1804   // an unstable order, the order has no effect so it remains correct.
1805   for (auto &FunctionNodePair : NodeMap)
1806     FunctionNodePair.second->G = this;
1807 
1808   for (auto *RC : PostOrderRefSCCs)
1809     RC->G = this;
1810 }
1811 
1812 LazyCallGraph::Node &LazyCallGraph::initNode(Function &F) {
1813   Node &N = get(F);
1814   N.DFSNumber = N.LowLink = -1;
1815   N.populate();
1816   NodeMap[&F] = &N;
1817   return N;
1818 }
1819 
1820 template <typename RootsT, typename GetBeginT, typename GetEndT,
1821           typename GetNodeT, typename FormSCCCallbackT>
1822 void LazyCallGraph::buildGenericSCCs(RootsT &&Roots, GetBeginT &&GetBegin,
1823                                      GetEndT &&GetEnd, GetNodeT &&GetNode,
1824                                      FormSCCCallbackT &&FormSCC) {
1825   using EdgeItT = decltype(GetBegin(std::declval<Node &>()));
1826 
1827   SmallVector<std::pair<Node *, EdgeItT>, 16> DFSStack;
1828   SmallVector<Node *, 16> PendingSCCStack;
1829 
1830   // Scan down the stack and DFS across the call edges.
1831   for (Node *RootN : Roots) {
1832     assert(DFSStack.empty() &&
1833            "Cannot begin a new root with a non-empty DFS stack!");
1834     assert(PendingSCCStack.empty() &&
1835            "Cannot begin a new root with pending nodes for an SCC!");
1836 
1837     // Skip any nodes we've already reached in the DFS.
1838     if (RootN->DFSNumber != 0) {
1839       assert(RootN->DFSNumber == -1 &&
1840              "Shouldn't have any mid-DFS root nodes!");
1841       continue;
1842     }
1843 
1844     RootN->DFSNumber = RootN->LowLink = 1;
1845     int NextDFSNumber = 2;
1846 
1847     DFSStack.push_back({RootN, GetBegin(*RootN)});
1848     do {
1849       Node *N;
1850       EdgeItT I;
1851       std::tie(N, I) = DFSStack.pop_back_val();
1852       auto E = GetEnd(*N);
1853       while (I != E) {
1854         Node &ChildN = GetNode(I);
1855         if (ChildN.DFSNumber == 0) {
1856           // We haven't yet visited this child, so descend, pushing the current
1857           // node onto the stack.
1858           DFSStack.push_back({N, I});
1859 
1860           ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1861           N = &ChildN;
1862           I = GetBegin(*N);
1863           E = GetEnd(*N);
1864           continue;
1865         }
1866 
1867         // If the child has already been added to some child component, it
1868         // couldn't impact the low-link of this parent because it isn't
1869         // connected, and thus its low-link isn't relevant so skip it.
1870         if (ChildN.DFSNumber == -1) {
1871           ++I;
1872           continue;
1873         }
1874 
1875         // Track the lowest linked child as the lowest link for this node.
1876         assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1877         if (ChildN.LowLink < N->LowLink)
1878           N->LowLink = ChildN.LowLink;
1879 
1880         // Move to the next edge.
1881         ++I;
1882       }
1883 
1884       // We've finished processing N and its descendants, put it on our pending
1885       // SCC stack to eventually get merged into an SCC of nodes.
1886       PendingSCCStack.push_back(N);
1887 
1888       // If this node is linked to some lower entry, continue walking up the
1889       // stack.
1890       if (N->LowLink != N->DFSNumber)
1891         continue;
1892 
1893       // Otherwise, we've completed an SCC. Append it to our post order list of
1894       // SCCs.
1895       int RootDFSNumber = N->DFSNumber;
1896       // Find the range of the node stack by walking down until we pass the
1897       // root DFS number.
1898       auto SCCNodes = make_range(
1899           PendingSCCStack.rbegin(),
1900           find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1901             return N->DFSNumber < RootDFSNumber;
1902           }));
1903       // Form a new SCC out of these nodes and then clear them off our pending
1904       // stack.
1905       FormSCC(SCCNodes);
1906       PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1907     } while (!DFSStack.empty());
1908   }
1909 }
1910 
1911 /// Build the internal SCCs for a RefSCC from a sequence of nodes.
1912 ///
1913 /// Appends the SCCs to the provided vector and updates the map with their
1914 /// indices. Both the vector and map must be empty when passed into this
1915 /// routine.
1916 void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1917   assert(RC.SCCs.empty() && "Already built SCCs!");
1918   assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
1919 
1920   for (Node *N : Nodes) {
1921     assert(N->LowLink >= (*Nodes.begin())->LowLink &&
1922            "We cannot have a low link in an SCC lower than its root on the "
1923            "stack!");
1924 
1925     // This node will go into the next RefSCC, clear out its DFS and low link
1926     // as we scan.
1927     N->DFSNumber = N->LowLink = 0;
1928   }
1929 
1930   // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1931   // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1932   // internal storage as we won't need it for the outer graph's DFS any longer.
1933   buildGenericSCCs(
1934       Nodes, [](Node &N) { return N->call_begin(); },
1935       [](Node &N) { return N->call_end(); },
1936       [](EdgeSequence::call_iterator I) -> Node & { return I->getNode(); },
1937       [this, &RC](node_stack_range Nodes) {
1938         RC.SCCs.push_back(createSCC(RC, Nodes));
1939         for (Node &N : *RC.SCCs.back()) {
1940           N.DFSNumber = N.LowLink = -1;
1941           SCCMap[&N] = RC.SCCs.back();
1942         }
1943       });
1944 
1945   // Wire up the SCC indices.
1946   for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1947     RC.SCCIndices[RC.SCCs[i]] = i;
1948 }
1949 
1950 void LazyCallGraph::buildRefSCCs() {
1951   if (EntryEdges.empty() || !PostOrderRefSCCs.empty())
1952     // RefSCCs are either non-existent or already built!
1953     return;
1954 
1955   assert(RefSCCIndices.empty() && "Already mapped RefSCC indices!");
1956 
1957   SmallVector<Node *, 16> Roots;
1958   for (Edge &E : *this)
1959     Roots.push_back(&E.getNode());
1960 
1961   // The roots will be iterated in order.
1962   buildGenericSCCs(
1963       Roots,
1964       [](Node &N) {
1965         // We need to populate each node as we begin to walk its edges.
1966         N.populate();
1967         return N->begin();
1968       },
1969       [](Node &N) { return N->end(); },
1970       [](EdgeSequence::iterator I) -> Node & { return I->getNode(); },
1971       [this](node_stack_range Nodes) {
1972         RefSCC *NewRC = createRefSCC(*this);
1973         buildSCCs(*NewRC, Nodes);
1974 
1975         // Push the new node into the postorder list and remember its position
1976         // in the index map.
1977         bool Inserted =
1978             RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second;
1979         (void)Inserted;
1980         assert(Inserted && "Cannot already have this RefSCC in the index map!");
1981         PostOrderRefSCCs.push_back(NewRC);
1982 #ifndef NDEBUG
1983         NewRC->verify();
1984 #endif
1985       });
1986 }
1987 
1988 AnalysisKey LazyCallGraphAnalysis::Key;
1989 
1990 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1991 
1992 static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
1993   OS << "  Edges in function: " << N.getFunction().getName() << "\n";
1994   for (LazyCallGraph::Edge &E : N.populate())
1995     OS << "    " << (E.isCall() ? "call" : "ref ") << " -> "
1996        << E.getFunction().getName() << "\n";
1997 
1998   OS << "\n";
1999 }
2000 
2001 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
2002   OS << "    SCC with " << C.size() << " functions:\n";
2003 
2004   for (LazyCallGraph::Node &N : C)
2005     OS << "      " << N.getFunction().getName() << "\n";
2006 }
2007 
2008 static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
2009   OS << "  RefSCC with " << C.size() << " call SCCs:\n";
2010 
2011   for (LazyCallGraph::SCC &InnerC : C)
2012     printSCC(OS, InnerC);
2013 
2014   OS << "\n";
2015 }
2016 
2017 PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
2018                                                 ModuleAnalysisManager &AM) {
2019   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
2020 
2021   OS << "Printing the call graph for module: " << M.getModuleIdentifier()
2022      << "\n\n";
2023 
2024   for (Function &F : M)
2025     printNode(OS, G.get(F));
2026 
2027   G.buildRefSCCs();
2028   for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
2029     printRefSCC(OS, C);
2030 
2031   return PreservedAnalyses::all();
2032 }
2033 
2034 LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
2035     : OS(OS) {}
2036 
2037 static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
2038   std::string Name =
2039       "\"" + DOT::EscapeString(std::string(N.getFunction().getName())) + "\"";
2040 
2041   for (LazyCallGraph::Edge &E : N.populate()) {
2042     OS << "  " << Name << " -> \""
2043        << DOT::EscapeString(std::string(E.getFunction().getName())) << "\"";
2044     if (!E.isCall()) // It is a ref edge.
2045       OS << " [style=dashed,label=\"ref\"]";
2046     OS << ";\n";
2047   }
2048 
2049   OS << "\n";
2050 }
2051 
2052 PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
2053                                                    ModuleAnalysisManager &AM) {
2054   LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
2055 
2056   OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
2057 
2058   for (Function &F : M)
2059     printNodeDOT(OS, G.get(F));
2060 
2061   OS << "}\n";
2062 
2063   return PreservedAnalyses::all();
2064 }
2065