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