1 //===------ DumpModulePass.cpp ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Write a module to a file.
11 //===----------------------------------------------------------------------===//
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"
21 #define DEBUG_TYPE "polly-dump-module"
24 using namespace polly
;
28 static void runDumpModule(llvm::Module
&M
, StringRef Filename
, bool IsSuffix
) {
31 StringRef ModuleName
= M
.getName();
32 StringRef Stem
= sys::path::stem(ModuleName
);
33 Dumpfile
= (Twine(Stem
) + Filename
+ ".ll").str();
35 Dumpfile
= Filename
.str();
37 LLVM_DEBUG(dbgs() << "Dumping module to " << Dumpfile
<< '\n');
39 std::unique_ptr
<ToolOutputFile
> Out
;
41 Out
.reset(new ToolOutputFile(Dumpfile
, EC
, sys::fs::OF_None
));
43 errs() << EC
.message() << '\n';
47 M
.print(Out
->os(), nullptr);
51 class DumpModuleWrapperPass final
: public ModulePass
{
53 DumpModuleWrapperPass(const DumpModuleWrapperPass
&) = delete;
54 const DumpModuleWrapperPass
&
55 operator=(const DumpModuleWrapperPass
&) = delete;
63 /// This constructor is used e.g. if using opt -polly-dump-module.
65 /// Provide a default suffix to not overwrite the original file.
66 explicit DumpModuleWrapperPass()
67 : ModulePass(ID
), Filename("-dump"), IsSuffix(true) {}
69 explicit DumpModuleWrapperPass(std::string Filename
, bool IsSuffix
)
70 : ModulePass(ID
), Filename(std::move(Filename
)), IsSuffix(IsSuffix
) {}
72 /// @name ModulePass interface
74 void getAnalysisUsage(llvm::AnalysisUsage
&AU
) const override
{
78 bool runOnModule(llvm::Module
&M
) override
{
79 runDumpModule(M
, Filename
, IsSuffix
);
85 char DumpModuleWrapperPass::ID
;
88 ModulePass
*polly::createDumpModuleWrapperPass(std::string Filename
,
90 return new DumpModuleWrapperPass(std::move(Filename
), IsSuffix
);
93 llvm::PreservedAnalyses
DumpModulePass::run(llvm::Module
&M
,
94 llvm::ModuleAnalysisManager
&AM
) {
95 runDumpModule(M
, Filename
, IsSuffix
);
96 return PreservedAnalyses::all();
99 INITIALIZE_PASS_BEGIN(DumpModuleWrapperPass
, "polly-dump-module",
100 "Polly - Dump Module", false, false)
101 INITIALIZE_PASS_END(DumpModuleWrapperPass
, "polly-dump-module",
102 "Polly - Dump Module", false, false)