2003-10-22 18:03:49 +02:00
|
|
|
//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-22 18:03:49 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-22 18:03:49 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-09-23 14:47:39 +02:00
|
|
|
// This file defines a '-dot-cfg' analysis pass, which emits the
|
2003-10-22 18:03:49 +02:00
|
|
|
// cfg.<fnname>.dot file for each function in the program, with a graph of the
|
|
|
|
// CFG for that function.
|
|
|
|
//
|
|
|
|
// The other main feature of this file is that it implements the
|
|
|
|
// Function::viewCFG method, which is useful for debugging passes which operate
|
|
|
|
// on the CFG.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-04-26 18:27:08 +02:00
|
|
|
#include "llvm/Analysis/CFGPrinter.h"
|
2009-10-18 06:09:11 +02:00
|
|
|
|
|
|
|
#include "llvm/Pass.h"
|
2003-12-11 22:48:18 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2003-10-22 18:03:49 +02:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct CFGViewer : public FunctionPass {
|
2007-05-14 16:25:08 +02:00
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2008-09-04 19:05:41 +02:00
|
|
|
CFGViewer() : FunctionPass(&ID) {}
|
2008-03-18 01:39:19 +01:00
|
|
|
|
2007-05-14 16:25:08 +02:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
F.viewCFG();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 08:03:38 +02:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2007-05-14 16:25:08 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 02:00:25 +02:00
|
|
|
}
|
2007-05-14 16:25:08 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char CFGViewer::ID = 0;
|
|
|
|
static RegisterPass<CFGViewer>
|
|
|
|
V0("view-cfg", "View CFG of function", false, true);
|
2007-05-14 16:25:08 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct CFGOnlyViewer : public FunctionPass {
|
2007-05-14 16:25:08 +02:00
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2008-09-04 19:05:41 +02:00
|
|
|
CFGOnlyViewer() : FunctionPass(&ID) {}
|
2008-03-18 01:39:19 +01:00
|
|
|
|
2007-05-14 16:25:08 +02:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-09-19 13:25:44 +02:00
|
|
|
F.viewCFGOnly();
|
2007-05-14 16:25:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 08:03:38 +02:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2007-05-14 16:25:08 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 02:00:25 +02:00
|
|
|
}
|
2007-05-14 16:25:08 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char CFGOnlyViewer::ID = 0;
|
|
|
|
static RegisterPass<CFGOnlyViewer>
|
|
|
|
V1("view-cfg-only",
|
|
|
|
"View CFG of function (with no function bodies)", false, true);
|
2007-05-14 16:25:08 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct CFGPrinter : public FunctionPass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 19:05:41 +02:00
|
|
|
CFGPrinter() : FunctionPass(&ID) {}
|
|
|
|
explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
|
2008-03-18 01:39:19 +01:00
|
|
|
|
2003-10-22 18:03:49 +02:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-07-24 10:24:36 +02:00
|
|
|
std::string Filename = "cfg." + F.getNameStr() + ".dot";
|
2009-08-23 09:19:13 +02:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
|
|
|
|
|
|
|
std::string ErrorInfo;
|
2009-08-25 17:34:52 +02:00
|
|
|
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2009-08-23 09:19:13 +02:00
|
|
|
if (ErrorInfo.empty())
|
2003-10-22 18:03:49 +02:00
|
|
|
WriteGraph(File, (const Function*)&F);
|
|
|
|
else
|
2009-08-23 09:19:13 +02:00
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2003-10-22 18:03:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 08:03:38 +02:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-10-22 18:03:49 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 02:00:25 +02:00
|
|
|
}
|
2003-10-22 18:03:49 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char CFGPrinter::ID = 0;
|
|
|
|
static RegisterPass<CFGPrinter>
|
2008-09-23 14:47:39 +02:00
|
|
|
P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
|
2003-12-11 22:48:18 +01:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct CFGOnlyPrinter : public FunctionPass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2009-06-24 19:37:09 +02:00
|
|
|
CFGOnlyPrinter() : FunctionPass(&ID) {}
|
|
|
|
explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
|
2003-12-11 22:48:18 +01:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-07-24 10:24:36 +02:00
|
|
|
std::string Filename = "cfg." + F.getNameStr() + ".dot";
|
2009-08-23 09:19:13 +02:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
2009-06-24 19:37:09 +02:00
|
|
|
|
2009-08-23 09:19:13 +02:00
|
|
|
std::string ErrorInfo;
|
2009-08-25 17:34:52 +02:00
|
|
|
raw_fd_ostream File(Filename.c_str(), ErrorInfo);
|
2009-08-23 09:19:13 +02:00
|
|
|
|
|
|
|
if (ErrorInfo.empty())
|
2009-06-24 19:37:09 +02:00
|
|
|
WriteGraph(File, (const Function*)&F, true);
|
|
|
|
else
|
2009-08-23 09:19:13 +02:00
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2003-12-11 22:48:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-23 08:03:38 +02:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-12-11 22:48:18 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2003-10-22 18:03:49 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char CFGOnlyPrinter::ID = 0;
|
|
|
|
static RegisterPass<CFGOnlyPrinter>
|
2008-09-23 14:47:39 +02:00
|
|
|
P2("dot-cfg-only",
|
2008-05-13 02:00:25 +02:00
|
|
|
"Print CFG of function to 'dot' file (with no function bodies)", false, true);
|
|
|
|
|
2003-10-22 18:03:49 +02:00
|
|
|
/// viewCFG - This function is meant for use from the debugger. You can just
|
|
|
|
/// say 'call F->viewCFG()' and a ghostview window should pop up from the
|
|
|
|
/// program, displaying the CFG of the current function. This depends on there
|
|
|
|
/// being a 'dot' and 'gv' program in your path.
|
|
|
|
///
|
|
|
|
void Function::viewCFG() const {
|
2009-07-24 10:24:36 +02:00
|
|
|
ViewGraph(this, "cfg" + getNameStr());
|
2003-10-22 18:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// viewCFGOnly - This function is meant for use from the debugger. It works
|
|
|
|
/// just like viewCFG, but it does not include the contents of basic blocks
|
|
|
|
/// into the nodes, just the label. If you are only interested in the CFG t
|
|
|
|
/// his can make the graph smaller.
|
|
|
|
///
|
|
|
|
void Function::viewCFGOnly() const {
|
2009-07-24 10:24:36 +02:00
|
|
|
ViewGraph(this, "cfg" + getNameStr(), true);
|
2003-10-22 18:03:49 +02:00
|
|
|
}
|
2004-04-26 18:27:08 +02:00
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGPrinterPass () {
|
|
|
|
return new CFGPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGOnlyPrinterPass () {
|
|
|
|
return new CFGOnlyPrinter();
|
|
|
|
}
|
|
|
|
|