2016-03-15 08:04:37 +08:00
|
|
|
//===- ModuleSummaryIndexObjectFile.cpp - Summary index file implementation ==//
|
2015-10-04 22:33:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-03-15 08:04:37 +08:00
|
|
|
// Part of the ModuleSummaryIndexObjectFile class implementation.
|
2015-10-04 22:33:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
|
2015-10-04 22:33:43 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2015-10-04 22:33:43 +08:00
|
|
|
#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;
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndexObjectFile::ModuleSummaryIndexObjectFile(
|
|
|
|
MemoryBufferRef Object, std::unique_ptr<ModuleSummaryIndex> I)
|
|
|
|
: SymbolicFile(Binary::ID_ModuleSummaryIndex, Object), Index(std::move(I)) {
|
|
|
|
}
|
2015-10-04 22:33:43 +08:00
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndexObjectFile::~ModuleSummaryIndexObjectFile() {}
|
2015-10-04 22:33:43 +08:00
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> ModuleSummaryIndexObjectFile::takeIndex() {
|
2015-10-04 22:33:43 +08:00
|
|
|
return std::move(Index);
|
|
|
|
}
|
|
|
|
|
2015-11-03 02:02:11 +08:00
|
|
|
ErrorOr<MemoryBufferRef>
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
|
2015-10-04 22:33:43 +08:00
|
|
|
for (const SectionRef &Sec : Obj.sections()) {
|
2016-03-01 03:40:10 +08:00
|
|
|
if (Sec.isBitcode()) {
|
2015-10-04 22:33:43 +08:00
|
|
|
StringRef SecContents;
|
2015-11-03 02:02:11 +08:00
|
|
|
if (std::error_code EC = Sec.getContents(SecContents))
|
|
|
|
return EC;
|
2015-10-04 22:33:43 +08:00
|
|
|
return MemoryBufferRef(SecContents, Obj.getFileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return object_error::bitcode_section_not_found;
|
|
|
|
}
|
|
|
|
|
2015-11-03 02:02:11 +08:00
|
|
|
ErrorOr<MemoryBufferRef>
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) {
|
2015-10-04 22:33:43 +08:00
|
|
|
sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
|
|
|
|
switch (Type) {
|
2015-11-03 02:02:11 +08: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: {
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> ObjFile =
|
2015-11-03 02:02:11 +08:00
|
|
|
ObjectFile::createObjectFile(Object, Type);
|
|
|
|
if (!ObjFile)
|
2016-04-07 06:14:09 +08:00
|
|
|
return errorToErrorCode(ObjFile.takeError());
|
2015-11-03 02:02:11 +08:00
|
|
|
return findBitcodeInObject(*ObjFile->get());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return object_error::invalid_file_type;
|
2015-10-04 22:33:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
// Parse module summary index in the given memory buffer.
|
|
|
|
// Return new ModuleSummaryIndexObjectFile instance containing parsed
|
|
|
|
// module summary/index.
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndexObjectFile>>
|
|
|
|
ModuleSummaryIndexObjectFile::create(MemoryBufferRef Object) {
|
2015-10-04 22:33:43 +08:00
|
|
|
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
|
2015-11-03 02:02:11 +08:00
|
|
|
if (!BCOrErr)
|
2016-11-12 03:50:39 +08:00
|
|
|
return errorCodeToError(BCOrErr.getError());
|
2015-10-04 22:33:43 +08:00
|
|
|
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndex>> IOrErr =
|
|
|
|
getModuleSummaryIndex(BCOrErr.get());
|
2015-10-04 22:33:43 +08:00
|
|
|
|
2016-11-12 03:50:39 +08:00
|
|
|
if (!IOrErr)
|
2016-11-12 04:39:02 +08:00
|
|
|
return IOrErr.takeError();
|
2015-10-04 22:33:43 +08:00
|
|
|
|
2016-11-12 03:50:39 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> Index = std::move(IOrErr.get());
|
2016-03-15 08:04:37 +08:00
|
|
|
return llvm::make_unique<ModuleSummaryIndexObjectFile>(Object,
|
|
|
|
std::move(Index));
|
2015-10-04 22:33:43 +08:00
|
|
|
}
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
// Parse the module summary index out of an IR file and return the summary
|
2015-11-24 03:19:11 +08:00
|
|
|
// index object if found, or nullptr if not.
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndex>>
|
|
|
|
llvm::getModuleSummaryIndexForFile(StringRef Path) {
|
2015-11-24 03:19:11 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Path);
|
|
|
|
std::error_code EC = FileOrErr.getError();
|
|
|
|
if (EC)
|
2016-11-12 03:50:39 +08:00
|
|
|
return errorCodeToError(EC);
|
2015-11-24 03:19:11 +08:00
|
|
|
MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
|
|
|
|
object::ModuleSummaryIndexObjectFile::create(BufferRef);
|
|
|
|
if (!ObjOrErr)
|
|
|
|
return ObjOrErr.takeError();
|
2015-11-24 03:19:11 +08:00
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr;
|
2015-11-24 03:19:11 +08:00
|
|
|
return Obj.takeIndex();
|
|
|
|
}
|