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