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_LAZY_CALL_GRAPH
36 #define LLVM_ANALYSIS_LAZY_CALL_GRAPH
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_range.h"
45 #include "llvm/IR/BasicBlock.h"
46 #include "llvm/IR/Function.h"
47 #include "llvm/IR/Module.h"
48 #include "llvm/Support/Allocator.h"
52 class ModuleAnalysisManager;
53 class PreservedAnalyses;
56 /// \brief A lazily constructed view of the call graph of a module.
58 /// With the edges of this graph, the motivating constraint that we are
59 /// attempting to maintain is that function-local optimization, CGSCC-local
60 /// optimizations, and optimizations transforming a pair of functions connected
61 /// by an edge in the graph, do not invalidate a bottom-up traversal of the SCC
62 /// DAG. That is, no optimizations will delete, remove, or add an edge such
63 /// that functions already visited in a bottom-up order of the SCC DAG are no
64 /// longer valid to have visited, or such that functions not yet visited in
65 /// a bottom-up order of the SCC DAG are not required to have already been
68 /// Within this constraint, the desire is to minimize the merge points of the
69 /// SCC DAG. The greater the fanout of the SCC DAG and the fewer merge points
70 /// in the SCC DAG, the more independence there is in optimizing within it.
71 /// There is a strong desire to enable parallelization of optimizations over
72 /// the call graph, and both limited fanout and merge points will (artificially
73 /// in some cases) limit the scaling of such an effort.
75 /// To this end, graph represents both direct and any potential resolution to
76 /// an indirect call edge. Another way to think about it is that it represents
77 /// both the direct call edges and any direct call edges that might be formed
78 /// through static optimizations. Specifically, it considers taking the address
79 /// of a function to be an edge in the call graph because this might be
80 /// forwarded to become a direct call by some subsequent function-local
81 /// optimization. The result is that the graph closely follows the use-def
82 /// edges for functions. Walking "up" the graph can be done by looking at all
83 /// of the uses of a function.
85 /// The roots of the call graph are the external functions and functions
86 /// escaped into global variables. Those functions can be called from outside
87 /// of the module or via unknowable means in the IR -- we may not be able to
88 /// form even a potential call edge from a function body which may dynamically
89 /// load the function and call it.
91 /// This analysis still requires updates to remain valid after optimizations
92 /// which could potentially change the set of potential callees. The
93 /// constraints it operates under only make the traversal order remain valid.
95 /// The entire analysis must be re-computed if full interprocedural
96 /// optimizations run at any point. For example, globalopt completely
97 /// invalidates the information in this analysis.
99 /// FIXME: This class is named LazyCallGraph in a lame attempt to distinguish
100 /// it from the existing CallGraph. At some point, it is expected that this
101 /// will be the only call graph and it will be renamed accordingly.
102 class LazyCallGraph {
106 typedef SmallVector<PointerUnion<Function *, Node *>, 4> NodeVectorT;
107 typedef SmallVectorImpl<PointerUnion<Function *, Node *>> NodeVectorImplT;
109 /// \brief A lazy iterator used for both the entry nodes and child nodes.
111 /// When this iterator is dereferenced, if not yet available, a function will
112 /// be scanned for "calls" or uses of functions and its child information
113 /// will be constructed. All of these results are accumulated and cached in
115 class iterator : public std::iterator<std::bidirectional_iterator_tag, Node> {
116 friend class LazyCallGraph;
117 friend class LazyCallGraph::Node;
119 /// \brief Nonce type to select the constructor for the end iterator.
123 NodeVectorImplT::iterator NI;
125 // Build the begin iterator for a node.
126 explicit iterator(LazyCallGraph &G, NodeVectorImplT &Nodes)
127 : G(&G), NI(Nodes.begin()) {}
129 // Build the end iterator for a node. This is selected purely by overload.
130 iterator(LazyCallGraph &G, NodeVectorImplT &Nodes, IsAtEndT /*Nonce*/)
131 : G(&G), NI(Nodes.end()) {}
134 bool operator==(const iterator &Arg) const { return NI == Arg.NI; }
135 bool operator!=(const iterator &Arg) const { return !operator==(Arg); }
137 reference operator*() const {
138 if (NI->is<Node *>())
139 return *NI->get<Node *>();
141 Function *F = NI->get<Function *>();
142 Node &ChildN = G->get(*F);
146 pointer operator->() const { return &operator*(); }
148 iterator &operator++() {
152 iterator operator++(int) {
153 iterator prev = *this;
158 iterator &operator--() {
162 iterator operator--(int) {
163 iterator next = *this;
169 /// \brief A node in the call graph.
171 /// This represents a single node. It's primary roles are to cache the list of
172 /// callees, de-duplicate and provide fast testing of whether a function is
173 /// a callee, and facilitate iteration of child nodes in the graph.
175 friend class LazyCallGraph;
176 friend class LazyCallGraph::SCC;
181 // We provide for the DFS numbering and Tarjan walk lowlink numbers to be
182 // stored directly within the node.
186 mutable NodeVectorT Callees;
187 DenseMap<Function *, size_t> CalleeIndexMap;
189 /// \brief Basic constructor implements the scanning of F into Callees and
191 Node(LazyCallGraph &G, Function &F);
194 typedef LazyCallGraph::iterator iterator;
196 Function &getFunction() const {
200 iterator begin() const { return iterator(*G, Callees); }
201 iterator end() const { return iterator(*G, Callees, iterator::IsAtEndT()); }
203 /// Equality is defined as address equality.
204 bool operator==(const Node &N) const { return this == &N; }
205 bool operator!=(const Node &N) const { return !operator==(N); }
208 /// \brief An SCC of the call graph.
210 /// This represents a Strongly Connected Component of the call graph as
211 /// a collection of call graph nodes. While the order of nodes in the SCC is
212 /// stable, it is not any particular order.
214 friend class LazyCallGraph;
215 friend class LazyCallGraph::Node;
217 SmallSetVector<SCC *, 1> ParentSCCs;
218 SmallVector<Node *, 1> Nodes;
219 SmallPtrSet<Function *, 1> NodeSet;
223 void removeEdge(LazyCallGraph &G, Function &Caller, Function &Callee,
226 SmallVector<LazyCallGraph::SCC *, 1>
227 removeInternalEdge(LazyCallGraph &G, Node &Caller, Node &Callee);
230 typedef SmallVectorImpl<Node *>::const_iterator iterator;
231 typedef SmallSetVector<SCC *, 1>::const_iterator parent_iterator;
233 iterator begin() const { return Nodes.begin(); }
234 iterator end() const { return Nodes.end(); }
236 parent_iterator parent_begin() const { return ParentSCCs.begin(); }
237 parent_iterator parent_end() const { return ParentSCCs.end(); }
239 iterator_range<parent_iterator> parents() const {
240 return iterator_range<parent_iterator>(parent_begin(), parent_end());
244 /// \brief A post-order depth-first SCC iterator over the call graph.
246 /// This iterator triggers the Tarjan DFS-based formation of the SCC DAG for
247 /// the call graph, walking it lazily in depth-first post-order. That is, it
248 /// always visits SCCs for a callee prior to visiting the SCC for a caller
249 /// (when they are in different SCCs).
250 class postorder_scc_iterator
251 : public std::iterator<std::forward_iterator_tag, SCC *, ptrdiff_t, SCC *,
253 friend class LazyCallGraph;
254 friend class LazyCallGraph::Node;
256 /// \brief Nonce type to select the constructor for the end iterator.
262 // Build the begin iterator for a node.
263 postorder_scc_iterator(LazyCallGraph &G) : G(&G) {
264 C = G.getNextSCCInPostOrder();
267 // Build the end iterator for a node. This is selected purely by overload.
268 postorder_scc_iterator(LazyCallGraph &G, IsAtEndT /*Nonce*/)
269 : G(&G), C(nullptr) {}
272 bool operator==(const postorder_scc_iterator &Arg) const {
273 return G == Arg.G && C == Arg.C;
275 bool operator!=(const postorder_scc_iterator &Arg) const {
276 return !operator==(Arg);
279 reference operator*() const { return C; }
280 pointer operator->() const { return operator*(); }
282 postorder_scc_iterator &operator++() {
283 C = G->getNextSCCInPostOrder();
286 postorder_scc_iterator operator++(int) {
287 postorder_scc_iterator prev = *this;
293 /// \brief Construct a graph for the given module.
295 /// This sets up the graph and computes all of the entry points of the graph.
296 /// No function definitions are scanned until their nodes in the graph are
297 /// requested during traversal.
298 LazyCallGraph(Module &M);
300 LazyCallGraph(LazyCallGraph &&G);
301 LazyCallGraph &operator=(LazyCallGraph &&RHS);
303 iterator begin() { return iterator(*this, EntryNodes); }
304 iterator end() { return iterator(*this, EntryNodes, iterator::IsAtEndT()); }
306 postorder_scc_iterator postorder_scc_begin() {
307 return postorder_scc_iterator(*this);
309 postorder_scc_iterator postorder_scc_end() {
310 return postorder_scc_iterator(*this, postorder_scc_iterator::IsAtEndT());
313 iterator_range<postorder_scc_iterator> postorder_sccs() {
314 return iterator_range<postorder_scc_iterator>(postorder_scc_begin(),
315 postorder_scc_end());
318 /// \brief Lookup a function in the graph which has already been scanned and
320 Node *lookup(const Function &F) const { return NodeMap.lookup(&F); }
322 /// \brief Lookup a function's SCC in the graph.
324 /// \returns null if the function hasn't been assigned an SCC via the SCC
326 SCC *lookupSCC(Node &N) const { return SCCMap.lookup(&N); }
328 /// \brief Get a graph node for a given function, scanning it to populate the
329 /// graph data as necessary.
330 Node &get(Function &F) {
331 Node *&N = NodeMap[&F];
335 return insertInto(F, N);
338 /// \brief Update the call graph after deleting an edge.
339 void removeEdge(Node &Caller, Function &Callee);
341 /// \brief Update the call graph after deleting an edge.
342 void removeEdge(Function &Caller, Function &Callee) {
343 return removeEdge(get(Caller), Callee);
347 /// \brief Allocator that holds all the call graph nodes.
348 SpecificBumpPtrAllocator<Node> BPA;
350 /// \brief Maps function->node for fast lookup.
351 DenseMap<const Function *, Node *> NodeMap;
353 /// \brief The entry nodes to the graph.
355 /// These nodes are reachable through "external" means. Put another way, they
356 /// escape at the module scope.
357 NodeVectorT EntryNodes;
359 /// \brief Map of the entry nodes in the graph to their indices in
361 DenseMap<Function *, size_t> EntryIndexMap;
363 /// \brief Allocator that holds all the call graph SCCs.
364 SpecificBumpPtrAllocator<SCC> SCCBPA;
366 /// \brief Maps Function -> SCC for fast lookup.
367 DenseMap<Node *, SCC *> SCCMap;
369 /// \brief The leaf SCCs of the graph.
371 /// These are all of the SCCs which have no children.
372 SmallVector<SCC *, 4> LeafSCCs;
374 /// \brief Stack of nodes not-yet-processed into SCCs.
375 SmallVector<std::pair<Node *, iterator>, 4> DFSStack;
377 /// \brief Set of entry nodes not-yet-processed into SCCs.
378 SmallSetVector<Function *, 4> SCCEntryNodes;
380 /// \brief Counter for the next DFS number to assign.
383 /// \brief Helper to insert a new function, with an already looked-up entry in
385 Node &insertInto(Function &F, Node *&MappedN);
387 /// \brief Helper to update pointers back to the graph object during moves.
388 void updateGraphPtrs();
390 /// \brief Helper to form a new SCC out of the top of a DFSStack-like
392 SCC *formSCCFromDFSStack(
393 SmallVectorImpl<std::pair<Node *, Node::iterator>> &DFSStack,
394 SmallVectorImpl<std::pair<Node *, Node::iterator>>::iterator SCCBegin);
396 /// \brief Retrieve the next node in the post-order SCC walk of the call graph.
397 SCC *getNextSCCInPostOrder();
400 // Provide GraphTraits specializations for call graphs.
401 template <> struct GraphTraits<LazyCallGraph::Node *> {
402 typedef LazyCallGraph::Node NodeType;
403 typedef LazyCallGraph::iterator ChildIteratorType;
405 static NodeType *getEntryNode(NodeType *N) { return N; }
406 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
407 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
409 template <> struct GraphTraits<LazyCallGraph *> {
410 typedef LazyCallGraph::Node NodeType;
411 typedef LazyCallGraph::iterator ChildIteratorType;
413 static NodeType *getEntryNode(NodeType *N) { return N; }
414 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
415 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
418 /// \brief An analysis pass which computes the call graph for a module.
419 class LazyCallGraphAnalysis {
421 /// \brief Inform generic clients of the result type.
422 typedef LazyCallGraph Result;
424 static void *ID() { return (void *)&PassID; }
426 /// \brief Compute the \c LazyCallGraph for a the module \c M.
428 /// This just builds the set of entry points to the call graph. The rest is
429 /// built lazily as it is walked.
430 LazyCallGraph run(Module *M) { return LazyCallGraph(*M); }
436 /// \brief A pass which prints the call graph to a \c raw_ostream.
438 /// This is primarily useful for testing the analysis.
439 class LazyCallGraphPrinterPass {
443 explicit LazyCallGraphPrinterPass(raw_ostream &OS);
445 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM);
447 static StringRef name() { return "LazyCallGraphPrinterPass"; }