1 //===--- X86.cpp - X86 Helpers for Tools ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ToolChains/CommonArgs.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/DriverDiagnostic.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Option/ArgList.h"
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang;
21 using namespace llvm::opt;
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")
29 // FIXME: Reject attempts to use -march=native unless the target matches
32 // FIXME: We should also incorporate the detected target features for use
34 std::string CPU = llvm::sys::getHostCPUName();
35 if (!CPU.empty() && CPU != "generic")
36 return Args.MakeArgString(CPU);
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)
46 .Case("SSE", "pentium3")
47 .Case("SSE2", "pentium4")
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")
64 // Select the default CPU if none was given (or detection failed).
66 if (Triple.getArch() != llvm::Triple::x86_64 &&
67 Triple.getArch() != llvm::Triple::x86)
68 return nullptr; // This routine is only handling x86 targets.
70 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
72 // FIXME: Need target hooks.
73 if (Triple.isOSDarwin()) {
74 if (Triple.getArchName() == "x86_64h")
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))
80 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
81 return Is64Bit ? "core2" : "yonah";
84 // Set up default CPU name for PS4 compilers.
85 if (Triple.isPS4CPU())
88 // On Android use targets compatible with gcc
89 if (Triple.isAndroid())
90 return Is64Bit ? "x86-64" : "i686";
92 // Everything else goes to x86-64 in 64-bit mode.
96 switch (Triple.getOS()) {
97 case llvm::Triple::FreeBSD:
98 case llvm::Triple::NetBSD:
99 case llvm::Triple::OpenBSD:
101 case llvm::Triple::Haiku:
109 void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
111 std::vector<StringRef> &Features) {
112 // If -march=native, autodetect the feature list.
113 if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
114 if (StringRef(A->getValue()) == "native") {
115 llvm::StringMap<bool> HostFeatures;
116 if (llvm::sys::getHostCPUFeatures(HostFeatures))
117 for (auto &F : HostFeatures)
119 Args.MakeArgString((F.second ? "+" : "-") + F.first()));
123 if (Triple.getArchName() == "x86_64h") {
124 // x86_64h implies quite a few of the more modern subtarget features
125 // for Haswell class CPUs, but not all of them. Opt-out of a few.
126 Features.push_back("-rdrnd");
127 Features.push_back("-aes");
128 Features.push_back("-pclmul");
129 Features.push_back("-rtm");
130 Features.push_back("-fsgsbase");
133 const llvm::Triple::ArchType ArchType = Triple.getArch();
134 // Add features to be compatible with gcc for Android.
135 if (Triple.isAndroid()) {
136 if (ArchType == llvm::Triple::x86_64) {
137 Features.push_back("+sse4.2");
138 Features.push_back("+popcnt");
140 Features.push_back("+ssse3");
143 // Translate the high level `-mretpoline` flag to the specific target feature
144 // flags. We also detect if the user asked for retpoline external thunks but
145 // failed to ask for retpolines themselves (through any of the different
146 // flags). This is a bit hacky but keeps existing usages working. We should
147 // consider deprecating this and instead warn if the user requests external
148 // retpoline thunks and *doesn't* request some form of retpolines.
149 if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
150 options::OPT_mspeculative_load_hardening,
151 options::OPT_mno_speculative_load_hardening)) {
152 if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
154 Features.push_back("+retpoline-indirect-calls");
155 Features.push_back("+retpoline-indirect-branches");
156 } else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
157 options::OPT_mno_speculative_load_hardening,
159 // On x86, speculative load hardening relies on at least using retpolines
160 // for indirect calls.
161 Features.push_back("+retpoline-indirect-calls");
163 } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
164 options::OPT_mno_retpoline_external_thunk, false)) {
165 // FIXME: Add a warning about failing to specify `-mretpoline` and
166 // eventually switch to an error here.
167 Features.push_back("+retpoline-indirect-calls");
168 Features.push_back("+retpoline-indirect-branches");
171 // Now add any that the user explicitly requested on the command line,
172 // which may override the defaults.
173 handleTargetFeaturesGroup(Args, Features, options::OPT_m_x86_Features_Group);