1 //===- LazyCallGraph.h - Analysis of a Module's call graph ------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
11 /// Implements a lazy call graph analysis and related passes for the new pass
14 /// NB: This is *not* a traditional call graph! It is a graph which models both
15 /// the current calls and potential calls. As a consequence there are many
16 /// edges in this call graph that do not correspond to a 'call' or 'invoke'
19 /// The primary use cases of this graph analysis is to facilitate iterating
20 /// across the functions of a module in ways that ensure all callees are
21 /// visited prior to a caller (given any SCC constraints), or vice versa. As
22 /// such is it particularly well suited to organizing CGSCC optimizations such
23 /// as inlining, outlining, argument promotion, etc. That is its primary use
24 /// case and motivates the design. It may not be appropriate for other
25 /// purposes. The use graph of functions or some other conservative analysis of
26 /// call instructions may be interesting for optimizations and subsequent
27 /// analyses which don't work in the context of an overly specified
28 /// potential-call-edge graph.
30 /// To understand the specific rules and nature of this call graph analysis,
31 /// see the documentation of the \c LazyCallGraph below.
33 //===----------------------------------------------------------------------===//
35 #ifndef LLVM_ANALYSIS_LAZYCALLGRAPH_H
36 #define LLVM_ANALYSIS_LAZYCALLGRAPH_H
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/PointerUnion.h"
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/ADT/SetVector.h"
42 #include "llvm/ADT/SmallPtrSet.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/ADT/iterator.h"
45 #include "llvm/ADT/iterator_range.h"
46 #include "llvm/IR/BasicBlock.h"
47 #include "llvm/IR/Constants.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/IR/PassManager.h"
51 #include "llvm/Support/Allocator.h"
52 #include "llvm/Support/raw_ostream.h"
57 class PreservedAnalyses;
60 /// A lazily constructed view of the call graph of a module.
62 /// With the edges of this graph, the motivating constraint that we are
63 /// attempting to maintain is that function-local optimization, CGSCC-local
64 /// optimizations, and optimizations transforming a pair of functions connected
65 /// by an edge in the graph, do not invalidate a bottom-up traversal of the SCC
66 /// DAG. That is, no optimizations will delete, remove, or add an edge such
67 /// that functions already visited in a bottom-up order of the SCC DAG are no
68 /// longer valid to have visited, or such that functions not yet visited in
69 /// a bottom-up order of the SCC DAG are not required to have already been
72 /// Within this constraint, the desire is to minimize the merge points of the
73 /// SCC DAG. The greater the fanout of the SCC DAG and the fewer merge points
74 /// in the SCC DAG, the more independence there is in optimizing within it.
75 /// There is a strong desire to enable parallelization of optimizations over
76 /// the call graph, and both limited fanout and merge points will (artificially
77 /// in some cases) limit the scaling of such an effort.
79 /// To this end, graph represents both direct and any potential resolution to
80 /// an indirect call edge. Another way to think about it is that it represents
81 /// both the direct call edges and any direct call edges that might be formed
82 /// through static optimizations. Specifically, it considers taking the address
83 /// of a function to be an edge in the call graph because this might be
84 /// forwarded to become a direct call by some subsequent function-local
85 /// optimization. The result is that the graph closely follows the use-def
86 /// edges for functions. Walking "up" the graph can be done by looking at all
87 /// of the uses of a function.
89 /// The roots of the call graph are the external functions and functions
90 /// escaped into global variables. Those functions can be called from outside
91 /// of the module or via unknowable means in the IR -- we may not be able to
92 /// form even a potential call edge from a function body which may dynamically
93 /// load the function and call it.
95 /// This analysis still requires updates to remain valid after optimizations
96 /// which could potentially change the set of potential callees. The
97 /// constraints it operates under only make the traversal order remain valid.
99 /// The entire analysis must be re-computed if full interprocedural
100 /// optimizations run at any point. For example, globalopt completely
101 /// invalidates the information in this analysis.
103 /// FIXME: This class is named LazyCallGraph in a lame attempt to distinguish
104 /// it from the existing CallGraph. At some point, it is expected that this
105 /// will be the only call graph and it will be renamed accordingly.
106 class LazyCallGraph {
113 class call_edge_iterator;
115 /// A class used to represent edges in the call graph.
117 /// The lazy call graph models both *call* edges and *reference* edges. Call
118 /// edges are much what you would expect, and exist when there is a 'call' or
119 /// 'invoke' instruction of some function. Reference edges are also tracked
120 /// along side these, and exist whenever any instruction (transitively
121 /// through its operands) references a function. All call edges are
122 /// inherently reference edges, and so the reference graph forms a superset
123 /// of the formal call graph.
125 /// All of these forms of edges are fundamentally represented as outgoing
126 /// edges. The edges are stored in the source node and point at the target
127 /// node. This allows the edge structure itself to be a very compact data
128 /// structure: essentially a tagged pointer.
131 /// The kind of edge in the graph.
132 enum Kind : bool { Ref = false, Call = true };
135 explicit Edge(Node &N, Kind K);
137 /// Test whether the edge is null.
139 /// This happens when an edge has been deleted. We leave the edge objects
140 /// around but clear them.
141 explicit operator bool() const;
143 /// Returnss the \c Kind of the edge.
144 Kind getKind() const;
146 /// Test whether the edge represents a direct call to a function.
148 /// This requires that the edge is not null.
151 /// Get the call graph node referenced by this edge.
153 /// This requires that the edge is not null.
154 Node &getNode() const;
156 /// Get the function referenced by this edge.
158 /// This requires that the edge is not null.
159 Function &getFunction() const;
162 friend class LazyCallGraph::EdgeSequence;
163 friend class LazyCallGraph::RefSCC;
165 PointerIntPair<Node *, 1, Kind> Value;
167 void setKind(Kind K) { Value.setInt(K); }
170 /// The edge sequence object.
172 /// This typically exists entirely within the node but is exposed as
173 /// a separate type because a node doesn't initially have edges. An explicit
174 /// population step is required to produce this sequence at first and it is
175 /// then cached in the node. It is also used to represent edges entering the
176 /// graph from outside the module to model the graph's roots.
178 /// The sequence itself both iterable and indexable. The indexes remain
179 /// stable even as the sequence mutates (including removal).
181 friend class LazyCallGraph;
182 friend class LazyCallGraph::Node;
183 friend class LazyCallGraph::RefSCC;
185 typedef SmallVector<Edge, 4> VectorT;
186 typedef SmallVectorImpl<Edge> VectorImplT;
189 /// An iterator used for the edges to both entry nodes and child nodes.
191 : public iterator_adaptor_base<iterator, VectorImplT::iterator,
192 std::forward_iterator_tag> {
193 friend class LazyCallGraph;
194 friend class LazyCallGraph::Node;
196 VectorImplT::iterator E;
198 // Build the iterator for a specific position in the edge list.
199 iterator(VectorImplT::iterator BaseI, VectorImplT::iterator E)
200 : iterator_adaptor_base(BaseI), E(E) {
201 while (I != E && !*I)
208 using iterator_adaptor_base::operator++;
209 iterator &operator++() {
212 } while (I != E && !*I);
217 /// An iterator over specifically call edges.
219 /// This has the same iteration properties as the \c iterator, but
220 /// restricts itself to edges which represent actual calls.
222 : public iterator_adaptor_base<call_iterator, VectorImplT::iterator,
223 std::forward_iterator_tag> {
224 friend class LazyCallGraph;
225 friend class LazyCallGraph::Node;
227 VectorImplT::iterator E;
229 /// Advance the iterator to the next valid, call edge.
230 void advanceToNextEdge() {
231 while (I != E && (!*I || !I->isCall()))
235 // Build the iterator for a specific position in the edge list.
236 call_iterator(VectorImplT::iterator BaseI, VectorImplT::iterator E)
237 : iterator_adaptor_base(BaseI), E(E) {
244 using iterator_adaptor_base::operator++;
245 call_iterator &operator++() {
252 iterator begin() { return iterator(Edges.begin(), Edges.end()); }
253 iterator end() { return iterator(Edges.end(), Edges.end()); }
255 Edge &operator[](int i) { return Edges[i]; }
256 Edge &operator[](Node &N) {
257 assert(EdgeIndexMap.find(&N) != EdgeIndexMap.end() && "No such edge!");
258 return Edges[EdgeIndexMap.find(&N)->second];
260 Edge *lookup(Node &N) {
261 auto EI = EdgeIndexMap.find(&N);
262 return EI != EdgeIndexMap.end() ? &Edges[EI->second] : nullptr;
265 call_iterator call_begin() {
266 return call_iterator(Edges.begin(), Edges.end());
268 call_iterator call_end() { return call_iterator(Edges.end(), Edges.end()); }
270 iterator_range<call_iterator> calls() {
271 return make_range(call_begin(), call_end());
275 for (auto &E : Edges)
284 DenseMap<Node *, int> EdgeIndexMap;
286 EdgeSequence() = default;
288 /// Internal helper to insert an edge to a node.
289 void insertEdgeInternal(Node &ChildN, Edge::Kind EK);
291 /// Internal helper to change an edge kind.
292 void setEdgeKind(Node &ChildN, Edge::Kind EK);
294 /// Internal helper to remove the edge to the given function.
295 bool removeEdgeInternal(Node &ChildN);
297 /// Internal helper to replace an edge key with a new one.
299 /// This should be used when the function for a particular node in the
300 /// graph gets replaced and we are updating all of the edges to that node
301 /// to use the new function as the key.
302 void replaceEdgeKey(Function &OldTarget, Function &NewTarget);
305 /// A node in the call graph.
307 /// This represents a single node. It's primary roles are to cache the list of
308 /// callees, de-duplicate and provide fast testing of whether a function is
309 /// a callee, and facilitate iteration of child nodes in the graph.
311 /// The node works much like an optional in order to lazily populate the
312 /// edges of each node. Until populated, there are no edges. Once populated,
313 /// you can access the edges by dereferencing the node or using the `->`
314 /// operator as if the node was an `Optional<EdgeSequence>`.
316 friend class LazyCallGraph;
317 friend class LazyCallGraph::RefSCC;
320 LazyCallGraph &getGraph() const { return *G; }
322 Function &getFunction() const { return *F; }
324 StringRef getName() const { return F->getName(); }
326 /// Equality is defined as address equality.
327 bool operator==(const Node &N) const { return this == &N; }
328 bool operator!=(const Node &N) const { return !operator==(N); }
330 /// Tests whether the node has been populated with edges.
331 operator bool() const { return Edges.hasValue(); }
333 // We allow accessing the edges by dereferencing or using the arrow
334 // operator, essentially wrapping the internal optional.
335 EdgeSequence &operator*() const {
336 // Rip const off because the node itself isn't changing here.
337 return const_cast<EdgeSequence &>(*Edges);
339 EdgeSequence *operator->() const { return &**this; }
341 /// Populate the edges of this node if necessary.
343 /// The first time this is called it will populate the edges for this node
344 /// in the graph. It does this by scanning the underlying function, so once
345 /// this is done, any changes to that function must be explicitly reflected
346 /// in updates to the graph.
348 /// \returns the populated \c EdgeSequence to simplify walking it.
350 /// This will not update or re-scan anything if called repeatedly. Instead,
351 /// the edge sequence is cached and returned immediately on subsequent
353 EdgeSequence &populate() {
357 return populateSlow();
364 // We provide for the DFS numbering and Tarjan walk lowlink numbers to be
365 // stored directly within the node. These are both '-1' when nodes are part
366 // of an SCC (or RefSCC), or '0' when not yet reached in a DFS walk.
370 Optional<EdgeSequence> Edges;
372 /// Basic constructor implements the scanning of F into Edges and
374 Node(LazyCallGraph &G, Function &F)
375 : G(&G), F(&F), DFSNumber(0), LowLink(0) {}
377 /// Implementation of the scan when populating.
378 EdgeSequence &populateSlow();
380 /// Internal helper to directly replace the function with a new one.
382 /// This is used to facilitate tranfsormations which need to replace the
383 /// formal Function object but directly move the body and users from one to
385 void replaceFunction(Function &NewF);
387 void clear() { Edges.reset(); }
389 /// Print the name of this node's function.
390 friend raw_ostream &operator<<(raw_ostream &OS, const Node &N) {
391 return OS << N.F->getName();
394 /// Dump the name of this node's function to stderr.
398 /// An SCC of the call graph.
400 /// This represents a Strongly Connected Component of the direct call graph
401 /// -- ignoring indirect calls and function references. It stores this as
402 /// a collection of call graph nodes. While the order of nodes in the SCC is
403 /// stable, it is not any particular order.
405 /// The SCCs are nested within a \c RefSCC, see below for details about that
406 /// outer structure. SCCs do not support mutation of the call graph, that
407 /// must be done through the containing \c RefSCC in order to fully reason
408 /// about the ordering and connections of the graph.
410 friend class LazyCallGraph;
411 friend class LazyCallGraph::Node;
414 SmallVector<Node *, 1> Nodes;
416 template <typename NodeRangeT>
417 SCC(RefSCC &OuterRefSCC, NodeRangeT &&Nodes)
418 : OuterRefSCC(&OuterRefSCC), Nodes(std::forward<NodeRangeT>(Nodes)) {}
421 OuterRefSCC = nullptr;
425 /// Print a short descrtiption useful for debugging or logging.
427 /// We print the function names in the SCC wrapped in '()'s and skipping
428 /// the middle functions if there are a large number.
430 // Note: this is defined inline to dodge issues with GCC's interpretation
431 // of enclosing namespaces for friend function declarations.
432 friend raw_ostream &operator<<(raw_ostream &OS, const SCC &C) {
435 for (LazyCallGraph::Node &N : C) {
438 // Elide the inner elements if there are too many.
440 OS << "..., " << *C.Nodes.back();
450 /// Dump a short description of this SCC to stderr.
454 /// Verify invariants about the SCC.
456 /// This will attempt to validate all of the basic invariants within an
457 /// SCC, but not that it is a strongly connected componet per-se. Primarily
458 /// useful while building and updating the graph to check that basic
459 /// properties are in place rather than having inexplicable crashes later.
464 typedef pointee_iterator<SmallVectorImpl<Node *>::const_iterator> iterator;
466 iterator begin() const { return Nodes.begin(); }
467 iterator end() const { return Nodes.end(); }
469 int size() const { return Nodes.size(); }
471 RefSCC &getOuterRefSCC() const { return *OuterRefSCC; }
473 /// Test if this SCC is a parent of \a C.
475 /// Note that this is linear in the number of edges departing the current
477 bool isParentOf(const SCC &C) const;
479 /// Test if this SCC is an ancestor of \a C.
481 /// Note that in the worst case this is linear in the number of edges
482 /// departing the current SCC and every SCC in the entire graph reachable
483 /// from this SCC. Thus this very well may walk every edge in the entire
484 /// call graph! Do not call this in a tight loop!
485 bool isAncestorOf(const SCC &C) const;
487 /// Test if this SCC is a child of \a C.
489 /// See the comments for \c isParentOf for detailed notes about the
490 /// complexity of this routine.
491 bool isChildOf(const SCC &C) const { return C.isParentOf(*this); }
493 /// Test if this SCC is a descendant of \a C.
495 /// See the comments for \c isParentOf for detailed notes about the
496 /// complexity of this routine.
497 bool isDescendantOf(const SCC &C) const { return C.isAncestorOf(*this); }
499 /// Provide a short name by printing this SCC to a std::string.
501 /// This copes with the fact that we don't have a name per-se for an SCC
502 /// while still making the use of this in debugging and logging useful.
503 std::string getName() const {
505 raw_string_ostream OS(Name);
512 /// A RefSCC of the call graph.
514 /// This models a Strongly Connected Component of function reference edges in
515 /// the call graph. As opposed to actual SCCs, these can be used to scope
516 /// subgraphs of the module which are independent from other subgraphs of the
517 /// module because they do not reference it in any way. This is also the unit
518 /// where we do mutation of the graph in order to restrict mutations to those
519 /// which don't violate this independence.
521 /// A RefSCC contains a DAG of actual SCCs. All the nodes within the RefSCC
522 /// are necessarily within some actual SCC that nests within it. Since
523 /// a direct call *is* a reference, there will always be at least one RefSCC
526 friend class LazyCallGraph;
527 friend class LazyCallGraph::Node;
530 SmallPtrSet<RefSCC *, 1> Parents;
532 /// A postorder list of the inner SCCs.
533 SmallVector<SCC *, 4> SCCs;
535 /// A map from SCC to index in the postorder list.
536 SmallDenseMap<SCC *, int, 4> SCCIndices;
538 /// Fast-path constructor. RefSCCs should instead be constructed by calling
539 /// formRefSCCFast on the graph itself.
540 RefSCC(LazyCallGraph &G);
548 /// Print a short description useful for debugging or logging.
550 /// We print the SCCs wrapped in '[]'s and skipping the middle SCCs if
551 /// there are a large number.
553 // Note: this is defined inline to dodge issues with GCC's interpretation
554 // of enclosing namespaces for friend function declarations.
555 friend raw_ostream &operator<<(raw_ostream &OS, const RefSCC &RC) {
558 for (LazyCallGraph::SCC &C : RC) {
561 // Elide the inner elements if there are too many.
563 OS << "..., " << *RC.SCCs.back();
573 /// Dump a short description of this RefSCC to stderr.
577 /// Verify invariants about the RefSCC and all its SCCs.
579 /// This will attempt to validate all of the invariants *within* the
580 /// RefSCC, but not that it is a strongly connected component of the larger
581 /// graph. This makes it useful even when partially through an update.
583 /// Invariants checked:
584 /// - SCCs and their indices match.
585 /// - The SCCs list is in fact in post-order.
589 /// Handle any necessary parent set updates after inserting a trivial ref
591 void handleTrivialEdgeInsertion(Node &SourceN, Node &TargetN);
594 typedef pointee_iterator<SmallVectorImpl<SCC *>::const_iterator> iterator;
595 typedef iterator_range<iterator> range;
596 typedef pointee_iterator<SmallPtrSetImpl<RefSCC *>::const_iterator>
599 iterator begin() const { return SCCs.begin(); }
600 iterator end() const { return SCCs.end(); }
602 ssize_t size() const { return SCCs.size(); }
604 SCC &operator[](int Idx) { return *SCCs[Idx]; }
606 iterator find(SCC &C) const {
607 return SCCs.begin() + SCCIndices.find(&C)->second;
610 parent_iterator parent_begin() const { return Parents.begin(); }
611 parent_iterator parent_end() const { return Parents.end(); }
613 iterator_range<parent_iterator> parents() const {
614 return make_range(parent_begin(), parent_end());
617 /// Test if this RefSCC is a parent of \a C.
618 bool isParentOf(const RefSCC &C) const { return C.isChildOf(*this); }
620 /// Test if this RefSCC is an ancestor of \a C.
621 bool isAncestorOf(const RefSCC &C) const { return C.isDescendantOf(*this); }
623 /// Test if this RefSCC is a child of \a C.
624 bool isChildOf(const RefSCC &C) const {
625 return Parents.count(const_cast<RefSCC *>(&C));
628 /// Test if this RefSCC is a descendant of \a C.
629 bool isDescendantOf(const RefSCC &C) const;
631 /// Provide a short name by printing this RefSCC to a std::string.
633 /// This copes with the fact that we don't have a name per-se for an RefSCC
634 /// while still making the use of this in debugging and logging useful.
635 std::string getName() const {
637 raw_string_ostream OS(Name);
644 /// \name Mutation API
646 /// These methods provide the core API for updating the call graph in the
647 /// presence of (potentially still in-flight) DFS-found RefSCCs and SCCs.
649 /// Note that these methods sometimes have complex runtimes, so be careful
650 /// how you call them.
652 /// Make an existing internal ref edge into a call edge.
654 /// This may form a larger cycle and thus collapse SCCs into TargetN's SCC.
655 /// If that happens, the deleted SCC pointers are returned. These SCCs are
656 /// not in a valid state any longer but the pointers will remain valid
657 /// until destruction of the parent graph instance for the purpose of
658 /// clearing cached information.
660 /// After this operation, both SourceN's SCC and TargetN's SCC may move
661 /// position within this RefSCC's postorder list. Any SCCs merged are
662 /// merged into the TargetN's SCC in order to preserve reachability analyses
663 /// which took place on that SCC.
664 SmallVector<SCC *, 1> switchInternalEdgeToCall(Node &SourceN,
667 /// Make an existing internal call edge between separate SCCs into a ref
670 /// If SourceN and TargetN in separate SCCs within this RefSCC, changing
671 /// the call edge between them to a ref edge is a trivial operation that
672 /// does not require any structural changes to the call graph.
673 void switchTrivialInternalEdgeToRef(Node &SourceN, Node &TargetN);
675 /// Make an existing internal call edge within a single SCC into a ref
678 /// Since SourceN and TargetN are part of a single SCC, this SCC may be
679 /// split up due to breaking a cycle in the call edges that formed it. If
680 /// that happens, then this routine will insert new SCCs into the postorder
681 /// list *before* the SCC of TargetN (previously the SCC of both). This
682 /// preserves postorder as the TargetN can reach all of the other nodes by
683 /// definition of previously being in a single SCC formed by the cycle from
684 /// SourceN to TargetN.
686 /// The newly added SCCs are added *immediately* and contiguously
687 /// prior to the TargetN SCC and return the range covering the new SCCs in
688 /// the RefSCC's postorder sequence. You can directly iterate the returned
689 /// range to observe all of the new SCCs in postorder.
691 /// Note that if SourceN and TargetN are in separate SCCs, the simpler
692 /// routine `switchTrivialInternalEdgeToRef` should be used instead.
693 iterator_range<iterator> switchInternalEdgeToRef(Node &SourceN,
696 /// Make an existing outgoing ref edge into a call edge.
698 /// Note that this is trivial as there are no cyclic impacts and there
699 /// remains a reference edge.
700 void switchOutgoingEdgeToCall(Node &SourceN, Node &TargetN);
702 /// Make an existing outgoing call edge into a ref edge.
704 /// This is trivial as there are no cyclic impacts and there remains
705 /// a reference edge.
706 void switchOutgoingEdgeToRef(Node &SourceN, Node &TargetN);
708 /// Insert a ref edge from one node in this RefSCC to another in this
711 /// This is always a trivial operation as it doesn't change any part of the
712 /// graph structure besides connecting the two nodes.
714 /// Note that we don't support directly inserting internal *call* edges
715 /// because that could change the graph structure and requires returning
716 /// information about what became invalid. As a consequence, the pattern
717 /// should be to first insert the necessary ref edge, and then to switch it
718 /// to a call edge if needed and handle any invalidation that results. See
719 /// the \c switchInternalEdgeToCall routine for details.
720 void insertInternalRefEdge(Node &SourceN, Node &TargetN);
722 /// Insert an edge whose parent is in this RefSCC and child is in some
725 /// There must be an existing path from the \p SourceN to the \p TargetN.
726 /// This operation is inexpensive and does not change the set of SCCs and
727 /// RefSCCs in the graph.
728 void insertOutgoingEdge(Node &SourceN, Node &TargetN, Edge::Kind EK);
730 /// Insert an edge whose source is in a descendant RefSCC and target is in
733 /// There must be an existing path from the target to the source in this
736 /// NB! This is has the potential to be a very expensive function. It
737 /// inherently forms a cycle in the prior RefSCC DAG and we have to merge
738 /// RefSCCs to resolve that cycle. But finding all of the RefSCCs which
739 /// participate in the cycle can in the worst case require traversing every
740 /// RefSCC in the graph. Every attempt is made to avoid that, but passes
741 /// must still exercise caution calling this routine repeatedly.
743 /// Also note that this can only insert ref edges. In order to insert
744 /// a call edge, first insert a ref edge and then switch it to a call edge.
745 /// These are intentionally kept as separate interfaces because each step
746 /// of the operation invalidates a different set of data structures.
748 /// This returns all the RefSCCs which were merged into the this RefSCC
749 /// (the target's). This allows callers to invalidate any cached
752 /// FIXME: We could possibly optimize this quite a bit for cases where the
753 /// caller and callee are very nearby in the graph. See comments in the
754 /// implementation for details, but that use case might impact users.
755 SmallVector<RefSCC *, 1> insertIncomingRefEdge(Node &SourceN,
758 /// Remove an edge whose source is in this RefSCC and target is *not*.
760 /// This removes an inter-RefSCC edge. All inter-RefSCC edges originating
761 /// from this SCC have been fully explored by any in-flight DFS graph
762 /// formation, so this is always safe to call once you have the source
765 /// This operation does not change the cyclic structure of the graph and so
766 /// is very inexpensive. It may change the connectivity graph of the SCCs
767 /// though, so be careful calling this while iterating over them.
768 void removeOutgoingEdge(Node &SourceN, Node &TargetN);
770 /// Remove a ref edge which is entirely within this RefSCC.
772 /// Both the \a SourceN and the \a TargetN must be within this RefSCC.
773 /// Removing such an edge may break cycles that form this RefSCC and thus
774 /// this operation may change the RefSCC graph significantly. In
775 /// particular, this operation will re-form new RefSCCs based on the
776 /// remaining connectivity of the graph. The following invariants are
777 /// guaranteed to hold after calling this method:
779 /// 1) This RefSCC is still a RefSCC in the graph.
780 /// 2) This RefSCC will be the parent of any new RefSCCs. Thus, this RefSCC
781 /// is preserved as the root of any new RefSCC DAG formed.
782 /// 3) No RefSCC other than this RefSCC has its member set changed (this is
783 /// inherent in the definition of removing such an edge).
784 /// 4) All of the parent links of the RefSCC graph will be updated to
785 /// reflect the new RefSCC structure.
786 /// 5) All RefSCCs formed out of this RefSCC, excluding this RefSCC, will
787 /// be returned in post-order.
788 /// 6) The order of the RefSCCs in the vector will be a valid postorder
789 /// traversal of the new RefSCCs.
791 /// These invariants are very important to ensure that we can build
792 /// optimization pipelines on top of the CGSCC pass manager which
793 /// intelligently update the RefSCC graph without invalidating other parts
794 /// of the RefSCC graph.
796 /// Note that we provide no routine to remove a *call* edge. Instead, you
797 /// must first switch it to a ref edge using \c switchInternalEdgeToRef.
798 /// This split API is intentional as each of these two steps can invalidate
799 /// a different aspect of the graph structure and needs to have the
800 /// invalidation handled independently.
802 /// The runtime complexity of this method is, in the worst case, O(V+E)
803 /// where V is the number of nodes in this RefSCC and E is the number of
804 /// edges leaving the nodes in this RefSCC. Note that E includes both edges
805 /// within this RefSCC and edges from this RefSCC to child RefSCCs. Some
806 /// effort has been made to minimize the overhead of common cases such as
807 /// self-edges and edge removals which result in a spanning tree with no
808 /// more cycles. There are also detailed comments within the implementation
809 /// on techniques which could substantially improve this routine's
811 SmallVector<RefSCC *, 1> removeInternalRefEdge(Node &SourceN,
814 /// A convenience wrapper around the above to handle trivial cases of
815 /// inserting a new call edge.
817 /// This is trivial whenever the target is in the same SCC as the source or
818 /// the edge is an outgoing edge to some descendant SCC. In these cases
819 /// there is no change to the cyclic structure of SCCs or RefSCCs.
821 /// To further make calling this convenient, it also handles inserting
822 /// already existing edges.
823 void insertTrivialCallEdge(Node &SourceN, Node &TargetN);
825 /// A convenience wrapper around the above to handle trivial cases of
826 /// inserting a new ref edge.
828 /// This is trivial whenever the target is in the same RefSCC as the source
829 /// or the edge is an outgoing edge to some descendant RefSCC. In these
830 /// cases there is no change to the cyclic structure of the RefSCCs.
832 /// To further make calling this convenient, it also handles inserting
833 /// already existing edges.
834 void insertTrivialRefEdge(Node &SourceN, Node &TargetN);
836 /// Directly replace a node's function with a new function.
838 /// This should be used when moving the body and users of a function to
839 /// a new formal function object but not otherwise changing the call graph
840 /// structure in any way.
842 /// It requires that the old function in the provided node have zero uses
843 /// and the new function must have calls and references to it establishing
844 /// an equivalent graph.
845 void replaceNodeFunction(Node &N, Function &NewF);
850 /// A post-order depth-first RefSCC iterator over the call graph.
852 /// This iterator walks the cached post-order sequence of RefSCCs. However,
853 /// it trades stability for flexibility. It is restricted to a forward
854 /// iterator but will survive mutations which insert new RefSCCs and continue
855 /// to point to the same RefSCC even if it moves in the post-order sequence.
856 class postorder_ref_scc_iterator
857 : public iterator_facade_base<postorder_ref_scc_iterator,
858 std::forward_iterator_tag, RefSCC> {
859 friend class LazyCallGraph;
860 friend class LazyCallGraph::Node;
862 /// Nonce type to select the constructor for the end iterator.
868 /// Build the begin iterator for a node.
869 postorder_ref_scc_iterator(LazyCallGraph &G) : G(&G), RC(getRC(G, 0)) {}
871 /// Build the end iterator for a node. This is selected purely by overload.
872 postorder_ref_scc_iterator(LazyCallGraph &G, IsAtEndT /*Nonce*/)
873 : G(&G), RC(nullptr) {}
875 /// Get the post-order RefSCC at the given index of the postorder walk,
876 /// populating it if necessary.
877 static RefSCC *getRC(LazyCallGraph &G, int Index) {
878 if (Index == (int)G.PostOrderRefSCCs.size())
882 return G.PostOrderRefSCCs[Index];
886 bool operator==(const postorder_ref_scc_iterator &Arg) const {
887 return G == Arg.G && RC == Arg.RC;
890 reference operator*() const { return *RC; }
892 using iterator_facade_base::operator++;
893 postorder_ref_scc_iterator &operator++() {
894 assert(RC && "Cannot increment the end iterator!");
895 RC = getRC(*G, G->RefSCCIndices.find(RC)->second + 1);
900 /// Construct a graph for the given module.
902 /// This sets up the graph and computes all of the entry points of the graph.
903 /// No function definitions are scanned until their nodes in the graph are
904 /// requested during traversal.
905 LazyCallGraph(Module &M);
907 LazyCallGraph(LazyCallGraph &&G);
908 LazyCallGraph &operator=(LazyCallGraph &&RHS);
910 EdgeSequence::iterator begin() { return EntryEdges.begin(); }
911 EdgeSequence::iterator end() { return EntryEdges.end(); }
915 postorder_ref_scc_iterator postorder_ref_scc_begin() {
916 if (!EntryEdges.empty())
917 assert(!PostOrderRefSCCs.empty() &&
918 "Must form RefSCCs before iterating them!");
919 return postorder_ref_scc_iterator(*this);
921 postorder_ref_scc_iterator postorder_ref_scc_end() {
922 if (!EntryEdges.empty())
923 assert(!PostOrderRefSCCs.empty() &&
924 "Must form RefSCCs before iterating them!");
925 return postorder_ref_scc_iterator(*this,
926 postorder_ref_scc_iterator::IsAtEndT());
929 iterator_range<postorder_ref_scc_iterator> postorder_ref_sccs() {
930 return make_range(postorder_ref_scc_begin(), postorder_ref_scc_end());
933 /// Lookup a function in the graph which has already been scanned and added.
934 Node *lookup(const Function &F) const { return NodeMap.lookup(&F); }
936 /// Lookup a function's SCC in the graph.
938 /// \returns null if the function hasn't been assigned an SCC via the RefSCC
940 SCC *lookupSCC(Node &N) const { return SCCMap.lookup(&N); }
942 /// Lookup a function's RefSCC in the graph.
944 /// \returns null if the function hasn't been assigned a RefSCC via the
945 /// RefSCC iterator walk.
946 RefSCC *lookupRefSCC(Node &N) const {
947 if (SCC *C = lookupSCC(N))
948 return &C->getOuterRefSCC();
953 /// Get a graph node for a given function, scanning it to populate the graph
954 /// data as necessary.
955 Node &get(Function &F) {
956 Node *&N = NodeMap[&F];
960 return insertInto(F, N);
964 /// \name Pre-SCC Mutation API
966 /// These methods are only valid to call prior to forming any SCCs for this
967 /// call graph. They can be used to update the core node-graph during
968 /// a node-based inorder traversal that precedes any SCC-based traversal.
970 /// Once you begin manipulating a call graph's SCCs, most mutation of the
971 /// graph must be performed via a RefSCC method. There are some exceptions
974 /// Update the call graph after inserting a new edge.
975 void insertEdge(Node &SourceN, Node &TargetN, Edge::Kind EK);
977 /// Update the call graph after inserting a new edge.
978 void insertEdge(Function &Source, Function &Target, Edge::Kind EK) {
979 return insertEdge(get(Source), get(Target), EK);
982 /// Update the call graph after deleting an edge.
983 void removeEdge(Node &SourceN, Node &TargetN);
985 /// Update the call graph after deleting an edge.
986 void removeEdge(Function &Source, Function &Target) {
987 return removeEdge(get(Source), get(Target));
993 /// \name General Mutation API
995 /// There are a very limited set of mutations allowed on the graph as a whole
996 /// once SCCs have started to be formed. These routines have strict contracts
997 /// but may be called at any point.
999 /// Remove a dead function from the call graph (typically to delete it).
1001 /// Note that the function must have an empty use list, and the call graph
1002 /// must be up-to-date prior to calling this. That means it is by itself in
1003 /// a maximal SCC which is by itself in a maximal RefSCC, etc. No structural
1004 /// changes result from calling this routine other than potentially removing
1005 /// entry points into the call graph.
1007 /// If SCC formation has begun, this function must not be part of the current
1008 /// DFS in order to call this safely. Typically, the function will have been
1009 /// fully visited by the DFS prior to calling this routine.
1010 void removeDeadFunction(Function &F);
1015 /// \name Static helpers for code doing updates to the call graph.
1017 /// These helpers are used to implement parts of the call graph but are also
1018 /// useful to code doing updates or otherwise wanting to walk the IR in the
1019 /// same patterns as when we build the call graph.
1021 /// Recursively visits the defined functions whose address is reachable from
1022 /// every constant in the \p Worklist.
1024 /// Doesn't recurse through any constants already in the \p Visited set, and
1025 /// updates that set with every constant visited.
1027 /// For each defined function, calls \p Callback with that function.
1028 template <typename CallbackT>
1029 static void visitReferences(SmallVectorImpl<Constant *> &Worklist,
1030 SmallPtrSetImpl<Constant *> &Visited,
1031 CallbackT Callback) {
1032 while (!Worklist.empty()) {
1033 Constant *C = Worklist.pop_back_val();
1035 if (Function *F = dyn_cast<Function>(C)) {
1036 if (!F->isDeclaration())
1041 if (BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1042 // The blockaddress constant expression is a weird special case, we
1043 // can't generically walk its operands the way we do for all other
1045 if (Visited.insert(BA->getFunction()).second)
1046 Worklist.push_back(BA->getFunction());
1050 for (Value *Op : C->operand_values())
1051 if (Visited.insert(cast<Constant>(Op)).second)
1052 Worklist.push_back(cast<Constant>(Op));
1059 typedef SmallVectorImpl<Node *>::reverse_iterator node_stack_iterator;
1060 typedef iterator_range<node_stack_iterator> node_stack_range;
1062 /// Allocator that holds all the call graph nodes.
1063 SpecificBumpPtrAllocator<Node> BPA;
1065 /// Maps function->node for fast lookup.
1066 DenseMap<const Function *, Node *> NodeMap;
1068 /// The entry edges into the graph.
1070 /// These edges are from "external" sources. Put another way, they
1071 /// escape at the module scope.
1072 EdgeSequence EntryEdges;
1074 /// Allocator that holds all the call graph SCCs.
1075 SpecificBumpPtrAllocator<SCC> SCCBPA;
1077 /// Maps Function -> SCC for fast lookup.
1078 DenseMap<Node *, SCC *> SCCMap;
1080 /// Allocator that holds all the call graph RefSCCs.
1081 SpecificBumpPtrAllocator<RefSCC> RefSCCBPA;
1083 /// The post-order sequence of RefSCCs.
1085 /// This list is lazily formed the first time we walk the graph.
1086 SmallVector<RefSCC *, 16> PostOrderRefSCCs;
1088 /// A map from RefSCC to the index for it in the postorder sequence of
1090 DenseMap<RefSCC *, int> RefSCCIndices;
1092 /// The leaf RefSCCs of the graph.
1094 /// These are all of the RefSCCs which have no children.
1095 SmallVector<RefSCC *, 4> LeafRefSCCs;
1097 /// Helper to insert a new function, with an already looked-up entry in
1099 Node &insertInto(Function &F, Node *&MappedN);
1101 /// Helper to update pointers back to the graph object during moves.
1102 void updateGraphPtrs();
1104 /// Allocates an SCC and constructs it using the graph allocator.
1106 /// The arguments are forwarded to the constructor.
1107 template <typename... Ts> SCC *createSCC(Ts &&... Args) {
1108 return new (SCCBPA.Allocate()) SCC(std::forward<Ts>(Args)...);
1111 /// Allocates a RefSCC and constructs it using the graph allocator.
1113 /// The arguments are forwarded to the constructor.
1114 template <typename... Ts> RefSCC *createRefSCC(Ts &&... Args) {
1115 return new (RefSCCBPA.Allocate()) RefSCC(std::forward<Ts>(Args)...);
1118 /// Common logic for building SCCs from a sequence of roots.
1120 /// This is a very generic implementation of the depth-first walk and SCC
1121 /// formation algorithm. It uses a generic sequence of roots and generic
1122 /// callbacks for each step. This is designed to be used to implement both
1123 /// the RefSCC formation and SCC formation with shared logic.
1125 /// Currently this is a relatively naive implementation of Tarjan's DFS
1126 /// algorithm to form the SCCs.
1128 /// FIXME: We should consider newer variants such as Nuutila.
1129 template <typename RootsT, typename GetBeginT, typename GetEndT,
1130 typename GetNodeT, typename FormSCCCallbackT>
1131 static void buildGenericSCCs(RootsT &&Roots, GetBeginT &&GetBegin,
1132 GetEndT &&GetEnd, GetNodeT &&GetNode,
1133 FormSCCCallbackT &&FormSCC);
1135 /// Build the SCCs for a RefSCC out of a list of nodes.
1136 void buildSCCs(RefSCC &RC, node_stack_range Nodes);
1138 /// Connect a RefSCC into the larger graph.
1140 /// This walks the edges to connect the RefSCC to its children's parent set,
1141 /// and updates the root leaf list.
1142 void connectRefSCC(RefSCC &RC);
1144 /// Get the index of a RefSCC within the postorder traversal.
1146 /// Requires that this RefSCC is a valid one in the (perhaps partial)
1147 /// postorder traversed part of the graph.
1148 int getRefSCCIndex(RefSCC &RC) {
1149 auto IndexIt = RefSCCIndices.find(&RC);
1150 assert(IndexIt != RefSCCIndices.end() && "RefSCC doesn't have an index!");
1151 assert(PostOrderRefSCCs[IndexIt->second] == &RC &&
1152 "Index does not point back at RC!");
1153 return IndexIt->second;
1157 inline LazyCallGraph::Edge::Edge() : Value() {}
1158 inline LazyCallGraph::Edge::Edge(Node &N, Kind K) : Value(&N, K) {}
1160 inline LazyCallGraph::Edge::operator bool() const { return Value.getPointer(); }
1162 inline LazyCallGraph::Edge::Kind LazyCallGraph::Edge::getKind() const {
1163 assert(*this && "Queried a null edge!");
1164 return Value.getInt();
1167 inline bool LazyCallGraph::Edge::isCall() const {
1168 assert(*this && "Queried a null edge!");
1169 return getKind() == Call;
1172 inline LazyCallGraph::Node &LazyCallGraph::Edge::getNode() const {
1173 assert(*this && "Queried a null edge!");
1174 return *Value.getPointer();
1177 inline Function &LazyCallGraph::Edge::getFunction() const {
1178 assert(*this && "Queried a null edge!");
1179 return getNode().getFunction();
1182 // Provide GraphTraits specializations for call graphs.
1183 template <> struct GraphTraits<LazyCallGraph::Node *> {
1184 typedef LazyCallGraph::Node *NodeRef;
1185 typedef LazyCallGraph::EdgeSequence::iterator ChildIteratorType;
1187 static NodeRef getEntryNode(NodeRef N) { return N; }
1188 static ChildIteratorType child_begin(NodeRef N) { return (*N)->begin(); }
1189 static ChildIteratorType child_end(NodeRef N) { return (*N)->end(); }
1191 template <> struct GraphTraits<LazyCallGraph *> {
1192 typedef LazyCallGraph::Node *NodeRef;
1193 typedef LazyCallGraph::EdgeSequence::iterator ChildIteratorType;
1195 static NodeRef getEntryNode(NodeRef N) { return N; }
1196 static ChildIteratorType child_begin(NodeRef N) { return (*N)->begin(); }
1197 static ChildIteratorType child_end(NodeRef N) { return (*N)->end(); }
1200 /// An analysis pass which computes the call graph for a module.
1201 class LazyCallGraphAnalysis : public AnalysisInfoMixin<LazyCallGraphAnalysis> {
1202 friend AnalysisInfoMixin<LazyCallGraphAnalysis>;
1203 static AnalysisKey Key;
1206 /// Inform generic clients of the result type.
1207 typedef LazyCallGraph Result;
1209 /// Compute the \c LazyCallGraph for the module \c M.
1211 /// This just builds the set of entry points to the call graph. The rest is
1212 /// built lazily as it is walked.
1213 LazyCallGraph run(Module &M, ModuleAnalysisManager &) {
1214 return LazyCallGraph(M);
1218 /// A pass which prints the call graph to a \c raw_ostream.
1220 /// This is primarily useful for testing the analysis.
1221 class LazyCallGraphPrinterPass
1222 : public PassInfoMixin<LazyCallGraphPrinterPass> {
1226 explicit LazyCallGraphPrinterPass(raw_ostream &OS);
1228 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
1231 /// A pass which prints the call graph as a DOT file to a \c raw_ostream.
1233 /// This is primarily useful for visualization purposes.
1234 class LazyCallGraphDOTPrinterPass
1235 : public PassInfoMixin<LazyCallGraphDOTPrinterPass> {
1239 explicit LazyCallGraphDOTPrinterPass(raw_ostream &OS);
1241 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);