blob: bbf8d122e7c7ef01c524010f56fe000b5b76438b [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 << "'...";
Chris Lattner2e35bec2009-08-23 07:31:22 +000031 std::string ErrInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +000032 raw_fd_ostream F(Filename.c_str(), ErrInfo);
Misha Brukman3da94ae2005-04-22 00:00:37 +000033
Chris Lattner2e35bec2009-08-23 07:31:22 +000034 if (ErrInfo.empty())
Chris Lattnerac859db2002-10-07 18:38:01 +000035 WriteGraph(F, GT);
36 else
37 O << " error opening file for writing!";
38 O << "\n";
39}
40
41
Chris Lattnerf7482542002-11-04 02:55:30 +000042//===----------------------------------------------------------------------===//
43// Call Graph Printer
44//===----------------------------------------------------------------------===//
45
Chris Lattner27829ec2004-04-12 05:38:01 +000046namespace llvm {
47 template<>
48 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000049
50 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
51
Chris Lattner27829ec2004-04-12 05:38:01 +000052 static std::string getGraphName(CallGraph *F) {
53 return "Call Graph";
54 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000055
Tobias Grosser56f4ef32009-11-30 12:38:47 +000056 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
Chris Lattner27829ec2004-04-12 05:38:01 +000057 if (Node->getFunction())
58 return ((Value*)Node->getFunction())->getName();
59 else
60 return "Indirect call node";
61 }
62 };
63}
Chris Lattnerf7482542002-11-04 02:55:30 +000064
65
66namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000067 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000068 static char ID; // Pass ID, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000069 CallGraphPrinter() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000070
Chris Lattnerb12914b2004-09-20 04:48:05 +000071 virtual bool runOnModule(Module &M) {
Chris Lattnerf7482542002-11-04 02:55:30 +000072 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
73 return false;
74 }
75
Chris Lattner45cfe542009-08-23 06:03:38 +000076 void print(raw_ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000077
Chris Lattnerf7482542002-11-04 02:55:30 +000078 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.addRequired<CallGraph>();
80 AU.setPreservesAll();
81 }
82 };
83
Devang Patel19974732007-05-03 01:11:54 +000084 char CallGraphPrinter::ID = 0;
Duncan Sands3ee8fc92008-09-23 12:47:39 +000085 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Chris Lattner5d8925c2006-08-27 22:30:17 +000086 "Print Call Graph to 'dot' file");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000087}
Devang Patel687e03b2008-06-30 17:32:58 +000088
89//===----------------------------------------------------------------------===//
90// DomInfoPrinter Pass
91//===----------------------------------------------------------------------===//
92
93namespace {
94 class DomInfoPrinter : public FunctionPass {
95 public:
96 static char ID; // Pass identification, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000097 DomInfoPrinter() : FunctionPass(&ID) {}
Devang Patel687e03b2008-06-30 17:32:58 +000098
99 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 AU.setPreservesAll();
101 AU.addRequired<DominatorTree>();
102 AU.addRequired<DominanceFrontier>();
103
104 }
105
106 virtual bool runOnFunction(Function &F) {
107 DominatorTree &DT = getAnalysis<DominatorTree>();
108 DT.dump();
109 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
110 DF.dump();
111 return false;
112 }
113 };
114
115 char DomInfoPrinter::ID = 0;
116 static RegisterPass<DomInfoPrinter>
117 DIP("print-dom-info", "Dominator Info Printer", true, true);
118}