1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

[PM] Add a really simple trait to the DOTGraphTraitsPass class templates

that lets the analysis and graph types be separate and the graph
computed from the analysis through some arbitrary user-supplied code.

This will allow a call graph to an independent entity from the pass
which creates it which is necessary for the new pass manager.

llvm-svn: 195717
This commit is contained in:
Chandler Carruth 2013-11-26 03:43:52 +00:00
parent 1370a1e1ee
commit 6477bb6d16

View File

@ -19,50 +19,62 @@
namespace llvm { namespace llvm {
template <class Analysis, bool Simple> /// \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> >
class DOTGraphTraitsViewer : public FunctionPass { class DOTGraphTraitsViewer : public FunctionPass {
public: public:
DOTGraphTraitsViewer(StringRef GraphName, char &ID) DOTGraphTraitsViewer(StringRef GraphName, char &ID)
: FunctionPass(ID), Name(GraphName) {} : FunctionPass(ID), Name(GraphName) {}
virtual bool runOnFunction(Function &F) { virtual bool runOnFunction(Function &F) {
Analysis *Graph = &getAnalysis<Analysis>(); GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string GraphName = DOTGraphTraits<Analysis *>::getGraphName(Graph); std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function"; std::string Title = GraphName + " for '" + F.getName().str() + "' function";
ViewGraph(Graph, Name, Simple, Title); ViewGraph(Graph, Name, IsSimple, Title);
return false; return false;
} }
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll(); AU.setPreservesAll();
AU.addRequired<Analysis>(); AU.addRequired<AnalysisT>();
} }
private: private:
std::string Name; std::string Name;
}; };
template <class Analysis, bool Simple> template <
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
class DOTGraphTraitsPrinter : public FunctionPass { class DOTGraphTraitsPrinter : public FunctionPass {
public: public:
DOTGraphTraitsPrinter(StringRef GraphName, char &ID) DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
: FunctionPass(ID), Name(GraphName) {} : FunctionPass(ID), Name(GraphName) {}
virtual bool runOnFunction(Function &F) { virtual bool runOnFunction(Function &F) {
Analysis *Graph = &getAnalysis<Analysis>(); GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string Filename = Name + "." + F.getName().str() + ".dot"; std::string Filename = Name + "." + F.getName().str() + ".dot";
std::string ErrorInfo; std::string ErrorInfo;
errs() << "Writing '" << Filename << "'..."; errs() << "Writing '" << Filename << "'...";
raw_fd_ostream File(Filename.c_str(), ErrorInfo); raw_fd_ostream File(Filename.c_str(), ErrorInfo);
std::string GraphName = DOTGraphTraits<Analysis *>::getGraphName(Graph); std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function"; std::string Title = GraphName + " for '" + F.getName().str() + "' function";
if (ErrorInfo.empty()) if (ErrorInfo.empty())
WriteGraph(File, Graph, Simple, Title); WriteGraph(File, Graph, IsSimple, Title);
else else
errs() << " error opening file for writing!"; errs() << " error opening file for writing!";
errs() << "\n"; errs() << "\n";
@ -72,55 +84,59 @@ public:
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll(); AU.setPreservesAll();
AU.addRequired<Analysis>(); AU.addRequired<AnalysisT>();
} }
private: private:
std::string Name; std::string Name;
}; };
template <class Analysis, bool Simple> template <
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
class DOTGraphTraitsModuleViewer : public ModulePass { class DOTGraphTraitsModuleViewer : public ModulePass {
public: public:
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID) DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
: ModulePass(ID), Name(GraphName) {} : ModulePass(ID), Name(GraphName) {}
virtual bool runOnModule(Module &M) { virtual bool runOnModule(Module &M) {
Analysis *Graph = &getAnalysis<Analysis>(); GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string Title = DOTGraphTraits<Analysis *>::getGraphName(Graph); std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
ViewGraph(Graph, Name, Simple, Title); ViewGraph(Graph, Name, IsSimple, Title);
return false; return false;
} }
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll(); AU.setPreservesAll();
AU.addRequired<Analysis>(); AU.addRequired<AnalysisT>();
} }
private: private:
std::string Name; std::string Name;
}; };
template <class Analysis, bool Simple> template <
typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
class DOTGraphTraitsModulePrinter : public ModulePass { class DOTGraphTraitsModulePrinter : public ModulePass {
public: public:
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID) DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
: ModulePass(ID), Name(GraphName) {} : ModulePass(ID), Name(GraphName) {}
virtual bool runOnModule(Module &M) { virtual bool runOnModule(Module &M) {
Analysis *Graph = &getAnalysis<Analysis>(); GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string Filename = Name + ".dot"; std::string Filename = Name + ".dot";
std::string ErrorInfo; std::string ErrorInfo;
errs() << "Writing '" << Filename << "'..."; errs() << "Writing '" << Filename << "'...";
raw_fd_ostream File(Filename.c_str(), ErrorInfo); raw_fd_ostream File(Filename.c_str(), ErrorInfo);
std::string Title = DOTGraphTraits<Analysis *>::getGraphName(Graph); std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
if (ErrorInfo.empty()) if (ErrorInfo.empty())
WriteGraph(File, Graph, Simple, Title); WriteGraph(File, Graph, IsSimple, Title);
else else
errs() << " error opening file for writing!"; errs() << " error opening file for writing!";
errs() << "\n"; errs() << "\n";
@ -130,7 +146,7 @@ public:
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll(); AU.setPreservesAll();
AU.addRequired<Analysis>(); AU.addRequired<AnalysisT>();
} }
private: private: