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