bb2912b060e4885bb3ce6315a8cabf23b58e8d19
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Support/GraphWriter.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Function.h"
23 #include "llvm/iTerminators.h"
24 #include "llvm/Support/CFG.h"
25 #include <sstream>
26 #include <fstream>
27
28 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
29 /// prints out the contents of basic blocks or not.  This is acceptable because
30 /// this code is only really used for debugging purposes.
31 ///
32 static bool CFGOnly = false;
33
34 template<>
35 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
36   static std::string getGraphName(const Function *F) {
37     return "CFG for '" + F->getName() + "' function";
38   }
39
40   static std::string getNodeLabel(const BasicBlock *Node,
41                                   const Function *Graph) {
42     if (CFGOnly) return Node->getName() + ":";
43
44     std::ostringstream Out;
45     Out << *Node;
46     std::string OutStr = Out.str();
47     if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
48
49     // Process string output to make it nicer...
50     for (unsigned i = 0; i != OutStr.length(); ++i)
51       if (OutStr[i] == '\n') {                            // Left justify
52         OutStr[i] = '\\';
53         OutStr.insert(OutStr.begin()+i+1, 'l');
54       } else if (OutStr[i] == ';') {                      // Delete comments!
55         unsigned Idx = OutStr.find('\n', i+1);            // Find end of line
56         OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
57         --i;
58       }
59
60     return OutStr;
61   }
62
63   static std::string getNodeAttributes(const BasicBlock *N) {
64     return "fontname=Courier";
65   }
66   
67   static std::string getEdgeSourceLabel(const BasicBlock *Node,
68                                         succ_const_iterator I) {
69     // Label source of conditional branches with "T" or "F"
70     if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
71       if (BI->isConditional())
72         return (I == succ_begin(Node)) ? "T" : "F";
73     return "";
74   }
75 };
76
77 namespace {
78   struct CFGPrinter : public FunctionPass {
79     virtual bool runOnFunction(Function &F) {
80       std::string Filename = "cfg." + F.getName() + ".dot";
81       std::cerr << "Writing '" << Filename << "'...";
82       std::ofstream File(Filename.c_str());
83       
84       if (File.good())
85         WriteGraph(File, (const Function*)&F);
86       else
87         std::cerr << "  error opening file for writing!";
88       std::cerr << "\n";
89       return false;
90     }
91
92     void print(std::ostream &OS) const {}
93     
94     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
95       AU.setPreservesAll();
96     }
97   };
98
99   RegisterAnalysis<CFGPrinter> P1("print-cfg",
100                                   "Print CFG of function to 'dot' file");
101 };
102
103
104
105
106 /// viewCFG - This function is meant for use from the debugger.  You can just
107 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
108 /// program, displaying the CFG of the current function.  This depends on there
109 /// being a 'dot' and 'gv' program in your path.
110 ///
111 void Function::viewCFG() const {
112   std::string Filename = "/tmp/cfg." + getName() + ".dot";
113   std::cerr << "Writing '" << Filename << "'... ";
114   std::ofstream F(Filename.c_str());
115   
116   if (!F.good()) {
117     std::cerr << "  error opening file for writing!\n";
118     return;
119   }
120
121   WriteGraph(F, this);
122   F.close();
123   std::cerr << "\n";
124
125   std::cerr << "Running 'dot' program... " << std::flush;
126   if (system(("dot -Tps " + Filename + " > /tmp/cfg.tempgraph.ps").c_str())) {
127     std::cerr << "Error running dot: 'dot' not in path?\n";
128   } else {
129     std::cerr << "\n";
130     system("gv /tmp/cfg.tempgraph.ps");
131   }
132   system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
133 }
134
135 /// viewCFGOnly - This function is meant for use from the debugger.  It works
136 /// just like viewCFG, but it does not include the contents of basic blocks
137 /// into the nodes, just the label.  If you are only interested in the CFG t
138 /// his can make the graph smaller.
139 ///
140 void Function::viewCFGOnly() const {
141   CFGOnly = true;
142   viewCFG();
143   CFGOnly = false;
144 }