24 : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)),
32 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)),
33 ExternalCallingNode(Arg.ExternalCallingNode),
34 CallsExternalNode(std::move(Arg.CallsExternalNode)) {
35 Arg.FunctionMap.clear();
36 Arg.ExternalCallingNode =
nullptr;
41 if (CallsExternalNode)
42 CallsExternalNode->allReferencesDropped();
47 for (
auto &
I : FunctionMap)
48 I.second->allReferencesDropped();
52 void CallGraph::addToCallGraph(
Function *
F) {
69 const Function *Callee = CS.getCalledFunction();
86 Nodes.
reserve(FunctionMap.size());
88 for (
const auto &
I : *
this)
94 if (
Function *RF = RHS->getFunction())
95 return LF->getName() < RF->getName();
97 return RHS->getFunction() !=
nullptr;
104 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
115 assert(CGN->
empty() &&
"Cannot remove function from call "
116 "graph if it references other functions!");
118 FunctionMap.erase(F);
130 assert(FunctionMap.count(From) &&
"No CallGraphNode for function!");
131 assert(!FunctionMap.count(To) &&
132 "Pointing CallGraphNode at a function that already exists");
133 FunctionMapTy::iterator
I = FunctionMap.find(From);
134 I->second->F =
const_cast<Function*
>(To);
135 FunctionMap[To] = std::move(I->second);
136 FunctionMap.erase(I);
143 auto &CGN = FunctionMap[
F];
147 assert((!F || F->
getParent() == &M) &&
"Function not in current module!");
148 CGN = llvm::make_unique<CallGraphNode>(
const_cast<Function *
>(
F));
158 OS <<
"Call graph node for function: '" << F->getName() <<
"'";
160 OS <<
"Call graph node <<null function>>";
164 for (
const auto &I : *
this) {
165 OS <<
" CS<" << I.first <<
"> calls ";
166 if (
Function *FI = I.second->getFunction())
167 OS <<
"function '" << FI->getName() <<
"'\n";
169 OS <<
"external node\n";
174 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
182 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
183 assert(I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
185 I->second->DropRef();
186 *I = CalledFunctions.back();
187 CalledFunctions.pop_back();
197 for (
unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
198 if (CalledFunctions[i].
second == Callee) {
200 CalledFunctions[i] = CalledFunctions.back();
201 CalledFunctions.pop_back();
209 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
210 assert(I != CalledFunctions.end() &&
"Cannot find callee to remove!");
212 if (CR.second == Callee && CR.first ==
nullptr) {
214 *I = CalledFunctions.back();
215 CalledFunctions.pop_back();
226 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
227 assert(I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
229 I->second->DropRef();
280 OS <<
"No call graph has been built!\n";
288 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
294 struct CallGraphPrinterLegacyPass :
public ModulePass {
296 CallGraphPrinterLegacyPass() :
ModulePass(ID) {
304 bool runOnModule(
Module &M)
override {
305 getAnalysis<CallGraphWrapperPass>().print(
errs(), &M);
314 "Print a call graph",
true,
true)
void push_back(const T &Elt)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void Print(const Unit &v, const char *PrintAfter)
#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 dump() const
Print out this call graph node.
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
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.
Function * getFunction() const
Returns the function that this call graph node represents.
unsigned getNumReferences() const
Returns the number of other CallGraphNodes in this CallGraph that reference this node in their callee...
void removeOneAbstractEdgeTo(CallGraphNode *Callee)
Removes one edge associated with a null callsite from this node to the specified callee function...
void addCalledFunction(CallSite CS, CallGraphNode *M)
Adds a function to the list of functions called by this one.
std::pair< WeakTrackingVH, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called...
#define INITIALIZE_PASS_DEPENDENCY(depName)
void print(raw_ostream &OS) const
void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Function * removeFunctionFromModule(CallGraphNode *CGN)
Unlink the function from this module, returning it.
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
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.
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
LLVM Basic Block Representation.
void initializeCallGraphWrapperPassPass(PassRegistry &)
INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass,"print-callgraph","Print a call graph", true, true) INITIALIZE_PASS_END(CallGraphPrinterLegacyPass
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
~CallGraphWrapperPass() override
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
InstrTy * getInstruction() const
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.
void setPreservesAll()
Set by analyses that do not transform their input at all.
void print(raw_ostream &OS) const
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
AnalysisUsage & addRequiredTransitive()
bool hasLocalLinkage() const
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...
void removeCallEdgeFor(CallSite CS)
Removes the edge in the node for the specified call site.
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.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
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...