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 extern bool WriteNewDbgInfoFormatToBitcode; 22 23 PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 24 ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat && 25 WriteNewDbgInfoFormatToBitcode); 26 if (M.IsNewDbgInfoFormat) 27 M.removeDebugIntrinsicDeclarations(); 28 29 const ModuleSummaryIndex *Index = 30 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M)) 31 : nullptr; 32 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash); 33 34 return PreservedAnalyses::all(); 35 } 36 37 namespace { 38 class WriteBitcodePass : public ModulePass { 39 raw_ostream &OS; // raw_ostream to print on 40 bool ShouldPreserveUseListOrder; 41 42 public: 43 static char ID; // Pass identification, replacement for typeid 44 WriteBitcodePass() : ModulePass(ID), OS(dbgs()) { 45 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 46 } 47 48 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder) 49 : ModulePass(ID), OS(o), 50 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { 51 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 52 } 53 54 StringRef getPassName() const override { return "Bitcode Writer"; } 55 56 bool runOnModule(Module &M) override { 57 ScopedDbgInfoFormatSetter FormatSetter( 58 M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode); 59 if (M.IsNewDbgInfoFormat) 60 M.removeDebugIntrinsicDeclarations(); 61 62 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr, 63 /*EmitModuleHash=*/false); 64 65 return false; 66 } 67 void getAnalysisUsage(AnalysisUsage &AU) const override { 68 AU.setPreservesAll(); 69 } 70 }; 71 } 72 73 char WriteBitcodePass::ID = 0; 74 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 75 true) 76 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 77 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 78 true) 79 80 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str, 81 bool ShouldPreserveUseListOrder) { 82 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder); 83 } 84 85 bool llvm::isBitcodeWriterPass(Pass *P) { 86 return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID; 87 } 88