diff --git a/include/llvm/LTO/LTOModule.h b/include/llvm/LTO/LTOModule.h index 64c066e6520..2f1f3c2152a 100644 --- a/include/llvm/LTO/LTOModule.h +++ b/include/llvm/LTO/LTOModule.h @@ -70,13 +70,14 @@ public: static bool isBitcodeFile(const void *mem, size_t length); static bool isBitcodeFile(const char *path); - /// Returns 'true' if the file or memory contents is LLVM bitcode for the - /// specified triple. - static bool isBitcodeFileForTarget(const void *mem, - size_t length, - const char *triplePrefix); - static bool isBitcodeFileForTarget(const char *path, - const char *triplePrefix); + /// Returns 'true' if the memory buffer is LLVM bitcode for the specified + /// triple. + static bool isBitcodeForTarget(MemoryBuffer *memBuffer, + StringRef triplePrefix); + + /// Create a MemoryBuffer from a memory range with an optional name. + static MemoryBuffer *makeBuffer(const void *mem, size_t length, + StringRef name = ""); /// Create an LTOModule. N.B. These methods take ownership of the buffer. The /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters, @@ -202,17 +203,10 @@ private: /// Get string that the data pointer points to. bool objcClassNameFromExpression(const Constant *c, std::string &name); - /// Returns 'true' if the bitcode BC is for the specified target triple. - static bool isTargetMatch(StringRef BC, const char *TriplePrefix); - /// Create an LTOModule (private version). N.B. This method takes ownership of /// the buffer. static LTOModule *makeLTOModule(std::unique_ptr Buffer, TargetOptions options, std::string &errMsg); - - /// Create a MemoryBuffer from a memory range with an optional name. - static MemoryBuffer *makeBuffer(const void *mem, size_t length, - StringRef name = ""); }; } #endif // LTO_MODULE_H diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp index 6ba99eda219..bdf3ed563d9 100644 --- a/lib/LTO/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -68,30 +68,9 @@ bool LTOModule::isBitcodeFile(const char *path) { return type == sys::fs::file_magic::bitcode; } -/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is -/// LLVM bitcode for the specified triple. -bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, - const char *triplePrefix) { - MemoryBuffer *buffer = makeBuffer(mem, length); - if (!buffer) - return false; - return isTargetMatch(StringRef((const char *)mem, length), triplePrefix); -} - -bool LTOModule::isBitcodeFileForTarget(const char *path, - const char *triplePrefix) { - std::unique_ptr buffer; - if (MemoryBuffer::getFile(path, buffer)) - return false; - return isTargetMatch(buffer->getBuffer(), triplePrefix); -} - -/// Returns 'true' if the bitcode BC is for the specified target triple. -bool LTOModule::isTargetMatch(StringRef BC, const char *TriplePrefix) { - std::unique_ptr Buffer( - MemoryBuffer::getMemBuffer(BC, "", false)); - std::string Triple = getBitcodeTargetTriple(Buffer.get(), getGlobalContext()); - return strncmp(Triple.c_str(), TriplePrefix, strlen(TriplePrefix)) == 0; +bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer, + StringRef triplePrefix) { + return getBitcodeTargetTriple(buffer, getGlobalContext()) == triplePrefix; } LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options, diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index 5f021244249..8df7315ff79 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -16,6 +16,7 @@ #include "llvm/CodeGen/CommandFlags.h" #include "llvm/LTO/LTOCodeGenerator.h" #include "llvm/LTO/LTOModule.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/TargetSelect.h" // extra command-line flags needed for LTOCodeGenerator @@ -87,7 +88,10 @@ bool lto_module_is_object_file(const char* path) { bool lto_module_is_object_file_for_target(const char* path, const char* target_triplet_prefix) { - return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix); + std::unique_ptr buffer; + if (MemoryBuffer::getFile(path, buffer)) + return false; + return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); } bool lto_module_is_object_file_in_memory(const void* mem, size_t length) { @@ -98,7 +102,10 @@ bool lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length, const char* target_triplet_prefix) { - return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix); + std::unique_ptr buffer(LTOModule::makeBuffer(mem, length)); + if (!buffer) + return false; + return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); } lto_module_t lto_module_create(const char* path) {