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 14 #include "PPCRegisterInfo.h" 15 #include "PPCFrameLowering.h" 16 #include "PPCInstrBuilder.h" 17 #include "PPCMachineFunctionInfo.h" 18 #include "PPCSubtarget.h" 19 #include "PPCTargetMachine.h" 20 #include "llvm/ADT/BitVector.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/CodeGen/VirtRegMap.h" 31 #include "llvm/IR/CallingConv.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 43 using namespace llvm; 44 45 #define DEBUG_TYPE "reginfo" 46 47 #define GET_REGINFO_TARGET_DESC 48 #include "PPCGenRegisterInfo.inc" 49 50 STATISTIC(InflateGPRC, "Number of gprc inputs for getLargestLegalClass"); 51 STATISTIC(InflateGP8RC, "Number of g8rc inputs for getLargestLegalClass"); 52 53 static cl::opt<bool> 54 EnableBasePointer("ppc-use-base-pointer", cl::Hidden, cl::init(true), 55 cl::desc("Enable use of a base pointer for complex stack frames")); 56 57 static cl::opt<bool> 58 AlwaysBasePointer("ppc-always-use-base-pointer", cl::Hidden, cl::init(false), 59 cl::desc("Force the use of a base pointer in every function")); 60 61 static cl::opt<bool> 62 EnableGPRToVecSpills("ppc-enable-gpr-to-vsr-spills", cl::Hidden, cl::init(false), 63 cl::desc("Enable spills from gpr to vsr rather than stack")); 64 65 static cl::opt<bool> 66 StackPtrConst("ppc-stack-ptr-caller-preserved", 67 cl::desc("Consider R1 caller preserved so stack saves of " 68 "caller preserved registers can be LICM candidates"), 69 cl::init(true), cl::Hidden); 70 71 static cl::opt<unsigned> 72 MaxCRBitSpillDist("ppc-max-crbit-spill-dist", 73 cl::desc("Maximum search distance for definition of CR bit " 74 "spill on ppc"), 75 cl::Hidden, cl::init(100)); 76 77 // Copies/moves of physical accumulators are expensive operations 78 // that should be avoided whenever possible. MMA instructions are 79 // meant to be used in performance-sensitive computational kernels. 80 // This option is provided, at least for the time being, to give the 81 // user a tool to detect this expensive operation and either rework 82 // their code or report a compiler bug if that turns out to be the 83 // cause. 84 #ifndef NDEBUG 85 static cl::opt<bool> 86 ReportAccMoves("ppc-report-acc-moves", 87 cl::desc("Emit information about accumulator register spills " 88 "and copies"), 89 cl::Hidden, cl::init(false)); 90 #endif 91 92 extern cl::opt<bool> DisableAutoPairedVecSt; 93 94 static unsigned offsetMinAlignForOpcode(unsigned OpC); 95 96 PPCRegisterInfo::PPCRegisterInfo(const PPCTargetMachine &TM) 97 : PPCGenRegisterInfo(TM.isPPC64() ? PPC::LR8 : PPC::LR, 98 TM.isPPC64() ? 0 : 1, 99 TM.isPPC64() ? 0 : 1), 100 TM(TM) { 101 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; 102 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; 103 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; 104 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; 105 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; 106 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; 107 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; 108 ImmToIdxMap[PPC::ADDI] = PPC::ADD4; 109 ImmToIdxMap[PPC::LWA_32] = PPC::LWAX_32; 110 111 // 64-bit 112 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8; 113 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8; 114 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8; 115 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX; 116 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; 117 ImmToIdxMap[PPC::LQ] = PPC::LQX_PSEUDO; 118 ImmToIdxMap[PPC::STQ] = PPC::STQX_PSEUDO; 119 120 // VSX 121 ImmToIdxMap[PPC::DFLOADf32] = PPC::LXSSPX; 122 ImmToIdxMap[PPC::DFLOADf64] = PPC::LXSDX; 123 ImmToIdxMap[PPC::SPILLTOVSR_LD] = PPC::SPILLTOVSR_LDX; 124 ImmToIdxMap[PPC::SPILLTOVSR_ST] = PPC::SPILLTOVSR_STX; 125 ImmToIdxMap[PPC::DFSTOREf32] = PPC::STXSSPX; 126 ImmToIdxMap[PPC::DFSTOREf64] = PPC::STXSDX; 127 ImmToIdxMap[PPC::LXV] = PPC::LXVX; 128 ImmToIdxMap[PPC::LXSD] = PPC::LXSDX; 129 ImmToIdxMap[PPC::LXSSP] = PPC::LXSSPX; 130 ImmToIdxMap[PPC::STXV] = PPC::STXVX; 131 ImmToIdxMap[PPC::STXSD] = PPC::STXSDX; 132 ImmToIdxMap[PPC::STXSSP] = PPC::STXSSPX; 133 134 // SPE 135 ImmToIdxMap[PPC::EVLDD] = PPC::EVLDDX; 136 ImmToIdxMap[PPC::EVSTDD] = PPC::EVSTDDX; 137 ImmToIdxMap[PPC::SPESTW] = PPC::SPESTWX; 138 ImmToIdxMap[PPC::SPELWZ] = PPC::SPELWZX; 139 140 // Power10 141 ImmToIdxMap[PPC::PLBZ] = PPC::LBZX; ImmToIdxMap[PPC::PLBZ8] = PPC::LBZX8; 142 ImmToIdxMap[PPC::PLHZ] = PPC::LHZX; ImmToIdxMap[PPC::PLHZ8] = PPC::LHZX8; 143 ImmToIdxMap[PPC::PLHA] = PPC::LHAX; ImmToIdxMap[PPC::PLHA8] = PPC::LHAX8; 144 ImmToIdxMap[PPC::PLWZ] = PPC::LWZX; ImmToIdxMap[PPC::PLWZ8] = PPC::LWZX8; 145 ImmToIdxMap[PPC::PLWA] = PPC::LWAX; ImmToIdxMap[PPC::PLWA8] = PPC::LWAX; 146 ImmToIdxMap[PPC::PLD] = PPC::LDX; ImmToIdxMap[PPC::PSTD] = PPC::STDX; 147 148 ImmToIdxMap[PPC::PSTB] = PPC::STBX; ImmToIdxMap[PPC::PSTB8] = PPC::STBX8; 149 ImmToIdxMap[PPC::PSTH] = PPC::STHX; ImmToIdxMap[PPC::PSTH8] = PPC::STHX8; 150 ImmToIdxMap[PPC::PSTW] = PPC::STWX; ImmToIdxMap[PPC::PSTW8] = PPC::STWX8; 151 152 ImmToIdxMap[PPC::PLFS] = PPC::LFSX; ImmToIdxMap[PPC::PSTFS] = PPC::STFSX; 153 ImmToIdxMap[PPC::PLFD] = PPC::LFDX; ImmToIdxMap[PPC::PSTFD] = PPC::STFDX; 154 ImmToIdxMap[PPC::PLXSSP] = PPC::LXSSPX; ImmToIdxMap[PPC::PSTXSSP] = PPC::STXSSPX; 155 ImmToIdxMap[PPC::PLXSD] = PPC::LXSDX; ImmToIdxMap[PPC::PSTXSD] = PPC::STXSDX; 156 ImmToIdxMap[PPC::PLXV] = PPC::LXVX; ImmToIdxMap[PPC::PSTXV] = PPC::STXVX; 157 158 ImmToIdxMap[PPC::LXVP] = PPC::LXVPX; 159 ImmToIdxMap[PPC::STXVP] = PPC::STXVPX; 160 ImmToIdxMap[PPC::PLXVP] = PPC::LXVPX; 161 ImmToIdxMap[PPC::PSTXVP] = PPC::STXVPX; 162 } 163 164 /// getPointerRegClass - Return the register class to use to hold pointers. 165 /// This is used for addressing modes. 166 const TargetRegisterClass * 167 PPCRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind) 168 const { 169 // Note that PPCInstrInfo::foldImmediate also directly uses this Kind value 170 // when it checks for ZERO folding. 171 if (Kind == 1) { 172 if (TM.isPPC64()) 173 return &PPC::G8RC_NOX0RegClass; 174 return &PPC::GPRC_NOR0RegClass; 175 } 176 177 if (TM.isPPC64()) 178 return &PPC::G8RCRegClass; 179 return &PPC::GPRCRegClass; 180 } 181 182 const MCPhysReg* 183 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const { 184 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 185 if (MF->getFunction().getCallingConv() == CallingConv::AnyReg) { 186 if (!TM.isPPC64() && Subtarget.isAIXABI()) 187 report_fatal_error("AnyReg unimplemented on 32-bit AIX."); 188 if (Subtarget.hasVSX()) { 189 if (Subtarget.pairedVectorMemops()) 190 return CSR_64_AllRegs_VSRP_SaveList; 191 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 192 return CSR_64_AllRegs_AIX_Dflt_VSX_SaveList; 193 return CSR_64_AllRegs_VSX_SaveList; 194 } 195 if (Subtarget.hasAltivec()) { 196 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 197 return CSR_64_AllRegs_AIX_Dflt_Altivec_SaveList; 198 return CSR_64_AllRegs_Altivec_SaveList; 199 } 200 return CSR_64_AllRegs_SaveList; 201 } 202 203 // On PPC64, we might need to save r2 (but only if it is not reserved). 204 // We do not need to treat R2 as callee-saved when using PC-Relative calls 205 // because any direct uses of R2 will cause it to be reserved. If the function 206 // is a leaf or the only uses of R2 are implicit uses for calls, the calls 207 // will use the @notoc relocation which will cause this function to set the 208 // st_other bit to 1, thereby communicating to its caller that it arbitrarily 209 // clobbers the TOC. 210 bool SaveR2 = MF->getRegInfo().isAllocatable(PPC::X2) && 211 !Subtarget.isUsingPCRelativeCalls(); 212 213 // Cold calling convention CSRs. 214 if (MF->getFunction().getCallingConv() == CallingConv::Cold) { 215 if (Subtarget.isAIXABI()) 216 report_fatal_error("Cold calling unimplemented on AIX."); 217 if (TM.isPPC64()) { 218 if (Subtarget.pairedVectorMemops()) 219 return SaveR2 ? CSR_SVR64_ColdCC_R2_VSRP_SaveList 220 : CSR_SVR64_ColdCC_VSRP_SaveList; 221 if (Subtarget.hasAltivec()) 222 return SaveR2 ? CSR_SVR64_ColdCC_R2_Altivec_SaveList 223 : CSR_SVR64_ColdCC_Altivec_SaveList; 224 return SaveR2 ? CSR_SVR64_ColdCC_R2_SaveList 225 : CSR_SVR64_ColdCC_SaveList; 226 } 227 // 32-bit targets. 228 if (Subtarget.pairedVectorMemops()) 229 return CSR_SVR32_ColdCC_VSRP_SaveList; 230 else if (Subtarget.hasAltivec()) 231 return CSR_SVR32_ColdCC_Altivec_SaveList; 232 else if (Subtarget.hasSPE()) 233 return CSR_SVR32_ColdCC_SPE_SaveList; 234 return CSR_SVR32_ColdCC_SaveList; 235 } 236 // Standard calling convention CSRs. 237 if (TM.isPPC64()) { 238 if (Subtarget.pairedVectorMemops()) { 239 if (Subtarget.isAIXABI()) { 240 if (!TM.getAIXExtendedAltivecABI()) 241 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 242 return SaveR2 ? CSR_AIX64_R2_VSRP_SaveList : CSR_AIX64_VSRP_SaveList; 243 } 244 return SaveR2 ? CSR_SVR464_R2_VSRP_SaveList : CSR_SVR464_VSRP_SaveList; 245 } 246 if (Subtarget.hasAltivec() && 247 (!Subtarget.isAIXABI() || TM.getAIXExtendedAltivecABI())) { 248 return SaveR2 ? CSR_PPC64_R2_Altivec_SaveList 249 : CSR_PPC64_Altivec_SaveList; 250 } 251 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 252 } 253 // 32-bit targets. 254 if (Subtarget.isAIXABI()) { 255 if (Subtarget.pairedVectorMemops()) 256 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_VSRP_SaveList 257 : CSR_AIX32_SaveList; 258 if (Subtarget.hasAltivec()) 259 return TM.getAIXExtendedAltivecABI() ? CSR_AIX32_Altivec_SaveList 260 : CSR_AIX32_SaveList; 261 return CSR_AIX32_SaveList; 262 } 263 if (Subtarget.pairedVectorMemops()) 264 return CSR_SVR432_VSRP_SaveList; 265 if (Subtarget.hasAltivec()) 266 return CSR_SVR432_Altivec_SaveList; 267 else if (Subtarget.hasSPE()) { 268 if (TM.isPositionIndependent() && !TM.isPPC64()) 269 return CSR_SVR432_SPE_NO_S30_31_SaveList; 270 return CSR_SVR432_SPE_SaveList; 271 } 272 return CSR_SVR432_SaveList; 273 } 274 275 const uint32_t * 276 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 277 CallingConv::ID CC) const { 278 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 279 if (CC == CallingConv::AnyReg) { 280 if (Subtarget.hasVSX()) { 281 if (Subtarget.pairedVectorMemops()) 282 return CSR_64_AllRegs_VSRP_RegMask; 283 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 284 return CSR_64_AllRegs_AIX_Dflt_VSX_RegMask; 285 return CSR_64_AllRegs_VSX_RegMask; 286 } 287 if (Subtarget.hasAltivec()) { 288 if (Subtarget.isAIXABI() && !TM.getAIXExtendedAltivecABI()) 289 return CSR_64_AllRegs_AIX_Dflt_Altivec_RegMask; 290 return CSR_64_AllRegs_Altivec_RegMask; 291 } 292 return CSR_64_AllRegs_RegMask; 293 } 294 295 if (Subtarget.isAIXABI()) { 296 if (Subtarget.pairedVectorMemops()) { 297 if (!TM.getAIXExtendedAltivecABI()) 298 return TM.isPPC64() ? CSR_PPC64_RegMask : CSR_AIX32_RegMask; 299 return TM.isPPC64() ? CSR_AIX64_VSRP_RegMask : CSR_AIX32_VSRP_RegMask; 300 } 301 return TM.isPPC64() 302 ? ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI()) 303 ? CSR_PPC64_Altivec_RegMask 304 : CSR_PPC64_RegMask) 305 : ((Subtarget.hasAltivec() && TM.getAIXExtendedAltivecABI()) 306 ? CSR_AIX32_Altivec_RegMask 307 : CSR_AIX32_RegMask); 308 } 309 310 if (CC == CallingConv::Cold) { 311 if (TM.isPPC64()) 312 return Subtarget.pairedVectorMemops() 313 ? CSR_SVR64_ColdCC_VSRP_RegMask 314 : (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 315 : CSR_SVR64_ColdCC_RegMask); 316 else 317 return Subtarget.pairedVectorMemops() 318 ? CSR_SVR32_ColdCC_VSRP_RegMask 319 : (Subtarget.hasAltivec() 320 ? CSR_SVR32_ColdCC_Altivec_RegMask 321 : (Subtarget.hasSPE() ? CSR_SVR32_ColdCC_SPE_RegMask 322 : CSR_SVR32_ColdCC_RegMask)); 323 } 324 325 if (TM.isPPC64()) 326 return Subtarget.pairedVectorMemops() 327 ? CSR_SVR464_VSRP_RegMask 328 : (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 329 : CSR_PPC64_RegMask); 330 else 331 return Subtarget.pairedVectorMemops() 332 ? CSR_SVR432_VSRP_RegMask 333 : (Subtarget.hasAltivec() 334 ? CSR_SVR432_Altivec_RegMask 335 : (Subtarget.hasSPE() 336 ? (TM.isPositionIndependent() 337 ? CSR_SVR432_SPE_NO_S30_31_RegMask 338 : CSR_SVR432_SPE_RegMask) 339 : CSR_SVR432_RegMask)); 340 } 341 342 const uint32_t* 343 PPCRegisterInfo::getNoPreservedMask() const { 344 return CSR_NoRegs_RegMask; 345 } 346 347 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 348 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 349 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 350 } 351 352 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 353 BitVector Reserved(getNumRegs()); 354 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 355 const PPCFrameLowering *TFI = getFrameLowering(MF); 356 357 // The ZERO register is not really a register, but the representation of r0 358 // when used in instructions that treat r0 as the constant 0. 359 markSuperRegs(Reserved, PPC::ZERO); 360 361 // The FP register is also not really a register, but is the representation 362 // of the frame pointer register used by ISD::FRAMEADDR. 363 markSuperRegs(Reserved, PPC::FP); 364 365 // The BP register is also not really a register, but is the representation 366 // of the base pointer register used by setjmp. 367 markSuperRegs(Reserved, PPC::BP); 368 369 // The counter registers must be reserved so that counter-based loops can 370 // be correctly formed (and the mtctr instructions are not DCE'd). 371 markSuperRegs(Reserved, PPC::CTR); 372 markSuperRegs(Reserved, PPC::CTR8); 373 374 markSuperRegs(Reserved, PPC::R1); 375 markSuperRegs(Reserved, PPC::LR); 376 markSuperRegs(Reserved, PPC::LR8); 377 markSuperRegs(Reserved, PPC::RM); 378 379 markSuperRegs(Reserved, PPC::VRSAVE); 380 381 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 382 bool UsesTOCBasePtr = FuncInfo->usesTOCBasePtr(); 383 // The SVR4 ABI reserves r2 and r13 384 if (Subtarget.isSVR4ABI() || Subtarget.isAIXABI()) { 385 // We only reserve r2 if we need to use the TOC pointer. If we have no 386 // explicit uses of the TOC pointer (meaning we're a leaf function with 387 // no constant-pool loads, etc.) and we have no potential uses inside an 388 // inline asm block, then we can treat r2 has an ordinary callee-saved 389 // register. 390 if (!TM.isPPC64() || UsesTOCBasePtr || MF.hasInlineAsm()) 391 markSuperRegs(Reserved, PPC::R2); // System-reserved register. 392 393 if (Subtarget.isSVR4ABI()) 394 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register. 395 } 396 397 // On PPC64, r13 is the thread pointer. Never allocate this register. 398 if (TM.isPPC64()) 399 markSuperRegs(Reserved, PPC::R13); 400 401 if (TFI->needsFP(MF)) 402 markSuperRegs(Reserved, PPC::R31); 403 404 bool IsPositionIndependent = TM.isPositionIndependent(); 405 if (hasBasePointer(MF)) { 406 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 407 markSuperRegs(Reserved, PPC::R29); 408 else 409 markSuperRegs(Reserved, PPC::R30); 410 } 411 412 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 413 markSuperRegs(Reserved, PPC::R30); 414 415 // Reserve Altivec registers when Altivec is unavailable. 416 if (!Subtarget.hasAltivec()) 417 for (MCRegister Reg : PPC::VRRCRegClass) 418 markSuperRegs(Reserved, Reg); 419 420 if (Subtarget.isAIXABI() && Subtarget.hasAltivec() && 421 !TM.getAIXExtendedAltivecABI()) { 422 // In the AIX default Altivec ABI, vector registers VR20-VR31 are reserved 423 // and cannot be used. 424 for (auto Reg : CSR_Altivec_SaveList) { 425 if (Reg == 0) 426 break; 427 markSuperRegs(Reserved, Reg); 428 for (MCRegAliasIterator AS(Reg, this, true); AS.isValid(); ++AS) { 429 Reserved.set(*AS); 430 } 431 } 432 } 433 434 assert(checkAllSuperRegsMarked(Reserved)); 435 return Reserved; 436 } 437 438 bool PPCRegisterInfo::isAsmClobberable(const MachineFunction &MF, 439 MCRegister PhysReg) const { 440 // CTR and LR registers are always reserved, but they are asm clobberable. 441 if (PhysReg == PPC::CTR || PhysReg == PPC::CTR8 || PhysReg == PPC::LR || 442 PhysReg == PPC::LR8) 443 return true; 444 445 return !getReservedRegs(MF).test(PhysReg); 446 } 447 448 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const { 449 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 450 const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo(); 451 const MachineFrameInfo &MFI = MF.getFrameInfo(); 452 const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo(); 453 454 LLVM_DEBUG(dbgs() << "requiresFrameIndexScavenging for " << MF.getName() 455 << ".\n"); 456 // If the callee saved info is invalid we have to default to true for safety. 457 if (!MFI.isCalleeSavedInfoValid()) { 458 LLVM_DEBUG(dbgs() << "TRUE - Invalid callee saved info.\n"); 459 return true; 460 } 461 462 // We will require the use of X-Forms because the frame is larger than what 463 // can be represented in signed 16 bits that fit in the immediate of a D-Form. 464 // If we need an X-Form then we need a register to store the address offset. 465 unsigned FrameSize = MFI.getStackSize(); 466 // Signed 16 bits means that the FrameSize cannot be more than 15 bits. 467 if (FrameSize & ~0x7FFF) { 468 LLVM_DEBUG(dbgs() << "TRUE - Frame size is too large for D-Form.\n"); 469 return true; 470 } 471 472 // The callee saved info is valid so it can be traversed. 473 // Checking for registers that need saving that do not have load or store 474 // forms where the address offset is an immediate. 475 for (const CalleeSavedInfo &CSI : Info) { 476 // If the spill is to a register no scavenging is required. 477 if (CSI.isSpilledToReg()) 478 continue; 479 480 int FrIdx = CSI.getFrameIdx(); 481 Register Reg = CSI.getReg(); 482 483 const TargetRegisterClass *RC = getMinimalPhysRegClass(Reg); 484 unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(RC); 485 if (!MFI.isFixedObjectIndex(FrIdx)) { 486 // This is not a fixed object. If it requires alignment then we may still 487 // need to use the XForm. 488 if (offsetMinAlignForOpcode(Opcode) > 1) { 489 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 490 << " for register " << printReg(Reg, this) << ".\n"); 491 LLVM_DEBUG(dbgs() << "TRUE - Not fixed frame object that requires " 492 << "alignment.\n"); 493 return true; 494 } 495 } 496 497 // This is eiher: 498 // 1) A fixed frame index object which we know are aligned so 499 // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't 500 // need to consider the alignment here. 501 // 2) A not fixed object but in that case we now know that the min required 502 // alignment is no more than 1 based on the previous check. 503 if (InstrInfo->isXFormMemOp(Opcode)) { 504 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 505 << " for register " << printReg(Reg, this) << ".\n"); 506 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is X-Form.\n"); 507 return true; 508 } 509 510 // This is a spill/restore of a quadword. 511 if ((Opcode == PPC::RESTORE_QUADWORD) || (Opcode == PPC::SPILL_QUADWORD)) { 512 LLVM_DEBUG(dbgs() << "Memory Operand: " << InstrInfo->getName(Opcode) 513 << " for register " << printReg(Reg, this) << ".\n"); 514 LLVM_DEBUG(dbgs() << "TRUE - Memory operand is a quadword.\n"); 515 return true; 516 } 517 } 518 LLVM_DEBUG(dbgs() << "FALSE - Scavenging is not required.\n"); 519 return false; 520 } 521 522 bool PPCRegisterInfo::requiresVirtualBaseRegisters( 523 const MachineFunction &MF) const { 524 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 525 // Do not use virtual base registers when ROP protection is turned on. 526 // Virtual base registers break the layout of the local variable space and may 527 // push the ROP Hash location past the 512 byte range of the ROP store 528 // instruction. 529 return !Subtarget.hasROPProtect(); 530 } 531 532 bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg, 533 const MachineFunction &MF) const { 534 assert(PhysReg.isPhysical()); 535 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 536 const MachineFrameInfo &MFI = MF.getFrameInfo(); 537 538 if (!Subtarget.is64BitELFABI() && !Subtarget.isAIXABI()) 539 return false; 540 if (PhysReg == Subtarget.getTOCPointerRegister()) 541 // X2/R2 is guaranteed to be preserved within a function if it is reserved. 542 // The reason it's reserved is that it's the TOC pointer (and the function 543 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 544 // with no TOC access), we can't claim that it is preserved. 545 return (getReservedRegs(MF).test(PhysReg)); 546 if (StackPtrConst && PhysReg == Subtarget.getStackPointerRegister() && 547 !MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment()) 548 // The value of the stack pointer does not change within a function after 549 // the prologue and before the epilogue if there are no dynamic allocations 550 // and no inline asm which clobbers X1/R1. 551 return true; 552 return false; 553 } 554 555 bool PPCRegisterInfo::getRegAllocationHints(Register VirtReg, 556 ArrayRef<MCPhysReg> Order, 557 SmallVectorImpl<MCPhysReg> &Hints, 558 const MachineFunction &MF, 559 const VirtRegMap *VRM, 560 const LiveRegMatrix *Matrix) const { 561 const MachineRegisterInfo *MRI = &MF.getRegInfo(); 562 563 // Call the base implementation first to set any hints based on the usual 564 // heuristics and decide what the return value should be. We want to return 565 // the same value returned by the base implementation. If the base 566 // implementation decides to return true and force the allocation then we 567 // will leave it as such. On the other hand if the base implementation 568 // decides to return false the following code will not force the allocation 569 // as we are just looking to provide a hint. 570 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints( 571 VirtReg, Order, Hints, MF, VRM, Matrix); 572 573 // Don't use the allocation hints for ISAFuture. 574 // The WACC registers used in ISAFuture are unlike the ACC registers on 575 // Power 10 and so this logic to register allocation hints does not apply. 576 if (MF.getSubtarget<PPCSubtarget>().isISAFuture()) 577 return BaseImplRetVal; 578 579 // We are interested in instructions that copy values to ACC/UACC. 580 // The copy into UACC will be simply a COPY to a subreg so we 581 // want to allocate the corresponding physical subreg for the source. 582 // The copy into ACC will be a BUILD_UACC so we want to allocate 583 // the same number UACC for the source. 584 const TargetRegisterClass *RegClass = MRI->getRegClass(VirtReg); 585 for (MachineInstr &Use : MRI->reg_nodbg_instructions(VirtReg)) { 586 const MachineOperand *ResultOp = nullptr; 587 Register ResultReg; 588 switch (Use.getOpcode()) { 589 case TargetOpcode::COPY: { 590 ResultOp = &Use.getOperand(0); 591 ResultReg = ResultOp->getReg(); 592 if (ResultReg.isVirtual() && 593 MRI->getRegClass(ResultReg)->contains(PPC::UACC0) && 594 VRM->hasPhys(ResultReg)) { 595 Register UACCPhys = VRM->getPhys(ResultReg); 596 Register HintReg; 597 if (RegClass->contains(PPC::VSRp0)) { 598 HintReg = getSubReg(UACCPhys, ResultOp->getSubReg()); 599 // Ensure that the hint is a VSRp register. 600 if (HintReg >= PPC::VSRp0 && HintReg <= PPC::VSRp31) 601 Hints.push_back(HintReg); 602 } else if (RegClass->contains(PPC::ACC0)) { 603 HintReg = PPC::ACC0 + (UACCPhys - PPC::UACC0); 604 if (HintReg >= PPC::ACC0 && HintReg <= PPC::ACC7) 605 Hints.push_back(HintReg); 606 } 607 } 608 break; 609 } 610 case PPC::BUILD_UACC: { 611 ResultOp = &Use.getOperand(0); 612 ResultReg = ResultOp->getReg(); 613 if (MRI->getRegClass(ResultReg)->contains(PPC::ACC0) && 614 VRM->hasPhys(ResultReg)) { 615 Register ACCPhys = VRM->getPhys(ResultReg); 616 assert((ACCPhys >= PPC::ACC0 && ACCPhys <= PPC::ACC7) && 617 "Expecting an ACC register for BUILD_UACC."); 618 Register HintReg = PPC::UACC0 + (ACCPhys - PPC::ACC0); 619 Hints.push_back(HintReg); 620 } 621 break; 622 } 623 } 624 } 625 return BaseImplRetVal; 626 } 627 628 const TargetRegisterClass * 629 PPCRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const { 630 if (RC == &PPC::CARRYRCRegClass) 631 return TM.isPPC64() ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; 632 return RC; 633 } 634 635 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 636 MachineFunction &MF) const { 637 const PPCFrameLowering *TFI = getFrameLowering(MF); 638 const unsigned DefaultSafety = 1; 639 640 switch (RC->getID()) { 641 default: 642 return 0; 643 case PPC::G8RC_NOX0RegClassID: 644 case PPC::GPRC_NOR0RegClassID: 645 case PPC::SPERCRegClassID: 646 case PPC::G8RCRegClassID: 647 case PPC::GPRCRegClassID: { 648 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 649 return 32 - FP - DefaultSafety; 650 } 651 case PPC::F4RCRegClassID: 652 case PPC::F8RCRegClassID: 653 case PPC::VSLRCRegClassID: 654 return 32 - DefaultSafety; 655 case PPC::VFRCRegClassID: 656 case PPC::VRRCRegClassID: { 657 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 658 // Vector registers VR20-VR31 are reserved and cannot be used in the default 659 // Altivec ABI on AIX. 660 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI()) 661 return 20 - DefaultSafety; 662 } 663 return 32 - DefaultSafety; 664 case PPC::VSFRCRegClassID: 665 case PPC::VSSRCRegClassID: 666 case PPC::VSRCRegClassID: { 667 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 668 if (!TM.getAIXExtendedAltivecABI() && Subtarget.isAIXABI()) 669 // Vector registers VR20-VR31 are reserved and cannot be used in the 670 // default Altivec ABI on AIX. 671 return 52 - DefaultSafety; 672 } 673 return 64 - DefaultSafety; 674 case PPC::CRRCRegClassID: 675 return 8 - DefaultSafety; 676 } 677 } 678 679 const TargetRegisterClass * 680 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 681 const MachineFunction &MF) const { 682 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 683 const auto *DefaultSuperclass = 684 TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 685 if (Subtarget.hasVSX()) { 686 // With VSX, we can inflate various sub-register classes to the full VSX 687 // register set. 688 689 // For Power9 we allow the user to enable GPR to vector spills. 690 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 691 // support to spill GPRC. 692 if (TM.isELFv2ABI() || Subtarget.isAIXABI()) { 693 if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 694 RC == &PPC::G8RCRegClass) { 695 InflateGP8RC++; 696 return &PPC::SPILLTOVSRRCRegClass; 697 } 698 if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 699 InflateGPRC++; 700 } 701 702 for (unsigned SuperID : RC->superclasses()) { 703 if (getRegSizeInBits(*getRegClass(SuperID)) != getRegSizeInBits(*RC)) 704 continue; 705 706 switch (SuperID) { 707 case PPC::VSSRCRegClassID: 708 return Subtarget.hasP8Vector() ? getRegClass(SuperID) 709 : DefaultSuperclass; 710 case PPC::VSFRCRegClassID: 711 case PPC::VSRCRegClassID: 712 return getRegClass(SuperID); 713 case PPC::VSRpRCRegClassID: 714 return Subtarget.pairedVectorMemops() ? getRegClass(SuperID) 715 : DefaultSuperclass; 716 case PPC::ACCRCRegClassID: 717 case PPC::UACCRCRegClassID: 718 return Subtarget.hasMMA() ? getRegClass(SuperID) : DefaultSuperclass; 719 } 720 } 721 } 722 723 return DefaultSuperclass; 724 } 725 726 //===----------------------------------------------------------------------===// 727 // Stack Frame Processing methods 728 //===----------------------------------------------------------------------===// 729 730 /// lowerDynamicAlloc - Generate the code for allocating an object in the 731 /// current frame. The sequence of code will be in the general form 732 /// 733 /// addi R0, SP, \#frameSize ; get the address of the previous frame 734 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 735 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 736 /// 737 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 738 // Get the instruction. 739 MachineInstr &MI = *II; 740 // Get the instruction's basic block. 741 MachineBasicBlock &MBB = *MI.getParent(); 742 // Get the basic block's function. 743 MachineFunction &MF = *MBB.getParent(); 744 // Get the frame info. 745 MachineFrameInfo &MFI = MF.getFrameInfo(); 746 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 747 // Get the instruction info. 748 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 749 // Determine whether 64-bit pointers are used. 750 bool LP64 = TM.isPPC64(); 751 DebugLoc dl = MI.getDebugLoc(); 752 753 // Get the maximum call stack size. 754 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 755 Align MaxAlign = MFI.getMaxAlign(); 756 assert(isAligned(MaxAlign, maxCallFrameSize) && 757 "Maximum call-frame size not sufficiently aligned"); 758 (void)MaxAlign; 759 760 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 761 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 762 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 763 bool KillNegSizeReg = MI.getOperand(1).isKill(); 764 Register NegSizeReg = MI.getOperand(1).getReg(); 765 766 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, Reg); 767 // Grow the stack and update the stack pointer link, then determine the 768 // address of new allocated space. 769 if (LP64) { 770 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 771 .addReg(Reg, RegState::Kill) 772 .addReg(PPC::X1) 773 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 774 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 775 .addReg(PPC::X1) 776 .addImm(maxCallFrameSize); 777 } else { 778 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 779 .addReg(Reg, RegState::Kill) 780 .addReg(PPC::R1) 781 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 782 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 783 .addReg(PPC::R1) 784 .addImm(maxCallFrameSize); 785 } 786 787 // Discard the DYNALLOC instruction. 788 MBB.erase(II); 789 } 790 791 /// To accomplish dynamic stack allocation, we have to calculate exact size 792 /// subtracted from the stack pointer according alignment information and get 793 /// previous frame pointer. 794 void PPCRegisterInfo::prepareDynamicAlloca(MachineBasicBlock::iterator II, 795 Register &NegSizeReg, 796 bool &KillNegSizeReg, 797 Register &FramePointer) const { 798 // Get the instruction. 799 MachineInstr &MI = *II; 800 // Get the instruction's basic block. 801 MachineBasicBlock &MBB = *MI.getParent(); 802 // Get the basic block's function. 803 MachineFunction &MF = *MBB.getParent(); 804 // Get the frame info. 805 MachineFrameInfo &MFI = MF.getFrameInfo(); 806 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 807 // Get the instruction info. 808 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 809 // Determine whether 64-bit pointers are used. 810 bool LP64 = TM.isPPC64(); 811 DebugLoc dl = MI.getDebugLoc(); 812 // Get the total frame size. 813 unsigned FrameSize = MFI.getStackSize(); 814 815 // Get stack alignments. 816 const PPCFrameLowering *TFI = getFrameLowering(MF); 817 Align TargetAlign = TFI->getStackAlign(); 818 Align MaxAlign = MFI.getMaxAlign(); 819 820 // Determine the previous frame's address. If FrameSize can't be 821 // represented as 16 bits or we need special alignment, then we load the 822 // previous frame's address from 0(SP). Why not do an addis of the hi? 823 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 824 // Constructing the constant and adding would take 3 instructions. 825 // Fortunately, a frame greater than 32K is rare. 826 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 827 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 828 829 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 830 if (LP64) 831 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), FramePointer) 832 .addReg(PPC::X31) 833 .addImm(FrameSize); 834 else 835 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), FramePointer) 836 .addReg(PPC::R31) 837 .addImm(FrameSize); 838 } else if (LP64) { 839 BuildMI(MBB, II, dl, TII.get(PPC::LD), FramePointer) 840 .addImm(0) 841 .addReg(PPC::X1); 842 } else { 843 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), FramePointer) 844 .addImm(0) 845 .addReg(PPC::R1); 846 } 847 // Determine the actual NegSizeReg according to alignment info. 848 if (LP64) { 849 if (MaxAlign > TargetAlign) { 850 unsigned UnalNegSizeReg = NegSizeReg; 851 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 852 853 // Unfortunately, there is no andi, only andi., and we can't insert that 854 // here because we might clobber cr0 while it is live. 855 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 856 .addImm(~(MaxAlign.value() - 1)); 857 858 unsigned NegSizeReg1 = NegSizeReg; 859 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 860 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 861 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 862 .addReg(NegSizeReg1, RegState::Kill); 863 KillNegSizeReg = true; 864 } 865 } else { 866 if (MaxAlign > TargetAlign) { 867 unsigned UnalNegSizeReg = NegSizeReg; 868 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 869 870 // Unfortunately, there is no andi, only andi., and we can't insert that 871 // here because we might clobber cr0 while it is live. 872 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 873 .addImm(~(MaxAlign.value() - 1)); 874 875 unsigned NegSizeReg1 = NegSizeReg; 876 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 877 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 878 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 879 .addReg(NegSizeReg1, RegState::Kill); 880 KillNegSizeReg = true; 881 } 882 } 883 } 884 885 void PPCRegisterInfo::lowerPrepareProbedAlloca( 886 MachineBasicBlock::iterator II) const { 887 MachineInstr &MI = *II; 888 // Get the instruction's basic block. 889 MachineBasicBlock &MBB = *MI.getParent(); 890 // Get the basic block's function. 891 MachineFunction &MF = *MBB.getParent(); 892 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 893 // Get the instruction info. 894 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 895 // Determine whether 64-bit pointers are used. 896 bool LP64 = TM.isPPC64(); 897 DebugLoc dl = MI.getDebugLoc(); 898 Register FramePointer = MI.getOperand(0).getReg(); 899 const Register ActualNegSizeReg = MI.getOperand(1).getReg(); 900 bool KillNegSizeReg = MI.getOperand(2).isKill(); 901 Register NegSizeReg = MI.getOperand(2).getReg(); 902 const MCInstrDesc &CopyInst = TII.get(LP64 ? PPC::OR8 : PPC::OR); 903 // RegAllocator might allocate FramePointer and NegSizeReg in the same phyreg. 904 if (FramePointer == NegSizeReg) { 905 assert(KillNegSizeReg && "FramePointer is a def and NegSizeReg is an use, " 906 "NegSizeReg should be killed"); 907 // FramePointer is clobbered earlier than the use of NegSizeReg in 908 // prepareDynamicAlloca, save NegSizeReg in ActualNegSizeReg to avoid 909 // misuse. 910 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 911 .addReg(NegSizeReg) 912 .addReg(NegSizeReg); 913 NegSizeReg = ActualNegSizeReg; 914 KillNegSizeReg = false; 915 } 916 prepareDynamicAlloca(II, NegSizeReg, KillNegSizeReg, FramePointer); 917 // NegSizeReg might be updated in prepareDynamicAlloca if MaxAlign > 918 // TargetAlign. 919 if (NegSizeReg != ActualNegSizeReg) 920 BuildMI(MBB, II, dl, CopyInst, ActualNegSizeReg) 921 .addReg(NegSizeReg) 922 .addReg(NegSizeReg); 923 MBB.erase(II); 924 } 925 926 void PPCRegisterInfo::lowerDynamicAreaOffset( 927 MachineBasicBlock::iterator II) const { 928 // Get the instruction. 929 MachineInstr &MI = *II; 930 // Get the instruction's basic block. 931 MachineBasicBlock &MBB = *MI.getParent(); 932 // Get the basic block's function. 933 MachineFunction &MF = *MBB.getParent(); 934 // Get the frame info. 935 MachineFrameInfo &MFI = MF.getFrameInfo(); 936 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 937 // Get the instruction info. 938 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 939 940 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 941 bool is64Bit = TM.isPPC64(); 942 DebugLoc dl = MI.getDebugLoc(); 943 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 944 MI.getOperand(0).getReg()) 945 .addImm(maxCallFrameSize); 946 MBB.erase(II); 947 } 948 949 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 950 /// reserving a whole register (R0), we scrounge for one here. This generates 951 /// code like this: 952 /// 953 /// mfcr rA ; Move the conditional register into GPR rA. 954 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 955 /// stw rA, FI ; Store rA to the frame. 956 /// 957 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 958 unsigned FrameIndex) const { 959 // Get the instruction. 960 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 961 // Get the instruction's basic block. 962 MachineBasicBlock &MBB = *MI.getParent(); 963 MachineFunction &MF = *MBB.getParent(); 964 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 965 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 966 DebugLoc dl = MI.getDebugLoc(); 967 968 bool LP64 = TM.isPPC64(); 969 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 970 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 971 972 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 973 Register SrcReg = MI.getOperand(0).getReg(); 974 975 // We need to store the CR in the low 4-bits of the saved value. First, issue 976 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 977 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 978 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 979 980 // If the saved register wasn't CR0, shift the bits left so that they are in 981 // CR0's slot. 982 if (SrcReg != PPC::CR0) { 983 Register Reg1 = Reg; 984 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 985 986 // rlwinm rA, rA, ShiftBits, 0, 31. 987 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 988 .addReg(Reg1, RegState::Kill) 989 .addImm(getEncodingValue(SrcReg) * 4) 990 .addImm(0) 991 .addImm(31); 992 } 993 994 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 995 .addReg(Reg, RegState::Kill), 996 FrameIndex); 997 998 // Discard the pseudo instruction. 999 MBB.erase(II); 1000 } 1001 1002 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 1003 unsigned FrameIndex) const { 1004 // Get the instruction. 1005 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 1006 // Get the instruction's basic block. 1007 MachineBasicBlock &MBB = *MI.getParent(); 1008 MachineFunction &MF = *MBB.getParent(); 1009 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1010 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1011 DebugLoc dl = MI.getDebugLoc(); 1012 1013 bool LP64 = TM.isPPC64(); 1014 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1015 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1016 1017 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1018 Register DestReg = MI.getOperand(0).getReg(); 1019 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) && 1020 "RESTORE_CR does not define its destination"); 1021 1022 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 1023 Reg), FrameIndex); 1024 1025 // If the reloaded register isn't CR0, shift the bits right so that they are 1026 // in the right CR's slot. 1027 if (DestReg != PPC::CR0) { 1028 Register Reg1 = Reg; 1029 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1030 1031 unsigned ShiftBits = getEncodingValue(DestReg)*4; 1032 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 1033 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 1034 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 1035 .addImm(31); 1036 } 1037 1038 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 1039 .addReg(Reg, RegState::Kill); 1040 1041 // Discard the pseudo instruction. 1042 MBB.erase(II); 1043 } 1044 1045 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 1046 unsigned FrameIndex) const { 1047 // Get the instruction. 1048 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 1049 // Get the instruction's basic block. 1050 MachineBasicBlock &MBB = *MI.getParent(); 1051 MachineFunction &MF = *MBB.getParent(); 1052 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1053 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1054 const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 1055 DebugLoc dl = MI.getDebugLoc(); 1056 1057 bool LP64 = TM.isPPC64(); 1058 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1059 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1060 1061 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1062 Register SrcReg = MI.getOperand(0).getReg(); 1063 1064 // Search up the BB to find the definition of the CR bit. 1065 MachineBasicBlock::reverse_iterator Ins = MI; 1066 MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 1067 ++Ins; 1068 unsigned CRBitSpillDistance = 0; 1069 bool SeenUse = false; 1070 for (; Ins != Rend; ++Ins) { 1071 // Definition found. 1072 if (Ins->modifiesRegister(SrcReg, TRI)) 1073 break; 1074 // Use found. 1075 if (Ins->readsRegister(SrcReg, TRI)) 1076 SeenUse = true; 1077 // Unable to find CR bit definition within maximum search distance. 1078 if (CRBitSpillDistance == MaxCRBitSpillDist) { 1079 Ins = MI; 1080 break; 1081 } 1082 // Skip debug instructions when counting CR bit spill distance. 1083 if (!Ins->isDebugInstr()) 1084 CRBitSpillDistance++; 1085 } 1086 1087 // Unable to find the definition of the CR bit in the MBB. 1088 if (Ins == MBB.rend()) 1089 Ins = MI; 1090 1091 bool SpillsKnownBit = false; 1092 // There is no need to extract the CR bit if its value is already known. 1093 switch (Ins->getOpcode()) { 1094 case PPC::CRUNSET: 1095 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 1096 .addImm(0); 1097 SpillsKnownBit = true; 1098 break; 1099 case PPC::CRSET: 1100 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 1101 .addImm(-32768); 1102 SpillsKnownBit = true; 1103 break; 1104 default: 1105 // On Power10, we can use SETNBC to spill all CR bits. SETNBC will set all 1106 // bits (specifically, it produces a -1 if the CR bit is set). Ultimately, 1107 // the bit that is of importance to us is bit 32 (bit 0 of a 32-bit 1108 // register), and SETNBC will set this. 1109 if (Subtarget.isISA3_1()) { 1110 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETNBC8 : PPC::SETNBC), Reg) 1111 .addReg(SrcReg, RegState::Undef); 1112 break; 1113 } 1114 1115 // On Power9, we can use SETB to extract the LT bit. This only works for 1116 // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 1117 // of the bit we care about (32-bit sign bit) will be set to the value of 1118 // the LT bit (regardless of the other bits in the CR field). 1119 if (Subtarget.isISA3_0()) { 1120 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 1121 SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 1122 SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 1123 SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 1124 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 1125 .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 1126 break; 1127 } 1128 } 1129 1130 // We need to move the CR field that contains the CR bit we are spilling. 1131 // The super register may not be explicitly defined (i.e. it can be defined 1132 // by a CR-logical that only defines the subreg) so we state that the CR 1133 // field is undef. Also, in order to preserve the kill flag on the CR bit, 1134 // we add it as an implicit use. 1135 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 1136 .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 1137 .addReg(SrcReg, 1138 RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 1139 1140 // If the saved register wasn't CR0LT, shift the bits left so that the bit 1141 // to store is the first one. Mask all but that bit. 1142 Register Reg1 = Reg; 1143 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1144 1145 // rlwinm rA, rA, ShiftBits, 0, 0. 1146 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 1147 .addReg(Reg1, RegState::Kill) 1148 .addImm(getEncodingValue(SrcReg)) 1149 .addImm(0).addImm(0); 1150 } 1151 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 1152 .addReg(Reg, RegState::Kill), 1153 FrameIndex); 1154 1155 bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 1156 // Discard the pseudo instruction. 1157 MBB.erase(II); 1158 if (SpillsKnownBit && KillsCRBit && !SeenUse) { 1159 Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 1160 Ins->removeOperand(0); 1161 } 1162 } 1163 1164 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 1165 unsigned FrameIndex) const { 1166 // Get the instruction. 1167 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 1168 // Get the instruction's basic block. 1169 MachineBasicBlock &MBB = *MI.getParent(); 1170 MachineFunction &MF = *MBB.getParent(); 1171 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1172 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1173 DebugLoc dl = MI.getDebugLoc(); 1174 1175 bool LP64 = TM.isPPC64(); 1176 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1177 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1178 1179 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1180 Register DestReg = MI.getOperand(0).getReg(); 1181 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) && 1182 "RESTORE_CRBIT does not define its destination"); 1183 1184 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 1185 Reg), FrameIndex); 1186 1187 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 1188 1189 Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 1190 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 1191 .addReg(getCRFromCRBit(DestReg)); 1192 1193 unsigned ShiftBits = getEncodingValue(DestReg); 1194 // rlwimi r11, r10, 32-ShiftBits, ..., ... 1195 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 1196 .addReg(RegO, RegState::Kill) 1197 .addReg(Reg, RegState::Kill) 1198 .addImm(ShiftBits ? 32 - ShiftBits : 0) 1199 .addImm(ShiftBits) 1200 .addImm(ShiftBits); 1201 1202 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 1203 getCRFromCRBit(DestReg)) 1204 .addReg(RegO, RegState::Kill) 1205 // Make sure we have a use dependency all the way through this 1206 // sequence of instructions. We can't have the other bits in the CR 1207 // modified in between the mfocrf and the mtocrf. 1208 .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 1209 1210 // Discard the pseudo instruction. 1211 MBB.erase(II); 1212 } 1213 1214 void PPCRegisterInfo::emitAccCopyInfo(MachineBasicBlock &MBB, 1215 MCRegister DestReg, MCRegister SrcReg) { 1216 #ifdef NDEBUG 1217 return; 1218 #else 1219 if (ReportAccMoves) { 1220 std::string Dest = PPC::ACCRCRegClass.contains(DestReg) ? "acc" : "uacc"; 1221 std::string Src = PPC::ACCRCRegClass.contains(SrcReg) ? "acc" : "uacc"; 1222 dbgs() << "Emitting copy from " << Src << " to " << Dest << ":\n"; 1223 MBB.dump(); 1224 } 1225 #endif 1226 } 1227 1228 static void emitAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsPrimed, 1229 bool IsRestore) { 1230 #ifdef NDEBUG 1231 return; 1232 #else 1233 if (ReportAccMoves) { 1234 dbgs() << "Emitting " << (IsPrimed ? "acc" : "uacc") << " register " 1235 << (IsRestore ? "restore" : "spill") << ":\n"; 1236 MBB.dump(); 1237 } 1238 #endif 1239 } 1240 1241 void PPCRegisterInfo::spillRegPair(MachineBasicBlock &MBB, 1242 MachineBasicBlock::iterator II, DebugLoc DL, 1243 const TargetInstrInfo &TII, 1244 unsigned FrameIndex, bool IsLittleEndian, 1245 bool IsKilled, Register Reg, 1246 int Offset) const { 1247 1248 // This function does not support virtual registers. 1249 assert(!Reg.isVirtual() && 1250 "Spilling register pairs does not support virtual registers."); 1251 1252 addFrameReference( 1253 BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1254 .addReg(TargetRegisterInfo::getSubReg(Reg, PPC::sub_vsx0), 1255 getKillRegState(IsKilled)), 1256 FrameIndex, Offset); 1257 1258 addFrameReference( 1259 BuildMI(MBB, II, DL, TII.get(PPC::STXV)) 1260 .addReg(TargetRegisterInfo::getSubReg(Reg, PPC::sub_vsx1), 1261 getKillRegState(IsKilled)), 1262 FrameIndex, IsLittleEndian ? Offset - 16 : Offset + 16); 1263 } 1264 1265 /// Remove any STXVP[X] instructions and split them out into a pair of 1266 /// STXV[X] instructions if --disable-auto-paired-vec-st is specified on 1267 /// the command line. 1268 void PPCRegisterInfo::lowerOctWordSpilling(MachineBasicBlock::iterator II, 1269 unsigned FrameIndex) const { 1270 assert(DisableAutoPairedVecSt && 1271 "Expecting to do this only if paired vector stores are disabled."); 1272 MachineInstr &MI = *II; // STXVP <SrcReg>, <offset> 1273 MachineBasicBlock &MBB = *MI.getParent(); 1274 MachineFunction &MF = *MBB.getParent(); 1275 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1276 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1277 DebugLoc DL = MI.getDebugLoc(); 1278 Register SrcReg = MI.getOperand(0).getReg(); 1279 bool IsLittleEndian = Subtarget.isLittleEndian(); 1280 bool IsKilled = MI.getOperand(0).isKill(); 1281 1282 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled, SrcReg, 1283 IsLittleEndian ? 16 : 0); 1284 1285 // Discard the original instruction. 1286 MBB.erase(II); 1287 } 1288 1289 static void emitWAccSpillRestoreInfo(MachineBasicBlock &MBB, bool IsRestore) { 1290 #ifdef NDEBUG 1291 return; 1292 #else 1293 if (ReportAccMoves) { 1294 dbgs() << "Emitting wacc register " << (IsRestore ? "restore" : "spill") 1295 << ":\n"; 1296 MBB.dump(); 1297 } 1298 #endif 1299 } 1300 1301 /// lowerACCSpilling - Generate the code for spilling the accumulator register. 1302 /// Similarly to other spills/reloads that use pseudo-ops, we do not actually 1303 /// eliminate the FrameIndex here nor compute the stack offset. We simply 1304 /// create a real instruction with an FI and rely on eliminateFrameIndex to 1305 /// handle the FI elimination. 1306 void PPCRegisterInfo::lowerACCSpilling(MachineBasicBlock::iterator II, 1307 unsigned FrameIndex) const { 1308 MachineInstr &MI = *II; // SPILL_ACC <SrcReg>, <offset> 1309 MachineBasicBlock &MBB = *MI.getParent(); 1310 MachineFunction &MF = *MBB.getParent(); 1311 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1312 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1313 DebugLoc DL = MI.getDebugLoc(); 1314 Register SrcReg = MI.getOperand(0).getReg(); 1315 bool IsKilled = MI.getOperand(0).isKill(); 1316 1317 bool IsPrimed = PPC::ACCRCRegClass.contains(SrcReg); 1318 bool IsLittleEndian = Subtarget.isLittleEndian(); 1319 1320 emitAccSpillRestoreInfo(MBB, IsPrimed, false); 1321 1322 // De-prime the register being spilled, create two stores for the pair 1323 // subregisters accounting for endianness and then re-prime the register if 1324 // it isn't killed. This uses the Offset parameter to addFrameReference() to 1325 // adjust the offset of the store that is within the 64-byte stack slot. 1326 if (IsPrimed) 1327 BuildMI(MBB, II, DL, TII.get(PPC::XXMFACC), SrcReg).addReg(SrcReg); 1328 if (DisableAutoPairedVecSt) { 1329 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled, 1330 TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair0), 1331 IsLittleEndian ? 48 : 0); 1332 spillRegPair(MBB, II, DL, TII, FrameIndex, IsLittleEndian, IsKilled, 1333 TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair1), 1334 IsLittleEndian ? 16 : 32); 1335 } else { 1336 addFrameReference( 1337 BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1338 .addReg(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair0), 1339 getKillRegState(IsKilled)), 1340 FrameIndex, IsLittleEndian ? 32 : 0); 1341 addFrameReference( 1342 BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1343 .addReg(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_pair1), 1344 getKillRegState(IsKilled)), 1345 FrameIndex, IsLittleEndian ? 0 : 32); 1346 } 1347 if (IsPrimed && !IsKilled) 1348 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), SrcReg).addReg(SrcReg); 1349 1350 // Discard the pseudo instruction. 1351 MBB.erase(II); 1352 } 1353 1354 /// lowerACCRestore - Generate the code to restore the accumulator register. 1355 void PPCRegisterInfo::lowerACCRestore(MachineBasicBlock::iterator II, 1356 unsigned FrameIndex) const { 1357 MachineInstr &MI = *II; // <DestReg> = RESTORE_ACC <offset> 1358 MachineBasicBlock &MBB = *MI.getParent(); 1359 MachineFunction &MF = *MBB.getParent(); 1360 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1361 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1362 DebugLoc DL = MI.getDebugLoc(); 1363 1364 Register DestReg = MI.getOperand(0).getReg(); 1365 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) && 1366 "RESTORE_ACC does not define its destination"); 1367 1368 bool IsPrimed = PPC::ACCRCRegClass.contains(DestReg); 1369 Register Reg = 1370 PPC::VSRp0 + (DestReg - (IsPrimed ? PPC::ACC0 : PPC::UACC0)) * 2; 1371 bool IsLittleEndian = Subtarget.isLittleEndian(); 1372 1373 emitAccSpillRestoreInfo(MBB, IsPrimed, true); 1374 1375 // Create two loads for the pair subregisters accounting for endianness and 1376 // then prime the accumulator register being restored. 1377 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg), 1378 FrameIndex, IsLittleEndian ? 32 : 0); 1379 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), Reg + 1), 1380 FrameIndex, IsLittleEndian ? 0 : 32); 1381 if (IsPrimed) 1382 BuildMI(MBB, II, DL, TII.get(PPC::XXMTACC), DestReg).addReg(DestReg); 1383 1384 // Discard the pseudo instruction. 1385 MBB.erase(II); 1386 } 1387 1388 /// lowerWACCSpilling - Generate the code for spilling the wide accumulator 1389 /// register. 1390 void PPCRegisterInfo::lowerWACCSpilling(MachineBasicBlock::iterator II, 1391 unsigned FrameIndex) const { 1392 MachineInstr &MI = *II; // SPILL_WACC <SrcReg>, <offset> 1393 MachineBasicBlock &MBB = *MI.getParent(); 1394 MachineFunction &MF = *MBB.getParent(); 1395 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1396 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1397 DebugLoc DL = MI.getDebugLoc(); 1398 bool IsLittleEndian = Subtarget.isLittleEndian(); 1399 1400 emitWAccSpillRestoreInfo(MBB, false); 1401 1402 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass; 1403 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC); 1404 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC); 1405 Register SrcReg = MI.getOperand(0).getReg(); 1406 1407 BuildMI(MBB, II, DL, TII.get(PPC::DMXXEXTFDMR512), VSRpReg0) 1408 .addDef(VSRpReg1) 1409 .addReg(SrcReg); 1410 1411 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1412 .addReg(VSRpReg0, RegState::Kill), 1413 FrameIndex, IsLittleEndian ? 32 : 0); 1414 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1415 .addReg(VSRpReg1, RegState::Kill), 1416 FrameIndex, IsLittleEndian ? 0 : 32); 1417 1418 // Discard the pseudo instruction. 1419 MBB.erase(II); 1420 } 1421 1422 /// lowerWACCRestore - Generate the code to restore the wide accumulator 1423 /// register. 1424 void PPCRegisterInfo::lowerWACCRestore(MachineBasicBlock::iterator II, 1425 unsigned FrameIndex) const { 1426 MachineInstr &MI = *II; // <DestReg> = RESTORE_WACC <offset> 1427 MachineBasicBlock &MBB = *MI.getParent(); 1428 MachineFunction &MF = *MBB.getParent(); 1429 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1430 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1431 DebugLoc DL = MI.getDebugLoc(); 1432 bool IsLittleEndian = Subtarget.isLittleEndian(); 1433 1434 emitWAccSpillRestoreInfo(MBB, true); 1435 1436 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass; 1437 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC); 1438 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC); 1439 Register DestReg = MI.getOperand(0).getReg(); 1440 1441 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg0), 1442 FrameIndex, IsLittleEndian ? 32 : 0); 1443 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg1), 1444 FrameIndex, IsLittleEndian ? 0 : 32); 1445 1446 // Kill VSRpReg0, VSRpReg1 (killedRegState::Killed) 1447 BuildMI(MBB, II, DL, TII.get(PPC::DMXXINSTDMR512), DestReg) 1448 .addReg(VSRpReg0, RegState::Kill) 1449 .addReg(VSRpReg1, RegState::Kill); 1450 1451 // Discard the pseudo instruction. 1452 MBB.erase(II); 1453 } 1454 1455 /// lowerQuadwordSpilling - Generate code to spill paired general register. 1456 void PPCRegisterInfo::lowerQuadwordSpilling(MachineBasicBlock::iterator II, 1457 unsigned FrameIndex) const { 1458 MachineInstr &MI = *II; 1459 MachineBasicBlock &MBB = *MI.getParent(); 1460 MachineFunction &MF = *MBB.getParent(); 1461 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1462 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1463 DebugLoc DL = MI.getDebugLoc(); 1464 1465 Register SrcReg = MI.getOperand(0).getReg(); 1466 bool IsKilled = MI.getOperand(0).isKill(); 1467 1468 Register Reg = PPC::X0 + (SrcReg - PPC::G8p0) * 2; 1469 bool IsLittleEndian = Subtarget.isLittleEndian(); 1470 1471 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD)) 1472 .addReg(Reg, getKillRegState(IsKilled)), 1473 FrameIndex, IsLittleEndian ? 8 : 0); 1474 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STD)) 1475 .addReg(Reg + 1, getKillRegState(IsKilled)), 1476 FrameIndex, IsLittleEndian ? 0 : 8); 1477 1478 // Discard the pseudo instruction. 1479 MBB.erase(II); 1480 } 1481 1482 /// lowerQuadwordRestore - Generate code to restore paired general register. 1483 void PPCRegisterInfo::lowerQuadwordRestore(MachineBasicBlock::iterator II, 1484 unsigned FrameIndex) const { 1485 MachineInstr &MI = *II; 1486 MachineBasicBlock &MBB = *MI.getParent(); 1487 MachineFunction &MF = *MBB.getParent(); 1488 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1489 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1490 DebugLoc DL = MI.getDebugLoc(); 1491 1492 Register DestReg = MI.getOperand(0).getReg(); 1493 assert(MI.definesRegister(DestReg, /*TRI=*/nullptr) && 1494 "RESTORE_QUADWORD does not define its destination"); 1495 1496 Register Reg = PPC::X0 + (DestReg - PPC::G8p0) * 2; 1497 bool IsLittleEndian = Subtarget.isLittleEndian(); 1498 1499 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg), FrameIndex, 1500 IsLittleEndian ? 8 : 0); 1501 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LD), Reg + 1), FrameIndex, 1502 IsLittleEndian ? 0 : 8); 1503 1504 // Discard the pseudo instruction. 1505 MBB.erase(II); 1506 } 1507 1508 /// lowerDMRSpilling - Generate the code for spilling the DMR register. 1509 void PPCRegisterInfo::lowerDMRSpilling(MachineBasicBlock::iterator II, 1510 unsigned FrameIndex) const { 1511 MachineInstr &MI = *II; // SPILL_DMR <SrcReg>, <offset> 1512 MachineBasicBlock &MBB = *MI.getParent(); 1513 MachineFunction &MF = *MBB.getParent(); 1514 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1515 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1516 DebugLoc DL = MI.getDebugLoc(); 1517 bool IsLittleEndian = Subtarget.isLittleEndian(); 1518 1519 // DMR is made up of WACC and WACC_HI, so DMXXEXTFDMR512 to spill 1520 // the corresponding 512 bits. 1521 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass; 1522 auto spillDMR = [&](Register SrcReg, int BEIdx, int LEIdx) { 1523 auto spillWACC = [&](unsigned Opc, unsigned RegIdx, int IdxBE, int IdxLE) { 1524 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC); 1525 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC); 1526 1527 BuildMI(MBB, II, DL, TII.get(Opc), VSRpReg0) 1528 .addDef(VSRpReg1) 1529 .addReg(TargetRegisterInfo::getSubReg(SrcReg, RegIdx)); 1530 1531 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1532 .addReg(VSRpReg0, RegState::Kill), 1533 FrameIndex, IsLittleEndian ? IdxLE : IdxBE); 1534 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::STXVP)) 1535 .addReg(VSRpReg1, RegState::Kill), 1536 FrameIndex, IsLittleEndian ? IdxLE - 32 : IdxBE + 32); 1537 }; 1538 spillWACC(PPC::DMXXEXTFDMR512, PPC::sub_wacc_lo, BEIdx, LEIdx); 1539 spillWACC(PPC::DMXXEXTFDMR512_HI, PPC::sub_wacc_hi, BEIdx + 64, LEIdx - 64); 1540 }; 1541 1542 Register SrcReg = MI.getOperand(0).getReg(); 1543 if (MI.getOpcode() == PPC::SPILL_DMRP) { 1544 spillDMR(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_dmr1), 0, 96); 1545 spillDMR(TargetRegisterInfo::getSubReg(SrcReg, PPC::sub_dmr0), 128, 224); 1546 } else 1547 spillDMR(SrcReg, 0, 96); 1548 1549 // Discard the pseudo instruction. 1550 MBB.erase(II); 1551 } 1552 1553 /// lowerDMRRestore - Generate the code to restore the DMR register. 1554 void PPCRegisterInfo::lowerDMRRestore(MachineBasicBlock::iterator II, 1555 unsigned FrameIndex) const { 1556 MachineInstr &MI = *II; // <DestReg> = RESTORE_DMR[P] <offset> 1557 MachineBasicBlock &MBB = *MI.getParent(); 1558 MachineFunction &MF = *MBB.getParent(); 1559 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1560 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 1561 DebugLoc DL = MI.getDebugLoc(); 1562 bool IsLittleEndian = Subtarget.isLittleEndian(); 1563 1564 const TargetRegisterClass *RC = &PPC::VSRpRCRegClass; 1565 auto restoreDMR = [&](Register DestReg, int BEIdx, int LEIdx) { 1566 auto restoreWACC = [&](unsigned Opc, unsigned RegIdx, int IdxBE, 1567 int IdxLE) { 1568 Register VSRpReg0 = MF.getRegInfo().createVirtualRegister(RC); 1569 Register VSRpReg1 = MF.getRegInfo().createVirtualRegister(RC); 1570 1571 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg0), 1572 FrameIndex, IsLittleEndian ? IdxLE : IdxBE); 1573 addFrameReference(BuildMI(MBB, II, DL, TII.get(PPC::LXVP), VSRpReg1), 1574 FrameIndex, IsLittleEndian ? IdxLE - 32 : IdxBE + 32); 1575 1576 // Kill virtual registers (killedRegState::Killed). 1577 BuildMI(MBB, II, DL, TII.get(Opc), 1578 TargetRegisterInfo::getSubReg(DestReg, RegIdx)) 1579 .addReg(VSRpReg0, RegState::Kill) 1580 .addReg(VSRpReg1, RegState::Kill); 1581 }; 1582 restoreWACC(PPC::DMXXINSTDMR512, PPC::sub_wacc_lo, BEIdx, LEIdx); 1583 restoreWACC(PPC::DMXXINSTDMR512_HI, PPC::sub_wacc_hi, BEIdx + 64, 1584 LEIdx - 64); 1585 }; 1586 1587 Register DestReg = MI.getOperand(0).getReg(); 1588 if (MI.getOpcode() == PPC::RESTORE_DMRP) { 1589 restoreDMR(TargetRegisterInfo::getSubReg(DestReg, PPC::sub_dmr1), 0, 96); 1590 restoreDMR(TargetRegisterInfo::getSubReg(DestReg, PPC::sub_dmr0), 128, 224); 1591 } else 1592 restoreDMR(DestReg, 0, 96); 1593 1594 // Discard the pseudo instruction. 1595 MBB.erase(II); 1596 } 1597 1598 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 1599 Register Reg, int &FrameIdx) const { 1600 // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 1601 // prevent allocating an additional frame slot. 1602 // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 1603 // for 32-bit AIX the CR save area is in the linkage area at SP+4. 1604 // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 1605 // valid. 1606 // For 32-bit ELF, we have previously created the stack slot if needed, so 1607 // return its FrameIdx. 1608 if (PPC::CR2 <= Reg && Reg <= PPC::CR4) { 1609 FrameIdx = MF.getInfo<PPCFunctionInfo>()->getCRSpillFrameIndex(); 1610 return true; 1611 } 1612 return false; 1613 } 1614 1615 // If the offset must be a multiple of some value, return what that value is. 1616 static unsigned offsetMinAlignForOpcode(unsigned OpC) { 1617 switch (OpC) { 1618 default: 1619 return 1; 1620 case PPC::LWA: 1621 case PPC::LWA_32: 1622 case PPC::LD: 1623 case PPC::LDU: 1624 case PPC::STD: 1625 case PPC::STDU: 1626 case PPC::DFLOADf32: 1627 case PPC::DFLOADf64: 1628 case PPC::DFSTOREf32: 1629 case PPC::DFSTOREf64: 1630 case PPC::LXSD: 1631 case PPC::LXSSP: 1632 case PPC::STXSD: 1633 case PPC::STXSSP: 1634 case PPC::STQ: 1635 return 4; 1636 case PPC::EVLDD: 1637 case PPC::EVSTDD: 1638 return 8; 1639 case PPC::LXV: 1640 case PPC::STXV: 1641 case PPC::LQ: 1642 case PPC::LXVP: 1643 case PPC::STXVP: 1644 return 16; 1645 } 1646 } 1647 1648 // If the offset must be a multiple of some value, return what that value is. 1649 static unsigned offsetMinAlign(const MachineInstr &MI) { 1650 unsigned OpC = MI.getOpcode(); 1651 return offsetMinAlignForOpcode(OpC); 1652 } 1653 1654 // Return the OffsetOperandNo given the FIOperandNum (and the instruction). 1655 static unsigned getOffsetONFromFION(const MachineInstr &MI, 1656 unsigned FIOperandNum) { 1657 // Take into account whether it's an add or mem instruction 1658 unsigned OffsetOperandNo = (FIOperandNum == 2) ? 1 : 2; 1659 if (MI.isInlineAsm()) 1660 OffsetOperandNo = FIOperandNum - 1; 1661 else if (MI.getOpcode() == TargetOpcode::STACKMAP || 1662 MI.getOpcode() == TargetOpcode::PATCHPOINT) 1663 OffsetOperandNo = FIOperandNum + 1; 1664 1665 return OffsetOperandNo; 1666 } 1667 1668 bool 1669 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II, 1670 int SPAdj, unsigned FIOperandNum, 1671 RegScavenger *RS) const { 1672 assert(SPAdj == 0 && "Unexpected"); 1673 1674 // Get the instruction. 1675 MachineInstr &MI = *II; 1676 // Get the instruction's basic block. 1677 MachineBasicBlock &MBB = *MI.getParent(); 1678 // Get the basic block's function. 1679 MachineFunction &MF = *MBB.getParent(); 1680 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1681 // Get the instruction info. 1682 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 1683 // Get the frame info. 1684 MachineFrameInfo &MFI = MF.getFrameInfo(); 1685 DebugLoc dl = MI.getDebugLoc(); 1686 1687 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 1688 1689 // Get the frame index. 1690 int FrameIndex = MI.getOperand(FIOperandNum).getIndex(); 1691 1692 // Get the frame pointer save index. Users of this index are primarily 1693 // DYNALLOC instructions. 1694 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 1695 int FPSI = FI->getFramePointerSaveIndex(); 1696 // Get the instruction opcode. 1697 unsigned OpC = MI.getOpcode(); 1698 1699 switch (OpC) { 1700 default: 1701 break; 1702 case PPC::DYNAREAOFFSET: 1703 case PPC::DYNAREAOFFSET8: 1704 lowerDynamicAreaOffset(II); 1705 // lowerDynamicAreaOffset erases II 1706 return true; 1707 case PPC::DYNALLOC: 1708 case PPC::DYNALLOC8: { 1709 // Special case for dynamic alloca. 1710 if (FPSI && FrameIndex == FPSI) { 1711 lowerDynamicAlloc(II); // lowerDynamicAlloc erases II 1712 return true; 1713 } 1714 break; 1715 } 1716 case PPC::PREPARE_PROBED_ALLOCA_64: 1717 case PPC::PREPARE_PROBED_ALLOCA_32: 1718 case PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_64: 1719 case PPC::PREPARE_PROBED_ALLOCA_NEGSIZE_SAME_REG_32: { 1720 if (FPSI && FrameIndex == FPSI) { 1721 lowerPrepareProbedAlloca(II); // lowerPrepareProbedAlloca erases II 1722 return true; 1723 } 1724 break; 1725 } 1726 case PPC::SPILL_CR: 1727 // Special case for pseudo-ops SPILL_CR and RESTORE_CR, etc. 1728 lowerCRSpilling(II, FrameIndex); 1729 return true; 1730 case PPC::RESTORE_CR: 1731 lowerCRRestore(II, FrameIndex); 1732 return true; 1733 case PPC::SPILL_CRBIT: 1734 lowerCRBitSpilling(II, FrameIndex); 1735 return true; 1736 case PPC::RESTORE_CRBIT: 1737 lowerCRBitRestore(II, FrameIndex); 1738 return true; 1739 case PPC::SPILL_ACC: 1740 case PPC::SPILL_UACC: 1741 lowerACCSpilling(II, FrameIndex); 1742 return true; 1743 case PPC::RESTORE_ACC: 1744 case PPC::RESTORE_UACC: 1745 lowerACCRestore(II, FrameIndex); 1746 return true; 1747 case PPC::STXVP: { 1748 if (DisableAutoPairedVecSt) { 1749 lowerOctWordSpilling(II, FrameIndex); 1750 return true; 1751 } 1752 break; 1753 } 1754 case PPC::SPILL_WACC: 1755 lowerWACCSpilling(II, FrameIndex); 1756 return true; 1757 case PPC::RESTORE_WACC: 1758 lowerWACCRestore(II, FrameIndex); 1759 return true; 1760 case PPC::SPILL_DMRP: 1761 case PPC::SPILL_DMR: 1762 lowerDMRSpilling(II, FrameIndex); 1763 return true; 1764 case PPC::RESTORE_DMRP: 1765 case PPC::RESTORE_DMR: 1766 lowerDMRRestore(II, FrameIndex); 1767 return true; 1768 case PPC::SPILL_QUADWORD: 1769 lowerQuadwordSpilling(II, FrameIndex); 1770 return true; 1771 case PPC::RESTORE_QUADWORD: 1772 lowerQuadwordRestore(II, FrameIndex); 1773 return true; 1774 } 1775 1776 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). 1777 MI.getOperand(FIOperandNum).ChangeToRegister( 1778 FrameIndex < 0 ? getBaseRegister(MF) : getFrameRegister(MF), false); 1779 1780 // If the instruction is not present in ImmToIdxMap, then it has no immediate 1781 // form (and must be r+r). 1782 bool noImmForm = !MI.isInlineAsm() && OpC != TargetOpcode::STACKMAP && 1783 OpC != TargetOpcode::PATCHPOINT && !ImmToIdxMap.count(OpC); 1784 1785 // Now add the frame object offset to the offset from r1. 1786 int64_t Offset = MFI.getObjectOffset(FrameIndex); 1787 Offset += MI.getOperand(OffsetOperandNo).getImm(); 1788 1789 // If we're not using a Frame Pointer that has been set to the value of the 1790 // SP before having the stack size subtracted from it, then add the stack size 1791 // to Offset to get the correct offset. 1792 // Naked functions have stack size 0, although getStackSize may not reflect 1793 // that because we didn't call all the pieces that compute it for naked 1794 // functions. 1795 if (!MF.getFunction().hasFnAttribute(Attribute::Naked)) { 1796 if (!(hasBasePointer(MF) && FrameIndex < 0)) 1797 Offset += MFI.getStackSize(); 1798 } 1799 1800 // If we encounter an LXVP/STXVP with an offset that doesn't fit, we can 1801 // transform it to the prefixed version so we don't have to use the XForm. 1802 if ((OpC == PPC::LXVP || OpC == PPC::STXVP) && 1803 (!isInt<16>(Offset) || (Offset % offsetMinAlign(MI)) != 0) && 1804 Subtarget.hasPrefixInstrs() && Subtarget.hasP10Vector()) { 1805 unsigned NewOpc = OpC == PPC::LXVP ? PPC::PLXVP : PPC::PSTXVP; 1806 MI.setDesc(TII.get(NewOpc)); 1807 OpC = NewOpc; 1808 } 1809 1810 // If we can, encode the offset directly into the instruction. If this is a 1811 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If 1812 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits 1813 // clear can be encoded. This is extremely uncommon, because normally you 1814 // only "std" to a stack slot that is at least 4-byte aligned, but it can 1815 // happen in invalid code. 1816 assert(OpC != PPC::DBG_VALUE && 1817 "This should be handled in a target-independent way"); 1818 // FIXME: This should be factored out to a separate function as prefixed 1819 // instructions add a number of opcodes for which we can use 34-bit imm. 1820 bool OffsetFitsMnemonic = (OpC == PPC::EVSTDD || OpC == PPC::EVLDD) ? 1821 isUInt<8>(Offset) : 1822 isInt<16>(Offset); 1823 if (TII.isPrefixed(MI.getOpcode())) 1824 OffsetFitsMnemonic = isInt<34>(Offset); 1825 if (!noImmForm && ((OffsetFitsMnemonic && 1826 ((Offset % offsetMinAlign(MI)) == 0)) || 1827 OpC == TargetOpcode::STACKMAP || 1828 OpC == TargetOpcode::PATCHPOINT)) { 1829 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 1830 return false; 1831 } 1832 1833 // The offset doesn't fit into a single register, scavenge one to build the 1834 // offset in. 1835 1836 bool is64Bit = TM.isPPC64(); 1837 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1838 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1839 const TargetRegisterClass *RC = is64Bit ? G8RC : GPRC; 1840 unsigned NewOpcode = 0u; 1841 bool ScavengingFailed = RS && RS->getRegsAvailable(RC).none() && 1842 RS->getRegsAvailable(&PPC::VSFRCRegClass).any(); 1843 Register SRegHi, SReg, VSReg; 1844 1845 // The register scavenger is unable to get a GPR but can get a VSR. We 1846 // need to stash a GPR into a VSR so that we can free one up. 1847 if (ScavengingFailed && Subtarget.hasDirectMove()) { 1848 // Pick a volatile register and if we are spilling/restoring that 1849 // particular one, pick the next one. 1850 SRegHi = SReg = is64Bit ? PPC::X4 : PPC::R4; 1851 if (MI.getOperand(0).getReg() == SReg) 1852 SRegHi = SReg = SReg + 1; 1853 VSReg = MF.getRegInfo().createVirtualRegister(&PPC::VSFRCRegClass); 1854 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::MTVSRD : PPC::MTVSRWZ), VSReg) 1855 .addReg(SReg); 1856 } else { 1857 SRegHi = MF.getRegInfo().createVirtualRegister(RC); 1858 SReg = MF.getRegInfo().createVirtualRegister(RC); 1859 } 1860 1861 // Insert a set of rA with the full offset value before the ld, st, or add 1862 if (isInt<16>(Offset)) 1863 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), SReg) 1864 .addImm(Offset); 1865 else if (isInt<32>(Offset)) { 1866 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LIS8 : PPC::LIS), SRegHi) 1867 .addImm(Offset >> 16); 1868 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::ORI8 : PPC::ORI), SReg) 1869 .addReg(SRegHi, RegState::Kill) 1870 .addImm(Offset); 1871 } else { 1872 assert(is64Bit && "Huge stack is only supported on PPC64"); 1873 TII.materializeImmPostRA(MBB, II, dl, SReg, Offset); 1874 } 1875 1876 // Convert into indexed form of the instruction: 1877 // 1878 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 1879 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 1880 unsigned OperandBase; 1881 1882 if (noImmForm) 1883 OperandBase = 1; 1884 else if (OpC != TargetOpcode::INLINEASM && 1885 OpC != TargetOpcode::INLINEASM_BR) { 1886 assert(ImmToIdxMap.count(OpC) && 1887 "No indexed form of load or store available!"); 1888 NewOpcode = ImmToIdxMap.find(OpC)->second; 1889 MI.setDesc(TII.get(NewOpcode)); 1890 OperandBase = 1; 1891 } else { 1892 OperandBase = OffsetOperandNo; 1893 } 1894 1895 Register StackReg = MI.getOperand(FIOperandNum).getReg(); 1896 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false); 1897 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false, false, true); 1898 1899 // If we stashed a value from a GPR into a VSR, we need to get it back after 1900 // spilling the register. 1901 if (ScavengingFailed && Subtarget.hasDirectMove()) 1902 BuildMI(MBB, ++II, dl, TII.get(is64Bit ? PPC::MFVSRD : PPC::MFVSRWZ), SReg) 1903 .addReg(VSReg); 1904 1905 // Since these are not real X-Form instructions, we must 1906 // add the registers and access 0(NewReg) rather than 1907 // emitting the X-Form pseudo. 1908 if (NewOpcode == PPC::LQX_PSEUDO || NewOpcode == PPC::STQX_PSEUDO) { 1909 assert(is64Bit && "Quadword loads/stores only supported in 64-bit mode"); 1910 Register NewReg = MF.getRegInfo().createVirtualRegister(&PPC::G8RCRegClass); 1911 BuildMI(MBB, II, dl, TII.get(PPC::ADD8), NewReg) 1912 .addReg(SReg, RegState::Kill) 1913 .addReg(StackReg); 1914 MI.setDesc(TII.get(NewOpcode == PPC::LQX_PSEUDO ? PPC::LQ : PPC::STQ)); 1915 MI.getOperand(OperandBase + 1).ChangeToRegister(NewReg, false); 1916 MI.getOperand(OperandBase).ChangeToImmediate(0); 1917 } 1918 return false; 1919 } 1920 1921 Register PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const { 1922 const PPCFrameLowering *TFI = getFrameLowering(MF); 1923 1924 if (!TM.isPPC64()) 1925 return TFI->hasFP(MF) ? PPC::R31 : PPC::R1; 1926 else 1927 return TFI->hasFP(MF) ? PPC::X31 : PPC::X1; 1928 } 1929 1930 Register PPCRegisterInfo::getBaseRegister(const MachineFunction &MF) const { 1931 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 1932 if (!hasBasePointer(MF)) 1933 return getFrameRegister(MF); 1934 1935 if (TM.isPPC64()) 1936 return PPC::X30; 1937 1938 if (Subtarget.isSVR4ABI() && TM.isPositionIndependent()) 1939 return PPC::R29; 1940 1941 return PPC::R30; 1942 } 1943 1944 bool PPCRegisterInfo::hasBasePointer(const MachineFunction &MF) const { 1945 if (!EnableBasePointer) 1946 return false; 1947 if (AlwaysBasePointer) 1948 return true; 1949 1950 // If we need to realign the stack, then the stack pointer can no longer 1951 // serve as an offset into the caller's stack space. As a result, we need a 1952 // base pointer. 1953 return hasStackRealignment(MF); 1954 } 1955 1956 /// Returns true if the instruction's frame index 1957 /// reference would be better served by a base register other than FP 1958 /// or SP. Used by LocalStackFrameAllocation to determine which frame index 1959 /// references it should create new base registers for. 1960 bool PPCRegisterInfo:: 1961 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const { 1962 assert(Offset < 0 && "Local offset must be negative"); 1963 1964 // It's the load/store FI references that cause issues, as it can be difficult 1965 // to materialize the offset if it won't fit in the literal field. Estimate 1966 // based on the size of the local frame and some conservative assumptions 1967 // about the rest of the stack frame (note, this is pre-regalloc, so 1968 // we don't know everything for certain yet) whether this offset is likely 1969 // to be out of range of the immediate. Return true if so. 1970 1971 // We only generate virtual base registers for loads and stores that have 1972 // an r+i form. Return false for everything else. 1973 unsigned OpC = MI->getOpcode(); 1974 if (!ImmToIdxMap.count(OpC)) 1975 return false; 1976 1977 // Don't generate a new virtual base register just to add zero to it. 1978 if ((OpC == PPC::ADDI || OpC == PPC::ADDI8) && 1979 MI->getOperand(2).getImm() == 0) 1980 return false; 1981 1982 MachineBasicBlock &MBB = *MI->getParent(); 1983 MachineFunction &MF = *MBB.getParent(); 1984 const PPCFrameLowering *TFI = getFrameLowering(MF); 1985 unsigned StackEst = TFI->determineFrameLayout(MF, true); 1986 1987 // If we likely don't need a stack frame, then we probably don't need a 1988 // virtual base register either. 1989 if (!StackEst) 1990 return false; 1991 1992 // Estimate an offset from the stack pointer. 1993 // The incoming offset is relating to the SP at the start of the function, 1994 // but when we access the local it'll be relative to the SP after local 1995 // allocation, so adjust our SP-relative offset by that allocation size. 1996 Offset += StackEst; 1997 1998 // The frame pointer will point to the end of the stack, so estimate the 1999 // offset as the difference between the object offset and the FP location. 2000 return !isFrameOffsetLegal(MI, getBaseRegister(MF), Offset); 2001 } 2002 2003 /// Insert defining instruction(s) for BaseReg to 2004 /// be a pointer to FrameIdx at the beginning of the basic block. 2005 Register PPCRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB, 2006 int FrameIdx, 2007 int64_t Offset) const { 2008 unsigned ADDriOpc = TM.isPPC64() ? PPC::ADDI8 : PPC::ADDI; 2009 2010 MachineBasicBlock::iterator Ins = MBB->begin(); 2011 DebugLoc DL; // Defaults to "unknown" 2012 if (Ins != MBB->end()) 2013 DL = Ins->getDebugLoc(); 2014 2015 const MachineFunction &MF = *MBB->getParent(); 2016 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2017 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 2018 const MCInstrDesc &MCID = TII.get(ADDriOpc); 2019 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 2020 const TargetRegisterClass *RC = getPointerRegClass(MF); 2021 Register BaseReg = MRI.createVirtualRegister(RC); 2022 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF)); 2023 2024 BuildMI(*MBB, Ins, DL, MCID, BaseReg) 2025 .addFrameIndex(FrameIdx).addImm(Offset); 2026 2027 return BaseReg; 2028 } 2029 2030 void PPCRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg, 2031 int64_t Offset) const { 2032 unsigned FIOperandNum = 0; 2033 while (!MI.getOperand(FIOperandNum).isFI()) { 2034 ++FIOperandNum; 2035 assert(FIOperandNum < MI.getNumOperands() && 2036 "Instr doesn't have FrameIndex operand!"); 2037 } 2038 2039 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false); 2040 unsigned OffsetOperandNo = getOffsetONFromFION(MI, FIOperandNum); 2041 Offset += MI.getOperand(OffsetOperandNo).getImm(); 2042 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset); 2043 2044 MachineBasicBlock &MBB = *MI.getParent(); 2045 MachineFunction &MF = *MBB.getParent(); 2046 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 2047 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 2048 const MCInstrDesc &MCID = MI.getDesc(); 2049 MachineRegisterInfo &MRI = MF.getRegInfo(); 2050 MRI.constrainRegClass(BaseReg, 2051 TII.getRegClass(MCID, FIOperandNum, this, MF)); 2052 } 2053 2054 bool PPCRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, 2055 Register BaseReg, 2056 int64_t Offset) const { 2057 unsigned FIOperandNum = 0; 2058 while (!MI->getOperand(FIOperandNum).isFI()) { 2059 ++FIOperandNum; 2060 assert(FIOperandNum < MI->getNumOperands() && 2061 "Instr doesn't have FrameIndex operand!"); 2062 } 2063 2064 unsigned OffsetOperandNo = getOffsetONFromFION(*MI, FIOperandNum); 2065 Offset += MI->getOperand(OffsetOperandNo).getImm(); 2066 2067 return MI->getOpcode() == PPC::DBG_VALUE || // DBG_VALUE is always Reg+Imm 2068 MI->getOpcode() == TargetOpcode::STACKMAP || 2069 MI->getOpcode() == TargetOpcode::PATCHPOINT || 2070 (isInt<16>(Offset) && (Offset % offsetMinAlign(*MI)) == 0); 2071 } 2072