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)
68ac1d23edSserge-sans-paille CGOPT(bool, DontPlaceZerosInBSS)
69ac1d23edSserge-sans-paille CGOPT(bool, EnableGuaranteedTailCallOpt)
70ac1d23edSserge-sans-paille CGOPT(bool, DisableTailCalls)
71ac1d23edSserge-sans-paille CGOPT(bool, StackSymbolOrdering)
72ac1d23edSserge-sans-paille CGOPT(unsigned, OverrideStackAlignment)
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)
827c3fea77SXiang1 Zhang CGOPT(std::string, StackProtectorGuard)
83*ea8416bfSNick Desaulniers CGOPT(int, StackProtectorGuardOffset)
847c3fea77SXiang1 Zhang CGOPT(std::string, StackProtectorGuardReg)
85ac1d23edSserge-sans-paille CGOPT(unsigned, TLSSize)
86ac1d23edSserge-sans-paille CGOPT(bool, EmulatedTLS)
87ac1d23edSserge-sans-paille CGOPT(bool, UniqueSectionNames)
88e0bca46bSSriraman Tallam CGOPT(bool, UniqueBasicBlockSectionNames)
89ac1d23edSserge-sans-paille CGOPT(EABI, EABIVersion)
90ac1d23edSserge-sans-paille CGOPT(DebuggerKind, DebuggerTuningOpt)
91ac1d23edSserge-sans-paille CGOPT(bool, EnableStackSizeSection)
92ac1d23edSserge-sans-paille CGOPT(bool, EnableAddrsig)
93ac1d23edSserge-sans-paille CGOPT(bool, EmitCallSiteInfo)
9494faadacSSnehasish Kumar CGOPT(bool, EnableMachineFunctionSplitter)
95ac1d23edSserge-sans-paille CGOPT(bool, EnableDebugEntryValues)
9624d4291cSHongtao Yu CGOPT(bool, PseudoProbeForProfiling)
97121a49d8SJeremy Morse CGOPT(bool, ValueTrackingVariableLocations)
98ac1d23edSserge-sans-paille CGOPT(bool, ForceDwarfFrameSection)
997c7c8e0dSIan Levesque CGOPT(bool, XRayOmitFunctionIndex)
100ac1d23edSserge-sans-paille 
101ac1d23edSserge-sans-paille codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
102ac1d23edSserge-sans-paille #define CGBINDOPT(NAME)                                                        \
103ac1d23edSserge-sans-paille   do {                                                                         \
104ac1d23edSserge-sans-paille     NAME##View = std::addressof(NAME);                                         \
105ac1d23edSserge-sans-paille   } while (0)
106ac1d23edSserge-sans-paille 
107ac1d23edSserge-sans-paille   static cl::opt<std::string> MArch(
108ac1d23edSserge-sans-paille       "march", cl::desc("Architecture to generate code for (see --version)"));
109ac1d23edSserge-sans-paille   CGBINDOPT(MArch);
110ac1d23edSserge-sans-paille 
111ac1d23edSserge-sans-paille   static cl::opt<std::string> MCPU(
112ac1d23edSserge-sans-paille       "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
113ac1d23edSserge-sans-paille       cl::value_desc("cpu-name"), cl::init(""));
114ac1d23edSserge-sans-paille   CGBINDOPT(MCPU);
115ac1d23edSserge-sans-paille 
116ac1d23edSserge-sans-paille   static cl::list<std::string> MAttrs(
117ac1d23edSserge-sans-paille       "mattr", cl::CommaSeparated,
118ac1d23edSserge-sans-paille       cl::desc("Target specific attributes (-mattr=help for details)"),
119ac1d23edSserge-sans-paille       cl::value_desc("a1,+a2,-a3,..."));
120ac1d23edSserge-sans-paille   CGBINDOPT(MAttrs);
121ac1d23edSserge-sans-paille 
122ac1d23edSserge-sans-paille   static cl::opt<Reloc::Model> RelocModel(
123ac1d23edSserge-sans-paille       "relocation-model", cl::desc("Choose relocation model"),
124ac1d23edSserge-sans-paille       cl::values(
125ac1d23edSserge-sans-paille           clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
126ac1d23edSserge-sans-paille           clEnumValN(Reloc::PIC_, "pic",
127ac1d23edSserge-sans-paille                      "Fully relocatable, position independent code"),
128ac1d23edSserge-sans-paille           clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
129ac1d23edSserge-sans-paille                      "Relocatable external references, non-relocatable code"),
130ac1d23edSserge-sans-paille           clEnumValN(
131ac1d23edSserge-sans-paille               Reloc::ROPI, "ropi",
132ac1d23edSserge-sans-paille               "Code and read-only data relocatable, accessed PC-relative"),
133ac1d23edSserge-sans-paille           clEnumValN(
134ac1d23edSserge-sans-paille               Reloc::RWPI, "rwpi",
135ac1d23edSserge-sans-paille               "Read-write data relocatable, accessed relative to static base"),
136ac1d23edSserge-sans-paille           clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
137ac1d23edSserge-sans-paille                      "Combination of ropi and rwpi")));
138ac1d23edSserge-sans-paille   CGBINDOPT(RelocModel);
139ac1d23edSserge-sans-paille 
140ac1d23edSserge-sans-paille   static cl::opt<ThreadModel::Model> ThreadModel(
141ac1d23edSserge-sans-paille       "thread-model", cl::desc("Choose threading model"),
142ac1d23edSserge-sans-paille       cl::init(ThreadModel::POSIX),
143ac1d23edSserge-sans-paille       cl::values(
144ac1d23edSserge-sans-paille           clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
145ac1d23edSserge-sans-paille           clEnumValN(ThreadModel::Single, "single", "Single thread model")));
146ac1d23edSserge-sans-paille   CGBINDOPT(ThreadModel);
147ac1d23edSserge-sans-paille 
148ac1d23edSserge-sans-paille   static cl::opt<CodeModel::Model> CodeModel(
149ac1d23edSserge-sans-paille       "code-model", cl::desc("Choose code model"),
150ac1d23edSserge-sans-paille       cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
151ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Small, "small", "Small code model"),
152ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
153ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
154ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Large, "large", "Large code model")));
155ac1d23edSserge-sans-paille   CGBINDOPT(CodeModel);
156ac1d23edSserge-sans-paille 
157ac1d23edSserge-sans-paille   static cl::opt<ExceptionHandling> ExceptionModel(
158ac1d23edSserge-sans-paille       "exception-model", cl::desc("exception model"),
159ac1d23edSserge-sans-paille       cl::init(ExceptionHandling::None),
160ac1d23edSserge-sans-paille       cl::values(
161ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::None, "default",
162ac1d23edSserge-sans-paille                      "default exception handling model"),
163ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
164ac1d23edSserge-sans-paille                      "DWARF-like CFI based exception handling"),
165ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::SjLj, "sjlj",
166ac1d23edSserge-sans-paille                      "SjLj exception handling"),
167ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
168ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::WinEH, "wineh",
169ac1d23edSserge-sans-paille                      "Windows exception model"),
170ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::Wasm, "wasm",
171ac1d23edSserge-sans-paille                      "WebAssembly exception handling")));
172ac1d23edSserge-sans-paille   CGBINDOPT(ExceptionModel);
173ac1d23edSserge-sans-paille 
174ac1d23edSserge-sans-paille   static cl::opt<CodeGenFileType> FileType(
175ac1d23edSserge-sans-paille       "filetype", cl::init(CGFT_AssemblyFile),
176ac1d23edSserge-sans-paille       cl::desc(
177ac1d23edSserge-sans-paille           "Choose a file type (not all types are supported by all targets):"),
178ac1d23edSserge-sans-paille       cl::values(
179ac1d23edSserge-sans-paille           clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"),
180ac1d23edSserge-sans-paille           clEnumValN(CGFT_ObjectFile, "obj",
181ac1d23edSserge-sans-paille                      "Emit a native object ('.o') file"),
182ac1d23edSserge-sans-paille           clEnumValN(CGFT_Null, "null",
183ac1d23edSserge-sans-paille                      "Emit nothing, for performance testing")));
184ac1d23edSserge-sans-paille   CGBINDOPT(FileType);
185ac1d23edSserge-sans-paille 
1862786e673SFangrui Song   static cl::opt<FramePointerKind> FramePointerUsage(
187ac1d23edSserge-sans-paille       "frame-pointer",
188ac1d23edSserge-sans-paille       cl::desc("Specify frame pointer elimination optimization"),
1892786e673SFangrui Song       cl::init(FramePointerKind::None),
190ac1d23edSserge-sans-paille       cl::values(
1912786e673SFangrui Song           clEnumValN(FramePointerKind::All, "all",
192ac1d23edSserge-sans-paille                      "Disable frame pointer elimination"),
1932786e673SFangrui Song           clEnumValN(FramePointerKind::NonLeaf, "non-leaf",
194ac1d23edSserge-sans-paille                      "Disable frame pointer elimination for non-leaf frame"),
1952786e673SFangrui Song           clEnumValN(FramePointerKind::None, "none",
196ac1d23edSserge-sans-paille                      "Enable frame pointer elimination")));
197ac1d23edSserge-sans-paille   CGBINDOPT(FramePointerUsage);
198ac1d23edSserge-sans-paille 
199ac1d23edSserge-sans-paille   static cl::opt<bool> EnableUnsafeFPMath(
200ac1d23edSserge-sans-paille       "enable-unsafe-fp-math",
201ac1d23edSserge-sans-paille       cl::desc("Enable optimizations that may decrease FP precision"),
202ac1d23edSserge-sans-paille       cl::init(false));
203ac1d23edSserge-sans-paille   CGBINDOPT(EnableUnsafeFPMath);
204ac1d23edSserge-sans-paille 
205ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoInfsFPMath(
206ac1d23edSserge-sans-paille       "enable-no-infs-fp-math",
207ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume no +-Infs"),
208ac1d23edSserge-sans-paille       cl::init(false));
209ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoInfsFPMath);
210ac1d23edSserge-sans-paille 
211ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoNaNsFPMath(
212ac1d23edSserge-sans-paille       "enable-no-nans-fp-math",
213ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume no NaNs"),
214ac1d23edSserge-sans-paille       cl::init(false));
215ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoNaNsFPMath);
216ac1d23edSserge-sans-paille 
217ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoSignedZerosFPMath(
218ac1d23edSserge-sans-paille       "enable-no-signed-zeros-fp-math",
219ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume "
220ac1d23edSserge-sans-paille                "the sign of 0 is insignificant"),
221ac1d23edSserge-sans-paille       cl::init(false));
222ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoSignedZerosFPMath);
223ac1d23edSserge-sans-paille 
224ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoTrappingFPMath(
225ac1d23edSserge-sans-paille       "enable-no-trapping-fp-math",
226ac1d23edSserge-sans-paille       cl::desc("Enable setting the FP exceptions build "
227ac1d23edSserge-sans-paille                "attribute not to use exceptions"),
228ac1d23edSserge-sans-paille       cl::init(false));
229ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoTrappingFPMath);
230ac1d23edSserge-sans-paille 
231a8cc9047SMatt Arsenault   static const auto DenormFlagEnumOptions =
232a8cc9047SMatt Arsenault   cl::values(clEnumValN(DenormalMode::IEEE, "ieee",
233a8cc9047SMatt Arsenault                         "IEEE 754 denormal numbers"),
2340ab5b5b8SMatt Arsenault              clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
235ac1d23edSserge-sans-paille                         "the sign of a  flushed-to-zero number is preserved "
236ac1d23edSserge-sans-paille                         "in the sign of 0"),
2370ab5b5b8SMatt Arsenault              clEnumValN(DenormalMode::PositiveZero, "positive-zero",
238a8cc9047SMatt Arsenault                         "denormals are flushed to positive zero"));
239a8cc9047SMatt Arsenault 
240a8cc9047SMatt Arsenault   // FIXME: Doesn't have way to specify separate input and output modes.
241a8cc9047SMatt Arsenault   static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
242a8cc9047SMatt Arsenault     "denormal-fp-math",
243a8cc9047SMatt Arsenault     cl::desc("Select which denormal numbers the code is permitted to require"),
244a8cc9047SMatt Arsenault     cl::init(DenormalMode::IEEE),
245a8cc9047SMatt Arsenault     DenormFlagEnumOptions);
246ac1d23edSserge-sans-paille   CGBINDOPT(DenormalFPMath);
247ac1d23edSserge-sans-paille 
248a8cc9047SMatt Arsenault   static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math(
249a8cc9047SMatt Arsenault     "denormal-fp-math-f32",
250a8cc9047SMatt Arsenault     cl::desc("Select which denormal numbers the code is permitted to require for float"),
251a8cc9047SMatt Arsenault     cl::init(DenormalMode::Invalid),
252a8cc9047SMatt Arsenault     DenormFlagEnumOptions);
253a8cc9047SMatt Arsenault   CGBINDOPT(DenormalFP32Math);
254a8cc9047SMatt Arsenault 
255ac1d23edSserge-sans-paille   static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
256ac1d23edSserge-sans-paille       "enable-sign-dependent-rounding-fp-math", cl::Hidden,
257ac1d23edSserge-sans-paille       cl::desc("Force codegen to assume rounding mode can change dynamically"),
258ac1d23edSserge-sans-paille       cl::init(false));
259ac1d23edSserge-sans-paille   CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
260ac1d23edSserge-sans-paille 
261ac1d23edSserge-sans-paille   static cl::opt<FloatABI::ABIType> FloatABIForCalls(
262ac1d23edSserge-sans-paille       "float-abi", cl::desc("Choose float ABI type"),
263ac1d23edSserge-sans-paille       cl::init(FloatABI::Default),
264ac1d23edSserge-sans-paille       cl::values(clEnumValN(FloatABI::Default, "default",
265ac1d23edSserge-sans-paille                             "Target default float ABI type"),
266ac1d23edSserge-sans-paille                  clEnumValN(FloatABI::Soft, "soft",
267ac1d23edSserge-sans-paille                             "Soft float ABI (implied by -soft-float)"),
268ac1d23edSserge-sans-paille                  clEnumValN(FloatABI::Hard, "hard",
269ac1d23edSserge-sans-paille                             "Hard float ABI (uses FP registers)")));
270ac1d23edSserge-sans-paille   CGBINDOPT(FloatABIForCalls);
271ac1d23edSserge-sans-paille 
272ac1d23edSserge-sans-paille   static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
273ac1d23edSserge-sans-paille       "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
274ac1d23edSserge-sans-paille       cl::init(FPOpFusion::Standard),
275ac1d23edSserge-sans-paille       cl::values(
276ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Fast, "fast",
277ac1d23edSserge-sans-paille                      "Fuse FP ops whenever profitable"),
278ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
279ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Strict, "off",
280ac1d23edSserge-sans-paille                      "Only fuse FP ops when the result won't be affected.")));
281ac1d23edSserge-sans-paille   CGBINDOPT(FuseFPOps);
282ac1d23edSserge-sans-paille 
283ac1d23edSserge-sans-paille   static cl::opt<bool> DontPlaceZerosInBSS(
284ac1d23edSserge-sans-paille       "nozero-initialized-in-bss",
285ac1d23edSserge-sans-paille       cl::desc("Don't place zero-initialized symbols into bss section"),
286ac1d23edSserge-sans-paille       cl::init(false));
287ac1d23edSserge-sans-paille   CGBINDOPT(DontPlaceZerosInBSS);
288ac1d23edSserge-sans-paille 
289c92f29b0SZarko Todorovski   static cl::opt<bool> EnableAIXExtendedAltivecABI(
290c92f29b0SZarko Todorovski       "vec-extabi", cl::desc("Enable the AIX Extended Altivec ABI."),
291c92f29b0SZarko Todorovski       cl::init(false));
292c92f29b0SZarko Todorovski   CGBINDOPT(EnableAIXExtendedAltivecABI);
293c92f29b0SZarko Todorovski 
294ac1d23edSserge-sans-paille   static cl::opt<bool> EnableGuaranteedTailCallOpt(
295ac1d23edSserge-sans-paille       "tailcallopt",
296ac1d23edSserge-sans-paille       cl::desc(
297ac1d23edSserge-sans-paille           "Turn fastcc calls into tail calls by (potentially) changing ABI."),
298ac1d23edSserge-sans-paille       cl::init(false));
299ac1d23edSserge-sans-paille   CGBINDOPT(EnableGuaranteedTailCallOpt);
300ac1d23edSserge-sans-paille 
301ac1d23edSserge-sans-paille   static cl::opt<bool> DisableTailCalls(
302ac1d23edSserge-sans-paille       "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
303ac1d23edSserge-sans-paille   CGBINDOPT(DisableTailCalls);
304ac1d23edSserge-sans-paille 
305ac1d23edSserge-sans-paille   static cl::opt<bool> StackSymbolOrdering(
306ac1d23edSserge-sans-paille       "stack-symbol-ordering", cl::desc("Order local stack symbols."),
307ac1d23edSserge-sans-paille       cl::init(true));
308ac1d23edSserge-sans-paille   CGBINDOPT(StackSymbolOrdering);
309ac1d23edSserge-sans-paille 
310ac1d23edSserge-sans-paille   static cl::opt<unsigned> OverrideStackAlignment(
311ac1d23edSserge-sans-paille       "stack-alignment", cl::desc("Override default stack alignment"),
312ac1d23edSserge-sans-paille       cl::init(0));
313ac1d23edSserge-sans-paille   CGBINDOPT(OverrideStackAlignment);
314ac1d23edSserge-sans-paille 
315ac1d23edSserge-sans-paille   static cl::opt<bool> StackRealign(
316ac1d23edSserge-sans-paille       "stackrealign",
317ac1d23edSserge-sans-paille       cl::desc("Force align the stack to the minimum alignment"),
318ac1d23edSserge-sans-paille       cl::init(false));
319ac1d23edSserge-sans-paille   CGBINDOPT(StackRealign);
320ac1d23edSserge-sans-paille 
321ac1d23edSserge-sans-paille   static cl::opt<std::string> TrapFuncName(
322ac1d23edSserge-sans-paille       "trap-func", cl::Hidden,
323ac1d23edSserge-sans-paille       cl::desc("Emit a call to trap function rather than a trap instruction"),
324ac1d23edSserge-sans-paille       cl::init(""));
325ac1d23edSserge-sans-paille   CGBINDOPT(TrapFuncName);
326ac1d23edSserge-sans-paille 
327ac1d23edSserge-sans-paille   static cl::opt<bool> UseCtors("use-ctors",
328ac1d23edSserge-sans-paille                                 cl::desc("Use .ctors instead of .init_array."),
329ac1d23edSserge-sans-paille                                 cl::init(false));
330ac1d23edSserge-sans-paille   CGBINDOPT(UseCtors);
331ac1d23edSserge-sans-paille 
332ac1d23edSserge-sans-paille   static cl::opt<bool> RelaxELFRelocations(
333ac1d23edSserge-sans-paille       "relax-elf-relocations",
334ac1d23edSserge-sans-paille       cl::desc(
335ac1d23edSserge-sans-paille           "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
336ac1d23edSserge-sans-paille       cl::init(false));
337ac1d23edSserge-sans-paille   CGBINDOPT(RelaxELFRelocations);
338ac1d23edSserge-sans-paille 
339ac1d23edSserge-sans-paille   static cl::opt<bool> DataSections(
340ac1d23edSserge-sans-paille       "data-sections", cl::desc("Emit data into separate sections"),
341ac1d23edSserge-sans-paille       cl::init(false));
342ac1d23edSserge-sans-paille   CGBINDOPT(DataSections);
343ac1d23edSserge-sans-paille 
344ac1d23edSserge-sans-paille   static cl::opt<bool> FunctionSections(
345ac1d23edSserge-sans-paille       "function-sections", cl::desc("Emit functions into separate sections"),
346ac1d23edSserge-sans-paille       cl::init(false));
347ac1d23edSserge-sans-paille   CGBINDOPT(FunctionSections);
348ac1d23edSserge-sans-paille 
34992bca128Sdiggerlin   static cl::opt<bool> IgnoreXCOFFVisibility(
35092bca128Sdiggerlin       "ignore-xcoff-visibility",
35192bca128Sdiggerlin       cl::desc("Not emit the visibility attribute for asm in AIX OS or give "
35292bca128Sdiggerlin                "all symbols 'unspecified' visibility in XCOFF object file"),
35392bca128Sdiggerlin       cl::init(false));
35492bca128Sdiggerlin   CGBINDOPT(IgnoreXCOFFVisibility);
35592bca128Sdiggerlin 
356997d286fSdiggerlin   static cl::opt<bool> XCOFFTracebackTable(
357997d286fSdiggerlin       "xcoff-traceback-table", cl::desc("Emit the XCOFF traceback table"),
358997d286fSdiggerlin       cl::init(true));
359997d286fSdiggerlin   CGBINDOPT(XCOFFTracebackTable);
360997d286fSdiggerlin 
361ac1d23edSserge-sans-paille   static cl::opt<std::string> BBSections(
362ca6b6d40SSriraman Tallam       "basic-block-sections",
363ac1d23edSserge-sans-paille       cl::desc("Emit basic blocks into separate sections"),
364ac1d23edSserge-sans-paille       cl::value_desc("all | <function list (file)> | labels | none"),
365ac1d23edSserge-sans-paille       cl::init("none"));
366ac1d23edSserge-sans-paille   CGBINDOPT(BBSections);
367ac1d23edSserge-sans-paille 
3687c3fea77SXiang1 Zhang   static cl::opt<std::string> StackProtectorGuard(
3697c3fea77SXiang1 Zhang       "stack-protector-guard", cl::desc("Stack protector guard mode"),
3707c3fea77SXiang1 Zhang       cl::init("none"));
3717c3fea77SXiang1 Zhang   CGBINDOPT(StackProtectorGuard);
3727c3fea77SXiang1 Zhang 
3737c3fea77SXiang1 Zhang   static cl::opt<std::string> StackProtectorGuardReg(
3747c3fea77SXiang1 Zhang       "stack-protector-guard-reg", cl::desc("Stack protector guard register"),
3757c3fea77SXiang1 Zhang       cl::init("none"));
3767c3fea77SXiang1 Zhang   CGBINDOPT(StackProtectorGuardReg);
3777c3fea77SXiang1 Zhang 
378*ea8416bfSNick Desaulniers   static cl::opt<int> StackProtectorGuardOffset(
3797c3fea77SXiang1 Zhang       "stack-protector-guard-offset", cl::desc("Stack protector guard offset"),
380*ea8416bfSNick Desaulniers       cl::init(INT_MAX));
3817c3fea77SXiang1 Zhang   CGBINDOPT(StackProtectorGuardOffset);
3827c3fea77SXiang1 Zhang 
383ac1d23edSserge-sans-paille   static cl::opt<unsigned> TLSSize(
384ac1d23edSserge-sans-paille       "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
385ac1d23edSserge-sans-paille   CGBINDOPT(TLSSize);
386ac1d23edSserge-sans-paille 
387ac1d23edSserge-sans-paille   static cl::opt<bool> EmulatedTLS(
388ac1d23edSserge-sans-paille       "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
389ac1d23edSserge-sans-paille   CGBINDOPT(EmulatedTLS);
390ac1d23edSserge-sans-paille 
391ac1d23edSserge-sans-paille   static cl::opt<bool> UniqueSectionNames(
392ac1d23edSserge-sans-paille       "unique-section-names", cl::desc("Give unique names to every section"),
393ac1d23edSserge-sans-paille       cl::init(true));
394ac1d23edSserge-sans-paille   CGBINDOPT(UniqueSectionNames);
395ac1d23edSserge-sans-paille 
396e0bca46bSSriraman Tallam   static cl::opt<bool> UniqueBasicBlockSectionNames(
397ca6b6d40SSriraman Tallam       "unique-basic-block-section-names",
398ac1d23edSserge-sans-paille       cl::desc("Give unique names to every basic block section"),
399ac1d23edSserge-sans-paille       cl::init(false));
400e0bca46bSSriraman Tallam   CGBINDOPT(UniqueBasicBlockSectionNames);
401ac1d23edSserge-sans-paille 
402ac1d23edSserge-sans-paille   static cl::opt<EABI> EABIVersion(
403ac1d23edSserge-sans-paille       "meabi", cl::desc("Set EABI type (default depends on triple):"),
404ac1d23edSserge-sans-paille       cl::init(EABI::Default),
405ac1d23edSserge-sans-paille       cl::values(
406ac1d23edSserge-sans-paille           clEnumValN(EABI::Default, "default", "Triple default EABI version"),
407ac1d23edSserge-sans-paille           clEnumValN(EABI::EABI4, "4", "EABI version 4"),
408ac1d23edSserge-sans-paille           clEnumValN(EABI::EABI5, "5", "EABI version 5"),
409ac1d23edSserge-sans-paille           clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
410ac1d23edSserge-sans-paille   CGBINDOPT(EABIVersion);
411ac1d23edSserge-sans-paille 
412ac1d23edSserge-sans-paille   static cl::opt<DebuggerKind> DebuggerTuningOpt(
413ac1d23edSserge-sans-paille       "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
414ac1d23edSserge-sans-paille       cl::init(DebuggerKind::Default),
415ac1d23edSserge-sans-paille       cl::values(
416ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
417ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
4180c36da72SEsme-Yi           clEnumValN(DebuggerKind::DBX, "dbx", "dbx"),
419ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
420ac1d23edSserge-sans-paille   CGBINDOPT(DebuggerTuningOpt);
421ac1d23edSserge-sans-paille 
422ac1d23edSserge-sans-paille   static cl::opt<bool> EnableStackSizeSection(
423ac1d23edSserge-sans-paille       "stack-size-section",
424ac1d23edSserge-sans-paille       cl::desc("Emit a section containing stack size metadata"),
425ac1d23edSserge-sans-paille       cl::init(false));
426ac1d23edSserge-sans-paille   CGBINDOPT(EnableStackSizeSection);
427ac1d23edSserge-sans-paille 
428ac1d23edSserge-sans-paille   static cl::opt<bool> EnableAddrsig(
429ac1d23edSserge-sans-paille       "addrsig", cl::desc("Emit an address-significance table"),
430ac1d23edSserge-sans-paille       cl::init(false));
431ac1d23edSserge-sans-paille   CGBINDOPT(EnableAddrsig);
432ac1d23edSserge-sans-paille 
433ac1d23edSserge-sans-paille   static cl::opt<bool> EmitCallSiteInfo(
434ac1d23edSserge-sans-paille       "emit-call-site-info",
435ac1d23edSserge-sans-paille       cl::desc(
436ac1d23edSserge-sans-paille           "Emit call site debug information, if debug information is enabled."),
437ac1d23edSserge-sans-paille       cl::init(false));
438ac1d23edSserge-sans-paille   CGBINDOPT(EmitCallSiteInfo);
439ac1d23edSserge-sans-paille 
440ac1d23edSserge-sans-paille   static cl::opt<bool> EnableDebugEntryValues(
441ac1d23edSserge-sans-paille       "debug-entry-values",
442d9b96210SDjordje Todorovic       cl::desc("Enable debug info for the debug entry values."),
443ac1d23edSserge-sans-paille       cl::init(false));
444ac1d23edSserge-sans-paille   CGBINDOPT(EnableDebugEntryValues);
445ac1d23edSserge-sans-paille 
44624d4291cSHongtao Yu   static cl::opt<bool> PseudoProbeForProfiling(
44724d4291cSHongtao Yu       "pseudo-probe-for-profiling", cl::desc("Emit pseudo probes for AutoFDO"),
44824d4291cSHongtao Yu       cl::init(false));
44924d4291cSHongtao Yu   CGBINDOPT(PseudoProbeForProfiling);
45024d4291cSHongtao Yu 
451121a49d8SJeremy Morse   static cl::opt<bool> ValueTrackingVariableLocations(
452121a49d8SJeremy Morse       "experimental-debug-variable-locations",
453121a49d8SJeremy Morse       cl::desc("Use experimental new value-tracking variable locations"),
454121a49d8SJeremy Morse       cl::init(false));
455121a49d8SJeremy Morse   CGBINDOPT(ValueTrackingVariableLocations);
456121a49d8SJeremy Morse 
45794faadacSSnehasish Kumar   static cl::opt<bool> EnableMachineFunctionSplitter(
45894faadacSSnehasish Kumar       "split-machine-functions",
45994faadacSSnehasish Kumar       cl::desc("Split out cold basic blocks from machine functions based on "
46094faadacSSnehasish Kumar                "profile information"),
46194faadacSSnehasish Kumar       cl::init(false));
46294faadacSSnehasish Kumar   CGBINDOPT(EnableMachineFunctionSplitter);
46394faadacSSnehasish Kumar 
464ac1d23edSserge-sans-paille   static cl::opt<bool> ForceDwarfFrameSection(
465ac1d23edSserge-sans-paille       "force-dwarf-frame-section",
466ac1d23edSserge-sans-paille       cl::desc("Always emit a debug frame section."), cl::init(false));
467ac1d23edSserge-sans-paille   CGBINDOPT(ForceDwarfFrameSection);
468ac1d23edSserge-sans-paille 
4697c7c8e0dSIan Levesque   static cl::opt<bool> XRayOmitFunctionIndex(
4707c7c8e0dSIan Levesque       "no-xray-index", cl::desc("Don't emit xray_fn_idx section"),
4717c7c8e0dSIan Levesque       cl::init(false));
4727c7c8e0dSIan Levesque   CGBINDOPT(XRayOmitFunctionIndex);
4737c7c8e0dSIan Levesque 
474ac1d23edSserge-sans-paille #undef CGBINDOPT
475ac1d23edSserge-sans-paille 
476ac1d23edSserge-sans-paille   mc::RegisterMCTargetOptionsFlags();
477ac1d23edSserge-sans-paille }
478ac1d23edSserge-sans-paille 
479ac1d23edSserge-sans-paille llvm::BasicBlockSection
480ac1d23edSserge-sans-paille codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
481ac1d23edSserge-sans-paille   if (getBBSections() == "all")
482ac1d23edSserge-sans-paille     return BasicBlockSection::All;
483ac1d23edSserge-sans-paille   else if (getBBSections() == "labels")
484ac1d23edSserge-sans-paille     return BasicBlockSection::Labels;
485ac1d23edSserge-sans-paille   else if (getBBSections() == "none")
486ac1d23edSserge-sans-paille     return BasicBlockSection::None;
487ac1d23edSserge-sans-paille   else {
488ac1d23edSserge-sans-paille     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
489ac1d23edSserge-sans-paille         MemoryBuffer::getFile(getBBSections());
490ac1d23edSserge-sans-paille     if (!MBOrErr) {
491ac1d23edSserge-sans-paille       errs() << "Error loading basic block sections function list file: "
492ac1d23edSserge-sans-paille              << MBOrErr.getError().message() << "\n";
493ac1d23edSserge-sans-paille     } else {
494ac1d23edSserge-sans-paille       Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
495ac1d23edSserge-sans-paille     }
496ac1d23edSserge-sans-paille     return BasicBlockSection::List;
497ac1d23edSserge-sans-paille   }
498ac1d23edSserge-sans-paille }
499ac1d23edSserge-sans-paille 
5007c3fea77SXiang1 Zhang llvm::StackProtectorGuards
5017c3fea77SXiang1 Zhang codegen::getStackProtectorGuardMode(llvm::TargetOptions &Options) {
5027c3fea77SXiang1 Zhang   if (getStackProtectorGuard() == "tls")
5037c3fea77SXiang1 Zhang     return StackProtectorGuards::TLS;
5047c3fea77SXiang1 Zhang   if (getStackProtectorGuard() == "global")
5057c3fea77SXiang1 Zhang     return StackProtectorGuards::Global;
5067c3fea77SXiang1 Zhang   if (getStackProtectorGuard() != "none") {
5077c3fea77SXiang1 Zhang     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
5087c3fea77SXiang1 Zhang         MemoryBuffer::getFile(getStackProtectorGuard());
5097c3fea77SXiang1 Zhang     if (!MBOrErr)
5107c3fea77SXiang1 Zhang       errs() << "error illegal stack protector guard mode: "
5117c3fea77SXiang1 Zhang              << MBOrErr.getError().message() << "\n";
5127c3fea77SXiang1 Zhang     else
5137c3fea77SXiang1 Zhang       Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
5147c3fea77SXiang1 Zhang   }
5157c3fea77SXiang1 Zhang   return StackProtectorGuards::None;
5167c3fea77SXiang1 Zhang }
5177c3fea77SXiang1 Zhang 
518ac1d23edSserge-sans-paille // Common utility function tightly tied to the options listed here. Initializes
519ac1d23edSserge-sans-paille // a TargetOptions object with CodeGen flags and returns it.
520f85bcc21Sjasonliu TargetOptions
521f85bcc21Sjasonliu codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
522ac1d23edSserge-sans-paille   TargetOptions Options;
523ac1d23edSserge-sans-paille   Options.AllowFPOpFusion = getFuseFPOps();
524ac1d23edSserge-sans-paille   Options.UnsafeFPMath = getEnableUnsafeFPMath();
525ac1d23edSserge-sans-paille   Options.NoInfsFPMath = getEnableNoInfsFPMath();
526ac1d23edSserge-sans-paille   Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
527ac1d23edSserge-sans-paille   Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
528ac1d23edSserge-sans-paille   Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
5290ab5b5b8SMatt Arsenault 
5300ab5b5b8SMatt Arsenault   DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
5310ab5b5b8SMatt Arsenault 
5320ab5b5b8SMatt Arsenault   // FIXME: Should have separate input and output flags
5330ab5b5b8SMatt Arsenault   Options.setFPDenormalMode(DenormalMode(DenormKind, DenormKind));
5340ab5b5b8SMatt Arsenault 
535ac1d23edSserge-sans-paille   Options.HonorSignDependentRoundingFPMathOption =
536ac1d23edSserge-sans-paille       getEnableHonorSignDependentRoundingFPMath();
537ac1d23edSserge-sans-paille   if (getFloatABIForCalls() != FloatABI::Default)
538ac1d23edSserge-sans-paille     Options.FloatABIType = getFloatABIForCalls();
539c92f29b0SZarko Todorovski   Options.EnableAIXExtendedAltivecABI = getEnableAIXExtendedAltivecABI();
540ac1d23edSserge-sans-paille   Options.NoZerosInBSS = getDontPlaceZerosInBSS();
541ac1d23edSserge-sans-paille   Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
542ac1d23edSserge-sans-paille   Options.StackAlignmentOverride = getOverrideStackAlignment();
543ac1d23edSserge-sans-paille   Options.StackSymbolOrdering = getStackSymbolOrdering();
544ac1d23edSserge-sans-paille   Options.UseInitArray = !getUseCtors();
545ac1d23edSserge-sans-paille   Options.RelaxELFRelocations = getRelaxELFRelocations();
546f85bcc21Sjasonliu   Options.DataSections =
547f85bcc21Sjasonliu       getExplicitDataSections().getValueOr(TheTriple.hasDefaultDataSections());
548ac1d23edSserge-sans-paille   Options.FunctionSections = getFunctionSections();
54992bca128Sdiggerlin   Options.IgnoreXCOFFVisibility = getIgnoreXCOFFVisibility();
550997d286fSdiggerlin   Options.XCOFFTracebackTable = getXCOFFTracebackTable();
551ac1d23edSserge-sans-paille   Options.BBSections = getBBSectionsMode(Options);
552ac1d23edSserge-sans-paille   Options.UniqueSectionNames = getUniqueSectionNames();
553e0bca46bSSriraman Tallam   Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
5547c3fea77SXiang1 Zhang   Options.StackProtectorGuard = getStackProtectorGuardMode(Options);
5557c3fea77SXiang1 Zhang   Options.StackProtectorGuardOffset = getStackProtectorGuardOffset();
5567c3fea77SXiang1 Zhang   Options.StackProtectorGuardReg = getStackProtectorGuardReg();
557ac1d23edSserge-sans-paille   Options.TLSSize = getTLSSize();
558ac1d23edSserge-sans-paille   Options.EmulatedTLS = getEmulatedTLS();
559ac1d23edSserge-sans-paille   Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
560ac1d23edSserge-sans-paille   Options.ExceptionModel = getExceptionModel();
561ac1d23edSserge-sans-paille   Options.EmitStackSizeSection = getEnableStackSizeSection();
56294faadacSSnehasish Kumar   Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();
563ac1d23edSserge-sans-paille   Options.EmitAddrsig = getEnableAddrsig();
564ac1d23edSserge-sans-paille   Options.EmitCallSiteInfo = getEmitCallSiteInfo();
565ac1d23edSserge-sans-paille   Options.EnableDebugEntryValues = getEnableDebugEntryValues();
56624d4291cSHongtao Yu   Options.PseudoProbeForProfiling = getPseudoProbeForProfiling();
567121a49d8SJeremy Morse   Options.ValueTrackingVariableLocations = getValueTrackingVariableLocations();
568ac1d23edSserge-sans-paille   Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
5697c7c8e0dSIan Levesque   Options.XRayOmitFunctionIndex = getXRayOmitFunctionIndex();
570ac1d23edSserge-sans-paille 
571ac1d23edSserge-sans-paille   Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
572ac1d23edSserge-sans-paille 
573ac1d23edSserge-sans-paille   Options.ThreadModel = getThreadModel();
574ac1d23edSserge-sans-paille   Options.EABIVersion = getEABIVersion();
575ac1d23edSserge-sans-paille   Options.DebuggerTuning = getDebuggerTuningOpt();
576ac1d23edSserge-sans-paille 
577ac1d23edSserge-sans-paille   return Options;
578ac1d23edSserge-sans-paille }
579ac1d23edSserge-sans-paille 
580ac1d23edSserge-sans-paille std::string codegen::getCPUStr() {
581ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, autodetect here. If autodection fails,
582ac1d23edSserge-sans-paille   // this will set the CPU to an empty string which tells the target to
583ac1d23edSserge-sans-paille   // pick a basic default.
584ac1d23edSserge-sans-paille   if (getMCPU() == "native")
585ac1d23edSserge-sans-paille     return std::string(sys::getHostCPUName());
586ac1d23edSserge-sans-paille 
587ac1d23edSserge-sans-paille   return getMCPU();
588ac1d23edSserge-sans-paille }
589ac1d23edSserge-sans-paille 
590ac1d23edSserge-sans-paille std::string codegen::getFeaturesStr() {
591ac1d23edSserge-sans-paille   SubtargetFeatures Features;
592ac1d23edSserge-sans-paille 
593ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, we need to autodetect features.
594ac1d23edSserge-sans-paille   // This is necessary for x86 where the CPU might not support all the
595ac1d23edSserge-sans-paille   // features the autodetected CPU name lists in the target. For example,
596ac1d23edSserge-sans-paille   // not all Sandybridge processors support AVX.
597ac1d23edSserge-sans-paille   if (getMCPU() == "native") {
598ac1d23edSserge-sans-paille     StringMap<bool> HostFeatures;
599ac1d23edSserge-sans-paille     if (sys::getHostCPUFeatures(HostFeatures))
600ac1d23edSserge-sans-paille       for (auto &F : HostFeatures)
601ac1d23edSserge-sans-paille         Features.AddFeature(F.first(), F.second);
602ac1d23edSserge-sans-paille   }
603ac1d23edSserge-sans-paille 
604ac1d23edSserge-sans-paille   for (auto const &MAttr : getMAttrs())
605ac1d23edSserge-sans-paille     Features.AddFeature(MAttr);
606ac1d23edSserge-sans-paille 
607ac1d23edSserge-sans-paille   return Features.getString();
608ac1d23edSserge-sans-paille }
609ac1d23edSserge-sans-paille 
610ac1d23edSserge-sans-paille std::vector<std::string> codegen::getFeatureList() {
611ac1d23edSserge-sans-paille   SubtargetFeatures Features;
612ac1d23edSserge-sans-paille 
613ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, we need to autodetect features.
614ac1d23edSserge-sans-paille   // This is necessary for x86 where the CPU might not support all the
615ac1d23edSserge-sans-paille   // features the autodetected CPU name lists in the target. For example,
616ac1d23edSserge-sans-paille   // not all Sandybridge processors support AVX.
617ac1d23edSserge-sans-paille   if (getMCPU() == "native") {
618ac1d23edSserge-sans-paille     StringMap<bool> HostFeatures;
619ac1d23edSserge-sans-paille     if (sys::getHostCPUFeatures(HostFeatures))
620ac1d23edSserge-sans-paille       for (auto &F : HostFeatures)
621ac1d23edSserge-sans-paille         Features.AddFeature(F.first(), F.second);
622ac1d23edSserge-sans-paille   }
623ac1d23edSserge-sans-paille 
624ac1d23edSserge-sans-paille   for (auto const &MAttr : getMAttrs())
625ac1d23edSserge-sans-paille     Features.AddFeature(MAttr);
626ac1d23edSserge-sans-paille 
627ac1d23edSserge-sans-paille   return Features.getFeatures();
628ac1d23edSserge-sans-paille }
629ac1d23edSserge-sans-paille 
630ac1d23edSserge-sans-paille void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
631ac1d23edSserge-sans-paille   B.addAttribute(Name, Val ? "true" : "false");
632ac1d23edSserge-sans-paille }
633ac1d23edSserge-sans-paille 
634ac1d23edSserge-sans-paille #define HANDLE_BOOL_ATTR(CL, AttrName)                                         \
635ac1d23edSserge-sans-paille   do {                                                                         \
636ac1d23edSserge-sans-paille     if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName))            \
637ac1d23edSserge-sans-paille       renderBoolStringAttr(NewAttrs, AttrName, *CL);                           \
638ac1d23edSserge-sans-paille   } while (0)
639ac1d23edSserge-sans-paille 
640ac1d23edSserge-sans-paille /// Set function attributes of function \p F based on CPU, Features, and command
641ac1d23edSserge-sans-paille /// line flags.
642ac1d23edSserge-sans-paille void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
643ac1d23edSserge-sans-paille                                     Function &F) {
644ac1d23edSserge-sans-paille   auto &Ctx = F.getContext();
645ac1d23edSserge-sans-paille   AttributeList Attrs = F.getAttributes();
646ac1d23edSserge-sans-paille   AttrBuilder NewAttrs;
647ac1d23edSserge-sans-paille 
648ac1d23edSserge-sans-paille   if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
649ac1d23edSserge-sans-paille     NewAttrs.addAttribute("target-cpu", CPU);
650ac1d23edSserge-sans-paille   if (!Features.empty()) {
651ac1d23edSserge-sans-paille     // Append the command line features to any that are already on the function.
652ac1d23edSserge-sans-paille     StringRef OldFeatures =
653ac1d23edSserge-sans-paille         F.getFnAttribute("target-features").getValueAsString();
654ac1d23edSserge-sans-paille     if (OldFeatures.empty())
655ac1d23edSserge-sans-paille       NewAttrs.addAttribute("target-features", Features);
656ac1d23edSserge-sans-paille     else {
657ac1d23edSserge-sans-paille       SmallString<256> Appended(OldFeatures);
658ac1d23edSserge-sans-paille       Appended.push_back(',');
659ac1d23edSserge-sans-paille       Appended.append(Features);
660ac1d23edSserge-sans-paille       NewAttrs.addAttribute("target-features", Appended);
661ac1d23edSserge-sans-paille     }
662ac1d23edSserge-sans-paille   }
663ac1d23edSserge-sans-paille   if (FramePointerUsageView->getNumOccurrences() > 0 &&
664ac1d23edSserge-sans-paille       !F.hasFnAttribute("frame-pointer")) {
6652786e673SFangrui Song     if (getFramePointerUsage() == FramePointerKind::All)
666ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "all");
6672786e673SFangrui Song     else if (getFramePointerUsage() == FramePointerKind::NonLeaf)
668ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "non-leaf");
6692786e673SFangrui Song     else if (getFramePointerUsage() == FramePointerKind::None)
670ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "none");
671ac1d23edSserge-sans-paille   }
672ac1d23edSserge-sans-paille   if (DisableTailCallsView->getNumOccurrences() > 0)
673ac1d23edSserge-sans-paille     NewAttrs.addAttribute("disable-tail-calls",
674ac1d23edSserge-sans-paille                           toStringRef(getDisableTailCalls()));
675ac1d23edSserge-sans-paille   if (getStackRealign())
676ac1d23edSserge-sans-paille     NewAttrs.addAttribute("stackrealign");
677ac1d23edSserge-sans-paille 
678ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math");
679ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math");
680ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
681ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
682ac1d23edSserge-sans-paille 
6830ab5b5b8SMatt Arsenault   if (DenormalFPMathView->getNumOccurrences() > 0 &&
6840ab5b5b8SMatt Arsenault       !F.hasFnAttribute("denormal-fp-math")) {
6850ab5b5b8SMatt Arsenault     DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
6860ab5b5b8SMatt Arsenault 
6870ab5b5b8SMatt Arsenault     // FIXME: Command line flag should expose separate input/output modes.
6880ab5b5b8SMatt Arsenault     NewAttrs.addAttribute("denormal-fp-math",
6890ab5b5b8SMatt Arsenault                           DenormalMode(DenormKind, DenormKind).str());
6900ab5b5b8SMatt Arsenault   }
6910ab5b5b8SMatt Arsenault 
692a8cc9047SMatt Arsenault   if (DenormalFP32MathView->getNumOccurrences() > 0 &&
693a8cc9047SMatt Arsenault       !F.hasFnAttribute("denormal-fp-math-f32")) {
694a8cc9047SMatt Arsenault     // FIXME: Command line flag should expose separate input/output modes.
695a8cc9047SMatt Arsenault     DenormalMode::DenormalModeKind DenormKind = getDenormalFP32Math();
696a8cc9047SMatt Arsenault 
697a8cc9047SMatt Arsenault     NewAttrs.addAttribute(
698a8cc9047SMatt Arsenault       "denormal-fp-math-f32",
699a8cc9047SMatt Arsenault       DenormalMode(DenormKind, DenormKind).str());
700a8cc9047SMatt Arsenault   }
701a8cc9047SMatt Arsenault 
702ac1d23edSserge-sans-paille   if (TrapFuncNameView->getNumOccurrences() > 0)
703ac1d23edSserge-sans-paille     for (auto &B : F)
704ac1d23edSserge-sans-paille       for (auto &I : B)
705ac1d23edSserge-sans-paille         if (auto *Call = dyn_cast<CallInst>(&I))
706ac1d23edSserge-sans-paille           if (const auto *F = Call->getCalledFunction())
707ac1d23edSserge-sans-paille             if (F->getIntrinsicID() == Intrinsic::debugtrap ||
708ac1d23edSserge-sans-paille                 F->getIntrinsicID() == Intrinsic::trap)
709ac1d23edSserge-sans-paille               Call->addAttribute(
710ac1d23edSserge-sans-paille                   AttributeList::FunctionIndex,
711ac1d23edSserge-sans-paille                   Attribute::get(Ctx, "trap-func-name", getTrapFuncName()));
712ac1d23edSserge-sans-paille 
713ac1d23edSserge-sans-paille   // Let NewAttrs override Attrs.
714ac1d23edSserge-sans-paille   F.setAttributes(
715ac1d23edSserge-sans-paille       Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
716ac1d23edSserge-sans-paille }
717ac1d23edSserge-sans-paille 
718ac1d23edSserge-sans-paille /// Set function attributes of functions in Module M based on CPU,
719ac1d23edSserge-sans-paille /// Features, and command line flags.
720ac1d23edSserge-sans-paille void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
721ac1d23edSserge-sans-paille                                     Module &M) {
722ac1d23edSserge-sans-paille   for (Function &F : M)
723ac1d23edSserge-sans-paille     setFunctionAttributes(CPU, Features, F);
724ac1d23edSserge-sans-paille }
725