* @{
*/
-#define LTO_API_VERSION 11
+#define LTO_API_VERSION 12
/**
* \since prior to LTO_API_VERSION=3
/**
* Generates code for all added modules into one native object file.
+ * This calls lto_codegen_optimize then lto_codegen_compile_optimized.
+ *
* On success returns a pointer to a generated mach-o/ELF buffer and
* length set to the buffer size. The buffer is owned by the
* lto_code_gen_t and will be freed when lto_codegen_dispose()
/**
* Generates code for all added modules into one native object file.
+ * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead
+ * of returning a generated mach-o/ELF buffer, it writes to a file).
+ *
* The name of the file is written to name. Returns true on error.
*
* \since LTO_API_VERSION=5
extern lto_bool_t
lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
+/**
+ * Runs optimization for the merged module. Returns true on error.
+ *
+ * \since LTO_API_VERSION=12
+ */
+extern lto_bool_t
+lto_codegen_optimize(lto_code_gen_t cg);
+
+/**
+ * Generates code for the optimized merged module into one native object file.
+ * It will not run any IR optimizations on the merged module.
+ *
+ * On success returns a pointer to a generated mach-o/ELF buffer and length set
+ * to the buffer size. The buffer is owned by the lto_code_gen_t and will be
+ * freed when lto_codegen_dispose() is called, or
+ * lto_codegen_compile_optimized() is called again. On failure, returns NULL
+ * (check lto_get_error_message() for details).
+ *
+ * \since LTO_API_VERSION=12
+ */
+extern const void*
+lto_codegen_compile_optimized(lto_code_gen_t cg, size_t* length);
+
+/**
+ * Returns the runtime API version.
+ *
+ * \since LTO_API_VERSION=12
+ */
+extern unsigned int
+lto_api_version() {
+ return LTO_API_VERSION;
+}
/**
* Sets options to help debug codegen bugs.
bool disableVectorization,
std::string &errMsg);
+ // Optimizes the merged module. Returns true on success.
+ bool optimize(bool disableOpt,
+ bool disableInline,
+ bool disableGVNLoadPRE,
+ bool disableVectorization,
+ std::string &errMsg);
+
+ // Compiles the merged optimized module into a single object file. It brings
+ // the object to a buffer, and returns the buffer to the caller. Return NULL
+ // if the compilation was not successful.
+ const void *compileOptimized(size_t *length, std::string &errMsg);
+
void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
LLVMContext &getContext() { return Context; }
private:
void initializeLTOPasses();
- bool generateObjectFile(raw_ostream &out, bool disableOpt, bool disableInline,
- bool disableGVNLoadPRE, bool disableVectorization,
- std::string &errMsg);
+ bool compileOptimized(raw_ostream &out, std::string &errMsg);
+ bool compileOptimizedToFile(const char **name, std::string &errMsg);
void applyScopeRestrictions();
void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
std::vector<const char *> &MustPreserveList,
return true;
}
-bool LTOCodeGenerator::compile_to_file(const char** name,
- bool disableOpt,
- bool disableInline,
- bool disableGVNLoadPRE,
- bool disableVectorization,
- std::string& errMsg) {
+bool LTOCodeGenerator::compileOptimizedToFile(const char **name,
+ std::string &errMsg) {
// make unique temp .o file to put generated object file
SmallString<128> Filename;
int FD;
// generate object file
tool_output_file objFile(Filename.c_str(), FD);
- bool genResult =
- generateObjectFile(objFile.os(), disableOpt, disableInline,
- disableGVNLoadPRE, disableVectorization, errMsg);
+ bool genResult = compileOptimized(objFile.os(), errMsg);
objFile.os().close();
if (objFile.os().has_error()) {
objFile.os().clear_error();
return true;
}
-const void* LTOCodeGenerator::compile(size_t* length,
- bool disableOpt,
- bool disableInline,
- bool disableGVNLoadPRE,
- bool disableVectorization,
- std::string& errMsg) {
+const void *LTOCodeGenerator::compileOptimized(size_t *length,
+ std::string &errMsg) {
const char *name;
- if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE,
- disableVectorization, errMsg))
+ if (!compileOptimizedToFile(&name, errMsg))
return nullptr;
// read .o file into memory buffer
return NativeObjectFile->getBufferStart();
}
+
+bool LTOCodeGenerator::compile_to_file(const char **name,
+ bool disableOpt,
+ bool disableInline,
+ bool disableGVNLoadPRE,
+ bool disableVectorization,
+ std::string &errMsg) {
+ if (!optimize(disableOpt, disableInline, disableGVNLoadPRE,
+ disableVectorization, errMsg))
+ return false;
+
+ return compileOptimizedToFile(name, errMsg);
+}
+
+const void* LTOCodeGenerator::compile(size_t *length,
+ bool disableOpt,
+ bool disableInline,
+ bool disableGVNLoadPRE,
+ bool disableVectorization,
+ std::string &errMsg) {
+ if (!optimize(disableOpt, disableInline, disableGVNLoadPRE,
+ disableVectorization, errMsg))
+ return nullptr;
+
+ return compileOptimized(length, errMsg);
+}
+
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
if (TargetMach)
return true;
}
/// Optimize merged modules using various IPO passes
-bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
- bool DisableOpt,
- bool DisableInline,
- bool DisableGVNLoadPRE,
- bool DisableVectorization,
- std::string &errMsg) {
+bool LTOCodeGenerator::optimize(bool DisableOpt,
+ bool DisableInline,
+ bool DisableGVNLoadPRE,
+ bool DisableVectorization,
+ std::string &errMsg) {
if (!this->determineTarget(errMsg))
return false;
PMB.populateLTOPassManager(passes);
+ // Run our queue of passes all at once now, efficiently.
+ passes.run(*mergedModule);
+
+ return true;
+}
+
+bool LTOCodeGenerator::compileOptimized(raw_ostream &out, std::string &errMsg) {
+ if (!this->determineTarget(errMsg))
+ return false;
+
+ Module *mergedModule = IRLinker.getModule();
+
+ // Mark which symbols can not be internalized
+ this->applyScopeRestrictions();
+
PassManager codeGenPasses;
codeGenPasses.add(new DataLayoutPass());
return false;
}
- // Run our queue of passes all at once now, efficiently.
- passes.run(*mergedModule);
-
// Run the code generator, and write assembly file
codeGenPasses.run(*mergedModule);
sLastErrorString);
}
+bool lto_codegen_optimize(lto_code_gen_t cg) {
+ if (!parsedOptions) {
+ unwrap(cg)->parseCodeGenDebugOptions();
+ lto_add_attrs(cg);
+ parsedOptions = true;
+ }
+ return !unwrap(cg)->optimize(DisableOpt, DisableInline,
+ DisableGVNLoadPRE, DisableLTOVectorization,
+ sLastErrorString);
+}
+
+const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
+ if (!parsedOptions) {
+ unwrap(cg)->parseCodeGenDebugOptions();
+ lto_add_attrs(cg);
+ parsedOptions = true;
+ }
+ return unwrap(cg)->compileOptimized(length, sLastErrorString);
+}
+
bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
if (!parsedOptions) {
unwrap(cg)->parseCodeGenDebugOptions();
lto_module_is_object_file_in_memory
lto_module_is_object_file_in_memory_for_target
lto_module_dispose
+lto_api_version
lto_codegen_set_diagnostic_handler
lto_codegen_add_module
lto_codegen_add_must_preserve_symbol
lto_codegen_set_assembler_path
lto_codegen_set_cpu
lto_codegen_compile_to_file
+lto_codegen_optimize
+lto_codegen_compile_optimized
LLVMCreateDisasm
LLVMCreateDisasmCPU
LLVMDisasmDispose