Fix batch of converting RegisterPass<> to INTIALIZE_PASS().
[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/Analysis/CFGPrinter.h"
21
22 #include "llvm/Pass.h"
23 using namespace llvm;
24
25 namespace {
26   struct CFGViewer : public FunctionPass {
27     static char ID; // Pass identifcation, replacement for typeid
28     CFGViewer() : FunctionPass(&ID) {}
29
30     virtual bool runOnFunction(Function &F) {
31       F.viewCFG();
32       return false;
33     }
34
35     void print(raw_ostream &OS, const Module* = 0) const {}
36
37     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38       AU.setPreservesAll();
39     }
40   };
41 }
42
43 char CFGViewer::ID = 0;
44 INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true);
45
46 namespace {
47   struct CFGOnlyViewer : public FunctionPass {
48     static char ID; // Pass identifcation, replacement for typeid
49     CFGOnlyViewer() : FunctionPass(&ID) {}
50
51     virtual bool runOnFunction(Function &F) {
52       F.viewCFGOnly();
53       return false;
54     }
55
56     void print(raw_ostream &OS, const Module* = 0) const {}
57
58     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59       AU.setPreservesAll();
60     }
61   };
62 }
63
64 char CFGOnlyViewer::ID = 0;
65 static RegisterPass<CFGOnlyViewer>
66 V1("view-cfg-only",
67    "View CFG of function (with no function bodies)", false, true);
68
69 namespace {
70   struct CFGPrinter : public FunctionPass {
71     static char ID; // Pass identification, replacement for typeid
72     CFGPrinter() : FunctionPass(&ID) {}
73     explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
74
75     virtual bool runOnFunction(Function &F) {
76       std::string Filename = "cfg." + F.getNameStr() + ".dot";
77       errs() << "Writing '" << Filename << "'...";
78       
79       std::string ErrorInfo;
80       raw_fd_ostream File(Filename.c_str(), ErrorInfo);
81
82       if (ErrorInfo.empty())
83         WriteGraph(File, (const Function*)&F);
84       else
85         errs() << "  error opening file for writing!";
86       errs() << "\n";
87       return false;
88     }
89
90     void print(raw_ostream &OS, const Module* = 0) const {}
91
92     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93       AU.setPreservesAll();
94     }
95   };
96 }
97
98 char CFGPrinter::ID = 0;
99 static RegisterPass<CFGPrinter>
100 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
101
102 namespace {
103   struct CFGOnlyPrinter : public FunctionPass {
104     static char ID; // Pass identification, replacement for typeid
105     CFGOnlyPrinter() : FunctionPass(&ID) {}
106     explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
107     virtual bool runOnFunction(Function &F) {
108       std::string Filename = "cfg." + F.getNameStr() + ".dot";
109       errs() << "Writing '" << Filename << "'...";
110
111       std::string ErrorInfo;
112       raw_fd_ostream File(Filename.c_str(), ErrorInfo);
113       
114       if (ErrorInfo.empty())
115         WriteGraph(File, (const Function*)&F, true);
116       else
117         errs() << "  error opening file for writing!";
118       errs() << "\n";
119       return false;
120     }
121     void print(raw_ostream &OS, const Module* = 0) const {}
122
123     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124       AU.setPreservesAll();
125     }
126   };
127 }
128
129 char CFGOnlyPrinter::ID = 0;
130 static RegisterPass<CFGOnlyPrinter>
131 P2("dot-cfg-only",
132    "Print CFG of function to 'dot' file (with no function bodies)", false, true);
133
134 /// viewCFG - This function is meant for use from the debugger.  You can just
135 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
136 /// program, displaying the CFG of the current function.  This depends on there
137 /// being a 'dot' and 'gv' program in your path.
138 ///
139 void Function::viewCFG() const {
140   ViewGraph(this, "cfg" + getNameStr());
141 }
142
143 /// viewCFGOnly - This function is meant for use from the debugger.  It works
144 /// just like viewCFG, but it does not include the contents of basic blocks
145 /// into the nodes, just the label.  If you are only interested in the CFG t
146 /// his can make the graph smaller.
147 ///
148 void Function::viewCFGOnly() const {
149   ViewGraph(this, "cfg" + getNameStr(), true);
150 }
151
152 FunctionPass *llvm::createCFGPrinterPass () {
153   return new CFGPrinter();
154 }
155
156 FunctionPass *llvm::createCFGOnlyPrinterPass () {
157   return new CFGOnlyPrinter();
158 }
159