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/Module.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
22 PreservedAnalyses
BitcodeWriterPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
23 const ModuleSummaryIndex
*Index
=
24 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
26 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, Index
, EmitModuleHash
);
27 return PreservedAnalyses::all();
31 class WriteBitcodePass
: public ModulePass
{
32 raw_ostream
&OS
; // raw_ostream to print on
33 bool ShouldPreserveUseListOrder
;
34 bool EmitSummaryIndex
;
38 static char ID
; // Pass identification, replacement for typeid
39 WriteBitcodePass() : ModulePass(ID
), OS(dbgs()) {
40 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
43 explicit WriteBitcodePass(raw_ostream
&o
, bool ShouldPreserveUseListOrder
,
44 bool EmitSummaryIndex
, bool EmitModuleHash
)
45 : ModulePass(ID
), OS(o
),
46 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
),
47 EmitSummaryIndex(EmitSummaryIndex
), EmitModuleHash(EmitModuleHash
) {
48 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
51 StringRef
getPassName() const override
{ return "Bitcode Writer"; }
53 bool runOnModule(Module
&M
) override
{
54 const ModuleSummaryIndex
*Index
=
56 ? &(getAnalysis
<ModuleSummaryIndexWrapperPass
>().getIndex())
58 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, Index
,
62 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
65 AU
.addRequired
<ModuleSummaryIndexWrapperPass
>();
70 char WriteBitcodePass::ID
= 0;
71 INITIALIZE_PASS_BEGIN(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
73 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass
)
74 INITIALIZE_PASS_END(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
77 ModulePass
*llvm::createBitcodeWriterPass(raw_ostream
&Str
,
78 bool ShouldPreserveUseListOrder
,
79 bool EmitSummaryIndex
, bool EmitModuleHash
) {
80 return new WriteBitcodePass(Str
, ShouldPreserveUseListOrder
,
81 EmitSummaryIndex
, EmitModuleHash
);
84 bool llvm::isBitcodeWriterPass(Pass
*P
) {
85 return P
->getPassID() == (llvm::AnalysisID
)&WriteBitcodePass::ID
;