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   const ModuleSummaryIndex *Index =
23       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
24                        : nullptr;
25   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
26   return PreservedAnalyses::all();
27 }
28 
29 namespace {
30   class WriteBitcodePass : public ModulePass {
31     raw_ostream &OS; // raw_ostream to print on
32     bool ShouldPreserveUseListOrder;
33     bool EmitSummaryIndex;
34     bool EmitModuleHash;
35 
36   public:
37     static char ID; // Pass identification, replacement for typeid
38     WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
39       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
40     }
41 
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());
48     }
49 
50     StringRef getPassName() const override { return "Bitcode Writer"; }
51 
52     bool runOnModule(Module &M) override {
53       const ModuleSummaryIndex *Index =
54           EmitSummaryIndex
55               ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
56               : nullptr;
57       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
58                          EmitModuleHash);
59       return false;
60     }
61     void getAnalysisUsage(AnalysisUsage &AU) const override {
62       AU.setPreservesAll();
63       if (EmitSummaryIndex)
64         AU.addRequired<ModuleSummaryIndexWrapperPass>();
65     }
66   };
67 }
68 
69 char WriteBitcodePass::ID = 0;
70 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
71                       true)
72 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
73 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
74                     true)
75 
76 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
77                                           bool ShouldPreserveUseListOrder,
78                                           bool EmitSummaryIndex, bool EmitModuleHash) {
79   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
80                               EmitSummaryIndex, EmitModuleHash);
81 }
82 
83 bool llvm::isBitcodeWriterPass(Pass *P) {
84   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
85 }
86