LLVM 20.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 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 <typename GraphT>
103 bool IsSimple) {
104 std::string Filename = Name.str() + "." + F.getName().str();
105 shortenFileName(Filename);
106 Filename = Filename + ".dot";
107 std::error_code EC;
108
109 errs() << "Writing '" << Filename << "'...";
110
111 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
112 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
113
114 if (!EC)
115 WriteGraph(File, Graph, IsSimple,
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>> {
129 DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
130
131 /// Return true if this function should be processed.
132 ///
133 /// An implementation of this class my override this function to indicate that
134 /// only certain functions should be viewed.
135 ///
136 /// @param Result The current analysis result for this function.
138 const typename AnalysisT::Result &Result) {
139 return true;
140 }
141
143 auto &Result = FAM.getResult<AnalysisT>(F);
144 if (!processFunction(F, Result))
145 return PreservedAnalyses::all();
146
147 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
148
149 printGraphForFunction(F, Graph, Name, IsSimple);
150
151 return PreservedAnalyses::all();
152 };
153
154protected:
155 /// Avoid compiler warning "has virtual functions but non-virtual destructor
156 /// [-Wnon-virtual-dtor]" in derived classes.
157 ///
158 /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
159 /// implementation of printer passes, ie there should be no
160 /// runtime-polymorphisms/downcasting involving this class and hence no
161 /// virtual destructor needed. Making this dtor protected stops accidental
162 /// invocation when the derived class destructor should have been called.
163 /// Those derived classes sould be marked final to avoid the warning.
165
166private:
167 StringRef Name;
168};
169
170/// Default traits class for extracting a graph from an analysis pass.
171///
172/// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
173template <typename AnalysisT, typename GraphT = AnalysisT *>
175 static GraphT getGraph(AnalysisT *A) { return A; }
176};
177
178template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
179 typename AnalysisGraphTraitsT =
180 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
182public:
184 : FunctionPass(ID), Name(GraphName) {}
185
186 /// Return true if this function should be processed.
187 ///
188 /// An implementation of this class my override this function to indicate that
189 /// only certain functions should be viewed.
190 ///
191 /// @param Analysis The current analysis result for this function.
192 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
193 return true;
194 }
195
196 bool runOnFunction(Function &F) override {
197 auto &Analysis = getAnalysis<AnalysisT>();
198
200 return false;
201
202 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
203 viewGraphForFunction(F, Graph, Name, IsSimple);
204
205 return false;
206 }
207
208 void getAnalysisUsage(AnalysisUsage &AU) const override {
209 AU.setPreservesAll();
210 AU.addRequired<AnalysisT>();
211 }
212
213private:
214 std::string Name;
215};
216
217template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
218 typename AnalysisGraphTraitsT =
219 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
221public:
223 : FunctionPass(ID), Name(GraphName) {}
224
225 /// Return true if this function should be processed.
226 ///
227 /// An implementation of this class my override this function to indicate that
228 /// only certain functions should be printed.
229 ///
230 /// @param Analysis The current analysis result for this function.
231 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
232 return true;
233 }
234
235 bool runOnFunction(Function &F) override {
236 auto &Analysis = getAnalysis<AnalysisT>();
237
239 return false;
240
241 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
242 printGraphForFunction(F, Graph, Name, IsSimple);
243
244 return false;
245 }
246
247 void getAnalysisUsage(AnalysisUsage &AU) const override {
248 AU.setPreservesAll();
249 AU.addRequired<AnalysisT>();
250 }
251
252private:
253 std::string Name;
254};
255
256template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
257 typename AnalysisGraphTraitsT =
258 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
260public:
262 : ModulePass(ID), Name(GraphName) {}
263
264 bool runOnModule(Module &M) override {
265 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
266 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
267
268 ViewGraph(Graph, Name, IsSimple, Title);
269
270 return false;
271 }
272
273 void getAnalysisUsage(AnalysisUsage &AU) const override {
274 AU.setPreservesAll();
275 AU.addRequired<AnalysisT>();
276 }
277
278private:
279 std::string Name;
280};
281
282template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
283 typename AnalysisGraphTraitsT =
284 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
286public:
288 : ModulePass(ID), Name(GraphName) {}
289
290 bool runOnModule(Module &M) override {
291 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
292 shortenFileName(Name);
293 std::string Filename = Name + ".dot";
294 std::error_code EC;
295
296 errs() << "Writing '" << Filename << "'...";
297
298 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
299 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
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
310 void getAnalysisUsage(AnalysisUsage &AU) const override {
311 AU.setPreservesAll();
312 AU.addRequired<AnalysisT>();
313 }
314
315private:
316 std::string Name;
317};
318
319template <typename GraphT>
320void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
321 std::string FileNamePrefix, bool IsSimple) {
322 std::string Filename = FileNamePrefix + "." + F.getName().str();
323 shortenFileName(Filename);
324 Filename = Filename + ".dot";
325 std::error_code EC;
326
327 errs() << "Writing '" << Filename << "'...";
328
329 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
330 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
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} // end namespace llvm
341
342#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:253
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:310
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:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:460
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:767
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:69