xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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/Module.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
20 using namespace llvm;
21 
22 PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
23   const ModuleSummaryIndex *Index =
24       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
25                        : nullptr;
26   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
27   return PreservedAnalyses::all();
28 }
29 
30 namespace {
31   class WriteBitcodePass : public ModulePass {
32     raw_ostream &OS; // raw_ostream to print on
33     bool ShouldPreserveUseListOrder;
34     bool EmitSummaryIndex;
35     bool EmitModuleHash;
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                               bool EmitSummaryIndex, bool EmitModuleHash)
45         : ModulePass(ID), OS(o),
46           ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
47           EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
48       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
49     }
50 
51     StringRef getPassName() const override { return "Bitcode Writer"; }
52 
53     bool runOnModule(Module &M) override {
54       const ModuleSummaryIndex *Index =
55           EmitSummaryIndex
56               ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
57               : nullptr;
58       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
59                          EmitModuleHash);
60       return false;
61     }
62     void getAnalysisUsage(AnalysisUsage &AU) const override {
63       AU.setPreservesAll();
64       if (EmitSummaryIndex)
65         AU.addRequired<ModuleSummaryIndexWrapperPass>();
66     }
67   };
68 }
69 
70 char WriteBitcodePass::ID = 0;
71 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
72                       true)
73 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
74 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
75                     true)
76 
77 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
78                                           bool ShouldPreserveUseListOrder,
79                                           bool EmitSummaryIndex, bool EmitModuleHash) {
80   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
81                               EmitSummaryIndex, EmitModuleHash);
82 }
83 
84 bool llvm::isBitcodeWriterPass(Pass *P) {
85   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
86 }
87