blob: 7f1199a1e3d66e2c4dd523d202e2b6903d599427 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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 "llvm/Support/GraphWriter.h"
18#include "llvm/Pass.h"
19#include "llvm/Value.h"
20#include "llvm/Analysis/CallGraph.h"
Devang Patel4a805612008-06-30 17:32:58 +000021#include "llvm/Analysis/Dominators.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <iostream>
23#include <fstream>
24using namespace llvm;
25
26template<typename GraphType>
27static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
28 const GraphType &GT) {
29 std::string Filename = GraphName + ".dot";
30 O << "Writing '" << Filename << "'...";
31 std::ofstream F(Filename.c_str());
32
33 if (F.good())
34 WriteGraph(F, GT);
35 else
36 O << " error opening file for writing!";
37 O << "\n";
38}
39
40
41//===----------------------------------------------------------------------===//
42// Call Graph Printer
43//===----------------------------------------------------------------------===//
44
45namespace llvm {
46 template<>
47 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
48 static std::string getGraphName(CallGraph *F) {
49 return "Call Graph";
50 }
51
52 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
53 if (Node->getFunction())
54 return ((Value*)Node->getFunction())->getName();
55 else
56 return "Indirect call node";
57 }
58 };
59}
60
61
62namespace {
63 struct CallGraphPrinter : public ModulePass {
64 static char ID; // Pass ID, replacement for typeid
65 CallGraphPrinter() : ModulePass((intptr_t)&ID) {}
66
67 virtual bool runOnModule(Module &M) {
68 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
69 return false;
70 }
71
72 void print(std::ostream &OS) const {}
73 void print(std::ostream &OS, const llvm::Module*) const {}
74
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<CallGraph>();
77 AU.setPreservesAll();
78 }
79 };
80
81 char CallGraphPrinter::ID = 0;
82 RegisterPass<CallGraphPrinter> P2("print-callgraph",
83 "Print Call Graph to 'dot' file");
84}
Devang Patel4a805612008-06-30 17:32:58 +000085
86//===----------------------------------------------------------------------===//
87// DomInfoPrinter Pass
88//===----------------------------------------------------------------------===//
89
90namespace {
91 class DomInfoPrinter : public FunctionPass {
92 public:
93 static char ID; // Pass identification, replacement for typeid
94 DomInfoPrinter() : FunctionPass((intptr_t)&ID) {}
95
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 AU.setPreservesAll();
98 AU.addRequired<DominatorTree>();
99 AU.addRequired<DominanceFrontier>();
100
101 }
102
103 virtual bool runOnFunction(Function &F) {
104 DominatorTree &DT = getAnalysis<DominatorTree>();
105 DT.dump();
106 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
107 DF.dump();
108 return false;
109 }
110 };
111
112 char DomInfoPrinter::ID = 0;
113 static RegisterPass<DomInfoPrinter>
114 DIP("print-dom-info", "Dominator Info Printer", true, true);
115}