AArch64RegisterInfo.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. //===- AArch64RegisterInfo.cpp - AArch64 Register Information -------------===//
  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 contains the AArch64 implementation of the TargetRegisterInfo
  10. // class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "AArch64RegisterInfo.h"
  14. #include "AArch64FrameLowering.h"
  15. #include "AArch64InstrInfo.h"
  16. #include "AArch64MachineFunctionInfo.h"
  17. #include "AArch64Subtarget.h"
  18. #include "MCTargetDesc/AArch64AddressingModes.h"
  19. #include "MCTargetDesc/AArch64InstPrinter.h"
  20. #include "llvm/ADT/BitVector.h"
  21. #include "llvm/ADT/Triple.h"
  22. #include "llvm/BinaryFormat/Dwarf.h"
  23. #include "llvm/CodeGen/MachineFrameInfo.h"
  24. #include "llvm/CodeGen/MachineInstrBuilder.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/RegisterScavenging.h"
  27. #include "llvm/CodeGen/TargetFrameLowering.h"
  28. #include "llvm/IR/DebugInfoMetadata.h"
  29. #include "llvm/IR/DiagnosticInfo.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include "llvm/Target/TargetOptions.h"
  33. using namespace llvm;
  34. #define GET_CC_REGISTER_LISTS
  35. #include "AArch64GenCallingConv.inc"
  36. #define GET_REGINFO_TARGET_DESC
  37. #include "AArch64GenRegisterInfo.inc"
  38. AArch64RegisterInfo::AArch64RegisterInfo(const Triple &TT)
  39. : AArch64GenRegisterInfo(AArch64::LR), TT(TT) {
  40. AArch64_MC::initLLVMToCVRegMapping(this);
  41. }
  42. /// Return whether the register needs a CFI entry. Not all unwinders may know
  43. /// about SVE registers, so we assume the lowest common denominator, i.e. the
  44. /// callee-saves required by the base ABI. For the SVE registers z8-z15 only the
  45. /// lower 64-bits (d8-d15) need to be saved. The lower 64-bits subreg is
  46. /// returned in \p RegToUseForCFI.
  47. bool AArch64RegisterInfo::regNeedsCFI(unsigned Reg,
  48. unsigned &RegToUseForCFI) const {
  49. if (AArch64::PPRRegClass.contains(Reg))
  50. return false;
  51. if (AArch64::ZPRRegClass.contains(Reg)) {
  52. RegToUseForCFI = getSubReg(Reg, AArch64::dsub);
  53. for (int I = 0; CSR_AArch64_AAPCS_SaveList[I]; ++I) {
  54. if (CSR_AArch64_AAPCS_SaveList[I] == RegToUseForCFI)
  55. return true;
  56. }
  57. return false;
  58. }
  59. RegToUseForCFI = Reg;
  60. return true;
  61. }
  62. const MCPhysReg *
  63. AArch64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
  64. assert(MF && "Invalid MachineFunction pointer.");
  65. if (MF->getFunction().getCallingConv() == CallingConv::GHC)
  66. // GHC set of callee saved regs is empty as all those regs are
  67. // used for passing STG regs around
  68. return CSR_AArch64_NoRegs_SaveList;
  69. if (MF->getFunction().getCallingConv() == CallingConv::AnyReg)
  70. return CSR_AArch64_AllRegs_SaveList;
  71. // Darwin has its own CSR_AArch64_AAPCS_SaveList, which means most CSR save
  72. // lists depending on that will need to have their Darwin variant as well.
  73. if (MF->getSubtarget<AArch64Subtarget>().isTargetDarwin())
  74. return getDarwinCalleeSavedRegs(MF);
  75. if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
  76. return CSR_Win_AArch64_CFGuard_Check_SaveList;
  77. if (MF->getSubtarget<AArch64Subtarget>().isTargetWindows())
  78. return CSR_Win_AArch64_AAPCS_SaveList;
  79. if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
  80. return CSR_AArch64_AAVPCS_SaveList;
  81. if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
  82. return CSR_AArch64_SVE_AAPCS_SaveList;
  83. if (MF->getFunction().getCallingConv() ==
  84. CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
  85. report_fatal_error(
  86. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
  87. "only supported to improve calls to SME ACLE save/restore/disable-za "
  88. "functions, and is not intended to be used beyond that scope.");
  89. if (MF->getFunction().getCallingConv() ==
  90. CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
  91. report_fatal_error(
  92. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
  93. "only supported to improve calls to SME ACLE __arm_sme_state "
  94. "and is not intended to be used beyond that scope.");
  95. if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
  96. ->supportSwiftError() &&
  97. MF->getFunction().getAttributes().hasAttrSomewhere(
  98. Attribute::SwiftError))
  99. return CSR_AArch64_AAPCS_SwiftError_SaveList;
  100. if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
  101. return CSR_AArch64_AAPCS_SwiftTail_SaveList;
  102. if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
  103. return CSR_AArch64_RT_MostRegs_SaveList;
  104. if (MF->getFunction().getCallingConv() == CallingConv::Win64)
  105. // This is for OSes other than Windows; Windows is a separate case further
  106. // above.
  107. return CSR_AArch64_AAPCS_X18_SaveList;
  108. if (MF->getInfo<AArch64FunctionInfo>()->isSVECC())
  109. return CSR_AArch64_SVE_AAPCS_SaveList;
  110. return CSR_AArch64_AAPCS_SaveList;
  111. }
  112. const MCPhysReg *
  113. AArch64RegisterInfo::getDarwinCalleeSavedRegs(const MachineFunction *MF) const {
  114. assert(MF && "Invalid MachineFunction pointer.");
  115. assert(MF->getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
  116. "Invalid subtarget for getDarwinCalleeSavedRegs");
  117. if (MF->getFunction().getCallingConv() == CallingConv::CFGuard_Check)
  118. report_fatal_error(
  119. "Calling convention CFGuard_Check is unsupported on Darwin.");
  120. if (MF->getFunction().getCallingConv() == CallingConv::AArch64_VectorCall)
  121. return CSR_Darwin_AArch64_AAVPCS_SaveList;
  122. if (MF->getFunction().getCallingConv() == CallingConv::AArch64_SVE_VectorCall)
  123. report_fatal_error(
  124. "Calling convention SVE_VectorCall is unsupported on Darwin.");
  125. if (MF->getFunction().getCallingConv() ==
  126. CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
  127. report_fatal_error(
  128. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
  129. "only supported to improve calls to SME ACLE save/restore/disable-za "
  130. "functions, and is not intended to be used beyond that scope.");
  131. if (MF->getFunction().getCallingConv() ==
  132. CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
  133. report_fatal_error(
  134. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
  135. "only supported to improve calls to SME ACLE __arm_sme_state "
  136. "and is not intended to be used beyond that scope.");
  137. if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS)
  138. return MF->getInfo<AArch64FunctionInfo>()->isSplitCSR()
  139. ? CSR_Darwin_AArch64_CXX_TLS_PE_SaveList
  140. : CSR_Darwin_AArch64_CXX_TLS_SaveList;
  141. if (MF->getSubtarget<AArch64Subtarget>().getTargetLowering()
  142. ->supportSwiftError() &&
  143. MF->getFunction().getAttributes().hasAttrSomewhere(
  144. Attribute::SwiftError))
  145. return CSR_Darwin_AArch64_AAPCS_SwiftError_SaveList;
  146. if (MF->getFunction().getCallingConv() == CallingConv::SwiftTail)
  147. return CSR_Darwin_AArch64_AAPCS_SwiftTail_SaveList;
  148. if (MF->getFunction().getCallingConv() == CallingConv::PreserveMost)
  149. return CSR_Darwin_AArch64_RT_MostRegs_SaveList;
  150. if (MF->getFunction().getCallingConv() == CallingConv::Win64)
  151. return CSR_Darwin_AArch64_AAPCS_Win64_SaveList;
  152. return CSR_Darwin_AArch64_AAPCS_SaveList;
  153. }
  154. const MCPhysReg *AArch64RegisterInfo::getCalleeSavedRegsViaCopy(
  155. const MachineFunction *MF) const {
  156. assert(MF && "Invalid MachineFunction pointer.");
  157. if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
  158. MF->getInfo<AArch64FunctionInfo>()->isSplitCSR())
  159. return CSR_Darwin_AArch64_CXX_TLS_ViaCopy_SaveList;
  160. return nullptr;
  161. }
  162. void AArch64RegisterInfo::UpdateCustomCalleeSavedRegs(
  163. MachineFunction &MF) const {
  164. const MCPhysReg *CSRs = getCalleeSavedRegs(&MF);
  165. SmallVector<MCPhysReg, 32> UpdatedCSRs;
  166. for (const MCPhysReg *I = CSRs; *I; ++I)
  167. UpdatedCSRs.push_back(*I);
  168. for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
  169. if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
  170. UpdatedCSRs.push_back(AArch64::GPR64commonRegClass.getRegister(i));
  171. }
  172. }
  173. // Register lists are zero-terminated.
  174. UpdatedCSRs.push_back(0);
  175. MF.getRegInfo().setCalleeSavedRegs(UpdatedCSRs);
  176. }
  177. const TargetRegisterClass *
  178. AArch64RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
  179. unsigned Idx) const {
  180. // edge case for GPR/FPR register classes
  181. if (RC == &AArch64::GPR32allRegClass && Idx == AArch64::hsub)
  182. return &AArch64::FPR32RegClass;
  183. else if (RC == &AArch64::GPR64allRegClass && Idx == AArch64::hsub)
  184. return &AArch64::FPR64RegClass;
  185. // Forward to TableGen's default version.
  186. return AArch64GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
  187. }
  188. const uint32_t *
  189. AArch64RegisterInfo::getDarwinCallPreservedMask(const MachineFunction &MF,
  190. CallingConv::ID CC) const {
  191. assert(MF.getSubtarget<AArch64Subtarget>().isTargetDarwin() &&
  192. "Invalid subtarget for getDarwinCallPreservedMask");
  193. if (CC == CallingConv::CXX_FAST_TLS)
  194. return CSR_Darwin_AArch64_CXX_TLS_RegMask;
  195. if (CC == CallingConv::AArch64_VectorCall)
  196. return CSR_Darwin_AArch64_AAVPCS_RegMask;
  197. if (CC == CallingConv::AArch64_SVE_VectorCall)
  198. report_fatal_error(
  199. "Calling convention SVE_VectorCall is unsupported on Darwin.");
  200. if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
  201. report_fatal_error(
  202. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0 is "
  203. "unsupported on Darwin.");
  204. if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
  205. report_fatal_error(
  206. "Calling convention AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2 is "
  207. "unsupported on Darwin.");
  208. if (CC == CallingConv::CFGuard_Check)
  209. report_fatal_error(
  210. "Calling convention CFGuard_Check is unsupported on Darwin.");
  211. if (MF.getSubtarget<AArch64Subtarget>()
  212. .getTargetLowering()
  213. ->supportSwiftError() &&
  214. MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
  215. return CSR_Darwin_AArch64_AAPCS_SwiftError_RegMask;
  216. if (CC == CallingConv::SwiftTail)
  217. return CSR_Darwin_AArch64_AAPCS_SwiftTail_RegMask;
  218. if (CC == CallingConv::PreserveMost)
  219. return CSR_Darwin_AArch64_RT_MostRegs_RegMask;
  220. return CSR_Darwin_AArch64_AAPCS_RegMask;
  221. }
  222. const uint32_t *
  223. AArch64RegisterInfo::getCallPreservedMask(const MachineFunction &MF,
  224. CallingConv::ID CC) const {
  225. bool SCS = MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack);
  226. if (CC == CallingConv::GHC)
  227. // This is academic because all GHC calls are (supposed to be) tail calls
  228. return SCS ? CSR_AArch64_NoRegs_SCS_RegMask : CSR_AArch64_NoRegs_RegMask;
  229. if (CC == CallingConv::AnyReg)
  230. return SCS ? CSR_AArch64_AllRegs_SCS_RegMask : CSR_AArch64_AllRegs_RegMask;
  231. // All the following calling conventions are handled differently on Darwin.
  232. if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin()) {
  233. if (SCS)
  234. report_fatal_error("ShadowCallStack attribute not supported on Darwin.");
  235. return getDarwinCallPreservedMask(MF, CC);
  236. }
  237. if (CC == CallingConv::AArch64_VectorCall)
  238. return SCS ? CSR_AArch64_AAVPCS_SCS_RegMask : CSR_AArch64_AAVPCS_RegMask;
  239. if (CC == CallingConv::AArch64_SVE_VectorCall)
  240. return SCS ? CSR_AArch64_SVE_AAPCS_SCS_RegMask
  241. : CSR_AArch64_SVE_AAPCS_RegMask;
  242. if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0)
  243. return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask;
  244. if (CC == CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2)
  245. return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2_RegMask;
  246. if (CC == CallingConv::CFGuard_Check)
  247. return CSR_Win_AArch64_CFGuard_Check_RegMask;
  248. if (MF.getSubtarget<AArch64Subtarget>().getTargetLowering()
  249. ->supportSwiftError() &&
  250. MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
  251. return SCS ? CSR_AArch64_AAPCS_SwiftError_SCS_RegMask
  252. : CSR_AArch64_AAPCS_SwiftError_RegMask;
  253. if (CC == CallingConv::SwiftTail) {
  254. if (SCS)
  255. report_fatal_error("ShadowCallStack attribute not supported with swifttail");
  256. return CSR_AArch64_AAPCS_SwiftTail_RegMask;
  257. }
  258. if (CC == CallingConv::PreserveMost)
  259. return SCS ? CSR_AArch64_RT_MostRegs_SCS_RegMask
  260. : CSR_AArch64_RT_MostRegs_RegMask;
  261. else
  262. return SCS ? CSR_AArch64_AAPCS_SCS_RegMask : CSR_AArch64_AAPCS_RegMask;
  263. }
  264. const uint32_t *AArch64RegisterInfo::getCustomEHPadPreservedMask(
  265. const MachineFunction &MF) const {
  266. if (MF.getSubtarget<AArch64Subtarget>().isTargetLinux())
  267. return CSR_AArch64_AAPCS_RegMask;
  268. return nullptr;
  269. }
  270. const uint32_t *AArch64RegisterInfo::getTLSCallPreservedMask() const {
  271. if (TT.isOSDarwin())
  272. return CSR_Darwin_AArch64_TLS_RegMask;
  273. assert(TT.isOSBinFormatELF() && "Invalid target");
  274. return CSR_AArch64_TLS_ELF_RegMask;
  275. }
  276. void AArch64RegisterInfo::UpdateCustomCallPreservedMask(MachineFunction &MF,
  277. const uint32_t **Mask) const {
  278. uint32_t *UpdatedMask = MF.allocateRegMask();
  279. unsigned RegMaskSize = MachineOperand::getRegMaskSize(getNumRegs());
  280. memcpy(UpdatedMask, *Mask, sizeof(UpdatedMask[0]) * RegMaskSize);
  281. for (size_t i = 0; i < AArch64::GPR64commonRegClass.getNumRegs(); ++i) {
  282. if (MF.getSubtarget<AArch64Subtarget>().isXRegCustomCalleeSaved(i)) {
  283. for (MCSubRegIterator SubReg(AArch64::GPR64commonRegClass.getRegister(i),
  284. this, true);
  285. SubReg.isValid(); ++SubReg) {
  286. // See TargetRegisterInfo::getCallPreservedMask for how to interpret the
  287. // register mask.
  288. UpdatedMask[*SubReg / 32] |= 1u << (*SubReg % 32);
  289. }
  290. }
  291. }
  292. *Mask = UpdatedMask;
  293. }
  294. const uint32_t *AArch64RegisterInfo::getSMStartStopCallPreservedMask() const {
  295. return CSR_AArch64_SMStartStop_RegMask;
  296. }
  297. const uint32_t *
  298. AArch64RegisterInfo::SMEABISupportRoutinesCallPreservedMaskFromX0() const {
  299. return CSR_AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0_RegMask;
  300. }
  301. const uint32_t *AArch64RegisterInfo::getNoPreservedMask() const {
  302. return CSR_AArch64_NoRegs_RegMask;
  303. }
  304. const uint32_t *
  305. AArch64RegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
  306. CallingConv::ID CC) const {
  307. // This should return a register mask that is the same as that returned by
  308. // getCallPreservedMask but that additionally preserves the register used for
  309. // the first i64 argument (which must also be the register used to return a
  310. // single i64 return value)
  311. //
  312. // In case that the calling convention does not use the same register for
  313. // both, the function should return NULL (does not currently apply)
  314. assert(CC != CallingConv::GHC && "should not be GHC calling convention.");
  315. if (MF.getSubtarget<AArch64Subtarget>().isTargetDarwin())
  316. return CSR_Darwin_AArch64_AAPCS_ThisReturn_RegMask;
  317. return CSR_AArch64_AAPCS_ThisReturn_RegMask;
  318. }
  319. const uint32_t *AArch64RegisterInfo::getWindowsStackProbePreservedMask() const {
  320. return CSR_AArch64_StackProbe_Windows_RegMask;
  321. }
  322. std::optional<std::string>
  323. AArch64RegisterInfo::explainReservedReg(const MachineFunction &MF,
  324. MCRegister PhysReg) const {
  325. if (hasBasePointer(MF) && MCRegisterInfo::regsOverlap(PhysReg, AArch64::X19))
  326. return std::string("X19 is used as the frame base pointer register.");
  327. if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) {
  328. bool warn = false;
  329. if (MCRegisterInfo::regsOverlap(PhysReg, AArch64::X13) ||
  330. MCRegisterInfo::regsOverlap(PhysReg, AArch64::X14) ||
  331. MCRegisterInfo::regsOverlap(PhysReg, AArch64::X23) ||
  332. MCRegisterInfo::regsOverlap(PhysReg, AArch64::X24) ||
  333. MCRegisterInfo::regsOverlap(PhysReg, AArch64::X28))
  334. warn = true;
  335. for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i)
  336. if (MCRegisterInfo::regsOverlap(PhysReg, i))
  337. warn = true;
  338. if (warn)
  339. return std::string(AArch64InstPrinter::getRegisterName(PhysReg)) +
  340. " is clobbered by asynchronous signals when using Arm64EC.";
  341. }
  342. return {};
  343. }
  344. BitVector
  345. AArch64RegisterInfo::getStrictlyReservedRegs(const MachineFunction &MF) const {
  346. const AArch64FrameLowering *TFI = getFrameLowering(MF);
  347. // FIXME: avoid re-calculating this every time.
  348. BitVector Reserved(getNumRegs());
  349. markSuperRegs(Reserved, AArch64::WSP);
  350. markSuperRegs(Reserved, AArch64::WZR);
  351. if (TFI->hasFP(MF) || TT.isOSDarwin())
  352. markSuperRegs(Reserved, AArch64::W29);
  353. if (MF.getSubtarget<AArch64Subtarget>().isWindowsArm64EC()) {
  354. // x13, x14, x23, x24, x28, and v16-v31 are clobbered by asynchronous
  355. // signals, so we can't ever use them.
  356. markSuperRegs(Reserved, AArch64::W13);
  357. markSuperRegs(Reserved, AArch64::W14);
  358. markSuperRegs(Reserved, AArch64::W23);
  359. markSuperRegs(Reserved, AArch64::W24);
  360. markSuperRegs(Reserved, AArch64::W28);
  361. for (unsigned i = AArch64::B16; i <= AArch64::B31; ++i)
  362. markSuperRegs(Reserved, i);
  363. }
  364. for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
  365. if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReserved(i))
  366. markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
  367. }
  368. if (hasBasePointer(MF))
  369. markSuperRegs(Reserved, AArch64::W19);
  370. // SLH uses register W16/X16 as the taint register.
  371. if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening))
  372. markSuperRegs(Reserved, AArch64::W16);
  373. // SME tiles are not allocatable.
  374. if (MF.getSubtarget<AArch64Subtarget>().hasSME()) {
  375. for (MCSubRegIterator SubReg(AArch64::ZA, this, /*self=*/true);
  376. SubReg.isValid(); ++SubReg)
  377. Reserved.set(*SubReg);
  378. }
  379. markSuperRegs(Reserved, AArch64::FPCR);
  380. assert(checkAllSuperRegsMarked(Reserved));
  381. return Reserved;
  382. }
  383. BitVector
  384. AArch64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
  385. BitVector Reserved = getStrictlyReservedRegs(MF);
  386. for (size_t i = 0; i < AArch64::GPR32commonRegClass.getNumRegs(); ++i) {
  387. if (MF.getSubtarget<AArch64Subtarget>().isXRegisterReservedForRA(i))
  388. markSuperRegs(Reserved, AArch64::GPR32commonRegClass.getRegister(i));
  389. }
  390. assert(checkAllSuperRegsMarked(Reserved));
  391. return Reserved;
  392. }
  393. bool AArch64RegisterInfo::isReservedReg(const MachineFunction &MF,
  394. MCRegister Reg) const {
  395. return getReservedRegs(MF)[Reg];
  396. }
  397. bool AArch64RegisterInfo::isStrictlyReservedReg(const MachineFunction &MF,
  398. MCRegister Reg) const {
  399. return getStrictlyReservedRegs(MF)[Reg];
  400. }
  401. bool AArch64RegisterInfo::isAnyArgRegReserved(const MachineFunction &MF) const {
  402. return llvm::any_of(*AArch64::GPR64argRegClass.MC, [this, &MF](MCPhysReg r) {
  403. return isStrictlyReservedReg(MF, r);
  404. });
  405. }
  406. void AArch64RegisterInfo::emitReservedArgRegCallError(
  407. const MachineFunction &MF) const {
  408. const Function &F = MF.getFunction();
  409. F.getContext().diagnose(DiagnosticInfoUnsupported{F, ("AArch64 doesn't support"
  410. " function calls if any of the argument registers is reserved.")});
  411. }
  412. bool AArch64RegisterInfo::isAsmClobberable(const MachineFunction &MF,
  413. MCRegister PhysReg) const {
  414. // SLH uses register X16 as the taint register but it will fallback to a different
  415. // method if the user clobbers it. So X16 is not reserved for inline asm but is
  416. // for normal codegen.
  417. if (MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening) &&
  418. MCRegisterInfo::regsOverlap(PhysReg, AArch64::X16))
  419. return true;
  420. return !isReservedReg(MF, PhysReg);
  421. }
  422. const TargetRegisterClass *
  423. AArch64RegisterInfo::getPointerRegClass(const MachineFunction &MF,
  424. unsigned Kind) const {
  425. return &AArch64::GPR64spRegClass;
  426. }
  427. const TargetRegisterClass *
  428. AArch64RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
  429. if (RC == &AArch64::CCRRegClass)
  430. return &AArch64::GPR64RegClass; // Only MSR & MRS copy NZCV.
  431. return RC;
  432. }
  433. unsigned AArch64RegisterInfo::getBaseRegister() const { return AArch64::X19; }
  434. bool AArch64RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
  435. const MachineFrameInfo &MFI = MF.getFrameInfo();
  436. // In the presence of variable sized objects or funclets, if the fixed stack
  437. // size is large enough that referencing from the FP won't result in things
  438. // being in range relatively often, we can use a base pointer to allow access
  439. // from the other direction like the SP normally works.
  440. //
  441. // Furthermore, if both variable sized objects are present, and the
  442. // stack needs to be dynamically re-aligned, the base pointer is the only
  443. // reliable way to reference the locals.
  444. if (MFI.hasVarSizedObjects() || MF.hasEHFunclets()) {
  445. if (hasStackRealignment(MF))
  446. return true;
  447. if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) {
  448. const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
  449. // Frames that have variable sized objects and scalable SVE objects,
  450. // should always use a basepointer.
  451. if (!AFI->hasCalculatedStackSizeSVE() || AFI->getStackSizeSVE())
  452. return true;
  453. }
  454. // Conservatively estimate whether the negative offset from the frame
  455. // pointer will be sufficient to reach. If a function has a smallish
  456. // frame, it's less likely to have lots of spills and callee saved
  457. // space, so it's all more likely to be within range of the frame pointer.
  458. // If it's wrong, we'll materialize the constant and still get to the
  459. // object; it's just suboptimal. Negative offsets use the unscaled
  460. // load/store instructions, which have a 9-bit signed immediate.
  461. return MFI.getLocalFrameSize() >= 256;
  462. }
  463. return false;
  464. }
  465. bool AArch64RegisterInfo::isArgumentRegister(const MachineFunction &MF,
  466. MCRegister Reg) const {
  467. CallingConv::ID CC = MF.getFunction().getCallingConv();
  468. const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>();
  469. bool IsVarArg = STI.isCallingConvWin64(MF.getFunction().getCallingConv());
  470. auto HasReg = [](ArrayRef<MCRegister> RegList, MCRegister Reg) {
  471. return llvm::is_contained(RegList, Reg);
  472. };
  473. switch (CC) {
  474. default:
  475. report_fatal_error("Unsupported calling convention.");
  476. case CallingConv::WebKit_JS:
  477. return HasReg(CC_AArch64_WebKit_JS_ArgRegs, Reg);
  478. case CallingConv::GHC:
  479. return HasReg(CC_AArch64_GHC_ArgRegs, Reg);
  480. case CallingConv::C:
  481. case CallingConv::Fast:
  482. case CallingConv::PreserveMost:
  483. case CallingConv::CXX_FAST_TLS:
  484. case CallingConv::Swift:
  485. case CallingConv::SwiftTail:
  486. case CallingConv::Tail:
  487. if (STI.isTargetWindows() && IsVarArg)
  488. return HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
  489. if (!STI.isTargetDarwin()) {
  490. switch (CC) {
  491. default:
  492. return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
  493. case CallingConv::Swift:
  494. case CallingConv::SwiftTail:
  495. return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg) ||
  496. HasReg(CC_AArch64_AAPCS_Swift_ArgRegs, Reg);
  497. }
  498. }
  499. if (!IsVarArg) {
  500. switch (CC) {
  501. default:
  502. return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg);
  503. case CallingConv::Swift:
  504. case CallingConv::SwiftTail:
  505. return HasReg(CC_AArch64_DarwinPCS_ArgRegs, Reg) ||
  506. HasReg(CC_AArch64_DarwinPCS_Swift_ArgRegs, Reg);
  507. }
  508. }
  509. if (STI.isTargetILP32())
  510. return HasReg(CC_AArch64_DarwinPCS_ILP32_VarArg_ArgRegs, Reg);
  511. return HasReg(CC_AArch64_DarwinPCS_VarArg_ArgRegs, Reg);
  512. case CallingConv::Win64:
  513. if (IsVarArg)
  514. HasReg(CC_AArch64_Win64_VarArg_ArgRegs, Reg);
  515. return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
  516. case CallingConv::CFGuard_Check:
  517. return HasReg(CC_AArch64_Win64_CFGuard_Check_ArgRegs, Reg);
  518. case CallingConv::AArch64_VectorCall:
  519. case CallingConv::AArch64_SVE_VectorCall:
  520. case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
  521. case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
  522. return HasReg(CC_AArch64_AAPCS_ArgRegs, Reg);
  523. }
  524. }
  525. Register
  526. AArch64RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
  527. const AArch64FrameLowering *TFI = getFrameLowering(MF);
  528. return TFI->hasFP(MF) ? AArch64::FP : AArch64::SP;
  529. }
  530. bool AArch64RegisterInfo::requiresRegisterScavenging(
  531. const MachineFunction &MF) const {
  532. return true;
  533. }
  534. bool AArch64RegisterInfo::requiresVirtualBaseRegisters(
  535. const MachineFunction &MF) const {
  536. return true;
  537. }
  538. bool
  539. AArch64RegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
  540. // This function indicates whether the emergency spillslot should be placed
  541. // close to the beginning of the stackframe (closer to FP) or the end
  542. // (closer to SP).
  543. //
  544. // The beginning works most reliably if we have a frame pointer.
  545. // In the presence of any non-constant space between FP and locals,
  546. // (e.g. in case of stack realignment or a scalable SVE area), it is
  547. // better to use SP or BP.
  548. const AArch64FrameLowering &TFI = *getFrameLowering(MF);
  549. const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
  550. assert((!MF.getSubtarget<AArch64Subtarget>().hasSVE() ||
  551. AFI->hasCalculatedStackSizeSVE()) &&
  552. "Expected SVE area to be calculated by this point");
  553. return TFI.hasFP(MF) && !hasStackRealignment(MF) && !AFI->getStackSizeSVE();
  554. }
  555. bool AArch64RegisterInfo::requiresFrameIndexScavenging(
  556. const MachineFunction &MF) const {
  557. return true;
  558. }
  559. bool
  560. AArch64RegisterInfo::cannotEliminateFrame(const MachineFunction &MF) const {
  561. const MachineFrameInfo &MFI = MF.getFrameInfo();
  562. if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
  563. return true;
  564. return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken();
  565. }
  566. /// needsFrameBaseReg - Returns true if the instruction's frame index
  567. /// reference would be better served by a base register other than FP
  568. /// or SP. Used by LocalStackFrameAllocation to determine which frame index
  569. /// references it should create new base registers for.
  570. bool AArch64RegisterInfo::needsFrameBaseReg(MachineInstr *MI,
  571. int64_t Offset) const {
  572. for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i)
  573. assert(i < MI->getNumOperands() &&
  574. "Instr doesn't have FrameIndex operand!");
  575. // It's the load/store FI references that cause issues, as it can be difficult
  576. // to materialize the offset if it won't fit in the literal field. Estimate
  577. // based on the size of the local frame and some conservative assumptions
  578. // about the rest of the stack frame (note, this is pre-regalloc, so
  579. // we don't know everything for certain yet) whether this offset is likely
  580. // to be out of range of the immediate. Return true if so.
  581. // We only generate virtual base registers for loads and stores, so
  582. // return false for everything else.
  583. if (!MI->mayLoad() && !MI->mayStore())
  584. return false;
  585. // Without a virtual base register, if the function has variable sized
  586. // objects, all fixed-size local references will be via the frame pointer,
  587. // Approximate the offset and see if it's legal for the instruction.
  588. // Note that the incoming offset is based on the SP value at function entry,
  589. // so it'll be negative.
  590. MachineFunction &MF = *MI->getParent()->getParent();
  591. const AArch64FrameLowering *TFI = getFrameLowering(MF);
  592. MachineFrameInfo &MFI = MF.getFrameInfo();
  593. // Estimate an offset from the frame pointer.
  594. // Conservatively assume all GPR callee-saved registers get pushed.
  595. // FP, LR, X19-X28, D8-D15. 64-bits each.
  596. int64_t FPOffset = Offset - 16 * 20;
  597. // Estimate an offset from the stack pointer.
  598. // The incoming offset is relating to the SP at the start of the function,
  599. // but when we access the local it'll be relative to the SP after local
  600. // allocation, so adjust our SP-relative offset by that allocation size.
  601. Offset += MFI.getLocalFrameSize();
  602. // Assume that we'll have at least some spill slots allocated.
  603. // FIXME: This is a total SWAG number. We should run some statistics
  604. // and pick a real one.
  605. Offset += 128; // 128 bytes of spill slots
  606. // If there is a frame pointer, try using it.
  607. // The FP is only available if there is no dynamic realignment. We
  608. // don't know for sure yet whether we'll need that, so we guess based
  609. // on whether there are any local variables that would trigger it.
  610. if (TFI->hasFP(MF) && isFrameOffsetLegal(MI, AArch64::FP, FPOffset))
  611. return false;
  612. // If we can reference via the stack pointer or base pointer, try that.
  613. // FIXME: This (and the code that resolves the references) can be improved
  614. // to only disallow SP relative references in the live range of
  615. // the VLA(s). In practice, it's unclear how much difference that
  616. // would make, but it may be worth doing.
  617. if (isFrameOffsetLegal(MI, AArch64::SP, Offset))
  618. return false;
  619. // If even offset 0 is illegal, we don't want a virtual base register.
  620. if (!isFrameOffsetLegal(MI, AArch64::SP, 0))
  621. return false;
  622. // The offset likely isn't legal; we want to allocate a virtual base register.
  623. return true;
  624. }
  625. bool AArch64RegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
  626. Register BaseReg,
  627. int64_t Offset) const {
  628. assert(MI && "Unable to get the legal offset for nil instruction.");
  629. StackOffset SaveOffset = StackOffset::getFixed(Offset);
  630. return isAArch64FrameOffsetLegal(*MI, SaveOffset) & AArch64FrameOffsetIsLegal;
  631. }
  632. /// Insert defining instruction(s) for BaseReg to be a pointer to FrameIdx
  633. /// at the beginning of the basic block.
  634. Register
  635. AArch64RegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
  636. int FrameIdx,
  637. int64_t Offset) const {
  638. MachineBasicBlock::iterator Ins = MBB->begin();
  639. DebugLoc DL; // Defaults to "unknown"
  640. if (Ins != MBB->end())
  641. DL = Ins->getDebugLoc();
  642. const MachineFunction &MF = *MBB->getParent();
  643. const AArch64InstrInfo *TII =
  644. MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
  645. const MCInstrDesc &MCID = TII->get(AArch64::ADDXri);
  646. MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
  647. Register BaseReg = MRI.createVirtualRegister(&AArch64::GPR64spRegClass);
  648. MRI.constrainRegClass(BaseReg, TII->getRegClass(MCID, 0, this, MF));
  649. unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
  650. BuildMI(*MBB, Ins, DL, MCID, BaseReg)
  651. .addFrameIndex(FrameIdx)
  652. .addImm(Offset)
  653. .addImm(Shifter);
  654. return BaseReg;
  655. }
  656. void AArch64RegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
  657. int64_t Offset) const {
  658. // ARM doesn't need the general 64-bit offsets
  659. StackOffset Off = StackOffset::getFixed(Offset);
  660. unsigned i = 0;
  661. while (!MI.getOperand(i).isFI()) {
  662. ++i;
  663. assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
  664. }
  665. const MachineFunction *MF = MI.getParent()->getParent();
  666. const AArch64InstrInfo *TII =
  667. MF->getSubtarget<AArch64Subtarget>().getInstrInfo();
  668. bool Done = rewriteAArch64FrameIndex(MI, i, BaseReg, Off, TII);
  669. assert(Done && "Unable to resolve frame index!");
  670. (void)Done;
  671. }
  672. // Create a scratch register for the frame index elimination in an instruction.
  673. // This function has special handling of stack tagging loop pseudos, in which
  674. // case it can also change the instruction opcode.
  675. static Register
  676. createScratchRegisterForInstruction(MachineInstr &MI, unsigned FIOperandNum,
  677. const AArch64InstrInfo *TII) {
  678. // ST*Gloop have a reserved scratch register in operand 1. Use it, and also
  679. // replace the instruction with the writeback variant because it will now
  680. // satisfy the operand constraints for it.
  681. Register ScratchReg;
  682. if (MI.getOpcode() == AArch64::STGloop ||
  683. MI.getOpcode() == AArch64::STZGloop) {
  684. assert(FIOperandNum == 3 &&
  685. "Wrong frame index operand for STGloop/STZGloop");
  686. unsigned Op = MI.getOpcode() == AArch64::STGloop ? AArch64::STGloop_wback
  687. : AArch64::STZGloop_wback;
  688. ScratchReg = MI.getOperand(1).getReg();
  689. MI.getOperand(3).ChangeToRegister(ScratchReg, false, false, true);
  690. MI.setDesc(TII->get(Op));
  691. MI.tieOperands(1, 3);
  692. } else {
  693. ScratchReg =
  694. MI.getMF()->getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
  695. MI.getOperand(FIOperandNum)
  696. .ChangeToRegister(ScratchReg, false, false, true);
  697. }
  698. return ScratchReg;
  699. }
  700. void AArch64RegisterInfo::getOffsetOpcodes(
  701. const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
  702. // The smallest scalable element supported by scaled SVE addressing
  703. // modes are predicates, which are 2 scalable bytes in size. So the scalable
  704. // byte offset must always be a multiple of 2.
  705. assert(Offset.getScalable() % 2 == 0 && "Invalid frame offset");
  706. // Add fixed-sized offset using existing DIExpression interface.
  707. DIExpression::appendOffset(Ops, Offset.getFixed());
  708. unsigned VG = getDwarfRegNum(AArch64::VG, true);
  709. int64_t VGSized = Offset.getScalable() / 2;
  710. if (VGSized > 0) {
  711. Ops.push_back(dwarf::DW_OP_constu);
  712. Ops.push_back(VGSized);
  713. Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
  714. Ops.push_back(dwarf::DW_OP_mul);
  715. Ops.push_back(dwarf::DW_OP_plus);
  716. } else if (VGSized < 0) {
  717. Ops.push_back(dwarf::DW_OP_constu);
  718. Ops.push_back(-VGSized);
  719. Ops.append({dwarf::DW_OP_bregx, VG, 0ULL});
  720. Ops.push_back(dwarf::DW_OP_mul);
  721. Ops.push_back(dwarf::DW_OP_minus);
  722. }
  723. }
  724. bool AArch64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
  725. int SPAdj, unsigned FIOperandNum,
  726. RegScavenger *RS) const {
  727. assert(SPAdj == 0 && "Unexpected");
  728. MachineInstr &MI = *II;
  729. MachineBasicBlock &MBB = *MI.getParent();
  730. MachineFunction &MF = *MBB.getParent();
  731. const MachineFrameInfo &MFI = MF.getFrameInfo();
  732. const AArch64InstrInfo *TII =
  733. MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
  734. const AArch64FrameLowering *TFI = getFrameLowering(MF);
  735. int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
  736. bool Tagged =
  737. MI.getOperand(FIOperandNum).getTargetFlags() & AArch64II::MO_TAGGED;
  738. Register FrameReg;
  739. // Special handling of dbg_value, stackmap patchpoint statepoint instructions.
  740. if (MI.getOpcode() == TargetOpcode::STACKMAP ||
  741. MI.getOpcode() == TargetOpcode::PATCHPOINT ||
  742. MI.getOpcode() == TargetOpcode::STATEPOINT) {
  743. StackOffset Offset =
  744. TFI->resolveFrameIndexReference(MF, FrameIndex, FrameReg,
  745. /*PreferFP=*/true,
  746. /*ForSimm=*/false);
  747. Offset += StackOffset::getFixed(MI.getOperand(FIOperandNum + 1).getImm());
  748. MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false /*isDef*/);
  749. MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset.getFixed());
  750. return false;
  751. }
  752. if (MI.getOpcode() == TargetOpcode::LOCAL_ESCAPE) {
  753. MachineOperand &FI = MI.getOperand(FIOperandNum);
  754. StackOffset Offset = TFI->getNonLocalFrameIndexReference(MF, FrameIndex);
  755. assert(!Offset.getScalable() &&
  756. "Frame offsets with a scalable component are not supported");
  757. FI.ChangeToImmediate(Offset.getFixed());
  758. return false;
  759. }
  760. StackOffset Offset;
  761. if (MI.getOpcode() == AArch64::TAGPstack) {
  762. // TAGPstack must use the virtual frame register in its 3rd operand.
  763. const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
  764. FrameReg = MI.getOperand(3).getReg();
  765. Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
  766. AFI->getTaggedBasePointerOffset());
  767. } else if (Tagged) {
  768. StackOffset SPOffset = StackOffset::getFixed(
  769. MFI.getObjectOffset(FrameIndex) + (int64_t)MFI.getStackSize());
  770. if (MFI.hasVarSizedObjects() ||
  771. isAArch64FrameOffsetLegal(MI, SPOffset, nullptr, nullptr, nullptr) !=
  772. (AArch64FrameOffsetCanUpdate | AArch64FrameOffsetIsLegal)) {
  773. // Can't update to SP + offset in place. Precalculate the tagged pointer
  774. // in a scratch register.
  775. Offset = TFI->resolveFrameIndexReference(
  776. MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
  777. Register ScratchReg =
  778. MF.getRegInfo().createVirtualRegister(&AArch64::GPR64RegClass);
  779. emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset,
  780. TII);
  781. BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(AArch64::LDG), ScratchReg)
  782. .addReg(ScratchReg)
  783. .addReg(ScratchReg)
  784. .addImm(0);
  785. MI.getOperand(FIOperandNum)
  786. .ChangeToRegister(ScratchReg, false, false, true);
  787. return false;
  788. }
  789. FrameReg = AArch64::SP;
  790. Offset = StackOffset::getFixed(MFI.getObjectOffset(FrameIndex) +
  791. (int64_t)MFI.getStackSize());
  792. } else {
  793. Offset = TFI->resolveFrameIndexReference(
  794. MF, FrameIndex, FrameReg, /*PreferFP=*/false, /*ForSimm=*/true);
  795. }
  796. // Modify MI as necessary to handle as much of 'Offset' as possible
  797. if (rewriteAArch64FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII))
  798. return true;
  799. assert((!RS || !RS->isScavengingFrameIndex(FrameIndex)) &&
  800. "Emergency spill slot is out of reach");
  801. // If we get here, the immediate doesn't fit into the instruction. We folded
  802. // as much as possible above. Handle the rest, providing a register that is
  803. // SP+LargeImm.
  804. Register ScratchReg =
  805. createScratchRegisterForInstruction(MI, FIOperandNum, TII);
  806. emitFrameOffset(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg, Offset, TII);
  807. return false;
  808. }
  809. unsigned AArch64RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
  810. MachineFunction &MF) const {
  811. const AArch64FrameLowering *TFI = getFrameLowering(MF);
  812. switch (RC->getID()) {
  813. default:
  814. return 0;
  815. case AArch64::GPR32RegClassID:
  816. case AArch64::GPR32spRegClassID:
  817. case AArch64::GPR32allRegClassID:
  818. case AArch64::GPR64spRegClassID:
  819. case AArch64::GPR64allRegClassID:
  820. case AArch64::GPR64RegClassID:
  821. case AArch64::GPR32commonRegClassID:
  822. case AArch64::GPR64commonRegClassID:
  823. return 32 - 1 // XZR/SP
  824. - (TFI->hasFP(MF) || TT.isOSDarwin()) // FP
  825. - MF.getSubtarget<AArch64Subtarget>().getNumXRegisterReserved()
  826. - hasBasePointer(MF); // X19
  827. case AArch64::FPR8RegClassID:
  828. case AArch64::FPR16RegClassID:
  829. case AArch64::FPR32RegClassID:
  830. case AArch64::FPR64RegClassID:
  831. case AArch64::FPR128RegClassID:
  832. return 32;
  833. case AArch64::MatrixIndexGPR32_8_11RegClassID:
  834. case AArch64::MatrixIndexGPR32_12_15RegClassID:
  835. return 4;
  836. case AArch64::DDRegClassID:
  837. case AArch64::DDDRegClassID:
  838. case AArch64::DDDDRegClassID:
  839. case AArch64::QQRegClassID:
  840. case AArch64::QQQRegClassID:
  841. case AArch64::QQQQRegClassID:
  842. return 32;
  843. case AArch64::FPR128_loRegClassID:
  844. case AArch64::FPR64_loRegClassID:
  845. case AArch64::FPR16_loRegClassID:
  846. return 16;
  847. }
  848. }
  849. unsigned AArch64RegisterInfo::getLocalAddressRegister(
  850. const MachineFunction &MF) const {
  851. const auto &MFI = MF.getFrameInfo();
  852. if (!MF.hasEHFunclets() && !MFI.hasVarSizedObjects())
  853. return AArch64::SP;
  854. else if (hasStackRealignment(MF))
  855. return getBaseRegister();
  856. return getFrameRegister(MF);
  857. }
  858. /// SrcRC and DstRC will be morphed into NewRC if this returns true
  859. bool AArch64RegisterInfo::shouldCoalesce(
  860. MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg,
  861. const TargetRegisterClass *DstRC, unsigned DstSubReg,
  862. const TargetRegisterClass *NewRC, LiveIntervals &LIS) const {
  863. if (MI->isCopy() &&
  864. ((DstRC->getID() == AArch64::GPR64RegClassID) ||
  865. (DstRC->getID() == AArch64::GPR64commonRegClassID)) &&
  866. MI->getOperand(0).getSubReg() && MI->getOperand(1).getSubReg())
  867. // Do not coalesce in the case of a 32-bit subregister copy
  868. // which implements a 32 to 64 bit zero extension
  869. // which relies on the upper 32 bits being zeroed.
  870. return false;
  871. return true;
  872. }