IRPrintingPasses.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
  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. // PrintModulePass and PrintFunctionPass implementations for the legacy pass
  10. // manager.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/IRPrintingPasses.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/PrintPasses.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. namespace {
  24. class PrintModulePassWrapper : public ModulePass {
  25. raw_ostream &OS;
  26. std::string Banner;
  27. bool ShouldPreserveUseListOrder;
  28. public:
  29. static char ID;
  30. PrintModulePassWrapper() : ModulePass(ID), OS(dbgs()) {}
  31. PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
  32. bool ShouldPreserveUseListOrder)
  33. : ModulePass(ID), OS(OS), Banner(Banner),
  34. ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
  35. bool runOnModule(Module &M) override {
  36. if (llvm::isFunctionInPrintList("*")) {
  37. if (!Banner.empty())
  38. OS << Banner << "\n";
  39. M.print(OS, nullptr, ShouldPreserveUseListOrder);
  40. } else {
  41. bool BannerPrinted = false;
  42. for (const auto &F : M.functions()) {
  43. if (llvm::isFunctionInPrintList(F.getName())) {
  44. if (!BannerPrinted && !Banner.empty()) {
  45. OS << Banner << "\n";
  46. BannerPrinted = true;
  47. }
  48. F.print(OS);
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.setPreservesAll();
  56. }
  57. StringRef getPassName() const override { return "Print Module IR"; }
  58. };
  59. class PrintFunctionPassWrapper : public FunctionPass {
  60. raw_ostream &OS;
  61. std::string Banner;
  62. public:
  63. static char ID;
  64. PrintFunctionPassWrapper() : FunctionPass(ID), OS(dbgs()) {}
  65. PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
  66. : FunctionPass(ID), OS(OS), Banner(Banner) {}
  67. // This pass just prints a banner followed by the function as it's processed.
  68. bool runOnFunction(Function &F) override {
  69. if (isFunctionInPrintList(F.getName())) {
  70. if (forcePrintModuleIR())
  71. OS << Banner << " (function: " << F.getName() << ")\n"
  72. << *F.getParent();
  73. else
  74. OS << Banner << '\n' << static_cast<Value &>(F);
  75. }
  76. return false;
  77. }
  78. void getAnalysisUsage(AnalysisUsage &AU) const override {
  79. AU.setPreservesAll();
  80. }
  81. StringRef getPassName() const override { return "Print Function IR"; }
  82. };
  83. } // namespace
  84. char PrintModulePassWrapper::ID = 0;
  85. INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
  86. "Print module to stderr", false, true)
  87. char PrintFunctionPassWrapper::ID = 0;
  88. INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
  89. "Print function to stderr", false, true)
  90. ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
  91. const std::string &Banner,
  92. bool ShouldPreserveUseListOrder) {
  93. return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
  94. }
  95. FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
  96. const std::string &Banner) {
  97. return new PrintFunctionPassWrapper(OS, Banner);
  98. }
  99. bool llvm::isIRPrintingPass(Pass *P) {
  100. const char *PID = (const char *)P->getPassID();
  101. return (PID == &PrintModulePassWrapper::ID) ||
  102. (PID == &PrintFunctionPassWrapper::ID);
  103. }