blob: 9a8c06770dd390cb4ab8f77a28c978e9fa4beef7 [file] [log] [blame]
Chris Lattnerac859db2002-10-07 18:38:01 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerac859db2002-10-07 18:38:01 +00009//
10// This file defines several printers for various different types of graphs used
11// by the LLVM infrastructure. It uses the generic graph interface to convert
12// the graph into a .dot graph. These graphs can then be processed with the
13// "dot" tool to convert them to postscript or some other suitable format.
14//
15//===----------------------------------------------------------------------===//
16
17#include "Support/GraphWriter.h"
18#include "llvm/Pass.h"
19#include "llvm/iTerminators.h"
Chris Lattnerf7482542002-11-04 02:55:30 +000020#include "llvm/Analysis/CallGraph.h"
Chris Lattnerac859db2002-10-07 18:38:01 +000021#include "llvm/Support/CFG.h"
22#include <sstream>
23#include <fstream>
24
Chris Lattnerf7482542002-11-04 02:55:30 +000025//===----------------------------------------------------------------------===//
26// Control Flow Graph Printer
27//===----------------------------------------------------------------------===//
28
Chris Lattnerac859db2002-10-07 18:38:01 +000029template<>
30struct DOTGraphTraits<Function*> : public DefaultDOTGraphTraits {
31 static std::string getGraphName(Function *F) {
32 return "CFG for '" + F->getName() + "' function";
33 }
34
35 static std::string getNodeLabel(BasicBlock *Node, Function *Graph) {
36 std::ostringstream Out;
37 Out << Node;
38 std::string OutStr = Out.str();
39 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
40
41 // Process string output to make it nicer...
42 for (unsigned i = 0; i != OutStr.length(); ++i)
43 if (OutStr[i] == '\n') { // Left justify
44 OutStr[i] = '\\';
45 OutStr.insert(OutStr.begin()+i+1, 'l');
46 } else if (OutStr[i] == ';') { // Delete comments!
47 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
48 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
49 --i;
50 }
51
52 return OutStr;
53 }
54
55 static std::string getNodeAttributes(BasicBlock *N) {
56 return "fontname=Courier";
57 }
58
59 static std::string getEdgeSourceLabel(BasicBlock *Node, succ_iterator I) {
60 // Label source of conditional branches with "T" or "F"
61 if (BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
62 if (BI->isConditional())
63 return (I == succ_begin(Node)) ? "T" : "F";
64 return "";
65 }
66};
67
68template<typename GraphType>
69static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
70 const GraphType &GT) {
71 std::string Filename = GraphName + ".dot";
72 O << "Writing '" << Filename << "'...";
73 std::ofstream F(Filename.c_str());
74
75 if (F.good())
76 WriteGraph(F, GT);
77 else
78 O << " error opening file for writing!";
79 O << "\n";
80}
81
82
83namespace {
84 struct CFGPrinter : public FunctionPass {
Chris Lattnerac859db2002-10-07 18:38:01 +000085 virtual bool runOnFunction(Function &Func) {
86 WriteGraphToFile(std::cerr, "cfg."+Func.getName(), &Func);
87 return false;
88 }
89
90 void print(std::ostream &OS) const {}
91
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
93 AU.setPreservesAll();
94 }
95 };
96
97 RegisterAnalysis<CFGPrinter> P1("print-cfg",
98 "Print CFG of function to 'dot' file");
99};
Chris Lattnerf7482542002-11-04 02:55:30 +0000100
101
102
103//===----------------------------------------------------------------------===//
104// Call Graph Printer
105//===----------------------------------------------------------------------===//
106
107template<>
108struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
109 static std::string getGraphName(CallGraph *F) {
110 return "Call Graph";
111 }
112
113 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
114 if (Node->getFunction())
115 return Node->getFunction()->getName();
116 else
117 return "Indirect call node";
118 }
119};
120
121
122namespace {
123 struct CallGraphPrinter : public Pass {
124 virtual bool run(Module &M) {
125 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
126 return false;
127 }
128
129 void print(std::ostream &OS) const {}
130
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
132 AU.addRequired<CallGraph>();
133 AU.setPreservesAll();
134 }
135 };
136
137 RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
138 "Print Call Graph to 'dot' file");
139};