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