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