1 //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Templates to create dotty viewer and printer passes for GraphTraits graphs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
14 #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
15 
16 #include "llvm/Analysis/CFGPrinter.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/GraphWriter.h"
19 #include <unordered_set>
20 
21 static std::unordered_set<std::string> nameObj;
22 
23 namespace llvm {
24 
25 /// Default traits class for extracting a graph from an analysis pass.
26 ///
27 /// This assumes that 'GraphT' is 'AnalysisT::Result *', and pass it through
28 template <typename Result, typename GraphT = Result *>
29 struct DefaultAnalysisGraphTraits {
getGraphDefaultAnalysisGraphTraits30   static GraphT getGraph(Result R) { return &R; }
31 };
32 
33 template <typename GraphT>
viewGraphForFunction(Function & F,GraphT Graph,StringRef Name,bool IsSimple)34 void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name,
35                           bool IsSimple) {
36   std::string GraphName = DOTGraphTraits<GraphT *>::getGraphName(&Graph);
37 
38   ViewGraph(Graph, Name, IsSimple,
39             GraphName + " for '" + F.getName() + "' function");
40 }
41 
42 template <typename AnalysisT, bool IsSimple,
43           typename GraphT = typename AnalysisT::Result *,
44           typename AnalysisGraphTraitsT =
45               DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
46 struct DOTGraphTraitsViewer
47     : PassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
48                                          AnalysisGraphTraitsT>> {
DOTGraphTraitsViewerDOTGraphTraitsViewer49   DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
50 
51   /// Return true if this function should be processed.
52   ///
53   /// An implementation of this class my override this function to indicate that
54   /// only certain functions should be viewed.
55   ///
56   /// @param Result The current analysis result for this function.
processFunctionDOTGraphTraitsViewer57   virtual bool processFunction(Function &F,
58                                const typename AnalysisT::Result &Result) {
59     return true;
60   }
61 
runDOTGraphTraitsViewer62   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
63     auto &Result = FAM.getResult<AnalysisT>(F);
64     if (!processFunction(F, Result))
65       return PreservedAnalyses::all();
66 
67     GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
68     viewGraphForFunction(F, Graph, Name, IsSimple);
69 
70     return PreservedAnalyses::all();
71   };
72 
73 protected:
74   /// Avoid compiler warning "has virtual functions but non-virtual destructor
75   /// [-Wnon-virtual-dtor]" in derived classes.
76   ///
77   /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
78   /// implementation of viewer passes, ie there should be no
79   /// runtime-polymorphisms/downcasting involving this class and hence no
80   /// virtual destructor needed. Making this dtor protected stops accidental
81   /// invocation when the derived class destructor should have been called.
82   /// Those derived classes sould be marked final to avoid the warning.
~DOTGraphTraitsViewerDOTGraphTraitsViewer83   ~DOTGraphTraitsViewer() {}
84 
85 private:
86   StringRef Name;
87 };
88 
89 static inline void shortenFileName(std::string &FN, unsigned char len = 250) {
90 
91   FN = FN.substr(0, len);
92 
93   auto strLen = FN.length();
94   while (strLen > 0) {
95     if (auto it = nameObj.find(FN); it != nameObj.end()) {
96       FN = FN.substr(0, --len);
97     } else {
98       nameObj.insert(FN);
99       break;
100     }
101     strLen--;
102   }
103 }
104 
105 template <typename GraphT>
printGraphForFunction(Function & F,GraphT Graph,StringRef Name,bool IsSimple)106 void printGraphForFunction(Function &F, GraphT Graph, StringRef Name,
107                            bool IsSimple) {
108   std::string Filename = Name.str() + "." + F.getName().str();
109   shortenFileName(Filename);
110   Filename = Filename + ".dot";
111   std::error_code EC;
112 
113   errs() << "Writing '" << Filename << "'...";
114 
115   raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
116   std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
117 
118   if (!EC)
119     WriteGraph(File, Graph, IsSimple,
120                GraphName + " for '" + F.getName() + "' function");
121   else
122     errs() << "  error opening file for writing!";
123   errs() << "\n";
124 }
125 
126 template <typename AnalysisT, bool IsSimple,
127           typename GraphT = typename AnalysisT::Result *,
128           typename AnalysisGraphTraitsT =
129               DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
130 struct DOTGraphTraitsPrinter
131     : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
132                                           AnalysisGraphTraitsT>> {
DOTGraphTraitsPrinterDOTGraphTraitsPrinter133   DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
134 
135   /// Return true if this function should be processed.
136   ///
137   /// An implementation of this class my override this function to indicate that
138   /// only certain functions should be viewed.
139   ///
140   /// @param Result The current analysis result for this function.
processFunctionDOTGraphTraitsPrinter141   virtual bool processFunction(Function &F,
142                                const typename AnalysisT::Result &Result) {
143     return true;
144   }
145 
runDOTGraphTraitsPrinter146   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
147     auto &Result = FAM.getResult<AnalysisT>(F);
148     if (!processFunction(F, Result))
149       return PreservedAnalyses::all();
150 
151     GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
152 
153     printGraphForFunction(F, Graph, Name, IsSimple);
154 
155     return PreservedAnalyses::all();
156   };
157 
158 protected:
159   /// Avoid compiler warning "has virtual functions but non-virtual destructor
160   /// [-Wnon-virtual-dtor]" in derived classes.
161   ///
162   /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
163   /// implementation of printer passes, ie there should be no
164   /// runtime-polymorphisms/downcasting involving this class and hence no
165   /// virtual destructor needed. Making this dtor protected stops accidental
166   /// invocation when the derived class destructor should have been called.
167   /// Those derived classes sould be marked final to avoid the warning.
~DOTGraphTraitsPrinterDOTGraphTraitsPrinter168   ~DOTGraphTraitsPrinter() {}
169 
170 private:
171   StringRef Name;
172 };
173 
174 /// Default traits class for extracting a graph from an analysis pass.
175 ///
176 /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
177 template <typename AnalysisT, typename GraphT = AnalysisT *>
178 struct LegacyDefaultAnalysisGraphTraits {
getGraphLegacyDefaultAnalysisGraphTraits179   static GraphT getGraph(AnalysisT *A) { return A; }
180 };
181 
182 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
183           typename AnalysisGraphTraitsT =
184               LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
185 class DOTGraphTraitsViewerWrapperPass : public FunctionPass {
186 public:
DOTGraphTraitsViewerWrapperPass(StringRef GraphName,char & ID)187   DOTGraphTraitsViewerWrapperPass(StringRef GraphName, char &ID)
188       : FunctionPass(ID), Name(GraphName) {}
189 
190   /// Return true if this function should be processed.
191   ///
192   /// An implementation of this class my override this function to indicate that
193   /// only certain functions should be viewed.
194   ///
195   /// @param Analysis The current analysis result for this function.
processFunction(Function & F,AnalysisT & Analysis)196   virtual bool processFunction(Function &F, AnalysisT &Analysis) {
197     return true;
198   }
199 
runOnFunction(Function & F)200   bool runOnFunction(Function &F) override {
201     auto &Analysis = getAnalysis<AnalysisT>();
202 
203     if (!processFunction(F, Analysis))
204       return false;
205 
206     GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
207     viewGraphForFunction(F, Graph, Name, IsSimple);
208 
209     return false;
210   }
211 
getAnalysisUsage(AnalysisUsage & AU)212   void getAnalysisUsage(AnalysisUsage &AU) const override {
213     AU.setPreservesAll();
214     AU.addRequired<AnalysisT>();
215   }
216 
217 private:
218   std::string Name;
219 };
220 
221 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
222           typename AnalysisGraphTraitsT =
223               LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
224 class DOTGraphTraitsPrinterWrapperPass : public FunctionPass {
225 public:
DOTGraphTraitsPrinterWrapperPass(StringRef GraphName,char & ID)226   DOTGraphTraitsPrinterWrapperPass(StringRef GraphName, char &ID)
227       : FunctionPass(ID), Name(GraphName) {}
228 
229   /// Return true if this function should be processed.
230   ///
231   /// An implementation of this class my override this function to indicate that
232   /// only certain functions should be printed.
233   ///
234   /// @param Analysis The current analysis result for this function.
processFunction(Function & F,AnalysisT & Analysis)235   virtual bool processFunction(Function &F, AnalysisT &Analysis) {
236     return true;
237   }
238 
runOnFunction(Function & F)239   bool runOnFunction(Function &F) override {
240     auto &Analysis = getAnalysis<AnalysisT>();
241 
242     if (!processFunction(F, Analysis))
243       return false;
244 
245     GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
246     printGraphForFunction(F, Graph, Name, IsSimple);
247 
248     return false;
249   }
250 
getAnalysisUsage(AnalysisUsage & AU)251   void getAnalysisUsage(AnalysisUsage &AU) const override {
252     AU.setPreservesAll();
253     AU.addRequired<AnalysisT>();
254   }
255 
256 private:
257   std::string Name;
258 };
259 
260 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
261           typename AnalysisGraphTraitsT =
262               LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
263 class DOTGraphTraitsModuleViewerWrapperPass : public ModulePass {
264 public:
DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName,char & ID)265   DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName, char &ID)
266       : ModulePass(ID), Name(GraphName) {}
267 
runOnModule(Module & M)268   bool runOnModule(Module &M) override {
269     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
270     std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
271 
272     ViewGraph(Graph, Name, IsSimple, Title);
273 
274     return false;
275   }
276 
getAnalysisUsage(AnalysisUsage & AU)277   void getAnalysisUsage(AnalysisUsage &AU) const override {
278     AU.setPreservesAll();
279     AU.addRequired<AnalysisT>();
280   }
281 
282 private:
283   std::string Name;
284 };
285 
286 template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
287           typename AnalysisGraphTraitsT =
288               LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
289 class DOTGraphTraitsModulePrinterWrapperPass : public ModulePass {
290 public:
DOTGraphTraitsModulePrinterWrapperPass(StringRef GraphName,char & ID)291   DOTGraphTraitsModulePrinterWrapperPass(StringRef GraphName, char &ID)
292       : ModulePass(ID), Name(GraphName) {}
293 
runOnModule(Module & M)294   bool runOnModule(Module &M) override {
295     GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
296     shortenFileName(Name);
297     std::string Filename = Name + ".dot";
298     std::error_code EC;
299 
300     errs() << "Writing '" << Filename << "'...";
301 
302     raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
303     std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
304 
305     if (!EC)
306       WriteGraph(File, Graph, IsSimple, Title);
307     else
308       errs() << "  error opening file for writing!";
309     errs() << "\n";
310 
311     return false;
312   }
313 
getAnalysisUsage(AnalysisUsage & AU)314   void getAnalysisUsage(AnalysisUsage &AU) const override {
315     AU.setPreservesAll();
316     AU.addRequired<AnalysisT>();
317   }
318 
319 private:
320   std::string Name;
321 };
322 
323 template <typename GraphT>
WriteDOTGraphToFile(Function & F,GraphT && Graph,std::string FileNamePrefix,bool IsSimple)324 void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
325                          std::string FileNamePrefix, bool IsSimple) {
326   std::string Filename = FileNamePrefix + "." + F.getName().str();
327   shortenFileName(Filename);
328   Filename = Filename + ".dot";
329   std::error_code EC;
330 
331   errs() << "Writing '" << Filename << "'...";
332 
333   raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
334   std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
335   std::string Title = GraphName + " for '" + F.getName().str() + "' function";
336 
337   if (!EC)
338     WriteGraph(File, Graph, IsSimple, Title);
339   else
340     errs() << "  error opening file for writing!";
341   errs() << "\n";
342 }
343 
344 } // end namespace llvm
345 
346 #endif
347