1 //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // BitcodeWriterPass implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Bitcode/BitcodeWriterPass.h" 14 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 15 #include "llvm/Bitcode/BitcodeWriter.h" 16 #include "llvm/IR/PassManager.h" 17 #include "llvm/InitializePasses.h" 18 #include "llvm/Pass.h" 19 using namespace llvm; 20 21 PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 22 // RemoveDIs: there's no bitcode representation of the DPValue debug-info, 23 // convert to dbg.values before writing out. 24 bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat; 25 if (IsNewDbgInfoFormat) 26 M.convertFromNewDbgValues(); 27 28 const ModuleSummaryIndex *Index = 29 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M)) 30 : nullptr; 31 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash); 32 33 if (IsNewDbgInfoFormat) 34 M.convertToNewDbgValues(); 35 36 return PreservedAnalyses::all(); 37 } 38 39 namespace { 40 class WriteBitcodePass : public ModulePass { 41 raw_ostream &OS; // raw_ostream to print on 42 bool ShouldPreserveUseListOrder; 43 bool EmitSummaryIndex; 44 bool EmitModuleHash; 45 46 public: 47 static char ID; // Pass identification, replacement for typeid 48 WriteBitcodePass() : ModulePass(ID), OS(dbgs()) { 49 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 50 } 51 52 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder, 53 bool EmitSummaryIndex, bool EmitModuleHash) 54 : ModulePass(ID), OS(o), 55 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder), 56 EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) { 57 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 58 } 59 60 StringRef getPassName() const override { return "Bitcode Writer"; } 61 62 bool runOnModule(Module &M) override { 63 const ModuleSummaryIndex *Index = 64 EmitSummaryIndex 65 ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()) 66 : nullptr; 67 // RemoveDIs: there's no bitcode representation of the DPValue debug-info, 68 // convert to dbg.values before writing out. 69 bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat; 70 if (IsNewDbgInfoFormat) 71 M.convertFromNewDbgValues(); 72 73 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, 74 EmitModuleHash); 75 76 if (IsNewDbgInfoFormat) 77 M.convertToNewDbgValues(); 78 return false; 79 } 80 void getAnalysisUsage(AnalysisUsage &AU) const override { 81 AU.setPreservesAll(); 82 if (EmitSummaryIndex) 83 AU.addRequired<ModuleSummaryIndexWrapperPass>(); 84 } 85 }; 86 } 87 88 char WriteBitcodePass::ID = 0; 89 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 90 true) 91 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 92 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 93 true) 94 95 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str, 96 bool ShouldPreserveUseListOrder, 97 bool EmitSummaryIndex, bool EmitModuleHash) { 98 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder, 99 EmitSummaryIndex, EmitModuleHash); 100 } 101 102 bool llvm::isBitcodeWriterPass(Pass *P) { 103 return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID; 104 } 105