blob: 1c9856b252006d5c067d3216f6151512fa4787c9 [file] [log] [blame]
Andrew Trick962318f62013-01-11 17:28:14 +00001//===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
11// containing the call graph of a module.
12//
13// There is also a pass available to directly call dotty ('-view-callgraph').
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/CallGraph.h"
18#include "llvm/Analysis/CallPrinter.h"
19#include "llvm/Analysis/DOTGraphTraitsPass.h"
20
21using namespace llvm;
22
23namespace llvm {
24
Chandler Carruth878b5532013-11-26 03:45:26 +000025template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
26 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
Andrew Trick962318f62013-01-11 17:28:14 +000027
Chandler Carruth878b5532013-11-26 03:45:26 +000028 static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
Andrew Trick962318f62013-01-11 17:28:14 +000029
30 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
31 if (Function *Func = Node->getFunction())
32 return Func->getName();
33
34 return "external node";
35 }
36};
37
38} // end llvm namespace
39
40namespace {
41
Chandler Carruth878b5532013-11-26 03:45:26 +000042struct CallGraphViewer : public DOTGraphTraitsModuleViewer<CallGraph, true> {
Andrew Trick962318f62013-01-11 17:28:14 +000043 static char ID;
44
45 CallGraphViewer()
Chandler Carruth878b5532013-11-26 03:45:26 +000046 : DOTGraphTraitsModuleViewer<CallGraph, true>("callgraph", ID) {
Andrew Trick962318f62013-01-11 17:28:14 +000047 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
48 }
49};
50
Chandler Carruth878b5532013-11-26 03:45:26 +000051struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<CallGraph, true> {
Andrew Trick962318f62013-01-11 17:28:14 +000052 static char ID;
53
54 CallGraphPrinter()
Chandler Carruth878b5532013-11-26 03:45:26 +000055 : DOTGraphTraitsModulePrinter<CallGraph, true>("callgraph", ID) {
56 initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
Andrew Trick962318f62013-01-11 17:28:14 +000057 }
58};
59
60} // end anonymous namespace
61
62char CallGraphViewer::ID = 0;
Chandler Carruth878b5532013-11-26 03:45:26 +000063INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
64 false)
Andrew Trick962318f62013-01-11 17:28:14 +000065
66char CallGraphPrinter::ID = 0;
67INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
Chandler Carruth878b5532013-11-26 03:45:26 +000068 "Print call graph to 'dot' file", false, false)
Andrew Trick962318f62013-01-11 17:28:14 +000069
70// Create methods available outside of this file, to use them
71// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
72// the link time optimization.
73
Chandler Carruth878b5532013-11-26 03:45:26 +000074ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
Andrew Trick962318f62013-01-11 17:28:14 +000075
76ModulePass *llvm::createCallGraphPrinterPass() {
77 return new CallGraphPrinter();
78}