LLVM  14.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 
17 
18 namespace llvm {
19 
20 /// Default traits class for extracting a graph from an analysis pass.
21 ///
22 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
23 template <typename AnalysisT, typename GraphT = AnalysisT *>
25  static GraphT getGraph(AnalysisT *A) { return A; }
26 };
27 
28 template <
29  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
30  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
32 public:
33  DOTGraphTraitsViewer(StringRef GraphName, char &ID)
34  : FunctionPass(ID), Name(GraphName) {}
35 
36  /// Return true if this function should be processed.
37  ///
38  /// An implementation of this class my override this function to indicate that
39  /// only certain functions should be viewed.
40  ///
41  /// @param Analysis The current analysis result for this function.
42  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
43  return true;
44  }
45 
46  bool runOnFunction(Function &F) override {
47  auto &Analysis = getAnalysis<AnalysisT>();
48 
49  if (!processFunction(F, Analysis))
50  return false;
51 
52  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
53  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
54  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
55 
56  ViewGraph(Graph, Name, IsSimple, Title);
57 
58  return false;
59  }
60 
61  void getAnalysisUsage(AnalysisUsage &AU) const override {
62  AU.setPreservesAll();
63  AU.addRequired<AnalysisT>();
64  }
65 
66 private:
67  std::string Name;
68 };
69 
70 template <
71  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
72  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
74 public:
76  : FunctionPass(ID), Name(GraphName) {}
77 
78  /// Return true if this function should be processed.
79  ///
80  /// An implementation of this class my override this function to indicate that
81  /// only certain functions should be printed.
82  ///
83  /// @param Analysis The current analysis result for this function.
84  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
85  return true;
86  }
87 
88  bool runOnFunction(Function &F) override {
89  auto &Analysis = getAnalysis<AnalysisT>();
90 
91  if (!processFunction(F, Analysis))
92  return false;
93 
94  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
95  std::string Filename = Name + "." + F.getName().str() + ".dot";
96  std::error_code EC;
97 
98  errs() << "Writing '" << Filename << "'...";
99 
101  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
102  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
103 
104  if (!EC)
105  WriteGraph(File, Graph, IsSimple, Title);
106  else
107  errs() << " error opening file for writing!";
108  errs() << "\n";
109 
110  return false;
111  }
112 
113  void getAnalysisUsage(AnalysisUsage &AU) const override {
114  AU.setPreservesAll();
115  AU.addRequired<AnalysisT>();
116  }
117 
118 private:
119  std::string Name;
120 };
121 
122 template <
123  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
124  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
126 public:
128  : ModulePass(ID), Name(GraphName) {}
129 
130  bool runOnModule(Module &M) override {
131  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
132  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
133 
134  ViewGraph(Graph, Name, IsSimple, Title);
135 
136  return false;
137  }
138 
139  void getAnalysisUsage(AnalysisUsage &AU) const override {
140  AU.setPreservesAll();
141  AU.addRequired<AnalysisT>();
142  }
143 
144 private:
145  std::string Name;
146 };
147 
148 template <
149  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
150  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
152 public:
154  : ModulePass(ID), Name(GraphName) {}
155 
156  bool runOnModule(Module &M) override {
157  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
158  std::string Filename = Name + ".dot";
159  std::error_code EC;
160 
161  errs() << "Writing '" << Filename << "'...";
162 
164  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
165 
166  if (!EC)
167  WriteGraph(File, Graph, IsSimple, Title);
168  else
169  errs() << " error opening file for writing!";
170  errs() << "\n";
171 
172  return false;
173  }
174 
175  void getAnalysisUsage(AnalysisUsage &AU) const override {
176  AU.setPreservesAll();
177  AU.addRequired<AnalysisT>();
178  }
179 
180 private:
181  std::string Name;
182 };
183 
184 } // end namespace llvm
185 
186 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::DOTGraphTraitsViewer::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: DOTGraphTraitsPass.h:46
llvm::DOTGraphTraitsViewer::processFunction
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:42
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:62
llvm::DOTGraphTraitsModulePrinter::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:175
llvm::DOTGraphTraitsModuleViewer
Definition: DOTGraphTraitsPass.h:125
llvm::DefaultAnalysisGraphTraits::getGraph
static GraphT getGraph(AnalysisT *A)
Definition: DOTGraphTraitsPass.h:25
llvm::DOTGraphTraitsPrinter::DOTGraphTraitsPrinter
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:75
Analysis
block Block Frequency Analysis
Definition: BlockFrequencyInfo.cpp:302
llvm::DOTGraphTraitsViewer::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:61
llvm::DOTGraphTraitsModuleViewer::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:139
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
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:430
llvm::DOTGraphTraitsModuleViewer::DOTGraphTraitsModuleViewer
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:127
llvm::DOTGraphTraitsPrinter::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:113
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::DOTGraphTraitsViewer
Definition: DOTGraphTraitsPass.h:31
llvm::DOTGraphTraitsModulePrinter::DOTGraphTraitsModulePrinter
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:153
llvm::DOTGraphTraitsPrinter
Definition: DOTGraphTraitsPass.h:73
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
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::DOTGraphTraitsViewer::DOTGraphTraitsViewer
DOTGraphTraitsViewer(StringRef GraphName, char &ID)
Definition: DOTGraphTraitsPass.h:33
CFGPrinter.h
llvm::DefaultDOTGraphTraits::getGraphName
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
Definition: DOTGraphTraits.h:44
llvm::DOTGraphTraitsPrinter::processFunction
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Definition: DOTGraphTraitsPass.h:84
llvm::WriteGraph
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:362
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::DefaultAnalysisGraphTraits
Default traits class for extracting a graph from an analysis pass.
Definition: DOTGraphTraitsPass.h:24
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:443
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::DOTGraphTraitsPrinter::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: DOTGraphTraitsPass.h:88
llvm::DOTGraphTraitsModulePrinter
Definition: DOTGraphTraitsPass.h:151
llvm::DOTGraphTraitsModuleViewer::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: DOTGraphTraitsPass.h:130
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205
llvm::DOTGraphTraitsModulePrinter::runOnModule
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
Definition: DOTGraphTraitsPass.h:156
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38