LLVM 24.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
16#include "llvm/ADT/StringSet.h"
20
21namespace llvm {
22
23/// Default traits class for extracting a graph from an analysis pass.
24///
25/// This assumes that 'GraphT' is 'AnalysisT::Result *', and pass it through
26template <typename Result, typename GraphT = Result *>
28 static GraphT getGraph(Result R) { return &R; }
29};
30
31template <typename GraphT>
32void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name,
33 bool IsSimple) {
34 std::string GraphName = DOTGraphTraits<GraphT *>::getGraphName(&Graph);
35
36 ViewGraph(Graph, Name, IsSimple,
37 GraphName + " for '" + F.getName() + "' function");
38}
39
40template <typename AnalysisT, bool IsSimple,
41 typename GraphT = typename AnalysisT::Result *,
42 typename AnalysisGraphTraitsT =
43 DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
45 : RequiredPassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
46 AnalysisGraphTraitsT>> {
47 DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
48
49 /// Return true if this function should be processed.
50 ///
51 /// An implementation of this class my override this function to indicate that
52 /// only certain functions should be viewed.
53 ///
54 /// @param Result The current analysis result for this function.
55 virtual bool processFunction(Function &F,
56 const typename AnalysisT::Result &Result) {
57 return true;
58 }
59
61 auto &Result = FAM.getResult<AnalysisT>(F);
62 if (!processFunction(F, Result))
64
65 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
66 viewGraphForFunction(F, Graph, Name, IsSimple);
67
69 };
70
71protected:
72 /// Avoid compiler warning "has virtual functions but non-virtual destructor
73 /// [-Wnon-virtual-dtor]" in derived classes.
74 ///
75 /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
76 /// implementation of viewer passes, ie there should be no
77 /// runtime-polymorphisms/downcasting involving this class and hence no
78 /// virtual destructor needed. Making this dtor protected stops accidental
79 /// invocation when the derived class destructor should have been called.
80 /// Those derived classes sould be marked final to avoid the warning.
82
83private:
84 StringRef Name;
85};
86
87static inline void shortenFileName(std::string &FN, unsigned char len = 250) {
88 static llvm::StringSet<> nameObj;
89 if (FN.length() > len)
90 FN.resize(len);
91 auto strLen = FN.length();
92 while (strLen > 0) {
93 if (nameObj.insert(FN).second)
94 break;
95 FN.resize(--len);
96 strLen--;
97 }
98}
99
100template <typename GraphT>
101void printGraphForFunction(Function &F, GraphT Graph, StringRef Name,
102 bool IsSimple) {
103 std::string Filename = Name.str() + "." + F.getName().str();
105 Filename = Filename + ".dot";
106 std::error_code EC;
107
108 errs() << "Writing '" << Filename << "'...";
109
111 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
112
113 if (!EC)
114 WriteGraph(File, Graph, IsSimple,
115 GraphName + " for '" + F.getName() + "' function");
116 else
117 errs() << " error opening file for writing!";
118 errs() << "\n";
119}
120
121template <typename AnalysisT, bool IsSimple,
122 typename GraphT = typename AnalysisT::Result *,
123 typename AnalysisGraphTraitsT =
124 DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
126 : RequiredPassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
127 AnalysisGraphTraitsT>> {
128 DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
129
130 /// Return true if this function should be processed.
131 ///
132 /// An implementation of this class my override this function to indicate that
133 /// only certain functions should be viewed.
134 ///
135 /// @param Result The current analysis result for this function.
137 const typename AnalysisT::Result &Result) {
138 return true;
139 }
140
142 auto &Result = FAM.getResult<AnalysisT>(F);
143 if (!processFunction(F, Result))
144 return PreservedAnalyses::all();
145
146 GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
147
148 printGraphForFunction(F, Graph, Name, IsSimple);
149
150 return PreservedAnalyses::all();
151 };
152
153protected:
154 /// Avoid compiler warning "has virtual functions but non-virtual destructor
155 /// [-Wnon-virtual-dtor]" in derived classes.
156 ///
157 /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
158 /// implementation of printer passes, ie there should be no
159 /// runtime-polymorphisms/downcasting involving this class and hence no
160 /// virtual destructor needed. Making this dtor protected stops accidental
161 /// invocation when the derived class destructor should have been called.
162 /// Those derived classes sould be marked final to avoid the warning.
164
165private:
166 StringRef Name;
167};
168
169/// Default traits class for extracting a graph from an analysis pass.
170///
171/// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
172template <typename AnalysisT, typename GraphT = AnalysisT *>
174 static GraphT getGraph(AnalysisT *A) { return A; }
175};
176
177template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
178 typename AnalysisGraphTraitsT =
179 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
181public:
183 : FunctionPass(ID), Name(GraphName) {}
184
185 /// Return true if this function should be processed.
186 ///
187 /// An implementation of this class my override this function to indicate that
188 /// only certain functions should be viewed.
189 ///
190 /// @param Analysis The current analysis result for this function.
191 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
192 return true;
193 }
194
195 bool runOnFunction(Function &F) override {
197
199 return false;
200
201 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
202 viewGraphForFunction(F, Graph, Name, IsSimple);
203
204 return false;
205 }
206
207 void getAnalysisUsage(AnalysisUsage &AU) const override {
208 AU.setPreservesAll();
209 AU.addRequired<AnalysisT>();
210 }
211
212private:
213 std::string Name;
214};
215
216template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
217 typename AnalysisGraphTraitsT =
218 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
220public:
222 : FunctionPass(ID), Name(GraphName) {}
223
224 /// Return true if this function should be processed.
225 ///
226 /// An implementation of this class my override this function to indicate that
227 /// only certain functions should be printed.
228 ///
229 /// @param Analysis The current analysis result for this function.
230 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
231 return true;
232 }
233
234 bool runOnFunction(Function &F) override {
236
238 return false;
239
240 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
241 printGraphForFunction(F, Graph, Name, IsSimple);
242
243 return false;
244 }
245
246 void getAnalysisUsage(AnalysisUsage &AU) const override {
247 AU.setPreservesAll();
248 AU.addRequired<AnalysisT>();
249 }
250
251private:
252 std::string Name;
253};
254
255template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
256 typename AnalysisGraphTraitsT =
257 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
259public:
261 : ModulePass(ID), Name(GraphName) {}
262
263 bool runOnModule(Module &M) override {
264 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
265 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
266
267 ViewGraph(Graph, Name, IsSimple, Title);
268
269 return false;
270 }
271
272 void getAnalysisUsage(AnalysisUsage &AU) const override {
273 AU.setPreservesAll();
274 AU.addRequired<AnalysisT>();
275 }
276
277private:
278 std::string Name;
279};
280
281template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
282 typename AnalysisGraphTraitsT =
283 LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
285public:
287 : ModulePass(ID), Name(GraphName) {}
288
289 bool runOnModule(Module &M) override {
290 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
291 shortenFileName(Name);
292 std::string Filename = Name + ".dot";
293 std::error_code EC;
294
295 errs() << "Writing '" << Filename << "'...";
296
298 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
299
300 if (!EC)
301 WriteGraph(File, Graph, IsSimple, Title);
302 else
303 errs() << " error opening file for writing!";
304 errs() << "\n";
305
306 return false;
307 }
308
309 void getAnalysisUsage(AnalysisUsage &AU) const override {
310 AU.setPreservesAll();
311 AU.addRequired<AnalysisT>();
312 }
313
314private:
315 std::string Name;
316};
317
318} // end namespace llvm
319
320#endif
block Block Frequency Analysis
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition MD5.cpp:54
static constexpr StringLiteral Filename
FunctionAnalysisManager FAM
StringSet - A set-like wrapper for the StringMap.
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(char &pid)
Definition Pass.h:314
ModulePass(char &pid)
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - 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.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition StringSet.h:39
A raw_ostream that writes to a file descriptor.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:802
This is an optimization pass for GlobalISel generic memory operations.
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="")
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)
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)
~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.
DOTGraphTraitsPrinter(StringRef GraphName)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
~DOTGraphTraitsViewer()=default
Avoid compiler warning "has virtual functions but non-virtual destructor[-Wnon-virtual-dtor]" in deri...
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 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.
A CRTP mix-in for passes that should not be skipped.