1 //===-- CommandFlags.h - 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/ADT/StringExtras.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/MC/MCTargetOptionsCommandFlags.inc"
20 #include "llvm/MC/SubtargetFeature.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
29 static cl::opt<std::string>
31 cl::desc("Architecture to generate code for (see --version)"));
33 static cl::opt<std::string>
35 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
36 cl::value_desc("cpu-name"), cl::init(""));
38 static cl::list<std::string>
39 MAttrs("mattr", cl::CommaSeparated,
40 cl::desc("Target specific attributes (-mattr=help for details)"),
41 cl::value_desc("a1,+a2,-a3,..."));
43 static cl::opt<Reloc::Model> RelocModel(
44 "relocation-model", cl::desc("Choose relocation model"),
46 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
47 clEnumValN(Reloc::PIC_, "pic",
48 "Fully relocatable, position independent code"),
49 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
50 "Relocatable external references, non-relocatable code"),
51 clEnumValN(Reloc::ROPI, "ropi",
52 "Code and read-only data relocatable, accessed PC-relative"),
55 "Read-write data relocatable, accessed relative to static base"),
56 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
57 "Combination of ropi and rwpi")));
59 LLVM_ATTRIBUTE_UNUSED static Optional<Reloc::Model> getRelocModel() {
60 if (RelocModel.getNumOccurrences()) {
61 Reloc::Model R = RelocModel;
67 static cl::opt<ThreadModel::Model> TMModel(
68 "thread-model", cl::desc("Choose threading model"),
69 cl::init(ThreadModel::POSIX),
70 cl::values(clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
71 clEnumValN(ThreadModel::Single, "single",
72 "Single thread model")));
74 static cl::opt<llvm::CodeModel::Model> CMModel(
75 "code-model", cl::desc("Choose code model"),
76 cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
77 clEnumValN(CodeModel::Small, "small", "Small code model"),
78 clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
79 clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
80 clEnumValN(CodeModel::Large, "large", "Large code model")));
82 LLVM_ATTRIBUTE_UNUSED static Optional<CodeModel::Model> getCodeModel() {
83 if (CMModel.getNumOccurrences()) {
84 CodeModel::Model M = CMModel;
90 static cl::opt<llvm::ExceptionHandling> ExceptionModel(
91 "exception-model", cl::desc("exception model"),
92 cl::init(ExceptionHandling::None),
94 clEnumValN(ExceptionHandling::None, "default",
95 "default exception handling model"),
96 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
97 "DWARF-like CFI based exception handling"),
98 clEnumValN(ExceptionHandling::SjLj, "sjlj", "SjLj exception handling"),
99 clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
100 clEnumValN(ExceptionHandling::WinEH, "wineh",
101 "Windows exception model"),
102 clEnumValN(ExceptionHandling::Wasm, "wasm",
103 "WebAssembly exception handling")));
105 static cl::opt<TargetMachine::CodeGenFileType> FileType(
106 "filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
108 "Choose a file type (not all types are supported by all targets):"),
109 cl::values(clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
110 "Emit an assembly ('.s') file"),
111 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
112 "Emit a native object ('.o') file"),
113 clEnumValN(TargetMachine::CGFT_Null, "null",
114 "Emit nothing, for performance testing")));
116 static cl::opt<llvm::FramePointer::FP> FramePointerUsage(
117 "frame-pointer", cl::desc("Specify frame pointer elimination optimization"),
118 cl::init(llvm::FramePointer::None),
120 clEnumValN(llvm::FramePointer::All, "all",
121 "Disable frame pointer elimination"),
122 clEnumValN(llvm::FramePointer::NonLeaf, "non-leaf",
123 "Disable frame pointer elimination for non-leaf frame"),
124 clEnumValN(llvm::FramePointer::None, "none",
125 "Enable frame pointer elimination")));
127 static cl::opt<bool> EnableUnsafeFPMath(
128 "enable-unsafe-fp-math",
129 cl::desc("Enable optimizations that may decrease FP precision"),
132 static cl::opt<bool> EnableNoInfsFPMath(
133 "enable-no-infs-fp-math",
134 cl::desc("Enable FP math optimizations that assume no +-Infs"),
137 static cl::opt<bool> EnableNoNaNsFPMath(
138 "enable-no-nans-fp-math",
139 cl::desc("Enable FP math optimizations that assume no NaNs"),
142 static cl::opt<bool> EnableNoSignedZerosFPMath(
143 "enable-no-signed-zeros-fp-math",
144 cl::desc("Enable FP math optimizations that assume "
145 "the sign of 0 is insignificant"),
149 EnableNoTrappingFPMath("enable-no-trapping-fp-math",
150 cl::desc("Enable setting the FP exceptions build "
151 "attribute not to use exceptions"),
154 static cl::opt<llvm::FPDenormal::DenormalMode> DenormalMode(
156 cl::desc("Select which denormal numbers the code is permitted to require"),
157 cl::init(FPDenormal::IEEE),
158 cl::values(clEnumValN(FPDenormal::IEEE, "ieee",
159 "IEEE 754 denormal numbers"),
160 clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
161 "the sign of a flushed-to-zero number is preserved "
163 clEnumValN(FPDenormal::PositiveZero, "positive-zero",
164 "denormals are flushed to positive zero")));
166 static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
167 "enable-sign-dependent-rounding-fp-math", cl::Hidden,
168 cl::desc("Force codegen to assume rounding mode can change dynamically"),
171 static cl::opt<llvm::FloatABI::ABIType> FloatABIForCalls(
172 "float-abi", cl::desc("Choose float ABI type"), cl::init(FloatABI::Default),
173 cl::values(clEnumValN(FloatABI::Default, "default",
174 "Target default float ABI type"),
175 clEnumValN(FloatABI::Soft, "soft",
176 "Soft float ABI (implied by -soft-float)"),
177 clEnumValN(FloatABI::Hard, "hard",
178 "Hard float ABI (uses FP registers)")));
180 static cl::opt<llvm::FPOpFusion::FPOpFusionMode> FuseFPOps(
181 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
182 cl::init(FPOpFusion::Standard),
184 clEnumValN(FPOpFusion::Fast, "fast", "Fuse FP ops whenever profitable"),
185 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
186 clEnumValN(FPOpFusion::Strict, "off",
187 "Only fuse FP ops when the result won't be affected.")));
189 static cl::opt<bool> DontPlaceZerosInBSS(
190 "nozero-initialized-in-bss",
191 cl::desc("Don't place zero-initialized symbols into bss section"),
194 static cl::opt<bool> EnableGuaranteedTailCallOpt(
197 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
200 static cl::opt<bool> DisableTailCalls("disable-tail-calls",
201 cl::desc("Never emit tail calls"),
204 static cl::opt<bool> StackSymbolOrdering("stack-symbol-ordering",
205 cl::desc("Order local stack symbols."),
208 static cl::opt<unsigned>
209 OverrideStackAlignment("stack-alignment",
210 cl::desc("Override default stack alignment"),
214 StackRealign("stackrealign",
215 cl::desc("Force align the stack to the minimum alignment"),
218 static cl::opt<std::string> TrapFuncName(
219 "trap-func", cl::Hidden,
220 cl::desc("Emit a call to trap function rather than a trap instruction"),
223 static cl::opt<bool> UseCtors("use-ctors",
224 cl::desc("Use .ctors instead of .init_array."),
227 static cl::opt<bool> RelaxELFRelocations(
228 "relax-elf-relocations",
229 cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
232 static cl::opt<bool> DataSections("data-sections",
233 cl::desc("Emit data into separate sections"),
237 FunctionSections("function-sections",
238 cl::desc("Emit functions into separate sections"),
241 static cl::opt<bool> EmulatedTLS("emulated-tls",
242 cl::desc("Use emulated TLS model"),
246 UniqueSectionNames("unique-section-names",
247 cl::desc("Give unique names to every section"),
250 static cl::opt<llvm::EABI>
251 EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"),
252 cl::init(EABI::Default),
253 cl::values(clEnumValN(EABI::Default, "default",
254 "Triple default EABI version"),
255 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
256 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
257 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
259 static cl::opt<DebuggerKind> DebuggerTuningOpt(
260 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
261 cl::init(DebuggerKind::Default),
262 cl::values(clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
263 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
264 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
266 static cl::opt<bool> EnableStackSizeSection(
267 "stack-size-section",
268 cl::desc("Emit a section containing stack size metadata"), cl::init(false));
271 EnableAddrsig("addrsig", cl::desc("Emit an address-significance table"),
275 EnableDebugEntryValues("debug-entry-values",
276 cl::desc("Emit debug info about parameter's entry values"),
279 // Common utility function tightly tied to the options listed here. Initializes
280 // a TargetOptions object with CodeGen flags and returns it.
281 static TargetOptions InitTargetOptionsFromCodeGenFlags() {
282 TargetOptions Options;
283 Options.AllowFPOpFusion = FuseFPOps;
284 Options.UnsafeFPMath = EnableUnsafeFPMath;
285 Options.NoInfsFPMath = EnableNoInfsFPMath;
286 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
287 Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
288 Options.NoTrappingFPMath = EnableNoTrappingFPMath;
289 Options.FPDenormalMode = DenormalMode;
290 Options.HonorSignDependentRoundingFPMathOption =
291 EnableHonorSignDependentRoundingFPMath;
292 if (FloatABIForCalls != FloatABI::Default)
293 Options.FloatABIType = FloatABIForCalls;
294 Options.NoZerosInBSS = DontPlaceZerosInBSS;
295 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
296 Options.StackAlignmentOverride = OverrideStackAlignment;
297 Options.StackSymbolOrdering = StackSymbolOrdering;
298 Options.UseInitArray = !UseCtors;
299 Options.RelaxELFRelocations = RelaxELFRelocations;
300 Options.DataSections = DataSections;
301 Options.FunctionSections = FunctionSections;
302 Options.UniqueSectionNames = UniqueSectionNames;
303 Options.EmulatedTLS = EmulatedTLS;
304 Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0;
305 Options.ExceptionModel = ExceptionModel;
306 Options.EmitStackSizeSection = EnableStackSizeSection;
307 Options.EmitAddrsig = EnableAddrsig;
308 Options.EnableDebugEntryValues = EnableDebugEntryValues;
310 Options.MCOptions = InitMCTargetOptionsFromFlags();
312 Options.ThreadModel = TMModel;
313 Options.EABIVersion = EABIVersion;
314 Options.DebuggerTuning = DebuggerTuningOpt;
319 LLVM_ATTRIBUTE_UNUSED static std::string getCPUStr() {
320 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
321 // this will set the CPU to an empty string which tells the target to
322 // pick a basic default.
323 if (MCPU == "native")
324 return sys::getHostCPUName();
329 LLVM_ATTRIBUTE_UNUSED static std::string getFeaturesStr() {
330 SubtargetFeatures Features;
332 // If user asked for the 'native' CPU, we need to autodetect features.
333 // This is necessary for x86 where the CPU might not support all the
334 // features the autodetected CPU name lists in the target. For example,
335 // not all Sandybridge processors support AVX.
336 if (MCPU == "native") {
337 StringMap<bool> HostFeatures;
338 if (sys::getHostCPUFeatures(HostFeatures))
339 for (auto &F : HostFeatures)
340 Features.AddFeature(F.first(), F.second);
343 for (unsigned i = 0; i != MAttrs.size(); ++i)
344 Features.AddFeature(MAttrs[i]);
346 return Features.getString();
349 LLVM_ATTRIBUTE_UNUSED static std::vector<std::string> getFeatureList() {
350 SubtargetFeatures Features;
352 // If user asked for the 'native' CPU, we need to autodetect features.
353 // This is necessary for x86 where the CPU might not support all the
354 // features the autodetected CPU name lists in the target. For example,
355 // not all Sandybridge processors support AVX.
356 if (MCPU == "native") {
357 StringMap<bool> HostFeatures;
358 if (sys::getHostCPUFeatures(HostFeatures))
359 for (auto &F : HostFeatures)
360 Features.AddFeature(F.first(), F.second);
363 for (unsigned i = 0; i != MAttrs.size(); ++i)
364 Features.AddFeature(MAttrs[i]);
366 return Features.getFeatures();
369 /// Set function attributes of functions in Module M based on CPU,
370 /// Features, and command line flags.
371 LLVM_ATTRIBUTE_UNUSED static void
372 setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
374 auto &Ctx = F.getContext();
375 AttributeList Attrs = F.getAttributes();
376 AttrBuilder NewAttrs;
379 NewAttrs.addAttribute("target-cpu", CPU);
380 if (!Features.empty())
381 NewAttrs.addAttribute("target-features", Features);
382 if (FramePointerUsage.getNumOccurrences() > 0) {
383 if (FramePointerUsage == llvm::FramePointer::All)
384 NewAttrs.addAttribute("frame-pointer", "all");
385 else if (FramePointerUsage == llvm::FramePointer::NonLeaf)
386 NewAttrs.addAttribute("frame-pointer", "non-leaf");
387 else if (FramePointerUsage == llvm::FramePointer::None)
388 NewAttrs.addAttribute("frame-pointer", "none");
390 if (DisableTailCalls.getNumOccurrences() > 0)
391 NewAttrs.addAttribute("disable-tail-calls",
392 toStringRef(DisableTailCalls));
394 NewAttrs.addAttribute("stackrealign");
396 if (TrapFuncName.getNumOccurrences() > 0)
399 if (auto *Call = dyn_cast<CallInst>(&I))
400 if (const auto *F = Call->getCalledFunction())
401 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
402 F->getIntrinsicID() == Intrinsic::trap)
404 llvm::AttributeList::FunctionIndex,
405 Attribute::get(Ctx, "trap-func-name", TrapFuncName));
407 // Let NewAttrs override Attrs.
409 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));