191bc56edSDimitry Andric //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // BitcodeWriterPass implementation. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 1491bc56edSDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h" 153ca95b02SDimitry Andric #include "llvm/Analysis/ModuleSummaryAnalysis.h" 16d88c1a5aSDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h" 1791bc56edSDimitry Andric #include "llvm/IR/Module.h" 1891bc56edSDimitry Andric #include "llvm/IR/PassManager.h" 19f22ef01cSRoman Divacky #include "llvm/Pass.h" 20f22ef01cSRoman Divacky using namespace llvm; 21f22ef01cSRoman Divacky 22d88c1a5aSDimitry Andric PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { 23d88c1a5aSDimitry Andric const ModuleSummaryIndex *Index = 24d88c1a5aSDimitry Andric EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M)) 25d88c1a5aSDimitry Andric : nullptr; 26d88c1a5aSDimitry Andric WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash); 2791bc56edSDimitry Andric return PreservedAnalyses::all(); 2891bc56edSDimitry Andric } 2991bc56edSDimitry Andric 30f22ef01cSRoman Divacky namespace { 31f22ef01cSRoman Divacky class WriteBitcodePass : public ModulePass { 32f22ef01cSRoman Divacky raw_ostream &OS; // raw_ostream to print on 33ff0cc061SDimitry Andric bool ShouldPreserveUseListOrder; 343ca95b02SDimitry Andric bool EmitSummaryIndex; 353ca95b02SDimitry Andric bool EmitModuleHash; 36ff0cc061SDimitry Andric 37f22ef01cSRoman Divacky public: 38f22ef01cSRoman Divacky static char ID; // Pass identification, replacement for typeid 393ca95b02SDimitry Andric WriteBitcodePass() : ModulePass(ID), OS(dbgs()) { 403ca95b02SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 413ca95b02SDimitry Andric } 423ca95b02SDimitry Andric 437d523365SDimitry Andric explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder, 443ca95b02SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash) 45ff0cc061SDimitry Andric : ModulePass(ID), OS(o), 467d523365SDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder), 473ca95b02SDimitry Andric EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) { 483ca95b02SDimitry Andric initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry()); 493ca95b02SDimitry Andric } 50f22ef01cSRoman Divacky 51d88c1a5aSDimitry Andric StringRef getPassName() const override { return "Bitcode Writer"; } 52f22ef01cSRoman Divacky 5391bc56edSDimitry Andric bool runOnModule(Module &M) override { 543ca95b02SDimitry Andric const ModuleSummaryIndex *Index = 553ca95b02SDimitry Andric EmitSummaryIndex 563ca95b02SDimitry Andric ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()) 573ca95b02SDimitry Andric : nullptr; 583ca95b02SDimitry Andric WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index, 593ca95b02SDimitry Andric EmitModuleHash); 60f22ef01cSRoman Divacky return false; 61f22ef01cSRoman Divacky } 623ca95b02SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 633ca95b02SDimitry Andric AU.setPreservesAll(); 643ca95b02SDimitry Andric if (EmitSummaryIndex) 653ca95b02SDimitry Andric AU.addRequired<ModuleSummaryIndexWrapperPass>(); 663ca95b02SDimitry Andric } 67f22ef01cSRoman Divacky }; 683dac3a9bSDimitry Andric } 69f22ef01cSRoman Divacky 70f22ef01cSRoman Divacky char WriteBitcodePass::ID = 0; 713ca95b02SDimitry Andric INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 723ca95b02SDimitry Andric true) 733ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) 743ca95b02SDimitry Andric INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false, 753ca95b02SDimitry Andric true) 76f22ef01cSRoman Divacky 77ff0cc061SDimitry Andric ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str, 787d523365SDimitry Andric bool ShouldPreserveUseListOrder, 793ca95b02SDimitry Andric bool EmitSummaryIndex, bool EmitModuleHash) { 807d523365SDimitry Andric return new WriteBitcodePass(Str, ShouldPreserveUseListOrder, 813ca95b02SDimitry Andric EmitSummaryIndex, EmitModuleHash); 82f22ef01cSRoman Divacky } 83