CommandFlags.cpp 27 KB

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