LLVM API Documentation
00001 //===- LazyCallGraph.h - Analysis of a Module's call graph ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// \file 00010 /// 00011 /// Implements a lazy call graph analysis and related passes for the new pass 00012 /// manager. 00013 /// 00014 /// NB: This is *not* a traditional call graph! It is a graph which models both 00015 /// the current calls and potential calls. As a consequence there are many 00016 /// edges in this call graph that do not correspond to a 'call' or 'invoke' 00017 /// instruction. 00018 /// 00019 /// The primary use cases of this graph analysis is to facilitate iterating 00020 /// across the functions of a module in ways that ensure all callees are 00021 /// visited prior to a caller (given any SCC constraints), or vice versa. As 00022 /// such is it particularly well suited to organizing CGSCC optimizations such 00023 /// as inlining, outlining, argument promotion, etc. That is its primary use 00024 /// case and motivates the design. It may not be appropriate for other 00025 /// purposes. The use graph of functions or some other conservative analysis of 00026 /// call instructions may be interesting for optimizations and subsequent 00027 /// analyses which don't work in the context of an overly specified 00028 /// potential-call-edge graph. 00029 /// 00030 /// To understand the specific rules and nature of this call graph analysis, 00031 /// see the documentation of the \c LazyCallGraph below. 00032 /// 00033 //===----------------------------------------------------------------------===// 00034 00035 #ifndef LLVM_ANALYSIS_LAZYCALLGRAPH_H 00036 #define LLVM_ANALYSIS_LAZYCALLGRAPH_H 00037 00038 #include "llvm/ADT/DenseMap.h" 00039 #include "llvm/ADT/PointerUnion.h" 00040 #include "llvm/ADT/STLExtras.h" 00041 #include "llvm/ADT/SetVector.h" 00042 #include "llvm/ADT/SmallPtrSet.h" 00043 #include "llvm/ADT/SmallVector.h" 00044 #include "llvm/ADT/iterator.h" 00045 #include "llvm/ADT/iterator_range.h" 00046 #include "llvm/IR/BasicBlock.h" 00047 #include "llvm/IR/Function.h" 00048 #include "llvm/IR/Module.h" 00049 #include "llvm/Support/Allocator.h" 00050 #include <iterator> 00051 00052 namespace llvm { 00053 class ModuleAnalysisManager; 00054 class PreservedAnalyses; 00055 class raw_ostream; 00056 00057 /// \brief A lazily constructed view of the call graph of a module. 00058 /// 00059 /// With the edges of this graph, the motivating constraint that we are 00060 /// attempting to maintain is that function-local optimization, CGSCC-local 00061 /// optimizations, and optimizations transforming a pair of functions connected 00062 /// by an edge in the graph, do not invalidate a bottom-up traversal of the SCC 00063 /// DAG. That is, no optimizations will delete, remove, or add an edge such 00064 /// that functions already visited in a bottom-up order of the SCC DAG are no 00065 /// longer valid to have visited, or such that functions not yet visited in 00066 /// a bottom-up order of the SCC DAG are not required to have already been 00067 /// visited. 00068 /// 00069 /// Within this constraint, the desire is to minimize the merge points of the 00070 /// SCC DAG. The greater the fanout of the SCC DAG and the fewer merge points 00071 /// in the SCC DAG, the more independence there is in optimizing within it. 00072 /// There is a strong desire to enable parallelization of optimizations over 00073 /// the call graph, and both limited fanout and merge points will (artificially 00074 /// in some cases) limit the scaling of such an effort. 00075 /// 00076 /// To this end, graph represents both direct and any potential resolution to 00077 /// an indirect call edge. Another way to think about it is that it represents 00078 /// both the direct call edges and any direct call edges that might be formed 00079 /// through static optimizations. Specifically, it considers taking the address 00080 /// of a function to be an edge in the call graph because this might be 00081 /// forwarded to become a direct call by some subsequent function-local 00082 /// optimization. The result is that the graph closely follows the use-def 00083 /// edges for functions. Walking "up" the graph can be done by looking at all 00084 /// of the uses of a function. 00085 /// 00086 /// The roots of the call graph are the external functions and functions 00087 /// escaped into global variables. Those functions can be called from outside 00088 /// of the module or via unknowable means in the IR -- we may not be able to 00089 /// form even a potential call edge from a function body which may dynamically 00090 /// load the function and call it. 00091 /// 00092 /// This analysis still requires updates to remain valid after optimizations 00093 /// which could potentially change the set of potential callees. The 00094 /// constraints it operates under only make the traversal order remain valid. 00095 /// 00096 /// The entire analysis must be re-computed if full interprocedural 00097 /// optimizations run at any point. For example, globalopt completely 00098 /// invalidates the information in this analysis. 00099 /// 00100 /// FIXME: This class is named LazyCallGraph in a lame attempt to distinguish 00101 /// it from the existing CallGraph. At some point, it is expected that this 00102 /// will be the only call graph and it will be renamed accordingly. 00103 class LazyCallGraph { 00104 public: 00105 class Node; 00106 class SCC; 00107 typedef SmallVector<PointerUnion<Function *, Node *>, 4> NodeVectorT; 00108 typedef SmallVectorImpl<PointerUnion<Function *, Node *>> NodeVectorImplT; 00109 00110 /// \brief A lazy iterator used for both the entry nodes and child nodes. 00111 /// 00112 /// When this iterator is dereferenced, if not yet available, a function will 00113 /// be scanned for "calls" or uses of functions and its child information 00114 /// will be constructed. All of these results are accumulated and cached in 00115 /// the graph. 00116 class iterator 00117 : public iterator_adaptor_base<iterator, NodeVectorImplT::iterator, 00118 std::forward_iterator_tag, Node> { 00119 friend class LazyCallGraph; 00120 friend class LazyCallGraph::Node; 00121 00122 LazyCallGraph *G; 00123 NodeVectorImplT::iterator E; 00124 00125 // Build the iterator for a specific position in a node list. 00126 iterator(LazyCallGraph &G, NodeVectorImplT::iterator NI, 00127 NodeVectorImplT::iterator E) 00128 : iterator_adaptor_base(NI), G(&G), E(E) { 00129 while (I != E && I->isNull()) 00130 ++I; 00131 } 00132 00133 public: 00134 iterator() {} 00135 00136 using iterator_adaptor_base::operator++; 00137 iterator &operator++() { 00138 do { 00139 ++I; 00140 } while (I != E && I->isNull()); 00141 return *this; 00142 } 00143 00144 reference operator*() const { 00145 if (I->is<Node *>()) 00146 return *I->get<Node *>(); 00147 00148 Function *F = I->get<Function *>(); 00149 Node &ChildN = G->get(*F); 00150 *I = &ChildN; 00151 return ChildN; 00152 } 00153 }; 00154 00155 /// \brief A node in the call graph. 00156 /// 00157 /// This represents a single node. It's primary roles are to cache the list of 00158 /// callees, de-duplicate and provide fast testing of whether a function is 00159 /// a callee, and facilitate iteration of child nodes in the graph. 00160 class Node { 00161 friend class LazyCallGraph; 00162 friend class LazyCallGraph::SCC; 00163 00164 LazyCallGraph *G; 00165 Function &F; 00166 00167 // We provide for the DFS numbering and Tarjan walk lowlink numbers to be 00168 // stored directly within the node. 00169 int DFSNumber; 00170 int LowLink; 00171 00172 mutable NodeVectorT Callees; 00173 DenseMap<Function *, size_t> CalleeIndexMap; 00174 00175 /// \brief Basic constructor implements the scanning of F into Callees and 00176 /// CalleeIndexMap. 00177 Node(LazyCallGraph &G, Function &F); 00178 00179 /// \brief Internal helper to insert a callee. 00180 void insertEdgeInternal(Function &Callee); 00181 00182 /// \brief Internal helper to insert a callee. 00183 void insertEdgeInternal(Node &CalleeN); 00184 00185 /// \brief Internal helper to remove a callee from this node. 00186 void removeEdgeInternal(Function &Callee); 00187 00188 public: 00189 typedef LazyCallGraph::iterator iterator; 00190 00191 Function &getFunction() const { 00192 return F; 00193 }; 00194 00195 iterator begin() const { 00196 return iterator(*G, Callees.begin(), Callees.end()); 00197 } 00198 iterator end() const { return iterator(*G, Callees.end(), Callees.end()); } 00199 00200 /// Equality is defined as address equality. 00201 bool operator==(const Node &N) const { return this == &N; } 00202 bool operator!=(const Node &N) const { return !operator==(N); } 00203 }; 00204 00205 /// \brief An SCC of the call graph. 00206 /// 00207 /// This represents a Strongly Connected Component of the call graph as 00208 /// a collection of call graph nodes. While the order of nodes in the SCC is 00209 /// stable, it is not any particular order. 00210 class SCC { 00211 friend class LazyCallGraph; 00212 friend class LazyCallGraph::Node; 00213 00214 LazyCallGraph *G; 00215 SmallPtrSet<SCC *, 1> ParentSCCs; 00216 SmallVector<Node *, 1> Nodes; 00217 00218 SCC(LazyCallGraph &G) : G(&G) {} 00219 00220 void insert(Node &N); 00221 00222 void 00223 internalDFS(SmallVectorImpl<std::pair<Node *, Node::iterator>> &DFSStack, 00224 SmallVectorImpl<Node *> &PendingSCCStack, Node *N, 00225 SmallVectorImpl<SCC *> &ResultSCCs); 00226 00227 public: 00228 typedef SmallVectorImpl<Node *>::const_iterator iterator; 00229 typedef pointee_iterator<SmallPtrSet<SCC *, 1>::const_iterator> parent_iterator; 00230 00231 iterator begin() const { return Nodes.begin(); } 00232 iterator end() const { return Nodes.end(); } 00233 00234 parent_iterator parent_begin() const { return ParentSCCs.begin(); } 00235 parent_iterator parent_end() const { return ParentSCCs.end(); } 00236 00237 iterator_range<parent_iterator> parents() const { 00238 return iterator_range<parent_iterator>(parent_begin(), parent_end()); 00239 } 00240 00241 /// \brief Test if this SCC is a parent of \a C. 00242 bool isParentOf(const SCC &C) const { return C.isChildOf(*this); } 00243 00244 /// \brief Test if this SCC is an ancestor of \a C. 00245 bool isAncestorOf(const SCC &C) const { return C.isDescendantOf(*this); } 00246 00247 /// \brief Test if this SCC is a child of \a C. 00248 bool isChildOf(const SCC &C) const { 00249 return ParentSCCs.count(const_cast<SCC *>(&C)); 00250 } 00251 00252 /// \brief Test if this SCC is a descendant of \a C. 00253 bool isDescendantOf(const SCC &C) const; 00254 00255 ///@{ 00256 /// \name Mutation API 00257 /// 00258 /// These methods provide the core API for updating the call graph in the 00259 /// presence of a (potentially still in-flight) DFS-found SCCs. 00260 /// 00261 /// Note that these methods sometimes have complex runtimes, so be careful 00262 /// how you call them. 00263 00264 /// \brief Insert an edge from one node in this SCC to another in this SCC. 00265 /// 00266 /// By the definition of an SCC, this does not change the nature or make-up 00267 /// of any SCCs. 00268 void insertIntraSCCEdge(Node &CallerN, Node &CalleeN); 00269 00270 /// \brief Insert an edge whose tail is in this SCC and head is in some 00271 /// child SCC. 00272 /// 00273 /// There must be an existing path from the caller to the callee. This 00274 /// operation is inexpensive and does not change the set of SCCs in the 00275 /// graph. 00276 void insertOutgoingEdge(Node &CallerN, Node &CalleeN); 00277 00278 /// \brief Insert an edge whose tail is in a descendant SCC and head is in 00279 /// this SCC. 00280 /// 00281 /// There must be an existing path from the callee to the caller in this 00282 /// case. NB! This is has the potential to be a very expensive function. It 00283 /// inherently forms a cycle in the prior SCC DAG and we have to merge SCCs 00284 /// to resolve that cycle. But finding all of the SCCs which participate in 00285 /// the cycle can in the worst case require traversing every SCC in the 00286 /// graph. Every attempt is made to avoid that, but passes must still 00287 /// exercise caution calling this routine repeatedly. 00288 /// 00289 /// FIXME: We could possibly optimize this quite a bit for cases where the 00290 /// caller and callee are very nearby in the graph. See comments in the 00291 /// implementation for details, but that use case might impact users. 00292 SmallVector<SCC *, 1> insertIncomingEdge(Node &CallerN, Node &CalleeN); 00293 00294 /// \brief Remove an edge whose source is in this SCC and target is *not*. 00295 /// 00296 /// This removes an inter-SCC edge. All inter-SCC edges originating from 00297 /// this SCC have been fully explored by any in-flight DFS SCC formation, 00298 /// so this is always safe to call once you have the source SCC. 00299 /// 00300 /// This operation does not change the set of SCCs or the members of the 00301 /// SCCs and so is very inexpensive. It may change the connectivity graph 00302 /// of the SCCs though, so be careful calling this while iterating over 00303 /// them. 00304 void removeInterSCCEdge(Node &CallerN, Node &CalleeN); 00305 00306 /// \brief Remove an edge which is entirely within this SCC. 00307 /// 00308 /// Both the \a Caller and the \a Callee must be within this SCC. Removing 00309 /// such an edge make break cycles that form this SCC and thus this 00310 /// operation may change the SCC graph significantly. In particular, this 00311 /// operation will re-form new SCCs based on the remaining connectivity of 00312 /// the graph. The following invariants are guaranteed to hold after 00313 /// calling this method: 00314 /// 00315 /// 1) This SCC is still an SCC in the graph. 00316 /// 2) This SCC will be the parent of any new SCCs. Thus, this SCC is 00317 /// preserved as the root of any new SCC directed graph formed. 00318 /// 3) No SCC other than this SCC has its member set changed (this is 00319 /// inherent in the definition of removing such an edge). 00320 /// 4) All of the parent links of the SCC graph will be updated to reflect 00321 /// the new SCC structure. 00322 /// 5) All SCCs formed out of this SCC, excluding this SCC, will be 00323 /// returned in a vector. 00324 /// 6) The order of the SCCs in the vector will be a valid postorder 00325 /// traversal of the new SCCs. 00326 /// 00327 /// These invariants are very important to ensure that we can build 00328 /// optimization pipeliens on top of the CGSCC pass manager which 00329 /// intelligently update the SCC graph without invalidating other parts of 00330 /// the SCC graph. 00331 /// 00332 /// The runtime complexity of this method is, in the worst case, O(V+E) 00333 /// where V is the number of nodes in this SCC and E is the number of edges 00334 /// leaving the nodes in this SCC. Note that E includes both edges within 00335 /// this SCC and edges from this SCC to child SCCs. Some effort has been 00336 /// made to minimize the overhead of common cases such as self-edges and 00337 /// edge removals which result in a spanning tree with no more cycles. 00338 SmallVector<SCC *, 1> removeIntraSCCEdge(Node &CallerN, Node &CalleeN); 00339 00340 ///@} 00341 }; 00342 00343 /// \brief A post-order depth-first SCC iterator over the call graph. 00344 /// 00345 /// This iterator triggers the Tarjan DFS-based formation of the SCC DAG for 00346 /// the call graph, walking it lazily in depth-first post-order. That is, it 00347 /// always visits SCCs for a callee prior to visiting the SCC for a caller 00348 /// (when they are in different SCCs). 00349 class postorder_scc_iterator 00350 : public iterator_facade_base<postorder_scc_iterator, 00351 std::forward_iterator_tag, SCC> { 00352 friend class LazyCallGraph; 00353 friend class LazyCallGraph::Node; 00354 00355 /// \brief Nonce type to select the constructor for the end iterator. 00356 struct IsAtEndT {}; 00357 00358 LazyCallGraph *G; 00359 SCC *C; 00360 00361 // Build the begin iterator for a node. 00362 postorder_scc_iterator(LazyCallGraph &G) : G(&G) { 00363 C = G.getNextSCCInPostOrder(); 00364 } 00365 00366 // Build the end iterator for a node. This is selected purely by overload. 00367 postorder_scc_iterator(LazyCallGraph &G, IsAtEndT /*Nonce*/) 00368 : G(&G), C(nullptr) {} 00369 00370 public: 00371 bool operator==(const postorder_scc_iterator &Arg) const { 00372 return G == Arg.G && C == Arg.C; 00373 } 00374 00375 reference operator*() const { return *C; } 00376 00377 using iterator_facade_base::operator++; 00378 postorder_scc_iterator &operator++() { 00379 C = G->getNextSCCInPostOrder(); 00380 return *this; 00381 } 00382 }; 00383 00384 /// \brief Construct a graph for the given module. 00385 /// 00386 /// This sets up the graph and computes all of the entry points of the graph. 00387 /// No function definitions are scanned until their nodes in the graph are 00388 /// requested during traversal. 00389 LazyCallGraph(Module &M); 00390 00391 LazyCallGraph(LazyCallGraph &&G); 00392 LazyCallGraph &operator=(LazyCallGraph &&RHS); 00393 00394 iterator begin() { 00395 return iterator(*this, EntryNodes.begin(), EntryNodes.end()); 00396 } 00397 iterator end() { return iterator(*this, EntryNodes.end(), EntryNodes.end()); } 00398 00399 postorder_scc_iterator postorder_scc_begin() { 00400 return postorder_scc_iterator(*this); 00401 } 00402 postorder_scc_iterator postorder_scc_end() { 00403 return postorder_scc_iterator(*this, postorder_scc_iterator::IsAtEndT()); 00404 } 00405 00406 iterator_range<postorder_scc_iterator> postorder_sccs() { 00407 return iterator_range<postorder_scc_iterator>(postorder_scc_begin(), 00408 postorder_scc_end()); 00409 } 00410 00411 /// \brief Lookup a function in the graph which has already been scanned and 00412 /// added. 00413 Node *lookup(const Function &F) const { return NodeMap.lookup(&F); } 00414 00415 /// \brief Lookup a function's SCC in the graph. 00416 /// 00417 /// \returns null if the function hasn't been assigned an SCC via the SCC 00418 /// iterator walk. 00419 SCC *lookupSCC(Node &N) const { return SCCMap.lookup(&N); } 00420 00421 /// \brief Get a graph node for a given function, scanning it to populate the 00422 /// graph data as necessary. 00423 Node &get(Function &F) { 00424 Node *&N = NodeMap[&F]; 00425 if (N) 00426 return *N; 00427 00428 return insertInto(F, N); 00429 } 00430 00431 ///@{ 00432 /// \name Pre-SCC Mutation API 00433 /// 00434 /// These methods are only valid to call prior to forming any SCCs for this 00435 /// call graph. They can be used to update the core node-graph during 00436 /// a node-based inorder traversal that precedes any SCC-based traversal. 00437 /// 00438 /// Once you begin manipulating a call graph's SCCs, you must perform all 00439 /// mutation of the graph via the SCC methods. 00440 00441 /// \brief Update the call graph after inserting a new edge. 00442 void insertEdge(Node &Caller, Function &Callee); 00443 00444 /// \brief Update the call graph after inserting a new edge. 00445 void insertEdge(Function &Caller, Function &Callee) { 00446 return insertEdge(get(Caller), Callee); 00447 } 00448 00449 /// \brief Update the call graph after deleting an edge. 00450 void removeEdge(Node &Caller, Function &Callee); 00451 00452 /// \brief Update the call graph after deleting an edge. 00453 void removeEdge(Function &Caller, Function &Callee) { 00454 return removeEdge(get(Caller), Callee); 00455 } 00456 00457 ///@} 00458 00459 private: 00460 /// \brief Allocator that holds all the call graph nodes. 00461 SpecificBumpPtrAllocator<Node> BPA; 00462 00463 /// \brief Maps function->node for fast lookup. 00464 DenseMap<const Function *, Node *> NodeMap; 00465 00466 /// \brief The entry nodes to the graph. 00467 /// 00468 /// These nodes are reachable through "external" means. Put another way, they 00469 /// escape at the module scope. 00470 NodeVectorT EntryNodes; 00471 00472 /// \brief Map of the entry nodes in the graph to their indices in 00473 /// \c EntryNodes. 00474 DenseMap<Function *, size_t> EntryIndexMap; 00475 00476 /// \brief Allocator that holds all the call graph SCCs. 00477 SpecificBumpPtrAllocator<SCC> SCCBPA; 00478 00479 /// \brief Maps Function -> SCC for fast lookup. 00480 DenseMap<Node *, SCC *> SCCMap; 00481 00482 /// \brief The leaf SCCs of the graph. 00483 /// 00484 /// These are all of the SCCs which have no children. 00485 SmallVector<SCC *, 4> LeafSCCs; 00486 00487 /// \brief Stack of nodes in the DFS walk. 00488 SmallVector<std::pair<Node *, iterator>, 4> DFSStack; 00489 00490 /// \brief Set of entry nodes not-yet-processed into SCCs. 00491 SmallVector<Function *, 4> SCCEntryNodes; 00492 00493 /// \brief Stack of nodes the DFS has walked but not yet put into a SCC. 00494 SmallVector<Node *, 4> PendingSCCStack; 00495 00496 /// \brief Counter for the next DFS number to assign. 00497 int NextDFSNumber; 00498 00499 /// \brief Helper to insert a new function, with an already looked-up entry in 00500 /// the NodeMap. 00501 Node &insertInto(Function &F, Node *&MappedN); 00502 00503 /// \brief Helper to update pointers back to the graph object during moves. 00504 void updateGraphPtrs(); 00505 00506 /// \brief Helper to form a new SCC out of the top of a DFSStack-like 00507 /// structure. 00508 SCC *formSCC(Node *RootN, SmallVectorImpl<Node *> &NodeStack); 00509 00510 /// \brief Retrieve the next node in the post-order SCC walk of the call graph. 00511 SCC *getNextSCCInPostOrder(); 00512 }; 00513 00514 // Provide GraphTraits specializations for call graphs. 00515 template <> struct GraphTraits<LazyCallGraph::Node *> { 00516 typedef LazyCallGraph::Node NodeType; 00517 typedef LazyCallGraph::iterator ChildIteratorType; 00518 00519 static NodeType *getEntryNode(NodeType *N) { return N; } 00520 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); } 00521 static ChildIteratorType child_end(NodeType *N) { return N->end(); } 00522 }; 00523 template <> struct GraphTraits<LazyCallGraph *> { 00524 typedef LazyCallGraph::Node NodeType; 00525 typedef LazyCallGraph::iterator ChildIteratorType; 00526 00527 static NodeType *getEntryNode(NodeType *N) { return N; } 00528 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); } 00529 static ChildIteratorType child_end(NodeType *N) { return N->end(); } 00530 }; 00531 00532 /// \brief An analysis pass which computes the call graph for a module. 00533 class LazyCallGraphAnalysis { 00534 public: 00535 /// \brief Inform generic clients of the result type. 00536 typedef LazyCallGraph Result; 00537 00538 static void *ID() { return (void *)&PassID; } 00539 00540 /// \brief Compute the \c LazyCallGraph for the module \c M. 00541 /// 00542 /// This just builds the set of entry points to the call graph. The rest is 00543 /// built lazily as it is walked. 00544 LazyCallGraph run(Module *M) { return LazyCallGraph(*M); } 00545 00546 private: 00547 static char PassID; 00548 }; 00549 00550 /// \brief A pass which prints the call graph to a \c raw_ostream. 00551 /// 00552 /// This is primarily useful for testing the analysis. 00553 class LazyCallGraphPrinterPass { 00554 raw_ostream &OS; 00555 00556 public: 00557 explicit LazyCallGraphPrinterPass(raw_ostream &OS); 00558 00559 PreservedAnalyses run(Module *M, ModuleAnalysisManager *AM); 00560 00561 static StringRef name() { return "LazyCallGraphPrinterPass"; } 00562 }; 00563 00564 } 00565 00566 #endif