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