1 //===-- CommandFlags.cpp - Command Line Flags Interface ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains codegen-specific flags that are shared between different 10 // command line tools. The tools "llc" and "opt" both use this file to prevent 11 // flag duplication. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/CommandFlags.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/MC/SubtargetFeature.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Support/Host.h" 20 21 using namespace llvm; 22 23 #define CGOPT(TY, NAME) \ 24 static cl::opt<TY> *NAME##View; \ 25 TY codegen::get##NAME() { \ 26 assert(NAME##View && "RegisterCodeGenFlags not created."); \ 27 return *NAME##View; \ 28 } 29 30 #define CGLIST(TY, NAME) \ 31 static cl::list<TY> *NAME##View; \ 32 std::vector<TY> codegen::get##NAME() { \ 33 assert(NAME##View && "RegisterCodeGenFlags not created."); \ 34 return *NAME##View; \ 35 } 36 37 #define CGOPT_EXP(TY, NAME) \ 38 CGOPT(TY, NAME) \ 39 Optional<TY> codegen::getExplicit##NAME() { \ 40 if (NAME##View->getNumOccurrences()) { \ 41 TY res = *NAME##View; \ 42 return res; \ 43 } \ 44 return None; \ 45 } 46 47 CGOPT(std::string, MArch) 48 CGOPT(std::string, MCPU) 49 CGLIST(std::string, MAttrs) 50 CGOPT_EXP(Reloc::Model, RelocModel) 51 CGOPT(ThreadModel::Model, ThreadModel) 52 CGOPT_EXP(CodeModel::Model, CodeModel) 53 CGOPT(ExceptionHandling, ExceptionModel) 54 CGOPT_EXP(CodeGenFileType, FileType) 55 CGOPT(FramePointer::FP, FramePointerUsage) 56 CGOPT(bool, EnableUnsafeFPMath) 57 CGOPT(bool, EnableNoInfsFPMath) 58 CGOPT(bool, EnableNoNaNsFPMath) 59 CGOPT(bool, EnableNoSignedZerosFPMath) 60 CGOPT(bool, EnableNoTrappingFPMath) 61 CGOPT(bool, EnableAIXExtendedAltivecABI) 62 CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath) 63 CGOPT(DenormalMode::DenormalModeKind, DenormalFP32Math) 64 CGOPT(bool, EnableHonorSignDependentRoundingFPMath) 65 CGOPT(FloatABI::ABIType, FloatABIForCalls) 66 CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps) 67 CGOPT(bool, DontPlaceZerosInBSS) 68 CGOPT(bool, EnableGuaranteedTailCallOpt) 69 CGOPT(bool, DisableTailCalls) 70 CGOPT(bool, StackSymbolOrdering) 71 CGOPT(unsigned, OverrideStackAlignment) 72 CGOPT(bool, StackRealign) 73 CGOPT(std::string, TrapFuncName) 74 CGOPT(bool, UseCtors) 75 CGOPT(bool, RelaxELFRelocations) 76 CGOPT_EXP(bool, DataSections) 77 CGOPT_EXP(bool, FunctionSections) 78 CGOPT(bool, IgnoreXCOFFVisibility) 79 CGOPT(std::string, BBSections) 80 CGOPT(std::string, StackProtectorGuard) 81 CGOPT(unsigned, StackProtectorGuardOffset) 82 CGOPT(std::string, StackProtectorGuardReg) 83 CGOPT(unsigned, TLSSize) 84 CGOPT(bool, EmulatedTLS) 85 CGOPT(bool, UniqueSectionNames) 86 CGOPT(bool, UniqueBasicBlockSectionNames) 87 CGOPT(EABI, EABIVersion) 88 CGOPT(DebuggerKind, DebuggerTuningOpt) 89 CGOPT(bool, EnableStackSizeSection) 90 CGOPT(bool, EnableAddrsig) 91 CGOPT(bool, EmitCallSiteInfo) 92 CGOPT(bool, EnableMachineFunctionSplitter) 93 CGOPT(bool, EnableDebugEntryValues) 94 CGOPT(bool, ValueTrackingVariableLocations) 95 CGOPT(bool, ForceDwarfFrameSection) 96 CGOPT(bool, XRayOmitFunctionIndex) 97 98 codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() { 99 #define CGBINDOPT(NAME) \ 100 do { \ 101 NAME##View = std::addressof(NAME); \ 102 } while (0) 103 104 static cl::opt<std::string> MArch( 105 "march", cl::desc("Architecture to generate code for (see --version)")); 106 CGBINDOPT(MArch); 107 108 static cl::opt<std::string> MCPU( 109 "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"), 110 cl::value_desc("cpu-name"), cl::init("")); 111 CGBINDOPT(MCPU); 112 113 static cl::list<std::string> MAttrs( 114 "mattr", cl::CommaSeparated, 115 cl::desc("Target specific attributes (-mattr=help for details)"), 116 cl::value_desc("a1,+a2,-a3,...")); 117 CGBINDOPT(MAttrs); 118 119 static cl::opt<Reloc::Model> RelocModel( 120 "relocation-model", cl::desc("Choose relocation model"), 121 cl::values( 122 clEnumValN(Reloc::Static, "static", "Non-relocatable code"), 123 clEnumValN(Reloc::PIC_, "pic", 124 "Fully relocatable, position independent code"), 125 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", 126 "Relocatable external references, non-relocatable code"), 127 clEnumValN( 128 Reloc::ROPI, "ropi", 129 "Code and read-only data relocatable, accessed PC-relative"), 130 clEnumValN( 131 Reloc::RWPI, "rwpi", 132 "Read-write data relocatable, accessed relative to static base"), 133 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi", 134 "Combination of ropi and rwpi"))); 135 CGBINDOPT(RelocModel); 136 137 static cl::opt<ThreadModel::Model> ThreadModel( 138 "thread-model", cl::desc("Choose threading model"), 139 cl::init(ThreadModel::POSIX), 140 cl::values( 141 clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"), 142 clEnumValN(ThreadModel::Single, "single", "Single thread model"))); 143 CGBINDOPT(ThreadModel); 144 145 static cl::opt<CodeModel::Model> CodeModel( 146 "code-model", cl::desc("Choose code model"), 147 cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"), 148 clEnumValN(CodeModel::Small, "small", "Small code model"), 149 clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"), 150 clEnumValN(CodeModel::Medium, "medium", "Medium code model"), 151 clEnumValN(CodeModel::Large, "large", "Large code model"))); 152 CGBINDOPT(CodeModel); 153 154 static cl::opt<ExceptionHandling> ExceptionModel( 155 "exception-model", cl::desc("exception model"), 156 cl::init(ExceptionHandling::None), 157 cl::values( 158 clEnumValN(ExceptionHandling::None, "default", 159 "default exception handling model"), 160 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf", 161 "DWARF-like CFI based exception handling"), 162 clEnumValN(ExceptionHandling::SjLj, "sjlj", 163 "SjLj exception handling"), 164 clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"), 165 clEnumValN(ExceptionHandling::WinEH, "wineh", 166 "Windows exception model"), 167 clEnumValN(ExceptionHandling::Wasm, "wasm", 168 "WebAssembly exception handling"))); 169 CGBINDOPT(ExceptionModel); 170 171 static cl::opt<CodeGenFileType> FileType( 172 "filetype", cl::init(CGFT_AssemblyFile), 173 cl::desc( 174 "Choose a file type (not all types are supported by all targets):"), 175 cl::values( 176 clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"), 177 clEnumValN(CGFT_ObjectFile, "obj", 178 "Emit a native object ('.o') file"), 179 clEnumValN(CGFT_Null, "null", 180 "Emit nothing, for performance testing"))); 181 CGBINDOPT(FileType); 182 183 static cl::opt<FramePointer::FP> FramePointerUsage( 184 "frame-pointer", 185 cl::desc("Specify frame pointer elimination optimization"), 186 cl::init(FramePointer::None), 187 cl::values( 188 clEnumValN(FramePointer::All, "all", 189 "Disable frame pointer elimination"), 190 clEnumValN(FramePointer::NonLeaf, "non-leaf", 191 "Disable frame pointer elimination for non-leaf frame"), 192 clEnumValN(FramePointer::None, "none", 193 "Enable frame pointer elimination"))); 194 CGBINDOPT(FramePointerUsage); 195 196 static cl::opt<bool> EnableUnsafeFPMath( 197 "enable-unsafe-fp-math", 198 cl::desc("Enable optimizations that may decrease FP precision"), 199 cl::init(false)); 200 CGBINDOPT(EnableUnsafeFPMath); 201 202 static cl::opt<bool> EnableNoInfsFPMath( 203 "enable-no-infs-fp-math", 204 cl::desc("Enable FP math optimizations that assume no +-Infs"), 205 cl::init(false)); 206 CGBINDOPT(EnableNoInfsFPMath); 207 208 static cl::opt<bool> EnableNoNaNsFPMath( 209 "enable-no-nans-fp-math", 210 cl::desc("Enable FP math optimizations that assume no NaNs"), 211 cl::init(false)); 212 CGBINDOPT(EnableNoNaNsFPMath); 213 214 static cl::opt<bool> EnableNoSignedZerosFPMath( 215 "enable-no-signed-zeros-fp-math", 216 cl::desc("Enable FP math optimizations that assume " 217 "the sign of 0 is insignificant"), 218 cl::init(false)); 219 CGBINDOPT(EnableNoSignedZerosFPMath); 220 221 static cl::opt<bool> EnableNoTrappingFPMath( 222 "enable-no-trapping-fp-math", 223 cl::desc("Enable setting the FP exceptions build " 224 "attribute not to use exceptions"), 225 cl::init(false)); 226 CGBINDOPT(EnableNoTrappingFPMath); 227 228 static const auto DenormFlagEnumOptions = 229 cl::values(clEnumValN(DenormalMode::IEEE, "ieee", 230 "IEEE 754 denormal numbers"), 231 clEnumValN(DenormalMode::PreserveSign, "preserve-sign", 232 "the sign of a flushed-to-zero number is preserved " 233 "in the sign of 0"), 234 clEnumValN(DenormalMode::PositiveZero, "positive-zero", 235 "denormals are flushed to positive zero")); 236 237 // FIXME: Doesn't have way to specify separate input and output modes. 238 static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath( 239 "denormal-fp-math", 240 cl::desc("Select which denormal numbers the code is permitted to require"), 241 cl::init(DenormalMode::IEEE), 242 DenormFlagEnumOptions); 243 CGBINDOPT(DenormalFPMath); 244 245 static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math( 246 "denormal-fp-math-f32", 247 cl::desc("Select which denormal numbers the code is permitted to require for float"), 248 cl::init(DenormalMode::Invalid), 249 DenormFlagEnumOptions); 250 CGBINDOPT(DenormalFP32Math); 251 252 static cl::opt<bool> EnableHonorSignDependentRoundingFPMath( 253 "enable-sign-dependent-rounding-fp-math", cl::Hidden, 254 cl::desc("Force codegen to assume rounding mode can change dynamically"), 255 cl::init(false)); 256 CGBINDOPT(EnableHonorSignDependentRoundingFPMath); 257 258 static cl::opt<FloatABI::ABIType> FloatABIForCalls( 259 "float-abi", cl::desc("Choose float ABI type"), 260 cl::init(FloatABI::Default), 261 cl::values(clEnumValN(FloatABI::Default, "default", 262 "Target default float ABI type"), 263 clEnumValN(FloatABI::Soft, "soft", 264 "Soft float ABI (implied by -soft-float)"), 265 clEnumValN(FloatABI::Hard, "hard", 266 "Hard float ABI (uses FP registers)"))); 267 CGBINDOPT(FloatABIForCalls); 268 269 static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps( 270 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"), 271 cl::init(FPOpFusion::Standard), 272 cl::values( 273 clEnumValN(FPOpFusion::Fast, "fast", 274 "Fuse FP ops whenever profitable"), 275 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."), 276 clEnumValN(FPOpFusion::Strict, "off", 277 "Only fuse FP ops when the result won't be affected."))); 278 CGBINDOPT(FuseFPOps); 279 280 static cl::opt<bool> DontPlaceZerosInBSS( 281 "nozero-initialized-in-bss", 282 cl::desc("Don't place zero-initialized symbols into bss section"), 283 cl::init(false)); 284 CGBINDOPT(DontPlaceZerosInBSS); 285 286 static cl::opt<bool> EnableAIXExtendedAltivecABI( 287 "vec-extabi", cl::desc("Enable the AIX Extended Altivec ABI."), 288 cl::init(false)); 289 CGBINDOPT(EnableAIXExtendedAltivecABI); 290 291 static cl::opt<bool> EnableGuaranteedTailCallOpt( 292 "tailcallopt", 293 cl::desc( 294 "Turn fastcc calls into tail calls by (potentially) changing ABI."), 295 cl::init(false)); 296 CGBINDOPT(EnableGuaranteedTailCallOpt); 297 298 static cl::opt<bool> DisableTailCalls( 299 "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false)); 300 CGBINDOPT(DisableTailCalls); 301 302 static cl::opt<bool> StackSymbolOrdering( 303 "stack-symbol-ordering", cl::desc("Order local stack symbols."), 304 cl::init(true)); 305 CGBINDOPT(StackSymbolOrdering); 306 307 static cl::opt<unsigned> OverrideStackAlignment( 308 "stack-alignment", cl::desc("Override default stack alignment"), 309 cl::init(0)); 310 CGBINDOPT(OverrideStackAlignment); 311 312 static cl::opt<bool> StackRealign( 313 "stackrealign", 314 cl::desc("Force align the stack to the minimum alignment"), 315 cl::init(false)); 316 CGBINDOPT(StackRealign); 317 318 static cl::opt<std::string> TrapFuncName( 319 "trap-func", cl::Hidden, 320 cl::desc("Emit a call to trap function rather than a trap instruction"), 321 cl::init("")); 322 CGBINDOPT(TrapFuncName); 323 324 static cl::opt<bool> UseCtors("use-ctors", 325 cl::desc("Use .ctors instead of .init_array."), 326 cl::init(false)); 327 CGBINDOPT(UseCtors); 328 329 static cl::opt<bool> RelaxELFRelocations( 330 "relax-elf-relocations", 331 cl::desc( 332 "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"), 333 cl::init(false)); 334 CGBINDOPT(RelaxELFRelocations); 335 336 static cl::opt<bool> DataSections( 337 "data-sections", cl::desc("Emit data into separate sections"), 338 cl::init(false)); 339 CGBINDOPT(DataSections); 340 341 static cl::opt<bool> FunctionSections( 342 "function-sections", cl::desc("Emit functions into separate sections"), 343 cl::init(false)); 344 CGBINDOPT(FunctionSections); 345 346 static cl::opt<bool> IgnoreXCOFFVisibility( 347 "ignore-xcoff-visibility", 348 cl::desc("Not emit the visibility attribute for asm in AIX OS or give " 349 "all symbols 'unspecified' visibility in XCOFF object file"), 350 cl::init(false)); 351 CGBINDOPT(IgnoreXCOFFVisibility); 352 353 static cl::opt<std::string> BBSections( 354 "basic-block-sections", 355 cl::desc("Emit basic blocks into separate sections"), 356 cl::value_desc("all | <function list (file)> | labels | none"), 357 cl::init("none")); 358 CGBINDOPT(BBSections); 359 360 static cl::opt<std::string> StackProtectorGuard( 361 "stack-protector-guard", cl::desc("Stack protector guard mode"), 362 cl::init("none")); 363 CGBINDOPT(StackProtectorGuard); 364 365 static cl::opt<std::string> StackProtectorGuardReg( 366 "stack-protector-guard-reg", cl::desc("Stack protector guard register"), 367 cl::init("none")); 368 CGBINDOPT(StackProtectorGuardReg); 369 370 static cl::opt<unsigned> StackProtectorGuardOffset( 371 "stack-protector-guard-offset", cl::desc("Stack protector guard offset"), 372 cl::init((unsigned)-1)); 373 CGBINDOPT(StackProtectorGuardOffset); 374 375 static cl::opt<unsigned> TLSSize( 376 "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0)); 377 CGBINDOPT(TLSSize); 378 379 static cl::opt<bool> EmulatedTLS( 380 "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false)); 381 CGBINDOPT(EmulatedTLS); 382 383 static cl::opt<bool> UniqueSectionNames( 384 "unique-section-names", cl::desc("Give unique names to every section"), 385 cl::init(true)); 386 CGBINDOPT(UniqueSectionNames); 387 388 static cl::opt<bool> UniqueBasicBlockSectionNames( 389 "unique-basic-block-section-names", 390 cl::desc("Give unique names to every basic block section"), 391 cl::init(false)); 392 CGBINDOPT(UniqueBasicBlockSectionNames); 393 394 static cl::opt<EABI> EABIVersion( 395 "meabi", cl::desc("Set EABI type (default depends on triple):"), 396 cl::init(EABI::Default), 397 cl::values( 398 clEnumValN(EABI::Default, "default", "Triple default EABI version"), 399 clEnumValN(EABI::EABI4, "4", "EABI version 4"), 400 clEnumValN(EABI::EABI5, "5", "EABI version 5"), 401 clEnumValN(EABI::GNU, "gnu", "EABI GNU"))); 402 CGBINDOPT(EABIVersion); 403 404 static cl::opt<DebuggerKind> DebuggerTuningOpt( 405 "debugger-tune", cl::desc("Tune debug info for a particular debugger"), 406 cl::init(DebuggerKind::Default), 407 cl::values( 408 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"), 409 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"), 410 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)"))); 411 CGBINDOPT(DebuggerTuningOpt); 412 413 static cl::opt<bool> EnableStackSizeSection( 414 "stack-size-section", 415 cl::desc("Emit a section containing stack size metadata"), 416 cl::init(false)); 417 CGBINDOPT(EnableStackSizeSection); 418 419 static cl::opt<bool> EnableAddrsig( 420 "addrsig", cl::desc("Emit an address-significance table"), 421 cl::init(false)); 422 CGBINDOPT(EnableAddrsig); 423 424 static cl::opt<bool> EmitCallSiteInfo( 425 "emit-call-site-info", 426 cl::desc( 427 "Emit call site debug information, if debug information is enabled."), 428 cl::init(false)); 429 CGBINDOPT(EmitCallSiteInfo); 430 431 static cl::opt<bool> EnableDebugEntryValues( 432 "debug-entry-values", 433 cl::desc("Enable debug info for the debug entry values."), 434 cl::init(false)); 435 CGBINDOPT(EnableDebugEntryValues); 436 437 static cl::opt<bool> ValueTrackingVariableLocations( 438 "experimental-debug-variable-locations", 439 cl::desc("Use experimental new value-tracking variable locations"), 440 cl::init(false)); 441 CGBINDOPT(ValueTrackingVariableLocations); 442 443 static cl::opt<bool> EnableMachineFunctionSplitter( 444 "split-machine-functions", 445 cl::desc("Split out cold basic blocks from machine functions based on " 446 "profile information"), 447 cl::init(false)); 448 CGBINDOPT(EnableMachineFunctionSplitter); 449 450 static cl::opt<bool> ForceDwarfFrameSection( 451 "force-dwarf-frame-section", 452 cl::desc("Always emit a debug frame section."), cl::init(false)); 453 CGBINDOPT(ForceDwarfFrameSection); 454 455 static cl::opt<bool> XRayOmitFunctionIndex( 456 "no-xray-index", cl::desc("Don't emit xray_fn_idx section"), 457 cl::init(false)); 458 CGBINDOPT(XRayOmitFunctionIndex); 459 460 #undef CGBINDOPT 461 462 mc::RegisterMCTargetOptionsFlags(); 463 } 464 465 llvm::BasicBlockSection 466 codegen::getBBSectionsMode(llvm::TargetOptions &Options) { 467 if (getBBSections() == "all") 468 return BasicBlockSection::All; 469 else if (getBBSections() == "labels") 470 return BasicBlockSection::Labels; 471 else if (getBBSections() == "none") 472 return BasicBlockSection::None; 473 else { 474 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 475 MemoryBuffer::getFile(getBBSections()); 476 if (!MBOrErr) { 477 errs() << "Error loading basic block sections function list file: " 478 << MBOrErr.getError().message() << "\n"; 479 } else { 480 Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 481 } 482 return BasicBlockSection::List; 483 } 484 } 485 486 llvm::StackProtectorGuards 487 codegen::getStackProtectorGuardMode(llvm::TargetOptions &Options) { 488 if (getStackProtectorGuard() == "tls") 489 return StackProtectorGuards::TLS; 490 if (getStackProtectorGuard() == "global") 491 return StackProtectorGuards::Global; 492 if (getStackProtectorGuard() != "none") { 493 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 494 MemoryBuffer::getFile(getStackProtectorGuard()); 495 if (!MBOrErr) 496 errs() << "error illegal stack protector guard mode: " 497 << MBOrErr.getError().message() << "\n"; 498 else 499 Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 500 } 501 return StackProtectorGuards::None; 502 } 503 504 // Common utility function tightly tied to the options listed here. Initializes 505 // a TargetOptions object with CodeGen flags and returns it. 506 TargetOptions 507 codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) { 508 TargetOptions Options; 509 Options.AllowFPOpFusion = getFuseFPOps(); 510 Options.UnsafeFPMath = getEnableUnsafeFPMath(); 511 Options.NoInfsFPMath = getEnableNoInfsFPMath(); 512 Options.NoNaNsFPMath = getEnableNoNaNsFPMath(); 513 Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath(); 514 Options.NoTrappingFPMath = getEnableNoTrappingFPMath(); 515 516 DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath(); 517 518 // FIXME: Should have separate input and output flags 519 Options.setFPDenormalMode(DenormalMode(DenormKind, DenormKind)); 520 521 Options.HonorSignDependentRoundingFPMathOption = 522 getEnableHonorSignDependentRoundingFPMath(); 523 if (getFloatABIForCalls() != FloatABI::Default) 524 Options.FloatABIType = getFloatABIForCalls(); 525 Options.EnableAIXExtendedAltivecABI = getEnableAIXExtendedAltivecABI(); 526 Options.NoZerosInBSS = getDontPlaceZerosInBSS(); 527 Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt(); 528 Options.StackAlignmentOverride = getOverrideStackAlignment(); 529 Options.StackSymbolOrdering = getStackSymbolOrdering(); 530 Options.UseInitArray = !getUseCtors(); 531 Options.RelaxELFRelocations = getRelaxELFRelocations(); 532 Options.DataSections = 533 getExplicitDataSections().getValueOr(TheTriple.hasDefaultDataSections()); 534 Options.FunctionSections = getFunctionSections(); 535 Options.IgnoreXCOFFVisibility = getIgnoreXCOFFVisibility(); 536 Options.BBSections = getBBSectionsMode(Options); 537 Options.UniqueSectionNames = getUniqueSectionNames(); 538 Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames(); 539 Options.StackProtectorGuard = getStackProtectorGuardMode(Options); 540 Options.StackProtectorGuardOffset = getStackProtectorGuardOffset(); 541 Options.StackProtectorGuardReg = getStackProtectorGuardReg(); 542 Options.TLSSize = getTLSSize(); 543 Options.EmulatedTLS = getEmulatedTLS(); 544 Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0; 545 Options.ExceptionModel = getExceptionModel(); 546 Options.EmitStackSizeSection = getEnableStackSizeSection(); 547 Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter(); 548 Options.EmitAddrsig = getEnableAddrsig(); 549 Options.EmitCallSiteInfo = getEmitCallSiteInfo(); 550 Options.EnableDebugEntryValues = getEnableDebugEntryValues(); 551 Options.ValueTrackingVariableLocations = getValueTrackingVariableLocations(); 552 Options.ForceDwarfFrameSection = getForceDwarfFrameSection(); 553 Options.XRayOmitFunctionIndex = getXRayOmitFunctionIndex(); 554 555 Options.MCOptions = mc::InitMCTargetOptionsFromFlags(); 556 557 Options.ThreadModel = getThreadModel(); 558 Options.EABIVersion = getEABIVersion(); 559 Options.DebuggerTuning = getDebuggerTuningOpt(); 560 561 return Options; 562 } 563 564 std::string codegen::getCPUStr() { 565 // If user asked for the 'native' CPU, autodetect here. If autodection fails, 566 // this will set the CPU to an empty string which tells the target to 567 // pick a basic default. 568 if (getMCPU() == "native") 569 return std::string(sys::getHostCPUName()); 570 571 return getMCPU(); 572 } 573 574 std::string codegen::getFeaturesStr() { 575 SubtargetFeatures Features; 576 577 // If user asked for the 'native' CPU, we need to autodetect features. 578 // This is necessary for x86 where the CPU might not support all the 579 // features the autodetected CPU name lists in the target. For example, 580 // not all Sandybridge processors support AVX. 581 if (getMCPU() == "native") { 582 StringMap<bool> HostFeatures; 583 if (sys::getHostCPUFeatures(HostFeatures)) 584 for (auto &F : HostFeatures) 585 Features.AddFeature(F.first(), F.second); 586 } 587 588 for (auto const &MAttr : getMAttrs()) 589 Features.AddFeature(MAttr); 590 591 return Features.getString(); 592 } 593 594 std::vector<std::string> codegen::getFeatureList() { 595 SubtargetFeatures Features; 596 597 // If user asked for the 'native' CPU, we need to autodetect features. 598 // This is necessary for x86 where the CPU might not support all the 599 // features the autodetected CPU name lists in the target. For example, 600 // not all Sandybridge processors support AVX. 601 if (getMCPU() == "native") { 602 StringMap<bool> HostFeatures; 603 if (sys::getHostCPUFeatures(HostFeatures)) 604 for (auto &F : HostFeatures) 605 Features.AddFeature(F.first(), F.second); 606 } 607 608 for (auto const &MAttr : getMAttrs()) 609 Features.AddFeature(MAttr); 610 611 return Features.getFeatures(); 612 } 613 614 void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) { 615 B.addAttribute(Name, Val ? "true" : "false"); 616 } 617 618 #define HANDLE_BOOL_ATTR(CL, AttrName) \ 619 do { \ 620 if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \ 621 renderBoolStringAttr(NewAttrs, AttrName, *CL); \ 622 } while (0) 623 624 /// Set function attributes of function \p F based on CPU, Features, and command 625 /// line flags. 626 void codegen::setFunctionAttributes(StringRef CPU, StringRef Features, 627 Function &F) { 628 auto &Ctx = F.getContext(); 629 AttributeList Attrs = F.getAttributes(); 630 AttrBuilder NewAttrs; 631 632 if (!CPU.empty() && !F.hasFnAttribute("target-cpu")) 633 NewAttrs.addAttribute("target-cpu", CPU); 634 if (!Features.empty()) { 635 // Append the command line features to any that are already on the function. 636 StringRef OldFeatures = 637 F.getFnAttribute("target-features").getValueAsString(); 638 if (OldFeatures.empty()) 639 NewAttrs.addAttribute("target-features", Features); 640 else { 641 SmallString<256> Appended(OldFeatures); 642 Appended.push_back(','); 643 Appended.append(Features); 644 NewAttrs.addAttribute("target-features", Appended); 645 } 646 } 647 if (FramePointerUsageView->getNumOccurrences() > 0 && 648 !F.hasFnAttribute("frame-pointer")) { 649 if (getFramePointerUsage() == FramePointer::All) 650 NewAttrs.addAttribute("frame-pointer", "all"); 651 else if (getFramePointerUsage() == FramePointer::NonLeaf) 652 NewAttrs.addAttribute("frame-pointer", "non-leaf"); 653 else if (getFramePointerUsage() == FramePointer::None) 654 NewAttrs.addAttribute("frame-pointer", "none"); 655 } 656 if (DisableTailCallsView->getNumOccurrences() > 0) 657 NewAttrs.addAttribute("disable-tail-calls", 658 toStringRef(getDisableTailCalls())); 659 if (getStackRealign()) 660 NewAttrs.addAttribute("stackrealign"); 661 662 HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math"); 663 HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math"); 664 HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math"); 665 HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math"); 666 667 if (DenormalFPMathView->getNumOccurrences() > 0 && 668 !F.hasFnAttribute("denormal-fp-math")) { 669 DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath(); 670 671 // FIXME: Command line flag should expose separate input/output modes. 672 NewAttrs.addAttribute("denormal-fp-math", 673 DenormalMode(DenormKind, DenormKind).str()); 674 } 675 676 if (DenormalFP32MathView->getNumOccurrences() > 0 && 677 !F.hasFnAttribute("denormal-fp-math-f32")) { 678 // FIXME: Command line flag should expose separate input/output modes. 679 DenormalMode::DenormalModeKind DenormKind = getDenormalFP32Math(); 680 681 NewAttrs.addAttribute( 682 "denormal-fp-math-f32", 683 DenormalMode(DenormKind, DenormKind).str()); 684 } 685 686 if (TrapFuncNameView->getNumOccurrences() > 0) 687 for (auto &B : F) 688 for (auto &I : B) 689 if (auto *Call = dyn_cast<CallInst>(&I)) 690 if (const auto *F = Call->getCalledFunction()) 691 if (F->getIntrinsicID() == Intrinsic::debugtrap || 692 F->getIntrinsicID() == Intrinsic::trap) 693 Call->addAttribute( 694 AttributeList::FunctionIndex, 695 Attribute::get(Ctx, "trap-func-name", getTrapFuncName())); 696 697 // Let NewAttrs override Attrs. 698 F.setAttributes( 699 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs)); 700 } 701 702 /// Set function attributes of functions in Module M based on CPU, 703 /// Features, and command line flags. 704 void codegen::setFunctionAttributes(StringRef CPU, StringRef Features, 705 Module &M) { 706 for (Function &F : M) 707 setFunctionAttributes(CPU, Features, F); 708 } 709