IRPrintingPasses.cpp 4.4 KB

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