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