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