xref: /src/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ca98fd9SDimitry Andric //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // BitcodeWriterPass implementation.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
135ca98fd9SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
1401095a5dSDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
15b915e9e0SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
165ca98fd9SDimitry Andric #include "llvm/IR/PassManager.h"
17706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
18009b1c42SEd Schouten #include "llvm/Pass.h"
19009b1c42SEd Schouten using namespace llvm;
20009b1c42SEd Schouten 
21ac9a064cSDimitry Andric extern bool WriteNewDbgInfoFormatToBitcode;
22ac9a064cSDimitry Andric 
run(Module & M,ModuleAnalysisManager & AM)23b915e9e0SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
24ac9a064cSDimitry Andric   ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
25ac9a064cSDimitry Andric                                                 WriteNewDbgInfoFormatToBitcode);
26ac9a064cSDimitry Andric   if (M.IsNewDbgInfoFormat)
27ac9a064cSDimitry Andric     M.removeDebugIntrinsicDeclarations();
28b1c73532SDimitry Andric 
29b915e9e0SDimitry Andric   const ModuleSummaryIndex *Index =
30b915e9e0SDimitry Andric       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
31b915e9e0SDimitry Andric                        : nullptr;
32eb11fae6SDimitry Andric   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
33b1c73532SDimitry Andric 
345ca98fd9SDimitry Andric   return PreservedAnalyses::all();
355ca98fd9SDimitry Andric }
365ca98fd9SDimitry Andric 
37009b1c42SEd Schouten namespace {
38009b1c42SEd Schouten   class WriteBitcodePass : public ModulePass {
3959850d08SRoman Divacky     raw_ostream &OS; // raw_ostream to print on
405a5ac124SDimitry Andric     bool ShouldPreserveUseListOrder;
415a5ac124SDimitry Andric 
42009b1c42SEd Schouten   public:
43009b1c42SEd Schouten     static char ID; // Pass identification, replacement for typeid
WriteBitcodePass()4401095a5dSDimitry Andric     WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
4501095a5dSDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
4601095a5dSDimitry Andric     }
4701095a5dSDimitry Andric 
WriteBitcodePass(raw_ostream & o,bool ShouldPreserveUseListOrder)484df029ccSDimitry Andric     explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder)
495a5ac124SDimitry Andric         : ModulePass(ID), OS(o),
504df029ccSDimitry Andric           ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
5101095a5dSDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
5201095a5dSDimitry Andric     }
53009b1c42SEd Schouten 
getPassName() const54b915e9e0SDimitry Andric     StringRef getPassName() const override { return "Bitcode Writer"; }
55009b1c42SEd Schouten 
runOnModule(Module & M)565ca98fd9SDimitry Andric     bool runOnModule(Module &M) override {
57ac9a064cSDimitry Andric       ScopedDbgInfoFormatSetter FormatSetter(
58ac9a064cSDimitry Andric           M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);
59ac9a064cSDimitry Andric       if (M.IsNewDbgInfoFormat)
60ac9a064cSDimitry Andric         M.removeDebugIntrinsicDeclarations();
61b1c73532SDimitry Andric 
624df029ccSDimitry Andric       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
634df029ccSDimitry Andric                          /*EmitModuleHash=*/false);
64b1c73532SDimitry Andric 
65009b1c42SEd Schouten       return false;
66009b1c42SEd Schouten     }
getAnalysisUsage(AnalysisUsage & AU) const6701095a5dSDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
6801095a5dSDimitry Andric       AU.setPreservesAll();
6901095a5dSDimitry Andric     }
70009b1c42SEd Schouten   };
711a82d4c0SDimitry Andric }
72009b1c42SEd Schouten 
73009b1c42SEd Schouten char WriteBitcodePass::ID = 0;
7401095a5dSDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
7501095a5dSDimitry Andric                       true)
INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)7601095a5dSDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
7701095a5dSDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
7801095a5dSDimitry Andric                     true)
79009b1c42SEd Schouten 
805a5ac124SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
814df029ccSDimitry Andric                                           bool ShouldPreserveUseListOrder) {
824df029ccSDimitry Andric   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder);
83009b1c42SEd Schouten }
84eb11fae6SDimitry Andric 
isBitcodeWriterPass(Pass * P)85eb11fae6SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
86eb11fae6SDimitry Andric   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
87eb11fae6SDimitry Andric }
88