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