blob: 2eeac3da87eff07065e09752c0b3144add266d3b [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 << "'...";
Chris Lattner51da0782009-08-23 07:31:22 +000031 std::string ErrInfo;
Dan Gohman176426d2009-08-25 15:34:52 +000032 raw_fd_ostream F(Filename.c_str(), ErrInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033
Chris Lattner51da0782009-08-23 07:31:22 +000034 if (ErrInfo.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 WriteGraph(F, GT);
36 else
37 O << " error opening file for writing!";
38 O << "\n";
39}
40
41
42//===----------------------------------------------------------------------===//
43// Call Graph Printer
44//===----------------------------------------------------------------------===//
45
46namespace llvm {
47 template<>
48 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +000049
50 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
51
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 static std::string getGraphName(CallGraph *F) {
53 return "Call Graph";
54 }
55
Owen Andersonf4a15462009-06-24 17:37:09 +000056 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph,
57 bool ShortNames) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 if (Node->getFunction())
59 return ((Value*)Node->getFunction())->getName();
60 else
61 return "Indirect call node";
62 }
63 };
64}
65
66
67namespace {
68 struct CallGraphPrinter : public ModulePass {
69 static char ID; // Pass ID, replacement for typeid
Dan Gohmanc74a1972009-02-18 05:09:16 +000070 CallGraphPrinter() : ModulePass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 virtual bool runOnModule(Module &M) {
73 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
74 return false;
75 }
76
Chris Lattner397f4562009-08-23 06:03:38 +000077 void print(raw_ostream &OS, const llvm::Module*) const {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.addRequired<CallGraph>();
81 AU.setPreservesAll();
82 }
83 };
84
85 char CallGraphPrinter::ID = 0;
Duncan Sandsd10d6f72008-09-23 12:47:39 +000086 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 "Print Call Graph to 'dot' file");
88}
Devang Patel4a805612008-06-30 17:32:58 +000089
90//===----------------------------------------------------------------------===//
91// DomInfoPrinter Pass
92//===----------------------------------------------------------------------===//
93
94namespace {
95 class DomInfoPrinter : public FunctionPass {
96 public:
97 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc74a1972009-02-18 05:09:16 +000098 DomInfoPrinter() : FunctionPass(&ID) {}
Devang Patel4a805612008-06-30 17:32:58 +000099
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101 AU.setPreservesAll();
102 AU.addRequired<DominatorTree>();
103 AU.addRequired<DominanceFrontier>();
104
105 }
106
107 virtual bool runOnFunction(Function &F) {
108 DominatorTree &DT = getAnalysis<DominatorTree>();
109 DT.dump();
110 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
111 DF.dump();
112 return false;
113 }
114 };
115
116 char DomInfoPrinter::ID = 0;
117 static RegisterPass<DomInfoPrinter>
118 DIP("print-dom-info", "Dominator Info Printer", true, true);
119}