LLVM  16.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 
20 namespace llvm {
21 
22 /// Default traits class for extracting a graph from an analysis pass.
23 ///
24 /// This assumes that 'GraphT' is 'AnalysisT::Result *', and pass it through
25 template <typename Result, typename GraphT = Result *>
27  static GraphT getGraph(Result R) { return &R; }
28 };
29 
30 template <typename GraphT>
31 void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name,
32  bool IsSimple) {
33  std::string GraphName = DOTGraphTraits<GraphT *>::getGraphName(&Graph);
34 
35  ViewGraph(Graph, Name, IsSimple,
36  GraphName + " for '" + F.getName() + "' function");
37 }
38 
39 template <typename AnalysisT, bool IsSimple,
40  typename GraphT = typename AnalysisT::Result *,
41  typename AnalysisGraphTraitsT =
42  DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
44  : PassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
45  AnalysisGraphTraitsT>> {
46  DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
47 
48  /// Return true if this function should be processed.
49  ///
50  /// An implementation of this class my override this function to indicate that
51  /// only certain functions should be viewed.
52  ///
53  /// @param Result The current analysis result for this function.
54  virtual bool processFunction(Function &F,
55  const typename AnalysisT::Result &Result) {
56  return true;
57  }
58 
60  auto &Result = FAM.getResult<AnalysisT>(F);
61  if (!processFunction(F, Result))
62  return PreservedAnalyses::all();
63 
64  GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
65  viewGraphForFunction(F, Graph, Name, IsSimple);
66 
67  return PreservedAnalyses::all();
68  };
69 
70 protected:
71  /// Avoid compiler warning "has virtual functions but non-virtual destructor
72  /// [-Wnon-virtual-dtor]" in derived classes.
73  ///
74  /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
75  /// implementation of viewer passes, ie there should be no
76  /// runtime-polymorphisms/downcasting involving this class and hence no
77  /// virtual destructor needed. Making this dtor protected stops accidental
78  /// invocation when the derived class destructor should have been called.
79  /// Those derived classes sould be marked final to avoid the warning.
81 
82 private:
83  StringRef Name;
84 };
85 
86 template <typename GraphT>
87 void printGraphForFunction(Function &F, GraphT Graph, StringRef Name,
88  bool IsSimple) {
89  std::string Filename = Name.str() + "." + F.getName().str() + ".dot";
90  std::error_code EC;
91 
92  errs() << "Writing '" << Filename << "'...";
93 
94  raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
95  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
96 
97  if (!EC)
98  WriteGraph(File, Graph, IsSimple,
99  GraphName + " for '" + F.getName() + "' function");
100  else
101  errs() << " error opening file for writing!";
102  errs() << "\n";
103 }
104 
105 template <typename AnalysisT, bool IsSimple,
106  typename GraphT = typename AnalysisT::Result *,
107  typename AnalysisGraphTraitsT =
108  DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
110  : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
111  AnalysisGraphTraitsT>> {
112  DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
113 
114  /// Return true if this function should be processed.
115  ///
116  /// An implementation of this class my override this function to indicate that
117  /// only certain functions should be viewed.
118  ///
119  /// @param Result The current analysis result for this function.
120  virtual bool processFunction(Function &F,
121  const typename AnalysisT::Result &Result) {
122  return true;
123  }
124 
126  auto &Result = FAM.getResult<AnalysisT>(F);
127  if (!processFunction(F, Result))
128  return PreservedAnalyses::all();
129 
130  GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
131 
132  printGraphForFunction(F, Graph, Name, IsSimple);
133 
134  return PreservedAnalyses::all();
135  };
136 
137 protected:
138  /// Avoid compiler warning "has virtual functions but non-virtual destructor
139  /// [-Wnon-virtual-dtor]" in derived classes.
140  ///
141  /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
142  /// implementation of printer passes, ie there should be no
143  /// runtime-polymorphisms/downcasting involving this class and hence no
144  /// virtual destructor needed. Making this dtor protected stops accidental
145  /// invocation when the derived class destructor should have been called.
146  /// Those derived classes sould be marked final to avoid the warning.
148 
149 private:
150  StringRef Name;
151 };
152 
153 /// Default traits class for extracting a graph from an analysis pass.
154 ///
155 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
156 template <typename AnalysisT, typename GraphT = AnalysisT *>
158  static GraphT getGraph(AnalysisT *A) { return A; }
159 };
160 
161 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
162  typename AnalysisGraphTraitsT =
163  LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
165 public:
167  : FunctionPass(ID), Name(GraphName) {}
168 
169  /// Return true if this function should be processed.
170  ///
171  /// An implementation of this class my override this function to indicate that
172  /// only certain functions should be viewed.
173  ///
174  /// @param Analysis The current analysis result for this function.
175  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
176  return true;
177  }
178 
179  bool runOnFunction(Function &F) override {
180  auto &Analysis = getAnalysis<AnalysisT>();
181 
182  if (!processFunction(F, Analysis))
183  return false;
184 
185  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
186  viewGraphForFunction(F, Graph, Name, IsSimple);
187 
188  return false;
189  }
190 
191  void getAnalysisUsage(AnalysisUsage &AU) const override {
192  AU.setPreservesAll();
193  AU.addRequired<AnalysisT>();
194  }
195 
196 private:
197  std::string Name;
198 };
199 
200 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
201  typename AnalysisGraphTraitsT =
202  LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
204 public:
206  : FunctionPass(ID), Name(GraphName) {}
207 
208  /// Return true if this function should be processed.
209  ///
210  /// An implementation of this class my override this function to indicate that
211  /// only certain functions should be printed.
212  ///
213  /// @param Analysis The current analysis result for this function.
214  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
215  return true;
216  }
217 
218  bool runOnFunction(Function &F) override {
219  auto &Analysis = getAnalysis<AnalysisT>();
220 
221  if (!processFunction(F, Analysis))
222  return false;
223 
224  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
225  printGraphForFunction(F, Graph, Name, IsSimple);
226 
227  return false;
228  }
229 
230  void getAnalysisUsage(AnalysisUsage &AU) const override {
231  AU.setPreservesAll();
232  AU.addRequired<AnalysisT>();
233  }
234 
235 private:
236  std::string Name;
237 };
238 
239 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
240  typename AnalysisGraphTraitsT =
241  LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
243 public:
245  : ModulePass(ID), Name(GraphName) {}
246 
247  bool runOnModule(Module &M) override {
248  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
249  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
250 
251  ViewGraph(Graph, Name, IsSimple, Title);
252 
253  return false;
254  }
255 
256  void getAnalysisUsage(AnalysisUsage &AU) const override {
257  AU.setPreservesAll();
258  AU.addRequired<AnalysisT>();
259  }
260 
261 private:
262  std::string Name;
263 };
264 
265 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
266  typename AnalysisGraphTraitsT =
267  LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
269 public:
271  : ModulePass(ID), Name(GraphName) {}
272 
273  bool runOnModule(Module &M) override {
274  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
275  std::string Filename = Name + ".dot";
276  std::error_code EC;
277 
278  errs() << "Writing '" << Filename << "'...";
279 
280  raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
281  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
282 
283  if (!EC)
284  WriteGraph(File, Graph, IsSimple, Title);
285  else
286  errs() << " error opening file for writing!";
287  errs() << "\n";
288 
289  return false;
290  }
291 
292  void getAnalysisUsage(AnalysisUsage &AU) const override {
293  AU.setPreservesAll();
294  AU.addRequired<AnalysisT>();
295  }
296 
297 private:
298  std::string Name;
299 };
300 
301 template <typename GraphT>
302 void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
303  std::string FileNamePrefix, bool IsSimple) {
304  std::string Filename = FileNamePrefix + "." + F.getName().str() + ".dot";
305  std::error_code EC;
306 
307  errs() << "Writing '" << Filename << "'...";
308 
309  raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
310  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
311  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
312 
313  if (!EC)
314  WriteGraph(File, Graph, IsSimple, Title);
315  else
316  errs() << " error opening file for writing!";
317  errs() << "\n";
318 }
319 
320 } // end namespace llvm
321 
322 #endif
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FileSystem.h
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
llvm::DOTGraphTraitsPrinter::DOTGraphTraitsPrinter
DOTGraphTraitsPrinter(StringRef GraphName)
Definition: DOTGraphTraitsPass.h:112
llvm::DOTGraphTraitsModuleViewerWrapperPass::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: DOTGraphTraitsPass.h:247
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::DOTGraphTraitsViewer
Definition: DOTGraphTraitsPass.h:43
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
llvm::ViewGraph
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
llvm::DOTGraphTraitsModuleViewerWrapperPass
Definition: DOTGraphTraitsPass.h:242
llvm::DOTGraphTraitsPrinterWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: DOTGraphTraitsPass.h:218
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::DOTGraphTraitsPrinter::processFunction
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:120
llvm::DOTGraphTraitsPrinter::~DOTGraphTraitsPrinter
~DOTGraphTraitsPrinter()
Avoid compiler warning "has virtual functions but non-virtual destructor [-Wnon-virtual-dtor]" in der...
Definition: DOTGraphTraitsPass.h:147
llvm::DOTGraphTraitsModulePrinterWrapperPass::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: DOTGraphTraitsPass.h:273
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::printGraphForFunction
void printGraphForFunction(Function &F, GraphT Graph, StringRef Name, bool IsSimple)
Definition: DOTGraphTraitsPass.h:87
llvm::DOTGraphTraitsPrinterWrapperPass::processFunction
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:214
llvm::DOTGraphTraitsViewer::DOTGraphTraitsViewer
DOTGraphTraitsViewer(StringRef GraphName)
Definition: DOTGraphTraitsPass.h:46
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::sys::fs::OF_TextWithCRLF
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
llvm::LegacyDefaultAnalysisGraphTraits
Default traits class for extracting a graph from an analysis pass.
Definition: DOTGraphTraitsPass.h:157
llvm::DOTGraphTraitsPrinterWrapperPass::DOTGraphTraitsPrinterWrapperPass
DOTGraphTraitsPrinterWrapperPass(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:205
llvm::DOTGraphTraitsModuleViewerWrapperPass::DOTGraphTraitsModuleViewerWrapperPass
DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:244
llvm::DOTGraphTraitsPrinter::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Definition: DOTGraphTraitsPass.h:125
CFGPrinter.h
llvm::DOTGraphTraitsViewerWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: DOTGraphTraitsPass.h:191
llvm::DefaultDOTGraphTraits::getGraphName
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
Definition: DOTGraphTraits.h:44
llvm::WriteGraph
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:359
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::DOTGraphTraitsPrinterWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: DOTGraphTraitsPass.h:230
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::DefaultAnalysisGraphTraits
Default traits class for extracting a graph from an analysis pass.
Definition: DOTGraphTraitsPass.h:26
llvm::LegacyDefaultAnalysisGraphTraits::getGraph
static GraphT getGraph(AnalysisT *A)
Definition: DOTGraphTraitsPass.h:158
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:440
Analysis
block Block Frequency Analysis
Definition: BlockFrequencyInfo.cpp:301
llvm::WriteDOTGraphToFile
void WriteDOTGraphToFile(Function &F, GraphT &&Graph, std::string FileNamePrefix, bool IsSimple)
Definition: DOTGraphTraitsPass.h:302
llvm::DefaultAnalysisGraphTraits::getGraph
static GraphT getGraph(Result R)
Definition: DOTGraphTraitsPass.h:27
GraphWriter.h
llvm::DOTGraphTraitsViewer::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Definition: DOTGraphTraitsPass.h:59
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::DOTGraphTraitsViewerWrapperPass::processFunction
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:175
llvm::DOTGraphTraitsViewer::processFunction
virtual bool processFunction(Function &F, const typename AnalysisT::Result &Result)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:54
llvm::DOTGraphTraitsViewerWrapperPass::DOTGraphTraitsViewerWrapperPass
DOTGraphTraitsViewerWrapperPass(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:166
llvm::DOTGraphTraitsViewerWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: DOTGraphTraitsPass.h:179
llvm::DOTGraphTraitsModulePrinterWrapperPass::DOTGraphTraitsModulePrinterWrapperPass
DOTGraphTraitsModulePrinterWrapperPass(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:270
llvm::DOTGraphTraitsModulePrinterWrapperPass
Definition: DOTGraphTraitsPass.h:268
llvm::DOTGraphTraitsPrinter
Definition: DOTGraphTraitsPass.h:109
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::DOTGraphTraitsViewerWrapperPass
Definition: DOTGraphTraitsPass.h:164
llvm::DOTGraphTraitsViewer::~DOTGraphTraitsViewer
~DOTGraphTraitsViewer()
Avoid compiler warning "has virtual functions but non-virtual destructor [-Wnon-virtual-dtor]" in der...
Definition: DOTGraphTraitsPass.h:80
llvm::DOTGraphTraitsModulePrinterWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: DOTGraphTraitsPass.h:292
llvm::DOTGraphTraitsPrinterWrapperPass
Definition: DOTGraphTraitsPass.h:203
llvm::viewGraphForFunction
void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name, bool IsSimple)
Definition: DOTGraphTraitsPass.h:31
llvm::DOTGraphTraitsModuleViewerWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: DOTGraphTraitsPass.h:256