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