1 //===-- CommandFlags.cpp - Command Line Flags Interface ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains codegen-specific flags that are shared between different
10 // command line tools. The tools "llc" and "opt" both use this file to prevent
11 // flag duplication.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CodeGen/CommandFlags.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/MC/SubtargetFeature.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Host.h"
20
21 using namespace llvm;
22
23 #define CGOPT(TY, NAME) \
24 static cl::opt<TY> *NAME##View; \
25 TY codegen::get##NAME() { \
26 assert(NAME##View && "RegisterCodeGenFlags not created."); \
27 return *NAME##View; \
28 }
29
30 #define CGLIST(TY, NAME) \
31 static cl::list<TY> *NAME##View; \
32 std::vector<TY> codegen::get##NAME() { \
33 assert(NAME##View && "RegisterCodeGenFlags not created."); \
34 return *NAME##View; \
35 }
36
37 #define CGOPT_EXP(TY, NAME) \
38 CGOPT(TY, NAME) \
39 Optional<TY> codegen::getExplicit##NAME() { \
40 if (NAME##View->getNumOccurrences()) { \
41 TY res = *NAME##View; \
42 return res; \
43 } \
44 return None; \
45 }
46
CGOPT(std::string,MArch)47 CGOPT(std::string, MArch)
48 CGOPT(std::string, MCPU)
49 CGLIST(std::string, MAttrs)
50 CGOPT_EXP(Reloc::Model, RelocModel)
51 CGOPT(ThreadModel::Model, ThreadModel)
52 CGOPT_EXP(CodeModel::Model, CodeModel)
53 CGOPT(ExceptionHandling, ExceptionModel)
54 CGOPT_EXP(CodeGenFileType, FileType)
55 CGOPT(FramePointer::FP, FramePointerUsage)
56 CGOPT(bool, EnableUnsafeFPMath)
57 CGOPT(bool, EnableNoInfsFPMath)
58 CGOPT(bool, EnableNoNaNsFPMath)
59 CGOPT(bool, EnableNoSignedZerosFPMath)
60 CGOPT(bool, EnableNoTrappingFPMath)
61 CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath)
62 CGOPT(DenormalMode::DenormalModeKind, DenormalFP32Math)
63 CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
64 CGOPT(FloatABI::ABIType, FloatABIForCalls)
65 CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
66 CGOPT(bool, DontPlaceZerosInBSS)
67 CGOPT(bool, EnableGuaranteedTailCallOpt)
68 CGOPT(bool, DisableTailCalls)
69 CGOPT(bool, StackSymbolOrdering)
70 CGOPT(unsigned, OverrideStackAlignment)
71 CGOPT(bool, StackRealign)
72 CGOPT(std::string, TrapFuncName)
73 CGOPT(bool, UseCtors)
74 CGOPT(bool, RelaxELFRelocations)
75 CGOPT_EXP(bool, DataSections)
76 CGOPT_EXP(bool, FunctionSections)
77 CGOPT(std::string, BBSections)
78 CGOPT(unsigned, TLSSize)
79 CGOPT(bool, EmulatedTLS)
80 CGOPT(bool, UniqueSectionNames)
81 CGOPT(bool, UniqueBasicBlockSectionNames)
82 CGOPT(EABI, EABIVersion)
83 CGOPT(DebuggerKind, DebuggerTuningOpt)
84 CGOPT(bool, EnableStackSizeSection)
85 CGOPT(bool, EnableAddrsig)
86 CGOPT(bool, EmitCallSiteInfo)
87 CGOPT(bool, EnableDebugEntryValues)
88 CGOPT(bool, ForceDwarfFrameSection)
89 CGOPT(bool, XRayOmitFunctionIndex)
90
91 codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
92 #define CGBINDOPT(NAME) \
93 do { \
94 NAME##View = std::addressof(NAME); \
95 } while (0)
96
97 static cl::opt<std::string> MArch(
98 "march", cl::desc("Architecture to generate code for (see --version)"));
99 CGBINDOPT(MArch);
100
101 static cl::opt<std::string> MCPU(
102 "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
103 cl::value_desc("cpu-name"), cl::init(""));
104 CGBINDOPT(MCPU);
105
106 static cl::list<std::string> MAttrs(
107 "mattr", cl::CommaSeparated,
108 cl::desc("Target specific attributes (-mattr=help for details)"),
109 cl::value_desc("a1,+a2,-a3,..."));
110 CGBINDOPT(MAttrs);
111
112 static cl::opt<Reloc::Model> RelocModel(
113 "relocation-model", cl::desc("Choose relocation model"),
114 cl::values(
115 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
116 clEnumValN(Reloc::PIC_, "pic",
117 "Fully relocatable, position independent code"),
118 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
119 "Relocatable external references, non-relocatable code"),
120 clEnumValN(
121 Reloc::ROPI, "ropi",
122 "Code and read-only data relocatable, accessed PC-relative"),
123 clEnumValN(
124 Reloc::RWPI, "rwpi",
125 "Read-write data relocatable, accessed relative to static base"),
126 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
127 "Combination of ropi and rwpi")));
128 CGBINDOPT(RelocModel);
129
130 static cl::opt<ThreadModel::Model> ThreadModel(
131 "thread-model", cl::desc("Choose threading model"),
132 cl::init(ThreadModel::POSIX),
133 cl::values(
134 clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
135 clEnumValN(ThreadModel::Single, "single", "Single thread model")));
136 CGBINDOPT(ThreadModel);
137
138 static cl::opt<CodeModel::Model> CodeModel(
139 "code-model", cl::desc("Choose code model"),
140 cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
141 clEnumValN(CodeModel::Small, "small", "Small code model"),
142 clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
143 clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
144 clEnumValN(CodeModel::Large, "large", "Large code model")));
145 CGBINDOPT(CodeModel);
146
147 static cl::opt<ExceptionHandling> ExceptionModel(
148 "exception-model", cl::desc("exception model"),
149 cl::init(ExceptionHandling::None),
150 cl::values(
151 clEnumValN(ExceptionHandling::None, "default",
152 "default exception handling model"),
153 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
154 "DWARF-like CFI based exception handling"),
155 clEnumValN(ExceptionHandling::SjLj, "sjlj",
156 "SjLj exception handling"),
157 clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
158 clEnumValN(ExceptionHandling::WinEH, "wineh",
159 "Windows exception model"),
160 clEnumValN(ExceptionHandling::Wasm, "wasm",
161 "WebAssembly exception handling")));
162 CGBINDOPT(ExceptionModel);
163
164 static cl::opt<CodeGenFileType> FileType(
165 "filetype", cl::init(CGFT_AssemblyFile),
166 cl::desc(
167 "Choose a file type (not all types are supported by all targets):"),
168 cl::values(
169 clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"),
170 clEnumValN(CGFT_ObjectFile, "obj",
171 "Emit a native object ('.o') file"),
172 clEnumValN(CGFT_Null, "null",
173 "Emit nothing, for performance testing")));
174 CGBINDOPT(FileType);
175
176 static cl::opt<FramePointer::FP> FramePointerUsage(
177 "frame-pointer",
178 cl::desc("Specify frame pointer elimination optimization"),
179 cl::init(FramePointer::None),
180 cl::values(
181 clEnumValN(FramePointer::All, "all",
182 "Disable frame pointer elimination"),
183 clEnumValN(FramePointer::NonLeaf, "non-leaf",
184 "Disable frame pointer elimination for non-leaf frame"),
185 clEnumValN(FramePointer::None, "none",
186 "Enable frame pointer elimination")));
187 CGBINDOPT(FramePointerUsage);
188
189 static cl::opt<bool> EnableUnsafeFPMath(
190 "enable-unsafe-fp-math",
191 cl::desc("Enable optimizations that may decrease FP precision"),
192 cl::init(false));
193 CGBINDOPT(EnableUnsafeFPMath);
194
195 static cl::opt<bool> EnableNoInfsFPMath(
196 "enable-no-infs-fp-math",
197 cl::desc("Enable FP math optimizations that assume no +-Infs"),
198 cl::init(false));
199 CGBINDOPT(EnableNoInfsFPMath);
200
201 static cl::opt<bool> EnableNoNaNsFPMath(
202 "enable-no-nans-fp-math",
203 cl::desc("Enable FP math optimizations that assume no NaNs"),
204 cl::init(false));
205 CGBINDOPT(EnableNoNaNsFPMath);
206
207 static cl::opt<bool> EnableNoSignedZerosFPMath(
208 "enable-no-signed-zeros-fp-math",
209 cl::desc("Enable FP math optimizations that assume "
210 "the sign of 0 is insignificant"),
211 cl::init(false));
212 CGBINDOPT(EnableNoSignedZerosFPMath);
213
214 static cl::opt<bool> EnableNoTrappingFPMath(
215 "enable-no-trapping-fp-math",
216 cl::desc("Enable setting the FP exceptions build "
217 "attribute not to use exceptions"),
218 cl::init(false));
219 CGBINDOPT(EnableNoTrappingFPMath);
220
221 static const auto DenormFlagEnumOptions =
222 cl::values(clEnumValN(DenormalMode::IEEE, "ieee",
223 "IEEE 754 denormal numbers"),
224 clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
225 "the sign of a flushed-to-zero number is preserved "
226 "in the sign of 0"),
227 clEnumValN(DenormalMode::PositiveZero, "positive-zero",
228 "denormals are flushed to positive zero"));
229
230 // FIXME: Doesn't have way to specify separate input and output modes.
231 static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
232 "denormal-fp-math",
233 cl::desc("Select which denormal numbers the code is permitted to require"),
234 cl::init(DenormalMode::IEEE),
235 DenormFlagEnumOptions);
236 CGBINDOPT(DenormalFPMath);
237
238 static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math(
239 "denormal-fp-math-f32",
240 cl::desc("Select which denormal numbers the code is permitted to require for float"),
241 cl::init(DenormalMode::Invalid),
242 DenormFlagEnumOptions);
243 CGBINDOPT(DenormalFP32Math);
244
245 static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
246 "enable-sign-dependent-rounding-fp-math", cl::Hidden,
247 cl::desc("Force codegen to assume rounding mode can change dynamically"),
248 cl::init(false));
249 CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
250
251 static cl::opt<FloatABI::ABIType> FloatABIForCalls(
252 "float-abi", cl::desc("Choose float ABI type"),
253 cl::init(FloatABI::Default),
254 cl::values(clEnumValN(FloatABI::Default, "default",
255 "Target default float ABI type"),
256 clEnumValN(FloatABI::Soft, "soft",
257 "Soft float ABI (implied by -soft-float)"),
258 clEnumValN(FloatABI::Hard, "hard",
259 "Hard float ABI (uses FP registers)")));
260 CGBINDOPT(FloatABIForCalls);
261
262 static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
263 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
264 cl::init(FPOpFusion::Standard),
265 cl::values(
266 clEnumValN(FPOpFusion::Fast, "fast",
267 "Fuse FP ops whenever profitable"),
268 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
269 clEnumValN(FPOpFusion::Strict, "off",
270 "Only fuse FP ops when the result won't be affected.")));
271 CGBINDOPT(FuseFPOps);
272
273 static cl::opt<bool> DontPlaceZerosInBSS(
274 "nozero-initialized-in-bss",
275 cl::desc("Don't place zero-initialized symbols into bss section"),
276 cl::init(false));
277 CGBINDOPT(DontPlaceZerosInBSS);
278
279 static cl::opt<bool> EnableGuaranteedTailCallOpt(
280 "tailcallopt",
281 cl::desc(
282 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
283 cl::init(false));
284 CGBINDOPT(EnableGuaranteedTailCallOpt);
285
286 static cl::opt<bool> DisableTailCalls(
287 "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
288 CGBINDOPT(DisableTailCalls);
289
290 static cl::opt<bool> StackSymbolOrdering(
291 "stack-symbol-ordering", cl::desc("Order local stack symbols."),
292 cl::init(true));
293 CGBINDOPT(StackSymbolOrdering);
294
295 static cl::opt<unsigned> OverrideStackAlignment(
296 "stack-alignment", cl::desc("Override default stack alignment"),
297 cl::init(0));
298 CGBINDOPT(OverrideStackAlignment);
299
300 static cl::opt<bool> StackRealign(
301 "stackrealign",
302 cl::desc("Force align the stack to the minimum alignment"),
303 cl::init(false));
304 CGBINDOPT(StackRealign);
305
306 static cl::opt<std::string> TrapFuncName(
307 "trap-func", cl::Hidden,
308 cl::desc("Emit a call to trap function rather than a trap instruction"),
309 cl::init(""));
310 CGBINDOPT(TrapFuncName);
311
312 static cl::opt<bool> UseCtors("use-ctors",
313 cl::desc("Use .ctors instead of .init_array."),
314 cl::init(false));
315 CGBINDOPT(UseCtors);
316
317 static cl::opt<bool> RelaxELFRelocations(
318 "relax-elf-relocations",
319 cl::desc(
320 "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
321 cl::init(false));
322 CGBINDOPT(RelaxELFRelocations);
323
324 static cl::opt<bool> DataSections(
325 "data-sections", cl::desc("Emit data into separate sections"),
326 cl::init(false));
327 CGBINDOPT(DataSections);
328
329 static cl::opt<bool> FunctionSections(
330 "function-sections", cl::desc("Emit functions into separate sections"),
331 cl::init(false));
332 CGBINDOPT(FunctionSections);
333
334 static cl::opt<std::string> BBSections(
335 "basicblock-sections",
336 cl::desc("Emit basic blocks into separate sections"),
337 cl::value_desc("all | <function list (file)> | labels | none"),
338 cl::init("none"));
339 CGBINDOPT(BBSections);
340
341 static cl::opt<unsigned> TLSSize(
342 "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
343 CGBINDOPT(TLSSize);
344
345 static cl::opt<bool> EmulatedTLS(
346 "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
347 CGBINDOPT(EmulatedTLS);
348
349 static cl::opt<bool> UniqueSectionNames(
350 "unique-section-names", cl::desc("Give unique names to every section"),
351 cl::init(true));
352 CGBINDOPT(UniqueSectionNames);
353
354 static cl::opt<bool> UniqueBasicBlockSectionNames(
355 "unique-bb-section-names",
356 cl::desc("Give unique names to every basic block section"),
357 cl::init(false));
358 CGBINDOPT(UniqueBasicBlockSectionNames);
359
360 static cl::opt<EABI> EABIVersion(
361 "meabi", cl::desc("Set EABI type (default depends on triple):"),
362 cl::init(EABI::Default),
363 cl::values(
364 clEnumValN(EABI::Default, "default", "Triple default EABI version"),
365 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
366 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
367 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
368 CGBINDOPT(EABIVersion);
369
370 static cl::opt<DebuggerKind> DebuggerTuningOpt(
371 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
372 cl::init(DebuggerKind::Default),
373 cl::values(
374 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
375 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
376 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
377 CGBINDOPT(DebuggerTuningOpt);
378
379 static cl::opt<bool> EnableStackSizeSection(
380 "stack-size-section",
381 cl::desc("Emit a section containing stack size metadata"),
382 cl::init(false));
383 CGBINDOPT(EnableStackSizeSection);
384
385 static cl::opt<bool> EnableAddrsig(
386 "addrsig", cl::desc("Emit an address-significance table"),
387 cl::init(false));
388 CGBINDOPT(EnableAddrsig);
389
390 static cl::opt<bool> EmitCallSiteInfo(
391 "emit-call-site-info",
392 cl::desc(
393 "Emit call site debug information, if debug information is enabled."),
394 cl::init(false));
395 CGBINDOPT(EmitCallSiteInfo);
396
397 static cl::opt<bool> EnableDebugEntryValues(
398 "debug-entry-values",
399 cl::desc("Enable debug info for the debug entry values."),
400 cl::init(false));
401 CGBINDOPT(EnableDebugEntryValues);
402
403 static cl::opt<bool> ForceDwarfFrameSection(
404 "force-dwarf-frame-section",
405 cl::desc("Always emit a debug frame section."), cl::init(false));
406 CGBINDOPT(ForceDwarfFrameSection);
407
408 static cl::opt<bool> XRayOmitFunctionIndex(
409 "no-xray-index", cl::desc("Don't emit xray_fn_idx section"),
410 cl::init(false));
411 CGBINDOPT(XRayOmitFunctionIndex);
412
413 #undef CGBINDOPT
414
415 mc::RegisterMCTargetOptionsFlags();
416 }
417
418 llvm::BasicBlockSection
getBBSectionsMode(llvm::TargetOptions & Options)419 codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
420 if (getBBSections() == "all")
421 return BasicBlockSection::All;
422 else if (getBBSections() == "labels")
423 return BasicBlockSection::Labels;
424 else if (getBBSections() == "none")
425 return BasicBlockSection::None;
426 else {
427 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
428 MemoryBuffer::getFile(getBBSections());
429 if (!MBOrErr) {
430 errs() << "Error loading basic block sections function list file: "
431 << MBOrErr.getError().message() << "\n";
432 } else {
433 Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
434 }
435 return BasicBlockSection::List;
436 }
437 }
438
439 // Common utility function tightly tied to the options listed here. Initializes
440 // a TargetOptions object with CodeGen flags and returns it.
InitTargetOptionsFromCodeGenFlags()441 TargetOptions codegen::InitTargetOptionsFromCodeGenFlags() {
442 TargetOptions Options;
443 Options.AllowFPOpFusion = getFuseFPOps();
444 Options.UnsafeFPMath = getEnableUnsafeFPMath();
445 Options.NoInfsFPMath = getEnableNoInfsFPMath();
446 Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
447 Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
448 Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
449
450 DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
451
452 // FIXME: Should have separate input and output flags
453 Options.setFPDenormalMode(DenormalMode(DenormKind, DenormKind));
454
455 Options.HonorSignDependentRoundingFPMathOption =
456 getEnableHonorSignDependentRoundingFPMath();
457 if (getFloatABIForCalls() != FloatABI::Default)
458 Options.FloatABIType = getFloatABIForCalls();
459 Options.NoZerosInBSS = getDontPlaceZerosInBSS();
460 Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
461 Options.StackAlignmentOverride = getOverrideStackAlignment();
462 Options.StackSymbolOrdering = getStackSymbolOrdering();
463 Options.UseInitArray = !getUseCtors();
464 Options.RelaxELFRelocations = getRelaxELFRelocations();
465 Options.DataSections = getDataSections();
466 Options.FunctionSections = getFunctionSections();
467 Options.BBSections = getBBSectionsMode(Options);
468 Options.UniqueSectionNames = getUniqueSectionNames();
469 Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
470 Options.TLSSize = getTLSSize();
471 Options.EmulatedTLS = getEmulatedTLS();
472 Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
473 Options.ExceptionModel = getExceptionModel();
474 Options.EmitStackSizeSection = getEnableStackSizeSection();
475 Options.EmitAddrsig = getEnableAddrsig();
476 Options.EmitCallSiteInfo = getEmitCallSiteInfo();
477 Options.EnableDebugEntryValues = getEnableDebugEntryValues();
478 Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
479 Options.XRayOmitFunctionIndex = getXRayOmitFunctionIndex();
480
481 Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
482
483 Options.ThreadModel = getThreadModel();
484 Options.EABIVersion = getEABIVersion();
485 Options.DebuggerTuning = getDebuggerTuningOpt();
486
487 return Options;
488 }
489
getCPUStr()490 std::string codegen::getCPUStr() {
491 // If user asked for the 'native' CPU, autodetect here. If autodection fails,
492 // this will set the CPU to an empty string which tells the target to
493 // pick a basic default.
494 if (getMCPU() == "native")
495 return std::string(sys::getHostCPUName());
496
497 return getMCPU();
498 }
499
getFeaturesStr()500 std::string codegen::getFeaturesStr() {
501 SubtargetFeatures Features;
502
503 // If user asked for the 'native' CPU, we need to autodetect features.
504 // This is necessary for x86 where the CPU might not support all the
505 // features the autodetected CPU name lists in the target. For example,
506 // not all Sandybridge processors support AVX.
507 if (getMCPU() == "native") {
508 StringMap<bool> HostFeatures;
509 if (sys::getHostCPUFeatures(HostFeatures))
510 for (auto &F : HostFeatures)
511 Features.AddFeature(F.first(), F.second);
512 }
513
514 for (auto const &MAttr : getMAttrs())
515 Features.AddFeature(MAttr);
516
517 return Features.getString();
518 }
519
getFeatureList()520 std::vector<std::string> codegen::getFeatureList() {
521 SubtargetFeatures Features;
522
523 // If user asked for the 'native' CPU, we need to autodetect features.
524 // This is necessary for x86 where the CPU might not support all the
525 // features the autodetected CPU name lists in the target. For example,
526 // not all Sandybridge processors support AVX.
527 if (getMCPU() == "native") {
528 StringMap<bool> HostFeatures;
529 if (sys::getHostCPUFeatures(HostFeatures))
530 for (auto &F : HostFeatures)
531 Features.AddFeature(F.first(), F.second);
532 }
533
534 for (auto const &MAttr : getMAttrs())
535 Features.AddFeature(MAttr);
536
537 return Features.getFeatures();
538 }
539
renderBoolStringAttr(AttrBuilder & B,StringRef Name,bool Val)540 void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
541 B.addAttribute(Name, Val ? "true" : "false");
542 }
543
544 #define HANDLE_BOOL_ATTR(CL, AttrName) \
545 do { \
546 if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
547 renderBoolStringAttr(NewAttrs, AttrName, *CL); \
548 } while (0)
549
550 /// Set function attributes of function \p F based on CPU, Features, and command
551 /// line flags.
setFunctionAttributes(StringRef CPU,StringRef Features,Function & F)552 void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
553 Function &F) {
554 auto &Ctx = F.getContext();
555 AttributeList Attrs = F.getAttributes();
556 AttrBuilder NewAttrs;
557
558 if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
559 NewAttrs.addAttribute("target-cpu", CPU);
560 if (!Features.empty()) {
561 // Append the command line features to any that are already on the function.
562 StringRef OldFeatures =
563 F.getFnAttribute("target-features").getValueAsString();
564 if (OldFeatures.empty())
565 NewAttrs.addAttribute("target-features", Features);
566 else {
567 SmallString<256> Appended(OldFeatures);
568 Appended.push_back(',');
569 Appended.append(Features);
570 NewAttrs.addAttribute("target-features", Appended);
571 }
572 }
573 if (FramePointerUsageView->getNumOccurrences() > 0 &&
574 !F.hasFnAttribute("frame-pointer")) {
575 if (getFramePointerUsage() == FramePointer::All)
576 NewAttrs.addAttribute("frame-pointer", "all");
577 else if (getFramePointerUsage() == FramePointer::NonLeaf)
578 NewAttrs.addAttribute("frame-pointer", "non-leaf");
579 else if (getFramePointerUsage() == FramePointer::None)
580 NewAttrs.addAttribute("frame-pointer", "none");
581 }
582 if (DisableTailCallsView->getNumOccurrences() > 0)
583 NewAttrs.addAttribute("disable-tail-calls",
584 toStringRef(getDisableTailCalls()));
585 if (getStackRealign())
586 NewAttrs.addAttribute("stackrealign");
587
588 HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math");
589 HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math");
590 HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
591 HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
592
593 if (DenormalFPMathView->getNumOccurrences() > 0 &&
594 !F.hasFnAttribute("denormal-fp-math")) {
595 DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
596
597 // FIXME: Command line flag should expose separate input/output modes.
598 NewAttrs.addAttribute("denormal-fp-math",
599 DenormalMode(DenormKind, DenormKind).str());
600 }
601
602 if (DenormalFP32MathView->getNumOccurrences() > 0 &&
603 !F.hasFnAttribute("denormal-fp-math-f32")) {
604 // FIXME: Command line flag should expose separate input/output modes.
605 DenormalMode::DenormalModeKind DenormKind = getDenormalFP32Math();
606
607 NewAttrs.addAttribute(
608 "denormal-fp-math-f32",
609 DenormalMode(DenormKind, DenormKind).str());
610 }
611
612 if (TrapFuncNameView->getNumOccurrences() > 0)
613 for (auto &B : F)
614 for (auto &I : B)
615 if (auto *Call = dyn_cast<CallInst>(&I))
616 if (const auto *F = Call->getCalledFunction())
617 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
618 F->getIntrinsicID() == Intrinsic::trap)
619 Call->addAttribute(
620 AttributeList::FunctionIndex,
621 Attribute::get(Ctx, "trap-func-name", getTrapFuncName()));
622
623 // Let NewAttrs override Attrs.
624 F.setAttributes(
625 Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
626 }
627
628 /// Set function attributes of functions in Module M based on CPU,
629 /// Features, and command line flags.
setFunctionAttributes(StringRef CPU,StringRef Features,Module & M)630 void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
631 Module &M) {
632 for (Function &F : M)
633 setFunctionAttributes(CPU, Features, F);
634 }
635