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/Module.h"
21 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
22 #include "llvm//MC/SubtargetFeature.h"
23 #include "llvm/Support/CodeGen.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Host.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Target/TargetRecip.h"
33 MArch("march", cl::desc("Architecture to generate code for (see --version)"));
37 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
38 cl::value_desc("cpu-name"),
44 cl::desc("Target specific attributes (-mattr=help for details)"),
45 cl::value_desc("a1,+a2,-a3,..."));
48 RelocModel("relocation-model",
49 cl::desc("Choose relocation model"),
50 cl::init(Reloc::Default),
52 clEnumValN(Reloc::Default, "default",
53 "Target default relocation model"),
54 clEnumValN(Reloc::Static, "static",
55 "Non-relocatable code"),
56 clEnumValN(Reloc::PIC_, "pic",
57 "Fully relocatable, position independent code"),
58 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
59 "Relocatable external references, non-relocatable code"),
62 cl::opt<ThreadModel::Model>
63 TMModel("thread-model",
64 cl::desc("Choose threading model"),
65 cl::init(ThreadModel::POSIX),
66 cl::values(clEnumValN(ThreadModel::POSIX, "posix",
67 "POSIX thread model"),
68 clEnumValN(ThreadModel::Single, "single",
69 "Single thread model"),
72 cl::opt<llvm::CodeModel::Model>
74 cl::desc("Choose code model"),
75 cl::init(CodeModel::Default),
76 cl::values(clEnumValN(CodeModel::Default, "default",
77 "Target default code model"),
78 clEnumValN(CodeModel::Small, "small",
80 clEnumValN(CodeModel::Kernel, "kernel",
82 clEnumValN(CodeModel::Medium, "medium",
84 clEnumValN(CodeModel::Large, "large",
88 cl::opt<TargetMachine::CodeGenFileType>
89 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
90 cl::desc("Choose a file type (not all types are supported by all targets):"),
92 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
93 "Emit an assembly ('.s') file"),
94 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
95 "Emit a native object ('.o') file"),
96 clEnumValN(TargetMachine::CGFT_Null, "null",
97 "Emit nothing, for performance testing"),
101 EnableFPMAD("enable-fp-mad",
102 cl::desc("Enable less precise MAD instructions to be generated"),
106 DisableFPElim("disable-fp-elim",
107 cl::desc("Disable frame pointer elimination optimization"),
111 EnableUnsafeFPMath("enable-unsafe-fp-math",
112 cl::desc("Enable optimizations that may decrease FP precision"),
116 EnableNoInfsFPMath("enable-no-infs-fp-math",
117 cl::desc("Enable FP math optimizations that assume no +-Infs"),
121 EnableNoNaNsFPMath("enable-no-nans-fp-math",
122 cl::desc("Enable FP math optimizations that assume no NaNs"),
126 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
128 cl::desc("Force codegen to assume rounding mode can change dynamically"),
131 cl::opt<llvm::FloatABI::ABIType>
132 FloatABIForCalls("float-abi",
133 cl::desc("Choose float ABI type"),
134 cl::init(FloatABI::Default),
136 clEnumValN(FloatABI::Default, "default",
137 "Target default float ABI type"),
138 clEnumValN(FloatABI::Soft, "soft",
139 "Soft float ABI (implied by -soft-float)"),
140 clEnumValN(FloatABI::Hard, "hard",
141 "Hard float ABI (uses FP registers)"),
144 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
145 FuseFPOps("fp-contract",
146 cl::desc("Enable aggressive formation of fused FP ops"),
147 cl::init(FPOpFusion::Standard),
149 clEnumValN(FPOpFusion::Fast, "fast",
150 "Fuse FP ops whenever profitable"),
151 clEnumValN(FPOpFusion::Standard, "on",
152 "Only fuse 'blessed' FP ops."),
153 clEnumValN(FPOpFusion::Strict, "off",
154 "Only fuse FP ops when the result won't be effected."),
157 cl::list<std::string>
158 ReciprocalOps("recip",
160 cl::desc("Choose reciprocal operation types and parameters."),
161 cl::value_desc("all,none,default,divf,!vec-sqrtd,vec-divd:0,sqrt:9..."));
164 DontPlaceZerosInBSS("nozero-initialized-in-bss",
165 cl::desc("Don't place zero-initialized symbols into bss section"),
169 EnableGuaranteedTailCallOpt("tailcallopt",
170 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
174 DisableTailCalls("disable-tail-calls",
175 cl::desc("Never emit tail calls"),
179 OverrideStackAlignment("stack-alignment",
180 cl::desc("Override default stack alignment"),
184 TrapFuncName("trap-func", cl::Hidden,
185 cl::desc("Emit a call to trap function rather than a trap instruction"),
189 EnablePIE("enable-pie",
190 cl::desc("Assume the creation of a position independent executable."),
194 UseCtors("use-ctors",
195 cl::desc("Use .ctors instead of .init_array."),
198 cl::opt<std::string> StopAfter("stop-after",
199 cl::desc("Stop compilation after a specific pass"),
200 cl::value_desc("pass-name"),
202 cl::opt<std::string> StartAfter("start-after",
203 cl::desc("Resume compilation after a specific pass"),
204 cl::value_desc("pass-name"),
207 cl::opt<bool> DataSections("data-sections",
208 cl::desc("Emit data into separate sections"),
212 FunctionSections("function-sections",
213 cl::desc("Emit functions into separate sections"),
216 cl::opt<bool> UniqueSectionNames("unique-section-names",
217 cl::desc("Give unique names to every section"),
220 cl::opt<llvm::JumpTable::JumpTableType>
221 JTableType("jump-table-type",
222 cl::desc("Choose the type of Jump-Instruction Table for jumptable."),
223 cl::init(JumpTable::Single),
225 clEnumValN(JumpTable::Single, "single",
226 "Create a single table for all jumptable functions"),
227 clEnumValN(JumpTable::Arity, "arity",
228 "Create one table per number of parameters."),
229 clEnumValN(JumpTable::Simplified, "simplified",
230 "Create one table per simplified function type."),
231 clEnumValN(JumpTable::Full, "full",
232 "Create one table per unique function type."),
235 // Common utility function tightly tied to the options listed here. Initializes
236 // a TargetOptions object with CodeGen flags and returns it.
237 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
238 TargetOptions Options;
239 Options.LessPreciseFPMADOption = EnableFPMAD;
240 Options.AllowFPOpFusion = FuseFPOps;
241 Options.Reciprocals = TargetRecip(ReciprocalOps);
242 Options.UnsafeFPMath = EnableUnsafeFPMath;
243 Options.NoInfsFPMath = EnableNoInfsFPMath;
244 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
245 Options.HonorSignDependentRoundingFPMathOption =
246 EnableHonorSignDependentRoundingFPMath;
247 if (FloatABIForCalls != FloatABI::Default)
248 Options.FloatABIType = FloatABIForCalls;
249 Options.NoZerosInBSS = DontPlaceZerosInBSS;
250 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
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 if (DisableTailCalls.getNumOccurrences() > 0)
319 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
320 "disable-tail-calls",
321 toStringRef(DisableTailCalls));
323 // Let NewAttrs override Attrs.
324 NewAttrs = Attrs.addAttributes(Ctx, AttributeSet::FunctionIndex, NewAttrs);
325 F.setAttributes(NewAttrs);