LLVM: lib/Analysis/CallGraph.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
13#include "llvm/Config/llvm-config.h"
23#include
24
25using namespace llvm;
26
27
28
29
30
38
40 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)),
41 ExternalCallingNode(Arg.ExternalCallingNode),
42 CallsExternalNode(std::move(Arg.CallsExternalNode)) {
43 Arg.FunctionMap.clear();
44 Arg.ExternalCallingNode = nullptr;
45
46
47 CallsExternalNode->CG = this;
48 for (auto &P : FunctionMap)
49 P.second->CG = this;
50}
51
53
54 if (CallsExternalNode)
55 CallsExternalNode->allReferencesDropped();
56
57
58
59#ifndef NDEBUG
60 for (auto &I : FunctionMap)
61 I.second->allReferencesDropped();
62#endif
63}
64
66 ModuleAnalysisManager::Invalidator &) {
67
68
71}
72
75
76
77
78 if (->hasLocalLinkage() ||
79 F->hasAddressTaken(nullptr, true,
80 true,
81 false))
82 ExternalCallingNode->addCalledFunction(nullptr, Node);
83
85}
86
89
90
91
92 if (F->isDeclaration() && ->hasFnAttribute(Attribute::NoCallback))
93 Node->addCalledFunction(nullptr, CallsExternalNode.get());
94
95
99 const Function *Callee = Call->getCalledFunction();
100 if (!Callee)
101 Node->addCalledFunction(Call, CallsExternalNode.get());
102 else
104
105
108 });
109 }
110 }
111}
112
114
115
116
118 Nodes.reserve(FunctionMap.size());
119
120 for (const auto &I : *this)
122
126 return LF->getName() < RF->getName();
127
128 return RHS->getFunction() != nullptr;
129 });
130
132 CN->print(OS);
133}
134
135#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
137#endif
138
139
140
141
142
143
144
146 assert(CGN->empty() && "Cannot remove function from call "
147 "graph if it references other functions!");
148 Function *F = CGN->getFunction();
150
151 M.getFunctionList().remove(F);
152 return F;
153}
154
155
156
157
159 auto &CGN = FunctionMap[F];
160 if (CGN)
161 return CGN.get();
162
163 assert(( || F->getParent() == &M) && "Function not in current module!");
164 CGN = std::make_unique(this, const_cast<Function *>(F));
165 return CGN.get();
166}
167
168
169
170
171
174 OS << "Call graph node for function: '" << F->getName() << "'";
175 else
176 OS << "Call graph node <>";
177
178 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
179
180 for (const auto &I : *this) {
181 OS << " CS<" << I.first << "> calls ";
182 if (Function *FI = I.second->getFunction())
183 OS << "function '" << FI->getName() <<"'\n";
184 else
185 OS << "external node\n";
186 }
187 OS << '\n';
188}
189
190#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
192#endif
193
194
195
197 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
198 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
200 if (CR.second == Callee && !CR.first) {
201 Callee->DropRef();
202 *I = CalledFunctions.back();
203 CalledFunctions.pop_back();
204 return;
205 }
206 }
207}
208
209
210
211
214 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
215 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
216 if (I->first && *I->first == &Call) {
217 I->second->DropRef();
218 I->first = &NewCall;
219 I->second = NewNode;
220 NewNode->AddRef();
221
222
223
227 OldCBs.push_back(CG->getOrInsertFunction(CB));
228 });
230 NewCBs.push_back(CG->getOrInsertFunction(CB));
231 });
232 if (OldCBs.size() == NewCBs.size()) {
233 for (unsigned N = 0; N < OldCBs.size(); ++N) {
236 for (auto J = CalledFunctions.begin();; ++J) {
237 assert(J != CalledFunctions.end() &&
238 "Cannot find callsite to update!");
239 if (!J->first && J->second == OldNode) {
240 J->second = NewNode;
241 OldNode->DropRef();
242 NewNode->AddRef();
243 break;
244 }
245 }
246 }
247 } else {
248 for (auto *CGN : OldCBs)
250 for (auto *CGN : NewCBs)
252 }
253 return;
254 }
255 }
256}
257
258
260
266
270 unsigned sccNum = 0;
271 OS << "SCCs for the program in PostOrder:";
273 ++SCCI) {
274 const std::vector<CallGraphNode *> &nextSCC = *SCCI;
275 OS << "\nSCC #" << ++sccNum << ": ";
276 bool First = true;
280 else
281 OS << ", ";
282 OS << (CGN->getFunction() ? CGN->getFunction()->getName()
283 : "external node");
284 }
285
286 if (nextSCC.size() == 1 && SCCI.hasCycle())
287 OS << " (Has self-loop).";
288 }
289 OS << "\n";
291}
292
293
294
295
296
297
298
299
300
302
304
308
310
312 return false;
313}
314
316 false, true)
317
319
321
323 if (!G) {
324 OS << "No call graph has been built!\n";
325 return;
326 }
327
328
329 G->print(OS);
330}
331
332#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
335#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
This file defines the SmallVector class.
This templated class represents "all analyses that operate over " (e....
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
An analysis pass to compute the CallGraph for a Module.
A node in the call graph for a module.
LLVM_ABI void print(raw_ostream &OS) const
Definition CallGraph.cpp:172
void addCalledFunction(CallBase *Call, CallGraphNode *M)
Adds a function to the list of functions called by this one.
CallGraphNode(CallGraph *CG, Function *F)
Creates a node for the specified function.
LLVM_ABI void replaceCallEdge(CallBase &Call, CallBase &NewCall, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
Definition CallGraph.cpp:212
LLVM_ABI void dump() const
Print out this call graph node.
Definition CallGraph.cpp:191
Function * getFunction() const
Returns the function that this call graph node represents.
LLVM_ABI void removeOneAbstractEdgeTo(CallGraphNode *Callee)
Removes one edge associated with a null callsite from this node to the specified callee function.
Definition CallGraph.cpp:196
unsigned getNumReferences() const
Returns the number of other CallGraphNodes in this CallGraph that reference this node in their callee...
std::pair< std::optional< WeakTrackingVH >, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition CallGraph.cpp:261
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition CallGraph.cpp:267
The ModulePass which wraps up a CallGraph and the logic to build it.
void dump() const
Definition CallGraph.cpp:334
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition CallGraph.cpp:309
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition CallGraph.cpp:305
void print(raw_ostream &o, const Module *) const override
print - Print out the internal state of the pass.
Definition CallGraph.cpp:322
~CallGraphWrapperPass() override
CallGraphWrapperPass()
Definition CallGraph.cpp:301
The basic data container for the call graph of a Module of IR.
LLVM_ABI Function * removeFunctionFromModule(CallGraphNode *CGN)
Unlink the function from this module, returning it.
Definition CallGraph.cpp:145
LLVM_ABI void print(raw_ostream &OS) const
Definition CallGraph.cpp:113
LLVM_ABI void dump() const
Definition CallGraph.cpp:136
LLVM_ABI void populateCallGraphNode(CallGraphNode *CGN)
Populate CGN based on the calls inside the associated function.
Definition CallGraph.cpp:87
LLVM_ABI ~CallGraph()
Definition CallGraph.cpp:52
LLVM_ABI void addToCallGraph(Function *F)
Add a function to the call graph, and link the node to all of the functions that it calls.
Definition CallGraph.cpp:73
LLVM_ABI CallGraphNode * getOrInsertFunction(const Function *F)
Similar to operator[], but this will insert a new CallGraphNode for F if one does not already exist.
Definition CallGraph.cpp:158
LLVM_ABI bool invalidate(Module &, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
Definition CallGraph.cpp:65
LLVM_ABI CallGraph(Module &M)
Definition CallGraph.cpp:31
const Function & getFunction() const
Function::iterator erase(Function::iterator FromIt, Function::iterator ToIt)
Erases a range of BasicBlocks from FromIt to (not including) ToIt.
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements an extremely fast bulk output stream that can only output to a stream.
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
bool isAtEnd() const
Direct loop termination test which is more efficient than comparison with end().
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void forEachCallbackFunction(const CallBase &CB, UnaryFunction Func)
Apply function Func to each CB's callback function.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
A special type used by analysis passes to provide an address that identifies that particular analysis...