LLVM: include/llvm/Analysis/DOTGraphTraitsPass.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
14#define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15
19#include <unordered_set>
20
21static std::unordered_setstd::string nameObj;
22
23namespace llvm {
24
25
26
27
28template <typename Result, typename GraphT = Result *>
30 static GraphT getGraph(Result R) { return &R; }
31};
32
33template
35 bool IsSimple) {
37
39 GraphName + " for '" + F.getName() + "' function");
40}
41
42template <typename AnalysisT, bool IsSimple,
43 typename GraphT = typename AnalysisT::Result *,
44 typename AnalysisGraphTraitsT =
45 DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
47 : PassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
48 AnalysisGraphTraitsT>> {
50
51
52
53
54
55
56
58 const typename AnalysisT::Result &Result) {
59 return true;
60 }
61
63 auto &Result = FAM.getResult(F);
66
67 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
69
71 };
72
73protected:
74
75
76
77
78
79
80
81
82
84
85private:
87};
88
89static inline void shortenFileName(std::string &FN, unsigned char len = 250) {
90 if (FN.length() > len)
91 FN.resize(len);
92 auto strLen = FN.length();
93 while (strLen > 0) {
94 if (nameObj.insert(FN).second)
95 break;
96 FN.resize(--len);
97 strLen--;
98 }
99}
100
101template
103 bool IsSimple) {
104 std::string Filename = Name.str() + "." + F.getName().str();
106 Filename = Filename + ".dot";
107 std::error_code EC;
108
109 errs() << "Writing '" << Filename << "'...";
110
113
114 if (!EC)
116 GraphName + " for '" + F.getName() + "' function");
117 else
118 errs() << " error opening file for writing!";
119 errs() << "\n";
120}
121
122template <typename AnalysisT, bool IsSimple,
123 typename GraphT = typename AnalysisT::Result *,
124 typename AnalysisGraphTraitsT =
125 DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
127 : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
128 AnalysisGraphTraitsT>> {
130
131
132
133
134
135
136
138 const typename AnalysisT::Result &Result) {
139 return true;
140 }
141
143 auto &Result = FAM.getResult(F);
146
147 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
148
150
152 };
153
154protected:
155
156
157
158
159
160
161
162
163
165
166private:
168};
169
170
171
172
173template <typename AnalysisT, typename GraphT = AnalysisT *>
177
178template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
179 typename AnalysisGraphTraitsT =
180 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
182public:
185
186
187
188
189
190
191
195
198
200 return false;
201
202 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
204
205 return false;
206 }
207
212
213private:
214 std::string Name;
215};
216
217template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
218 typename AnalysisGraphTraitsT =
219 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
221public:
224
225
226
227
228
229
230
234
237
239 return false;
240
241 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
243
244 return false;
245 }
246
251
252private:
253 std::string Name;
254};
255
256template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
257 typename AnalysisGraphTraitsT =
258 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
260public:
263
267
268 ViewGraph(Graph, Name, IsSimple, Title);
269
270 return false;
271 }
272
277
278private:
279 std::string Name;
280};
281
282template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
283 typename AnalysisGraphTraitsT =
284 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
286public:
289
293 std::string Filename = Name + ".dot";
294 std::error_code EC;
295
296 errs() << "Writing '" << Filename << "'...";
297
300
301 if (!EC)
302 WriteGraph(File, Graph, IsSimple, Title);
303 else
304 errs() << " error opening file for writing!";
305 errs() << "\n";
306
307 return false;
308 }
309
314
315private:
316 std::string Name;
317};
318
319template
321 std::string FileNamePrefix, bool IsSimple) {
322 std::string Filename = FileNamePrefix + "." + F.getName().str();
324 Filename = Filename + ".dot";
325 std::error_code EC;
326
327 errs() << "Writing '" << Filename << "'...";
328
331 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
332
333 if (!EC)
334 WriteGraph(File, Graph, IsSimple, Title);
335 else
336 errs() << " error opening file for writing!";
337 errs() << "\n";
338}
339
340}
341
342#endif
block Block Frequency Analysis
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static std::unordered_set< std::string > nameObj
Definition DOTGraphTraitsPass.h:21
FunctionAnalysisManager FAM
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
DOTGraphTraitsModulePrinterWrapperPass(StringRef GraphName, char &ID)
Definition DOTGraphTraitsPass.h:287
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition DOTGraphTraitsPass.h:290
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition DOTGraphTraitsPass.h:310
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition DOTGraphTraitsPass.h:273
DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName, char &ID)
Definition DOTGraphTraitsPass.h:261
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition DOTGraphTraitsPass.h:264
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition DOTGraphTraitsPass.h:235
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition DOTGraphTraitsPass.h:247
DOTGraphTraitsPrinterWrapperPass(StringRef GraphName, char &ID)
Definition DOTGraphTraitsPass.h:222
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition DOTGraphTraitsPass.h:231
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition DOTGraphTraitsPass.h:196
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition DOTGraphTraitsPass.h:192
DOTGraphTraitsViewerWrapperPass(StringRef GraphName, char &ID)
Definition DOTGraphTraitsPass.h:183
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition DOTGraphTraitsPass.h:208
A Module instance is used to store all the information related to an LLVM module.
AnalysisType & getAnalysis() const
getAnalysis() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
StringRef - Represent a constant reference to a string, i.e.
A raw_ostream that writes to a file descriptor.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
This is an optimization pass for GlobalISel generic memory operations.
static void shortenFileName(std::string &FN, unsigned char len=250)
Definition DOTGraphTraitsPass.h:89
void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name, bool IsSimple)
Definition DOTGraphTraitsPass.h:34
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
void WriteDOTGraphToFile(Function &F, GraphT &&Graph, std::string FileNamePrefix, bool IsSimple)
Definition DOTGraphTraitsPass.h:320
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void printGraphForFunction(Function &F, GraphT Graph, StringRef Name, bool IsSimple)
Definition DOTGraphTraitsPass.h:102
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Definition DOTGraphTraitsPass.h:142
~DOTGraphTraitsPrinter()=default
Avoid compiler warning "has virtual functions but non-virtual destructor[-Wnon-virtual-dtor]" in deri...
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
Definition DOTGraphTraitsPass.h:137
DOTGraphTraitsPrinter(StringRef GraphName)
Definition DOTGraphTraitsPass.h:129
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Definition DOTGraphTraitsPass.h:62
~DOTGraphTraitsViewer()=default
Avoid compiler warning "has virtual functions but non-virtual destructor[-Wnon-virtual-dtor]" in deri...
DOTGraphTraitsViewer(StringRef GraphName)
Definition DOTGraphTraitsPass.h:49
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
Definition DOTGraphTraitsPass.h:57
Default traits class for extracting a graph from an analysis pass.
Definition DOTGraphTraitsPass.h:29
static GraphT getGraph(Result R)
Definition DOTGraphTraitsPass.h:30
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
Default traits class for extracting a graph from an analysis pass.
Definition DOTGraphTraitsPass.h:174
static GraphT getGraph(AnalysisT *A)
Definition DOTGraphTraitsPass.h:175
A CRTP mix-in to automatically provide informational APIs needed for passes.