1 //===-LTOCodeGenerator.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/LTO/LTOCodeGenerator.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/Analysis/Passes.h" 18 #include "llvm/Analysis/TargetLibraryInfo.h" 19 #include "llvm/Analysis/TargetTransformInfo.h" 20 #include "llvm/Bitcode/ReaderWriter.h" 21 #include "llvm/CodeGen/RuntimeLibcalls.h" 22 #include "llvm/Config/config.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/DerivedTypes.h" 26 #include "llvm/IR/DiagnosticInfo.h" 27 #include "llvm/IR/DiagnosticPrinter.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/Mangler.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/IR/Verifier.h" 32 #include "llvm/InitializePasses.h" 33 #include "llvm/LTO/LTOModule.h" 34 #include "llvm/Linker/Linker.h" 35 #include "llvm/MC/MCAsmInfo.h" 36 #include "llvm/MC/MCContext.h" 37 #include "llvm/MC/SubtargetFeature.h" 38 #include "llvm/PassManager.h" 39 #include "llvm/Support/CommandLine.h" 40 #include "llvm/Support/FileSystem.h" 41 #include "llvm/Support/FormattedStream.h" 42 #include "llvm/Support/Host.h" 43 #include "llvm/Support/MemoryBuffer.h" 44 #include "llvm/Support/Signals.h" 45 #include "llvm/Support/TargetRegistry.h" 46 #include "llvm/Support/TargetSelect.h" 47 #include "llvm/Support/ToolOutputFile.h" 48 #include "llvm/Support/raw_ostream.h" 49 #include "llvm/Target/TargetLowering.h" 50 #include "llvm/Target/TargetOptions.h" 51 #include "llvm/Target/TargetRegisterInfo.h" 52 #include "llvm/Target/TargetSubtargetInfo.h" 53 #include "llvm/Transforms/IPO.h" 54 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 55 #include "llvm/Transforms/ObjCARC.h" 56 #include <system_error> 57 using namespace llvm; 58 59 const char* LTOCodeGenerator::getVersionString() { 60 #ifdef LLVM_VERSION_INFO 61 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO; 62 #else 63 return PACKAGE_NAME " version " PACKAGE_VERSION; 64 #endif 65 } 66 67 LTOCodeGenerator::LTOCodeGenerator() 68 : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) { 69 initialize(); 70 } 71 72 LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context) 73 : OwnedContext(std::move(Context)), Context(*OwnedContext), 74 IRLinker(new Module("ld-temp.o", *OwnedContext)) { 75 initialize(); 76 } 77 78 void LTOCodeGenerator::initialize() { 79 TargetMach = nullptr; 80 EmitDwarfDebugInfo = false; 81 ScopeRestrictionsDone = false; 82 CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT; 83 DiagHandler = nullptr; 84 DiagContext = nullptr; 85 86 initializeLTOPasses(); 87 } 88 89 LTOCodeGenerator::~LTOCodeGenerator() { 90 delete TargetMach; 91 TargetMach = nullptr; 92 93 IRLinker.deleteModule(); 94 95 for (std::vector<char *>::iterator I = CodegenOptions.begin(), 96 E = CodegenOptions.end(); 97 I != E; ++I) 98 free(*I); 99 } 100 101 // Initialize LTO passes. Please keep this funciton in sync with 102 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO 103 // passes are initialized. 104 void LTOCodeGenerator::initializeLTOPasses() { 105 PassRegistry &R = *PassRegistry::getPassRegistry(); 106 107 initializeInternalizePassPass(R); 108 initializeIPSCCPPass(R); 109 initializeGlobalOptPass(R); 110 initializeConstantMergePass(R); 111 initializeDAHPass(R); 112 initializeInstructionCombiningPassPass(R); 113 initializeSimpleInlinerPass(R); 114 initializePruneEHPass(R); 115 initializeGlobalDCEPass(R); 116 initializeArgPromotionPass(R); 117 initializeJumpThreadingPass(R); 118 initializeSROAPass(R); 119 initializeSROA_DTPass(R); 120 initializeSROA_SSAUpPass(R); 121 initializeFunctionAttrsPass(R); 122 initializeGlobalsModRefPass(R); 123 initializeLICMPass(R); 124 initializeMergedLoadStoreMotionPass(R); 125 initializeGVNPass(R); 126 initializeMemCpyOptPass(R); 127 initializeDCEPass(R); 128 initializeCFGSimplifyPassPass(R); 129 } 130 131 bool LTOCodeGenerator::addModule(LTOModule *mod) { 132 assert(&mod->getModule().getContext() == &Context && 133 "Expected module in same context"); 134 135 bool ret = IRLinker.linkInModule(&mod->getModule()); 136 137 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs(); 138 for (int i = 0, e = undefs.size(); i != e; ++i) 139 AsmUndefinedRefs[undefs[i]] = 1; 140 141 return !ret; 142 } 143 144 void LTOCodeGenerator::setTargetOptions(TargetOptions options) { 145 Options = options; 146 } 147 148 void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) { 149 switch (debug) { 150 case LTO_DEBUG_MODEL_NONE: 151 EmitDwarfDebugInfo = false; 152 return; 153 154 case LTO_DEBUG_MODEL_DWARF: 155 EmitDwarfDebugInfo = true; 156 return; 157 } 158 llvm_unreachable("Unknown debug format!"); 159 } 160 161 void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) { 162 switch (model) { 163 case LTO_CODEGEN_PIC_MODEL_STATIC: 164 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 165 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 166 case LTO_CODEGEN_PIC_MODEL_DEFAULT: 167 CodeModel = model; 168 return; 169 } 170 llvm_unreachable("Unknown PIC model!"); 171 } 172 173 bool LTOCodeGenerator::writeMergedModules(const char *path, 174 std::string &errMsg) { 175 if (!determineTarget(errMsg)) 176 return false; 177 178 // mark which symbols can not be internalized 179 applyScopeRestrictions(); 180 181 // create output file 182 std::error_code EC; 183 tool_output_file Out(path, EC, sys::fs::F_None); 184 if (EC) { 185 errMsg = "could not open bitcode file for writing: "; 186 errMsg += path; 187 return false; 188 } 189 190 // write bitcode to it 191 WriteBitcodeToFile(IRLinker.getModule(), Out.os()); 192 Out.os().close(); 193 194 if (Out.os().has_error()) { 195 errMsg = "could not write bitcode file: "; 196 errMsg += path; 197 Out.os().clear_error(); 198 return false; 199 } 200 201 Out.keep(); 202 return true; 203 } 204 205 bool LTOCodeGenerator::compile_to_file(const char** name, 206 bool disableOpt, 207 bool disableInline, 208 bool disableGVNLoadPRE, 209 bool disableVectorization, 210 std::string& errMsg) { 211 // make unique temp .o file to put generated object file 212 SmallString<128> Filename; 213 int FD; 214 std::error_code EC = 215 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename); 216 if (EC) { 217 errMsg = EC.message(); 218 return false; 219 } 220 221 // generate object file 222 tool_output_file objFile(Filename.c_str(), FD); 223 224 bool genResult = 225 generateObjectFile(objFile.os(), disableOpt, disableInline, 226 disableGVNLoadPRE, disableVectorization, errMsg); 227 objFile.os().close(); 228 if (objFile.os().has_error()) { 229 objFile.os().clear_error(); 230 sys::fs::remove(Twine(Filename)); 231 return false; 232 } 233 234 objFile.keep(); 235 if (!genResult) { 236 sys::fs::remove(Twine(Filename)); 237 return false; 238 } 239 240 NativeObjectPath = Filename.c_str(); 241 *name = NativeObjectPath.c_str(); 242 return true; 243 } 244 245 const void* LTOCodeGenerator::compile(size_t* length, 246 bool disableOpt, 247 bool disableInline, 248 bool disableGVNLoadPRE, 249 bool disableVectorization, 250 std::string& errMsg) { 251 const char *name; 252 if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE, 253 disableVectorization, errMsg)) 254 return nullptr; 255 256 // read .o file into memory buffer 257 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = 258 MemoryBuffer::getFile(name, -1, false); 259 if (std::error_code EC = BufferOrErr.getError()) { 260 errMsg = EC.message(); 261 sys::fs::remove(NativeObjectPath); 262 return nullptr; 263 } 264 NativeObjectFile = std::move(*BufferOrErr); 265 266 // remove temp files 267 sys::fs::remove(NativeObjectPath); 268 269 // return buffer, unless error 270 if (!NativeObjectFile) 271 return nullptr; 272 *length = NativeObjectFile->getBufferSize(); 273 return NativeObjectFile->getBufferStart(); 274 } 275 276 bool LTOCodeGenerator::determineTarget(std::string &errMsg) { 277 if (TargetMach) 278 return true; 279 280 std::string TripleStr = IRLinker.getModule()->getTargetTriple(); 281 if (TripleStr.empty()) 282 TripleStr = sys::getDefaultTargetTriple(); 283 llvm::Triple Triple(TripleStr); 284 285 // create target machine from info for merged modules 286 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); 287 if (!march) 288 return false; 289 290 // The relocation model is actually a static member of TargetMachine and 291 // needs to be set before the TargetMachine is instantiated. 292 Reloc::Model RelocModel = Reloc::Default; 293 switch (CodeModel) { 294 case LTO_CODEGEN_PIC_MODEL_STATIC: 295 RelocModel = Reloc::Static; 296 break; 297 case LTO_CODEGEN_PIC_MODEL_DYNAMIC: 298 RelocModel = Reloc::PIC_; 299 break; 300 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC: 301 RelocModel = Reloc::DynamicNoPIC; 302 break; 303 case LTO_CODEGEN_PIC_MODEL_DEFAULT: 304 // RelocModel is already the default, so leave it that way. 305 break; 306 } 307 308 // Construct LTOModule, hand over ownership of module and target. Use MAttr as 309 // the default set of features. 310 SubtargetFeatures Features(MAttr); 311 Features.getDefaultSubtargetFeatures(Triple); 312 std::string FeatureStr = Features.getString(); 313 // Set a default CPU for Darwin triples. 314 if (MCpu.empty() && Triple.isOSDarwin()) { 315 if (Triple.getArch() == llvm::Triple::x86_64) 316 MCpu = "core2"; 317 else if (Triple.getArch() == llvm::Triple::x86) 318 MCpu = "yonah"; 319 else if (Triple.getArch() == llvm::Triple::aarch64) 320 MCpu = "cyclone"; 321 } 322 323 TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options, 324 RelocModel, CodeModel::Default, 325 CodeGenOpt::Aggressive); 326 return true; 327 } 328 329 void LTOCodeGenerator:: 330 applyRestriction(GlobalValue &GV, 331 ArrayRef<StringRef> Libcalls, 332 std::vector<const char*> &MustPreserveList, 333 SmallPtrSetImpl<GlobalValue*> &AsmUsed, 334 Mangler &Mangler) { 335 // There are no restrictions to apply to declarations. 336 if (GV.isDeclaration()) 337 return; 338 339 // There is nothing more restrictive than private linkage. 340 if (GV.hasPrivateLinkage()) 341 return; 342 343 SmallString<64> Buffer; 344 TargetMach->getNameWithPrefix(Buffer, &GV, Mangler); 345 346 if (MustPreserveSymbols.count(Buffer)) 347 MustPreserveList.push_back(GV.getName().data()); 348 if (AsmUndefinedRefs.count(Buffer)) 349 AsmUsed.insert(&GV); 350 351 // Conservatively append user-supplied runtime library functions to 352 // llvm.compiler.used. These could be internalized and deleted by 353 // optimizations like -globalopt, causing problems when later optimizations 354 // add new library calls (e.g., llvm.memset => memset and printf => puts). 355 // Leave it to the linker to remove any dead code (e.g. with -dead_strip). 356 if (isa<Function>(GV) && 357 std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName())) 358 AsmUsed.insert(&GV); 359 } 360 361 static void findUsedValues(GlobalVariable *LLVMUsed, 362 SmallPtrSetImpl<GlobalValue*> &UsedValues) { 363 if (!LLVMUsed) return; 364 365 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer()); 366 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) 367 if (GlobalValue *GV = 368 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts())) 369 UsedValues.insert(GV); 370 } 371 372 // Collect names of runtime library functions. User-defined functions with the 373 // same names are added to llvm.compiler.used to prevent them from being 374 // deleted by optimizations. 375 static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls, 376 const TargetLibraryInfo& TLI, 377 const Module &Mod, 378 const TargetMachine &TM) { 379 // TargetLibraryInfo has info on C runtime library calls on the current 380 // target. 381 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); 382 I != E; ++I) { 383 LibFunc::Func F = static_cast<LibFunc::Func>(I); 384 if (TLI.has(F)) 385 Libcalls.push_back(TLI.getName(F)); 386 } 387 388 SmallPtrSet<const TargetLowering *, 1> TLSet; 389 390 for (const Function &F : Mod) { 391 const TargetLowering *Lowering = 392 TM.getSubtargetImpl(F)->getTargetLowering(); 393 394 if (Lowering && TLSet.insert(Lowering).second) 395 // TargetLowering has info on library calls that CodeGen expects to be 396 // available, both from the C runtime and compiler-rt. 397 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL); 398 I != E; ++I) 399 if (const char *Name = 400 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I))) 401 Libcalls.push_back(Name); 402 } 403 404 array_pod_sort(Libcalls.begin(), Libcalls.end()); 405 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()), 406 Libcalls.end()); 407 } 408 409 void LTOCodeGenerator::applyScopeRestrictions() { 410 if (ScopeRestrictionsDone) 411 return; 412 Module *mergedModule = IRLinker.getModule(); 413 414 // Start off with a verification pass. 415 PassManager passes; 416 passes.add(createVerifierPass()); 417 passes.add(createDebugInfoVerifierPass()); 418 419 // mark which symbols can not be internalized 420 Mangler Mangler(TargetMach->getDataLayout()); 421 std::vector<const char*> MustPreserveList; 422 SmallPtrSet<GlobalValue*, 8> AsmUsed; 423 std::vector<StringRef> Libcalls; 424 TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple())); 425 TargetLibraryInfo TLI(TLII); 426 427 accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach); 428 429 for (Module::iterator f = mergedModule->begin(), 430 e = mergedModule->end(); f != e; ++f) 431 applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler); 432 for (Module::global_iterator v = mergedModule->global_begin(), 433 e = mergedModule->global_end(); v != e; ++v) 434 applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler); 435 for (Module::alias_iterator a = mergedModule->alias_begin(), 436 e = mergedModule->alias_end(); a != e; ++a) 437 applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler); 438 439 GlobalVariable *LLVMCompilerUsed = 440 mergedModule->getGlobalVariable("llvm.compiler.used"); 441 findUsedValues(LLVMCompilerUsed, AsmUsed); 442 if (LLVMCompilerUsed) 443 LLVMCompilerUsed->eraseFromParent(); 444 445 if (!AsmUsed.empty()) { 446 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context); 447 std::vector<Constant*> asmUsed2; 448 for (auto *GV : AsmUsed) { 449 Constant *c = ConstantExpr::getBitCast(GV, i8PTy); 450 asmUsed2.push_back(c); 451 } 452 453 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size()); 454 LLVMCompilerUsed = 455 new llvm::GlobalVariable(*mergedModule, ATy, false, 456 llvm::GlobalValue::AppendingLinkage, 457 llvm::ConstantArray::get(ATy, asmUsed2), 458 "llvm.compiler.used"); 459 460 LLVMCompilerUsed->setSection("llvm.metadata"); 461 } 462 463 passes.add(createInternalizePass(MustPreserveList)); 464 465 // apply scope restrictions 466 passes.run(*mergedModule); 467 468 ScopeRestrictionsDone = true; 469 } 470 471 /// Optimize merged modules using various IPO passes 472 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out, 473 bool DisableOpt, 474 bool DisableInline, 475 bool DisableGVNLoadPRE, 476 bool DisableVectorization, 477 std::string &errMsg) { 478 if (!this->determineTarget(errMsg)) 479 return false; 480 481 Module *mergedModule = IRLinker.getModule(); 482 483 // Mark which symbols can not be internalized 484 this->applyScopeRestrictions(); 485 486 // Instantiate the pass manager to organize the passes. 487 PassManager passes; 488 489 // Add an appropriate DataLayout instance for this module... 490 mergedModule->setDataLayout(TargetMach->getDataLayout()); 491 492 passes.add(new DataLayoutPass()); 493 passes.add( 494 createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); 495 496 Triple TargetTriple(TargetMach->getTargetTriple()); 497 PassManagerBuilder PMB; 498 PMB.DisableGVNLoadPRE = DisableGVNLoadPRE; 499 PMB.LoopVectorize = !DisableVectorization; 500 PMB.SLPVectorize = !DisableVectorization; 501 if (!DisableInline) 502 PMB.Inliner = createFunctionInliningPass(); 503 PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple); 504 if (DisableOpt) 505 PMB.OptLevel = 0; 506 PMB.VerifyInput = true; 507 PMB.VerifyOutput = true; 508 509 PMB.populateLTOPassManager(passes); 510 511 PassManager codeGenPasses; 512 513 codeGenPasses.add(new DataLayoutPass()); 514 515 formatted_raw_ostream Out(out); 516 517 // If the bitcode files contain ARC code and were compiled with optimization, 518 // the ObjCARCContractPass must be run, so do it unconditionally here. 519 codeGenPasses.add(createObjCARCContractPass()); 520 521 if (TargetMach->addPassesToEmitFile(codeGenPasses, Out, 522 TargetMachine::CGFT_ObjectFile)) { 523 errMsg = "target file type not supported"; 524 return false; 525 } 526 527 // Run our queue of passes all at once now, efficiently. 528 passes.run(*mergedModule); 529 530 // Run the code generator, and write assembly file 531 codeGenPasses.run(*mergedModule); 532 533 return true; 534 } 535 536 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging 537 /// LTO problems. 538 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) { 539 for (std::pair<StringRef, StringRef> o = getToken(options); 540 !o.first.empty(); o = getToken(o.second)) { 541 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add 542 // that. 543 if (CodegenOptions.empty()) 544 CodegenOptions.push_back(strdup("libLLVMLTO")); 545 CodegenOptions.push_back(strdup(o.first.str().c_str())); 546 } 547 } 548 549 void LTOCodeGenerator::parseCodeGenDebugOptions() { 550 // if options were requested, set them 551 if (!CodegenOptions.empty()) 552 cl::ParseCommandLineOptions(CodegenOptions.size(), 553 const_cast<char **>(&CodegenOptions[0])); 554 } 555 556 void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI, 557 void *Context) { 558 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI); 559 } 560 561 void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) { 562 // Map the LLVM internal diagnostic severity to the LTO diagnostic severity. 563 lto_codegen_diagnostic_severity_t Severity; 564 switch (DI.getSeverity()) { 565 case DS_Error: 566 Severity = LTO_DS_ERROR; 567 break; 568 case DS_Warning: 569 Severity = LTO_DS_WARNING; 570 break; 571 case DS_Remark: 572 Severity = LTO_DS_REMARK; 573 break; 574 case DS_Note: 575 Severity = LTO_DS_NOTE; 576 break; 577 } 578 // Create the string that will be reported to the external diagnostic handler. 579 std::string MsgStorage; 580 raw_string_ostream Stream(MsgStorage); 581 DiagnosticPrinterRawOStream DP(Stream); 582 DI.print(DP); 583 Stream.flush(); 584 585 // If this method has been called it means someone has set up an external 586 // diagnostic handler. Assert on that. 587 assert(DiagHandler && "Invalid diagnostic handler"); 588 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext); 589 } 590 591 void 592 LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler, 593 void *Ctxt) { 594 this->DiagHandler = DiagHandler; 595 this->DiagContext = Ctxt; 596 if (!DiagHandler) 597 return Context.setDiagnosticHandler(nullptr, nullptr); 598 // Register the LTOCodeGenerator stub in the LLVMContext to forward the 599 // diagnostic to the external DiagHandler. 600 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this, 601 /* RespectFilters */ true); 602 } 603