CallPrinter.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "llvm/Analysis/CallGraph.h"
  17. #include "llvm/Analysis/CallPrinter.h"
  18. #include "llvm/Analysis/DOTGraphTraitsPass.h"
  19. using namespace llvm;
  20. namespace llvm {
  21. template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
  22. DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
  23. static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
  24. std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
  25. if (Function *Func = Node->getFunction())
  26. return Func->getName();
  27. return "external node";
  28. }
  29. };
  30. struct AnalysisCallGraphWrapperPassTraits {
  31. static CallGraph *getGraph(CallGraphWrapperPass *P) {
  32. return &P->getCallGraph();
  33. }
  34. };
  35. } // end llvm namespace
  36. namespace {
  37. struct CallGraphViewer
  38. : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
  39. AnalysisCallGraphWrapperPassTraits> {
  40. static char ID;
  41. CallGraphViewer()
  42. : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
  43. AnalysisCallGraphWrapperPassTraits>(
  44. "callgraph", ID) {
  45. initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
  46. }
  47. };
  48. struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<
  49. CallGraphWrapperPass, true, CallGraph *,
  50. AnalysisCallGraphWrapperPassTraits> {
  51. static char ID;
  52. CallGraphPrinter()
  53. : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
  54. AnalysisCallGraphWrapperPassTraits>(
  55. "callgraph", ID) {
  56. initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
  57. }
  58. };
  59. } // end anonymous namespace
  60. char CallGraphViewer::ID = 0;
  61. INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
  62. false)
  63. char CallGraphPrinter::ID = 0;
  64. INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
  65. "Print call graph to 'dot' file", false, false)
  66. // Create methods available outside of this file, to use them
  67. // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
  68. // the link time optimization.
  69. ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
  70. ModulePass *llvm::createCallGraphPrinterPass() {
  71. return new CallGraphPrinter();
  72. }