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