clang 8.0.0

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"
12 #include "clang/Driver/DriverDiagnostic.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 /// \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();
26 }
27
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) {
33 std::string CPU;
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();
38 }
39
40 // Handle CPU name is 'native'.
41 if (CPU == "native")
42 return llvm::sys::getHostCPUName();
43 else if (CPU.size())
44 return CPU;
45
46 // Make sure we pick "cyclone" if -arch is used or when targetting a Darwin
47 // OS.
48 if (Args.getLastArg(options::OPT_arch) || Triple.isOSDarwin())
49 return "cyclone";
50
51 return "generic";
52 }
53
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);
59
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);
66 else
67 return false;
68 }
69 return true;
70 }
71
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("+");
77 CPU = Split.first;
78
79 if (CPU == "native")
80 CPU = llvm::sys::getHostCPUName();
81
82 if (CPU == "generic") {
83 Features.push_back("+neon");
84 } else {
85 llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU);
86 if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
87 return false;
88
89 unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
90 if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
91 return false;
92 }
93
94 if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
95 return false;
96
97 return true;
98 }
99
100 static bool
101 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
102 const ArgList &Args,
103 std::vector<StringRef> &Features) {
104 std::string MarchLowerCase = March.lower();
105 std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
106
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)))
111 return false;
112
113 return true;
114 }
115
116 static bool
117 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
118 const ArgList &Args,
119 std::vector<StringRef> &Features) {
120 StringRef CPU;
121 std::string McpuLowerCase = Mcpu.lower();
122 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
123 return false;
124
125 return true;
126 }
127
128 static bool
129 getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
130 const ArgList &Args,
131 std::vector<StringRef> &Features) {
132 std::string MtuneLowerCase = Mtune.lower();
133 // Check CPU name is valid
134 std::vector<StringRef> MtuneFeatures;
135 StringRef Tune;
136 if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
137 return false;
138
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");
145 }
146 return true;
147 }
148
149 static bool
150 getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
151 const ArgList &Args,
152 std::vector<StringRef> &Features) {
153 StringRef CPU;
154 std::vector<StringRef> DecodedFeature;
155 std::string McpuLowerCase = Mcpu.lower();
156 if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
157 return false;
158
159 return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
160 }
161
162 void aarch64::getAArch64TargetFeatures(const Driver &D,
163 const llvm::Triple &Triple,
164 const ArgList &Args,
165 std::vector<StringRef> &Features) {
166 Arg *A;
167 bool success = true;
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);
177
178 if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
179 success =
180 getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
181 else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
182 success =
183 getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
184 else if (success &&
185 (Args.hasArg(options::OPT_arch) || isCPUDeterminedByTriple(Triple)))
186 success = getAArch64MicroArchFeaturesFromMcpu(
187 D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
188
189 if (!success)
190 D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
191
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");
196 }
197
198 // En/disable crc
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");
202 else
203 Features.push_back("-crc");
204 }
205
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");
218 }
219 else
220 goto fp16_fml_fallthrough;
221 }
222 else {
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");
230 }
231
232 // FIXME: this needs reimplementation too after the TargetParser rewrite
233 //
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;
247
248 bool NoCrypto = false;
249 if (HasCrypto && HasNoCrypto) {
250 if (PosNoCrypto < PosCrypto)
251 NoCrypto = true;
252 }
253
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);
263 if (HasSM4)
264 Features.push_back("+sm4");
265 if (HasSHA3)
266 Features.push_back("+sha3");
267 if (HasSHA2)
268 Features.push_back("+sha2");
269 if (HasAES)
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);
279 if (!HasSM4)
280 Features.push_back("-sm4");
281 if (!HasSHA3)
282 Features.push_back("-sha3");
283 if (!HasSHA2)
284 Features.push_back("-sha2");
285 if (!HasAES)
286 Features.push_back("-aes");
287 }
288 } else {
289 if (HasCrypto && !NoCrypto) {
290 const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
291 const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
292 if (HasSHA2)
293 Features.push_back("+sha2");
294 if (HasAES)
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);
302 if (!HasSHA2)
303 Features.push_back("-sha2");
304 if (!HasAES)
305 Features.push_back("-aes");
306 if (HasV82a || HasV83a || HasV84a) {
307 Features.push_back("-sm4");
308 Features.push_back("-sha3");
309 }
310 }
311 }
312
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");
317
318 if (Args.hasArg(options::OPT_ffixed_x1))
319 Features.push_back("+reserve-x1");
320
321 if (Args.hasArg(options::OPT_ffixed_x2))
322 Features.push_back("+reserve-x2");
323
324 if (Args.hasArg(options::OPT_ffixed_x3))
325 Features.push_back("+reserve-x3");
326
327 if (Args.hasArg(options::OPT_ffixed_x4))
328 Features.push_back("+reserve-x4");
329
330 if (Args.hasArg(options::OPT_ffixed_x5))
331 Features.push_back("+reserve-x5");
332
333 if (Args.hasArg(options::OPT_ffixed_x6))
334 Features.push_back("+reserve-x6");
335
336 if (Args.hasArg(options::OPT_ffixed_x7))
337 Features.push_back("+reserve-x7");
338
339 if (Args.hasArg(options::OPT_ffixed_x18))
340 Features.push_back("+reserve-x18");
341
342 if (Args.hasArg(options::OPT_ffixed_x20))
343 Features.push_back("+reserve-x20");
344
345 if (Args.hasArg(options::OPT_fcall_saved_x8))
346 Features.push_back("+call-saved-x8");
347
348 if (Args.hasArg(options::OPT_fcall_saved_x9))
349 Features.push_back("+call-saved-x9");
350
351 if (Args.hasArg(options::OPT_fcall_saved_x10))
352 Features.push_back("+call-saved-x10");
353
354 if (Args.hasArg(options::OPT_fcall_saved_x11))
355 Features.push_back("+call-saved-x11");
356
357 if (Args.hasArg(options::OPT_fcall_saved_x12))
358 Features.push_back("+call-saved-x12");
359
360 if (Args.hasArg(options::OPT_fcall_saved_x13))
361 Features.push_back("+call-saved-x13");
362
363 if (Args.hasArg(options::OPT_fcall_saved_x14))
364 Features.push_back("+call-saved-x14");
365
366 if (Args.hasArg(options::OPT_fcall_saved_x15))
367 Features.push_back("+call-saved-x15");
368
369 if (Args.hasArg(options::OPT_fcall_saved_x18))
370 Features.push_back("+call-saved-x18");
371
372 if (Args.hasArg(options::OPT_mno_neg_immediates))
373 Features.push_back("+no-neg-immediates");
374 }
DecodeAArch64Mcpu
static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU, std::vector< StringRef > &Features)
Definition: AArch64.cpp:74
clang::driver::tools::aarch64::getAArch64TargetFeatures
void getAArch64TargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
getAArch64MicroArchFeaturesFromMcpu
static bool getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:150
clang::driver::tools::aarch64::getAArch64TargetCPU
std::string getAArch64TargetCPU(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, llvm::opt::Arg *&A)
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
getExtensionFeatures
static void getExtensionFeatures(const Driver &D, const ArgList &Args, std::vector< StringRef > &Features, StringRef &MArch, StringRef &Exts)
Definition: RISCV.cpp:102
DecodeAArch64Features
static bool DecodeAArch64Features(const Driver &D, StringRef text, std::vector< StringRef > &Features)
Definition: AArch64.cpp:55
clang::driver::tools
Definition: AMDGPU.h:21
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
Driver.h
getAArch64ArchFeaturesFromMcpu
static bool getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:117
llvm::opt
Definition: Action.h:24
getAArch64MicroArchFeaturesFromMtune
static bool getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:129
llvm::SmallVector
Definition: LLVM.h:36
Options.h
ptrdiff_t
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
Definition: opencl-c.h:76
DwarfFissionKind::Split
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::comments::tok::text
Definition: CommentLexer.h:36
getAArch64ArchFeaturesFromMarch
static bool getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:101
clang::driver
Definition: Action.h:32
DriverDiagnostic.h
isCPUDeterminedByTriple
static bool isCPUDeterminedByTriple(const llvm::Triple &Triple)
Definition: AArch64.cpp:24
AArch64.h