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
21*0fca6ea1SDimitry Andric extern bool WriteNewDbgInfoFormatToBitcode;
22*0fca6ea1SDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)230b57cec5SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
24*0fca6ea1SDimitry Andric ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
25*0fca6ea1SDimitry Andric WriteNewDbgInfoFormatToBitcode);
26*0fca6ea1SDimitry Andric if (M.IsNewDbgInfoFormat)
27*0fca6ea1SDimitry Andric M.removeDebugIntrinsicDeclarations();
285f757f3fSDimitry Andric
290b57cec5SDimitry Andric const ModuleSummaryIndex *Index =
300b57cec5SDimitry Andric EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
310b57cec5SDimitry Andric : nullptr;
320b57cec5SDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
335f757f3fSDimitry Andric
340b57cec5SDimitry Andric return PreservedAnalyses::all();
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric namespace {
380b57cec5SDimitry Andric class WriteBitcodePass : public ModulePass {
390b57cec5SDimitry Andric raw_ostream &OS; // raw_ostream to print on
400b57cec5SDimitry Andric bool ShouldPreserveUseListOrder;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric public:
430b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
WriteBitcodePass()440b57cec5SDimitry Andric WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
450b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
WriteBitcodePass(raw_ostream & o,bool ShouldPreserveUseListOrder)487a6dacacSDimitry Andric explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder)
490b57cec5SDimitry Andric : ModulePass(ID), OS(o),
507a6dacacSDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
510b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
getPassName() const540b57cec5SDimitry Andric StringRef getPassName() const override { return "Bitcode Writer"; }
550b57cec5SDimitry Andric
runOnModule(Module & M)560b57cec5SDimitry Andric bool runOnModule(Module &M) override {
57*0fca6ea1SDimitry Andric ScopedDbgInfoFormatSetter FormatSetter(
58*0fca6ea1SDimitry Andric M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);
59*0fca6ea1SDimitry Andric if (M.IsNewDbgInfoFormat)
60*0fca6ea1SDimitry Andric M.removeDebugIntrinsicDeclarations();
615f757f3fSDimitry Andric
627a6dacacSDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
637a6dacacSDimitry Andric /*EmitModuleHash=*/false);
645f757f3fSDimitry Andric
650b57cec5SDimitry Andric return false;
660b57cec5SDimitry Andric }
getAnalysisUsage(AnalysisUsage & AU) const670b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
680b57cec5SDimitry Andric AU.setPreservesAll();
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric };
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric char WriteBitcodePass::ID = 0;
740b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
750b57cec5SDimitry Andric true)
INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)760b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
770b57cec5SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
780b57cec5SDimitry Andric true)
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
817a6dacacSDimitry Andric bool ShouldPreserveUseListOrder) {
827a6dacacSDimitry Andric return new WriteBitcodePass(Str, ShouldPreserveUseListOrder);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
isBitcodeWriterPass(Pass * P)850b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) {
860b57cec5SDimitry Andric return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
870b57cec5SDimitry Andric }
88