2003-10-22 16:03:49 +00:00
|
|
|
//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-22 16:03:49 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-22 16:03:49 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-09-23 12:47:39 +00:00
|
|
|
// This file defines a '-dot-cfg' analysis pass, which emits the
|
2003-10-22 16:03:49 +00: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 16:27:08 +00:00
|
|
|
#include "llvm/Analysis/CFGPrinter.h"
|
2009-10-18 04:09:11 +00:00
|
|
|
#include "llvm/Pass.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2003-12-11 21:48:18 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct CFGViewer : public FunctionPass {
|
2007-05-14 14:25:08 +00:00
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
CFGViewer() : FunctionPass(ID) {
|
|
|
|
initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2007-05-14 14:25:08 +00:00
|
|
|
F.viewCFG();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:59:12 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = nullptr) const override {}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2007-05-14 14:25:08 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGViewer::ID = 0;
|
2010-10-07 22:25:06 +00:00
|
|
|
INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true)
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct CFGOnlyViewer : public FunctionPass {
|
2007-05-14 14:25:08 +00:00
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
CFGOnlyViewer() : FunctionPass(ID) {
|
|
|
|
initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2009-09-19 11:25:44 +00:00
|
|
|
F.viewCFGOnly();
|
2007-05-14 14:25:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:59:12 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = nullptr) const override {}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2007-05-14 14:25:08 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGOnlyViewer::ID = 0;
|
2010-07-21 23:07:00 +00:00
|
|
|
INITIALIZE_PASS(CFGOnlyViewer, "view-cfg-only",
|
2010-10-07 22:25:06 +00:00
|
|
|
"View CFG of function (with no function bodies)", false, true)
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct CFGPrinter : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
CFGPrinter() : FunctionPass(ID) {
|
|
|
|
initializeCFGPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2015-03-27 17:51:30 +00:00
|
|
|
std::string Filename = ("cfg." + F.getName() + ".dot").str();
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
|
|
|
|
|
|
|
if (!EC)
|
2003-10-22 16:03:49 +00:00
|
|
|
WriteGraph(File, (const Function*)&F);
|
|
|
|
else
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2003-10-22 16:03:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:59:12 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = nullptr) const override {}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2003-10-22 16:03:49 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2003-10-22 16:03:49 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGPrinter::ID = 0;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_PASS(CFGPrinter, "dot-cfg", "Print CFG of function to 'dot' file",
|
2010-10-07 22:25:06 +00:00
|
|
|
false, true)
|
2003-12-11 21:48:18 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct CFGOnlyPrinter : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
CFGOnlyPrinter() : FunctionPass(ID) {
|
|
|
|
initializeCFGOnlyPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 07:30:04 +00:00
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override {
|
2015-03-27 17:51:30 +00:00
|
|
|
std::string Filename = ("cfg." + F.getName() + ".dot").str();
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
2009-06-24 17:37:09 +00:00
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
|
|
|
|
|
|
|
if (!EC)
|
2009-06-24 17:37:09 +00:00
|
|
|
WriteGraph(File, (const Function*)&F, true);
|
|
|
|
else
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2003-12-11 21:48:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-15 04:59:12 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = nullptr) const override {}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2003-12-11 21:48:18 +00:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2003-10-22 16:03:49 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGOnlyPrinter::ID = 0;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_PASS(CFGOnlyPrinter, "dot-cfg-only",
|
|
|
|
"Print CFG of function to 'dot' file (with no function bodies)",
|
2010-10-07 22:25:06 +00:00
|
|
|
false, true)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2003-10-22 16:03:49 +00: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 {
|
2011-11-15 16:26:38 +00:00
|
|
|
ViewGraph(this, "cfg" + getName());
|
2003-10-22 16:03:49 +00: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
|
2014-05-20 17:11:11 +00:00
|
|
|
/// into the nodes, just the label. If you are only interested in the CFG
|
|
|
|
/// this can make the graph smaller.
|
2003-10-22 16:03:49 +00:00
|
|
|
///
|
|
|
|
void Function::viewCFGOnly() const {
|
2011-11-15 16:26:38 +00:00
|
|
|
ViewGraph(this, "cfg" + getName(), true);
|
2003-10-22 16:03:49 +00:00
|
|
|
}
|
2004-04-26 16:27:08 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGPrinterPass () {
|
|
|
|
return new CFGPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGOnlyPrinterPass () {
|
|
|
|
return new CFGOnlyPrinter();
|
|
|
|
}
|
|
|
|
|