From 26ab5772b058fcddabfecb6736a3b78c67bc1751 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Tue, 15 Mar 2016 00:04:37 +0000 Subject: [ThinLTO] Renaming of function index to module summary index (NFC) (Resubmitting after fixing missing file issue) With the changes in r263275, there are now more than just functions in the summary. Completed the renaming of data structures (started in r263275) to reflect the wider scope. In particular, changed the FunctionIndex* data structures to ModuleIndex*, and renamed related variables and comments. Also renamed the files to reflect the changes. A companion clang patch will immediately succeed this patch to reflect this renaming. llvm-svn: 263513 --- llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp | 142 +++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp (limited to 'llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp') diff --git a/llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp b/llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp new file mode 100644 index 00000000000..53d3f8e61b5 --- /dev/null +++ b/llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp @@ -0,0 +1,142 @@ +//===- ModuleSummaryIndexObjectFile.cpp - Summary index file implementation ==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Part of the ModuleSummaryIndexObjectFile class implementation. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/ModuleSummaryIndexObjectFile.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/ModuleSummaryIndex.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; + +ModuleSummaryIndexObjectFile::ModuleSummaryIndexObjectFile( + MemoryBufferRef Object, std::unique_ptr I) + : SymbolicFile(Binary::ID_ModuleSummaryIndex, Object), Index(std::move(I)) { +} + +ModuleSummaryIndexObjectFile::~ModuleSummaryIndexObjectFile() {} + +std::unique_ptr ModuleSummaryIndexObjectFile::takeIndex() { + return std::move(Index); +} + +ErrorOr +ModuleSummaryIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) { + for (const SectionRef &Sec : Obj.sections()) { + if (Sec.isBitcode()) { + StringRef SecContents; + if (std::error_code EC = Sec.getContents(SecContents)) + return EC; + return MemoryBufferRef(SecContents, Obj.getFileName()); + } + } + + return object_error::bitcode_section_not_found; +} + +ErrorOr +ModuleSummaryIndexObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Object) { + sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); + switch (Type) { + 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> ObjFile = + ObjectFile::createObjectFile(Object, Type); + if (!ObjFile) + return ObjFile.getError(); + return findBitcodeInObject(*ObjFile->get()); + } + default: + return object_error::invalid_file_type; + } +} + +// Looks for module summary index in the given memory buffer. +// returns true if found, else false. +bool ModuleSummaryIndexObjectFile::hasGlobalValueSummaryInMemBuffer( + MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler) { + ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); + if (!BCOrErr) + return false; + + return hasGlobalValueSummary(BCOrErr.get(), DiagnosticHandler); +} + +// Parse module summary index in the given memory buffer. +// Return new ModuleSummaryIndexObjectFile instance containing parsed +// module summary/index. +ErrorOr> +ModuleSummaryIndexObjectFile::create( + MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler, + bool IsLazy) { + std::unique_ptr Index; + + ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); + if (!BCOrErr) + return BCOrErr.getError(); + + ErrorOr> IOrErr = + getModuleSummaryIndex(BCOrErr.get(), DiagnosticHandler, IsLazy); + + if (std::error_code EC = IOrErr.getError()) + return EC; + + Index = std::move(IOrErr.get()); + + return llvm::make_unique(Object, + std::move(Index)); +} + +// Parse the summary information for value with the +// given name out of the given buffer. Parsed information is +// stored on the index object saved in this object. +std::error_code ModuleSummaryIndexObjectFile::findGlobalValueSummaryInMemBuffer( + MemoryBufferRef Object, DiagnosticHandlerFunction DiagnosticHandler, + StringRef ValueName) { + sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); + switch (Type) { + case sys::fs::file_magic::bitcode: { + return readGlobalValueSummary(Object, DiagnosticHandler, ValueName, + std::move(Index)); + } + default: + return object_error::invalid_file_type; + } +} + +// Parse the module summary index out of an IR file and return the summary +// index object if found, or nullptr if not. +ErrorOr> llvm::getModuleSummaryIndexForFile( + StringRef Path, DiagnosticHandlerFunction DiagnosticHandler) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Path); + std::error_code EC = FileOrErr.getError(); + if (EC) + return EC; + MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); + ErrorOr> ObjOrErr = + object::ModuleSummaryIndexObjectFile::create(BufferRef, + DiagnosticHandler); + EC = ObjOrErr.getError(); + if (EC) + return EC; + + object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr; + return Obj.takeIndex(); +} -- cgit v1.2.3