12 #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;
49 if (CallsExternalNode)
50 CallsExternalNode->allReferencesDropped();
55 for (
auto &
I : FunctionMap)
56 I.second->allReferencesDropped();
60 void CallGraph::addToCallGraph(
Function *
F) {
76 if (
auto *Call = dyn_cast<CallBase>(&
I)) {
94 Nodes.
reserve(FunctionMap.size());
96 for (
const auto &
I : *
this)
102 return LF->getName() < RF->getName();
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 122 assert(CGN->
empty() &&
"Cannot remove function from call " 123 "graph if it references other functions!");
125 FunctionMap.erase(F);
136 assert(FunctionMap.count(From) &&
"No CallGraphNode for function!");
137 assert(!FunctionMap.count(To) &&
138 "Pointing CallGraphNode at a function that already exists");
139 FunctionMapTy::iterator
I = FunctionMap.find(From);
140 I->second->F =
const_cast<Function*
>(To);
141 FunctionMap[To] = std::move(I->second);
142 FunctionMap.erase(I);
149 auto &CGN = FunctionMap[
F];
153 assert((!F || F->
getParent() == &M) &&
"Function not in current module!");
154 CGN = std::make_unique<CallGraphNode>(
const_cast<Function *
>(
F));
164 OS <<
"Call graph node for function: '" << F->
getName() <<
"'";
166 OS <<
"Call graph node <<null function>>";
168 OS <<
"<<" <<
this <<
">> #uses=" << getNumReferences() <<
'\n';
170 for (
const auto &
I : *
this) {
171 OS <<
" CS<" <<
I.first <<
"> calls ";
172 if (
Function *FI =
I.second->getFunction())
173 OS <<
"function '" << FI->getName() <<
"'\n";
175 OS <<
"external node\n";
180 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 188 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
189 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
190 if (
I->first == &Call) {
191 I->second->DropRef();
192 *
I = CalledFunctions.back();
193 CalledFunctions.pop_back();
203 for (
unsigned i = 0,
e = CalledFunctions.size(); i !=
e; ++i)
204 if (CalledFunctions[i].
second == Callee) {
206 CalledFunctions[i] = CalledFunctions.back();
207 CalledFunctions.pop_back();
215 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
216 assert(
I != CalledFunctions.end() &&
"Cannot find callee to remove!");
218 if (CR.second == Callee && CR.first ==
nullptr) {
220 *
I = CalledFunctions.back();
221 CalledFunctions.pop_back();
232 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
233 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
234 if (
I->first == &Call) {
235 I->second->DropRef();
286 OS <<
"No call graph has been built!\n";
294 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 301 struct CallGraphPrinterLegacyPass :
public ModulePass {
304 CallGraphPrinterLegacyPass() :
ModulePass(ID) {
314 getAnalysis<CallGraphWrapperPass>().
print(
errs(), &M);
324 "Print a call graph",
true,
true)
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool hasLocalLinkage() const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
A Module instance is used to store all the information related to an LLVM module. ...
void push_back(const T &Elt)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
void reserve(size_type N)
A node in the call graph for a module.
void removeOneAbstractEdgeTo(CallGraphNode *Callee)
Removes one edge associated with a null callsite from this node to the specified callee function...
#define INITIALIZE_PASS_DEPENDENCY(depName)
void print(raw_ostream &OS) const
Function * removeFunctionFromModule(CallGraphNode *CGN)
Unlink the function from this module, returning it.
void dump() const
Print out this call graph node.
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
static Function * getFunction(Constant *C)
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void initializeCallGraphPrinterLegacyPassPass(PassRegistry &)
void removeAnyCallEdgeTo(CallGraphNode *Callee)
Removes all call edges from this node to the specified callee function.
A set of analyses that are preserved following a run of a transformation pass.
The ModulePass which wraps up a CallGraph and the logic to build it.
bool isLeaf(ID id)
Returns true if the intrinsic is a leaf, i.e.
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
LLVM Basic Block Representation.
void initializeCallGraphWrapperPassPass(PassRegistry &)
Represent the analysis usage information of a pass.
void removeCallEdgeFor(CallBase &Call)
Removes the edge in the node for the specified call site.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
~CallGraphWrapperPass() override
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
void sort(IteratorTy Start, IteratorTy End)
Function * getFunction() const
Returns the function that this call graph node represents.
BlockVerifier::State From
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
amdgpu Simplify well known AMD library false FunctionCallee Callee
INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph", "Print a call graph", true, true) INITIALIZE_PASS_END(CallGraphPrinterLegacyPass
void setPreservesAll()
Set by analyses that do not transform their input at all.
An analysis pass to compute the CallGraph for a Module.
pointer remove(iterator &IT)
The basic data container for the call graph of a Module of IR.
void replaceCallEdge(CallBase &Call, CallBase &NewCall, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
StringRef getName() const
Return a constant reference to the value's name.
std::pair< WeakTrackingVH, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
AnalysisUsage & addRequiredTransitive()
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Module * getParent()
Get the module that this global value is contained inside of...
CallGraphNode * getOrInsertFunction(const Function *F)
Similar to operator[], but this will insert a new CallGraphNode for F if one does not already exist...
This class implements an extremely fast bulk output stream that can only output to a stream...
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
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.
A special type used by analysis passes to provide an address that identifies that particular analysis...
void print(raw_ostream &OS) const
void addCalledFunction(CallBase *Call, CallGraphNode *M)
Adds a function to the list of functions called by this one.