blob: fa4339a0554e6eeca5b026d7dc8a3de59acb81d8 [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"
Daniel Dunbar8f109152010-04-15 03:47:24 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner27829ec2004-04-12 05:38:01 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnerac859db2002-10-07 18:38:01 +000025template<typename GraphType>
Daniel Dunbar8f109152010-04-15 03:47:24 +000026static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName,
Chris Lattnerac859db2002-10-07 18:38:01 +000027 const GraphType &GT) {
28 std::string Filename = GraphName + ".dot";
29 O << "Writing '" << Filename << "'...";
Chris Lattner2e35bec2009-08-23 07:31:22 +000030 std::string ErrInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +000031 raw_fd_ostream F(Filename.c_str(), ErrInfo);
Misha Brukman3da94ae2005-04-22 00:00:37 +000032
Chris Lattner2e35bec2009-08-23 07:31:22 +000033 if (ErrInfo.empty())
Chris Lattnerac859db2002-10-07 18:38:01 +000034 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 {
Tobias Grossera10d5982009-11-30 12:38:13 +000048
49 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
50
Chris Lattner27829ec2004-04-12 05:38:01 +000051 static std::string getGraphName(CallGraph *F) {
52 return "Call Graph";
53 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000054
Tobias Grosser56f4ef32009-11-30 12:38:47 +000055 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
Chris Lattner27829ec2004-04-12 05:38:01 +000056 if (Node->getFunction())
57 return ((Value*)Node->getFunction())->getName();
58 else
Duncan Sands3982c2c2010-06-09 17:39:05 +000059 return "external node";
Chris Lattner27829ec2004-04-12 05:38:01 +000060 }
61 };
62}
Chris Lattnerf7482542002-11-04 02:55:30 +000063
64
65namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000066 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000067 static char ID; // Pass ID, replacement for typeid
Owen Anderson9ccaf532010-08-05 23:42:04 +000068 CallGraphPrinter() : ModulePass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000069
Chris Lattnerb12914b2004-09-20 04:48:05 +000070 virtual bool runOnModule(Module &M) {
Daniel Dunbar8f109152010-04-15 03:47:24 +000071 WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>());
Chris Lattnerf7482542002-11-04 02:55:30 +000072 return false;
73 }
74
Chris Lattner45cfe542009-08-23 06:03:38 +000075 void print(raw_ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000076
Chris Lattnerf7482542002-11-04 02:55:30 +000077 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.addRequired<CallGraph>();
79 AU.setPreservesAll();
80 }
81 };
82
Devang Patel19974732007-05-03 01:11:54 +000083 char CallGraphPrinter::ID = 0;
Duncan Sands3ee8fc92008-09-23 12:47:39 +000084 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
Chris Lattner5d8925c2006-08-27 22:30:17 +000085 "Print Call Graph to 'dot' file");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000086}
Devang Patel687e03b2008-06-30 17:32:58 +000087
88//===----------------------------------------------------------------------===//
89// DomInfoPrinter Pass
90//===----------------------------------------------------------------------===//
91
92namespace {
93 class DomInfoPrinter : public FunctionPass {
94 public:
95 static char ID; // Pass identification, replacement for typeid
Owen Anderson9ccaf532010-08-05 23:42:04 +000096 DomInfoPrinter() : FunctionPass(ID) {}
Devang Patel687e03b2008-06-30 17:32:58 +000097
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.setPreservesAll();
100 AU.addRequired<DominatorTree>();
101 AU.addRequired<DominanceFrontier>();
102
103 }
104
105 virtual bool runOnFunction(Function &F) {
106 DominatorTree &DT = getAnalysis<DominatorTree>();
107 DT.dump();
108 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
109 DF.dump();
110 return false;
111 }
112 };
113
114 char DomInfoPrinter::ID = 0;
115 static RegisterPass<DomInfoPrinter>
116 DIP("print-dom-info", "Dominator Info Printer", true, true);
117}