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