13 #include "llvm/Config/llvm-config.h" 33 : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)),
41 : M(
Arg.M), FunctionMap(
std::move(
Arg.FunctionMap)),
42 ExternalCallingNode(
Arg.ExternalCallingNode),
43 CallsExternalNode(
std::move(
Arg.CallsExternalNode)) {
44 Arg.FunctionMap.clear();
45 Arg.ExternalCallingNode =
nullptr;
50 if (CallsExternalNode)
51 CallsExternalNode->allReferencesDropped();
56 for (
auto &
I : FunctionMap)
57 I.second->allReferencesDropped();
61 void CallGraph::addToCallGraph(
Function *
F) {
95 Nodes.
reserve(FunctionMap.size());
97 for (
const auto &
I : *
this)
103 return LF->getName() < RF->getName();
112 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 123 assert(CGN->
empty() &&
"Cannot remove function from call " 124 "graph if it references other functions!");
126 FunctionMap.erase(F);
137 assert(FunctionMap.count(From) &&
"No CallGraphNode for function!");
138 assert(!FunctionMap.count(To) &&
139 "Pointing CallGraphNode at a function that already exists");
140 FunctionMapTy::iterator
I = FunctionMap.find(From);
141 I->second->F =
const_cast<Function*
>(To);
142 FunctionMap[To] = std::move(I->second);
143 FunctionMap.erase(I);
150 auto &CGN = FunctionMap[
F];
154 assert((!F || F->
getParent() == &M) &&
"Function not in current module!");
155 CGN = llvm::make_unique<CallGraphNode>(
const_cast<Function *
>(
F));
165 OS <<
"Call graph node for function: '" << F->
getName() <<
"'";
167 OS <<
"Call graph node <<null function>>";
169 OS <<
"<<" <<
this <<
">> #uses=" << getNumReferences() <<
'\n';
171 for (
const auto &
I : *
this) {
172 OS <<
" CS<" <<
I.first <<
"> calls ";
173 if (
Function *FI =
I.second->getFunction())
174 OS <<
"function '" << FI->getName() <<
"'\n";
176 OS <<
"external node\n";
181 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 189 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
190 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
192 I->second->DropRef();
193 *
I = CalledFunctions.back();
194 CalledFunctions.pop_back();
204 for (
unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
205 if (CalledFunctions[i].
second == Callee) {
207 CalledFunctions[i] = CalledFunctions.back();
208 CalledFunctions.pop_back();
216 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
217 assert(
I != CalledFunctions.end() &&
"Cannot find callee to remove!");
219 if (CR.second == Callee && CR.first ==
nullptr) {
221 *
I = CalledFunctions.back();
222 CalledFunctions.pop_back();
233 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
234 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
236 I->second->DropRef();
287 OS <<
"No call graph has been built!\n";
295 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 302 struct CallGraphPrinterLegacyPass :
public ModulePass {
305 CallGraphPrinterLegacyPass() :
ModulePass(ID) {
315 getAnalysis<CallGraphWrapperPass>().
print(
errs(), &M);
325 "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.
A Module instance is used to store all the information related to an LLVM module. ...
void push_back(const T &Elt)
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...
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...
void addCalledFunction(CallSite CS, CallGraphNode *M)
Adds a function to the list of functions called by this one.
#define INITIALIZE_PASS_DEPENDENCY(depName)
void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
void print(raw_ostream &OS) const
Function * removeFunctionFromModule(CallGraphNode *CGN)
Unlink the function from this module, returning it.
InstrTy * getInstruction() const
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)
amdgpu Simplify well known AMD library false Value * Callee
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.
~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.
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.
amdgpu Simplify well known AMD library false Value Value * Arg
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.
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...
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.
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