blob: 5d581e4af0a2dffc8c941e4be22728c1bf68321f [file] [log] [blame]
Chris Lattner91264832002-10-07 18:38:01 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner91264832002-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 Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/Support/GraphWriter.h"
Chris Lattner91264832002-10-07 18:38:01 +000018#include "llvm/Pass.h"
Chris Lattner638b5372003-10-22 16:02:58 +000019#include "llvm/Value.h"
Chris Lattner2743db52002-11-04 02:55:30 +000020#include "llvm/Analysis/CallGraph.h"
Devang Patel81ea3bb2008-06-30 17:32:58 +000021#include "llvm/Analysis/Dominators.h"
Bill Wendling605795e2006-11-17 10:05:07 +000022#include <iostream>
Chris Lattner91264832002-10-07 18:38:01 +000023#include <fstream>
Chris Lattner0a752642004-04-12 05:38:01 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner91264832002-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 Brukman650ba8e2005-04-22 00:00:37 +000032
Chris Lattner91264832002-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 Lattner2743db52002-11-04 02:55:30 +000041//===----------------------------------------------------------------------===//
42// Call Graph Printer
43//===----------------------------------------------------------------------===//
44
Chris Lattner0a752642004-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 Brukman650ba8e2005-04-22 00:00:37 +000051
Owen Andersonb70adf22009-06-24 17:37:09 +000052 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph,
53 bool ShortNames) {
Chris Lattner0a752642004-04-12 05:38:01 +000054 if (Node->getFunction())
55 return ((Value*)Node->getFunction())->getName();
56 else
57 return "Indirect call node";
58 }
59 };
60}
Chris Lattner2743db52002-11-04 02:55:30 +000061
62
63namespace {
Chris Lattner4f2cf032004-09-20 04:48:05 +000064 struct CallGraphPrinter : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000065 static char ID; // Pass ID, replacement for typeid
Dan Gohman38a96312009-02-18 05:09:16 +000066 CallGraphPrinter() : ModulePass(&ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000067
Chris Lattner4f2cf032004-09-20 04:48:05 +000068 virtual bool runOnModule(Module &M) {
Chris Lattner2743db52002-11-04 02:55:30 +000069 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
70 return false;
71 }
72
73 void print(std::ostream &OS) const {}
Reid Spencere7141c82006-08-28 01:02:49 +000074 void print(std::ostream &OS, const llvm::Module*) const {}
Misha Brukman650ba8e2005-04-22 00:00:37 +000075
Chris Lattner2743db52002-11-04 02:55:30 +000076 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.addRequired<CallGraph>();
78 AU.setPreservesAll();
79 }
80 };
81
Devang Patel8c78a0b2007-05-03 01:11:54 +000082 char CallGraphPrinter::ID = 0;
Duncan Sands9c40c282008-09-23 12:47:39 +000083 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Chris Lattner3c9b2422006-08-27 22:30:17 +000084 "Print Call Graph to 'dot' file");
Chris Lattneraa2372562006-05-24 17:04:05 +000085}
Devang Patel81ea3bb2008-06-30 17:32:58 +000086
87//===----------------------------------------------------------------------===//
88// DomInfoPrinter Pass
89//===----------------------------------------------------------------------===//
90
91namespace {
92 class DomInfoPrinter : public FunctionPass {
93 public:
94 static char ID; // Pass identification, replacement for typeid
Dan Gohman38a96312009-02-18 05:09:16 +000095 DomInfoPrinter() : FunctionPass(&ID) {}
Devang Patel81ea3bb2008-06-30 17:32:58 +000096
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesAll();
99 AU.addRequired<DominatorTree>();
100 AU.addRequired<DominanceFrontier>();
101
102 }
103
104 virtual bool runOnFunction(Function &F) {
105 DominatorTree &DT = getAnalysis<DominatorTree>();
106 DT.dump();
107 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
108 DF.dump();
109 return false;
110 }
111 };
112
113 char DomInfoPrinter::ID = 0;
114 static RegisterPass<DomInfoPrinter>
115 DIP("print-dom-info", "Dominator Info Printer", true, true);
116}