2002-10-07 18:38:01 +00:00
|
|
|
//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-07 18:38:01 +00:00
|
|
|
//
|
|
|
|
// This file defines several printers for various different types of graphs used
|
|
|
|
// by the LLVM infrastructure. It uses the generic graph interface to convert
|
|
|
|
// the graph into a .dot graph. These graphs can then be processed with the
|
|
|
|
// "dot" tool to convert them to postscript or some other suitable format.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2002-10-07 18:38:01 +00:00
|
|
|
#include "llvm/Pass.h"
|
2003-10-22 16:02:58 +00:00
|
|
|
#include "llvm/Value.h"
|
2002-11-04 02:55:30 +00:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2008-06-30 17:32:58 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2006-11-17 10:05:07 +00:00
|
|
|
#include <iostream>
|
2002-10-07 18:38:01 +00:00
|
|
|
#include <fstream>
|
2004-04-12 05:38:01 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-10-07 18:38:01 +00:00
|
|
|
template<typename GraphType>
|
|
|
|
static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
|
|
|
|
const GraphType >) {
|
|
|
|
std::string Filename = GraphName + ".dot";
|
|
|
|
O << "Writing '" << Filename << "'...";
|
2009-08-23 07:31:22 +00:00
|
|
|
std::string ErrInfo;
|
2009-08-25 15:34:52 +00:00
|
|
|
raw_fd_ostream F(Filename.c_str(), ErrInfo);
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2009-08-23 07:31:22 +00:00
|
|
|
if (ErrInfo.empty())
|
2002-10-07 18:38:01 +00:00
|
|
|
WriteGraph(F, GT);
|
|
|
|
else
|
|
|
|
O << " error opening file for writing!";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-04 02:55:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Call Graph Printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-04-12 05:38:01 +00:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(CallGraph *F) {
|
|
|
|
return "Call Graph";
|
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2009-06-24 17:37:09 +00:00
|
|
|
static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph,
|
|
|
|
bool ShortNames) {
|
2004-04-12 05:38:01 +00:00
|
|
|
if (Node->getFunction())
|
|
|
|
return ((Value*)Node->getFunction())->getName();
|
|
|
|
else
|
|
|
|
return "Indirect call node";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2002-11-04 02:55:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2004-09-20 04:48:05 +00:00
|
|
|
struct CallGraphPrinter : public ModulePass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2009-02-18 05:09:16 +00:00
|
|
|
CallGraphPrinter() : ModulePass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
virtual bool runOnModule(Module &M) {
|
2002-11-04 02:55:30 +00:00
|
|
|
WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-23 06:03:38 +00:00
|
|
|
void print(raw_ostream &OS, const llvm::Module*) const {}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2002-11-04 02:55:30 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 01:11:54 +00:00
|
|
|
char CallGraphPrinter::ID = 0;
|
2008-09-23 12:47:39 +00:00
|
|
|
RegisterPass<CallGraphPrinter> P2("dot-callgraph",
|
2006-08-27 22:30:17 +00:00
|
|
|
"Print Call Graph to 'dot' file");
|
2006-05-24 17:04:05 +00:00
|
|
|
}
|
2008-06-30 17:32:58 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DomInfoPrinter Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DomInfoPrinter : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2009-02-18 05:09:16 +00:00
|
|
|
DomInfoPrinter() : FunctionPass(&ID) {}
|
2008-06-30 17:32:58 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<DominanceFrontier>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
DominatorTree &DT = getAnalysis<DominatorTree>();
|
|
|
|
DT.dump();
|
|
|
|
DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
|
|
|
|
DF.dump();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char DomInfoPrinter::ID = 0;
|
|
|
|
static RegisterPass<DomInfoPrinter>
|
|
|
|
DIP("print-dom-info", "Dominator Info Printer", true, true);
|
|
|
|
}
|