1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a '-print-cfg' analysis pass, which emits the
11 // cfg.<fnname>.dot file for each function in the program, with a graph of the
12 // CFG for that function.
14 // The other main feature of this file is that it implements the
15 // Function::viewCFG method, which is useful for debugging passes which operate
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Analysis/CFGPrinter.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/GraphWriter.h"
27 #include "llvm/Config/config.h"
32 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
33 /// prints out the contents of basic blocks or not. This is acceptable because
34 /// this code is only really used for debugging purposes.
36 static bool CFGOnly = false;
40 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
41 static std::string getGraphName(const Function *F) {
42 return "CFG for '" + F->getName() + "' function";
45 static std::string getNodeLabel(const BasicBlock *Node,
46 const Function *Graph) {
47 if (CFGOnly && !Node->getName().empty())
48 return Node->getName() + ":";
50 std::ostringstream Out;
52 WriteAsOperand(Out, Node, false, true);
56 if (Node->getName().empty()) {
57 WriteAsOperand(Out, Node, false, true);
62 std::string OutStr = Out.str();
63 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
65 // Process string output to make it nicer...
66 for (unsigned i = 0; i != OutStr.length(); ++i)
67 if (OutStr[i] == '\n') { // Left justify
69 OutStr.insert(OutStr.begin()+i+1, 'l');
70 } else if (OutStr[i] == ';') { // Delete comments!
71 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
72 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
79 static std::string getEdgeSourceLabel(const BasicBlock *Node,
80 succ_const_iterator I) {
81 // Label source of conditional branches with "T" or "F"
82 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
83 if (BI->isConditional())
84 return (I == succ_begin(Node)) ? "T" : "F";
91 struct CFGPrinter : public FunctionPass {
92 virtual bool runOnFunction(Function &F) {
93 std::string Filename = "cfg." + F.getName() + ".dot";
94 std::cerr << "Writing '" << Filename << "'...";
95 std::ofstream File(Filename.c_str());
98 WriteGraph(File, (const Function*)&F);
100 std::cerr << " error opening file for writing!";
105 void print(std::ostream &OS, const Module* = 0) const {}
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesAll();
112 RegisterAnalysis<CFGPrinter> P1("print-cfg",
113 "Print CFG of function to 'dot' file");
115 struct CFGOnlyPrinter : public CFGPrinter {
116 virtual bool runOnFunction(Function &F) {
117 bool OldCFGOnly = CFGOnly;
119 CFGPrinter::runOnFunction(F);
120 CFGOnly = OldCFGOnly;
123 void print(std::ostream &OS, const Module* = 0) const {}
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
130 RegisterAnalysis<CFGOnlyPrinter>
132 "Print CFG of function to 'dot' file (with no function bodies)");
135 /// viewCFG - This function is meant for use from the debugger. You can just
136 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
137 /// program, displaying the CFG of the current function. This depends on there
138 /// being a 'dot' and 'gv' program in your path.
140 void Function::viewCFG() const {
142 std::string Filename = "/tmp/cfg." + getName() + ".dot";
143 std::cerr << "Writing '" << Filename << "'... ";
144 std::ofstream F(Filename.c_str());
147 std::cerr << " error opening file for writing!\n";
156 std::cerr << "Running 'Graphviz' program... " << std::flush;
157 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
158 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
160 system(("rm " + Filename).c_str());
163 #endif // HAVE_GRAPHVIZ
166 std::cerr << "Running 'dot' program... " << std::flush;
167 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
168 + " > /tmp/cfg.tempgraph.ps").c_str())) {
169 std::cerr << "Error running dot: 'dot' not in path?\n";
172 system("gv /tmp/cfg.tempgraph.ps");
174 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
178 std::cerr << "Function::viewCFG is only available in debug builds on "
179 << "systems with Graphviz or gv!\n";
182 system(("rm " + Filename).c_str());
186 /// viewCFGOnly - This function is meant for use from the debugger. It works
187 /// just like viewCFG, but it does not include the contents of basic blocks
188 /// into the nodes, just the label. If you are only interested in the CFG t
189 /// his can make the graph smaller.
191 void Function::viewCFGOnly() const {
197 FunctionPass *llvm::createCFGPrinterPass () {
198 return new CFGPrinter();
201 FunctionPass *llvm::createCFGOnlyPrinterPass () {
202 return new CFGOnlyPrinter();