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