AArch64.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. //===--- AArch64.cpp - Implement AArch64 target feature support -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements AArch64 TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "AArch64.h"
  13. #include "clang/Basic/LangOptions.h"
  14. #include "clang/Basic/TargetBuiltins.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. #include "llvm/Support/AArch64TargetParser.h"
  20. #include "llvm/Support/ARMTargetParserCommon.h"
  21. #include <optional>
  22. using namespace clang;
  23. using namespace clang::targets;
  24. static constexpr Builtin::Info BuiltinInfo[] = {
  25. #define BUILTIN(ID, TYPE, ATTRS) \
  26. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  27. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  28. {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  29. #include "clang/Basic/BuiltinsNEON.def"
  30. #define BUILTIN(ID, TYPE, ATTRS) \
  31. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  32. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  33. {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  34. #include "clang/Basic/BuiltinsSVE.def"
  35. #define BUILTIN(ID, TYPE, ATTRS) \
  36. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  37. #define LANGBUILTIN(ID, TYPE, ATTRS, LANG) \
  38. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, LANG},
  39. #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
  40. {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  41. #define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANGS, FEATURE) \
  42. {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::HEADER, LANGS},
  43. #include "clang/Basic/BuiltinsAArch64.def"
  44. };
  45. void AArch64TargetInfo::setArchFeatures() {
  46. if (*ArchInfo == llvm::AArch64::ARMV8R) {
  47. HasDotProd = true;
  48. HasDIT = true;
  49. HasFlagM = true;
  50. HasRCPC = true;
  51. FPU |= NeonMode;
  52. HasCCPP = true;
  53. HasCRC = true;
  54. HasLSE = true;
  55. HasRDM = true;
  56. } else if (ArchInfo->Version.getMajor() == 8) {
  57. if (ArchInfo->Version.getMinor() >= 7u) {
  58. HasWFxT = true;
  59. }
  60. if (ArchInfo->Version.getMinor() >= 6u) {
  61. HasBFloat16 = true;
  62. HasMatMul = true;
  63. }
  64. if (ArchInfo->Version.getMinor() >= 5u) {
  65. HasAlternativeNZCV = true;
  66. HasFRInt3264 = true;
  67. HasSSBS = true;
  68. HasSB = true;
  69. HasPredRes = true;
  70. HasBTI = true;
  71. }
  72. if (ArchInfo->Version.getMinor() >= 4u) {
  73. HasDotProd = true;
  74. HasDIT = true;
  75. HasFlagM = true;
  76. }
  77. if (ArchInfo->Version.getMinor() >= 3u) {
  78. HasRCPC = true;
  79. FPU |= NeonMode;
  80. }
  81. if (ArchInfo->Version.getMinor() >= 2u) {
  82. HasCCPP = true;
  83. }
  84. if (ArchInfo->Version.getMinor() >= 1u) {
  85. HasCRC = true;
  86. HasLSE = true;
  87. HasRDM = true;
  88. }
  89. } else if (ArchInfo->Version.getMajor() == 9) {
  90. if (ArchInfo->Version.getMinor() >= 2u) {
  91. HasWFxT = true;
  92. }
  93. if (ArchInfo->Version.getMinor() >= 1u) {
  94. HasBFloat16 = true;
  95. HasMatMul = true;
  96. }
  97. FPU |= SveMode;
  98. HasSVE2 = true;
  99. HasFullFP16 = true;
  100. HasAlternativeNZCV = true;
  101. HasFRInt3264 = true;
  102. HasSSBS = true;
  103. HasSB = true;
  104. HasPredRes = true;
  105. HasBTI = true;
  106. HasDotProd = true;
  107. HasDIT = true;
  108. HasFlagM = true;
  109. HasRCPC = true;
  110. FPU |= NeonMode;
  111. HasCCPP = true;
  112. HasCRC = true;
  113. HasLSE = true;
  114. HasRDM = true;
  115. }
  116. }
  117. AArch64TargetInfo::AArch64TargetInfo(const llvm::Triple &Triple,
  118. const TargetOptions &Opts)
  119. : TargetInfo(Triple), ABI("aapcs") {
  120. if (getTriple().isOSOpenBSD()) {
  121. Int64Type = SignedLongLong;
  122. IntMaxType = SignedLongLong;
  123. } else {
  124. if (!getTriple().isOSDarwin() && !getTriple().isOSNetBSD())
  125. WCharType = UnsignedInt;
  126. Int64Type = SignedLong;
  127. IntMaxType = SignedLong;
  128. }
  129. // All AArch64 implementations support ARMv8 FP, which makes half a legal type.
  130. HasLegalHalfType = true;
  131. HalfArgsAndReturns = true;
  132. HasFloat16 = true;
  133. HasStrictFP = true;
  134. if (Triple.isArch64Bit())
  135. LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
  136. else
  137. LongWidth = LongAlign = PointerWidth = PointerAlign = 32;
  138. MaxVectorAlign = 128;
  139. MaxAtomicInlineWidth = 128;
  140. MaxAtomicPromoteWidth = 128;
  141. LongDoubleWidth = LongDoubleAlign = SuitableAlign = 128;
  142. LongDoubleFormat = &llvm::APFloat::IEEEquad();
  143. BFloat16Width = BFloat16Align = 16;
  144. BFloat16Format = &llvm::APFloat::BFloat();
  145. // Make __builtin_ms_va_list available.
  146. HasBuiltinMSVaList = true;
  147. // Make the SVE types available. Note that this deliberately doesn't
  148. // depend on SveMode, since in principle it should be possible to turn
  149. // SVE on and off within a translation unit. It should also be possible
  150. // to compile the global declaration:
  151. //
  152. // __SVInt8_t *ptr;
  153. //
  154. // even without SVE.
  155. HasAArch64SVETypes = true;
  156. // {} in inline assembly are neon specifiers, not assembly variant
  157. // specifiers.
  158. NoAsmVariants = true;
  159. // AAPCS gives rules for bitfields. 7.1.7 says: "The container type
  160. // contributes to the alignment of the containing aggregate in the same way
  161. // a plain (non bit-field) member of that type would, without exception for
  162. // zero-sized or anonymous bit-fields."
  163. assert(UseBitFieldTypeAlignment && "bitfields affect type alignment");
  164. UseZeroLengthBitfieldAlignment = true;
  165. // AArch64 targets default to using the ARM C++ ABI.
  166. TheCXXABI.set(TargetCXXABI::GenericAArch64);
  167. if (Triple.getOS() == llvm::Triple::Linux)
  168. this->MCountName = "\01_mcount";
  169. else if (Triple.getOS() == llvm::Triple::UnknownOS)
  170. this->MCountName =
  171. Opts.EABIVersion == llvm::EABI::GNU ? "\01_mcount" : "mcount";
  172. }
  173. StringRef AArch64TargetInfo::getABI() const { return ABI; }
  174. bool AArch64TargetInfo::setABI(const std::string &Name) {
  175. if (Name != "aapcs" && Name != "darwinpcs")
  176. return false;
  177. ABI = Name;
  178. return true;
  179. }
  180. bool AArch64TargetInfo::validateBranchProtection(StringRef Spec, StringRef,
  181. BranchProtectionInfo &BPI,
  182. StringRef &Err) const {
  183. llvm::ARM::ParsedBranchProtection PBP;
  184. if (!llvm::ARM::parseBranchProtection(Spec, PBP, Err))
  185. return false;
  186. BPI.SignReturnAddr =
  187. llvm::StringSwitch<LangOptions::SignReturnAddressScopeKind>(PBP.Scope)
  188. .Case("non-leaf", LangOptions::SignReturnAddressScopeKind::NonLeaf)
  189. .Case("all", LangOptions::SignReturnAddressScopeKind::All)
  190. .Default(LangOptions::SignReturnAddressScopeKind::None);
  191. if (PBP.Key == "a_key")
  192. BPI.SignKey = LangOptions::SignReturnAddressKeyKind::AKey;
  193. else
  194. BPI.SignKey = LangOptions::SignReturnAddressKeyKind::BKey;
  195. BPI.BranchTargetEnforcement = PBP.BranchTargetEnforcement;
  196. return true;
  197. }
  198. bool AArch64TargetInfo::isValidCPUName(StringRef Name) const {
  199. return Name == "generic" ||
  200. llvm::AArch64::parseCpu(Name).Arch != llvm::AArch64::INVALID;
  201. }
  202. bool AArch64TargetInfo::setCPU(const std::string &Name) {
  203. return isValidCPUName(Name);
  204. }
  205. void AArch64TargetInfo::fillValidCPUList(
  206. SmallVectorImpl<StringRef> &Values) const {
  207. llvm::AArch64::fillValidCPUArchList(Values);
  208. }
  209. void AArch64TargetInfo::getTargetDefinesARMV81A(const LangOptions &Opts,
  210. MacroBuilder &Builder) const {
  211. Builder.defineMacro("__ARM_FEATURE_QRDMX", "1");
  212. Builder.defineMacro("__ARM_FEATURE_ATOMICS", "1");
  213. Builder.defineMacro("__ARM_FEATURE_CRC32", "1");
  214. }
  215. void AArch64TargetInfo::getTargetDefinesARMV82A(const LangOptions &Opts,
  216. MacroBuilder &Builder) const {
  217. // Also include the ARMv8.1 defines
  218. getTargetDefinesARMV81A(Opts, Builder);
  219. }
  220. void AArch64TargetInfo::getTargetDefinesARMV83A(const LangOptions &Opts,
  221. MacroBuilder &Builder) const {
  222. Builder.defineMacro("__ARM_FEATURE_COMPLEX", "1");
  223. Builder.defineMacro("__ARM_FEATURE_JCVT", "1");
  224. Builder.defineMacro("__ARM_FEATURE_PAUTH", "1");
  225. // Also include the Armv8.2 defines
  226. getTargetDefinesARMV82A(Opts, Builder);
  227. }
  228. void AArch64TargetInfo::getTargetDefinesARMV84A(const LangOptions &Opts,
  229. MacroBuilder &Builder) const {
  230. // Also include the Armv8.3 defines
  231. getTargetDefinesARMV83A(Opts, Builder);
  232. }
  233. void AArch64TargetInfo::getTargetDefinesARMV85A(const LangOptions &Opts,
  234. MacroBuilder &Builder) const {
  235. Builder.defineMacro("__ARM_FEATURE_FRINT", "1");
  236. Builder.defineMacro("__ARM_FEATURE_BTI", "1");
  237. // Also include the Armv8.4 defines
  238. getTargetDefinesARMV84A(Opts, Builder);
  239. }
  240. void AArch64TargetInfo::getTargetDefinesARMV86A(const LangOptions &Opts,
  241. MacroBuilder &Builder) const {
  242. // Also include the Armv8.5 defines
  243. // FIXME: Armv8.6 makes the following extensions mandatory:
  244. // - __ARM_FEATURE_BF16
  245. // - __ARM_FEATURE_MATMUL_INT8
  246. // Handle them here.
  247. getTargetDefinesARMV85A(Opts, Builder);
  248. }
  249. void AArch64TargetInfo::getTargetDefinesARMV87A(const LangOptions &Opts,
  250. MacroBuilder &Builder) const {
  251. // Also include the Armv8.6 defines
  252. getTargetDefinesARMV86A(Opts, Builder);
  253. }
  254. void AArch64TargetInfo::getTargetDefinesARMV88A(const LangOptions &Opts,
  255. MacroBuilder &Builder) const {
  256. // Also include the Armv8.7 defines
  257. getTargetDefinesARMV87A(Opts, Builder);
  258. }
  259. void AArch64TargetInfo::getTargetDefinesARMV89A(const LangOptions &Opts,
  260. MacroBuilder &Builder) const {
  261. // Also include the Armv8.8 defines
  262. getTargetDefinesARMV88A(Opts, Builder);
  263. }
  264. void AArch64TargetInfo::getTargetDefinesARMV9A(const LangOptions &Opts,
  265. MacroBuilder &Builder) const {
  266. // Armv9-A maps to Armv8.5-A
  267. getTargetDefinesARMV85A(Opts, Builder);
  268. }
  269. void AArch64TargetInfo::getTargetDefinesARMV91A(const LangOptions &Opts,
  270. MacroBuilder &Builder) const {
  271. // Armv9.1-A maps to Armv8.6-A
  272. getTargetDefinesARMV86A(Opts, Builder);
  273. }
  274. void AArch64TargetInfo::getTargetDefinesARMV92A(const LangOptions &Opts,
  275. MacroBuilder &Builder) const {
  276. // Armv9.2-A maps to Armv8.7-A
  277. getTargetDefinesARMV87A(Opts, Builder);
  278. }
  279. void AArch64TargetInfo::getTargetDefinesARMV93A(const LangOptions &Opts,
  280. MacroBuilder &Builder) const {
  281. // Armv9.3-A maps to Armv8.8-A
  282. getTargetDefinesARMV88A(Opts, Builder);
  283. }
  284. void AArch64TargetInfo::getTargetDefinesARMV94A(const LangOptions &Opts,
  285. MacroBuilder &Builder) const {
  286. // Armv9.4-A maps to Armv8.9-A
  287. getTargetDefinesARMV89A(Opts, Builder);
  288. }
  289. void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
  290. MacroBuilder &Builder) const {
  291. // Target identification.
  292. Builder.defineMacro("__aarch64__");
  293. // For bare-metal.
  294. if (getTriple().getOS() == llvm::Triple::UnknownOS &&
  295. getTriple().isOSBinFormatELF())
  296. Builder.defineMacro("__ELF__");
  297. // Target properties.
  298. if (!getTriple().isOSWindows() && getTriple().isArch64Bit()) {
  299. Builder.defineMacro("_LP64");
  300. Builder.defineMacro("__LP64__");
  301. }
  302. std::string CodeModel = getTargetOpts().CodeModel;
  303. if (CodeModel == "default")
  304. CodeModel = "small";
  305. for (char &c : CodeModel)
  306. c = toupper(c);
  307. Builder.defineMacro("__AARCH64_CMODEL_" + CodeModel + "__");
  308. // ACLE predefines. Many can only have one possible value on v8 AArch64.
  309. Builder.defineMacro("__ARM_ACLE", "200");
  310. Builder.defineMacro("__ARM_ARCH",
  311. std::to_string(ArchInfo->Version.getMajor()));
  312. Builder.defineMacro("__ARM_ARCH_PROFILE",
  313. std::string("'") + (char)ArchInfo->Profile + "'");
  314. Builder.defineMacro("__ARM_64BIT_STATE", "1");
  315. Builder.defineMacro("__ARM_PCS_AAPCS64", "1");
  316. Builder.defineMacro("__ARM_ARCH_ISA_A64", "1");
  317. Builder.defineMacro("__ARM_FEATURE_CLZ", "1");
  318. Builder.defineMacro("__ARM_FEATURE_FMA", "1");
  319. Builder.defineMacro("__ARM_FEATURE_LDREX", "0xF");
  320. Builder.defineMacro("__ARM_FEATURE_IDIV", "1"); // As specified in ACLE
  321. Builder.defineMacro("__ARM_FEATURE_DIV"); // For backwards compatibility
  322. Builder.defineMacro("__ARM_FEATURE_NUMERIC_MAXMIN", "1");
  323. Builder.defineMacro("__ARM_FEATURE_DIRECTED_ROUNDING", "1");
  324. Builder.defineMacro("__ARM_ALIGN_MAX_STACK_PWR", "4");
  325. // 0xe implies support for half, single and double precision operations.
  326. Builder.defineMacro("__ARM_FP", "0xE");
  327. // PCS specifies this for SysV variants, which is all we support. Other ABIs
  328. // may choose __ARM_FP16_FORMAT_ALTERNATIVE.
  329. Builder.defineMacro("__ARM_FP16_FORMAT_IEEE", "1");
  330. Builder.defineMacro("__ARM_FP16_ARGS", "1");
  331. if (Opts.UnsafeFPMath)
  332. Builder.defineMacro("__ARM_FP_FAST", "1");
  333. Builder.defineMacro("__ARM_SIZEOF_WCHAR_T",
  334. Twine(Opts.WCharSize ? Opts.WCharSize : 4));
  335. Builder.defineMacro("__ARM_SIZEOF_MINIMAL_ENUM", Opts.ShortEnums ? "1" : "4");
  336. if (FPU & NeonMode) {
  337. Builder.defineMacro("__ARM_NEON", "1");
  338. // 64-bit NEON supports half, single and double precision operations.
  339. Builder.defineMacro("__ARM_NEON_FP", "0xE");
  340. }
  341. if (FPU & SveMode)
  342. Builder.defineMacro("__ARM_FEATURE_SVE", "1");
  343. if ((FPU & NeonMode) && (FPU & SveMode))
  344. Builder.defineMacro("__ARM_NEON_SVE_BRIDGE", "1");
  345. if (HasSVE2)
  346. Builder.defineMacro("__ARM_FEATURE_SVE2", "1");
  347. if (HasSVE2 && HasSVE2AES)
  348. Builder.defineMacro("__ARM_FEATURE_SVE2_AES", "1");
  349. if (HasSVE2 && HasSVE2BitPerm)
  350. Builder.defineMacro("__ARM_FEATURE_SVE2_BITPERM", "1");
  351. if (HasSVE2 && HasSVE2SHA3)
  352. Builder.defineMacro("__ARM_FEATURE_SVE2_SHA3", "1");
  353. if (HasSVE2 && HasSVE2SM4)
  354. Builder.defineMacro("__ARM_FEATURE_SVE2_SM4", "1");
  355. if (HasCRC)
  356. Builder.defineMacro("__ARM_FEATURE_CRC32", "1");
  357. if (HasRCPC)
  358. Builder.defineMacro("__ARM_FEATURE_RCPC", "1");
  359. if (HasFMV)
  360. Builder.defineMacro("__HAVE_FUNCTION_MULTI_VERSIONING", "1");
  361. // The __ARM_FEATURE_CRYPTO is deprecated in favor of finer grained feature
  362. // macros for AES, SHA2, SHA3 and SM4
  363. if (HasAES && HasSHA2)
  364. Builder.defineMacro("__ARM_FEATURE_CRYPTO", "1");
  365. if (HasAES)
  366. Builder.defineMacro("__ARM_FEATURE_AES", "1");
  367. if (HasSHA2)
  368. Builder.defineMacro("__ARM_FEATURE_SHA2", "1");
  369. if (HasSHA3) {
  370. Builder.defineMacro("__ARM_FEATURE_SHA3", "1");
  371. Builder.defineMacro("__ARM_FEATURE_SHA512", "1");
  372. }
  373. if (HasSM4) {
  374. Builder.defineMacro("__ARM_FEATURE_SM3", "1");
  375. Builder.defineMacro("__ARM_FEATURE_SM4", "1");
  376. }
  377. if (HasPAuth)
  378. Builder.defineMacro("__ARM_FEATURE_PAUTH", "1");
  379. if (HasUnaligned)
  380. Builder.defineMacro("__ARM_FEATURE_UNALIGNED", "1");
  381. if ((FPU & NeonMode) && HasFullFP16)
  382. Builder.defineMacro("__ARM_FEATURE_FP16_VECTOR_ARITHMETIC", "1");
  383. if (HasFullFP16)
  384. Builder.defineMacro("__ARM_FEATURE_FP16_SCALAR_ARITHMETIC", "1");
  385. if (HasDotProd)
  386. Builder.defineMacro("__ARM_FEATURE_DOTPROD", "1");
  387. if (HasMTE)
  388. Builder.defineMacro("__ARM_FEATURE_MEMORY_TAGGING", "1");
  389. if (HasTME)
  390. Builder.defineMacro("__ARM_FEATURE_TME", "1");
  391. if (HasMatMul)
  392. Builder.defineMacro("__ARM_FEATURE_MATMUL_INT8", "1");
  393. if (HasLSE)
  394. Builder.defineMacro("__ARM_FEATURE_ATOMICS", "1");
  395. if (HasBFloat16) {
  396. Builder.defineMacro("__ARM_FEATURE_BF16", "1");
  397. Builder.defineMacro("__ARM_FEATURE_BF16_VECTOR_ARITHMETIC", "1");
  398. Builder.defineMacro("__ARM_BF16_FORMAT_ALTERNATIVE", "1");
  399. Builder.defineMacro("__ARM_FEATURE_BF16_SCALAR_ARITHMETIC", "1");
  400. }
  401. if ((FPU & SveMode) && HasBFloat16) {
  402. Builder.defineMacro("__ARM_FEATURE_SVE_BF16", "1");
  403. }
  404. if ((FPU & SveMode) && HasMatmulFP64)
  405. Builder.defineMacro("__ARM_FEATURE_SVE_MATMUL_FP64", "1");
  406. if ((FPU & SveMode) && HasMatmulFP32)
  407. Builder.defineMacro("__ARM_FEATURE_SVE_MATMUL_FP32", "1");
  408. if ((FPU & SveMode) && HasMatMul)
  409. Builder.defineMacro("__ARM_FEATURE_SVE_MATMUL_INT8", "1");
  410. if ((FPU & NeonMode) && HasFP16FML)
  411. Builder.defineMacro("__ARM_FEATURE_FP16_FML", "1");
  412. if (Opts.hasSignReturnAddress()) {
  413. // Bitmask:
  414. // 0: Protection using the A key
  415. // 1: Protection using the B key
  416. // 2: Protection including leaf functions
  417. unsigned Value = 0;
  418. if (Opts.isSignReturnAddressWithAKey())
  419. Value |= (1 << 0);
  420. else
  421. Value |= (1 << 1);
  422. if (Opts.isSignReturnAddressScopeAll())
  423. Value |= (1 << 2);
  424. Builder.defineMacro("__ARM_FEATURE_PAC_DEFAULT", std::to_string(Value));
  425. }
  426. if (Opts.BranchTargetEnforcement)
  427. Builder.defineMacro("__ARM_FEATURE_BTI_DEFAULT", "1");
  428. if (HasLS64)
  429. Builder.defineMacro("__ARM_FEATURE_LS64", "1");
  430. if (HasRandGen)
  431. Builder.defineMacro("__ARM_FEATURE_RNG", "1");
  432. if (HasMOPS)
  433. Builder.defineMacro("__ARM_FEATURE_MOPS", "1");
  434. if (HasD128)
  435. Builder.defineMacro("__ARM_FEATURE_SYSREG128", "1");
  436. if (*ArchInfo == llvm::AArch64::ARMV8_1A)
  437. getTargetDefinesARMV81A(Opts, Builder);
  438. else if (*ArchInfo == llvm::AArch64::ARMV8_2A)
  439. getTargetDefinesARMV82A(Opts, Builder);
  440. else if (*ArchInfo == llvm::AArch64::ARMV8_3A)
  441. getTargetDefinesARMV83A(Opts, Builder);
  442. else if (*ArchInfo == llvm::AArch64::ARMV8_4A)
  443. getTargetDefinesARMV84A(Opts, Builder);
  444. else if (*ArchInfo == llvm::AArch64::ARMV8_5A)
  445. getTargetDefinesARMV85A(Opts, Builder);
  446. else if (*ArchInfo == llvm::AArch64::ARMV8_6A)
  447. getTargetDefinesARMV86A(Opts, Builder);
  448. else if (*ArchInfo == llvm::AArch64::ARMV8_7A)
  449. getTargetDefinesARMV87A(Opts, Builder);
  450. else if (*ArchInfo == llvm::AArch64::ARMV8_8A)
  451. getTargetDefinesARMV88A(Opts, Builder);
  452. else if (*ArchInfo == llvm::AArch64::ARMV8_9A)
  453. getTargetDefinesARMV89A(Opts, Builder);
  454. else if (*ArchInfo == llvm::AArch64::ARMV9A)
  455. getTargetDefinesARMV9A(Opts, Builder);
  456. else if (*ArchInfo == llvm::AArch64::ARMV9_1A)
  457. getTargetDefinesARMV91A(Opts, Builder);
  458. else if (*ArchInfo == llvm::AArch64::ARMV9_2A)
  459. getTargetDefinesARMV92A(Opts, Builder);
  460. else if (*ArchInfo == llvm::AArch64::ARMV9_3A)
  461. getTargetDefinesARMV93A(Opts, Builder);
  462. else if (*ArchInfo == llvm::AArch64::ARMV9_4A)
  463. getTargetDefinesARMV94A(Opts, Builder);
  464. // All of the __sync_(bool|val)_compare_and_swap_(1|2|4|8) builtins work.
  465. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  466. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  467. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  468. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  469. // Allow detection of fast FMA support.
  470. Builder.defineMacro("__FP_FAST_FMA", "1");
  471. Builder.defineMacro("__FP_FAST_FMAF", "1");
  472. // C/C++ operators work on both VLS and VLA SVE types
  473. if (FPU & SveMode)
  474. Builder.defineMacro("__ARM_FEATURE_SVE_VECTOR_OPERATORS", "2");
  475. if (Opts.VScaleMin && Opts.VScaleMin == Opts.VScaleMax) {
  476. Builder.defineMacro("__ARM_FEATURE_SVE_BITS", Twine(Opts.VScaleMin * 128));
  477. }
  478. }
  479. ArrayRef<Builtin::Info> AArch64TargetInfo::getTargetBuiltins() const {
  480. return llvm::ArrayRef(BuiltinInfo, clang::AArch64::LastTSBuiltin -
  481. Builtin::FirstTSBuiltin);
  482. }
  483. std::optional<std::pair<unsigned, unsigned>>
  484. AArch64TargetInfo::getVScaleRange(const LangOptions &LangOpts) const {
  485. if (LangOpts.VScaleMin || LangOpts.VScaleMax)
  486. return std::pair<unsigned, unsigned>(
  487. LangOpts.VScaleMin ? LangOpts.VScaleMin : 1, LangOpts.VScaleMax);
  488. if (hasFeature("sve"))
  489. return std::pair<unsigned, unsigned>(1, 16);
  490. return std::nullopt;
  491. }
  492. unsigned AArch64TargetInfo::multiVersionSortPriority(StringRef Name) const {
  493. if (Name == "default")
  494. return 0;
  495. for (const auto &E : llvm::AArch64::Extensions)
  496. if (Name == E.Name)
  497. return E.FmvPriority;
  498. return 0;
  499. }
  500. unsigned AArch64TargetInfo::multiVersionFeatureCost() const {
  501. // Take the maximum priority as per feature cost, so more features win.
  502. return llvm::AArch64::ExtensionInfo::MaxFMVPriority;
  503. }
  504. bool AArch64TargetInfo::getFeatureDepOptions(StringRef Name,
  505. std::string &FeatureVec) const {
  506. FeatureVec = "";
  507. for (const auto &E : llvm::AArch64::Extensions) {
  508. if (Name == E.Name) {
  509. FeatureVec = E.DependentFeatures;
  510. break;
  511. }
  512. }
  513. return FeatureVec != "";
  514. }
  515. bool AArch64TargetInfo::validateCpuSupports(StringRef FeatureStr) const {
  516. for (const auto &E : llvm::AArch64::Extensions)
  517. if (FeatureStr == E.Name)
  518. return true;
  519. return false;
  520. }
  521. bool AArch64TargetInfo::hasFeature(StringRef Feature) const {
  522. return llvm::StringSwitch<bool>(Feature)
  523. .Cases("aarch64", "arm64", "arm", true)
  524. .Case("fmv", HasFMV)
  525. .Cases("neon", "fp", "simd", FPU & NeonMode)
  526. .Case("jscvt", HasJSCVT)
  527. .Case("fcma", HasFCMA)
  528. .Case("rng", HasRandGen)
  529. .Case("flagm", HasFlagM)
  530. .Case("flagm2", HasAlternativeNZCV)
  531. .Case("fp16fml", HasFP16FML)
  532. .Case("dotprod", HasDotProd)
  533. .Case("sm4", HasSM4)
  534. .Case("rdm", HasRDM)
  535. .Case("lse", HasLSE)
  536. .Case("crc", HasCRC)
  537. .Case("sha2", HasSHA2)
  538. .Case("sha3", HasSHA3)
  539. .Cases("aes", "pmull", HasAES)
  540. .Cases("fp16", "fullfp16", HasFullFP16)
  541. .Case("dit", HasDIT)
  542. .Case("dpb", HasCCPP)
  543. .Case("dpb2", HasCCDP)
  544. .Case("rcpc", HasRCPC)
  545. .Case("frintts", HasFRInt3264)
  546. .Case("i8mm", HasMatMul)
  547. .Case("bf16", HasBFloat16)
  548. .Case("sve", FPU & SveMode)
  549. .Case("sve-bf16", FPU & SveMode && HasBFloat16)
  550. .Case("sve-i8mm", FPU & SveMode && HasMatMul)
  551. .Case("f32mm", FPU & SveMode && HasMatmulFP32)
  552. .Case("f64mm", FPU & SveMode && HasMatmulFP64)
  553. .Case("sve2", FPU & SveMode && HasSVE2)
  554. .Case("sve2-pmull128", FPU & SveMode && HasSVE2AES)
  555. .Case("sve2-bitperm", FPU & SveMode && HasSVE2BitPerm)
  556. .Case("sve2-sha3", FPU & SveMode && HasSVE2SHA3)
  557. .Case("sve2-sm4", FPU & SveMode && HasSVE2SM4)
  558. .Case("sme", HasSME)
  559. .Case("sme-f64f64", HasSMEF64)
  560. .Case("sme-i16i64", HasSMEI64)
  561. .Cases("memtag", "memtag2", HasMTE)
  562. .Case("sb", HasSB)
  563. .Case("predres", HasPredRes)
  564. .Cases("ssbs", "ssbs2", HasSSBS)
  565. .Case("bti", HasBTI)
  566. .Cases("ls64", "ls64_v", "ls64_accdata", HasLS64)
  567. .Case("wfxt", HasWFxT)
  568. .Default(false);
  569. }
  570. void AArch64TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
  571. StringRef Name, bool Enabled) const {
  572. Features[Name] = Enabled;
  573. // If the feature is an architecture feature (like v8.2a), add all previous
  574. // architecture versions and any dependant target features.
  575. const llvm::AArch64::ArchInfo &ArchInfo =
  576. llvm::AArch64::ArchInfo::findBySubArch(Name);
  577. if (ArchInfo == llvm::AArch64::INVALID)
  578. return; // Not an architecure, nothing more to do.
  579. // Disabling an architecture feature does not affect dependent features
  580. if (!Enabled)
  581. return;
  582. for (const auto *OtherArch : llvm::AArch64::ArchInfos)
  583. if (ArchInfo.implies(*OtherArch))
  584. Features[OtherArch->getSubArch()] = true;
  585. // Set any features implied by the architecture
  586. uint64_t Extensions =
  587. llvm::AArch64::getDefaultExtensions("generic", ArchInfo);
  588. std::vector<StringRef> CPUFeats;
  589. if (llvm::AArch64::getExtensionFeatures(Extensions, CPUFeats)) {
  590. for (auto F : CPUFeats) {
  591. assert(F[0] == '+' && "Expected + in target feature!");
  592. Features[F.drop_front(1)] = true;
  593. }
  594. }
  595. }
  596. bool AArch64TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  597. DiagnosticsEngine &Diags) {
  598. for (const auto &Feature : Features) {
  599. if (Feature == "-neon")
  600. HasNoNeon = true;
  601. if (Feature == "-sve")
  602. HasNoSVE = true;
  603. if (Feature == "+neon" || Feature == "+fp-armv8")
  604. FPU |= NeonMode;
  605. if (Feature == "+jscvt") {
  606. HasJSCVT = true;
  607. FPU |= NeonMode;
  608. }
  609. if (Feature == "+fcma") {
  610. HasFCMA = true;
  611. FPU |= NeonMode;
  612. }
  613. if (Feature == "+sve") {
  614. FPU |= NeonMode;
  615. FPU |= SveMode;
  616. HasFullFP16 = true;
  617. }
  618. if (Feature == "+sve2") {
  619. FPU |= NeonMode;
  620. FPU |= SveMode;
  621. HasFullFP16 = true;
  622. HasSVE2 = true;
  623. }
  624. if (Feature == "+sve2-aes") {
  625. FPU |= NeonMode;
  626. FPU |= SveMode;
  627. HasFullFP16 = true;
  628. HasSVE2 = true;
  629. HasSVE2AES = true;
  630. }
  631. if (Feature == "+sve2-sha3") {
  632. FPU |= NeonMode;
  633. FPU |= SveMode;
  634. HasFullFP16 = true;
  635. HasSVE2 = true;
  636. HasSVE2SHA3 = true;
  637. }
  638. if (Feature == "+sve2-sm4") {
  639. FPU |= NeonMode;
  640. FPU |= SveMode;
  641. HasFullFP16 = true;
  642. HasSVE2 = true;
  643. HasSVE2SM4 = true;
  644. }
  645. if (Feature == "+sve2-bitperm") {
  646. FPU |= NeonMode;
  647. FPU |= SveMode;
  648. HasFullFP16 = true;
  649. HasSVE2 = true;
  650. HasSVE2BitPerm = true;
  651. }
  652. if (Feature == "+f32mm") {
  653. FPU |= NeonMode;
  654. FPU |= SveMode;
  655. HasFullFP16 = true;
  656. HasMatmulFP32 = true;
  657. }
  658. if (Feature == "+f64mm") {
  659. FPU |= NeonMode;
  660. FPU |= SveMode;
  661. HasFullFP16 = true;
  662. HasMatmulFP64 = true;
  663. }
  664. if (Feature == "+sme") {
  665. HasSME = true;
  666. HasBFloat16 = true;
  667. }
  668. if (Feature == "+sme-f64f64") {
  669. HasSME = true;
  670. HasSMEF64 = true;
  671. HasBFloat16 = true;
  672. }
  673. if (Feature == "+sme-i16i64") {
  674. HasSME = true;
  675. HasSMEI64 = true;
  676. HasBFloat16 = true;
  677. }
  678. if (Feature == "+sb")
  679. HasSB = true;
  680. if (Feature == "+predres")
  681. HasPredRes = true;
  682. if (Feature == "+ssbs")
  683. HasSSBS = true;
  684. if (Feature == "+bti")
  685. HasBTI = true;
  686. if (Feature == "+wfxt")
  687. HasWFxT = true;
  688. if (Feature == "-fmv")
  689. HasFMV = false;
  690. if (Feature == "+crc")
  691. HasCRC = true;
  692. if (Feature == "+rcpc")
  693. HasRCPC = true;
  694. if (Feature == "+aes") {
  695. FPU |= NeonMode;
  696. HasAES = true;
  697. }
  698. if (Feature == "+sha2") {
  699. FPU |= NeonMode;
  700. HasSHA2 = true;
  701. }
  702. if (Feature == "+sha3") {
  703. FPU |= NeonMode;
  704. HasSHA2 = true;
  705. HasSHA3 = true;
  706. }
  707. if (Feature == "+rdm") {
  708. FPU |= NeonMode;
  709. HasRDM = true;
  710. }
  711. if (Feature == "+dit")
  712. HasDIT = true;
  713. if (Feature == "+cccp")
  714. HasCCPP = true;
  715. if (Feature == "+ccdp") {
  716. HasCCPP = true;
  717. HasCCDP = true;
  718. }
  719. if (Feature == "+fptoint")
  720. HasFRInt3264 = true;
  721. if (Feature == "+sm4") {
  722. FPU |= NeonMode;
  723. HasSM4 = true;
  724. }
  725. if (Feature == "+strict-align")
  726. HasUnaligned = false;
  727. // All predecessor archs are added but select the latest one for ArchKind.
  728. if (Feature == "+v8a" && ArchInfo->Version < llvm::AArch64::ARMV8A.Version)
  729. ArchInfo = &llvm::AArch64::ARMV8A;
  730. if (Feature == "+v8.1a" &&
  731. ArchInfo->Version < llvm::AArch64::ARMV8_1A.Version)
  732. ArchInfo = &llvm::AArch64::ARMV8_1A;
  733. if (Feature == "+v8.2a" &&
  734. ArchInfo->Version < llvm::AArch64::ARMV8_2A.Version)
  735. ArchInfo = &llvm::AArch64::ARMV8_2A;
  736. if (Feature == "+v8.3a" &&
  737. ArchInfo->Version < llvm::AArch64::ARMV8_3A.Version)
  738. ArchInfo = &llvm::AArch64::ARMV8_3A;
  739. if (Feature == "+v8.4a" &&
  740. ArchInfo->Version < llvm::AArch64::ARMV8_4A.Version)
  741. ArchInfo = &llvm::AArch64::ARMV8_4A;
  742. if (Feature == "+v8.5a" &&
  743. ArchInfo->Version < llvm::AArch64::ARMV8_5A.Version)
  744. ArchInfo = &llvm::AArch64::ARMV8_5A;
  745. if (Feature == "+v8.6a" &&
  746. ArchInfo->Version < llvm::AArch64::ARMV8_6A.Version)
  747. ArchInfo = &llvm::AArch64::ARMV8_6A;
  748. if (Feature == "+v8.7a" &&
  749. ArchInfo->Version < llvm::AArch64::ARMV8_7A.Version)
  750. ArchInfo = &llvm::AArch64::ARMV8_7A;
  751. if (Feature == "+v8.8a" &&
  752. ArchInfo->Version < llvm::AArch64::ARMV8_8A.Version)
  753. ArchInfo = &llvm::AArch64::ARMV8_8A;
  754. if (Feature == "+v8.9a" &&
  755. ArchInfo->Version < llvm::AArch64::ARMV8_9A.Version)
  756. ArchInfo = &llvm::AArch64::ARMV8_9A;
  757. if (Feature == "+v9a" && ArchInfo->Version < llvm::AArch64::ARMV9A.Version)
  758. ArchInfo = &llvm::AArch64::ARMV9A;
  759. if (Feature == "+v9.1a" &&
  760. ArchInfo->Version < llvm::AArch64::ARMV9_1A.Version)
  761. ArchInfo = &llvm::AArch64::ARMV9_1A;
  762. if (Feature == "+v9.2a" &&
  763. ArchInfo->Version < llvm::AArch64::ARMV9_2A.Version)
  764. ArchInfo = &llvm::AArch64::ARMV9_2A;
  765. if (Feature == "+v9.3a" &&
  766. ArchInfo->Version < llvm::AArch64::ARMV9_3A.Version)
  767. ArchInfo = &llvm::AArch64::ARMV9_3A;
  768. if (Feature == "+v9.4a" &&
  769. ArchInfo->Version < llvm::AArch64::ARMV9_4A.Version)
  770. ArchInfo = &llvm::AArch64::ARMV9_4A;
  771. if (Feature == "+v8r")
  772. ArchInfo = &llvm::AArch64::ARMV8R;
  773. if (Feature == "+fullfp16") {
  774. FPU |= NeonMode;
  775. HasFullFP16 = true;
  776. }
  777. if (Feature == "+dotprod") {
  778. FPU |= NeonMode;
  779. HasDotProd = true;
  780. }
  781. if (Feature == "+fp16fml") {
  782. FPU |= NeonMode;
  783. HasFullFP16 = true;
  784. HasFP16FML = true;
  785. }
  786. if (Feature == "+mte")
  787. HasMTE = true;
  788. if (Feature == "+tme")
  789. HasTME = true;
  790. if (Feature == "+pauth")
  791. HasPAuth = true;
  792. if (Feature == "+i8mm")
  793. HasMatMul = true;
  794. if (Feature == "+bf16")
  795. HasBFloat16 = true;
  796. if (Feature == "+lse")
  797. HasLSE = true;
  798. if (Feature == "+ls64")
  799. HasLS64 = true;
  800. if (Feature == "+rand")
  801. HasRandGen = true;
  802. if (Feature == "+flagm")
  803. HasFlagM = true;
  804. if (Feature == "+altnzcv") {
  805. HasFlagM = true;
  806. HasAlternativeNZCV = true;
  807. }
  808. if (Feature == "+mops")
  809. HasMOPS = true;
  810. if (Feature == "+d128")
  811. HasD128 = true;
  812. }
  813. // Check features that are manually disabled by command line options.
  814. // This needs to be checked after architecture-related features are handled,
  815. // making sure they are properly disabled when required.
  816. for (const auto &Feature : Features) {
  817. if (Feature == "-d128")
  818. HasD128 = false;
  819. }
  820. setDataLayout();
  821. setArchFeatures();
  822. if (HasNoNeon) {
  823. FPU &= ~NeonMode;
  824. FPU &= ~SveMode;
  825. }
  826. if (HasNoSVE)
  827. FPU &= ~SveMode;
  828. return true;
  829. }
  830. bool AArch64TargetInfo::initFeatureMap(
  831. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  832. const std::vector<std::string> &FeaturesVec) const {
  833. std::vector<std::string> UpdatedFeaturesVec;
  834. // Parse the CPU and add any implied features.
  835. const llvm::AArch64::ArchInfo &Arch = llvm::AArch64::parseCpu(CPU).Arch;
  836. if (Arch != llvm::AArch64::INVALID) {
  837. uint64_t Exts = llvm::AArch64::getDefaultExtensions(CPU, Arch);
  838. std::vector<StringRef> CPUFeats;
  839. llvm::AArch64::getExtensionFeatures(Exts, CPUFeats);
  840. for (auto F : CPUFeats) {
  841. assert((F[0] == '+' || F[0] == '-') && "Expected +/- in target feature!");
  842. UpdatedFeaturesVec.push_back(F.str());
  843. }
  844. }
  845. // Process target and dependent features. This is done in two loops collecting
  846. // them into UpdatedFeaturesVec: first to add dependent '+'features,
  847. // second to add target '+/-'features that can later disable some of
  848. // features added on the first loop.
  849. for (const auto &Feature : FeaturesVec)
  850. if ((Feature[0] == '?' || Feature[0] == '+')) {
  851. std::string Options;
  852. if (AArch64TargetInfo::getFeatureDepOptions(Feature.substr(1), Options)) {
  853. SmallVector<StringRef, 1> AttrFeatures;
  854. StringRef(Options).split(AttrFeatures, ",");
  855. for (auto F : AttrFeatures)
  856. UpdatedFeaturesVec.push_back(F.str());
  857. }
  858. }
  859. for (const auto &Feature : FeaturesVec)
  860. if (Feature[0] == '+') {
  861. std::string F;
  862. llvm::AArch64::getFeatureOption(Feature, F);
  863. UpdatedFeaturesVec.push_back(F);
  864. } else if (Feature[0] != '?')
  865. UpdatedFeaturesVec.push_back(Feature);
  866. return TargetInfo::initFeatureMap(Features, Diags, CPU, UpdatedFeaturesVec);
  867. }
  868. // Parse AArch64 Target attributes, which are a comma separated list of:
  869. // "arch=<arch>" - parsed to features as per -march=..
  870. // "cpu=<cpu>" - parsed to features as per -mcpu=.., with CPU set to <cpu>
  871. // "tune=<cpu>" - TuneCPU set to <cpu>
  872. // "feature", "no-feature" - Add (or remove) feature.
  873. // "+feature", "+nofeature" - Add (or remove) feature.
  874. ParsedTargetAttr AArch64TargetInfo::parseTargetAttr(StringRef Features) const {
  875. ParsedTargetAttr Ret;
  876. if (Features == "default")
  877. return Ret;
  878. SmallVector<StringRef, 1> AttrFeatures;
  879. Features.split(AttrFeatures, ",");
  880. bool FoundArch = false;
  881. auto SplitAndAddFeatures = [](StringRef FeatString,
  882. std::vector<std::string> &Features) {
  883. SmallVector<StringRef, 8> SplitFeatures;
  884. FeatString.split(SplitFeatures, StringRef("+"), -1, false);
  885. for (StringRef Feature : SplitFeatures) {
  886. StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
  887. if (!FeatureName.empty())
  888. Features.push_back(FeatureName.str());
  889. else
  890. // Pushing the original feature string to give a sema error later on
  891. // when they get checked.
  892. if (Feature.startswith("no"))
  893. Features.push_back("-" + Feature.drop_front(2).str());
  894. else
  895. Features.push_back("+" + Feature.str());
  896. }
  897. };
  898. for (auto &Feature : AttrFeatures) {
  899. Feature = Feature.trim();
  900. if (Feature.startswith("fpmath="))
  901. continue;
  902. if (Feature.startswith("branch-protection=")) {
  903. Ret.BranchProtection = Feature.split('=').second.trim();
  904. continue;
  905. }
  906. if (Feature.startswith("arch=")) {
  907. if (FoundArch)
  908. Ret.Duplicate = "arch=";
  909. FoundArch = true;
  910. std::pair<StringRef, StringRef> Split =
  911. Feature.split("=").second.trim().split("+");
  912. const llvm::AArch64::ArchInfo &AI = llvm::AArch64::parseArch(Split.first);
  913. // Parse the architecture version, adding the required features to
  914. // Ret.Features.
  915. if (AI == llvm::AArch64::INVALID)
  916. continue;
  917. Ret.Features.push_back(AI.ArchFeature.str());
  918. // Add any extra features, after the +
  919. SplitAndAddFeatures(Split.second, Ret.Features);
  920. } else if (Feature.startswith("cpu=")) {
  921. if (!Ret.CPU.empty())
  922. Ret.Duplicate = "cpu=";
  923. else {
  924. // Split the cpu string into "cpu=", "cortex-a710" and any remaining
  925. // "+feat" features.
  926. std::pair<StringRef, StringRef> Split =
  927. Feature.split("=").second.trim().split("+");
  928. Ret.CPU = Split.first;
  929. SplitAndAddFeatures(Split.second, Ret.Features);
  930. }
  931. } else if (Feature.startswith("tune=")) {
  932. if (!Ret.Tune.empty())
  933. Ret.Duplicate = "tune=";
  934. else
  935. Ret.Tune = Feature.split("=").second.trim();
  936. } else if (Feature.startswith("+")) {
  937. SplitAndAddFeatures(Feature, Ret.Features);
  938. } else if (Feature.startswith("no-")) {
  939. StringRef FeatureName =
  940. llvm::AArch64::getArchExtFeature(Feature.split("-").second);
  941. if (!FeatureName.empty())
  942. Ret.Features.push_back("-" + FeatureName.drop_front(1).str());
  943. else
  944. Ret.Features.push_back("-" + Feature.split("-").second.str());
  945. } else {
  946. // Try parsing the string to the internal target feature name. If it is
  947. // invalid, add the original string (which could already be an internal
  948. // name). These should be checked later by isValidFeatureName.
  949. StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
  950. if (!FeatureName.empty())
  951. Ret.Features.push_back(FeatureName.str());
  952. else
  953. Ret.Features.push_back("+" + Feature.str());
  954. }
  955. }
  956. return Ret;
  957. }
  958. bool AArch64TargetInfo::hasBFloat16Type() const {
  959. return true;
  960. }
  961. TargetInfo::CallingConvCheckResult
  962. AArch64TargetInfo::checkCallingConvention(CallingConv CC) const {
  963. switch (CC) {
  964. case CC_C:
  965. case CC_Swift:
  966. case CC_SwiftAsync:
  967. case CC_PreserveMost:
  968. case CC_PreserveAll:
  969. case CC_OpenCLKernel:
  970. case CC_AArch64VectorCall:
  971. case CC_AArch64SVEPCS:
  972. case CC_Win64:
  973. return CCCR_OK;
  974. default:
  975. return CCCR_Warning;
  976. }
  977. }
  978. bool AArch64TargetInfo::isCLZForZeroUndef() const { return false; }
  979. TargetInfo::BuiltinVaListKind AArch64TargetInfo::getBuiltinVaListKind() const {
  980. return TargetInfo::AArch64ABIBuiltinVaList;
  981. }
  982. const char *const AArch64TargetInfo::GCCRegNames[] = {
  983. // 32-bit Integer registers
  984. "w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10", "w11",
  985. "w12", "w13", "w14", "w15", "w16", "w17", "w18", "w19", "w20", "w21", "w22",
  986. "w23", "w24", "w25", "w26", "w27", "w28", "w29", "w30", "wsp",
  987. // 64-bit Integer registers
  988. "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",
  989. "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22",
  990. "x23", "x24", "x25", "x26", "x27", "x28", "fp", "lr", "sp",
  991. // 32-bit floating point regsisters
  992. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11",
  993. "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20", "s21", "s22",
  994. "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
  995. // 64-bit floating point regsisters
  996. "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "d11",
  997. "d12", "d13", "d14", "d15", "d16", "d17", "d18", "d19", "d20", "d21", "d22",
  998. "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
  999. // Neon vector registers
  1000. "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11",
  1001. "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22",
  1002. "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
  1003. // SVE vector registers
  1004. "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7", "z8", "z9", "z10",
  1005. "z11", "z12", "z13", "z14", "z15", "z16", "z17", "z18", "z19", "z20", "z21",
  1006. "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
  1007. // SVE predicate registers
  1008. "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10",
  1009. "p11", "p12", "p13", "p14", "p15"
  1010. };
  1011. ArrayRef<const char *> AArch64TargetInfo::getGCCRegNames() const {
  1012. return llvm::ArrayRef(GCCRegNames);
  1013. }
  1014. const TargetInfo::GCCRegAlias AArch64TargetInfo::GCCRegAliases[] = {
  1015. {{"w31"}, "wsp"},
  1016. {{"x31"}, "sp"},
  1017. // GCC rN registers are aliases of xN registers.
  1018. {{"r0"}, "x0"},
  1019. {{"r1"}, "x1"},
  1020. {{"r2"}, "x2"},
  1021. {{"r3"}, "x3"},
  1022. {{"r4"}, "x4"},
  1023. {{"r5"}, "x5"},
  1024. {{"r6"}, "x6"},
  1025. {{"r7"}, "x7"},
  1026. {{"r8"}, "x8"},
  1027. {{"r9"}, "x9"},
  1028. {{"r10"}, "x10"},
  1029. {{"r11"}, "x11"},
  1030. {{"r12"}, "x12"},
  1031. {{"r13"}, "x13"},
  1032. {{"r14"}, "x14"},
  1033. {{"r15"}, "x15"},
  1034. {{"r16"}, "x16"},
  1035. {{"r17"}, "x17"},
  1036. {{"r18"}, "x18"},
  1037. {{"r19"}, "x19"},
  1038. {{"r20"}, "x20"},
  1039. {{"r21"}, "x21"},
  1040. {{"r22"}, "x22"},
  1041. {{"r23"}, "x23"},
  1042. {{"r24"}, "x24"},
  1043. {{"r25"}, "x25"},
  1044. {{"r26"}, "x26"},
  1045. {{"r27"}, "x27"},
  1046. {{"r28"}, "x28"},
  1047. {{"r29", "x29"}, "fp"},
  1048. {{"r30", "x30"}, "lr"},
  1049. // The S/D/Q and W/X registers overlap, but aren't really aliases; we
  1050. // don't want to substitute one of these for a different-sized one.
  1051. };
  1052. ArrayRef<TargetInfo::GCCRegAlias> AArch64TargetInfo::getGCCRegAliases() const {
  1053. return llvm::ArrayRef(GCCRegAliases);
  1054. }
  1055. bool AArch64TargetInfo::validateAsmConstraint(
  1056. const char *&Name, TargetInfo::ConstraintInfo &Info) const {
  1057. switch (*Name) {
  1058. default:
  1059. return false;
  1060. case 'w': // Floating point and SIMD registers (V0-V31)
  1061. Info.setAllowsRegister();
  1062. return true;
  1063. case 'I': // Constant that can be used with an ADD instruction
  1064. case 'J': // Constant that can be used with a SUB instruction
  1065. case 'K': // Constant that can be used with a 32-bit logical instruction
  1066. case 'L': // Constant that can be used with a 64-bit logical instruction
  1067. case 'M': // Constant that can be used as a 32-bit MOV immediate
  1068. case 'N': // Constant that can be used as a 64-bit MOV immediate
  1069. case 'Y': // Floating point constant zero
  1070. case 'Z': // Integer constant zero
  1071. return true;
  1072. case 'Q': // A memory reference with base register and no offset
  1073. Info.setAllowsMemory();
  1074. return true;
  1075. case 'S': // A symbolic address
  1076. Info.setAllowsRegister();
  1077. return true;
  1078. case 'U':
  1079. if (Name[1] == 'p' && (Name[2] == 'l' || Name[2] == 'a')) {
  1080. // SVE predicate registers ("Upa"=P0-15, "Upl"=P0-P7)
  1081. Info.setAllowsRegister();
  1082. Name += 2;
  1083. return true;
  1084. }
  1085. // Ump: A memory address suitable for ldp/stp in SI, DI, SF and DF modes.
  1086. // Utf: A memory address suitable for ldp/stp in TF mode.
  1087. // Usa: An absolute symbolic address.
  1088. // Ush: The high part (bits 32:12) of a pc-relative symbolic address.
  1089. // Better to return an error saying that it's an unrecognised constraint
  1090. // even if this is a valid constraint in gcc.
  1091. return false;
  1092. case 'z': // Zero register, wzr or xzr
  1093. Info.setAllowsRegister();
  1094. return true;
  1095. case 'x': // Floating point and SIMD registers (V0-V15)
  1096. Info.setAllowsRegister();
  1097. return true;
  1098. case 'y': // SVE registers (V0-V7)
  1099. Info.setAllowsRegister();
  1100. return true;
  1101. }
  1102. return false;
  1103. }
  1104. bool AArch64TargetInfo::validateConstraintModifier(
  1105. StringRef Constraint, char Modifier, unsigned Size,
  1106. std::string &SuggestedModifier) const {
  1107. // Strip off constraint modifiers.
  1108. while (Constraint[0] == '=' || Constraint[0] == '+' || Constraint[0] == '&')
  1109. Constraint = Constraint.substr(1);
  1110. switch (Constraint[0]) {
  1111. default:
  1112. return true;
  1113. case 'z':
  1114. case 'r': {
  1115. switch (Modifier) {
  1116. case 'x':
  1117. case 'w':
  1118. // For now assume that the person knows what they're
  1119. // doing with the modifier.
  1120. return true;
  1121. default:
  1122. // By default an 'r' constraint will be in the 'x'
  1123. // registers.
  1124. if (Size == 64)
  1125. return true;
  1126. if (Size == 512)
  1127. return HasLS64;
  1128. SuggestedModifier = "w";
  1129. return false;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. const char *AArch64TargetInfo::getClobbers() const { return ""; }
  1135. int AArch64TargetInfo::getEHDataRegisterNumber(unsigned RegNo) const {
  1136. if (RegNo == 0)
  1137. return 0;
  1138. if (RegNo == 1)
  1139. return 1;
  1140. return -1;
  1141. }
  1142. bool AArch64TargetInfo::hasInt128Type() const { return true; }
  1143. AArch64leTargetInfo::AArch64leTargetInfo(const llvm::Triple &Triple,
  1144. const TargetOptions &Opts)
  1145. : AArch64TargetInfo(Triple, Opts) {}
  1146. void AArch64leTargetInfo::setDataLayout() {
  1147. if (getTriple().isOSBinFormatMachO()) {
  1148. if(getTriple().isArch32Bit())
  1149. resetDataLayout("e-m:o-p:32:32-i64:64-i128:128-n32:64-S128", "_");
  1150. else
  1151. resetDataLayout("e-m:o-i64:64-i128:128-n32:64-S128", "_");
  1152. } else
  1153. resetDataLayout("e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
  1154. }
  1155. void AArch64leTargetInfo::getTargetDefines(const LangOptions &Opts,
  1156. MacroBuilder &Builder) const {
  1157. Builder.defineMacro("__AARCH64EL__");
  1158. AArch64TargetInfo::getTargetDefines(Opts, Builder);
  1159. }
  1160. AArch64beTargetInfo::AArch64beTargetInfo(const llvm::Triple &Triple,
  1161. const TargetOptions &Opts)
  1162. : AArch64TargetInfo(Triple, Opts) {}
  1163. void AArch64beTargetInfo::getTargetDefines(const LangOptions &Opts,
  1164. MacroBuilder &Builder) const {
  1165. Builder.defineMacro("__AARCH64EB__");
  1166. Builder.defineMacro("__AARCH_BIG_ENDIAN");
  1167. Builder.defineMacro("__ARM_BIG_ENDIAN");
  1168. AArch64TargetInfo::getTargetDefines(Opts, Builder);
  1169. }
  1170. void AArch64beTargetInfo::setDataLayout() {
  1171. assert(!getTriple().isOSBinFormatMachO());
  1172. resetDataLayout("E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128");
  1173. }
  1174. WindowsARM64TargetInfo::WindowsARM64TargetInfo(const llvm::Triple &Triple,
  1175. const TargetOptions &Opts)
  1176. : WindowsTargetInfo<AArch64leTargetInfo>(Triple, Opts), Triple(Triple) {
  1177. // This is an LLP64 platform.
  1178. // int:4, long:4, long long:8, long double:8.
  1179. IntWidth = IntAlign = 32;
  1180. LongWidth = LongAlign = 32;
  1181. DoubleAlign = LongLongAlign = 64;
  1182. LongDoubleWidth = LongDoubleAlign = 64;
  1183. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  1184. IntMaxType = SignedLongLong;
  1185. Int64Type = SignedLongLong;
  1186. SizeType = UnsignedLongLong;
  1187. PtrDiffType = SignedLongLong;
  1188. IntPtrType = SignedLongLong;
  1189. }
  1190. void WindowsARM64TargetInfo::setDataLayout() {
  1191. resetDataLayout(Triple.isOSBinFormatMachO()
  1192. ? "e-m:o-i64:64-i128:128-n32:64-S128"
  1193. : "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128",
  1194. Triple.isOSBinFormatMachO() ? "_" : "");
  1195. }
  1196. TargetInfo::BuiltinVaListKind
  1197. WindowsARM64TargetInfo::getBuiltinVaListKind() const {
  1198. return TargetInfo::CharPtrBuiltinVaList;
  1199. }
  1200. TargetInfo::CallingConvCheckResult
  1201. WindowsARM64TargetInfo::checkCallingConvention(CallingConv CC) const {
  1202. switch (CC) {
  1203. case CC_X86StdCall:
  1204. case CC_X86ThisCall:
  1205. case CC_X86FastCall:
  1206. case CC_X86VectorCall:
  1207. return CCCR_Ignore;
  1208. case CC_C:
  1209. case CC_OpenCLKernel:
  1210. case CC_PreserveMost:
  1211. case CC_PreserveAll:
  1212. case CC_Swift:
  1213. case CC_SwiftAsync:
  1214. case CC_Win64:
  1215. return CCCR_OK;
  1216. default:
  1217. return CCCR_Warning;
  1218. }
  1219. }
  1220. MicrosoftARM64TargetInfo::MicrosoftARM64TargetInfo(const llvm::Triple &Triple,
  1221. const TargetOptions &Opts)
  1222. : WindowsARM64TargetInfo(Triple, Opts) {
  1223. TheCXXABI.set(TargetCXXABI::Microsoft);
  1224. }
  1225. void MicrosoftARM64TargetInfo::getTargetDefines(const LangOptions &Opts,
  1226. MacroBuilder &Builder) const {
  1227. WindowsARM64TargetInfo::getTargetDefines(Opts, Builder);
  1228. Builder.defineMacro("_M_ARM64", "1");
  1229. }
  1230. TargetInfo::CallingConvKind
  1231. MicrosoftARM64TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
  1232. return CCK_MicrosoftWin64;
  1233. }
  1234. unsigned MicrosoftARM64TargetInfo::getMinGlobalAlign(uint64_t TypeSize) const {
  1235. unsigned Align = WindowsARM64TargetInfo::getMinGlobalAlign(TypeSize);
  1236. // MSVC does size based alignment for arm64 based on alignment section in
  1237. // below document, replicate that to keep alignment consistent with object
  1238. // files compiled by MSVC.
  1239. // https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions
  1240. if (TypeSize >= 512) { // TypeSize >= 64 bytes
  1241. Align = std::max(Align, 128u); // align type at least 16 bytes
  1242. } else if (TypeSize >= 64) { // TypeSize >= 8 bytes
  1243. Align = std::max(Align, 64u); // align type at least 8 butes
  1244. } else if (TypeSize >= 16) { // TypeSize >= 2 bytes
  1245. Align = std::max(Align, 32u); // align type at least 4 bytes
  1246. }
  1247. return Align;
  1248. }
  1249. MinGWARM64TargetInfo::MinGWARM64TargetInfo(const llvm::Triple &Triple,
  1250. const TargetOptions &Opts)
  1251. : WindowsARM64TargetInfo(Triple, Opts) {
  1252. TheCXXABI.set(TargetCXXABI::GenericAArch64);
  1253. }
  1254. DarwinAArch64TargetInfo::DarwinAArch64TargetInfo(const llvm::Triple &Triple,
  1255. const TargetOptions &Opts)
  1256. : DarwinTargetInfo<AArch64leTargetInfo>(Triple, Opts) {
  1257. Int64Type = SignedLongLong;
  1258. if (getTriple().isArch32Bit())
  1259. IntMaxType = SignedLongLong;
  1260. WCharType = SignedInt;
  1261. UseSignedCharForObjCBool = false;
  1262. LongDoubleWidth = LongDoubleAlign = SuitableAlign = 64;
  1263. LongDoubleFormat = &llvm::APFloat::IEEEdouble();
  1264. UseZeroLengthBitfieldAlignment = false;
  1265. if (getTriple().isArch32Bit()) {
  1266. UseBitFieldTypeAlignment = false;
  1267. ZeroLengthBitfieldBoundary = 32;
  1268. UseZeroLengthBitfieldAlignment = true;
  1269. TheCXXABI.set(TargetCXXABI::WatchOS);
  1270. } else
  1271. TheCXXABI.set(TargetCXXABI::AppleARM64);
  1272. }
  1273. void DarwinAArch64TargetInfo::getOSDefines(const LangOptions &Opts,
  1274. const llvm::Triple &Triple,
  1275. MacroBuilder &Builder) const {
  1276. Builder.defineMacro("__AARCH64_SIMD__");
  1277. if (Triple.isArch32Bit())
  1278. Builder.defineMacro("__ARM64_ARCH_8_32__");
  1279. else
  1280. Builder.defineMacro("__ARM64_ARCH_8__");
  1281. Builder.defineMacro("__ARM_NEON__");
  1282. Builder.defineMacro("__LITTLE_ENDIAN__");
  1283. Builder.defineMacro("__REGISTER_PREFIX__", "");
  1284. Builder.defineMacro("__arm64", "1");
  1285. Builder.defineMacro("__arm64__", "1");
  1286. if (Triple.isArm64e())
  1287. Builder.defineMacro("__arm64e__", "1");
  1288. getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
  1289. }
  1290. TargetInfo::BuiltinVaListKind
  1291. DarwinAArch64TargetInfo::getBuiltinVaListKind() const {
  1292. return TargetInfo::CharPtrBuiltinVaList;
  1293. }
  1294. // 64-bit RenderScript is aarch64
  1295. RenderScript64TargetInfo::RenderScript64TargetInfo(const llvm::Triple &Triple,
  1296. const TargetOptions &Opts)
  1297. : AArch64leTargetInfo(llvm::Triple("aarch64", Triple.getVendorName(),
  1298. Triple.getOSName(),
  1299. Triple.getEnvironmentName()),
  1300. Opts) {
  1301. IsRenderScriptTarget = true;
  1302. }
  1303. void RenderScript64TargetInfo::getTargetDefines(const LangOptions &Opts,
  1304. MacroBuilder &Builder) const {
  1305. Builder.defineMacro("__RENDERSCRIPT__");
  1306. AArch64leTargetInfo::getTargetDefines(Opts, Builder);
  1307. }