blob: 6535e068b6e819894ec17012cec0a6812432b057 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- CommandFlags.h - Command Line Flags Interface -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
12// flag duplication.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/Intrinsics.h"
19#include "llvm/IR/Module.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010020#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021#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 <string>
28using namespace llvm;
29
30static cl::opt<std::string>
31 MArch("march",
32 cl::desc("Architecture to generate code for (see --version)"));
33
34static cl::opt<std::string>
35 MCPU("mcpu",
36 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
37 cl::value_desc("cpu-name"), cl::init(""));
38
39static cl::list<std::string>
40 MAttrs("mattr", cl::CommaSeparated,
41 cl::desc("Target specific attributes (-mattr=help for details)"),
42 cl::value_desc("a1,+a2,-a3,..."));
43
44static cl::opt<Reloc::Model> RelocModel(
45 "relocation-model", cl::desc("Choose relocation model"),
46 cl::values(
47 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
48 clEnumValN(Reloc::PIC_, "pic",
49 "Fully relocatable, position independent code"),
50 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
51 "Relocatable external references, non-relocatable code"),
52 clEnumValN(Reloc::ROPI, "ropi",
53 "Code and read-only data relocatable, accessed PC-relative"),
54 clEnumValN(
55 Reloc::RWPI, "rwpi",
56 "Read-write data relocatable, accessed relative to static base"),
57 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
58 "Combination of ropi and rwpi")));
59
60LLVM_ATTRIBUTE_UNUSED static Optional<Reloc::Model> getRelocModel() {
61 if (RelocModel.getNumOccurrences()) {
62 Reloc::Model R = RelocModel;
63 return R;
64 }
65 return None;
66}
67
68static cl::opt<ThreadModel::Model> TMModel(
69 "thread-model", cl::desc("Choose threading model"),
70 cl::init(ThreadModel::POSIX),
71 cl::values(clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
72 clEnumValN(ThreadModel::Single, "single",
73 "Single thread model")));
74
75static cl::opt<llvm::CodeModel::Model> CMModel(
76 "code-model", cl::desc("Choose code model"),
Andrew Scull0372a572018-11-16 15:47:06 +000077 cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
78 clEnumValN(CodeModel::Small, "small", "Small code model"),
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
80 clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
81 clEnumValN(CodeModel::Large, "large", "Large code model")));
82
83LLVM_ATTRIBUTE_UNUSED static Optional<CodeModel::Model> getCodeModel() {
84 if (CMModel.getNumOccurrences()) {
85 CodeModel::Model M = CMModel;
86 return M;
87 }
88 return None;
89}
90
91static cl::opt<llvm::ExceptionHandling> ExceptionModel(
92 "exception-model", cl::desc("exception model"),
93 cl::init(ExceptionHandling::None),
94 cl::values(
95 clEnumValN(ExceptionHandling::None, "default",
96 "default exception handling model"),
97 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
98 "DWARF-like CFI based exception handling"),
99 clEnumValN(ExceptionHandling::SjLj, "sjlj", "SjLj exception handling"),
100 clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
101 clEnumValN(ExceptionHandling::WinEH, "wineh",
102 "Windows exception model"),
103 clEnumValN(ExceptionHandling::Wasm, "wasm",
104 "WebAssembly exception handling")));
105
106static cl::opt<TargetMachine::CodeGenFileType> FileType(
107 "filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
108 cl::desc(
109 "Choose a file type (not all types are supported by all targets):"),
110 cl::values(clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
111 "Emit an assembly ('.s') file"),
112 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
113 "Emit a native object ('.o') file"),
114 clEnumValN(TargetMachine::CGFT_Null, "null",
115 "Emit nothing, for performance testing")));
116
117static cl::opt<bool>
118 DisableFPElim("disable-fp-elim",
119 cl::desc("Disable frame pointer elimination optimization"),
120 cl::init(false));
121
122static cl::opt<bool> EnableUnsafeFPMath(
123 "enable-unsafe-fp-math",
124 cl::desc("Enable optimizations that may decrease FP precision"),
125 cl::init(false));
126
127static cl::opt<bool> EnableNoInfsFPMath(
128 "enable-no-infs-fp-math",
129 cl::desc("Enable FP math optimizations that assume no +-Infs"),
130 cl::init(false));
131
132static cl::opt<bool> EnableNoNaNsFPMath(
133 "enable-no-nans-fp-math",
134 cl::desc("Enable FP math optimizations that assume no NaNs"),
135 cl::init(false));
136
137static cl::opt<bool> EnableNoSignedZerosFPMath(
138 "enable-no-signed-zeros-fp-math",
139 cl::desc("Enable FP math optimizations that assume "
140 "the sign of 0 is insignificant"),
141 cl::init(false));
142
143static cl::opt<bool>
144 EnableNoTrappingFPMath("enable-no-trapping-fp-math",
145 cl::desc("Enable setting the FP exceptions build "
146 "attribute not to use exceptions"),
147 cl::init(false));
148
149static cl::opt<llvm::FPDenormal::DenormalMode> DenormalMode(
150 "denormal-fp-math",
151 cl::desc("Select which denormal numbers the code is permitted to require"),
152 cl::init(FPDenormal::IEEE),
153 cl::values(clEnumValN(FPDenormal::IEEE, "ieee",
154 "IEEE 754 denormal numbers"),
155 clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
156 "the sign of a flushed-to-zero number is preserved "
157 "in the sign of 0"),
158 clEnumValN(FPDenormal::PositiveZero, "positive-zero",
159 "denormals are flushed to positive zero")));
160
161static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
162 "enable-sign-dependent-rounding-fp-math", cl::Hidden,
163 cl::desc("Force codegen to assume rounding mode can change dynamically"),
164 cl::init(false));
165
166static cl::opt<llvm::FloatABI::ABIType> FloatABIForCalls(
167 "float-abi", cl::desc("Choose float ABI type"), cl::init(FloatABI::Default),
168 cl::values(clEnumValN(FloatABI::Default, "default",
169 "Target default float ABI type"),
170 clEnumValN(FloatABI::Soft, "soft",
171 "Soft float ABI (implied by -soft-float)"),
172 clEnumValN(FloatABI::Hard, "hard",
173 "Hard float ABI (uses FP registers)")));
174
175static cl::opt<llvm::FPOpFusion::FPOpFusionMode> FuseFPOps(
176 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
177 cl::init(FPOpFusion::Standard),
178 cl::values(
179 clEnumValN(FPOpFusion::Fast, "fast", "Fuse FP ops whenever profitable"),
180 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
181 clEnumValN(FPOpFusion::Strict, "off",
182 "Only fuse FP ops when the result won't be affected.")));
183
184static cl::opt<bool> DontPlaceZerosInBSS(
185 "nozero-initialized-in-bss",
186 cl::desc("Don't place zero-initialized symbols into bss section"),
187 cl::init(false));
188
189static cl::opt<bool> EnableGuaranteedTailCallOpt(
190 "tailcallopt",
191 cl::desc(
192 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
193 cl::init(false));
194
195static cl::opt<bool> DisableTailCalls("disable-tail-calls",
196 cl::desc("Never emit tail calls"),
197 cl::init(false));
198
199static cl::opt<bool> StackSymbolOrdering("stack-symbol-ordering",
200 cl::desc("Order local stack symbols."),
201 cl::init(true));
202
203static cl::opt<unsigned>
204 OverrideStackAlignment("stack-alignment",
205 cl::desc("Override default stack alignment"),
206 cl::init(0));
207
208static cl::opt<bool>
209 StackRealign("stackrealign",
210 cl::desc("Force align the stack to the minimum alignment"),
211 cl::init(false));
212
213static cl::opt<std::string> TrapFuncName(
214 "trap-func", cl::Hidden,
215 cl::desc("Emit a call to trap function rather than a trap instruction"),
216 cl::init(""));
217
218static cl::opt<bool> UseCtors("use-ctors",
219 cl::desc("Use .ctors instead of .init_array."),
220 cl::init(false));
221
222static cl::opt<bool> RelaxELFRelocations(
223 "relax-elf-relocations",
224 cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
225 cl::init(false));
226
227static cl::opt<bool> DataSections("data-sections",
228 cl::desc("Emit data into separate sections"),
229 cl::init(false));
230
231static cl::opt<bool>
232 FunctionSections("function-sections",
233 cl::desc("Emit functions into separate sections"),
234 cl::init(false));
235
236static cl::opt<bool> EmulatedTLS("emulated-tls",
237 cl::desc("Use emulated TLS model"),
238 cl::init(false));
239
240static cl::opt<bool>
241 UniqueSectionNames("unique-section-names",
242 cl::desc("Give unique names to every section"),
243 cl::init(true));
244
245static cl::opt<llvm::EABI>
246 EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"),
247 cl::init(EABI::Default),
248 cl::values(clEnumValN(EABI::Default, "default",
249 "Triple default EABI version"),
250 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
251 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
252 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
253
254static cl::opt<DebuggerKind> DebuggerTuningOpt(
255 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
256 cl::init(DebuggerKind::Default),
257 cl::values(clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
258 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
259 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
260
261static cl::opt<bool> EnableStackSizeSection(
262 "stack-size-section",
263 cl::desc("Emit a section containing stack size metadata"), cl::init(false));
264
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100265static cl::opt<bool>
266 EnableAddrsig("addrsig", cl::desc("Emit an address-significance table"),
267 cl::init(false));
268
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100269// Common utility function tightly tied to the options listed here. Initializes
270// a TargetOptions object with CodeGen flags and returns it.
271static TargetOptions InitTargetOptionsFromCodeGenFlags() {
272 TargetOptions Options;
273 Options.AllowFPOpFusion = FuseFPOps;
274 Options.UnsafeFPMath = EnableUnsafeFPMath;
275 Options.NoInfsFPMath = EnableNoInfsFPMath;
276 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
277 Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
278 Options.NoTrappingFPMath = EnableNoTrappingFPMath;
279 Options.FPDenormalMode = DenormalMode;
280 Options.HonorSignDependentRoundingFPMathOption =
281 EnableHonorSignDependentRoundingFPMath;
282 if (FloatABIForCalls != FloatABI::Default)
283 Options.FloatABIType = FloatABIForCalls;
284 Options.NoZerosInBSS = DontPlaceZerosInBSS;
285 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
286 Options.StackAlignmentOverride = OverrideStackAlignment;
287 Options.StackSymbolOrdering = StackSymbolOrdering;
288 Options.UseInitArray = !UseCtors;
289 Options.RelaxELFRelocations = RelaxELFRelocations;
290 Options.DataSections = DataSections;
291 Options.FunctionSections = FunctionSections;
292 Options.UniqueSectionNames = UniqueSectionNames;
293 Options.EmulatedTLS = EmulatedTLS;
294 Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0;
295 Options.ExceptionModel = ExceptionModel;
296 Options.EmitStackSizeSection = EnableStackSizeSection;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100297 Options.EmitAddrsig = EnableAddrsig;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298
299 Options.MCOptions = InitMCTargetOptionsFromFlags();
300
301 Options.ThreadModel = TMModel;
302 Options.EABIVersion = EABIVersion;
303 Options.DebuggerTuning = DebuggerTuningOpt;
304
305 return Options;
306}
307
308LLVM_ATTRIBUTE_UNUSED static std::string getCPUStr() {
309 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
310 // this will set the CPU to an empty string which tells the target to
311 // pick a basic default.
312 if (MCPU == "native")
313 return sys::getHostCPUName();
314
315 return MCPU;
316}
317
318LLVM_ATTRIBUTE_UNUSED static std::string getFeaturesStr() {
319 SubtargetFeatures Features;
320
321 // If user asked for the 'native' CPU, we need to autodetect features.
322 // This is necessary for x86 where the CPU might not support all the
323 // features the autodetected CPU name lists in the target. For example,
324 // not all Sandybridge processors support AVX.
325 if (MCPU == "native") {
326 StringMap<bool> HostFeatures;
327 if (sys::getHostCPUFeatures(HostFeatures))
328 for (auto &F : HostFeatures)
329 Features.AddFeature(F.first(), F.second);
330 }
331
332 for (unsigned i = 0; i != MAttrs.size(); ++i)
333 Features.AddFeature(MAttrs[i]);
334
335 return Features.getString();
336}
337
338LLVM_ATTRIBUTE_UNUSED static std::vector<std::string> getFeatureList() {
339 SubtargetFeatures Features;
340
341 // If user asked for the 'native' CPU, we need to autodetect features.
342 // This is necessary for x86 where the CPU might not support all the
343 // features the autodetected CPU name lists in the target. For example,
344 // not all Sandybridge processors support AVX.
345 if (MCPU == "native") {
346 StringMap<bool> HostFeatures;
347 if (sys::getHostCPUFeatures(HostFeatures))
348 for (auto &F : HostFeatures)
349 Features.AddFeature(F.first(), F.second);
350 }
351
352 for (unsigned i = 0; i != MAttrs.size(); ++i)
353 Features.AddFeature(MAttrs[i]);
354
355 return Features.getFeatures();
356}
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358/// Set function attributes of functions in Module M based on CPU,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359/// Features, and command line flags.
360LLVM_ATTRIBUTE_UNUSED static void
361setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
362 for (auto &F : M) {
363 auto &Ctx = F.getContext();
364 AttributeList Attrs = F.getAttributes();
365 AttrBuilder NewAttrs;
366
367 if (!CPU.empty())
368 NewAttrs.addAttribute("target-cpu", CPU);
369 if (!Features.empty())
370 NewAttrs.addAttribute("target-features", Features);
371 if (DisableFPElim.getNumOccurrences() > 0)
372 NewAttrs.addAttribute("no-frame-pointer-elim",
373 DisableFPElim ? "true" : "false");
374 if (DisableTailCalls.getNumOccurrences() > 0)
375 NewAttrs.addAttribute("disable-tail-calls",
376 toStringRef(DisableTailCalls));
377 if (StackRealign)
378 NewAttrs.addAttribute("stackrealign");
379
380 if (TrapFuncName.getNumOccurrences() > 0)
381 for (auto &B : F)
382 for (auto &I : B)
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(
388 llvm::AttributeList::FunctionIndex,
389 Attribute::get(Ctx, "trap-func-name", TrapFuncName));
390
391 // Let NewAttrs override Attrs.
392 F.setAttributes(
393 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
394 }
395}