ARM.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. //===--- ARM.cpp - Implement ARM 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 ARM TargetInfo objects.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARM.h"
  13. #include "clang/Basic/Builtins.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Basic/TargetBuiltins.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. using namespace clang;
  20. using namespace clang::targets;
  21. void ARMTargetInfo::setABIAAPCS() {
  22. IsAAPCS = true;
  23. DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 64;
  24. BFloat16Width = BFloat16Align = 16;
  25. BFloat16Format = &llvm::APFloat::BFloat();
  26. const llvm::Triple &T = getTriple();
  27. bool IsNetBSD = T.isOSNetBSD();
  28. bool IsOpenBSD = T.isOSOpenBSD();
  29. if (!T.isOSWindows() && !IsNetBSD && !IsOpenBSD)
  30. WCharType = UnsignedInt;
  31. UseBitFieldTypeAlignment = true;
  32. ZeroLengthBitfieldBoundary = 0;
  33. // Thumb1 add sp, #imm requires the immediate value be multiple of 4,
  34. // so set preferred for small types to 32.
  35. if (T.isOSBinFormatMachO()) {
  36. resetDataLayout(BigEndian
  37. ? "E-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
  38. : "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
  39. "_");
  40. } else if (T.isOSWindows()) {
  41. assert(!BigEndian && "Windows on ARM does not support big endian");
  42. resetDataLayout("e"
  43. "-m:w"
  44. "-p:32:32"
  45. "-Fi8"
  46. "-i64:64"
  47. "-v128:64:128"
  48. "-a:0:32"
  49. "-n32"
  50. "-S64");
  51. } else if (T.isOSNaCl()) {
  52. assert(!BigEndian && "NaCl on ARM does not support big endian");
  53. resetDataLayout("e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S128");
  54. } else {
  55. resetDataLayout(BigEndian
  56. ? "E-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
  57. : "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64");
  58. }
  59. // FIXME: Enumerated types are variable width in straight AAPCS.
  60. }
  61. void ARMTargetInfo::setABIAPCS(bool IsAAPCS16) {
  62. const llvm::Triple &T = getTriple();
  63. IsAAPCS = false;
  64. if (IsAAPCS16)
  65. DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 64;
  66. else
  67. DoubleAlign = LongLongAlign = LongDoubleAlign = SuitableAlign = 32;
  68. BFloat16Width = BFloat16Align = 16;
  69. BFloat16Format = &llvm::APFloat::BFloat();
  70. WCharType = SignedInt;
  71. // Do not respect the alignment of bit-field types when laying out
  72. // structures. This corresponds to PCC_BITFIELD_TYPE_MATTERS in gcc.
  73. UseBitFieldTypeAlignment = false;
  74. /// gcc forces the alignment to 4 bytes, regardless of the type of the
  75. /// zero length bitfield. This corresponds to EMPTY_FIELD_BOUNDARY in
  76. /// gcc.
  77. ZeroLengthBitfieldBoundary = 32;
  78. if (T.isOSBinFormatMachO() && IsAAPCS16) {
  79. assert(!BigEndian && "AAPCS16 does not support big-endian");
  80. resetDataLayout("e-m:o-p:32:32-Fi8-i64:64-a:0:32-n32-S128", "_");
  81. } else if (T.isOSBinFormatMachO())
  82. resetDataLayout(
  83. BigEndian
  84. ? "E-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
  85. : "e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32",
  86. "_");
  87. else
  88. resetDataLayout(
  89. BigEndian
  90. ? "E-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
  91. : "e-m:e-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32");
  92. // FIXME: Override "preferred align" for double and long long.
  93. }
  94. void ARMTargetInfo::setArchInfo() {
  95. StringRef ArchName = getTriple().getArchName();
  96. ArchISA = llvm::ARM::parseArchISA(ArchName);
  97. CPU = std::string(llvm::ARM::getDefaultCPU(ArchName));
  98. llvm::ARM::ArchKind AK = llvm::ARM::parseArch(ArchName);
  99. if (AK != llvm::ARM::ArchKind::INVALID)
  100. ArchKind = AK;
  101. setArchInfo(ArchKind);
  102. }
  103. void ARMTargetInfo::setArchInfo(llvm::ARM::ArchKind Kind) {
  104. StringRef SubArch;
  105. // cache TargetParser info
  106. ArchKind = Kind;
  107. SubArch = llvm::ARM::getSubArch(ArchKind);
  108. ArchProfile = llvm::ARM::parseArchProfile(SubArch);
  109. ArchVersion = llvm::ARM::parseArchVersion(SubArch);
  110. // cache CPU related strings
  111. CPUAttr = getCPUAttr();
  112. CPUProfile = getCPUProfile();
  113. }
  114. void ARMTargetInfo::setAtomic() {
  115. // when triple does not specify a sub arch,
  116. // then we are not using inline atomics
  117. bool ShouldUseInlineAtomic =
  118. (ArchISA == llvm::ARM::ISAKind::ARM && ArchVersion >= 6) ||
  119. (ArchISA == llvm::ARM::ISAKind::THUMB && ArchVersion >= 7);
  120. // Cortex M does not support 8 byte atomics, while general Thumb2 does.
  121. if (ArchProfile == llvm::ARM::ProfileKind::M) {
  122. MaxAtomicPromoteWidth = 32;
  123. if (ShouldUseInlineAtomic)
  124. MaxAtomicInlineWidth = 32;
  125. } else {
  126. MaxAtomicPromoteWidth = 64;
  127. if (ShouldUseInlineAtomic)
  128. MaxAtomicInlineWidth = 64;
  129. }
  130. }
  131. bool ARMTargetInfo::hasMVE() const {
  132. return ArchKind == llvm::ARM::ArchKind::ARMV8_1MMainline && MVE != 0;
  133. }
  134. bool ARMTargetInfo::hasMVEFloat() const {
  135. return hasMVE() && (MVE & MVE_FP);
  136. }
  137. bool ARMTargetInfo::hasCDE() const { return getARMCDECoprocMask() != 0; }
  138. bool ARMTargetInfo::isThumb() const {
  139. return ArchISA == llvm::ARM::ISAKind::THUMB;
  140. }
  141. bool ARMTargetInfo::supportsThumb() const {
  142. return CPUAttr.count('T') || ArchVersion >= 6;
  143. }
  144. bool ARMTargetInfo::supportsThumb2() const {
  145. return CPUAttr.equals("6T2") ||
  146. (ArchVersion >= 7 && !CPUAttr.equals("8M_BASE"));
  147. }
  148. StringRef ARMTargetInfo::getCPUAttr() const {
  149. // For most sub-arches, the build attribute CPU name is enough.
  150. // For Cortex variants, it's slightly different.
  151. switch (ArchKind) {
  152. default:
  153. return llvm::ARM::getCPUAttr(ArchKind);
  154. case llvm::ARM::ArchKind::ARMV6M:
  155. return "6M";
  156. case llvm::ARM::ArchKind::ARMV7S:
  157. return "7S";
  158. case llvm::ARM::ArchKind::ARMV7A:
  159. return "7A";
  160. case llvm::ARM::ArchKind::ARMV7R:
  161. return "7R";
  162. case llvm::ARM::ArchKind::ARMV7M:
  163. return "7M";
  164. case llvm::ARM::ArchKind::ARMV7EM:
  165. return "7EM";
  166. case llvm::ARM::ArchKind::ARMV7VE:
  167. return "7VE";
  168. case llvm::ARM::ArchKind::ARMV8A:
  169. return "8A";
  170. case llvm::ARM::ArchKind::ARMV8_1A:
  171. return "8_1A";
  172. case llvm::ARM::ArchKind::ARMV8_2A:
  173. return "8_2A";
  174. case llvm::ARM::ArchKind::ARMV8_3A:
  175. return "8_3A";
  176. case llvm::ARM::ArchKind::ARMV8_4A:
  177. return "8_4A";
  178. case llvm::ARM::ArchKind::ARMV8_5A:
  179. return "8_5A";
  180. case llvm::ARM::ArchKind::ARMV8_6A:
  181. return "8_6A";
  182. case llvm::ARM::ArchKind::ARMV8_7A:
  183. return "8_7A";
  184. case llvm::ARM::ArchKind::ARMV8_8A:
  185. return "8_8A";
  186. case llvm::ARM::ArchKind::ARMV9A:
  187. return "9A";
  188. case llvm::ARM::ArchKind::ARMV9_1A:
  189. return "9_1A";
  190. case llvm::ARM::ArchKind::ARMV9_2A:
  191. return "9_2A";
  192. case llvm::ARM::ArchKind::ARMV9_3A:
  193. return "9_3A";
  194. case llvm::ARM::ArchKind::ARMV8MBaseline:
  195. return "8M_BASE";
  196. case llvm::ARM::ArchKind::ARMV8MMainline:
  197. return "8M_MAIN";
  198. case llvm::ARM::ArchKind::ARMV8R:
  199. return "8R";
  200. case llvm::ARM::ArchKind::ARMV8_1MMainline:
  201. return "8_1M_MAIN";
  202. }
  203. }
  204. StringRef ARMTargetInfo::getCPUProfile() const {
  205. switch (ArchProfile) {
  206. case llvm::ARM::ProfileKind::A:
  207. return "A";
  208. case llvm::ARM::ProfileKind::R:
  209. return "R";
  210. case llvm::ARM::ProfileKind::M:
  211. return "M";
  212. default:
  213. return "";
  214. }
  215. }
  216. ARMTargetInfo::ARMTargetInfo(const llvm::Triple &Triple,
  217. const TargetOptions &Opts)
  218. : TargetInfo(Triple), FPMath(FP_Default), IsAAPCS(true), LDREX(0),
  219. HW_FP(0) {
  220. bool IsOpenBSD = Triple.isOSOpenBSD();
  221. bool IsNetBSD = Triple.isOSNetBSD();
  222. // FIXME: the isOSBinFormatMachO is a workaround for identifying a Darwin-like
  223. // environment where size_t is `unsigned long` rather than `unsigned int`
  224. PtrDiffType = IntPtrType =
  225. (Triple.isOSDarwin() || Triple.isOSBinFormatMachO() || IsOpenBSD ||
  226. IsNetBSD)
  227. ? SignedLong
  228. : SignedInt;
  229. SizeType = (Triple.isOSDarwin() || Triple.isOSBinFormatMachO() || IsOpenBSD ||
  230. IsNetBSD)
  231. ? UnsignedLong
  232. : UnsignedInt;
  233. // ptrdiff_t is inconsistent on Darwin
  234. if ((Triple.isOSDarwin() || Triple.isOSBinFormatMachO()) &&
  235. !Triple.isWatchABI())
  236. PtrDiffType = SignedInt;
  237. // Cache arch related info.
  238. setArchInfo();
  239. // {} in inline assembly are neon specifiers, not assembly variant
  240. // specifiers.
  241. NoAsmVariants = true;
  242. // FIXME: This duplicates code from the driver that sets the -target-abi
  243. // option - this code is used if -target-abi isn't passed and should
  244. // be unified in some way.
  245. if (Triple.isOSBinFormatMachO()) {
  246. // The backend is hardwired to assume AAPCS for M-class processors, ensure
  247. // the frontend matches that.
  248. if (Triple.getEnvironment() == llvm::Triple::EABI ||
  249. Triple.getOS() == llvm::Triple::UnknownOS ||
  250. ArchProfile == llvm::ARM::ProfileKind::M) {
  251. setABI("aapcs");
  252. } else if (Triple.isWatchABI()) {
  253. setABI("aapcs16");
  254. } else {
  255. setABI("apcs-gnu");
  256. }
  257. } else if (Triple.isOSWindows()) {
  258. // FIXME: this is invalid for WindowsCE
  259. setABI("aapcs");
  260. } else {
  261. // Select the default based on the platform.
  262. switch (Triple.getEnvironment()) {
  263. case llvm::Triple::Android:
  264. case llvm::Triple::GNUEABI:
  265. case llvm::Triple::GNUEABIHF:
  266. case llvm::Triple::MuslEABI:
  267. case llvm::Triple::MuslEABIHF:
  268. setABI("aapcs-linux");
  269. break;
  270. case llvm::Triple::EABIHF:
  271. case llvm::Triple::EABI:
  272. setABI("aapcs");
  273. break;
  274. case llvm::Triple::GNU:
  275. setABI("apcs-gnu");
  276. break;
  277. default:
  278. if (IsNetBSD)
  279. setABI("apcs-gnu");
  280. else if (IsOpenBSD)
  281. setABI("aapcs-linux");
  282. else
  283. setABI("aapcs");
  284. break;
  285. }
  286. }
  287. // ARM targets default to using the ARM C++ ABI.
  288. TheCXXABI.set(TargetCXXABI::GenericARM);
  289. // ARM has atomics up to 8 bytes
  290. setAtomic();
  291. // Maximum alignment for ARM NEON data types should be 64-bits (AAPCS)
  292. // as well the default alignment
  293. if (IsAAPCS && !Triple.isAndroid())
  294. DefaultAlignForAttributeAligned = MaxVectorAlign = 64;
  295. // Do force alignment of members that follow zero length bitfields. If
  296. // the alignment of the zero-length bitfield is greater than the member
  297. // that follows it, `bar', `bar' will be aligned as the type of the
  298. // zero length bitfield.
  299. UseZeroLengthBitfieldAlignment = true;
  300. if (Triple.getOS() == llvm::Triple::Linux ||
  301. Triple.getOS() == llvm::Triple::UnknownOS)
  302. this->MCountName = Opts.EABIVersion == llvm::EABI::GNU
  303. ? "llvm.arm.gnu.eabi.mcount"
  304. : "\01mcount";
  305. SoftFloatABI = llvm::is_contained(Opts.FeaturesAsWritten, "+soft-float-abi");
  306. }
  307. StringRef ARMTargetInfo::getABI() const { return ABI; }
  308. bool ARMTargetInfo::setABI(const std::string &Name) {
  309. ABI = Name;
  310. // The defaults (above) are for AAPCS, check if we need to change them.
  311. //
  312. // FIXME: We need support for -meabi... we could just mangle it into the
  313. // name.
  314. if (Name == "apcs-gnu" || Name == "aapcs16") {
  315. setABIAPCS(Name == "aapcs16");
  316. return true;
  317. }
  318. if (Name == "aapcs" || Name == "aapcs-vfp" || Name == "aapcs-linux") {
  319. setABIAAPCS();
  320. return true;
  321. }
  322. return false;
  323. }
  324. bool ARMTargetInfo::isBranchProtectionSupportedArch(StringRef Arch) const {
  325. llvm::ARM::ArchKind CPUArch = llvm::ARM::parseCPUArch(Arch);
  326. if (CPUArch == llvm::ARM::ArchKind::INVALID)
  327. CPUArch = llvm::ARM::parseArch(getTriple().getArchName());
  328. if (CPUArch == llvm::ARM::ArchKind::INVALID)
  329. return false;
  330. StringRef ArchFeature = llvm::ARM::getArchName(CPUArch);
  331. auto a =
  332. llvm::Triple(ArchFeature, getTriple().getVendorName(),
  333. getTriple().getOSName(), getTriple().getEnvironmentName());
  334. StringRef SubArch = llvm::ARM::getSubArch(CPUArch);
  335. llvm::ARM::ProfileKind Profile = llvm::ARM::parseArchProfile(SubArch);
  336. return a.isArmT32() && (Profile == llvm::ARM::ProfileKind::M);
  337. }
  338. bool ARMTargetInfo::validateBranchProtection(StringRef Spec, StringRef Arch,
  339. BranchProtectionInfo &BPI,
  340. StringRef &Err) const {
  341. llvm::ARM::ParsedBranchProtection PBP;
  342. if (!llvm::ARM::parseBranchProtection(Spec, PBP, Err))
  343. return false;
  344. if (!isBranchProtectionSupportedArch(Arch))
  345. return false;
  346. BPI.SignReturnAddr =
  347. llvm::StringSwitch<LangOptions::SignReturnAddressScopeKind>(PBP.Scope)
  348. .Case("non-leaf", LangOptions::SignReturnAddressScopeKind::NonLeaf)
  349. .Case("all", LangOptions::SignReturnAddressScopeKind::All)
  350. .Default(LangOptions::SignReturnAddressScopeKind::None);
  351. // Don't care for the sign key, beyond issuing a warning.
  352. if (PBP.Key == "b_key")
  353. Err = "b-key";
  354. BPI.SignKey = LangOptions::SignReturnAddressKeyKind::AKey;
  355. BPI.BranchTargetEnforcement = PBP.BranchTargetEnforcement;
  356. return true;
  357. }
  358. // FIXME: This should be based on Arch attributes, not CPU names.
  359. bool ARMTargetInfo::initFeatureMap(
  360. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  361. const std::vector<std::string> &FeaturesVec) const {
  362. std::string ArchFeature;
  363. std::vector<StringRef> TargetFeatures;
  364. llvm::ARM::ArchKind Arch = llvm::ARM::parseArch(getTriple().getArchName());
  365. // Map the base architecture to an appropriate target feature, so we don't
  366. // rely on the target triple.
  367. llvm::ARM::ArchKind CPUArch = llvm::ARM::parseCPUArch(CPU);
  368. if (CPUArch == llvm::ARM::ArchKind::INVALID)
  369. CPUArch = Arch;
  370. if (CPUArch != llvm::ARM::ArchKind::INVALID) {
  371. ArchFeature = ("+" + llvm::ARM::getArchName(CPUArch)).str();
  372. TargetFeatures.push_back(ArchFeature);
  373. }
  374. // get default FPU features
  375. unsigned FPUKind = llvm::ARM::getDefaultFPU(CPU, Arch);
  376. llvm::ARM::getFPUFeatures(FPUKind, TargetFeatures);
  377. // get default Extension features
  378. uint64_t Extensions = llvm::ARM::getDefaultExtensions(CPU, Arch);
  379. llvm::ARM::getExtensionFeatures(Extensions, TargetFeatures);
  380. for (auto Feature : TargetFeatures)
  381. if (Feature[0] == '+')
  382. Features[Feature.drop_front(1)] = true;
  383. // Enable or disable thumb-mode explicitly per function to enable mixed
  384. // ARM and Thumb code generation.
  385. if (isThumb())
  386. Features["thumb-mode"] = true;
  387. else
  388. Features["thumb-mode"] = false;
  389. // Convert user-provided arm and thumb GNU target attributes to
  390. // [-|+]thumb-mode target features respectively.
  391. std::vector<std::string> UpdatedFeaturesVec;
  392. for (const auto &Feature : FeaturesVec) {
  393. // Skip soft-float-abi; it's something we only use to initialize a bit of
  394. // class state, and is otherwise unrecognized.
  395. if (Feature == "+soft-float-abi")
  396. continue;
  397. StringRef FixedFeature;
  398. if (Feature == "+arm")
  399. FixedFeature = "-thumb-mode";
  400. else if (Feature == "+thumb")
  401. FixedFeature = "+thumb-mode";
  402. else
  403. FixedFeature = Feature;
  404. UpdatedFeaturesVec.push_back(FixedFeature.str());
  405. }
  406. return TargetInfo::initFeatureMap(Features, Diags, CPU, UpdatedFeaturesVec);
  407. }
  408. bool ARMTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  409. DiagnosticsEngine &Diags) {
  410. FPU = 0;
  411. MVE = 0;
  412. CRC = 0;
  413. Crypto = 0;
  414. SHA2 = 0;
  415. AES = 0;
  416. DSP = 0;
  417. Unaligned = 1;
  418. SoftFloat = false;
  419. // Note that SoftFloatABI is initialized in our constructor.
  420. HWDiv = 0;
  421. DotProd = 0;
  422. HasMatMul = 0;
  423. HasPAC = 0;
  424. HasBTI = 0;
  425. HasFloat16 = true;
  426. ARMCDECoprocMask = 0;
  427. HasBFloat16 = false;
  428. FPRegsDisabled = false;
  429. // This does not diagnose illegal cases like having both
  430. // "+vfpv2" and "+vfpv3" or having "+neon" and "-fp64".
  431. for (const auto &Feature : Features) {
  432. if (Feature == "+soft-float") {
  433. SoftFloat = true;
  434. } else if (Feature == "+vfp2sp" || Feature == "+vfp2") {
  435. FPU |= VFP2FPU;
  436. HW_FP |= HW_FP_SP;
  437. if (Feature == "+vfp2")
  438. HW_FP |= HW_FP_DP;
  439. } else if (Feature == "+vfp3sp" || Feature == "+vfp3d16sp" ||
  440. Feature == "+vfp3" || Feature == "+vfp3d16") {
  441. FPU |= VFP3FPU;
  442. HW_FP |= HW_FP_SP;
  443. if (Feature == "+vfp3" || Feature == "+vfp3d16")
  444. HW_FP |= HW_FP_DP;
  445. } else if (Feature == "+vfp4sp" || Feature == "+vfp4d16sp" ||
  446. Feature == "+vfp4" || Feature == "+vfp4d16") {
  447. FPU |= VFP4FPU;
  448. HW_FP |= HW_FP_SP | HW_FP_HP;
  449. if (Feature == "+vfp4" || Feature == "+vfp4d16")
  450. HW_FP |= HW_FP_DP;
  451. } else if (Feature == "+fp-armv8sp" || Feature == "+fp-armv8d16sp" ||
  452. Feature == "+fp-armv8" || Feature == "+fp-armv8d16") {
  453. FPU |= FPARMV8;
  454. HW_FP |= HW_FP_SP | HW_FP_HP;
  455. if (Feature == "+fp-armv8" || Feature == "+fp-armv8d16")
  456. HW_FP |= HW_FP_DP;
  457. } else if (Feature == "+neon") {
  458. FPU |= NeonFPU;
  459. HW_FP |= HW_FP_SP;
  460. } else if (Feature == "+hwdiv") {
  461. HWDiv |= HWDivThumb;
  462. } else if (Feature == "+hwdiv-arm") {
  463. HWDiv |= HWDivARM;
  464. } else if (Feature == "+crc") {
  465. CRC = 1;
  466. } else if (Feature == "+crypto") {
  467. Crypto = 1;
  468. } else if (Feature == "+sha2") {
  469. SHA2 = 1;
  470. } else if (Feature == "+aes") {
  471. AES = 1;
  472. } else if (Feature == "+dsp") {
  473. DSP = 1;
  474. } else if (Feature == "+fp64") {
  475. HW_FP |= HW_FP_DP;
  476. } else if (Feature == "+8msecext") {
  477. if (CPUProfile != "M" || ArchVersion != 8) {
  478. Diags.Report(diag::err_target_unsupported_mcmse) << CPU;
  479. return false;
  480. }
  481. } else if (Feature == "+strict-align") {
  482. Unaligned = 0;
  483. } else if (Feature == "+fp16") {
  484. HW_FP |= HW_FP_HP;
  485. } else if (Feature == "+fullfp16") {
  486. HasLegalHalfType = true;
  487. } else if (Feature == "+dotprod") {
  488. DotProd = true;
  489. } else if (Feature == "+mve") {
  490. MVE |= MVE_INT;
  491. } else if (Feature == "+mve.fp") {
  492. HasLegalHalfType = true;
  493. FPU |= FPARMV8;
  494. MVE |= MVE_INT | MVE_FP;
  495. HW_FP |= HW_FP_SP | HW_FP_HP;
  496. } else if (Feature == "+i8mm") {
  497. HasMatMul = 1;
  498. } else if (Feature.size() == strlen("+cdecp0") && Feature >= "+cdecp0" &&
  499. Feature <= "+cdecp7") {
  500. unsigned Coproc = Feature.back() - '0';
  501. ARMCDECoprocMask |= (1U << Coproc);
  502. } else if (Feature == "+bf16") {
  503. HasBFloat16 = true;
  504. } else if (Feature == "-fpregs") {
  505. FPRegsDisabled = true;
  506. } else if (Feature == "+pacbti") {
  507. HasPAC = 1;
  508. HasBTI = 1;
  509. }
  510. }
  511. switch (ArchVersion) {
  512. case 6:
  513. if (ArchProfile == llvm::ARM::ProfileKind::M)
  514. LDREX = 0;
  515. else if (ArchKind == llvm::ARM::ArchKind::ARMV6K)
  516. LDREX = LDREX_D | LDREX_W | LDREX_H | LDREX_B;
  517. else
  518. LDREX = LDREX_W;
  519. break;
  520. case 7:
  521. if (ArchProfile == llvm::ARM::ProfileKind::M)
  522. LDREX = LDREX_W | LDREX_H | LDREX_B;
  523. else
  524. LDREX = LDREX_D | LDREX_W | LDREX_H | LDREX_B;
  525. break;
  526. case 8:
  527. case 9:
  528. LDREX = LDREX_D | LDREX_W | LDREX_H | LDREX_B;
  529. }
  530. if (!(FPU & NeonFPU) && FPMath == FP_Neon) {
  531. Diags.Report(diag::err_target_unsupported_fpmath) << "neon";
  532. return false;
  533. }
  534. if (FPMath == FP_Neon)
  535. Features.push_back("+neonfp");
  536. else if (FPMath == FP_VFP)
  537. Features.push_back("-neonfp");
  538. return true;
  539. }
  540. bool ARMTargetInfo::hasFeature(StringRef Feature) const {
  541. return llvm::StringSwitch<bool>(Feature)
  542. .Case("arm", true)
  543. .Case("aarch32", true)
  544. .Case("softfloat", SoftFloat)
  545. .Case("thumb", isThumb())
  546. .Case("neon", (FPU & NeonFPU) && !SoftFloat)
  547. .Case("vfp", FPU && !SoftFloat)
  548. .Case("hwdiv", HWDiv & HWDivThumb)
  549. .Case("hwdiv-arm", HWDiv & HWDivARM)
  550. .Case("mve", hasMVE())
  551. .Default(false);
  552. }
  553. bool ARMTargetInfo::hasBFloat16Type() const {
  554. return HasBFloat16 && !SoftFloat;
  555. }
  556. bool ARMTargetInfo::isValidCPUName(StringRef Name) const {
  557. return Name == "generic" ||
  558. llvm::ARM::parseCPUArch(Name) != llvm::ARM::ArchKind::INVALID;
  559. }
  560. void ARMTargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
  561. llvm::ARM::fillValidCPUArchList(Values);
  562. }
  563. bool ARMTargetInfo::setCPU(const std::string &Name) {
  564. if (Name != "generic")
  565. setArchInfo(llvm::ARM::parseCPUArch(Name));
  566. if (ArchKind == llvm::ARM::ArchKind::INVALID)
  567. return false;
  568. setAtomic();
  569. CPU = Name;
  570. return true;
  571. }
  572. bool ARMTargetInfo::setFPMath(StringRef Name) {
  573. if (Name == "neon") {
  574. FPMath = FP_Neon;
  575. return true;
  576. } else if (Name == "vfp" || Name == "vfp2" || Name == "vfp3" ||
  577. Name == "vfp4") {
  578. FPMath = FP_VFP;
  579. return true;
  580. }
  581. return false;
  582. }
  583. void ARMTargetInfo::getTargetDefinesARMV81A(const LangOptions &Opts,
  584. MacroBuilder &Builder) const {
  585. Builder.defineMacro("__ARM_FEATURE_QRDMX", "1");
  586. }
  587. void ARMTargetInfo::getTargetDefinesARMV82A(const LangOptions &Opts,
  588. MacroBuilder &Builder) const {
  589. // Also include the ARMv8.1-A defines
  590. getTargetDefinesARMV81A(Opts, Builder);
  591. }
  592. void ARMTargetInfo::getTargetDefinesARMV83A(const LangOptions &Opts,
  593. MacroBuilder &Builder) const {
  594. // Also include the ARMv8.2-A defines
  595. Builder.defineMacro("__ARM_FEATURE_COMPLEX", "1");
  596. getTargetDefinesARMV82A(Opts, Builder);
  597. }
  598. void ARMTargetInfo::getTargetDefines(const LangOptions &Opts,
  599. MacroBuilder &Builder) const {
  600. // Target identification.
  601. Builder.defineMacro("__arm");
  602. Builder.defineMacro("__arm__");
  603. // For bare-metal none-eabi.
  604. if (getTriple().getOS() == llvm::Triple::UnknownOS &&
  605. (getTriple().getEnvironment() == llvm::Triple::EABI ||
  606. getTriple().getEnvironment() == llvm::Triple::EABIHF))
  607. Builder.defineMacro("__ELF__");
  608. // Target properties.
  609. Builder.defineMacro("__REGISTER_PREFIX__", "");
  610. // Unfortunately, __ARM_ARCH_7K__ is now more of an ABI descriptor. The CPU
  611. // happens to be Cortex-A7 though, so it should still get __ARM_ARCH_7A__.
  612. if (getTriple().isWatchABI())
  613. Builder.defineMacro("__ARM_ARCH_7K__", "2");
  614. if (!CPUAttr.empty())
  615. Builder.defineMacro("__ARM_ARCH_" + CPUAttr + "__");
  616. // ACLE 6.4.1 ARM/Thumb instruction set architecture
  617. // __ARM_ARCH is defined as an integer value indicating the current ARM ISA
  618. Builder.defineMacro("__ARM_ARCH", Twine(ArchVersion));
  619. if (ArchVersion >= 8) {
  620. // ACLE 6.5.7 Crypto Extension
  621. // The __ARM_FEATURE_CRYPTO is deprecated in favor of finer grained
  622. // feature macros for AES and SHA2
  623. if (SHA2 && AES)
  624. Builder.defineMacro("__ARM_FEATURE_CRYPTO", "1");
  625. if (SHA2)
  626. Builder.defineMacro("__ARM_FEATURE_SHA2", "1");
  627. if (AES)
  628. Builder.defineMacro("__ARM_FEATURE_AES", "1");
  629. // ACLE 6.5.8 CRC32 Extension
  630. if (CRC)
  631. Builder.defineMacro("__ARM_FEATURE_CRC32", "1");
  632. // ACLE 6.5.10 Numeric Maximum and Minimum
  633. Builder.defineMacro("__ARM_FEATURE_NUMERIC_MAXMIN", "1");
  634. // ACLE 6.5.9 Directed Rounding
  635. Builder.defineMacro("__ARM_FEATURE_DIRECTED_ROUNDING", "1");
  636. }
  637. // __ARM_ARCH_ISA_ARM is defined to 1 if the core supports the ARM ISA. It
  638. // is not defined for the M-profile.
  639. // NOTE that the default profile is assumed to be 'A'
  640. if (CPUProfile.empty() || ArchProfile != llvm::ARM::ProfileKind::M)
  641. Builder.defineMacro("__ARM_ARCH_ISA_ARM", "1");
  642. // __ARM_ARCH_ISA_THUMB is defined to 1 if the core supports the original
  643. // Thumb ISA (including v6-M and v8-M Baseline). It is set to 2 if the
  644. // core supports the Thumb-2 ISA as found in the v6T2 architecture and all
  645. // v7 and v8 architectures excluding v8-M Baseline.
  646. if (supportsThumb2())
  647. Builder.defineMacro("__ARM_ARCH_ISA_THUMB", "2");
  648. else if (supportsThumb())
  649. Builder.defineMacro("__ARM_ARCH_ISA_THUMB", "1");
  650. // __ARM_32BIT_STATE is defined to 1 if code is being generated for a 32-bit
  651. // instruction set such as ARM or Thumb.
  652. Builder.defineMacro("__ARM_32BIT_STATE", "1");
  653. // ACLE 6.4.2 Architectural Profile (A, R, M or pre-Cortex)
  654. // __ARM_ARCH_PROFILE is defined as 'A', 'R', 'M' or 'S', or unset.
  655. if (!CPUProfile.empty())
  656. Builder.defineMacro("__ARM_ARCH_PROFILE", "'" + CPUProfile + "'");
  657. // ACLE 6.4.3 Unaligned access supported in hardware
  658. if (Unaligned)
  659. Builder.defineMacro("__ARM_FEATURE_UNALIGNED", "1");
  660. // ACLE 6.4.4 LDREX/STREX
  661. if (LDREX)
  662. Builder.defineMacro("__ARM_FEATURE_LDREX", "0x" + Twine::utohexstr(LDREX));
  663. // ACLE 6.4.5 CLZ
  664. if (ArchVersion == 5 || (ArchVersion == 6 && CPUProfile != "M") ||
  665. ArchVersion > 6)
  666. Builder.defineMacro("__ARM_FEATURE_CLZ", "1");
  667. // ACLE 6.5.1 Hardware Floating Point
  668. if (HW_FP)
  669. Builder.defineMacro("__ARM_FP", "0x" + Twine::utohexstr(HW_FP));
  670. // ACLE predefines.
  671. Builder.defineMacro("__ARM_ACLE", "200");
  672. // FP16 support (we currently only support IEEE format).
  673. Builder.defineMacro("__ARM_FP16_FORMAT_IEEE", "1");
  674. Builder.defineMacro("__ARM_FP16_ARGS", "1");
  675. // ACLE 6.5.3 Fused multiply-accumulate (FMA)
  676. if (ArchVersion >= 7 && (FPU & VFP4FPU))
  677. Builder.defineMacro("__ARM_FEATURE_FMA", "1");
  678. // Subtarget options.
  679. // FIXME: It's more complicated than this and we don't really support
  680. // interworking.
  681. // Windows on ARM does not "support" interworking
  682. if (5 <= ArchVersion && ArchVersion <= 8 && !getTriple().isOSWindows())
  683. Builder.defineMacro("__THUMB_INTERWORK__");
  684. if (ABI == "aapcs" || ABI == "aapcs-linux" || ABI == "aapcs-vfp") {
  685. // Embedded targets on Darwin follow AAPCS, but not EABI.
  686. // Windows on ARM follows AAPCS VFP, but does not conform to EABI.
  687. if (!getTriple().isOSBinFormatMachO() && !getTriple().isOSWindows())
  688. Builder.defineMacro("__ARM_EABI__");
  689. Builder.defineMacro("__ARM_PCS", "1");
  690. }
  691. if ((!SoftFloat && !SoftFloatABI) || ABI == "aapcs-vfp" || ABI == "aapcs16")
  692. Builder.defineMacro("__ARM_PCS_VFP", "1");
  693. if (SoftFloat)
  694. Builder.defineMacro("__SOFTFP__");
  695. // ACLE position independent code macros.
  696. if (Opts.ROPI)
  697. Builder.defineMacro("__ARM_ROPI", "1");
  698. if (Opts.RWPI)
  699. Builder.defineMacro("__ARM_RWPI", "1");
  700. if (ArchKind == llvm::ARM::ArchKind::XSCALE)
  701. Builder.defineMacro("__XSCALE__");
  702. if (isThumb()) {
  703. Builder.defineMacro("__THUMBEL__");
  704. Builder.defineMacro("__thumb__");
  705. if (supportsThumb2())
  706. Builder.defineMacro("__thumb2__");
  707. }
  708. // ACLE 6.4.9 32-bit SIMD instructions
  709. if ((CPUProfile != "M" && ArchVersion >= 6) || (CPUProfile == "M" && DSP))
  710. Builder.defineMacro("__ARM_FEATURE_SIMD32", "1");
  711. // ACLE 6.4.10 Hardware Integer Divide
  712. if (((HWDiv & HWDivThumb) && isThumb()) ||
  713. ((HWDiv & HWDivARM) && !isThumb())) {
  714. Builder.defineMacro("__ARM_FEATURE_IDIV", "1");
  715. Builder.defineMacro("__ARM_ARCH_EXT_IDIV__", "1");
  716. }
  717. // Note, this is always on in gcc, even though it doesn't make sense.
  718. Builder.defineMacro("__APCS_32__");
  719. // __VFP_FP__ means that the floating-point format is VFP, not that a hardware
  720. // FPU is present. Moreover, the VFP format is the only one supported by
  721. // clang. For these reasons, this macro is always defined.
  722. Builder.defineMacro("__VFP_FP__");
  723. if (FPUModeIsVFP((FPUMode)FPU)) {
  724. if (FPU & VFP2FPU)
  725. Builder.defineMacro("__ARM_VFPV2__");
  726. if (FPU & VFP3FPU)
  727. Builder.defineMacro("__ARM_VFPV3__");
  728. if (FPU & VFP4FPU)
  729. Builder.defineMacro("__ARM_VFPV4__");
  730. if (FPU & FPARMV8)
  731. Builder.defineMacro("__ARM_FPV5__");
  732. }
  733. // This only gets set when Neon instructions are actually available, unlike
  734. // the VFP define, hence the soft float and arch check. This is subtly
  735. // different from gcc, we follow the intent which was that it should be set
  736. // when Neon instructions are actually available.
  737. if ((FPU & NeonFPU) && !SoftFloat && ArchVersion >= 7) {
  738. Builder.defineMacro("__ARM_NEON", "1");
  739. Builder.defineMacro("__ARM_NEON__");
  740. // current AArch32 NEON implementations do not support double-precision
  741. // floating-point even when it is present in VFP.
  742. Builder.defineMacro("__ARM_NEON_FP",
  743. "0x" + Twine::utohexstr(HW_FP & ~HW_FP_DP));
  744. }
  745. if (hasMVE()) {
  746. Builder.defineMacro("__ARM_FEATURE_MVE", hasMVEFloat() ? "3" : "1");
  747. }
  748. if (hasCDE()) {
  749. Builder.defineMacro("__ARM_FEATURE_CDE", "1");
  750. Builder.defineMacro("__ARM_FEATURE_CDE_COPROC",
  751. "0x" + Twine::utohexstr(getARMCDECoprocMask()));
  752. }
  753. Builder.defineMacro("__ARM_SIZEOF_WCHAR_T",
  754. Twine(Opts.WCharSize ? Opts.WCharSize : 4));
  755. Builder.defineMacro("__ARM_SIZEOF_MINIMAL_ENUM", Opts.ShortEnums ? "1" : "4");
  756. // CMSE
  757. if (ArchVersion == 8 && ArchProfile == llvm::ARM::ProfileKind::M)
  758. Builder.defineMacro("__ARM_FEATURE_CMSE", Opts.Cmse ? "3" : "1");
  759. if (ArchVersion >= 6 && CPUAttr != "6M" && CPUAttr != "8M_BASE") {
  760. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  761. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  762. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  763. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  764. }
  765. // ACLE 6.4.7 DSP instructions
  766. if (DSP) {
  767. Builder.defineMacro("__ARM_FEATURE_DSP", "1");
  768. }
  769. // ACLE 6.4.8 Saturation instructions
  770. bool SAT = false;
  771. if ((ArchVersion == 6 && CPUProfile != "M") || ArchVersion > 6) {
  772. Builder.defineMacro("__ARM_FEATURE_SAT", "1");
  773. SAT = true;
  774. }
  775. // ACLE 6.4.6 Q (saturation) flag
  776. if (DSP || SAT)
  777. Builder.defineMacro("__ARM_FEATURE_QBIT", "1");
  778. if (Opts.UnsafeFPMath)
  779. Builder.defineMacro("__ARM_FP_FAST", "1");
  780. // Armv8.2-A FP16 vector intrinsic
  781. if ((FPU & NeonFPU) && HasLegalHalfType)
  782. Builder.defineMacro("__ARM_FEATURE_FP16_VECTOR_ARITHMETIC", "1");
  783. // Armv8.2-A FP16 scalar intrinsics
  784. if (HasLegalHalfType)
  785. Builder.defineMacro("__ARM_FEATURE_FP16_SCALAR_ARITHMETIC", "1");
  786. // Armv8.2-A dot product intrinsics
  787. if (DotProd)
  788. Builder.defineMacro("__ARM_FEATURE_DOTPROD", "1");
  789. if (HasMatMul)
  790. Builder.defineMacro("__ARM_FEATURE_MATMUL_INT8", "1");
  791. if (HasPAC)
  792. Builder.defineMacro("__ARM_FEATURE_PAUTH", "1");
  793. if (HasBTI)
  794. Builder.defineMacro("__ARM_FEATURE_BTI", "1");
  795. if (HasBFloat16) {
  796. Builder.defineMacro("__ARM_FEATURE_BF16", "1");
  797. Builder.defineMacro("__ARM_FEATURE_BF16_VECTOR_ARITHMETIC", "1");
  798. Builder.defineMacro("__ARM_BF16_FORMAT_ALTERNATIVE", "1");
  799. }
  800. if (Opts.BranchTargetEnforcement)
  801. Builder.defineMacro("__ARM_FEATURE_BTI_DEFAULT", "1");
  802. if (Opts.hasSignReturnAddress()) {
  803. unsigned Value = 1;
  804. if (Opts.isSignReturnAddressScopeAll())
  805. Value |= 1 << 2;
  806. Builder.defineMacro("__ARM_FEATURE_PAC_DEFAULT", Twine(Value));
  807. }
  808. switch (ArchKind) {
  809. default:
  810. break;
  811. case llvm::ARM::ArchKind::ARMV8_1A:
  812. getTargetDefinesARMV81A(Opts, Builder);
  813. break;
  814. case llvm::ARM::ArchKind::ARMV8_2A:
  815. getTargetDefinesARMV82A(Opts, Builder);
  816. break;
  817. case llvm::ARM::ArchKind::ARMV8_3A:
  818. case llvm::ARM::ArchKind::ARMV8_4A:
  819. case llvm::ARM::ArchKind::ARMV8_5A:
  820. case llvm::ARM::ArchKind::ARMV8_6A:
  821. case llvm::ARM::ArchKind::ARMV8_8A:
  822. case llvm::ARM::ArchKind::ARMV9A:
  823. case llvm::ARM::ArchKind::ARMV9_1A:
  824. case llvm::ARM::ArchKind::ARMV9_2A:
  825. case llvm::ARM::ArchKind::ARMV9_3A:
  826. getTargetDefinesARMV83A(Opts, Builder);
  827. break;
  828. }
  829. }
  830. const Builtin::Info ARMTargetInfo::BuiltinInfo[] = {
  831. #define BUILTIN(ID, TYPE, ATTRS) \
  832. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  833. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
  834. {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
  835. #include "clang/Basic/BuiltinsNEON.def"
  836. #define BUILTIN(ID, TYPE, ATTRS) \
  837. {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
  838. #define LANGBUILTIN(ID, TYPE, ATTRS, LANG) \
  839. {#ID, TYPE, ATTRS, nullptr, LANG, nullptr},
  840. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
  841. {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
  842. #define TARGET_HEADER_BUILTIN(ID, TYPE, ATTRS, HEADER, LANGS, FEATURE) \
  843. {#ID, TYPE, ATTRS, HEADER, LANGS, FEATURE},
  844. #include "clang/Basic/BuiltinsARM.def"
  845. };
  846. ArrayRef<Builtin::Info> ARMTargetInfo::getTargetBuiltins() const {
  847. return llvm::makeArrayRef(BuiltinInfo, clang::ARM::LastTSBuiltin -
  848. Builtin::FirstTSBuiltin);
  849. }
  850. bool ARMTargetInfo::isCLZForZeroUndef() const { return false; }
  851. TargetInfo::BuiltinVaListKind ARMTargetInfo::getBuiltinVaListKind() const {
  852. return IsAAPCS
  853. ? AAPCSABIBuiltinVaList
  854. : (getTriple().isWatchABI() ? TargetInfo::CharPtrBuiltinVaList
  855. : TargetInfo::VoidPtrBuiltinVaList);
  856. }
  857. const char *const ARMTargetInfo::GCCRegNames[] = {
  858. // Integer registers
  859. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
  860. "r12", "sp", "lr", "pc",
  861. // Float registers
  862. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11",
  863. "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20", "s21", "s22",
  864. "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
  865. // Double registers
  866. "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "d11",
  867. "d12", "d13", "d14", "d15", "d16", "d17", "d18", "d19", "d20", "d21", "d22",
  868. "d23", "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
  869. // Quad registers
  870. "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
  871. "q12", "q13", "q14", "q15"};
  872. ArrayRef<const char *> ARMTargetInfo::getGCCRegNames() const {
  873. return llvm::makeArrayRef(GCCRegNames);
  874. }
  875. const TargetInfo::GCCRegAlias ARMTargetInfo::GCCRegAliases[] = {
  876. {{"a1"}, "r0"}, {{"a2"}, "r1"}, {{"a3"}, "r2"}, {{"a4"}, "r3"},
  877. {{"v1"}, "r4"}, {{"v2"}, "r5"}, {{"v3"}, "r6"}, {{"v4"}, "r7"},
  878. {{"v5"}, "r8"}, {{"v6", "rfp"}, "r9"}, {{"sl"}, "r10"}, {{"fp"}, "r11"},
  879. {{"ip"}, "r12"}, {{"r13"}, "sp"}, {{"r14"}, "lr"}, {{"r15"}, "pc"},
  880. // The S, D and Q registers overlap, but aren't really aliases; we
  881. // don't want to substitute one of these for a different-sized one.
  882. };
  883. ArrayRef<TargetInfo::GCCRegAlias> ARMTargetInfo::getGCCRegAliases() const {
  884. return llvm::makeArrayRef(GCCRegAliases);
  885. }
  886. bool ARMTargetInfo::validateAsmConstraint(
  887. const char *&Name, TargetInfo::ConstraintInfo &Info) const {
  888. switch (*Name) {
  889. default:
  890. break;
  891. case 'l': // r0-r7 if thumb, r0-r15 if ARM
  892. Info.setAllowsRegister();
  893. return true;
  894. case 'h': // r8-r15, thumb only
  895. if (isThumb()) {
  896. Info.setAllowsRegister();
  897. return true;
  898. }
  899. break;
  900. case 's': // An integer constant, but allowing only relocatable values.
  901. return true;
  902. case 't': // s0-s31, d0-d31, or q0-q15
  903. case 'w': // s0-s15, d0-d7, or q0-q3
  904. case 'x': // s0-s31, d0-d15, or q0-q7
  905. if (FPRegsDisabled)
  906. return false;
  907. Info.setAllowsRegister();
  908. return true;
  909. case 'j': // An immediate integer between 0 and 65535 (valid for MOVW)
  910. // only available in ARMv6T2 and above
  911. if (CPUAttr.equals("6T2") || ArchVersion >= 7) {
  912. Info.setRequiresImmediate(0, 65535);
  913. return true;
  914. }
  915. break;
  916. case 'I':
  917. if (isThumb()) {
  918. if (!supportsThumb2())
  919. Info.setRequiresImmediate(0, 255);
  920. else
  921. // FIXME: should check if immediate value would be valid for a Thumb2
  922. // data-processing instruction
  923. Info.setRequiresImmediate();
  924. } else
  925. // FIXME: should check if immediate value would be valid for an ARM
  926. // data-processing instruction
  927. Info.setRequiresImmediate();
  928. return true;
  929. case 'J':
  930. if (isThumb() && !supportsThumb2())
  931. Info.setRequiresImmediate(-255, -1);
  932. else
  933. Info.setRequiresImmediate(-4095, 4095);
  934. return true;
  935. case 'K':
  936. if (isThumb()) {
  937. if (!supportsThumb2())
  938. // FIXME: should check if immediate value can be obtained from shifting
  939. // a value between 0 and 255 left by any amount
  940. Info.setRequiresImmediate();
  941. else
  942. // FIXME: should check if immediate value would be valid for a Thumb2
  943. // data-processing instruction when inverted
  944. Info.setRequiresImmediate();
  945. } else
  946. // FIXME: should check if immediate value would be valid for an ARM
  947. // data-processing instruction when inverted
  948. Info.setRequiresImmediate();
  949. return true;
  950. case 'L':
  951. if (isThumb()) {
  952. if (!supportsThumb2())
  953. Info.setRequiresImmediate(-7, 7);
  954. else
  955. // FIXME: should check if immediate value would be valid for a Thumb2
  956. // data-processing instruction when negated
  957. Info.setRequiresImmediate();
  958. } else
  959. // FIXME: should check if immediate value would be valid for an ARM
  960. // data-processing instruction when negated
  961. Info.setRequiresImmediate();
  962. return true;
  963. case 'M':
  964. if (isThumb() && !supportsThumb2())
  965. // FIXME: should check if immediate value is a multiple of 4 between 0 and
  966. // 1020
  967. Info.setRequiresImmediate();
  968. else
  969. // FIXME: should check if immediate value is a power of two or a integer
  970. // between 0 and 32
  971. Info.setRequiresImmediate();
  972. return true;
  973. case 'N':
  974. // Thumb1 only
  975. if (isThumb() && !supportsThumb2()) {
  976. Info.setRequiresImmediate(0, 31);
  977. return true;
  978. }
  979. break;
  980. case 'O':
  981. // Thumb1 only
  982. if (isThumb() && !supportsThumb2()) {
  983. // FIXME: should check if immediate value is a multiple of 4 between -508
  984. // and 508
  985. Info.setRequiresImmediate();
  986. return true;
  987. }
  988. break;
  989. case 'Q': // A memory address that is a single base register.
  990. Info.setAllowsMemory();
  991. return true;
  992. case 'T':
  993. switch (Name[1]) {
  994. default:
  995. break;
  996. case 'e': // Even general-purpose register
  997. case 'o': // Odd general-purpose register
  998. Info.setAllowsRegister();
  999. Name++;
  1000. return true;
  1001. }
  1002. break;
  1003. case 'U': // a memory reference...
  1004. switch (Name[1]) {
  1005. case 'q': // ...ARMV4 ldrsb
  1006. case 'v': // ...VFP load/store (reg+constant offset)
  1007. case 'y': // ...iWMMXt load/store
  1008. case 't': // address valid for load/store opaque types wider
  1009. // than 128-bits
  1010. case 'n': // valid address for Neon doubleword vector load/store
  1011. case 'm': // valid address for Neon element and structure load/store
  1012. case 's': // valid address for non-offset loads/stores of quad-word
  1013. // values in four ARM registers
  1014. Info.setAllowsMemory();
  1015. Name++;
  1016. return true;
  1017. }
  1018. break;
  1019. }
  1020. return false;
  1021. }
  1022. std::string ARMTargetInfo::convertConstraint(const char *&Constraint) const {
  1023. std::string R;
  1024. switch (*Constraint) {
  1025. case 'U': // Two-character constraint; add "^" hint for later parsing.
  1026. case 'T':
  1027. R = std::string("^") + std::string(Constraint, 2);
  1028. Constraint++;
  1029. break;
  1030. case 'p': // 'p' should be translated to 'r' by default.
  1031. R = std::string("r");
  1032. break;
  1033. default:
  1034. return std::string(1, *Constraint);
  1035. }
  1036. return R;
  1037. }
  1038. bool ARMTargetInfo::validateConstraintModifier(
  1039. StringRef Constraint, char Modifier, unsigned Size,
  1040. std::string &SuggestedModifier) const {
  1041. bool isOutput = (Constraint[0] == '=');
  1042. bool isInOut = (Constraint[0] == '+');
  1043. // Strip off constraint modifiers.
  1044. while (Constraint[0] == '=' || Constraint[0] == '+' || Constraint[0] == '&')
  1045. Constraint = Constraint.substr(1);
  1046. switch (Constraint[0]) {
  1047. default:
  1048. break;
  1049. case 'r': {
  1050. switch (Modifier) {
  1051. default:
  1052. return (isInOut || isOutput || Size <= 64);
  1053. case 'q':
  1054. // A register of size 32 cannot fit a vector type.
  1055. return false;
  1056. }
  1057. }
  1058. }
  1059. return true;
  1060. }
  1061. const char *ARMTargetInfo::getClobbers() const {
  1062. // FIXME: Is this really right?
  1063. return "";
  1064. }
  1065. TargetInfo::CallingConvCheckResult
  1066. ARMTargetInfo::checkCallingConvention(CallingConv CC) const {
  1067. switch (CC) {
  1068. case CC_AAPCS:
  1069. case CC_AAPCS_VFP:
  1070. case CC_Swift:
  1071. case CC_SwiftAsync:
  1072. case CC_OpenCLKernel:
  1073. return CCCR_OK;
  1074. default:
  1075. return CCCR_Warning;
  1076. }
  1077. }
  1078. int ARMTargetInfo::getEHDataRegisterNumber(unsigned RegNo) const {
  1079. if (RegNo == 0)
  1080. return 0;
  1081. if (RegNo == 1)
  1082. return 1;
  1083. return -1;
  1084. }
  1085. bool ARMTargetInfo::hasSjLjLowering() const { return true; }
  1086. ARMleTargetInfo::ARMleTargetInfo(const llvm::Triple &Triple,
  1087. const TargetOptions &Opts)
  1088. : ARMTargetInfo(Triple, Opts) {}
  1089. void ARMleTargetInfo::getTargetDefines(const LangOptions &Opts,
  1090. MacroBuilder &Builder) const {
  1091. Builder.defineMacro("__ARMEL__");
  1092. ARMTargetInfo::getTargetDefines(Opts, Builder);
  1093. }
  1094. ARMbeTargetInfo::ARMbeTargetInfo(const llvm::Triple &Triple,
  1095. const TargetOptions &Opts)
  1096. : ARMTargetInfo(Triple, Opts) {}
  1097. void ARMbeTargetInfo::getTargetDefines(const LangOptions &Opts,
  1098. MacroBuilder &Builder) const {
  1099. Builder.defineMacro("__ARMEB__");
  1100. Builder.defineMacro("__ARM_BIG_ENDIAN");
  1101. ARMTargetInfo::getTargetDefines(Opts, Builder);
  1102. }
  1103. WindowsARMTargetInfo::WindowsARMTargetInfo(const llvm::Triple &Triple,
  1104. const TargetOptions &Opts)
  1105. : WindowsTargetInfo<ARMleTargetInfo>(Triple, Opts), Triple(Triple) {
  1106. }
  1107. void WindowsARMTargetInfo::getVisualStudioDefines(const LangOptions &Opts,
  1108. MacroBuilder &Builder) const {
  1109. // FIXME: this is invalid for WindowsCE
  1110. Builder.defineMacro("_M_ARM_NT", "1");
  1111. Builder.defineMacro("_M_ARMT", "_M_ARM");
  1112. Builder.defineMacro("_M_THUMB", "_M_ARM");
  1113. assert((Triple.getArch() == llvm::Triple::arm ||
  1114. Triple.getArch() == llvm::Triple::thumb) &&
  1115. "invalid architecture for Windows ARM target info");
  1116. unsigned Offset = Triple.getArch() == llvm::Triple::arm ? 4 : 6;
  1117. Builder.defineMacro("_M_ARM", Triple.getArchName().substr(Offset));
  1118. // TODO map the complete set of values
  1119. // 31: VFPv3 40: VFPv4
  1120. Builder.defineMacro("_M_ARM_FP", "31");
  1121. }
  1122. TargetInfo::BuiltinVaListKind
  1123. WindowsARMTargetInfo::getBuiltinVaListKind() const {
  1124. return TargetInfo::CharPtrBuiltinVaList;
  1125. }
  1126. TargetInfo::CallingConvCheckResult
  1127. WindowsARMTargetInfo::checkCallingConvention(CallingConv CC) const {
  1128. switch (CC) {
  1129. case CC_X86StdCall:
  1130. case CC_X86ThisCall:
  1131. case CC_X86FastCall:
  1132. case CC_X86VectorCall:
  1133. return CCCR_Ignore;
  1134. case CC_C:
  1135. case CC_OpenCLKernel:
  1136. case CC_PreserveMost:
  1137. case CC_PreserveAll:
  1138. case CC_Swift:
  1139. case CC_SwiftAsync:
  1140. return CCCR_OK;
  1141. default:
  1142. return CCCR_Warning;
  1143. }
  1144. }
  1145. // Windows ARM + Itanium C++ ABI Target
  1146. ItaniumWindowsARMleTargetInfo::ItaniumWindowsARMleTargetInfo(
  1147. const llvm::Triple &Triple, const TargetOptions &Opts)
  1148. : WindowsARMTargetInfo(Triple, Opts) {
  1149. TheCXXABI.set(TargetCXXABI::GenericARM);
  1150. }
  1151. void ItaniumWindowsARMleTargetInfo::getTargetDefines(
  1152. const LangOptions &Opts, MacroBuilder &Builder) const {
  1153. WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
  1154. if (Opts.MSVCCompat)
  1155. WindowsARMTargetInfo::getVisualStudioDefines(Opts, Builder);
  1156. }
  1157. // Windows ARM, MS (C++) ABI
  1158. MicrosoftARMleTargetInfo::MicrosoftARMleTargetInfo(const llvm::Triple &Triple,
  1159. const TargetOptions &Opts)
  1160. : WindowsARMTargetInfo(Triple, Opts) {
  1161. TheCXXABI.set(TargetCXXABI::Microsoft);
  1162. }
  1163. void MicrosoftARMleTargetInfo::getTargetDefines(const LangOptions &Opts,
  1164. MacroBuilder &Builder) const {
  1165. WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
  1166. WindowsARMTargetInfo::getVisualStudioDefines(Opts, Builder);
  1167. }
  1168. MinGWARMTargetInfo::MinGWARMTargetInfo(const llvm::Triple &Triple,
  1169. const TargetOptions &Opts)
  1170. : WindowsARMTargetInfo(Triple, Opts) {
  1171. TheCXXABI.set(TargetCXXABI::GenericARM);
  1172. }
  1173. void MinGWARMTargetInfo::getTargetDefines(const LangOptions &Opts,
  1174. MacroBuilder &Builder) const {
  1175. WindowsARMTargetInfo::getTargetDefines(Opts, Builder);
  1176. Builder.defineMacro("_ARM_");
  1177. }
  1178. CygwinARMTargetInfo::CygwinARMTargetInfo(const llvm::Triple &Triple,
  1179. const TargetOptions &Opts)
  1180. : ARMleTargetInfo(Triple, Opts) {
  1181. this->WCharType = TargetInfo::UnsignedShort;
  1182. TLSSupported = false;
  1183. DoubleAlign = LongLongAlign = 64;
  1184. resetDataLayout("e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64");
  1185. }
  1186. void CygwinARMTargetInfo::getTargetDefines(const LangOptions &Opts,
  1187. MacroBuilder &Builder) const {
  1188. ARMleTargetInfo::getTargetDefines(Opts, Builder);
  1189. Builder.defineMacro("_ARM_");
  1190. Builder.defineMacro("__CYGWIN__");
  1191. Builder.defineMacro("__CYGWIN32__");
  1192. DefineStd(Builder, "unix", Opts);
  1193. if (Opts.CPlusPlus)
  1194. Builder.defineMacro("_GNU_SOURCE");
  1195. }
  1196. DarwinARMTargetInfo::DarwinARMTargetInfo(const llvm::Triple &Triple,
  1197. const TargetOptions &Opts)
  1198. : DarwinTargetInfo<ARMleTargetInfo>(Triple, Opts) {
  1199. HasAlignMac68kSupport = true;
  1200. // iOS always has 64-bit atomic instructions.
  1201. // FIXME: This should be based off of the target features in
  1202. // ARMleTargetInfo.
  1203. MaxAtomicInlineWidth = 64;
  1204. if (Triple.isWatchABI()) {
  1205. // Darwin on iOS uses a variant of the ARM C++ ABI.
  1206. TheCXXABI.set(TargetCXXABI::WatchOS);
  1207. // BOOL should be a real boolean on the new ABI
  1208. UseSignedCharForObjCBool = false;
  1209. } else
  1210. TheCXXABI.set(TargetCXXABI::iOS);
  1211. }
  1212. void DarwinARMTargetInfo::getOSDefines(const LangOptions &Opts,
  1213. const llvm::Triple &Triple,
  1214. MacroBuilder &Builder) const {
  1215. getDarwinDefines(Builder, Opts, Triple, PlatformName, PlatformMinVersion);
  1216. }
  1217. RenderScript32TargetInfo::RenderScript32TargetInfo(const llvm::Triple &Triple,
  1218. const TargetOptions &Opts)
  1219. : ARMleTargetInfo(llvm::Triple("armv7", Triple.getVendorName(),
  1220. Triple.getOSName(),
  1221. Triple.getEnvironmentName()),
  1222. Opts) {
  1223. IsRenderScriptTarget = true;
  1224. LongWidth = LongAlign = 64;
  1225. }
  1226. void RenderScript32TargetInfo::getTargetDefines(const LangOptions &Opts,
  1227. MacroBuilder &Builder) const {
  1228. Builder.defineMacro("__RENDERSCRIPT__");
  1229. ARMleTargetInfo::getTargetDefines(Opts, Builder);
  1230. }