LLVM  6.0.0svn
DOTGraphTraitsPass.h
Go to the documentation of this file.
1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
16 
18 #include "llvm/Pass.h"
20 
21 namespace llvm {
22 
23 /// \brief Default traits class for extracting a graph from an analysis pass.
24 ///
25 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
26 template <typename AnalysisT, typename GraphT = AnalysisT *>
28  static GraphT getGraph(AnalysisT *A) { return A; }
29 };
30 
31 template <
32  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
33  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
35 public:
36  DOTGraphTraitsViewer(StringRef GraphName, char &ID)
37  : FunctionPass(ID), Name(GraphName) {}
38 
39  /// @brief Return true if this function should be processed.
40  ///
41  /// An implementation of this class my override this function to indicate that
42  /// only certain functions should be viewed.
43  ///
44  /// @param Analysis The current analysis result for this function.
45  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
46  return true;
47  }
48 
49  bool runOnFunction(Function &F) override {
50  auto &Analysis = getAnalysis<AnalysisT>();
51 
52  if (!processFunction(F, Analysis))
53  return false;
54 
55  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
56  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
57  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
58 
59  ViewGraph(Graph, Name, IsSimple, Title);
60 
61  return false;
62  }
63 
64  void getAnalysisUsage(AnalysisUsage &AU) const override {
65  AU.setPreservesAll();
66  AU.addRequired<AnalysisT>();
67  }
68 
69 private:
70  std::string Name;
71 };
72 
73 template <
74  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
75  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
77 public:
79  : FunctionPass(ID), Name(GraphName) {}
80 
81  /// @brief Return true if this function should be processed.
82  ///
83  /// An implementation of this class my override this function to indicate that
84  /// only certain functions should be printed.
85  ///
86  /// @param Analysis The current analysis result for this function.
87  virtual bool processFunction(Function &F, AnalysisT &Analysis) {
88  return true;
89  }
90 
91  bool runOnFunction(Function &F) override {
92  auto &Analysis = getAnalysis<AnalysisT>();
93 
94  if (!processFunction(F, Analysis))
95  return false;
96 
97  GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
98  std::string Filename = Name + "." + F.getName().str() + ".dot";
99  std::error_code EC;
100 
101  errs() << "Writing '" << Filename << "'...";
102 
103  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
104  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
105  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
106 
107  if (!EC)
108  WriteGraph(File, Graph, IsSimple, Title);
109  else
110  errs() << " error opening file for writing!";
111  errs() << "\n";
112 
113  return false;
114  }
115 
116  void getAnalysisUsage(AnalysisUsage &AU) const override {
117  AU.setPreservesAll();
118  AU.addRequired<AnalysisT>();
119  }
120 
121 private:
122  std::string Name;
123 };
124 
125 template <
126  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
127  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
129 public:
131  : ModulePass(ID), Name(GraphName) {}
132 
133  bool runOnModule(Module &M) override {
134  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
135  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
136 
137  ViewGraph(Graph, Name, IsSimple, Title);
138 
139  return false;
140  }
141 
142  void getAnalysisUsage(AnalysisUsage &AU) const override {
143  AU.setPreservesAll();
144  AU.addRequired<AnalysisT>();
145  }
146 
147 private:
148  std::string Name;
149 };
150 
151 template <
152  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
153  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
155 public:
157  : ModulePass(ID), Name(GraphName) {}
158 
159  bool runOnModule(Module &M) override {
160  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
161  std::string Filename = Name + ".dot";
162  std::error_code EC;
163 
164  errs() << "Writing '" << Filename << "'...";
165 
166  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
167  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
168 
169  if (!EC)
170  WriteGraph(File, Graph, IsSimple, Title);
171  else
172  errs() << " error opening file for writing!";
173  errs() << "\n";
174 
175  return false;
176  }
177 
178  void getAnalysisUsage(AnalysisUsage &AU) const override {
179  AU.setPreservesAll();
180  AU.addRequired<AnalysisT>();
181  }
182 
183 private:
184  std::string Name;
185 };
186 
187 } // end namespace llvm
188 
189 #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:228
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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...
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...
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
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...
#define F(x, y, z)
Definition: MD5.cpp:55
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...
const char * Name
virtual bool processFunction(Function &F, AnalysisT &Analysis)
Return true if this function should be processed.
DOTGraphTraitsViewer(StringRef GraphName, char &ID)
#define A
Definition: LargeTest.cpp:12
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
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 &#39;dot&#39;, run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:348
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
void setPreservesAll()
Set by analyses that do not transform their input at all.
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:708
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:357
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:218
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:235
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:49
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)