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. |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 7 | // |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is |
Nick Kledzik | 77595fc | 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 | d59c5f9 | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Teresa Johnson | a547919 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 17 | #include "llvm/Bitcode/BitcodeReader.h" |
David Blaikie | 461bf52 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.inc" |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DiagnosticInfo.h" |
| 20 | #include "llvm/IR/DiagnosticPrinter.h" |
Duncan P. N. Exon Smith | acc1d12 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LLVMContext.h" |
Peter Collingbourne | ef2acb5 | 2016-07-14 21:21:16 +0000 | [diff] [blame] | 22 | #include "llvm/LTO/legacy/LTOCodeGenerator.h" |
| 23 | #include "llvm/LTO/legacy/LTOModule.h" |
| 24 | #include "llvm/LTO/legacy/ThinLTOCodeGenerator.h" |
Alp Toker | 2255dc7 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | ab9c161 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Signals.h" |
Rafael Espindola | 01ccb9c | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TargetSelect.h" |
Reid Kleckner | cb176fd | 2016-01-29 00:03:34 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 29 | |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 30 | // extra command-line flags needed for LTOCodeGenerator |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 31 | static cl::opt<char> |
| 32 | OptLevel("O", |
| 33 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 34 | "(default = '-O2')"), |
| 35 | cl::Prefix, |
| 36 | cl::ZeroOrMore, |
| 37 | cl::init('2')); |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 38 | |
| 39 | static cl::opt<bool> |
| 40 | DisableInline("disable-inlining", cl::init(false), |
| 41 | cl::desc("Do not run the inliner pass")); |
| 42 | |
| 43 | static cl::opt<bool> |
| 44 | DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false), |
| 45 | cl::desc("Do not run the GVN load PRE pass")); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 46 | |
Mehdi Amini | e4c5b69 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 47 | static cl::opt<bool> DisableLTOVectorization( |
| 48 | "disable-lto-vectorization", cl::init(false), |
| 49 | cl::desc("Do not run loop or slp vectorization during LTO")); |
| 50 | |
| 51 | static cl::opt<bool> EnableFreestanding( |
| 52 | "lto-freestanding", cl::init(false), |
| 53 | cl::desc("Enable Freestanding (disable builtins / TLI) during LTO")); |
Arnold Schwaighofer | b26fb77 | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 54 | |
Duncan P. N. Exon Smith | 5e60a68 | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 55 | #ifdef NDEBUG |
| 56 | static bool VerifyByDefault = false; |
| 57 | #else |
| 58 | static bool VerifyByDefault = true; |
| 59 | #endif |
| 60 | |
| 61 | static cl::opt<bool> DisableVerify( |
| 62 | "disable-llvm-verifier", cl::init(!VerifyByDefault), |
| 63 | cl::desc("Don't run the LLVM verifier during the optimization pipeline")); |
| 64 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 65 | // Holds most recent error string. |
| 66 | // *** Not thread safe *** |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 67 | static std::string sLastErrorString; |
| 68 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 69 | // Holds the initialization state of the LTO module. |
| 70 | // *** Not thread safe *** |
| 71 | static bool initialized = false; |
| 72 | |
Rafael Espindola | 0e95b3a | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 73 | // Holds the command-line option parsing state of the LTO module. |
| 74 | static bool parsedOptions = false; |
| 75 | |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 76 | static LLVMContext *LTOContext = nullptr; |
| 77 | |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 78 | struct LTOToolDiagnosticHandler : public DiagnosticHandler { |
| 79 | bool handleDiagnostics(const DiagnosticInfo &DI) override { |
| 80 | if (DI.getSeverity() != DS_Error) { |
| 81 | DiagnosticPrinterRawOStream DP(errs()); |
| 82 | DI.print(DP); |
| 83 | errs() << '\n'; |
| 84 | return true; |
| 85 | } |
| 86 | sLastErrorString = ""; |
| 87 | { |
| 88 | raw_string_ostream Stream(sLastErrorString); |
| 89 | DiagnosticPrinterRawOStream DP(Stream); |
| 90 | DI.print(DP); |
| 91 | } |
| 92 | return true; |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 93 | } |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 94 | }; |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 95 | |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 96 | // Initialize the configured targets if they have not been initialized. |
| 97 | static void lto_initialize() { |
| 98 | if (!initialized) { |
Nico Weber | 63033d3 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 99 | #ifdef _WIN32 |
Michael J. Spencer | ab9c161 | 2015-01-29 17:20:41 +0000 | [diff] [blame] | 100 | // Dialog box on crash disabling doesn't work across DLL boundaries, so do |
| 101 | // it here. |
| 102 | llvm::sys::DisableSystemDialogsOnCrash(); |
| 103 | #endif |
| 104 | |
Rafael Espindola | 01ccb9c | 2014-06-19 19:11:22 +0000 | [diff] [blame] | 105 | InitializeAllTargetInfos(); |
| 106 | InitializeAllTargets(); |
| 107 | InitializeAllTargetMCs(); |
| 108 | InitializeAllAsmParsers(); |
| 109 | InitializeAllAsmPrinters(); |
| 110 | InitializeAllDisassemblers(); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 111 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 112 | static LLVMContext Context; |
| 113 | LTOContext = &Context; |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 114 | LTOContext->setDiagnosticHandler( |
| 115 | llvm::make_unique<LTOToolDiagnosticHandler>(), true); |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 116 | initialized = true; |
| 117 | } |
| 118 | } |
| 119 | |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 120 | namespace { |
| 121 | |
Yunzhong Gao | 8de34ad | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 122 | static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity, |
| 123 | const char *Msg, void *) { |
| 124 | sLastErrorString = Msg; |
Yunzhong Gao | 8de34ad | 2015-11-11 19:59:08 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 127 | // This derived class owns the native object file. This helps implement the |
| 128 | // libLTO API semantics, which require that the code generator owns the object |
| 129 | // file. |
| 130 | struct LibLTOCodeGenerator : LTOCodeGenerator { |
Duncan P. N. Exon Smith | 9c9a101 | 2016-04-16 22:25:36 +0000 | [diff] [blame] | 131 | LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) { init(); } |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 132 | LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context) |
Rafael Espindola | c6a202e | 2015-12-04 02:42:28 +0000 | [diff] [blame] | 133 | : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) { |
Duncan P. N. Exon Smith | 9c9a101 | 2016-04-16 22:25:36 +0000 | [diff] [blame] | 134 | init(); |
| 135 | } |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 136 | |
Steven Wu | 1208ef5 | 2015-12-09 03:37:51 +0000 | [diff] [blame] | 137 | // Reset the module first in case MergedModule is created in OwnedContext. |
| 138 | // Module must be destructed before its context gets destructed. |
| 139 | ~LibLTOCodeGenerator() { resetMergedModule(); } |
| 140 | |
Duncan P. N. Exon Smith | 9c9a101 | 2016-04-16 22:25:36 +0000 | [diff] [blame] | 141 | void init() { setDiagnosticHandler(handleLibLTODiagnostic, nullptr); } |
| 142 | |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 143 | std::unique_ptr<MemoryBuffer> NativeObjectFile; |
Rafael Espindola | c6a202e | 2015-12-04 02:42:28 +0000 | [diff] [blame] | 144 | std::unique_ptr<LLVMContext> OwnedContext; |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t) |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 150 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator, thinlto_code_gen_t) |
Patrik Hagglund | 862cd00 | 2014-05-05 12:24:08 +0000 | [diff] [blame] | 151 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t) |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 152 | |
Tom Roeder | 817f5e2 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 153 | // Convert the subtarget features into a string to pass to LTOCodeGenerator. |
| 154 | static void lto_add_attrs(lto_code_gen_t cg) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 155 | LTOCodeGenerator *CG = unwrap(cg); |
Tom Roeder | 817f5e2 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 156 | if (MAttrs.size()) { |
| 157 | std::string attrs; |
| 158 | for (unsigned i = 0; i < MAttrs.size(); ++i) { |
| 159 | if (i > 0) |
| 160 | attrs.append(","); |
| 161 | attrs.append(MAttrs[i]); |
| 162 | } |
| 163 | |
Malcolm Parsons | 4c12732 | 2016-11-02 16:43:50 +0000 | [diff] [blame] | 164 | CG->setAttr(attrs); |
Tom Roeder | 817f5e2 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 165 | } |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 166 | |
| 167 | if (OptLevel < '0' || OptLevel > '3') |
| 168 | report_fatal_error("Optimization level must be between 0 and 3"); |
| 169 | CG->setOptLevel(OptLevel - '0'); |
Mehdi Amini | e4c5b69 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 170 | CG->setFreestanding(EnableFreestanding); |
Tom Roeder | 817f5e2 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 173 | extern const char* lto_get_version() { |
| 174 | return LTOCodeGenerator::getVersionString(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 177 | const char* lto_get_error_message() { |
| 178 | return sLastErrorString.c_str(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 181 | bool lto_module_is_object_file(const char* path) { |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 182 | return LTOModule::isBitcodeFile(StringRef(path)); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 185 | bool lto_module_is_object_file_for_target(const char* path, |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 186 | const char* target_triplet_prefix) { |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 187 | ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path); |
| 188 | if (!Buffer) |
Alp Toker | 2255dc7 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 189 | return false; |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 190 | return LTOModule::isBitcodeForTarget(Buffer->get(), |
| 191 | StringRef(target_triplet_prefix)); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Mehdi Amini | 45e997d | 2016-07-11 23:10:18 +0000 | [diff] [blame] | 194 | bool lto_module_has_objc_category(const void *mem, size_t length) { |
| 195 | std::unique_ptr<MemoryBuffer> Buffer(LTOModule::makeBuffer(mem, length)); |
| 196 | if (!Buffer) |
| 197 | return false; |
| 198 | LLVMContext Ctx; |
Peter Collingbourne | 9b252f0 | 2016-11-11 19:50:24 +0000 | [diff] [blame] | 199 | ErrorOr<bool> Result = expectedToErrorOrAndEmitErrors( |
| 200 | Ctx, llvm::isBitcodeContainingObjCCategory(*Buffer)); |
| 201 | return Result && *Result; |
Mehdi Amini | 45e997d | 2016-07-11 23:10:18 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 204 | bool lto_module_is_object_file_in_memory(const void* mem, size_t length) { |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 205 | return LTOModule::isBitcodeFile(mem, length); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 208 | bool |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 209 | lto_module_is_object_file_in_memory_for_target(const void* mem, |
| 210 | size_t length, |
| 211 | const char* target_triplet_prefix) { |
Alp Toker | 2255dc7 | 2014-07-04 00:58:41 +0000 | [diff] [blame] | 212 | std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length)); |
| 213 | if (!buffer) |
| 214 | return false; |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 215 | return LTOModule::isBitcodeForTarget(buffer.get(), |
| 216 | StringRef(target_triplet_prefix)); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 219 | lto_module_t lto_module_create(const char* path) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 220 | lto_initialize(); |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 221 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 222 | ErrorOr<std::unique_ptr<LTOModule>> M = |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 223 | LTOModule::createFromFile(*LTOContext, StringRef(path), Options); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 224 | if (!M) |
| 225 | return nullptr; |
| 226 | return wrap(M->release()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 229 | lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 230 | lto_initialize(); |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 231 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 232 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFile( |
| 233 | *LTOContext, fd, StringRef(path), size, Options); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 234 | if (!M) |
| 235 | return nullptr; |
| 236 | return wrap(M->release()); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 237 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 238 | |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 239 | lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path, |
| 240 | size_t file_size, |
| 241 | size_t map_size, |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 242 | off_t offset) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 243 | lto_initialize(); |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 244 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 245 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice( |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 246 | *LTOContext, fd, StringRef(path), map_size, offset, Options); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 247 | if (!M) |
| 248 | return nullptr; |
| 249 | return wrap(M->release()); |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 252 | lto_module_t lto_module_create_from_memory(const void* mem, size_t length) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 253 | lto_initialize(); |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 254 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 255 | ErrorOr<std::unique_ptr<LTOModule>> M = |
| 256 | LTOModule::createFromBuffer(*LTOContext, mem, length, Options); |
| 257 | if (!M) |
| 258 | return nullptr; |
| 259 | return wrap(M->release()); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Manman Ren | f3fc8c9 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 262 | lto_module_t lto_module_create_from_memory_with_path(const void* mem, |
| 263 | size_t length, |
| 264 | const char *path) { |
| 265 | lto_initialize(); |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 266 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 267 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer( |
| 268 | *LTOContext, mem, length, Options, StringRef(path)); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 269 | if (!M) |
| 270 | return nullptr; |
| 271 | return wrap(M->release()); |
Manman Ren | f3fc8c9 | 2014-02-10 23:26:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Duncan P. N. Exon Smith | 4f89709 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 274 | lto_module_t lto_module_create_in_local_context(const void *mem, size_t length, |
| 275 | const char *path) { |
| 276 | lto_initialize(); |
| 277 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Petr Pavlu | 3a3e8dd | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 278 | |
Simon Pilgrim | 428e693 | 2017-03-30 12:59:53 +0000 | [diff] [blame] | 279 | // Create a local context. Ownership will be transferred to LTOModule. |
Petr Pavlu | 3a3e8dd | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 280 | std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>(); |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 281 | Context->setDiagnosticHandler(llvm::make_unique<LTOToolDiagnosticHandler>(), |
| 282 | true); |
Petr Pavlu | 3a3e8dd | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 283 | |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 284 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createInLocalContext( |
| 285 | std::move(Context), mem, length, Options, StringRef(path)); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 286 | if (!M) |
| 287 | return nullptr; |
| 288 | return wrap(M->release()); |
Duncan P. N. Exon Smith | 4f89709 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | lto_module_t lto_module_create_in_codegen_context(const void *mem, |
| 292 | size_t length, |
| 293 | const char *path, |
| 294 | lto_code_gen_t cg) { |
| 295 | lto_initialize(); |
| 296 | llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Petr Pavlu | 3a3e8dd | 2016-03-01 13:13:49 +0000 | [diff] [blame] | 297 | ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer( |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 298 | unwrap(cg)->getContext(), mem, length, Options, StringRef(path)); |
Rafael Espindola | 608d7b4 | 2015-12-04 16:14:31 +0000 | [diff] [blame] | 299 | return wrap(M->release()); |
Duncan P. N. Exon Smith | 4f89709 | 2014-11-11 23:19:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 302 | void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 303 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 304 | const char* lto_module_get_target_triple(lto_module_t mod) { |
Rafael Espindola | a593909 | 2014-07-04 14:19:41 +0000 | [diff] [blame] | 305 | return unwrap(mod)->getTargetTriple().c_str(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 308 | void lto_module_set_target_triple(lto_module_t mod, const char *triple) { |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 309 | return unwrap(mod)->setTargetTriple(StringRef(triple)); |
Rafael Espindola | cbb170d | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 312 | unsigned int lto_module_get_num_symbols(lto_module_t mod) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 313 | return unwrap(mod)->getSymbolCount(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 316 | const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) { |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 317 | return unwrap(mod)->getSymbolName(index).data(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 320 | lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, |
| 321 | unsigned int index) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 322 | return unwrap(mod)->getSymbolAttributes(index); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Peter Collingbourne | 10e9fb1 | 2015-06-29 23:09:12 +0000 | [diff] [blame] | 325 | const char* lto_module_get_linkeropts(lto_module_t mod) { |
Mehdi Amini | c16b74e | 2016-10-07 19:05:14 +0000 | [diff] [blame] | 326 | return unwrap(mod)->getLinkerOpts().data(); |
Yunzhong Gao | a747cf1 | 2014-01-21 18:31:27 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Quentin Colombet | 4c831d9 | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 329 | void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg, |
| 330 | lto_diagnostic_handler_t diag_handler, |
| 331 | void *ctxt) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 332 | unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt); |
Quentin Colombet | 4c831d9 | 2014-01-15 22:04:35 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Duncan P. N. Exon Smith | acc1d12 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 335 | static lto_code_gen_t createCodeGen(bool InLocalContext) { |
Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 336 | lto_initialize(); |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 337 | |
Eli Bendersky | cf42174 | 2014-02-19 17:09:35 +0000 | [diff] [blame] | 338 | TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); |
Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 339 | |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 340 | LibLTOCodeGenerator *CodeGen = |
| 341 | InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>()) |
| 342 | : new LibLTOCodeGenerator(); |
| 343 | CodeGen->setTargetOptions(Options); |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 344 | return wrap(CodeGen); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Duncan P. N. Exon Smith | acc1d12 | 2014-12-19 07:19:50 +0000 | [diff] [blame] | 347 | lto_code_gen_t lto_codegen_create(void) { |
| 348 | return createCodeGen(/* InLocalContext */ false); |
| 349 | } |
| 350 | |
| 351 | lto_code_gen_t lto_codegen_create_in_local_context(void) { |
| 352 | return createCodeGen(/* InLocalContext */ true); |
| 353 | } |
| 354 | |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 355 | void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 356 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 357 | bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) { |
Rafael Espindola | 72478e5 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 358 | return !unwrap(cg)->addModule(unwrap(mod)); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Manman Ren | bd26e6a | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 361 | void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) { |
Peter Collingbourne | 8c1602d | 2015-08-24 22:22:53 +0000 | [diff] [blame] | 362 | unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod))); |
Manman Ren | bd26e6a | 2015-02-24 00:45:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 365 | bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 366 | unwrap(cg)->setDebugInfo(debug); |
Shuxin Yang | 235089b | 2013-08-07 05:19:23 +0000 | [diff] [blame] | 367 | return false; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 370 | bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) { |
Peter Collingbourne | aa7ad07 | 2015-08-21 22:57:17 +0000 | [diff] [blame] | 371 | switch (model) { |
| 372 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 373 | unwrap(cg)->setCodePICModel(Reloc::Static); |
| 374 | return false; |
| 375 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 376 | unwrap(cg)->setCodePICModel(Reloc::PIC_); |
| 377 | return false; |
| 378 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 379 | unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC); |
| 380 | return false; |
| 381 | case LTO_CODEGEN_PIC_MODEL_DEFAULT: |
Rafael Espindola | ac8db59 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 382 | unwrap(cg)->setCodePICModel(None); |
Peter Collingbourne | aa7ad07 | 2015-08-21 22:57:17 +0000 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | sLastErrorString = "Unknown PIC model"; |
| 386 | return true; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 389 | void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 390 | return unwrap(cg)->setCpu(cpu); |
Rafael Espindola | 2d643ef | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 393 | void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) { |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 394 | // In here only for backwards compatibility. We use MC now. |
Nick Kledzik | cbad586 | 2009-06-04 00:28:45 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 397 | void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 398 | int nargs) { |
Rafael Espindola | e9efea1 | 2011-02-24 21:04:06 +0000 | [diff] [blame] | 399 | // In here only for backwards compatibility. We use MC now. |
Rafael Espindola | 98197e5 | 2010-08-10 18:55:09 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Bill Wendling | 8fd3fcd | 2012-03-30 10:29:38 +0000 | [diff] [blame] | 402 | void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 403 | const char *symbol) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 404 | unwrap(cg)->addMustPreserveSymbol(symbol); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 407 | static void maybeParseOptions(lto_code_gen_t cg) { |
Rafael Espindola | 0e95b3a | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 408 | if (!parsedOptions) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 409 | unwrap(cg)->parseCodeGenDebugOptions(); |
Tom Roeder | 817f5e2 | 2014-04-25 21:46:51 +0000 | [diff] [blame] | 410 | lto_add_attrs(cg); |
Rafael Espindola | 0e95b3a | 2013-10-02 14:36:23 +0000 | [diff] [blame] | 411 | parsedOptions = true; |
| 412 | } |
Peter Collingbourne | 416d8ec | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) { |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 416 | maybeParseOptions(cg); |
Yunzhong Gao | 576772b | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 417 | return !unwrap(cg)->writeMergedModules(path); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 420 | const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) { |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 421 | maybeParseOptions(cg); |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 422 | LibLTOCodeGenerator *CG = unwrap(cg); |
Duncan P. N. Exon Smith | 5e60a68 | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 423 | CG->NativeObjectFile = |
| 424 | CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 576772b | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 425 | DisableLTOVectorization); |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 426 | if (!CG->NativeObjectFile) |
| 427 | return nullptr; |
| 428 | *length = CG->NativeObjectFile->getBufferSize(); |
| 429 | return CG->NativeObjectFile->getBufferStart(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Manman Ren | 69e4dd1 | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 432 | bool lto_codegen_optimize(lto_code_gen_t cg) { |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 433 | maybeParseOptions(cg); |
Duncan P. N. Exon Smith | 5e60a68 | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 434 | return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 576772b | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 435 | DisableLTOVectorization); |
Manman Ren | 69e4dd1 | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) { |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 439 | maybeParseOptions(cg); |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 440 | LibLTOCodeGenerator *CG = unwrap(cg); |
Yunzhong Gao | 576772b | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 441 | CG->NativeObjectFile = CG->compileOptimized(); |
Peter Collingbourne | 3fcf5a9 | 2015-06-01 20:08:30 +0000 | [diff] [blame] | 442 | if (!CG->NativeObjectFile) |
| 443 | return nullptr; |
| 444 | *length = CG->NativeObjectFile->getBufferSize(); |
| 445 | return CG->NativeObjectFile->getBufferStart(); |
Manman Ren | 69e4dd1 | 2015-02-03 18:39:15 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Reid Kleckner | 07d5aef | 2013-10-24 22:26:04 +0000 | [diff] [blame] | 448 | bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) { |
Peter Collingbourne | 10f24ca | 2015-03-19 22:12:08 +0000 | [diff] [blame] | 449 | maybeParseOptions(cg); |
Arnold Schwaighofer | b26fb77 | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 450 | return !unwrap(cg)->compile_to_file( |
Duncan P. N. Exon Smith | 5e60a68 | 2015-09-15 23:05:59 +0000 | [diff] [blame] | 451 | name, DisableVerify, DisableInline, DisableGVNLoadPRE, |
Yunzhong Gao | 576772b | 2015-11-17 19:48:12 +0000 | [diff] [blame] | 452 | DisableLTOVectorization); |
Rafael Espindola | 6421a88 | 2011-03-22 20:57:13 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Bill Wendling | 168f142 | 2012-03-31 10:44:20 +0000 | [diff] [blame] | 455 | void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) { |
Rafael Espindola | dd32608 | 2014-05-03 14:59:52 +0000 | [diff] [blame] | 456 | unwrap(cg)->setCodeGenDebugOptions(opt); |
Duncan Sands | d44d4bf | 2009-07-03 15:38:01 +0000 | [diff] [blame] | 457 | } |
Rafael Espindola | f2e1126 | 2015-02-03 19:25:53 +0000 | [diff] [blame] | 458 | |
| 459 | unsigned int lto_api_version() { return LTO_API_VERSION; } |
Manman Ren | 44b800f | 2015-04-17 17:10:09 +0000 | [diff] [blame] | 460 | |
| 461 | void lto_codegen_set_should_internalize(lto_code_gen_t cg, |
| 462 | bool ShouldInternalize) { |
| 463 | unwrap(cg)->setShouldInternalize(ShouldInternalize); |
| 464 | } |
Duncan P. N. Exon Smith | 0b29dea | 2015-04-27 23:38:54 +0000 | [diff] [blame] | 465 | |
| 466 | void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg, |
| 467 | lto_bool_t ShouldEmbedUselists) { |
| 468 | unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists); |
| 469 | } |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 470 | |
| 471 | // ThinLTO API below |
| 472 | |
Mehdi Amini | 5e01ef4 | 2016-03-09 02:36:09 +0000 | [diff] [blame] | 473 | thinlto_code_gen_t thinlto_create_codegen(void) { |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 474 | lto_initialize(); |
| 475 | ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator(); |
| 476 | CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags()); |
Mehdi Amini | e4c5b69 | 2017-03-28 18:55:44 +0000 | [diff] [blame] | 477 | CodeGen->setFreestanding(EnableFreestanding); |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 478 | |
Mehdi Amini | c54021d | 2016-12-28 19:37:16 +0000 | [diff] [blame] | 479 | if (OptLevel.getNumOccurrences()) { |
| 480 | if (OptLevel < '0' || OptLevel > '3') |
| 481 | report_fatal_error("Optimization level must be between 0 and 3"); |
| 482 | CodeGen->setOptLevel(OptLevel - '0'); |
| 483 | switch (OptLevel) { |
| 484 | case '0': |
| 485 | CodeGen->setCodeGenOptLevel(CodeGenOpt::None); |
| 486 | break; |
| 487 | case '1': |
| 488 | CodeGen->setCodeGenOptLevel(CodeGenOpt::Less); |
| 489 | break; |
| 490 | case '2': |
| 491 | CodeGen->setCodeGenOptLevel(CodeGenOpt::Default); |
| 492 | break; |
| 493 | case '3': |
| 494 | CodeGen->setCodeGenOptLevel(CodeGenOpt::Aggressive); |
| 495 | break; |
| 496 | } |
| 497 | } |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 498 | return wrap(CodeGen); |
| 499 | } |
| 500 | |
| 501 | void thinlto_codegen_dispose(thinlto_code_gen_t cg) { delete unwrap(cg); } |
| 502 | |
| 503 | void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier, |
| 504 | const char *Data, int Length) { |
| 505 | unwrap(cg)->addModule(Identifier, StringRef(Data, Length)); |
| 506 | } |
| 507 | |
| 508 | void thinlto_codegen_process(thinlto_code_gen_t cg) { unwrap(cg)->run(); } |
| 509 | |
| 510 | unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) { |
| 511 | return unwrap(cg)->getProducedBinaries().size(); |
| 512 | } |
| 513 | LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg, |
| 514 | unsigned int index) { |
| 515 | assert(index < unwrap(cg)->getProducedBinaries().size() && "Index overflow"); |
| 516 | auto &MemBuffer = unwrap(cg)->getProducedBinaries()[index]; |
Mehdi Amini | 14e4632 | 2016-03-19 21:28:18 +0000 | [diff] [blame] | 517 | return LTOObjectBuffer{MemBuffer->getBufferStart(), |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 518 | MemBuffer->getBufferSize()}; |
| 519 | } |
| 520 | |
Mehdi Amini | dbca62e | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 521 | unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg) { |
| 522 | return unwrap(cg)->getProducedBinaryFiles().size(); |
| 523 | } |
| 524 | const char *thinlto_module_get_object_file(thinlto_code_gen_t cg, |
| 525 | unsigned int index) { |
| 526 | assert(index < unwrap(cg)->getProducedBinaryFiles().size() && |
| 527 | "Index overflow"); |
| 528 | return unwrap(cg)->getProducedBinaryFiles()[index].c_str(); |
| 529 | } |
| 530 | |
Mehdi Amini | 51f5548 | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 531 | void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg, |
| 532 | lto_bool_t disable) { |
| 533 | unwrap(cg)->disableCodeGen(disable); |
| 534 | } |
| 535 | |
| 536 | void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg, |
| 537 | lto_bool_t CodeGenOnly) { |
| 538 | unwrap(cg)->setCodeGenOnly(CodeGenOnly); |
| 539 | } |
| 540 | |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 541 | void thinlto_debug_options(const char *const *options, int number) { |
| 542 | // if options were requested, set them |
| 543 | if (number && options) { |
| 544 | std::vector<const char *> CodegenArgv(1, "libLTO"); |
| 545 | for (auto Arg : ArrayRef<const char *>(options, number)) |
| 546 | CodegenArgv.push_back(Arg); |
| 547 | cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data()); |
| 548 | } |
| 549 | } |
| 550 | |
Mehdi Amini | 51f5548 | 2016-04-01 06:47:02 +0000 | [diff] [blame] | 551 | lto_bool_t lto_module_is_thinlto(lto_module_t mod) { |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 552 | return unwrap(mod)->isThinLTO(); |
| 553 | } |
| 554 | |
| 555 | void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg, |
| 556 | const char *Name, int Length) { |
| 557 | unwrap(cg)->preserveSymbol(StringRef(Name, Length)); |
| 558 | } |
| 559 | |
| 560 | void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg, |
| 561 | const char *Name, int Length) { |
| 562 | unwrap(cg)->crossReferenceSymbol(StringRef(Name, Length)); |
| 563 | } |
| 564 | |
| 565 | void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) { |
| 566 | return unwrap(cg)->setCpu(cpu); |
| 567 | } |
| 568 | |
| 569 | void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg, |
| 570 | const char *cache_dir) { |
| 571 | return unwrap(cg)->setCacheDir(cache_dir); |
| 572 | } |
| 573 | |
| 574 | void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg, |
| 575 | int interval) { |
| 576 | return unwrap(cg)->setCachePruningInterval(interval); |
| 577 | } |
| 578 | |
| 579 | void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg, |
| 580 | unsigned expiration) { |
| 581 | return unwrap(cg)->setCacheEntryExpiration(expiration); |
| 582 | } |
| 583 | |
| 584 | void thinlto_codegen_set_final_cache_size_relative_to_available_space( |
| 585 | thinlto_code_gen_t cg, unsigned Percentage) { |
| 586 | return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage); |
| 587 | } |
| 588 | |
Ekaterina Romanova | 76899c6 | 2018-03-02 03:51:27 +0000 | [diff] [blame] | 589 | void thinlto_codegen_set_cache_size_bytes( |
| 590 | thinlto_code_gen_t cg, unsigned MaxSizeBytes) { |
| 591 | return unwrap(cg)->setCacheMaxSizeBytes(MaxSizeBytes); |
| 592 | } |
| 593 | |
James Henderson | b981f4f | 2018-09-17 10:21:26 +0000 | [diff] [blame] | 594 | void thinlto_codegen_set_cache_size_megabytes( |
| 595 | thinlto_code_gen_t cg, unsigned MaxSizeMegabytes) { |
| 596 | uint64_t MaxSizeBytes = MaxSizeMegabytes; |
| 597 | MaxSizeBytes *= 1024 * 1024; |
| 598 | return unwrap(cg)->setCacheMaxSizeBytes(MaxSizeBytes); |
| 599 | } |
| 600 | |
Ekaterina Romanova | 76899c6 | 2018-03-02 03:51:27 +0000 | [diff] [blame] | 601 | void thinlto_codegen_set_cache_size_files( |
| 602 | thinlto_code_gen_t cg, unsigned MaxSizeFiles) { |
| 603 | return unwrap(cg)->setCacheMaxSizeFiles(MaxSizeFiles); |
| 604 | } |
| 605 | |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 606 | void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg, |
| 607 | const char *save_temps_dir) { |
| 608 | return unwrap(cg)->setSaveTempsDir(save_temps_dir); |
| 609 | } |
| 610 | |
Mehdi Amini | dbca62e | 2016-12-14 04:56:42 +0000 | [diff] [blame] | 611 | void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg, |
| 612 | const char *save_temps_dir) { |
| 613 | unwrap(cg)->setGeneratedObjectsDirectory(save_temps_dir); |
| 614 | } |
| 615 | |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 616 | lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg, |
| 617 | lto_codegen_model model) { |
| 618 | switch (model) { |
| 619 | case LTO_CODEGEN_PIC_MODEL_STATIC: |
| 620 | unwrap(cg)->setCodePICModel(Reloc::Static); |
| 621 | return false; |
| 622 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC: |
| 623 | unwrap(cg)->setCodePICModel(Reloc::PIC_); |
| 624 | return false; |
| 625 | case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: |
| 626 | unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC); |
| 627 | return false; |
| 628 | case LTO_CODEGEN_PIC_MODEL_DEFAULT: |
Rafael Espindola | ac8db59 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 629 | unwrap(cg)->setCodePICModel(None); |
Mehdi Amini | 73cf01b | 2016-03-09 01:37:22 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | sLastErrorString = "Unknown PIC model"; |
| 633 | return true; |
| 634 | } |