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