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,..."));
50 RelocModel("relocation-model",
51 cl::desc("Choose relocation model"),
52 cl::init(Reloc::Default),
54 clEnumValN(Reloc::Default, "default",
55 "Target default relocation model"),
56 clEnumValN(Reloc::Static, "static",
57 "Non-relocatable code"),
58 clEnumValN(Reloc::PIC_, "pic",
59 "Fully relocatable, position independent code"),
60 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
61 "Relocatable external references, non-relocatable code"),
64 cl::opt<ThreadModel::Model>
65 TMModel("thread-model",
66 cl::desc("Choose threading model"),
67 cl::init(ThreadModel::POSIX),
68 cl::values(clEnumValN(ThreadModel::POSIX, "posix",
69 "POSIX thread model"),
70 clEnumValN(ThreadModel::Single, "single",
71 "Single thread model"),
74 cl::opt<llvm::CodeModel::Model>
76 cl::desc("Choose code model"),
77 cl::init(CodeModel::Default),
78 cl::values(clEnumValN(CodeModel::Default, "default",
79 "Target default code model"),
80 clEnumValN(CodeModel::Small, "small",
82 clEnumValN(CodeModel::Kernel, "kernel",
84 clEnumValN(CodeModel::Medium, "medium",
86 clEnumValN(CodeModel::Large, "large",
90 cl::opt<TargetMachine::CodeGenFileType>
91 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
92 cl::desc("Choose a file type (not all types are supported by all targets):"),
94 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
95 "Emit an assembly ('.s') file"),
96 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
97 "Emit a native object ('.o') file"),
98 clEnumValN(TargetMachine::CGFT_Null, "null",
99 "Emit nothing, for performance testing"),
103 EnableFPMAD("enable-fp-mad",
104 cl::desc("Enable less precise MAD instructions to be generated"),
108 DisableFPElim("disable-fp-elim",
109 cl::desc("Disable frame pointer elimination optimization"),
113 EnableUnsafeFPMath("enable-unsafe-fp-math",
114 cl::desc("Enable optimizations that may decrease FP precision"),
118 EnableNoInfsFPMath("enable-no-infs-fp-math",
119 cl::desc("Enable FP math optimizations that assume no +-Infs"),
123 EnableNoNaNsFPMath("enable-no-nans-fp-math",
124 cl::desc("Enable FP math optimizations that assume no NaNs"),
128 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
130 cl::desc("Force codegen to assume rounding mode can change dynamically"),
133 cl::opt<llvm::FloatABI::ABIType>
134 FloatABIForCalls("float-abi",
135 cl::desc("Choose float ABI type"),
136 cl::init(FloatABI::Default),
138 clEnumValN(FloatABI::Default, "default",
139 "Target default float ABI type"),
140 clEnumValN(FloatABI::Soft, "soft",
141 "Soft float ABI (implied by -soft-float)"),
142 clEnumValN(FloatABI::Hard, "hard",
143 "Hard float ABI (uses FP registers)"),
146 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
147 FuseFPOps("fp-contract",
148 cl::desc("Enable aggressive formation of fused FP ops"),
149 cl::init(FPOpFusion::Standard),
151 clEnumValN(FPOpFusion::Fast, "fast",
152 "Fuse FP ops whenever profitable"),
153 clEnumValN(FPOpFusion::Standard, "on",
154 "Only fuse 'blessed' FP ops."),
155 clEnumValN(FPOpFusion::Strict, "off",
156 "Only fuse FP ops when the result won't be affected."),
159 cl::list<std::string>
160 ReciprocalOps("recip",
162 cl::desc("Choose reciprocal operation types and parameters."),
163 cl::value_desc("all,none,default,divf,!vec-sqrtd,vec-divd:0,sqrt:9..."));
166 DontPlaceZerosInBSS("nozero-initialized-in-bss",
167 cl::desc("Don't place zero-initialized symbols into bss section"),
171 EnableGuaranteedTailCallOpt("tailcallopt",
172 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
176 DisableTailCalls("disable-tail-calls",
177 cl::desc("Never emit tail calls"),
181 OverrideStackAlignment("stack-alignment",
182 cl::desc("Override default stack alignment"),
186 StackRealign("stackrealign",
187 cl::desc("Force align the stack to the minimum alignment"),
191 TrapFuncName("trap-func", cl::Hidden,
192 cl::desc("Emit a call to trap function rather than a trap instruction"),
196 EnablePIE("enable-pie",
197 cl::desc("Assume the creation of a position independent executable."),
201 UseCtors("use-ctors",
202 cl::desc("Use .ctors instead of .init_array."),
205 cl::opt<std::string> StopAfter("stop-after",
206 cl::desc("Stop compilation after a specific pass"),
207 cl::value_desc("pass-name"),
209 cl::opt<std::string> StartAfter("start-after",
210 cl::desc("Resume compilation after a specific pass"),
211 cl::value_desc("pass-name"),
215 RunPass("run-pass", cl::desc("Run compiler only for one specific pass"),
216 cl::value_desc("pass-name"), cl::init(""));
218 cl::opt<bool> DataSections("data-sections",
219 cl::desc("Emit data into separate sections"),
223 FunctionSections("function-sections",
224 cl::desc("Emit functions into separate sections"),
227 cl::opt<bool> EmulatedTLS("emulated-tls",
228 cl::desc("Use emulated TLS model"),
231 cl::opt<bool> UniqueSectionNames("unique-section-names",
232 cl::desc("Give unique names to every section"),
235 cl::opt<llvm::JumpTable::JumpTableType>
236 JTableType("jump-table-type",
237 cl::desc("Choose the type of Jump-Instruction Table for jumptable."),
238 cl::init(JumpTable::Single),
240 clEnumValN(JumpTable::Single, "single",
241 "Create a single table for all jumptable functions"),
242 clEnumValN(JumpTable::Arity, "arity",
243 "Create one table per number of parameters."),
244 clEnumValN(JumpTable::Simplified, "simplified",
245 "Create one table per simplified function type."),
246 clEnumValN(JumpTable::Full, "full",
247 "Create one table per unique function type."),
250 // Common utility function tightly tied to the options listed here. Initializes
251 // a TargetOptions object with CodeGen flags and returns it.
252 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
253 TargetOptions Options;
254 Options.LessPreciseFPMADOption = EnableFPMAD;
255 Options.AllowFPOpFusion = FuseFPOps;
256 Options.Reciprocals = TargetRecip(ReciprocalOps);
257 Options.UnsafeFPMath = EnableUnsafeFPMath;
258 Options.NoInfsFPMath = EnableNoInfsFPMath;
259 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
260 Options.HonorSignDependentRoundingFPMathOption =
261 EnableHonorSignDependentRoundingFPMath;
262 if (FloatABIForCalls != FloatABI::Default)
263 Options.FloatABIType = FloatABIForCalls;
264 Options.NoZerosInBSS = DontPlaceZerosInBSS;
265 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
266 Options.StackAlignmentOverride = OverrideStackAlignment;
267 Options.PositionIndependentExecutable = EnablePIE;
268 Options.UseInitArray = !UseCtors;
269 Options.DataSections = DataSections;
270 Options.FunctionSections = FunctionSections;
271 Options.UniqueSectionNames = UniqueSectionNames;
272 Options.EmulatedTLS = EmulatedTLS;
274 Options.MCOptions = InitMCTargetOptionsFromFlags();
275 Options.JTType = JTableType;
277 Options.ThreadModel = TMModel;
282 static inline std::string getCPUStr() {
283 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
284 // this will set the CPU to an empty string which tells the target to
285 // pick a basic default.
286 if (MCPU == "native")
287 return sys::getHostCPUName();
292 static inline std::string getFeaturesStr() {
293 SubtargetFeatures Features;
295 // If user asked for the 'native' CPU, we need to autodetect features.
296 // This is necessary for x86 where the CPU might not support all the
297 // features the autodetected CPU name lists in the target. For example,
298 // not all Sandybridge processors support AVX.
299 if (MCPU == "native") {
300 StringMap<bool> HostFeatures;
301 if (sys::getHostCPUFeatures(HostFeatures))
302 for (auto &F : HostFeatures)
303 Features.AddFeature(F.first(), F.second);
306 for (unsigned i = 0; i != MAttrs.size(); ++i)
307 Features.AddFeature(MAttrs[i]);
309 return Features.getString();
312 /// \brief Set function attributes of functions in Module M based on CPU,
313 /// Features, and command line flags.
314 static inline void setFunctionAttributes(StringRef CPU, StringRef Features,
317 auto &Ctx = F.getContext();
318 AttributeSet Attrs = F.getAttributes(), NewAttrs;
321 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
324 if (!Features.empty())
325 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
326 "target-features", Features);
328 if (DisableFPElim.getNumOccurrences() > 0)
329 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
330 "no-frame-pointer-elim",
331 DisableFPElim ? "true" : "false");
333 if (DisableTailCalls.getNumOccurrences() > 0)
334 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
335 "disable-tail-calls",
336 toStringRef(DisableTailCalls));
339 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeSet::FunctionIndex,
342 if (TrapFuncName.getNumOccurrences() > 0)
345 if (auto *Call = dyn_cast<CallInst>(&I))
346 if (const auto *F = Call->getCalledFunction())
347 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
348 F->getIntrinsicID() == Intrinsic::trap)
349 Call->addAttribute(llvm::AttributeSet::FunctionIndex,
350 "trap-func-name", TrapFuncName);
352 // Let NewAttrs override Attrs.
353 NewAttrs = Attrs.addAttributes(Ctx, AttributeSet::FunctionIndex, NewAttrs);
354 F.setAttributes(NewAttrs);