Change Pass::print to take a raw ostream instead of std::ostream,
[oota-llvm.git] / lib / Analysis / CFGPrinter.cpp
1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a '-dot-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
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/Compiler.h"
27 #include "llvm/Support/GraphWriter.h"
28 #include "llvm/Config/config.h"
29 using namespace llvm;
30
31 namespace llvm {
32 template<>
33 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
34   static std::string getGraphName(const Function *F) {
35     return "CFG for '" + F->getNameStr() + "' function";
36   }
37
38   static std::string getNodeLabel(const BasicBlock *Node,
39                                   const Function *Graph,
40                                   bool ShortNames) {
41     if (ShortNames && !Node->getName().empty())
42       return Node->getNameStr() + ":";
43
44     std::string Str;
45     raw_string_ostream OS(Str);
46
47     if (ShortNames) {
48       WriteAsOperand(OS, Node, false);
49       return OS.str();
50     }
51
52     if (Node->getName().empty()) {
53       WriteAsOperand(OS, Node, false);
54       OS << ":";
55     }
56     
57     OS << *Node;
58     std::string OutStr = OS.str();
59     if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
60
61     // Process string output to make it nicer...
62     for (unsigned i = 0; i != OutStr.length(); ++i)
63       if (OutStr[i] == '\n') {                            // Left justify
64         OutStr[i] = '\\';
65         OutStr.insert(OutStr.begin()+i+1, 'l');
66       } else if (OutStr[i] == ';') {                      // Delete comments!
67         unsigned Idx = OutStr.find('\n', i+1);            // Find end of line
68         OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
69         --i;
70       }
71
72     return OutStr;
73   }
74
75   static std::string getEdgeSourceLabel(const BasicBlock *Node,
76                                         succ_const_iterator I) {
77     // Label source of conditional branches with "T" or "F"
78     if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
79       if (BI->isConditional())
80         return (I == succ_begin(Node)) ? "T" : "F";
81     return "";
82   }
83 };
84 }
85
86 namespace {
87   struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
88     static char ID; // Pass identifcation, replacement for typeid
89     CFGViewer() : FunctionPass(&ID) {}
90
91     virtual bool runOnFunction(Function &F) {
92       F.viewCFG();
93       return false;
94     }
95
96     void print(raw_ostream &OS, const Module* = 0) const {}
97
98     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99       AU.setPreservesAll();
100     }
101   };
102 }
103
104 char CFGViewer::ID = 0;
105 static RegisterPass<CFGViewer>
106 V0("view-cfg", "View CFG of function", false, true);
107
108 namespace {
109   struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
110     static char ID; // Pass identifcation, replacement for typeid
111     CFGOnlyViewer() : FunctionPass(&ID) {}
112
113     virtual bool runOnFunction(Function &F) {
114       F.viewCFG();
115       return false;
116     }
117
118     void print(raw_ostream &OS, const Module* = 0) const {}
119
120     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121       AU.setPreservesAll();
122     }
123   };
124 }
125
126 char CFGOnlyViewer::ID = 0;
127 static RegisterPass<CFGOnlyViewer>
128 V1("view-cfg-only",
129    "View CFG of function (with no function bodies)", false, true);
130
131 namespace {
132   struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
133     static char ID; // Pass identification, replacement for typeid
134     CFGPrinter() : FunctionPass(&ID) {}
135     explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
136
137     virtual bool runOnFunction(Function &F) {
138       std::string Filename = "cfg." + F.getNameStr() + ".dot";
139       cerr << "Writing '" << Filename << "'...";
140       std::ofstream File(Filename.c_str());
141
142       if (File.good())
143         WriteGraph(File, (const Function*)&F);
144       else
145         cerr << "  error opening file for writing!";
146       cerr << "\n";
147       return false;
148     }
149
150     void print(raw_ostream &OS, const Module* = 0) const {}
151
152     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
153       AU.setPreservesAll();
154     }
155   };
156 }
157
158 char CFGPrinter::ID = 0;
159 static RegisterPass<CFGPrinter>
160 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
161
162 namespace {
163   struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
164     static char ID; // Pass identification, replacement for typeid
165     CFGOnlyPrinter() : FunctionPass(&ID) {}
166     explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
167     virtual bool runOnFunction(Function &F) {
168       std::string Filename = "cfg." + F.getNameStr() + ".dot";
169       cerr << "Writing '" << Filename << "'...";
170       std::ofstream File(Filename.c_str());
171
172       if (File.good())
173         WriteGraph(File, (const Function*)&F, true);
174       else
175         cerr << "  error opening file for writing!";
176       cerr << "\n";
177       return false;
178     }
179     void print(raw_ostream &OS, const Module* = 0) const {}
180
181     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
182       AU.setPreservesAll();
183     }
184   };
185 }
186
187 char CFGOnlyPrinter::ID = 0;
188 static RegisterPass<CFGOnlyPrinter>
189 P2("dot-cfg-only",
190    "Print CFG of function to 'dot' file (with no function bodies)", false, true);
191
192 /// viewCFG - This function is meant for use from the debugger.  You can just
193 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
194 /// program, displaying the CFG of the current function.  This depends on there
195 /// being a 'dot' and 'gv' program in your path.
196 ///
197 void Function::viewCFG() const {
198   ViewGraph(this, "cfg" + getNameStr());
199 }
200
201 /// viewCFGOnly - This function is meant for use from the debugger.  It works
202 /// just like viewCFG, but it does not include the contents of basic blocks
203 /// into the nodes, just the label.  If you are only interested in the CFG t
204 /// his can make the graph smaller.
205 ///
206 void Function::viewCFGOnly() const {
207   ViewGraph(this, "cfg" + getNameStr(), true);
208 }
209
210 FunctionPass *llvm::createCFGPrinterPass () {
211   return new CFGPrinter();
212 }
213
214 FunctionPass *llvm::createCFGOnlyPrinterPass () {
215   return new CFGOnlyPrinter();
216 }
217