DOTGraphTraitsPass.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Templates to create dotty viewer and printer passes for GraphTraits graphs.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
  18. #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
  19. #include "llvm/Analysis/CFGPrinter.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/GraphWriter.h"
  22. namespace llvm {
  23. /// Default traits class for extracting a graph from an analysis pass.
  24. ///
  25. /// This assumes that 'GraphT' is 'AnalysisT::Result *', and pass it through
  26. template <typename Result, typename GraphT = Result *>
  27. struct DefaultAnalysisGraphTraits {
  28. static GraphT getGraph(Result R) { return &R; }
  29. };
  30. template <typename GraphT>
  31. void viewGraphForFunction(Function &F, GraphT Graph, StringRef Name,
  32. bool IsSimple) {
  33. std::string GraphName = DOTGraphTraits<GraphT *>::getGraphName(&Graph);
  34. ViewGraph(Graph, Name, IsSimple,
  35. GraphName + " for '" + F.getName() + "' function");
  36. }
  37. template <typename AnalysisT, bool IsSimple,
  38. typename GraphT = typename AnalysisT::Result *,
  39. typename AnalysisGraphTraitsT =
  40. DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
  41. struct DOTGraphTraitsViewer
  42. : PassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
  43. AnalysisGraphTraitsT>> {
  44. DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
  45. /// Return true if this function should be processed.
  46. ///
  47. /// An implementation of this class my override this function to indicate that
  48. /// only certain functions should be viewed.
  49. ///
  50. /// @param Result The current analysis result for this function.
  51. virtual bool processFunction(Function &F,
  52. const typename AnalysisT::Result &Result) {
  53. return true;
  54. }
  55. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
  56. auto &Result = FAM.getResult<AnalysisT>(F);
  57. if (!processFunction(F, Result))
  58. return PreservedAnalyses::all();
  59. GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
  60. viewGraphForFunction(F, Graph, Name, IsSimple);
  61. return PreservedAnalyses::all();
  62. };
  63. protected:
  64. /// Avoid compiler warning "has virtual functions but non-virtual destructor
  65. /// [-Wnon-virtual-dtor]" in derived classes.
  66. ///
  67. /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
  68. /// implementation of viewer passes, ie there should be no
  69. /// runtime-polymorphisms/downcasting involving this class and hence no
  70. /// virtual destructor needed. Making this dtor protected stops accidental
  71. /// invocation when the derived class destructor should have been called.
  72. /// Those derived classes sould be marked final to avoid the warning.
  73. ~DOTGraphTraitsViewer() {}
  74. private:
  75. StringRef Name;
  76. };
  77. template <typename GraphT>
  78. void printGraphForFunction(Function &F, GraphT Graph, StringRef Name,
  79. bool IsSimple) {
  80. std::string Filename = Name.str() + "." + F.getName().str() + ".dot";
  81. std::error_code EC;
  82. errs() << "Writing '" << Filename << "'...";
  83. raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
  84. std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
  85. if (!EC)
  86. WriteGraph(File, Graph, IsSimple,
  87. GraphName + " for '" + F.getName() + "' function");
  88. else
  89. errs() << " error opening file for writing!";
  90. errs() << "\n";
  91. }
  92. template <typename AnalysisT, bool IsSimple,
  93. typename GraphT = typename AnalysisT::Result *,
  94. typename AnalysisGraphTraitsT =
  95. DefaultAnalysisGraphTraits<typename AnalysisT::Result &, GraphT>>
  96. struct DOTGraphTraitsPrinter
  97. : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
  98. AnalysisGraphTraitsT>> {
  99. DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
  100. /// Return true if this function should be processed.
  101. ///
  102. /// An implementation of this class my override this function to indicate that
  103. /// only certain functions should be viewed.
  104. ///
  105. /// @param Result The current analysis result for this function.
  106. virtual bool processFunction(Function &F,
  107. const typename AnalysisT::Result &Result) {
  108. return true;
  109. }
  110. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
  111. auto &Result = FAM.getResult<AnalysisT>(F);
  112. if (!processFunction(F, Result))
  113. return PreservedAnalyses::all();
  114. GraphT Graph = AnalysisGraphTraitsT::getGraph(Result);
  115. printGraphForFunction(F, Graph, Name, IsSimple);
  116. return PreservedAnalyses::all();
  117. };
  118. protected:
  119. /// Avoid compiler warning "has virtual functions but non-virtual destructor
  120. /// [-Wnon-virtual-dtor]" in derived classes.
  121. ///
  122. /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
  123. /// implementation of printer passes, ie there should be no
  124. /// runtime-polymorphisms/downcasting involving this class and hence no
  125. /// virtual destructor needed. Making this dtor protected stops accidental
  126. /// invocation when the derived class destructor should have been called.
  127. /// Those derived classes sould be marked final to avoid the warning.
  128. ~DOTGraphTraitsPrinter() {}
  129. private:
  130. StringRef Name;
  131. };
  132. /// Default traits class for extracting a graph from an analysis pass.
  133. ///
  134. /// This assumes that 'GraphT' is 'AnalysisT *' and so just passes it through.
  135. template <typename AnalysisT, typename GraphT = AnalysisT *>
  136. struct LegacyDefaultAnalysisGraphTraits {
  137. static GraphT getGraph(AnalysisT *A) { return A; }
  138. };
  139. template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
  140. typename AnalysisGraphTraitsT =
  141. LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
  142. class DOTGraphTraitsViewerWrapperPass : public FunctionPass {
  143. public:
  144. DOTGraphTraitsViewerWrapperPass(StringRef GraphName, char &ID)
  145. : FunctionPass(ID), Name(GraphName) {}
  146. /// Return true if this function should be processed.
  147. ///
  148. /// An implementation of this class my override this function to indicate that
  149. /// only certain functions should be viewed.
  150. ///
  151. /// @param Analysis The current analysis result for this function.
  152. virtual bool processFunction(Function &F, AnalysisT &Analysis) {
  153. return true;
  154. }
  155. bool runOnFunction(Function &F) override {
  156. auto &Analysis = getAnalysis<AnalysisT>();
  157. if (!processFunction(F, Analysis))
  158. return false;
  159. GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
  160. viewGraphForFunction(F, Graph, Name, IsSimple);
  161. return false;
  162. }
  163. void getAnalysisUsage(AnalysisUsage &AU) const override {
  164. AU.setPreservesAll();
  165. AU.addRequired<AnalysisT>();
  166. }
  167. private:
  168. std::string Name;
  169. };
  170. template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
  171. typename AnalysisGraphTraitsT =
  172. LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
  173. class DOTGraphTraitsPrinterWrapperPass : public FunctionPass {
  174. public:
  175. DOTGraphTraitsPrinterWrapperPass(StringRef GraphName, char &ID)
  176. : FunctionPass(ID), Name(GraphName) {}
  177. /// Return true if this function should be processed.
  178. ///
  179. /// An implementation of this class my override this function to indicate that
  180. /// only certain functions should be printed.
  181. ///
  182. /// @param Analysis The current analysis result for this function.
  183. virtual bool processFunction(Function &F, AnalysisT &Analysis) {
  184. return true;
  185. }
  186. bool runOnFunction(Function &F) override {
  187. auto &Analysis = getAnalysis<AnalysisT>();
  188. if (!processFunction(F, Analysis))
  189. return false;
  190. GraphT Graph = AnalysisGraphTraitsT::getGraph(&Analysis);
  191. printGraphForFunction(F, Graph, Name, IsSimple);
  192. return false;
  193. }
  194. void getAnalysisUsage(AnalysisUsage &AU) const override {
  195. AU.setPreservesAll();
  196. AU.addRequired<AnalysisT>();
  197. }
  198. private:
  199. std::string Name;
  200. };
  201. template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
  202. typename AnalysisGraphTraitsT =
  203. LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
  204. class DOTGraphTraitsModuleViewerWrapperPass : public ModulePass {
  205. public:
  206. DOTGraphTraitsModuleViewerWrapperPass(StringRef GraphName, char &ID)
  207. : ModulePass(ID), Name(GraphName) {}
  208. bool runOnModule(Module &M) override {
  209. GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
  210. std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
  211. ViewGraph(Graph, Name, IsSimple, Title);
  212. return false;
  213. }
  214. void getAnalysisUsage(AnalysisUsage &AU) const override {
  215. AU.setPreservesAll();
  216. AU.addRequired<AnalysisT>();
  217. }
  218. private:
  219. std::string Name;
  220. };
  221. template <typename AnalysisT, bool IsSimple, typename GraphT = AnalysisT *,
  222. typename AnalysisGraphTraitsT =
  223. LegacyDefaultAnalysisGraphTraits<AnalysisT, GraphT>>
  224. class DOTGraphTraitsModulePrinterWrapperPass : public ModulePass {
  225. public:
  226. DOTGraphTraitsModulePrinterWrapperPass(StringRef GraphName, char &ID)
  227. : ModulePass(ID), Name(GraphName) {}
  228. bool runOnModule(Module &M) override {
  229. GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
  230. std::string Filename = Name + ".dot";
  231. std::error_code EC;
  232. errs() << "Writing '" << Filename << "'...";
  233. raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
  234. std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);
  235. if (!EC)
  236. WriteGraph(File, Graph, IsSimple, Title);
  237. else
  238. errs() << " error opening file for writing!";
  239. errs() << "\n";
  240. return false;
  241. }
  242. void getAnalysisUsage(AnalysisUsage &AU) const override {
  243. AU.setPreservesAll();
  244. AU.addRequired<AnalysisT>();
  245. }
  246. private:
  247. std::string Name;
  248. };
  249. template <typename GraphT>
  250. void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
  251. std::string FileNamePrefix, bool IsSimple) {
  252. std::string Filename = FileNamePrefix + "." + F.getName().str() + ".dot";
  253. std::error_code EC;
  254. errs() << "Writing '" << Filename << "'...";
  255. raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
  256. std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
  257. std::string Title = GraphName + " for '" + F.getName().str() + "' function";
  258. if (!EC)
  259. WriteGraph(File, Graph, IsSimple, Title);
  260. else
  261. errs() << " error opening file for writing!";
  262. errs() << "\n";
  263. }
  264. } // end namespace llvm
  265. #endif
  266. #ifdef __GNUC__
  267. #pragma GCC diagnostic pop
  268. #endif