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