blob: 6796cf8cee540ed2a0226693f6cefc788bd978be [file] [log] [blame]
Chandler Carruthb7bdfd62014-01-13 07:38:24 +00001//===- BitcodeWriterPass.cpp - Bitcode writing pass -----------------------===//
Chris Lattner7ef41712007-05-06 02:30:12 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner7ef41712007-05-06 02:30:12 +00006//
7//===----------------------------------------------------------------------===//
8//
9// BitcodeWriterPass implementation.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000013#include "llvm/Bitcode/BitcodeWriterPass.h"
Teresa Johnson2d5487c2016-04-11 13:58:45 +000014#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000015#include "llvm/Bitcode/BitcodeWriter.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000016#include "llvm/IR/Module.h"
17#include "llvm/IR/PassManager.h"
Chris Lattner7ef41712007-05-06 02:30:12 +000018#include "llvm/Pass.h"
19using namespace llvm;
20
Teresa Johnsonf93b2462016-08-12 13:53:02 +000021PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
22 const ModuleSummaryIndex *Index =
23 EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
24 : nullptr;
Rafael Espindola6a86e252018-02-14 19:11:32 +000025 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000026 return PreservedAnalyses::all();
27}
28
Chris Lattner7ef41712007-05-06 02:30:12 +000029namespace {
30 class WriteBitcodePass : public ModulePass {
Chris Lattner69733952009-08-23 07:49:08 +000031 raw_ostream &OS; // raw_ostream to print on
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000032 bool ShouldPreserveUseListOrder;
Teresa Johnson26ab5772016-03-15 00:04:37 +000033 bool EmitSummaryIndex;
Mehdi Aminif9e45762016-04-10 21:07:19 +000034 bool EmitModuleHash;
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000035
Chris Lattner7ef41712007-05-06 02:30:12 +000036 public:
Daniel Dunbar890d0de2008-10-22 17:39:14 +000037 static char ID; // Pass identification, replacement for typeid
Teresa Johnson2d5487c2016-04-11 13:58:45 +000038 WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
39 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
40 }
41
Teresa Johnson403a7872015-10-04 14:33:43 +000042 explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
Mehdi Aminif9e45762016-04-10 21:07:19 +000043 bool EmitSummaryIndex, bool EmitModuleHash)
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000044 : ModulePass(ID), OS(o),
Teresa Johnson403a7872015-10-04 14:33:43 +000045 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
Teresa Johnson2d5487c2016-04-11 13:58:45 +000046 EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
47 initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
48 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000049
Mehdi Amini117296c2016-10-01 02:56:57 +000050 StringRef getPassName() const override { return "Bitcode Writer"; }
Joe Abbey2ad8df22012-11-25 15:23:39 +000051
Craig Topper85482992014-03-05 07:52:44 +000052 bool runOnModule(Module &M) override {
Teresa Johnson2d5487c2016-04-11 13:58:45 +000053 const ModuleSummaryIndex *Index =
54 EmitSummaryIndex
55 ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
56 : nullptr;
Rafael Espindola6a86e252018-02-14 19:11:32 +000057 WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
Teresa Johnson2d5487c2016-04-11 13:58:45 +000058 EmitModuleHash);
Chris Lattner7ef41712007-05-06 02:30:12 +000059 return false;
60 }
Teresa Johnson2d5487c2016-04-11 13:58:45 +000061 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.setPreservesAll();
63 if (EmitSummaryIndex)
64 AU.addRequired<ModuleSummaryIndexWrapperPass>();
65 }
Chris Lattner7ef41712007-05-06 02:30:12 +000066 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000067}
Chris Lattner7ef41712007-05-06 02:30:12 +000068
69char WriteBitcodePass::ID = 0;
Teresa Johnson2d5487c2016-04-11 13:58:45 +000070INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
71 true)
72INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
73INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
74 true)
Chris Lattner7ef41712007-05-06 02:30:12 +000075
Duncan P. N. Exon Smith679db332015-04-15 00:34:24 +000076ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
Teresa Johnson403a7872015-10-04 14:33:43 +000077 bool ShouldPreserveUseListOrder,
Mehdi Aminif9e45762016-04-10 21:07:19 +000078 bool EmitSummaryIndex, bool EmitModuleHash) {
Teresa Johnson403a7872015-10-04 14:33:43 +000079 return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
Mehdi Aminif9e45762016-04-10 21:07:19 +000080 EmitSummaryIndex, EmitModuleHash);
Daniel Dunbar890d0de2008-10-22 17:39:14 +000081}
Vedant Kumaradbd27a2018-06-04 00:11:49 +000082
83bool llvm::isBitcodeWriterPass(Pass *P) {
84 return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
85}