1 | //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // BitcodeWriterPass implementation. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
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" |
19 | using namespace llvm; |
20 | |
21 | PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { |
22 | M.removeDebugIntrinsicDeclarations(); |
23 | |
24 | const ModuleSummaryIndex *Index = |
25 | EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(IR&: M)) |
26 | : nullptr; |
27 | WriteBitcodeToFile(M, Out&: OS, ShouldPreserveUseListOrder, Index, GenerateHash: EmitModuleHash); |
28 | |
29 | return PreservedAnalyses::all(); |
30 | } |
31 | |
32 | namespace { |
33 | class WriteBitcodePass : public ModulePass { |
34 | raw_ostream &OS; // raw_ostream to print on |
35 | bool ShouldPreserveUseListOrder; |
36 | |
37 | public: |
38 | static char ID; // Pass identification, replacement for typeid |
39 | WriteBitcodePass() : ModulePass(ID), OS(dbgs()) { |
40 | initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); |
41 | } |
42 | |
43 | explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder) |
44 | : ModulePass(ID), OS(o), |
45 | ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { |
46 | initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); |
47 | } |
48 | |
49 | StringRef getPassName() const override { return "Bitcode Writer" ; } |
50 | |
51 | bool runOnModule(Module &M) override { |
52 | M.removeDebugIntrinsicDeclarations(); |
53 | |
54 | WriteBitcodeToFile(M, Out&: OS, ShouldPreserveUseListOrder, /*Index=*/nullptr, |
55 | /*EmitModuleHash=*/GenerateHash: false); |
56 | |
57 | return false; |
58 | } |
59 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
60 | AU.setPreservesAll(); |
61 | } |
62 | }; |
63 | } |
64 | |
65 | char WriteBitcodePass::ID = 0; |
66 | INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode" , "Write Bitcode" , false, |
67 | true) |
68 | INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) |
69 | INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode" , "Write Bitcode" , false, |
70 | true) |
71 | |
72 | ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str, |
73 | bool ShouldPreserveUseListOrder) { |
74 | return new WriteBitcodePass(Str, ShouldPreserveUseListOrder); |
75 | } |
76 | |
77 | bool llvm::isBitcodeWriterPass(Pass *P) { |
78 | return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID; |
79 | } |
80 | |