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