13#include "llvm/Config/llvm-config.h"
32 : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)),
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;
47 CallsExternalNode->CG =
this;
48 for (
auto &
P : FunctionMap)
54 if (CallsExternalNode)
55 CallsExternalNode->allReferencesDropped();
60 for (
auto &
I : FunctionMap)
61 I.second->allReferencesDropped();
78 if (!
F->hasLocalLinkage() ||
79 F->hasAddressTaken(
nullptr,
true,
92 if (
F->isDeclaration() && !
F->hasFnAttribute(Attribute::NoCallback))
93 Node->addCalledFunction(
nullptr, CallsExternalNode.get());
98 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
99 const Function *Callee = Call->getCalledFunction();
101 Node->addCalledFunction(Call, CallsExternalNode.get());
118 Nodes.
reserve(FunctionMap.size());
120 for (
const auto &
I : *
this)
126 return LF->getName() < RF->getName();
128 return RHS->getFunction() !=
nullptr;
135#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
146 assert(CGN->
empty() &&
"Cannot remove function from call "
147 "graph if it references other functions!");
149 FunctionMap.erase(
F);
159 auto &CGN = FunctionMap[
F];
163 assert((!
F ||
F->getParent() == &M) &&
"Function not in current module!");
164 CGN = std::make_unique<CallGraphNode>(
this,
const_cast<Function *
>(
F));
174 OS <<
"Call graph node for function: '" << F->
getName() <<
"'";
176 OS <<
"Call graph node <<null function>>";
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";
185 OS <<
"external node\n";
190#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
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) {
202 *
I = CalledFunctions.back();
203 CalledFunctions.pop_back();
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();
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) {
248 for (
auto *CGN : OldCBs)
250 for (
auto *CGN : NewCBs)
271 OS <<
"SCCs for the program in PostOrder:";
274 const std::vector<CallGraphNode *> &nextSCC = *SCCI;
275 OS <<
"\nSCC #" << ++sccNum <<
": ";
282 OS << (CGN->getFunction() ? CGN->getFunction()->getName()
286 if (nextSCC.size() == 1 && SCCI.hasCycle())
287 OS <<
" (Has self-loop).";
324 OS <<
"No call graph has been built!\n";
332#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
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 <a particular IR unit>" (e....
API to communicate dependencies between analyses during invalidation.
A container for analyses that lazily runs them and caches their results.
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
void addCalledFunction(CallBase *Call, CallGraphNode *M)
Adds a function to the list of functions called by this one.
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.
LLVM_ABI void dump() const
Print out this call graph node.
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.
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)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The ModulePass which wraps up a CallGraph and the logic to build it.
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &o, const Module *) const override
print - Print out the internal state of the pass.
~CallGraphWrapperPass() override
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.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI void dump() const
LLVM_ABI void populateCallGraphNode(CallGraphNode *CGN)
Populate CGN based on the calls inside the associated function.
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.
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.
LLVM_ABI bool invalidate(Module &, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
LLVM_ABI CallGraph(Module &M)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
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.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
pointer remove(iterator &IT)
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.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
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.
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...