blob: a52baf7dae3c818a05c7c1df131491d26d7043ff [file] [log] [blame]
Chris Lattnerac859db2002-10-07 18:38:01 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/GraphWriter.h"
Chris Lattnerac859db2002-10-07 18:38:01 +000018#include "llvm/Pass.h"
Chris Lattner3b395372003-10-22 16:02:58 +000019#include "llvm/Value.h"
Chris Lattnerf7482542002-11-04 02:55:30 +000020#include "llvm/Analysis/CallGraph.h"
Devang Patel687e03b2008-06-30 17:32:58 +000021#include "llvm/Analysis/Dominators.h"
Bill Wendling31bf2b42006-11-17 10:05:07 +000022#include <iostream>
Chris Lattnerac859db2002-10-07 18:38:01 +000023#include <fstream>
Chris Lattner27829ec2004-04-12 05:38:01 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerac859db2002-10-07 18:38:01 +000026template<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());
Misha Brukman3da94ae2005-04-22 00:00:37 +000032
Chris Lattnerac859db2002-10-07 18:38:01 +000033 if (F.good())
34 WriteGraph(F, GT);
35 else
36 O << " error opening file for writing!";
37 O << "\n";
38}
39
40
Chris Lattnerf7482542002-11-04 02:55:30 +000041//===----------------------------------------------------------------------===//
42// Call Graph Printer
43//===----------------------------------------------------------------------===//
44
Chris Lattner27829ec2004-04-12 05:38:01 +000045namespace llvm {
46 template<>
47 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
48 static std::string getGraphName(CallGraph *F) {
49 return "Call Graph";
50 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000051
Chris Lattner27829ec2004-04-12 05:38:01 +000052 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}
Chris Lattnerf7482542002-11-04 02:55:30 +000060
61
62namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000063 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000064 static char ID; // Pass ID, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000065 CallGraphPrinter() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000066
Chris Lattnerb12914b2004-09-20 04:48:05 +000067 virtual bool runOnModule(Module &M) {
Chris Lattnerf7482542002-11-04 02:55:30 +000068 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
69 return false;
70 }
71
72 void print(std::ostream &OS) const {}
Reid Spencer3a9ec242006-08-28 01:02:49 +000073 void print(std::ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000074
Chris Lattnerf7482542002-11-04 02:55:30 +000075 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequired<CallGraph>();
77 AU.setPreservesAll();
78 }
79 };
80
Devang Patel19974732007-05-03 01:11:54 +000081 char CallGraphPrinter::ID = 0;
Duncan Sands3ee8fc92008-09-23 12:47:39 +000082 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Chris Lattner5d8925c2006-08-27 22:30:17 +000083 "Print Call Graph to 'dot' file");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000084}
Devang Patel687e03b2008-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
Dan Gohman865f0062009-02-18 05:09:16 +000094 DomInfoPrinter() : FunctionPass(&ID) {}
Devang Patel687e03b2008-06-30 17:32:58 +000095
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}