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