1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a '-print-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/GraphWriter.h"
27 #include "llvm/Config/config.h"
33 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
34 /// prints out the contents of basic blocks or not. This is acceptable because
35 /// this code is only really used for debugging purposes.
37 static bool CFGOnly = false;
41 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
42 static std::string getGraphName(const Function *F) {
43 return "CFG for '" + F->getName() + "' function";
46 static std::string getNodeLabel(const BasicBlock *Node,
47 const Function *Graph) {
48 if (CFGOnly && !Node->getName().empty())
49 return Node->getName() + ":";
51 std::ostringstream Out;
53 WriteAsOperand(Out, Node, false);
57 if (Node->getName().empty()) {
58 WriteAsOperand(Out, Node, false);
63 std::string OutStr = Out.str();
64 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
66 // Process string output to make it nicer...
67 for (unsigned i = 0; i != OutStr.length(); ++i)
68 if (OutStr[i] == '\n') { // Left justify
70 OutStr.insert(OutStr.begin()+i+1, 'l');
71 } else if (OutStr[i] == ';') { // Delete comments!
72 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
73 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
80 static std::string getEdgeSourceLabel(const BasicBlock *Node,
81 succ_const_iterator I) {
82 // Label source of conditional branches with "T" or "F"
83 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
84 if (BI->isConditional())
85 return (I == succ_begin(Node)) ? "T" : "F";
92 struct CFGPrinter : public FunctionPass {
93 virtual bool runOnFunction(Function &F) {
94 std::string Filename = "cfg." + F.getName() + ".dot";
95 cerr << "Writing '" << Filename << "'...";
96 std::ofstream File(Filename.c_str());
99 WriteGraph(File, (const Function*)&F);
101 cerr << " error opening file for writing!";
106 void print(std::ostream &OS, const Module* = 0) const {}
108 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.setPreservesAll();
113 RegisterPass<CFGPrinter> P1("print-cfg",
114 "Print CFG of function to 'dot' file");
116 struct CFGOnlyPrinter : public CFGPrinter {
117 virtual bool runOnFunction(Function &F) {
118 bool OldCFGOnly = CFGOnly;
120 CFGPrinter::runOnFunction(F);
121 CFGOnly = OldCFGOnly;
124 void print(std::ostream &OS, const Module* = 0) const {}
126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
127 AU.setPreservesAll();
131 RegisterPass<CFGOnlyPrinter>
133 "Print CFG of function to 'dot' file (with no function bodies)");
136 /// 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.
141 void Function::viewCFG() const {
142 ViewGraph(this, "cfg" + getName());
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.
150 void Function::viewCFGOnly() const {
156 FunctionPass *llvm::createCFGPrinterPass () {
157 return new CFGPrinter();
160 FunctionPass *llvm::createCFGOnlyPrinterPass () {
161 return new CFGOnlyPrinter();