13#include "llvm/Config/llvm-config.h"
33 : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)),
42 : M(Arg.M), FunctionMap(
std::
move(Arg.FunctionMap)),
43 ExternalCallingNode(Arg.ExternalCallingNode),
44 CallsExternalNode(
std::
move(Arg.CallsExternalNode)) {
45 Arg.FunctionMap.clear();
46 Arg.ExternalCallingNode =
nullptr;
49 CallsExternalNode->CG =
this;
50 for (
auto &
P : FunctionMap)
56 if (CallsExternalNode)
57 CallsExternalNode->allReferencesDropped();
62 for (
auto &
I : FunctionMap)
63 I.second->allReferencesDropped();
80 if (!
F->hasLocalLinkage() ||
81 F->hasAddressTaken(
nullptr,
true,
94 if (
F->isDeclaration() && !
F->hasFnAttribute(Attribute::NoCallback))
95 Node->addCalledFunction(
nullptr, CallsExternalNode.get());
100 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
101 const Function *Callee = Call->getCalledFunction();
103 Node->addCalledFunction(Call, CallsExternalNode.get());
120 Nodes.
reserve(FunctionMap.size());
122 for (
const auto &
I : *
this)
128 return LF->getName() < RF->getName();
130 return RHS->getFunction() !=
nullptr;
137#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
143 for (
auto &CR : ExternalCallingNode->CalledFunctions)
144 if (CR.second == Old) {
145 CR.second->DropRef();
158 assert(CGN->
empty() &&
"Cannot remove function from call "
159 "graph if it references other functions!");
161 FunctionMap.erase(
F);
171 auto &CGN = FunctionMap[
F];
175 assert((!
F ||
F->getParent() == &M) &&
"Function not in current module!");
176 CGN = std::make_unique<CallGraphNode>(
this,
const_cast<Function *
>(
F));
186 OS <<
"Call graph node for function: '" << F->
getName() <<
"'";
188 OS <<
"Call graph node <<null function>>";
192 for (
const auto &
I : *
this) {
193 OS <<
" CS<" <<
I.first <<
"> calls ";
194 if (
Function *FI =
I.second->getFunction())
195 OS <<
"function '" << FI->getName() <<
"'\n";
197 OS <<
"external node\n";
202#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
210 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
211 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
212 if (
I->first && *
I->first == &Call) {
213 I->second->DropRef();
214 *
I = CalledFunctions.back();
215 CalledFunctions.pop_back();
230 for (
unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
231 if (CalledFunctions[i].second == Callee) {
233 CalledFunctions[i] = CalledFunctions.back();
234 CalledFunctions.pop_back();
242 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
243 assert(
I != CalledFunctions.end() &&
"Cannot find callee to remove!");
245 if (CR.second == Callee && !CR.first) {
247 *
I = CalledFunctions.back();
248 CalledFunctions.pop_back();
259 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
260 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
261 if (
I->first && *
I->first == &Call) {
262 I->second->DropRef();
277 if (OldCBs.
size() == NewCBs.
size()) {
278 for (
unsigned N = 0;
N < OldCBs.
size(); ++
N) {
281 for (
auto J = CalledFunctions.begin();; ++J) {
282 assert(J != CalledFunctions.end() &&
283 "Cannot find callsite to update!");
284 if (!J->first && J->second == OldNode) {
293 for (
auto *CGN : OldCBs)
295 for (
auto *CGN : NewCBs)
316 OS <<
"SCCs for the program in PostOrder:";
319 const std::vector<CallGraphNode *> &nextSCC = *SCCI;
320 OS <<
"\nSCC #" << ++sccNum <<
": ";
327 OS << (CGN->getFunction() ? CGN->getFunction()->getName()
331 if (nextSCC.size() == 1 && SCCI.hasCycle())
332 OS <<
" (Has self-loop).";
371 OS <<
"No call graph has been built!\n";
379#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
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)
const MachineOperand & RHS
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
void removeCallEdgeFor(CallBase &Call)
Removes the edge in the node for the specified call site.
void print(raw_ostream &OS) const
void addCalledFunction(CallBase *Call, CallGraphNode *M)
Adds a function to the list of functions called by this one.
void replaceCallEdge(CallBase &Call, CallBase &NewCall, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
void dump() const
Print out this call graph node.
Function * getFunction() const
Returns the function that this call graph node represents.
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...
void removeAnyCallEdgeTo(CallGraphNode *Callee)
Removes all call edges from this node to the specified callee function.
std::pair< std::optional< WeakTrackingVH >, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
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.
Function * removeFunctionFromModule(CallGraphNode *CGN)
Unlink the function from this module, returning it.
void print(raw_ostream &OS) const
void populateCallGraphNode(CallGraphNode *CGN)
Populate CGN based on the calls inside the associated function.
void addToCallGraph(Function *F)
Add a function to the call graph, and link the node to all of the functions that it calls.
CallGraphNode * getOrInsertFunction(const Function *F)
Similar to operator[], but this will insert a new CallGraphNode for F if one does not already exist.
bool invalidate(Module &, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
void ReplaceExternalCallEdge(CallGraphNode *Old, CallGraphNode *New)
Old node has been deleted, and New is to be used in its place, update the ExternalCallingNode.
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).
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
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.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
void initializeCallGraphWrapperPassPass(PassRegistry &)
static bool isDbgInfoIntrinsic(Intrinsic::ID ID)
Check if ID corresponds to a debug info intrinsic.
void sort(IteratorTy Start, IteratorTy End)
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...