[PM] Add a really simple trait to the DOTGraphTraitsPass class templates
[oota-llvm.git] / include / llvm / Analysis / DOTGraphTraitsPass.h
1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
16
17 #include "llvm/Analysis/CFGPrinter.h"
18 #include "llvm/Pass.h"
19
20 namespace llvm {
21
22 /// \brief Default traits class for extracting a graph from an analysis pass.
23 ///
24 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
25 template <typename AnalysisT, typename GraphT = AnalysisT *>
26 struct DefaultAnalysisGraphTraits {
27   static GraphT getGraph(AnalysisT *A) { return A; }
28 };
29
30 template <
31     typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
32     typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
33 class DOTGraphTraitsViewer : public FunctionPass {
34 public:
35   DOTGraphTraitsViewer(StringRef GraphName, char &ID)
36       : FunctionPass(ID), Name(GraphName) {}
37
38   virtual bool runOnFunction(Function &F) {
39     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
40     std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
41     std::string Title = GraphName + " for '" + F.getName().str() + "' function";
42
43     ViewGraph(Graph, Name, IsSimple, Title);
44
45     return false;
46   }
47
48   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49     AU.setPreservesAll();
50     AU.addRequired<AnalysisT>();
51   }
52
53 private:
54   std::string Name;
55 };
56
57 template <
58     typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
59     typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
60 class DOTGraphTraitsPrinter : public FunctionPass {
61 public:
62   DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
63       : FunctionPass(ID), Name(GraphName) {}
64
65   virtual bool runOnFunction(Function &F) {
66     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
67     std::string Filename = Name + "." + F.getName().str() + ".dot";
68     std::string ErrorInfo;
69
70     errs() << "Writing '" << Filename << "'...";
71
72     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
73     std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
74     std::string Title = GraphName + " for '" + F.getName().str() + "' function";
75
76     if (ErrorInfo.empty())
77       WriteGraph(File, Graph, IsSimple, Title);
78     else
79       errs() << "  error opening file for writing!";
80     errs() << "\n";
81
82     return false;
83   }
84
85   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86     AU.setPreservesAll();
87     AU.addRequired<AnalysisT>();
88   }
89
90 private:
91   std::string Name;
92 };
93
94 template <
95     typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
96     typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
97 class DOTGraphTraitsModuleViewer : public ModulePass {
98 public:
99   DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
100       : ModulePass(ID), Name(GraphName) {}
101
102   virtual bool runOnModule(Module &M) {
103     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
104     std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
105
106     ViewGraph(Graph, Name, IsSimple, Title);
107
108     return false;
109   }
110
111   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
112     AU.setPreservesAll();
113     AU.addRequired<AnalysisT>();
114   }
115
116 private:
117   std::string Name;
118 };
119
120 template <
121     typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
122     typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
123 class DOTGraphTraitsModulePrinter : public ModulePass {
124 public:
125   DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
126       : ModulePass(ID), Name(GraphName) {}
127
128   virtual bool runOnModule(Module &M) {
129     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
130     std::string Filename = Name + ".dot";
131     std::string ErrorInfo;
132
133     errs() << "Writing '" << Filename << "'...";
134
135     raw_fd_ostream File(Filename.c_str(), ErrorInfo);
136     std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
137
138     if (ErrorInfo.empty())
139       WriteGraph(File, Graph, IsSimple, Title);
140     else
141       errs() << "  error opening file for writing!";
142     errs() << "\n";
143
144     return false;
145   }
146
147   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
148     AU.setPreservesAll();
149     AU.addRequired<AnalysisT>();
150   }
151
152 private:
153   std::string Name;
154 };
155
156 } // end namespace llvm
157
158 #endif