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