]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
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 | ||
970d7e83 LB |
14 | #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H |
15 | #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H | |
223e47cc | 16 | |
223e47cc | 17 | #include "llvm/Analysis/CFGPrinter.h" |
970d7e83 | 18 | #include "llvm/Pass.h" |
1a4d82fc | 19 | #include "llvm/Support/FileSystem.h" |
223e47cc LB |
20 | |
21 | namespace llvm { | |
223e47cc | 22 | |
1a4d82fc JJ |
23 | /// \brief Default traits class for extracting a graph from an analysis pass. |
24 | /// | |
25 | /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through. | |
26 | template <typename AnalysisT, typename GraphT = AnalysisT *> | |
27 | struct DefaultAnalysisGraphTraits { | |
28 | static GraphT getGraph(AnalysisT *A) { return A; } | |
29 | }; | |
30 | ||
31 | template < | |
32 | typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *, | |
33 | typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> > | |
970d7e83 LB |
34 | class DOTGraphTraitsViewer : public FunctionPass { |
35 | public: | |
36 | DOTGraphTraitsViewer(StringRef GraphName, char &ID) | |
1a4d82fc | 37 | : FunctionPass(ID), Name(GraphName) {} |
223e47cc | 38 | |
1a4d82fc JJ |
39 | bool runOnFunction(Function &F) override { |
40 | GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>()); | |
41 | std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph); | |
970d7e83 LB |
42 | std::string Title = GraphName + " for '" + F.getName().str() + "' function"; |
43 | ||
1a4d82fc | 44 | ViewGraph(Graph, Name, IsSimple, Title); |
223e47cc LB |
45 | |
46 | return false; | |
47 | } | |
48 | ||
1a4d82fc | 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
223e47cc | 50 | AU.setPreservesAll(); |
1a4d82fc | 51 | AU.addRequired<AnalysisT>(); |
223e47cc | 52 | } |
970d7e83 LB |
53 | |
54 | private: | |
55 | std::string Name; | |
223e47cc LB |
56 | }; |
57 | ||
1a4d82fc JJ |
58 | template < |
59 | typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *, | |
60 | typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> > | |
970d7e83 LB |
61 | class DOTGraphTraitsPrinter : public FunctionPass { |
62 | public: | |
63 | DOTGraphTraitsPrinter(StringRef GraphName, char &ID) | |
1a4d82fc | 64 | : FunctionPass(ID), Name(GraphName) {} |
970d7e83 | 65 | |
1a4d82fc JJ |
66 | bool runOnFunction(Function &F) override { |
67 | GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>()); | |
970d7e83 | 68 | std::string Filename = Name + "." + F.getName().str() + ".dot"; |
1a4d82fc | 69 | std::error_code EC; |
970d7e83 LB |
70 | |
71 | errs() << "Writing '" << Filename << "'..."; | |
223e47cc | 72 | |
1a4d82fc JJ |
73 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
74 | std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph); | |
970d7e83 LB |
75 | std::string Title = GraphName + " for '" + F.getName().str() + "' function"; |
76 | ||
1a4d82fc JJ |
77 | if (!EC) |
78 | WriteGraph(File, Graph, IsSimple, Title); | |
970d7e83 LB |
79 | else |
80 | errs() << " error opening file for writing!"; | |
81 | errs() << "\n"; | |
82 | ||
83 | return false; | |
84 | } | |
85 | ||
1a4d82fc | 86 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
970d7e83 | 87 | AU.setPreservesAll(); |
1a4d82fc | 88 | AU.addRequired<AnalysisT>(); |
970d7e83 LB |
89 | } |
90 | ||
91 | private: | |
223e47cc | 92 | std::string Name; |
970d7e83 LB |
93 | }; |
94 | ||
1a4d82fc JJ |
95 | template < |
96 | typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *, | |
97 | typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> > | |
970d7e83 LB |
98 | class DOTGraphTraitsModuleViewer : public ModulePass { |
99 | public: | |
100 | DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID) | |
1a4d82fc | 101 | : ModulePass(ID), Name(GraphName) {} |
223e47cc | 102 | |
1a4d82fc JJ |
103 | bool runOnModule(Module &M) override { |
104 | GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>()); | |
105 | std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph); | |
970d7e83 | 106 | |
1a4d82fc | 107 | ViewGraph(Graph, Name, IsSimple, Title); |
970d7e83 LB |
108 | |
109 | return false; | |
223e47cc LB |
110 | } |
111 | ||
1a4d82fc | 112 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
970d7e83 | 113 | AU.setPreservesAll(); |
1a4d82fc | 114 | AU.addRequired<AnalysisT>(); |
970d7e83 | 115 | } |
223e47cc | 116 | |
970d7e83 LB |
117 | private: |
118 | std::string Name; | |
119 | }; | |
120 | ||
1a4d82fc JJ |
121 | template < |
122 | typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *, | |
123 | typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> > | |
970d7e83 LB |
124 | class DOTGraphTraitsModulePrinter : public ModulePass { |
125 | public: | |
126 | DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID) | |
1a4d82fc | 127 | : ModulePass(ID), Name(GraphName) {} |
970d7e83 | 128 | |
1a4d82fc JJ |
129 | bool runOnModule(Module &M) override { |
130 | GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>()); | |
970d7e83 | 131 | std::string Filename = Name + ".dot"; |
1a4d82fc | 132 | std::error_code EC; |
223e47cc | 133 | |
970d7e83 LB |
134 | errs() << "Writing '" << Filename << "'..."; |
135 | ||
1a4d82fc JJ |
136 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
137 | std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph); | |
223e47cc | 138 | |
1a4d82fc JJ |
139 | if (!EC) |
140 | WriteGraph(File, Graph, IsSimple, Title); | |
223e47cc LB |
141 | else |
142 | errs() << " error opening file for writing!"; | |
143 | errs() << "\n"; | |
970d7e83 | 144 | |
223e47cc LB |
145 | return false; |
146 | } | |
147 | ||
1a4d82fc | 148 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
223e47cc | 149 | AU.setPreservesAll(); |
1a4d82fc | 150 | AU.addRequired<AnalysisT>(); |
223e47cc | 151 | } |
970d7e83 LB |
152 | |
153 | private: | |
154 | std::string Name; | |
223e47cc | 155 | }; |
970d7e83 LB |
156 | |
157 | } // end namespace llvm | |
158 | ||
223e47cc | 159 | #endif |