2013-01-11 18:28:14 +01:00
|
|
|
//===- CallPrinter.cpp - DOT printer for call graph -----------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-11 18:28:14 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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/CallPrinter.h"
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2013-01-11 18:28:14 +01:00
|
|
|
#include "llvm/Analysis/DOTGraphTraitsPass.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
template <> struct DOTGraphTraits<CallGraph *> : public DefaultDOTGraphTraits {
|
2013-11-26 04:45:26 +01:00
|
|
|
DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
static std::string getGraphName(CallGraph *Graph) { return "Call graph"; }
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
|
2013-01-11 18:28:14 +01:00
|
|
|
if (Function *Func = Node->getFunction())
|
|
|
|
return Func->getName();
|
|
|
|
|
|
|
|
return "external node";
|
|
|
|
}
|
2018-06-29 19:48:58 +02:00
|
|
|
};
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
struct AnalysisCallGraphWrapperPassTraits {
|
|
|
|
static CallGraph *getGraph(CallGraphWrapperPass *P) {
|
|
|
|
return &P->getCallGraph();
|
2013-11-26 05:19:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
} // end llvm namespace
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
struct CallGraphViewer
|
|
|
|
: public DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
|
|
|
|
AnalysisCallGraphWrapperPassTraits> {
|
2013-01-11 18:28:14 +01:00
|
|
|
static char ID;
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
CallGraphViewer()
|
|
|
|
: DOTGraphTraitsModuleViewer<CallGraphWrapperPass, true, CallGraph *,
|
|
|
|
AnalysisCallGraphWrapperPassTraits>(
|
|
|
|
"callgraph", ID) {
|
|
|
|
initializeCallGraphViewerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2013-01-11 18:28:14 +01:00
|
|
|
};
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
struct CallGraphDOTPrinter : public DOTGraphTraitsModulePrinter<
|
|
|
|
CallGraphWrapperPass, true, CallGraph *,
|
|
|
|
AnalysisCallGraphWrapperPassTraits> {
|
2013-01-11 18:28:14 +01:00
|
|
|
static char ID;
|
|
|
|
|
2018-06-29 19:48:58 +02:00
|
|
|
CallGraphDOTPrinter()
|
|
|
|
: DOTGraphTraitsModulePrinter<CallGraphWrapperPass, true, CallGraph *,
|
|
|
|
AnalysisCallGraphWrapperPassTraits>(
|
|
|
|
"callgraph", ID) {
|
|
|
|
initializeCallGraphDOTPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2013-01-11 18:28:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char CallGraphViewer::ID = 0;
|
2013-11-26 04:45:26 +01:00
|
|
|
INITIALIZE_PASS(CallGraphViewer, "view-callgraph", "View call graph", false,
|
|
|
|
false)
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2016-03-10 12:04:40 +01:00
|
|
|
char CallGraphDOTPrinter::ID = 0;
|
|
|
|
INITIALIZE_PASS(CallGraphDOTPrinter, "dot-callgraph",
|
2013-11-26 04:45:26 +01:00
|
|
|
"Print call graph to 'dot' file", false, false)
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2013-11-26 04:45:26 +01:00
|
|
|
ModulePass *llvm::createCallGraphViewerPass() { return new CallGraphViewer(); }
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2016-03-10 12:04:40 +01:00
|
|
|
ModulePass *llvm::createCallGraphDOTPrinterPass() {
|
|
|
|
return new CallGraphDOTPrinter();
|
2013-01-11 18:28:14 +01:00
|
|
|
}
|