IRPrintingPasses.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/IRPrinter/IRPrintingPasses.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Analysis/ModuleSummaryAnalysis.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/PrintPasses.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. bool EmitSummaryIndex)
  26. : OS(OS), Banner(Banner),
  27. ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
  28. EmitSummaryIndex(EmitSummaryIndex) {}
  29. PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &AM) {
  30. if (llvm::isFunctionInPrintList("*")) {
  31. if (!Banner.empty())
  32. OS << Banner << "\n";
  33. M.print(OS, nullptr, ShouldPreserveUseListOrder);
  34. } else {
  35. bool BannerPrinted = false;
  36. for (const auto &F : M.functions()) {
  37. if (llvm::isFunctionInPrintList(F.getName())) {
  38. if (!BannerPrinted && !Banner.empty()) {
  39. OS << Banner << "\n";
  40. BannerPrinted = true;
  41. }
  42. F.print(OS);
  43. }
  44. }
  45. }
  46. ModuleSummaryIndex *Index =
  47. EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
  48. : nullptr;
  49. if (Index) {
  50. if (Index->modulePaths().empty())
  51. Index->addModule("", 0);
  52. Index->print(OS);
  53. }
  54. return PreservedAnalyses::all();
  55. }
  56. PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
  57. PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
  58. : OS(OS), Banner(Banner) {}
  59. PreservedAnalyses PrintFunctionPass::run(Function &F,
  60. FunctionAnalysisManager &) {
  61. if (isFunctionInPrintList(F.getName())) {
  62. if (forcePrintModuleIR())
  63. OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
  64. else
  65. OS << Banner << '\n' << static_cast<Value &>(F);
  66. }
  67. return PreservedAnalyses::all();
  68. }