LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
19 
20 namespace llvm {
21 
22 template <class Analysis, bool Simple>
24 public:
25  DOTGraphTraitsViewer(StringRef GraphName, char &ID)
26  : FunctionPass(ID), Name(GraphName) {}
27 
28  virtual bool runOnFunction(Function &F) {
29  Analysis *Graph = &getAnalysis<Analysis>();
30  std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
31  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
32 
33  ViewGraph(Graph, Name, Simple, Title);
34 
35  return false;
36  }
37 
38  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39  AU.setPreservesAll();
40  AU.addRequired<Analysis>();
41  }
42 
43 private:
44  std::string Name;
45 };
46 
47 template <class Analysis, bool Simple>
49 public:
51  : FunctionPass(ID), Name(GraphName) {}
52 
53  virtual bool runOnFunction(Function &F) {
54  Analysis *Graph = &getAnalysis<Analysis>();
55  std::string Filename = Name + "." + F.getName().str() + ".dot";
56  std::string ErrorInfo;
57 
58  errs() << "Writing '" << Filename << "'...";
59 
60  raw_fd_ostream File(Filename.c_str(), ErrorInfo);
61  std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
62  std::string Title = GraphName + " for '" + F.getName().str() + "' function";
63 
64  if (ErrorInfo.empty())
65  WriteGraph(File, Graph, Simple, Title);
66  else
67  errs() << " error opening file for writing!";
68  errs() << "\n";
69 
70  return false;
71  }
72 
73  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74  AU.setPreservesAll();
75  AU.addRequired<Analysis>();
76  }
77 
78 private:
79  std::string Name;
80 };
81 
82 template <class Analysis, bool Simple>
84 public:
86  : ModulePass(ID), Name(GraphName) {}
87 
88  virtual bool runOnModule(Module &M) {
89  Analysis *Graph = &getAnalysis<Analysis>();
90  std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
91 
92  ViewGraph(Graph, Name, Simple, Title);
93 
94  return false;
95  }
96 
97  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98  AU.setPreservesAll();
99  AU.addRequired<Analysis>();
100  }
101 
102 private:
103  std::string Name;
104 };
105 
106 template <class Analysis, bool Simple>
108 public:
110  : ModulePass(ID), Name(GraphName) {}
111 
112  virtual bool runOnModule(Module &M) {
113  Analysis *Graph = &getAnalysis<Analysis>();
114  std::string Filename = Name + ".dot";
115  std::string ErrorInfo;
116 
117  errs() << "Writing '" << Filename << "'...";
118 
119  raw_fd_ostream File(Filename.c_str(), ErrorInfo);
120  std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
121 
122  if (ErrorInfo.empty())
123  WriteGraph(File, Graph, Simple, Title);
124  else
125  errs() << " error opening file for writing!";
126  errs() << "\n";
127 
128  return false;
129  }
130 
131  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
132  AU.setPreservesAll();
133  AU.addRequired<Analysis>();
134  }
135 
136 private:
137  std::string Name;
138 };
139 
140 } // end namespace llvm
141 
142 #endif
virtual void getAnalysisUsage(AnalysisUsage &AU) const
raw_ostream & errs()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
raw_ostream & WriteGraph(raw_ostream &O, const EdgeBundles &G, bool ShortNames=false, const Twine &Title="")
Specialize WriteGraph, the standard implementation won't work.
Definition: EdgeBundles.cpp:78
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
F(f)
StringRef getName() const
Definition: Value.cpp:167
AnalysisUsage & addRequired()
DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
virtual void getAnalysisUsage(AnalysisUsage &AU) const
virtual bool runOnFunction(Function &F)
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:346
virtual void getAnalysisUsage(AnalysisUsage &AU) const
DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
block Block Frequency Analysis
virtual void getAnalysisUsage(AnalysisUsage &AU) const
virtual bool runOnModule(Module &M)
virtual bool runOnModule(Module &M)
virtual bool runOnFunction(Function &F)
static std::string getGraphName(const GraphType &)
DOTGraphTraitsViewer(StringRef GraphName, char &ID)