Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Object / IRObjectFile.cpp
blob5001ac336be0f8a4be6625c2a6d08cff3e5d88ee
1 //===- IRObjectFile.cpp - IR object file implementation ---------*- C++ -*-===//
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 // Part of the IRObjectFile class implementation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Object/IRObjectFile.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/BinaryFormat/Magic.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
17 #include "llvm/IR/GVMaterializer.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26 using namespace object;
28 IRObjectFile::IRObjectFile(MemoryBufferRef Object,
29 std::vector<std::unique_ptr<Module>> Mods)
30 : SymbolicFile(Binary::ID_IR, Object), Mods(std::move(Mods)) {
31 for (auto &M : this->Mods)
32 SymTab.addModule(M.get());
35 IRObjectFile::~IRObjectFile() {}
37 static ModuleSymbolTable::Symbol getSym(DataRefImpl &Symb) {
38 return *reinterpret_cast<ModuleSymbolTable::Symbol *>(Symb.p);
41 void IRObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
42 Symb.p += sizeof(ModuleSymbolTable::Symbol);
45 std::error_code IRObjectFile::printSymbolName(raw_ostream &OS,
46 DataRefImpl Symb) const {
47 SymTab.printSymbolName(OS, getSym(Symb));
48 return std::error_code();
51 uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const {
52 return SymTab.getSymbolFlags(getSym(Symb));
55 basic_symbol_iterator IRObjectFile::symbol_begin() const {
56 DataRefImpl Ret;
57 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data());
58 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
61 basic_symbol_iterator IRObjectFile::symbol_end() const {
62 DataRefImpl Ret;
63 Ret.p = reinterpret_cast<uintptr_t>(SymTab.symbols().data() +
64 SymTab.symbols().size());
65 return basic_symbol_iterator(BasicSymbolRef(Ret, this));
68 StringRef IRObjectFile::getTargetTriple() const {
69 // Each module must have the same target triple, so we arbitrarily access the
70 // first one.
71 return Mods[0]->getTargetTriple();
74 Expected<MemoryBufferRef>
75 IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
76 for (const SectionRef &Sec : Obj.sections()) {
77 if (Sec.isBitcode()) {
78 StringRef SecContents;
79 if (std::error_code EC = Sec.getContents(SecContents))
80 return errorCodeToError(EC);
81 return MemoryBufferRef(SecContents, Obj.getFileName());
85 return errorCodeToError(object_error::bitcode_section_not_found);
88 Expected<MemoryBufferRef>
89 IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
90 file_magic Type = identify_magic(Object.getBuffer());
91 switch (Type) {
92 case file_magic::bitcode:
93 return Object;
94 case file_magic::elf_relocatable:
95 case file_magic::macho_object:
96 case file_magic::coff_object: {
97 Expected<std::unique_ptr<ObjectFile>> ObjFile =
98 ObjectFile::createObjectFile(Object, Type);
99 if (!ObjFile)
100 return ObjFile.takeError();
101 return findBitcodeInObject(*ObjFile->get());
103 default:
104 return errorCodeToError(object_error::invalid_file_type);
108 Expected<std::unique_ptr<IRObjectFile>>
109 IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) {
110 Expected<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
111 if (!BCOrErr)
112 return BCOrErr.takeError();
114 Expected<std::vector<BitcodeModule>> BMsOrErr =
115 getBitcodeModuleList(*BCOrErr);
116 if (!BMsOrErr)
117 return BMsOrErr.takeError();
119 std::vector<std::unique_ptr<Module>> Mods;
120 for (auto BM : *BMsOrErr) {
121 Expected<std::unique_ptr<Module>> MOrErr =
122 BM.getLazyModule(Context, /*ShouldLazyLoadMetadata*/ true,
123 /*IsImporting*/ false);
124 if (!MOrErr)
125 return MOrErr.takeError();
127 Mods.push_back(std::move(*MOrErr));
130 return std::unique_ptr<IRObjectFile>(
131 new IRObjectFile(*BCOrErr, std::move(Mods)));
134 Expected<IRSymtabFile> object::readIRSymtab(MemoryBufferRef MBRef) {
135 IRSymtabFile F;
136 Expected<MemoryBufferRef> BCOrErr =
137 IRObjectFile::findBitcodeInMemBuffer(MBRef);
138 if (!BCOrErr)
139 return BCOrErr.takeError();
141 Expected<BitcodeFileContents> BFCOrErr = getBitcodeFileContents(*BCOrErr);
142 if (!BFCOrErr)
143 return BFCOrErr.takeError();
145 Expected<irsymtab::FileContents> FCOrErr = irsymtab::readBitcode(*BFCOrErr);
146 if (!FCOrErr)
147 return FCOrErr.takeError();
149 F.Mods = std::move(BFCOrErr->Mods);
150 F.Symtab = std::move(FCOrErr->Symtab);
151 F.Strtab = std::move(FCOrErr->Strtab);
152 F.TheReader = std::move(FCOrErr->TheReader);
153 return std::move(F);