PPCRegisterInfo.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. //===-- PPCRegisterInfo.cpp - PowerPC 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 PowerPC implementation of the TargetRegisterInfo
  10. // class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "PPCRegisterInfo.h"
  14. #include "PPCFrameLowering.h"
  15. #include "PPCInstrBuilder.h"
  16. #include "PPCMachineFunctionInfo.h"
  17. #include "PPCSubtarget.h"
  18. #include "PPCTargetMachine.h"
  19. #include "llvm/ADT/BitVector.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/CodeGen/MachineFrameInfo.h"
  23. #include "llvm/CodeGen/MachineFunction.h"
  24. #include "llvm/CodeGen/MachineInstrBuilder.h"
  25. #include "llvm/CodeGen/MachineModuleInfo.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/CodeGen/RegisterScavenging.h"
  28. #include "llvm/CodeGen/TargetFrameLowering.h"
  29. #include "llvm/CodeGen/TargetInstrInfo.h"
  30. #include "llvm/IR/CallingConv.h"
  31. #include "llvm/IR/Constants.h"
  32. #include "llvm/IR/Function.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/ErrorHandling.h"
  37. #include "llvm/Support/MathExtras.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include "llvm/Target/TargetMachine.h"
  40. #include "llvm/Target/TargetOptions.h"
  41. #include <cstdlib>
  42. using namespace llvm;
  43. #define DEBUG_TYPE "reginfo"
  44. #define GET_REGINFO_TARGET_DESC
  45. #include "PPCGenRegisterInfo.inc"
  46. STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass");
  47. STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass");
  48. static cl::opt<bool>
  49. EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true),
  50. cl::desc("Enable use of a base pointer for complex stack frames"));
  51. static cl::opt<bool>
  52. AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false),
  53. cl::desc("Force the use of a base pointer in every function"));
  54. static cl::opt<bool>
  55. EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false),
  56. cl::desc("Enable spills from gpr to vsr rather than stack"));
  57. static cl::opt<bool>
  58. StackPtrConst("ppc-stack-ptr-caller-preserved",
  59. cl::desc("Consider R1 caller preserved so stack saves of "
  60. "caller preserved registers can be LICM candidates"),
  61. cl::init(true), cl::Hidden);
  62. static cl::opt<unsigned>
  63. MaxCRBitSpillDist("ppc-max-crbit-spill-dist",
  64. cl::desc("Maximum search distance for definition of CR bit "
  65. "spill on ppc"),
  66. cl::Hidden, cl::init(100));
  67. // Copies/moves of physical accumulators are expensive operations
  68. // that should be avoided whenever possible. MMA instructions are
  69. // meant to be used in performance-sensitive computational kernels.
  70. // This option is provided, at least for the time being, to give the
  71. // user a tool to detect this expensive operation and either rework
  72. // their code or report a compiler bug if that turns out to be the
  73. // cause.
  74. #ifndef NDEBUG
  75. static cl::opt<bool>
  76. ReportAccMoves("ppc-report-acc-moves",
  77. cl::desc("Emit information about accumulator register spills "
  78. "and copies"),
  79. cl::Hidden, cl::init(false));
  80. #endif
  81. static unsigned offsetMinAlignForOpcode(unsigned OpC);
  82. PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM)
  83. : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR,
  84. TM.isPPC64() ? 0 : 1,
  85. TM.isPPC64() ? 0 : 1),
  86. TM(TM) {
  87. ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
  88. ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
  89. ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
  90. ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
  91. ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
  92. ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
  93. ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
  94. ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
  95. ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32;
  96. // 64-bit
  97. ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
  98. ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
  99. ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
  100. ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
  101. ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
  102. // VSX
  103. ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX;
  104. ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX;
  105. ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX;
  106. ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX;
  107. ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX;
  108. ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX;
  109. ImmToIdxMap[PPC::LXV] = PPC::LXVX;
  110. ImmToIdxMap[PPC::LXSD] = PPC::LXSDX;
  111. ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX;
  112. ImmToIdxMap[PPC::STXV] = PPC::STXVX;
  113. ImmToIdxMap[PPC::STXSD] = PPC::STXSDX;
  114. ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX;
  115. // SPE
  116. ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX;
  117. ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX;
  118. ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX;
  119. ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX;
  120. // Power10
  121. ImmToIdxMap[PPC::PLBZ] = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8] = PPC::LBZX8;
  122. ImmToIdxMap[PPC::PLHZ] = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8] = PPC::LHZX8;
  123. ImmToIdxMap[PPC::PLHA] = PPC::LHAX; ImmToIdxMap[PPC::PLHA8] = PPC::LHAX8;
  124. ImmToIdxMap[PPC::PLWZ] = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8] = PPC::LWZX8;
  125. ImmToIdxMap[PPC::PLWA] = PPC::LWAX; ImmToIdxMap[PPC::PLWA8] = PPC::LWAX;
  126. ImmToIdxMap[PPC::PLD] = PPC::LDX; ImmToIdxMap[PPC::PSTD] = PPC::STDX;
  127. ImmToIdxMap[PPC::PSTB] = PPC::STBX; ImmToIdxMap[PPC::PSTB8] = PPC::STBX8;
  128. ImmToIdxMap[PPC::PSTH] = PPC::STHX; ImmToIdxMap[PPC::PSTH8] = PPC::STHX8;
  129. ImmToIdxMap[PPC::PSTW] = PPC::STWX; ImmToIdxMap[PPC::PSTW8] = PPC::STWX8;
  130. ImmToIdxMap[PPC::PLFS] = PPC::LFSX; ImmToIdxMap[PPC::PSTFS] = PPC::STFSX;
  131. ImmToIdxMap[PPC::PLFD] = PPC::LFDX; ImmToIdxMap[PPC::PSTFD] = PPC::STFDX;
  132. ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX;
  133. ImmToIdxMap[PPC::PLXSD] = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD] = PPC::STXSDX;
  134. ImmToIdxMap[PPC::PLXV] = PPC::LXVX; ImmToIdxMap[PPC::PSTXV] = PPC::STXVX;
  135. ImmToIdxMap[PPC::LXVP] = PPC::LXVPX;
  136. ImmToIdxMap[PPC::STXVP] = PPC::STXVPX;
  137. ImmToIdxMap[PPC::PLXVP] = PPC::LXVPX;
  138. ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX;
  139. }
  140. /// getPointerRegClass - Return the register class to use to hold pointers.
  141. /// This is used for addressing modes.
  142. const TargetRegisterClass *
  143. PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
  144. const {
  145. // Note that PPCInstrInfo::FoldImmediate also directly uses this Kind value
  146. // when it checks for ZERO folding.
  147. if (Kind == 1) {
  148. if (TM.isPPC64())
  149. return &PPC::G8RC_NOX0RegClass;
  150. return &PPC::GPRC_NOR0RegClass;
  151. }
  152. if (TM.isPPC64())
  153. return &PPC::G8RCRegClass;
  154. return &PPC::GPRCRegClass;
  155. }
  156. const MCPhysReg*
  157. PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
  158. const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
  159. if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) {
  160. if (!TM.isPPC64() && Subtarget.isAIXABI())
  161. report_fatal_error("AnyReg unimplemented on 32-bit AIX.");
  162. if (Subtarget.hasVSX()) {
  163. if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
  164. return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList;
  165. return CSR_64_AllRegs_VSX_SaveList;
  166. }
  167. if (Subtarget.hasAltivec()) {
  168. if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
  169. return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList;
  170. return CSR_64_AllRegs_Altivec_SaveList;
  171. }
  172. return CSR_64_AllRegs_SaveList;
  173. }
  174. // On PPC64, we might need to save r2 (but only if it is not reserved).
  175. // We do not need to treat R2 as callee-saved when using PC-Relative calls
  176. // because any direct uses of R2 will cause it to be reserved. If the function
  177. // is a leaf or the only uses of R2 are implicit uses for calls, the calls
  178. // will use the @notoc relocation which will cause this function to set the
  179. // st_other bit to 1, thereby communicating to its caller that it arbitrarily
  180. // clobbers the TOC.
  181. bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) &&
  182. !Subtarget.isUsingPCRelativeCalls();
  183. // Cold calling convention CSRs.
  184. if (MF->getFunction().getCallingConv() == CallingConv::Cold) {
  185. if (Subtarget.isAIXABI())
  186. report_fatal_error("Cold calling unimplemented on AIX.");
  187. if (TM.isPPC64()) {
  188. if (Subtarget.hasAltivec())
  189. return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList
  190. : CSR_SVR64_ColdCC_Altivec_SaveList;
  191. return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList
  192. : CSR_SVR64_ColdCC_SaveList;
  193. }
  194. // 32-bit targets.
  195. if (Subtarget.hasAltivec())
  196. return CSR_SVR32_ColdCC_Altivec_SaveList;
  197. else if (Subtarget.hasSPE())
  198. return CSR_SVR32_ColdCC_SPE_SaveList;
  199. return CSR_SVR32_ColdCC_SaveList;
  200. }
  201. // Standard calling convention CSRs.
  202. if (TM.isPPC64()) {
  203. if (Subtarget.hasAltivec() &&
  204. (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) {
  205. return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList
  206. : CSR_PPC64_Altivec_SaveList;
  207. }
  208. return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList;
  209. }
  210. // 32-bit targets.
  211. if (Subtarget.isAIXABI()) {
  212. if (Subtarget.hasAltivec())
  213. return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList
  214. : CSR_AIX32_SaveList;
  215. return CSR_AIX32_SaveList;
  216. }
  217. if (Subtarget.hasAltivec())
  218. return CSR_SVR432_Altivec_SaveList;
  219. else if (Subtarget.hasSPE())
  220. return CSR_SVR432_SPE_SaveList;
  221. return CSR_SVR432_SaveList;
  222. }
  223. const uint32_t *
  224. PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
  225. CallingConv::ID CC) const {
  226. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  227. if (CC == CallingConv::AnyReg) {
  228. if (Subtarget.hasVSX()) {
  229. if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
  230. return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask;
  231. return CSR_64_AllRegs_VSX_RegMask;
  232. }
  233. if (Subtarget.hasAltivec()) {
  234. if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI())
  235. return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask;
  236. return CSR_64_AllRegs_Altivec_RegMask;
  237. }
  238. return CSR_64_AllRegs_RegMask;
  239. }
  240. if (Subtarget.isAIXABI()) {
  241. return TM.isPPC64()
  242. ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
  243. ? CSR_PPC64_Altivec_RegMask
  244. : CSR_PPC64_RegMask)
  245. : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI())
  246. ? CSR_AIX32_Altivec_RegMask
  247. : CSR_AIX32_RegMask);
  248. }
  249. if (CC == CallingConv::Cold) {
  250. return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask
  251. : CSR_SVR64_ColdCC_RegMask)
  252. : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask
  253. : (Subtarget.hasSPE()
  254. ? CSR_SVR32_ColdCC_SPE_RegMask
  255. : CSR_SVR32_ColdCC_RegMask));
  256. }
  257. return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask
  258. : CSR_PPC64_RegMask)
  259. : (Subtarget.hasAltivec()
  260. ? CSR_SVR432_Altivec_RegMask
  261. : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask
  262. : CSR_SVR432_RegMask));
  263. }
  264. const uint32_t*
  265. PPCRegisterInfo::getNoPreservedMask() const {
  266. return CSR_NoRegs_RegMask;
  267. }
  268. void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const {
  269. for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM})
  270. Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32));
  271. }
  272. BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
  273. BitVector Reserved(getNumRegs());
  274. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  275. const PPCFrameLowering *TFI = getFrameLowering(MF);
  276. // The ZERO register is not really a register, but the representation of r0
  277. // when used in instructions that treat r0 as the constant 0.
  278. markSuperRegs(Reserved, PPC::ZERO);
  279. // The FP register is also not really a register, but is the representation
  280. // of the frame pointer register used by ISD::FRAMEADDR.
  281. markSuperRegs(Reserved, PPC::FP);
  282. // The BP register is also not really a register, but is the representation
  283. // of the base pointer register used by setjmp.
  284. markSuperRegs(Reserved, PPC::BP);
  285. // The counter registers must be reserved so that counter-based loops can
  286. // be correctly formed (and the mtctr instructions are not DCE'd).
  287. markSuperRegs(Reserved, PPC::CTR);
  288. markSuperRegs(Reserved, PPC::CTR8);
  289. markSuperRegs(Reserved, PPC::R1);
  290. markSuperRegs(Reserved, PPC::LR);
  291. markSuperRegs(Reserved, PPC::LR8);
  292. markSuperRegs(Reserved, PPC::RM);
  293. markSuperRegs(Reserved, PPC::VRSAVE);
  294. // The SVR4 ABI reserves r2 and r13
  295. if (Subtarget.isSVR4ABI()) {
  296. // We only reserve r2 if we need to use the TOC pointer. If we have no
  297. // explicit uses of the TOC pointer (meaning we're a leaf function with
  298. // no constant-pool loads, etc.) and we have no potential uses inside an
  299. // inline asm block, then we can treat r2 has an ordinary callee-saved
  300. // register.
  301. const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
  302. if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm())
  303. markSuperRegs(Reserved, PPC::R2); // System-reserved register
  304. markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register
  305. }
  306. // Always reserve r2 on AIX for now.
  307. // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions.
  308. if (Subtarget.isAIXABI())
  309. markSuperRegs(Reserved, PPC::R2); // System-reserved register
  310. // On PPC64, r13 is the thread pointer. Never allocate this register.
  311. if (TM.isPPC64())
  312. markSuperRegs(Reserved, PPC::R13);
  313. if (TFI->needsFP(MF))
  314. markSuperRegs(Reserved, PPC::R31);
  315. bool IsPositionIndependent = TM.isPositionIndependent();
  316. if (hasBasePointer(MF)) {
  317. if (Subtarget.is32BitELFABI() && IsPositionIndependent)
  318. markSuperRegs(Reserved, PPC::R29);
  319. else
  320. markSuperRegs(Reserved, PPC::R30);
  321. }
  322. if (Subtarget.is32BitELFABI() && IsPositionIndependent)
  323. markSuperRegs(Reserved, PPC::R30);
  324. // Reserve Altivec registers when Altivec is unavailable.
  325. if (!Subtarget.hasAltivec())
  326. for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(),
  327. IE = PPC::VRRCRegClass.end(); I != IE; ++I)
  328. markSuperRegs(Reserved, *I);
  329. if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
  330. !TM.getAIXExtendedAltivecABI()) {
  331. // In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved
  332. // and cannot be used.
  333. for (auto Reg : CSR_Altivec_SaveList) {
  334. if (Reg == 0)
  335. break;
  336. markSuperRegs(Reserved, Reg);
  337. for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) {
  338. Reserved.set(*AS);
  339. }
  340. }
  341. }
  342. assert(checkAllSuperRegsMarked(Reserved));
  343. return Reserved;
  344. }
  345. bool PPCRegisterInfo::isAsmClobberable(const MachineFunction &MF,
  346. MCRegister PhysReg) const {
  347. // We cannot use getReservedRegs() to find the registers that are not asm
  348. // clobberable because there are some reserved registers which can be
  349. // clobbered by inline asm. For example, when LR is clobbered, the register is
  350. // saved and restored. We will hardcode the registers that are not asm
  351. // cloberable in this function.
  352. // The stack pointer (R1/X1) is not clobberable by inline asm
  353. return PhysReg != PPC::R1 && PhysReg != PPC::X1;
  354. }
  355. bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const {
  356. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  357. const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo();
  358. const MachineFrameInfo &MFI = MF.getFrameInfo();
  359. const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo();
  360. LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName()
  361. << ".\n");
  362. // If the callee saved info is invalid we have to default to true for safety.
  363. if (!MFI.isCalleeSavedInfoValid()) {
  364. LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n");
  365. return true;
  366. }
  367. // We will require the use of X-Forms because the frame is larger than what
  368. // can be represented in signed 16 bits that fit in the immediate of a D-Form.
  369. // If we need an X-Form then we need a register to store the address offset.
  370. unsigned FrameSize = MFI.getStackSize();
  371. // Signed 16 bits means that the FrameSize cannot be more than 15 bits.
  372. if (FrameSize & ~0x7FFF) {
  373. LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n");
  374. return true;
  375. }
  376. // The callee saved info is valid so it can be traversed.
  377. // Checking for registers that need saving that do not have load or store
  378. // forms where the address offset is an immediate.
  379. for (unsigned i = 0; i < Info.size(); i++) {
  380. // If the spill is to a register no scavenging is required.
  381. if (Info[i].isSpilledToReg())
  382. continue;
  383. int FrIdx = Info[i].getFrameIdx();
  384. Register Reg = Info[i].getReg();
  385. const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg);
  386. unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC);
  387. if (!MFI.isFixedObjectIndex(FrIdx)) {
  388. // This is not a fixed object. If it requires alignment then we may still
  389. // need to use the XForm.
  390. if (offsetMinAlignForOpcode(Opcode) > 1) {
  391. LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
  392. << " for register " << printReg(Reg, this) << ".\n");
  393. LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires "
  394. << "alignment.\n");
  395. return true;
  396. }
  397. }
  398. // This is eiher:
  399. // 1) A fixed frame index object which we know are aligned so
  400. // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't
  401. // need to consider the alignment here.
  402. // 2) A not fixed object but in that case we now know that the min required
  403. // alignment is no more than 1 based on the previous check.
  404. if (InstrInfo->isXFormMemOp(Opcode)) {
  405. LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode)
  406. << " for register " << printReg(Reg, this) << ".\n");
  407. LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n");
  408. return true;
  409. }
  410. }
  411. LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n");
  412. return false;
  413. }
  414. bool PPCRegisterInfo::requiresVirtualBaseRegisters(
  415. const MachineFunction &MF) const {
  416. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  417. // Do not use virtual base registers when ROP protection is turned on.
  418. // Virtual base registers break the layout of the local variable space and may
  419. // push the ROP Hash location past the 512 byte range of the ROP store
  420. // instruction.
  421. return !Subtarget.hasROPProtect();
  422. }
  423. bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg,
  424. const MachineFunction &MF) const {
  425. assert(Register::isPhysicalRegister(PhysReg));
  426. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  427. const MachineFrameInfo &MFI = MF.getFrameInfo();
  428. if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI())
  429. return false;
  430. if (PhysReg == Subtarget.getTOCPointerRegister())
  431. // X2/R2 is guaranteed to be preserved within a function if it is reserved.
  432. // The reason it's reserved is that it's the TOC pointer (and the function
  433. // uses the TOC). In functions where it isn't reserved (i.e. leaf functions
  434. // with no TOC access), we can't claim that it is preserved.
  435. return (getReservedRegs(MF).test(PhysReg));
  436. if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() &&
  437. !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
  438. // The value of the stack pointer does not change within a function after
  439. // the prologue and before the epilogue if there are no dynamic allocations
  440. // and no inline asm which clobbers X1/R1.
  441. return true;
  442. return false;
  443. }
  444. bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg,
  445. ArrayRef<MCPhysReg> Order,
  446. SmallVectorImpl<MCPhysReg> &Hints,
  447. const MachineFunction &MF,
  448. const VirtRegMap *VRM,
  449. const LiveRegMatrix *Matrix) const {
  450. const MachineRegisterInfo *MRI = &MF.getRegInfo();
  451. // Call the base implementation first to set any hints based on the usual
  452. // heuristics and decide what the return value should be. We want to return
  453. // the same value returned by the base implementation. If the base
  454. // implementation decides to return true and force the allocation then we
  455. // will leave it as such. On the other hand if the base implementation
  456. // decides to return false the following code will not force the allocation
  457. // as we are just looking to provide a hint.
  458. bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
  459. VirtReg, Order, Hints, MF, VRM, Matrix);
  460. // We are interested in instructions that copy values to ACC/UACC.
  461. // The copy into UACC will be simply a COPY to a subreg so we
  462. // want to allocate the corresponding physical subreg for the source.
  463. // The copy into ACC will be a BUILD_UACC so we want to allocate
  464. // the same number UACC for the source.
  465. for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) {
  466. const MachineOperand *ResultOp = nullptr;
  467. Register ResultReg;
  468. switch (Use.getOpcode()) {
  469. case TargetOpcode::COPY: {
  470. ResultOp = &Use.getOperand(0);
  471. ResultReg = ResultOp->getReg();
  472. if (Register::isVirtualRegister(ResultReg) &&
  473. MRI->getRegClass(ResultReg)->contains(PPC::UACC0) &&
  474. VRM->hasPhys(ResultReg)) {
  475. Register UACCPhys = VRM->getPhys(ResultReg);
  476. Register HintReg = getSubReg(UACCPhys, ResultOp->getSubReg());
  477. // Ensure that the hint is a VSRp register.
  478. if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31)
  479. Hints.push_back(HintReg);
  480. }
  481. break;
  482. }
  483. case PPC::BUILD_UACC: {
  484. ResultOp = &Use.getOperand(0);
  485. ResultReg = ResultOp->getReg();
  486. if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) &&
  487. VRM->hasPhys(ResultReg)) {
  488. Register ACCPhys = VRM->getPhys(ResultReg);
  489. assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) &&
  490. "Expecting an ACC register for BUILD_UACC.");
  491. Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0);
  492. Hints.push_back(HintReg);
  493. }
  494. break;
  495. }
  496. }
  497. }
  498. return BaseImplRetVal;
  499. }
  500. unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
  501. MachineFunction &MF) const {
  502. const PPCFrameLowering *TFI = getFrameLowering(MF);
  503. const unsigned DefaultSafety = 1;
  504. switch (RC->getID()) {
  505. default:
  506. return 0;
  507. case PPC::G8RC_NOX0RegClassID:
  508. case PPC::GPRC_NOR0RegClassID:
  509. case PPC::SPERCRegClassID:
  510. case PPC::G8RCRegClassID:
  511. case PPC::GPRCRegClassID: {
  512. unsigned FP = TFI->hasFP(MF) ? 1 : 0;
  513. return 32 - FP - DefaultSafety;
  514. }
  515. case PPC::F4RCRegClassID:
  516. case PPC::F8RCRegClassID:
  517. case PPC::VSLRCRegClassID:
  518. return 32 - DefaultSafety;
  519. case PPC::VFRCRegClassID:
  520. case PPC::VRRCRegClassID: {
  521. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  522. // Vector registers VR20-VR31 are reserved and cannot be used in the default
  523. // Altivec ABI on AIX.
  524. if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
  525. return 20 - DefaultSafety;
  526. }
  527. return 32 - DefaultSafety;
  528. case PPC::VSFRCRegClassID:
  529. case PPC::VSSRCRegClassID:
  530. case PPC::VSRCRegClassID: {
  531. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  532. if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI())
  533. // Vector registers VR20-VR31 are reserved and cannot be used in the
  534. // default Altivec ABI on AIX.
  535. return 52 - DefaultSafety;
  536. }
  537. return 64 - DefaultSafety;
  538. case PPC::CRRCRegClassID:
  539. return 8 - DefaultSafety;
  540. }
  541. }
  542. const TargetRegisterClass *
  543. PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
  544. const MachineFunction &MF) const {
  545. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  546. const auto *DefaultSuperclass =
  547. TargetRegisterInfo::getLargestLegalSuperClass(RC, MF);
  548. if (Subtarget.hasVSX()) {
  549. // With VSX, we can inflate various sub-register classes to the full VSX
  550. // register set.
  551. // For Power9 we allow the user to enable GPR to vector spills.
  552. // FIXME: Currently limited to spilling GP8RC. A follow on patch will add
  553. // support to spill GPRC.
  554. if (TM.isELFv2ABI() || Subtarget.isAIXABI()) {
  555. if (Subtarget.hasP9Vector() && EnableGPRToVecSpills &&
  556. RC == &PPC::G8RCRegClass) {
  557. InflateGP8RC++;
  558. return &PPC::SPILLTOVSRRCRegClass;
  559. }
  560. if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills)
  561. InflateGPRC++;
  562. }
  563. for (const auto *I = RC->getSuperClasses(); *I; ++I) {
  564. if (getRegSizeInBits(**I) != getRegSizeInBits(*RC))
  565. continue;
  566. switch ((*I)->getID()) {
  567. case PPC::VSSRCRegClassID:
  568. return Subtarget.hasP8Vector() ? *I : DefaultSuperclass;
  569. case PPC::VSFRCRegClassID:
  570. case PPC::VSRCRegClassID:
  571. return *I;
  572. case PPC::VSRpRCRegClassID:
  573. return Subtarget.pairedVectorMemops() ? *I : DefaultSuperclass;
  574. case PPC::ACCRCRegClassID:
  575. case PPC::UACCRCRegClassID:
  576. return Subtarget.hasMMA() ? *I : DefaultSuperclass;
  577. }
  578. }
  579. }
  580. return DefaultSuperclass;
  581. }
  582. //===----------------------------------------------------------------------===//
  583. // Stack Frame Processing methods
  584. //===----------------------------------------------------------------------===//
  585. /// lowerDynamicAlloc - Generate the code for allocating an object in the
  586. /// current frame. The sequence of code will be in the general form
  587. ///
  588. /// addi R0, SP, \#frameSize ; get the address of the previous frame
  589. /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
  590. /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
  591. ///
  592. void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
  593. // Get the instruction.
  594. MachineInstr &MI = *II;
  595. // Get the instruction's basic block.
  596. MachineBasicBlock &MBB = *MI.getParent();
  597. // Get the basic block's function.
  598. MachineFunction &MF = *MBB.getParent();
  599. // Get the frame info.
  600. MachineFrameInfo &MFI = MF.getFrameInfo();
  601. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  602. // Get the instruction info.
  603. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  604. // Determine whether 64-bit pointers are used.
  605. bool LP64 = TM.isPPC64();
  606. DebugLoc dl = MI.getDebugLoc();
  607. // Get the maximum call stack size.
  608. unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
  609. Align MaxAlign = MFI.getMaxAlign();
  610. assert(isAligned(MaxAlign, maxCallFrameSize) &&
  611. "Maximum call-frame size not sufficiently aligned");
  612. (void)MaxAlign;
  613. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  614. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  615. Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  616. bool KillNegSizeReg = MI.getOperand(1).isKill();
  617. Register NegSizeReg = MI.getOperand(1).getReg();
  618. prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg);
  619. // Grow the stack and update the stack pointer link, then determine the
  620. // address of new allocated space.
  621. if (LP64) {
  622. BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1)
  623. .addReg(Reg, RegState::Kill)
  624. .addReg(PPC::X1)
  625. .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
  626. BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
  627. .addReg(PPC::X1)
  628. .addImm(maxCallFrameSize);
  629. } else {
  630. BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1)
  631. .addReg(Reg, RegState::Kill)
  632. .addReg(PPC::R1)
  633. .addReg(NegSizeReg, getKillRegState(KillNegSizeReg));
  634. BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
  635. .addReg(PPC::R1)
  636. .addImm(maxCallFrameSize);
  637. }
  638. // Discard the DYNALLOC instruction.
  639. MBB.erase(II);
  640. }
  641. /// To accomplish dynamic stack allocation, we have to calculate exact size
  642. /// subtracted from the stack pointer according alignment information and get
  643. /// previous frame pointer.
  644. void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II,
  645. Register &NegSizeReg,
  646. bool &KillNegSizeReg,
  647. Register &FramePointer) const {
  648. // Get the instruction.
  649. MachineInstr &MI = *II;
  650. // Get the instruction's basic block.
  651. MachineBasicBlock &MBB = *MI.getParent();
  652. // Get the basic block's function.
  653. MachineFunction &MF = *MBB.getParent();
  654. // Get the frame info.
  655. MachineFrameInfo &MFI = MF.getFrameInfo();
  656. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  657. // Get the instruction info.
  658. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  659. // Determine whether 64-bit pointers are used.
  660. bool LP64 = TM.isPPC64();
  661. DebugLoc dl = MI.getDebugLoc();
  662. // Get the total frame size.
  663. unsigned FrameSize = MFI.getStackSize();
  664. // Get stack alignments.
  665. const PPCFrameLowering *TFI = getFrameLowering(MF);
  666. Align TargetAlign = TFI->getStackAlign();
  667. Align MaxAlign = MFI.getMaxAlign();
  668. // Determine the previous frame's address. If FrameSize can't be
  669. // represented as 16 bits or we need special alignment, then we load the
  670. // previous frame's address from 0(SP). Why not do an addis of the hi?
  671. // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
  672. // Constructing the constant and adding would take 3 instructions.
  673. // Fortunately, a frame greater than 32K is rare.
  674. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  675. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  676. if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
  677. if (LP64)
  678. BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer)
  679. .addReg(PPC::X31)
  680. .addImm(FrameSize);
  681. else
  682. BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer)
  683. .addReg(PPC::R31)
  684. .addImm(FrameSize);
  685. } else if (LP64) {
  686. BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer)
  687. .addImm(0)
  688. .addReg(PPC::X1);
  689. } else {
  690. BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer)
  691. .addImm(0)
  692. .addReg(PPC::R1);
  693. }
  694. // Determine the actual NegSizeReg according to alignment info.
  695. if (LP64) {
  696. if (MaxAlign > TargetAlign) {
  697. unsigned UnalNegSizeReg = NegSizeReg;
  698. NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
  699. // Unfortunately, there is no andi, only andi., and we can't insert that
  700. // here because we might clobber cr0 while it is live.
  701. BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg)
  702. .addImm(~(MaxAlign.value() - 1));
  703. unsigned NegSizeReg1 = NegSizeReg;
  704. NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC);
  705. BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg)
  706. .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
  707. .addReg(NegSizeReg1, RegState::Kill);
  708. KillNegSizeReg = true;
  709. }
  710. } else {
  711. if (MaxAlign > TargetAlign) {
  712. unsigned UnalNegSizeReg = NegSizeReg;
  713. NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
  714. // Unfortunately, there is no andi, only andi., and we can't insert that
  715. // here because we might clobber cr0 while it is live.
  716. BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg)
  717. .addImm(~(MaxAlign.value() - 1));
  718. unsigned NegSizeReg1 = NegSizeReg;
  719. NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC);
  720. BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg)
  721. .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg))
  722. .addReg(NegSizeReg1, RegState::Kill);
  723. KillNegSizeReg = true;
  724. }
  725. }
  726. }
  727. void PPCRegisterInfo::lowerPrepareProbedAlloca(
  728. MachineBasicBlock::iterator II) const {
  729. MachineInstr &MI = *II;
  730. // Get the instruction's basic block.
  731. MachineBasicBlock &MBB = *MI.getParent();
  732. // Get the basic block's function.
  733. MachineFunction &MF = *MBB.getParent();
  734. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  735. // Get the instruction info.
  736. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  737. // Determine whether 64-bit pointers are used.
  738. bool LP64 = TM.isPPC64();
  739. DebugLoc dl = MI.getDebugLoc();
  740. Register FramePointer = MI.getOperand(0).getReg();
  741. const Register ActualNegSizeReg = MI.getOperand(1).getReg();
  742. bool KillNegSizeReg = MI.getOperand(2).isKill();
  743. Register NegSizeReg = MI.getOperand(2).getReg();
  744. const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR);
  745. // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg.
  746. if (FramePointer == NegSizeReg) {
  747. assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, "
  748. "NegSizeReg should be killed");
  749. // FramePointer is clobbered earlier than the use of NegSizeReg in
  750. // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid
  751. // misuse.
  752. BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
  753. .addReg(NegSizeReg)
  754. .addReg(NegSizeReg);
  755. NegSizeReg = ActualNegSizeReg;
  756. KillNegSizeReg = false;
  757. }
  758. prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer);
  759. // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign >
  760. // TargetAlign.
  761. if (NegSizeReg != ActualNegSizeReg)
  762. BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg)
  763. .addReg(NegSizeReg)
  764. .addReg(NegSizeReg);
  765. MBB.erase(II);
  766. }
  767. void PPCRegisterInfo::lowerDynamicAreaOffset(
  768. MachineBasicBlock::iterator II) const {
  769. // Get the instruction.
  770. MachineInstr &MI = *II;
  771. // Get the instruction's basic block.
  772. MachineBasicBlock &MBB = *MI.getParent();
  773. // Get the basic block's function.
  774. MachineFunction &MF = *MBB.getParent();
  775. // Get the frame info.
  776. MachineFrameInfo &MFI = MF.getFrameInfo();
  777. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  778. // Get the instruction info.
  779. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  780. unsigned maxCallFrameSize = MFI.getMaxCallFrameSize();
  781. bool is64Bit = TM.isPPC64();
  782. DebugLoc dl = MI.getDebugLoc();
  783. BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI),
  784. MI.getOperand(0).getReg())
  785. .addImm(maxCallFrameSize);
  786. MBB.erase(II);
  787. }
  788. /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
  789. /// reserving a whole register (R0), we scrounge for one here. This generates
  790. /// code like this:
  791. ///
  792. /// mfcr rA ; Move the conditional register into GPR rA.
  793. /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
  794. /// stw rA, FI ; Store rA to the frame.
  795. ///
  796. void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
  797. unsigned FrameIndex) const {
  798. // Get the instruction.
  799. MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>
  800. // Get the instruction's basic block.
  801. MachineBasicBlock &MBB = *MI.getParent();
  802. MachineFunction &MF = *MBB.getParent();
  803. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  804. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  805. DebugLoc dl = MI.getDebugLoc();
  806. bool LP64 = TM.isPPC64();
  807. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  808. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  809. Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  810. Register SrcReg = MI.getOperand(0).getReg();
  811. // We need to store the CR in the low 4-bits of the saved value. First, issue
  812. // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg.
  813. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
  814. .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
  815. // If the saved register wasn't CR0, shift the bits left so that they are in
  816. // CR0's slot.
  817. if (SrcReg != PPC::CR0) {
  818. Register Reg1 = Reg;
  819. Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  820. // rlwinm rA, rA, ShiftBits, 0, 31.
  821. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
  822. .addReg(Reg1, RegState::Kill)
  823. .addImm(getEncodingValue(SrcReg) * 4)
  824. .addImm(0)
  825. .addImm(31);
  826. }
  827. addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
  828. .addReg(Reg, RegState::Kill),
  829. FrameIndex);
  830. // Discard the pseudo instruction.
  831. MBB.erase(II);
  832. }
  833. void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II,
  834. unsigned FrameIndex) const {
  835. // Get the instruction.
  836. MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset>
  837. // Get the instruction's basic block.
  838. MachineBasicBlock &MBB = *MI.getParent();
  839. MachineFunction &MF = *MBB.getParent();
  840. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  841. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  842. DebugLoc dl = MI.getDebugLoc();
  843. bool LP64 = TM.isPPC64();
  844. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  845. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  846. Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  847. Register DestReg = MI.getOperand(0).getReg();
  848. assert(MI.definesRegister(DestReg) &&
  849. "RESTORE_CR does not define its destination");
  850. addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
  851. Reg), FrameIndex);
  852. // If the reloaded register isn't CR0, shift the bits right so that they are
  853. // in the right CR's slot.
  854. if (DestReg != PPC::CR0) {
  855. Register Reg1 = Reg;
  856. Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  857. unsigned ShiftBits = getEncodingValue(DestReg)*4;
  858. // rlwinm r11, r11, 32-ShiftBits, 0, 31.
  859. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
  860. .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0)
  861. .addImm(31);
  862. }
  863. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg)
  864. .addReg(Reg, RegState::Kill);
  865. // Discard the pseudo instruction.
  866. MBB.erase(II);
  867. }
  868. void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II,
  869. unsigned FrameIndex) const {
  870. // Get the instruction.
  871. MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset>
  872. // Get the instruction's basic block.
  873. MachineBasicBlock &MBB = *MI.getParent();
  874. MachineFunction &MF = *MBB.getParent();
  875. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  876. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  877. const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo();
  878. DebugLoc dl = MI.getDebugLoc();
  879. bool LP64 = TM.isPPC64();
  880. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  881. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  882. Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  883. Register SrcReg = MI.getOperand(0).getReg();
  884. // Search up the BB to find the definition of the CR bit.
  885. MachineBasicBlock::reverse_iterator Ins = MI;
  886. MachineBasicBlock::reverse_iterator Rend = MBB.rend();
  887. ++Ins;
  888. unsigned CRBitSpillDistance = 0;
  889. bool SeenUse = false;
  890. for (; Ins != Rend; ++Ins) {
  891. // Definition found.
  892. if (Ins->modifiesRegister(SrcReg, TRI))
  893. break;
  894. // Use found.
  895. if (Ins->readsRegister(SrcReg, TRI))
  896. SeenUse = true;
  897. // Unable to find CR bit definition within maximum search distance.
  898. if (CRBitSpillDistance == MaxCRBitSpillDist) {
  899. Ins = MI;
  900. break;
  901. }
  902. // Skip debug instructions when counting CR bit spill distance.
  903. if (!Ins->isDebugInstr())
  904. CRBitSpillDistance++;
  905. }
  906. // Unable to find the definition of the CR bit in the MBB.
  907. if (Ins == MBB.rend())
  908. Ins = MI;
  909. bool SpillsKnownBit = false;
  910. // There is no need to extract the CR bit if its value is already known.
  911. switch (Ins->getOpcode()) {
  912. case PPC::CRUNSET:
  913. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg)
  914. .addImm(0);
  915. SpillsKnownBit = true;
  916. break;
  917. case PPC::CRSET:
  918. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg)
  919. .addImm(-32768);
  920. SpillsKnownBit = true;
  921. break;
  922. default:
  923. // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all
  924. // bits (specifically, it produces a -1 if the CR bit is set). Ultimately,
  925. // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit
  926. // register), and SETNBC will set this.
  927. if (Subtarget.isISA3_1()) {
  928. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg)
  929. .addReg(SrcReg, RegState::Undef);
  930. break;
  931. }
  932. // On Power9, we can use SETB to extract the LT bit. This only works for
  933. // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value
  934. // of the bit we care about (32-bit sign bit) will be set to the value of
  935. // the LT bit (regardless of the other bits in the CR field).
  936. if (Subtarget.isISA3_0()) {
  937. if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT ||
  938. SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT ||
  939. SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT ||
  940. SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) {
  941. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg)
  942. .addReg(getCRFromCRBit(SrcReg), RegState::Undef);
  943. break;
  944. }
  945. }
  946. // We need to move the CR field that contains the CR bit we are spilling.
  947. // The super register may not be explicitly defined (i.e. it can be defined
  948. // by a CR-logical that only defines the subreg) so we state that the CR
  949. // field is undef. Also, in order to preserve the kill flag on the CR bit,
  950. // we add it as an implicit use.
  951. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg)
  952. .addReg(getCRFromCRBit(SrcReg), RegState::Undef)
  953. .addReg(SrcReg,
  954. RegState::Implicit | getKillRegState(MI.getOperand(0).isKill()));
  955. // If the saved register wasn't CR0LT, shift the bits left so that the bit
  956. // to store is the first one. Mask all but that bit.
  957. Register Reg1 = Reg;
  958. Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  959. // rlwinm rA, rA, ShiftBits, 0, 0.
  960. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg)
  961. .addReg(Reg1, RegState::Kill)
  962. .addImm(getEncodingValue(SrcReg))
  963. .addImm(0).addImm(0);
  964. }
  965. addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
  966. .addReg(Reg, RegState::Kill),
  967. FrameIndex);
  968. bool KillsCRBit = MI.killsRegister(SrcReg, TRI);
  969. // Discard the pseudo instruction.
  970. MBB.erase(II);
  971. if (SpillsKnownBit && KillsCRBit && !SeenUse) {
  972. Ins->setDesc(TII.get(PPC::UNENCODED_NOP));
  973. Ins->RemoveOperand(0);
  974. }
  975. }
  976. void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II,
  977. unsigned FrameIndex) const {
  978. // Get the instruction.
  979. MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset>
  980. // Get the instruction's basic block.
  981. MachineBasicBlock &MBB = *MI.getParent();
  982. MachineFunction &MF = *MBB.getParent();
  983. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  984. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  985. DebugLoc dl = MI.getDebugLoc();
  986. bool LP64 = TM.isPPC64();
  987. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  988. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  989. Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  990. Register DestReg = MI.getOperand(0).getReg();
  991. assert(MI.definesRegister(DestReg) &&
  992. "RESTORE_CRBIT does not define its destination");
  993. addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ),
  994. Reg), FrameIndex);
  995. BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg);
  996. Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC);
  997. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO)
  998. .addReg(getCRFromCRBit(DestReg));
  999. unsigned ShiftBits = getEncodingValue(DestReg);
  1000. // rlwimi r11, r10, 32-ShiftBits, ..., ...
  1001. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO)
  1002. .addReg(RegO, RegState::Kill)
  1003. .addReg(Reg, RegState::Kill)
  1004. .addImm(ShiftBits ? 32 - ShiftBits : 0)
  1005. .addImm(ShiftBits)
  1006. .addImm(ShiftBits);
  1007. BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF),
  1008. getCRFromCRBit(DestReg))
  1009. .addReg(RegO, RegState::Kill)
  1010. // Make sure we have a use dependency all the way through this
  1011. // sequence of instructions. We can't have the other bits in the CR
  1012. // modified in between the mfocrf and the mtocrf.
  1013. .addReg(getCRFromCRBit(DestReg), RegState::Implicit);
  1014. // Discard the pseudo instruction.
  1015. MBB.erase(II);
  1016. }
  1017. void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB,
  1018. MCRegister DestReg, MCRegister SrcReg) {
  1019. #ifdef NDEBUG
  1020. return;
  1021. #else
  1022. if (ReportAccMoves) {
  1023. std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc";
  1024. std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc";
  1025. dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n";
  1026. MBB.dump();
  1027. }
  1028. #endif
  1029. }
  1030. static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed,
  1031. bool IsRestore) {
  1032. #ifdef NDEBUG
  1033. return;
  1034. #else
  1035. if (ReportAccMoves) {
  1036. dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register "
  1037. << (IsRestore ? "restore" : "spill") << ":\n";
  1038. MBB.dump();
  1039. }
  1040. #endif
  1041. }
  1042. /// lowerACCSpilling - Generate the code for spilling the accumulator register.
  1043. /// Similarly to other spills/reloads that use pseudo-ops, we do not actually
  1044. /// eliminate the FrameIndex here nor compute the stack offset. We simply
  1045. /// create a real instruction with an FI and rely on eliminateFrameIndex to
  1046. /// handle the FI elimination.
  1047. void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II,
  1048. unsigned FrameIndex) const {
  1049. MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset>
  1050. MachineBasicBlock &MBB = *MI.getParent();
  1051. MachineFunction &MF = *MBB.getParent();
  1052. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1053. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1054. DebugLoc DL = MI.getDebugLoc();
  1055. Register SrcReg = MI.getOperand(0).getReg();
  1056. bool IsKilled = MI.getOperand(0).isKill();
  1057. bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg);
  1058. Register Reg =
  1059. PPC::VSRp0 + (SrcReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
  1060. bool IsLittleEndian = Subtarget.isLittleEndian();
  1061. emitAccSpillRestoreInfo(MBB, IsPrimed, false);
  1062. // De-prime the register being spilled, create two stores for the pair
  1063. // subregisters accounting for endianness and then re-prime the register if
  1064. // it isn't killed. This uses the Offset parameter to addFrameReference() to
  1065. // adjust the offset of the store that is within the 64-byte stack slot.
  1066. if (IsPrimed)
  1067. BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg);
  1068. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
  1069. .addReg(Reg, getKillRegState(IsKilled)),
  1070. FrameIndex, IsLittleEndian ? 32 : 0);
  1071. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP))
  1072. .addReg(Reg + 1, getKillRegState(IsKilled)),
  1073. FrameIndex, IsLittleEndian ? 0 : 32);
  1074. if (IsPrimed && !IsKilled)
  1075. BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg);
  1076. // Discard the pseudo instruction.
  1077. MBB.erase(II);
  1078. }
  1079. /// lowerACCRestore - Generate the code to restore the accumulator register.
  1080. void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II,
  1081. unsigned FrameIndex) const {
  1082. MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset>
  1083. MachineBasicBlock &MBB = *MI.getParent();
  1084. MachineFunction &MF = *MBB.getParent();
  1085. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1086. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1087. DebugLoc DL = MI.getDebugLoc();
  1088. Register DestReg = MI.getOperand(0).getReg();
  1089. assert(MI.definesRegister(DestReg) &&
  1090. "RESTORE_ACC does not define its destination");
  1091. bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg);
  1092. Register Reg =
  1093. PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2;
  1094. bool IsLittleEndian = Subtarget.isLittleEndian();
  1095. emitAccSpillRestoreInfo(MBB, IsPrimed, true);
  1096. // Create two loads for the pair subregisters accounting for endianness and
  1097. // then prime the accumulator register being restored.
  1098. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg),
  1099. FrameIndex, IsLittleEndian ? 32 : 0);
  1100. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1),
  1101. FrameIndex, IsLittleEndian ? 0 : 32);
  1102. if (IsPrimed)
  1103. BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg);
  1104. // Discard the pseudo instruction.
  1105. MBB.erase(II);
  1106. }
  1107. /// lowerQuadwordSpilling - Generate code to spill paired general register.
  1108. void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II,
  1109. unsigned FrameIndex) const {
  1110. MachineInstr &MI = *II;
  1111. MachineBasicBlock &MBB = *MI.getParent();
  1112. MachineFunction &MF = *MBB.getParent();
  1113. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1114. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1115. DebugLoc DL = MI.getDebugLoc();
  1116. Register SrcReg = MI.getOperand(0).getReg();
  1117. bool IsKilled = MI.getOperand(0).isKill();
  1118. Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2;
  1119. bool IsLittleEndian = Subtarget.isLittleEndian();
  1120. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
  1121. .addReg(Reg, getKillRegState(IsKilled)),
  1122. FrameIndex, IsLittleEndian ? 8 : 0);
  1123. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD))
  1124. .addReg(Reg + 1, getKillRegState(IsKilled)),
  1125. FrameIndex, IsLittleEndian ? 0 : 8);
  1126. // Discard the pseudo instruction.
  1127. MBB.erase(II);
  1128. }
  1129. /// lowerQuadwordRestore - Generate code to restore paired general register.
  1130. void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II,
  1131. unsigned FrameIndex) const {
  1132. MachineInstr &MI = *II;
  1133. MachineBasicBlock &MBB = *MI.getParent();
  1134. MachineFunction &MF = *MBB.getParent();
  1135. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1136. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1137. DebugLoc DL = MI.getDebugLoc();
  1138. Register DestReg = MI.getOperand(0).getReg();
  1139. assert(MI.definesRegister(DestReg) &&
  1140. "RESTORE_QUADWORD does not define its destination");
  1141. Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2;
  1142. bool IsLittleEndian = Subtarget.isLittleEndian();
  1143. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex,
  1144. IsLittleEndian ? 8 : 0);
  1145. addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex,
  1146. IsLittleEndian ? 0 : 8);
  1147. // Discard the pseudo instruction.
  1148. MBB.erase(II);
  1149. }
  1150. bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
  1151. Register Reg, int &FrameIdx) const {
  1152. // For the nonvolatile condition registers (CR2, CR3, CR4) return true to
  1153. // prevent allocating an additional frame slot.
  1154. // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8,
  1155. // for 32-bit AIX the CR save area is in the linkage area at SP+4.
  1156. // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos
  1157. // valid.
  1158. // For 32-bit ELF, we have previously created the stack slot if needed, so
  1159. // return its FrameIdx.
  1160. if (PPC::CR2 <= Reg && Reg <= PPC::CR4) {
  1161. FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex();
  1162. return true;
  1163. }
  1164. return false;
  1165. }
  1166. // If the offset must be a multiple of some value, return what that value is.
  1167. static unsigned offsetMinAlignForOpcode(unsigned OpC) {
  1168. switch (OpC) {
  1169. default:
  1170. return 1;
  1171. case PPC::LWA:
  1172. case PPC::LWA_32:
  1173. case PPC::LD:
  1174. case PPC::LDU:
  1175. case PPC::STD:
  1176. case PPC::STDU:
  1177. case PPC::DFLOADf32:
  1178. case PPC::DFLOADf64:
  1179. case PPC::DFSTOREf32:
  1180. case PPC::DFSTOREf64:
  1181. case PPC::LXSD:
  1182. case PPC::LXSSP:
  1183. case PPC::STXSD:
  1184. case PPC::STXSSP:
  1185. case PPC::STQ:
  1186. return 4;
  1187. case PPC::EVLDD:
  1188. case PPC::EVSTDD:
  1189. return 8;
  1190. case PPC::LXV:
  1191. case PPC::STXV:
  1192. case PPC::LQ:
  1193. case PPC::LXVP:
  1194. case PPC::STXVP:
  1195. return 16;
  1196. }
  1197. }
  1198. // If the offset must be a multiple of some value, return what that value is.
  1199. static unsigned offsetMinAlign(const MachineInstr &MI) {
  1200. unsigned OpC = MI.getOpcode();
  1201. return offsetMinAlignForOpcode(OpC);
  1202. }
  1203. // Return the OffsetOperandNo given the FIOperandNum (and the instruction).
  1204. static unsigned getOffsetONFromFION(const MachineInstr &MI,
  1205. unsigned FIOperandNum) {
  1206. // Take into account whether it's an add or mem instruction
  1207. unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2;
  1208. if (MI.isInlineAsm())
  1209. OffsetOperandNo = FIOperandNum - 1;
  1210. else if (MI.getOpcode() == TargetOpcode::STACKMAP ||
  1211. MI.getOpcode() == TargetOpcode::PATCHPOINT)
  1212. OffsetOperandNo = FIOperandNum + 1;
  1213. return OffsetOperandNo;
  1214. }
  1215. void
  1216. PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
  1217. int SPAdj, unsigned FIOperandNum,
  1218. RegScavenger *RS) const {
  1219. assert(SPAdj == 0 && "Unexpected");
  1220. // Get the instruction.
  1221. MachineInstr &MI = *II;
  1222. // Get the instruction's basic block.
  1223. MachineBasicBlock &MBB = *MI.getParent();
  1224. // Get the basic block's function.
  1225. MachineFunction &MF = *MBB.getParent();
  1226. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1227. // Get the instruction info.
  1228. const PPCInstrInfo &TII = *Subtarget.getInstrInfo();
  1229. // Get the frame info.
  1230. MachineFrameInfo &MFI = MF.getFrameInfo();
  1231. DebugLoc dl = MI.getDebugLoc();
  1232. unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
  1233. // Get the frame index.
  1234. int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
  1235. // Get the frame pointer save index. Users of this index are primarily
  1236. // DYNALLOC instructions.
  1237. PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
  1238. int FPSI = FI->getFramePointerSaveIndex();
  1239. // Get the instruction opcode.
  1240. unsigned OpC = MI.getOpcode();
  1241. if ((OpC == PPC::DYNAREAOFFSET || OpC == PPC::DYNAREAOFFSET8)) {
  1242. lowerDynamicAreaOffset(II);
  1243. return;
  1244. }
  1245. // Special case for dynamic alloca.
  1246. if (FPSI && FrameIndex == FPSI &&
  1247. (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
  1248. lowerDynamicAlloc(II);
  1249. return;
  1250. }
  1251. if (FPSI && FrameIndex == FPSI &&
  1252. (OpC == PPC::PREPARE_PROBED_ALLOCA_64 ||
  1253. OpC == PPC::PREPARE_PROBED_ALLOCA_32 ||
  1254. OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64 ||
  1255. OpC == PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32)) {
  1256. lowerPrepareProbedAlloca(II);
  1257. return;
  1258. }
  1259. // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc.
  1260. if (OpC == PPC::SPILL_CR) {
  1261. lowerCRSpilling(II, FrameIndex);
  1262. return;
  1263. } else if (OpC == PPC::RESTORE_CR) {
  1264. lowerCRRestore(II, FrameIndex);
  1265. return;
  1266. } else if (OpC == PPC::SPILL_CRBIT) {
  1267. lowerCRBitSpilling(II, FrameIndex);
  1268. return;
  1269. } else if (OpC == PPC::RESTORE_CRBIT) {
  1270. lowerCRBitRestore(II, FrameIndex);
  1271. return;
  1272. } else if (OpC == PPC::SPILL_ACC || OpC == PPC::SPILL_UACC) {
  1273. lowerACCSpilling(II, FrameIndex);
  1274. return;
  1275. } else if (OpC == PPC::RESTORE_ACC || OpC == PPC::RESTORE_UACC) {
  1276. lowerACCRestore(II, FrameIndex);
  1277. return;
  1278. } else if (OpC == PPC::SPILL_QUADWORD) {
  1279. lowerQuadwordSpilling(II, FrameIndex);
  1280. return;
  1281. } else if (OpC == PPC::RESTORE_QUADWORD) {
  1282. lowerQuadwordRestore(II, FrameIndex);
  1283. return;
  1284. }
  1285. // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
  1286. MI.getOperand(FIOperandNum).ChangeToRegister(
  1287. FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false);
  1288. // If the instruction is not present in ImmToIdxMap, then it has no immediate
  1289. // form (and must be r+r).
  1290. bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP &&
  1291. OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC);
  1292. // Now add the frame object offset to the offset from r1.
  1293. int Offset = MFI.getObjectOffset(FrameIndex);
  1294. Offset += MI.getOperand(OffsetOperandNo).getImm();
  1295. // If we're not using a Frame Pointer that has been set to the value of the
  1296. // SP before having the stack size subtracted from it, then add the stack size
  1297. // to Offset to get the correct offset.
  1298. // Naked functions have stack size 0, although getStackSize may not reflect
  1299. // that because we didn't call all the pieces that compute it for naked
  1300. // functions.
  1301. if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) {
  1302. if (!(hasBasePointer(MF) && FrameIndex < 0))
  1303. Offset += MFI.getStackSize();
  1304. }
  1305. // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can
  1306. // transform it to the prefixed version so we don't have to use the XForm.
  1307. if ((OpC == PPC::LXVP || OpC == PPC::STXVP) &&
  1308. (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) &&
  1309. Subtarget.hasPrefixInstrs()) {
  1310. unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP;
  1311. MI.setDesc(TII.get(NewOpc));
  1312. OpC = NewOpc;
  1313. }
  1314. // If we can, encode the offset directly into the instruction. If this is a
  1315. // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
  1316. // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
  1317. // clear can be encoded. This is extremely uncommon, because normally you
  1318. // only "std" to a stack slot that is at least 4-byte aligned, but it can
  1319. // happen in invalid code.
  1320. assert(OpC != PPC::DBG_VALUE &&
  1321. "This should be handled in a target-independent way");
  1322. // FIXME: This should be factored out to a separate function as prefixed
  1323. // instructions add a number of opcodes for which we can use 34-bit imm.
  1324. bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ?
  1325. isUInt<8>(Offset) :
  1326. isInt<16>(Offset);
  1327. if (TII.isPrefixed(MI.getOpcode()))
  1328. OffsetFitsMnemonic = isInt<34>(Offset);
  1329. if (!noImmForm && ((OffsetFitsMnemonic &&
  1330. ((Offset % offsetMinAlign(MI)) == 0)) ||
  1331. OpC == TargetOpcode::STACKMAP ||
  1332. OpC == TargetOpcode::PATCHPOINT)) {
  1333. MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
  1334. return;
  1335. }
  1336. // The offset doesn't fit into a single register, scavenge one to build the
  1337. // offset in.
  1338. bool is64Bit = TM.isPPC64();
  1339. const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
  1340. const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
  1341. const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC;
  1342. Register SRegHi = MF.getRegInfo().createVirtualRegister(RC),
  1343. SReg = MF.getRegInfo().createVirtualRegister(RC);
  1344. // Insert a set of rA with the full offset value before the ld, st, or add
  1345. if (isInt<16>(Offset))
  1346. BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg)
  1347. .addImm(Offset);
  1348. else {
  1349. BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi)
  1350. .addImm(Offset >> 16);
  1351. BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg)
  1352. .addReg(SRegHi, RegState::Kill)
  1353. .addImm(Offset);
  1354. }
  1355. // Convert into indexed form of the instruction:
  1356. //
  1357. // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
  1358. // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
  1359. unsigned OperandBase;
  1360. if (noImmForm)
  1361. OperandBase = 1;
  1362. else if (OpC != TargetOpcode::INLINEASM &&
  1363. OpC != TargetOpcode::INLINEASM_BR) {
  1364. assert(ImmToIdxMap.count(OpC) &&
  1365. "No indexed form of load or store available!");
  1366. unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
  1367. MI.setDesc(TII.get(NewOpcode));
  1368. OperandBase = 1;
  1369. } else {
  1370. OperandBase = OffsetOperandNo;
  1371. }
  1372. Register StackReg = MI.getOperand(FIOperandNum).getReg();
  1373. MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
  1374. MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true);
  1375. }
  1376. Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
  1377. const PPCFrameLowering *TFI = getFrameLowering(MF);
  1378. if (!TM.isPPC64())
  1379. return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
  1380. else
  1381. return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
  1382. }
  1383. Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const {
  1384. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1385. if (!hasBasePointer(MF))
  1386. return getFrameRegister(MF);
  1387. if (TM.isPPC64())
  1388. return PPC::X30;
  1389. if (Subtarget.isSVR4ABI() && TM.isPositionIndependent())
  1390. return PPC::R29;
  1391. return PPC::R30;
  1392. }
  1393. bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
  1394. if (!EnableBasePointer)
  1395. return false;
  1396. if (AlwaysBasePointer)
  1397. return true;
  1398. // If we need to realign the stack, then the stack pointer can no longer
  1399. // serve as an offset into the caller's stack space. As a result, we need a
  1400. // base pointer.
  1401. return hasStackRealignment(MF);
  1402. }
  1403. /// Returns true if the instruction's frame index
  1404. /// reference would be better served by a base register other than FP
  1405. /// or SP. Used by LocalStackFrameAllocation to determine which frame index
  1406. /// references it should create new base registers for.
  1407. bool PPCRegisterInfo::
  1408. needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
  1409. assert(Offset < 0 && "Local offset must be negative");
  1410. // It's the load/store FI references that cause issues, as it can be difficult
  1411. // to materialize the offset if it won't fit in the literal field. Estimate
  1412. // based on the size of the local frame and some conservative assumptions
  1413. // about the rest of the stack frame (note, this is pre-regalloc, so
  1414. // we don't know everything for certain yet) whether this offset is likely
  1415. // to be out of range of the immediate. Return true if so.
  1416. // We only generate virtual base registers for loads and stores that have
  1417. // an r+i form. Return false for everything else.
  1418. unsigned OpC = MI->getOpcode();
  1419. if (!ImmToIdxMap.count(OpC))
  1420. return false;
  1421. // Don't generate a new virtual base register just to add zero to it.
  1422. if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) &&
  1423. MI->getOperand(2).getImm() == 0)
  1424. return false;
  1425. MachineBasicBlock &MBB = *MI->getParent();
  1426. MachineFunction &MF = *MBB.getParent();
  1427. const PPCFrameLowering *TFI = getFrameLowering(MF);
  1428. unsigned StackEst = TFI->determineFrameLayout(MF, true);
  1429. // If we likely don't need a stack frame, then we probably don't need a
  1430. // virtual base register either.
  1431. if (!StackEst)
  1432. return false;
  1433. // Estimate an offset from the stack pointer.
  1434. // The incoming offset is relating to the SP at the start of the function,
  1435. // but when we access the local it'll be relative to the SP after local
  1436. // allocation, so adjust our SP-relative offset by that allocation size.
  1437. Offset += StackEst;
  1438. // The frame pointer will point to the end of the stack, so estimate the
  1439. // offset as the difference between the object offset and the FP location.
  1440. return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset);
  1441. }
  1442. /// Insert defining instruction(s) for BaseReg to
  1443. /// be a pointer to FrameIdx at the beginning of the basic block.
  1444. Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
  1445. int FrameIdx,
  1446. int64_t Offset) const {
  1447. unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI;
  1448. MachineBasicBlock::iterator Ins = MBB->begin();
  1449. DebugLoc DL; // Defaults to "unknown"
  1450. if (Ins != MBB->end())
  1451. DL = Ins->getDebugLoc();
  1452. const MachineFunction &MF = *MBB->getParent();
  1453. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1454. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1455. const MCInstrDesc &MCID = TII.get(ADDriOpc);
  1456. MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
  1457. const TargetRegisterClass *RC = getPointerRegClass(MF);
  1458. Register BaseReg = MRI.createVirtualRegister(RC);
  1459. MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
  1460. BuildMI(*MBB, Ins, DL, MCID, BaseReg)
  1461. .addFrameIndex(FrameIdx).addImm(Offset);
  1462. return BaseReg;
  1463. }
  1464. void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
  1465. int64_t Offset) const {
  1466. unsigned FIOperandNum = 0;
  1467. while (!MI.getOperand(FIOperandNum).isFI()) {
  1468. ++FIOperandNum;
  1469. assert(FIOperandNum < MI.getNumOperands() &&
  1470. "Instr doesn't have FrameIndex operand!");
  1471. }
  1472. MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
  1473. unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum);
  1474. Offset += MI.getOperand(OffsetOperandNo).getImm();
  1475. MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
  1476. MachineBasicBlock &MBB = *MI.getParent();
  1477. MachineFunction &MF = *MBB.getParent();
  1478. const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>();
  1479. const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
  1480. const MCInstrDesc &MCID = MI.getDesc();
  1481. MachineRegisterInfo &MRI = MF.getRegInfo();
  1482. MRI.constrainRegClass(BaseReg,
  1483. TII.getRegClass(MCID, FIOperandNum, this, MF));
  1484. }
  1485. bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
  1486. Register BaseReg,
  1487. int64_t Offset) const {
  1488. unsigned FIOperandNum = 0;
  1489. while (!MI->getOperand(FIOperandNum).isFI()) {
  1490. ++FIOperandNum;
  1491. assert(FIOperandNum < MI->getNumOperands() &&
  1492. "Instr doesn't have FrameIndex operand!");
  1493. }
  1494. unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum);
  1495. Offset += MI->getOperand(OffsetOperandNo).getImm();
  1496. return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm
  1497. MI->getOpcode() == TargetOpcode::STACKMAP ||
  1498. MI->getOpcode() == TargetOpcode::PATCHPOINT ||
  1499. (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0);
  1500. }