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