llvm.org GIT mirror llvm / 2255dc7
Sink undesirable LTO functions into the old C API We want to encourage users of the C++ LTO API to reuse memory buffers instead of repeatedly opening and reading the same file contents. This reverts commit r212305 and implements a tidier scheme. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212308 91177308-0d34-0410-b5e6-96231b3b80d8 Alp Toker 5 years ago
3 changed file(s) with 20 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
6969 static bool isBitcodeFile(const void *mem, size_t length);
7070 static bool isBitcodeFile(const char *path);
7171
72 /// Returns 'true' if the file or memory contents is LLVM bitcode for the
73 /// specified triple.
74 static bool isBitcodeFileForTarget(const void *mem,
75 size_t length,
76 const char *triplePrefix);
77 static bool isBitcodeFileForTarget(const char *path,
78 const char *triplePrefix);
72 /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
73 /// triple.
74 static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
75 StringRef triplePrefix);
76
77 /// Create a MemoryBuffer from a memory range with an optional name.
78 static MemoryBuffer *makeBuffer(const void *mem, size_t length,
79 StringRef name = "");
7980
8081 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
8182 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
201202 /// Get string that the data pointer points to.
202203 bool objcClassNameFromExpression(const Constant *c, std::string &name);
203204
204 /// Returns 'true' if the bitcode BC is for the specified target triple.
205 static bool isTargetMatch(StringRef BC, const char *TriplePrefix);
206
207205 /// Create an LTOModule (private version). N.B. This method takes ownership of
208206 /// the buffer.
209207 static LTOModule *makeLTOModule(std::unique_ptr Buffer,
210208 TargetOptions options, std::string &errMsg);
211
212 /// Create a MemoryBuffer from a memory range with an optional name.
213 static MemoryBuffer *makeBuffer(const void *mem, size_t length,
214 StringRef name = "");
215209 };
216210 }
217211 #endif // LTO_MODULE_H
6767 return type == sys::fs::file_magic::bitcode;
6868 }
6969
70 /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
71 /// LLVM bitcode for the specified triple.
72 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
73 const char *triplePrefix) {
74 MemoryBuffer *buffer = makeBuffer(mem, length);
75 if (!buffer)
76 return false;
77 return isTargetMatch(StringRef((const char *)mem, length), triplePrefix);
78 }
79
80 bool LTOModule::isBitcodeFileForTarget(const char *path,
81 const char *triplePrefix) {
82 std::unique_ptr buffer;
83 if (MemoryBuffer::getFile(path, buffer))
84 return false;
85 return isTargetMatch(buffer->getBuffer(), triplePrefix);
86 }
87
88 /// Returns 'true' if the bitcode BC is for the specified target triple.
89 bool LTOModule::isTargetMatch(StringRef BC, const char *TriplePrefix) {
90 std::unique_ptr Buffer(
91 MemoryBuffer::getMemBuffer(BC, "", false));
92 std::string Triple = getBitcodeTargetTriple(Buffer.get(), getGlobalContext());
93 return strncmp(Triple.c_str(), TriplePrefix, strlen(TriplePrefix)) == 0;
70 bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
71 StringRef triplePrefix) {
72 return getBitcodeTargetTriple(buffer, getGlobalContext()) == triplePrefix;
9473 }
9574
9675 LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
1515 #include "llvm/CodeGen/CommandFlags.h"
1616 #include "llvm/LTO/LTOCodeGenerator.h"
1717 #include "llvm/LTO/LTOModule.h"
18 #include "llvm/Support/MemoryBuffer.h"
1819 #include "llvm/Support/TargetSelect.h"
1920
2021 // extra command-line flags needed for LTOCodeGenerator
8687
8788 bool lto_module_is_object_file_for_target(const char* path,
8889 const char* target_triplet_prefix) {
89 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
90 std::unique_ptr buffer;
91 if (MemoryBuffer::getFile(path, buffer))
92 return false;
93 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
9094 }
9195
9296 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
97101 lto_module_is_object_file_in_memory_for_target(const void* mem,
98102 size_t length,
99103 const char* target_triplet_prefix) {
100 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
104 std::unique_ptr buffer(LTOModule::makeBuffer(mem, length));
105 if (!buffer)
106 return false;
107 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
101108 }
102109
103110 lto_module_t lto_module_create(const char* path) {