1 //===------ DumpModulePass.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Write a module to a file.
12 //===----------------------------------------------------------------------===//
14 #include "polly/Support/DumpModulePass.h"
16 #include "polly/Options.h"
17 #include "llvm/IR/LegacyPassManagers.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/ToolOutputFile.h"
25 #define DEBUG_TYPE "polly-dump-module"
28 using namespace polly
;
32 class DumpModule
: public ModulePass
{
34 DumpModule(const DumpModule
&) = delete;
35 const DumpModule
&operator=(const DumpModule
&) = delete;
43 /// This constructor is used e.g. if using opt -polly-dump-module.
45 /// Provide a default suffix to not overwrite the original file.
46 explicit DumpModule() : ModulePass(ID
), Filename("-dump"), IsSuffix(true) {}
48 explicit DumpModule(llvm::StringRef Filename
, bool IsSuffix
)
49 : ModulePass(ID
), Filename(Filename
), IsSuffix(IsSuffix
) {}
51 /// @name ModulePass interface
53 virtual void getAnalysisUsage(llvm::AnalysisUsage
&AU
) const override
{
57 virtual bool runOnModule(llvm::Module
&M
) override
{
60 auto ModuleName
= M
.getName();
61 auto Stem
= sys::path::stem(ModuleName
);
62 Dumpfile
= (Twine(Stem
) + Filename
+ ".ll").str();
66 DEBUG(dbgs() << "Dumping module to " << Dumpfile
<< '\n');
68 std::unique_ptr
<tool_output_file
> Out
;
70 Out
.reset(new tool_output_file(Dumpfile
, EC
, sys::fs::F_None
));
72 errs() << EC
.message() << '\n';
76 M
.print(Out
->os(), nullptr);
87 ModulePass
*polly::createDumpModulePass(llvm::StringRef Filename
,
89 return new DumpModule(Filename
, IsSuffix
);
92 INITIALIZE_PASS_BEGIN(DumpModule
, "polly-dump-module", "Polly - Dump Module",
94 INITIALIZE_PASS_END(DumpModule
, "polly-dump-module", "Polly - Dump Module",