1 //===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // BitcodeWriterPass implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Bitcode/BitcodeWriterPass.h"
15 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/Pass.h"
20 using namespace llvm;
21
run(Module & M,ModuleAnalysisManager &)22 PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &) {
23 std::unique_ptr<ModuleSummaryIndex> Index;
24 if (EmitSummaryIndex)
25 Index = ModuleSummaryIndexBuilder(&M).takeIndex();
26 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index.get(),
27 EmitModuleHash);
28 return PreservedAnalyses::all();
29 }
30
31 namespace {
32 class WriteBitcodePass : public ModulePass {
33 raw_ostream &OS; // raw_ostream to print on
34 bool ShouldPreserveUseListOrder;
35 bool EmitSummaryIndex;
36 bool EmitModuleHash;
37
38 public:
39 static char ID; // Pass identification, replacement for typeid
WriteBitcodePass()40 WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
41 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
42 }
43
WriteBitcodePass(raw_ostream & o,bool ShouldPreserveUseListOrder,bool EmitSummaryIndex,bool EmitModuleHash)44 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
45 bool EmitSummaryIndex, bool EmitModuleHash)
46 : ModulePass(ID), OS(o),
47 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
48 EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
49 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
50 }
51
getPassName() const52 const char *getPassName() const override { return "Bitcode Writer"; }
53
runOnModule(Module & M)54 bool runOnModule(Module &M) override {
55 const ModuleSummaryIndex *Index =
56 EmitSummaryIndex
57 ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
58 : nullptr;
59 WriteBitcodeToFile(&M, OS, ShouldPreserveUseListOrder, Index,
60 EmitModuleHash);
61 return false;
62 }
getAnalysisUsage(AnalysisUsage & AU) const63 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.setPreservesAll();
65 if (EmitSummaryIndex)
66 AU.addRequired<ModuleSummaryIndexWrapperPass>();
67 }
68 };
69 }
70
71 char WriteBitcodePass::ID = 0;
72 INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
73 true)
INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)74 INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
75 INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
76 true)
77
78 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
79 bool ShouldPreserveUseListOrder,
80 bool EmitSummaryIndex, bool EmitModuleHash) {
81 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
82 EmitSummaryIndex, EmitModuleHash);
83 }
84