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") {
27 if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
33 // FIXME: Reject attempts to use -march=native unless the target matches
36 // FIXME: We should also incorporate the detected target features for use
38 std::string CPU = llvm::sys::getHostCPUName();
39 if (!CPU.empty() && CPU != "generic")
40 return Args.MakeArgString(CPU);
43 if (const Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
44 // Mapping built by referring to X86TargetInfo::getDefaultFeatures().
45 StringRef Arch = A->getValue();
47 if (Triple.getArch() == llvm::Triple::x86) {
48 CPU = llvm::StringSwitch<const char *>(Arch)
50 .Case("SSE", "pentium3")
51 .Case("SSE2", "pentium4")
52 .Case("AVX", "sandybridge")
53 .Case("AVX2", "haswell")
56 CPU = llvm::StringSwitch<const char *>(Arch)
57 .Case("AVX", "sandybridge")
58 .Case("AVX2", "haswell")
65 // Select the default CPU if none was given (or detection failed).
67 if (Triple.getArch() != llvm::Triple::x86_64 &&
68 Triple.getArch() != llvm::Triple::x86)
69 return nullptr; // This routine is only handling x86 targets.
71 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
73 // FIXME: Need target hooks.
74 if (Triple.isOSDarwin()) {
75 if (Triple.getArchName() == "x86_64h")
77 // macosx10.12 drops support for all pre-Penryn Macs.
78 // Simulators can still run on 10.11 though, like Xcode.
79 if (Triple.isMacOSX() && !Triple.isOSVersionLT(10, 12))
81 // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
82 return Is64Bit ? "core2" : "yonah";
85 // Set up default CPU name for PS4 compilers.
86 if (Triple.isPS4CPU())
89 // On Android use targets compatible with gcc
90 if (Triple.isAndroid())
91 return Is64Bit ? "x86-64" : "i686";
93 // Everything else goes to x86-64 in 64-bit mode.
97 switch (Triple.getOS()) {
98 case llvm::Triple::FreeBSD:
99 case llvm::Triple::NetBSD:
100 case llvm::Triple::OpenBSD:
102 case llvm::Triple::Haiku:
110 void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
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)
120 Args.MakeArgString((F.second ? "+" : "-") + F.first()));
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");
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");
141 Features.push_back("+ssse3");
144 // Set features according to the -arch flag on MSVC.
145 if (Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
146 StringRef Arch = A->getValue();
147 bool ArchUsed = false;
148 // First, look for flags that are shared in x86 and x86-64.
149 if (ArchType == llvm::Triple::x86_64 || ArchType == llvm::Triple::x86) {
150 if (Arch == "AVX" || Arch == "AVX2") {
152 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
155 // Then, look for x86-specific flags.
156 if (ArchType == llvm::Triple::x86) {
157 if (Arch == "IA32") {
159 } else if (Arch == "SSE" || Arch == "SSE2") {
161 Features.push_back(Args.MakeArgString("+" + Arch.lower()));
165 D.Diag(clang::diag::warn_drv_unused_argument) << A->getAsString(Args);
168 // Now add any that the user explicitly requested on the command line,
169 // which may override the defaults.
170 handleTargetFeaturesGroup(Args, Features, options::OPT_m_x86_Features_Group);