24 : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
32 Root = ExternalCallingNode;
36 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), Root(Arg.Root),
37 ExternalCallingNode(Arg.ExternalCallingNode),
38 CallsExternalNode(std::move(Arg.CallsExternalNode)) {
39 Arg.FunctionMap.clear();
41 Arg.ExternalCallingNode =
nullptr;
46 if (CallsExternalNode)
47 CallsExternalNode->allReferencesDropped();
52 for (
auto &
I : FunctionMap)
53 I.second->allReferencesDropped();
57 void CallGraph::addToCallGraph(
Function *
F) {
67 Root = ExternalCallingNode;
86 const Function *Callee =
CS.getCalledFunction();
99 OS <<
"CallGraph Root is: ";
101 OS << F->getName() <<
"\n";
103 OS <<
"<<null function: 0x" << Root <<
">>\n";
110 Nodes.
reserve(FunctionMap.size());
112 for (
const auto &
I : *
this)
115 std::sort(Nodes.
begin(), Nodes.
end(),
118 if (
Function *RF = RHS->getFunction())
119 return LF->getName() < RF->getName();
121 return RHS->getFunction() !=
nullptr;
138 assert(CGN->
empty() &&
"Cannot remove function from call "
139 "graph if it references other functions!");
141 FunctionMap.erase(F);
153 assert(FunctionMap.count(From) &&
"No CallGraphNode for function!");
154 assert(!FunctionMap.count(To) &&
155 "Pointing CallGraphNode at a function that already exists");
156 FunctionMapTy::iterator
I = FunctionMap.find(From);
157 I->second->F =
const_cast<Function*
>(To);
158 FunctionMap[To] = std::move(I->second);
159 FunctionMap.erase(I);
166 auto &CGN = FunctionMap[
F];
170 assert((!F || F->
getParent() == &M) &&
"Function not in current module!");
171 CGN = llvm::make_unique<CallGraphNode>(
const_cast<Function *
>(
F));
181 OS <<
"Call graph node for function: '" << F->getName() <<
"'";
183 OS <<
"Call graph node <<null function>>";
187 for (
const auto &I : *
this) {
188 OS <<
" CS<" << I.first <<
"> calls ";
189 if (
Function *FI = I.second->getFunction())
190 OS <<
"function '" << FI->getName() <<
"'\n";
192 OS <<
"external node\n";
204 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
205 assert(I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
207 I->second->DropRef();
208 *I = CalledFunctions.back();
209 CalledFunctions.pop_back();
219 for (
unsigned i = 0, e = CalledFunctions.size();
i != e; ++
i)
220 if (CalledFunctions[
i].second == Callee) {
222 CalledFunctions[
i] = CalledFunctions.back();
223 CalledFunctions.pop_back();
231 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
232 assert(I != CalledFunctions.end() &&
"Cannot find callee to remove!");
234 if (CR.second == Callee && CR.first ==
nullptr) {
236 *I = CalledFunctions.back();
237 CalledFunctions.pop_back();
248 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++
I) {
249 assert(I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
251 I->second->DropRef();
302 OS <<
"No call graph has been built!\n";
314 struct CallGraphPrinterLegacyPass :
public ModulePass {
316 CallGraphPrinterLegacyPass() :
ModulePass(ID) {
324 bool runOnModule(
Module &M)
override {
325 getAnalysis<CallGraphWrapperPass>().print(
errs(), &M);
334 "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.
StringRef getName() const
Return a constant reference to the value's name.
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.
#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.
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.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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
std::pair< WeakVH, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called...
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 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...