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 #ifndef LLVM_CODEGEN_COMMANDFLAGS_H
17 #define LLVM_CODEGEN_COMMANDFLAGS_H
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
24 #include "llvm/MC/SubtargetFeature.h"
25 #include "llvm/Support/CodeGen.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Host.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetOptions.h"
34 MArch("march", cl::desc("Architecture to generate code for (see --version)"));
38 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
39 cl::value_desc("cpu-name"),
45 cl::desc("Target specific attributes (-mattr=help for details)"),
46 cl::value_desc("a1,+a2,-a3,..."));
48 cl::opt<Reloc::Model> RelocModel(
49 "relocation-model", cl::desc("Choose relocation model"),
51 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
52 clEnumValN(Reloc::PIC_, "pic",
53 "Fully relocatable, position independent code"),
54 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
55 "Relocatable external references, non-relocatable code"),
56 clEnumValN(Reloc::ROPI, "ropi",
57 "Code and read-only data relocatable, accessed PC-relative"),
58 clEnumValN(Reloc::RWPI, "rwpi",
59 "Read-write data relocatable, accessed relative to static base"),
60 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
61 "Combination of ropi and rwpi")));
63 static inline Optional<Reloc::Model> getRelocModel() {
64 if (RelocModel.getNumOccurrences()) {
65 Reloc::Model R = RelocModel;
71 cl::opt<ThreadModel::Model>
72 TMModel("thread-model",
73 cl::desc("Choose threading model"),
74 cl::init(ThreadModel::POSIX),
75 cl::values(clEnumValN(ThreadModel::POSIX, "posix",
76 "POSIX thread model"),
77 clEnumValN(ThreadModel::Single, "single",
78 "Single thread model")));
80 cl::opt<llvm::CodeModel::Model>
82 cl::desc("Choose code model"),
83 cl::init(CodeModel::Default),
84 cl::values(clEnumValN(CodeModel::Default, "default",
85 "Target default code model"),
86 clEnumValN(CodeModel::Small, "small",
88 clEnumValN(CodeModel::Kernel, "kernel",
90 clEnumValN(CodeModel::Medium, "medium",
92 clEnumValN(CodeModel::Large, "large",
93 "Large code model")));
95 cl::opt<llvm::ExceptionHandling>
96 ExceptionModel("exception-model",
97 cl::desc("exception model"),
98 cl::init(ExceptionHandling::None),
99 cl::values(clEnumValN(ExceptionHandling::None, "default",
100 "default exception handling model"),
101 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
102 "DWARF-like CFI based exception handling"),
103 clEnumValN(ExceptionHandling::SjLj, "sjlj",
104 "SjLj exception handling"),
105 clEnumValN(ExceptionHandling::ARM, "arm",
106 "ARM EHABI exceptions"),
107 clEnumValN(ExceptionHandling::WinEH, "wineh",
108 "Windows exception model")));
110 cl::opt<TargetMachine::CodeGenFileType>
111 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
112 cl::desc("Choose a file type (not all types are supported by all targets):"),
114 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
115 "Emit an assembly ('.s') file"),
116 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
117 "Emit a native object ('.o') file"),
118 clEnumValN(TargetMachine::CGFT_Null, "null",
119 "Emit nothing, for performance testing")));
122 DisableFPElim("disable-fp-elim",
123 cl::desc("Disable frame pointer elimination optimization"),
127 EnableUnsafeFPMath("enable-unsafe-fp-math",
128 cl::desc("Enable optimizations that may decrease FP precision"),
132 EnableNoInfsFPMath("enable-no-infs-fp-math",
133 cl::desc("Enable FP math optimizations that assume no +-Infs"),
137 EnableNoNaNsFPMath("enable-no-nans-fp-math",
138 cl::desc("Enable FP math optimizations that assume no NaNs"),
142 EnableNoSignedZerosFPMath("enable-no-signed-zeros-fp-math",
143 cl::desc("Enable FP math optimizations that assume "
144 "the sign of 0 is insignificant"),
148 EnableNoTrappingFPMath("enable-no-trapping-fp-math",
149 cl::desc("Enable setting the FP exceptions build "
150 "attribute not to use exceptions"),
153 cl::opt<llvm::FPDenormal::DenormalMode>
154 DenormalMode("denormal-fp-math",
155 cl::desc("Select which denormal numbers the code is permitted to require"),
156 cl::init(FPDenormal::IEEE),
158 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")));
167 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
169 cl::desc("Force codegen to assume rounding mode can change dynamically"),
172 cl::opt<llvm::FloatABI::ABIType>
173 FloatABIForCalls("float-abi",
174 cl::desc("Choose float ABI type"),
175 cl::init(FloatABI::Default),
177 clEnumValN(FloatABI::Default, "default",
178 "Target default float ABI type"),
179 clEnumValN(FloatABI::Soft, "soft",
180 "Soft float ABI (implied by -soft-float)"),
181 clEnumValN(FloatABI::Hard, "hard",
182 "Hard float ABI (uses FP registers)")));
184 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
185 FuseFPOps("fp-contract",
186 cl::desc("Enable aggressive formation of fused FP ops"),
187 cl::init(FPOpFusion::Standard),
189 clEnumValN(FPOpFusion::Fast, "fast",
190 "Fuse FP ops whenever profitable"),
191 clEnumValN(FPOpFusion::Standard, "on",
192 "Only fuse 'blessed' FP ops."),
193 clEnumValN(FPOpFusion::Strict, "off",
194 "Only fuse FP ops when the result won't be affected.")));
197 DontPlaceZerosInBSS("nozero-initialized-in-bss",
198 cl::desc("Don't place zero-initialized symbols into bss section"),
202 EnableGuaranteedTailCallOpt("tailcallopt",
203 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
207 DisableTailCalls("disable-tail-calls",
208 cl::desc("Never emit tail calls"),
212 StackSymbolOrdering("stack-symbol-ordering",
213 cl::desc("Order local stack symbols."),
217 OverrideStackAlignment("stack-alignment",
218 cl::desc("Override default stack alignment"),
222 StackRealign("stackrealign",
223 cl::desc("Force align the stack to the minimum alignment"),
227 TrapFuncName("trap-func", cl::Hidden,
228 cl::desc("Emit a call to trap function rather than a trap instruction"),
232 UseCtors("use-ctors",
233 cl::desc("Use .ctors instead of .init_array."),
236 cl::opt<bool> RelaxELFRelocations(
237 "relax-elf-relocations",
238 cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
241 cl::opt<bool> DataSections("data-sections",
242 cl::desc("Emit data into separate sections"),
246 FunctionSections("function-sections",
247 cl::desc("Emit functions into separate sections"),
250 cl::opt<bool> EmulatedTLS("emulated-tls",
251 cl::desc("Use emulated TLS model"),
254 cl::opt<bool> UniqueSectionNames("unique-section-names",
255 cl::desc("Give unique names to every section"),
258 cl::opt<llvm::EABI> EABIVersion(
259 "meabi", cl::desc("Set EABI type (default depends on triple):"),
260 cl::init(EABI::Default),
261 cl::values(clEnumValN(EABI::Default, "default",
262 "Triple default EABI version"),
263 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
264 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
265 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
267 cl::opt<DebuggerKind>
268 DebuggerTuningOpt("debugger-tune",
269 cl::desc("Tune debug info for a particular debugger"),
270 cl::init(DebuggerKind::Default),
272 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
273 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
274 clEnumValN(DebuggerKind::SCE, "sce",
275 "SCE targets (e.g. PS4)")));
277 // Common utility function tightly tied to the options listed here. Initializes
278 // a TargetOptions object with CodeGen flags and returns it.
279 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
280 TargetOptions Options;
281 Options.AllowFPOpFusion = FuseFPOps;
282 Options.UnsafeFPMath = EnableUnsafeFPMath;
283 Options.NoInfsFPMath = EnableNoInfsFPMath;
284 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
285 Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
286 Options.NoTrappingFPMath = EnableNoTrappingFPMath;
287 Options.FPDenormalMode = DenormalMode;
288 Options.HonorSignDependentRoundingFPMathOption =
289 EnableHonorSignDependentRoundingFPMath;
290 if (FloatABIForCalls != FloatABI::Default)
291 Options.FloatABIType = FloatABIForCalls;
292 Options.NoZerosInBSS = DontPlaceZerosInBSS;
293 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
294 Options.StackAlignmentOverride = OverrideStackAlignment;
295 Options.StackSymbolOrdering = StackSymbolOrdering;
296 Options.UseInitArray = !UseCtors;
297 Options.RelaxELFRelocations = RelaxELFRelocations;
298 Options.DataSections = DataSections;
299 Options.FunctionSections = FunctionSections;
300 Options.UniqueSectionNames = UniqueSectionNames;
301 Options.EmulatedTLS = EmulatedTLS;
302 Options.ExceptionModel = ExceptionModel;
304 Options.MCOptions = InitMCTargetOptionsFromFlags();
306 Options.ThreadModel = TMModel;
307 Options.EABIVersion = EABIVersion;
308 Options.DebuggerTuning = DebuggerTuningOpt;
313 static inline std::string getCPUStr() {
314 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
315 // this will set the CPU to an empty string which tells the target to
316 // pick a basic default.
317 if (MCPU == "native")
318 return sys::getHostCPUName();
323 static inline std::string getFeaturesStr() {
324 SubtargetFeatures Features;
326 // If user asked for the 'native' CPU, we need to autodetect features.
327 // This is necessary for x86 where the CPU might not support all the
328 // features the autodetected CPU name lists in the target. For example,
329 // not all Sandybridge processors support AVX.
330 if (MCPU == "native") {
331 StringMap<bool> HostFeatures;
332 if (sys::getHostCPUFeatures(HostFeatures))
333 for (auto &F : HostFeatures)
334 Features.AddFeature(F.first(), F.second);
337 for (unsigned i = 0; i != MAttrs.size(); ++i)
338 Features.AddFeature(MAttrs[i]);
340 return Features.getString();
343 /// \brief Set function attributes of functions in Module M based on CPU,
344 /// Features, and command line flags.
345 static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
348 auto &Ctx = F.getContext();
349 AttributeList Attrs = F.getAttributes();
350 AttrBuilder NewAttrs;
353 NewAttrs.addAttribute("target-cpu", CPU);
354 if (!Features.empty())
355 NewAttrs.addAttribute("target-features", Features);
356 if (DisableFPElim.getNumOccurrences() > 0)
357 NewAttrs.addAttribute("no-frame-pointer-elim",
358 DisableFPElim ? "true" : "false");
359 if (DisableTailCalls.getNumOccurrences() > 0)
360 NewAttrs.addAttribute("disable-tail-calls",
361 toStringRef(DisableTailCalls));
363 NewAttrs.addAttribute("stackrealign");
365 if (TrapFuncName.getNumOccurrences() > 0)
368 if (auto *Call = dyn_cast<CallInst>(&I))
369 if (const auto *F = Call->getCalledFunction())
370 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
371 F->getIntrinsicID() == Intrinsic::trap)
373 llvm::AttributeList::FunctionIndex,
374 Attribute::get(Ctx, "trap-func-name", TrapFuncName));
376 // Let NewAttrs override Attrs.
378 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));