1 //===- FunctionIndexObjectFile.cpp - Function index file implementation ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Part of the FunctionIndexObjectFile class implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/FunctionIndexObjectFile.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/FunctionInfo.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace object;
25 FunctionIndexObjectFile::FunctionIndexObjectFile(
26 MemoryBufferRef Object, std::unique_ptr<FunctionInfoIndex> I)
27 : SymbolicFile(Binary::ID_FunctionIndex, Object), Index(std::move(I)) {}
29 FunctionIndexObjectFile::~FunctionIndexObjectFile() {}
31 std::unique_ptr<FunctionInfoIndex> FunctionIndexObjectFile::takeIndex() {
32 return std::move(Index);
35 ErrorOr<MemoryBufferRef>
36 FunctionIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
37 for (const SectionRef &Sec : Obj.sections()) {
39 if (std::error_code EC = Sec.getName(SecName))
41 if (SecName == ".llvmbc") {
42 StringRef SecContents;
43 if (std::error_code EC = Sec.getContents(SecContents))
45 return MemoryBufferRef(SecContents, Obj.getFileName());
49 return object_error::bitcode_section_not_found;
52 ErrorOr<MemoryBufferRef>
53 FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
54 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
56 case sys::fs::file_magic::bitcode:
58 case sys::fs::file_magic::elf_relocatable:
59 case sys::fs::file_magic::macho_object:
60 case sys::fs::file_magic::coff_object: {
61 ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
62 ObjectFile::createObjectFile(Object, Type);
64 return ObjFile.getError();
65 return findBitcodeInObject(*ObjFile->get());
68 return object_error::invalid_file_type;
72 // Looks for function index in the given memory buffer.
73 // returns true if found, else false.
74 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
75 MemoryBufferRef Object, LLVMContext &Context) {
76 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
80 return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
83 // Parse function index in the given memory buffer.
84 // Return new FunctionIndexObjectFile instance containing parsed
85 // function summary/index.
86 ErrorOr<std::unique_ptr<FunctionIndexObjectFile>>
87 FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
88 const Module *ExportingModule, bool IsLazy) {
89 std::unique_ptr<FunctionInfoIndex> Index;
91 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
93 return BCOrErr.getError();
95 ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr = getFunctionInfoIndex(
96 BCOrErr.get(), Context, nullptr, ExportingModule, IsLazy);
98 if (std::error_code EC = IOrErr.getError())
101 Index = std::move(IOrErr.get());
103 return llvm::make_unique<FunctionIndexObjectFile>(Object, std::move(Index));
106 // Parse the function summary information for function with the
107 // given name out of the given buffer. Parsed information is
108 // stored on the index object saved in this object.
109 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
110 MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
111 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
113 case sys::fs::file_magic::bitcode: {
114 return readFunctionSummary(Object, Context, nullptr, FunctionName,
118 return object_error::invalid_file_type;