1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===// 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 #include "clang/CodeGen/BackendUtil.h" 11 #include "clang/Basic/Diagnostic.h" 12 #include "clang/Basic/LangOptions.h" 13 #include "clang/Basic/TargetOptions.h" 14 #include "clang/Frontend/CodeGenOptions.h" 15 #include "clang/Frontend/FrontendDiagnostic.h" 16 #include "clang/Frontend/Utils.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/Analysis/TargetLibraryInfo.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/Bitcode/BitcodeWriterPass.h" 22 #include "llvm/CodeGen/RegAllocRegistry.h" 23 #include "llvm/CodeGen/SchedulerRegistry.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/FunctionInfo.h" 26 #include "llvm/IR/IRPrintingPasses.h" 27 #include "llvm/IR/LegacyPassManager.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/IR/Verifier.h" 30 #include "llvm/MC/SubtargetFeature.h" 31 #include "llvm/Object/FunctionIndexObjectFile.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/PrettyStackTrace.h" 34 #include "llvm/Support/TargetRegistry.h" 35 #include "llvm/Support/Timer.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include "llvm/Target/TargetMachine.h" 38 #include "llvm/Target/TargetOptions.h" 39 #include "llvm/Target/TargetSubtargetInfo.h" 40 #include "llvm/Transforms/IPO.h" 41 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 42 #include "llvm/Transforms/Instrumentation.h" 43 #include "llvm/Transforms/ObjCARC.h" 44 #include "llvm/Transforms/Scalar.h" 45 #include "llvm/Transforms/Utils/SymbolRewriter.h" 46 #include <memory> 47 using namespace clang; 48 using namespace llvm; 49 50 namespace { 51 52 class EmitAssemblyHelper { 53 DiagnosticsEngine &Diags; 54 const CodeGenOptions &CodeGenOpts; 55 const clang::TargetOptions &TargetOpts; 56 const LangOptions &LangOpts; 57 Module *TheModule; 58 59 Timer CodeGenerationTime; 60 61 mutable legacy::PassManager *CodeGenPasses; 62 mutable legacy::PassManager *PerModulePasses; 63 mutable legacy::FunctionPassManager *PerFunctionPasses; 64 65 private: 66 TargetIRAnalysis getTargetIRAnalysis() const { 67 if (TM) 68 return TM->getTargetIRAnalysis(); 69 70 return TargetIRAnalysis(); 71 } 72 73 legacy::PassManager *getCodeGenPasses() const { 74 if (!CodeGenPasses) { 75 CodeGenPasses = new legacy::PassManager(); 76 CodeGenPasses->add( 77 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 78 } 79 return CodeGenPasses; 80 } 81 82 legacy::PassManager *getPerModulePasses() const { 83 if (!PerModulePasses) { 84 PerModulePasses = new legacy::PassManager(); 85 PerModulePasses->add( 86 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 87 } 88 return PerModulePasses; 89 } 90 91 legacy::FunctionPassManager *getPerFunctionPasses() const { 92 if (!PerFunctionPasses) { 93 PerFunctionPasses = new legacy::FunctionPassManager(TheModule); 94 PerFunctionPasses->add( 95 createTargetTransformInfoWrapperPass(getTargetIRAnalysis())); 96 } 97 return PerFunctionPasses; 98 } 99 100 void CreatePasses(FunctionInfoIndex *FunctionIndex); 101 102 /// Generates the TargetMachine. 103 /// Returns Null if it is unable to create the target machine. 104 /// Some of our clang tests specify triples which are not built 105 /// into clang. This is okay because these tests check the generated 106 /// IR, and they require DataLayout which depends on the triple. 107 /// In this case, we allow this method to fail and not report an error. 108 /// When MustCreateTM is used, we print an error if we are unable to load 109 /// the requested target. 110 TargetMachine *CreateTargetMachine(bool MustCreateTM); 111 112 /// Add passes necessary to emit assembly or LLVM IR. 113 /// 114 /// \return True on success. 115 bool AddEmitPasses(BackendAction Action, raw_pwrite_stream &OS); 116 117 public: 118 EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts, 119 const clang::TargetOptions &TOpts, 120 const LangOptions &LOpts, Module *M) 121 : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts), 122 TheModule(M), CodeGenerationTime("Code Generation Time"), 123 CodeGenPasses(nullptr), PerModulePasses(nullptr), 124 PerFunctionPasses(nullptr) {} 125 126 ~EmitAssemblyHelper() { 127 delete CodeGenPasses; 128 delete PerModulePasses; 129 delete PerFunctionPasses; 130 if (CodeGenOpts.DisableFree) 131 BuryPointer(std::move(TM)); 132 } 133 134 std::unique_ptr<TargetMachine> TM; 135 136 void EmitAssembly(BackendAction Action, raw_pwrite_stream *OS); 137 }; 138 139 // We need this wrapper to access LangOpts and CGOpts from extension functions 140 // that we add to the PassManagerBuilder. 141 class PassManagerBuilderWrapper : public PassManagerBuilder { 142 public: 143 PassManagerBuilderWrapper(const CodeGenOptions &CGOpts, 144 const LangOptions &LangOpts) 145 : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {} 146 const CodeGenOptions &getCGOpts() const { return CGOpts; } 147 const LangOptions &getLangOpts() const { return LangOpts; } 148 private: 149 const CodeGenOptions &CGOpts; 150 const LangOptions &LangOpts; 151 }; 152 153 } 154 155 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 156 if (Builder.OptLevel > 0) 157 PM.add(createObjCARCAPElimPass()); 158 } 159 160 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 161 if (Builder.OptLevel > 0) 162 PM.add(createObjCARCExpandPass()); 163 } 164 165 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 166 if (Builder.OptLevel > 0) 167 PM.add(createObjCARCOptPass()); 168 } 169 170 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder, 171 legacy::PassManagerBase &PM) { 172 PM.add(createAddDiscriminatorsPass()); 173 } 174 175 static void addBoundsCheckingPass(const PassManagerBuilder &Builder, 176 legacy::PassManagerBase &PM) { 177 PM.add(createBoundsCheckingPass()); 178 } 179 180 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder, 181 legacy::PassManagerBase &PM) { 182 const PassManagerBuilderWrapper &BuilderWrapper = 183 static_cast<const PassManagerBuilderWrapper&>(Builder); 184 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 185 SanitizerCoverageOptions Opts; 186 Opts.CoverageType = 187 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType); 188 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls; 189 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB; 190 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp; 191 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters; 192 PM.add(createSanitizerCoverageModulePass(Opts)); 193 } 194 195 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder, 196 legacy::PassManagerBase &PM) { 197 const PassManagerBuilderWrapper &BuilderWrapper = 198 static_cast<const PassManagerBuilderWrapper&>(Builder); 199 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 200 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address); 201 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/false, Recover)); 202 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover)); 203 } 204 205 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder, 206 legacy::PassManagerBase &PM) { 207 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/true, 208 /*Recover*/true)); 209 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true, 210 /*Recover*/true)); 211 } 212 213 static void addMemorySanitizerPass(const PassManagerBuilder &Builder, 214 legacy::PassManagerBase &PM) { 215 const PassManagerBuilderWrapper &BuilderWrapper = 216 static_cast<const PassManagerBuilderWrapper&>(Builder); 217 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts(); 218 PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins)); 219 220 // MemorySanitizer inserts complex instrumentation that mostly follows 221 // the logic of the original code, but operates on "shadow" values. 222 // It can benefit from re-running some general purpose optimization passes. 223 if (Builder.OptLevel > 0) { 224 PM.add(createEarlyCSEPass()); 225 PM.add(createReassociatePass()); 226 PM.add(createLICMPass()); 227 PM.add(createGVNPass()); 228 PM.add(createInstructionCombiningPass()); 229 PM.add(createDeadStoreEliminationPass()); 230 } 231 } 232 233 static void addThreadSanitizerPass(const PassManagerBuilder &Builder, 234 legacy::PassManagerBase &PM) { 235 PM.add(createThreadSanitizerPass()); 236 } 237 238 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder, 239 legacy::PassManagerBase &PM) { 240 const PassManagerBuilderWrapper &BuilderWrapper = 241 static_cast<const PassManagerBuilderWrapper&>(Builder); 242 const LangOptions &LangOpts = BuilderWrapper.getLangOpts(); 243 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles)); 244 } 245 246 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple, 247 const CodeGenOptions &CodeGenOpts) { 248 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple); 249 if (!CodeGenOpts.SimplifyLibCalls) 250 TLII->disableAllFunctions(); 251 else { 252 // Disable individual libc/libm calls in TargetLibraryInfo. 253 LibFunc::Func F; 254 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs()) 255 if (TLII->getLibFunc(FuncName, F)) 256 TLII->setUnavailable(F); 257 } 258 259 switch (CodeGenOpts.getVecLib()) { 260 case CodeGenOptions::Accelerate: 261 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate); 262 break; 263 default: 264 break; 265 } 266 return TLII; 267 } 268 269 static void addSymbolRewriterPass(const CodeGenOptions &Opts, 270 legacy::PassManager *MPM) { 271 llvm::SymbolRewriter::RewriteDescriptorList DL; 272 273 llvm::SymbolRewriter::RewriteMapParser MapParser; 274 for (const auto &MapFile : Opts.RewriteMapFiles) 275 MapParser.parse(MapFile, &DL); 276 277 MPM->add(createRewriteSymbolsPass(DL)); 278 } 279 280 void EmitAssemblyHelper::CreatePasses(FunctionInfoIndex *FunctionIndex) { 281 if (CodeGenOpts.DisableLLVMPasses) 282 return; 283 284 unsigned OptLevel = CodeGenOpts.OptimizationLevel; 285 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining(); 286 287 // Handle disabling of LLVM optimization, where we want to preserve the 288 // internal module before any optimization. 289 if (CodeGenOpts.DisableLLVMOpts) { 290 OptLevel = 0; 291 Inlining = CodeGenOpts.NoInlining; 292 } 293 294 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts); 295 296 // Figure out TargetLibraryInfo. 297 Triple TargetTriple(TheModule->getTargetTriple()); 298 PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts); 299 300 switch (Inlining) { 301 case CodeGenOptions::NoInlining: 302 break; 303 case CodeGenOptions::NormalInlining: { 304 PMBuilder.Inliner = 305 createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize); 306 break; 307 } 308 case CodeGenOptions::OnlyAlwaysInlining: 309 // Respect always_inline. 310 if (OptLevel == 0) 311 // Do not insert lifetime intrinsics at -O0. 312 PMBuilder.Inliner = createAlwaysInlinerPass(false); 313 else 314 PMBuilder.Inliner = createAlwaysInlinerPass(); 315 break; 316 } 317 318 PMBuilder.OptLevel = OptLevel; 319 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; 320 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB; 321 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP; 322 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop; 323 324 PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime; 325 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 326 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; 327 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO; 328 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; 329 330 legacy::PassManager *MPM = getPerModulePasses(); 331 332 // If we are performing a ThinLTO importing compile, invoke the LTO 333 // pipeline and pass down the in-memory function index. 334 if (FunctionIndex) { 335 PMBuilder.FunctionIndex = FunctionIndex; 336 PMBuilder.populateLTOPassManager(*MPM); 337 return; 338 } 339 340 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 341 addAddDiscriminatorsPass); 342 343 // In ObjC ARC mode, add the main ARC optimization passes. 344 if (LangOpts.ObjCAutoRefCount) { 345 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 346 addObjCARCExpandPass); 347 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly, 348 addObjCARCAPElimPass); 349 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 350 addObjCARCOptPass); 351 } 352 353 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) { 354 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 355 addBoundsCheckingPass); 356 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 357 addBoundsCheckingPass); 358 } 359 360 if (CodeGenOpts.SanitizeCoverageType || 361 CodeGenOpts.SanitizeCoverageIndirectCalls || 362 CodeGenOpts.SanitizeCoverageTraceCmp) { 363 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 364 addSanitizerCoveragePass); 365 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 366 addSanitizerCoveragePass); 367 } 368 369 if (LangOpts.Sanitize.has(SanitizerKind::Address)) { 370 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 371 addAddressSanitizerPasses); 372 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 373 addAddressSanitizerPasses); 374 } 375 376 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) { 377 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 378 addKernelAddressSanitizerPasses); 379 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 380 addKernelAddressSanitizerPasses); 381 } 382 383 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) { 384 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 385 addMemorySanitizerPass); 386 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 387 addMemorySanitizerPass); 388 } 389 390 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) { 391 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 392 addThreadSanitizerPass); 393 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 394 addThreadSanitizerPass); 395 } 396 397 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) { 398 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 399 addDataFlowSanitizerPass); 400 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 401 addDataFlowSanitizerPass); 402 } 403 404 // Set up the per-function pass manager. 405 legacy::FunctionPassManager *FPM = getPerFunctionPasses(); 406 if (CodeGenOpts.VerifyModule) 407 FPM->add(createVerifierPass()); 408 PMBuilder.populateFunctionPassManager(*FPM); 409 410 // Set up the per-module pass manager. 411 if (!CodeGenOpts.RewriteMapFiles.empty()) 412 addSymbolRewriterPass(CodeGenOpts, MPM); 413 414 if (!CodeGenOpts.DisableGCov && 415 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) { 416 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if 417 // LLVM's -default-gcov-version flag is set to something invalid. 418 GCOVOptions Options; 419 Options.EmitNotes = CodeGenOpts.EmitGcovNotes; 420 Options.EmitData = CodeGenOpts.EmitGcovArcs; 421 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4); 422 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum; 423 Options.NoRedZone = CodeGenOpts.DisableRedZone; 424 Options.FunctionNamesInData = 425 !CodeGenOpts.CoverageNoFunctionNamesInData; 426 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody; 427 MPM->add(createGCOVProfilerPass(Options)); 428 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo) 429 MPM->add(createStripSymbolsPass(true)); 430 } 431 432 if (CodeGenOpts.hasProfileClangInstr()) { 433 InstrProfOptions Options; 434 Options.NoRedZone = CodeGenOpts.DisableRedZone; 435 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; 436 MPM->add(createInstrProfilingPass(Options)); 437 } 438 439 if (!CodeGenOpts.SampleProfileFile.empty()) 440 MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile)); 441 442 PMBuilder.populateModulePassManager(*MPM); 443 } 444 445 TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 446 // Create the TargetMachine for generating code. 447 std::string Error; 448 std::string Triple = TheModule->getTargetTriple(); 449 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 450 if (!TheTarget) { 451 if (MustCreateTM) 452 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 453 return nullptr; 454 } 455 456 unsigned CodeModel = 457 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel) 458 .Case("small", llvm::CodeModel::Small) 459 .Case("kernel", llvm::CodeModel::Kernel) 460 .Case("medium", llvm::CodeModel::Medium) 461 .Case("large", llvm::CodeModel::Large) 462 .Case("default", llvm::CodeModel::Default) 463 .Default(~0u); 464 assert(CodeModel != ~0u && "invalid code model!"); 465 llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel); 466 467 SmallVector<const char *, 16> BackendArgs; 468 BackendArgs.push_back("clang"); // Fake program name. 469 if (!CodeGenOpts.DebugPass.empty()) { 470 BackendArgs.push_back("-debug-pass"); 471 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 472 } 473 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 474 BackendArgs.push_back("-limit-float-precision"); 475 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 476 } 477 for (const std::string &BackendOption : CodeGenOpts.BackendOptions) 478 BackendArgs.push_back(BackendOption.c_str()); 479 BackendArgs.push_back(nullptr); 480 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 481 BackendArgs.data()); 482 483 std::string FeaturesStr = 484 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ","); 485 486 // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp. 487 llvm::Reloc::Model RM = llvm::Reloc::Default; 488 if (CodeGenOpts.RelocationModel == "static") { 489 RM = llvm::Reloc::Static; 490 } else if (CodeGenOpts.RelocationModel == "pic") { 491 RM = llvm::Reloc::PIC_; 492 } else { 493 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && 494 "Invalid PIC model!"); 495 RM = llvm::Reloc::DynamicNoPIC; 496 } 497 498 CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 499 switch (CodeGenOpts.OptimizationLevel) { 500 default: break; 501 case 0: OptLevel = CodeGenOpt::None; break; 502 case 3: OptLevel = CodeGenOpt::Aggressive; break; 503 } 504 505 llvm::TargetOptions Options; 506 507 if (!TargetOpts.Reciprocals.empty()) 508 Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals); 509 510 Options.ThreadModel = 511 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel) 512 .Case("posix", llvm::ThreadModel::POSIX) 513 .Case("single", llvm::ThreadModel::Single); 514 515 // Set float ABI type. 516 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" || 517 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) && 518 "Invalid Floating Point ABI!"); 519 Options.FloatABIType = 520 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI) 521 .Case("soft", llvm::FloatABI::Soft) 522 .Case("softfp", llvm::FloatABI::Soft) 523 .Case("hard", llvm::FloatABI::Hard) 524 .Default(llvm::FloatABI::Default); 525 526 // Set FP fusion mode. 527 switch (CodeGenOpts.getFPContractMode()) { 528 case CodeGenOptions::FPC_Off: 529 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict; 530 break; 531 case CodeGenOptions::FPC_On: 532 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 533 break; 534 case CodeGenOptions::FPC_Fast: 535 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 536 break; 537 } 538 539 Options.UseInitArray = CodeGenOpts.UseInitArray; 540 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS; 541 Options.CompressDebugSections = CodeGenOpts.CompressDebugSections; 542 543 // Set EABI version. 544 Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(CodeGenOpts.EABIVersion) 545 .Case("4", llvm::EABI::EABI4) 546 .Case("5", llvm::EABI::EABI5) 547 .Case("gnu", llvm::EABI::GNU) 548 .Default(llvm::EABI::Default); 549 550 Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD; 551 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 552 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 553 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 554 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 555 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment; 556 Options.PositionIndependentExecutable = LangOpts.PIELevel != 0; 557 Options.FunctionSections = CodeGenOpts.FunctionSections; 558 Options.DataSections = CodeGenOpts.DataSections; 559 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames; 560 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS; 561 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning(); 562 563 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll; 564 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels; 565 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm; 566 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack; 567 Options.MCOptions.MCIncrementalLinkerCompatible = 568 CodeGenOpts.IncrementalLinkerCompatible; 569 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings; 570 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose; 571 Options.MCOptions.ABIName = TargetOpts.ABI; 572 573 TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU, 574 FeaturesStr, Options, 575 RM, CM, OptLevel); 576 577 return TM; 578 } 579 580 bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action, 581 raw_pwrite_stream &OS) { 582 583 // Create the code generator passes. 584 legacy::PassManager *PM = getCodeGenPasses(); 585 586 // Add LibraryInfo. 587 llvm::Triple TargetTriple(TheModule->getTargetTriple()); 588 std::unique_ptr<TargetLibraryInfoImpl> TLII( 589 createTLII(TargetTriple, CodeGenOpts)); 590 PM->add(new TargetLibraryInfoWrapperPass(*TLII)); 591 592 // Normal mode, emit a .s or .o file by running the code generator. Note, 593 // this also adds codegenerator level optimization passes. 594 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; 595 if (Action == Backend_EmitObj) 596 CGFT = TargetMachine::CGFT_ObjectFile; 597 else if (Action == Backend_EmitMCNull) 598 CGFT = TargetMachine::CGFT_Null; 599 else 600 assert(Action == Backend_EmitAssembly && "Invalid action!"); 601 602 // Add ObjC ARC final-cleanup optimizations. This is done as part of the 603 // "codegen" passes so that it isn't run multiple times when there is 604 // inlining happening. 605 if (CodeGenOpts.OptimizationLevel > 0) 606 PM->add(createObjCARCContractPass()); 607 608 if (TM->addPassesToEmitFile(*PM, OS, CGFT, 609 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 610 Diags.Report(diag::err_fe_unable_to_interface_with_target); 611 return false; 612 } 613 614 return true; 615 } 616 617 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, 618 raw_pwrite_stream *OS) { 619 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr); 620 621 bool UsesCodeGen = (Action != Backend_EmitNothing && 622 Action != Backend_EmitBC && 623 Action != Backend_EmitLL); 624 if (!TM) 625 TM.reset(CreateTargetMachine(UsesCodeGen)); 626 627 if (UsesCodeGen && !TM) 628 return; 629 if (TM) 630 TheModule->setDataLayout(TM->createDataLayout()); 631 632 // If we are performing a ThinLTO importing compile, load the function 633 // index into memory and pass it into CreatePasses, which will add it 634 // to the PassManagerBuilder and invoke LTO passes. 635 std::unique_ptr<FunctionInfoIndex> FunctionIndex; 636 if (!CodeGenOpts.ThinLTOIndexFile.empty()) { 637 ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = 638 llvm::getFunctionIndexForFile(CodeGenOpts.ThinLTOIndexFile, 639 [&](const DiagnosticInfo &DI) { 640 TheModule->getContext().diagnose(DI); 641 }); 642 if (std::error_code EC = IndexOrErr.getError()) { 643 std::string Error = EC.message(); 644 errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile 645 << "': " << Error << "\n"; 646 return; 647 } 648 FunctionIndex = std::move(IndexOrErr.get()); 649 assert(FunctionIndex && "Expected non-empty function index"); 650 } 651 652 CreatePasses(FunctionIndex.get()); 653 654 switch (Action) { 655 case Backend_EmitNothing: 656 break; 657 658 case Backend_EmitBC: 659 getPerModulePasses()->add(createBitcodeWriterPass( 660 *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitFunctionSummary)); 661 break; 662 663 case Backend_EmitLL: 664 getPerModulePasses()->add( 665 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists)); 666 break; 667 668 default: 669 if (!AddEmitPasses(Action, *OS)) 670 return; 671 } 672 673 // Before executing passes, print the final values of the LLVM options. 674 cl::PrintOptionValues(); 675 676 // Run passes. For now we do all passes at once, but eventually we 677 // would like to have the option of streaming code generation. 678 679 if (PerFunctionPasses) { 680 PrettyStackTraceString CrashInfo("Per-function optimization"); 681 682 PerFunctionPasses->doInitialization(); 683 for (Function &F : *TheModule) 684 if (!F.isDeclaration()) 685 PerFunctionPasses->run(F); 686 PerFunctionPasses->doFinalization(); 687 } 688 689 if (PerModulePasses) { 690 PrettyStackTraceString CrashInfo("Per-module optimization passes"); 691 PerModulePasses->run(*TheModule); 692 } 693 694 if (CodeGenPasses) { 695 PrettyStackTraceString CrashInfo("Code generation"); 696 CodeGenPasses->run(*TheModule); 697 } 698 } 699 700 void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 701 const CodeGenOptions &CGOpts, 702 const clang::TargetOptions &TOpts, 703 const LangOptions &LOpts, StringRef TDesc, 704 Module *M, BackendAction Action, 705 raw_pwrite_stream *OS) { 706 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); 707 708 AsmHelper.EmitAssembly(Action, OS); 709 710 // If an optional clang TargetInfo description string was passed in, use it to 711 // verify the LLVM TargetMachine's DataLayout. 712 if (AsmHelper.TM && !TDesc.empty()) { 713 std::string DLDesc = M->getDataLayout().getStringRepresentation(); 714 if (DLDesc != TDesc) { 715 unsigned DiagID = Diags.getCustomDiagID( 716 DiagnosticsEngine::Error, "backend data layout '%0' does not match " 717 "expected target description '%1'"); 718 Diags.Report(DiagID) << DLDesc << TDesc; 719 } 720 } 721 } 722