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/Compiler.h"
27 #include "llvm/Support/GraphWriter.h"
28 #include "llvm/Config/config.h"
34 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
35 /// prints out the contents of basic blocks or not. This is acceptable because
36 /// this code is only really used for debugging purposes.
38 static bool CFGOnly = false;
42 struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
43 static std::string getGraphName(const Function *F) {
44 return "CFG for '" + F->getName() + "' function";
47 static std::string getNodeLabel(const BasicBlock *Node,
48 const Function *Graph) {
49 if (CFGOnly && !Node->getName().empty())
50 return Node->getName() + ":";
52 std::ostringstream Out;
54 WriteAsOperand(Out, Node, false);
58 if (Node->getName().empty()) {
59 WriteAsOperand(Out, Node, false);
64 std::string OutStr = Out.str();
65 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
67 // Process string output to make it nicer...
68 for (unsigned i = 0; i != OutStr.length(); ++i)
69 if (OutStr[i] == '\n') { // Left justify
71 OutStr.insert(OutStr.begin()+i+1, 'l');
72 } else if (OutStr[i] == ';') { // Delete comments!
73 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
74 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
81 static std::string getEdgeSourceLabel(const BasicBlock *Node,
82 succ_const_iterator I) {
83 // Label source of conditional branches with "T" or "F"
84 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
85 if (BI->isConditional())
86 return (I == succ_begin(Node)) ? "T" : "F";
93 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
94 virtual bool runOnFunction(Function &F) {
95 std::string Filename = "cfg." + F.getName() + ".dot";
96 cerr << "Writing '" << Filename << "'...";
97 std::ofstream File(Filename.c_str());
100 WriteGraph(File, (const Function*)&F);
102 cerr << " error opening file for writing!";
107 void print(std::ostream &OS, const Module* = 0) const {}
109 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
110 AU.setPreservesAll();
114 RegisterPass<CFGPrinter> P1("print-cfg",
115 "Print CFG of function to 'dot' file");
117 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter {
118 virtual bool runOnFunction(Function &F) {
119 bool OldCFGOnly = CFGOnly;
121 CFGPrinter::runOnFunction(F);
122 CFGOnly = OldCFGOnly;
125 void print(std::ostream &OS, const Module* = 0) const {}
127 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
128 AU.setPreservesAll();
132 RegisterPass<CFGOnlyPrinter>
134 "Print CFG of function to 'dot' file (with no function bodies)");
137 /// viewCFG - This function is meant for use from the debugger. You can just
138 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
139 /// program, displaying the CFG of the current function. This depends on there
140 /// being a 'dot' and 'gv' program in your path.
142 void Function::viewCFG() const {
143 ViewGraph(this, "cfg" + getName());
146 /// viewCFGOnly - This function is meant for use from the debugger. It works
147 /// just like viewCFG, but it does not include the contents of basic blocks
148 /// into the nodes, just the label. If you are only interested in the CFG t
149 /// his can make the graph smaller.
151 void Function::viewCFGOnly() const {
157 FunctionPass *llvm::createCFGPrinterPass () {
158 return new CFGPrinter();
161 FunctionPass *llvm::createCFGOnlyPrinterPass () {
162 return new CFGOnlyPrinter();