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 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
23 /// targeting. Set \p A to the Arg corresponding to the -mcpu or -mtune
24 /// arguments if they are provided, or to nullptr otherwise.
25 std::string aarch64::getAArch64TargetCPU(const ArgList &Args, Arg *&A) {
27 // If we have -mtune or -mcpu, use that.
28 if ((A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ))) {
29 CPU = StringRef(A->getValue()).lower();
30 } else if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
31 StringRef Mcpu = A->getValue();
32 CPU = Mcpu.split("+").first.lower();
35 // Handle CPU name is 'native'.
37 return llvm::sys::getHostCPUName();
41 // Make sure we pick "cyclone" if -arch is used.
42 // FIXME: Should this be picked by checking the target triple instead?
43 if (Args.getLastArg(options::OPT_arch))
49 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
50 static bool DecodeAArch64Features(const Driver &D, StringRef text,
51 std::vector<StringRef> &Features) {
52 SmallVector<StringRef, 8> Split;
53 text.split(Split, StringRef("+"), -1, false);
55 for (StringRef Feature : Split) {
56 StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
57 if (!FeatureName.empty())
58 Features.push_back(FeatureName);
59 else if (Feature == "neon" || Feature == "noneon")
60 D.Diag(clang::diag::err_drv_no_neon_modifier);
67 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
68 // decode CPU and feature.
69 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
70 std::vector<StringRef> &Features) {
71 std::pair<StringRef, StringRef> Split = Mcpu.split("+");
74 if (CPU == "generic") {
75 Features.push_back("+neon");
77 unsigned ArchKind = llvm::AArch64::parseCPUArch(CPU);
78 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
81 unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
82 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
86 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
93 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
95 std::vector<StringRef> &Features) {
96 std::string MarchLowerCase = March.lower();
97 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
99 unsigned ArchKind = llvm::AArch64::parseArch(Split.first);
100 if (ArchKind == static_cast<unsigned>(llvm::AArch64::ArchKind::AK_INVALID) ||
101 !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
102 (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features)))
109 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
111 std::vector<StringRef> &Features) {
113 std::string McpuLowerCase = Mcpu.lower();
114 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
121 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
123 std::vector<StringRef> &Features) {
124 std::string MtuneLowerCase = Mtune.lower();
125 // Handle CPU name is 'native'.
126 if (MtuneLowerCase == "native")
127 MtuneLowerCase = llvm::sys::getHostCPUName();
128 if (MtuneLowerCase == "cyclone") {
129 Features.push_back("+zcm");
130 Features.push_back("+zcz");
136 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
138 std::vector<StringRef> &Features) {
140 std::vector<StringRef> DecodedFeature;
141 std::string McpuLowerCase = Mcpu.lower();
142 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
145 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
148 void aarch64::getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
149 std::vector<StringRef> &Features) {
152 // Enable NEON by default.
153 Features.push_back("+neon");
154 if ((A = Args.getLastArg(options::OPT_march_EQ)))
155 success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
156 else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
157 success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
158 else if (Args.hasArg(options::OPT_arch))
159 success = getAArch64ArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args, A),
162 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
164 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
165 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
167 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
168 else if (success && Args.hasArg(options::OPT_arch))
169 success = getAArch64MicroArchFeaturesFromMcpu(
170 D, getAArch64TargetCPU(Args, A), Args, Features);
173 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
175 if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
176 Features.push_back("-fp-armv8");
177 Features.push_back("-crypto");
178 Features.push_back("-neon");
182 if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
183 if (A->getOption().matches(options::OPT_mcrc))
184 Features.push_back("+crc");
186 Features.push_back("-crc");
189 if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
190 options::OPT_munaligned_access))
191 if (A->getOption().matches(options::OPT_mno_unaligned_access))
192 Features.push_back("+strict-align");
194 if (Args.hasArg(options::OPT_ffixed_x18))
195 Features.push_back("+reserve-x18");
197 if (Args.hasArg(options::OPT_mno_neg_immediates))
198 Features.push_back("+no-neg-immediates");