xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // BitcodeWriterPass implementation.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
14*0b57cec5SDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
15*0b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
18*0b57cec5SDimitry Andric #include "llvm/Pass.h"
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
22*0b57cec5SDimitry Andric   const ModuleSummaryIndex *Index =
23*0b57cec5SDimitry Andric       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
24*0b57cec5SDimitry Andric                        : nullptr;
25*0b57cec5SDimitry Andric   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
26*0b57cec5SDimitry Andric   return PreservedAnalyses::all();
27*0b57cec5SDimitry Andric }
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric namespace {
30*0b57cec5SDimitry Andric   class WriteBitcodePass : public ModulePass {
31*0b57cec5SDimitry Andric     raw_ostream &OS; // raw_ostream to print on
32*0b57cec5SDimitry Andric     bool ShouldPreserveUseListOrder;
33*0b57cec5SDimitry Andric     bool EmitSummaryIndex;
34*0b57cec5SDimitry Andric     bool EmitModuleHash;
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric   public:
37*0b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
38*0b57cec5SDimitry Andric     WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
39*0b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
40*0b57cec5SDimitry Andric     }
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric     explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
43*0b57cec5SDimitry Andric                               bool EmitSummaryIndex, bool EmitModuleHash)
44*0b57cec5SDimitry Andric         : ModulePass(ID), OS(o),
45*0b57cec5SDimitry Andric           ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
46*0b57cec5SDimitry Andric           EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
47*0b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
48*0b57cec5SDimitry Andric     }
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric     StringRef getPassName() const override { return "Bitcode Writer"; }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric     bool runOnModule(Module &M) override {
53*0b57cec5SDimitry Andric       const ModuleSummaryIndex *Index =
54*0b57cec5SDimitry Andric           EmitSummaryIndex
55*0b57cec5SDimitry Andric               ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
56*0b57cec5SDimitry Andric               : nullptr;
57*0b57cec5SDimitry Andric       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
58*0b57cec5SDimitry Andric                          EmitModuleHash);
59*0b57cec5SDimitry Andric       return false;
60*0b57cec5SDimitry Andric     }
61*0b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
62*0b57cec5SDimitry Andric       AU.setPreservesAll();
63*0b57cec5SDimitry Andric       if (EmitSummaryIndex)
64*0b57cec5SDimitry Andric         AU.addRequired<ModuleSummaryIndexWrapperPass>();
65*0b57cec5SDimitry Andric     }
66*0b57cec5SDimitry Andric   };
67*0b57cec5SDimitry Andric }
68*0b57cec5SDimitry Andric 
69*0b57cec5SDimitry Andric char WriteBitcodePass::ID = 0;
70*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
71*0b57cec5SDimitry Andric                       true)
72*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
73*0b57cec5SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
74*0b57cec5SDimitry Andric                     true)
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
77*0b57cec5SDimitry Andric                                           bool ShouldPreserveUseListOrder,
78*0b57cec5SDimitry Andric                                           bool EmitSummaryIndex, bool EmitModuleHash) {
79*0b57cec5SDimitry Andric   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
80*0b57cec5SDimitry Andric                               EmitSummaryIndex, EmitModuleHash);
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
84*0b57cec5SDimitry Andric   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
85*0b57cec5SDimitry Andric }
86