1 //===-- CommandFlags.h - Command Line Flags Interface -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains codegen-specific flags that are shared between different
11 // command line tools. The tools "llc" and "opt" both use this file to prevent
14 //===----------------------------------------------------------------------===//
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.def"
21 #include "llvm/MC/SubtargetFeature.h"
22 #include "llvm/Support/CodeGen.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
30 static cl::opt<std::string>
32 cl::desc("Architecture to generate code for (see --version)"));
34 static cl::opt<std::string>
36 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
37 cl::value_desc("cpu-name"), cl::init(""));
39 static cl::list<std::string>
40 MAttrs("mattr", cl::CommaSeparated,
41 cl::desc("Target specific attributes (-mattr=help for details)"),
42 cl::value_desc("a1,+a2,-a3,..."));
44 static cl::opt<Reloc::Model> RelocModel(
45 "relocation-model", cl::desc("Choose relocation model"),
47 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
48 clEnumValN(Reloc::PIC_, "pic",
49 "Fully relocatable, position independent code"),
50 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
51 "Relocatable external references, non-relocatable code"),
52 clEnumValN(Reloc::ROPI, "ropi",
53 "Code and read-only data relocatable, accessed PC-relative"),
56 "Read-write data relocatable, accessed relative to static base"),
57 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
58 "Combination of ropi and rwpi")));
60 LLVM_ATTRIBUTE_UNUSED static Optional<Reloc::Model> getRelocModel() {
61 if (RelocModel.getNumOccurrences()) {
62 Reloc::Model R = RelocModel;
68 static cl::opt<ThreadModel::Model> TMModel(
69 "thread-model", cl::desc("Choose threading model"),
70 cl::init(ThreadModel::POSIX),
71 cl::values(clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
72 clEnumValN(ThreadModel::Single, "single",
73 "Single thread model")));
75 static cl::opt<llvm::CodeModel::Model> CMModel(
76 "code-model", cl::desc("Choose code model"),
77 cl::values(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")));
103 static cl::opt<TargetMachine::CodeGenFileType> FileType(
104 "filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
106 "Choose a file type (not all types are supported by all targets):"),
107 cl::values(clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
108 "Emit an assembly ('.s') file"),
109 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
110 "Emit a native object ('.o') file"),
111 clEnumValN(TargetMachine::CGFT_Null, "null",
112 "Emit nothing, for performance testing")));
115 DisableFPElim("disable-fp-elim",
116 cl::desc("Disable frame pointer elimination optimization"),
119 static cl::opt<bool> EnableUnsafeFPMath(
120 "enable-unsafe-fp-math",
121 cl::desc("Enable optimizations that may decrease FP precision"),
124 static cl::opt<bool> EnableNoInfsFPMath(
125 "enable-no-infs-fp-math",
126 cl::desc("Enable FP math optimizations that assume no +-Infs"),
129 static cl::opt<bool> EnableNoNaNsFPMath(
130 "enable-no-nans-fp-math",
131 cl::desc("Enable FP math optimizations that assume no NaNs"),
134 static cl::opt<bool> EnableNoSignedZerosFPMath(
135 "enable-no-signed-zeros-fp-math",
136 cl::desc("Enable FP math optimizations that assume "
137 "the sign of 0 is insignificant"),
141 EnableNoTrappingFPMath("enable-no-trapping-fp-math",
142 cl::desc("Enable setting the FP exceptions build "
143 "attribute not to use exceptions"),
146 static cl::opt<llvm::FPDenormal::DenormalMode> DenormalMode(
148 cl::desc("Select which denormal numbers the code is permitted to require"),
149 cl::init(FPDenormal::IEEE),
150 cl::values(clEnumValN(FPDenormal::IEEE, "ieee",
151 "IEEE 754 denormal numbers"),
152 clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
153 "the sign of a flushed-to-zero number is preserved "
155 clEnumValN(FPDenormal::PositiveZero, "positive-zero",
156 "denormals are flushed to positive zero")));
158 static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
159 "enable-sign-dependent-rounding-fp-math", cl::Hidden,
160 cl::desc("Force codegen to assume rounding mode can change dynamically"),
163 static cl::opt<llvm::FloatABI::ABIType> FloatABIForCalls(
164 "float-abi", cl::desc("Choose float ABI type"), cl::init(FloatABI::Default),
165 cl::values(clEnumValN(FloatABI::Default, "default",
166 "Target default float ABI type"),
167 clEnumValN(FloatABI::Soft, "soft",
168 "Soft float ABI (implied by -soft-float)"),
169 clEnumValN(FloatABI::Hard, "hard",
170 "Hard float ABI (uses FP registers)")));
172 static cl::opt<llvm::FPOpFusion::FPOpFusionMode> FuseFPOps(
173 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
174 cl::init(FPOpFusion::Standard),
176 clEnumValN(FPOpFusion::Fast, "fast", "Fuse FP ops whenever profitable"),
177 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
178 clEnumValN(FPOpFusion::Strict, "off",
179 "Only fuse FP ops when the result won't be affected.")));
181 static cl::opt<bool> DontPlaceZerosInBSS(
182 "nozero-initialized-in-bss",
183 cl::desc("Don't place zero-initialized symbols into bss section"),
186 static cl::opt<bool> EnableGuaranteedTailCallOpt(
189 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
192 static cl::opt<bool> DisableTailCalls("disable-tail-calls",
193 cl::desc("Never emit tail calls"),
196 static cl::opt<bool> StackSymbolOrdering("stack-symbol-ordering",
197 cl::desc("Order local stack symbols."),
200 static cl::opt<unsigned>
201 OverrideStackAlignment("stack-alignment",
202 cl::desc("Override default stack alignment"),
206 StackRealign("stackrealign",
207 cl::desc("Force align the stack to the minimum alignment"),
210 static cl::opt<std::string> TrapFuncName(
211 "trap-func", cl::Hidden,
212 cl::desc("Emit a call to trap function rather than a trap instruction"),
215 static cl::opt<bool> UseCtors("use-ctors",
216 cl::desc("Use .ctors instead of .init_array."),
219 static cl::opt<bool> RelaxELFRelocations(
220 "relax-elf-relocations",
221 cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
224 static cl::opt<bool> DataSections("data-sections",
225 cl::desc("Emit data into separate sections"),
229 FunctionSections("function-sections",
230 cl::desc("Emit functions into separate sections"),
233 static cl::opt<bool> EmulatedTLS("emulated-tls",
234 cl::desc("Use emulated TLS model"),
238 UniqueSectionNames("unique-section-names",
239 cl::desc("Give unique names to every section"),
242 static cl::opt<llvm::EABI>
243 EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"),
244 cl::init(EABI::Default),
245 cl::values(clEnumValN(EABI::Default, "default",
246 "Triple default EABI version"),
247 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
248 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
249 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
251 static cl::opt<DebuggerKind> DebuggerTuningOpt(
252 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
253 cl::init(DebuggerKind::Default),
254 cl::values(clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
255 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
256 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
258 static cl::opt<bool> EnableStackSizeSection(
259 "stack-size-section",
260 cl::desc("Emit a section containing stack size metadata"), cl::init(false));
262 // Common utility function tightly tied to the options listed here. Initializes
263 // a TargetOptions object with CodeGen flags and returns it.
264 static TargetOptions InitTargetOptionsFromCodeGenFlags() {
265 TargetOptions Options;
266 Options.AllowFPOpFusion = FuseFPOps;
267 Options.UnsafeFPMath = EnableUnsafeFPMath;
268 Options.NoInfsFPMath = EnableNoInfsFPMath;
269 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
270 Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
271 Options.NoTrappingFPMath = EnableNoTrappingFPMath;
272 Options.FPDenormalMode = DenormalMode;
273 Options.HonorSignDependentRoundingFPMathOption =
274 EnableHonorSignDependentRoundingFPMath;
275 if (FloatABIForCalls != FloatABI::Default)
276 Options.FloatABIType = FloatABIForCalls;
277 Options.NoZerosInBSS = DontPlaceZerosInBSS;
278 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
279 Options.StackAlignmentOverride = OverrideStackAlignment;
280 Options.StackSymbolOrdering = StackSymbolOrdering;
281 Options.UseInitArray = !UseCtors;
282 Options.RelaxELFRelocations = RelaxELFRelocations;
283 Options.DataSections = DataSections;
284 Options.FunctionSections = FunctionSections;
285 Options.UniqueSectionNames = UniqueSectionNames;
286 Options.EmulatedTLS = EmulatedTLS;
287 Options.ExceptionModel = ExceptionModel;
288 Options.EmitStackSizeSection = EnableStackSizeSection;
290 Options.MCOptions = InitMCTargetOptionsFromFlags();
292 Options.ThreadModel = TMModel;
293 Options.EABIVersion = EABIVersion;
294 Options.DebuggerTuning = DebuggerTuningOpt;
299 LLVM_ATTRIBUTE_UNUSED static std::string getCPUStr() {
300 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
301 // this will set the CPU to an empty string which tells the target to
302 // pick a basic default.
303 if (MCPU == "native")
304 return sys::getHostCPUName();
309 LLVM_ATTRIBUTE_UNUSED static std::string getFeaturesStr() {
310 SubtargetFeatures Features;
312 // If user asked for the 'native' CPU, we need to autodetect features.
313 // This is necessary for x86 where the CPU might not support all the
314 // features the autodetected CPU name lists in the target. For example,
315 // not all Sandybridge processors support AVX.
316 if (MCPU == "native") {
317 StringMap<bool> HostFeatures;
318 if (sys::getHostCPUFeatures(HostFeatures))
319 for (auto &F : HostFeatures)
320 Features.AddFeature(F.first(), F.second);
323 for (unsigned i = 0; i != MAttrs.size(); ++i)
324 Features.AddFeature(MAttrs[i]);
326 return Features.getString();
329 /// \brief Set function attributes of functions in Module M based on CPU,
330 /// Features, and command line flags.
331 LLVM_ATTRIBUTE_UNUSED static void
332 setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
334 auto &Ctx = F.getContext();
335 AttributeList Attrs = F.getAttributes();
336 AttrBuilder NewAttrs;
339 NewAttrs.addAttribute("target-cpu", CPU);
340 if (!Features.empty())
341 NewAttrs.addAttribute("target-features", Features);
342 if (DisableFPElim.getNumOccurrences() > 0)
343 NewAttrs.addAttribute("no-frame-pointer-elim",
344 DisableFPElim ? "true" : "false");
345 if (DisableTailCalls.getNumOccurrences() > 0)
346 NewAttrs.addAttribute("disable-tail-calls",
347 toStringRef(DisableTailCalls));
349 NewAttrs.addAttribute("stackrealign");
351 if (TrapFuncName.getNumOccurrences() > 0)
354 if (auto *Call = dyn_cast<CallInst>(&I))
355 if (const auto *F = Call->getCalledFunction())
356 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
357 F->getIntrinsicID() == Intrinsic::trap)
359 llvm::AttributeList::FunctionIndex,
360 Attribute::get(Ctx, "trap-func-name", TrapFuncName));
362 // Let NewAttrs override Attrs.
364 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));