blob: a1b518fc5942345b60fbdb01252f0c786814715f [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"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000022#include "llvm/Support/ToolOutputFile.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 Gohmanf2914012010-08-20 16:59:15 +000031 tool_output_file F(Filename.c_str(), ErrInfo);
Misha Brukman3da94ae2005-04-22 00:00:37 +000032
Dan Gohmanf2914012010-08-20 16:59:15 +000033 if (ErrInfo.empty()) {
Dan Gohmand4c45432010-09-01 14:20:41 +000034 WriteGraph(F.os(), GT);
35 F.os().close();
36 if (!F.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +000037 O << "\n";
38 F.keep();
39 return;
40 }
41 }
Dan Gohmanf2914012010-08-20 16:59:15 +000042 O << " error opening file for writing!\n";
Dan Gohmand4c45432010-09-01 14:20:41 +000043 F.os().clear_error();
Chris Lattnerac859db2002-10-07 18:38:01 +000044}
45
46
Chris Lattnerf7482542002-11-04 02:55:30 +000047//===----------------------------------------------------------------------===//
48// Call Graph Printer
49//===----------------------------------------------------------------------===//
50
Chris Lattner27829ec2004-04-12 05:38:01 +000051namespace llvm {
52 template<>
53 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +000054
55 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
56
Chris Lattner27829ec2004-04-12 05:38:01 +000057 static std::string getGraphName(CallGraph *F) {
58 return "Call Graph";
59 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000060
Tobias Grosser56f4ef32009-11-30 12:38:47 +000061 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
Chris Lattner27829ec2004-04-12 05:38:01 +000062 if (Node->getFunction())
63 return ((Value*)Node->getFunction())->getName();
64 else
Duncan Sands3982c2c2010-06-09 17:39:05 +000065 return "external node";
Chris Lattner27829ec2004-04-12 05:38:01 +000066 }
67 };
68}
Chris Lattnerf7482542002-11-04 02:55:30 +000069
70
71namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000072 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000073 static char ID; // Pass ID, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000074 CallGraphPrinter() : ModulePass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000075
Chris Lattnerb12914b2004-09-20 04:48:05 +000076 virtual bool runOnModule(Module &M) {
Daniel Dunbar8f109152010-04-15 03:47:24 +000077 WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>());
Chris Lattnerf7482542002-11-04 02:55:30 +000078 return false;
79 }
80
Chris Lattner45cfe542009-08-23 06:03:38 +000081 void print(raw_ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000082
Chris Lattnerf7482542002-11-04 02:55:30 +000083 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
84 AU.addRequired<CallGraph>();
85 AU.setPreservesAll();
86 }
87 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000088}
Devang Patel687e03b2008-06-30 17:32:58 +000089
Dan Gohmana2a3bbc2010-08-20 00:56:16 +000090char CallGraphPrinter::ID = 0;
91static RegisterPass<CallGraphPrinter> P2("dot-callgraph",
92 "Print Call Graph to 'dot' file");
93
Devang Patel687e03b2008-06-30 17:32:58 +000094//===----------------------------------------------------------------------===//
95// DomInfoPrinter Pass
96//===----------------------------------------------------------------------===//
97
98namespace {
99 class DomInfoPrinter : public FunctionPass {
100 public:
101 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000102 DomInfoPrinter() : FunctionPass(ID) {}
Devang Patel687e03b2008-06-30 17:32:58 +0000103
104 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesAll();
106 AU.addRequired<DominatorTree>();
107 AU.addRequired<DominanceFrontier>();
108
109 }
110
111 virtual bool runOnFunction(Function &F) {
112 DominatorTree &DT = getAnalysis<DominatorTree>();
113 DT.dump();
114 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
115 DF.dump();
116 return false;
117 }
118 };
Devang Patel687e03b2008-06-30 17:32:58 +0000119}
Dan Gohmana2a3bbc2010-08-20 00:56:16 +0000120
121char DomInfoPrinter::ID = 0;
122static RegisterPass<DomInfoPrinter>
123DIP("print-dom-info", "Dominator Info Printer", true, true);