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"
19 #include "llvm/InitializePasses.h"
25 template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
26 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
28 static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
30 std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
31 if (Function *Func = Node->getFunction())
32 return Func->getName();
34 return "external node";
38 struct AnalysisCallGraphWrapperPassTraits {
39 static CallGraph *getGraph(CallGraphWrapperPass *P) {
40 return &P->getCallGraph();
44 } // end llvm namespace
48 struct CallGraphViewer
49 : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
50 AnalysisCallGraphWrapperPassTraits> {
54 : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
55 AnalysisCallGraphWrapperPassTraits>(
57 initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
61 struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter<
62 CallGraphWrapperPass, true, CallGraph *,
63 AnalysisCallGraphWrapperPassTraits> {
67 : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
68 AnalysisCallGraphWrapperPassTraits>(
70 initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry());
74 } // end anonymous namespace
76 char CallGraphViewer::ID = 0;
77 INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
80 char CallGraphDOTPrinter::ID = 0;
81 INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph",
82 "Print call graph to 'dot' file", false, false)
84 // Create methods available outside of this file, to use them
85 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
86 // the link time optimization.
88 ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
90 ModulePass *llvm::createCallGraphDOTPrinterPass() {
91 return new CallGraphDOTPrinter();