blob: fc74c480cf2fb26970a3775399490eeb5d6b8c6b [file] [log] [blame]
Chris Lattnerac859db2002-10-07 18:38:01 +00001//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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
17#include "Support/GraphWriter.h"
18#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"
Chris Lattnerac859db2002-10-07 18:38:01 +000021#include <fstream>
Chris Lattner27829ec2004-04-12 05:38:01 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerac859db2002-10-07 18:38:01 +000024template<typename GraphType>
25static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
26 const GraphType &GT) {
27 std::string Filename = GraphName + ".dot";
28 O << "Writing '" << Filename << "'...";
29 std::ofstream F(Filename.c_str());
30
31 if (F.good())
32 WriteGraph(F, GT);
33 else
34 O << " error opening file for writing!";
35 O << "\n";
36}
37
38
Chris Lattnerf7482542002-11-04 02:55:30 +000039//===----------------------------------------------------------------------===//
40// Call Graph Printer
41//===----------------------------------------------------------------------===//
42
Chris Lattner27829ec2004-04-12 05:38:01 +000043namespace llvm {
44 template<>
45 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
46 static std::string getGraphName(CallGraph *F) {
47 return "Call Graph";
48 }
49
50 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
51 if (Node->getFunction())
52 return ((Value*)Node->getFunction())->getName();
53 else
54 return "Indirect call node";
55 }
56 };
57}
Chris Lattnerf7482542002-11-04 02:55:30 +000058
59
60namespace {
61 struct CallGraphPrinter : public Pass {
62 virtual bool run(Module &M) {
63 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
64 return false;
65 }
66
67 void print(std::ostream &OS) const {}
68
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.addRequired<CallGraph>();
71 AU.setPreservesAll();
72 }
73 };
74
75 RegisterAnalysis<CallGraphPrinter> P2("print-callgraph",
76 "Print Call Graph to 'dot' file");
77};