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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Function.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-04-29 04:04:47 +00:00
|
|
|
#include "llvm/Pass.h"
|
2004-04-26 16:27:08 +00:00
|
|
|
#include "llvm/Analysis/CFGPrinter.h"
|
2004-04-29 04:04:47 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-10-22 16:03:49 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-05 23:42:17 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2005-08-03 17:55:05 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2003-12-11 21:48:18 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-12-11 21:48:18 +00:00
|
|
|
namespace llvm {
|
2003-10-22 16:03:49 +00:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const Function *F) {
|
2009-07-24 08:24:36 +00:00
|
|
|
return "CFG for '" + F->getNameStr() + "' function";
|
2003-10-22 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeLabel(const BasicBlock *Node,
|
2009-06-24 17:37:09 +00:00
|
|
|
const Function *Graph,
|
|
|
|
bool ShortNames) {
|
|
|
|
if (ShortNames && !Node->getName().empty())
|
2009-07-24 08:24:36 +00:00
|
|
|
return Node->getNameStr() + ":";
|
2003-10-22 16:03:49 +00:00
|
|
|
|
2009-08-23 04:37:46 +00:00
|
|
|
std::string Str;
|
|
|
|
raw_string_ostream OS(Str);
|
|
|
|
|
2009-06-24 17:37:09 +00:00
|
|
|
if (ShortNames) {
|
2009-08-23 04:37:46 +00:00
|
|
|
WriteAsOperand(OS, Node, false);
|
|
|
|
return OS.str();
|
2003-10-22 16:22:42 +00:00
|
|
|
}
|
|
|
|
|
2003-10-22 16:30:58 +00:00
|
|
|
if (Node->getName().empty()) {
|
2009-08-23 04:37:46 +00:00
|
|
|
WriteAsOperand(OS, Node, false);
|
|
|
|
OS << ":";
|
2003-10-22 16:30:58 +00:00
|
|
|
}
|
2009-08-23 04:37:46 +00:00
|
|
|
|
|
|
|
OS << *Node;
|
|
|
|
std::string OutStr = OS.str();
|
2003-10-22 16:03:49 +00:00
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
|
|
|
|
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
} else if (OutStr[i] == ';') { // Delete comments!
|
|
|
|
unsigned Idx = OutStr.find('\n', i+1); // Find end of line
|
|
|
|
OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OutStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getEdgeSourceLabel(const BasicBlock *Node,
|
|
|
|
succ_const_iterator I) {
|
|
|
|
// Label source of conditional branches with "T" or "F"
|
|
|
|
if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
|
|
|
|
if (BI->isConditional())
|
|
|
|
return (I == succ_begin(Node)) ? "T" : "F";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2003-12-11 21:48:18 +00:00
|
|
|
}
|
2003-10-22 16:03:49 +00:00
|
|
|
|
|
|
|
namespace {
|
2007-05-14 14:25:08 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
|
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
CFGViewer() : FunctionPass(&ID) {}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2007-05-14 14:25:08 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
F.viewCFG();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGViewer::ID = 0;
|
|
|
|
static RegisterPass<CFGViewer>
|
|
|
|
V0("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 {
|
2007-05-14 14:25:08 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
|
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
CFGOnlyViewer() : FunctionPass(&ID) {}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2007-05-14 14:25:08 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
F.viewCFG();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2007-05-14 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00: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 14:25:08 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2007-02-05 23:42:17 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
CFGPrinter() : FunctionPass(&ID) {}
|
|
|
|
explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-07-24 08:24:36 +00:00
|
|
|
std::string Filename = "cfg." + F.getNameStr() + ".dot";
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Writing '" << Filename << "'...";
|
2003-10-22 16:03:49 +00:00
|
|
|
std::ofstream File(Filename.c_str());
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
if (File.good())
|
|
|
|
WriteGraph(File, (const Function*)&F);
|
|
|
|
else
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << " error opening file for writing!";
|
|
|
|
cerr << "\n";
|
2003-10-22 16:03:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
}
|
2003-10-22 16:03:49 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGPrinter::ID = 0;
|
|
|
|
static RegisterPass<CFGPrinter>
|
2008-09-23 12:47:39 +00:00
|
|
|
P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
|
2003-12-11 21:48:18 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2009-06-24 17:37:09 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2009-06-24 17:37:09 +00:00
|
|
|
CFGOnlyPrinter() : FunctionPass(&ID) {}
|
|
|
|
explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
|
2003-12-11 21:48:18 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
2009-07-24 08:24:36 +00:00
|
|
|
std::string Filename = "cfg." + F.getNameStr() + ".dot";
|
2009-06-24 17:37:09 +00:00
|
|
|
cerr << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream File(Filename.c_str());
|
|
|
|
|
|
|
|
if (File.good())
|
|
|
|
WriteGraph(File, (const Function*)&F, true);
|
|
|
|
else
|
|
|
|
cerr << " error opening file for writing!";
|
|
|
|
cerr << "\n";
|
2003-12-11 21:48:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-23 06:03:38 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) const {}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-12-11 21:48:18 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2003-10-22 16:03:49 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char CFGOnlyPrinter::ID = 0;
|
|
|
|
static RegisterPass<CFGOnlyPrinter>
|
2008-09-23 12:47:39 +00:00
|
|
|
P2("dot-cfg-only",
|
2008-05-13 00:00:25 +00:00
|
|
|
"Print CFG of function to 'dot' file (with no function bodies)", false, true);
|
|
|
|
|
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 {
|
2009-07-24 08:24:36 +00:00
|
|
|
ViewGraph(this, "cfg" + getNameStr());
|
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
|
|
|
|
/// 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 08:24:36 +00:00
|
|
|
ViewGraph(this, "cfg" + getNameStr(), 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();
|
|
|
|
}
|
|
|
|
|