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_PPC64_R2_Altivec_SaveList 178 : CSR_PPC64_Altivec_SaveList; 179 return SaveR2 ? CSR_PPC64_R2_SaveList : CSR_PPC64_SaveList; 180 } 181 // 32-bit targets. 182 if (Subtarget.hasAltivec()) 183 return CSR_SVR432_Altivec_SaveList; 184 else if (Subtarget.hasSPE()) 185 return CSR_SVR432_SPE_SaveList; 186 return CSR_SVR432_SaveList; 187 } 188 189 const MCPhysReg * 190 PPCRegisterInfo::getCalleeSavedRegsViaCopy(const MachineFunction *MF) const { 191 assert(MF && "Invalid MachineFunction pointer."); 192 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 193 if (!TM.isPPC64()) 194 return nullptr; 195 if (MF->getFunction().getCallingConv() != CallingConv::CXX_FAST_TLS) 196 return nullptr; 197 if (!MF->getInfo<PPCFunctionInfo>()->isSplitCSR()) 198 return nullptr; 199 200 // On PPC64, we might need to save r2 (but only if it is not reserved). 201 bool SaveR2 = !getReservedRegs(*MF).test(PPC::X2); 202 if (Subtarget.hasAltivec()) 203 return SaveR2 204 ? CSR_SVR464_R2_Altivec_ViaCopy_SaveList 205 : CSR_SVR464_Altivec_ViaCopy_SaveList; 206 else 207 return SaveR2 208 ? CSR_SVR464_R2_ViaCopy_SaveList 209 : CSR_SVR464_ViaCopy_SaveList; 210 } 211 212 const uint32_t * 213 PPCRegisterInfo::getCallPreservedMask(const MachineFunction &MF, 214 CallingConv::ID CC) const { 215 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 216 if (CC == CallingConv::AnyReg) { 217 if (Subtarget.hasVSX()) 218 return CSR_64_AllRegs_VSX_RegMask; 219 if (Subtarget.hasAltivec()) 220 return CSR_64_AllRegs_Altivec_RegMask; 221 return CSR_64_AllRegs_RegMask; 222 } 223 224 if (Subtarget.isAIXABI()) { 225 assert(!Subtarget.hasAltivec() && "Altivec is not implemented on AIX yet."); 226 return TM.isPPC64() ? CSR_PPC64_RegMask : CSR_AIX32_RegMask; 227 } 228 229 if (CC == CallingConv::Cold) { 230 return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_SVR64_ColdCC_Altivec_RegMask 231 : CSR_SVR64_ColdCC_RegMask) 232 : (Subtarget.hasAltivec() ? CSR_SVR32_ColdCC_Altivec_RegMask 233 : (Subtarget.hasSPE() 234 ? CSR_SVR32_ColdCC_SPE_RegMask 235 : CSR_SVR32_ColdCC_RegMask)); 236 } 237 238 return TM.isPPC64() ? (Subtarget.hasAltivec() ? CSR_PPC64_Altivec_RegMask 239 : CSR_PPC64_RegMask) 240 : (Subtarget.hasAltivec() 241 ? CSR_SVR432_Altivec_RegMask 242 : (Subtarget.hasSPE() ? CSR_SVR432_SPE_RegMask 243 : CSR_SVR432_RegMask)); 244 } 245 246 const uint32_t* 247 PPCRegisterInfo::getNoPreservedMask() const { 248 return CSR_NoRegs_RegMask; 249 } 250 251 void PPCRegisterInfo::adjustStackMapLiveOutMask(uint32_t *Mask) const { 252 for (unsigned PseudoReg : {PPC::ZERO, PPC::ZERO8, PPC::RM}) 253 Mask[PseudoReg / 32] &= ~(1u << (PseudoReg % 32)); 254 } 255 256 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 257 BitVector Reserved(getNumRegs()); 258 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 259 const PPCFrameLowering *TFI = getFrameLowering(MF); 260 261 // The ZERO register is not really a register, but the representation of r0 262 // when used in instructions that treat r0 as the constant 0. 263 markSuperRegs(Reserved, PPC::ZERO); 264 265 // The FP register is also not really a register, but is the representation 266 // of the frame pointer register used by ISD::FRAMEADDR. 267 markSuperRegs(Reserved, PPC::FP); 268 269 // The BP register is also not really a register, but is the representation 270 // of the base pointer register used by setjmp. 271 markSuperRegs(Reserved, PPC::BP); 272 273 // The counter registers must be reserved so that counter-based loops can 274 // be correctly formed (and the mtctr instructions are not DCE'd). 275 markSuperRegs(Reserved, PPC::CTR); 276 markSuperRegs(Reserved, PPC::CTR8); 277 278 markSuperRegs(Reserved, PPC::R1); 279 markSuperRegs(Reserved, PPC::LR); 280 markSuperRegs(Reserved, PPC::LR8); 281 markSuperRegs(Reserved, PPC::RM); 282 283 markSuperRegs(Reserved, PPC::VRSAVE); 284 285 // The SVR4 ABI reserves r2 and r13 286 if (Subtarget.isSVR4ABI()) { 287 // We only reserve r2 if we need to use the TOC pointer. If we have no 288 // explicit uses of the TOC pointer (meaning we're a leaf function with 289 // no constant-pool loads, etc.) and we have no potential uses inside an 290 // inline asm block, then we can treat r2 has an ordinary callee-saved 291 // register. 292 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 293 if (!TM.isPPC64() || FuncInfo->usesTOCBasePtr() || MF.hasInlineAsm()) 294 markSuperRegs(Reserved, PPC::R2); // System-reserved register 295 markSuperRegs(Reserved, PPC::R13); // Small Data Area pointer register 296 } 297 298 // Always reserve r2 on AIX for now. 299 // TODO: Make r2 allocatable on AIX/XCOFF for some leaf functions. 300 if (Subtarget.isAIXABI()) 301 markSuperRegs(Reserved, PPC::R2); // System-reserved register 302 303 // On PPC64, r13 is the thread pointer. Never allocate this register. 304 if (TM.isPPC64()) 305 markSuperRegs(Reserved, PPC::R13); 306 307 if (TFI->needsFP(MF)) 308 markSuperRegs(Reserved, PPC::R31); 309 310 bool IsPositionIndependent = TM.isPositionIndependent(); 311 if (hasBasePointer(MF)) { 312 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 313 markSuperRegs(Reserved, PPC::R29); 314 else 315 markSuperRegs(Reserved, PPC::R30); 316 } 317 318 if (Subtarget.is32BitELFABI() && IsPositionIndependent) 319 markSuperRegs(Reserved, PPC::R30); 320 321 // Reserve Altivec registers when Altivec is unavailable. 322 if (!Subtarget.hasAltivec()) 323 for (TargetRegisterClass::iterator I = PPC::VRRCRegClass.begin(), 324 IE = PPC::VRRCRegClass.end(); I != IE; ++I) 325 markSuperRegs(Reserved, *I); 326 327 assert(checkAllSuperRegsMarked(Reserved)); 328 return Reserved; 329 } 330 331 bool PPCRegisterInfo::requiresFrameIndexScavenging(const MachineFunction &MF) const { 332 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 333 const PPCInstrInfo *InstrInfo = Subtarget.getInstrInfo(); 334 const MachineFrameInfo &MFI = MF.getFrameInfo(); 335 const std::vector<CalleeSavedInfo> &Info = MFI.getCalleeSavedInfo(); 336 337 // If the callee saved info is invalid we have to default to true for safety. 338 if (!MFI.isCalleeSavedInfoValid()) 339 return true; 340 341 // We will require the use of X-Forms because the frame is larger than what 342 // can be represented in signed 16 bits that fit in the immediate of a D-Form. 343 // If we need an X-Form then we need a register to store the address offset. 344 unsigned FrameSize = MFI.getStackSize(); 345 // Signed 16 bits means that the FrameSize cannot be more than 15 bits. 346 if (FrameSize & ~0x7FFF) 347 return true; 348 349 // The callee saved info is valid so it can be traversed. 350 // Checking for registers that need saving that do not have load or store 351 // forms where the address offset is an immediate. 352 for (unsigned i = 0; i < Info.size(); i++) { 353 int FrIdx = Info[i].getFrameIdx(); 354 unsigned Reg = Info[i].getReg(); 355 356 unsigned Opcode = InstrInfo->getStoreOpcodeForSpill(Reg); 357 if (!MFI.isFixedObjectIndex(FrIdx)) { 358 // This is not a fixed object. If it requires alignment then we may still 359 // need to use the XForm. 360 if (offsetMinAlignForOpcode(Opcode) > 1) 361 return true; 362 } 363 364 // This is eiher: 365 // 1) A fixed frame index object which we know are aligned so 366 // as long as we have a valid DForm/DSForm/DQForm (non XForm) we don't 367 // need to consider the alignment here. 368 // 2) A not fixed object but in that case we now know that the min required 369 // alignment is no more than 1 based on the previous check. 370 if (InstrInfo->isXFormMemOp(Opcode)) 371 return true; 372 } 373 return false; 374 } 375 376 bool PPCRegisterInfo::isCallerPreservedPhysReg(MCRegister PhysReg, 377 const MachineFunction &MF) const { 378 assert(Register::isPhysicalRegister(PhysReg)); 379 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 380 const MachineFrameInfo &MFI = MF.getFrameInfo(); 381 if (!TM.isPPC64()) 382 return false; 383 384 if (!Subtarget.isSVR4ABI()) 385 return false; 386 if (PhysReg == PPC::X2) 387 // X2 is guaranteed to be preserved within a function if it is reserved. 388 // The reason it's reserved is that it's the TOC pointer (and the function 389 // uses the TOC). In functions where it isn't reserved (i.e. leaf functions 390 // with no TOC access), we can't claim that it is preserved. 391 return (getReservedRegs(MF).test(PPC::X2)); 392 if (StackPtrConst && (PhysReg == PPC::X1) && !MFI.hasVarSizedObjects() 393 && !MFI.hasOpaqueSPAdjustment()) 394 // The value of the stack pointer does not change within a function after 395 // the prologue and before the epilogue if there are no dynamic allocations 396 // and no inline asm which clobbers X1. 397 return true; 398 return false; 399 } 400 401 unsigned PPCRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC, 402 MachineFunction &MF) const { 403 const PPCFrameLowering *TFI = getFrameLowering(MF); 404 const unsigned DefaultSafety = 1; 405 406 switch (RC->getID()) { 407 default: 408 return 0; 409 case PPC::G8RC_NOX0RegClassID: 410 case PPC::GPRC_NOR0RegClassID: 411 case PPC::SPERCRegClassID: 412 case PPC::G8RCRegClassID: 413 case PPC::GPRCRegClassID: { 414 unsigned FP = TFI->hasFP(MF) ? 1 : 0; 415 return 32 - FP - DefaultSafety; 416 } 417 case PPC::F8RCRegClassID: 418 case PPC::F4RCRegClassID: 419 case PPC::QFRCRegClassID: 420 case PPC::QSRCRegClassID: 421 case PPC::QBRCRegClassID: 422 case PPC::VRRCRegClassID: 423 case PPC::VFRCRegClassID: 424 case PPC::VSLRCRegClassID: 425 return 32 - DefaultSafety; 426 case PPC::VSRCRegClassID: 427 case PPC::VSFRCRegClassID: 428 case PPC::VSSRCRegClassID: 429 return 64 - DefaultSafety; 430 case PPC::CRRCRegClassID: 431 return 8 - DefaultSafety; 432 } 433 } 434 435 const TargetRegisterClass * 436 PPCRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC, 437 const MachineFunction &MF) const { 438 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 439 if (Subtarget.hasVSX()) { 440 // With VSX, we can inflate various sub-register classes to the full VSX 441 // register set. 442 443 // For Power9 we allow the user to enable GPR to vector spills. 444 // FIXME: Currently limited to spilling GP8RC. A follow on patch will add 445 // support to spill GPRC. 446 if (TM.isELFv2ABI()) { 447 if (Subtarget.hasP9Vector() && EnableGPRToVecSpills && 448 RC == &PPC::G8RCRegClass) { 449 InflateGP8RC++; 450 return &PPC::SPILLTOVSRRCRegClass; 451 } 452 if (RC == &PPC::GPRCRegClass && EnableGPRToVecSpills) 453 InflateGPRC++; 454 } 455 if (RC == &PPC::F8RCRegClass) 456 return &PPC::VSFRCRegClass; 457 else if (RC == &PPC::VRRCRegClass) 458 return &PPC::VSRCRegClass; 459 else if (RC == &PPC::F4RCRegClass && Subtarget.hasP8Vector()) 460 return &PPC::VSSRCRegClass; 461 } 462 463 return TargetRegisterInfo::getLargestLegalSuperClass(RC, MF); 464 } 465 466 //===----------------------------------------------------------------------===// 467 // Stack Frame Processing methods 468 //===----------------------------------------------------------------------===// 469 470 /// lowerDynamicAlloc - Generate the code for allocating an object in the 471 /// current frame. The sequence of code will be in the general form 472 /// 473 /// addi R0, SP, \#frameSize ; get the address of the previous frame 474 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size 475 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation 476 /// 477 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { 478 // Get the instruction. 479 MachineInstr &MI = *II; 480 // Get the instruction's basic block. 481 MachineBasicBlock &MBB = *MI.getParent(); 482 // Get the basic block's function. 483 MachineFunction &MF = *MBB.getParent(); 484 // Get the frame info. 485 MachineFrameInfo &MFI = MF.getFrameInfo(); 486 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 487 // Get the instruction info. 488 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 489 // Determine whether 64-bit pointers are used. 490 bool LP64 = TM.isPPC64(); 491 DebugLoc dl = MI.getDebugLoc(); 492 493 // Get the maximum call stack size. 494 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 495 // Get the total frame size. 496 unsigned FrameSize = MFI.getStackSize(); 497 498 // Get stack alignments. 499 const PPCFrameLowering *TFI = getFrameLowering(MF); 500 Align TargetAlign = TFI->getStackAlign(); 501 Align MaxAlign = MFI.getMaxAlign(); 502 assert(isAligned(MaxAlign, maxCallFrameSize) && 503 "Maximum call-frame size not sufficiently aligned"); 504 505 // Determine the previous frame's address. If FrameSize can't be 506 // represented as 16 bits or we need special alignment, then we load the 507 // previous frame's address from 0(SP). Why not do an addis of the hi? 508 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero. 509 // Constructing the constant and adding would take 3 instructions. 510 // Fortunately, a frame greater than 32K is rare. 511 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 512 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 513 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 514 515 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { 516 if (LP64) 517 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), Reg) 518 .addReg(PPC::X31) 519 .addImm(FrameSize); 520 else 521 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg) 522 .addReg(PPC::R31) 523 .addImm(FrameSize); 524 } else if (LP64) { 525 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg) 526 .addImm(0) 527 .addReg(PPC::X1); 528 } else { 529 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg) 530 .addImm(0) 531 .addReg(PPC::R1); 532 } 533 534 bool KillNegSizeReg = MI.getOperand(1).isKill(); 535 Register NegSizeReg = MI.getOperand(1).getReg(); 536 537 // Grow the stack and update the stack pointer link, then determine the 538 // address of new allocated space. 539 if (LP64) { 540 if (MaxAlign > TargetAlign) { 541 unsigned UnalNegSizeReg = NegSizeReg; 542 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 543 544 // Unfortunately, there is no andi, only andi., and we can't insert that 545 // here because we might clobber cr0 while it is live. 546 BuildMI(MBB, II, dl, TII.get(PPC::LI8), NegSizeReg) 547 .addImm(~(MaxAlign.value() - 1)); 548 549 unsigned NegSizeReg1 = NegSizeReg; 550 NegSizeReg = MF.getRegInfo().createVirtualRegister(G8RC); 551 BuildMI(MBB, II, dl, TII.get(PPC::AND8), NegSizeReg) 552 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 553 .addReg(NegSizeReg1, RegState::Kill); 554 KillNegSizeReg = true; 555 } 556 557 BuildMI(MBB, II, dl, TII.get(PPC::STDUX), PPC::X1) 558 .addReg(Reg, RegState::Kill) 559 .addReg(PPC::X1) 560 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 561 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg()) 562 .addReg(PPC::X1) 563 .addImm(maxCallFrameSize); 564 } else { 565 if (MaxAlign > TargetAlign) { 566 unsigned UnalNegSizeReg = NegSizeReg; 567 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 568 569 // Unfortunately, there is no andi, only andi., and we can't insert that 570 // here because we might clobber cr0 while it is live. 571 BuildMI(MBB, II, dl, TII.get(PPC::LI), NegSizeReg) 572 .addImm(~(MaxAlign.value() - 1)); 573 574 unsigned NegSizeReg1 = NegSizeReg; 575 NegSizeReg = MF.getRegInfo().createVirtualRegister(GPRC); 576 BuildMI(MBB, II, dl, TII.get(PPC::AND), NegSizeReg) 577 .addReg(UnalNegSizeReg, getKillRegState(KillNegSizeReg)) 578 .addReg(NegSizeReg1, RegState::Kill); 579 KillNegSizeReg = true; 580 } 581 582 BuildMI(MBB, II, dl, TII.get(PPC::STWUX), PPC::R1) 583 .addReg(Reg, RegState::Kill) 584 .addReg(PPC::R1) 585 .addReg(NegSizeReg, getKillRegState(KillNegSizeReg)); 586 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg()) 587 .addReg(PPC::R1) 588 .addImm(maxCallFrameSize); 589 } 590 591 // Discard the DYNALLOC instruction. 592 MBB.erase(II); 593 } 594 595 void PPCRegisterInfo::lowerDynamicAreaOffset( 596 MachineBasicBlock::iterator II) const { 597 // Get the instruction. 598 MachineInstr &MI = *II; 599 // Get the instruction's basic block. 600 MachineBasicBlock &MBB = *MI.getParent(); 601 // Get the basic block's function. 602 MachineFunction &MF = *MBB.getParent(); 603 // Get the frame info. 604 MachineFrameInfo &MFI = MF.getFrameInfo(); 605 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 606 // Get the instruction info. 607 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 608 609 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 610 bool is64Bit = TM.isPPC64(); 611 DebugLoc dl = MI.getDebugLoc(); 612 BuildMI(MBB, II, dl, TII.get(is64Bit ? PPC::LI8 : PPC::LI), 613 MI.getOperand(0).getReg()) 614 .addImm(maxCallFrameSize); 615 MBB.erase(II); 616 } 617 618 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of 619 /// reserving a whole register (R0), we scrounge for one here. This generates 620 /// code like this: 621 /// 622 /// mfcr rA ; Move the conditional register into GPR rA. 623 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot. 624 /// stw rA, FI ; Store rA to the frame. 625 /// 626 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II, 627 unsigned FrameIndex) const { 628 // Get the instruction. 629 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset> 630 // Get the instruction's basic block. 631 MachineBasicBlock &MBB = *MI.getParent(); 632 MachineFunction &MF = *MBB.getParent(); 633 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 634 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 635 DebugLoc dl = MI.getDebugLoc(); 636 637 bool LP64 = TM.isPPC64(); 638 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 639 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 640 641 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 642 Register SrcReg = MI.getOperand(0).getReg(); 643 644 // We need to store the CR in the low 4-bits of the saved value. First, issue 645 // an MFOCRF to save all of the CRBits and, if needed, kill the SrcReg. 646 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 647 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 648 649 // If the saved register wasn't CR0, shift the bits left so that they are in 650 // CR0's slot. 651 if (SrcReg != PPC::CR0) { 652 Register Reg1 = Reg; 653 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 654 655 // rlwinm rA, rA, ShiftBits, 0, 31. 656 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 657 .addReg(Reg1, RegState::Kill) 658 .addImm(getEncodingValue(SrcReg) * 4) 659 .addImm(0) 660 .addImm(31); 661 } 662 663 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 664 .addReg(Reg, RegState::Kill), 665 FrameIndex); 666 667 // Discard the pseudo instruction. 668 MBB.erase(II); 669 } 670 671 void PPCRegisterInfo::lowerCRRestore(MachineBasicBlock::iterator II, 672 unsigned FrameIndex) const { 673 // Get the instruction. 674 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CR <offset> 675 // Get the instruction's basic block. 676 MachineBasicBlock &MBB = *MI.getParent(); 677 MachineFunction &MF = *MBB.getParent(); 678 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 679 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 680 DebugLoc dl = MI.getDebugLoc(); 681 682 bool LP64 = TM.isPPC64(); 683 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 684 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 685 686 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 687 Register DestReg = MI.getOperand(0).getReg(); 688 assert(MI.definesRegister(DestReg) && 689 "RESTORE_CR does not define its destination"); 690 691 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 692 Reg), FrameIndex); 693 694 // If the reloaded register isn't CR0, shift the bits right so that they are 695 // in the right CR's slot. 696 if (DestReg != PPC::CR0) { 697 Register Reg1 = Reg; 698 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 699 700 unsigned ShiftBits = getEncodingValue(DestReg)*4; 701 // rlwinm r11, r11, 32-ShiftBits, 0, 31. 702 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 703 .addReg(Reg1, RegState::Kill).addImm(32-ShiftBits).addImm(0) 704 .addImm(31); 705 } 706 707 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), DestReg) 708 .addReg(Reg, RegState::Kill); 709 710 // Discard the pseudo instruction. 711 MBB.erase(II); 712 } 713 714 void PPCRegisterInfo::lowerCRBitSpilling(MachineBasicBlock::iterator II, 715 unsigned FrameIndex) const { 716 // Get the instruction. 717 MachineInstr &MI = *II; // ; SPILL_CRBIT <SrcReg>, <offset> 718 // Get the instruction's basic block. 719 MachineBasicBlock &MBB = *MI.getParent(); 720 MachineFunction &MF = *MBB.getParent(); 721 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 722 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 723 const TargetRegisterInfo* TRI = Subtarget.getRegisterInfo(); 724 DebugLoc dl = MI.getDebugLoc(); 725 726 bool LP64 = TM.isPPC64(); 727 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 728 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 729 730 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 731 Register SrcReg = MI.getOperand(0).getReg(); 732 733 // Search up the BB to find the definition of the CR bit. 734 MachineBasicBlock::reverse_iterator Ins = MI; 735 MachineBasicBlock::reverse_iterator Rend = MBB.rend(); 736 ++Ins; 737 unsigned CRBitSpillDistance = 0; 738 bool SeenUse = false; 739 for (; Ins != Rend; ++Ins) { 740 // Definition found. 741 if (Ins->modifiesRegister(SrcReg, TRI)) 742 break; 743 // Use found. 744 if (Ins->readsRegister(SrcReg, TRI)) 745 SeenUse = true; 746 // Unable to find CR bit definition within maximum search distance. 747 if (CRBitSpillDistance == MaxCRBitSpillDist) { 748 Ins = MI; 749 break; 750 } 751 // Skip debug instructions when counting CR bit spill distance. 752 if (!Ins->isDebugInstr()) 753 CRBitSpillDistance++; 754 } 755 756 // Unable to find the definition of the CR bit in the MBB. 757 if (Ins == MBB.rend()) 758 Ins = MI; 759 760 bool SpillsKnownBit = false; 761 // There is no need to extract the CR bit if its value is already known. 762 switch (Ins->getOpcode()) { 763 case PPC::CRUNSET: 764 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LI8 : PPC::LI), Reg) 765 .addImm(0); 766 SpillsKnownBit = true; 767 break; 768 case PPC::CRSET: 769 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LIS8 : PPC::LIS), Reg) 770 .addImm(-32768); 771 SpillsKnownBit = true; 772 break; 773 default: 774 // On Power9, we can use SETB to extract the LT bit. This only works for 775 // the LT bit since SETB produces -1/1/0 for LT/GT/<neither>. So the value 776 // of the bit we care about (32-bit sign bit) will be set to the value of 777 // the LT bit (regardless of the other bits in the CR field). 778 if (Subtarget.isISA3_0()) { 779 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR1LT || 780 SrcReg == PPC::CR2LT || SrcReg == PPC::CR3LT || 781 SrcReg == PPC::CR4LT || SrcReg == PPC::CR5LT || 782 SrcReg == PPC::CR6LT || SrcReg == PPC::CR7LT) { 783 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::SETB8 : PPC::SETB), Reg) 784 .addReg(getCRFromCRBit(SrcReg), RegState::Undef); 785 break; 786 } 787 } 788 789 // We need to move the CR field that contains the CR bit we are spilling. 790 // The super register may not be explicitly defined (i.e. it can be defined 791 // by a CR-logical that only defines the subreg) so we state that the CR 792 // field is undef. Also, in order to preserve the kill flag on the CR bit, 793 // we add it as an implicit use. 794 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), Reg) 795 .addReg(getCRFromCRBit(SrcReg), RegState::Undef) 796 .addReg(SrcReg, 797 RegState::Implicit | getKillRegState(MI.getOperand(0).isKill())); 798 799 // If the saved register wasn't CR0LT, shift the bits left so that the bit 800 // to store is the first one. Mask all but that bit. 801 Register Reg1 = Reg; 802 Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 803 804 // rlwinm rA, rA, ShiftBits, 0, 0. 805 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWINM8 : PPC::RLWINM), Reg) 806 .addReg(Reg1, RegState::Kill) 807 .addImm(getEncodingValue(SrcReg)) 808 .addImm(0).addImm(0); 809 } 810 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW)) 811 .addReg(Reg, RegState::Kill), 812 FrameIndex); 813 814 bool KillsCRBit = MI.killsRegister(SrcReg, TRI); 815 // Discard the pseudo instruction. 816 MBB.erase(II); 817 if (SpillsKnownBit && KillsCRBit && !SeenUse) { 818 Ins->setDesc(TII.get(PPC::UNENCODED_NOP)); 819 Ins->RemoveOperand(0); 820 } 821 } 822 823 void PPCRegisterInfo::lowerCRBitRestore(MachineBasicBlock::iterator II, 824 unsigned FrameIndex) const { 825 // Get the instruction. 826 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_CRBIT <offset> 827 // Get the instruction's basic block. 828 MachineBasicBlock &MBB = *MI.getParent(); 829 MachineFunction &MF = *MBB.getParent(); 830 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 831 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 832 DebugLoc dl = MI.getDebugLoc(); 833 834 bool LP64 = TM.isPPC64(); 835 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 836 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 837 838 Register Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 839 Register DestReg = MI.getOperand(0).getReg(); 840 assert(MI.definesRegister(DestReg) && 841 "RESTORE_CRBIT does not define its destination"); 842 843 addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::LWZ8 : PPC::LWZ), 844 Reg), FrameIndex); 845 846 BuildMI(MBB, II, dl, TII.get(TargetOpcode::IMPLICIT_DEF), DestReg); 847 848 Register RegO = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); 849 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MFOCRF8 : PPC::MFOCRF), RegO) 850 .addReg(getCRFromCRBit(DestReg)); 851 852 unsigned ShiftBits = getEncodingValue(DestReg); 853 // rlwimi r11, r10, 32-ShiftBits, ..., ... 854 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::RLWIMI8 : PPC::RLWIMI), RegO) 855 .addReg(RegO, RegState::Kill) 856 .addReg(Reg, RegState::Kill) 857 .addImm(ShiftBits ? 32 - ShiftBits : 0) 858 .addImm(ShiftBits) 859 .addImm(ShiftBits); 860 861 BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::MTOCRF8 : PPC::MTOCRF), 862 getCRFromCRBit(DestReg)) 863 .addReg(RegO, RegState::Kill) 864 // Make sure we have a use dependency all the way through this 865 // sequence of instructions. We can't have the other bits in the CR 866 // modified in between the mfocrf and the mtocrf. 867 .addReg(getCRFromCRBit(DestReg), RegState::Implicit); 868 869 // Discard the pseudo instruction. 870 MBB.erase(II); 871 } 872 873 void PPCRegisterInfo::lowerVRSAVESpilling(MachineBasicBlock::iterator II, 874 unsigned FrameIndex) const { 875 // Get the instruction. 876 MachineInstr &MI = *II; // ; SPILL_VRSAVE <SrcReg>, <offset> 877 // Get the instruction's basic block. 878 MachineBasicBlock &MBB = *MI.getParent(); 879 MachineFunction &MF = *MBB.getParent(); 880 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 881 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 882 DebugLoc dl = MI.getDebugLoc(); 883 884 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 885 Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 886 Register SrcReg = MI.getOperand(0).getReg(); 887 888 BuildMI(MBB, II, dl, TII.get(PPC::MFVRSAVEv), Reg) 889 .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill())); 890 891 addFrameReference( 892 BuildMI(MBB, II, dl, TII.get(PPC::STW)).addReg(Reg, RegState::Kill), 893 FrameIndex); 894 895 // Discard the pseudo instruction. 896 MBB.erase(II); 897 } 898 899 void PPCRegisterInfo::lowerVRSAVERestore(MachineBasicBlock::iterator II, 900 unsigned FrameIndex) const { 901 // Get the instruction. 902 MachineInstr &MI = *II; // ; <DestReg> = RESTORE_VRSAVE <offset> 903 // Get the instruction's basic block. 904 MachineBasicBlock &MBB = *MI.getParent(); 905 MachineFunction &MF = *MBB.getParent(); 906 const PPCSubtarget &Subtarget = MF.getSubtarget<PPCSubtarget>(); 907 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 908 DebugLoc dl = MI.getDebugLoc(); 909 910 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 911 Register Reg = MF.getRegInfo().createVirtualRegister(GPRC); 912 Register DestReg = MI.getOperand(0).getReg(); 913 assert(MI.definesRegister(DestReg) && 914 "RESTORE_VRSAVE does not define its destination"); 915 916 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::LWZ), 917 Reg), FrameIndex); 918 919 BuildMI(MBB, II, dl, TII.get(PPC::MTVRSAVEv), DestReg) 920 .addReg(Reg, RegState::Kill); 921 922 // Discard the pseudo instruction. 923 MBB.erase(II); 924 } 925 926 bool PPCRegisterInfo::hasReservedSpillSlot(const MachineFunction &MF, 927 Register Reg, int &FrameIdx) const { 928 // For the nonvolatile condition registers (CR2, CR3, CR4) return true to 929 // prevent allocating an additional frame slot. 930 // For 64-bit ELF and AIX, the CR save area is in the linkage area at SP+8, 931 // for 32-bit AIX the CR save area is in the linkage area at SP+4. 932 // We have created a FrameIndex to that spill slot to keep the CalleSaveInfos 933 // valid. 934 // For 32-bit ELF, we have previously created the stack slot if needed, so 935 // return its FrameIdx. 936 if (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 Register 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::materializeFrameBaseRegister(MachineBasicBlock *MBB, 1230 Register BaseReg, 1231 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, Register 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 Register 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