Added -view-callgraph module pass.

-dot-callgraph similarly follows a standard module pass pattern.

Patch by Speziale Ettore!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172220 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Andrew Trick 2013-01-11 17:28:14 +00:00
parent dced3cdb04
commit a125cacf7d
7 changed files with 208 additions and 99 deletions

View File

@ -0,0 +1,27 @@
//===-- CallPrinter.h - Call graph printer external interface ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines external functions that can be called to explicitly
// instantiate the call graph printer.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_CALLPRINTER_H
#define LLVM_ANALYSIS_CALLPRINTER_H
namespace llvm {
class ModulePass;
ModulePass *createCallGraphViewerPass();
ModulePass *createCallGraphPrinterPass();
} // end namespace llvm
#endif

View File

@ -18,20 +18,18 @@
#include "llvm/Pass.h"
namespace llvm {
template <class Analysis, bool Simple>
struct DOTGraphTraitsViewer : public FunctionPass {
std::string Name;
DOTGraphTraitsViewer(std::string GraphName, char &ID) : FunctionPass(ID) {
Name = GraphName;
}
template <class Analysis, bool Simple>
class DOTGraphTraitsViewer : public FunctionPass {
public:
DOTGraphTraitsViewer(llvm::StringRef GraphName, char &ID)
: FunctionPass(ID), Name(GraphName) {}
virtual bool runOnFunction(Function &F) {
Analysis *Graph;
std::string Title, GraphName;
Graph = &getAnalysis<Analysis>();
GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
Title = GraphName + " for '" + F.getName().str() + "' function";
Analysis *Graph = &getAnalysis<Analysis>();
std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
ViewGraph(Graph, Name, Simple, Title);
return false;
@ -41,36 +39,34 @@ struct DOTGraphTraitsViewer : public FunctionPass {
AU.setPreservesAll();
AU.addRequired<Analysis>();
}
private:
std::string Name;
};
template <class Analysis, bool Simple>
struct DOTGraphTraitsPrinter : public FunctionPass {
std::string Name;
DOTGraphTraitsPrinter(std::string GraphName, char &ID)
: FunctionPass(ID) {
Name = GraphName;
}
class DOTGraphTraitsPrinter : public FunctionPass {
public:
DOTGraphTraitsPrinter(llvm::StringRef GraphName, char &ID)
: FunctionPass(ID), Name(GraphName) {}
virtual bool runOnFunction(Function &F) {
Analysis *Graph;
Analysis *Graph = &getAnalysis<Analysis>();
std::string Filename = Name + "." + F.getName().str() + ".dot";
std::string ErrorInfo;
errs() << "Writing '" << Filename << "'...";
std::string ErrorInfo;
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Graph = &getAnalysis<Analysis>();
std::string Title, GraphName;
GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
Title = GraphName + " for '" + F.getName().str() + "' function";
std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
if (ErrorInfo.empty())
WriteGraph(File, Graph, Simple, Title);
else
errs() << " error opening file for writing!";
errs() << "\n";
return false;
}
@ -78,6 +74,69 @@ struct DOTGraphTraitsPrinter : public FunctionPass {
AU.setPreservesAll();
AU.addRequired<Analysis>();
}
private:
std::string Name;
};
}
template <class Analysis, bool Simple>
class DOTGraphTraitsModuleViewer : public ModulePass {
public:
DOTGraphTraitsModuleViewer(llvm::StringRef GraphName, char &ID)
: ModulePass(ID), Name(GraphName) {}
virtual bool runOnModule(Module &M) {
Analysis *Graph = &getAnalysis<Analysis>();
std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
ViewGraph(Graph, Name, Simple, Title);
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<Analysis>();
}
private:
std::string Name;
};
template <class Analysis, bool Simple>
class DOTGraphTraitsModulePrinter : public ModulePass {
public:
DOTGraphTraitsModulePrinter(llvm::StringRef GraphName, char &ID)
: ModulePass(ID), Name(GraphName) {}
virtual bool runOnModule(Module &M) {
Analysis *Graph = &getAnalysis<Analysis>();
std::string Filename = Name + ".dot";
std::string ErrorInfo;
errs() << "Writing '" << Filename << "'...";
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
if (ErrorInfo.empty())
WriteGraph(File, Graph, Simple, Title);
else
errs() << " error opening file for writing!";
errs() << "\n";
return false;
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<Analysis>();
}
private:
std::string Name;
};
} // end namespace llvm
#endif

View File

@ -77,6 +77,8 @@ void initializeBoundsCheckingPass(PassRegistry&);
void initializeBranchFolderPassPass(PassRegistry&);
void initializeBranchProbabilityInfoPass(PassRegistry&);
void initializeBreakCriticalEdgesPass(PassRegistry&);
void initializeCallGraphPrinterPass(PassRegistry&);
void initializeCallGraphViewerPass(PassRegistry&);
void initializeCFGOnlyPrinterPass(PassRegistry&);
void initializeCFGOnlyViewerPass(PassRegistry&);
void initializeCFGPrinterPass(PassRegistry&);

View File

@ -16,6 +16,7 @@
#define LLVM_LINKALLPASSES_H
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/CallPrinter.h"
#include "llvm/Analysis/DomPrinter.h"
#include "llvm/Analysis/FindUsedTypes.h"
#include "llvm/Analysis/IntervalPartition.h"
@ -57,6 +58,8 @@ namespace {
(void) llvm::createBlockPlacementPass();
(void) llvm::createBoundsCheckingPass();
(void) llvm::createBreakCriticalEdgesPass();
(void) llvm::createCallGraphPrinterPass();
(void) llvm::createCallGraphViewerPass();
(void) llvm::createCFGSimplificationPass();
(void) llvm::createConstantMergePass();
(void) llvm::createConstantPropagationPass();

View File

@ -0,0 +1,87 @@
//===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
// containing the call graph of a module.
//
// There is also a pass available to directly call dotty ('-view-callgraph').
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallPrinter.h"
#include "llvm/Analysis/DOTGraphTraitsPass.h"
using namespace llvm;
namespace llvm {
template<>
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
static std::string getGraphName(CallGraph *Graph) {
return "Call graph";
}
std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
if (Function *Func = Node->getFunction())
return Func->getName();
return "external node";
}
};
} // end llvm namespace
namespace {
struct CallGraphViewer
: public DOTGraphTraitsModuleViewer<CallGraph, true> {
static char ID;
CallGraphViewer()
: DOTGraphTraitsModuleViewer<CallGraph, true>("callgraph", ID) {
initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
}
};
struct CallGraphPrinter
: public DOTGraphTraitsModulePrinter<CallGraph, true> {
static char ID;
CallGraphPrinter()
: DOTGraphTraitsModulePrinter<CallGraph, true>("callgraph", ID) {
initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
}
};
} // end anonymous namespace
char CallGraphViewer::ID = 0;
INITIALIZE_PASS(CallGraphViewer, "view-callgraph",
"View call graph",
false, false)
char CallGraphPrinter::ID = 0;
INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
"Print call graph to 'dot' file",
false, false)
// Create methods available outside of this file, to use them
// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
// the link time optimization.
ModulePass *llvm::createCallGraphViewerPass() {
return new CallGraphViewer();
}
ModulePass *llvm::createCallGraphPrinterPass() {
return new CallGraphPrinter();
}

View File

@ -20,6 +20,8 @@ using namespace llvm;
void llvm::initializeIPA(PassRegistry &Registry) {
initializeBasicCallGraphPass(Registry);
initializeCallGraphAnalysisGroup(Registry);
initializeCallGraphPrinterPass(Registry);
initializeCallGraphViewerPass(Registry);
initializeFindUsedTypesPass(Registry);
initializeGlobalsModRefPass(Registry);
}

View File

@ -14,82 +14,11 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/ToolOutputFile.h"
using namespace llvm;
template<typename GraphType>
static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName,
const GraphType &GT) {
std::string Filename = GraphName + ".dot";
O << "Writing '" << Filename << "'...";
std::string ErrInfo;
tool_output_file F(Filename.c_str(), ErrInfo);
if (ErrInfo.empty()) {
WriteGraph(F.os(), GT);
F.os().close();
if (!F.os().has_error()) {
O << "\n";
F.keep();
return;
}
}
O << " error opening file for writing!\n";
F.os().clear_error();
}
//===----------------------------------------------------------------------===//
// Call Graph Printer
//===----------------------------------------------------------------------===//
namespace llvm {
template<>
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
static std::string getGraphName(CallGraph *F) {
return "Call Graph";
}
static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
if (Node->getFunction())
return ((Value*)Node->getFunction())->getName();
return "external node";
}
};
}
namespace {
struct CallGraphPrinter : public ModulePass {
static char ID; // Pass ID, replacement for typeid
CallGraphPrinter() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>());
return false;
}
void print(raw_ostream &OS, const llvm::Module*) const {}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<CallGraph>();
AU.setPreservesAll();
}
};
}
char CallGraphPrinter::ID = 0;
static RegisterPass<CallGraphPrinter> P2("dot-callgraph",
"Print Call Graph to 'dot' file");
//===----------------------------------------------------------------------===//
// DomInfoPrinter Pass
//===----------------------------------------------------------------------===//