blob: da2f0a6dbf0c31917b78133476d10e188339f061 [file] [log] [blame]
Chris Lattner002362c2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner002362c2003-10-22 16:03:49 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner002362c2003-10-22 16:03:49 +00008//===----------------------------------------------------------------------===//
9//
Duncan Sands3ee8fc92008-09-23 12:47:39 +000010// This file defines a '-dot-cfg' analysis pass, which emits the
Chris Lattner002362c2003-10-22 16:03:49 +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
Brian Gaekec6e2d8a2004-04-26 16:27:08 +000020#include "llvm/Analysis/CFGPrinter.h"
Chris Lattner88067b92009-10-18 04:09:11 +000021
22#include "llvm/Pass.h"
Chris Lattner1ca2a582003-12-11 21:48:18 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner002362c2003-10-22 16:03:49 +000025namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000026 struct CFGViewer : public FunctionPass {
Dan Gohmana196b992007-05-14 14:25:08 +000027 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson1f745902010-08-06 00:23:35 +000028 CFGViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000029
Dan Gohmana196b992007-05-14 14:25:08 +000030 virtual bool runOnFunction(Function &F) {
31 F.viewCFG();
32 return false;
33 }
34
Chris Lattner45cfe542009-08-23 06:03:38 +000035 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +000036
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll();
39 }
40 };
Dan Gohman844731a2008-05-13 00:00:25 +000041}
Dan Gohmana196b992007-05-14 14:25:08 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043char CFGViewer::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000044INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +000045
Dan Gohman844731a2008-05-13 00:00:25 +000046namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000047 struct CFGOnlyViewer : public FunctionPass {
Dan Gohmana196b992007-05-14 14:25:08 +000048 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson1f745902010-08-06 00:23:35 +000049 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000050
Dan Gohmana196b992007-05-14 14:25:08 +000051 virtual bool runOnFunction(Function &F) {
Duncan Sandsad389362009-09-19 11:25:44 +000052 F.viewCFGOnly();
Dan Gohmana196b992007-05-14 14:25:08 +000053 return false;
54 }
55
Chris Lattner45cfe542009-08-23 06:03:38 +000056 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +000057
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
61 };
Dan Gohman844731a2008-05-13 00:00:25 +000062}
Dan Gohmana196b992007-05-14 14:25:08 +000063
Dan Gohman844731a2008-05-13 00:00:25 +000064char CFGOnlyViewer::ID = 0;
Owen Andersond8cc7be2010-07-21 23:07:00 +000065INITIALIZE_PASS(CFGOnlyViewer, "view-cfg-only",
66 "View CFG of function (with no function bodies)", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +000067
Dan Gohman844731a2008-05-13 00:00:25 +000068namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000069 struct CFGPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000070 static char ID; // Pass identification, replacement for typeid
Owen Anderson1f745902010-08-06 00:23:35 +000071 CFGPrinter() : FunctionPass(&ID) {}
72 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000073
Chris Lattner002362c2003-10-22 16:03:49 +000074 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000075 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattner103289e2009-08-23 07:19:13 +000076 errs() << "Writing '" << Filename << "'...";
77
78 std::string ErrorInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +000079 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000080
Chris Lattner103289e2009-08-23 07:19:13 +000081 if (ErrorInfo.empty())
Chris Lattner002362c2003-10-22 16:03:49 +000082 WriteGraph(File, (const Function*)&F);
83 else
Chris Lattner103289e2009-08-23 07:19:13 +000084 errs() << " error opening file for writing!";
85 errs() << "\n";
Chris Lattner002362c2003-10-22 16:03:49 +000086 return false;
87 }
88
Chris Lattner45cfe542009-08-23 06:03:38 +000089 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +000090
Chris Lattner002362c2003-10-22 16:03:49 +000091 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
92 AU.setPreservesAll();
93 }
94 };
Dan Gohman844731a2008-05-13 00:00:25 +000095}
Chris Lattner002362c2003-10-22 16:03:49 +000096
Dan Gohman844731a2008-05-13 00:00:25 +000097char CFGPrinter::ID = 0;
98static RegisterPass<CFGPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +000099P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Chris Lattner1ca2a582003-12-11 21:48:18 +0000100
Dan Gohman844731a2008-05-13 00:00:25 +0000101namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000102 struct CFGOnlyPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000103 static char ID; // Pass identification, replacement for typeid
Owen Anderson1f745902010-08-06 00:23:35 +0000104 CFGOnlyPrinter() : FunctionPass(&ID) {}
105 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Chris Lattner1ca2a582003-12-11 21:48:18 +0000106 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000107 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattner103289e2009-08-23 07:19:13 +0000108 errs() << "Writing '" << Filename << "'...";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000109
Chris Lattner103289e2009-08-23 07:19:13 +0000110 std::string ErrorInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +0000111 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Chris Lattner103289e2009-08-23 07:19:13 +0000112
113 if (ErrorInfo.empty())
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000114 WriteGraph(File, (const Function*)&F, true);
115 else
Chris Lattner103289e2009-08-23 07:19:13 +0000116 errs() << " error opening file for writing!";
117 errs() << "\n";
Chris Lattner1ca2a582003-12-11 21:48:18 +0000118 return false;
119 }
Chris Lattner45cfe542009-08-23 06:03:38 +0000120 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000121
Chris Lattner1ca2a582003-12-11 21:48:18 +0000122 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
123 AU.setPreservesAll();
124 }
125 };
Chris Lattner1ca2a582003-12-11 21:48:18 +0000126}
Chris Lattner002362c2003-10-22 16:03:49 +0000127
Dan Gohman844731a2008-05-13 00:00:25 +0000128char CFGOnlyPrinter::ID = 0;
129static RegisterPass<CFGOnlyPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +0000130P2("dot-cfg-only",
Dan Gohman844731a2008-05-13 00:00:25 +0000131 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
132
Chris Lattner002362c2003-10-22 16:03:49 +0000133/// viewCFG - This function is meant for use from the debugger. You can just
134/// say 'call F->viewCFG()' and a ghostview window should pop up from the
135/// program, displaying the CFG of the current function. This depends on there
136/// being a 'dot' and 'gv' program in your path.
137///
138void Function::viewCFG() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000139 ViewGraph(this, "cfg" + getNameStr());
Chris Lattner002362c2003-10-22 16:03:49 +0000140}
141
142/// viewCFGOnly - This function is meant for use from the debugger. It works
143/// just like viewCFG, but it does not include the contents of basic blocks
144/// into the nodes, just the label. If you are only interested in the CFG t
145/// his can make the graph smaller.
146///
147void Function::viewCFGOnly() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000148 ViewGraph(this, "cfg" + getNameStr(), true);
Chris Lattner002362c2003-10-22 16:03:49 +0000149}
Brian Gaekec6e2d8a2004-04-26 16:27:08 +0000150
151FunctionPass *llvm::createCFGPrinterPass () {
152 return new CFGPrinter();
153}
154
155FunctionPass *llvm::createCFGOnlyPrinterPass () {
156 return new CFGOnlyPrinter();
157}
158