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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a '-print-cfg' analysis pass, which emits the
|
|
|
|
// 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"
|
2006-11-28 22:46:12 +00:00
|
|
|
#include <iosfwd>
|
2003-10-22 16:03:49 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
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
|
|
|
/// CFGOnly flag - This is used to control whether or not the CFG graph printer
|
|
|
|
/// prints out the contents of basic blocks or not. This is acceptable because
|
|
|
|
/// this code is only really used for debugging purposes.
|
|
|
|
///
|
|
|
|
static bool CFGOnly = false;
|
|
|
|
|
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) {
|
|
|
|
return "CFG for '" + F->getName() + "' function";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeLabel(const BasicBlock *Node,
|
|
|
|
const Function *Graph) {
|
2003-10-22 16:30:58 +00:00
|
|
|
if (CFGOnly && !Node->getName().empty())
|
|
|
|
return Node->getName() + ":";
|
2003-10-22 16:03:49 +00:00
|
|
|
|
|
|
|
std::ostringstream Out;
|
2003-10-22 16:22:42 +00:00
|
|
|
if (CFGOnly) {
|
2006-12-06 06:16:21 +00:00
|
|
|
WriteAsOperand(Out, Node, false);
|
2003-10-22 16:22:42 +00:00
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
|
2003-10-22 16:30:58 +00:00
|
|
|
if (Node->getName().empty()) {
|
2006-12-06 06:16:21 +00:00
|
|
|
WriteAsOperand(Out, Node, false);
|
2003-10-22 16:30:58 +00:00
|
|
|
Out << ":";
|
|
|
|
}
|
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
Out << *Node;
|
|
|
|
std::string OutStr = Out.str();
|
|
|
|
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-03-19 21:56:59 +00:00
|
|
|
CFGViewer() : FunctionPass((intptr_t)&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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(std::ostream &OS, const Module* = 0) const {}
|
|
|
|
|
|
|
|
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-03-19 21:56:59 +00:00
|
|
|
CFGOnlyViewer() : FunctionPass((intptr_t)&ID) {}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2007-05-14 14:25:08 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
CFGOnly = true;
|
|
|
|
F.viewCFG();
|
|
|
|
CFGOnly = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(std::ostream &OS, const Module* = 0) const {}
|
|
|
|
|
|
|
|
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
|
2007-05-01 21:15:47 +00:00
|
|
|
CFGPrinter() : FunctionPass((intptr_t)&ID) {}
|
2008-03-19 21:56:59 +00:00
|
|
|
explicit CFGPrinter(intptr_t pid) : FunctionPass(pid) {}
|
2008-03-18 00:39:19 +00:00
|
|
|
|
2003-10-22 16:03:49 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
std::string Filename = "cfg." + F.getName() + ".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;
|
|
|
|
}
|
|
|
|
|
2004-12-07 04:03:45 +00:00
|
|
|
void print(std::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>
|
|
|
|
P1("print-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 {
|
2007-02-05 23:42:17 +00:00
|
|
|
struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-06-05 20:24:36 +00:00
|
|
|
CFGOnlyPrinter() : CFGPrinter((intptr_t)&ID) {}
|
2003-12-11 21:48:18 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
bool OldCFGOnly = CFGOnly;
|
|
|
|
CFGOnly = true;
|
|
|
|
CFGPrinter::runOnFunction(F);
|
|
|
|
CFGOnly = OldCFGOnly;
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-07 04:03:45 +00:00
|
|
|
void print(std::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>
|
|
|
|
P2("print-cfg-only",
|
|
|
|
"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 {
|
2006-06-27 16:49:46 +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
|
|
|
|
/// 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 {
|
|
|
|
CFGOnly = true;
|
|
|
|
viewCFG();
|
|
|
|
CFGOnly = false;
|
|
|
|
}
|
2004-04-26 16:27:08 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGPrinterPass () {
|
|
|
|
return new CFGPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createCFGOnlyPrinterPass () {
|
|
|
|
return new CFGOnlyPrinter();
|
|
|
|
}
|
|
|
|
|