make DOTGraphTraits public, patch by Tobias Grosser!
[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 #include "llvm/Support/Compiler.h"
24 using namespace llvm;
25
26 namespace {
27   struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
28     static char ID; // Pass identifcation, replacement for typeid
29     CFGViewer() : FunctionPass(&ID) {}
30
31     virtual bool runOnFunction(Function &F) {
32       F.viewCFG();
33       return false;
34     }
35
36     void print(raw_ostream &OS, const Module* = 0) const {}
37
38     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39       AU.setPreservesAll();
40     }
41   };
42 }
43
44 char CFGViewer::ID = 0;
45 static RegisterPass<CFGViewer>
46 V0("view-cfg", "View CFG of function", false, true);
47
48 namespace {
49   struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
50     static char ID; // Pass identifcation, replacement for typeid
51     CFGOnlyViewer() : FunctionPass(&ID) {}
52
53     virtual bool runOnFunction(Function &F) {
54       F.viewCFGOnly();
55       return false;
56     }
57
58     void print(raw_ostream &OS, const Module* = 0) const {}
59
60     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61       AU.setPreservesAll();
62     }
63   };
64 }
65
66 char CFGOnlyViewer::ID = 0;
67 static RegisterPass<CFGOnlyViewer>
68 V1("view-cfg-only",
69    "View CFG of function (with no function bodies)", false, true);
70
71 namespace {
72   struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
73     static char ID; // Pass identification, replacement for typeid
74     CFGPrinter() : FunctionPass(&ID) {}
75     explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
76
77     virtual bool runOnFunction(Function &F) {
78       std::string Filename = "cfg." + F.getNameStr() + ".dot";
79       errs() << "Writing '" << Filename << "'...";
80       
81       std::string ErrorInfo;
82       raw_fd_ostream File(Filename.c_str(), ErrorInfo);
83
84       if (ErrorInfo.empty())
85         WriteGraph(File, (const Function*)&F);
86       else
87         errs() << "  error opening file for writing!";
88       errs() << "\n";
89       return false;
90     }
91
92     void print(raw_ostream &OS, const Module* = 0) const {}
93
94     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
95       AU.setPreservesAll();
96     }
97   };
98 }
99
100 char CFGPrinter::ID = 0;
101 static RegisterPass<CFGPrinter>
102 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
103
104 namespace {
105   struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
106     static char ID; // Pass identification, replacement for typeid
107     CFGOnlyPrinter() : FunctionPass(&ID) {}
108     explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
109     virtual bool runOnFunction(Function &F) {
110       std::string Filename = "cfg." + F.getNameStr() + ".dot";
111       errs() << "Writing '" << Filename << "'...";
112
113       std::string ErrorInfo;
114       raw_fd_ostream File(Filename.c_str(), ErrorInfo);
115       
116       if (ErrorInfo.empty())
117         WriteGraph(File, (const Function*)&F, true);
118       else
119         errs() << "  error opening file for writing!";
120       errs() << "\n";
121       return false;
122     }
123     void print(raw_ostream &OS, const Module* = 0) const {}
124
125     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126       AU.setPreservesAll();
127     }
128   };
129 }
130
131 char CFGOnlyPrinter::ID = 0;
132 static RegisterPass<CFGOnlyPrinter>
133 P2("dot-cfg-only",
134    "Print CFG of function to 'dot' file (with no function bodies)", false, true);
135
136 /// 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 ///
141 void Function::viewCFG() const {
142   ViewGraph(this, "cfg" + getNameStr());
143 }
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 ///
150 void Function::viewCFGOnly() const {
151   ViewGraph(this, "cfg" + getNameStr(), true);
152 }
153
154 FunctionPass *llvm::createCFGPrinterPass () {
155   return new CFGPrinter();
156 }
157
158 FunctionPass *llvm::createCFGOnlyPrinterPass () {
159   return new CFGOnlyPrinter();
160 }
161