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.inc"
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 : MArch("march",
32 : cl::desc("Architecture to generate code for (see --version)"));
33 :
34 : static 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 :
39 : static 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 :
44 : static 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 :
60 : LLVM_ATTRIBUTE_UNUSED static Optional<Reloc::Model> getRelocModel() {
61 25266 : if (RelocModel.getNumOccurrences()) {
62 : Reloc::Model R = RelocModel;
63 : return R;
64 : }
65 : return None;
66 : }
67 :
68 : static 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 :
75 : static cl::opt<llvm::CodeModel::Model> CMModel(
76 : "code-model", cl::desc("Choose code model"),
77 : cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
78 : clEnumValN(CodeModel::Small, "small", "Small code model"),
79 : clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
80 : clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
81 : clEnumValN(CodeModel::Large, "large", "Large code model")));
82 :
83 : LLVM_ATTRIBUTE_UNUSED static Optional<CodeModel::Model> getCodeModel() {
84 25393 : if (CMModel.getNumOccurrences()) {
85 : CodeModel::Model M = CMModel;
86 : return M;
87 : }
88 : return None;
89 : }
90 :
91 : static 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 :
106 : static 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 :
117 : static cl::opt<bool>
118 : DisableFPElim("disable-fp-elim",
119 : cl::desc("Disable frame pointer elimination optimization"),
120 : cl::init(false));
121 :
122 : static cl::opt<bool> EnableUnsafeFPMath(
123 : "enable-unsafe-fp-math",
124 : cl::desc("Enable optimizations that may decrease FP precision"),
125 : cl::init(false));
126 :
127 : static 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 :
132 : static 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 :
137 : static 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 :
143 : static 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 :
149 : static 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 :
161 : static 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 :
166 : static 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 :
175 : static 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 :
184 : static 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 :
189 : static 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 :
195 : static cl::opt<bool> DisableTailCalls("disable-tail-calls",
196 : cl::desc("Never emit tail calls"),
197 : cl::init(false));
198 :
199 : static cl::opt<bool> StackSymbolOrdering("stack-symbol-ordering",
200 : cl::desc("Order local stack symbols."),
201 : cl::init(true));
202 :
203 : static cl::opt<unsigned>
204 : OverrideStackAlignment("stack-alignment",
205 : cl::desc("Override default stack alignment"),
206 : cl::init(0));
207 :
208 : static cl::opt<bool>
209 : StackRealign("stackrealign",
210 : cl::desc("Force align the stack to the minimum alignment"),
211 : cl::init(false));
212 :
213 : static 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 :
218 : static cl::opt<bool> UseCtors("use-ctors",
219 : cl::desc("Use .ctors instead of .init_array."),
220 : cl::init(false));
221 :
222 : static 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 :
227 : static cl::opt<bool> DataSections("data-sections",
228 : cl::desc("Emit data into separate sections"),
229 : cl::init(false));
230 :
231 : static cl::opt<bool>
232 : FunctionSections("function-sections",
233 : cl::desc("Emit functions into separate sections"),
234 : cl::init(false));
235 :
236 : static cl::opt<bool> EmulatedTLS("emulated-tls",
237 : cl::desc("Use emulated TLS model"),
238 : cl::init(false));
239 :
240 : static cl::opt<bool>
241 : UniqueSectionNames("unique-section-names",
242 : cl::desc("Give unique names to every section"),
243 : cl::init(true));
244 :
245 : static 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 :
254 : static 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 :
261 : static cl::opt<bool> EnableStackSizeSection(
262 : "stack-size-section",
263 : cl::desc("Emit a section containing stack size metadata"), cl::init(false));
264 :
265 : static cl::opt<bool>
266 : EnableAddrsig("addrsig", cl::desc("Emit an address-significance table"),
267 : cl::init(false));
268 :
269 : // Common utility function tightly tied to the options listed here. Initializes
270 : // a TargetOptions object with CodeGen flags and returns it.
271 32827 : static TargetOptions InitTargetOptionsFromCodeGenFlags() {
272 32827 : TargetOptions Options;
273 32827 : Options.AllowFPOpFusion = FuseFPOps;
274 32827 : Options.UnsafeFPMath = EnableUnsafeFPMath;
275 32827 : Options.NoInfsFPMath = EnableNoInfsFPMath;
276 32827 : Options.NoNaNsFPMath = EnableNoNaNsFPMath;
277 32827 : Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath;
278 32827 : Options.NoTrappingFPMath = EnableNoTrappingFPMath;
279 32827 : Options.FPDenormalMode = DenormalMode;
280 32827 : Options.HonorSignDependentRoundingFPMathOption =
281 : EnableHonorSignDependentRoundingFPMath;
282 32827 : if (FloatABIForCalls != FloatABI::Default)
283 109 : Options.FloatABIType = FloatABIForCalls;
284 32827 : Options.NoZerosInBSS = DontPlaceZerosInBSS;
285 32827 : Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
286 32827 : Options.StackAlignmentOverride = OverrideStackAlignment;
287 32827 : Options.StackSymbolOrdering = StackSymbolOrdering;
288 32827 : Options.UseInitArray = !UseCtors;
289 32827 : Options.RelaxELFRelocations = RelaxELFRelocations;
290 32827 : Options.DataSections = DataSections;
291 32827 : Options.FunctionSections = FunctionSections;
292 32827 : Options.UniqueSectionNames = UniqueSectionNames;
293 32827 : Options.EmulatedTLS = EmulatedTLS;
294 32827 : Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0;
295 32827 : Options.ExceptionModel = ExceptionModel;
296 32827 : Options.EmitStackSizeSection = EnableStackSizeSection;
297 32827 : Options.EmitAddrsig = EnableAddrsig;
298 :
299 32827 : Options.MCOptions = InitMCTargetOptionsFromFlags();
300 :
301 32827 : Options.ThreadModel = TMModel;
302 32827 : Options.EABIVersion = EABIVersion;
303 32827 : Options.DebuggerTuning = DebuggerTuningOpt;
304 :
305 32827 : return Options;
306 : }
307 :
308 25336 : LLVM_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 25336 : if (MCPU == "native")
313 0 : return sys::getHostCPUName();
314 :
315 : return MCPU;
316 : }
317 :
318 24975 : LLVM_ATTRIBUTE_UNUSED static std::string getFeaturesStr() {
319 24975 : 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 24975 : if (MCPU == "native") {
326 0 : StringMap<bool> HostFeatures;
327 0 : if (sys::getHostCPUFeatures(HostFeatures))
328 0 : for (auto &F : HostFeatures)
329 0 : Features.AddFeature(F.first(), F.second);
330 : }
331 :
332 66658 : for (unsigned i = 0; i != MAttrs.size(); ++i)
333 8354 : Features.AddFeature(MAttrs[i]);
334 :
335 24975 : return Features.getString();
336 : }
337 :
338 194 : LLVM_ATTRIBUTE_UNUSED static std::vector<std::string> getFeatureList() {
339 194 : 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 194 : if (MCPU == "native") {
346 0 : StringMap<bool> HostFeatures;
347 0 : if (sys::getHostCPUFeatures(HostFeatures))
348 0 : for (auto &F : HostFeatures)
349 0 : Features.AddFeature(F.first(), F.second);
350 : }
351 :
352 388 : for (unsigned i = 0; i != MAttrs.size(); ++i)
353 0 : Features.AddFeature(MAttrs[i]);
354 :
355 194 : return Features.getFeatures();
356 : }
357 :
358 : /// Set function attributes of functions in Module M based on CPU,
359 : /// Features, and command line flags.
360 : LLVM_ATTRIBUTE_UNUSED static void
361 31938 : setFunctionAttributes(StringRef CPU, StringRef Features, Module &M) {
362 345205 : for (auto &F : M) {
363 313267 : auto &Ctx = F.getContext();
364 313267 : AttributeList Attrs = F.getAttributes();
365 : AttrBuilder NewAttrs;
366 :
367 313267 : if (!CPU.empty())
368 89841 : NewAttrs.addAttribute("target-cpu", CPU);
369 313267 : if (!Features.empty())
370 132475 : NewAttrs.addAttribute("target-features", Features);
371 313267 : if (DisableFPElim.getNumOccurrences() > 0)
372 : NewAttrs.addAttribute("no-frame-pointer-elim",
373 2058 : DisableFPElim ? "true" : "false");
374 313267 : if (DisableTailCalls.getNumOccurrences() > 0)
375 : NewAttrs.addAttribute("disable-tail-calls",
376 36 : toStringRef(DisableTailCalls));
377 313267 : if (StackRealign)
378 168 : NewAttrs.addAttribute("stackrealign");
379 :
380 313267 : if (TrapFuncName.getNumOccurrences() > 0)
381 19 : for (auto &B : F)
382 21 : for (auto &I : B)
383 : if (auto *Call = dyn_cast<CallInst>(&I))
384 : if (const auto *F = Call->getCalledFunction())
385 7 : if (F->getIntrinsicID() == Intrinsic::debugtrap ||
386 : F->getIntrinsicID() == Intrinsic::trap)
387 14 : Call->addAttribute(
388 : llvm::AttributeList::FunctionIndex,
389 : Attribute::get(Ctx, "trap-func-name", TrapFuncName));
390 :
391 : // Let NewAttrs override Attrs.
392 313267 : F.setAttributes(
393 : Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
394 : }
395 31938 : }
|