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