1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//                     The LLVM Compiler Infrastructure
4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source
6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details.
7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \file
10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This file provides interfaces used to build and manipulate a call graph,
12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// which is a very useful tool for interprocedural optimization.
13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Every function in a module is represented as a node in the call graph.  The
15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// callgraph node keeps track of which functions are called by the function
16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// corresponding to the node.
17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// A call graph may contain nodes where the function that they correspond to
19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// is null.  These 'external' nodes are used to represent control flow that is
20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// not represented (or analyzable) in the module.  In particular, this
21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// analysis builds one external node such that:
22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///   1. All functions in the module without internal linkage will have edges
23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      from this external node, indicating that they could be called by
24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      functions outside of the module.
25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///   2. All functions whose address is used for something more than a direct
26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      call, for example being stored into a memory location will also have
27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      an edge from this external node.  Since they may be called by an
28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      unknown caller later, they must be tracked as such.
29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// There is a second external node added for calls that leave this module.
31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Functions have a call edge to the external node iff:
32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///   1. The function is external, reflecting the fact that they could call
33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///      anything without internal linkage or that has its address taken.
34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///   2. The function contains an indirect function call.
35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// As an extension in the future, there may be multiple nodes with a null
37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// function.  These will be used when we can prove (through pointer analysis)
38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// that an indirect call site can call only a specific set of functions.
39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Because of these properties, the CallGraph captures a conservative superset
41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// of all of the caller-callee relationships, which is useful for
42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// transformations.
43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_ANALYSIS_CALLGRAPH_H
47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_ANALYSIS_CALLGRAPH_H
48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/GraphTraits.h"
50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/STLExtras.h"
51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/CallSite.h"
52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/Function.h"
53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/Intrinsics.h"
54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/PassManager.h"
55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/ValueHandle.h"
56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Pass.h"
57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <cassert>
58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <map>
59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <memory>
60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <utility>
61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <vector>
62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm {
64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphNode;
66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass Module;
67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass raw_ostream;
68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief The basic data container for the call graph of a \c Module of IR.
70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This class exposes both the interface to the call graph for a module of IR.
72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// The core call graph itself can also be updated to reflect changes to the IR.
74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraph {
75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Module &M;
76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using FunctionMapTy =
78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      std::map<const Function *, std::unique_ptr<CallGraphNode>>;
79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief A map from \c Function* to \c CallGraphNode*.
81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  FunctionMapTy FunctionMap;
82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief This node has edges to all external functions and those internal
84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// functions that have their address taken.
85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *ExternalCallingNode;
86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief This node has edges to it from all functions making indirect calls
88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// or calling an external function.
89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::unique_ptr<CallGraphNode> CallsExternalNode;
90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Replace the function represented by this node by another.
92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// This does not rescan the body of the function, so it is suitable when
94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// splicing the body of one function to another while also updating all
95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// callers from the old function to the new.
96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void spliceFunction(const Function *From, const Function *To);
97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Add a function to the call graph, and link the node to all of the
99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// functions that it calls.
100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void addToCallGraph(Function *F);
101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  explicit CallGraph(Module &M);
104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraph(CallGraph &&Arg);
105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ~CallGraph();
106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void print(raw_ostream &OS) const;
108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void dump() const;
109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using iterator = FunctionMapTy::iterator;
111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using const_iterator = FunctionMapTy::const_iterator;
112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the module the call graph corresponds to.
114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Module &getModule() const { return M; }
115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator begin() { return FunctionMap.begin(); }
117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator end() { return FunctionMap.end(); }
118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator begin() const { return FunctionMap.begin(); }
119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator end() const { return FunctionMap.end(); }
120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the call graph node for the provided function.
122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const CallGraphNode *operator[](const Function *F) const {
123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    const_iterator I = FunctionMap.find(F);
124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(I != FunctionMap.end() && "Function not in callgraph!");
125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return I->second.get();
126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the call graph node for the provided function.
129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline CallGraphNode *operator[](const Function *F) {
130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    const_iterator I = FunctionMap.find(F);
131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(I != FunctionMap.end() && "Function not in callgraph!");
132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return I->second.get();
133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
134c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
135c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the \c CallGraphNode which is used to represent
136c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// undetermined calls into the callgraph.
137c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
138c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
139c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getCallsExternalNode() const {
140c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return CallsExternalNode.get();
141c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
142c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
143c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===---------------------------------------------------------------------
144c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Functions to keep a call graph up to date with a function that has been
145c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // modified.
146c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //
147c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
148c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Unlink the function from this module, returning it.
149c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
150c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Because this removes the function from the module, the call graph node is
151c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// destroyed.  This is only valid if the function does not call any other
152c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// functions (ie, there are no edges in it's CGN).  The easiest way to do
153c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// this is to dropAllReferences before calling this.
154c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Function *removeFunctionFromModule(CallGraphNode *CGN);
155c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
156c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Similar to operator[], but this will insert a new CallGraphNode for
157c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \c F if one does not already exist.
158c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getOrInsertFunction(const Function *F);
159c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
160c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
161c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief A node in the call graph for a module.
162c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
163c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Typically represents a function in the call graph. There are also special
164c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// "null" nodes used to represent theoretical entries in the call graph.
165c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphNode {
166c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
167c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief A pair of the calling instruction (a call or invoke)
168c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// and the call graph node being called.
169c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using CallRecord = std::pair<WeakTrackingVH, CallGraphNode *>;
170c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
171c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
172c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using CalledFunctionsVector = std::vector<CallRecord>;
173c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
174c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Creates a node for the specified function.
175c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline CallGraphNode(Function *F) : F(F) {}
176c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
177c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode(const CallGraphNode &) = delete;
178c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode &operator=(const CallGraphNode &) = delete;
179c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
180c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ~CallGraphNode() {
181c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(NumReferences == 0 && "Node deleted while references remain");
182c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
183c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
184c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using iterator = std::vector<CallRecord>::iterator;
185c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using const_iterator = std::vector<CallRecord>::const_iterator;
186c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
187c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the function that this call graph node represents.
188c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Function *getFunction() const { return F; }
189c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
190c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator begin() { return CalledFunctions.begin(); }
191c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator end() { return CalledFunctions.end(); }
192c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator begin() const { return CalledFunctions.begin(); }
193c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator end() const { return CalledFunctions.end(); }
194c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline bool empty() const { return CalledFunctions.empty(); }
195c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
196c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
197c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the number of other CallGraphNodes in this CallGraph that
198c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// reference this node in their callee list.
199c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getNumReferences() const { return NumReferences; }
200c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
201c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the i'th called function.
202c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *operator[](unsigned i) const {
203c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(i < CalledFunctions.size() && "Invalid index");
204c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return CalledFunctions[i].second;
205c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
206c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
207c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Print out this call graph node.
208c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void dump() const;
209c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void print(raw_ostream &OS) const;
210c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
211c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===---------------------------------------------------------------------
212c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Methods to keep a call graph up to date with a function that has been
213c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // modified
214c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //
215c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
216c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Removes all edges from this CallGraphNode to any functions it
217c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// calls.
218c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void removeAllCalledFunctions() {
219c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    while (!CalledFunctions.empty()) {
220c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      CalledFunctions.back().second->DropRef();
221c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      CalledFunctions.pop_back();
222c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    }
223c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
224c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
225c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Moves all the callee information from N to this node.
226c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void stealCalledFunctionsFrom(CallGraphNode *N) {
227c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(CalledFunctions.empty() &&
228c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot           "Cannot steal callsite information if I already have some");
229c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    std::swap(CalledFunctions, N->CalledFunctions);
230c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
231c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
232c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Adds a function to the list of functions called by this one.
233c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void addCalledFunction(CallSite CS, CallGraphNode *M) {
234c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(!CS.getInstruction() || !CS.getCalledFunction() ||
235c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot           !CS.getCalledFunction()->isIntrinsic() ||
236c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot           !Intrinsic::isLeaf(CS.getCalledFunction()->getIntrinsicID()));
237c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    CalledFunctions.emplace_back(CS.getInstruction(), M);
238c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    M->AddRef();
239c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
240c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
241c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void removeCallEdge(iterator I) {
242c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    I->second->DropRef();
243c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    *I = CalledFunctions.back();
244c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    CalledFunctions.pop_back();
245c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
246c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
247c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Removes the edge in the node for the specified call site.
248c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
249c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Note that this method takes linear time, so it should be used sparingly.
250c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void removeCallEdgeFor(CallSite CS);
251c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
252c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Removes all call edges from this node to the specified callee
253c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// function.
254c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
255c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// This takes more time to execute than removeCallEdgeTo, so it should not
256c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// be used unless necessary.
257c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void removeAnyCallEdgeTo(CallGraphNode *Callee);
258c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
259c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Removes one edge associated with a null callsite from this node to
260c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the specified callee function.
261c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void removeOneAbstractEdgeTo(CallGraphNode *Callee);
262c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
263c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Replaces the edge in the node for the specified call site with a
264c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// new one.
265c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
266c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Note that this method takes linear time, so it should be used sparingly.
267c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
268c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
269c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate:
270c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  friend class CallGraph;
271c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
272c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Function *F;
273c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
274c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::vector<CallRecord> CalledFunctions;
275c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
276c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief The number of times that this CallGraphNode occurs in the
277c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// CalledFunctions array of this or other CallGraphNodes.
278c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned NumReferences = 0;
279c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
280c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void DropRef() { --NumReferences; }
281c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void AddRef() { ++NumReferences; }
282c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
283c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief A special function that should only be used by the CallGraph class.
284c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void allReferencesDropped() { NumReferences = 0; }
285c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
286c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
287c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief An analysis pass to compute the \c CallGraph for a \c Module.
288c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
289c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This class implements the concept of an analysis pass used by the \c
290c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// ModuleAnalysisManager to run an analysis over a module and cache the
291c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// resulting data.
292c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphAnalysis : public AnalysisInfoMixin<CallGraphAnalysis> {
293c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  friend AnalysisInfoMixin<CallGraphAnalysis>;
294c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
295c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static AnalysisKey Key;
296c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
297c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
298c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief A formulaic type to inform clients of the result type.
299c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using Result = CallGraph;
300c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
301c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Compute the \c CallGraph for the module \c M.
302c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
303c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// The real work here is done in the \c CallGraph constructor.
304c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraph run(Module &M, ModuleAnalysisManager &) { return CallGraph(M); }
305c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
306c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
307c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief Printer pass for the \c CallGraphAnalysis results.
308c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphPrinterPass : public PassInfoMixin<CallGraphPrinterPass> {
309c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  raw_ostream &OS;
310c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
311c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
312c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  explicit CallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
313c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
314c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
315c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
316c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
317c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief The \c ModulePass which wraps up a \c CallGraph and the logic to
318c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// build it.
319c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot///
320c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This class exposes both the interface to the call graph container and the
321c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// module pass which runs over a module of IR and produces the call graph. The
322c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// call graph interface is entirelly a wrapper around a \c CallGraph object
323c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// which is stored internally for each module.
324c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass CallGraphWrapperPass : public ModulePass {
325c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::unique_ptr<CallGraph> G;
326c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
327c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
328c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static char ID; // Class identification, replacement for typeinfo
329c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
330c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphWrapperPass();
331c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ~CallGraphWrapperPass() override;
332c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
333c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief The internal \c CallGraph around which the rest of this interface
334c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// is wrapped.
335c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const CallGraph &getCallGraph() const { return *G; }
336c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraph &getCallGraph() { return *G; }
337c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
338c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using iterator = CallGraph::iterator;
339c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using const_iterator = CallGraph::const_iterator;
340c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
341c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the module the call graph corresponds to.
342c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Module &getModule() const { return G->getModule(); }
343c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
344c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator begin() { return G->begin(); }
345c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline iterator end() { return G->end(); }
346c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator begin() const { return G->begin(); }
347c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const_iterator end() const { return G->end(); }
348c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
349c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the call graph node for the provided function.
350c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline const CallGraphNode *operator[](const Function *F) const {
351c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return (*G)[F];
352c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
353c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
354c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the call graph node for the provided function.
355c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
356c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
357c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Returns the \c CallGraphNode which is used to represent
358c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// undetermined calls into the callgraph.
359c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getExternalCallingNode() const {
360c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return G->getExternalCallingNode();
361c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
362c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
363c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getCallsExternalNode() const {
364c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return G->getCallsExternalNode();
365c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
366c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
367c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===---------------------------------------------------------------------
368c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Functions to keep a call graph up to date with a function that has been
369c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // modified.
370c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //
371c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
372c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Unlink the function from this module, returning it.
373c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
374c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Because this removes the function from the module, the call graph node is
375c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// destroyed.  This is only valid if the function does not call any other
376c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// functions (ie, there are no edges in it's CGN).  The easiest way to do
377c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// this is to dropAllReferences before calling this.
378c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  Function *removeFunctionFromModule(CallGraphNode *CGN) {
379c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return G->removeFunctionFromModule(CGN);
380c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
381c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
382c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \brief Similar to operator[], but this will insert a new CallGraphNode for
383c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// \c F if one does not already exist.
384c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  CallGraphNode *getOrInsertFunction(const Function *F) {
385c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return G->getOrInsertFunction(F);
386c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
387c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
388c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //===---------------------------------------------------------------------
389c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Implementation of the ModulePass interface needed here.
390c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  //
391c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
392c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void getAnalysisUsage(AnalysisUsage &AU) const override;
393c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool runOnModule(Module &M) override;
394c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void releaseMemory() override;
395c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
396c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void print(raw_ostream &o, const Module *) const override;
397c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void dump() const;
398c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
399c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
400c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
401c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// GraphTraits specializations for call graphs so that they can be treated as
402c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// graphs by the generic graph algorithms.
403c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
404c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
405c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// Provide graph traits for tranversing call graphs using standard graph
406c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// traversals.
407c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <> struct GraphTraits<CallGraphNode *> {
408c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using NodeRef = CallGraphNode *;
409c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using CGNPairTy = CallGraphNode::CallRecord;
410c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
411c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static NodeRef getEntryNode(CallGraphNode *CGN) { return CGN; }
412c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
413c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
414c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using ChildIteratorType =
415c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      mapped_iterator<CallGraphNode::iterator, decltype(&CGNGetValue)>;
416c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
417c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static ChildIteratorType child_begin(NodeRef N) {
418c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return ChildIteratorType(N->begin(), &CGNGetValue);
419c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
420c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
421c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static ChildIteratorType child_end(NodeRef N) {
422c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return ChildIteratorType(N->end(), &CGNGetValue);
423c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
424c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
425c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
426c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <> struct GraphTraits<const CallGraphNode *> {
427c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using NodeRef = const CallGraphNode *;
428c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using CGNPairTy = CallGraphNode::CallRecord;
429c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
430c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static NodeRef getEntryNode(const CallGraphNode *CGN) { return CGN; }
431c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static const CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
432c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
433c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using ChildIteratorType =
434c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      mapped_iterator<CallGraphNode::const_iterator, decltype(&CGNGetValue)>;
435c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
436c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static ChildIteratorType child_begin(NodeRef N) {
437c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return ChildIteratorType(N->begin(), &CGNGetValue);
438c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
439c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
440c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static ChildIteratorType child_end(NodeRef N) {
441c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return ChildIteratorType(N->end(), &CGNGetValue);
442c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
443c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
444c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
445c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <>
446c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstruct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
447c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using PairTy =
448c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
449c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
450c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static NodeRef getEntryNode(CallGraph *CGN) {
451c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return CGN->getExternalCallingNode(); // Start at the external node!
452c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
453c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
454c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static CallGraphNode *CGGetValuePtr(const PairTy &P) {
455c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return P.second.get();
456c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
457c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
458c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
459c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using nodes_iterator =
460c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      mapped_iterator<CallGraph::iterator, decltype(&CGGetValuePtr)>;
461c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
462c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static nodes_iterator nodes_begin(CallGraph *CG) {
463c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return nodes_iterator(CG->begin(), &CGGetValuePtr);
464c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
465c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
466c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static nodes_iterator nodes_end(CallGraph *CG) {
467c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return nodes_iterator(CG->end(), &CGGetValuePtr);
468c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
469c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
470c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
471c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <>
472c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstruct GraphTraits<const CallGraph *> : public GraphTraits<
473c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                            const CallGraphNode *> {
474c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using PairTy =
475c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
476c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
477c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static NodeRef getEntryNode(const CallGraph *CGN) {
478c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return CGN->getExternalCallingNode(); // Start at the external node!
479c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
480c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
481c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static const CallGraphNode *CGGetValuePtr(const PairTy &P) {
482c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return P.second.get();
483c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
484c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
485c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
486c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  using nodes_iterator =
487c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      mapped_iterator<CallGraph::const_iterator, decltype(&CGGetValuePtr)>;
488c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
489c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static nodes_iterator nodes_begin(const CallGraph *CG) {
490c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return nodes_iterator(CG->begin(), &CGGetValuePtr);
491c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
492c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
493c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  static nodes_iterator nodes_end(const CallGraph *CG) {
494c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return nodes_iterator(CG->end(), &CGGetValuePtr);
495c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
496c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
497c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
498c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm
499c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
500c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_ANALYSIS_CALLGRAPH_H
501