2010-01-16 11:56:41 +01: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 01:45:19 +01:00
|
|
|
#ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
|
|
|
|
#define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
|
2010-01-16 11:56:41 +01:00
|
|
|
|
|
|
|
#include "llvm/Analysis/CFGPrinter.h"
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/Pass.h"
|
2014-04-30 01:26:49 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-01-16 11:56:41 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-11-26 04:43:52 +01: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 18:28:14 +01:00
|
|
|
class DOTGraphTraitsViewer : public FunctionPass {
|
|
|
|
public:
|
2013-01-13 17:01:15 +01:00
|
|
|
DOTGraphTraitsViewer(StringRef GraphName, char &ID)
|
2013-11-26 04:22:09 +01:00
|
|
|
: FunctionPass(ID), Name(GraphName) {}
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2015-08-03 18:37:12 +02:00
|
|
|
/// @brief Return true if this function should be processed.
|
|
|
|
///
|
|
|
|
/// An implementation of this class my override this function to indicate that
|
|
|
|
/// only certain functions should be viewed.
|
2015-12-17 13:48:25 +01:00
|
|
|
///
|
|
|
|
/// @param Analysis The current analysis result for this function.
|
|
|
|
virtual bool processFunction(Function &F, AnalysisT &Analysis) {
|
2015-08-03 18:37:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnFunction(Function &F) override {
|
2015-12-17 13:48:25 +01:00
|
|
|
auto &Analysis = getAnalysis<AnalysisT>();
|
|
|
|
|
|
|
|
if (!processFunction(F, Analysis))
|
2015-08-03 18:37:12 +02:00
|
|
|
return false;
|
|
|
|
|
2015-12-17 13:48:25 +01:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
|
2013-11-26 04:43:52 +01:00
|
|
|
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2013-01-11 18:28:14 +01:00
|
|
|
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
|
|
|
|
|
2013-11-26 04:43:52 +01:00
|
|
|
ViewGraph(Graph, Name, IsSimple, Title);
|
2010-01-16 11:56:41 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-01-16 11:56:41 +01:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 04:43:52 +01:00
|
|
|
AU.addRequired<AnalysisT>();
|
2010-01-16 11:56:41 +01:00
|
|
|
}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string Name;
|
2010-01-16 11:56:41 +01:00
|
|
|
};
|
|
|
|
|
2013-11-26 04:43:52 +01:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 18:28:14 +01:00
|
|
|
class DOTGraphTraitsPrinter : public FunctionPass {
|
|
|
|
public:
|
2013-01-13 17:01:15 +01:00
|
|
|
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
|
2013-11-26 04:22:09 +01:00
|
|
|
: FunctionPass(ID), Name(GraphName) {}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2015-08-03 18:37:12 +02:00
|
|
|
/// @brief Return true if this function should be processed.
|
|
|
|
///
|
|
|
|
/// An implementation of this class my override this function to indicate that
|
|
|
|
/// only certain functions should be printed.
|
2015-12-17 13:48:25 +01:00
|
|
|
///
|
|
|
|
/// @param Analysis The current analysis result for this function.
|
|
|
|
virtual bool processFunction(Function &F, AnalysisT &Analysis) {
|
2015-08-03 18:37:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnFunction(Function &F) override {
|
2015-12-17 13:48:25 +01:00
|
|
|
auto &Analysis = getAnalysis<AnalysisT>();
|
|
|
|
|
|
|
|
if (!processFunction(F, Analysis))
|
2015-08-03 18:37:12 +02:00
|
|
|
return false;
|
|
|
|
|
2015-12-17 13:48:25 +01:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
|
2013-01-11 18:28:14 +01:00
|
|
|
std::string Filename = Name + "." + F.getName().str() + ".dot";
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
errs() << "Writing '" << Filename << "'...";
|
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
2013-11-26 04:43:52 +01:00
|
|
|
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2013-01-11 18:28:14 +01:00
|
|
|
std::string Title = GraphName + " for '" + F.getName().str() + "' function";
|
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
if (!EC)
|
2013-11-26 04:43:52 +01:00
|
|
|
WriteGraph(File, Graph, IsSimple, Title);
|
2013-01-11 18:28:14 +01:00
|
|
|
else
|
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-01-11 18:28:14 +01:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 04:43:52 +01:00
|
|
|
AU.addRequired<AnalysisT>();
|
2013-01-11 18:28:14 +01:00
|
|
|
}
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2013-01-11 18:28:14 +01:00
|
|
|
private:
|
2010-01-16 11:56:41 +01:00
|
|
|
std::string Name;
|
2013-01-11 18:28:14 +01:00
|
|
|
};
|
|
|
|
|
2013-11-26 04:43:52 +01:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 18:28:14 +01:00
|
|
|
class DOTGraphTraitsModuleViewer : public ModulePass {
|
|
|
|
public:
|
2013-01-13 17:01:15 +01:00
|
|
|
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
|
2013-11-26 04:22:09 +01:00
|
|
|
: ModulePass(ID), Name(GraphName) {}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnModule(Module &M) override {
|
2013-11-26 04:43:52 +01:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
|
|
|
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2013-11-26 04:43:52 +01:00
|
|
|
ViewGraph(Graph, Name, IsSimple, Title);
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
return false;
|
2010-01-16 11:56:41 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-01-11 18:28:14 +01:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 04:43:52 +01:00
|
|
|
AU.addRequired<AnalysisT>();
|
2013-01-11 18:28:14 +01:00
|
|
|
}
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2013-01-11 18:28:14 +01:00
|
|
|
private:
|
|
|
|
std::string Name;
|
|
|
|
};
|
|
|
|
|
2013-11-26 04:43:52 +01:00
|
|
|
template <
|
|
|
|
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
|
|
|
|
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
|
2013-01-11 18:28:14 +01:00
|
|
|
class DOTGraphTraitsModulePrinter : public ModulePass {
|
|
|
|
public:
|
2013-01-13 17:01:15 +01:00
|
|
|
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
|
2013-11-26 04:22:09 +01:00
|
|
|
: ModulePass(ID), Name(GraphName) {}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnModule(Module &M) override {
|
2013-11-26 04:43:52 +01:00
|
|
|
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
|
2013-01-11 18:28:14 +01:00
|
|
|
std::string Filename = Name + ".dot";
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2013-01-11 18:28:14 +01:00
|
|
|
errs() << "Writing '" << Filename << "'...";
|
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
|
2013-11-26 04:43:52 +01:00
|
|
|
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
|
2010-01-16 11:56:41 +01:00
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
if (!EC)
|
2013-11-26 04:43:52 +01:00
|
|
|
WriteGraph(File, Graph, IsSimple, Title);
|
2010-01-16 11:56:41 +01:00
|
|
|
else
|
|
|
|
errs() << " error opening file for writing!";
|
|
|
|
errs() << "\n";
|
2013-01-11 18:28:14 +01:00
|
|
|
|
2010-01-16 11:56:41 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:30:04 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-01-16 11:56:41 +01:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 04:43:52 +01:00
|
|
|
AU.addRequired<AnalysisT>();
|
2010-01-16 11:56:41 +01:00
|
|
|
}
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string Name;
|
2010-01-16 11:56:41 +01:00
|
|
|
};
|
2013-01-11 18:28:14 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2010-01-16 11:56:41 +01:00
|
|
|
#endif
|