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/Module.h" 170b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 18*480093f4SDimitry Andric #include "llvm/InitializePasses.h" 190b57cec5SDimitry Andric #include "llvm/Pass.h" 200b57cec5SDimitry Andric using namespace llvm; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 230b57cec5SDimitry Andric const ModuleSummaryIndex *Index = 240b57cec5SDimitry Andric EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M)) 250b57cec5SDimitry Andric : nullptr; 260b57cec5SDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash); 270b57cec5SDimitry Andric return PreservedAnalyses::all(); 280b57cec5SDimitry Andric } 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric namespace { 310b57cec5SDimitry Andric class WriteBitcodePass : public ModulePass { 320b57cec5SDimitry Andric raw_ostream &OS; // raw_ostream to print on 330b57cec5SDimitry Andric bool ShouldPreserveUseListOrder; 340b57cec5SDimitry Andric bool EmitSummaryIndex; 350b57cec5SDimitry Andric bool EmitModuleHash; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric public: 380b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 390b57cec5SDimitry Andric WriteBitcodePass() : ModulePass(ID), OS(dbgs()) { 400b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 410b57cec5SDimitry Andric } 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder, 440b57cec5SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash) 450b57cec5SDimitry Andric : ModulePass(ID), OS(o), 460b57cec5SDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder), 470b57cec5SDimitry Andric EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) { 480b57cec5SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric StringRef getPassName() const override { return "Bitcode Writer"; } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric bool runOnModule(Module &M) override { 540b57cec5SDimitry Andric const ModuleSummaryIndex *Index = 550b57cec5SDimitry Andric EmitSummaryIndex 560b57cec5SDimitry Andric ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()) 570b57cec5SDimitry Andric : nullptr; 580b57cec5SDimitry Andric WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, 590b57cec5SDimitry Andric EmitModuleHash); 600b57cec5SDimitry Andric return false; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 630b57cec5SDimitry Andric AU.setPreservesAll(); 640b57cec5SDimitry Andric if (EmitSummaryIndex) 650b57cec5SDimitry Andric AU.addRequired<ModuleSummaryIndexWrapperPass>(); 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric }; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric char WriteBitcodePass::ID = 0; 710b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 720b57cec5SDimitry Andric true) 730b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 740b57cec5SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 750b57cec5SDimitry Andric true) 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str, 780b57cec5SDimitry Andric bool ShouldPreserveUseListOrder, 790b57cec5SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash) { 800b57cec5SDimitry Andric return new WriteBitcodePass(Str, ShouldPreserveUseListOrder, 810b57cec5SDimitry Andric EmitSummaryIndex, EmitModuleHash); 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric bool llvm::isBitcodeWriterPass(Pass *P) { 850b57cec5SDimitry Andric return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID; 860b57cec5SDimitry Andric } 87