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" |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DiagnosticInfo.h" |
| 19 | #include "llvm/IR/DiagnosticPrinter.h" |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 21 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 22 | #include "llvm/LTO/LTOModule.h" |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 23 | #include "llvm/LTO/ThinLTOCodeGenerator.h" |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 50a20c0 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Signals.h" |
Rafael Espindola | 77c50d2 | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 26 | #include "llvm/Support/TargetSelect.h" |
Reid Kleckner | 7c6b5e1 | 2016-01-29 00:03:34 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 28 | |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 29 | // extra command-line flags needed for LTOCodeGenerator |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 30 | static cl::opt<char> |
| 31 | OptLevel("O", |
| 32 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 33 | "(default = '-O2')"), |
| 34 | cl::Prefix, |
| 35 | cl::ZeroOrMore, |
| 36 | cl::init('2')); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 37 | |
| 38 | static cl::opt<bool> |
| 39 | DisableInline("disable-inlining", cl::init(false), |
| 40 | cl::desc("Do not run the inliner pass")); |
| 41 | |
| 42 | static cl::opt<bool> |
| 43 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 44 | cl::desc("Do not run the GVN load PRE pass")); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 45 | |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 46 | static cl::opt<bool> |
| 47 | DisableLTOVectorization("disable-lto-vectorization", cl::init(false), |
| 48 | cl::desc("Do not run loop or slp vectorization during LTO")); |
| 49 | |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 50 | #ifdef NDEBUG |
| 51 | static bool VerifyByDefault = false; |
| 52 | #else |
| 53 | static bool VerifyByDefault = true; |
| 54 | #endif |
| 55 | |
| 56 | static cl::opt<bool> DisableVerify( |
| 57 | "disable-llvm-verifier", cl::init(!VerifyByDefault), |
| 58 | cl::desc("Don't run the LLVM verifier during the optimization pipeline")); |
| 59 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 60 | // Holds most recent error string. |
| 61 | // *** Not thread safe *** |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 62 | static std::string sLastErrorString; |
| 63 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 64 | // Holds the initialization state of the LTO module. |
| 65 | // *** Not thread safe *** |
| 66 | static bool initialized = false; |
| 67 | |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 68 | // Holds the command-line option parsing state of the LTO module. |
| 69 | static bool parsedOptions = false; |
| 70 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 71 | static LLVMContext *LTOContext = nullptr; |
| 72 | |
| 73 | static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) { |
| 74 | if (DI.getSeverity() != DS_Error) { |
| 75 | DiagnosticPrinterRawOStream DP(errs()); |
| 76 | DI.print(DP); |
| 77 | errs() << '\n'; |
| 78 | return; |
| 79 | } |
| 80 | sLastErrorString = ""; |
| 81 | { |
| 82 | raw_string_ostream Stream(sLastErrorString); |
| 83 | DiagnosticPrinterRawOStream DP(Stream); |
| 84 | DI.print(DP); |
| 85 | } |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 88 | // Initialize the configured targets if they have not been initialized. |
| 89 | static void lto_initialize() { |
| 90 | if (!initialized) { |
Michael J. Spencer | 50a20c0 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 91 | #ifdef LLVM_ON_WIN32 |
| 92 | // Dialog box on crash disabling doesn't work across DLL boundaries, so do |
| 93 | // it here. |
| 94 | llvm::sys::DisableSystemDialogsOnCrash(); |
| 95 | #endif |
| 96 | |
Rafael Espindola | 77c50d2 | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 97 | InitializeAllTargetInfos(); |
| 98 | InitializeAllTargets(); |
| 99 | InitializeAllTargetMCs(); |
| 100 | InitializeAllAsmParsers(); |
| 101 | InitializeAllAsmPrinters(); |
| 102 | InitializeAllDisassemblers(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 103 | |
| 104 | LTOContext = &getGlobalContext(); |
| 105 | LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true); |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 106 | initialized = true; |
| 107 | } |
| 108 | } |
| 109 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 110 | namespace { |
| 111 | |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 112 | static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity, |
| 113 | const char *Msg, void *) { |
| 114 | sLastErrorString = Msg; |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 117 | // This derived class owns the native object file. This helps implement the |
| 118 | // libLTO API semantics, which require that the code generator owns the object |
| 119 | // file. |
| 120 | struct LibLTOCodeGenerator : LTOCodeGenerator { |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 121 | LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) { |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 122 | setDiagnosticHandler(handleLibLTODiagnostic, nullptr); } |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 123 | LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context) |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame] | 124 | : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) { |
Yunzhong Gao | ea7b3a2 | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 125 | setDiagnosticHandler(handleLibLTODiagnostic, nullptr); } |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 126 | |
Steven Wu | b5104b5 | 2015-12-09 03:37:51 +0000 | [diff] [blame] | 127 | // Reset the module first in case MergedModule is created in OwnedContext. |
| 128 | // Module must be destructed before its context gets destructed. |
| 129 | ~LibLTOCodeGenerator() { resetMergedModule(); } |
| 130 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 131 | std::unique_ptr<MemoryBuffer> NativeObjectFile; |
Rafael Espindola | 7b8a24e | 2015-12-04 02:42:28 +0000 | [diff] [blame] | 132 | std::unique_ptr<LLVMContext> OwnedContext; |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t) |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 138 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator, thinlto_code_gen_t) |
Patrik Hagglund | 9be9d87 | 2014-05-05 12:24:08 +0000 | [diff] [blame] | 139 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t) |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 140 | |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 141 | // Convert the subtarget features into a string to pass to LTOCodeGenerator. |
| 142 | static void lto_add_attrs(lto_code_gen_t cg) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 143 | LTOCodeGenerator *CG = unwrap(cg); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 144 | if (MAttrs.size()) { |
| 145 | std::string attrs; |
| 146 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 147 | if (i > 0) |
| 148 | attrs.append(","); |
| 149 | attrs.append(MAttrs[i]); |
| 150 | } |
| 151 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 152 | CG->setAttr(attrs.c_str()); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 153 | } |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 154 | |
| 155 | if (OptLevel < '0' || OptLevel > '3') |
| 156 | report_fatal_error("Optimization level must be between 0 and 3"); |
| 157 | CG->setOptLevel(OptLevel - '0'); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 160 | extern const char* lto_get_version() { |
| 161 | return LTOCodeGenerator::getVersionString(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 164 | const char* lto_get_error_message() { |
| 165 | return sLastErrorString.c_str(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 168 | bool lto_module_is_object_file(const char* path) { |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 169 | return LTOModule::isBitcodeFile(path); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 172 | bool lto_module_is_object_file_for_target(const char* path, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 173 | const char* target_triplet_prefix) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 174 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path); |
| 175 | if (!Buffer) |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 176 | return false; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 177 | return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 180 | 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] | 181 | return LTOModule::isBitcodeFile(mem, length); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 184 | bool |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 185 | lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 186 | size_t length, |
| 187 | const char* target_triplet_prefix) { |
Alp Toker | ac90380 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 188 | std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length)); |
| 189 | if (!buffer) |
| 190 | return false; |
| 191 | return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 194 | lto_module_t lto_module_create(const char* path) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 195 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 196 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 197 | ErrorOr<std::unique_ptr<LTOModule>> M = |
| 198 | LTOModule::createFromFile(*LTOContext, path, Options); |
| 199 | if (!M) |
| 200 | return nullptr; |
| 201 | return wrap(M->release()); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 204 | 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] | 205 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 206 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 207 | ErrorOr<std::unique_ptr<LTOModule>> M = |
| 208 | LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options); |
| 209 | if (!M) |
| 210 | return nullptr; |
| 211 | return wrap(M->release()); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 212 | } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 214 | lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, |
| 215 | size_t file_size, |
| 216 | size_t map_size, |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 217 | off_t offset) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 218 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 219 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 220 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice( |
| 221 | *LTOContext, fd, path, map_size, offset, Options); |
| 222 | if (!M) |
| 223 | return nullptr; |
| 224 | return wrap(M->release()); |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 227 | 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] | 228 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 229 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 230 | ErrorOr<std::unique_ptr<LTOModule>> M = |
| 231 | LTOModule::createFromBuffer(*LTOContext, mem, length, Options); |
| 232 | if (!M) |
| 233 | return nullptr; |
| 234 | return wrap(M->release()); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 237 | lto_module_t lto_module_create_from_memory_with_path(const void* mem, |
| 238 | size_t length, |
| 239 | const char *path) { |
| 240 | lto_initialize(); |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 241 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 242 | ErrorOr<std::unique_ptr<LTOModule>> M = |
| 243 | LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path); |
| 244 | if (!M) |
| 245 | return nullptr; |
| 246 | return wrap(M->release()); |
Manman Ren | 03456a1 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Duncan P. N. Exon Smith | c5800f6 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 249 | lto_module_t lto_module_create_in_local_context(const void *mem, size_t length, |
| 250 | const char *path) { |
| 251 | lto_initialize(); |
| 252 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 253 | |
| 254 | // Create a local context. Ownership will be transfered to LTOModule. |
| 255 | std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>(); |
| 256 | Context->setDiagnosticHandler(diagnosticHandler, nullptr, true); |
| 257 | |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 258 | ErrorOr<std::unique_ptr<LTOModule>> M = |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 259 | LTOModule::createInLocalContext(std::move(Context), mem, length, Options, |
| 260 | path); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 261 | if (!M) |
| 262 | return nullptr; |
| 263 | return wrap(M->release()); |
Duncan P. N. Exon Smith | c5800f6 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | lto_module_t lto_module_create_in_codegen_context(const void *mem, |
| 267 | size_t length, |
| 268 | const char *path, |
| 269 | lto_code_gen_t cg) { |
| 270 | lto_initialize(); |
| 271 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Petr Pavlu | 7ad9ec9 | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 272 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer( |
| 273 | unwrap(cg)->getContext(), mem, length, Options, path); |
Rafael Espindola | a7612b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 274 | return wrap(M->release()); |
Duncan P. N. Exon Smith | c5800f6 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 277 | void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 278 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 279 | const char* lto_module_get_target_triple(lto_module_t mod) { |
Rafael Espindola | d749fb5 | 2014-07-04 14:19:41 +0000 | [diff] [blame] | 280 | return unwrap(mod)->getTargetTriple().c_str(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 283 | 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] | 284 | return unwrap(mod)->setTargetTriple(triple); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 287 | unsigned int lto_module_get_num_symbols(lto_module_t mod) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 288 | return unwrap(mod)->getSymbolCount(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 291 | 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] | 292 | return unwrap(mod)->getSymbolName(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 295 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
| 296 | unsigned int index) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 297 | return unwrap(mod)->getSymbolAttributes(index); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Peter Collingbourne | 5d7dffb | 2015-06-29 23:09:12 +0000 | [diff] [blame] | 300 | const char* lto_module_get_linkeropts(lto_module_t mod) { |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 301 | return unwrap(mod)->getLinkerOpts(); |
Yunzhong Gao | a88d7ab | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 304 | void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, |
| 305 | lto_diagnostic_handler_t diag_handler, |
| 306 | void *ctxt) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 307 | unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt); |
Quentin Colombet | 5fa1f6f | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 310 | static lto_code_gen_t createCodeGen(bool InLocalContext) { |
Peter Collingbourne | 4ccf0f1 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 311 | lto_initialize(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 312 | |
Eli Bendersky | f0f2100 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 313 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 0b385c7 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 314 | |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 315 | LibLTOCodeGenerator *CodeGen = |
| 316 | InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>()) |
| 317 | : new LibLTOCodeGenerator(); |
| 318 | CodeGen->setTargetOptions(Options); |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 319 | return wrap(CodeGen); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Duncan P. N. Exon Smith | d34b613 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 322 | lto_code_gen_t lto_codegen_create(void) { |
| 323 | return createCodeGen(/* InLocalContext */ false); |
| 324 | } |
| 325 | |
| 326 | lto_code_gen_t lto_codegen_create_in_local_context(void) { |
| 327 | return createCodeGen(/* InLocalContext */ true); |
| 328 | } |
| 329 | |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 330 | void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); } |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 331 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 332 | 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] | 333 | return !unwrap(cg)->addModule(unwrap(mod)); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 336 | 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] | 337 | unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod))); |
Manman Ren | 6487ce9 | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 340 | 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] | 341 | unwrap(cg)->setDebugInfo(debug); |
Shuxin Yang | b6696a9 | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 342 | return false; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 345 | 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] | 346 | switch (model) { |
| 347 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 348 | unwrap(cg)->setCodePICModel(Reloc::Static); |
| 349 | return false; |
| 350 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 351 | unwrap(cg)->setCodePICModel(Reloc::PIC_); |
| 352 | return false; |
| 353 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 354 | unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC); |
| 355 | return false; |
| 356 | case LTO_CODEGEN_PIC_MODEL_DEFAULT: |
| 357 | unwrap(cg)->setCodePICModel(Reloc::Default); |
| 358 | return false; |
| 359 | } |
| 360 | sLastErrorString = "Unknown PIC model"; |
| 361 | return true; |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 364 | 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] | 365 | return unwrap(cg)->setCpu(cpu); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 368 | 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] | 369 | // In here only for backwards compatibility. We use MC now. |
Nick Kledzik | cac8c8a | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 372 | 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] | 373 | int nargs) { |
Rafael Espindola | fac373c | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 374 | // In here only for backwards compatibility. We use MC now. |
Rafael Espindola | 0045646 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Bill Wendling | 36cbf03 | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 377 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 378 | const char *symbol) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 379 | unwrap(cg)->addMustPreserveSymbol(symbol); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 382 | static void maybeParseOptions(lto_code_gen_t cg) { |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 383 | if (!parsedOptions) { |
Rafael Espindola | 83ceb8e | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 384 | unwrap(cg)->parseCodeGenDebugOptions(); |
Tom Roeder | fd1bc60 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 385 | lto_add_attrs(cg); |
Rafael Espindola | efa02d5 | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 386 | parsedOptions = true; |
| 387 | } |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | 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] | 391 | maybeParseOptions(cg); |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 392 | return !unwrap(cg)->writeMergedModules(path); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 395 | 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] | 396 | maybeParseOptions(cg); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 397 | LibLTOCodeGenerator *CG = unwrap(cg); |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 398 | CG->NativeObjectFile = |
| 399 | CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 400 | DisableLTOVectorization); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 401 | if (!CG->NativeObjectFile) |
| 402 | return nullptr; |
| 403 | *length = CG->NativeObjectFile->getBufferSize(); |
| 404 | return CG->NativeObjectFile->getBufferStart(); |
Nick Kledzik | 07b4a62 | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 407 | bool lto_codegen_optimize(lto_code_gen_t cg) { |
Peter Collingbourne | 78240e0 | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 408 | maybeParseOptions(cg); |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 409 | return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 410 | DisableLTOVectorization); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | 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] | 414 | maybeParseOptions(cg); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 415 | LibLTOCodeGenerator *CG = unwrap(cg); |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 416 | CG->NativeObjectFile = CG->compileOptimized(); |
Peter Collingbourne | 3cc69d90 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 417 | if (!CG->NativeObjectFile) |
| 418 | return nullptr; |
| 419 | *length = CG->NativeObjectFile->getBufferSize(); |
| 420 | return CG->NativeObjectFile->getBufferStart(); |
Manman Ren | 8121e1d | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Reid Kleckner | ddac151 | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 423 | 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] | 424 | maybeParseOptions(cg); |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 425 | return !unwrap(cg)->compile_to_file( |
Duncan P. N. Exon Smith | cff5fef | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 426 | name, DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 8e348cc | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 427 | DisableLTOVectorization); |
Rafael Espindola | 26b57ff | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Bill Wendling | 152e473 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 430 | 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] | 431 | unwrap(cg)->setCodeGenDebugOptions(opt); |
Duncan Sands | 31554ab | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 432 | } |
Rafael Espindola | a5ef490 | 2015-02-03 19:25:53 +0000 | [diff] [blame] | 433 | |
| 434 | unsigned int lto_api_version() { return LTO_API_VERSION; } |
Manman Ren | ce0a066 | 2015-04-17 17:10:09 +0000 | [diff] [blame] | 435 | |
| 436 | void lto_codegen_set_should_internalize(lto_code_gen_t cg, |
| 437 | bool ShouldInternalize) { |
| 438 | unwrap(cg)->setShouldInternalize(ShouldInternalize); |
| 439 | } |
Duncan P. N. Exon Smith | 5a490d0 | 2015-04-27 23:38:54 +0000 | [diff] [blame] | 440 | |
| 441 | void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg, |
| 442 | lto_bool_t ShouldEmbedUselists) { |
| 443 | unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists); |
| 444 | } |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 445 | |
| 446 | // ThinLTO API below |
| 447 | |
Mehdi Amini | 3ed41d6 | 2016-03-09 02:36:09 +0000 | [diff] [blame] | 448 | thinlto_code_gen_t thinlto_create_codegen(void) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 449 | lto_initialize(); |
| 450 | ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator(); |
| 451 | CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags()); |
| 452 | |
| 453 | return wrap(CodeGen); |
| 454 | } |
| 455 | |
| 456 | void thinlto_codegen_dispose(thinlto_code_gen_t cg) { delete unwrap(cg); } |
| 457 | |
| 458 | void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier, |
| 459 | const char *Data, int Length) { |
| 460 | unwrap(cg)->addModule(Identifier, StringRef(Data, Length)); |
| 461 | } |
| 462 | |
| 463 | void thinlto_codegen_process(thinlto_code_gen_t cg) { unwrap(cg)->run(); } |
| 464 | |
| 465 | unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) { |
| 466 | return unwrap(cg)->getProducedBinaries().size(); |
| 467 | } |
| 468 | LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg, |
| 469 | unsigned int index) { |
| 470 | assert(index < unwrap(cg)->getProducedBinaries().size() && "Index overflow"); |
| 471 | auto &MemBuffer = unwrap(cg)->getProducedBinaries()[index]; |
Mehdi Amini | c286b9f | 2016-03-19 21:28:18 +0000 | [diff] [blame] | 472 | return LTOObjectBuffer{MemBuffer->getBufferStart(), |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 473 | MemBuffer->getBufferSize()}; |
| 474 | } |
| 475 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 476 | void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg, |
| 477 | lto_bool_t disable) { |
| 478 | unwrap(cg)->disableCodeGen(disable); |
| 479 | } |
| 480 | |
| 481 | void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg, |
| 482 | lto_bool_t CodeGenOnly) { |
| 483 | unwrap(cg)->setCodeGenOnly(CodeGenOnly); |
| 484 | } |
| 485 | |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 486 | void thinlto_debug_options(const char *const *options, int number) { |
| 487 | // if options were requested, set them |
| 488 | if (number && options) { |
| 489 | std::vector<const char *> CodegenArgv(1, "libLTO"); |
| 490 | for (auto Arg : ArrayRef<const char *>(options, number)) |
| 491 | CodegenArgv.push_back(Arg); |
| 492 | cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data()); |
| 493 | } |
| 494 | } |
| 495 | |
Mehdi Amini | 43b657b | 2016-04-01 06:47:02 +0000 | [diff] [blame^] | 496 | lto_bool_t lto_module_is_thinlto(lto_module_t mod) { |
Mehdi Amini | 7c4a1a8 | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 497 | return unwrap(mod)->isThinLTO(); |
| 498 | } |
| 499 | |
| 500 | void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg, |
| 501 | const char *Name, int Length) { |
| 502 | unwrap(cg)->preserveSymbol(StringRef(Name, Length)); |
| 503 | } |
| 504 | |
| 505 | void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg, |
| 506 | const char *Name, int Length) { |
| 507 | unwrap(cg)->crossReferenceSymbol(StringRef(Name, Length)); |
| 508 | } |
| 509 | |
| 510 | void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) { |
| 511 | return unwrap(cg)->setCpu(cpu); |
| 512 | } |
| 513 | |
| 514 | void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg, |
| 515 | const char *cache_dir) { |
| 516 | return unwrap(cg)->setCacheDir(cache_dir); |
| 517 | } |
| 518 | |
| 519 | void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg, |
| 520 | int interval) { |
| 521 | return unwrap(cg)->setCachePruningInterval(interval); |
| 522 | } |
| 523 | |
| 524 | void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg, |
| 525 | unsigned expiration) { |
| 526 | return unwrap(cg)->setCacheEntryExpiration(expiration); |
| 527 | } |
| 528 | |
| 529 | void thinlto_codegen_set_final_cache_size_relative_to_available_space( |
| 530 | thinlto_code_gen_t cg, unsigned Percentage) { |
| 531 | return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage); |
| 532 | } |
| 533 | |
| 534 | void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg, |
| 535 | const char *save_temps_dir) { |
| 536 | return unwrap(cg)->setSaveTempsDir(save_temps_dir); |
| 537 | } |
| 538 | |
| 539 | lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg, |
| 540 | lto_codegen_model model) { |
| 541 | switch (model) { |
| 542 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 543 | unwrap(cg)->setCodePICModel(Reloc::Static); |
| 544 | return false; |
| 545 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 546 | unwrap(cg)->setCodePICModel(Reloc::PIC_); |
| 547 | return false; |
| 548 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 549 | unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC); |
| 550 | return false; |
| 551 | case LTO_CODEGEN_PIC_MODEL_DEFAULT: |
| 552 | unwrap(cg)->setCodePICModel(Reloc::Default); |
| 553 | return false; |
| 554 | } |
| 555 | sLastErrorString = "Unknown PIC model"; |
| 556 | return true; |
| 557 | } |