PassPrinters.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //===- PassPrinters.cpp - Utilities to print analysis info for passes -----===//
  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. /// \file
  11. /// \brief Utilities to print analysis info for various kinds of passes.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/PassPrinters/PassPrinters.h"
  15. #include "llvm/Analysis/CallGraphSCCPass.h"
  16. #include "llvm/Analysis/LoopPass.h"
  17. #include "llvm/Analysis/RegionPass.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/Pass.h"
  20. #include <string>
  21. // //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. using namespace llvm;
  24. namespace {
  25. struct FunctionPassPrinter : public FunctionPass {
  26. const PassInfo *PassToPrint;
  27. raw_ostream &Out;
  28. static char ID;
  29. std::string PassName;
  30. bool QuietPass;
  31. FunctionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  32. : FunctionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  33. std::string PassToPrintName = PassToPrint->getPassName();
  34. PassName = "FunctionPass Printer: " + PassToPrintName;
  35. }
  36. bool runOnFunction(Function &F) override {
  37. if (!QuietPass)
  38. Out << "Printing analysis '" << PassToPrint->getPassName()
  39. << "' for function '" << F.getName() << "':\n";
  40. // Get and print pass...
  41. getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, F.getParent());
  42. return false;
  43. }
  44. const char *getPassName() const override { return PassName.c_str(); }
  45. void getAnalysisUsage(AnalysisUsage &AU) const override {
  46. AU.addRequiredID(PassToPrint->getTypeInfo());
  47. AU.setPreservesAll();
  48. }
  49. };
  50. char FunctionPassPrinter::ID = 0;
  51. struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
  52. static char ID;
  53. const PassInfo *PassToPrint;
  54. raw_ostream &Out;
  55. std::string PassName;
  56. bool QuietPass;
  57. CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  58. : CallGraphSCCPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  59. std::string PassToPrintName = PassToPrint->getPassName();
  60. PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
  61. }
  62. bool runOnSCC(CallGraphSCC &SCC) override {
  63. if (!QuietPass)
  64. Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
  65. // Get and print pass...
  66. for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
  67. Function *F = (*I)->getFunction();
  68. if (F)
  69. getAnalysisID<Pass>(PassToPrint->getTypeInfo())
  70. .print(Out, F->getParent());
  71. }
  72. return false;
  73. }
  74. const char *getPassName() const override { return PassName.c_str(); }
  75. void getAnalysisUsage(AnalysisUsage &AU) const override {
  76. AU.addRequiredID(PassToPrint->getTypeInfo());
  77. AU.setPreservesAll();
  78. }
  79. };
  80. char CallGraphSCCPassPrinter::ID = 0;
  81. struct ModulePassPrinter : public ModulePass {
  82. static char ID;
  83. const PassInfo *PassToPrint;
  84. raw_ostream &Out;
  85. std::string PassName;
  86. bool QuietPass;
  87. ModulePassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  88. : ModulePass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  89. std::string PassToPrintName = PassToPrint->getPassName();
  90. PassName = "ModulePass Printer: " + PassToPrintName;
  91. }
  92. bool runOnModule(Module &M) override {
  93. if (!QuietPass)
  94. Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
  95. // Get and print pass...
  96. getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
  97. return false;
  98. }
  99. const char *getPassName() const override { return PassName.c_str(); }
  100. void getAnalysisUsage(AnalysisUsage &AU) const override {
  101. AU.addRequiredID(PassToPrint->getTypeInfo());
  102. AU.setPreservesAll();
  103. }
  104. };
  105. char ModulePassPrinter::ID = 0;
  106. struct LoopPassPrinter : public LoopPass {
  107. static char ID;
  108. const PassInfo *PassToPrint;
  109. raw_ostream &Out;
  110. std::string PassName;
  111. bool QuietPass;
  112. LoopPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  113. : LoopPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  114. std::string PassToPrintName = PassToPrint->getPassName();
  115. PassName = "LoopPass Printer: " + PassToPrintName;
  116. }
  117. bool runOnLoop(Loop *L, LPPassManager &LPM) override {
  118. if (!QuietPass)
  119. Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
  120. // Get and print pass...
  121. getAnalysisID<Pass>(PassToPrint->getTypeInfo())
  122. .print(Out, L->getHeader()->getParent()->getParent());
  123. return false;
  124. }
  125. const char *getPassName() const override { return PassName.c_str(); }
  126. void getAnalysisUsage(AnalysisUsage &AU) const override {
  127. AU.addRequiredID(PassToPrint->getTypeInfo());
  128. AU.setPreservesAll();
  129. }
  130. };
  131. char LoopPassPrinter::ID = 0;
  132. struct RegionPassPrinter : public RegionPass {
  133. static char ID;
  134. const PassInfo *PassToPrint;
  135. raw_ostream &Out;
  136. std::string PassName;
  137. bool QuietPass;
  138. RegionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  139. : RegionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  140. std::string PassToPrintName = PassToPrint->getPassName();
  141. PassName = "RegionPass Printer: " + PassToPrintName;
  142. }
  143. bool runOnRegion(Region *R, RGPassManager &RGM) override {
  144. if (!QuietPass) {
  145. Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
  146. << "region: '" << R->getNameStr() << "' in function '"
  147. << R->getEntry()->getParent()->getName() << "':\n";
  148. }
  149. // Get and print pass...
  150. getAnalysisID<Pass>(PassToPrint->getTypeInfo())
  151. .print(Out, R->getEntry()->getParent()->getParent());
  152. return false;
  153. }
  154. const char *getPassName() const override { return PassName.c_str(); }
  155. void getAnalysisUsage(AnalysisUsage &AU) const override {
  156. AU.addRequiredID(PassToPrint->getTypeInfo());
  157. AU.setPreservesAll();
  158. }
  159. };
  160. char RegionPassPrinter::ID = 0;
  161. struct BasicBlockPassPrinter : public BasicBlockPass {
  162. const PassInfo *PassToPrint;
  163. raw_ostream &Out;
  164. static char ID;
  165. std::string PassName;
  166. bool QuietPass;
  167. BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
  168. : BasicBlockPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
  169. std::string PassToPrintName = PassToPrint->getPassName();
  170. PassName = "BasicBlockPass Printer: " + PassToPrintName;
  171. }
  172. bool runOnBasicBlock(BasicBlock &BB) override {
  173. if (!QuietPass)
  174. Out << "Printing Analysis info for BasicBlock '" << BB.getName()
  175. << "': Pass " << PassToPrint->getPassName() << ":\n";
  176. // Get and print pass...
  177. getAnalysisID<Pass>(PassToPrint->getTypeInfo())
  178. .print(Out, BB.getParent()->getParent());
  179. return false;
  180. }
  181. const char *getPassName() const override { return PassName.c_str(); }
  182. void getAnalysisUsage(AnalysisUsage &AU) const override {
  183. AU.addRequiredID(PassToPrint->getTypeInfo());
  184. AU.setPreservesAll();
  185. }
  186. };
  187. char BasicBlockPassPrinter::ID = 0;
  188. }
  189. FunctionPass *llvm::createFunctionPassPrinter(const PassInfo *PI,
  190. raw_ostream &OS, bool Quiet) {
  191. return new FunctionPassPrinter(PI, OS, Quiet);
  192. }
  193. CallGraphSCCPass *llvm::createCallGraphPassPrinter(const PassInfo *PI,
  194. raw_ostream &OS,
  195. bool Quiet) {
  196. return new CallGraphSCCPassPrinter(PI, OS, Quiet);
  197. }
  198. ModulePass *llvm::createModulePassPrinter(const PassInfo *PI, raw_ostream &OS,
  199. bool Quiet) {
  200. return new ModulePassPrinter(PI, OS, Quiet);
  201. }
  202. LoopPass *llvm::createLoopPassPrinter(const PassInfo *PI, raw_ostream &OS,
  203. bool Quiet) {
  204. return new LoopPassPrinter(PI, OS, Quiet);
  205. }
  206. RegionPass *llvm::createRegionPassPrinter(const PassInfo *PI, raw_ostream &OS,
  207. bool Quiet) {
  208. return new RegionPassPrinter(PI, OS, Quiet);
  209. }
  210. BasicBlockPass *llvm::createBasicBlockPassPrinter(const PassInfo *PI,
  211. raw_ostream &OS, bool Quiet) {
  212. return new BasicBlockPassPrinter(PI, OS, Quiet);
  213. }