1 //===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines several printers for various different types of graphs used
11 // by the LLVM infrastructure. It uses the generic graph interface to convert
12 // the graph into a .dot graph. These graphs can then be processed with the
13 // "dot" tool to convert them to postscript or some other suitable format.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/GraphWriter.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Value.h"
20 #include "llvm/Analysis/CallGraph.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Support/raw_ostream.h"
25 template<typename GraphType>
26 static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName,
27 const GraphType >) {
28 std::string Filename = GraphName + ".dot";
29 O << "Writing '" << Filename << "'...";
31 raw_fd_ostream F(Filename.c_str(), ErrInfo);
36 O << " error opening file for writing!";
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
47 struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
49 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
51 static std::string getGraphName(CallGraph *F) {
55 static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
56 if (Node->getFunction())
57 return ((Value*)Node->getFunction())->getName();
59 return "Indirect call node";
66 struct CallGraphPrinter : public ModulePass {
67 static char ID; // Pass ID, replacement for typeid
68 CallGraphPrinter() : ModulePass(&ID) {}
70 virtual bool runOnModule(Module &M) {
71 WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>());
75 void print(raw_ostream &OS, const llvm::Module*) const {}
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 AU.addRequired<CallGraph>();
83 char CallGraphPrinter::ID = 0;
84 RegisterPass<CallGraphPrinter> P2("dot-callgraph",
85 "Print Call Graph to 'dot' file");
88 //===----------------------------------------------------------------------===//
89 // DomInfoPrinter Pass
90 //===----------------------------------------------------------------------===//
93 class DomInfoPrinter : public FunctionPass {
95 static char ID; // Pass identification, replacement for typeid
96 DomInfoPrinter() : FunctionPass(&ID) {}
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 AU.addRequired<DominatorTree>();
101 AU.addRequired<DominanceFrontier>();
105 virtual bool runOnFunction(Function &F) {
106 DominatorTree &DT = getAnalysis<DominatorTree>();
108 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
114 char DomInfoPrinter::ID = 0;
115 static RegisterPass<DomInfoPrinter>
116 DIP("print-dom-info", "Dominator Info Printer", true, true);