xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
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) {
225f757f3fSDimitry Andric   // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
235f757f3fSDimitry Andric   // convert to dbg.values before writing out.
245f757f3fSDimitry Andric   bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
255f757f3fSDimitry Andric   if (IsNewDbgInfoFormat)
265f757f3fSDimitry Andric     M.convertFromNewDbgValues();
275f757f3fSDimitry Andric 
280b57cec5SDimitry Andric   const ModuleSummaryIndex *Index =
290b57cec5SDimitry Andric       EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
300b57cec5SDimitry Andric                        : nullptr;
310b57cec5SDimitry Andric   WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
325f757f3fSDimitry Andric 
335f757f3fSDimitry Andric   if (IsNewDbgInfoFormat)
345f757f3fSDimitry Andric     M.convertToNewDbgValues();
355f757f3fSDimitry 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 
440b57cec5SDimitry Andric   public:
450b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
460b57cec5SDimitry Andric     WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
470b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
480b57cec5SDimitry Andric     }
490b57cec5SDimitry Andric 
50*7a6dacacSDimitry Andric     explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder)
510b57cec5SDimitry Andric         : ModulePass(ID), OS(o),
52*7a6dacacSDimitry Andric           ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
530b57cec5SDimitry Andric       initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
540b57cec5SDimitry Andric     }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric     StringRef getPassName() const override { return "Bitcode Writer"; }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric     bool runOnModule(Module &M) override {
595f757f3fSDimitry Andric       // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
605f757f3fSDimitry Andric       // convert to dbg.values before writing out.
615f757f3fSDimitry Andric       bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
625f757f3fSDimitry Andric       if (IsNewDbgInfoFormat)
635f757f3fSDimitry Andric         M.convertFromNewDbgValues();
645f757f3fSDimitry Andric 
65*7a6dacacSDimitry Andric       WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
66*7a6dacacSDimitry Andric                          /*EmitModuleHash=*/false);
675f757f3fSDimitry Andric 
685f757f3fSDimitry Andric       if (IsNewDbgInfoFormat)
695f757f3fSDimitry Andric         M.convertToNewDbgValues();
700b57cec5SDimitry Andric       return false;
710b57cec5SDimitry Andric     }
720b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
730b57cec5SDimitry Andric       AU.setPreservesAll();
740b57cec5SDimitry Andric     }
750b57cec5SDimitry Andric   };
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric char WriteBitcodePass::ID = 0;
790b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
800b57cec5SDimitry Andric                       true)
810b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
820b57cec5SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
830b57cec5SDimitry Andric                     true)
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
86*7a6dacacSDimitry Andric                                           bool ShouldPreserveUseListOrder) {
87*7a6dacacSDimitry Andric   return new WriteBitcodePass(Str, ShouldPreserveUseListOrder);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
910b57cec5SDimitry Andric   return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
920b57cec5SDimitry Andric }
93