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/Pass.h"
21 PreservedAnalyses
BitcodeWriterPass::run(Module
&M
, ModuleAnalysisManager
&AM
) {
22 const ModuleSummaryIndex
*Index
=
23 EmitSummaryIndex
? &(AM
.getResult
<ModuleSummaryIndexAnalysis
>(M
))
25 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, Index
, EmitModuleHash
);
26 return PreservedAnalyses::all();
30 class WriteBitcodePass
: public ModulePass
{
31 raw_ostream
&OS
; // raw_ostream to print on
32 bool ShouldPreserveUseListOrder
;
33 bool EmitSummaryIndex
;
37 static char ID
; // Pass identification, replacement for typeid
38 WriteBitcodePass() : ModulePass(ID
), OS(dbgs()) {
39 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
42 explicit WriteBitcodePass(raw_ostream
&o
, bool ShouldPreserveUseListOrder
,
43 bool EmitSummaryIndex
, bool EmitModuleHash
)
44 : ModulePass(ID
), OS(o
),
45 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
),
46 EmitSummaryIndex(EmitSummaryIndex
), EmitModuleHash(EmitModuleHash
) {
47 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
50 StringRef
getPassName() const override
{ return "Bitcode Writer"; }
52 bool runOnModule(Module
&M
) override
{
53 const ModuleSummaryIndex
*Index
=
55 ? &(getAnalysis
<ModuleSummaryIndexWrapperPass
>().getIndex())
57 WriteBitcodeToFile(M
, OS
, ShouldPreserveUseListOrder
, Index
,
61 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
64 AU
.addRequired
<ModuleSummaryIndexWrapperPass
>();
69 char WriteBitcodePass::ID
= 0;
70 INITIALIZE_PASS_BEGIN(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
72 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass
)
73 INITIALIZE_PASS_END(WriteBitcodePass
, "write-bitcode", "Write Bitcode", false,
76 ModulePass
*llvm::createBitcodeWriterPass(raw_ostream
&Str
,
77 bool ShouldPreserveUseListOrder
,
78 bool EmitSummaryIndex
, bool EmitModuleHash
) {
79 return new WriteBitcodePass(Str
, ShouldPreserveUseListOrder
,
80 EmitSummaryIndex
, EmitModuleHash
);
83 bool llvm::isBitcodeWriterPass(Pass
*P
) {
84 return P
->getPassID() == (llvm::AnalysisID
)&WriteBitcodePass::ID
;