1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
14 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
16 #include "llvm/Analysis/CFGPrinter.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/FileSystem.h"
22 /// Default traits class for extracting a graph from an analysis pass.
24 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
25 template <typename AnalysisT
, typename GraphT
= AnalysisT
*>
26 struct DefaultAnalysisGraphTraits
{
27 static GraphT
getGraph(AnalysisT
*A
) { return A
; }
31 typename AnalysisT
, bool IsSimple
, typename GraphT
= AnalysisT
*,
32 typename AnalysisGraphTraitsT
= DefaultAnalysisGraphTraits
<AnalysisT
, GraphT
> >
33 class DOTGraphTraitsViewer
: public FunctionPass
{
35 DOTGraphTraitsViewer(StringRef GraphName
, char &ID
)
36 : FunctionPass(ID
), Name(GraphName
) {}
38 /// Return true if this function should be processed.
40 /// An implementation of this class my override this function to indicate that
41 /// only certain functions should be viewed.
43 /// @param Analysis The current analysis result for this function.
44 virtual bool processFunction(Function
&F
, AnalysisT
&Analysis
) {
48 bool runOnFunction(Function
&F
) override
{
49 auto &Analysis
= getAnalysis
<AnalysisT
>();
51 if (!processFunction(F
, Analysis
))
54 GraphT Graph
= AnalysisGraphTraitsT::getGraph(&Analysis
);
55 std::string GraphName
= DOTGraphTraits
<GraphT
>::getGraphName(Graph
);
56 std::string Title
= GraphName
+ " for '" + F
.getName().str() + "' function";
58 ViewGraph(Graph
, Name
, IsSimple
, Title
);
63 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
65 AU
.addRequired
<AnalysisT
>();
73 typename AnalysisT
, bool IsSimple
, typename GraphT
= AnalysisT
*,
74 typename AnalysisGraphTraitsT
= DefaultAnalysisGraphTraits
<AnalysisT
, GraphT
> >
75 class DOTGraphTraitsPrinter
: public FunctionPass
{
77 DOTGraphTraitsPrinter(StringRef GraphName
, char &ID
)
78 : FunctionPass(ID
), Name(GraphName
) {}
80 /// Return true if this function should be processed.
82 /// An implementation of this class my override this function to indicate that
83 /// only certain functions should be printed.
85 /// @param Analysis The current analysis result for this function.
86 virtual bool processFunction(Function
&F
, AnalysisT
&Analysis
) {
90 bool runOnFunction(Function
&F
) override
{
91 auto &Analysis
= getAnalysis
<AnalysisT
>();
93 if (!processFunction(F
, Analysis
))
96 GraphT Graph
= AnalysisGraphTraitsT::getGraph(&Analysis
);
97 std::string Filename
= Name
+ "." + F
.getName().str() + ".dot";
100 errs() << "Writing '" << Filename
<< "'...";
102 raw_fd_ostream
File(Filename
, EC
, sys::fs::OF_Text
);
103 std::string GraphName
= DOTGraphTraits
<GraphT
>::getGraphName(Graph
);
104 std::string Title
= GraphName
+ " for '" + F
.getName().str() + "' function";
107 WriteGraph(File
, Graph
, IsSimple
, Title
);
109 errs() << " error opening file for writing!";
115 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
116 AU
.setPreservesAll();
117 AU
.addRequired
<AnalysisT
>();
125 typename AnalysisT
, bool IsSimple
, typename GraphT
= AnalysisT
*,
126 typename AnalysisGraphTraitsT
= DefaultAnalysisGraphTraits
<AnalysisT
, GraphT
> >
127 class DOTGraphTraitsModuleViewer
: public ModulePass
{
129 DOTGraphTraitsModuleViewer(StringRef GraphName
, char &ID
)
130 : ModulePass(ID
), Name(GraphName
) {}
132 bool runOnModule(Module
&M
) override
{
133 GraphT Graph
= AnalysisGraphTraitsT::getGraph(&getAnalysis
<AnalysisT
>());
134 std::string Title
= DOTGraphTraits
<GraphT
>::getGraphName(Graph
);
136 ViewGraph(Graph
, Name
, IsSimple
, Title
);
141 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
142 AU
.setPreservesAll();
143 AU
.addRequired
<AnalysisT
>();
151 typename AnalysisT
, bool IsSimple
, typename GraphT
= AnalysisT
*,
152 typename AnalysisGraphTraitsT
= DefaultAnalysisGraphTraits
<AnalysisT
, GraphT
> >
153 class DOTGraphTraitsModulePrinter
: public ModulePass
{
155 DOTGraphTraitsModulePrinter(StringRef GraphName
, char &ID
)
156 : ModulePass(ID
), Name(GraphName
) {}
158 bool runOnModule(Module
&M
) override
{
159 GraphT Graph
= AnalysisGraphTraitsT::getGraph(&getAnalysis
<AnalysisT
>());
160 std::string Filename
= Name
+ ".dot";
163 errs() << "Writing '" << Filename
<< "'...";
165 raw_fd_ostream
File(Filename
, EC
, sys::fs::OF_Text
);
166 std::string Title
= DOTGraphTraits
<GraphT
>::getGraphName(Graph
);
169 WriteGraph(File
, Graph
, IsSimple
, Title
);
171 errs() << " error opening file for writing!";
177 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
178 AU
.setPreservesAll();
179 AU
.addRequired
<AnalysisT
>();
186 } // end namespace llvm