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/IR/Module.h"
20 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
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"
27 #include "llvm/Target/TargetRecip.h"
32 MArch("march", cl::desc("Architecture to generate code for (see --version)"));
36 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
37 cl::value_desc("cpu-name"),
43 cl::desc("Target specific attributes (-mattr=help for details)"),
44 cl::value_desc("a1,+a2,-a3,..."));
47 RelocModel("relocation-model",
48 cl::desc("Choose relocation model"),
49 cl::init(Reloc::Default),
51 clEnumValN(Reloc::Default, "default",
52 "Target default relocation model"),
53 clEnumValN(Reloc::Static, "static",
54 "Non-relocatable code"),
55 clEnumValN(Reloc::PIC_, "pic",
56 "Fully relocatable, position independent code"),
57 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
58 "Relocatable external references, non-relocatable code"),
61 cl::opt<ThreadModel::Model>
62 TMModel("thread-model",
63 cl::desc("Choose threading model"),
64 cl::init(ThreadModel::POSIX),
65 cl::values(clEnumValN(ThreadModel::POSIX, "posix",
66 "POSIX thread model"),
67 clEnumValN(ThreadModel::Single, "single",
68 "Single thread model"),
71 cl::opt<llvm::CodeModel::Model>
73 cl::desc("Choose code model"),
74 cl::init(CodeModel::Default),
75 cl::values(clEnumValN(CodeModel::Default, "default",
76 "Target default code model"),
77 clEnumValN(CodeModel::Small, "small",
79 clEnumValN(CodeModel::Kernel, "kernel",
81 clEnumValN(CodeModel::Medium, "medium",
83 clEnumValN(CodeModel::Large, "large",
87 cl::opt<TargetMachine::CodeGenFileType>
88 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
89 cl::desc("Choose a file type (not all types are supported by all targets):"),
91 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
92 "Emit an assembly ('.s') file"),
93 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
94 "Emit a native object ('.o') file"),
95 clEnumValN(TargetMachine::CGFT_Null, "null",
96 "Emit nothing, for performance testing"),
100 EnableFPMAD("enable-fp-mad",
101 cl::desc("Enable less precise MAD instructions to be generated"),
105 DisableFPElim("disable-fp-elim",
106 cl::desc("Disable frame pointer elimination optimization"),
110 EnableUnsafeFPMath("enable-unsafe-fp-math",
111 cl::desc("Enable optimizations that may decrease FP precision"),
115 EnableNoInfsFPMath("enable-no-infs-fp-math",
116 cl::desc("Enable FP math optimizations that assume no +-Infs"),
120 EnableNoNaNsFPMath("enable-no-nans-fp-math",
121 cl::desc("Enable FP math optimizations that assume no NaNs"),
125 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
127 cl::desc("Force codegen to assume rounding mode can change dynamically"),
130 cl::opt<llvm::FloatABI::ABIType>
131 FloatABIForCalls("float-abi",
132 cl::desc("Choose float ABI type"),
133 cl::init(FloatABI::Default),
135 clEnumValN(FloatABI::Default, "default",
136 "Target default float ABI type"),
137 clEnumValN(FloatABI::Soft, "soft",
138 "Soft float ABI (implied by -soft-float)"),
139 clEnumValN(FloatABI::Hard, "hard",
140 "Hard float ABI (uses FP registers)"),
143 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
144 FuseFPOps("fp-contract",
145 cl::desc("Enable aggressive formation of fused FP ops"),
146 cl::init(FPOpFusion::Standard),
148 clEnumValN(FPOpFusion::Fast, "fast",
149 "Fuse FP ops whenever profitable"),
150 clEnumValN(FPOpFusion::Standard, "on",
151 "Only fuse 'blessed' FP ops."),
152 clEnumValN(FPOpFusion::Strict, "off",
153 "Only fuse FP ops when the result won't be effected."),
156 cl::list<std::string>
157 ReciprocalOps("recip",
159 cl::desc("Choose reciprocal operation types and parameters."),
160 cl::value_desc("all,none,default,divf,!vec-sqrtd,vec-divd:0,sqrt:9..."));
163 DontPlaceZerosInBSS("nozero-initialized-in-bss",
164 cl::desc("Don't place zero-initialized symbols into bss section"),
168 EnableGuaranteedTailCallOpt("tailcallopt",
169 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
173 DisableTailCalls("disable-tail-calls",
174 cl::desc("Never emit tail calls"),
178 OverrideStackAlignment("stack-alignment",
179 cl::desc("Override default stack alignment"),
183 TrapFuncName("trap-func", cl::Hidden,
184 cl::desc("Emit a call to trap function rather than a trap instruction"),
188 EnablePIE("enable-pie",
189 cl::desc("Assume the creation of a position independent executable."),
193 UseCtors("use-ctors",
194 cl::desc("Use .ctors instead of .init_array."),
197 cl::opt<std::string> StopAfter("stop-after",
198 cl::desc("Stop compilation after a specific pass"),
199 cl::value_desc("pass-name"),
201 cl::opt<std::string> StartAfter("start-after",
202 cl::desc("Resume compilation after a specific pass"),
203 cl::value_desc("pass-name"),
206 cl::opt<bool> DataSections("data-sections",
207 cl::desc("Emit data into separate sections"),
211 FunctionSections("function-sections",
212 cl::desc("Emit functions into separate sections"),
215 cl::opt<bool> UniqueSectionNames("unique-section-names",
216 cl::desc("Give unique names to every section"),
219 cl::opt<llvm::JumpTable::JumpTableType>
220 JTableType("jump-table-type",
221 cl::desc("Choose the type of Jump-Instruction Table for jumptable."),
222 cl::init(JumpTable::Single),
224 clEnumValN(JumpTable::Single, "single",
225 "Create a single table for all jumptable functions"),
226 clEnumValN(JumpTable::Arity, "arity",
227 "Create one table per number of parameters."),
228 clEnumValN(JumpTable::Simplified, "simplified",
229 "Create one table per simplified function type."),
230 clEnumValN(JumpTable::Full, "full",
231 "Create one table per unique function type."),
234 // Common utility function tightly tied to the options listed here. Initializes
235 // a TargetOptions object with CodeGen flags and returns it.
236 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
237 TargetOptions Options;
238 Options.LessPreciseFPMADOption = EnableFPMAD;
239 Options.AllowFPOpFusion = FuseFPOps;
240 Options.Reciprocals = TargetRecip(ReciprocalOps);
241 Options.UnsafeFPMath = EnableUnsafeFPMath;
242 Options.NoInfsFPMath = EnableNoInfsFPMath;
243 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
244 Options.HonorSignDependentRoundingFPMathOption =
245 EnableHonorSignDependentRoundingFPMath;
246 if (FloatABIForCalls != FloatABI::Default)
247 Options.FloatABIType = FloatABIForCalls;
248 Options.NoZerosInBSS = DontPlaceZerosInBSS;
249 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
250 Options.DisableTailCalls = DisableTailCalls;
251 Options.StackAlignmentOverride = OverrideStackAlignment;
252 Options.TrapFuncName = TrapFuncName;
253 Options.PositionIndependentExecutable = EnablePIE;
254 Options.UseInitArray = !UseCtors;
255 Options.DataSections = DataSections;
256 Options.FunctionSections = FunctionSections;
257 Options.UniqueSectionNames = UniqueSectionNames;
259 Options.MCOptions = InitMCTargetOptionsFromFlags();
260 Options.JTType = JTableType;
262 Options.ThreadModel = TMModel;
267 static inline std::string getCPUStr() {
268 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
269 // this will set the CPU to an empty string which tells the target to
270 // pick a basic default.
271 if (MCPU == "native")
272 return sys::getHostCPUName();
277 static inline std::string getFeaturesStr() {
278 SubtargetFeatures Features;
280 // If user asked for the 'native' CPU, we need to autodetect features.
281 // This is necessary for x86 where the CPU might not support all the
282 // features the autodetected CPU name lists in the target. For example,
283 // not all Sandybridge processors support AVX.
284 if (MCPU == "native") {
285 StringMap<bool> HostFeatures;
286 if (sys::getHostCPUFeatures(HostFeatures))
287 for (auto &F : HostFeatures)
288 Features.AddFeature(F.first(), F.second);
291 for (unsigned i = 0; i != MAttrs.size(); ++i)
292 Features.AddFeature(MAttrs[i]);
294 return Features.getString();
297 /// \brief Set function attributes of functions in Module M based on CPU,
298 /// Features, and command line flags.
299 static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
302 auto &Ctx = F.getContext();
303 AttributeSet Attrs = F.getAttributes(), NewAttrs;
306 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
309 if (!Features.empty())
310 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
311 "target-features", Features);
313 if (DisableFPElim.getNumOccurrences() > 0)
314 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
315 "no-frame-pointer-elim",
316 DisableFPElim ? "true" : "false");
318 // Let NewAttrs override Attrs.
319 NewAttrs = Attrs.addAttributes(Ctx, AttributeSet::FunctionIndex, NewAttrs);
320 F.setAttributes(NewAttrs);