Index: include/llvm/Object/Binary.h =================================================================== --- include/llvm/Object/Binary.h +++ include/llvm/Object/Binary.h @@ -42,6 +42,7 @@ ID_Archive, ID_MachOUniversalBinary, ID_IR, // LLVM IR + ID_THINLTO, // ThinLTO info // Object and children. ID_StartObjects, @@ -117,6 +118,10 @@ return TypeID == ID_IR; } + bool isThinLTO() const { + return TypeID == ID_THINLTO; + } + bool isLittleEndian() const { return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B || TypeID == ID_MachO32B || TypeID == ID_MachO64B); Index: include/llvm/Object/ThinLTOObjectFile.h =================================================================== --- /dev/null +++ include/llvm/Object/ThinLTOObjectFile.h @@ -0,0 +1,78 @@ +//===- ThinLTOObjectFile.h - ThinLTO object file implementation -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the ThinLTOObjectFile template class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECT_THINLTOOBJECTFILE_H +#define LLVM_OBJECT_THINLTOOBJECTFILE_H + +#include "llvm/Object/SymbolicFile.h" + +namespace llvm { +class ThinLTOFunctionSummaryIndex; + +namespace object { +class ObjectFile; + +class ThinLTOObjectFile : public SymbolicFile { + std::unique_ptr Index; + +public: + ThinLTOObjectFile(MemoryBufferRef Object, + std::unique_ptr I); + ~ThinLTOObjectFile() override; + + // TODO: Walk through ThinLTOFunctionMap entries for function symbols. + void moveSymbolNext(DataRefImpl &Symb) const override { } + std::error_code printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override { + return std::error_code(); + } + uint32_t getSymbolFlags(DataRefImpl Symb) const override { return 0; } + basic_symbol_iterator symbol_begin_impl() const override { } + basic_symbol_iterator symbol_end_impl() const override { } + + const ThinLTOFunctionSummaryIndex &getIndex() const { + return const_cast(this)->getIndex(); + } + ThinLTOFunctionSummaryIndex &getIndex() { + return *Index; + } + std::unique_ptr takeIndex(); + + static inline bool classof(const Binary *v) { + return v->isThinLTO(); + } + + /// \brief Write the index saved here to the given file. + std::error_code writeToFile(const char* path); + + /// \brief Looks for ThinLTO information in the given memory buffer, + /// returns true if found, else false. + static bool hasThinLTOInMemBuffer(MemoryBufferRef Object, + LLVMContext &Context); + + /// \brief Parse ThinLTO information in the given memory buffer. + /// Return new ThinLTOObjectFile instance containing parsed ThinLTO + /// summary/index. + static ErrorOr> + create(MemoryBufferRef Object, LLVMContext &Context, + bool ReadFuncSummaryData = true); + + /// \brief Parse the ThinLTO 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 findThinLTOFunctionInfoInMemBuffer( + MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName); +}; +} +} + +#endif Index: lib/Object/CMakeLists.txt =================================================================== --- lib/Object/CMakeLists.txt +++ lib/Object/CMakeLists.txt @@ -16,6 +16,7 @@ RecordStreamer.cpp SymbolicFile.cpp SymbolSize.cpp + ThinLTOObjectFile.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/Object Index: lib/Object/ThinLTOObjectFile.cpp =================================================================== --- /dev/null +++ lib/Object/ThinLTOObjectFile.cpp @@ -0,0 +1,100 @@ +//===- ThinLTOObjectFile.cpp - ThinLTO object 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 ThinLTOObjectFile class implementation. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/ThinLTOObjectFile.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/ThinLTOInfo.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; + +ThinLTOObjectFile::ThinLTOObjectFile( + MemoryBufferRef Object, std::unique_ptr I) + : SymbolicFile(Binary::ID_THINLTO, Object), Index(std::move(I)) { +} + +ThinLTOObjectFile::~ThinLTOObjectFile() { +} + +// Write the index saved here to the given file. +std::error_code ThinLTOObjectFile::writeToFile(const char* path) { + std::error_code EC; + raw_fd_ostream OS(path, EC, sys::fs::OpenFlags::F_None); + WriteThinLTOToFile(Index.get(), OS); + return EC; +} + +std::unique_ptr ThinLTOObjectFile::takeIndex() { + return std::move(Index); +} + +// Looks for ThinLTO information in the given memory buffer +// returns true if found, else false. +bool ThinLTOObjectFile::hasThinLTOInMemBuffer(MemoryBufferRef Object, + LLVMContext &Context) { + sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); + switch (Type) { + case sys::fs::file_magic::bitcode: + return hasThinLTOIndex(Object, Context, nullptr); + default: + assert(false); + } + return false; +} + +// Parse ThinLTO information in the given memory buffer. +// Return new ThinLTOObjectFile instance containing parsed ThinLTO +// summary/index. +ErrorOr> +llvm::object::ThinLTOObjectFile::create(MemoryBufferRef Object, + LLVMContext &Context, + bool ReadFuncSummaryData) { + std::unique_ptr Index; + sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); + switch (Type) { + case sys::fs::file_magic::bitcode: { + ErrorOr> IOrErr = + getThinLTOIndex(Object, Context, nullptr, ReadFuncSummaryData); + if (std::error_code EC = IOrErr.getError()) + return EC; + + Index = std::move(IOrErr.get()); + + break; + } + default: + return object_error::invalid_file_type; + } + + return llvm::make_unique(Object, std::move(Index)); +} + +// Parse the ThinLTO 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 ThinLTOObjectFile::findThinLTOFunctionInfoInMemBuffer( + MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) { + sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer()); + switch (Type) { + case sys::fs::file_magic::bitcode: { + return readThinLTOFunctionInfo(Object, Context, nullptr, + FunctionName, std::move(Index)); + } + default: + return object_error::invalid_file_type; + } +}