LLVM  9.0.0svn
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 #include "llvm/Pass.h"
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 *' and so just passes it through.
25 template <typename AnalysisT, typename GraphT = AnalysisT *>
27  static GraphT getGraph(AnalysisT *A) { return A; }
28 };
29 
30 template <
31  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
32  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
34 public:
35  DOTGraphTraitsViewer(StringRef GraphName, char &ID)
36  : FunctionPass(ID), Name(GraphName) {}
37 
38  /// Return true if this function should be processed.
39  ///
40  /// An implementation of this class my override this function to indicate that
41  /// only certain functions should be viewed.
42  ///
43  /// @param Analysis The current analysis result for this function.
44  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
45  return true;
46  }
47 
48  bool runOnFunction(Function &F) override {
49  auto &Analysis = getAnalysis<AnalysisT>();
50 
51  if (!processFunction(F, Analysis))
52  return false;
53 
54  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
55  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
56  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
57 
58  ViewGraph(Graph, Name, IsSimple, Title);
59 
60  return false;
61  }
62 
63  void getAnalysisUsage(AnalysisUsage &AU) const override {
64  AU.setPreservesAll();
65  AU.addRequired<AnalysisT>();
66  }
67 
68 private:
69  std::string Name;
70 };
71 
72 template <
73  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
74  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
76 public:
78  : FunctionPass(ID), Name(GraphName) {}
79 
80  /// Return true if this function should be processed.
81  ///
82  /// An implementation of this class my override this function to indicate that
83  /// only certain functions should be printed.
84  ///
85  /// @param Analysis The current analysis result for this function.
86  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
87  return true;
88  }
89 
90  bool runOnFunction(Function &F) override {
91  auto &Analysis = getAnalysis<AnalysisT>();
92 
93  if (!processFunction(F, Analysis))
94  return false;
95 
96  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
97  std::string Filename = Name + "." + F.getName().str() + ".dot";
98  std::error_code EC;
99 
100  errs() << "Writing '" << Filename << "'...";
101 
102  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
103  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
104  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
105 
106  if (!EC)
107  WriteGraph(File, Graph, IsSimple, Title);
108  else
109  errs() << " error opening file for writing!";
110  errs() << "\n";
111 
112  return false;
113  }
114 
115  void getAnalysisUsage(AnalysisUsage &AU) const override {
116  AU.setPreservesAll();
117  AU.addRequired<AnalysisT>();
118  }
119 
120 private:
121  std::string Name;
122 };
123 
124 template <
125  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
126  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
128 public:
130  : ModulePass(ID), Name(GraphName) {}
131 
132  bool runOnModule(Module &M) override {
133  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
134  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
135 
136  ViewGraph(Graph, Name, IsSimple, Title);
137 
138  return false;
139  }
140 
141  void getAnalysisUsage(AnalysisUsage &AU) const override {
142  AU.setPreservesAll();
143  AU.addRequired<AnalysisT>();
144  }
145 
146 private:
147  std::string Name;
148 };
149 
150 template <
151  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
152  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT, GraphT> >
154 public:
156  : ModulePass(ID), Name(GraphName) {}
157 
158  bool runOnModule(Module &M) override {
159  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
160  std::string Filename = Name + ".dot";
161  std::error_code EC;
162 
163  errs() << "Writing '" << Filename << "'...";
164 
165  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
166  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
167 
168  if (!EC)
169  WriteGraph(File, Graph, IsSimple, Title);
170  else
171  errs() << " error opening file for writing!";
172  errs() << "\n";
173 
174  return false;
175  }
176 
177  void getAnalysisUsage(AnalysisUsage &AU) const override {
178  AU.setPreservesAll();
179  AU.addRequired<AnalysisT>();
180  }
181 
182 private:
183  std::string Name;
184 };
185 
186 } // end namespace llvm
187 
188 #endif
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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...
F(f)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
*ViewGraph Emit a dot run run gv on the postscript *then cleanup For use from the debugger *void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:366
AnalysisUsage & addRequired()
static GraphT getGraph(AnalysisT *A)
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:309
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.
DOTGraphTraitsViewer(StringRef GraphName, char &ID)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
void setPreservesAll()
Set by analyses that do not transform their input at all.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
block Block Frequency Analysis
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:365
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
Default traits class for extracting a graph from an analysis pass.
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)