2010-01-16 10:56:41 +00:00
|
|
|
//===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Templates to create dotty viewer and printer passes for GraphTraits graphs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
|
|
|
|
#define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
|
2010-01-16 10:56:41 +00:00
|
|
|
|
|
|
|
#include "llvm/Analysis/CFGPrinter.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Pass.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-01-16 10:56:41 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
/// \brief Default traits class for extracting a graph from an analysis pass.
|
|
|
|
///
|
|
|
|
/// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
|
|
|
|
template <typename AnalysisT, typename GraphT = AnalysisT *>
|
|
|
|
struct DefaultAnalysisGraphTraits {
|
|
|
|
static GraphT getGraph(AnalysisT *A) { return A; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 17:28:14 +00:00
|
|
|
class DOTGraphTraitsViewer : public FunctionPass {
|
|
|
|
public:
|
2013-01-13 16:01:15 +00:00
|
|
|
DOTGraphTraitsViewer(StringRef GraphName, char &ID)
|
2013-11-26 03:22:09 +00:00
|
|
|
: FunctionPass(ID), Name(GraphName) {}
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2013-11-26 03:43:52 +00:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
|
|
|
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2013-01-11 17:28:14 +00:00
|
|
|
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
|
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
ViewGraph(Graph, Name, IsSimple, Title);
|
2010-01-16 10:56:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-01-16 10:56:41 +00:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 03:43:52 +00:00
|
|
|
AU.addRequired<AnalysisT>();
|
2010-01-16 10:56:41 +00:00
|
|
|
}
|
2013-01-11 17:28:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string Name;
|
2010-01-16 10:56:41 +00:00
|
|
|
};
|
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 17:28:14 +00:00
|
|
|
class DOTGraphTraitsPrinter : public FunctionPass {
|
|
|
|
public:
|
2013-01-13 16:01:15 +00:00
|
|
|
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
|
2013-11-26 03:22:09 +00:00
|
|
|
: FunctionPass(ID), Name(GraphName) {}
|
2013-01-11 17:28:14 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnFunction(Function &F) override {
|
2013-11-26 03:43:52 +00:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
2013-01-11 17:28:14 +00:00
|
|
|
std::string Filename = Name + "." + F.getName().str() + ".dot";
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
2013-01-11 17:28:14 +00:00
|
|
|
|
|
|
|
errs() << "Writing '" << Filename << "'...";
|
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
2013-11-26 03:43:52 +00:00
|
|
|
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2013-01-11 17:28:14 +00:00
|
|
|
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
|
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
if (!EC)
|
2013-11-26 03:43:52 +00:00
|
|
|
WriteGraph(File, Graph, IsSimple, Title);
|
2013-01-11 17:28:14 +00:00
|
|
|
else
|
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-01-11 17:28:14 +00:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 03:43:52 +00:00
|
|
|
AU.addRequired<AnalysisT>();
|
2013-01-11 17:28:14 +00:00
|
|
|
}
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2013-01-11 17:28:14 +00:00
|
|
|
private:
|
2010-01-16 10:56:41 +00:00
|
|
|
std::string Name;
|
2013-01-11 17:28:14 +00:00
|
|
|
};
|
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 17:28:14 +00:00
|
|
|
class DOTGraphTraitsModuleViewer : public ModulePass {
|
|
|
|
public:
|
2013-01-13 16:01:15 +00:00
|
|
|
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
|
2013-11-26 03:22:09 +00:00
|
|
|
: ModulePass(ID), Name(GraphName) {}
|
2013-01-11 17:28:14 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnModule(Module &M) override {
|
2013-11-26 03:43:52 +00:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
|
|
|
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
ViewGraph(Graph, Name, IsSimple, Title);
|
2013-01-11 17:28:14 +00:00
|
|
|
|
|
|
|
return false;
|
2010-01-16 10:56:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-01-11 17:28:14 +00:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 03:43:52 +00:00
|
|
|
AU.addRequired<AnalysisT>();
|
2013-01-11 17:28:14 +00:00
|
|
|
}
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2013-01-11 17:28:14 +00:00
|
|
|
private:
|
|
|
|
std::string Name;
|
|
|
|
};
|
|
|
|
|
2013-11-26 03:43:52 +00:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 17:28:14 +00:00
|
|
|
class DOTGraphTraitsModulePrinter : public ModulePass {
|
|
|
|
public:
|
2013-01-13 16:01:15 +00:00
|
|
|
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
|
2013-11-26 03:22:09 +00:00
|
|
|
: ModulePass(ID), Name(GraphName) {}
|
2013-01-11 17:28:14 +00:00
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
bool runOnModule(Module &M) override {
|
2013-11-26 03:43:52 +00:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
2013-01-11 17:28:14 +00:00
|
|
|
std::string Filename = Name + ".dot";
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2013-01-11 17:28:14 +00:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
2013-11-26 03:43:52 +00:00
|
|
|
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2010-01-16 10:56:41 +00:00
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
if (!EC)
|
2013-11-26 03:43:52 +00:00
|
|
|
WriteGraph(File, Graph, IsSimple, Title);
|
2010-01-16 10:56:41 +00:00
|
|
|
else
|
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2013-01-11 17:28:14 +00:00
|
|
|
|
2010-01-16 10:56:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:30:04 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-01-16 10:56:41 +00:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 03:43:52 +00:00
|
|
|
AU.addRequired<AnalysisT>();
|
2010-01-16 10:56:41 +00:00
|
|
|
}
|
2013-01-11 17:28:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string Name;
|
2010-01-16 10:56:41 +00:00
|
|
|
};
|
2013-01-11 17:28:14 +00:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2010-01-16 10:56:41 +00:00
|
|
|
#endif
|