clang  7.0.0svn
AArch64.cpp
Go to the documentation of this file.
1 //===--- AArch64.cpp - AArch64 (not ARM) 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 "AArch64.h"
11 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/TargetParser.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
23 /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
24 /// provided, or to nullptr otherwise.
25 std::string aarch64::getAArch64TargetCPU(const ArgList &Args, Arg *&A) {
26  std::string CPU;
27  // If we have -mcpu, use that.
28  if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
29  StringRef Mcpu = A->getValue();
30  CPU = Mcpu.split("+").first.lower();
31  }
32 
33  // Handle CPU name is 'native'.
34  if (CPU == "native")
35  return llvm::sys::getHostCPUName();
36  else if (CPU.size())
37  return CPU;
38 
39  // Make sure we pick "cyclone" if -arch is used.
40  // FIXME: Should this be picked by checking the target triple instead?
41  if (Args.getLastArg(options::OPT_arch))
42  return "cyclone";
43 
44  return "generic";
45 }
46 
47 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
48 static bool DecodeAArch64Features(const Driver &D, StringRef text,
49  std::vector<StringRef> &Features) {
51  text.split(Split, StringRef("+"), -1, false);
52 
53  for (StringRef Feature : Split) {
54  StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
55  if (!FeatureName.empty())
56  Features.push_back(FeatureName);
57  else if (Feature == "neon" || Feature == "noneon")
58  D.Diag(clang::diag::err_drv_no_neon_modifier);
59  else
60  return false;
61  }
62  return true;
63 }
64 
65 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
66 // decode CPU and feature.
67 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
68  std::vector<StringRef> &Features) {
69  std::pair<StringRef, StringRef> Split = Mcpu.split("+");
70  CPU = Split.first;
71 
72  if (CPU == "native")
73  CPU = llvm::sys::getHostCPUName();
74 
75  if (CPU == "generic") {
76  Features.push_back("+neon");
77  } else {
78  llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU);
79  if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
80  return false;
81 
82  unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
83  if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
84  return false;
85  }
86 
87  if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
88  return false;
89 
90  return true;
91 }
92 
93 static bool
94 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
95  const ArgList &Args,
96  std::vector<StringRef> &Features) {
97  std::string MarchLowerCase = March.lower();
98  std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
99 
100  llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
101  if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
102  !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
103  (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features)))
104  return false;
105 
106  return true;
107 }
108 
109 static bool
110 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
111  const ArgList &Args,
112  std::vector<StringRef> &Features) {
113  StringRef CPU;
114  std::string McpuLowerCase = Mcpu.lower();
115  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
116  return false;
117 
118  return true;
119 }
120 
121 static bool
123  const ArgList &Args,
124  std::vector<StringRef> &Features) {
125  std::string MtuneLowerCase = Mtune.lower();
126  // Check CPU name is valid
127  std::vector<StringRef> MtuneFeatures;
128  StringRef Tune;
129  if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
130  return false;
131 
132  // Handle CPU name is 'native'.
133  if (MtuneLowerCase == "native")
134  MtuneLowerCase = llvm::sys::getHostCPUName();
135  if (MtuneLowerCase == "cyclone") {
136  Features.push_back("+zcm");
137  Features.push_back("+zcz");
138  }
139  return true;
140 }
141 
142 static bool
144  const ArgList &Args,
145  std::vector<StringRef> &Features) {
146  StringRef CPU;
147  std::vector<StringRef> DecodedFeature;
148  std::string McpuLowerCase = Mcpu.lower();
149  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
150  return false;
151 
152  return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
153 }
154 
155 void aarch64::getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
156  std::vector<StringRef> &Features) {
157  Arg *A;
158  bool success = true;
159  // Enable NEON by default.
160  Features.push_back("+neon");
161  if ((A = Args.getLastArg(options::OPT_march_EQ)))
162  success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
163  else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
164  success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
165  else if (Args.hasArg(options::OPT_arch))
167  Args, Features);
168 
169  if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
170  success =
171  getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
172  else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
173  success =
174  getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
175  else if (success && Args.hasArg(options::OPT_arch))
177  D, getAArch64TargetCPU(Args, A), Args, Features);
178 
179  if (!success)
180  D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
181 
182  if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
183  Features.push_back("-fp-armv8");
184  Features.push_back("-crypto");
185  Features.push_back("-neon");
186  }
187 
188  // En/disable crc
189  if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
190  if (A->getOption().matches(options::OPT_mcrc))
191  Features.push_back("+crc");
192  else
193  Features.push_back("-crc");
194  }
195 
196  if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
197  options::OPT_munaligned_access))
198  if (A->getOption().matches(options::OPT_mno_unaligned_access))
199  Features.push_back("+strict-align");
200 
201  if (Args.hasArg(options::OPT_ffixed_x18))
202  Features.push_back("+reserve-x18");
203 
204  if (Args.hasArg(options::OPT_ffixed_x20))
205  Features.push_back("+reserve-x20");
206 
207  if (Args.hasArg(options::OPT_mno_neg_immediates))
208  Features.push_back("+no-neg-immediates");
209 }
static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU, std::vector< StringRef > &Features)
Definition: AArch64.cpp:67
static bool getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:143
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:110
static void getExtensionFeatures(const Driver &D, const ArgList &Args, std::vector< StringRef > &Features, StringRef &MArch, StringRef &Exts)
Definition: RISCV.cpp:102
static bool DecodeAArch64Features(const Driver &D, StringRef text, std::vector< StringRef > &Features)
Definition: AArch64.cpp:48
std::string getAArch64TargetCPU(const llvm::opt::ArgList &Args, llvm::opt::Arg *&A)
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
static bool getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:110
static bool getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:122
void getAArch64TargetFeatures(const Driver &D, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
Dataflow Directional Tag Classes.
static bool getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:94