2015-11-03 00:14:15 +00:00
|
|
|
//===- FunctionIndexObjectFile.cpp - Function index file implementation ---===//
|
2015-10-04 14:33:43 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the FunctionIndexObjectFile class implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/FunctionIndexObjectFile.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/IR/FunctionInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
FunctionIndexObjectFile::FunctionIndexObjectFile(
|
|
|
|
MemoryBufferRef Object, std::unique_ptr<FunctionInfoIndex> I)
|
|
|
|
: SymbolicFile(Binary::ID_FunctionIndex, Object), Index(std::move(I)) {}
|
|
|
|
|
|
|
|
FunctionIndexObjectFile::~FunctionIndexObjectFile() {}
|
|
|
|
|
|
|
|
std::unique_ptr<FunctionInfoIndex> FunctionIndexObjectFile::takeIndex() {
|
|
|
|
return std::move(Index);
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:02:11 +00:00
|
|
|
ErrorOr<MemoryBufferRef>
|
|
|
|
FunctionIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
|
2015-10-04 14:33:43 +00:00
|
|
|
for (const SectionRef &Sec : Obj.sections()) {
|
|
|
|
StringRef SecName;
|
2015-11-02 18:02:11 +00:00
|
|
|
if (std::error_code EC = Sec.getName(SecName))
|
|
|
|
return EC;
|
2015-10-04 14:33:43 +00:00
|
|
|
if (SecName == ".llvmbc") {
|
|
|
|
StringRef SecContents;
|
2015-11-02 18:02:11 +00:00
|
|
|
if (std::error_code EC = Sec.getContents(SecContents))
|
|
|
|
return EC;
|
2015-10-04 14:33:43 +00:00
|
|
|
return MemoryBufferRef(SecContents, Obj.getFileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return object_error::bitcode_section_not_found;
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:02:11 +00:00
|
|
|
ErrorOr<MemoryBufferRef>
|
|
|
|
FunctionIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
|
2015-10-04 14:33:43 +00:00
|
|
|
sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
|
|
|
|
switch (Type) {
|
2015-11-02 18:02:11 +00:00
|
|
|
case sys::fs::file_magic::bitcode:
|
|
|
|
return Object;
|
|
|
|
case sys::fs::file_magic::elf_relocatable:
|
|
|
|
case sys::fs::file_magic::macho_object:
|
|
|
|
case sys::fs::file_magic::coff_object: {
|
|
|
|
ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
|
|
|
|
ObjectFile::createObjectFile(Object, Type);
|
|
|
|
if (!ObjFile)
|
|
|
|
return ObjFile.getError();
|
|
|
|
return findBitcodeInObject(*ObjFile->get());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return object_error::invalid_file_type;
|
2015-10-04 14:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks for function index in the given memory buffer.
|
|
|
|
// returns true if found, else false.
|
|
|
|
bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
|
2015-11-19 05:52:29 +00:00
|
|
|
MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) {
|
2015-10-04 14:33:43 +00:00
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
2015-11-02 18:02:11 +00:00
|
|
|
if (!BCOrErr)
|
|
|
|
return false;
|
2015-10-04 14:33:43 +00:00
|
|
|
|
2015-11-19 05:52:29 +00:00
|
|
|
return hasFunctionSummary(BCOrErr.get(), DiagnosticHandler);
|
2015-10-04 14:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse function index in the given memory buffer.
|
|
|
|
// Return new FunctionIndexObjectFile instance containing parsed
|
|
|
|
// function summary/index.
|
|
|
|
ErrorOr<std::unique_ptr<FunctionIndexObjectFile>>
|
2015-11-19 05:52:29 +00:00
|
|
|
FunctionIndexObjectFile::create(MemoryBufferRef Object,
|
|
|
|
DiagnosticHandlerFunction DiagnosticHandler,
|
2015-12-03 02:37:23 +00:00
|
|
|
bool IsLazy) {
|
2015-10-04 14:33:43 +00:00
|
|
|
std::unique_ptr<FunctionInfoIndex> Index;
|
|
|
|
|
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
2015-11-02 18:02:11 +00:00
|
|
|
if (!BCOrErr)
|
|
|
|
return BCOrErr.getError();
|
2015-10-04 14:33:43 +00:00
|
|
|
|
2015-11-03 00:14:15 +00:00
|
|
|
ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr = getFunctionInfoIndex(
|
2015-12-03 02:37:23 +00:00
|
|
|
BCOrErr.get(), DiagnosticHandler, IsLazy);
|
2015-10-04 14:33:43 +00:00
|
|
|
|
2015-11-02 18:02:11 +00:00
|
|
|
if (std::error_code EC = IOrErr.getError())
|
|
|
|
return EC;
|
2015-10-04 14:33:43 +00:00
|
|
|
|
|
|
|
Index = std::move(IOrErr.get());
|
|
|
|
|
|
|
|
return llvm::make_unique<FunctionIndexObjectFile>(Object, std::move(Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the function summary information for function with the
|
|
|
|
// given name out of the given buffer. Parsed information is
|
|
|
|
// stored on the index object saved in this object.
|
|
|
|
std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
|
2015-11-19 05:52:29 +00:00
|
|
|
MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler,
|
|
|
|
StringRef FunctionName) {
|
2015-10-04 14:33:43 +00:00
|
|
|
sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
|
|
|
|
switch (Type) {
|
2015-11-02 18:02:11 +00:00
|
|
|
case sys::fs::file_magic::bitcode: {
|
2015-11-19 05:52:29 +00:00
|
|
|
return readFunctionSummary(Object, DiagnosticHandler, FunctionName,
|
2015-11-02 18:02:11 +00:00
|
|
|
std::move(Index));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return object_error::invalid_file_type;
|
2015-10-04 14:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 19:19:11 +00:00
|
|
|
|
|
|
|
// Parse the function index out of an IR file and return the function
|
|
|
|
// index object if found, or nullptr if not.
|
|
|
|
ErrorOr<std::unique_ptr<FunctionInfoIndex>>
|
|
|
|
llvm::getFunctionIndexForFile(StringRef Path,
|
2015-12-03 02:37:23 +00:00
|
|
|
DiagnosticHandlerFunction DiagnosticHandler) {
|
2015-11-23 19:19:11 +00:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Path);
|
|
|
|
std::error_code EC = FileOrErr.getError();
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
|
|
|
|
ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
|
2015-12-03 02:37:23 +00:00
|
|
|
object::FunctionIndexObjectFile::create(BufferRef, DiagnosticHandler);
|
2015-11-23 19:19:11 +00:00
|
|
|
EC = ObjOrErr.getError();
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
object::FunctionIndexObjectFile &Obj = **ObjOrErr;
|
|
|
|
return Obj.takeIndex();
|
|
|
|
}
|