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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Link Time Optimization library. This library is |
| 11 | // intended to be used by linker to optimize code at link time. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm-c/lto.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 16 | #include "llvm-c/Core.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 17 | |
| 18 | #include "LTOModule.h" |
| 19 | #include "LTOCodeGenerator.h" |
| 20 | |
| 21 | |
| 22 | // holds most recent error string |
| 23 | // *** not thread safe *** |
| 24 | static std::string sLastErrorString; |
| 25 | |
| 26 | |
| 27 | |
| 28 | // |
| 29 | // returns a printable string |
| 30 | // |
| 31 | extern const char* lto_get_version() |
| 32 | { |
| 33 | return LTOCodeGenerator::getVersionString(); |
| 34 | } |
| 35 | |
| 36 | // |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 37 | // returns the last error string or NULL if last operation was successful |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 38 | // |
| 39 | const char* lto_get_error_message() |
| 40 | { |
| 41 | return sLastErrorString.c_str(); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | // |
| 47 | // validates if a file is a loadable object file |
| 48 | // |
| 49 | bool lto_module_is_object_file(const char* path) |
| 50 | { |
| 51 | return LTOModule::isBitcodeFile(path); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | // |
| 56 | // validates if a file is a loadable object file compilable for requested target |
| 57 | // |
| 58 | bool lto_module_is_object_file_for_target(const char* path, |
| 59 | const char* target_triplet_prefix) |
| 60 | { |
| 61 | return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // |
| 66 | // validates if a buffer is a loadable object file |
| 67 | // |
| 68 | bool lto_module_is_object_file_in_memory(const void* mem, size_t length) |
| 69 | { |
| 70 | return LTOModule::isBitcodeFile(mem, length); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | // |
| 75 | // validates if a buffer is a loadable object file compilable for the target |
| 76 | // |
| 77 | bool lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 78 | size_t length, const char* target_triplet_prefix) |
| 79 | { |
| 80 | return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | // |
| 86 | // loads an object file from disk |
| 87 | // returns NULL on error (check lto_get_error_message() for details) |
| 88 | // |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 89 | lto_module_t lto_module_create(const char* path) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 90 | { |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 91 | return LTOModule::makeLTOModule(path, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | // |
| 96 | // loads an object file from memory |
| 97 | // returns NULL on error (check lto_get_error_message() for details) |
| 98 | // |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 99 | lto_module_t lto_module_create_from_memory(const void* mem, size_t length) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 100 | { |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 101 | return LTOModule::makeLTOModule(mem, length, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | // |
| 106 | // frees all memory for a module |
| 107 | // upon return the lto_module_t is no longer valid |
| 108 | // |
| 109 | void lto_module_dispose(lto_module_t mod) |
| 110 | { |
| 111 | delete mod; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // |
| 116 | // returns triplet string which the object module was compiled under |
| 117 | // |
| 118 | const char* lto_module_get_target_triple(lto_module_t mod) |
| 119 | { |
| 120 | return mod->getTargetTriple(); |
| 121 | } |
| 122 | |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 123 | // |
| 124 | // sets triple string with which the object will be codegened. |
| 125 | // |
| 126 | void lto_module_set_target_triple(lto_module_t mod, const char *triple) |
| 127 | { |
| 128 | return mod->setTargetTriple(triple); |
| 129 | } |
| 130 | |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 131 | |
| 132 | // |
| 133 | // returns the number of symbols in the object module |
| 134 | // |
| 135 | uint32_t lto_module_get_num_symbols(lto_module_t mod) |
| 136 | { |
| 137 | return mod->getSymbolCount(); |
| 138 | } |
| 139 | |
| 140 | // |
| 141 | // returns the name of the ith symbol in the object module |
| 142 | // |
| 143 | const char* lto_module_get_symbol_name(lto_module_t mod, uint32_t index) |
| 144 | { |
| 145 | return mod->getSymbolName(index); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | // |
| 150 | // returns the attributes of the ith symbol in the object module |
| 151 | // |
| 152 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
Devang Patel | 767b5b6 | 2010-08-26 17:47:45 +0000 | [diff] [blame^] | 153 | uint32_t index) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 154 | { |
| 155 | return mod->getSymbolAttributes(index); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | // |
| 163 | // instantiates a code generator |
| 164 | // returns NULL if there is an error |
| 165 | // |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 166 | lto_code_gen_t lto_codegen_create(void) |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 167 | { |
Owen Anderson | 0e7a546 | 2009-07-02 00:31:14 +0000 | [diff] [blame] | 168 | return new LTOCodeGenerator(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | // |
| 174 | // frees all memory for a code generator |
| 175 | // upon return the lto_code_gen_t is no longer valid |
| 176 | // |
| 177 | void lto_codegen_dispose(lto_code_gen_t cg) |
| 178 | { |
| 179 | delete cg; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | // |
| 185 | // add an object module to the set of modules for which code will be generated |
| 186 | // returns true on error (check lto_get_error_message() for details) |
| 187 | // |
| 188 | bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) |
| 189 | { |
| 190 | return cg->addModule(mod, sLastErrorString); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | // |
| 195 | // sets what if any format of debug info should be generated |
| 196 | // returns true on error (check lto_get_error_message() for details) |
| 197 | // |
| 198 | bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) |
| 199 | { |
| 200 | return cg->setDebugInfo(debug, sLastErrorString); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | // |
| 205 | // sets what code model to generated |
| 206 | // returns true on error (check lto_get_error_message() for details) |
| 207 | // |
| 208 | bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) |
| 209 | { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 210 | return cg->setCodePICModel(model, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 214 | // sets the cpu to generate code for |
| 215 | // |
| 216 | void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu) |
| 217 | { |
| 218 | return cg->setCpu(cpu); |
| 219 | } |
| 220 | |
| 221 | // |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 222 | // sets the path to the assembler tool |
| 223 | // |
| 224 | void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path) |
| 225 | { |
| 226 | cg->setAssemblerPath(path); |
| 227 | } |
| 228 | |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 229 | |
| 230 | // |
| 231 | // sets extra arguments that libLTO should pass to the assembler |
| 232 | // |
| 233 | void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char** args, |
| 234 | int nargs) |
| 235 | { |
| 236 | cg->setAssemblerArgs(args, nargs); |
| 237 | } |
| 238 | |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 239 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 240 | // adds to a list of all global symbols that must exist in the final |
| 241 | // generated code. If a function is not listed there, it might be |
| 242 | // inlined into every usage and optimized away. |
| 243 | // |
| 244 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol) |
| 245 | { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 246 | cg->addMustPreserveSymbol(symbol); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | // |
| 251 | // writes a new file at the specified path that contains the |
| 252 | // merged contents of all modules added so far. |
| 253 | // returns true on error (check lto_get_error_message() for details) |
| 254 | // |
| 255 | bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path) |
| 256 | { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 257 | return cg->writeMergedModules(path, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
| 261 | // |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 262 | // Generates code for all added modules into one native object file. |
| 263 | // On sucess returns a pointer to a generated mach-o/ELF buffer and |
| 264 | // length set to the buffer size. The buffer is owned by the |
| 265 | // lto_code_gen_t and will be freed when lto_codegen_dispose() |
| 266 | // is called, or lto_codegen_compile() is called again. |
| 267 | // On failure, returns NULL (check lto_get_error_message() for details). |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 268 | // |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 269 | extern const void* |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 270 | lto_codegen_compile(lto_code_gen_t cg, size_t* length) |
| 271 | { |
Evan Cheng | 855a168 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 272 | return cg->compile(length, sLastErrorString); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Nick Kledzik | 920ae98 | 2008-07-08 21:14:10 +0000 | [diff] [blame] | 275 | |
| 276 | // |
| 277 | // Used to pass extra options to the code generator |
| 278 | // |
Devang Patel | a93ae71 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 279 | extern void |
| 280 | lto_codegen_debug_options(lto_code_gen_t cg, const char * opt) |
| 281 | { |
| 282 | cg->setCodeGenDebugOptions(opt); |
Duncan Sands | d44d4bf | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 283 | } |