1 //===--- AArch64.cpp - AArch64 (not ARM) 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 "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/TargetParser.h"
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
22 /// \returns true if the given triple can determine the default CPU type even
23 /// if -arch is not specified.
24 static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) {
25 return Triple.isOSDarwin();
28 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
29 /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
30 /// provided, or to nullptr otherwise.
31 std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
32 const llvm::Triple &Triple, Arg *&A) {
34 // If we have -mcpu, use that.
35 if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
36 StringRef Mcpu = A->getValue();
37 CPU = Mcpu.split("+").first.lower();
40 // Handle CPU name is 'native'.
42 return llvm::sys::getHostCPUName();
46 // Make sure we pick "cyclone" if -arch is used or when targetting a Darwin
48 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin())
54 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
55 static bool DecodeAArch64Features(const Driver &D, StringRef text,
56 std::vector<StringRef> &Features) {
57 SmallVector<StringRef, 8> Split;
58 text.split(Split, StringRef("+"), -1, false);
60 for (StringRef Feature : Split) {
61 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
62 if (!FeatureName.empty())
63 Features.push_back(FeatureName);
64 else if (Feature == "neon" || Feature == "noneon")
65 D.Diag(clang::diag::err_drv_no_neon_modifier);
72 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
73 // decode CPU and feature.
74 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
75 std::vector<StringRef> &Features) {
76 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
80 CPU = llvm::sys::getHostCPUName();
82 if (CPU == "generic") {
83 Features.push_back("+neon");
85 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU);
86 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
89 unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
90 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
94 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
101 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
103 std::vector<StringRef> &Features) {
104 std::string MarchLowerCase = March.lower();
105 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
107 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
108 if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
109 !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
110 (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features)))
117 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
119 std::vector<StringRef> &Features) {
121 std::string McpuLowerCase = Mcpu.lower();
122 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
129 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
131 std::vector<StringRef> &Features) {
132 std::string MtuneLowerCase = Mtune.lower();
133 // Check CPU name is valid
134 std::vector<StringRef> MtuneFeatures;
136 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
139 // Handle CPU name is 'native'.
140 if (MtuneLowerCase == "native")
141 MtuneLowerCase = llvm::sys::getHostCPUName();
142 if (MtuneLowerCase == "cyclone") {
143 Features.push_back("+zcm");
144 Features.push_back("+zcz");
150 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
152 std::vector<StringRef> &Features) {
154 std::vector<StringRef> DecodedFeature;
155 std::string McpuLowerCase = Mcpu.lower();
156 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
159 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
162 void aarch64::getAArch64TargetFeatures(const Driver &D,
163 const llvm::Triple &Triple,
165 std::vector<StringRef> &Features) {
168 // Enable NEON by default.
169 Features.push_back("+neon");
170 if ((A = Args.getLastArg(options::OPT_march_EQ)))
171 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
172 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
173 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
174 else if (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple))
175 success = getAArch64ArchFeaturesFromMcpu(
176 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
178 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
180 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
181 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
183 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
185 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
186 success = getAArch64MicroArchFeaturesFromMcpu(
187 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
190 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
192 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
193 Features.push_back("-fp-armv8");
194 Features.push_back("-crypto");
195 Features.push_back("-neon");
199 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
200 if (A->getOption().matches(options::OPT_mcrc))
201 Features.push_back("+crc");
203 Features.push_back("-crc");
206 // Handle (arch-dependent) fp16fml/fullfp16 relationship.
207 // FIXME: this fp16fml option handling will be reimplemented after the
208 // TargetParser rewrite.
209 const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
210 const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
211 if (std::find(Features.begin(), Features.end(), "+v8.4a") != Features.end()) {
212 const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
213 if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
214 // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
215 // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
216 if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
217 Features.push_back("+fp16fml");
220 goto fp16_fml_fallthrough;
223 fp16_fml_fallthrough:
224 // In both of these cases, putting the 'other' feature on the end of the vector will
225 // result in the same effect as placing it immediately after the current feature.
226 if (ItRNoFullFP16 < ItRFP16FML)
227 Features.push_back("-fp16fml");
228 else if (ItRNoFullFP16 > ItRFP16FML)
229 Features.push_back("+fullfp16");
232 // FIXME: this needs reimplementation too after the TargetParser rewrite
234 // Context sensitive meaning of Crypto:
235 // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes
236 // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes
237 const auto ItBegin = Features.begin();
238 const auto ItEnd = Features.end();
239 const auto ItRBegin = Features.rbegin();
240 const auto ItREnd = Features.rend();
241 const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
242 const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
243 const auto HasCrypto = ItRCrypto != ItREnd;
244 const auto HasNoCrypto = ItRNoCrypto != ItREnd;
245 const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
246 const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
248 bool NoCrypto = false;
249 if (HasCrypto && HasNoCrypto) {
250 if (PosNoCrypto < PosCrypto)
254 if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
255 if (HasCrypto && !NoCrypto) {
256 // Check if we have NOT disabled an algorithm with something like:
257 // +crypto, -algorithm
258 // And if "-algorithm" does not occur, we enable that crypto algorithm.
259 const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
260 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
261 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
262 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
264 Features.push_back("+sm4");
266 Features.push_back("+sha3");
268 Features.push_back("+sha2");
270 Features.push_back("+aes");
271 } else if (HasNoCrypto) {
272 // Check if we have NOT enabled a crypto algorithm with something like:
273 // -crypto, +algorithm
274 // And if "+algorithm" does not occur, we disable that crypto algorithm.
275 const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
276 const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
277 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
278 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
280 Features.push_back("-sm4");
282 Features.push_back("-sha3");
284 Features.push_back("-sha2");
286 Features.push_back("-aes");
289 if (HasCrypto && !NoCrypto) {
290 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
291 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
293 Features.push_back("+sha2");
295 Features.push_back("+aes");
296 } else if (HasNoCrypto) {
297 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
298 const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
299 const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
300 const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
301 const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
303 Features.push_back("-sha2");
305 Features.push_back("-aes");
306 if (HasV82a || HasV83a || HasV84a) {
307 Features.push_back("-sm4");
308 Features.push_back("-sha3");
313 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
314 options::OPT_munaligned_access))
315 if (A->getOption().matches(options::OPT_mno_unaligned_access))
316 Features.push_back("+strict-align");
318 if (Args.hasArg(options::OPT_ffixed_x1))
319 Features.push_back("+reserve-x1");
321 if (Args.hasArg(options::OPT_ffixed_x2))
322 Features.push_back("+reserve-x2");
324 if (Args.hasArg(options::OPT_ffixed_x3))
325 Features.push_back("+reserve-x3");
327 if (Args.hasArg(options::OPT_ffixed_x4))
328 Features.push_back("+reserve-x4");
330 if (Args.hasArg(options::OPT_ffixed_x5))
331 Features.push_back("+reserve-x5");
333 if (Args.hasArg(options::OPT_ffixed_x6))
334 Features.push_back("+reserve-x6");
336 if (Args.hasArg(options::OPT_ffixed_x7))
337 Features.push_back("+reserve-x7");
339 if (Args.hasArg(options::OPT_ffixed_x18))
340 Features.push_back("+reserve-x18");
342 if (Args.hasArg(options::OPT_ffixed_x20))
343 Features.push_back("+reserve-x20");
345 if (Args.hasArg(options::OPT_fcall_saved_x8))
346 Features.push_back("+call-saved-x8");
348 if (Args.hasArg(options::OPT_fcall_saved_x9))
349 Features.push_back("+call-saved-x9");
351 if (Args.hasArg(options::OPT_fcall_saved_x10))
352 Features.push_back("+call-saved-x10");
354 if (Args.hasArg(options::OPT_fcall_saved_x11))
355 Features.push_back("+call-saved-x11");
357 if (Args.hasArg(options::OPT_fcall_saved_x12))
358 Features.push_back("+call-saved-x12");
360 if (Args.hasArg(options::OPT_fcall_saved_x13))
361 Features.push_back("+call-saved-x13");
363 if (Args.hasArg(options::OPT_fcall_saved_x14))
364 Features.push_back("+call-saved-x14");
366 if (Args.hasArg(options::OPT_fcall_saved_x15))
367 Features.push_back("+call-saved-x15");
369 if (Args.hasArg(options::OPT_fcall_saved_x18))
370 Features.push_back("+call-saved-x18");
372 if (Args.hasArg(options::OPT_mno_neg_immediates))
373 Features.push_back("+no-neg-immediates");