clang  6.0.0svn
X86.cpp
Go to the documentation of this file.
1 //===--- X86.cpp - X86 Helpers for Tools ------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "X86.h"
11 #include "ToolChains/CommonArgs.h"
12 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Option/ArgList.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  if (Triple.isOSDarwin() && Triple.getArchName() == "x86_64h")
28  return "core-avx2";
29 
30  return A->getValue();
31  }
32 
33  // FIXME: Reject attempts to use -march=native unless the target matches
34  // the host.
35  //
36  // FIXME: We should also incorporate the detected target features for use
37  // with -native.
38  std::string CPU = llvm::sys::getHostCPUName();
39  if (!CPU.empty() && CPU != "generic")
40  return Args.MakeArgString(CPU);
41  }
42 
43  if (const Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
44  // Mapping built by referring to X86TargetInfo::getDefaultFeatures().
45  StringRef Arch = A->getValue();
46  const char *CPU;
47  if (Triple.getArch() == llvm::Triple::x86) {
48  CPU = llvm::StringSwitch<const char *>(Arch)
49  .Case("IA32", "i386")
50  .Case("SSE", "pentium3")
51  .Case("SSE2", "pentium4")
52  .Case("AVX", "sandybridge")
53  .Case("AVX2", "haswell")
54  .Default(nullptr);
55  } else {
56  CPU = llvm::StringSwitch<const char *>(Arch)
57  .Case("AVX", "sandybridge")
58  .Case("AVX2", "haswell")
59  .Default(nullptr);
60  }
61  if (CPU)
62  return CPU;
63  }
64 
65  // Select the default CPU if none was given (or detection failed).
66 
67  if (Triple.getArch() != llvm::Triple::x86_64 &&
68  Triple.getArch() != llvm::Triple::x86)
69  return nullptr; // This routine is only handling x86 targets.
70 
71  bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
72 
73  // FIXME: Need target hooks.
74  if (Triple.isOSDarwin()) {
75  if (Triple.getArchName() == "x86_64h")
76  return "core-avx2";
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))
80  return "penryn";
81  // The oldest x86_64 Macs have core2/Merom; the oldest x86 Macs have Yonah.
82  return Is64Bit ? "core2" : "yonah";
83  }
84 
85  // Set up default CPU name for PS4 compilers.
86  if (Triple.isPS4CPU())
87  return "btver2";
88 
89  // On Android use targets compatible with gcc
90  if (Triple.isAndroid())
91  return Is64Bit ? "x86-64" : "i686";
92 
93  // Everything else goes to x86-64 in 64-bit mode.
94  if (Is64Bit)
95  return "x86-64";
96 
97  switch (Triple.getOS()) {
98  case llvm::Triple::FreeBSD:
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  } else
141  Features.push_back("+ssse3");
142  }
143 
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") {
151  ArchUsed = true;
152  Features.push_back(Args.MakeArgString("+" + Arch.lower()));
153  }
154  }
155  // Then, look for x86-specific flags.
156  if (ArchType == llvm::Triple::x86) {
157  if (Arch == "IA32") {
158  ArchUsed = true;
159  } else if (Arch == "SSE" || Arch == "SSE2") {
160  ArchUsed = true;
161  Features.push_back(Args.MakeArgString("+" + Arch.lower()));
162  }
163  }
164  if (!ArchUsed)
165  D.Diag(clang::diag::warn_drv_unused_argument) << A->getAsString(Args);
166  }
167 
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);
171 }
void handleTargetFeaturesGroup(const llvm::opt::ArgList &Args, std::vector< StringRef > &Features, llvm::opt::OptSpecifier Group)
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const char * getX86TargetCPU(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
Dataflow Directional Tag Classes.
void getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)