blob: 5bf908808d56cd231e57272047d965925eb9a3e2 [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//
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.
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"
Bill Wendling31bf2b42006-11-17 10:05:07 +000021#include <iostream>
Chris Lattnerac859db2002-10-07 18:38:01 +000022#include <fstream>
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>
26static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
27 const GraphType &GT) {
28 std::string Filename = GraphName + ".dot";
29 O << "Writing '" << Filename << "'...";
30 std::ofstream F(Filename.c_str());
Misha Brukman3da94ae2005-04-22 00:00:37 +000031
Chris Lattnerac859db2002-10-07 18:38:01 +000032 if (F.good())
33 WriteGraph(F, GT);
34 else
35 O << " error opening file for writing!";
36 O << "\n";
37}
38
39
Chris Lattnerf7482542002-11-04 02:55:30 +000040//===----------------------------------------------------------------------===//
41// Call Graph Printer
42//===----------------------------------------------------------------------===//
43
Chris Lattner27829ec2004-04-12 05:38:01 +000044namespace llvm {
45 template<>
46 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
47 static std::string getGraphName(CallGraph *F) {
48 return "Call Graph";
49 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000050
Chris Lattner27829ec2004-04-12 05:38:01 +000051 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
52 if (Node->getFunction())
53 return ((Value*)Node->getFunction())->getName();
54 else
55 return "Indirect call node";
56 }
57 };
58}
Chris Lattnerf7482542002-11-04 02:55:30 +000059
60
61namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000062 struct CallGraphPrinter : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000063 static char ID; // Pass ID, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000064 CallGraphPrinter() : ModulePass((intptr_t)&ID) {}
65
Chris Lattnerb12914b2004-09-20 04:48:05 +000066 virtual bool runOnModule(Module &M) {
Chris Lattnerf7482542002-11-04 02:55:30 +000067 WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
68 return false;
69 }
70
71 void print(std::ostream &OS) const {}
Reid Spencer3a9ec242006-08-28 01:02:49 +000072 void print(std::ostream &OS, const llvm::Module*) const {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000073
Chris Lattnerf7482542002-11-04 02:55:30 +000074 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.addRequired<CallGraph>();
76 AU.setPreservesAll();
77 }
78 };
79
Devang Patel19974732007-05-03 01:11:54 +000080 char CallGraphPrinter::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000081 RegisterPass<CallGraphPrinter> P2("print-callgraph",
82 "Print Call Graph to 'dot' file");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000083}