xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // BitcodeWriterPass implementation.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
140b57cec5SDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h"
150b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
160b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
17480093f4SDimitry Andric #include "llvm/InitializePasses.h"
180b57cec5SDimitry Andric #include "llvm/Pass.h"
190b57cec5SDimitry Andric using namespace llvm;
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
22*5f757f3fSDimitry Andric   // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
23*5f757f3fSDimitry Andric   // convert to dbg.values before writing out.
24*5f757f3fSDimitry Andric   bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
25*5f757f3fSDimitry Andric   if (IsNewDbgInfoFormat)
26*5f757f3fSDimitry Andric     M.convertFromNewDbgValues();
27*5f757f3fSDimitry Andric 
280b57cec5SDimitry Andric   const ModuleSummaryIndex *Index =
290b57cec5SDimitry Andric       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
300b57cec5SDimitry Andric                        : nullptr;
310b57cec5SDimitry Andric   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
32*5f757f3fSDimitry Andric 
33*5f757f3fSDimitry Andric   if (IsNewDbgInfoFormat)
34*5f757f3fSDimitry Andric     M.convertToNewDbgValues();
35*5f757f3fSDimitry Andric 
360b57cec5SDimitry Andric   return PreservedAnalyses::all();
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace {
400b57cec5SDimitry Andric   class WriteBitcodePass : public ModulePass {
410b57cec5SDimitry Andric     raw_ostream &OS; // raw_ostream to print on
420b57cec5SDimitry Andric     bool ShouldPreserveUseListOrder;
430b57cec5SDimitry Andric     bool EmitSummaryIndex;
440b57cec5SDimitry Andric     bool EmitModuleHash;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   public:
470b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
480b57cec5SDimitry Andric     WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
490b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric     explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
530b57cec5SDimitry Andric                               bool EmitSummaryIndex, bool EmitModuleHash)
540b57cec5SDimitry Andric         : ModulePass(ID), OS(o),
550b57cec5SDimitry Andric           ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
560b57cec5SDimitry Andric           EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
570b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
580b57cec5SDimitry Andric     }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric     StringRef getPassName() const override { return "Bitcode Writer"; }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric     bool runOnModule(Module &M) override {
630b57cec5SDimitry Andric       const ModuleSummaryIndex *Index =
640b57cec5SDimitry Andric           EmitSummaryIndex
650b57cec5SDimitry Andric               ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
660b57cec5SDimitry Andric               : nullptr;
67*5f757f3fSDimitry Andric       // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
68*5f757f3fSDimitry Andric       // convert to dbg.values before writing out.
69*5f757f3fSDimitry Andric       bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
70*5f757f3fSDimitry Andric       if (IsNewDbgInfoFormat)
71*5f757f3fSDimitry Andric         M.convertFromNewDbgValues();
72*5f757f3fSDimitry Andric 
730b57cec5SDimitry Andric       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
740b57cec5SDimitry Andric                          EmitModuleHash);
75*5f757f3fSDimitry Andric 
76*5f757f3fSDimitry Andric       if (IsNewDbgInfoFormat)
77*5f757f3fSDimitry Andric         M.convertToNewDbgValues();
780b57cec5SDimitry Andric       return false;
790b57cec5SDimitry Andric     }
800b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
810b57cec5SDimitry Andric       AU.setPreservesAll();
820b57cec5SDimitry Andric       if (EmitSummaryIndex)
830b57cec5SDimitry Andric         AU.addRequired<ModuleSummaryIndexWrapperPass>();
840b57cec5SDimitry Andric     }
850b57cec5SDimitry Andric   };
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric char WriteBitcodePass::ID = 0;
890b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
900b57cec5SDimitry Andric                       true)
910b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
920b57cec5SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
930b57cec5SDimitry Andric                     true)
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
960b57cec5SDimitry Andric                                           bool ShouldPreserveUseListOrder,
970b57cec5SDimitry Andric                                           bool EmitSummaryIndex, bool EmitModuleHash) {
980b57cec5SDimitry Andric   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
990b57cec5SDimitry Andric                               EmitSummaryIndex, EmitModuleHash);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
1030b57cec5SDimitry Andric   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
1040b57cec5SDimitry Andric }
105