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