LLVM  10.0.0
CommandFlags.inc
Go to the documentation of this file.
1 //===-- CommandFlags.h - 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/ADT/StringExtras.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
21 #include "llvm/Support/CodeGen.h"
23 #include "llvm/Support/Host.h"
26 #include <string>
27 using namespace llvm;
28 
30  MArch("march",
31  cl::desc("Architecture to generate code for (see --version)"));
32 
34  MCPU("mcpu",
35  cl::desc("Target a specific cpu type (-mcpu=help for details)"),
36  cl::value_desc("cpu-name"), cl::init(""));
37 
39  MAttrs("mattr", cl::CommaSeparated,
40  cl::desc("Target specific attributes (-mattr=help for details)"),
41  cl::value_desc("a1,+a2,-a3,..."));
42 
43 static cl::opt<Reloc::Model> RelocModel(
44  "relocation-model", cl::desc("Choose relocation model"),
45  cl::values(
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"),
53  clEnumValN(
54  Reloc::RWPI, "rwpi",
55  "Read-write data relocatable, accessed relative to static base"),
56  clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
57  "Combination of ropi and rwpi")));
58 
60  if (RelocModel.getNumOccurrences()) {
61  Reloc::Model R = RelocModel;
62  return R;
63  }
64  return None;
65 }
66 
67 static cl::opt<ThreadModel::Model> TMModel(
68  "thread-model", cl::desc("Choose threading model"),
70  cl::values(clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
72  "Single thread model")));
73 
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")));
81 
83  if (CMModel.getNumOccurrences()) {
84  CodeModel::Model M = CMModel;
85  return M;
86  }
87  return None;
88 }
89 
90 static cl::opt<llvm::ExceptionHandling> ExceptionModel(
91  "exception-model", cl::desc("exception model"),
93  cl::values(
95  "default exception handling model"),
97  "DWARF-like CFI based exception handling"),
98  clEnumValN(ExceptionHandling::SjLj, "sjlj", "SjLj exception handling"),
99  clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
101  "Windows exception model"),
103  "WebAssembly exception handling")));
104 
106  "filetype", cl::init(CGFT_AssemblyFile),
107  cl::desc(
108  "Choose a file type (not all types are supported by all targets):"),
110  "Emit an assembly ('.s') file"),
112  "Emit a native object ('.o') file"),
113  clEnumValN(CGFT_Null, "null",
114  "Emit nothing, for performance testing")));
115 
116 static cl::opt<llvm::FramePointer::FP> FramePointerUsage(
117  "frame-pointer", cl::desc("Specify frame pointer elimination optimization"),
119  cl::values(
121  "Disable frame pointer elimination"),
123  "Disable frame pointer elimination for non-leaf frame"),
125  "Enable frame pointer elimination")));
126 
127 static cl::opt<bool> EnableUnsafeFPMath(
128  "enable-unsafe-fp-math",
129  cl::desc("Enable optimizations that may decrease FP precision"),
130  cl::init(false));
131 
132 static cl::opt<bool> EnableNoInfsFPMath(
133  "enable-no-infs-fp-math",
134  cl::desc("Enable FP math optimizations that assume no +-Infs"),
135  cl::init(false));
136 
137 static cl::opt<bool> EnableNoNaNsFPMath(
138  "enable-no-nans-fp-math",
139  cl::desc("Enable FP math optimizations that assume no NaNs"),
140  cl::init(false));
141 
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"),
146  cl::init(false));
147 
148 static cl::opt<bool>
149  EnableNoTrappingFPMath("enable-no-trapping-fp-math",
150  cl::desc("Enable setting the FP exceptions build "
151  "attribute not to use exceptions"),
152  cl::init(false));
153 
154 static cl::opt<llvm::FPDenormal::DenormalMode> DenormalFPMath(
155  "denormal-fp-math",
156  cl::desc("Select which denormal numbers the code is permitted to require"),
159  "IEEE 754 denormal numbers"),
160  clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
161  "the sign of a flushed-to-zero number is preserved "
162  "in the sign of 0"),
163  clEnumValN(FPDenormal::PositiveZero, "positive-zero",
164  "denormals are flushed to positive zero")));
165 
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"),
169  cl::init(false));
170 
171 static cl::opt<llvm::FloatABI::ABIType> FloatABIForCalls(
172  "float-abi", cl::desc("Choose float ABI type"), cl::init(FloatABI::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)")));
179 
181  "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
183  cl::values(
184  clEnumValN(FPOpFusion::Fast, "fast", "Fuse FP ops whenever profitable"),
185  clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
187  "Only fuse FP ops when the result won't be affected.")));
188 
189 static cl::opt<bool> DontPlaceZerosInBSS(
190  "nozero-initialized-in-bss",
191  cl::desc("Don't place zero-initialized symbols into bss section"),
192  cl::init(false));
193 
194 static cl::opt<bool> EnableGuaranteedTailCallOpt(
195  "tailcallopt",
196  cl::desc(
197  "Turn fastcc calls into tail calls by (potentially) changing ABI."),
198  cl::init(false));
199 
200 static cl::opt<bool> DisableTailCalls("disable-tail-calls",
201  cl::desc("Never emit tail calls"),
202  cl::init(false));
203 
204 static cl::opt<bool> StackSymbolOrdering("stack-symbol-ordering",
205  cl::desc("Order local stack symbols."),
206  cl::init(true));
207 
208 static cl::opt<unsigned>
209  OverrideStackAlignment("stack-alignment",
210  cl::desc("Override default stack alignment"),
211  cl::init(0));
212 
213 static cl::opt<bool>
214  StackRealign("stackrealign",
215  cl::desc("Force align the stack to the minimum alignment"),
216  cl::init(false));
217 
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"),
221  cl::init(""));
222 
223 static cl::opt<bool> UseCtors("use-ctors",
224  cl::desc("Use .ctors instead of .init_array."),
225  cl::init(false));
226 
227 static cl::opt<bool> RelaxELFRelocations(
228  "relax-elf-relocations",
229  cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
230  cl::init(false));
231 
232 static cl::opt<bool> DataSections("data-sections",
233  cl::desc("Emit data into separate sections"),
234  cl::init(false));
235 
236 static cl::opt<bool>
237  FunctionSections("function-sections",
238  cl::desc("Emit functions into separate sections"),
239  cl::init(false));
240 
241 static cl::opt<unsigned> TLSSize("tls-size",
242  cl::desc("Bit size of immediate TLS offsets"),
243  cl::init(0));
244 
245 static cl::opt<bool> EmulatedTLS("emulated-tls",
246  cl::desc("Use emulated TLS model"),
247  cl::init(false));
248 
249 static cl::opt<bool>
250  UniqueSectionNames("unique-section-names",
251  cl::desc("Give unique names to every section"),
252  cl::init(true));
253 
254 static cl::opt<llvm::EABI>
255  EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"),
258  "Triple default EABI version"),
259  clEnumValN(EABI::EABI4, "4", "EABI version 4"),
260  clEnumValN(EABI::EABI5, "5", "EABI version 5"),
261  clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
262 
263 static cl::opt<DebuggerKind> DebuggerTuningOpt(
264  "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
266  cl::values(clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
267  clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
268  clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
269 
270 static cl::opt<bool> EnableStackSizeSection(
271  "stack-size-section",
272  cl::desc("Emit a section containing stack size metadata"), cl::init(false));
273 
274 static cl::opt<bool>
275  EnableAddrsig("addrsig", cl::desc("Emit an address-significance table"),
276  cl::init(false));
277 
278 static cl::opt<bool>
279  EnableDebugEntryValues("debug-entry-values",
280  cl::desc("Emit debug info about parameter's entry values"),
281  cl::init(false));
282 
283 static cl::opt<bool>
284  ForceDwarfFrameSection("force-dwarf-frame-section",
285  cl::desc("Always emit a debug frame section."),
286  cl::init(false));
287 
288 // Common utility function tightly tied to the options listed here. Initializes
289 // a TargetOptions object with CodeGen flags and returns it.
290 static TargetOptions InitTargetOptionsFromCodeGenFlags() {
291  TargetOptions Options;
292  Options.AllowFPOpFusion = FuseFPOps;
293  Options.UnsafeFPMath = EnableUnsafeFPMath;
294  Options.NoInfsFPMath = EnableNoInfsFPMath;
295  Options.NoNaNsFPMath = EnableNoNaNsFPMath;
296  Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
297  Options.NoTrappingFPMath = EnableNoTrappingFPMath;
298  Options.FPDenormalMode = DenormalFPMath;
300  EnableHonorSignDependentRoundingFPMath;
301  if (FloatABIForCalls != FloatABI::Default)
302  Options.FloatABIType = FloatABIForCalls;
303  Options.NoZerosInBSS = DontPlaceZerosInBSS;
304  Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
305  Options.StackAlignmentOverride = OverrideStackAlignment;
306  Options.StackSymbolOrdering = StackSymbolOrdering;
307  Options.UseInitArray = !UseCtors;
308  Options.RelaxELFRelocations = RelaxELFRelocations;
309  Options.DataSections = DataSections;
310  Options.FunctionSections = FunctionSections;
311  Options.UniqueSectionNames = UniqueSectionNames;
312  Options.TLSSize = TLSSize;
313  Options.EmulatedTLS = EmulatedTLS;
314  Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0;
315  Options.ExceptionModel = ExceptionModel;
316  Options.EmitStackSizeSection = EnableStackSizeSection;
317  Options.EmitAddrsig = EnableAddrsig;
318  Options.EnableDebugEntryValues = EnableDebugEntryValues;
319  Options.ForceDwarfFrameSection = ForceDwarfFrameSection;
320 
321  Options.MCOptions = InitMCTargetOptionsFromFlags();
322 
323  Options.ThreadModel = TMModel;
324  Options.EABIVersion = EABIVersion;
325  Options.DebuggerTuning = DebuggerTuningOpt;
326 
327  return Options;
328 }
329 
330 LLVM_ATTRIBUTE_UNUSED static std::string getCPUStr() {
331  // If user asked for the 'native' CPU, autodetect here. If autodection fails,
332  // this will set the CPU to an empty string which tells the target to
333  // pick a basic default.
334  if (MCPU == "native")
335  return sys::getHostCPUName();
336 
337  return MCPU;
338 }
339 
340 LLVM_ATTRIBUTE_UNUSED static std::string getFeaturesStr() {
341  SubtargetFeatures Features;
342 
343  // If user asked for the 'native' CPU, we need to autodetect features.
344  // This is necessary for x86 where the CPU might not support all the
345  // features the autodetected CPU name lists in the target. For example,
346  // not all Sandybridge processors support AVX.
347  if (MCPU == "native") {
348  StringMap<bool> HostFeatures;
349  if (sys::getHostCPUFeatures(HostFeatures))
350  for (auto &F : HostFeatures)
351  Features.AddFeature(F.first(), F.second);
352  }
353 
354  for (unsigned i = 0; i != MAttrs.size(); ++i)
355  Features.AddFeature(MAttrs[i]);
356 
357  return Features.getString();
358 }
359 
360 LLVM_ATTRIBUTE_UNUSED static std::vector<std::string> getFeatureList() {
361  SubtargetFeatures Features;
362 
363  // If user asked for the 'native' CPU, we need to autodetect features.
364  // This is necessary for x86 where the CPU might not support all the
365  // features the autodetected CPU name lists in the target. For example,
366  // not all Sandybridge processors support AVX.
367  if (MCPU == "native") {
368  StringMap<bool> HostFeatures;
369  if (sys::getHostCPUFeatures(HostFeatures))
370  for (auto &F : HostFeatures)
371  Features.AddFeature(F.first(), F.second);
372  }
373 
374  for (unsigned i = 0; i != MAttrs.size(); ++i)
375  Features.AddFeature(MAttrs[i]);
376 
377  return Features.getFeatures();
378 }
379 
380 /// Set function attributes of function \p F based on CPU, Features, and command
381 /// line flags.
382 LLVM_ATTRIBUTE_UNUSED static void
383 setFunctionAttributes(StringRef CPU, StringRef Features, Function &F) {
384  auto &Ctx = F.getContext();
386  AttrBuilder NewAttrs;
387 
388  if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
389  NewAttrs.addAttribute("target-cpu", CPU);
390  if (!Features.empty())
391  NewAttrs.addAttribute("target-features", Features);
392  if (FramePointerUsage.getNumOccurrences() > 0) {
393  if (FramePointerUsage == llvm::FramePointer::All)
394  NewAttrs.addAttribute("frame-pointer", "all");
395  else if (FramePointerUsage == llvm::FramePointer::NonLeaf)
396  NewAttrs.addAttribute("frame-pointer", "non-leaf");
397  else if (FramePointerUsage == llvm::FramePointer::None)
398  NewAttrs.addAttribute("frame-pointer", "none");
399  }
400  if (DisableTailCalls.getNumOccurrences() > 0)
401  NewAttrs.addAttribute("disable-tail-calls",
402  toStringRef(DisableTailCalls));
403  if (StackRealign)
404  NewAttrs.addAttribute("stackrealign");
405 
406  if (TrapFuncName.getNumOccurrences() > 0)
407  for (auto &B : F)
408  for (auto &I : B)
409  if (auto *Call = dyn_cast<CallInst>(&I))
410  if (const auto *F = Call->getCalledFunction())
411  if (F->getIntrinsicID() == Intrinsic::debugtrap ||
412  F->getIntrinsicID() == Intrinsic::trap)
413  Call->addAttribute(
415  Attribute::get(Ctx, "trap-func-name", TrapFuncName));
416 
417  // Let NewAttrs override Attrs.
418  F.setAttributes(
419  Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
420 }
421 
422 /// Set function attributes of functions in Module M based on CPU,
423 /// Features, and command line flags.
424 LLVM_ATTRIBUTE_UNUSED static void
425 setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
426  for (Function &F : M)
427  setFunctionAttributes(CPU, Features, F);
428 }
unsigned StackSymbolOrdering
StackSymbolOrdering - When true, this will allow CodeGen to order the local stack symbols (for code s...
const NoneType None
Definition: None.h:23
unsigned NoTrappingFPMath
NoTrappingFPMath - This flag is enabled when the -enable-no-trapping-fp-math is specified on the comm...
MCTargetOptions MCOptions
Machine level options.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned EnableDebugEntryValues
Emit debug info about parameter&#39;s entry values.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
EABI EABIVersion
EABIVersion - This flag specifies the EABI version.
unsigned DataSections
Emit data into separate sections.
setjmp/longjmp based exceptions
std::string getString() const
Returns features as a string.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
F(f)
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
ThreadModel::Model ThreadModel
ThreadModel - This flag specifies the type of threading model to assume for things like atomics...
ExceptionHandling ExceptionModel
What exception model to use.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
void AddFeature(StringRef String, bool Enable=true)
Adds Features.
unsigned NoNaNsFPMath
NoNaNsFPMath - This flag is enabled when the -enable-no-nans-fp-math flag is specified on the command...
unsigned FunctionSections
Emit functions into separate sections.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:223
unsigned UniqueSectionNames
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
static Reloc::Model getRelocModel(Optional< Reloc::Model > RM)
WebAssembly Exception Handling.
DebuggerKind DebuggerTuning
Which debugger to tune for.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
const std::vector< std::string > & getFeatures() const
Returns the vector of individual subtarget features.
unsigned UnsafeFPMath
UnsafeFPMath - This flag is enabled when the -enable-unsafe-fp-math flag is specified on the command ...
FPOpFusion::FPOpFusionMode AllowFPOpFusion
AllowFPOpFusion - This flag is set by the -fuse-fp-ops=xxx option.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned ExplicitEmulatedTLS
Whether -emulated-tls or -no-emulated-tls is set.
unsigned TLSSize
Bit size of immediate TLS offsets (0 == use the default).
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:692
unsigned EmitStackSizeSection
Emit section containing metadata on function stack sizes.
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:186
unsigned GuaranteedTailCallOpt
GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is specified on the commandline...
unsigned NoInfsFPMath
NoInfsFPMath - This flag is enabled when the -enable-no-infs-fp-math flag is specified on the command...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
FPDenormal::DenormalMode FPDenormalMode
FPDenormalMode - This flags specificies which denormal numbers the code is permitted to require...
unsigned NoZerosInBSS
NoZerosInBSS - By default some codegens place zero-initialized data to .bss section.
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:52
unsigned ForceDwarfFrameSection
Emit DWARF debug frame section.
FileType
Defines the file type this file represents.
Definition: InterfaceFile.h:57
unsigned UseInitArray
UseInitArray - Use .init_array instead of .ctors for static constructors.
Module.h This file contains the declarations for the Module class.
unsigned EmitAddrsig
Emit address-significance table.
unsigned StackAlignmentOverride
StackAlignmentOverride - Override default stack alignment for target.
Manages the enabling and disabling of subtarget specific features.
unsigned EmulatedTLS
EmulatedTLS - This flag enables emulated TLS model, using emutls function in the runtime library...
unsigned RelaxELFRelocations
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:667
StringRef getHostCPUName()
getHostCPUName - Get the LLVM name for the host CPU.
Definition: Host.cpp:1262
No exception support.
Windows Exception Handling.
#define I(x, y, z)
Definition: MD5.cpp:58
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:80
LLVM_NODISCARD AttributeList addAttributes(LLVMContext &C, unsigned Index, const AttrBuilder &B) const
Add attributes to the attribute set at the given index.
unsigned HonorSignDependentRoundingFPMathOption
HonorSignDependentRoundingFPMath - This returns true when the -enable-sign-dependent-rounding-fp-math...
unsigned NoSignedZerosFPMath
NoSignedZerosFPMath - This flag is enabled when the -enable-no-signed-zeros-fp-math is specified on t...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
bool getHostCPUFeatures(StringMap< bool > &Features)
getHostCPUFeatures - Get the LLVM names for the host CPU features.
Definition: Host.cpp:1570
DWARF-like instruction based exceptions.
FloatABI::ABIType FloatABIType
FloatABIType - This setting is set by -float-abi=xxx option is specfied on the command line...