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"
30 #include "llvm/Target/TargetRecip.h"
35 MArch("march", cl::desc("Architecture to generate code for (see --version)"));
39 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
40 cl::value_desc("cpu-name"),
46 cl::desc("Target specific attributes (-mattr=help for details)"),
47 cl::value_desc("a1,+a2,-a3,..."));
49 cl::opt<Reloc::Model> RelocModel(
50 "relocation-model", cl::desc("Choose relocation model"),
52 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
53 clEnumValN(Reloc::PIC_, "pic",
54 "Fully relocatable, position independent code"),
55 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
56 "Relocatable external references, non-relocatable code"),
59 static inline Optional<Reloc::Model> getRelocModel() {
60 if (RelocModel.getNumOccurrences()) {
61 Reloc::Model R = RelocModel;
67 cl::opt<ThreadModel::Model>
68 TMModel("thread-model",
69 cl::desc("Choose threading model"),
70 cl::init(ThreadModel::POSIX),
71 cl::values(clEnumValN(ThreadModel::POSIX, "posix",
72 "POSIX thread model"),
73 clEnumValN(ThreadModel::Single, "single",
74 "Single thread model"),
77 cl::opt<llvm::CodeModel::Model>
79 cl::desc("Choose code model"),
80 cl::init(CodeModel::Default),
81 cl::values(clEnumValN(CodeModel::Default, "default",
82 "Target default code model"),
83 clEnumValN(CodeModel::Small, "small",
85 clEnumValN(CodeModel::Kernel, "kernel",
87 clEnumValN(CodeModel::Medium, "medium",
89 clEnumValN(CodeModel::Large, "large",
93 cl::opt<llvm::ExceptionHandling>
94 ExceptionModel("exception-model",
95 cl::desc("exception model"),
96 cl::init(ExceptionHandling::None),
97 cl::values(clEnumValN(ExceptionHandling::None, "default",
98 "default exception handling model"),
99 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
100 "DWARF-like CFI based exception handling"),
101 clEnumValN(ExceptionHandling::SjLj, "sjlj",
102 "SjLj exception handling"),
103 clEnumValN(ExceptionHandling::ARM, "arm",
104 "ARM EHABI exceptions"),
105 clEnumValN(ExceptionHandling::WinEH, "wineh",
106 "Windows exception model"),
109 cl::opt<TargetMachine::CodeGenFileType>
110 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
111 cl::desc("Choose a file type (not all types are supported by all targets):"),
113 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
114 "Emit an assembly ('.s') file"),
115 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
116 "Emit a native object ('.o') file"),
117 clEnumValN(TargetMachine::CGFT_Null, "null",
118 "Emit nothing, for performance testing"),
122 EnableFPMAD("enable-fp-mad",
123 cl::desc("Enable less precise MAD instructions to be generated"),
127 DisableFPElim("disable-fp-elim",
128 cl::desc("Disable frame pointer elimination optimization"),
132 EnableUnsafeFPMath("enable-unsafe-fp-math",
133 cl::desc("Enable optimizations that may decrease FP precision"),
137 EnableNoInfsFPMath("enable-no-infs-fp-math",
138 cl::desc("Enable FP math optimizations that assume no +-Infs"),
142 EnableNoNaNsFPMath("enable-no-nans-fp-math",
143 cl::desc("Enable FP math optimizations that assume no NaNs"),
147 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
149 cl::desc("Force codegen to assume rounding mode can change dynamically"),
152 cl::opt<llvm::FloatABI::ABIType>
153 FloatABIForCalls("float-abi",
154 cl::desc("Choose float ABI type"),
155 cl::init(FloatABI::Default),
157 clEnumValN(FloatABI::Default, "default",
158 "Target default float ABI type"),
159 clEnumValN(FloatABI::Soft, "soft",
160 "Soft float ABI (implied by -soft-float)"),
161 clEnumValN(FloatABI::Hard, "hard",
162 "Hard float ABI (uses FP registers)"),
165 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
166 FuseFPOps("fp-contract",
167 cl::desc("Enable aggressive formation of fused FP ops"),
168 cl::init(FPOpFusion::Standard),
170 clEnumValN(FPOpFusion::Fast, "fast",
171 "Fuse FP ops whenever profitable"),
172 clEnumValN(FPOpFusion::Standard, "on",
173 "Only fuse 'blessed' FP ops."),
174 clEnumValN(FPOpFusion::Strict, "off",
175 "Only fuse FP ops when the result won't be affected."),
178 cl::list<std::string>
179 ReciprocalOps("recip",
181 cl::desc("Choose reciprocal operation types and parameters."),
182 cl::value_desc("all,none,default,divf,!vec-sqrtd,vec-divd:0,sqrt:9..."));
185 DontPlaceZerosInBSS("nozero-initialized-in-bss",
186 cl::desc("Don't place zero-initialized symbols into bss section"),
190 EnableGuaranteedTailCallOpt("tailcallopt",
191 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
195 DisableTailCalls("disable-tail-calls",
196 cl::desc("Never emit tail calls"),
200 StackSymbolOrdering("stack-symbol-ordering",
201 cl::desc("Order local stack symbols."),
205 OverrideStackAlignment("stack-alignment",
206 cl::desc("Override default stack alignment"),
210 StackRealign("stackrealign",
211 cl::desc("Force align the stack to the minimum alignment"),
215 TrapFuncName("trap-func", cl::Hidden,
216 cl::desc("Emit a call to trap function rather than a trap instruction"),
220 UseCtors("use-ctors",
221 cl::desc("Use .ctors instead of .init_array."),
224 cl::opt<std::string> StopAfter("stop-after",
225 cl::desc("Stop compilation after a specific pass"),
226 cl::value_desc("pass-name"),
228 cl::opt<std::string> StartAfter("start-after",
229 cl::desc("Resume compilation after a specific pass"),
230 cl::value_desc("pass-name"),
233 cl::opt<bool> DataSections("data-sections",
234 cl::desc("Emit data into separate sections"),
238 FunctionSections("function-sections",
239 cl::desc("Emit functions into separate sections"),
242 cl::opt<bool> EmulatedTLS("emulated-tls",
243 cl::desc("Use emulated TLS model"),
246 cl::opt<bool> UniqueSectionNames("unique-section-names",
247 cl::desc("Give unique names to every section"),
250 cl::opt<llvm::JumpTable::JumpTableType>
251 JTableType("jump-table-type",
252 cl::desc("Choose the type of Jump-Instruction Table for jumptable."),
253 cl::init(JumpTable::Single),
255 clEnumValN(JumpTable::Single, "single",
256 "Create a single table for all jumptable functions"),
257 clEnumValN(JumpTable::Arity, "arity",
258 "Create one table per number of parameters."),
259 clEnumValN(JumpTable::Simplified, "simplified",
260 "Create one table per simplified function type."),
261 clEnumValN(JumpTable::Full, "full",
262 "Create one table per unique function type."),
265 cl::opt<llvm::EABI> EABIVersion(
266 "meabi", cl::desc("Set EABI type (default depends on triple):"),
267 cl::init(EABI::Default),
268 cl::values(clEnumValN(EABI::Default, "default",
269 "Triple default EABI version"),
270 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
271 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
272 clEnumValN(EABI::GNU, "gnu", "EABI GNU"), clEnumValEnd));
274 cl::opt<DebuggerKind>
275 DebuggerTuningOpt("debugger-tune",
276 cl::desc("Tune debug info for a particular debugger"),
277 cl::init(DebuggerKind::Default),
279 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
280 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
281 clEnumValN(DebuggerKind::SCE, "sce",
282 "SCE targets (e.g. PS4)"),
285 // Common utility function tightly tied to the options listed here. Initializes
286 // a TargetOptions object with CodeGen flags and returns it.
287 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
288 TargetOptions Options;
289 Options.LessPreciseFPMADOption = EnableFPMAD;
290 Options.AllowFPOpFusion = FuseFPOps;
291 Options.Reciprocals = TargetRecip(ReciprocalOps);
292 Options.UnsafeFPMath = EnableUnsafeFPMath;
293 Options.NoInfsFPMath = EnableNoInfsFPMath;
294 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
295 Options.HonorSignDependentRoundingFPMathOption =
296 EnableHonorSignDependentRoundingFPMath;
297 if (FloatABIForCalls != FloatABI::Default)
298 Options.FloatABIType = FloatABIForCalls;
299 Options.NoZerosInBSS = DontPlaceZerosInBSS;
300 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
301 Options.StackAlignmentOverride = OverrideStackAlignment;
302 Options.StackSymbolOrdering = StackSymbolOrdering;
303 Options.UseInitArray = !UseCtors;
304 Options.DataSections = DataSections;
305 Options.FunctionSections = FunctionSections;
306 Options.UniqueSectionNames = UniqueSectionNames;
307 Options.EmulatedTLS = EmulatedTLS;
308 Options.ExceptionModel = ExceptionModel;
310 Options.MCOptions = InitMCTargetOptionsFromFlags();
311 Options.JTType = JTableType;
313 Options.ThreadModel = TMModel;
314 Options.EABIVersion = EABIVersion;
315 Options.DebuggerTuning = DebuggerTuningOpt;
320 static inline std::string getCPUStr() {
321 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
322 // this will set the CPU to an empty string which tells the target to
323 // pick a basic default.
324 if (MCPU == "native")
325 return sys::getHostCPUName();
330 static inline std::string getFeaturesStr() {
331 SubtargetFeatures Features;
333 // If user asked for the 'native' CPU, we need to autodetect features.
334 // This is necessary for x86 where the CPU might not support all the
335 // features the autodetected CPU name lists in the target. For example,
336 // not all Sandybridge processors support AVX.
337 if (MCPU == "native") {
338 StringMap<bool> HostFeatures;
339 if (sys::getHostCPUFeatures(HostFeatures))
340 for (auto &F : HostFeatures)
341 Features.AddFeature(F.first(), F.second);
344 for (unsigned i = 0; i != MAttrs.size(); ++i)
345 Features.AddFeature(MAttrs[i]);
347 return Features.getString();
350 /// \brief Set function attributes of functions in Module M based on CPU,
351 /// Features, and command line flags.
352 static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
355 auto &Ctx = F.getContext();
356 AttributeSet Attrs = F.getAttributes(), NewAttrs;
359 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
362 if (!Features.empty())
363 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
364 "target-features", Features);
366 if (DisableFPElim.getNumOccurrences() > 0)
367 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
368 "no-frame-pointer-elim",
369 DisableFPElim ? "true" : "false");
371 if (DisableTailCalls.getNumOccurrences() > 0)
372 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
373 "disable-tail-calls",
374 toStringRef(DisableTailCalls));
377 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
380 if (TrapFuncName.getNumOccurrences() > 0)
383 if (auto *Call = dyn_cast<CallInst>(&I))
384 if (const auto *F = Call->getCalledFunction())
385 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
386 F->getIntrinsicID() == Intrinsic::trap)
387 Call->addAttribute(llvm::AttributeSet::FunctionIndex,
388 "trap-func-name", TrapFuncName);
390 // Let NewAttrs override Attrs.
391 NewAttrs = Attrs.addAttributes(Ctx, AttributeSet::FunctionIndex, NewAttrs);
392 F.setAttributes(NewAttrs);