Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 1 | //===- CallPrinter.cpp - DOT printer for call graph -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot |
| 11 | // containing the call graph of a module. |
| 12 | // |
| 13 | // There is also a pass available to directly call dotty ('-view-callgraph'). |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CallPrinter.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraph.h" |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/DOTGraphTraitsPass.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 25 | template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits { |
Chandler Carruth | bdd300b | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 26 | DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 27 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 28 | static std::string getGraphName(CallGraph *Graph) { return "Call graph"; } |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 29 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 30 | std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) { |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 31 | if (Function *Func = Node->getFunction()) |
| 32 | return Func->getName(); |
| 33 | |
| 34 | return "external node"; |
| 35 | } |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 36 | }; |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 37 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 38 | struct AnalysisCallGraphWrapperPassTraits { |
| 39 | static CallGraph *getGraph(CallGraphWrapperPass *P) { |
| 40 | return &P->getCallGraph(); |
Chandler Carruth | 54fec07 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 41 | } |
| 42 | }; |
| 43 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 44 | } // end llvm namespace |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 48 | struct CallGraphViewer |
| 49 | : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *, |
| 50 | AnalysisCallGraphWrapperPassTraits> { |
Sean Fertile | 8b6f52a | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 51 | static char ID; |
Sean Fertile | 8b6f52a | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 52 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 53 | CallGraphViewer() |
| 54 | : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *, |
| 55 | AnalysisCallGraphWrapperPassTraits>( |
| 56 | "callgraph", ID) { |
| 57 | initializeCallGraphViewerPass(*PassRegistry::getPassRegistry()); |
| 58 | } |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 61 | struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter< |
| 62 | CallGraphWrapperPass, true, CallGraph *, |
| 63 | AnalysisCallGraphWrapperPassTraits> { |
Sean Fertile | 8b6f52a | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 64 | static char ID; |
Sean Fertile | 8b6f52a | 2018-06-29 17:13:58 +0000 | [diff] [blame] | 65 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 66 | CallGraphDOTPrinter() |
| 67 | : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *, |
| 68 | AnalysisCallGraphWrapperPassTraits>( |
| 69 | "callgraph", ID) { |
| 70 | initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry()); |
| 71 | } |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // end anonymous namespace |
| 75 | |
| 76 | char CallGraphViewer::ID = 0; |
Chandler Carruth | bdd300b | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 77 | INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false, |
| 78 | false) |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | 562873f | 2016-03-10 11:04:40 +0000 | [diff] [blame] | 80 | char CallGraphDOTPrinter::ID = 0; |
| 81 | INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph", |
Chandler Carruth | bdd300b | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 82 | "Print call graph to 'dot' file", false, false) |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 83 | |
| 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. |
| 87 | |
Chandler Carruth | bdd300b | 2013-11-26 03:45:26 +0000 | [diff] [blame] | 88 | ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); } |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 89 | |
Chandler Carruth | 562873f | 2016-03-10 11:04:40 +0000 | [diff] [blame] | 90 | ModulePass *llvm::createCallGraphDOTPrinterPass() { |
| 91 | return new CallGraphDOTPrinter(); |
Andrew Trick | a125cac | 2013-01-11 17:28:14 +0000 | [diff] [blame] | 92 | } |