ARMBaseRegisterInfo.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. //===-- ARMBaseRegisterInfo.cpp - ARM 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 base ARM implementation of TargetRegisterInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARMBaseRegisterInfo.h"
  13. #include "ARM.h"
  14. #include "ARMBaseInstrInfo.h"
  15. #include "ARMFrameLowering.h"
  16. #include "ARMMachineFunctionInfo.h"
  17. #include "ARMSubtarget.h"
  18. #include "MCTargetDesc/ARMAddressingModes.h"
  19. #include "MCTargetDesc/ARMBaseInfo.h"
  20. #include "llvm/ADT/BitVector.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/CodeGen/MachineBasicBlock.h"
  24. #include "llvm/CodeGen/MachineConstantPool.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/CodeGen/MachineFunction.h"
  27. #include "llvm/CodeGen/MachineInstr.h"
  28. #include "llvm/CodeGen/MachineInstrBuilder.h"
  29. #include "llvm/CodeGen/MachineOperand.h"
  30. #include "llvm/CodeGen/MachineRegisterInfo.h"
  31. #include "llvm/CodeGen/RegisterScavenging.h"
  32. #include "llvm/CodeGen/TargetInstrInfo.h"
  33. #include "llvm/CodeGen/TargetRegisterInfo.h"
  34. #include "llvm/CodeGen/VirtRegMap.h"
  35. #include "llvm/IR/Attributes.h"
  36. #include "llvm/IR/Constants.h"
  37. #include "llvm/IR/DebugLoc.h"
  38. #include "llvm/IR/Function.h"
  39. #include "llvm/IR/Type.h"
  40. #include "llvm/MC/MCInstrDesc.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/raw_ostream.h"
  44. #include "llvm/Target/TargetMachine.h"
  45. #include "llvm/Target/TargetOptions.h"
  46. #include <cassert>
  47. #include <utility>
  48. #define DEBUG_TYPE "arm-register-info"
  49. #define GET_REGINFO_TARGET_DESC
  50. #include "ARMGenRegisterInfo.inc"
  51. using namespace llvm;
  52. ARMBaseRegisterInfo::ARMBaseRegisterInfo()
  53. : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC) {
  54. ARM_MC::initLLVMToCVRegMapping(this);
  55. }
  56. const MCPhysReg*
  57. ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
  58. const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
  59. bool UseSplitPush = STI.splitFramePushPop(*MF);
  60. const MCPhysReg *RegList =
  61. STI.isTargetDarwin()
  62. ? CSR_iOS_SaveList
  63. : (UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList);
  64. const Function &F = MF->getFunction();
  65. if (F.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_NoRegs_SaveList;
  69. } else if (F.getCallingConv() == CallingConv::CFGuard_Check) {
  70. return CSR_Win_AAPCS_CFGuard_Check_SaveList;
  71. } else if (F.getCallingConv() == CallingConv::SwiftTail) {
  72. return STI.isTargetDarwin()
  73. ? CSR_iOS_SwiftTail_SaveList
  74. : (UseSplitPush ? CSR_AAPCS_SplitPush_SwiftTail_SaveList
  75. : CSR_AAPCS_SwiftTail_SaveList);
  76. } else if (F.hasFnAttribute("interrupt")) {
  77. if (STI.isMClass()) {
  78. // M-class CPUs have hardware which saves the registers needed to allow a
  79. // function conforming to the AAPCS to function as a handler.
  80. return UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList;
  81. } else if (F.getFnAttribute("interrupt").getValueAsString() == "FIQ") {
  82. // Fast interrupt mode gives the handler a private copy of R8-R14, so less
  83. // need to be saved to restore user-mode state.
  84. return CSR_FIQ_SaveList;
  85. } else {
  86. // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
  87. // exception handling.
  88. return CSR_GenericInt_SaveList;
  89. }
  90. }
  91. if (STI.getTargetLowering()->supportSwiftError() &&
  92. F.getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
  93. if (STI.isTargetDarwin())
  94. return CSR_iOS_SwiftError_SaveList;
  95. return UseSplitPush ? CSR_AAPCS_SplitPush_SwiftError_SaveList :
  96. CSR_AAPCS_SwiftError_SaveList;
  97. }
  98. if (STI.isTargetDarwin() && F.getCallingConv() == CallingConv::CXX_FAST_TLS)
  99. return MF->getInfo<ARMFunctionInfo>()->isSplitCSR()
  100. ? CSR_iOS_CXX_TLS_PE_SaveList
  101. : CSR_iOS_CXX_TLS_SaveList;
  102. return RegList;
  103. }
  104. const MCPhysReg *ARMBaseRegisterInfo::getCalleeSavedRegsViaCopy(
  105. const MachineFunction *MF) const {
  106. assert(MF && "Invalid MachineFunction pointer.");
  107. if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
  108. MF->getInfo<ARMFunctionInfo>()->isSplitCSR())
  109. return CSR_iOS_CXX_TLS_ViaCopy_SaveList;
  110. return nullptr;
  111. }
  112. const uint32_t *
  113. ARMBaseRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
  114. CallingConv::ID CC) const {
  115. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  116. if (CC == CallingConv::GHC)
  117. // This is academic because all GHC calls are (supposed to be) tail calls
  118. return CSR_NoRegs_RegMask;
  119. if (CC == CallingConv::CFGuard_Check)
  120. return CSR_Win_AAPCS_CFGuard_Check_RegMask;
  121. if (CC == CallingConv::SwiftTail) {
  122. return STI.isTargetDarwin() ? CSR_iOS_SwiftTail_RegMask
  123. : CSR_AAPCS_SwiftTail_RegMask;
  124. }
  125. if (STI.getTargetLowering()->supportSwiftError() &&
  126. MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
  127. return STI.isTargetDarwin() ? CSR_iOS_SwiftError_RegMask
  128. : CSR_AAPCS_SwiftError_RegMask;
  129. if (STI.isTargetDarwin() && CC == CallingConv::CXX_FAST_TLS)
  130. return CSR_iOS_CXX_TLS_RegMask;
  131. return STI.isTargetDarwin() ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
  132. }
  133. const uint32_t*
  134. ARMBaseRegisterInfo::getNoPreservedMask() const {
  135. return CSR_NoRegs_RegMask;
  136. }
  137. const uint32_t *
  138. ARMBaseRegisterInfo::getTLSCallPreservedMask(const MachineFunction &MF) const {
  139. assert(MF.getSubtarget<ARMSubtarget>().isTargetDarwin() &&
  140. "only know about special TLS call on Darwin");
  141. return CSR_iOS_TLSCall_RegMask;
  142. }
  143. const uint32_t *
  144. ARMBaseRegisterInfo::getSjLjDispatchPreservedMask(const MachineFunction &MF) const {
  145. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  146. if (!STI.useSoftFloat() && STI.hasVFP2Base() && !STI.isThumb1Only())
  147. return CSR_NoRegs_RegMask;
  148. else
  149. return CSR_FPRegs_RegMask;
  150. }
  151. const uint32_t *
  152. ARMBaseRegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
  153. CallingConv::ID CC) const {
  154. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  155. // This should return a register mask that is the same as that returned by
  156. // getCallPreservedMask but that additionally preserves the register used for
  157. // the first i32 argument (which must also be the register used to return a
  158. // single i32 return value)
  159. //
  160. // In case that the calling convention does not use the same register for
  161. // both or otherwise does not want to enable this optimization, the function
  162. // should return NULL
  163. if (CC == CallingConv::GHC)
  164. // This is academic because all GHC calls are (supposed to be) tail calls
  165. return nullptr;
  166. return STI.isTargetDarwin() ? CSR_iOS_ThisReturn_RegMask
  167. : CSR_AAPCS_ThisReturn_RegMask;
  168. }
  169. ArrayRef<MCPhysReg> ARMBaseRegisterInfo::getIntraCallClobberedRegs(
  170. const MachineFunction *MF) const {
  171. static const MCPhysReg IntraCallClobberedRegs[] = {ARM::R12};
  172. return ArrayRef<MCPhysReg>(IntraCallClobberedRegs);
  173. }
  174. BitVector ARMBaseRegisterInfo::
  175. getReservedRegs(const MachineFunction &MF) const {
  176. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  177. const ARMFrameLowering *TFI = getFrameLowering(MF);
  178. // FIXME: avoid re-calculating this every time.
  179. BitVector Reserved(getNumRegs());
  180. markSuperRegs(Reserved, ARM::SP);
  181. markSuperRegs(Reserved, ARM::PC);
  182. markSuperRegs(Reserved, ARM::FPSCR);
  183. markSuperRegs(Reserved, ARM::APSR_NZCV);
  184. if (TFI->hasFP(MF))
  185. markSuperRegs(Reserved, STI.getFramePointerReg());
  186. if (hasBasePointer(MF))
  187. markSuperRegs(Reserved, BasePtr);
  188. // Some targets reserve R9.
  189. if (STI.isR9Reserved())
  190. markSuperRegs(Reserved, ARM::R9);
  191. // Reserve D16-D31 if the subtarget doesn't support them.
  192. if (!STI.hasD32()) {
  193. static_assert(ARM::D31 == ARM::D16 + 15, "Register list not consecutive!");
  194. for (unsigned R = 0; R < 16; ++R)
  195. markSuperRegs(Reserved, ARM::D16 + R);
  196. }
  197. const TargetRegisterClass &RC = ARM::GPRPairRegClass;
  198. for (unsigned Reg : RC)
  199. for (MCSubRegIterator SI(Reg, this); SI.isValid(); ++SI)
  200. if (Reserved.test(*SI))
  201. markSuperRegs(Reserved, Reg);
  202. // For v8.1m architecture
  203. markSuperRegs(Reserved, ARM::ZR);
  204. assert(checkAllSuperRegsMarked(Reserved));
  205. return Reserved;
  206. }
  207. bool ARMBaseRegisterInfo::
  208. isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const {
  209. return !getReservedRegs(MF).test(PhysReg);
  210. }
  211. bool ARMBaseRegisterInfo::isInlineAsmReadOnlyReg(const MachineFunction &MF,
  212. unsigned PhysReg) const {
  213. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  214. const ARMFrameLowering *TFI = getFrameLowering(MF);
  215. BitVector Reserved(getNumRegs());
  216. markSuperRegs(Reserved, ARM::PC);
  217. if (TFI->hasFP(MF))
  218. markSuperRegs(Reserved, STI.getFramePointerReg());
  219. if (hasBasePointer(MF))
  220. markSuperRegs(Reserved, BasePtr);
  221. assert(checkAllSuperRegsMarked(Reserved));
  222. return Reserved.test(PhysReg);
  223. }
  224. const TargetRegisterClass *
  225. ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
  226. const MachineFunction &MF) const {
  227. const TargetRegisterClass *Super = RC;
  228. TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
  229. do {
  230. switch (Super->getID()) {
  231. case ARM::GPRRegClassID:
  232. case ARM::SPRRegClassID:
  233. case ARM::DPRRegClassID:
  234. case ARM::GPRPairRegClassID:
  235. return Super;
  236. case ARM::QPRRegClassID:
  237. case ARM::QQPRRegClassID:
  238. case ARM::QQQQPRRegClassID:
  239. if (MF.getSubtarget<ARMSubtarget>().hasNEON())
  240. return Super;
  241. break;
  242. case ARM::MQPRRegClassID:
  243. case ARM::MQQPRRegClassID:
  244. case ARM::MQQQQPRRegClassID:
  245. if (MF.getSubtarget<ARMSubtarget>().hasMVEIntegerOps())
  246. return Super;
  247. break;
  248. }
  249. Super = *I++;
  250. } while (Super);
  251. return RC;
  252. }
  253. const TargetRegisterClass *
  254. ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
  255. const {
  256. return &ARM::GPRRegClass;
  257. }
  258. const TargetRegisterClass *
  259. ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
  260. if (RC == &ARM::CCRRegClass)
  261. return &ARM::rGPRRegClass; // Can't copy CCR registers.
  262. return RC;
  263. }
  264. unsigned
  265. ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
  266. MachineFunction &MF) const {
  267. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  268. const ARMFrameLowering *TFI = getFrameLowering(MF);
  269. switch (RC->getID()) {
  270. default:
  271. return 0;
  272. case ARM::tGPRRegClassID: {
  273. // hasFP ends up calling getMaxCallFrameComputed() which may not be
  274. // available when getPressureLimit() is called as part of
  275. // ScheduleDAGRRList.
  276. bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
  277. ? TFI->hasFP(MF) : true;
  278. return 5 - HasFP;
  279. }
  280. case ARM::GPRRegClassID: {
  281. bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
  282. ? TFI->hasFP(MF) : true;
  283. return 10 - HasFP - (STI.isR9Reserved() ? 1 : 0);
  284. }
  285. case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
  286. case ARM::DPRRegClassID:
  287. return 32 - 10;
  288. }
  289. }
  290. // Get the other register in a GPRPair.
  291. static MCPhysReg getPairedGPR(MCPhysReg Reg, bool Odd,
  292. const MCRegisterInfo *RI) {
  293. for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
  294. if (ARM::GPRPairRegClass.contains(*Supers))
  295. return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
  296. return 0;
  297. }
  298. // Resolve the RegPairEven / RegPairOdd register allocator hints.
  299. bool ARMBaseRegisterInfo::getRegAllocationHints(
  300. Register VirtReg, ArrayRef<MCPhysReg> Order,
  301. SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
  302. const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
  303. const MachineRegisterInfo &MRI = MF.getRegInfo();
  304. std::pair<Register, Register> Hint = MRI.getRegAllocationHint(VirtReg);
  305. unsigned Odd;
  306. switch (Hint.first) {
  307. case ARMRI::RegPairEven:
  308. Odd = 0;
  309. break;
  310. case ARMRI::RegPairOdd:
  311. Odd = 1;
  312. break;
  313. case ARMRI::RegLR:
  314. TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
  315. if (MRI.getRegClass(VirtReg)->contains(ARM::LR))
  316. Hints.push_back(ARM::LR);
  317. return false;
  318. default:
  319. return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
  320. }
  321. // This register should preferably be even (Odd == 0) or odd (Odd == 1).
  322. // Check if the other part of the pair has already been assigned, and provide
  323. // the paired register as the first hint.
  324. Register Paired = Hint.second;
  325. if (!Paired)
  326. return false;
  327. Register PairedPhys;
  328. if (Paired.isPhysical()) {
  329. PairedPhys = Paired;
  330. } else if (VRM && VRM->hasPhys(Paired)) {
  331. PairedPhys = getPairedGPR(VRM->getPhys(Paired), Odd, this);
  332. }
  333. // First prefer the paired physreg.
  334. if (PairedPhys && is_contained(Order, PairedPhys))
  335. Hints.push_back(PairedPhys);
  336. // Then prefer even or odd registers.
  337. for (MCPhysReg Reg : Order) {
  338. if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
  339. continue;
  340. // Don't provide hints that are paired to a reserved register.
  341. MCPhysReg Paired = getPairedGPR(Reg, !Odd, this);
  342. if (!Paired || MRI.isReserved(Paired))
  343. continue;
  344. Hints.push_back(Reg);
  345. }
  346. return false;
  347. }
  348. void ARMBaseRegisterInfo::updateRegAllocHint(Register Reg, Register NewReg,
  349. MachineFunction &MF) const {
  350. MachineRegisterInfo *MRI = &MF.getRegInfo();
  351. std::pair<Register, Register> Hint = MRI->getRegAllocationHint(Reg);
  352. if ((Hint.first == ARMRI::RegPairOdd || Hint.first == ARMRI::RegPairEven) &&
  353. Hint.second.isVirtual()) {
  354. // If 'Reg' is one of the even / odd register pair and it's now changed
  355. // (e.g. coalesced) into a different register. The other register of the
  356. // pair allocation hint must be updated to reflect the relationship
  357. // change.
  358. Register OtherReg = Hint.second;
  359. Hint = MRI->getRegAllocationHint(OtherReg);
  360. // Make sure the pair has not already divorced.
  361. if (Hint.second == Reg) {
  362. MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
  363. if (Register::isVirtualRegister(NewReg))
  364. MRI->setRegAllocationHint(NewReg,
  365. Hint.first == ARMRI::RegPairOdd
  366. ? ARMRI::RegPairEven
  367. : ARMRI::RegPairOdd,
  368. OtherReg);
  369. }
  370. }
  371. }
  372. bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
  373. const MachineFrameInfo &MFI = MF.getFrameInfo();
  374. const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
  375. const ARMFrameLowering *TFI = getFrameLowering(MF);
  376. // If we have stack realignment and VLAs, we have no pointer to use to
  377. // access the stack. If we have stack realignment, and a large call frame,
  378. // we have no place to allocate the emergency spill slot.
  379. if (hasStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
  380. return true;
  381. // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
  382. // negative range for ldr/str (255), and Thumb1 is positive offsets only.
  383. //
  384. // It's going to be better to use the SP or Base Pointer instead. When there
  385. // are variable sized objects, we can't reference off of the SP, so we
  386. // reserve a Base Pointer.
  387. //
  388. // For Thumb2, estimate whether a negative offset from the frame pointer
  389. // will be sufficient to reach the whole stack frame. If a function has a
  390. // smallish frame, it's less likely to have lots of spills and callee saved
  391. // space, so it's all more likely to be within range of the frame pointer.
  392. // If it's wrong, the scavenger will still enable access to work, it just
  393. // won't be optimal. (We should always be able to reach the emergency
  394. // spill slot from the frame pointer.)
  395. if (AFI->isThumb2Function() && MFI.hasVarSizedObjects() &&
  396. MFI.getLocalFrameSize() >= 128)
  397. return true;
  398. // For Thumb1, if sp moves, nothing is in range, so force a base pointer.
  399. // This is necessary for correctness in cases where we need an emergency
  400. // spill slot. (In Thumb1, we can't use a negative offset from the frame
  401. // pointer.)
  402. if (AFI->isThumb1OnlyFunction() && !TFI->hasReservedCallFrame(MF))
  403. return true;
  404. return false;
  405. }
  406. bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
  407. const MachineRegisterInfo *MRI = &MF.getRegInfo();
  408. const ARMFrameLowering *TFI = getFrameLowering(MF);
  409. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  410. // We can't realign the stack if:
  411. // 1. Dynamic stack realignment is explicitly disabled,
  412. // 2. There are VLAs in the function and the base pointer is disabled.
  413. if (!TargetRegisterInfo::canRealignStack(MF))
  414. return false;
  415. // Stack realignment requires a frame pointer. If we already started
  416. // register allocation with frame pointer elimination, it is too late now.
  417. if (!MRI->canReserveReg(STI.getFramePointerReg()))
  418. return false;
  419. // We may also need a base pointer if there are dynamic allocas or stack
  420. // pointer adjustments around calls.
  421. if (TFI->hasReservedCallFrame(MF))
  422. return true;
  423. // A base pointer is required and allowed. Check that it isn't too late to
  424. // reserve it.
  425. return MRI->canReserveReg(BasePtr);
  426. }
  427. bool ARMBaseRegisterInfo::
  428. cannotEliminateFrame(const MachineFunction &MF) const {
  429. const MachineFrameInfo &MFI = MF.getFrameInfo();
  430. if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
  431. return true;
  432. return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
  433. hasStackRealignment(MF);
  434. }
  435. Register
  436. ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
  437. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  438. const ARMFrameLowering *TFI = getFrameLowering(MF);
  439. if (TFI->hasFP(MF))
  440. return STI.getFramePointerReg();
  441. return ARM::SP;
  442. }
  443. /// emitLoadConstPool - Emits a load from constpool to materialize the
  444. /// specified immediate.
  445. void ARMBaseRegisterInfo::emitLoadConstPool(
  446. MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
  447. const DebugLoc &dl, Register DestReg, unsigned SubIdx, int Val,
  448. ARMCC::CondCodes Pred, Register PredReg, unsigned MIFlags) const {
  449. MachineFunction &MF = *MBB.getParent();
  450. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  451. MachineConstantPool *ConstantPool = MF.getConstantPool();
  452. const Constant *C =
  453. ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), Val);
  454. unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
  455. BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
  456. .addReg(DestReg, getDefRegState(true), SubIdx)
  457. .addConstantPoolIndex(Idx)
  458. .addImm(0)
  459. .add(predOps(Pred, PredReg))
  460. .setMIFlags(MIFlags);
  461. }
  462. bool ARMBaseRegisterInfo::
  463. requiresRegisterScavenging(const MachineFunction &MF) const {
  464. return true;
  465. }
  466. bool ARMBaseRegisterInfo::
  467. requiresFrameIndexScavenging(const MachineFunction &MF) const {
  468. return true;
  469. }
  470. bool ARMBaseRegisterInfo::
  471. requiresVirtualBaseRegisters(const MachineFunction &MF) const {
  472. return true;
  473. }
  474. int64_t ARMBaseRegisterInfo::
  475. getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
  476. const MCInstrDesc &Desc = MI->getDesc();
  477. unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
  478. int64_t InstrOffs = 0;
  479. int Scale = 1;
  480. unsigned ImmIdx = 0;
  481. switch (AddrMode) {
  482. case ARMII::AddrModeT2_i8:
  483. case ARMII::AddrModeT2_i8neg:
  484. case ARMII::AddrModeT2_i8pos:
  485. case ARMII::AddrModeT2_i12:
  486. case ARMII::AddrMode_i12:
  487. InstrOffs = MI->getOperand(Idx+1).getImm();
  488. Scale = 1;
  489. break;
  490. case ARMII::AddrMode5: {
  491. // VFP address mode.
  492. const MachineOperand &OffOp = MI->getOperand(Idx+1);
  493. InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
  494. if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
  495. InstrOffs = -InstrOffs;
  496. Scale = 4;
  497. break;
  498. }
  499. case ARMII::AddrMode2:
  500. ImmIdx = Idx+2;
  501. InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
  502. if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  503. InstrOffs = -InstrOffs;
  504. break;
  505. case ARMII::AddrMode3:
  506. ImmIdx = Idx+2;
  507. InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
  508. if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
  509. InstrOffs = -InstrOffs;
  510. break;
  511. case ARMII::AddrModeT1_s:
  512. ImmIdx = Idx+1;
  513. InstrOffs = MI->getOperand(ImmIdx).getImm();
  514. Scale = 4;
  515. break;
  516. default:
  517. llvm_unreachable("Unsupported addressing mode!");
  518. }
  519. return InstrOffs * Scale;
  520. }
  521. /// needsFrameBaseReg - Returns true if the instruction's frame index
  522. /// reference would be better served by a base register other than FP
  523. /// or SP. Used by LocalStackFrameAllocation to determine which frame index
  524. /// references it should create new base registers for.
  525. bool ARMBaseRegisterInfo::
  526. needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
  527. for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
  528. assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
  529. }
  530. // It's the load/store FI references that cause issues, as it can be difficult
  531. // to materialize the offset if it won't fit in the literal field. Estimate
  532. // based on the size of the local frame and some conservative assumptions
  533. // about the rest of the stack frame (note, this is pre-regalloc, so
  534. // we don't know everything for certain yet) whether this offset is likely
  535. // to be out of range of the immediate. Return true if so.
  536. // We only generate virtual base registers for loads and stores, so
  537. // return false for everything else.
  538. unsigned Opc = MI->getOpcode();
  539. switch (Opc) {
  540. case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
  541. case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
  542. case ARM::t2LDRi12: case ARM::t2LDRi8:
  543. case ARM::t2STRi12: case ARM::t2STRi8:
  544. case ARM::VLDRS: case ARM::VLDRD:
  545. case ARM::VSTRS: case ARM::VSTRD:
  546. case ARM::tSTRspi: case ARM::tLDRspi:
  547. break;
  548. default:
  549. return false;
  550. }
  551. // Without a virtual base register, if the function has variable sized
  552. // objects, all fixed-size local references will be via the frame pointer,
  553. // Approximate the offset and see if it's legal for the instruction.
  554. // Note that the incoming offset is based on the SP value at function entry,
  555. // so it'll be negative.
  556. MachineFunction &MF = *MI->getParent()->getParent();
  557. const ARMFrameLowering *TFI = getFrameLowering(MF);
  558. MachineFrameInfo &MFI = MF.getFrameInfo();
  559. ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
  560. // Estimate an offset from the frame pointer.
  561. // Conservatively assume all callee-saved registers get pushed. R4-R6
  562. // will be earlier than the FP, so we ignore those.
  563. // R7, LR
  564. int64_t FPOffset = Offset - 8;
  565. // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
  566. if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
  567. FPOffset -= 80;
  568. // Estimate an offset from the stack pointer.
  569. // The incoming offset is relating to the SP at the start of the function,
  570. // but when we access the local it'll be relative to the SP after local
  571. // allocation, so adjust our SP-relative offset by that allocation size.
  572. Offset += MFI.getLocalFrameSize();
  573. // Assume that we'll have at least some spill slots allocated.
  574. // FIXME: This is a total SWAG number. We should run some statistics
  575. // and pick a real one.
  576. Offset += 128; // 128 bytes of spill slots
  577. // If there's a frame pointer and the addressing mode allows it, try using it.
  578. // The FP is only available if there is no dynamic realignment. We
  579. // don't know for sure yet whether we'll need that, so we guess based
  580. // on whether there are any local variables that would trigger it.
  581. if (TFI->hasFP(MF) &&
  582. !((MFI.getLocalFrameMaxAlign() > TFI->getStackAlign()) &&
  583. canRealignStack(MF))) {
  584. if (isFrameOffsetLegal(MI, getFrameRegister(MF), FPOffset))
  585. return false;
  586. }
  587. // If we can reference via the stack pointer, try that.
  588. // FIXME: This (and the code that resolves the references) can be improved
  589. // to only disallow SP relative references in the live range of
  590. // the VLA(s). In practice, it's unclear how much difference that
  591. // would make, but it may be worth doing.
  592. if (!MFI.hasVarSizedObjects() && isFrameOffsetLegal(MI, ARM::SP, Offset))
  593. return false;
  594. // The offset likely isn't legal, we want to allocate a virtual base register.
  595. return true;
  596. }
  597. /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
  598. /// be a pointer to FrameIdx at the beginning of the basic block.
  599. Register
  600. ARMBaseRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
  601. int FrameIdx,
  602. int64_t Offset) const {
  603. ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
  604. unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
  605. (AFI->isThumb1OnlyFunction() ? ARM::tADDframe : ARM::t2ADDri);
  606. MachineBasicBlock::iterator Ins = MBB->begin();
  607. DebugLoc DL; // Defaults to "unknown"
  608. if (Ins != MBB->end())
  609. DL = Ins->getDebugLoc();
  610. const MachineFunction &MF = *MBB->getParent();
  611. MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
  612. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  613. const MCInstrDesc &MCID = TII.get(ADDriOpc);
  614. Register BaseReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
  615. MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
  616. MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, MCID, BaseReg)
  617. .addFrameIndex(FrameIdx).addImm(Offset);
  618. if (!AFI->isThumb1OnlyFunction())
  619. MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
  620. return BaseReg;
  621. }
  622. void ARMBaseRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
  623. int64_t Offset) const {
  624. MachineBasicBlock &MBB = *MI.getParent();
  625. MachineFunction &MF = *MBB.getParent();
  626. const ARMBaseInstrInfo &TII =
  627. *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
  628. ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
  629. int Off = Offset; // ARM doesn't need the general 64-bit offsets
  630. unsigned i = 0;
  631. assert(!AFI->isThumb1OnlyFunction() &&
  632. "This resolveFrameIndex does not support Thumb1!");
  633. while (!MI.getOperand(i).isFI()) {
  634. ++i;
  635. assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
  636. }
  637. bool Done = false;
  638. if (!AFI->isThumbFunction())
  639. Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
  640. else {
  641. assert(AFI->isThumb2Function());
  642. Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII, this);
  643. }
  644. assert(Done && "Unable to resolve frame index!");
  645. (void)Done;
  646. }
  647. bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
  648. Register BaseReg,
  649. int64_t Offset) const {
  650. const MCInstrDesc &Desc = MI->getDesc();
  651. unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
  652. unsigned i = 0;
  653. for (; !MI->getOperand(i).isFI(); ++i)
  654. assert(i+1 < MI->getNumOperands() && "Instr doesn't have FrameIndex operand!");
  655. // AddrMode4 and AddrMode6 cannot handle any offset.
  656. if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
  657. return Offset == 0;
  658. unsigned NumBits = 0;
  659. unsigned Scale = 1;
  660. bool isSigned = true;
  661. switch (AddrMode) {
  662. case ARMII::AddrModeT2_i8:
  663. case ARMII::AddrModeT2_i8pos:
  664. case ARMII::AddrModeT2_i8neg:
  665. case ARMII::AddrModeT2_i12:
  666. // i8 supports only negative, and i12 supports only positive, so
  667. // based on Offset sign, consider the appropriate instruction
  668. Scale = 1;
  669. if (Offset < 0) {
  670. NumBits = 8;
  671. Offset = -Offset;
  672. } else {
  673. NumBits = 12;
  674. }
  675. break;
  676. case ARMII::AddrMode5:
  677. // VFP address mode.
  678. NumBits = 8;
  679. Scale = 4;
  680. break;
  681. case ARMII::AddrMode_i12:
  682. case ARMII::AddrMode2:
  683. NumBits = 12;
  684. break;
  685. case ARMII::AddrMode3:
  686. NumBits = 8;
  687. break;
  688. case ARMII::AddrModeT1_s:
  689. NumBits = (BaseReg == ARM::SP ? 8 : 5);
  690. Scale = 4;
  691. isSigned = false;
  692. break;
  693. default:
  694. llvm_unreachable("Unsupported addressing mode!");
  695. }
  696. Offset += getFrameIndexInstrOffset(MI, i);
  697. // Make sure the offset is encodable for instructions that scale the
  698. // immediate.
  699. if ((Offset & (Scale-1)) != 0)
  700. return false;
  701. if (isSigned && Offset < 0)
  702. Offset = -Offset;
  703. unsigned Mask = (1 << NumBits) - 1;
  704. if ((unsigned)Offset <= Mask * Scale)
  705. return true;
  706. return false;
  707. }
  708. void
  709. ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
  710. int SPAdj, unsigned FIOperandNum,
  711. RegScavenger *RS) const {
  712. MachineInstr &MI = *II;
  713. MachineBasicBlock &MBB = *MI.getParent();
  714. MachineFunction &MF = *MBB.getParent();
  715. const ARMBaseInstrInfo &TII =
  716. *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
  717. const ARMFrameLowering *TFI = getFrameLowering(MF);
  718. ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
  719. assert(!AFI->isThumb1OnlyFunction() &&
  720. "This eliminateFrameIndex does not support Thumb1!");
  721. int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
  722. Register FrameReg;
  723. int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
  724. // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
  725. // call frame setup/destroy instructions have already been eliminated. That
  726. // means the stack pointer cannot be used to access the emergency spill slot
  727. // when !hasReservedCallFrame().
  728. #ifndef NDEBUG
  729. if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
  730. assert(TFI->hasReservedCallFrame(MF) &&
  731. "Cannot use SP to access the emergency spill slot in "
  732. "functions without a reserved call frame");
  733. assert(!MF.getFrameInfo().hasVarSizedObjects() &&
  734. "Cannot use SP to access the emergency spill slot in "
  735. "functions with variable sized frame objects");
  736. }
  737. #endif // NDEBUG
  738. assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
  739. // Modify MI as necessary to handle as much of 'Offset' as possible
  740. bool Done = false;
  741. if (!AFI->isThumbFunction())
  742. Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
  743. else {
  744. assert(AFI->isThumb2Function());
  745. Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII, this);
  746. }
  747. if (Done)
  748. return;
  749. // If we get here, the immediate doesn't fit into the instruction. We folded
  750. // as much as possible above, handle the rest, providing a register that is
  751. // SP+LargeImm.
  752. assert(
  753. (Offset ||
  754. (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
  755. (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6 ||
  756. (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7 ||
  757. (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7s2 ||
  758. (MI.getDesc().TSFlags & ARMII::AddrModeMask) ==
  759. ARMII::AddrModeT2_i7s4) &&
  760. "This code isn't needed if offset already handled!");
  761. unsigned ScratchReg = 0;
  762. int PIdx = MI.findFirstPredOperandIdx();
  763. ARMCC::CondCodes Pred = (PIdx == -1)
  764. ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
  765. Register PredReg = (PIdx == -1) ? Register() : MI.getOperand(PIdx+1).getReg();
  766. const MCInstrDesc &MCID = MI.getDesc();
  767. const TargetRegisterClass *RegClass =
  768. TII.getRegClass(MCID, FIOperandNum, this, *MI.getParent()->getParent());
  769. if (Offset == 0 &&
  770. (Register::isVirtualRegister(FrameReg) || RegClass->contains(FrameReg)))
  771. // Must be addrmode4/6.
  772. MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
  773. else {
  774. ScratchReg = MF.getRegInfo().createVirtualRegister(RegClass);
  775. if (!AFI->isThumbFunction())
  776. emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
  777. Offset, Pred, PredReg, TII);
  778. else {
  779. assert(AFI->isThumb2Function());
  780. emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
  781. Offset, Pred, PredReg, TII);
  782. }
  783. // Update the original instruction to use the scratch register.
  784. MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
  785. }
  786. }
  787. bool ARMBaseRegisterInfo::shouldCoalesce(MachineInstr *MI,
  788. const TargetRegisterClass *SrcRC,
  789. unsigned SubReg,
  790. const TargetRegisterClass *DstRC,
  791. unsigned DstSubReg,
  792. const TargetRegisterClass *NewRC,
  793. LiveIntervals &LIS) const {
  794. auto MBB = MI->getParent();
  795. auto MF = MBB->getParent();
  796. const MachineRegisterInfo &MRI = MF->getRegInfo();
  797. // If not copying into a sub-register this should be ok because we shouldn't
  798. // need to split the reg.
  799. if (!DstSubReg)
  800. return true;
  801. // Small registers don't frequently cause a problem, so we can coalesce them.
  802. if (getRegSizeInBits(*NewRC) < 256 && getRegSizeInBits(*DstRC) < 256 &&
  803. getRegSizeInBits(*SrcRC) < 256)
  804. return true;
  805. auto NewRCWeight =
  806. MRI.getTargetRegisterInfo()->getRegClassWeight(NewRC);
  807. auto SrcRCWeight =
  808. MRI.getTargetRegisterInfo()->getRegClassWeight(SrcRC);
  809. auto DstRCWeight =
  810. MRI.getTargetRegisterInfo()->getRegClassWeight(DstRC);
  811. // If the source register class is more expensive than the destination, the
  812. // coalescing is probably profitable.
  813. if (SrcRCWeight.RegWeight > NewRCWeight.RegWeight)
  814. return true;
  815. if (DstRCWeight.RegWeight > NewRCWeight.RegWeight)
  816. return true;
  817. // If the register allocator isn't constrained, we can always allow coalescing
  818. // unfortunately we don't know yet if we will be constrained.
  819. // The goal of this heuristic is to restrict how many expensive registers
  820. // we allow to coalesce in a given basic block.
  821. auto AFI = MF->getInfo<ARMFunctionInfo>();
  822. auto It = AFI->getCoalescedWeight(MBB);
  823. LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Coalesced Weight: "
  824. << It->second << "\n");
  825. LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Reg Weight: "
  826. << NewRCWeight.RegWeight << "\n");
  827. // This number is the largest round number that which meets the criteria:
  828. // (1) addresses PR18825
  829. // (2) generates better code in some test cases (like vldm-shed-a9.ll)
  830. // (3) Doesn't regress any test cases (in-tree, test-suite, and SPEC)
  831. // In practice the SizeMultiplier will only factor in for straight line code
  832. // that uses a lot of NEON vectors, which isn't terribly common.
  833. unsigned SizeMultiplier = MBB->size()/100;
  834. SizeMultiplier = SizeMultiplier ? SizeMultiplier : 1;
  835. if (It->second < NewRCWeight.WeightLimit * SizeMultiplier) {
  836. It->second += NewRCWeight.RegWeight;
  837. return true;
  838. }
  839. return false;
  840. }
  841. bool ARMBaseRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
  842. unsigned DefSubReg,
  843. const TargetRegisterClass *SrcRC,
  844. unsigned SrcSubReg) const {
  845. // We can't extract an SPR from an arbitary DPR (as opposed to a DPR_VFP2).
  846. if (DefRC == &ARM::SPRRegClass && DefSubReg == 0 &&
  847. SrcRC == &ARM::DPRRegClass &&
  848. (SrcSubReg == ARM::ssub_0 || SrcSubReg == ARM::ssub_1))
  849. return false;
  850. return TargetRegisterInfo::shouldRewriteCopySrc(DefRC, DefSubReg,
  851. SrcRC, SrcSubReg);
  852. }