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" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/CommandFlags.h" |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.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" |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 50a20c0 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Signals.h" |
Rafael Espindola | 77c50d2 | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetSelect.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 24 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 25 | // extra command-line flags needed for LTOCodeGenerator |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 26 | static cl::opt<char> |
| 27 | OptLevel("O", |
| 28 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 29 | "(default = '-O2')"), |
| 30 | cl::Prefix, |
| 31 | cl::ZeroOrMore, |
| 32 | cl::init('2')); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 33 | |
| 34 | static cl::opt<bool> |
| 35 | DisableInline("disable-inlining", cl::init(false), |
| 36 | cl::desc("Do not run the inliner pass")); |
| 37 | |
| 38 | static cl::opt<bool> |
| 39 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 40 | cl::desc("Do not run the GVN load PRE pass")); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 41 | |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | DisableLTOVectorization("disable-lto-vectorization", cl::init(false), |
| 44 | cl::desc("Do not run loop or slp vectorization during LTO")); |
| 45 | |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 46 | #ifdef NDEBUG |
| 47 | static bool VerifyByDefault = false; |
| 48 | #else |
| 49 | static bool VerifyByDefault = true; |
| 50 | #endif |
| 51 | |
| 52 | static cl::opt<bool> DisableVerify( |
| 53 | "disable-llvm-verifier", cl::init(!VerifyByDefault), |
| 54 | cl::desc("Don't run the LLVM verifier during the optimization pipeline")); |
| 55 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 56 | // Holds most recent error string. |
| 57 | // *** Not thread safe *** |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 58 | static std::string sLastErrorString; |
| 59 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 60 | // Holds the initialization state of the LTO module. |
| 61 | // *** Not thread safe *** |
| 62 | static bool initialized = false; |
| 63 | |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 64 | // Holds the command-line option parsing state of the LTO module. |
| 65 | static bool parsedOptions = false; |
| 66 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 67 | // Initialize the configured targets if they have not been initialized. |
| 68 | static void lto_initialize() { |
| 69 | if (!initialized) { |
Michael J. Spencer | 50a20c0 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 70 | #ifdef LLVM_ON_WIN32 |
| 71 | // Dialog box on crash disabling doesn't work across DLL boundaries, so do |
| 72 | // it here. |
| 73 | llvm::sys::DisableSystemDialogsOnCrash(); |
| 74 | #endif |
| 75 | |
Rafael Espindola | 77c50d2 | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 76 | InitializeAllTargetInfos(); |
| 77 | InitializeAllTargets(); |
| 78 | InitializeAllTargetMCs(); |
| 79 | InitializeAllAsmParsers(); |
| 80 | InitializeAllAsmPrinters(); |
| 81 | InitializeAllDisassemblers(); |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 82 | initialized = true; |
| 83 | } |
| 84 | } |
| 85 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 86 | namespace { |
| 87 | |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 88 | static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity, |
| 89 | const char *Msg, void *) { |
| 90 | sLastErrorString = Msg; |
| 91 | sLastErrorString += "\n"; |
| 92 | } |
| 93 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 94 | // This derived class owns the native object file. This helps implement the |
| 95 | // libLTO API semantics, which require that the code generator owns the object |
| 96 | // file. |
| 97 | struct LibLTOCodeGenerator : LTOCodeGenerator { |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame^] | 98 | LibLTOCodeGenerator() : LTOCodeGenerator(getGlobalContext()) { |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 99 | setDiagnosticHandler(handleLibLTODiagnostic, nullptr); } |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 100 | LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context) |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame^] | 101 | : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) { |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 102 | setDiagnosticHandler(handleLibLTODiagnostic, nullptr); } |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 103 | |
| 104 | std::unique_ptr<MemoryBuffer> NativeObjectFile; |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame^] | 105 | std::unique_ptr<LLVMContext> OwnedContext; |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t) |
Patrik Hagglund | 9be9d87 | 2014-05-05 12:24:08 +0000 | [diff] [blame] | 111 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t) |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 112 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 113 | // Convert the subtarget features into a string to pass to LTOCodeGenerator. |
| 114 | static void lto_add_attrs(lto_code_gen_t cg) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 115 | LTOCodeGenerator *CG = unwrap(cg); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 116 | if (MAttrs.size()) { |
| 117 | std::string attrs; |
| 118 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 119 | if (i > 0) |
| 120 | attrs.append(","); |
| 121 | attrs.append(MAttrs[i]); |
| 122 | } |
| 123 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 124 | CG->setAttr(attrs.c_str()); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 125 | } |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 126 | |
| 127 | if (OptLevel < '0' || OptLevel > '3') |
| 128 | report_fatal_error("Optimization level must be between 0 and 3"); |
| 129 | CG->setOptLevel(OptLevel - '0'); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 132 | extern const char* lto_get_version() { |
| 133 | return LTOCodeGenerator::getVersionString(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 136 | const char* lto_get_error_message() { |
| 137 | return sLastErrorString.c_str(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 140 | bool lto_module_is_object_file(const char* path) { |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 141 | return LTOModule::isBitcodeFile(path); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 144 | bool lto_module_is_object_file_for_target(const char* path, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 145 | const char* target_triplet_prefix) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 146 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path); |
| 147 | if (!Buffer) |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 148 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 149 | return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 152 | 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] | 153 | return LTOModule::isBitcodeFile(mem, length); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 156 | bool |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 157 | lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 158 | size_t length, |
| 159 | const char* target_triplet_prefix) { |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 160 | std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length)); |
| 161 | if (!buffer) |
| 162 | return false; |
| 163 | return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 166 | lto_module_t lto_module_create(const char* path) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 167 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 168 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 169 | return wrap(LTOModule::createFromFile(path, Options, sLastErrorString)); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 172 | 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] | 173 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 174 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 175 | return wrap( |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 176 | LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString)); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 177 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 178 | |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 179 | lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, |
| 180 | size_t file_size, |
| 181 | size_t map_size, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 182 | off_t offset) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 183 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 184 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 185 | return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset, |
| 186 | Options, sLastErrorString)); |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 189 | 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] | 190 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 191 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 192 | return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString)); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 195 | lto_module_t lto_module_create_from_memory_with_path(const void* mem, |
| 196 | size_t length, |
| 197 | const char *path) { |
| 198 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 199 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 200 | return wrap( |
Peter Collingbourne | 63086fe | 2014-07-03 23:28:00 +0000 | [diff] [blame] | 201 | LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path)); |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Duncan P. N. Exon Smith | c5800f6 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 204 | lto_module_t lto_module_create_in_local_context(const void *mem, size_t length, |
| 205 | const char *path) { |
| 206 | lto_initialize(); |
| 207 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
| 208 | return wrap(LTOModule::createInLocalContext(mem, length, Options, |
| 209 | sLastErrorString, path)); |
| 210 | } |
| 211 | |
| 212 | lto_module_t lto_module_create_in_codegen_context(const void *mem, |
| 213 | size_t length, |
| 214 | const char *path, |
| 215 | lto_code_gen_t cg) { |
| 216 | lto_initialize(); |
| 217 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
| 218 | return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString, |
| 219 | path, &unwrap(cg)->getContext())); |
| 220 | } |
| 221 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 222 | void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 223 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 224 | const char* lto_module_get_target_triple(lto_module_t mod) { |
Rafael Espindola | d749fb5 | 2014-07-04 14:19:41 +0000 | [diff] [blame] | 225 | return unwrap(mod)->getTargetTriple().c_str(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 228 | void lto_module_set_target_triple(lto_module_t mod, const char *triple) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 229 | return unwrap(mod)->setTargetTriple(triple); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 232 | unsigned int lto_module_get_num_symbols(lto_module_t mod) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 233 | return unwrap(mod)->getSymbolCount(); |
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 | const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 237 | return unwrap(mod)->getSymbolName(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 240 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
| 241 | unsigned int index) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 242 | return unwrap(mod)->getSymbolAttributes(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Peter Collingbourne | 5d7dffb | 2015-06-29 23:09:12 +0000 | [diff] [blame] | 245 | const char* lto_module_get_linkeropts(lto_module_t mod) { |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 246 | return unwrap(mod)->getLinkerOpts(); |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 249 | void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, |
| 250 | lto_diagnostic_handler_t diag_handler, |
| 251 | void *ctxt) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 252 | unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 255 | static lto_code_gen_t createCodeGen(bool InLocalContext) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 256 | lto_initialize(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 257 | |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 258 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 259 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 260 | LibLTOCodeGenerator *CodeGen = |
| 261 | InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>()) |
| 262 | : new LibLTOCodeGenerator(); |
| 263 | CodeGen->setTargetOptions(Options); |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 264 | return wrap(CodeGen); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 267 | lto_code_gen_t lto_codegen_create(void) { |
| 268 | return createCodeGen(/* InLocalContext */ false); |
| 269 | } |
| 270 | |
| 271 | lto_code_gen_t lto_codegen_create_in_local_context(void) { |
| 272 | return createCodeGen(/* InLocalContext */ true); |
| 273 | } |
| 274 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 275 | void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 276 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 277 | bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) { |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 278 | return !unwrap(cg)->addModule(unwrap(mod)); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 281 | void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) { |
Peter Collingbourne | 9c8909d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 282 | unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod))); |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 285 | bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 286 | unwrap(cg)->setDebugInfo(debug); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 287 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 290 | bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) { |
Peter Collingbourne | 44ee84e | 2015-08-21 22:57:17 +0000 | [diff] [blame] | 291 | switch (model) { |
| 292 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 293 | unwrap(cg)->setCodePICModel(Reloc::Static); |
| 294 | return false; |
| 295 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 296 | unwrap(cg)->setCodePICModel(Reloc::PIC_); |
| 297 | return false; |
| 298 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 299 | unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC); |
| 300 | return false; |
| 301 | case LTO_CODEGEN_PIC_MODEL_DEFAULT: |
| 302 | unwrap(cg)->setCodePICModel(Reloc::Default); |
| 303 | return false; |
| 304 | } |
| 305 | sLastErrorString = "Unknown PIC model"; |
| 306 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 309 | void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 310 | return unwrap(cg)->setCpu(cpu); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 313 | 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] | 314 | // In here only for backwards compatibility. We use MC now. |
Nick Kledzik | cac8c8a | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 317 | 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] | 318 | int nargs) { |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 319 | // In here only for backwards compatibility. We use MC now. |
Rafael Espindola | 0045646 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 322 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 323 | const char *symbol) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 324 | unwrap(cg)->addMustPreserveSymbol(symbol); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 327 | static void maybeParseOptions(lto_code_gen_t cg) { |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 328 | if (!parsedOptions) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 329 | unwrap(cg)->parseCodeGenDebugOptions(); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 330 | lto_add_attrs(cg); |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 331 | parsedOptions = true; |
| 332 | } |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 336 | maybeParseOptions(cg); |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 337 | return !unwrap(cg)->writeMergedModules(path); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 340 | const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 341 | maybeParseOptions(cg); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 342 | LibLTOCodeGenerator *CG = unwrap(cg); |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 343 | CG->NativeObjectFile = |
| 344 | CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 345 | DisableLTOVectorization); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 346 | if (!CG->NativeObjectFile) |
| 347 | return nullptr; |
| 348 | *length = CG->NativeObjectFile->getBufferSize(); |
| 349 | return CG->NativeObjectFile->getBufferStart(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 352 | bool lto_codegen_optimize(lto_code_gen_t cg) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 353 | maybeParseOptions(cg); |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 354 | return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 355 | DisableLTOVectorization); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 359 | maybeParseOptions(cg); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 360 | LibLTOCodeGenerator *CG = unwrap(cg); |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 361 | CG->NativeObjectFile = CG->compileOptimized(); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 362 | if (!CG->NativeObjectFile) |
| 363 | return nullptr; |
| 364 | *length = CG->NativeObjectFile->getBufferSize(); |
| 365 | return CG->NativeObjectFile->getBufferStart(); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 368 | bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 369 | maybeParseOptions(cg); |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 370 | return !unwrap(cg)->compile_to_file( |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 371 | name, DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 372 | DisableLTOVectorization); |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 375 | void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 376 | unwrap(cg)->setCodeGenDebugOptions(opt); |
Duncan Sands | 31554ab | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 377 | } |
Rafael Espindola | a5ef490 | 2015-02-03 19:25:53 +0000 | [diff] [blame] | 378 | |
| 379 | unsigned int lto_api_version() { return LTO_API_VERSION; } |
Manman Ren | ce0a066 | 2015-04-17 17:10:09 +0000 | [diff] [blame] | 380 | |
| 381 | void lto_codegen_set_should_internalize(lto_code_gen_t cg, |
| 382 | bool ShouldInternalize) { |
| 383 | unwrap(cg)->setShouldInternalize(ShouldInternalize); |
| 384 | } |
Duncan P. N. Exon Smith | 5a490d0 | 2015-04-27 23:38:54 +0000 | [diff] [blame] | 385 | |
| 386 | void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg, |
| 387 | lto_bool_t ShouldEmbedUselists) { |
| 388 | unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists); |
| 389 | } |