David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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 | |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 10 | #include "AArch64.h" |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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 | /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are |
Peter Smith | 820e46f | 2017-10-24 09:51:55 +0000 | [diff] [blame] | 23 | /// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is |
| 24 | /// provided, or to nullptr otherwise. |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 25 | std::string aarch64::getAArch64TargetCPU(const ArgList &Args, Arg *&A) { |
| 26 | std::string CPU; |
Peter Smith | 820e46f | 2017-10-24 09:51:55 +0000 | [diff] [blame] | 27 | // If we have -mcpu, use that. |
| 28 | if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) { |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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) { |
| 50 | SmallVector<StringRef, 8> Split; |
| 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 | |
Florian Hahn | 4327b3e | 2018-07-06 10:49:59 +0000 | [diff] [blame] | 72 | if (CPU == "native") |
| 73 | CPU = llvm::sys::getHostCPUName(); |
| 74 | |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 75 | if (CPU == "generic") { |
| 76 | Features.push_back("+neon"); |
| 77 | } else { |
Florian Hahn | ef5bbd6 | 2017-07-27 16:28:39 +0000 | [diff] [blame] | 78 | llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU); |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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 | |
Florian Hahn | ef5bbd6 | 2017-07-27 16:28:39 +0000 | [diff] [blame] | 100 | llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first); |
| 101 | if (ArchKind == llvm::AArch64::ArchKind::INVALID || |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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 |
| 122 | getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, |
| 123 | const ArgList &Args, |
| 124 | std::vector<StringRef> &Features) { |
| 125 | std::string MtuneLowerCase = Mtune.lower(); |
Peter Smith | 820e46f | 2017-10-24 09:51:55 +0000 | [diff] [blame] | 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 | |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 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 |
| 143 | getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, |
| 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)) |
| 166 | success = getAArch64ArchFeaturesFromMcpu(D, getAArch64TargetCPU(Args, A), |
| 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)) |
| 176 | success = getAArch64MicroArchFeaturesFromMcpu( |
| 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 | |
Sjoerd Meijer | c017656 | 2018-09-24 07:55:20 +0000 | [diff] [blame] | 196 | // Handle (arch-dependent) fp16fml/fullfp16 relationship. |
| 197 | // FIXME: this fp16fml option handling will be reimplemented after the |
| 198 | // TargetParser rewrite. |
| 199 | const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16"); |
| 200 | const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml"); |
| 201 | if (std::find(Features.begin(), Features.end(), "+v8.4a") != Features.end()) { |
| 202 | const auto ItRFullFP16 = std::find(Features.rbegin(), Features.rend(), "+fullfp16"); |
| 203 | if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) { |
| 204 | // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml. |
| 205 | // Only append the +fp16fml if there is no -fp16fml after the +fullfp16. |
| 206 | if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16) |
| 207 | Features.push_back("+fp16fml"); |
| 208 | } |
| 209 | else |
| 210 | goto fp16_fml_fallthrough; |
| 211 | } |
| 212 | else { |
| 213 | fp16_fml_fallthrough: |
| 214 | // In both of these cases, putting the 'other' feature on the end of the vector will |
| 215 | // result in the same effect as placing it immediately after the current feature. |
| 216 | if (ItRNoFullFP16 < ItRFP16FML) |
| 217 | Features.push_back("-fp16fml"); |
| 218 | else if (ItRNoFullFP16 > ItRFP16FML) |
| 219 | Features.push_back("+fullfp16"); |
| 220 | } |
| 221 | |
Sjoerd Meijer | d60540a | 2018-10-04 07:38:53 +0000 | [diff] [blame^] | 222 | // FIXME: this needs reimplementation too after the TargetParser rewrite |
| 223 | // |
| 224 | // Context sensitive meaning of Crypto: |
| 225 | // 1) For Arch >= ARMv8.4a: crypto = sm4 + sha3 + sha2 + aes |
| 226 | // 2) For Arch <= ARMv8.3a: crypto = sha2 + aes |
| 227 | const auto ItBegin = Features.begin(); |
| 228 | const auto ItEnd = Features.end(); |
| 229 | const auto ItRBegin = Features.rbegin(); |
| 230 | const auto ItREnd = Features.rend(); |
| 231 | const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto"); |
| 232 | const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto"); |
| 233 | const auto HasCrypto = ItRCrypto != ItREnd; |
| 234 | const auto HasNoCrypto = ItRNoCrypto != ItREnd; |
| 235 | const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin; |
| 236 | const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin; |
| 237 | |
| 238 | bool NoCrypto = false; |
| 239 | if (HasCrypto && HasNoCrypto) { |
| 240 | if (PosNoCrypto < PosCrypto) |
| 241 | NoCrypto = true; |
| 242 | } |
| 243 | |
| 244 | if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) { |
| 245 | if (HasCrypto && !NoCrypto) { |
| 246 | // Check if we have NOT disabled an algorithm with something like: |
| 247 | // +crypto, -algorithm |
| 248 | // And if "-algorithm" does not occur, we enable that crypto algorithm. |
| 249 | const bool HasSM4 = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd); |
| 250 | const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd); |
| 251 | const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd); |
| 252 | const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd); |
| 253 | if (HasSM4) |
| 254 | Features.push_back("+sm4"); |
| 255 | if (HasSHA3) |
| 256 | Features.push_back("+sha3"); |
| 257 | if (HasSHA2) |
| 258 | Features.push_back("+sha2"); |
| 259 | if (HasAES) |
| 260 | Features.push_back("+aes"); |
| 261 | } else if (HasNoCrypto) { |
| 262 | // Check if we have NOT enabled a crypto algorithm with something like: |
| 263 | // -crypto, +algorithm |
| 264 | // And if "+algorithm" does not occur, we disable that crypto algorithm. |
| 265 | const bool HasSM4 = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd); |
| 266 | const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd); |
| 267 | const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd); |
| 268 | const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd); |
| 269 | if (!HasSM4) |
| 270 | Features.push_back("-sm4"); |
| 271 | if (!HasSHA3) |
| 272 | Features.push_back("-sha3"); |
| 273 | if (!HasSHA2) |
| 274 | Features.push_back("-sha2"); |
| 275 | if (!HasAES) |
| 276 | Features.push_back("-aes"); |
| 277 | } |
| 278 | } else { |
| 279 | if (HasCrypto && !NoCrypto) { |
| 280 | const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd); |
| 281 | const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd); |
| 282 | if (HasSHA2) |
| 283 | Features.push_back("+sha2"); |
| 284 | if (HasAES) |
| 285 | Features.push_back("+aes"); |
| 286 | } else if (HasNoCrypto) { |
| 287 | const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd); |
| 288 | const bool HasAES = (std::find(ItBegin, ItEnd, "+aes") != ItEnd); |
| 289 | const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd); |
| 290 | const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd); |
| 291 | const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd); |
| 292 | if (!HasSHA2) |
| 293 | Features.push_back("-sha2"); |
| 294 | if (!HasAES) |
| 295 | Features.push_back("-aes"); |
| 296 | if (HasV82a || HasV83a || HasV84a) { |
| 297 | Features.push_back("-sm4"); |
| 298 | Features.push_back("-sha3"); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 303 | if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, |
| 304 | options::OPT_munaligned_access)) |
| 305 | if (A->getOption().matches(options::OPT_mno_unaligned_access)) |
| 306 | Features.push_back("+strict-align"); |
| 307 | |
Tri Vo | 6e8abbc | 2018-09-12 23:45:04 +0000 | [diff] [blame] | 308 | if (Args.hasArg(options::OPT_ffixed_x1)) |
| 309 | Features.push_back("+reserve-x1"); |
| 310 | |
| 311 | if (Args.hasArg(options::OPT_ffixed_x2)) |
| 312 | Features.push_back("+reserve-x2"); |
| 313 | |
| 314 | if (Args.hasArg(options::OPT_ffixed_x3)) |
| 315 | Features.push_back("+reserve-x3"); |
| 316 | |
| 317 | if (Args.hasArg(options::OPT_ffixed_x4)) |
| 318 | Features.push_back("+reserve-x4"); |
| 319 | |
| 320 | if (Args.hasArg(options::OPT_ffixed_x5)) |
| 321 | Features.push_back("+reserve-x5"); |
| 322 | |
| 323 | if (Args.hasArg(options::OPT_ffixed_x6)) |
| 324 | Features.push_back("+reserve-x6"); |
| 325 | |
| 326 | if (Args.hasArg(options::OPT_ffixed_x7)) |
| 327 | Features.push_back("+reserve-x7"); |
| 328 | |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 329 | if (Args.hasArg(options::OPT_ffixed_x18)) |
| 330 | Features.push_back("+reserve-x18"); |
Sanne Wouda | 784004e | 2017-03-27 15:34:52 +0000 | [diff] [blame] | 331 | |
Petr Hosek | 7250908 | 2018-06-12 20:00:50 +0000 | [diff] [blame] | 332 | if (Args.hasArg(options::OPT_ffixed_x20)) |
| 333 | Features.push_back("+reserve-x20"); |
| 334 | |
Tri Vo | 28e7e60 | 2018-09-25 16:48:40 +0000 | [diff] [blame] | 335 | if (Args.hasArg(options::OPT_fcall_saved_x8)) |
| 336 | Features.push_back("+call-saved-x8"); |
| 337 | |
| 338 | if (Args.hasArg(options::OPT_fcall_saved_x9)) |
| 339 | Features.push_back("+call-saved-x9"); |
| 340 | |
| 341 | if (Args.hasArg(options::OPT_fcall_saved_x10)) |
| 342 | Features.push_back("+call-saved-x10"); |
| 343 | |
| 344 | if (Args.hasArg(options::OPT_fcall_saved_x11)) |
| 345 | Features.push_back("+call-saved-x11"); |
| 346 | |
| 347 | if (Args.hasArg(options::OPT_fcall_saved_x12)) |
| 348 | Features.push_back("+call-saved-x12"); |
| 349 | |
| 350 | if (Args.hasArg(options::OPT_fcall_saved_x13)) |
| 351 | Features.push_back("+call-saved-x13"); |
| 352 | |
| 353 | if (Args.hasArg(options::OPT_fcall_saved_x14)) |
| 354 | Features.push_back("+call-saved-x14"); |
| 355 | |
| 356 | if (Args.hasArg(options::OPT_fcall_saved_x15)) |
| 357 | Features.push_back("+call-saved-x15"); |
| 358 | |
| 359 | if (Args.hasArg(options::OPT_fcall_saved_x18)) |
| 360 | Features.push_back("+call-saved-x18"); |
| 361 | |
Sanne Wouda | 784004e | 2017-03-27 15:34:52 +0000 | [diff] [blame] | 362 | if (Args.hasArg(options::OPT_mno_neg_immediates)) |
| 363 | Features.push_back("+no-neg-immediates"); |
David L. Jones | ecc6de3 | 2017-02-24 00:28:01 +0000 | [diff] [blame] | 364 | } |