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