1 //===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 #define DEBUG_TYPE "lcg"
26 void LazyCallGraph::EdgeSequence::insertEdgeInternal(Node &TargetN,
28 EdgeIndexMap.insert({&TargetN, Edges.size()});
29 Edges.emplace_back(TargetN, EK);
32 void LazyCallGraph::EdgeSequence::setEdgeKind(Node &TargetN, Edge::Kind EK) {
33 Edges[EdgeIndexMap.find(&TargetN)->second].setKind(EK);
36 bool LazyCallGraph::EdgeSequence::removeEdgeInternal(Node &TargetN) {
37 auto IndexMapI = EdgeIndexMap.find(&TargetN);
38 if (IndexMapI == EdgeIndexMap.end())
41 Edges[IndexMapI->second] = Edge();
42 EdgeIndexMap.erase(IndexMapI);
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)
52 DEBUG(dbgs() << " Added callable function: " << N.getName() << "\n");
53 Edges.emplace_back(LazyCallGraph::Edge(N, EK));
56 LazyCallGraph::EdgeSequence &LazyCallGraph::Node::populateSlow() {
57 assert(!Edges && "Must not have already populated the edges for this node!");
59 DEBUG(dbgs() << " Adding functions called by '" << getName()
60 << "' to the graph.\n");
62 Edges = EdgeSequence();
64 SmallVector<Constant *, 16> Worklist;
65 SmallPtrSet<Function *, 4> Callees;
66 SmallPtrSet<Constant *, 16> Visited;
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
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);
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);
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);
112 void LazyCallGraph::Node::replaceFunction(Function &NewF) {
113 assert(F != &NewF && "Must not replace a function with itself!");
117 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
118 LLVM_DUMP_METHOD void LazyCallGraph::Node::dump() const {
119 dbgs() << *this << '\n';
123 LazyCallGraph::LazyCallGraph(Module &M) {
124 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
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);
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());
141 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
143 visitReferences(Worklist, Visited, [&](Function &F) {
144 addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(F),
145 LazyCallGraph::Edge::Ref);
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)) {
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);
167 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
168 LLVM_DUMP_METHOD void LazyCallGraph::SCC::dump() const {
169 dbgs() << *this << '\n';
174 void LazyCallGraph::SCC::verify() {
175 assert(OuterRefSCC && "Can't have a null RefSCC!");
176 assert(!Nodes.empty() && "Can't have an empty SCC!");
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!");
187 assert(E.getNode() && "Can't have an unpopulated node!");
192 bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
196 for (Node &N : *this)
197 for (Edge &E : N->calls())
198 if (OuterRefSCC->G->lookupSCC(E.getNode()) == &C)
205 bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
206 if (this == &TargetC)
209 LazyCallGraph &G = *OuterRefSCC->G;
211 // Start with this SCC.
212 SmallPtrSet<const SCC *, 16> Visited = {this};
213 SmallVector<const SCC *, 16> Worklist = {this};
215 // Walk down the graph until we run out of edges or find a path to TargetC.
217 const SCC &C = *Worklist.pop_back_val();
219 for (Edge &E : N->calls()) {
220 SCC *CalleeC = G.lookupSCC(E.getNode());
224 // If the callee's SCC is the TargetC, we're done.
225 if (CalleeC == &TargetC)
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);
233 } while (!Worklist.empty());
239 LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
241 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
242 LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
243 dbgs() << *this << '\n';
248 void LazyCallGraph::RefSCC::verify() {
249 assert(G && "Can't have a null graph!");
250 assert(!SCCs.empty() && "Can't have an empty SCC!");
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!");
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!");
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!");
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)
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.");
288 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
289 "Edge to a RefSCC missing us in its parent set.");
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)
300 if (G->lookupRefSCC(E.getNode()) == this)
304 assert(HasConnectingEdge() && "No edge connects the parent to us!");
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);
314 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
315 if (AncestorC->isChildOf(C))
317 for (const RefSCC *ParentC : AncestorC->Parents)
318 AncestorWorklist.push_back(ParentC);
319 } while (!AncestorWorklist.empty());
324 /// Generic helper that updates a postorder sequence of SCCs for a potentially
325 /// cycle-introducing edge insertion.
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.
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.
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:
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
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
364 /// This process has various important properties:
365 /// - Only mutates the SCCs when adding the edge actually changes the SCC
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.
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.
378 /// The sequence and the map must operate on pointers to the SCC type.
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
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!");
399 SmallPtrSet<SCCT *, 4> ConnectedSet;
401 // Compute the SCCs which (transitively) reach the source.
402 ComputeSourceConnectedSet(ConnectedSet);
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;
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.");
421 // Return an empty range at the target SCC indicating there is nothing to
423 return make_range(std::prev(SourceI), std::prev(SourceI));
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!");
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
436 if (SourceIdx + 1 < TargetIdx) {
437 ConnectedSet.clear();
438 ComputeTargetConnectedSet(ConnectedSet);
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!");
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
456 return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
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;
465 // In a debug build, verify the RefSCC is valid to start with and when this
468 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
471 SCC &SourceSCC = *G->lookupSCC(SourceN);
472 SCC &TargetSCC = *G->lookupSCC(TargetN);
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);
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.
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);
494 // Compute the SCCs which (transitively) reach the source.
495 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
497 // Check that the RefSCC is still valid before computing this as the
498 // results will be nonsensical of we've broken its invariants.
501 ConnectedSet.insert(&SourceSCC);
502 auto IsConnected = [&](SCC &C) {
504 for (Edge &E : N->calls())
505 if (ConnectedSet.count(G->lookupSCC(E.getNode())))
512 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
514 ConnectedSet.insert(C);
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
521 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
523 // Check that the RefSCC is still valid before computing this as the
524 // results will be nonsensical of we've broken its invariants.
527 ConnectedSet.insert(&TargetSCC);
528 SmallVector<SCC *, 4> Worklist;
529 Worklist.push_back(&TargetSCC);
531 SCC &C = *Worklist.pop_back_val();
536 SCC &EdgeC = *G->lookupSCC(E.getNode());
537 if (&EdgeC.getOuterRefSCC() != this)
538 // Not in this RefSCC...
540 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
541 // Not in the postorder sequence between source and target.
544 if (ConnectedSet.insert(&EdgeC).second)
545 Worklist.push_back(&EdgeC);
547 } while (!Worklist.empty());
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
554 auto MergeRange = updatePostorderSequenceForEdgeInsertion(
555 SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
556 ComputeTargetConnectedSet);
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);
567 // Before merging, check that the RefSCC remains valid after all the
568 // postorder updates.
572 // Otherwise we need to merge all of the SCCs in the cycle into a single
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!");
582 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
583 for (Node *N : C->Nodes)
584 G->SCCMap[N] = &TargetSCC;
586 DeletedSCCs.push_back(C);
589 // Erase the merged SCCs from the list and update the indices of the
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;
596 // Now that the SCC structure is finalized, flip the kind to call.
597 SourceN->setEdgeKind(TargetN, Edge::Call);
603 void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
605 assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
608 // In a debug build, verify the RefSCC is valid to start with and when this
611 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
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!");
621 // Set the edge kind.
622 SourceN->setEdgeKind(TargetN, Edge::Ref);
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!");
630 // In a debug build, verify the RefSCC is valid to start with and when this
633 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
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.");
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 "
646 // Set the edge kind.
647 SourceN->setEdgeKind(TargetN, Edge::Ref);
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.
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,
661 SCC &OldSCC = TargetSCC;
662 SmallVector<std::pair<Node *, EdgeSequence::call_iterator>, 16> DFSStack;
663 SmallVector<Node *, 16> PendingSCCStack;
664 SmallVector<SCC *, 4> NewSCCs;
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;
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;
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!");
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!");
700 RootN->DFSNumber = RootN->LowLink = 1;
701 int NextDFSNumber = 2;
703 DFSStack.push_back({RootN, (*RootN)->call_begin()});
706 EdgeSequence::call_iterator I;
707 std::tie(N, I) = DFSStack.pop_back_val();
708 auto E = (*N)->call_end();
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});
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++;
720 I = (*N)->call_begin();
721 E = (*N)->call_end();
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;
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.
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;
758 // Move to the next edge.
762 // Cleared the DFS early, start another round.
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);
769 // If this node is linked to some lower entry, continue walking up the
771 if (N->LowLink != N->DFSNumber)
774 // Otherwise, we've completed an SCC. Append it to our post order list of
776 int RootDFSNumber = N->DFSNumber;
777 // Find the range of the node stack by walking down until we pass the
779 auto SCCNodes = make_range(
780 PendingSCCStack.rbegin(),
781 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
782 return N->DFSNumber < RootDFSNumber;
785 // Form a new SCC out of these nodes and then clear them off our pending
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();
792 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
793 } while (!DFSStack.empty());
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());
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;
808 return make_range(SCCs.begin() + OldIdx,
809 SCCs.begin() + OldIdx + NewSCCs.size());
812 void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
814 assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
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.");
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);
829 // Check that the RefSCC is still valid.
834 void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
836 assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
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.");
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);
851 // Check that the RefSCC is still valid.
856 void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
858 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
859 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
861 SourceN->insertEdgeInternal(TargetN, Edge::Ref);
864 // Check that the RefSCC is still valid.
869 void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
871 // First insert it into the caller.
872 SourceN->insertEdgeInternal(TargetN, EK);
874 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
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.");
883 // The only change required is to add this SCC to the parent set of the
885 TargetC.Parents.insert(this);
888 // Check that the RefSCC is still valid.
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.");
903 SmallVector<RefSCC *, 1> DeletedRefSCCs;
906 // In a debug build, verify the RefSCC is valid to start with and when this
909 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
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!");
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);
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)
937 if (Set.insert(&ParentRC).second)
938 // First edge connecting to this parent, add it to our worklist.
939 Worklist.push_back(&ParentRC);
941 } while (!Worklist.empty());
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
948 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
950 SmallVector<RefSCC *, 4> Worklist;
951 Worklist.push_back(this);
953 RefSCC &RC = *Worklist.pop_back_val();
957 RefSCC &EdgeRC = *G->lookupRefSCC(E.getNode());
958 if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
959 // Not in the postorder sequence between source and target.
962 if (Set.insert(&EdgeRC).second)
963 Worklist.push_back(&EdgeRC);
965 } while (!Worklist.empty());
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
972 iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
973 updatePostorderSequenceForEdgeInsertion(
974 SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
975 ComputeSourceConnectedSet, ComputeTargetConnectedSet);
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());
981 // This RefSCC will always be part of that set, so just insert it here.
982 MergeSet.insert(this);
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;
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.");
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);
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))
1011 ChildRC.Parents.erase(RC);
1012 ChildRC.Parents.insert(this);
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);
1022 MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
1024 DeletedRefSCCs.push_back(RC);
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);
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();
1038 G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
1039 for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
1040 G->RefSCCIndices[RC] -= IndexOffset;
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);
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;
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.");
1057 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1058 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
1060 assert(!is_contained(G->LeafRefSCCs, this) &&
1061 "Cannot have a leaf RefSCC source.");
1064 // In a debug build, verify the RefSCC is valid to start with and when this
1065 // routine finishes.
1067 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1070 // First remove it from the node.
1071 bool Removed = SourceN->removeEdgeInternal(TargetN);
1073 assert(Removed && "Target not in the edge set for this caller?");
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;
1085 if (&OtherChildRC != this)
1086 HasOtherChildRC = true;
1088 if (HasOtherEdgeToChildRC)
1091 if (HasOtherEdgeToChildRC)
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);
1102 "Did not find the source SCC in the target SCC's parent list!");
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");
1111 // It may make the Source SCC a leaf SCC.
1112 if (!HasOtherChildRC)
1113 G->LeafRefSCCs.push_back(this);
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");
1123 // In a debug build, verify the RefSCC is valid to start with and when this
1124 // routine finishes.
1126 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1129 // First remove the actual edge.
1130 bool Removed = SourceN->removeEdgeInternal(TargetN);
1132 assert(Removed && "Target not in the edge set for this caller?");
1134 // We return a list of the resulting *new* RefSCCs in post-order.
1135 SmallVector<RefSCC *, 1> Result;
1137 // Direct recursion doesn't impact the SCC graph at all.
1138 if (&SourceN == &TargetN)
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)
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;
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.
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;
1173 // Reset all the other nodes to prepare for a DFS over them, and add them to
1175 SmallVector<Node *, 8> Worklist;
1176 for (SCC *C : SCCs) {
1181 N.DFSNumber = N.LowLink = 0;
1183 Worklist.append(C->Nodes.begin(), C->Nodes.end());
1186 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1187 N.DFSNumber = N.LowLink = -1;
1188 PostOrderMapping[&N] = Number;
1191 SmallVector<std::pair<Node *, EdgeSequence::iterator>, 4> DFSStack;
1192 SmallVector<Node *, 4> PendingRefSCCStack;
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!");
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!");
1207 RootN->DFSNumber = RootN->LowLink = 1;
1208 int NextDFSNumber = 2;
1210 DFSStack.push_back({RootN, (*RootN)->begin()});
1213 EdgeSequence::iterator I;
1214 std::tie(N, I) = DFSStack.pop_back_val();
1215 auto E = (*N)->end();
1217 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1218 "before processing a node.");
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});
1228 // Continue, resetting to the child node.
1229 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1231 I = ChildN->begin();
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.
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
1259 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1260 ChildRC.Parents.erase(this);
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;
1274 // We short-circuited this node.
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);
1281 // If this node is linked to some lower entry, continue walking up the
1283 if (N->LowLink != N->DFSNumber) {
1284 assert(!DFSStack.empty() &&
1285 "We never found a viable root for a RefSCC to pop off!");
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
1293 auto RefSCCNodes = make_range(
1294 PendingRefSCCStack.rbegin(),
1295 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1296 return N->DFSNumber < RootDFSNumber;
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);
1306 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1307 PendingRefSCCStack.end());
1308 } while (!DFSStack.empty());
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());
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));
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.
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!");
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;
1348 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1349 "Cannot have different numbers for nodes in the same SCC!");
1352 // The root node is handled separately by removing the SCCs.
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;
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);
1369 // Now erase all but the root's SCCs.
1370 SCCs.erase(remove_if(SCCs,
1372 return PostOrderMapping.lookup(&*C->begin()) !=
1373 RootPostOrderNumber;
1377 for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1378 SCCIndices[SCCs[i]] = i;
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.
1386 for (Node &N : *C) {
1387 for (Edge &E : *N) {
1388 RefSCC &ChildRC = *G->lookupRefSCC(E.getNode());
1389 if (&ChildRC == this)
1391 ChildRC.Parents.insert(this);
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.");
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());
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);
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
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());
1429 // Verify all of the new RefSCCs.
1430 for (RefSCC *RC : Result)
1434 // Return the new list of SCCs.
1438 void LazyCallGraph::RefSCC::handleTrivialEdgeInsertion(Node &SourceN,
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) {
1451 #ifdef EXPENSIVE_CHECKS
1452 assert(TargetRC.isDescendantOf(*this) &&
1453 "Target must be a descendant of the Source.");
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);
1460 void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
1463 // Check that the RefSCC is still valid when we finish.
1464 auto ExitVerifier = make_scope_exit([this] { verify(); });
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
1477 // First insert it into the source or find the existing edge.
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];
1484 return; // Nothing to do!
1485 E.setKind(Edge::Call);
1487 // Create the new edge.
1488 SourceN->Edges.emplace_back(TargetN, Edge::Call);
1491 // Now that we have the edge, handle the graph fallout.
1492 handleTrivialEdgeInsertion(SourceN, TargetN);
1495 void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
1497 // Check that the RefSCC is still valid when we finish.
1498 auto ExitVerifier = make_scope_exit([this] { verify(); });
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
1510 // First insert it into the source or find the existing edge.
1512 SourceN->EdgeIndexMap.insert({&TargetN, SourceN->Edges.size()});
1513 if (!InsertResult.second)
1514 // Already an edge, we're done.
1517 // Create the new edge.
1518 SourceN->Edges.emplace_back(TargetN, Edge::Ref);
1520 // Now that we have the edge, handle the graph fallout.
1521 handleTrivialEdgeInsertion(SourceN, TargetN);
1524 void LazyCallGraph::RefSCC::replaceNodeFunction(Node &N, Function &NewF) {
1525 Function &OldF = N.getFunction();
1528 // Check that the RefSCC is still valid when we finish.
1529 auto ExitVerifier = make_scope_exit([this] { verify(); });
1531 assert(G->lookupRefSCC(N) == this &&
1532 "Cannot replace the function of a node outside this RefSCC.");
1534 assert(G->NodeMap.find(&NewF) == G->NodeMap.end() &&
1535 "Must not have already walked the new function!'");
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!");
1548 N.replaceFunction(NewF);
1550 // Update various call graph maps.
1551 G->NodeMap.erase(&OldF);
1552 G->NodeMap[&NewF] = &N;
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!");
1559 return SourceN->insertEdgeInternal(TargetN, EK);
1562 void LazyCallGraph::removeEdge(Node &SourceN, Node &TargetN) {
1563 assert(SCCMap.empty() &&
1564 "This method cannot be called after SCCs have been formed!");
1566 bool Removed = SourceN->removeEdgeInternal(TargetN);
1568 assert(Removed && "Target not in the edge set for this caller?");
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!");
1577 auto NI = NodeMap.find(&F);
1578 if (NI == NodeMap.end())
1579 // Not in the graph at all!
1582 Node &N = *NI->second;
1585 // Remove this from the entry edges if present.
1586 EntryEdges.removeEdgeInternal(N);
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.
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;
1602 RefSCC &RC = C.getOuterRefSCC();
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");
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)
1616 ParentN->removeEdgeInternal(N);
1618 // Now remove this RefSCC from any parents sets and the leaf list.
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
1624 auto LRI = find(LeafRefSCCs, &RC);
1625 if (LRI != LeafRefSCCs.end())
1626 LeafRefSCCs.erase(LRI);
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;
1635 // Finally clear out all the data structures from the node down through the
1641 // Nothing to delete as all the objects are allocated in stable bump pointer
1645 LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1646 return *new (MappedN = BPA.Allocate()) Node(*this, F);
1649 void LazyCallGraph::updateGraphPtrs() {
1650 // Process all nodes updating the graph pointers.
1652 SmallVector<Node *, 16> Worklist;
1653 for (Edge &E : EntryEdges)
1654 Worklist.push_back(&E.getNode());
1656 while (!Worklist.empty()) {
1657 Node &N = *Worklist.pop_back_val();
1661 Worklist.push_back(&E.getNode());
1665 // Process all SCCs updating the graph pointers.
1667 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
1669 while (!Worklist.empty()) {
1670 RefSCC &C = *Worklist.pop_back_val();
1672 for (RefSCC &ParentC : C.parents())
1673 Worklist.push_back(&ParentC);
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;
1685 SmallVector<std::pair<Node *, EdgeItT>, 16> DFSStack;
1686 SmallVector<Node *, 16> PendingSCCStack;
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!");
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!");
1702 RootN->DFSNumber = RootN->LowLink = 1;
1703 int NextDFSNumber = 2;
1705 DFSStack.push_back({RootN, GetBegin(*RootN)});
1709 std::tie(N, I) = DFSStack.pop_back_val();
1710 auto E = GetEnd(*N);
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});
1718 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
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) {
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;
1738 // Move to the next edge.
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);
1746 // If this node is linked to some lower entry, continue walking up the
1748 if (N->LowLink != N->DFSNumber)
1751 // Otherwise, we've completed an SCC. Append it to our post order list of
1753 int RootDFSNumber = N->DFSNumber;
1754 // Find the range of the node stack by walking down until we pass the
1756 auto SCCNodes = make_range(
1757 PendingSCCStack.rbegin(),
1758 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1759 return N->DFSNumber < RootDFSNumber;
1761 // Form a new SCC out of these nodes and then clear them off our pending
1764 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1765 } while (!DFSStack.empty());
1769 /// Build the internal SCCs for a RefSCC from a sequence of nodes.
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
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!");
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 "
1783 // This node will go into the next RefSCC, clear out its DFS and low link
1785 N->DFSNumber = N->LowLink = 0;
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.
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();
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;
1808 void LazyCallGraph::buildRefSCCs() {
1809 if (EntryEdges.empty() || !PostOrderRefSCCs.empty())
1810 // RefSCCs are either non-existent or already built!
1813 assert(RefSCCIndices.empty() && "Already mapped RefSCC indices!");
1815 SmallVector<Node *, 16> Roots;
1816 for (Edge &E : *this)
1817 Roots.push_back(&E.getNode());
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());
1826 // We need to populate each node as we begin to walk its edges.
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);
1837 // Push the new node into the postorder list and remember its position
1838 // in the index map.
1840 RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second;
1842 assert(Inserted && "Cannot already have this RefSCC in the index map!");
1843 PostOrderRefSCCs.push_back(NewRC);
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
1859 for (Edge &E : *N) {
1860 RefSCC &ChildRC = *lookupRefSCC(E.getNode());
1861 if (&ChildRC == &RC)
1863 ChildRC.Parents.insert(&RC);
1867 // For the SCCs where we find no child SCCs, add them to the leaf list.
1869 LeafRefSCCs.push_back(&RC);
1872 AnalysisKey LazyCallGraphAnalysis::Key;
1874 LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
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";
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";
1889 for (LazyCallGraph::Node &N : C)
1890 OS << " " << N.getFunction().getName() << "\n";
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";
1897 for (LazyCallGraph::SCC &InnerC : C)
1898 printSCC(OS, InnerC);
1903 PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
1904 ModuleAnalysisManager &AM) {
1905 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1907 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1910 for (Function &F : M)
1911 printNode(OS, G.get(F));
1914 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1917 return PreservedAnalyses::all();
1920 LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1923 static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1924 std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
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\"]";
1937 PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1938 ModuleAnalysisManager &AM) {
1939 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1941 OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1943 for (Function &F : M)
1944 printNodeDOT(OS, G.get(F));
1948 return PreservedAnalyses::all();