1fe013be4SDimitry Andric //===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===//
2fe013be4SDimitry Andric //
3fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe013be4SDimitry Andric //
7fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8fe013be4SDimitry Andric 
9fe013be4SDimitry Andric #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
10*a58f00eaSDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
11*a58f00eaSDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h"
12fe013be4SDimitry Andric #include "llvm/IR/PassManager.h"
13fe013be4SDimitry Andric #include "llvm/Pass.h"
14fe013be4SDimitry Andric #include "llvm/Support/ErrorHandling.h"
15fe013be4SDimitry Andric #include "llvm/Support/MemoryBufferRef.h"
16fe013be4SDimitry Andric #include "llvm/Support/raw_ostream.h"
17fe013be4SDimitry Andric #include "llvm/TargetParser/Triple.h"
18fe013be4SDimitry Andric #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
19fe013be4SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
20fe013be4SDimitry Andric 
21*a58f00eaSDimitry Andric #include <memory>
22fe013be4SDimitry Andric #include <string>
23fe013be4SDimitry Andric 
24fe013be4SDimitry Andric using namespace llvm;
25fe013be4SDimitry Andric 
run(Module & M,ModuleAnalysisManager & AM)26fe013be4SDimitry Andric PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {
27fe013be4SDimitry Andric   if (M.getGlobalVariable("llvm.embedded.module", /*AllowInternal=*/true))
28fe013be4SDimitry Andric     report_fatal_error("Can only embed the module once",
29fe013be4SDimitry Andric                        /*gen_crash_diag=*/false);
30fe013be4SDimitry Andric 
31fe013be4SDimitry Andric   Triple T(M.getTargetTriple());
32fe013be4SDimitry Andric   if (T.getObjectFormat() != Triple::ELF)
33fe013be4SDimitry Andric     report_fatal_error(
34fe013be4SDimitry Andric         "EmbedBitcode pass currently only supports ELF object format",
35fe013be4SDimitry Andric         /*gen_crash_diag=*/false);
36*a58f00eaSDimitry Andric 
37fe013be4SDimitry Andric   std::string Data;
38fe013be4SDimitry Andric   raw_string_ostream OS(Data);
39*a58f00eaSDimitry Andric   if (IsThinLTO)
40c9157d92SDimitry Andric     ThinLTOBitcodeWriterPass(OS, /*ThinLinkOS=*/nullptr).run(M, AM);
41*a58f00eaSDimitry Andric   else
42*a58f00eaSDimitry Andric     BitcodeWriterPass(OS, /*ShouldPreserveUseListOrder=*/false, EmitLTOSummary)
43*a58f00eaSDimitry Andric         .run(M, AM);
44*a58f00eaSDimitry Andric 
45fe013be4SDimitry Andric   embedBufferInModule(M, MemoryBufferRef(Data, "ModuleData"), ".llvm.lto");
46*a58f00eaSDimitry Andric 
47fe013be4SDimitry Andric   return PreservedAnalyses::all();
48fe013be4SDimitry Andric }
49