summaryrefslogtreecommitdiff
path: root/lib/Analysis/IPA/CallPrinter.cpp
blob: 68dcd3c064274d46bd345230465bf2c80f8b71bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines '-dot-callgraph', which emit a callgraph.<fnname>.dot
// containing the call graph of a module.
//
// There is also a pass available to directly call dotty ('-view-callgraph').
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallPrinter.h"
#include "llvm/Analysis/DOTGraphTraitsPass.h"

using namespace llvm;

namespace llvm {

template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
  DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}

  static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }

  std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
    if (Function *Func = Node->getFunction())
      return Func->getName();

    return "external node";
  }
};

struct AnalysisCallGraphWrapperPassTraits {
  static CallGraph *getGraph(CallGraphWrapperPass *P) {
    return &P->getCallGraph();
  }
};

} // end llvm namespace

namespace {

struct CallGraphViewer
    : public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
                                        AnalysisCallGraphWrapperPassTraits> {
  static char ID;

  CallGraphViewer()
      : DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
                                   AnalysisCallGraphWrapperPassTraits>(
            "callgraph", ID) {
    initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
  }
};

struct CallGraphPrinter : public DOTGraphTraitsModulePrinter<
                              CallGraphWrapperPass, true, CallGraph *,
                              AnalysisCallGraphWrapperPassTraits> {
  static char ID;

  CallGraphPrinter()
      : DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
                                    AnalysisCallGraphWrapperPassTraits>(
            "callgraph", ID) {
    initializeCallGraphPrinterPass(*PassRegistry::getPassRegistry());
  }
};

} // end anonymous namespace

char CallGraphViewer::ID = 0;
INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
                false)

char CallGraphPrinter::ID = 0;
INITIALIZE_PASS(CallGraphPrinter, "dot-callgraph",
                "Print call graph to 'dot' file", false, false)

// Create methods available outside of this file, to use them
// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
// the link time optimization.

ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }

ModulePass *llvm::createCallGraphPrinterPass() {
  return new CallGraphPrinter();
}