Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 1 | //===- CallPrinter.cpp - DOT printer for call graph -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 6 | // |
| 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 Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/CallPrinter.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CallGraph.h" |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 24 | template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits { |
Chandler Carruth | 878b553 | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 25 | DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 26 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 27 | static std::string getGraphName(CallGraph *Graph) { return "Call graph"; } |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 28 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 29 | std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) { |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 30 | if (Function *Func = Node->getFunction()) |
| 31 | return Func->getName(); |
| 32 | |
| 33 | return "external node"; |
| 34 | } |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 35 | }; |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 36 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 37 | struct AnalysisCallGraphWrapperPassTraits { |
| 38 | static CallGraph *getGraph(CallGraphWrapperPass *P) { |
| 39 | return &P->getCallGraph(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 43 | } // end llvm namespace |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 47 | struct CallGraphViewer |
| 48 | : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *, |
| 49 | AnalysisCallGraphWrapperPassTraits> { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 50 | static char ID; |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 51 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 52 | CallGraphViewer() |
| 53 | : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *, |
| 54 | AnalysisCallGraphWrapperPassTraits>( |
| 55 | "callgraph", ID) { |
| 56 | initializeCallGraphViewerPass(*PassRegistry::getPassRegistry()); |
| 57 | } |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 60 | struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter< |
| 61 | CallGraphWrapperPass, true, CallGraph *, |
| 62 | AnalysisCallGraphWrapperPassTraits> { |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 63 | static char ID; |
Sean Fertile | 3b0535b | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 64 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 65 | CallGraphDOTPrinter() |
| 66 | : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *, |
| 67 | AnalysisCallGraphWrapperPassTraits>( |
| 68 | "callgraph", ID) { |
| 69 | initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry()); |
| 70 | } |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // end anonymous namespace |
| 74 | |
| 75 | char CallGraphViewer::ID = 0; |
Chandler Carruth | 878b553 | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 76 | INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false, |
| 77 | false) |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 78 | |
Chandler Carruth | 5f43229 | 2016-03-10 11:04:40 +0000 | [diff] [blame] | 79 | char CallGraphDOTPrinter::ID = 0; |
| 80 | INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph", |
Chandler Carruth | 878b553 | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 81 | "Print call graph to 'dot' file", false, false) |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 82 | |
| 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 Carruth | 878b553 | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 87 | ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); } |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | 5f43229 | 2016-03-10 11:04:40 +0000 | [diff] [blame] | 89 | ModulePass *llvm::createCallGraphDOTPrinterPass() { |
| 90 | return new CallGraphDOTPrinter(); |
Andrew Trick | 962318f6 | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 91 | } |