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