1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
17 #include "llvm/Analysis/CFGPrinter.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/FileSystem.h"
23 /// \brief Default traits class for extracting a graph from an analysis pass.
25 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
26 template <typename AnalysisT, typename GraphT = AnalysisT *>
27 struct DefaultAnalysisGraphTraits {
28 static GraphT getGraph(AnalysisT *A) { return A; }
32 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
33 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
34 class DOTGraphTraitsViewer : public FunctionPass {
36 DOTGraphTraitsViewer(StringRef GraphName, char &ID)
37 : FunctionPass(ID), Name(GraphName) {}
39 /// @brief Return true if this function should be processed.
41 /// An implementation of this class my override this function to indicate that
42 /// only certain functions should be viewed.
44 /// @param Analysis The current analysis result for this function.
45 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
49 bool runOnFunction(Function &F) override {
50 auto &Analysis = getAnalysis<AnalysisT>();
52 if (!processFunction(F, Analysis))
55 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
56 std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
57 std::string Title = GraphName + " for '" + F.getName().str() + "' function";
59 ViewGraph(Graph, Name, IsSimple, Title);
64 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.addRequired<AnalysisT>();
74 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
75 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
76 class DOTGraphTraitsPrinter : public FunctionPass {
78 DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
79 : FunctionPass(ID), Name(GraphName) {}
81 /// @brief Return true if this function should be processed.
83 /// An implementation of this class my override this function to indicate that
84 /// only certain functions should be printed.
86 /// @param Analysis The current analysis result for this function.
87 virtual bool processFunction(Function &F, AnalysisT &Analysis) {
91 bool runOnFunction(Function &F) override {
92 auto &Analysis = getAnalysis<AnalysisT>();
94 if (!processFunction(F, Analysis))
97 GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
98 std::string Filename = Name + "." + F.getName().str() + ".dot";
101 errs() << "Writing '" << Filename << "'...";
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";
108 WriteGraph(File, Graph, IsSimple, Title);
110 errs() << " error opening file for writing!";
116 void getAnalysisUsage(AnalysisUsage &AU) const override {
117 AU.setPreservesAll();
118 AU.addRequired<AnalysisT>();
126 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
127 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
128 class DOTGraphTraitsModuleViewer : public ModulePass {
130 DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
131 : ModulePass(ID), Name(GraphName) {}
133 bool runOnModule(Module &M) override {
134 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
135 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
137 ViewGraph(Graph, Name, IsSimple, Title);
142 void getAnalysisUsage(AnalysisUsage &AU) const override {
143 AU.setPreservesAll();
144 AU.addRequired<AnalysisT>();
152 typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
153 typename AnalysisGraphTraitsT = DefaultAnalysisGraphTraits<AnalysisT> >
154 class DOTGraphTraitsModulePrinter : public ModulePass {
156 DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
157 : ModulePass(ID), Name(GraphName) {}
159 bool runOnModule(Module &M) override {
160 GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
161 std::string Filename = Name + ".dot";
164 errs() << "Writing '" << Filename << "'...";
166 raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
167 std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
170 WriteGraph(File, Graph, IsSimple, Title);
172 errs() << " error opening file for writing!";
178 void getAnalysisUsage(AnalysisUsage &AU) const override {
179 AU.setPreservesAll();
180 AU.addRequired<AnalysisT>();
187 } // end namespace llvm