xref: /aosp_15_r20/external/llvm/lib/Analysis/CallPrinter.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
11*9880d681SAndroid Build Coastguard Worker // containing the call graph of a module.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // There is also a pass available to directly call dotty ('-view-callgraph').
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallPrinter.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/DOTGraphTraitsPass.h"
20*9880d681SAndroid Build Coastguard Worker 
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker namespace llvm {
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits26*9880d681SAndroid Build Coastguard Worker   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
27*9880d681SAndroid Build Coastguard Worker 
getGraphNamellvm::DOTGraphTraits28*9880d681SAndroid Build Coastguard Worker   static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
29*9880d681SAndroid Build Coastguard Worker 
getNodeLabelllvm::DOTGraphTraits30*9880d681SAndroid Build Coastguard Worker   std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
31*9880d681SAndroid Build Coastguard Worker     if (Function *Func = Node->getFunction())
32*9880d681SAndroid Build Coastguard Worker       return Func->getName();
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker     return "external node";
35*9880d681SAndroid Build Coastguard Worker   }
36*9880d681SAndroid Build Coastguard Worker };
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker struct AnalysisCallGraphWrapperPassTraits {
getGraphllvm::AnalysisCallGraphWrapperPassTraits39*9880d681SAndroid Build Coastguard Worker   static CallGraph *getGraph(CallGraphWrapperPass *P) {
40*9880d681SAndroid Build Coastguard Worker     return &P->getCallGraph();
41*9880d681SAndroid Build Coastguard Worker   }
42*9880d681SAndroid Build Coastguard Worker };
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker } // end llvm namespace
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker namespace {
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker struct CallGraphViewer
49*9880d681SAndroid Build Coastguard Worker     : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
50*9880d681SAndroid Build Coastguard Worker                                         AnalysisCallGraphWrapperPassTraits> {
51*9880d681SAndroid Build Coastguard Worker   static char ID;
52*9880d681SAndroid Build Coastguard Worker 
CallGraphViewer__anonf17cd73b0111::CallGraphViewer53*9880d681SAndroid Build Coastguard Worker   CallGraphViewer()
54*9880d681SAndroid Build Coastguard Worker       : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
55*9880d681SAndroid Build Coastguard Worker                                    AnalysisCallGraphWrapperPassTraits>(
56*9880d681SAndroid Build Coastguard Worker             "callgraph", ID) {
57*9880d681SAndroid Build Coastguard Worker     initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
58*9880d681SAndroid Build Coastguard Worker   }
59*9880d681SAndroid Build Coastguard Worker };
60*9880d681SAndroid Build Coastguard Worker 
61*9880d681SAndroid Build Coastguard Worker struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter<
62*9880d681SAndroid Build Coastguard Worker                               CallGraphWrapperPass, true, CallGraph *,
63*9880d681SAndroid Build Coastguard Worker                               AnalysisCallGraphWrapperPassTraits> {
64*9880d681SAndroid Build Coastguard Worker   static char ID;
65*9880d681SAndroid Build Coastguard Worker 
CallGraphDOTPrinter__anonf17cd73b0111::CallGraphDOTPrinter66*9880d681SAndroid Build Coastguard Worker   CallGraphDOTPrinter()
67*9880d681SAndroid Build Coastguard Worker       : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
68*9880d681SAndroid Build Coastguard Worker                                     AnalysisCallGraphWrapperPassTraits>(
69*9880d681SAndroid Build Coastguard Worker             "callgraph", ID) {
70*9880d681SAndroid Build Coastguard Worker     initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry());
71*9880d681SAndroid Build Coastguard Worker   }
72*9880d681SAndroid Build Coastguard Worker };
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker char CallGraphViewer::ID = 0;
77*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
78*9880d681SAndroid Build Coastguard Worker                 false)
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker char CallGraphDOTPrinter::ID = 0;
81*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph",
82*9880d681SAndroid Build Coastguard Worker                 "Print call graph to 'dot' file", false, false)
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker // Create methods available outside of this file, to use them
85*9880d681SAndroid Build Coastguard Worker // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
86*9880d681SAndroid Build Coastguard Worker // the link time optimization.
87*9880d681SAndroid Build Coastguard Worker 
createCallGraphViewerPass()88*9880d681SAndroid Build Coastguard Worker ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
89*9880d681SAndroid Build Coastguard Worker 
createCallGraphDOTPrinterPass()90*9880d681SAndroid Build Coastguard Worker ModulePass *llvm::createCallGraphDOTPrinterPass() {
91*9880d681SAndroid Build Coastguard Worker   return new CallGraphDOTPrinter();
92*9880d681SAndroid Build Coastguard Worker }
93