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.isX86()) 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 case llvm::Triple::NetBSD: 98 return "i486"; 99 case llvm::Triple::Haiku: 100 case llvm::Triple::OpenBSD: 101 return "i586"; 102 default: 103 // Fallback to p4. 104 return "pentium4"; 105 } 106 } 107 108 void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple, 109 const ArgList &Args, 110 std::vector<StringRef> &Features) { 111 // If -march=native, autodetect the feature list. 112 if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) { 113 if (StringRef(A->getValue()) == "native") { 114 llvm::StringMap<bool> HostFeatures; 115 if (llvm::sys::getHostCPUFeatures(HostFeatures)) 116 for (auto &F : HostFeatures) 117 Features.push_back( 118 Args.MakeArgString((F.second ? "+" : "-") + F.first())); 119 } 120 } 121 122 if (Triple.getArchName() == "x86_64h") { 123 // x86_64h implies quite a few of the more modern subtarget features 124 // for Haswell class CPUs, but not all of them. Opt-out of a few. 125 Features.push_back("-rdrnd"); 126 Features.push_back("-aes"); 127 Features.push_back("-pclmul"); 128 Features.push_back("-rtm"); 129 Features.push_back("-fsgsbase"); 130 } 131 132 const llvm::Triple::ArchType ArchType = Triple.getArch(); 133 // Add features to be compatible with gcc for Android. 134 if (Triple.isAndroid()) { 135 if (ArchType == llvm::Triple::x86_64) { 136 Features.push_back("+sse4.2"); 137 Features.push_back("+popcnt"); 138 Features.push_back("+cx16"); 139 } else 140 Features.push_back("+ssse3"); 141 } 142 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 auto SpectreOpt = clang::driver::options::ID::OPT_INVALID; 150 if (Triple.isOSOpenBSD() && Triple.getArch() == llvm::Triple::x86_64 && 151 Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline, true)) { 152 Features.push_back("+retpoline-indirect-calls"); 153 Features.push_back("+retpoline-indirect-branches"); 154 SpectreOpt = options::OPT_mretpoline; 155 } else 156 if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline, 157 options::OPT_mspeculative_load_hardening, 158 options::OPT_mno_speculative_load_hardening)) { 159 if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline, 160 false)) { 161 Features.push_back("+retpoline-indirect-calls"); 162 Features.push_back("+retpoline-indirect-branches"); 163 SpectreOpt = options::OPT_mretpoline; 164 } else if (Args.hasFlag(options::OPT_mspeculative_load_hardening, 165 options::OPT_mno_speculative_load_hardening, 166 false)) { 167 // On x86, speculative load hardening relies on at least using retpolines 168 // for indirect calls. 169 Features.push_back("+retpoline-indirect-calls"); 170 SpectreOpt = options::OPT_mspeculative_load_hardening; 171 } 172 } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk, 173 options::OPT_mno_retpoline_external_thunk, false)) { 174 // FIXME: Add a warning about failing to specify `-mretpoline` and 175 // eventually switch to an error here. 176 Features.push_back("+retpoline-indirect-calls"); 177 Features.push_back("+retpoline-indirect-branches"); 178 SpectreOpt = options::OPT_mretpoline_external_thunk; 179 } 180 181 auto LVIOpt = clang::driver::options::ID::OPT_INVALID; 182 if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening, 183 false)) { 184 Features.push_back("+lvi-load-hardening"); 185 Features.push_back("+lvi-cfi"); // load hardening implies CFI protection 186 LVIOpt = options::OPT_mlvi_hardening; 187 } else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi, 188 false)) { 189 Features.push_back("+lvi-cfi"); 190 LVIOpt = options::OPT_mlvi_cfi; 191 } 192 193 if (SpectreOpt != clang::driver::options::ID::OPT_INVALID && 194 LVIOpt != clang::driver::options::ID::OPT_INVALID) { 195 D.Diag(diag::err_drv_argument_not_allowed_with) 196 << D.getOpts().getOptionName(SpectreOpt) 197 << D.getOpts().getOptionName(LVIOpt); 198 } 199 200 // Now add any that the user explicitly requested on the command line, 201 // which may override the defaults. 202 handleTargetFeaturesGroup(Args, Features, options::OPT_m_x86_Features_Group); 203 } 204