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