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)
104 return LF->getName() < RF->getName();
106 return RHS->getFunction() !=
nullptr;
113 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 124 assert(CGN->
empty() &&
"Cannot remove function from call " 125 "graph if it references other functions!");
127 FunctionMap.erase(F);
138 assert(FunctionMap.count(From) &&
"No CallGraphNode for function!");
139 assert(!FunctionMap.count(To) &&
140 "Pointing CallGraphNode at a function that already exists");
141 FunctionMapTy::iterator
I = FunctionMap.find(From);
142 I->second->F =
const_cast<Function*
>(To);
143 FunctionMap[To] = std::move(I->second);
144 FunctionMap.erase(I);
151 auto &CGN = FunctionMap[
F];
155 assert((!F || F->
getParent() == &M) &&
"Function not in current module!");
156 CGN = llvm::make_unique<CallGraphNode>(
const_cast<Function *
>(
F));
166 OS <<
"Call graph node for function: '" << F->
getName() <<
"'";
168 OS <<
"Call graph node <<null function>>";
170 OS <<
"<<" <<
this <<
">> #uses=" << getNumReferences() <<
'\n';
172 for (
const auto &
I : *
this) {
173 OS <<
" CS<" <<
I.first <<
"> calls ";
174 if (
Function *FI =
I.second->getFunction())
175 OS <<
"function '" << FI->getName() <<
"'\n";
177 OS <<
"external node\n";
182 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 190 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
191 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
193 I->second->DropRef();
194 *
I = CalledFunctions.back();
195 CalledFunctions.pop_back();
205 for (
unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
206 if (CalledFunctions[i].
second == Callee) {
208 CalledFunctions[i] = CalledFunctions.back();
209 CalledFunctions.pop_back();
217 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
218 assert(
I != CalledFunctions.end() &&
"Cannot find callee to remove!");
220 if (CR.second == Callee && CR.first ==
nullptr) {
222 *
I = CalledFunctions.back();
223 CalledFunctions.pop_back();
234 for (CalledFunctionsVector::iterator
I = CalledFunctions.begin(); ; ++
I) {
235 assert(
I != CalledFunctions.end() &&
"Cannot find callsite to remove!");
237 I->second->DropRef();
288 OS <<
"No call graph has been built!\n";
296 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 303 struct CallGraphPrinterLegacyPass :
public ModulePass {
306 CallGraphPrinterLegacyPass() :
ModulePass(ID) {
316 getAnalysis<CallGraphWrapperPass>().
print(
errs(), &M);
326 "Print a call graph",
true,
true)
const Function & getFunction() const
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
void push_back(const T &Elt)
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.
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
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 &)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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.
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
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