1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Analysis/CFGPrinter.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/Compiler.h"
27 struct CFGViewer : public FunctionPass {
28 static char ID; // Pass identifcation, replacement for typeid
29 CFGViewer() : FunctionPass(&ID) {}
31 virtual bool runOnFunction(Function &F) {
36 void print(raw_ostream &OS, const Module* = 0) const {}
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 char CFGViewer::ID = 0;
45 static RegisterPass<CFGViewer>
46 V0("view-cfg", "View CFG of function", false, true);
49 struct CFGOnlyViewer : public FunctionPass {
50 static char ID; // Pass identifcation, replacement for typeid
51 CFGOnlyViewer() : FunctionPass(&ID) {}
53 virtual bool runOnFunction(Function &F) {
58 void print(raw_ostream &OS, const Module* = 0) const {}
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 char CFGOnlyViewer::ID = 0;
67 static RegisterPass<CFGOnlyViewer>
69 "View CFG of function (with no function bodies)", false, true);
72 struct CFGPrinter : public FunctionPass {
73 static char ID; // Pass identification, replacement for typeid
74 CFGPrinter() : FunctionPass(&ID) {}
75 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
77 virtual bool runOnFunction(Function &F) {
78 std::string Filename = "cfg." + F.getNameStr() + ".dot";
79 errs() << "Writing '" << Filename << "'...";
81 std::string ErrorInfo;
82 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
84 if (ErrorInfo.empty())
85 WriteGraph(File, (const Function*)&F);
87 errs() << " error opening file for writing!";
92 void print(raw_ostream &OS, const Module* = 0) const {}
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 char CFGPrinter::ID = 0;
101 static RegisterPass<CFGPrinter>
102 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
105 struct 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 << "'...";
113 std::string ErrorInfo;
114 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
116 if (ErrorInfo.empty())
117 WriteGraph(File, (const Function*)&F, true);
119 errs() << " error opening file for writing!";
123 void print(raw_ostream &OS, const Module* = 0) const {}
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
131 char CFGOnlyPrinter::ID = 0;
132 static RegisterPass<CFGOnlyPrinter>
134 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
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.
141 void Function::viewCFG() const {
142 ViewGraph(this, "cfg" + getNameStr());
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.
150 void Function::viewCFGOnly() const {
151 ViewGraph(this, "cfg" + getNameStr(), true);
154 FunctionPass *llvm::createCFGPrinterPass () {
155 return new CFGPrinter();
158 FunctionPass *llvm::createCFGOnlyPrinterPass () {
159 return new CFGOnlyPrinter();