Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 1 | //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm-c/lto.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 16 | #include "LTOCodeGenerator.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 17 | #include "LTOModule.h" |
| 18 | #include "llvm-c/Core.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | |
| 20 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 21 | // Holds most recent error string. |
| 22 | // *** Not thread safe *** |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 23 | static std::string sLastErrorString; |
| 24 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 25 | /// lto_get_version - Returns a printable string. |
| 26 | extern const char* lto_get_version() { |
| 27 | return LTOCodeGenerator::getVersionString(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 30 | /// lto_get_error_message - Returns the last error string or NULL if last |
| 31 | /// operation was successful. |
| 32 | const char* lto_get_error_message() { |
| 33 | return sLastErrorString.c_str(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 36 | /// lto_module_is_object_file - Validates if a file is a loadable object file. |
| 37 | bool lto_module_is_object_file(const char* path) { |
| 38 | return LTOModule::isBitcodeFile(path); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 41 | /// lto_module_is_object_file_for_target - Validates if a file is a loadable |
| 42 | /// object file compilable for requested target. |
| 43 | bool lto_module_is_object_file_for_target(const char* path, |
| 44 | const char* target_triplet_prefix) { |
| 45 | return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 48 | /// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable |
| 49 | /// object file. |
| 50 | bool lto_module_is_object_file_in_memory(const void* mem, size_t length) { |
| 51 | return LTOModule::isBitcodeFile(mem, length); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 54 | /// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a |
| 55 | /// loadable object file compilable for the target. |
| 56 | bool |
| 57 | lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 58 | size_t length, |
| 59 | const char* target_triplet_prefix) { |
| 60 | return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 63 | /// lto_module_create - Loads an object file from disk. Returns NULL on error |
| 64 | /// (check lto_get_error_message() for details). |
| 65 | lto_module_t lto_module_create(const char* path) { |
| 66 | return LTOModule::makeLTOModule(path, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 69 | /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on |
| 70 | /// error (check lto_get_error_message() for details). |
| 71 | lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) { |
| 72 | return LTOModule::makeLTOModule(fd, path, size, sLastErrorString); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 73 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 74 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 75 | /// lto_module_create_from_fd_at_offset - Loads an object file from disk. |
| 76 | /// Returns NULL on error (check lto_get_error_message() for details). |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 77 | lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, |
| 78 | size_t file_size, |
| 79 | size_t map_size, |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 80 | off_t offset) { |
| 81 | return LTOModule::makeLTOModule(fd, path, file_size, map_size, |
| 82 | offset, sLastErrorString); |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 85 | /// lto_module_create_from_memory - Loads an object file from memory. Returns |
| 86 | /// NULL on error (check lto_get_error_message() for details). |
| 87 | lto_module_t lto_module_create_from_memory(const void* mem, size_t length) { |
| 88 | return LTOModule::makeLTOModule(mem, length, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 91 | /// lto_module_dispose - Frees all memory for a module. Upon return the |
| 92 | /// lto_module_t is no longer valid. |
| 93 | void lto_module_dispose(lto_module_t mod) { |
| 94 | delete mod; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 97 | /// lto_module_get_target_triple - Returns triplet string which the object |
| 98 | /// module was compiled under. |
| 99 | const char* lto_module_get_target_triple(lto_module_t mod) { |
| 100 | return mod->getTargetTriple(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 103 | /// lto_module_set_target_triple - Sets triple string with which the object will |
| 104 | /// be codegened. |
| 105 | void lto_module_set_target_triple(lto_module_t mod, const char *triple) { |
| 106 | return mod->setTargetTriple(triple); |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 109 | /// lto_module_get_num_symbols - Returns the number of symbols in the object |
| 110 | /// module. |
| 111 | unsigned int lto_module_get_num_symbols(lto_module_t mod) { |
| 112 | return mod->getSymbolCount(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 115 | /// lto_module_get_symbol_name - Returns the name of the ith symbol in the |
| 116 | /// object module. |
| 117 | const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) { |
| 118 | return mod->getSymbolName(index); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 121 | /// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol |
| 122 | /// in the object module. |
| 123 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
| 124 | unsigned int index) { |
| 125 | return mod->getSymbolAttributes(index); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 128 | /// lto_codegen_create - Instantiates a code generator. Returns NULL if there |
| 129 | /// is an error. |
| 130 | lto_code_gen_t lto_codegen_create(void) { |
| 131 | return new LTOCodeGenerator(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 134 | /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the |
| 135 | /// lto_code_gen_t is no longer valid. |
| 136 | void lto_codegen_dispose(lto_code_gen_t cg) { |
| 137 | delete cg; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 140 | /// lto_codegen_add_module - Add an object module to the set of modules for |
| 141 | /// which code will be generated. Returns true on error (check |
| 142 | /// lto_get_error_message() for details). |
| 143 | bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) { |
| 144 | return cg->addModule(mod, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 147 | /// lto_codegen_set_debug_model - Sets what if any format of debug info should |
| 148 | /// be generated. Returns true on error (check lto_get_error_message() for |
| 149 | /// details). |
| 150 | bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) { |
| 151 | return cg->setDebugInfo(debug, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 154 | /// lto_codegen_set_pic_model - Sets what code model to generated. Returns true |
| 155 | /// on error (check lto_get_error_message() for details). |
| 156 | bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 157 | return cg->setCodePICModel(model, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 160 | /// lto_codegen_set_cpu - Sets the cpu to generate code for. |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 161 | void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) { |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 162 | return cg->setCpu(cpu); |
| 163 | } |
| 164 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 165 | /// lto_codegen_set_assembler_path - Sets the path to the assembler tool. |
| 166 | void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) { |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 167 | // In here only for backwards compatibility. We use MC now. |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 170 | /// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should |
| 171 | /// pass to the assembler. |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 172 | void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 173 | int nargs) { |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 174 | // In here only for backwards compatibility. We use MC now. |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 177 | /// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols |
| 178 | /// that must exist in the final generated code. If a function is not listed |
| 179 | /// there, it might be inlined into every usage and optimized away. |
| 180 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 181 | const char *symbol) { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 182 | cg->addMustPreserveSymbol(symbol); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 185 | /// lto_codegen_write_merged_modules - Writes a new file at the specified path |
| 186 | /// that contains the merged contents of all modules added so far. Returns true |
| 187 | /// on error (check lto_get_error_message() for details). |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 188 | bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 189 | return cg->writeMergedModules(path, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 192 | /// lto_codegen_compile - Generates code for all added modules into one native |
| 193 | /// object file. On success returns a pointer to a generated mach-o/ELF buffer |
| 194 | /// and length set to the buffer size. The buffer is owned by the lto_code_gen_t |
| 195 | /// object and will be freed when lto_codegen_dispose() is called, or |
| 196 | /// lto_codegen_compile() is called again. On failure, returns NULL (check |
| 197 | /// lto_get_error_message() for details). |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 198 | const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 199 | return cg->compile(length, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 202 | /// lto_codegen_compile_to_file - Generates code for all added modules into one |
| 203 | /// native object file. The name of the file is written to name. Returns true on |
| 204 | /// error. |
| 205 | bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) { |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 206 | return cg->compile_to_file(name, sLastErrorString); |
| 207 | } |
| 208 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 209 | /// lto_codegen_debug_options - Used to pass extra options to the code |
| 210 | /// generator. |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 211 | void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) { |
Devang Patel | a93ae71 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 212 | cg->setCodeGenDebugOptions(opt); |
Duncan Sands | d44d4bf | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 213 | } |