40 cl::desc(
"Show heat colors in call-graph"));
44 cl::desc(
"Show edges labeled with weights"));
48 cl::desc(
"Show call-multigraph (do not remove parallel edges)"));
52 cl::desc(
"The prefix used for the CallGraph dot file names."));
71 for (
Function &
F : M->getFunctionList()) {
74 for (
User *U :
F.users())
79 if (localSumFreq >= MaxFreq)
80 MaxFreq = localSumFreq;
81 Freq[&
F] = localSumFreq;
84 removeParallelEdges();
96 void removeParallelEdges() {
97 for (
auto &
I : (*CG)) {
100 bool FoundParallelEdge =
true;
101 while (FoundParallelEdge) {
103 FoundParallelEdge =
false;
104 for (
auto CI = Node->begin(), CE = Node->end(); CI != CE; CI++) {
105 if (!(Visited.
insert(CI->second->getFunction())).second) {
106 FoundParallelEdge =
true;
107 Node->removeCallEdge(CI);
124 typedef std::pair<const Function *const, std::unique_ptr<CallGraphNode>>
127 return P.second.get();
148 return "Call graph: " +
162 return "external caller";
164 return "external callee";
166 if (
Function *Func = Node->getFunction())
167 return std::string(Func->getName());
168 return "external node";
176 decltype(&CGGetValuePtr)>
184 Function *Caller = Node->getFunction();
185 if (Caller ==
nullptr || Caller->isDeclaration())
188 Function *Callee = (*I)->getFunction();
189 if (Callee ==
nullptr)
194 1 + 2 * (double(Counter) / CGInfo->
getMaxFreq());
195 std::string Attrs =
"label=\"" + std::to_string(Counter) +
196 "\" penwidth=" + std::to_string(Width);
212 attrs =
"color=\"" + edgeColor +
"ff\", style=filled, fillcolor=\"" +
222void doCallGraphDOTPrinting(
228 Filename = (std::string(
M.getModuleIdentifier()) +
".callgraph.dot");
240 errs() <<
" error opening file for writing!";
244void viewCallGraph(
Module &M,
251 ViewGraph(&CFGInfo,
"callgraph",
true, Title);
265 doCallGraphDOTPrinting(M, LookupBFI);
280 viewCallGraph(M, LookupBFI);
297void CallGraphViewer::getAnalysisUsage(
AnalysisUsage &AU)
const {
298 ModulePass::getAnalysisUsage(AU);
303bool CallGraphViewer::runOnModule(
Module &M) {
305 return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(
F).getBFI();
308 viewCallGraph(M, LookupBFI);
315class CallGraphDOTPrinter :
public ModulePass {
324void CallGraphDOTPrinter::getAnalysisUsage(
AnalysisUsage &AU)
const {
325 ModulePass::getAnalysisUsage(AU);
330bool CallGraphDOTPrinter::runOnModule(
Module &M) {
332 return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(
F).getBFI();
335 doCallGraphDOTPrinting(M, LookupBFI);
342char CallGraphViewer::ID = 0;
343INITIALIZE_PASS(CallGraphViewer,
"view-callgraph",
"View call graph",
false,
346char CallGraphDOTPrinter::
ID = 0;
357 return new CallGraphDOTPrinter();
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
static cl::opt< bool > ShowHeatColors("callgraph-heat-colors", cl::init(false), cl::Hidden, cl::desc("Show heat colors in call-graph"))
static cl::opt< bool > ShowEdgeWeight("callgraph-show-weights", cl::init(false), cl::Hidden, cl::desc("Show edges labeled with weights"))
static cl::opt< bool > CallMultiGraph("callgraph-multigraph", cl::init(false), cl::Hidden, cl::desc("Show call-multigraph (do not remove parallel edges)"))
static cl::opt< std::string > CallGraphDotFilenamePrefix("callgraph-dot-filename-prefix", cl::Hidden, cl::desc("The prefix used for the CallGraph dot file names."))
This file defines the DenseMap class.
static Function * getFunction(Constant *C)
Module.h This file contains the declarations for the Module class.
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
dot regions Print regions of function to dot file(with no function bodies)"
static bool isSimple(Instruction *I)
This file defines the SmallSet class.
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Analysis pass which computes BlockFrequencyInfo.
Legacy analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
CallGraph * getCallGraph() const
std::function< BlockFrequencyInfo *(Function &)> LookupBFI
Module * getModule() const
uint64_t getFreq(const Function *F)
CallGraphDOTInfo(Module *M, CallGraph *CG, function_ref< BlockFrequencyInfo *(Function &)> LookupBFI)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A node in the call graph for a module.
std::vector< CallRecord >::const_iterator const_iterator
std::pair< std::optional< WeakTrackingVH >, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
The basic data container for the call graph of a Module of IR.
FunctionMapTy::const_iterator const_iterator
CallGraphNode * getCallsExternalNode() const
CallGraphNode * getExternalCallingNode() const
Returns the CallGraphNode which is used to represent undetermined calls into the callgraph.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to a file descriptor.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createCallGraphDOTPrinterPass()
uint64_t getNumOfCalls(Function &callerFunction, Function &calledFunction)
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
ModulePass * createCallGraphViewerPass()
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
std::string getHeatColor(uint64_t freq, uint64_t maxFreq)
DOTGraphTraits(bool isSimple=false)
std::string getNodeAttributes(const CallGraphNode *Node, CallGraphDOTInfo *CGInfo)
static std::string getGraphName(CallGraphDOTInfo *CGInfo)
static const CallGraphNode * CGGetValuePtr(CallGraphNode::CallRecord P)
static bool isNodeHidden(const CallGraphNode *Node, const CallGraphDOTInfo *CGInfo)
std::string getEdgeAttributes(const CallGraphNode *Node, nodes_iterator I, CallGraphDOTInfo *CGInfo)
std::string getNodeLabel(const CallGraphNode *Node, CallGraphDOTInfo *CGInfo)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
static NodeRef getEntryNode(CallGraphDOTInfo *CGInfo)
static const CallGraphNode * CGGetValuePtr(const PairTy &P)
static nodes_iterator nodes_begin(CallGraphDOTInfo *CGInfo)
std::pair< const Function *const, std::unique_ptr< CallGraphNode > > PairTy
static nodes_iterator nodes_end(CallGraphDOTInfo *CGInfo)