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