2002-10-07 20:38:01 +02:00
|
|
|
//===- GraphPrinters.cpp - DOT printers for various graph types -----------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-07 20:38:01 +02: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-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2002-10-07 20:38:01 +02:00
|
|
|
#include "llvm/Pass.h"
|
2003-10-22 18:02:58 +02:00
|
|
|
#include "llvm/Value.h"
|
2002-11-04 03:55:30 +01:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2008-06-30 19:32:58 +02:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2006-11-17 11:05:07 +01:00
|
|
|
#include <iostream>
|
2002-10-07 20:38:01 +02:00
|
|
|
#include <fstream>
|
2004-04-12 07:38:01 +02:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-10-07 20:38:01 +02:00
|
|
|
template<typename GraphType>
|
|
|
|
static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
|
|
|
|
const GraphType >) {
|
|
|
|
std::string Filename = GraphName + ".dot";
|
|
|
|
O << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream F(Filename.c_str());
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2002-10-07 20:38:01 +02:00
|
|
|
if (F.good())
|
|
|
|
WriteGraph(F, GT);
|
|
|
|
else
|
|
|
|
O << " error opening file for writing!";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-04 03:55:30 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Call Graph Printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-04-12 07:38:01 +02:00
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(CallGraph *F) {
|
|
|
|
return "Call Graph";
|
|
|
|
}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-04-12 07:38:01 +02:00
|
|
|
static std::string getNodeLabel(CallGraphNode *Node, CallGraph *Graph) {
|
|
|
|
if (Node->getFunction())
|
|
|
|
return ((Value*)Node->getFunction())->getName();
|
|
|
|
else
|
|
|
|
return "Indirect call node";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2002-11-04 03:55:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2004-09-20 06:48:05 +02:00
|
|
|
struct CallGraphPrinter : public ModulePass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID; // Pass ID, replacement for typeid
|
2007-05-01 23:15:47 +02:00
|
|
|
CallGraphPrinter() : ModulePass((intptr_t)&ID) {}
|
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
virtual bool runOnModule(Module &M) {
|
2002-11-04 03:55:30 +01:00
|
|
|
WriteGraphToFile(std::cerr, "callgraph", &getAnalysis<CallGraph>());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(std::ostream &OS) const {}
|
2006-08-28 03:02:49 +02:00
|
|
|
void print(std::ostream &OS, const llvm::Module*) const {}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2002-11-04 03:55:30 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-05-03 03:11:54 +02:00
|
|
|
char CallGraphPrinter::ID = 0;
|
2008-09-23 14:47:39 +02:00
|
|
|
RegisterPass<CallGraphPrinter> P2("dot-callgraph",
|
2006-08-28 00:30:17 +02:00
|
|
|
"Print Call Graph to 'dot' file");
|
2006-05-24 19:04:05 +02:00
|
|
|
}
|
2008-06-30 19:32:58 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DomInfoPrinter Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DomInfoPrinter : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
DomInfoPrinter() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|