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