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
run(Module & M,ModuleAnalysisManager & AM)210b57cec5SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
22c9157d92SDimitry Andric // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
23c9157d92SDimitry Andric // convert to dbg.values before writing out.
24c9157d92SDimitry Andric bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
25c9157d92SDimitry Andric if (IsNewDbgInfoFormat)
26c9157d92SDimitry Andric M.convertFromNewDbgValues();
27c9157d92SDimitry Andric
280b57cec5SDimitry Andric const ModuleSummaryIndex *Index =
290b57cec5SDimitry Andric EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
300b57cec5SDimitry Andric : nullptr;
310b57cec5SDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
32c9157d92SDimitry Andric
33c9157d92SDimitry Andric if (IsNewDbgInfoFormat)
34c9157d92SDimitry Andric M.convertToNewDbgValues();
35c9157d92SDimitry 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
WriteBitcodePass()460b57cec5SDimitry Andric WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
470b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric
WriteBitcodePass(raw_ostream & o,bool ShouldPreserveUseListOrder)50*a58f00eaSDimitry Andric explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder)
510b57cec5SDimitry Andric : ModulePass(ID), OS(o),
52*a58f00eaSDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
530b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
getPassName() const560b57cec5SDimitry Andric StringRef getPassName() const override { return "Bitcode Writer"; }
570b57cec5SDimitry Andric
runOnModule(Module & M)580b57cec5SDimitry Andric bool runOnModule(Module &M) override {
59c9157d92SDimitry Andric // RemoveDIs: there's no bitcode representation of the DPValue debug-info,
60c9157d92SDimitry Andric // convert to dbg.values before writing out.
61c9157d92SDimitry Andric bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
62c9157d92SDimitry Andric if (IsNewDbgInfoFormat)
63c9157d92SDimitry Andric M.convertFromNewDbgValues();
64c9157d92SDimitry Andric
65*a58f00eaSDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
66*a58f00eaSDimitry Andric /*EmitModuleHash=*/false);
67c9157d92SDimitry Andric
68c9157d92SDimitry Andric if (IsNewDbgInfoFormat)
69c9157d92SDimitry Andric M.convertToNewDbgValues();
700b57cec5SDimitry Andric return false;
710b57cec5SDimitry Andric }
getAnalysisUsage(AnalysisUsage & AU) const720b57cec5SDimitry 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)
INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)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*a58f00eaSDimitry Andric bool ShouldPreserveUseListOrder) {
87*a58f00eaSDimitry Andric return new WriteBitcodePass(Str, ShouldPreserveUseListOrder);
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
isBitcodeWriterPass(Pass * P)900b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
910b57cec5SDimitry Andric return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
920b57cec5SDimitry Andric }
93