Nick Kledzik | 07b4a62 | 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 | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 07b4a62 | 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" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 16 | #include "llvm-c/Core.h" |
| 17 | #include "llvm-c/Target.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 19 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 20 | #include "llvm/LTO/LTOModule.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 21 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 22 | // extra command-line flags needed for LTOCodeGenerator |
| 23 | static cl::opt<bool> |
| 24 | DisableOpt("disable-opt", cl::init(false), |
| 25 | cl::desc("Do not run any optimization passes")); |
| 26 | |
| 27 | static cl::opt<bool> |
| 28 | DisableInline("disable-inlining", cl::init(false), |
| 29 | cl::desc("Do not run the inliner pass")); |
| 30 | |
| 31 | static cl::opt<bool> |
| 32 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 33 | cl::desc("Do not run the GVN load PRE pass")); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 34 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 35 | // Holds most recent error string. |
| 36 | // *** Not thread safe *** |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 37 | static std::string sLastErrorString; |
| 38 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 39 | // Holds the initialization state of the LTO module. |
| 40 | // *** Not thread safe *** |
| 41 | static bool initialized = false; |
| 42 | |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 43 | // Holds the command-line option parsing state of the LTO module. |
| 44 | static bool parsedOptions = false; |
| 45 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 46 | // Initialize the configured targets if they have not been initialized. |
| 47 | static void lto_initialize() { |
| 48 | if (!initialized) { |
| 49 | LLVMInitializeAllTargetInfos(); |
| 50 | LLVMInitializeAllTargets(); |
| 51 | LLVMInitializeAllTargetMCs(); |
| 52 | LLVMInitializeAllAsmParsers(); |
| 53 | LLVMInitializeAllAsmPrinters(); |
| 54 | LLVMInitializeAllDisassemblers(); |
| 55 | initialized = true; |
| 56 | } |
| 57 | } |
| 58 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 59 | // Convert the subtarget features into a string to pass to LTOCodeGenerator. |
| 60 | static void lto_add_attrs(lto_code_gen_t cg) { |
| 61 | if (MAttrs.size()) { |
| 62 | std::string attrs; |
| 63 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 64 | if (i > 0) |
| 65 | attrs.append(","); |
| 66 | attrs.append(MAttrs[i]); |
| 67 | } |
| 68 | |
| 69 | cg->setAttr(attrs.c_str()); |
| 70 | } |
| 71 | } |
| 72 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 73 | /// lto_get_version - Returns a printable string. |
| 74 | extern const char* lto_get_version() { |
| 75 | return LTOCodeGenerator::getVersionString(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 78 | /// lto_get_error_message - Returns the last error string or NULL if last |
| 79 | /// operation was successful. |
| 80 | const char* lto_get_error_message() { |
| 81 | return sLastErrorString.c_str(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 84 | /// lto_module_is_object_file - Validates if a file is a loadable object file. |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 85 | bool lto_module_is_object_file(const char* path) { |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 86 | return LTOModule::isBitcodeFile(path); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 89 | /// lto_module_is_object_file_for_target - Validates if a file is a loadable |
| 90 | /// object file compilable for requested target. |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 91 | bool lto_module_is_object_file_for_target(const char* path, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 92 | const char* target_triplet_prefix) { |
| 93 | return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 96 | /// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable |
| 97 | /// object file. |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 98 | bool lto_module_is_object_file_in_memory(const void* mem, size_t length) { |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 99 | return LTOModule::isBitcodeFile(mem, length); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 102 | /// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a |
| 103 | /// loadable object file compilable for the target. |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 104 | bool |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 105 | lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 106 | size_t length, |
| 107 | const char* target_triplet_prefix) { |
| 108 | return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 111 | /// lto_module_create - Loads an object file from disk. Returns NULL on error |
| 112 | /// (check lto_get_error_message() for details). |
| 113 | lto_module_t lto_module_create(const char* path) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 114 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 115 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 116 | return LTOModule::makeLTOModule(path, Options, sLastErrorString); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 119 | /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on |
| 120 | /// error (check lto_get_error_message() for details). |
| 121 | lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 122 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 123 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 124 | return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 125 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 126 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 127 | /// lto_module_create_from_fd_at_offset - Loads an object file from disk. |
| 128 | /// Returns NULL on error (check lto_get_error_message() for details). |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 129 | lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, |
| 130 | size_t file_size, |
| 131 | size_t map_size, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 132 | off_t offset) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 133 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 134 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 135 | return LTOModule::makeLTOModule(fd, path, map_size, offset, Options, |
| 136 | sLastErrorString); |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 139 | /// lto_module_create_from_memory - Loads an object file from memory. Returns |
| 140 | /// NULL on error (check lto_get_error_message() for details). |
| 141 | lto_module_t lto_module_create_from_memory(const void* mem, size_t length) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 142 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 143 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 144 | return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 147 | /// Loads an object file from memory with an extra path argument. |
| 148 | /// Returns NULL on error (check lto_get_error_message() for details). |
| 149 | lto_module_t lto_module_create_from_memory_with_path(const void* mem, |
| 150 | size_t length, |
| 151 | const char *path) { |
| 152 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 153 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 154 | return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path); |
| 155 | } |
| 156 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 157 | /// lto_module_dispose - Frees all memory for a module. Upon return the |
| 158 | /// lto_module_t is no longer valid. |
| 159 | void lto_module_dispose(lto_module_t mod) { |
| 160 | delete mod; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 163 | /// lto_module_get_target_triple - Returns triplet string which the object |
| 164 | /// module was compiled under. |
| 165 | const char* lto_module_get_target_triple(lto_module_t mod) { |
| 166 | return mod->getTargetTriple(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 169 | /// lto_module_set_target_triple - Sets triple string with which the object will |
| 170 | /// be codegened. |
| 171 | void lto_module_set_target_triple(lto_module_t mod, const char *triple) { |
| 172 | return mod->setTargetTriple(triple); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 175 | /// lto_module_get_num_symbols - Returns the number of symbols in the object |
| 176 | /// module. |
| 177 | unsigned int lto_module_get_num_symbols(lto_module_t mod) { |
| 178 | return mod->getSymbolCount(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 181 | /// lto_module_get_symbol_name - Returns the name of the ith symbol in the |
| 182 | /// object module. |
| 183 | const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) { |
| 184 | return mod->getSymbolName(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 187 | /// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol |
| 188 | /// in the object module. |
| 189 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
| 190 | unsigned int index) { |
| 191 | return mod->getSymbolAttributes(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 194 | /// lto_module_get_num_deplibs - Returns the number of dependent libraries in |
| 195 | /// the object module. |
| 196 | unsigned int lto_module_get_num_deplibs(lto_module_t mod) { |
| 197 | return mod->getDependentLibraryCount(); |
| 198 | } |
| 199 | |
| 200 | /// lto_module_get_deplib - Returns the ith dependent library in the module. |
| 201 | const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) { |
| 202 | return mod->getDependentLibrary(index); |
| 203 | } |
| 204 | |
| 205 | /// lto_module_get_num_linkeropts - Returns the number of linker options in the |
| 206 | /// object module. |
| 207 | unsigned int lto_module_get_num_linkeropts(lto_module_t mod) { |
| 208 | return mod->getLinkerOptCount(); |
| 209 | } |
| 210 | |
| 211 | /// lto_module_get_linkeropt - Returns the ith linker option in the module. |
| 212 | const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) { |
| 213 | return mod->getLinkerOpt(index); |
| 214 | } |
| 215 | |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 216 | /// Set a diagnostic handler. |
| 217 | void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, |
| 218 | lto_diagnostic_handler_t diag_handler, |
| 219 | void *ctxt) { |
| 220 | cg->setDiagnosticHandler(diag_handler, ctxt); |
| 221 | } |
| 222 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 223 | /// lto_codegen_create - Instantiates a code generator. Returns NULL if there |
| 224 | /// is an error. |
| 225 | lto_code_gen_t lto_codegen_create(void) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 226 | lto_initialize(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 227 | |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 228 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 229 | |
| 230 | LTOCodeGenerator *CodeGen = new LTOCodeGenerator(); |
| 231 | if (CodeGen) |
| 232 | CodeGen->setTargetOptions(Options); |
| 233 | return CodeGen; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 236 | /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the |
| 237 | /// lto_code_gen_t is no longer valid. |
| 238 | void lto_codegen_dispose(lto_code_gen_t cg) { |
| 239 | delete cg; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 242 | /// lto_codegen_add_module - Add an object module to the set of modules for |
| 243 | /// which code will be generated. Returns true on error (check |
| 244 | /// lto_get_error_message() for details). |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 245 | bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) { |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 246 | return !cg->addModule(mod, sLastErrorString); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 249 | /// lto_codegen_set_debug_model - Sets what if any format of debug info should |
| 250 | /// be generated. Returns true on error (check lto_get_error_message() for |
| 251 | /// details). |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 252 | bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) { |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 253 | cg->setDebugInfo(debug); |
| 254 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 257 | /// lto_codegen_set_pic_model - Sets what code model to generated. Returns true |
| 258 | /// on error (check lto_get_error_message() for details). |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 259 | bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) { |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 260 | cg->setCodePICModel(model); |
| 261 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 264 | /// lto_codegen_set_cpu - Sets the cpu to generate code for. |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 265 | void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) { |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 266 | return cg->setCpu(cpu); |
| 267 | } |
| 268 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 269 | /// lto_codegen_set_attr - Sets the attr to generate code for. |
| 270 | void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) { |
| 271 | return cg->setAttr(attr); |
| 272 | } |
| 273 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 274 | /// lto_codegen_set_assembler_path - Sets the path to the assembler tool. |
| 275 | void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) { |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 276 | // In here only for backwards compatibility. We use MC now. |
Nick Kledzik | cac8c8a | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 279 | /// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should |
| 280 | /// pass to the assembler. |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 281 | void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 282 | int nargs) { |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 283 | // In here only for backwards compatibility. We use MC now. |
Rafael Espindola | 0045646 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 286 | /// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols |
| 287 | /// that must exist in the final generated code. If a function is not listed |
| 288 | /// there, it might be inlined into every usage and optimized away. |
| 289 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 290 | const char *symbol) { |
Evan Cheng | d646096 | 2009-06-26 06:57:16 +0000 | [diff] [blame] | 291 | cg->addMustPreserveSymbol(symbol); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 294 | /// lto_codegen_write_merged_modules - Writes a new file at the specified path |
| 295 | /// that contains the merged contents of all modules added so far. Returns true |
| 296 | /// on error (check lto_get_error_message() for details). |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 297 | bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) { |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 298 | if (!parsedOptions) { |
| 299 | cg->parseCodeGenDebugOptions(); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 300 | lto_add_attrs(cg); |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 301 | parsedOptions = true; |
| 302 | } |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 303 | return !cg->writeMergedModules(path, sLastErrorString); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 306 | /// lto_codegen_compile - Generates code for all added modules into one native |
| 307 | /// object file. On success returns a pointer to a generated mach-o/ELF buffer |
| 308 | /// and length set to the buffer size. The buffer is owned by the lto_code_gen_t |
| 309 | /// object and will be freed when lto_codegen_dispose() is called, or |
| 310 | /// lto_codegen_compile() is called again. On failure, returns NULL (check |
| 311 | /// lto_get_error_message() for details). |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 312 | const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) { |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 313 | if (!parsedOptions) { |
| 314 | cg->parseCodeGenDebugOptions(); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 315 | lto_add_attrs(cg); |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 316 | parsedOptions = true; |
| 317 | } |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 318 | return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE, |
| 319 | sLastErrorString); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 322 | /// lto_codegen_compile_to_file - Generates code for all added modules into one |
| 323 | /// native object file. The name of the file is written to name. Returns true on |
| 324 | /// error. |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 325 | bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) { |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 326 | if (!parsedOptions) { |
| 327 | cg->parseCodeGenDebugOptions(); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 328 | lto_add_attrs(cg); |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 329 | parsedOptions = true; |
| 330 | } |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 331 | return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE, |
| 332 | sLastErrorString); |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 335 | /// lto_codegen_debug_options - Used to pass extra options to the code |
| 336 | /// generator. |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 337 | void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) { |
Devang Patel | a0e4fb8 | 2008-07-03 22:53:14 +0000 | [diff] [blame] | 338 | cg->setCodeGenDebugOptions(opt); |
Duncan Sands | 31554ab | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 339 | } |