CommandFlags.cpp 29 KB

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