BitcodeWriterPass.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "llvm/Bitcode/BitcodeWriterPass.h"
  13. #include "llvm/Analysis/ModuleSummaryAnalysis.h"
  14. #include "llvm/Bitcode/BitcodeWriter.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/IR/PassManager.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. using namespace llvm;
  20. PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
  21. const ModuleSummaryIndex *Index =
  22. EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
  23. : nullptr;
  24. WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
  25. return PreservedAnalyses::all();
  26. }
  27. namespace {
  28. class WriteBitcodePass : public ModulePass {
  29. raw_ostream &OS; // raw_ostream to print on
  30. bool ShouldPreserveUseListOrder;
  31. bool EmitSummaryIndex;
  32. bool EmitModuleHash;
  33. public:
  34. static char ID; // Pass identification, replacement for typeid
  35. WriteBitcodePass() : ModulePass(ID), OS(dbgs()) {
  36. initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
  37. }
  38. explicit WriteBitcodePass(raw_ostream &o, bool ShouldPreserveUseListOrder,
  39. bool EmitSummaryIndex, bool EmitModuleHash)
  40. : ModulePass(ID), OS(o),
  41. ShouldPreserveUseListOrder(ShouldPreserveUseListOrder),
  42. EmitSummaryIndex(EmitSummaryIndex), EmitModuleHash(EmitModuleHash) {
  43. initializeWriteBitcodePassPass(*PassRegistry::getPassRegistry());
  44. }
  45. StringRef getPassName() const override { return "Bitcode Writer"; }
  46. bool runOnModule(Module &M) override {
  47. const ModuleSummaryIndex *Index =
  48. EmitSummaryIndex
  49. ? &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex())
  50. : nullptr;
  51. WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index,
  52. EmitModuleHash);
  53. return false;
  54. }
  55. void getAnalysisUsage(AnalysisUsage &AU) const override {
  56. AU.setPreservesAll();
  57. if (EmitSummaryIndex)
  58. AU.addRequired<ModuleSummaryIndexWrapperPass>();
  59. }
  60. };
  61. }
  62. char WriteBitcodePass::ID = 0;
  63. INITIALIZE_PASS_BEGIN(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
  64. true)
  65. INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
  66. INITIALIZE_PASS_END(WriteBitcodePass, "write-bitcode", "Write Bitcode", false,
  67. true)
  68. ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str,
  69. bool ShouldPreserveUseListOrder,
  70. bool EmitSummaryIndex, bool EmitModuleHash) {
  71. return new WriteBitcodePass(Str, ShouldPreserveUseListOrder,
  72. EmitSummaryIndex, EmitModuleHash);
  73. }
  74. bool llvm::isBitcodeWriterPass(Pass *P) {
  75. return P->getPassID() == (llvm::AnalysisID)&WriteBitcodePass::ID;
  76. }