blob: 3c67618450ce7cd810349731ab84ae8318beb6f3 [file] [log] [blame]
Chris Lattnera93d11b2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chris Lattnera93d11b2003-10-22 16:03:49 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
Chris Lattnera93d11b2003-10-22 16:03:49 +00008//===----------------------------------------------------------------------===//
9//
Duncan Sands9c40c282008-09-23 12:47:39 +000010// This file defines a '-dot-cfg' analysis pass, which emits the
Chris Lattnera93d11b2003-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 Gaeke104341f2004-04-26 16:27:08 +000020#include "llvm/Analysis/CFGPrinter.h"
Chris Lattnerda424262009-10-18 04:09:11 +000021#include "llvm/Pass.h"
Chris Lattner62aff842003-12-11 21:48:18 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattnera93d11b2003-10-22 16:03:49 +000024namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000025 struct CFGViewer : public FunctionPass {
Dan Gohman90d97ac2007-05-14 14:25:08 +000026 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000027 CFGViewer() : FunctionPass(ID) {
28 initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
29 }
Devang Patel864970e2008-03-18 00:39:19 +000030
Dan Gohman90d97ac2007-05-14 14:25:08 +000031 virtual bool runOnFunction(Function &F) {
32 F.viewCFG();
33 return false;
34 }
35
Chris Lattner13626022009-08-23 06:03:38 +000036 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohman90d97ac2007-05-14 14:25:08 +000037
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesAll();
40 }
41 };
Dan Gohmand78c4002008-05-13 00:00:25 +000042}
Dan Gohman90d97ac2007-05-14 14:25:08 +000043
Dan Gohmand78c4002008-05-13 00:00:25 +000044char CFGViewer::ID = 0;
Owen Andersondf7a4f22010-10-07 22:25:06 +000045INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true)
Dan Gohman90d97ac2007-05-14 14:25:08 +000046
Dan Gohmand78c4002008-05-13 00:00:25 +000047namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000048 struct CFGOnlyViewer : public FunctionPass {
Dan Gohman90d97ac2007-05-14 14:25:08 +000049 static char ID; // Pass identifcation, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000050 CFGOnlyViewer() : FunctionPass(ID) {
51 initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry());
52 }
Devang Patel864970e2008-03-18 00:39:19 +000053
Dan Gohman90d97ac2007-05-14 14:25:08 +000054 virtual bool runOnFunction(Function &F) {
Duncan Sands1636b7e2009-09-19 11:25:44 +000055 F.viewCFGOnly();
Dan Gohman90d97ac2007-05-14 14:25:08 +000056 return false;
57 }
58
Chris Lattner13626022009-08-23 06:03:38 +000059 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohman90d97ac2007-05-14 14:25:08 +000060
61 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.setPreservesAll();
63 }
64 };
Dan Gohmand78c4002008-05-13 00:00:25 +000065}
Dan Gohman90d97ac2007-05-14 14:25:08 +000066
Dan Gohmand78c4002008-05-13 00:00:25 +000067char CFGOnlyViewer::ID = 0;
Owen Andersonac4a1ed2010-07-21 23:07:00 +000068INITIALIZE_PASS(CFGOnlyViewer, "view-cfg-only",
Owen Andersondf7a4f22010-10-07 22:25:06 +000069 "View CFG of function (with no function bodies)", false, true)
Dan Gohman90d97ac2007-05-14 14:25:08 +000070
Dan Gohmand78c4002008-05-13 00:00:25 +000071namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000072 struct CFGPrinter : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000073 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000074 CFGPrinter() : FunctionPass(ID) {
75 initializeCFGPrinterPass(*PassRegistry::getPassRegistry());
76 }
Devang Patel864970e2008-03-18 00:39:19 +000077
Chris Lattnera93d11b2003-10-22 16:03:49 +000078 virtual bool runOnFunction(Function &F) {
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +000079 std::string Filename = "cfg." + F.getName().str() + ".dot";
Chris Lattner4883d902009-08-23 07:19:13 +000080 errs() << "Writing '" << Filename << "'...";
81
82 std::string ErrorInfo;
Rafael Espindola7dbcdd02014-02-24 15:07:20 +000083 raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_None);
Misha Brukman01808ca2005-04-21 21:13:18 +000084
Chris Lattner4883d902009-08-23 07:19:13 +000085 if (ErrorInfo.empty())
Chris Lattnera93d11b2003-10-22 16:03:49 +000086 WriteGraph(File, (const Function*)&F);
87 else
Chris Lattner4883d902009-08-23 07:19:13 +000088 errs() << " error opening file for writing!";
89 errs() << "\n";
Chris Lattnera93d11b2003-10-22 16:03:49 +000090 return false;
91 }
92
Chris Lattner13626022009-08-23 06:03:38 +000093 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +000094
Chris Lattnera93d11b2003-10-22 16:03:49 +000095 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
96 AU.setPreservesAll();
97 }
98 };
Dan Gohmand78c4002008-05-13 00:00:25 +000099}
Chris Lattnera93d11b2003-10-22 16:03:49 +0000100
Dan Gohmand78c4002008-05-13 00:00:25 +0000101char CFGPrinter::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000102INITIALIZE_PASS(CFGPrinter, "dot-cfg", "Print CFG of function to 'dot' file",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000103 false, true)
Chris Lattner62aff842003-12-11 21:48:18 +0000104
Dan Gohmand78c4002008-05-13 00:00:25 +0000105namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +0000106 struct CFGOnlyPrinter : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000107 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000108 CFGOnlyPrinter() : FunctionPass(ID) {
109 initializeCFGOnlyPrinterPass(*PassRegistry::getPassRegistry());
110 }
111
Chris Lattner62aff842003-12-11 21:48:18 +0000112 virtual bool runOnFunction(Function &F) {
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000113 std::string Filename = "cfg." + F.getName().str() + ".dot";
Chris Lattner4883d902009-08-23 07:19:13 +0000114 errs() << "Writing '" << Filename << "'...";
Owen Andersonb70adf22009-06-24 17:37:09 +0000115
Chris Lattner4883d902009-08-23 07:19:13 +0000116 std::string ErrorInfo;
Rafael Espindola7dbcdd02014-02-24 15:07:20 +0000117 raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_None);
Chris Lattner4883d902009-08-23 07:19:13 +0000118
119 if (ErrorInfo.empty())
Owen Andersonb70adf22009-06-24 17:37:09 +0000120 WriteGraph(File, (const Function*)&F, true);
121 else
Chris Lattner4883d902009-08-23 07:19:13 +0000122 errs() << " error opening file for writing!";
123 errs() << "\n";
Chris Lattner62aff842003-12-11 21:48:18 +0000124 return false;
125 }
Chris Lattner13626022009-08-23 06:03:38 +0000126 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +0000127
Chris Lattner62aff842003-12-11 21:48:18 +0000128 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
129 AU.setPreservesAll();
130 }
131 };
Chris Lattner62aff842003-12-11 21:48:18 +0000132}
Chris Lattnera93d11b2003-10-22 16:03:49 +0000133
Dan Gohmand78c4002008-05-13 00:00:25 +0000134char CFGOnlyPrinter::ID = 0;
Owen Andersond31d82d2010-08-23 17:52:01 +0000135INITIALIZE_PASS(CFGOnlyPrinter, "dot-cfg-only",
136 "Print CFG of function to 'dot' file (with no function bodies)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000137 false, true)
Dan Gohmand78c4002008-05-13 00:00:25 +0000138
Chris Lattnera93d11b2003-10-22 16:03:49 +0000139/// viewCFG - This function is meant for use from the debugger. You can just
140/// say 'call F->viewCFG()' and a ghostview window should pop up from the
141/// program, displaying the CFG of the current function. This depends on there
142/// being a 'dot' and 'gv' program in your path.
143///
144void Function::viewCFG() const {
Benjamin Kramer4c93d152011-11-15 16:26:38 +0000145 ViewGraph(this, "cfg" + getName());
Chris Lattnera93d11b2003-10-22 16:03:49 +0000146}
147
148/// viewCFGOnly - This function is meant for use from the debugger. It works
149/// just like viewCFG, but it does not include the contents of basic blocks
150/// into the nodes, just the label. If you are only interested in the CFG t
151/// his can make the graph smaller.
152///
153void Function::viewCFGOnly() const {
Benjamin Kramer4c93d152011-11-15 16:26:38 +0000154 ViewGraph(this, "cfg" + getName(), true);
Chris Lattnera93d11b2003-10-22 16:03:49 +0000155}
Brian Gaeke104341f2004-04-26 16:27:08 +0000156
157FunctionPass *llvm::createCFGPrinterPass () {
158 return new CFGPrinter();
159}
160
161FunctionPass *llvm::createCFGOnlyPrinterPass () {
162 return new CFGOnlyPrinter();
163}
164