1 //===- FunctionIndexObjectFile.cpp - Function index file implementation
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // Part of the FunctionIndexObjectFile class implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Object/FunctionIndexObjectFile.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/FunctionInfo.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace object;
26 FunctionIndexObjectFile::FunctionIndexObjectFile(
27 MemoryBufferRef Object, std::unique_ptr<FunctionInfoIndex> I)
28 : SymbolicFile(Binary::ID_FunctionIndex, Object), Index(std::move(I)) {}
30 FunctionIndexObjectFile::~FunctionIndexObjectFile() {}
32 std::unique_ptr<FunctionInfoIndex> FunctionIndexObjectFile::takeIndex() {
33 return std::move(Index);
36 ErrorOr<MemoryBufferRef>
37 FunctionIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
38 for (const SectionRef &Sec : Obj.sections()) {
40 if (std::error_code EC = Sec.getName(SecName))
42 if (SecName == ".llvmbc") {
43 StringRef SecContents;
44 if (std::error_code EC = Sec.getContents(SecContents))
46 return MemoryBufferRef(SecContents, Obj.getFileName());
50 return object_error::bitcode_section_not_found;
53 ErrorOr<MemoryBufferRef>
54 FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
55 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
57 case sys::fs::file_magic::bitcode:
59 case sys::fs::file_magic::elf_relocatable:
60 case sys::fs::file_magic::macho_object:
61 case sys::fs::file_magic::coff_object: {
62 ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
63 ObjectFile::createObjectFile(Object, Type);
65 return ObjFile.getError();
66 return findBitcodeInObject(*ObjFile->get());
69 return object_error::invalid_file_type;
73 // Looks for function index in the given memory buffer.
74 // returns true if found, else false.
75 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
76 MemoryBufferRef Object, LLVMContext &Context) {
77 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
81 return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
84 // Parse function index in the given memory buffer.
85 // Return new FunctionIndexObjectFile instance containing parsed
86 // function summary/index.
87 ErrorOr<std::unique_ptr<FunctionIndexObjectFile>>
88 FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
90 std::unique_ptr<FunctionInfoIndex> Index;
92 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
94 return BCOrErr.getError();
96 ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr =
97 getFunctionInfoIndex(BCOrErr.get(), Context, nullptr, IsLazy);
99 if (std::error_code EC = IOrErr.getError())
102 Index = std::move(IOrErr.get());
104 return llvm::make_unique<FunctionIndexObjectFile>(Object, std::move(Index));
107 // Parse the function summary information for function with the
108 // given name out of the given buffer. Parsed information is
109 // stored on the index object saved in this object.
110 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
111 MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
112 sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
114 case sys::fs::file_magic::bitcode: {
115 return readFunctionSummary(Object, Context, nullptr, FunctionName,
119 return object_error::invalid_file_type;