blob: 67799965556b4b0202cb185778b238b42a667a8c [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 +000023#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000027 struct CFGViewer : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000029 CFGViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000030
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 virtual bool runOnFunction(Function &F) {
32 F.viewCFG();
33 return false;
34 }
35
Chris Lattner397f4562009-08-23 06:03:38 +000036 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesAll();
40 }
41 };
Dan Gohman089efff2008-05-13 00:00:25 +000042}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
Dan Gohman089efff2008-05-13 00:00:25 +000044char CFGViewer::ID = 0;
45static RegisterPass<CFGViewer>
46V0("view-cfg", "View CFG of function", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Dan Gohman089efff2008-05-13 00:00:25 +000048namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000049 struct CFGOnlyViewer : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000051 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000052
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 virtual bool runOnFunction(Function &F) {
Duncan Sandsd899f592009-09-19 11:25:44 +000054 F.viewCFGOnly();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 return false;
56 }
57
Chris Lattner397f4562009-08-23 06:03:38 +000058 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 AU.setPreservesAll();
62 }
63 };
Dan Gohman089efff2008-05-13 00:00:25 +000064}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
Dan Gohman089efff2008-05-13 00:00:25 +000066char CFGOnlyViewer::ID = 0;
67static RegisterPass<CFGOnlyViewer>
68V1("view-cfg-only",
69 "View CFG of function (with no function bodies)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Dan Gohman089efff2008-05-13 00:00:25 +000071namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000072 struct CFGPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000074 CFGPrinter() : FunctionPass(&ID) {}
75 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000076
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +000078 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000079 errs() << "Writing '" << Filename << "'...";
80
81 std::string ErrorInfo;
Dan Gohman176426d2009-08-25 15:34:52 +000082 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000084 if (ErrorInfo.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 WriteGraph(File, (const Function*)&F);
86 else
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000087 errs() << " error opening file for writing!";
88 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 return false;
90 }
91
Chris Lattner397f4562009-08-23 06:03:38 +000092 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
95 AU.setPreservesAll();
96 }
97 };
Dan Gohman089efff2008-05-13 00:00:25 +000098}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Dan Gohman089efff2008-05-13 00:00:25 +0000100char CFGPrinter::ID = 0;
101static RegisterPass<CFGPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000102P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
Dan Gohman089efff2008-05-13 00:00:25 +0000104namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000105 struct CFGOnlyPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 static char ID; // Pass identification, replacement for typeid
Owen Andersonf4a15462009-06-24 17:37:09 +0000107 CFGOnlyPrinter() : FunctionPass(&ID) {}
108 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000110 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000111 errs() << "Writing '" << Filename << "'...";
Owen Andersonf4a15462009-06-24 17:37:09 +0000112
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000113 std::string ErrorInfo;
Dan Gohman176426d2009-08-25 15:34:52 +0000114 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000115
116 if (ErrorInfo.empty())
Owen Andersonf4a15462009-06-24 17:37:09 +0000117 WriteGraph(File, (const Function*)&F, true);
118 else
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000119 errs() << " error opening file for writing!";
120 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 return false;
122 }
Chris Lattner397f4562009-08-23 06:03:38 +0000123 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
127 }
128 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129}
130
Dan Gohman089efff2008-05-13 00:00:25 +0000131char CFGOnlyPrinter::ID = 0;
132static RegisterPass<CFGOnlyPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000133P2("dot-cfg-only",
Dan Gohman089efff2008-05-13 00:00:25 +0000134 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136/// 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.
140///
141void Function::viewCFG() const {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000142 ViewGraph(this, "cfg" + getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143}
144
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.
149///
150void Function::viewCFGOnly() const {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000151 ViewGraph(this, "cfg" + getNameStr(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152}
153
154FunctionPass *llvm::createCFGPrinterPass () {
155 return new CFGPrinter();
156}
157
158FunctionPass *llvm::createCFGOnlyPrinterPass () {
159 return new CFGOnlyPrinter();
160}
161