blob: c16f3286df2c48c802bac23f57536a5ecbb61de9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Duncan Sandsd10d6f72008-09-23 12:47:39 +000010// This file defines a '-dot-cfg' analysis pass, which emits the
Dan Gohmanf17a25c2007-07-18 16:29:46 +000011// 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Analysis/CFGPrinter.h"
Chris Lattner2f331142009-10-18 04:09:11 +000021
22#include "llvm/Pass.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000026 struct CFGViewer : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000028 CFGViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000029
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 virtual bool runOnFunction(Function &F) {
31 F.viewCFG();
32 return false;
33 }
34
Chris Lattner397f4562009-08-23 06:03:38 +000035 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
39 }
40 };
Dan Gohman089efff2008-05-13 00:00:25 +000041}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
Dan Gohman089efff2008-05-13 00:00:25 +000043char CFGViewer::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +000044INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
Dan Gohman089efff2008-05-13 00:00:25 +000046namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000047 struct CFGOnlyViewer : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000049 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000050
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 virtual bool runOnFunction(Function &F) {
Duncan Sandsd899f592009-09-19 11:25:44 +000052 F.viewCFGOnly();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 return false;
54 }
55
Chris Lattner397f4562009-08-23 06:03:38 +000056 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
61 };
Dan Gohman089efff2008-05-13 00:00:25 +000062}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
Dan Gohman089efff2008-05-13 00:00:25 +000064char CFGOnlyViewer::ID = 0;
65static RegisterPass<CFGOnlyViewer>
66V1("view-cfg-only",
67 "View CFG of function (with no function bodies)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
Dan Gohman089efff2008-05-13 00:00:25 +000069namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000070 struct CFGPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000072 CFGPrinter() : FunctionPass(&ID) {}
73 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000074
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +000076 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000077 errs() << "Writing '" << Filename << "'...";
78
79 std::string ErrorInfo;
Dan Gohman176426d2009-08-25 15:34:52 +000080 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000082 if (ErrorInfo.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 WriteGraph(File, (const Function*)&F);
84 else
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000085 errs() << " error opening file for writing!";
86 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 return false;
88 }
89
Chris Lattner397f4562009-08-23 06:03:38 +000090 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.setPreservesAll();
94 }
95 };
Dan Gohman089efff2008-05-13 00:00:25 +000096}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
Dan Gohman089efff2008-05-13 00:00:25 +000098char CFGPrinter::ID = 0;
99static RegisterPass<CFGPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000100P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101
Dan Gohman089efff2008-05-13 00:00:25 +0000102namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000103 struct CFGOnlyPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 static char ID; // Pass identification, replacement for typeid
Owen Andersonf4a15462009-06-24 17:37:09 +0000105 CFGOnlyPrinter() : FunctionPass(&ID) {}
106 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000108 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000109 errs() << "Writing '" << Filename << "'...";
Owen Andersonf4a15462009-06-24 17:37:09 +0000110
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000111 std::string ErrorInfo;
Dan Gohman176426d2009-08-25 15:34:52 +0000112 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000113
114 if (ErrorInfo.empty())
Owen Andersonf4a15462009-06-24 17:37:09 +0000115 WriteGraph(File, (const Function*)&F, true);
116 else
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000117 errs() << " error opening file for writing!";
118 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 return false;
120 }
Chris Lattner397f4562009-08-23 06:03:38 +0000121 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesAll();
125 }
126 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127}
128
Dan Gohman089efff2008-05-13 00:00:25 +0000129char CFGOnlyPrinter::ID = 0;
130static RegisterPass<CFGOnlyPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000131P2("dot-cfg-only",
Dan Gohman089efff2008-05-13 00:00:25 +0000132 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134/// 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///
139void Function::viewCFG() const {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000140 ViewGraph(this, "cfg" + getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141}
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///
148void Function::viewCFGOnly() const {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000149 ViewGraph(this, "cfg" + getNameStr(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150}
151
152FunctionPass *llvm::createCFGPrinterPass () {
153 return new CFGPrinter();
154}
155
156FunctionPass *llvm::createCFGOnlyPrinterPass () {
157 return new CFGOnlyPrinter();
158}
159