1 //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
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 // BitcodeWriterPass implementation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Bitcode/BitcodeWriterPass.h"
14 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
15 #include "llvm/Bitcode/BitcodeWriter.h"
16 #include "llvm/IR/PassManager.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
21 extern bool WriteNewDbgInfoFormatToBitcode
;
23 PreservedAnalyses
BitcodeWriterPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
24 ScopedDbgInfoFormatSetter
FormatSetter(M
, M
.IsNewDbgInfoFormat
&&
25 WriteNewDbgInfoFormatToBitcode
);
26 if (M
.IsNewDbgInfoFormat
)
27 M
.removeDebugIntrinsicDeclarations();
29 const ModuleSummaryIndex
*Index
=
30 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
32 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, Index
, EmitModuleHash
);
34 return PreservedAnalyses::all();
38 class WriteBitcodePass
: public ModulePass
{
39 raw_ostream
&OS
; // raw_ostream to print on
40 bool ShouldPreserveUseListOrder
;
43 static char ID
; // Pass identification, replacement for typeid
44 WriteBitcodePass() : ModulePass(ID
), OS(dbgs()) {
45 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
48 explicit WriteBitcodePass(raw_ostream
&o
, bool ShouldPreserveUseListOrder
)
49 : ModulePass(ID
), OS(o
),
50 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
) {
51 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
54 StringRef
getPassName() const override
{ return "Bitcode Writer"; }
56 bool runOnModule(Module
&M
) override
{
57 ScopedDbgInfoFormatSetter
FormatSetter(
58 M
, M
.IsNewDbgInfoFormat
&& WriteNewDbgInfoFormatToBitcode
);
59 if (M
.IsNewDbgInfoFormat
)
60 M
.removeDebugIntrinsicDeclarations();
62 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, /*Index=*/nullptr,
63 /*EmitModuleHash=*/false);
67 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
73 char WriteBitcodePass::ID
= 0;
74 INITIALIZE_PASS_BEGIN(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
76 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass
)
77 INITIALIZE_PASS_END(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
80 ModulePass
*llvm::createBitcodeWriterPass(raw_ostream
&Str
,
81 bool ShouldPreserveUseListOrder
) {
82 return new WriteBitcodePass(Str
, ShouldPreserveUseListOrder
);
85 bool llvm::isBitcodeWriterPass(Pass
*P
) {
86 return P
->getPassID() == (llvm::AnalysisID
)&WriteBitcodePass::ID
;