blob: 2eeac3da87eff07065e09752c0b3144add266d3b [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
Owen Anderson8cbc94a2009-06-24 17:37:09 +000056 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph,
57 bool ShortNames) {
Chris Lattner27829ec2004-04-12 05:38:01 +000058 if (Node->getFunction())
59 return ((Value*)Node->getFunction())->getName();
60 else
61 return "Indirect call node";
62 }
63 };
64}
Chris Lattnerf7482542002-11-04 02:55:30 +000065
66
67namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000068 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000069 static char ID; // Pass ID, replacement for typeid
Dan Gohman865f0062009-02-18 05:09:16 +000070 CallGraphPrinter() : ModulePass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000071
Chris Lattnerb12914b2004-09-20 04:48:05 +000072 virtual bool runOnModule(Module &M) {
Chris Lattnerf7482542002-11-04 02:55:30 +000073 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
74 return false;
75 }
76
Chris Lattner45cfe542009-08-23 06:03:38 +000077 void print(raw_ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000078
Chris Lattnerf7482542002-11-04 02:55:30 +000079 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 AU.addRequired<CallGraph>();
81 AU.setPreservesAll();
82 }
83 };
84
Devang Patel19974732007-05-03 01:11:54 +000085 char CallGraphPrinter::ID = 0;
Duncan Sands3ee8fc92008-09-23 12:47:39 +000086 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Chris Lattner5d8925c2006-08-27 22:30:17 +000087 "Print Call Graph to 'dot' file");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000088}
Devang Patel687e03b2008-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 Gohman865f0062009-02-18 05:09:16 +000098 DomInfoPrinter() : FunctionPass(&ID) {}
Devang Patel687e03b2008-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}