1 //===-- CommandFlags.cpp - Command Line Flags Interface ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
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"
24 #define CGOPT(TY, NAME) \
25 static cl::opt<TY> *NAME##View; \
26 TY codegen::get##NAME() { \
27 assert(NAME##View && "RegisterCodeGenFlags not created."); \
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."); \
38 #define CGOPT_EXP(TY, NAME) \
40 Optional<TY> codegen::getExplicit##NAME() { \
41 if (NAME##View->getNumOccurrences()) { \
42 TY res = *NAME##View; \
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(bool, StackRealign
)
73 CGOPT(std::string
, TrapFuncName
)
75 CGOPT(bool, RelaxELFRelocations
)
76 CGOPT_EXP(bool, DataSections
)
77 CGOPT_EXP(bool, FunctionSections
)
78 CGOPT(bool, IgnoreXCOFFVisibility
)
79 CGOPT(bool, XCOFFTracebackTable
)
80 CGOPT(std::string
, BBSections
)
81 CGOPT(unsigned, TLSSize
)
82 CGOPT(bool, EmulatedTLS
)
83 CGOPT(bool, UniqueSectionNames
)
84 CGOPT(bool, UniqueBasicBlockSectionNames
)
85 CGOPT(EABI
, EABIVersion
)
86 CGOPT(DebuggerKind
, DebuggerTuningOpt
)
87 CGOPT(bool, EnableStackSizeSection
)
88 CGOPT(bool, EnableAddrsig
)
89 CGOPT(bool, EmitCallSiteInfo
)
90 CGOPT(bool, EnableMachineFunctionSplitter
)
91 CGOPT(bool, EnableDebugEntryValues
)
92 CGOPT(bool, PseudoProbeForProfiling
)
93 CGOPT(bool, ValueTrackingVariableLocations
)
94 CGOPT(bool, ForceDwarfFrameSection
)
95 CGOPT(bool, XRayOmitFunctionIndex
)
96 CGOPT(bool, DebugStrictDwarf
)
97 CGOPT(unsigned, AlignLoops
)
99 codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
100 #define CGBINDOPT(NAME) \
102 NAME##View = std::addressof(NAME); \
105 static cl::opt
<std::string
> MArch(
106 "march", cl::desc("Architecture to generate code for (see --version)"));
109 static cl::opt
<std::string
> MCPU(
110 "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
111 cl::value_desc("cpu-name"), cl::init(""));
114 static cl::list
<std::string
> MAttrs(
115 "mattr", cl::CommaSeparated
,
116 cl::desc("Target specific attributes (-mattr=help for details)"),
117 cl::value_desc("a1,+a2,-a3,..."));
120 static cl::opt
<Reloc::Model
> RelocModel(
121 "relocation-model", cl::desc("Choose relocation model"),
123 clEnumValN(Reloc::Static
, "static", "Non-relocatable code"),
124 clEnumValN(Reloc::PIC_
, "pic",
125 "Fully relocatable, position independent code"),
126 clEnumValN(Reloc::DynamicNoPIC
, "dynamic-no-pic",
127 "Relocatable external references, non-relocatable code"),
130 "Code and read-only data relocatable, accessed PC-relative"),
133 "Read-write data relocatable, accessed relative to static base"),
134 clEnumValN(Reloc::ROPI_RWPI
, "ropi-rwpi",
135 "Combination of ropi and rwpi")));
136 CGBINDOPT(RelocModel
);
138 static cl::opt
<ThreadModel::Model
> ThreadModel(
139 "thread-model", cl::desc("Choose threading model"),
140 cl::init(ThreadModel::POSIX
),
142 clEnumValN(ThreadModel::POSIX
, "posix", "POSIX thread model"),
143 clEnumValN(ThreadModel::Single
, "single", "Single thread model")));
144 CGBINDOPT(ThreadModel
);
146 static cl::opt
<CodeModel::Model
> CodeModel(
147 "code-model", cl::desc("Choose code model"),
148 cl::values(clEnumValN(CodeModel::Tiny
, "tiny", "Tiny code model"),
149 clEnumValN(CodeModel::Small
, "small", "Small code model"),
150 clEnumValN(CodeModel::Kernel
, "kernel", "Kernel code model"),
151 clEnumValN(CodeModel::Medium
, "medium", "Medium code model"),
152 clEnumValN(CodeModel::Large
, "large", "Large code model")));
153 CGBINDOPT(CodeModel
);
155 static cl::opt
<ExceptionHandling
> ExceptionModel(
156 "exception-model", cl::desc("exception model"),
157 cl::init(ExceptionHandling::None
),
159 clEnumValN(ExceptionHandling::None
, "default",
160 "default exception handling model"),
161 clEnumValN(ExceptionHandling::DwarfCFI
, "dwarf",
162 "DWARF-like CFI based exception handling"),
163 clEnumValN(ExceptionHandling::SjLj
, "sjlj",
164 "SjLj exception handling"),
165 clEnumValN(ExceptionHandling::ARM
, "arm", "ARM EHABI exceptions"),
166 clEnumValN(ExceptionHandling::WinEH
, "wineh",
167 "Windows exception model"),
168 clEnumValN(ExceptionHandling::Wasm
, "wasm",
169 "WebAssembly exception handling")));
170 CGBINDOPT(ExceptionModel
);
172 static cl::opt
<CodeGenFileType
> FileType(
173 "filetype", cl::init(CGFT_AssemblyFile
),
175 "Choose a file type (not all types are supported by all targets):"),
177 clEnumValN(CGFT_AssemblyFile
, "asm", "Emit an assembly ('.s') file"),
178 clEnumValN(CGFT_ObjectFile
, "obj",
179 "Emit a native object ('.o') file"),
180 clEnumValN(CGFT_Null
, "null",
181 "Emit nothing, for performance testing")));
184 static cl::opt
<FramePointerKind
> FramePointerUsage(
186 cl::desc("Specify frame pointer elimination optimization"),
187 cl::init(FramePointerKind::None
),
189 clEnumValN(FramePointerKind::All
, "all",
190 "Disable frame pointer elimination"),
191 clEnumValN(FramePointerKind::NonLeaf
, "non-leaf",
192 "Disable frame pointer elimination for non-leaf frame"),
193 clEnumValN(FramePointerKind::None
, "none",
194 "Enable frame pointer elimination")));
195 CGBINDOPT(FramePointerUsage
);
197 static cl::opt
<bool> EnableUnsafeFPMath(
198 "enable-unsafe-fp-math",
199 cl::desc("Enable optimizations that may decrease FP precision"),
201 CGBINDOPT(EnableUnsafeFPMath
);
203 static cl::opt
<bool> EnableNoInfsFPMath(
204 "enable-no-infs-fp-math",
205 cl::desc("Enable FP math optimizations that assume no +-Infs"),
207 CGBINDOPT(EnableNoInfsFPMath
);
209 static cl::opt
<bool> EnableNoNaNsFPMath(
210 "enable-no-nans-fp-math",
211 cl::desc("Enable FP math optimizations that assume no NaNs"),
213 CGBINDOPT(EnableNoNaNsFPMath
);
215 static cl::opt
<bool> EnableNoSignedZerosFPMath(
216 "enable-no-signed-zeros-fp-math",
217 cl::desc("Enable FP math optimizations that assume "
218 "the sign of 0 is insignificant"),
220 CGBINDOPT(EnableNoSignedZerosFPMath
);
222 static cl::opt
<bool> EnableNoTrappingFPMath(
223 "enable-no-trapping-fp-math",
224 cl::desc("Enable setting the FP exceptions build "
225 "attribute not to use exceptions"),
227 CGBINDOPT(EnableNoTrappingFPMath
);
229 static const auto DenormFlagEnumOptions
=
230 cl::values(clEnumValN(DenormalMode::IEEE
, "ieee",
231 "IEEE 754 denormal numbers"),
232 clEnumValN(DenormalMode::PreserveSign
, "preserve-sign",
233 "the sign of a flushed-to-zero number is preserved "
235 clEnumValN(DenormalMode::PositiveZero
, "positive-zero",
236 "denormals are flushed to positive zero"));
238 // FIXME: Doesn't have way to specify separate input and output modes.
239 static cl::opt
<DenormalMode::DenormalModeKind
> DenormalFPMath(
241 cl::desc("Select which denormal numbers the code is permitted to require"),
242 cl::init(DenormalMode::IEEE
),
243 DenormFlagEnumOptions
);
244 CGBINDOPT(DenormalFPMath
);
246 static cl::opt
<DenormalMode::DenormalModeKind
> DenormalFP32Math(
247 "denormal-fp-math-f32",
248 cl::desc("Select which denormal numbers the code is permitted to require for float"),
249 cl::init(DenormalMode::Invalid
),
250 DenormFlagEnumOptions
);
251 CGBINDOPT(DenormalFP32Math
);
253 static cl::opt
<bool> EnableHonorSignDependentRoundingFPMath(
254 "enable-sign-dependent-rounding-fp-math", cl::Hidden
,
255 cl::desc("Force codegen to assume rounding mode can change dynamically"),
257 CGBINDOPT(EnableHonorSignDependentRoundingFPMath
);
259 static cl::opt
<FloatABI::ABIType
> FloatABIForCalls(
260 "float-abi", cl::desc("Choose float ABI type"),
261 cl::init(FloatABI::Default
),
262 cl::values(clEnumValN(FloatABI::Default
, "default",
263 "Target default float ABI type"),
264 clEnumValN(FloatABI::Soft
, "soft",
265 "Soft float ABI (implied by -soft-float)"),
266 clEnumValN(FloatABI::Hard
, "hard",
267 "Hard float ABI (uses FP registers)")));
268 CGBINDOPT(FloatABIForCalls
);
270 static cl::opt
<FPOpFusion::FPOpFusionMode
> FuseFPOps(
271 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
272 cl::init(FPOpFusion::Standard
),
274 clEnumValN(FPOpFusion::Fast
, "fast",
275 "Fuse FP ops whenever profitable"),
276 clEnumValN(FPOpFusion::Standard
, "on", "Only fuse 'blessed' FP ops."),
277 clEnumValN(FPOpFusion::Strict
, "off",
278 "Only fuse FP ops when the result won't be affected.")));
279 CGBINDOPT(FuseFPOps
);
281 static cl::opt
<bool> DontPlaceZerosInBSS(
282 "nozero-initialized-in-bss",
283 cl::desc("Don't place zero-initialized symbols into bss section"),
285 CGBINDOPT(DontPlaceZerosInBSS
);
287 static cl::opt
<bool> EnableAIXExtendedAltivecABI(
288 "vec-extabi", cl::desc("Enable the AIX Extended Altivec ABI."),
290 CGBINDOPT(EnableAIXExtendedAltivecABI
);
292 static cl::opt
<bool> EnableGuaranteedTailCallOpt(
295 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
297 CGBINDOPT(EnableGuaranteedTailCallOpt
);
299 static cl::opt
<bool> DisableTailCalls(
300 "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
301 CGBINDOPT(DisableTailCalls
);
303 static cl::opt
<bool> StackSymbolOrdering(
304 "stack-symbol-ordering", cl::desc("Order local stack symbols."),
306 CGBINDOPT(StackSymbolOrdering
);
308 static cl::opt
<bool> StackRealign(
310 cl::desc("Force align the stack to the minimum alignment"),
312 CGBINDOPT(StackRealign
);
314 static cl::opt
<std::string
> TrapFuncName(
315 "trap-func", cl::Hidden
,
316 cl::desc("Emit a call to trap function rather than a trap instruction"),
318 CGBINDOPT(TrapFuncName
);
320 static cl::opt
<bool> UseCtors("use-ctors",
321 cl::desc("Use .ctors instead of .init_array."),
325 static cl::opt
<bool> RelaxELFRelocations(
326 "relax-elf-relocations",
328 "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
330 CGBINDOPT(RelaxELFRelocations
);
332 static cl::opt
<bool> DataSections(
333 "data-sections", cl::desc("Emit data into separate sections"),
335 CGBINDOPT(DataSections
);
337 static cl::opt
<bool> FunctionSections(
338 "function-sections", cl::desc("Emit functions into separate sections"),
340 CGBINDOPT(FunctionSections
);
342 static cl::opt
<bool> IgnoreXCOFFVisibility(
343 "ignore-xcoff-visibility",
344 cl::desc("Not emit the visibility attribute for asm in AIX OS or give "
345 "all symbols 'unspecified' visibility in XCOFF object file"),
347 CGBINDOPT(IgnoreXCOFFVisibility
);
349 static cl::opt
<bool> XCOFFTracebackTable(
350 "xcoff-traceback-table", cl::desc("Emit the XCOFF traceback table"),
352 CGBINDOPT(XCOFFTracebackTable
);
354 static cl::opt
<std::string
> BBSections(
355 "basic-block-sections",
356 cl::desc("Emit basic blocks into separate sections"),
357 cl::value_desc("all | <function list (file)> | labels | none"),
359 CGBINDOPT(BBSections
);
361 static cl::opt
<unsigned> TLSSize(
362 "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
365 static cl::opt
<bool> EmulatedTLS(
366 "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
367 CGBINDOPT(EmulatedTLS
);
369 static cl::opt
<bool> UniqueSectionNames(
370 "unique-section-names", cl::desc("Give unique names to every section"),
372 CGBINDOPT(UniqueSectionNames
);
374 static cl::opt
<bool> UniqueBasicBlockSectionNames(
375 "unique-basic-block-section-names",
376 cl::desc("Give unique names to every basic block section"),
378 CGBINDOPT(UniqueBasicBlockSectionNames
);
380 static cl::opt
<EABI
> EABIVersion(
381 "meabi", cl::desc("Set EABI type (default depends on triple):"),
382 cl::init(EABI::Default
),
384 clEnumValN(EABI::Default
, "default", "Triple default EABI version"),
385 clEnumValN(EABI::EABI4
, "4", "EABI version 4"),
386 clEnumValN(EABI::EABI5
, "5", "EABI version 5"),
387 clEnumValN(EABI::GNU
, "gnu", "EABI GNU")));
388 CGBINDOPT(EABIVersion
);
390 static cl::opt
<DebuggerKind
> DebuggerTuningOpt(
391 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
392 cl::init(DebuggerKind::Default
),
394 clEnumValN(DebuggerKind::GDB
, "gdb", "gdb"),
395 clEnumValN(DebuggerKind::LLDB
, "lldb", "lldb"),
396 clEnumValN(DebuggerKind::DBX
, "dbx", "dbx"),
397 clEnumValN(DebuggerKind::SCE
, "sce", "SCE targets (e.g. PS4)")));
398 CGBINDOPT(DebuggerTuningOpt
);
400 static cl::opt
<bool> EnableStackSizeSection(
401 "stack-size-section",
402 cl::desc("Emit a section containing stack size metadata"),
404 CGBINDOPT(EnableStackSizeSection
);
406 static cl::opt
<bool> EnableAddrsig(
407 "addrsig", cl::desc("Emit an address-significance table"),
409 CGBINDOPT(EnableAddrsig
);
411 static cl::opt
<bool> EmitCallSiteInfo(
412 "emit-call-site-info",
414 "Emit call site debug information, if debug information is enabled."),
416 CGBINDOPT(EmitCallSiteInfo
);
418 static cl::opt
<bool> EnableDebugEntryValues(
419 "debug-entry-values",
420 cl::desc("Enable debug info for the debug entry values."),
422 CGBINDOPT(EnableDebugEntryValues
);
424 static cl::opt
<bool> PseudoProbeForProfiling(
425 "pseudo-probe-for-profiling", cl::desc("Emit pseudo probes for AutoFDO"),
427 CGBINDOPT(PseudoProbeForProfiling
);
429 static cl::opt
<bool> ValueTrackingVariableLocations(
430 "experimental-debug-variable-locations",
431 cl::desc("Use experimental new value-tracking variable locations"),
433 CGBINDOPT(ValueTrackingVariableLocations
);
435 static cl::opt
<bool> EnableMachineFunctionSplitter(
436 "split-machine-functions",
437 cl::desc("Split out cold basic blocks from machine functions based on "
438 "profile information"),
440 CGBINDOPT(EnableMachineFunctionSplitter
);
442 static cl::opt
<bool> ForceDwarfFrameSection(
443 "force-dwarf-frame-section",
444 cl::desc("Always emit a debug frame section."), cl::init(false));
445 CGBINDOPT(ForceDwarfFrameSection
);
447 static cl::opt
<bool> XRayOmitFunctionIndex(
448 "no-xray-index", cl::desc("Don't emit xray_fn_idx section"),
450 CGBINDOPT(XRayOmitFunctionIndex
);
452 static cl::opt
<bool> DebugStrictDwarf(
453 "strict-dwarf", cl::desc("use strict dwarf"), cl::init(false));
454 CGBINDOPT(DebugStrictDwarf
);
456 static cl::opt
<unsigned> AlignLoops("align-loops",
457 cl::desc("Default alignment for loops"));
458 CGBINDOPT(AlignLoops
);
462 mc::RegisterMCTargetOptionsFlags();
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
;
474 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
=
475 MemoryBuffer::getFile(getBBSections());
477 errs() << "Error loading basic block sections function list file: "
478 << MBOrErr
.getError().message() << "\n";
480 Options
.BBSectionsFuncListBuf
= std::move(*MBOrErr
);
482 return BasicBlockSection::List
;
486 // Common utility function tightly tied to the options listed here. Initializes
487 // a TargetOptions object with CodeGen flags and returns it.
489 codegen::InitTargetOptionsFromCodeGenFlags(const Triple
&TheTriple
) {
490 TargetOptions Options
;
491 Options
.AllowFPOpFusion
= getFuseFPOps();
492 Options
.UnsafeFPMath
= getEnableUnsafeFPMath();
493 Options
.NoInfsFPMath
= getEnableNoInfsFPMath();
494 Options
.NoNaNsFPMath
= getEnableNoNaNsFPMath();
495 Options
.NoSignedZerosFPMath
= getEnableNoSignedZerosFPMath();
496 Options
.NoTrappingFPMath
= getEnableNoTrappingFPMath();
498 DenormalMode::DenormalModeKind DenormKind
= getDenormalFPMath();
500 // FIXME: Should have separate input and output flags
501 Options
.setFPDenormalMode(DenormalMode(DenormKind
, DenormKind
));
503 Options
.HonorSignDependentRoundingFPMathOption
=
504 getEnableHonorSignDependentRoundingFPMath();
505 if (getFloatABIForCalls() != FloatABI::Default
)
506 Options
.FloatABIType
= getFloatABIForCalls();
507 Options
.EnableAIXExtendedAltivecABI
= getEnableAIXExtendedAltivecABI();
508 Options
.NoZerosInBSS
= getDontPlaceZerosInBSS();
509 Options
.GuaranteedTailCallOpt
= getEnableGuaranteedTailCallOpt();
510 Options
.StackSymbolOrdering
= getStackSymbolOrdering();
511 Options
.UseInitArray
= !getUseCtors();
512 Options
.RelaxELFRelocations
= getRelaxELFRelocations();
513 Options
.DataSections
=
514 getExplicitDataSections().getValueOr(TheTriple
.hasDefaultDataSections());
515 Options
.FunctionSections
= getFunctionSections();
516 Options
.IgnoreXCOFFVisibility
= getIgnoreXCOFFVisibility();
517 Options
.XCOFFTracebackTable
= getXCOFFTracebackTable();
518 Options
.BBSections
= getBBSectionsMode(Options
);
519 Options
.UniqueSectionNames
= getUniqueSectionNames();
520 Options
.UniqueBasicBlockSectionNames
= getUniqueBasicBlockSectionNames();
521 Options
.TLSSize
= getTLSSize();
522 Options
.EmulatedTLS
= getEmulatedTLS();
523 Options
.ExplicitEmulatedTLS
= EmulatedTLSView
->getNumOccurrences() > 0;
524 Options
.ExceptionModel
= getExceptionModel();
525 Options
.EmitStackSizeSection
= getEnableStackSizeSection();
526 Options
.EnableMachineFunctionSplitter
= getEnableMachineFunctionSplitter();
527 Options
.EmitAddrsig
= getEnableAddrsig();
528 Options
.EmitCallSiteInfo
= getEmitCallSiteInfo();
529 Options
.EnableDebugEntryValues
= getEnableDebugEntryValues();
530 Options
.PseudoProbeForProfiling
= getPseudoProbeForProfiling();
531 Options
.ValueTrackingVariableLocations
= getValueTrackingVariableLocations();
532 Options
.ForceDwarfFrameSection
= getForceDwarfFrameSection();
533 Options
.XRayOmitFunctionIndex
= getXRayOmitFunctionIndex();
534 Options
.DebugStrictDwarf
= getDebugStrictDwarf();
535 Options
.LoopAlignment
= getAlignLoops();
537 Options
.MCOptions
= mc::InitMCTargetOptionsFromFlags();
539 Options
.ThreadModel
= getThreadModel();
540 Options
.EABIVersion
= getEABIVersion();
541 Options
.DebuggerTuning
= getDebuggerTuningOpt();
546 std::string
codegen::getCPUStr() {
547 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
548 // this will set the CPU to an empty string which tells the target to
549 // pick a basic default.
550 if (getMCPU() == "native")
551 return std::string(sys::getHostCPUName());
556 std::string
codegen::getFeaturesStr() {
557 SubtargetFeatures Features
;
559 // If user asked for the 'native' CPU, we need to autodetect features.
560 // This is necessary for x86 where the CPU might not support all the
561 // features the autodetected CPU name lists in the target. For example,
562 // not all Sandybridge processors support AVX.
563 if (getMCPU() == "native") {
564 StringMap
<bool> HostFeatures
;
565 if (sys::getHostCPUFeatures(HostFeatures
))
566 for (auto &F
: HostFeatures
)
567 Features
.AddFeature(F
.first(), F
.second
);
570 for (auto const &MAttr
: getMAttrs())
571 Features
.AddFeature(MAttr
);
573 return Features
.getString();
576 std::vector
<std::string
> codegen::getFeatureList() {
577 SubtargetFeatures Features
;
579 // If user asked for the 'native' CPU, we need to autodetect features.
580 // This is necessary for x86 where the CPU might not support all the
581 // features the autodetected CPU name lists in the target. For example,
582 // not all Sandybridge processors support AVX.
583 if (getMCPU() == "native") {
584 StringMap
<bool> HostFeatures
;
585 if (sys::getHostCPUFeatures(HostFeatures
))
586 for (auto &F
: HostFeatures
)
587 Features
.AddFeature(F
.first(), F
.second
);
590 for (auto const &MAttr
: getMAttrs())
591 Features
.AddFeature(MAttr
);
593 return Features
.getFeatures();
596 void codegen::renderBoolStringAttr(AttrBuilder
&B
, StringRef Name
, bool Val
) {
597 B
.addAttribute(Name
, Val
? "true" : "false");
600 #define HANDLE_BOOL_ATTR(CL, AttrName) \
602 if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
603 renderBoolStringAttr(NewAttrs, AttrName, *CL); \
606 /// Set function attributes of function \p F based on CPU, Features, and command
608 void codegen::setFunctionAttributes(StringRef CPU
, StringRef Features
,
610 auto &Ctx
= F
.getContext();
611 AttributeList Attrs
= F
.getAttributes();
612 AttrBuilder NewAttrs
;
614 if (!CPU
.empty() && !F
.hasFnAttribute("target-cpu"))
615 NewAttrs
.addAttribute("target-cpu", CPU
);
616 if (!Features
.empty()) {
617 // Append the command line features to any that are already on the function.
618 StringRef OldFeatures
=
619 F
.getFnAttribute("target-features").getValueAsString();
620 if (OldFeatures
.empty())
621 NewAttrs
.addAttribute("target-features", Features
);
623 SmallString
<256> Appended(OldFeatures
);
624 Appended
.push_back(',');
625 Appended
.append(Features
);
626 NewAttrs
.addAttribute("target-features", Appended
);
629 if (FramePointerUsageView
->getNumOccurrences() > 0 &&
630 !F
.hasFnAttribute("frame-pointer")) {
631 if (getFramePointerUsage() == FramePointerKind::All
)
632 NewAttrs
.addAttribute("frame-pointer", "all");
633 else if (getFramePointerUsage() == FramePointerKind::NonLeaf
)
634 NewAttrs
.addAttribute("frame-pointer", "non-leaf");
635 else if (getFramePointerUsage() == FramePointerKind::None
)
636 NewAttrs
.addAttribute("frame-pointer", "none");
638 if (DisableTailCallsView
->getNumOccurrences() > 0)
639 NewAttrs
.addAttribute("disable-tail-calls",
640 toStringRef(getDisableTailCalls()));
641 if (getStackRealign())
642 NewAttrs
.addAttribute("stackrealign");
644 HANDLE_BOOL_ATTR(EnableUnsafeFPMathView
, "unsafe-fp-math");
645 HANDLE_BOOL_ATTR(EnableNoInfsFPMathView
, "no-infs-fp-math");
646 HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView
, "no-nans-fp-math");
647 HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView
, "no-signed-zeros-fp-math");
649 if (DenormalFPMathView
->getNumOccurrences() > 0 &&
650 !F
.hasFnAttribute("denormal-fp-math")) {
651 DenormalMode::DenormalModeKind DenormKind
= getDenormalFPMath();
653 // FIXME: Command line flag should expose separate input/output modes.
654 NewAttrs
.addAttribute("denormal-fp-math",
655 DenormalMode(DenormKind
, DenormKind
).str());
658 if (DenormalFP32MathView
->getNumOccurrences() > 0 &&
659 !F
.hasFnAttribute("denormal-fp-math-f32")) {
660 // FIXME: Command line flag should expose separate input/output modes.
661 DenormalMode::DenormalModeKind DenormKind
= getDenormalFP32Math();
663 NewAttrs
.addAttribute(
664 "denormal-fp-math-f32",
665 DenormalMode(DenormKind
, DenormKind
).str());
668 if (TrapFuncNameView
->getNumOccurrences() > 0)
671 if (auto *Call
= dyn_cast
<CallInst
>(&I
))
672 if (const auto *F
= Call
->getCalledFunction())
673 if (F
->getIntrinsicID() == Intrinsic::debugtrap
||
674 F
->getIntrinsicID() == Intrinsic::trap
)
676 Attribute::get(Ctx
, "trap-func-name", getTrapFuncName()));
678 // Let NewAttrs override Attrs.
679 F
.setAttributes(Attrs
.addFnAttributes(Ctx
, NewAttrs
));
682 /// Set function attributes of functions in Module M based on CPU,
683 /// Features, and command line flags.
684 void codegen::setFunctionAttributes(StringRef CPU
, StringRef Features
,
686 for (Function
&F
: M
)
687 setFunctionAttributes(CPU
, Features
, F
);