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