blob: f16dca833cd5fb17f241e3c470debb83291d71fd [file] [log] [blame]
Chris Lattner002362c2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner002362c2003-10-22 16:03:49 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner002362c2003-10-22 16:03:49 +00008//===----------------------------------------------------------------------===//
9//
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.
13//
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
16// on the CFG.
17//
18//===----------------------------------------------------------------------===//
19
Chris Lattner002362c2003-10-22 16:03:49 +000020#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000021#include "llvm/Instructions.h"
Misha Brukmanbf94a1e2004-04-29 04:04:47 +000022#include "llvm/Pass.h"
Brian Gaekec6e2d8a2004-04-26 16:27:08 +000023#include "llvm/Analysis/CFGPrinter.h"
Misha Brukmanbf94a1e2004-04-29 04:04:47 +000024#include "llvm/Assembly/Writer.h"
Chris Lattner002362c2003-10-22 16:03:49 +000025#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/GraphWriter.h"
Chris Lattnerb06f6772005-08-03 17:55:05 +000027#include "llvm/Config/config.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000028#include <iosfwd>
Chris Lattner002362c2003-10-22 16:03:49 +000029#include <sstream>
30#include <fstream>
Chris Lattner1ca2a582003-12-11 21:48:18 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner002362c2003-10-22 16:03:49 +000033/// CFGOnly flag - This is used to control whether or not the CFG graph printer
34/// prints out the contents of basic blocks or not. This is acceptable because
35/// this code is only really used for debugging purposes.
36///
37static bool CFGOnly = false;
38
Chris Lattner1ca2a582003-12-11 21:48:18 +000039namespace llvm {
Chris Lattner002362c2003-10-22 16:03:49 +000040template<>
41struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
42 static std::string getGraphName(const Function *F) {
43 return "CFG for '" + F->getName() + "' function";
44 }
45
46 static std::string getNodeLabel(const BasicBlock *Node,
47 const Function *Graph) {
Chris Lattnerd073ea02003-10-22 16:30:58 +000048 if (CFGOnly && !Node->getName().empty())
49 return Node->getName() + ":";
Chris Lattner002362c2003-10-22 16:03:49 +000050
51 std::ostringstream Out;
Chris Lattnera75b3ef2003-10-22 16:22:42 +000052 if (CFGOnly) {
Chris Lattner3749c9c2006-12-06 06:16:21 +000053 WriteAsOperand(Out, Node, false);
Chris Lattnera75b3ef2003-10-22 16:22:42 +000054 return Out.str();
55 }
56
Chris Lattnerd073ea02003-10-22 16:30:58 +000057 if (Node->getName().empty()) {
Chris Lattner3749c9c2006-12-06 06:16:21 +000058 WriteAsOperand(Out, Node, false);
Chris Lattnerd073ea02003-10-22 16:30:58 +000059 Out << ":";
60 }
61
Chris Lattner002362c2003-10-22 16:03:49 +000062 Out << *Node;
63 std::string OutStr = Out.str();
64 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
65
66 // Process string output to make it nicer...
67 for (unsigned i = 0; i != OutStr.length(); ++i)
68 if (OutStr[i] == '\n') { // Left justify
69 OutStr[i] = '\\';
70 OutStr.insert(OutStr.begin()+i+1, 'l');
71 } else if (OutStr[i] == ';') { // Delete comments!
72 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
73 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
74 --i;
75 }
76
77 return OutStr;
78 }
79
Chris Lattner002362c2003-10-22 16:03:49 +000080 static std::string getEdgeSourceLabel(const BasicBlock *Node,
81 succ_const_iterator I) {
82 // Label source of conditional branches with "T" or "F"
83 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
84 if (BI->isConditional())
85 return (I == succ_begin(Node)) ? "T" : "F";
86 return "";
87 }
88};
Chris Lattner1ca2a582003-12-11 21:48:18 +000089}
Chris Lattner002362c2003-10-22 16:03:49 +000090
91namespace {
92 struct CFGPrinter : public FunctionPass {
93 virtual bool runOnFunction(Function &F) {
94 std::string Filename = "cfg." + F.getName() + ".dot";
Bill Wendlinge8156192006-12-07 01:30:32 +000095 cerr << "Writing '" << Filename << "'...";
Chris Lattner002362c2003-10-22 16:03:49 +000096 std::ofstream File(Filename.c_str());
Misha Brukman2b37d7c2005-04-21 21:13:18 +000097
Chris Lattner002362c2003-10-22 16:03:49 +000098 if (File.good())
99 WriteGraph(File, (const Function*)&F);
100 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000101 cerr << " error opening file for writing!";
102 cerr << "\n";
Chris Lattner002362c2003-10-22 16:03:49 +0000103 return false;
104 }
105
Reid Spencerce9653c2004-12-07 04:03:45 +0000106 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000107
Chris Lattner002362c2003-10-22 16:03:49 +0000108 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.setPreservesAll();
110 }
111 };
112
Chris Lattner5d8925c2006-08-27 22:30:17 +0000113 RegisterPass<CFGPrinter> P1("print-cfg",
114 "Print CFG of function to 'dot' file");
Chris Lattner1ca2a582003-12-11 21:48:18 +0000115
116 struct CFGOnlyPrinter : public CFGPrinter {
117 virtual bool runOnFunction(Function &F) {
118 bool OldCFGOnly = CFGOnly;
119 CFGOnly = true;
120 CFGPrinter::runOnFunction(F);
121 CFGOnly = OldCFGOnly;
122 return false;
123 }
Reid Spencerce9653c2004-12-07 04:03:45 +0000124 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000125
Chris Lattner1ca2a582003-12-11 21:48:18 +0000126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
127 AU.setPreservesAll();
128 }
129 };
130
Chris Lattner5d8925c2006-08-27 22:30:17 +0000131 RegisterPass<CFGOnlyPrinter>
Chris Lattner1ca2a582003-12-11 21:48:18 +0000132 P2("print-cfg-only",
133 "Print CFG of function to 'dot' file (with no function bodies)");
134}
Chris Lattner002362c2003-10-22 16:03:49 +0000135
Chris Lattner002362c2003-10-22 16:03:49 +0000136/// viewCFG - This function is meant for use from the debugger. You can just
137/// say 'call F->viewCFG()' and a ghostview window should pop up from the
138/// program, displaying the CFG of the current function. This depends on there
139/// being a 'dot' and 'gv' program in your path.
140///
141void Function::viewCFG() const {
Reid Spencer9d5b5322006-06-27 16:49:46 +0000142 ViewGraph(this, "cfg" + getName());
Chris Lattner002362c2003-10-22 16:03:49 +0000143}
144
145/// viewCFGOnly - This function is meant for use from the debugger. It works
146/// just like viewCFG, but it does not include the contents of basic blocks
147/// into the nodes, just the label. If you are only interested in the CFG t
148/// his can make the graph smaller.
149///
150void Function::viewCFGOnly() const {
151 CFGOnly = true;
152 viewCFG();
153 CFGOnly = false;
154}
Brian Gaekec6e2d8a2004-04-26 16:27:08 +0000155
156FunctionPass *llvm::createCFGPrinterPass () {
157 return new CFGPrinter();
158}
159
160FunctionPass *llvm::createCFGOnlyPrinterPass () {
161 return new CFGOnlyPrinter();
162}
163