DumpModulePass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===------ DumpModulePass.cpp ----------------------------------*- C++ -*-===//
  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. // Write a module to a file.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/Support/DumpModulePass.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/Pass.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Support/Path.h"
  18. #include "llvm/Support/ToolOutputFile.h"
  19. #define DEBUG_TYPE "polly-dump-module"
  20. using namespace llvm;
  21. using namespace polly;
  22. namespace {
  23. static void runDumpModule(llvm::Module &M, StringRef Filename, bool IsSuffix) {
  24. std::string Dumpfile;
  25. if (IsSuffix) {
  26. StringRef ModuleName = M.getName();
  27. StringRef Stem = sys::path::stem(ModuleName);
  28. Dumpfile = (Twine(Stem) + Filename + ".ll").str();
  29. } else {
  30. Dumpfile = Filename.str();
  31. }
  32. LLVM_DEBUG(dbgs() << "Dumping module to " << Dumpfile << '\n');
  33. std::unique_ptr<ToolOutputFile> Out;
  34. std::error_code EC;
  35. Out.reset(new ToolOutputFile(Dumpfile, EC, sys::fs::OF_None));
  36. if (EC) {
  37. errs() << EC.message() << '\n';
  38. return;
  39. }
  40. M.print(Out->os(), nullptr);
  41. Out->keep();
  42. }
  43. class DumpModuleWrapperPass final : public ModulePass {
  44. private:
  45. DumpModuleWrapperPass(const DumpModuleWrapperPass &) = delete;
  46. const DumpModuleWrapperPass &
  47. operator=(const DumpModuleWrapperPass &) = delete;
  48. std::string Filename;
  49. bool IsSuffix;
  50. public:
  51. static char ID;
  52. /// This constructor is used e.g. if using opt -polly-dump-module.
  53. ///
  54. /// Provide a default suffix to not overwrite the original file.
  55. explicit DumpModuleWrapperPass()
  56. : ModulePass(ID), Filename("-dump"), IsSuffix(true) {}
  57. explicit DumpModuleWrapperPass(std::string Filename, bool IsSuffix)
  58. : ModulePass(ID), Filename(std::move(Filename)), IsSuffix(IsSuffix) {}
  59. /// @name ModulePass interface
  60. //@{
  61. void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
  62. AU.setPreservesAll();
  63. }
  64. bool runOnModule(llvm::Module &M) override {
  65. runDumpModule(M, Filename, IsSuffix);
  66. return false;
  67. }
  68. //@}
  69. };
  70. char DumpModuleWrapperPass::ID;
  71. } // namespace
  72. ModulePass *polly::createDumpModuleWrapperPass(std::string Filename,
  73. bool IsSuffix) {
  74. return new DumpModuleWrapperPass(std::move(Filename), IsSuffix);
  75. }
  76. llvm::PreservedAnalyses DumpModulePass::run(llvm::Module &M,
  77. llvm::ModuleAnalysisManager &AM) {
  78. runDumpModule(M, Filename, IsSuffix);
  79. return PreservedAnalyses::all();
  80. }
  81. INITIALIZE_PASS_BEGIN(DumpModuleWrapperPass, "polly-dump-module",
  82. "Polly - Dump Module", false, false)
  83. INITIALIZE_PASS_END(DumpModuleWrapperPass, "polly-dump-module",
  84. "Polly - Dump Module", false, false)