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