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/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Analysis/CFGPrinter.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/GraphWriter.h"
28 #include "llvm/Config/config.h"
36 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
37 static std::string getGraphName(const Function *F) {
38 return "CFG for '" + F->getNameStr() + "' function";
41 static std::string getNodeLabel(const BasicBlock *Node,
42 const Function *Graph,
44 if (ShortNames && !Node->getName().empty())
45 return Node->getNameStr() + ":";
48 raw_string_ostream OS(Str);
51 WriteAsOperand(OS, Node, false);
55 if (Node->getName().empty()) {
56 WriteAsOperand(OS, Node, false);
61 std::string OutStr = OS.str();
62 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
64 // Process string output to make it nicer...
65 for (unsigned i = 0; i != OutStr.length(); ++i)
66 if (OutStr[i] == '\n') { // Left justify
68 OutStr.insert(OutStr.begin()+i+1, 'l');
69 } else if (OutStr[i] == ';') { // Delete comments!
70 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
71 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
78 static std::string getEdgeSourceLabel(const BasicBlock *Node,
79 succ_const_iterator I) {
80 // Label source of conditional branches with "T" or "F"
81 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
82 if (BI->isConditional())
83 return (I == succ_begin(Node)) ? "T" : "F";
90 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
91 static char ID; // Pass identifcation, replacement for typeid
92 CFGViewer() : FunctionPass(&ID) {}
94 virtual bool runOnFunction(Function &F) {
99 void print(std::ostream &OS, const Module* = 0) const {}
101 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.setPreservesAll();
107 char CFGViewer::ID = 0;
108 static RegisterPass<CFGViewer>
109 V0("view-cfg", "View CFG of function", false, true);
112 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
113 static char ID; // Pass identifcation, replacement for typeid
114 CFGOnlyViewer() : FunctionPass(&ID) {}
116 virtual bool runOnFunction(Function &F) {
121 void print(std::ostream &OS, const Module* = 0) const {}
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesAll();
129 char CFGOnlyViewer::ID = 0;
130 static RegisterPass<CFGOnlyViewer>
132 "View CFG of function (with no function bodies)", false, true);
135 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
136 static char ID; // Pass identification, replacement for typeid
137 CFGPrinter() : FunctionPass(&ID) {}
138 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
140 virtual bool runOnFunction(Function &F) {
141 std::string Filename = "cfg." + F.getNameStr() + ".dot";
142 cerr << "Writing '" << Filename << "'...";
143 std::ofstream File(Filename.c_str());
146 WriteGraph(File, (const Function*)&F);
148 cerr << " error opening file for writing!";
153 void print(std::ostream &OS, const Module* = 0) const {}
155 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
156 AU.setPreservesAll();
161 char CFGPrinter::ID = 0;
162 static RegisterPass<CFGPrinter>
163 P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
166 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
167 static char ID; // Pass identification, replacement for typeid
168 CFGOnlyPrinter() : FunctionPass(&ID) {}
169 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
170 virtual bool runOnFunction(Function &F) {
171 std::string Filename = "cfg." + F.getNameStr() + ".dot";
172 cerr << "Writing '" << Filename << "'...";
173 std::ofstream File(Filename.c_str());
176 WriteGraph(File, (const Function*)&F, true);
178 cerr << " error opening file for writing!";
182 void print(std::ostream &OS, const Module* = 0) const {}
184 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
185 AU.setPreservesAll();
190 char CFGOnlyPrinter::ID = 0;
191 static RegisterPass<CFGOnlyPrinter>
193 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
195 /// viewCFG - This function is meant for use from the debugger. You can just
196 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
197 /// program, displaying the CFG of the current function. This depends on there
198 /// being a 'dot' and 'gv' program in your path.
200 void Function::viewCFG() const {
201 ViewGraph(this, "cfg" + getNameStr());
204 /// viewCFGOnly - This function is meant for use from the debugger. It works
205 /// just like viewCFG, but it does not include the contents of basic blocks
206 /// into the nodes, just the label. If you are only interested in the CFG t
207 /// his can make the graph smaller.
209 void Function::viewCFGOnly() const {
210 ViewGraph(this, "cfg" + getNameStr(), true);
213 FunctionPass *llvm::createCFGPrinterPass () {
214 return new CFGPrinter();
217 FunctionPass *llvm::createCFGOnlyPrinterPass () {
218 return new CFGOnlyPrinter();