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