blob: d24cbd104bf62eab8223c9beb2df2e7a0eb33510 [file] [log] [blame]
Andrew Trick962318f62013-01-11 17:28:14 +00001//===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Trick962318f62013-01-11 17:28:14 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
10// containing the call graph of a module.
11//
12// There is also a pass available to directly call dotty ('-view-callgraph').
13//
14//===----------------------------------------------------------------------===//
15
Andrew Trick962318f62013-01-11 17:28:14 +000016#include "llvm/Analysis/CallPrinter.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "llvm/Analysis/CallGraph.h"
Andrew Trick962318f62013-01-11 17:28:14 +000018#include "llvm/Analysis/DOTGraphTraitsPass.h"
19
20using namespace llvm;
21
22namespace llvm {
23
Sean Fertilecd0d7632018-06-29 17:48:58 +000024template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
Chandler Carruth878b5532013-11-26 03:45:26 +000025 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
Andrew Trick962318f62013-01-11 17:28:14 +000026
Sean Fertilecd0d7632018-06-29 17:48:58 +000027 static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
Andrew Trick962318f62013-01-11 17:28:14 +000028
Sean Fertilecd0d7632018-06-29 17:48:58 +000029 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
Andrew Trick962318f62013-01-11 17:28:14 +000030 if (Function *Func = Node->getFunction())
31 return Func->getName();
32
33 return "external node";
34 }
Sean Fertilecd0d7632018-06-29 17:48:58 +000035};
Andrew Trick962318f62013-01-11 17:28:14 +000036
Sean Fertilecd0d7632018-06-29 17:48:58 +000037struct AnalysisCallGraphWrapperPassTraits {
38 static CallGraph *getGraph(CallGraphWrapperPass *P) {
39 return &P->getCallGraph();
Chandler Carruth6378cf52013-11-26 04:19:30 +000040 }
41};
42
Sean Fertilecd0d7632018-06-29 17:48:58 +000043} // end llvm namespace
Andrew Trick962318f62013-01-11 17:28:14 +000044
45namespace {
46
Sean Fertilecd0d7632018-06-29 17:48:58 +000047struct CallGraphViewer
48 : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
49 AnalysisCallGraphWrapperPassTraits> {
Sean Fertile3b0535b2018-06-29 17:13:58 +000050 static char ID;
Sean Fertile3b0535b2018-06-29 17:13:58 +000051
Sean Fertilecd0d7632018-06-29 17:48:58 +000052 CallGraphViewer()
53 : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
54 AnalysisCallGraphWrapperPassTraits>(
55 "callgraph", ID) {
56 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
57 }
Andrew Trick962318f62013-01-11 17:28:14 +000058};
59
Sean Fertilecd0d7632018-06-29 17:48:58 +000060struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter<
61 CallGraphWrapperPass, true, CallGraph *,
62 AnalysisCallGraphWrapperPassTraits> {
Sean Fertile3b0535b2018-06-29 17:13:58 +000063 static char ID;
Sean Fertile3b0535b2018-06-29 17:13:58 +000064
Sean Fertilecd0d7632018-06-29 17:48:58 +000065 CallGraphDOTPrinter()
66 : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
67 AnalysisCallGraphWrapperPassTraits>(
68 "callgraph", ID) {
69 initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry());
70 }
Andrew Trick962318f62013-01-11 17:28:14 +000071};
72
73} // end anonymous namespace
74
75char CallGraphViewer::ID = 0;
Chandler Carruth878b5532013-11-26 03:45:26 +000076INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
77 false)
Andrew Trick962318f62013-01-11 17:28:14 +000078
Chandler Carruth5f432292016-03-10 11:04:40 +000079char CallGraphDOTPrinter::ID = 0;
80INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph",
Chandler Carruth878b5532013-11-26 03:45:26 +000081 "Print call graph to 'dot' file", false, false)
Andrew Trick962318f62013-01-11 17:28:14 +000082
83// Create methods available outside of this file, to use them
84// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
85// the link time optimization.
86
Chandler Carruth878b5532013-11-26 03:45:26 +000087ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
Andrew Trick962318f62013-01-11 17:28:14 +000088
Chandler Carruth5f432292016-03-10 11:04:40 +000089ModulePass *llvm::createCallGraphDOTPrinterPass() {
90 return new CallGraphDOTPrinter();
Andrew Trick962318f62013-01-11 17:28:14 +000091}