LLVM  3.7.0
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  bool runOnFunction(Function &F) override {
40  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
41  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
42  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
43 
44  ViewGraph(Graph, Name, IsSimple, Title);
45 
46  return false;
47  }
48 
49  void getAnalysisUsage(AnalysisUsage &AU) const override {
50  AU.setPreservesAll();
51  AU.addRequired<AnalysisT>();
52  }
53 
54 private:
55  std::string Name;
56 };
57 
58 template <
59  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
60  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
62 public:
64  : FunctionPass(ID), Name(GraphName) {}
65 
66  bool runOnFunction(Function &F) override {
67  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
68  std::string Filename = Name + "." + F.getName().str() + ".dot";
69  std::error_code EC;
70 
71  errs() << "Writing '" << Filename << "'...";
72 
73  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
74  std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
75  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
76 
77  if (!EC)
78  WriteGraph(File, Graph, IsSimple, Title);
79  else
80  errs() << " error opening file for writing!";
81  errs() << "\n";
82 
83  return false;
84  }
85 
86  void getAnalysisUsage(AnalysisUsage &AU) const override {
87  AU.setPreservesAll();
88  AU.addRequired<AnalysisT>();
89  }
90 
91 private:
92  std::string Name;
93 };
94 
95 template <
96  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
97  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
99 public:
101  : ModulePass(ID), Name(GraphName) {}
102 
103  bool runOnModule(Module &M) override {
104  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
105  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
106 
107  ViewGraph(Graph, Name, IsSimple, Title);
108 
109  return false;
110  }
111 
112  void getAnalysisUsage(AnalysisUsage &AU) const override {
113  AU.setPreservesAll();
114  AU.addRequired<AnalysisT>();
115  }
116 
117 private:
118  std::string Name;
119 };
120 
121 template <
122  typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
123  typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
125 public:
127  : ModulePass(ID), Name(GraphName) {}
128 
129  bool runOnModule(Module &M) override {
130  GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
131  std::string Filename = Name + ".dot";
132  std::error_code EC;
133 
134  errs() << "Writing '" << Filename << "'...";
135 
136  raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
137  std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
138 
139  if (!EC)
140  WriteGraph(File, Graph, IsSimple, Title);
141  else
142  errs() << " error opening file for writing!";
143  errs() << "\n";
144 
145  return false;
146  }
147 
148  void getAnalysisUsage(AnalysisUsage &AU) const override {
149  AU.setPreservesAll();
150  AU.addRequired<AnalysisT>();
151  }
152 
153 private:
154  std::string Name;
155 };
156 
157 } // end namespace llvm
158 
159 #endif
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
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...
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:188
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)
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
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...
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:294
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: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:592
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
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:40
static std::string getGraphName(const GraphType &)
getGraphName - Return the label for the graph as a whole.
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)