X86.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //===--- X86.cpp - X86 Helpers for Tools ------------------------*- 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. #include "X86.h"
  9. #include "ToolChains/CommonArgs.h"
  10. #include "clang/Driver/Driver.h"
  11. #include "clang/Driver/DriverDiagnostic.h"
  12. #include "clang/Driver/Options.h"
  13. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "llvm/Option/ArgList.h"
  17. #include "llvm/Support/Host.h"
  18. using namespace clang::driver;
  19. using namespace clang::driver::tools;
  20. using namespace clang;
  21. using namespace llvm::opt;
  22. std::string x86::getX86TargetCPU(const Driver &D, const ArgList &Args,
  23. const llvm::Triple &Triple) {
  24. if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
  25. StringRef CPU = A->getValue();
  26. if (CPU != "native")
  27. return std::string(CPU);
  28. // FIXME: Reject attempts to use -march=native unless the target matches
  29. // the host.
  30. //
  31. // FIXME: We should also incorporate the detected target features for use
  32. // with -native.
  33. CPU = llvm::sys::getHostCPUName();
  34. if (!CPU.empty() && CPU != "generic")
  35. return std::string(CPU);
  36. }
  37. if (const Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
  38. // Mapping built by looking at lib/Basic's X86TargetInfo::initFeatureMap().
  39. // The keys are case-sensitive; this matches link.exe.
  40. // 32-bit and 64-bit /arch: flags.
  41. llvm::StringMap<StringRef> ArchMap({
  42. {"AVX", "sandybridge"},
  43. {"AVX2", "haswell"},
  44. {"AVX512F", "knl"},
  45. {"AVX512", "skylake-avx512"},
  46. });
  47. if (Triple.getArch() == llvm::Triple::x86) {
  48. // 32-bit-only /arch: flags.
  49. ArchMap.insert({
  50. {"IA32", "i386"},
  51. {"SSE", "pentium3"},
  52. {"SSE2", "pentium4"},
  53. });
  54. }
  55. StringRef CPU = ArchMap.lookup(A->getValue());
  56. if (CPU.empty()) {
  57. std::vector<StringRef> ValidArchs{ArchMap.keys().begin(),
  58. ArchMap.keys().end()};
  59. sort(ValidArchs);
  60. D.Diag(diag::warn_drv_invalid_arch_name_with_suggestion)
  61. << A->getValue() << (Triple.getArch() == llvm::Triple::x86)
  62. << join(ValidArchs, ", ");
  63. }
  64. return std::string(CPU);
  65. }
  66. // Select the default CPU if none was given (or detection failed).
  67. if (!Triple.isX86())
  68. return ""; // This routine is only handling x86 targets.
  69. bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
  70. // FIXME: Need target hooks.
  71. if (Triple.isOSDarwin()) {
  72. if (Triple.getArchName() == "x86_64h")
  73. return "core-avx2";
  74. // macosx10.12 drops support for all pre-Penryn Macs.
  75. // Simulators can still run on 10.11 though, like Xcode.
  76. if (Triple.isMacOSX() && !Triple.isOSVersionLT(10, 12))
  77. return "penryn";
  78. // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
  79. return Is64Bit ? "core2" : "yonah";
  80. }
  81. // Set up default CPU name for PS4 compilers.
  82. if (Triple.isPS4CPU())
  83. return "btver2";
  84. // On Android use targets compatible with gcc
  85. if (Triple.isAndroid())
  86. return Is64Bit ? "x86-64" : "i686";
  87. // Everything else goes to x86-64 in 64-bit mode.
  88. if (Is64Bit)
  89. return "x86-64";
  90. switch (Triple.getOS()) {
  91. case llvm::Triple::NetBSD:
  92. return "i486";
  93. case llvm::Triple::Haiku:
  94. case llvm::Triple::OpenBSD:
  95. return "i586";
  96. case llvm::Triple::FreeBSD:
  97. return "i686";
  98. default:
  99. // Fallback to p4.
  100. return "pentium4";
  101. }
  102. }
  103. void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
  104. const ArgList &Args,
  105. std::vector<StringRef> &Features) {
  106. // If -march=native, autodetect the feature list.
  107. if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
  108. if (StringRef(A->getValue()) == "native") {
  109. llvm::StringMap<bool> HostFeatures;
  110. if (llvm::sys::getHostCPUFeatures(HostFeatures))
  111. for (auto &F : HostFeatures)
  112. Features.push_back(
  113. Args.MakeArgString((F.second ? "+" : "-") + F.first()));
  114. }
  115. }
  116. if (Triple.getArchName() == "x86_64h") {
  117. // x86_64h implies quite a few of the more modern subtarget features
  118. // for Haswell class CPUs, but not all of them. Opt-out of a few.
  119. Features.push_back("-rdrnd");
  120. Features.push_back("-aes");
  121. Features.push_back("-pclmul");
  122. Features.push_back("-rtm");
  123. Features.push_back("-fsgsbase");
  124. }
  125. const llvm::Triple::ArchType ArchType = Triple.getArch();
  126. // Add features to be compatible with gcc for Android.
  127. if (Triple.isAndroid()) {
  128. if (ArchType == llvm::Triple::x86_64) {
  129. Features.push_back("+sse4.2");
  130. Features.push_back("+popcnt");
  131. Features.push_back("+cx16");
  132. } else
  133. Features.push_back("+ssse3");
  134. }
  135. // Translate the high level `-mretpoline` flag to the specific target feature
  136. // flags. We also detect if the user asked for retpoline external thunks but
  137. // failed to ask for retpolines themselves (through any of the different
  138. // flags). This is a bit hacky but keeps existing usages working. We should
  139. // consider deprecating this and instead warn if the user requests external
  140. // retpoline thunks and *doesn't* request some form of retpolines.
  141. auto SpectreOpt = clang::driver::options::ID::OPT_INVALID;
  142. if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
  143. options::OPT_mspeculative_load_hardening,
  144. options::OPT_mno_speculative_load_hardening)) {
  145. if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
  146. false)) {
  147. Features.push_back("+retpoline-indirect-calls");
  148. Features.push_back("+retpoline-indirect-branches");
  149. SpectreOpt = options::OPT_mretpoline;
  150. } else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
  151. options::OPT_mno_speculative_load_hardening,
  152. false)) {
  153. // On x86, speculative load hardening relies on at least using retpolines
  154. // for indirect calls.
  155. Features.push_back("+retpoline-indirect-calls");
  156. SpectreOpt = options::OPT_mspeculative_load_hardening;
  157. }
  158. } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
  159. options::OPT_mno_retpoline_external_thunk, false)) {
  160. // FIXME: Add a warning about failing to specify `-mretpoline` and
  161. // eventually switch to an error here.
  162. Features.push_back("+retpoline-indirect-calls");
  163. Features.push_back("+retpoline-indirect-branches");
  164. SpectreOpt = options::OPT_mretpoline_external_thunk;
  165. }
  166. auto LVIOpt = clang::driver::options::ID::OPT_INVALID;
  167. if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening,
  168. false)) {
  169. Features.push_back("+lvi-load-hardening");
  170. Features.push_back("+lvi-cfi"); // load hardening implies CFI protection
  171. LVIOpt = options::OPT_mlvi_hardening;
  172. } else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi,
  173. false)) {
  174. Features.push_back("+lvi-cfi");
  175. LVIOpt = options::OPT_mlvi_cfi;
  176. }
  177. if (Args.hasFlag(options::OPT_m_seses, options::OPT_mno_seses, false)) {
  178. if (LVIOpt == options::OPT_mlvi_hardening)
  179. D.Diag(diag::err_drv_argument_not_allowed_with)
  180. << D.getOpts().getOptionName(options::OPT_mlvi_hardening)
  181. << D.getOpts().getOptionName(options::OPT_m_seses);
  182. if (SpectreOpt != clang::driver::options::ID::OPT_INVALID)
  183. D.Diag(diag::err_drv_argument_not_allowed_with)
  184. << D.getOpts().getOptionName(SpectreOpt)
  185. << D.getOpts().getOptionName(options::OPT_m_seses);
  186. Features.push_back("+seses");
  187. if (!Args.hasArg(options::OPT_mno_lvi_cfi)) {
  188. Features.push_back("+lvi-cfi");
  189. LVIOpt = options::OPT_mlvi_cfi;
  190. }
  191. }
  192. if (SpectreOpt != clang::driver::options::ID::OPT_INVALID &&
  193. LVIOpt != clang::driver::options::ID::OPT_INVALID) {
  194. D.Diag(diag::err_drv_argument_not_allowed_with)
  195. << D.getOpts().getOptionName(SpectreOpt)
  196. << D.getOpts().getOptionName(LVIOpt);
  197. }
  198. // Now add any that the user explicitly requested on the command line,
  199. // which may override the defaults.
  200. for (const Arg *A : Args.filtered(options::OPT_m_x86_Features_Group,
  201. options::OPT_mgeneral_regs_only)) {
  202. StringRef Name = A->getOption().getName();
  203. A->claim();
  204. // Skip over "-m".
  205. assert(Name.startswith("m") && "Invalid feature name.");
  206. Name = Name.substr(1);
  207. // Replace -mgeneral-regs-only with -x87, -mmx, -sse
  208. if (A->getOption().getID() == options::OPT_mgeneral_regs_only) {
  209. Features.insert(Features.end(), {"-x87", "-mmx", "-sse"});
  210. continue;
  211. }
  212. bool IsNegative = Name.startswith("no-");
  213. if (IsNegative)
  214. Name = Name.substr(3);
  215. Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
  216. }
  217. }