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"
16989f1c72Sserge-sans-paille #include "llvm/ADT/StringExtras.h"
17989f1c72Sserge-sans-paille #include "llvm/ADT/Triple.h"
18989f1c72Sserge-sans-paille #include "llvm/IR/Instructions.h"
19989f1c72Sserge-sans-paille #include "llvm/IR/Intrinsics.h"
20d9b9ce6cSSimon Pilgrim #include "llvm/IR/Module.h"
21989f1c72Sserge-sans-paille #include "llvm/MC/MCTargetOptionsCommandFlags.h"
22d9b9ce6cSSimon Pilgrim #include "llvm/MC/SubtargetFeature.h"
23d9b9ce6cSSimon Pilgrim #include "llvm/Support/CommandLine.h"
24d9b9ce6cSSimon Pilgrim #include "llvm/Support/Host.h"
25ba7a92c0SNico Weber #include "llvm/Support/MemoryBuffer.h"
26ac1d23edSserge-sans-paille 
27ac1d23edSserge-sans-paille using namespace llvm;
28ac1d23edSserge-sans-paille 
29ac1d23edSserge-sans-paille #define CGOPT(TY, NAME)                                                        \
30ac1d23edSserge-sans-paille   static cl::opt<TY> *NAME##View;                                              \
31ac1d23edSserge-sans-paille   TY codegen::get##NAME() {                                                    \
32ac1d23edSserge-sans-paille     assert(NAME##View && "RegisterCodeGenFlags not created.");                 \
33ac1d23edSserge-sans-paille     return *NAME##View;                                                        \
34ac1d23edSserge-sans-paille   }
35ac1d23edSserge-sans-paille 
36ac1d23edSserge-sans-paille #define CGLIST(TY, NAME)                                                       \
37ac1d23edSserge-sans-paille   static cl::list<TY> *NAME##View;                                             \
38ac1d23edSserge-sans-paille   std::vector<TY> codegen::get##NAME() {                                       \
39ac1d23edSserge-sans-paille     assert(NAME##View && "RegisterCodeGenFlags not created.");                 \
40ac1d23edSserge-sans-paille     return *NAME##View;                                                        \
41ac1d23edSserge-sans-paille   }
42ac1d23edSserge-sans-paille 
43ac1d23edSserge-sans-paille #define CGOPT_EXP(TY, NAME)                                                    \
44ac1d23edSserge-sans-paille   CGOPT(TY, NAME)                                                              \
45ac1d23edSserge-sans-paille   Optional<TY> codegen::getExplicit##NAME() {                                  \
46ac1d23edSserge-sans-paille     if (NAME##View->getNumOccurrences()) {                                     \
47ac1d23edSserge-sans-paille       TY res = *NAME##View;                                                    \
48ac1d23edSserge-sans-paille       return res;                                                              \
49ac1d23edSserge-sans-paille     }                                                                          \
50ac1d23edSserge-sans-paille     return None;                                                               \
51ac1d23edSserge-sans-paille   }
52ac1d23edSserge-sans-paille 
CGOPT(std::string,MArch)53ac1d23edSserge-sans-paille CGOPT(std::string, MArch)
54ac1d23edSserge-sans-paille CGOPT(std::string, MCPU)
55ac1d23edSserge-sans-paille CGLIST(std::string, MAttrs)
56ac1d23edSserge-sans-paille CGOPT_EXP(Reloc::Model, RelocModel)
57ac1d23edSserge-sans-paille CGOPT(ThreadModel::Model, ThreadModel)
58ac1d23edSserge-sans-paille CGOPT_EXP(CodeModel::Model, CodeModel)
59ac1d23edSserge-sans-paille CGOPT(ExceptionHandling, ExceptionModel)
60ac1d23edSserge-sans-paille CGOPT_EXP(CodeGenFileType, FileType)
612786e673SFangrui Song CGOPT(FramePointerKind, FramePointerUsage)
62ac1d23edSserge-sans-paille CGOPT(bool, EnableUnsafeFPMath)
63ac1d23edSserge-sans-paille CGOPT(bool, EnableNoInfsFPMath)
64ac1d23edSserge-sans-paille CGOPT(bool, EnableNoNaNsFPMath)
65ac1d23edSserge-sans-paille CGOPT(bool, EnableNoSignedZerosFPMath)
66256d2533SMasoud Ataei CGOPT(bool, EnableApproxFuncFPMath)
67ac1d23edSserge-sans-paille CGOPT(bool, EnableNoTrappingFPMath)
68c92f29b0SZarko Todorovski CGOPT(bool, EnableAIXExtendedAltivecABI)
690ab5b5b8SMatt Arsenault CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath)
70a8cc9047SMatt Arsenault CGOPT(DenormalMode::DenormalModeKind, DenormalFP32Math)
71ac1d23edSserge-sans-paille CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
72ac1d23edSserge-sans-paille CGOPT(FloatABI::ABIType, FloatABIForCalls)
73ac1d23edSserge-sans-paille CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
74a773db7dSDoug Gregor CGOPT(SwiftAsyncFramePointerMode, SwiftAsyncFramePointer)
75ac1d23edSserge-sans-paille CGOPT(bool, DontPlaceZerosInBSS)
76ac1d23edSserge-sans-paille CGOPT(bool, EnableGuaranteedTailCallOpt)
77ac1d23edSserge-sans-paille CGOPT(bool, DisableTailCalls)
78ac1d23edSserge-sans-paille CGOPT(bool, StackSymbolOrdering)
79ac1d23edSserge-sans-paille CGOPT(bool, StackRealign)
80ac1d23edSserge-sans-paille CGOPT(std::string, TrapFuncName)
81ac1d23edSserge-sans-paille CGOPT(bool, UseCtors)
8264902d33SJulian Lettner CGOPT(bool, LowerGlobalDtorsViaCxaAtExit)
83ac1d23edSserge-sans-paille CGOPT(bool, RelaxELFRelocations)
84ac1d23edSserge-sans-paille CGOPT_EXP(bool, DataSections)
85ac1d23edSserge-sans-paille CGOPT_EXP(bool, FunctionSections)
8692bca128Sdiggerlin CGOPT(bool, IgnoreXCOFFVisibility)
87997d286fSdiggerlin CGOPT(bool, XCOFFTracebackTable)
88ac1d23edSserge-sans-paille CGOPT(std::string, BBSections)
89ac1d23edSserge-sans-paille CGOPT(unsigned, TLSSize)
90ac1d23edSserge-sans-paille CGOPT(bool, EmulatedTLS)
91ac1d23edSserge-sans-paille CGOPT(bool, UniqueSectionNames)
92e0bca46bSSriraman Tallam CGOPT(bool, UniqueBasicBlockSectionNames)
93ac1d23edSserge-sans-paille CGOPT(EABI, EABIVersion)
94ac1d23edSserge-sans-paille CGOPT(DebuggerKind, DebuggerTuningOpt)
95ac1d23edSserge-sans-paille CGOPT(bool, EnableStackSizeSection)
96ac1d23edSserge-sans-paille CGOPT(bool, EnableAddrsig)
97ac1d23edSserge-sans-paille CGOPT(bool, EmitCallSiteInfo)
9894faadacSSnehasish Kumar CGOPT(bool, EnableMachineFunctionSplitter)
99ac1d23edSserge-sans-paille CGOPT(bool, EnableDebugEntryValues)
100ac1d23edSserge-sans-paille CGOPT(bool, ForceDwarfFrameSection)
1017c7c8e0dSIan Levesque CGOPT(bool, XRayOmitFunctionIndex)
102a0ca4c46SChen Zheng CGOPT(bool, DebugStrictDwarf)
103a1944386SFangrui Song CGOPT(unsigned, AlignLoops)
104f9270214SYuanfang Chen CGOPT(bool, JMCInstrument)
105ac1d23edSserge-sans-paille 
106ac1d23edSserge-sans-paille codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
107ac1d23edSserge-sans-paille #define CGBINDOPT(NAME)                                                        \
108ac1d23edSserge-sans-paille   do {                                                                         \
109ac1d23edSserge-sans-paille     NAME##View = std::addressof(NAME);                                         \
110ac1d23edSserge-sans-paille   } while (0)
111ac1d23edSserge-sans-paille 
112ac1d23edSserge-sans-paille   static cl::opt<std::string> MArch(
113ac1d23edSserge-sans-paille       "march", cl::desc("Architecture to generate code for (see --version)"));
114ac1d23edSserge-sans-paille   CGBINDOPT(MArch);
115ac1d23edSserge-sans-paille 
116ac1d23edSserge-sans-paille   static cl::opt<std::string> MCPU(
117ac1d23edSserge-sans-paille       "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
118ac1d23edSserge-sans-paille       cl::value_desc("cpu-name"), cl::init(""));
119ac1d23edSserge-sans-paille   CGBINDOPT(MCPU);
120ac1d23edSserge-sans-paille 
121ac1d23edSserge-sans-paille   static cl::list<std::string> MAttrs(
122ac1d23edSserge-sans-paille       "mattr", cl::CommaSeparated,
123ac1d23edSserge-sans-paille       cl::desc("Target specific attributes (-mattr=help for details)"),
124ac1d23edSserge-sans-paille       cl::value_desc("a1,+a2,-a3,..."));
125ac1d23edSserge-sans-paille   CGBINDOPT(MAttrs);
126ac1d23edSserge-sans-paille 
127ac1d23edSserge-sans-paille   static cl::opt<Reloc::Model> RelocModel(
128ac1d23edSserge-sans-paille       "relocation-model", cl::desc("Choose relocation model"),
129ac1d23edSserge-sans-paille       cl::values(
130ac1d23edSserge-sans-paille           clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
131ac1d23edSserge-sans-paille           clEnumValN(Reloc::PIC_, "pic",
132ac1d23edSserge-sans-paille                      "Fully relocatable, position independent code"),
133ac1d23edSserge-sans-paille           clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
134ac1d23edSserge-sans-paille                      "Relocatable external references, non-relocatable code"),
135ac1d23edSserge-sans-paille           clEnumValN(
136ac1d23edSserge-sans-paille               Reloc::ROPI, "ropi",
137ac1d23edSserge-sans-paille               "Code and read-only data relocatable, accessed PC-relative"),
138ac1d23edSserge-sans-paille           clEnumValN(
139ac1d23edSserge-sans-paille               Reloc::RWPI, "rwpi",
140ac1d23edSserge-sans-paille               "Read-write data relocatable, accessed relative to static base"),
141ac1d23edSserge-sans-paille           clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
142ac1d23edSserge-sans-paille                      "Combination of ropi and rwpi")));
143ac1d23edSserge-sans-paille   CGBINDOPT(RelocModel);
144ac1d23edSserge-sans-paille 
145ac1d23edSserge-sans-paille   static cl::opt<ThreadModel::Model> ThreadModel(
146ac1d23edSserge-sans-paille       "thread-model", cl::desc("Choose threading model"),
147ac1d23edSserge-sans-paille       cl::init(ThreadModel::POSIX),
148ac1d23edSserge-sans-paille       cl::values(
149ac1d23edSserge-sans-paille           clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
150ac1d23edSserge-sans-paille           clEnumValN(ThreadModel::Single, "single", "Single thread model")));
151ac1d23edSserge-sans-paille   CGBINDOPT(ThreadModel);
152ac1d23edSserge-sans-paille 
153ac1d23edSserge-sans-paille   static cl::opt<CodeModel::Model> CodeModel(
154ac1d23edSserge-sans-paille       "code-model", cl::desc("Choose code model"),
155ac1d23edSserge-sans-paille       cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
156ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Small, "small", "Small code model"),
157ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
158ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
159ac1d23edSserge-sans-paille                  clEnumValN(CodeModel::Large, "large", "Large code model")));
160ac1d23edSserge-sans-paille   CGBINDOPT(CodeModel);
161ac1d23edSserge-sans-paille 
162ac1d23edSserge-sans-paille   static cl::opt<ExceptionHandling> ExceptionModel(
163ac1d23edSserge-sans-paille       "exception-model", cl::desc("exception model"),
164ac1d23edSserge-sans-paille       cl::init(ExceptionHandling::None),
165ac1d23edSserge-sans-paille       cl::values(
166ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::None, "default",
167ac1d23edSserge-sans-paille                      "default exception handling model"),
168ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
169ac1d23edSserge-sans-paille                      "DWARF-like CFI based exception handling"),
170ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::SjLj, "sjlj",
171ac1d23edSserge-sans-paille                      "SjLj exception handling"),
172ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
173ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::WinEH, "wineh",
174ac1d23edSserge-sans-paille                      "Windows exception model"),
175ac1d23edSserge-sans-paille           clEnumValN(ExceptionHandling::Wasm, "wasm",
176ac1d23edSserge-sans-paille                      "WebAssembly exception handling")));
177ac1d23edSserge-sans-paille   CGBINDOPT(ExceptionModel);
178ac1d23edSserge-sans-paille 
179ac1d23edSserge-sans-paille   static cl::opt<CodeGenFileType> FileType(
180ac1d23edSserge-sans-paille       "filetype", cl::init(CGFT_AssemblyFile),
181ac1d23edSserge-sans-paille       cl::desc(
182ac1d23edSserge-sans-paille           "Choose a file type (not all types are supported by all targets):"),
183ac1d23edSserge-sans-paille       cl::values(
184ac1d23edSserge-sans-paille           clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"),
185ac1d23edSserge-sans-paille           clEnumValN(CGFT_ObjectFile, "obj",
186ac1d23edSserge-sans-paille                      "Emit a native object ('.o') file"),
187ac1d23edSserge-sans-paille           clEnumValN(CGFT_Null, "null",
188ac1d23edSserge-sans-paille                      "Emit nothing, for performance testing")));
189ac1d23edSserge-sans-paille   CGBINDOPT(FileType);
190ac1d23edSserge-sans-paille 
1912786e673SFangrui Song   static cl::opt<FramePointerKind> FramePointerUsage(
192ac1d23edSserge-sans-paille       "frame-pointer",
193ac1d23edSserge-sans-paille       cl::desc("Specify frame pointer elimination optimization"),
1942786e673SFangrui Song       cl::init(FramePointerKind::None),
195ac1d23edSserge-sans-paille       cl::values(
1962786e673SFangrui Song           clEnumValN(FramePointerKind::All, "all",
197ac1d23edSserge-sans-paille                      "Disable frame pointer elimination"),
1982786e673SFangrui Song           clEnumValN(FramePointerKind::NonLeaf, "non-leaf",
199ac1d23edSserge-sans-paille                      "Disable frame pointer elimination for non-leaf frame"),
2002786e673SFangrui Song           clEnumValN(FramePointerKind::None, "none",
201ac1d23edSserge-sans-paille                      "Enable frame pointer elimination")));
202ac1d23edSserge-sans-paille   CGBINDOPT(FramePointerUsage);
203ac1d23edSserge-sans-paille 
204ac1d23edSserge-sans-paille   static cl::opt<bool> EnableUnsafeFPMath(
205ac1d23edSserge-sans-paille       "enable-unsafe-fp-math",
206ac1d23edSserge-sans-paille       cl::desc("Enable optimizations that may decrease FP precision"),
207ac1d23edSserge-sans-paille       cl::init(false));
208ac1d23edSserge-sans-paille   CGBINDOPT(EnableUnsafeFPMath);
209ac1d23edSserge-sans-paille 
210ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoInfsFPMath(
211ac1d23edSserge-sans-paille       "enable-no-infs-fp-math",
212ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume no +-Infs"),
213ac1d23edSserge-sans-paille       cl::init(false));
214ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoInfsFPMath);
215ac1d23edSserge-sans-paille 
216ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoNaNsFPMath(
217ac1d23edSserge-sans-paille       "enable-no-nans-fp-math",
218ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume no NaNs"),
219ac1d23edSserge-sans-paille       cl::init(false));
220ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoNaNsFPMath);
221ac1d23edSserge-sans-paille 
222ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoSignedZerosFPMath(
223ac1d23edSserge-sans-paille       "enable-no-signed-zeros-fp-math",
224ac1d23edSserge-sans-paille       cl::desc("Enable FP math optimizations that assume "
225ac1d23edSserge-sans-paille                "the sign of 0 is insignificant"),
226ac1d23edSserge-sans-paille       cl::init(false));
227ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoSignedZerosFPMath);
228ac1d23edSserge-sans-paille 
229256d2533SMasoud Ataei   static cl::opt<bool> EnableApproxFuncFPMath(
230256d2533SMasoud Ataei       "enable-approx-func-fp-math",
231256d2533SMasoud Ataei       cl::desc("Enable FP math optimizations that assume approx func"),
232256d2533SMasoud Ataei       cl::init(false));
233256d2533SMasoud Ataei   CGBINDOPT(EnableApproxFuncFPMath);
234256d2533SMasoud Ataei 
235ac1d23edSserge-sans-paille   static cl::opt<bool> EnableNoTrappingFPMath(
236ac1d23edSserge-sans-paille       "enable-no-trapping-fp-math",
237ac1d23edSserge-sans-paille       cl::desc("Enable setting the FP exceptions build "
238ac1d23edSserge-sans-paille                "attribute not to use exceptions"),
239ac1d23edSserge-sans-paille       cl::init(false));
240ac1d23edSserge-sans-paille   CGBINDOPT(EnableNoTrappingFPMath);
241ac1d23edSserge-sans-paille 
242a8cc9047SMatt Arsenault   static const auto DenormFlagEnumOptions =
243a8cc9047SMatt Arsenault   cl::values(clEnumValN(DenormalMode::IEEE, "ieee",
244a8cc9047SMatt Arsenault                         "IEEE 754 denormal numbers"),
2450ab5b5b8SMatt Arsenault              clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
246ac1d23edSserge-sans-paille                         "the sign of a  flushed-to-zero number is preserved "
247ac1d23edSserge-sans-paille                         "in the sign of 0"),
2480ab5b5b8SMatt Arsenault              clEnumValN(DenormalMode::PositiveZero, "positive-zero",
249a8cc9047SMatt Arsenault                         "denormals are flushed to positive zero"));
250a8cc9047SMatt Arsenault 
251a8cc9047SMatt Arsenault   // FIXME: Doesn't have way to specify separate input and output modes.
252a8cc9047SMatt Arsenault   static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
253a8cc9047SMatt Arsenault     "denormal-fp-math",
254a8cc9047SMatt Arsenault     cl::desc("Select which denormal numbers the code is permitted to require"),
255a8cc9047SMatt Arsenault     cl::init(DenormalMode::IEEE),
256a8cc9047SMatt Arsenault     DenormFlagEnumOptions);
257ac1d23edSserge-sans-paille   CGBINDOPT(DenormalFPMath);
258ac1d23edSserge-sans-paille 
259a8cc9047SMatt Arsenault   static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math(
260a8cc9047SMatt Arsenault     "denormal-fp-math-f32",
261a8cc9047SMatt Arsenault     cl::desc("Select which denormal numbers the code is permitted to require for float"),
262a8cc9047SMatt Arsenault     cl::init(DenormalMode::Invalid),
263a8cc9047SMatt Arsenault     DenormFlagEnumOptions);
264a8cc9047SMatt Arsenault   CGBINDOPT(DenormalFP32Math);
265a8cc9047SMatt Arsenault 
266ac1d23edSserge-sans-paille   static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
267ac1d23edSserge-sans-paille       "enable-sign-dependent-rounding-fp-math", cl::Hidden,
268ac1d23edSserge-sans-paille       cl::desc("Force codegen to assume rounding mode can change dynamically"),
269ac1d23edSserge-sans-paille       cl::init(false));
270ac1d23edSserge-sans-paille   CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
271ac1d23edSserge-sans-paille 
272ac1d23edSserge-sans-paille   static cl::opt<FloatABI::ABIType> FloatABIForCalls(
273ac1d23edSserge-sans-paille       "float-abi", cl::desc("Choose float ABI type"),
274ac1d23edSserge-sans-paille       cl::init(FloatABI::Default),
275ac1d23edSserge-sans-paille       cl::values(clEnumValN(FloatABI::Default, "default",
276ac1d23edSserge-sans-paille                             "Target default float ABI type"),
277ac1d23edSserge-sans-paille                  clEnumValN(FloatABI::Soft, "soft",
278ac1d23edSserge-sans-paille                             "Soft float ABI (implied by -soft-float)"),
279ac1d23edSserge-sans-paille                  clEnumValN(FloatABI::Hard, "hard",
280ac1d23edSserge-sans-paille                             "Hard float ABI (uses FP registers)")));
281ac1d23edSserge-sans-paille   CGBINDOPT(FloatABIForCalls);
282ac1d23edSserge-sans-paille 
283ac1d23edSserge-sans-paille   static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
284ac1d23edSserge-sans-paille       "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
285ac1d23edSserge-sans-paille       cl::init(FPOpFusion::Standard),
286ac1d23edSserge-sans-paille       cl::values(
287ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Fast, "fast",
288ac1d23edSserge-sans-paille                      "Fuse FP ops whenever profitable"),
289ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
290ac1d23edSserge-sans-paille           clEnumValN(FPOpFusion::Strict, "off",
291ac1d23edSserge-sans-paille                      "Only fuse FP ops when the result won't be affected.")));
292ac1d23edSserge-sans-paille   CGBINDOPT(FuseFPOps);
293ac1d23edSserge-sans-paille 
294a773db7dSDoug Gregor   static cl::opt<SwiftAsyncFramePointerMode> SwiftAsyncFramePointer(
295a773db7dSDoug Gregor       "swift-async-fp",
296a773db7dSDoug Gregor       cl::desc("Determine when the Swift async frame pointer should be set"),
297a773db7dSDoug Gregor       cl::init(SwiftAsyncFramePointerMode::Always),
298a773db7dSDoug Gregor       cl::values(clEnumValN(SwiftAsyncFramePointerMode::DeploymentBased, "auto",
299a773db7dSDoug Gregor                             "Determine based on deployment target"),
300a773db7dSDoug Gregor                  clEnumValN(SwiftAsyncFramePointerMode::Always, "always",
301a773db7dSDoug Gregor                             "Always set the bit"),
302a773db7dSDoug Gregor                  clEnumValN(SwiftAsyncFramePointerMode::Never, "never",
303a773db7dSDoug Gregor                             "Never set the bit")));
304a773db7dSDoug Gregor   CGBINDOPT(SwiftAsyncFramePointer);
305a773db7dSDoug Gregor 
306ac1d23edSserge-sans-paille   static cl::opt<bool> DontPlaceZerosInBSS(
307ac1d23edSserge-sans-paille       "nozero-initialized-in-bss",
308ac1d23edSserge-sans-paille       cl::desc("Don't place zero-initialized symbols into bss section"),
309ac1d23edSserge-sans-paille       cl::init(false));
310ac1d23edSserge-sans-paille   CGBINDOPT(DontPlaceZerosInBSS);
311ac1d23edSserge-sans-paille 
312c92f29b0SZarko Todorovski   static cl::opt<bool> EnableAIXExtendedAltivecABI(
313c92f29b0SZarko Todorovski       "vec-extabi", cl::desc("Enable the AIX Extended Altivec ABI."),
314c92f29b0SZarko Todorovski       cl::init(false));
315c92f29b0SZarko Todorovski   CGBINDOPT(EnableAIXExtendedAltivecABI);
316c92f29b0SZarko Todorovski 
317ac1d23edSserge-sans-paille   static cl::opt<bool> EnableGuaranteedTailCallOpt(
318ac1d23edSserge-sans-paille       "tailcallopt",
319ac1d23edSserge-sans-paille       cl::desc(
320ac1d23edSserge-sans-paille           "Turn fastcc calls into tail calls by (potentially) changing ABI."),
321ac1d23edSserge-sans-paille       cl::init(false));
322ac1d23edSserge-sans-paille   CGBINDOPT(EnableGuaranteedTailCallOpt);
323ac1d23edSserge-sans-paille 
324ac1d23edSserge-sans-paille   static cl::opt<bool> DisableTailCalls(
325ac1d23edSserge-sans-paille       "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
326ac1d23edSserge-sans-paille   CGBINDOPT(DisableTailCalls);
327ac1d23edSserge-sans-paille 
328ac1d23edSserge-sans-paille   static cl::opt<bool> StackSymbolOrdering(
329ac1d23edSserge-sans-paille       "stack-symbol-ordering", cl::desc("Order local stack symbols."),
330ac1d23edSserge-sans-paille       cl::init(true));
331ac1d23edSserge-sans-paille   CGBINDOPT(StackSymbolOrdering);
332ac1d23edSserge-sans-paille 
333ac1d23edSserge-sans-paille   static cl::opt<bool> StackRealign(
334ac1d23edSserge-sans-paille       "stackrealign",
335ac1d23edSserge-sans-paille       cl::desc("Force align the stack to the minimum alignment"),
336ac1d23edSserge-sans-paille       cl::init(false));
337ac1d23edSserge-sans-paille   CGBINDOPT(StackRealign);
338ac1d23edSserge-sans-paille 
339ac1d23edSserge-sans-paille   static cl::opt<std::string> TrapFuncName(
340ac1d23edSserge-sans-paille       "trap-func", cl::Hidden,
341ac1d23edSserge-sans-paille       cl::desc("Emit a call to trap function rather than a trap instruction"),
342ac1d23edSserge-sans-paille       cl::init(""));
343ac1d23edSserge-sans-paille   CGBINDOPT(TrapFuncName);
344ac1d23edSserge-sans-paille 
345ac1d23edSserge-sans-paille   static cl::opt<bool> UseCtors("use-ctors",
346ac1d23edSserge-sans-paille                                 cl::desc("Use .ctors instead of .init_array."),
347ac1d23edSserge-sans-paille                                 cl::init(false));
348ac1d23edSserge-sans-paille   CGBINDOPT(UseCtors);
349ac1d23edSserge-sans-paille 
35064902d33SJulian Lettner   static cl::opt<bool> LowerGlobalDtorsViaCxaAtExit(
35164902d33SJulian Lettner       "lower-global-dtors-via-cxa-atexit",
35264902d33SJulian Lettner       cl::desc("Lower llvm.global_dtors (global destructors) via __cxa_atexit"),
35364902d33SJulian Lettner       cl::init(true));
35464902d33SJulian Lettner   CGBINDOPT(LowerGlobalDtorsViaCxaAtExit);
35564902d33SJulian Lettner 
356ac1d23edSserge-sans-paille   static cl::opt<bool> RelaxELFRelocations(
357ac1d23edSserge-sans-paille       "relax-elf-relocations",
358ac1d23edSserge-sans-paille       cl::desc(
359ac1d23edSserge-sans-paille           "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
360ac1d23edSserge-sans-paille       cl::init(false));
361ac1d23edSserge-sans-paille   CGBINDOPT(RelaxELFRelocations);
362ac1d23edSserge-sans-paille 
363ac1d23edSserge-sans-paille   static cl::opt<bool> DataSections(
364ac1d23edSserge-sans-paille       "data-sections", cl::desc("Emit data into separate sections"),
365ac1d23edSserge-sans-paille       cl::init(false));
366ac1d23edSserge-sans-paille   CGBINDOPT(DataSections);
367ac1d23edSserge-sans-paille 
368ac1d23edSserge-sans-paille   static cl::opt<bool> FunctionSections(
369ac1d23edSserge-sans-paille       "function-sections", cl::desc("Emit functions into separate sections"),
370ac1d23edSserge-sans-paille       cl::init(false));
371ac1d23edSserge-sans-paille   CGBINDOPT(FunctionSections);
372ac1d23edSserge-sans-paille 
37392bca128Sdiggerlin   static cl::opt<bool> IgnoreXCOFFVisibility(
37492bca128Sdiggerlin       "ignore-xcoff-visibility",
37592bca128Sdiggerlin       cl::desc("Not emit the visibility attribute for asm in AIX OS or give "
37692bca128Sdiggerlin                "all symbols 'unspecified' visibility in XCOFF object file"),
37792bca128Sdiggerlin       cl::init(false));
37892bca128Sdiggerlin   CGBINDOPT(IgnoreXCOFFVisibility);
37992bca128Sdiggerlin 
380997d286fSdiggerlin   static cl::opt<bool> XCOFFTracebackTable(
381997d286fSdiggerlin       "xcoff-traceback-table", cl::desc("Emit the XCOFF traceback table"),
382997d286fSdiggerlin       cl::init(true));
383997d286fSdiggerlin   CGBINDOPT(XCOFFTracebackTable);
384997d286fSdiggerlin 
385ac1d23edSserge-sans-paille   static cl::opt<std::string> BBSections(
386ca6b6d40SSriraman Tallam       "basic-block-sections",
387ac1d23edSserge-sans-paille       cl::desc("Emit basic blocks into separate sections"),
388ac1d23edSserge-sans-paille       cl::value_desc("all | <function list (file)> | labels | none"),
389ac1d23edSserge-sans-paille       cl::init("none"));
390ac1d23edSserge-sans-paille   CGBINDOPT(BBSections);
391ac1d23edSserge-sans-paille 
392ac1d23edSserge-sans-paille   static cl::opt<unsigned> TLSSize(
393ac1d23edSserge-sans-paille       "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
394ac1d23edSserge-sans-paille   CGBINDOPT(TLSSize);
395ac1d23edSserge-sans-paille 
396ac1d23edSserge-sans-paille   static cl::opt<bool> EmulatedTLS(
397ac1d23edSserge-sans-paille       "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
398ac1d23edSserge-sans-paille   CGBINDOPT(EmulatedTLS);
399ac1d23edSserge-sans-paille 
400ac1d23edSserge-sans-paille   static cl::opt<bool> UniqueSectionNames(
401ac1d23edSserge-sans-paille       "unique-section-names", cl::desc("Give unique names to every section"),
402ac1d23edSserge-sans-paille       cl::init(true));
403ac1d23edSserge-sans-paille   CGBINDOPT(UniqueSectionNames);
404ac1d23edSserge-sans-paille 
405e0bca46bSSriraman Tallam   static cl::opt<bool> UniqueBasicBlockSectionNames(
406ca6b6d40SSriraman Tallam       "unique-basic-block-section-names",
407ac1d23edSserge-sans-paille       cl::desc("Give unique names to every basic block section"),
408ac1d23edSserge-sans-paille       cl::init(false));
409e0bca46bSSriraman Tallam   CGBINDOPT(UniqueBasicBlockSectionNames);
410ac1d23edSserge-sans-paille 
411ac1d23edSserge-sans-paille   static cl::opt<EABI> EABIVersion(
412ac1d23edSserge-sans-paille       "meabi", cl::desc("Set EABI type (default depends on triple):"),
413ac1d23edSserge-sans-paille       cl::init(EABI::Default),
414ac1d23edSserge-sans-paille       cl::values(
415ac1d23edSserge-sans-paille           clEnumValN(EABI::Default, "default", "Triple default EABI version"),
416ac1d23edSserge-sans-paille           clEnumValN(EABI::EABI4, "4", "EABI version 4"),
417ac1d23edSserge-sans-paille           clEnumValN(EABI::EABI5, "5", "EABI version 5"),
418ac1d23edSserge-sans-paille           clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
419ac1d23edSserge-sans-paille   CGBINDOPT(EABIVersion);
420ac1d23edSserge-sans-paille 
421ac1d23edSserge-sans-paille   static cl::opt<DebuggerKind> DebuggerTuningOpt(
422ac1d23edSserge-sans-paille       "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
423ac1d23edSserge-sans-paille       cl::init(DebuggerKind::Default),
424ac1d23edSserge-sans-paille       cl::values(
425ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
426ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
4270c36da72SEsme-Yi           clEnumValN(DebuggerKind::DBX, "dbx", "dbx"),
428ac1d23edSserge-sans-paille           clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
429ac1d23edSserge-sans-paille   CGBINDOPT(DebuggerTuningOpt);
430ac1d23edSserge-sans-paille 
431ac1d23edSserge-sans-paille   static cl::opt<bool> EnableStackSizeSection(
432ac1d23edSserge-sans-paille       "stack-size-section",
433ac1d23edSserge-sans-paille       cl::desc("Emit a section containing stack size metadata"),
434ac1d23edSserge-sans-paille       cl::init(false));
435ac1d23edSserge-sans-paille   CGBINDOPT(EnableStackSizeSection);
436ac1d23edSserge-sans-paille 
437ac1d23edSserge-sans-paille   static cl::opt<bool> EnableAddrsig(
438ac1d23edSserge-sans-paille       "addrsig", cl::desc("Emit an address-significance table"),
439ac1d23edSserge-sans-paille       cl::init(false));
440ac1d23edSserge-sans-paille   CGBINDOPT(EnableAddrsig);
441ac1d23edSserge-sans-paille 
442ac1d23edSserge-sans-paille   static cl::opt<bool> EmitCallSiteInfo(
443ac1d23edSserge-sans-paille       "emit-call-site-info",
444ac1d23edSserge-sans-paille       cl::desc(
445ac1d23edSserge-sans-paille           "Emit call site debug information, if debug information is enabled."),
446ac1d23edSserge-sans-paille       cl::init(false));
447ac1d23edSserge-sans-paille   CGBINDOPT(EmitCallSiteInfo);
448ac1d23edSserge-sans-paille 
449ac1d23edSserge-sans-paille   static cl::opt<bool> EnableDebugEntryValues(
450ac1d23edSserge-sans-paille       "debug-entry-values",
451d9b96210SDjordje Todorovic       cl::desc("Enable debug info for the debug entry values."),
452ac1d23edSserge-sans-paille       cl::init(false));
453ac1d23edSserge-sans-paille   CGBINDOPT(EnableDebugEntryValues);
454ac1d23edSserge-sans-paille 
45594faadacSSnehasish Kumar   static cl::opt<bool> EnableMachineFunctionSplitter(
45694faadacSSnehasish Kumar       "split-machine-functions",
45794faadacSSnehasish Kumar       cl::desc("Split out cold basic blocks from machine functions based on "
45894faadacSSnehasish Kumar                "profile information"),
45994faadacSSnehasish Kumar       cl::init(false));
46094faadacSSnehasish Kumar   CGBINDOPT(EnableMachineFunctionSplitter);
46194faadacSSnehasish Kumar 
462ac1d23edSserge-sans-paille   static cl::opt<bool> ForceDwarfFrameSection(
463ac1d23edSserge-sans-paille       "force-dwarf-frame-section",
464ac1d23edSserge-sans-paille       cl::desc("Always emit a debug frame section."), cl::init(false));
465ac1d23edSserge-sans-paille   CGBINDOPT(ForceDwarfFrameSection);
466ac1d23edSserge-sans-paille 
4677c7c8e0dSIan Levesque   static cl::opt<bool> XRayOmitFunctionIndex(
4687c7c8e0dSIan Levesque       "no-xray-index", cl::desc("Don't emit xray_fn_idx section"),
4697c7c8e0dSIan Levesque       cl::init(false));
4707c7c8e0dSIan Levesque   CGBINDOPT(XRayOmitFunctionIndex);
4717c7c8e0dSIan Levesque 
472a0ca4c46SChen Zheng   static cl::opt<bool> DebugStrictDwarf(
473a0ca4c46SChen Zheng       "strict-dwarf", cl::desc("use strict dwarf"), cl::init(false));
474a0ca4c46SChen Zheng   CGBINDOPT(DebugStrictDwarf);
475a0ca4c46SChen Zheng 
476a1944386SFangrui Song   static cl::opt<unsigned> AlignLoops("align-loops",
477a1944386SFangrui Song                                       cl::desc("Default alignment for loops"));
478a1944386SFangrui Song   CGBINDOPT(AlignLoops);
479a1944386SFangrui Song 
480f9270214SYuanfang Chen   static cl::opt<bool> JMCInstrument(
481f9270214SYuanfang Chen       "enable-jmc-instrument",
482f9270214SYuanfang Chen       cl::desc("Instrument functions with a call to __CheckForDebuggerJustMyCode"),
483f9270214SYuanfang Chen       cl::init(false));
484f9270214SYuanfang Chen   CGBINDOPT(JMCInstrument);
485f9270214SYuanfang Chen 
486ac1d23edSserge-sans-paille #undef CGBINDOPT
487ac1d23edSserge-sans-paille 
488ac1d23edSserge-sans-paille   mc::RegisterMCTargetOptionsFlags();
489ac1d23edSserge-sans-paille }
490ac1d23edSserge-sans-paille 
491ac1d23edSserge-sans-paille llvm::BasicBlockSection
getBBSectionsMode(llvm::TargetOptions & Options)492ac1d23edSserge-sans-paille codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
493ac1d23edSserge-sans-paille   if (getBBSections() == "all")
494ac1d23edSserge-sans-paille     return BasicBlockSection::All;
495ac1d23edSserge-sans-paille   else if (getBBSections() == "labels")
496ac1d23edSserge-sans-paille     return BasicBlockSection::Labels;
497ac1d23edSserge-sans-paille   else if (getBBSections() == "none")
498ac1d23edSserge-sans-paille     return BasicBlockSection::None;
499ac1d23edSserge-sans-paille   else {
500ac1d23edSserge-sans-paille     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
501ac1d23edSserge-sans-paille         MemoryBuffer::getFile(getBBSections());
502ac1d23edSserge-sans-paille     if (!MBOrErr) {
503ac1d23edSserge-sans-paille       errs() << "Error loading basic block sections function list file: "
504ac1d23edSserge-sans-paille              << MBOrErr.getError().message() << "\n";
505ac1d23edSserge-sans-paille     } else {
506ac1d23edSserge-sans-paille       Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
507ac1d23edSserge-sans-paille     }
508ac1d23edSserge-sans-paille     return BasicBlockSection::List;
509ac1d23edSserge-sans-paille   }
510ac1d23edSserge-sans-paille }
511ac1d23edSserge-sans-paille 
512ac1d23edSserge-sans-paille // Common utility function tightly tied to the options listed here. Initializes
513ac1d23edSserge-sans-paille // a TargetOptions object with CodeGen flags and returns it.
514f85bcc21Sjasonliu TargetOptions
InitTargetOptionsFromCodeGenFlags(const Triple & TheTriple)515f85bcc21Sjasonliu codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
516ac1d23edSserge-sans-paille   TargetOptions Options;
517ac1d23edSserge-sans-paille   Options.AllowFPOpFusion = getFuseFPOps();
518ac1d23edSserge-sans-paille   Options.UnsafeFPMath = getEnableUnsafeFPMath();
519ac1d23edSserge-sans-paille   Options.NoInfsFPMath = getEnableNoInfsFPMath();
520ac1d23edSserge-sans-paille   Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
521ac1d23edSserge-sans-paille   Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
522256d2533SMasoud Ataei   Options.ApproxFuncFPMath = getEnableApproxFuncFPMath();
523ac1d23edSserge-sans-paille   Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
5240ab5b5b8SMatt Arsenault 
5250ab5b5b8SMatt Arsenault   DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
5260ab5b5b8SMatt Arsenault 
5270ab5b5b8SMatt Arsenault   // FIXME: Should have separate input and output flags
5280ab5b5b8SMatt Arsenault   Options.setFPDenormalMode(DenormalMode(DenormKind, DenormKind));
5290ab5b5b8SMatt Arsenault 
530ac1d23edSserge-sans-paille   Options.HonorSignDependentRoundingFPMathOption =
531ac1d23edSserge-sans-paille       getEnableHonorSignDependentRoundingFPMath();
532ac1d23edSserge-sans-paille   if (getFloatABIForCalls() != FloatABI::Default)
533ac1d23edSserge-sans-paille     Options.FloatABIType = getFloatABIForCalls();
534c92f29b0SZarko Todorovski   Options.EnableAIXExtendedAltivecABI = getEnableAIXExtendedAltivecABI();
535ac1d23edSserge-sans-paille   Options.NoZerosInBSS = getDontPlaceZerosInBSS();
536ac1d23edSserge-sans-paille   Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
537ac1d23edSserge-sans-paille   Options.StackSymbolOrdering = getStackSymbolOrdering();
538ac1d23edSserge-sans-paille   Options.UseInitArray = !getUseCtors();
53964902d33SJulian Lettner   Options.LowerGlobalDtorsViaCxaAtExit = getLowerGlobalDtorsViaCxaAtExit();
540ac1d23edSserge-sans-paille   Options.RelaxELFRelocations = getRelaxELFRelocations();
541f85bcc21Sjasonliu   Options.DataSections =
542*129b531cSKazu Hirata       getExplicitDataSections().value_or(TheTriple.hasDefaultDataSections());
543ac1d23edSserge-sans-paille   Options.FunctionSections = getFunctionSections();
54492bca128Sdiggerlin   Options.IgnoreXCOFFVisibility = getIgnoreXCOFFVisibility();
545997d286fSdiggerlin   Options.XCOFFTracebackTable = getXCOFFTracebackTable();
546ac1d23edSserge-sans-paille   Options.BBSections = getBBSectionsMode(Options);
547ac1d23edSserge-sans-paille   Options.UniqueSectionNames = getUniqueSectionNames();
548e0bca46bSSriraman Tallam   Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
549ac1d23edSserge-sans-paille   Options.TLSSize = getTLSSize();
550ac1d23edSserge-sans-paille   Options.EmulatedTLS = getEmulatedTLS();
551ac1d23edSserge-sans-paille   Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
552ac1d23edSserge-sans-paille   Options.ExceptionModel = getExceptionModel();
553ac1d23edSserge-sans-paille   Options.EmitStackSizeSection = getEnableStackSizeSection();
55494faadacSSnehasish Kumar   Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();
555ac1d23edSserge-sans-paille   Options.EmitAddrsig = getEnableAddrsig();
556ac1d23edSserge-sans-paille   Options.EmitCallSiteInfo = getEmitCallSiteInfo();
557ac1d23edSserge-sans-paille   Options.EnableDebugEntryValues = getEnableDebugEntryValues();
558ac1d23edSserge-sans-paille   Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
5597c7c8e0dSIan Levesque   Options.XRayOmitFunctionIndex = getXRayOmitFunctionIndex();
560a0ca4c46SChen Zheng   Options.DebugStrictDwarf = getDebugStrictDwarf();
561a1944386SFangrui Song   Options.LoopAlignment = getAlignLoops();
562f9270214SYuanfang Chen   Options.JMCInstrument = getJMCInstrument();
563ac1d23edSserge-sans-paille 
564ac1d23edSserge-sans-paille   Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
565ac1d23edSserge-sans-paille 
566ac1d23edSserge-sans-paille   Options.ThreadModel = getThreadModel();
567ac1d23edSserge-sans-paille   Options.EABIVersion = getEABIVersion();
568ac1d23edSserge-sans-paille   Options.DebuggerTuning = getDebuggerTuningOpt();
569a773db7dSDoug Gregor   Options.SwiftAsyncFramePointer = getSwiftAsyncFramePointer();
570ac1d23edSserge-sans-paille   return Options;
571ac1d23edSserge-sans-paille }
572ac1d23edSserge-sans-paille 
getCPUStr()573ac1d23edSserge-sans-paille std::string codegen::getCPUStr() {
574ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, autodetect here. If autodection fails,
575ac1d23edSserge-sans-paille   // this will set the CPU to an empty string which tells the target to
576ac1d23edSserge-sans-paille   // pick a basic default.
577ac1d23edSserge-sans-paille   if (getMCPU() == "native")
578ac1d23edSserge-sans-paille     return std::string(sys::getHostCPUName());
579ac1d23edSserge-sans-paille 
580ac1d23edSserge-sans-paille   return getMCPU();
581ac1d23edSserge-sans-paille }
582ac1d23edSserge-sans-paille 
getFeaturesStr()583ac1d23edSserge-sans-paille std::string codegen::getFeaturesStr() {
584ac1d23edSserge-sans-paille   SubtargetFeatures Features;
585ac1d23edSserge-sans-paille 
586ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, we need to autodetect features.
587ac1d23edSserge-sans-paille   // This is necessary for x86 where the CPU might not support all the
588ac1d23edSserge-sans-paille   // features the autodetected CPU name lists in the target. For example,
589ac1d23edSserge-sans-paille   // not all Sandybridge processors support AVX.
590ac1d23edSserge-sans-paille   if (getMCPU() == "native") {
591ac1d23edSserge-sans-paille     StringMap<bool> HostFeatures;
592ac1d23edSserge-sans-paille     if (sys::getHostCPUFeatures(HostFeatures))
593ac1d23edSserge-sans-paille       for (auto &F : HostFeatures)
594ac1d23edSserge-sans-paille         Features.AddFeature(F.first(), F.second);
595ac1d23edSserge-sans-paille   }
596ac1d23edSserge-sans-paille 
597ac1d23edSserge-sans-paille   for (auto const &MAttr : getMAttrs())
598ac1d23edSserge-sans-paille     Features.AddFeature(MAttr);
599ac1d23edSserge-sans-paille 
600ac1d23edSserge-sans-paille   return Features.getString();
601ac1d23edSserge-sans-paille }
602ac1d23edSserge-sans-paille 
getFeatureList()603ac1d23edSserge-sans-paille std::vector<std::string> codegen::getFeatureList() {
604ac1d23edSserge-sans-paille   SubtargetFeatures Features;
605ac1d23edSserge-sans-paille 
606ac1d23edSserge-sans-paille   // If user asked for the 'native' CPU, we need to autodetect features.
607ac1d23edSserge-sans-paille   // This is necessary for x86 where the CPU might not support all the
608ac1d23edSserge-sans-paille   // features the autodetected CPU name lists in the target. For example,
609ac1d23edSserge-sans-paille   // not all Sandybridge processors support AVX.
610ac1d23edSserge-sans-paille   if (getMCPU() == "native") {
611ac1d23edSserge-sans-paille     StringMap<bool> HostFeatures;
612ac1d23edSserge-sans-paille     if (sys::getHostCPUFeatures(HostFeatures))
613ac1d23edSserge-sans-paille       for (auto &F : HostFeatures)
614ac1d23edSserge-sans-paille         Features.AddFeature(F.first(), F.second);
615ac1d23edSserge-sans-paille   }
616ac1d23edSserge-sans-paille 
617ac1d23edSserge-sans-paille   for (auto const &MAttr : getMAttrs())
618ac1d23edSserge-sans-paille     Features.AddFeature(MAttr);
619ac1d23edSserge-sans-paille 
620ac1d23edSserge-sans-paille   return Features.getFeatures();
621ac1d23edSserge-sans-paille }
622ac1d23edSserge-sans-paille 
renderBoolStringAttr(AttrBuilder & B,StringRef Name,bool Val)623ac1d23edSserge-sans-paille void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
624ac1d23edSserge-sans-paille   B.addAttribute(Name, Val ? "true" : "false");
625ac1d23edSserge-sans-paille }
626ac1d23edSserge-sans-paille 
627ac1d23edSserge-sans-paille #define HANDLE_BOOL_ATTR(CL, AttrName)                                         \
628ac1d23edSserge-sans-paille   do {                                                                         \
629ac1d23edSserge-sans-paille     if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName))            \
630ac1d23edSserge-sans-paille       renderBoolStringAttr(NewAttrs, AttrName, *CL);                           \
631ac1d23edSserge-sans-paille   } while (0)
632ac1d23edSserge-sans-paille 
633ac1d23edSserge-sans-paille /// Set function attributes of function \p F based on CPU, Features, and command
634ac1d23edSserge-sans-paille /// line flags.
setFunctionAttributes(StringRef CPU,StringRef Features,Function & F)635ac1d23edSserge-sans-paille void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
636ac1d23edSserge-sans-paille                                     Function &F) {
637ac1d23edSserge-sans-paille   auto &Ctx = F.getContext();
638ac1d23edSserge-sans-paille   AttributeList Attrs = F.getAttributes();
639d2cc6c2dSSerge Guelton   AttrBuilder NewAttrs(Ctx);
640ac1d23edSserge-sans-paille 
641ac1d23edSserge-sans-paille   if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
642ac1d23edSserge-sans-paille     NewAttrs.addAttribute("target-cpu", CPU);
643ac1d23edSserge-sans-paille   if (!Features.empty()) {
644ac1d23edSserge-sans-paille     // Append the command line features to any that are already on the function.
645ac1d23edSserge-sans-paille     StringRef OldFeatures =
646ac1d23edSserge-sans-paille         F.getFnAttribute("target-features").getValueAsString();
647ac1d23edSserge-sans-paille     if (OldFeatures.empty())
648ac1d23edSserge-sans-paille       NewAttrs.addAttribute("target-features", Features);
649ac1d23edSserge-sans-paille     else {
650ac1d23edSserge-sans-paille       SmallString<256> Appended(OldFeatures);
651ac1d23edSserge-sans-paille       Appended.push_back(',');
652ac1d23edSserge-sans-paille       Appended.append(Features);
653ac1d23edSserge-sans-paille       NewAttrs.addAttribute("target-features", Appended);
654ac1d23edSserge-sans-paille     }
655ac1d23edSserge-sans-paille   }
656ac1d23edSserge-sans-paille   if (FramePointerUsageView->getNumOccurrences() > 0 &&
657ac1d23edSserge-sans-paille       !F.hasFnAttribute("frame-pointer")) {
6582786e673SFangrui Song     if (getFramePointerUsage() == FramePointerKind::All)
659ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "all");
6602786e673SFangrui Song     else if (getFramePointerUsage() == FramePointerKind::NonLeaf)
661ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "non-leaf");
6622786e673SFangrui Song     else if (getFramePointerUsage() == FramePointerKind::None)
663ac1d23edSserge-sans-paille       NewAttrs.addAttribute("frame-pointer", "none");
664ac1d23edSserge-sans-paille   }
665ac1d23edSserge-sans-paille   if (DisableTailCallsView->getNumOccurrences() > 0)
666ac1d23edSserge-sans-paille     NewAttrs.addAttribute("disable-tail-calls",
667ac1d23edSserge-sans-paille                           toStringRef(getDisableTailCalls()));
668ac1d23edSserge-sans-paille   if (getStackRealign())
669ac1d23edSserge-sans-paille     NewAttrs.addAttribute("stackrealign");
670ac1d23edSserge-sans-paille 
671ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math");
672ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math");
673ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
674ac1d23edSserge-sans-paille   HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
675256d2533SMasoud Ataei   HANDLE_BOOL_ATTR(EnableApproxFuncFPMathView, "approx-func-fp-math");
676ac1d23edSserge-sans-paille 
6770ab5b5b8SMatt Arsenault   if (DenormalFPMathView->getNumOccurrences() > 0 &&
6780ab5b5b8SMatt Arsenault       !F.hasFnAttribute("denormal-fp-math")) {
6790ab5b5b8SMatt Arsenault     DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
6800ab5b5b8SMatt Arsenault 
6810ab5b5b8SMatt Arsenault     // FIXME: Command line flag should expose separate input/output modes.
6820ab5b5b8SMatt Arsenault     NewAttrs.addAttribute("denormal-fp-math",
6830ab5b5b8SMatt Arsenault                           DenormalMode(DenormKind, DenormKind).str());
6840ab5b5b8SMatt Arsenault   }
6850ab5b5b8SMatt Arsenault 
686a8cc9047SMatt Arsenault   if (DenormalFP32MathView->getNumOccurrences() > 0 &&
687a8cc9047SMatt Arsenault       !F.hasFnAttribute("denormal-fp-math-f32")) {
688a8cc9047SMatt Arsenault     // FIXME: Command line flag should expose separate input/output modes.
689a8cc9047SMatt Arsenault     DenormalMode::DenormalModeKind DenormKind = getDenormalFP32Math();
690a8cc9047SMatt Arsenault 
691a8cc9047SMatt Arsenault     NewAttrs.addAttribute(
692a8cc9047SMatt Arsenault       "denormal-fp-math-f32",
693a8cc9047SMatt Arsenault       DenormalMode(DenormKind, DenormKind).str());
694a8cc9047SMatt Arsenault   }
695a8cc9047SMatt Arsenault 
696ac1d23edSserge-sans-paille   if (TrapFuncNameView->getNumOccurrences() > 0)
697ac1d23edSserge-sans-paille     for (auto &B : F)
698ac1d23edSserge-sans-paille       for (auto &I : B)
699ac1d23edSserge-sans-paille         if (auto *Call = dyn_cast<CallInst>(&I))
700ac1d23edSserge-sans-paille           if (const auto *F = Call->getCalledFunction())
701ac1d23edSserge-sans-paille             if (F->getIntrinsicID() == Intrinsic::debugtrap ||
702ac1d23edSserge-sans-paille                 F->getIntrinsicID() == Intrinsic::trap)
7033f4d00bcSArthur Eubanks               Call->addFnAttr(
704ac1d23edSserge-sans-paille                   Attribute::get(Ctx, "trap-func-name", getTrapFuncName()));
705ac1d23edSserge-sans-paille 
706ac1d23edSserge-sans-paille   // Let NewAttrs override Attrs.
707de0ae9e8SArthur Eubanks   F.setAttributes(Attrs.addFnAttributes(Ctx, NewAttrs));
708ac1d23edSserge-sans-paille }
709ac1d23edSserge-sans-paille 
710ac1d23edSserge-sans-paille /// Set function attributes of functions in Module M based on CPU,
711ac1d23edSserge-sans-paille /// Features, and command line flags.
setFunctionAttributes(StringRef CPU,StringRef Features,Module & M)712ac1d23edSserge-sans-paille void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
713ac1d23edSserge-sans-paille                                     Module &M) {
714ac1d23edSserge-sans-paille   for (Function &F : M)
715ac1d23edSserge-sans-paille     setFunctionAttributes(CPU, Features, F);
716ac1d23edSserge-sans-paille }
717