LLVM 19.0.0git
DOTGraphTraitsPass.h
Go to the documentation of this file.
1//===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Templates to create dotty viewer and printer passes for GraphTraits graphs.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
14#define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15
19#include <unordered_set>
20
21static std::unordered_set<std::string> nameObj;
22
23namespace llvm {
24
25/// Default traits class for extracting a graph from an analysis pass.
26///
27/// This assumes that 'GraphT' is 'AnalysisT::Result *', and pass it through
28template <typename Result, typename GraphT = Result *>
30 static GraphT getGraph(Result R) { return &R; }
31};
32
33template <typename GraphT>
35 bool IsSimple) {
36 std::string GraphName = DOTGraphTraits<GraphT *>::getGraphName(&Graph);
37
38 ViewGraph(Graph, Name, IsSimple,
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>> {
49 DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
50
51 /// Return true if this function should be processed.
52 ///
53 /// An implementation of this class my override this function to indicate that
54 /// only certain functions should be viewed.
55 ///
56 /// @param Result The current analysis result for this function.
57 virtual bool processFunction(Function &F,
58 const typename AnalysisT::Result &Result) {
59 return true;
60 }
61
63 auto &Result = FAM.getResult<AnalysisT>(F);
64 if (!processFunction(F, Result))
66
67 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
68 viewGraphForFunction(F, Graph, Name, IsSimple);
69
71 };
72
73protected:
74 /// Avoid compiler warning "has virtual functions but non-virtual destructor
75 /// [-Wnon-virtual-dtor]" in derived classes.
76 ///
77 /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
78 /// implementation of viewer passes, ie there should be no
79 /// runtime-polymorphisms/downcasting involving this class and hence no
80 /// virtual destructor needed. Making this dtor protected stops accidental
81 /// invocation when the derived class destructor should have been called.
82 /// Those derived classes sould be marked final to avoid the warning.
84
85private:
86 StringRef Name;
87};
88
89static inline void shortenFileName(std::string &FN, unsigned char len = 250) {
90
91 FN = FN.substr(0, len);
92
93 auto strLen = FN.length();
94 while (strLen > 0) {
95 if (auto it = nameObj.find(FN); it != nameObj.end()) {
96 FN = FN.substr(0, --len);
97 } else {
98 nameObj.insert(FN);
99 break;
100 }
101 strLen--;
102 }
103}
104
105template <typename GraphT>
107 bool IsSimple) {
108 std::string Filename = Name.str() + "." + F.getName().str();
109 shortenFileName(Filename);
110 Filename = Filename + ".dot";
111 std::error_code EC;
112
113 errs() << "Writing '" << Filename << "'...";
114
115 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
116 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
117
118 if (!EC)
119 WriteGraph(File, Graph, IsSimple,
120 GraphName + " for '" + F.getName() + "' function");
121 else
122 errs() << " error opening file for writing!";
123 errs() << "\n";
124}
125
126template <typename AnalysisT, bool IsSimple,
127 typename GraphT = typename AnalysisT::Result *,
128 typename AnalysisGraphTraitsT =
129 DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
131 : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
132 AnalysisGraphTraitsT>> {
133 DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
134
135 /// Return true if this function should be processed.
136 ///
137 /// An implementation of this class my override this function to indicate that
138 /// only certain functions should be viewed.
139 ///
140 /// @param Result The current analysis result for this function.
142 const typename AnalysisT::Result &Result) {
143 return true;
144 }
145
147 auto &Result = FAM.getResult<AnalysisT>(F);
148 if (!processFunction(F, Result))
149 return PreservedAnalyses::all();
150
151 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
152
153 printGraphForFunction(F, Graph, Name, IsSimple);
154
155 return PreservedAnalyses::all();
156 };
157
158protected:
159 /// Avoid compiler warning "has virtual functions but non-virtual destructor
160 /// [-Wnon-virtual-dtor]" in derived classes.
161 ///
162 /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
163 /// implementation of printer passes, ie there should be no
164 /// runtime-polymorphisms/downcasting involving this class and hence no
165 /// virtual destructor needed. Making this dtor protected stops accidental
166 /// invocation when the derived class destructor should have been called.
167 /// Those derived classes sould be marked final to avoid the warning.
169
170private:
171 StringRef Name;
172};
173
174/// Default traits class for extracting a graph from an analysis pass.
175///
176/// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
177template <typename AnalysisT, typename GraphT = AnalysisT *>
179 static GraphT getGraph(AnalysisT *A) { return A; }
180};
181
182template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
183 typename AnalysisGraphTraitsT =
184 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
186public:
188 : FunctionPass(ID), Name(GraphName) {}
189
190 /// Return true if this function should be processed.
191 ///
192 /// An implementation of this class my override this function to indicate that
193 /// only certain functions should be viewed.
194 ///
195 /// @param Analysis The current analysis result for this function.
196 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
197 return true;
198 }
199
200 bool runOnFunction(Function &F) override {
201 auto &Analysis = getAnalysis<AnalysisT>();
202
204 return false;
205
206 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
207 viewGraphForFunction(F, Graph, Name, IsSimple);
208
209 return false;
210 }
211
212 void getAnalysisUsage(AnalysisUsage &AU) const override {
213 AU.setPreservesAll();
214 AU.addRequired<AnalysisT>();
215 }
216
217private:
218 std::string Name;
219};
220
221template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
222 typename AnalysisGraphTraitsT =
223 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
225public:
227 : FunctionPass(ID), Name(GraphName) {}
228
229 /// Return true if this function should be processed.
230 ///
231 /// An implementation of this class my override this function to indicate that
232 /// only certain functions should be printed.
233 ///
234 /// @param Analysis The current analysis result for this function.
235 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
236 return true;
237 }
238
239 bool runOnFunction(Function &F) override {
240 auto &Analysis = getAnalysis<AnalysisT>();
241
243 return false;
244
245 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
246 printGraphForFunction(F, Graph, Name, IsSimple);
247
248 return false;
249 }
250
251 void getAnalysisUsage(AnalysisUsage &AU) const override {
252 AU.setPreservesAll();
253 AU.addRequired<AnalysisT>();
254 }
255
256private:
257 std::string Name;
258};
259
260template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
261 typename AnalysisGraphTraitsT =
262 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
264public:
266 : ModulePass(ID), Name(GraphName) {}
267
268 bool runOnModule(Module &M) override {
269 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
270 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
271
272 ViewGraph(Graph, Name, IsSimple, Title);
273
274 return false;
275 }
276
277 void getAnalysisUsage(AnalysisUsage &AU) const override {
278 AU.setPreservesAll();
279 AU.addRequired<AnalysisT>();
280 }
281
282private:
283 std::string Name;
284};
285
286template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
287 typename AnalysisGraphTraitsT =
288 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
290public:
292 : ModulePass(ID), Name(GraphName) {}
293
294 bool runOnModule(Module &M) override {
295 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
296 shortenFileName(Name);
297 std::string Filename = Name + ".dot";
298 std::error_code EC;
299
300 errs() << "Writing '" << Filename << "'...";
301
302 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
303 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
304
305 if (!EC)
306 WriteGraph(File, Graph, IsSimple, Title);
307 else
308 errs() << " error opening file for writing!";
309 errs() << "\n";
310
311 return false;
312 }
313
314 void getAnalysisUsage(AnalysisUsage &AU) const override {
315 AU.setPreservesAll();
316 AU.addRequired<AnalysisT>();
317 }
318
319private:
320 std::string Name;
321};
322
323template <typename GraphT>
324void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
325 std::string FileNamePrefix, bool IsSimple) {
326 std::string Filename = FileNamePrefix + "." + F.getName().str();
327 shortenFileName(Filename);
328 Filename = Filename + ".dot";
329 std::error_code EC;
330
331 errs() << "Writing '" << Filename << "'...";
332
333 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
334 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
335 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
336
337 if (!EC)
338 WriteGraph(File, Graph, IsSimple, Title);
339 else
340 errs() << " error opening file for writing!";
341 errs() << "\n";
342}
343
344} // end namespace llvm
345
346#endif
block Block Frequency Analysis
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static std::unordered_set< std::string > nameObj
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
FunctionAnalysisManager FAM
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
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)
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName, char &ID)
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
DOTGraphTraitsPrinterWrapperPass(StringRef GraphName, char &ID)
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
DOTGraphTraitsViewerWrapperPass(StringRef GraphName, char &ID)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:470
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:768
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static void shortenFileName(std::string &FN, unsigned char len=250)
void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name, bool IsSimple)
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:359
void WriteDOTGraphToFile(Function &F, GraphT &&Graph, std::string FileNamePrefix, bool IsSimple)
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)
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.
Definition: GraphWriter.h:427
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
~DOTGraphTraitsPrinter()
Avoid compiler warning "has virtual functions but non-virtual destructor [-Wnon-virtual-dtor]" in der...
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
DOTGraphTraitsPrinter(StringRef GraphName)
~DOTGraphTraitsViewer()
Avoid compiler warning "has virtual functions but non-virtual destructor [-Wnon-virtual-dtor]" in der...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
DOTGraphTraitsViewer(StringRef GraphName)
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
Default traits class for extracting a graph from an analysis pass.
static GraphT getGraph(Result R)
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.
static GraphT getGraph(AnalysisT *A)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:91