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