//===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// \file /// /// This file provides interfaces used to build and manipulate a call graph, /// which is a very useful tool for interprocedural optimization. /// /// Every function in a module is represented as a node in the call graph. The /// callgraph node keeps track of which functions are called by the function /// corresponding to the node. /// /// A call graph may contain nodes where the function that they correspond to /// is null. These 'external' nodes are used to represent control flow that is /// not represented (or analyzable) in the module. In particular, this /// analysis builds one external node such that: /// 1. All functions in the module without internal linkage will have edges /// from this external node, indicating that they could be called by /// functions outside of the module. /// 2. All functions whose address is used for something more than a direct /// call, for example being stored into a memory location will also have /// an edge from this external node. Since they may be called by an /// unknown caller later, they must be tracked as such. /// /// There is a second external node added for calls that leave this module. /// Functions have a call edge to the external node iff: /// 1. The function is external, reflecting the fact that they could call /// anything without internal linkage or that has its address taken. /// 2. The function contains an indirect function call. /// /// As an extension in the future, there may be multiple nodes with a null /// function. These will be used when we can prove (through pointer analysis) /// that an indirect call site can call only a specific set of functions. /// /// Because of these properties, the CallGraph captures a conservative superset /// of all of the caller-callee relationships, which is useful for /// transformations. /// //===----------------------------------------------------------------------===// #ifndef LLVM_ANALYSIS_CALLGRAPH_H #define LLVM_ANALYSIS_CALLGRAPH_H #include "llvm/IR/InstrTypes.h" #include "llvm/IR/PassManager.h" #include "llvm/IR/ValueHandle.h" #include "llvm/Pass.h" #include <cassert> #include <map> #include <memory> #include <utility> #include <vector> namespace llvm { template <class GraphType> struct GraphTraits; class CallGraphNode; class Function; class Module; class raw_ostream; /// The basic data container for the call graph of a \c Module of IR. /// /// This class exposes both the interface to the call graph for a module of IR. /// /// The core call graph itself can also be updated to reflect changes to the IR. class CallGraph { … }; /// A node in the call graph for a module. /// /// Typically represents a function in the call graph. There are also special /// "null" nodes used to represent theoretical entries in the call graph. class CallGraphNode { … }; /// An analysis pass to compute the \c CallGraph for a \c Module. /// /// This class implements the concept of an analysis pass used by the \c /// ModuleAnalysisManager to run an analysis over a module and cache the /// resulting data. class CallGraphAnalysis : public AnalysisInfoMixin<CallGraphAnalysis> { … }; /// Printer pass for the \c CallGraphAnalysis results. class CallGraphPrinterPass : public PassInfoMixin<CallGraphPrinterPass> { … }; /// Printer pass for the summarized \c CallGraphAnalysis results. class CallGraphSCCsPrinterPass : public PassInfoMixin<CallGraphSCCsPrinterPass> { … }; /// The \c ModulePass which wraps up a \c CallGraph and the logic to /// build it. /// /// This class exposes both the interface to the call graph container and the /// module pass which runs over a module of IR and produces the call graph. The /// call graph interface is entirelly a wrapper around a \c CallGraph object /// which is stored internally for each module. class CallGraphWrapperPass : public ModulePass { … }; //===----------------------------------------------------------------------===// // GraphTraits specializations for call graphs so that they can be treated as // graphs by the generic graph algorithms. // // Provide graph traits for traversing call graphs using standard graph // traversals. template <> struct GraphTraits<CallGraphNode *> { … }; template <> struct GraphTraits<const CallGraphNode *> { … }; template <> struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> { … }; template <> struct GraphTraits<const CallGraph *> : public GraphTraits< const CallGraphNode *> { … }; } // end namespace llvm #endif // LLVM_ANALYSIS_CALLGRAPH_H