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