1 //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
10 // containing the call graph of a module.
12 // There is also a pass available to directly call dotty ('-view-callgraph').
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/CallPrinter.h"
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/Analysis/DOTGraphTraitsPass.h"
24 template <> struct DOTGraphTraits
<CallGraph
*> : public DefaultDOTGraphTraits
{
25 DOTGraphTraits(bool isSimple
= false) : DefaultDOTGraphTraits(isSimple
) {}
27 static std::string
getGraphName(CallGraph
*Graph
) { return "Call graph"; }
29 std::string
getNodeLabel(CallGraphNode
*Node
, CallGraph
*Graph
) {
30 if (Function
*Func
= Node
->getFunction())
31 return Func
->getName();
33 return "external node";
37 struct AnalysisCallGraphWrapperPassTraits
{
38 static CallGraph
*getGraph(CallGraphWrapperPass
*P
) {
39 return &P
->getCallGraph();
43 } // end llvm namespace
47 struct CallGraphViewer
48 : public DOTGraphTraitsModuleViewer
<CallGraphWrapperPass
, true, CallGraph
*,
49 AnalysisCallGraphWrapperPassTraits
> {
53 : DOTGraphTraitsModuleViewer
<CallGraphWrapperPass
, true, CallGraph
*,
54 AnalysisCallGraphWrapperPassTraits
>(
56 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
60 struct CallGraphDOTPrinter
: public DOTGraphTraitsModulePrinter
<
61 CallGraphWrapperPass
, true, CallGraph
*,
62 AnalysisCallGraphWrapperPassTraits
> {
66 : DOTGraphTraitsModulePrinter
<CallGraphWrapperPass
, true, CallGraph
*,
67 AnalysisCallGraphWrapperPassTraits
>(
69 initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry());
73 } // end anonymous namespace
75 char CallGraphViewer::ID
= 0;
76 INITIALIZE_PASS(CallGraphViewer
, "view-callgraph", "View call graph", false,
79 char CallGraphDOTPrinter::ID
= 0;
80 INITIALIZE_PASS(CallGraphDOTPrinter
, "dot-callgraph",
81 "Print call graph to 'dot' file", false, false)
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.
87 ModulePass
*llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
89 ModulePass
*llvm::createCallGraphDOTPrinterPass() {
90 return new CallGraphDOTPrinter();