1 //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the PPC implementation of TargetFrameLowering class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "PPCFrameLowering.h" 15 #include "PPCInstrBuilder.h" 16 #include "PPCInstrInfo.h" 17 #include "PPCMachineFunctionInfo.h" 18 #include "PPCSubtarget.h" 19 #include "PPCTargetMachine.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineFunction.h" 22 #include "llvm/CodeGen/MachineInstrBuilder.h" 23 #include "llvm/CodeGen/MachineModuleInfo.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/CodeGen/RegisterScavenging.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/Target/TargetOptions.h" 28 29 using namespace llvm; 30 31 /// VRRegNo - Map from a numbered VR register to its enum value. 32 /// 33 static const MCPhysReg VRRegNo[] = { 34 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , 35 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, 36 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, 37 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 38 }; 39 40 static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) { 41 if (STI.isDarwinABI()) 42 return STI.isPPC64() ? 16 : 8; 43 // SVR4 ABI: 44 return STI.isPPC64() ? 16 : 4; 45 } 46 47 static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) { 48 return STI.isELFv2ABI() ? 24 : 40; 49 } 50 51 static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) { 52 // For the Darwin ABI: 53 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area 54 // for saving the frame pointer (if needed.) While the published ABI has 55 // not used this slot since at least MacOSX 10.2, there is older code 56 // around that does use it, and that needs to continue to work. 57 if (STI.isDarwinABI()) 58 return STI.isPPC64() ? -8U : -4U; 59 60 // SVR4 ABI: First slot in the general register save area. 61 return STI.isPPC64() ? -8U : -4U; 62 } 63 64 static unsigned computeLinkageSize(const PPCSubtarget &STI) { 65 if (STI.isDarwinABI() || STI.isPPC64()) 66 return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4); 67 68 // SVR4 ABI: 69 return 8; 70 } 71 72 static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) { 73 if (STI.isDarwinABI()) 74 return STI.isPPC64() ? -16U : -8U; 75 76 // SVR4 ABI: First slot in the general register save area. 77 return STI.isPPC64() 78 ? -16U 79 : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U; 80 } 81 82 PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI) 83 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 84 STI.getPlatformStackAlignment(), 0), 85 Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)), 86 TOCSaveOffset(computeTOCSaveOffset(Subtarget)), 87 FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)), 88 LinkageSize(computeLinkageSize(Subtarget)), 89 BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {} 90 91 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack. 92 const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots( 93 unsigned &NumEntries) const { 94 if (Subtarget.isDarwinABI()) { 95 NumEntries = 1; 96 if (Subtarget.isPPC64()) { 97 static const SpillSlot darwin64Offsets = {PPC::X31, -8}; 98 return &darwin64Offsets; 99 } else { 100 static const SpillSlot darwinOffsets = {PPC::R31, -4}; 101 return &darwinOffsets; 102 } 103 } 104 105 // Early exit if not using the SVR4 ABI. 106 if (!Subtarget.isSVR4ABI()) { 107 NumEntries = 0; 108 return nullptr; 109 } 110 111 // Note that the offsets here overlap, but this is fixed up in 112 // processFunctionBeforeFrameFinalized. 113 114 static const SpillSlot Offsets[] = { 115 // Floating-point register save area offsets. 116 {PPC::F31, -8}, 117 {PPC::F30, -16}, 118 {PPC::F29, -24}, 119 {PPC::F28, -32}, 120 {PPC::F27, -40}, 121 {PPC::F26, -48}, 122 {PPC::F25, -56}, 123 {PPC::F24, -64}, 124 {PPC::F23, -72}, 125 {PPC::F22, -80}, 126 {PPC::F21, -88}, 127 {PPC::F20, -96}, 128 {PPC::F19, -104}, 129 {PPC::F18, -112}, 130 {PPC::F17, -120}, 131 {PPC::F16, -128}, 132 {PPC::F15, -136}, 133 {PPC::F14, -144}, 134 135 // General register save area offsets. 136 {PPC::R31, -4}, 137 {PPC::R30, -8}, 138 {PPC::R29, -12}, 139 {PPC::R28, -16}, 140 {PPC::R27, -20}, 141 {PPC::R26, -24}, 142 {PPC::R25, -28}, 143 {PPC::R24, -32}, 144 {PPC::R23, -36}, 145 {PPC::R22, -40}, 146 {PPC::R21, -44}, 147 {PPC::R20, -48}, 148 {PPC::R19, -52}, 149 {PPC::R18, -56}, 150 {PPC::R17, -60}, 151 {PPC::R16, -64}, 152 {PPC::R15, -68}, 153 {PPC::R14, -72}, 154 155 // CR save area offset. We map each of the nonvolatile CR fields 156 // to the slot for CR2, which is the first of the nonvolatile CR 157 // fields to be assigned, so that we only allocate one save slot. 158 // See PPCRegisterInfo::hasReservedSpillSlot() for more information. 159 {PPC::CR2, -4}, 160 161 // VRSAVE save area offset. 162 {PPC::VRSAVE, -4}, 163 164 // Vector register save area 165 {PPC::V31, -16}, 166 {PPC::V30, -32}, 167 {PPC::V29, -48}, 168 {PPC::V28, -64}, 169 {PPC::V27, -80}, 170 {PPC::V26, -96}, 171 {PPC::V25, -112}, 172 {PPC::V24, -128}, 173 {PPC::V23, -144}, 174 {PPC::V22, -160}, 175 {PPC::V21, -176}, 176 {PPC::V20, -192}}; 177 178 static const SpillSlot Offsets64[] = { 179 // Floating-point register save area offsets. 180 {PPC::F31, -8}, 181 {PPC::F30, -16}, 182 {PPC::F29, -24}, 183 {PPC::F28, -32}, 184 {PPC::F27, -40}, 185 {PPC::F26, -48}, 186 {PPC::F25, -56}, 187 {PPC::F24, -64}, 188 {PPC::F23, -72}, 189 {PPC::F22, -80}, 190 {PPC::F21, -88}, 191 {PPC::F20, -96}, 192 {PPC::F19, -104}, 193 {PPC::F18, -112}, 194 {PPC::F17, -120}, 195 {PPC::F16, -128}, 196 {PPC::F15, -136}, 197 {PPC::F14, -144}, 198 199 // General register save area offsets. 200 {PPC::X31, -8}, 201 {PPC::X30, -16}, 202 {PPC::X29, -24}, 203 {PPC::X28, -32}, 204 {PPC::X27, -40}, 205 {PPC::X26, -48}, 206 {PPC::X25, -56}, 207 {PPC::X24, -64}, 208 {PPC::X23, -72}, 209 {PPC::X22, -80}, 210 {PPC::X21, -88}, 211 {PPC::X20, -96}, 212 {PPC::X19, -104}, 213 {PPC::X18, -112}, 214 {PPC::X17, -120}, 215 {PPC::X16, -128}, 216 {PPC::X15, -136}, 217 {PPC::X14, -144}, 218 219 // VRSAVE save area offset. 220 {PPC::VRSAVE, -4}, 221 222 // Vector register save area 223 {PPC::V31, -16}, 224 {PPC::V30, -32}, 225 {PPC::V29, -48}, 226 {PPC::V28, -64}, 227 {PPC::V27, -80}, 228 {PPC::V26, -96}, 229 {PPC::V25, -112}, 230 {PPC::V24, -128}, 231 {PPC::V23, -144}, 232 {PPC::V22, -160}, 233 {PPC::V21, -176}, 234 {PPC::V20, -192}}; 235 236 if (Subtarget.isPPC64()) { 237 NumEntries = array_lengthof(Offsets64); 238 239 return Offsets64; 240 } else { 241 NumEntries = array_lengthof(Offsets); 242 243 return Offsets; 244 } 245 } 246 247 /// RemoveVRSaveCode - We have found that this function does not need any code 248 /// to manipulate the VRSAVE register, even though it uses vector registers. 249 /// This can happen when the only registers used are known to be live in or out 250 /// of the function. Remove all of the VRSAVE related code from the function. 251 /// FIXME: The removal of the code results in a compile failure at -O0 when the 252 /// function contains a function call, as the GPR containing original VRSAVE 253 /// contents is spilled and reloaded around the call. Without the prolog code, 254 /// the spill instruction refers to an undefined register. This code needs 255 /// to account for all uses of that GPR. 256 static void RemoveVRSaveCode(MachineInstr &MI) { 257 MachineBasicBlock *Entry = MI.getParent(); 258 MachineFunction *MF = Entry->getParent(); 259 260 // We know that the MTVRSAVE instruction immediately follows MI. Remove it. 261 MachineBasicBlock::iterator MBBI = MI; 262 ++MBBI; 263 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); 264 MBBI->eraseFromParent(); 265 266 bool RemovedAllMTVRSAVEs = true; 267 // See if we can find and remove the MTVRSAVE instruction from all of the 268 // epilog blocks. 269 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { 270 // If last instruction is a return instruction, add an epilogue 271 if (I->isReturnBlock()) { 272 bool FoundIt = false; 273 for (MBBI = I->end(); MBBI != I->begin(); ) { 274 --MBBI; 275 if (MBBI->getOpcode() == PPC::MTVRSAVE) { 276 MBBI->eraseFromParent(); // remove it. 277 FoundIt = true; 278 break; 279 } 280 } 281 RemovedAllMTVRSAVEs &= FoundIt; 282 } 283 } 284 285 // If we found and removed all MTVRSAVE instructions, remove the read of 286 // VRSAVE as well. 287 if (RemovedAllMTVRSAVEs) { 288 MBBI = MI; 289 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); 290 --MBBI; 291 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); 292 MBBI->eraseFromParent(); 293 } 294 295 // Finally, nuke the UPDATE_VRSAVE. 296 MI.eraseFromParent(); 297 } 298 299 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the 300 // instruction selector. Based on the vector registers that have been used, 301 // transform this into the appropriate ORI instruction. 302 static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) { 303 MachineFunction *MF = MI.getParent()->getParent(); 304 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 305 DebugLoc dl = MI.getDebugLoc(); 306 307 const MachineRegisterInfo &MRI = MF->getRegInfo(); 308 unsigned UsedRegMask = 0; 309 for (unsigned i = 0; i != 32; ++i) 310 if (MRI.isPhysRegModified(VRRegNo[i])) 311 UsedRegMask |= 1 << (31-i); 312 313 // Live in and live out values already must be in the mask, so don't bother 314 // marking them. 315 for (MachineRegisterInfo::livein_iterator 316 I = MF->getRegInfo().livein_begin(), 317 E = MF->getRegInfo().livein_end(); I != E; ++I) { 318 unsigned RegNo = TRI->getEncodingValue(I->first); 319 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. 320 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. 321 } 322 323 // Live out registers appear as use operands on return instructions. 324 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end(); 325 UsedRegMask != 0 && BI != BE; ++BI) { 326 const MachineBasicBlock &MBB = *BI; 327 if (!MBB.isReturnBlock()) 328 continue; 329 const MachineInstr &Ret = MBB.back(); 330 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) { 331 const MachineOperand &MO = Ret.getOperand(I); 332 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg())) 333 continue; 334 unsigned RegNo = TRI->getEncodingValue(MO.getReg()); 335 UsedRegMask &= ~(1 << (31-RegNo)); 336 } 337 } 338 339 // If no registers are used, turn this into a copy. 340 if (UsedRegMask == 0) { 341 // Remove all VRSAVE code. 342 RemoveVRSaveCode(MI); 343 return; 344 } 345 346 unsigned SrcReg = MI.getOperand(1).getReg(); 347 unsigned DstReg = MI.getOperand(0).getReg(); 348 349 if ((UsedRegMask & 0xFFFF) == UsedRegMask) { 350 if (DstReg != SrcReg) 351 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 352 .addReg(SrcReg) 353 .addImm(UsedRegMask); 354 else 355 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 356 .addReg(SrcReg, RegState::Kill) 357 .addImm(UsedRegMask); 358 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { 359 if (DstReg != SrcReg) 360 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 361 .addReg(SrcReg) 362 .addImm(UsedRegMask >> 16); 363 else 364 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 365 .addReg(SrcReg, RegState::Kill) 366 .addImm(UsedRegMask >> 16); 367 } else { 368 if (DstReg != SrcReg) 369 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 370 .addReg(SrcReg) 371 .addImm(UsedRegMask >> 16); 372 else 373 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 374 .addReg(SrcReg, RegState::Kill) 375 .addImm(UsedRegMask >> 16); 376 377 BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 378 .addReg(DstReg, RegState::Kill) 379 .addImm(UsedRegMask & 0xFFFF); 380 } 381 382 // Remove the old UPDATE_VRSAVE instruction. 383 MI.eraseFromParent(); 384 } 385 386 static bool spillsCR(const MachineFunction &MF) { 387 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 388 return FuncInfo->isCRSpilled(); 389 } 390 391 static bool spillsVRSAVE(const MachineFunction &MF) { 392 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 393 return FuncInfo->isVRSAVESpilled(); 394 } 395 396 static bool hasSpills(const MachineFunction &MF) { 397 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 398 return FuncInfo->hasSpills(); 399 } 400 401 static bool hasNonRISpills(const MachineFunction &MF) { 402 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 403 return FuncInfo->hasNonRISpills(); 404 } 405 406 /// MustSaveLR - Return true if this function requires that we save the LR 407 /// register onto the stack in the prolog and restore it in the epilog of the 408 /// function. 409 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) { 410 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); 411 412 // We need a save/restore of LR if there is any def of LR (which is 413 // defined by calls, including the PIC setup sequence), or if there is 414 // some use of the LR stack slot (e.g. for builtin_return_address). 415 // (LR comes in 32 and 64 bit versions.) 416 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); 417 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); 418 } 419 420 /// determineFrameLayout - Determine the size of the frame and maximum call 421 /// frame size. 422 unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF, 423 bool UpdateMF, 424 bool UseEstimate) const { 425 MachineFrameInfo &MFI = MF.getFrameInfo(); 426 427 // Get the number of bytes to allocate from the FrameInfo 428 unsigned FrameSize = 429 UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize(); 430 431 // Get stack alignments. The frame must be aligned to the greatest of these: 432 unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI 433 unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame 434 unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1; 435 436 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 437 438 // If we are a leaf function, and use up to 224 bytes of stack space, 439 // don't have a frame pointer, calls, or dynamic alloca then we do not need 440 // to adjust the stack pointer (we fit in the Red Zone). 441 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate 442 // stackless code if all local vars are reg-allocated. 443 bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone); 444 unsigned LR = RegInfo->getRARegister(); 445 if (!DisableRedZone && 446 (Subtarget.isPPC64() || // 32-bit SVR4, no stack- 447 !Subtarget.isSVR4ABI() || // allocated locals. 448 FrameSize == 0) && 449 FrameSize <= 224 && // Fits in red zone. 450 !MFI.hasVarSizedObjects() && // No dynamic alloca. 451 !MFI.adjustsStack() && // No calls. 452 !MustSaveLR(MF, LR) && 453 !RegInfo->hasBasePointer(MF)) { // No special alignment. 454 // No need for frame 455 if (UpdateMF) 456 MFI.setStackSize(0); 457 return 0; 458 } 459 460 // Get the maximum call frame size of all the calls. 461 unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); 462 463 // Maximum call frame needs to be at least big enough for linkage area. 464 unsigned minCallFrameSize = getLinkageSize(); 465 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); 466 467 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so 468 // that allocations will be aligned. 469 if (MFI.hasVarSizedObjects()) 470 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; 471 472 // Update maximum call frame size. 473 if (UpdateMF) 474 MFI.setMaxCallFrameSize(maxCallFrameSize); 475 476 // Include call frame size in total. 477 FrameSize += maxCallFrameSize; 478 479 // Make sure the frame is aligned. 480 FrameSize = (FrameSize + AlignMask) & ~AlignMask; 481 482 // Update frame info. 483 if (UpdateMF) 484 MFI.setStackSize(FrameSize); 485 486 return FrameSize; 487 } 488 489 // hasFP - Return true if the specified function actually has a dedicated frame 490 // pointer register. 491 bool PPCFrameLowering::hasFP(const MachineFunction &MF) const { 492 const MachineFrameInfo &MFI = MF.getFrameInfo(); 493 // FIXME: This is pretty much broken by design: hasFP() might be called really 494 // early, before the stack layout was calculated and thus hasFP() might return 495 // true or false here depending on the time of call. 496 return (MFI.getStackSize()) && needsFP(MF); 497 } 498 499 // needsFP - Return true if the specified function should have a dedicated frame 500 // pointer register. This is true if the function has variable sized allocas or 501 // if frame pointer elimination is disabled. 502 bool PPCFrameLowering::needsFP(const MachineFunction &MF) const { 503 const MachineFrameInfo &MFI = MF.getFrameInfo(); 504 505 // Naked functions have no stack frame pushed, so we don't have a frame 506 // pointer. 507 if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) 508 return false; 509 510 return MF.getTarget().Options.DisableFramePointerElim(MF) || 511 MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() || 512 (MF.getTarget().Options.GuaranteedTailCallOpt && 513 MF.getInfo<PPCFunctionInfo>()->hasFastCall()); 514 } 515 516 void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const { 517 bool is31 = needsFP(MF); 518 unsigned FPReg = is31 ? PPC::R31 : PPC::R1; 519 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1; 520 521 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 522 bool HasBP = RegInfo->hasBasePointer(MF); 523 unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg; 524 unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg; 525 526 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); 527 BI != BE; ++BI) 528 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) { 529 --MBBI; 530 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) { 531 MachineOperand &MO = MBBI->getOperand(I); 532 if (!MO.isReg()) 533 continue; 534 535 switch (MO.getReg()) { 536 case PPC::FP: 537 MO.setReg(FPReg); 538 break; 539 case PPC::FP8: 540 MO.setReg(FP8Reg); 541 break; 542 case PPC::BP: 543 MO.setReg(BPReg); 544 break; 545 case PPC::BP8: 546 MO.setReg(BP8Reg); 547 break; 548 549 } 550 } 551 } 552 } 553 554 /* This function will do the following: 555 - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12 556 respectively (defaults recommended by the ABI) and return true 557 - If MBB is not an entry block, initialize the register scavenger and look 558 for available registers. 559 - If the defaults (R0/R12) are available, return true 560 - If TwoUniqueRegsRequired is set to true, it looks for two unique 561 registers. Otherwise, look for a single available register. 562 - If the required registers are found, set SR1 and SR2 and return true. 563 - If the required registers are not found, set SR2 or both SR1 and SR2 to 564 PPC::NoRegister and return false. 565 566 Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired 567 is not set, this function will attempt to find two different registers, but 568 still return true if only one register is available (and set SR1 == SR2). 569 */ 570 bool 571 PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB, 572 bool UseAtEnd, 573 bool TwoUniqueRegsRequired, 574 unsigned *SR1, 575 unsigned *SR2) const { 576 RegScavenger RS; 577 unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0; 578 unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12; 579 580 // Set the defaults for the two scratch registers. 581 if (SR1) 582 *SR1 = R0; 583 584 if (SR2) { 585 assert (SR1 && "Asking for the second scratch register but not the first?"); 586 *SR2 = R12; 587 } 588 589 // If MBB is an entry or exit block, use R0 and R12 as the scratch registers. 590 if ((UseAtEnd && MBB->isReturnBlock()) || 591 (!UseAtEnd && (&MBB->getParent()->front() == MBB))) 592 return true; 593 594 RS.enterBasicBlock(*MBB); 595 596 if (UseAtEnd && !MBB->empty()) { 597 // The scratch register will be used at the end of the block, so must 598 // consider all registers used within the block 599 600 MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator(); 601 // If no terminator, back iterator up to previous instruction. 602 if (MBBI == MBB->end()) 603 MBBI = std::prev(MBBI); 604 605 if (MBBI != MBB->begin()) 606 RS.forward(MBBI); 607 } 608 609 // If the two registers are available, we're all good. 610 // Note that we only return here if both R0 and R12 are available because 611 // although the function may not require two unique registers, it may benefit 612 // from having two so we should try to provide them. 613 if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12)) 614 return true; 615 616 // Get the list of callee-saved registers for the target. 617 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 618 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent()); 619 620 // Get all the available registers in the block. 621 BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass : 622 &PPC::GPRCRegClass); 623 624 // We shouldn't use callee-saved registers as scratch registers as they may be 625 // available when looking for a candidate block for shrink wrapping but not 626 // available when the actual prologue/epilogue is being emitted because they 627 // were added as live-in to the prologue block by PrologueEpilogueInserter. 628 for (int i = 0; CSRegs[i]; ++i) 629 BV.reset(CSRegs[i]); 630 631 // Set the first scratch register to the first available one. 632 if (SR1) { 633 int FirstScratchReg = BV.find_first(); 634 *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg; 635 } 636 637 // If there is another one available, set the second scratch register to that. 638 // Otherwise, set it to either PPC::NoRegister if this function requires two 639 // or to whatever SR1 is set to if this function doesn't require two. 640 if (SR2) { 641 int SecondScratchReg = BV.find_next(*SR1); 642 if (SecondScratchReg != -1) 643 *SR2 = SecondScratchReg; 644 else 645 *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1; 646 } 647 648 // Now that we've done our best to provide both registers, double check 649 // whether we were unable to provide enough. 650 if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U)) 651 return false; 652 653 return true; 654 } 655 656 // We need a scratch register for spilling LR and for spilling CR. By default, 657 // we use two scratch registers to hide latency. However, if only one scratch 658 // register is available, we can adjust for that by not overlapping the spill 659 // code. However, if we need to realign the stack (i.e. have a base pointer) 660 // and the stack frame is large, we need two scratch registers. 661 bool 662 PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const { 663 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 664 MachineFunction &MF = *(MBB->getParent()); 665 bool HasBP = RegInfo->hasBasePointer(MF); 666 unsigned FrameSize = determineFrameLayout(MF, false); 667 int NegFrameSize = -FrameSize; 668 bool IsLargeFrame = !isInt<16>(NegFrameSize); 669 MachineFrameInfo &MFI = MF.getFrameInfo(); 670 unsigned MaxAlign = MFI.getMaxAlignment(); 671 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI(); 672 673 return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1; 674 } 675 676 bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const { 677 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); 678 679 return findScratchRegister(TmpMBB, false, 680 twoUniqueScratchRegsRequired(TmpMBB)); 681 } 682 683 bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const { 684 MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); 685 686 return findScratchRegister(TmpMBB, true); 687 } 688 689 void PPCFrameLowering::emitPrologue(MachineFunction &MF, 690 MachineBasicBlock &MBB) const { 691 MachineBasicBlock::iterator MBBI = MBB.begin(); 692 MachineFrameInfo &MFI = MF.getFrameInfo(); 693 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 694 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 695 696 MachineModuleInfo &MMI = MF.getMMI(); 697 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 698 DebugLoc dl; 699 bool needsCFI = MMI.hasDebugInfo() || 700 MF.getFunction()->needsUnwindTableEntry(); 701 702 // Get processor type. 703 bool isPPC64 = Subtarget.isPPC64(); 704 // Get the ABI. 705 bool isSVR4ABI = Subtarget.isSVR4ABI(); 706 bool isELFv2ABI = Subtarget.isELFv2ABI(); 707 assert((Subtarget.isDarwinABI() || isSVR4ABI) && 708 "Currently only Darwin and SVR4 ABIs are supported for PowerPC."); 709 710 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, 711 // process it. 712 if (!isSVR4ABI) 713 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { 714 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { 715 HandleVRSaveUpdate(*MBBI, TII); 716 break; 717 } 718 } 719 720 // Move MBBI back to the beginning of the prologue block. 721 MBBI = MBB.begin(); 722 723 // Work out frame sizes. 724 unsigned FrameSize = determineFrameLayout(MF); 725 int NegFrameSize = -FrameSize; 726 if (!isInt<32>(NegFrameSize)) 727 llvm_unreachable("Unhandled stack size!"); 728 729 if (MFI.isFrameAddressTaken()) 730 replaceFPWithRealFP(MF); 731 732 // Check if the link register (LR) must be saved. 733 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 734 bool MustSaveLR = FI->mustSaveLR(); 735 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs(); 736 bool MustSaveCR = !MustSaveCRs.empty(); 737 // Do we have a frame pointer and/or base pointer for this function? 738 bool HasFP = hasFP(MF); 739 bool HasBP = RegInfo->hasBasePointer(MF); 740 bool HasRedZone = isPPC64 || !isSVR4ABI; 741 742 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1; 743 unsigned BPReg = RegInfo->getBaseRegister(MF); 744 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; 745 unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR; 746 unsigned ScratchReg = 0; 747 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg 748 // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.) 749 const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8 750 : PPC::MFLR ); 751 const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD 752 : PPC::STW ); 753 const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU 754 : PPC::STWU ); 755 const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX 756 : PPC::STWUX); 757 const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8 758 : PPC::LIS ); 759 const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8 760 : PPC::ORI ); 761 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8 762 : PPC::OR ); 763 const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8 764 : PPC::SUBFC); 765 const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8 766 : PPC::SUBFIC); 767 768 // Regarding this assert: Even though LR is saved in the caller's frame (i.e., 769 // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no 770 // Red Zone, an asynchronous event (a form of "callee") could claim a frame & 771 // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR. 772 assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) && 773 "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4."); 774 775 // Using the same bool variable as below to suppress compiler warnings. 776 bool SingleScratchReg = 777 findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB), 778 &ScratchReg, &TempReg); 779 assert(SingleScratchReg && 780 "Required number of registers not available in this block"); 781 782 SingleScratchReg = ScratchReg == TempReg; 783 784 int LROffset = getReturnSaveOffset(); 785 786 int FPOffset = 0; 787 if (HasFP) { 788 if (isSVR4ABI) { 789 MachineFrameInfo &MFI = MF.getFrameInfo(); 790 int FPIndex = FI->getFramePointerSaveIndex(); 791 assert(FPIndex && "No Frame Pointer Save Slot!"); 792 FPOffset = MFI.getObjectOffset(FPIndex); 793 } else { 794 FPOffset = getFramePointerSaveOffset(); 795 } 796 } 797 798 int BPOffset = 0; 799 if (HasBP) { 800 if (isSVR4ABI) { 801 MachineFrameInfo &MFI = MF.getFrameInfo(); 802 int BPIndex = FI->getBasePointerSaveIndex(); 803 assert(BPIndex && "No Base Pointer Save Slot!"); 804 BPOffset = MFI.getObjectOffset(BPIndex); 805 } else { 806 BPOffset = getBasePointerSaveOffset(); 807 } 808 } 809 810 int PBPOffset = 0; 811 if (FI->usesPICBase()) { 812 MachineFrameInfo &MFI = MF.getFrameInfo(); 813 int PBPIndex = FI->getPICBasePointerSaveIndex(); 814 assert(PBPIndex && "No PIC Base Pointer Save Slot!"); 815 PBPOffset = MFI.getObjectOffset(PBPIndex); 816 } 817 818 // Get stack alignments. 819 unsigned MaxAlign = MFI.getMaxAlignment(); 820 if (HasBP && MaxAlign > 1) 821 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 822 "Invalid alignment!"); 823 824 // Frames of 32KB & larger require special handling because they cannot be 825 // indexed into with a simple STDU/STWU/STD/STW immediate offset operand. 826 bool isLargeFrame = !isInt<16>(NegFrameSize); 827 828 assert((isPPC64 || !MustSaveCR) && 829 "Prologue CR saving supported only in 64-bit mode"); 830 831 // If we need to spill the CR and the LR but we don't have two separate 832 // registers available, we must spill them one at a time 833 if (MustSaveCR && SingleScratchReg && MustSaveLR) { 834 // In the ELFv2 ABI, we are not required to save all CR fields. 835 // If only one or two CR fields are clobbered, it is more efficient to use 836 // mfocrf to selectively save just those fields, because mfocrf has short 837 // latency compares to mfcr. 838 unsigned MfcrOpcode = PPC::MFCR8; 839 unsigned CrState = RegState::ImplicitKill; 840 if (isELFv2ABI && MustSaveCRs.size() == 1) { 841 MfcrOpcode = PPC::MFOCRF8; 842 CrState = RegState::Kill; 843 } 844 MachineInstrBuilder MIB = 845 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg); 846 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 847 MIB.addReg(MustSaveCRs[i], CrState); 848 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8)) 849 .addReg(TempReg, getKillRegState(true)) 850 .addImm(8) 851 .addReg(SPReg); 852 } 853 854 if (MustSaveLR) 855 BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg); 856 857 if (MustSaveCR && 858 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64 859 // In the ELFv2 ABI, we are not required to save all CR fields. 860 // If only one or two CR fields are clobbered, it is more efficient to use 861 // mfocrf to selectively save just those fields, because mfocrf has short 862 // latency compares to mfcr. 863 unsigned MfcrOpcode = PPC::MFCR8; 864 unsigned CrState = RegState::ImplicitKill; 865 if (isELFv2ABI && MustSaveCRs.size() == 1) { 866 MfcrOpcode = PPC::MFOCRF8; 867 CrState = RegState::Kill; 868 } 869 MachineInstrBuilder MIB = 870 BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg); 871 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 872 MIB.addReg(MustSaveCRs[i], CrState); 873 } 874 875 if (HasRedZone) { 876 if (HasFP) 877 BuildMI(MBB, MBBI, dl, StoreInst) 878 .addReg(FPReg) 879 .addImm(FPOffset) 880 .addReg(SPReg); 881 if (FI->usesPICBase()) 882 BuildMI(MBB, MBBI, dl, StoreInst) 883 .addReg(PPC::R30) 884 .addImm(PBPOffset) 885 .addReg(SPReg); 886 if (HasBP) 887 BuildMI(MBB, MBBI, dl, StoreInst) 888 .addReg(BPReg) 889 .addImm(BPOffset) 890 .addReg(SPReg); 891 } 892 893 if (MustSaveLR) 894 BuildMI(MBB, MBBI, dl, StoreInst) 895 .addReg(ScratchReg, getKillRegState(true)) 896 .addImm(LROffset) 897 .addReg(SPReg); 898 899 if (MustSaveCR && 900 !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64 901 assert(HasRedZone && "A red zone is always available on PPC64"); 902 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8)) 903 .addReg(TempReg, getKillRegState(true)) 904 .addImm(8) 905 .addReg(SPReg); 906 } 907 908 // Skip the rest if this is a leaf function & all spills fit in the Red Zone. 909 if (!FrameSize) 910 return; 911 912 // Adjust stack pointer: r1 += NegFrameSize. 913 // If there is a preferred stack alignment, align R1 now 914 915 if (HasBP && HasRedZone) { 916 // Save a copy of r1 as the base pointer. 917 BuildMI(MBB, MBBI, dl, OrInst, BPReg) 918 .addReg(SPReg) 919 .addReg(SPReg); 920 } 921 922 // Have we generated a STUX instruction to claim stack frame? If so, 923 // the negated frame size will be placed in ScratchReg. 924 bool HasSTUX = false; 925 926 // This condition must be kept in sync with canUseAsPrologue. 927 if (HasBP && MaxAlign > 1) { 928 if (isPPC64) 929 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg) 930 .addReg(SPReg) 931 .addImm(0) 932 .addImm(64 - Log2_32(MaxAlign)); 933 else // PPC32... 934 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg) 935 .addReg(SPReg) 936 .addImm(0) 937 .addImm(32 - Log2_32(MaxAlign)) 938 .addImm(31); 939 if (!isLargeFrame) { 940 BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg) 941 .addReg(ScratchReg, RegState::Kill) 942 .addImm(NegFrameSize); 943 } else { 944 assert(!SingleScratchReg && "Only a single scratch reg available"); 945 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg) 946 .addImm(NegFrameSize >> 16); 947 BuildMI(MBB, MBBI, dl, OrImmInst, TempReg) 948 .addReg(TempReg, RegState::Kill) 949 .addImm(NegFrameSize & 0xFFFF); 950 BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg) 951 .addReg(ScratchReg, RegState::Kill) 952 .addReg(TempReg, RegState::Kill); 953 } 954 955 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg) 956 .addReg(SPReg, RegState::Kill) 957 .addReg(SPReg) 958 .addReg(ScratchReg); 959 HasSTUX = true; 960 961 } else if (!isLargeFrame) { 962 BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg) 963 .addReg(SPReg) 964 .addImm(NegFrameSize) 965 .addReg(SPReg); 966 967 } else { 968 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) 969 .addImm(NegFrameSize >> 16); 970 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) 971 .addReg(ScratchReg, RegState::Kill) 972 .addImm(NegFrameSize & 0xFFFF); 973 BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg) 974 .addReg(SPReg, RegState::Kill) 975 .addReg(SPReg) 976 .addReg(ScratchReg); 977 HasSTUX = true; 978 } 979 980 if (!HasRedZone) { 981 assert(!isPPC64 && "A red zone is always available on PPC64"); 982 if (HasSTUX) { 983 // The negated frame size is in ScratchReg, and the SPReg has been 984 // decremented by the frame size: SPReg = old SPReg + ScratchReg. 985 // Since FPOffset, PBPOffset, etc. are relative to the beginning of 986 // the stack frame (i.e. the old SP), ideally, we would put the old 987 // SP into a register and use it as the base for the stores. The 988 // problem is that the only available register may be ScratchReg, 989 // which could be R0, and R0 cannot be used as a base address. 990 991 // First, set ScratchReg to the old SP. This may need to be modified 992 // later. 993 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg) 994 .addReg(ScratchReg, RegState::Kill) 995 .addReg(SPReg); 996 997 if (ScratchReg == PPC::R0) { 998 // R0 cannot be used as a base register, but it can be used as an 999 // index in a store-indexed. 1000 int LastOffset = 0; 1001 if (HasFP) { 1002 // R0 += (FPOffset-LastOffset). 1003 // Need addic, since addi treats R0 as 0. 1004 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) 1005 .addReg(ScratchReg) 1006 .addImm(FPOffset-LastOffset); 1007 LastOffset = FPOffset; 1008 // Store FP into *R0. 1009 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) 1010 .addReg(FPReg, RegState::Kill) // Save FP. 1011 .addReg(PPC::ZERO) 1012 .addReg(ScratchReg); // This will be the index (R0 is ok here). 1013 } 1014 if (FI->usesPICBase()) { 1015 // R0 += (PBPOffset-LastOffset). 1016 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) 1017 .addReg(ScratchReg) 1018 .addImm(PBPOffset-LastOffset); 1019 LastOffset = PBPOffset; 1020 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) 1021 .addReg(PPC::R30, RegState::Kill) // Save PIC base pointer. 1022 .addReg(PPC::ZERO) 1023 .addReg(ScratchReg); // This will be the index (R0 is ok here). 1024 } 1025 if (HasBP) { 1026 // R0 += (BPOffset-LastOffset). 1027 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) 1028 .addReg(ScratchReg) 1029 .addImm(BPOffset-LastOffset); 1030 LastOffset = BPOffset; 1031 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) 1032 .addReg(BPReg, RegState::Kill) // Save BP. 1033 .addReg(PPC::ZERO) 1034 .addReg(ScratchReg); // This will be the index (R0 is ok here). 1035 // BP = R0-LastOffset 1036 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg) 1037 .addReg(ScratchReg, RegState::Kill) 1038 .addImm(-LastOffset); 1039 } 1040 } else { 1041 // ScratchReg is not R0, so use it as the base register. It is 1042 // already set to the old SP, so we can use the offsets directly. 1043 1044 // Now that the stack frame has been allocated, save all the necessary 1045 // registers using ScratchReg as the base address. 1046 if (HasFP) 1047 BuildMI(MBB, MBBI, dl, StoreInst) 1048 .addReg(FPReg) 1049 .addImm(FPOffset) 1050 .addReg(ScratchReg); 1051 if (FI->usesPICBase()) 1052 BuildMI(MBB, MBBI, dl, StoreInst) 1053 .addReg(PPC::R30) 1054 .addImm(PBPOffset) 1055 .addReg(ScratchReg); 1056 if (HasBP) { 1057 BuildMI(MBB, MBBI, dl, StoreInst) 1058 .addReg(BPReg) 1059 .addImm(BPOffset) 1060 .addReg(ScratchReg); 1061 BuildMI(MBB, MBBI, dl, OrInst, BPReg) 1062 .addReg(ScratchReg, RegState::Kill) 1063 .addReg(ScratchReg); 1064 } 1065 } 1066 } else { 1067 // The frame size is a known 16-bit constant (fitting in the immediate 1068 // field of STWU). To be here we have to be compiling for PPC32. 1069 // Since the SPReg has been decreased by FrameSize, add it back to each 1070 // offset. 1071 if (HasFP) 1072 BuildMI(MBB, MBBI, dl, StoreInst) 1073 .addReg(FPReg) 1074 .addImm(FrameSize + FPOffset) 1075 .addReg(SPReg); 1076 if (FI->usesPICBase()) 1077 BuildMI(MBB, MBBI, dl, StoreInst) 1078 .addReg(PPC::R30) 1079 .addImm(FrameSize + PBPOffset) 1080 .addReg(SPReg); 1081 if (HasBP) { 1082 BuildMI(MBB, MBBI, dl, StoreInst) 1083 .addReg(BPReg) 1084 .addImm(FrameSize + BPOffset) 1085 .addReg(SPReg); 1086 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg) 1087 .addReg(SPReg) 1088 .addImm(FrameSize); 1089 } 1090 } 1091 } 1092 1093 // Add Call Frame Information for the instructions we generated above. 1094 if (needsCFI) { 1095 unsigned CFIIndex; 1096 1097 if (HasBP) { 1098 // Define CFA in terms of BP. Do this in preference to using FP/SP, 1099 // because if the stack needed aligning then CFA won't be at a fixed 1100 // offset from FP/SP. 1101 unsigned Reg = MRI->getDwarfRegNum(BPReg, true); 1102 CFIIndex = MF.addFrameInst( 1103 MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); 1104 } else { 1105 // Adjust the definition of CFA to account for the change in SP. 1106 assert(NegFrameSize); 1107 CFIIndex = MF.addFrameInst( 1108 MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize)); 1109 } 1110 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1111 .addCFIIndex(CFIIndex); 1112 1113 if (HasFP) { 1114 // Describe where FP was saved, at a fixed offset from CFA. 1115 unsigned Reg = MRI->getDwarfRegNum(FPReg, true); 1116 CFIIndex = MF.addFrameInst( 1117 MCCFIInstruction::createOffset(nullptr, Reg, FPOffset)); 1118 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1119 .addCFIIndex(CFIIndex); 1120 } 1121 1122 if (FI->usesPICBase()) { 1123 // Describe where FP was saved, at a fixed offset from CFA. 1124 unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true); 1125 CFIIndex = MF.addFrameInst( 1126 MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset)); 1127 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1128 .addCFIIndex(CFIIndex); 1129 } 1130 1131 if (HasBP) { 1132 // Describe where BP was saved, at a fixed offset from CFA. 1133 unsigned Reg = MRI->getDwarfRegNum(BPReg, true); 1134 CFIIndex = MF.addFrameInst( 1135 MCCFIInstruction::createOffset(nullptr, Reg, BPOffset)); 1136 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1137 .addCFIIndex(CFIIndex); 1138 } 1139 1140 if (MustSaveLR) { 1141 // Describe where LR was saved, at a fixed offset from CFA. 1142 unsigned Reg = MRI->getDwarfRegNum(LRReg, true); 1143 CFIIndex = MF.addFrameInst( 1144 MCCFIInstruction::createOffset(nullptr, Reg, LROffset)); 1145 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1146 .addCFIIndex(CFIIndex); 1147 } 1148 } 1149 1150 // If there is a frame pointer, copy R1 into R31 1151 if (HasFP) { 1152 BuildMI(MBB, MBBI, dl, OrInst, FPReg) 1153 .addReg(SPReg) 1154 .addReg(SPReg); 1155 1156 if (!HasBP && needsCFI) { 1157 // Change the definition of CFA from SP+offset to FP+offset, because SP 1158 // will change at every alloca. 1159 unsigned Reg = MRI->getDwarfRegNum(FPReg, true); 1160 unsigned CFIIndex = MF.addFrameInst( 1161 MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); 1162 1163 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1164 .addCFIIndex(CFIIndex); 1165 } 1166 } 1167 1168 if (needsCFI) { 1169 // Describe where callee saved registers were saved, at fixed offsets from 1170 // CFA. 1171 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 1172 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 1173 unsigned Reg = CSI[I].getReg(); 1174 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; 1175 1176 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just 1177 // subregisters of CR2. We just need to emit a move of CR2. 1178 if (PPC::CRBITRCRegClass.contains(Reg)) 1179 continue; 1180 1181 // For SVR4, don't emit a move for the CR spill slot if we haven't 1182 // spilled CRs. 1183 if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4) 1184 && !MustSaveCR) 1185 continue; 1186 1187 // For 64-bit SVR4 when we have spilled CRs, the spill location 1188 // is SP+8, not a frame-relative slot. 1189 if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 1190 // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for 1191 // the whole CR word. In the ELFv2 ABI, every CR that was 1192 // actually saved gets its own CFI record. 1193 unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2; 1194 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 1195 nullptr, MRI->getDwarfRegNum(CRReg, true), 8)); 1196 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1197 .addCFIIndex(CFIIndex); 1198 continue; 1199 } 1200 1201 int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx()); 1202 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( 1203 nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); 1204 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 1205 .addCFIIndex(CFIIndex); 1206 } 1207 } 1208 } 1209 1210 void PPCFrameLowering::emitEpilogue(MachineFunction &MF, 1211 MachineBasicBlock &MBB) const { 1212 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); 1213 DebugLoc dl; 1214 1215 if (MBBI != MBB.end()) 1216 dl = MBBI->getDebugLoc(); 1217 1218 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 1219 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 1220 1221 // Get alignment info so we know how to restore the SP. 1222 const MachineFrameInfo &MFI = MF.getFrameInfo(); 1223 1224 // Get the number of bytes allocated from the FrameInfo. 1225 int FrameSize = MFI.getStackSize(); 1226 1227 // Get processor type. 1228 bool isPPC64 = Subtarget.isPPC64(); 1229 // Get the ABI. 1230 bool isSVR4ABI = Subtarget.isSVR4ABI(); 1231 1232 // Check if the link register (LR) has been saved. 1233 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 1234 bool MustSaveLR = FI->mustSaveLR(); 1235 const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs(); 1236 bool MustSaveCR = !MustSaveCRs.empty(); 1237 // Do we have a frame pointer and/or base pointer for this function? 1238 bool HasFP = hasFP(MF); 1239 bool HasBP = RegInfo->hasBasePointer(MF); 1240 bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI(); 1241 1242 unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1; 1243 unsigned BPReg = RegInfo->getBaseRegister(MF); 1244 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; 1245 unsigned ScratchReg = 0; 1246 unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg 1247 const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8 1248 : PPC::MTLR ); 1249 const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD 1250 : PPC::LWZ ); 1251 const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8 1252 : PPC::LIS ); 1253 const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8 1254 : PPC::OR ); 1255 const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8 1256 : PPC::ORI ); 1257 const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8 1258 : PPC::ADDI ); 1259 const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8 1260 : PPC::ADD4 ); 1261 1262 int LROffset = getReturnSaveOffset(); 1263 1264 int FPOffset = 0; 1265 1266 // Using the same bool variable as below to suppress compiler warnings. 1267 bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg, 1268 &TempReg); 1269 assert(SingleScratchReg && 1270 "Could not find an available scratch register"); 1271 1272 SingleScratchReg = ScratchReg == TempReg; 1273 1274 if (HasFP) { 1275 if (isSVR4ABI) { 1276 int FPIndex = FI->getFramePointerSaveIndex(); 1277 assert(FPIndex && "No Frame Pointer Save Slot!"); 1278 FPOffset = MFI.getObjectOffset(FPIndex); 1279 } else { 1280 FPOffset = getFramePointerSaveOffset(); 1281 } 1282 } 1283 1284 int BPOffset = 0; 1285 if (HasBP) { 1286 if (isSVR4ABI) { 1287 int BPIndex = FI->getBasePointerSaveIndex(); 1288 assert(BPIndex && "No Base Pointer Save Slot!"); 1289 BPOffset = MFI.getObjectOffset(BPIndex); 1290 } else { 1291 BPOffset = getBasePointerSaveOffset(); 1292 } 1293 } 1294 1295 int PBPOffset = 0; 1296 if (FI->usesPICBase()) { 1297 int PBPIndex = FI->getPICBasePointerSaveIndex(); 1298 assert(PBPIndex && "No PIC Base Pointer Save Slot!"); 1299 PBPOffset = MFI.getObjectOffset(PBPIndex); 1300 } 1301 1302 bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn()); 1303 1304 if (IsReturnBlock) { 1305 unsigned RetOpcode = MBBI->getOpcode(); 1306 bool UsesTCRet = RetOpcode == PPC::TCRETURNri || 1307 RetOpcode == PPC::TCRETURNdi || 1308 RetOpcode == PPC::TCRETURNai || 1309 RetOpcode == PPC::TCRETURNri8 || 1310 RetOpcode == PPC::TCRETURNdi8 || 1311 RetOpcode == PPC::TCRETURNai8; 1312 1313 if (UsesTCRet) { 1314 int MaxTCRetDelta = FI->getTailCallSPDelta(); 1315 MachineOperand &StackAdjust = MBBI->getOperand(1); 1316 assert(StackAdjust.isImm() && "Expecting immediate value."); 1317 // Adjust stack pointer. 1318 int StackAdj = StackAdjust.getImm(); 1319 int Delta = StackAdj - MaxTCRetDelta; 1320 assert((Delta >= 0) && "Delta must be positive"); 1321 if (MaxTCRetDelta>0) 1322 FrameSize += (StackAdj +Delta); 1323 else 1324 FrameSize += StackAdj; 1325 } 1326 } 1327 1328 // Frames of 32KB & larger require special handling because they cannot be 1329 // indexed into with a simple LD/LWZ immediate offset operand. 1330 bool isLargeFrame = !isInt<16>(FrameSize); 1331 1332 // On targets without red zone, the SP needs to be restored last, so that 1333 // all live contents of the stack frame are upwards of the SP. This means 1334 // that we cannot restore SP just now, since there may be more registers 1335 // to restore from the stack frame (e.g. R31). If the frame size is not 1336 // a simple immediate value, we will need a spare register to hold the 1337 // restored SP. If the frame size is known and small, we can simply adjust 1338 // the offsets of the registers to be restored, and still use SP to restore 1339 // them. In such case, the final update of SP will be to add the frame 1340 // size to it. 1341 // To simplify the code, set RBReg to the base register used to restore 1342 // values from the stack, and set SPAdd to the value that needs to be added 1343 // to the SP at the end. The default values are as if red zone was present. 1344 unsigned RBReg = SPReg; 1345 unsigned SPAdd = 0; 1346 1347 if (FrameSize) { 1348 // In the prologue, the loaded (or persistent) stack pointer value is 1349 // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red 1350 // zone add this offset back now. 1351 1352 // If this function contained a fastcc call and GuaranteedTailCallOpt is 1353 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail 1354 // call which invalidates the stack pointer value in SP(0). So we use the 1355 // value of R31 in this case. 1356 if (FI->hasFastCall()) { 1357 assert(HasFP && "Expecting a valid frame pointer."); 1358 if (!HasRedZone) 1359 RBReg = FPReg; 1360 if (!isLargeFrame) { 1361 BuildMI(MBB, MBBI, dl, AddImmInst, RBReg) 1362 .addReg(FPReg).addImm(FrameSize); 1363 } else { 1364 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) 1365 .addImm(FrameSize >> 16); 1366 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) 1367 .addReg(ScratchReg, RegState::Kill) 1368 .addImm(FrameSize & 0xFFFF); 1369 BuildMI(MBB, MBBI, dl, AddInst) 1370 .addReg(RBReg) 1371 .addReg(FPReg) 1372 .addReg(ScratchReg); 1373 } 1374 } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) { 1375 if (HasRedZone) { 1376 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) 1377 .addReg(SPReg) 1378 .addImm(FrameSize); 1379 } else { 1380 // Make sure that adding FrameSize will not overflow the max offset 1381 // size. 1382 assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 && 1383 "Local offsets should be negative"); 1384 SPAdd = FrameSize; 1385 FPOffset += FrameSize; 1386 BPOffset += FrameSize; 1387 PBPOffset += FrameSize; 1388 } 1389 } else { 1390 // We don't want to use ScratchReg as a base register, because it 1391 // could happen to be R0. Use FP instead, but make sure to preserve it. 1392 if (!HasRedZone) { 1393 // If FP is not saved, copy it to ScratchReg. 1394 if (!HasFP) 1395 BuildMI(MBB, MBBI, dl, OrInst, ScratchReg) 1396 .addReg(FPReg) 1397 .addReg(FPReg); 1398 RBReg = FPReg; 1399 } 1400 BuildMI(MBB, MBBI, dl, LoadInst, RBReg) 1401 .addImm(0) 1402 .addReg(SPReg); 1403 } 1404 } 1405 assert(RBReg != ScratchReg && "Should have avoided ScratchReg"); 1406 // If there is no red zone, ScratchReg may be needed for holding a useful 1407 // value (although not the base register). Make sure it is not overwritten 1408 // too early. 1409 1410 assert((isPPC64 || !MustSaveCR) && 1411 "Epilogue CR restoring supported only in 64-bit mode"); 1412 1413 // If we need to restore both the LR and the CR and we only have one 1414 // available scratch register, we must do them one at a time. 1415 if (MustSaveCR && SingleScratchReg && MustSaveLR) { 1416 // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg 1417 // is live here. 1418 assert(HasRedZone && "Expecting red zone"); 1419 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg) 1420 .addImm(8) 1421 .addReg(SPReg); 1422 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 1423 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i]) 1424 .addReg(TempReg, getKillRegState(i == e-1)); 1425 } 1426 1427 // Delay restoring of the LR if ScratchReg is needed. This is ok, since 1428 // LR is stored in the caller's stack frame. ScratchReg will be needed 1429 // if RBReg is anything other than SP. We shouldn't use ScratchReg as 1430 // a base register anyway, because it may happen to be R0. 1431 bool LoadedLR = false; 1432 if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) { 1433 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) 1434 .addImm(LROffset+SPAdd) 1435 .addReg(RBReg); 1436 LoadedLR = true; 1437 } 1438 1439 if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) { 1440 // This will only occur for PPC64. 1441 assert(isPPC64 && "Expecting 64-bit mode"); 1442 assert(RBReg == SPReg && "Should be using SP as a base register"); 1443 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg) 1444 .addImm(8) 1445 .addReg(RBReg); 1446 } 1447 1448 if (HasFP) { 1449 // If there is red zone, restore FP directly, since SP has already been 1450 // restored. Otherwise, restore the value of FP into ScratchReg. 1451 if (HasRedZone || RBReg == SPReg) 1452 BuildMI(MBB, MBBI, dl, LoadInst, FPReg) 1453 .addImm(FPOffset) 1454 .addReg(SPReg); 1455 else 1456 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) 1457 .addImm(FPOffset) 1458 .addReg(RBReg); 1459 } 1460 1461 if (FI->usesPICBase()) 1462 BuildMI(MBB, MBBI, dl, LoadInst, PPC::R30) 1463 .addImm(PBPOffset) 1464 .addReg(RBReg); 1465 1466 if (HasBP) 1467 BuildMI(MBB, MBBI, dl, LoadInst, BPReg) 1468 .addImm(BPOffset) 1469 .addReg(RBReg); 1470 1471 // There is nothing more to be loaded from the stack, so now we can 1472 // restore SP: SP = RBReg + SPAdd. 1473 if (RBReg != SPReg || SPAdd != 0) { 1474 assert(!HasRedZone && "This should not happen with red zone"); 1475 // If SPAdd is 0, generate a copy. 1476 if (SPAdd == 0) 1477 BuildMI(MBB, MBBI, dl, OrInst, SPReg) 1478 .addReg(RBReg) 1479 .addReg(RBReg); 1480 else 1481 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) 1482 .addReg(RBReg) 1483 .addImm(SPAdd); 1484 1485 assert(RBReg != ScratchReg && "Should be using FP or SP as base register"); 1486 if (RBReg == FPReg) 1487 BuildMI(MBB, MBBI, dl, OrInst, FPReg) 1488 .addReg(ScratchReg) 1489 .addReg(ScratchReg); 1490 1491 // Now load the LR from the caller's stack frame. 1492 if (MustSaveLR && !LoadedLR) 1493 BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) 1494 .addImm(LROffset) 1495 .addReg(SPReg); 1496 } 1497 1498 if (MustSaveCR && 1499 !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64 1500 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 1501 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i]) 1502 .addReg(TempReg, getKillRegState(i == e-1)); 1503 1504 if (MustSaveLR) 1505 BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg); 1506 1507 // Callee pop calling convention. Pop parameter/linkage area. Used for tail 1508 // call optimization 1509 if (IsReturnBlock) { 1510 unsigned RetOpcode = MBBI->getOpcode(); 1511 if (MF.getTarget().Options.GuaranteedTailCallOpt && 1512 (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) && 1513 MF.getFunction()->getCallingConv() == CallingConv::Fast) { 1514 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 1515 unsigned CallerAllocatedAmt = FI->getMinReservedArea(); 1516 1517 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { 1518 BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) 1519 .addReg(SPReg).addImm(CallerAllocatedAmt); 1520 } else { 1521 BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) 1522 .addImm(CallerAllocatedAmt >> 16); 1523 BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) 1524 .addReg(ScratchReg, RegState::Kill) 1525 .addImm(CallerAllocatedAmt & 0xFFFF); 1526 BuildMI(MBB, MBBI, dl, AddInst) 1527 .addReg(SPReg) 1528 .addReg(FPReg) 1529 .addReg(ScratchReg); 1530 } 1531 } else { 1532 createTailCallBranchInstr(MBB); 1533 } 1534 } 1535 } 1536 1537 void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const { 1538 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); 1539 DebugLoc dl; 1540 1541 if (MBBI != MBB.end()) 1542 dl = MBBI->getDebugLoc(); 1543 1544 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 1545 1546 // Create branch instruction for pseudo tail call return instruction 1547 unsigned RetOpcode = MBBI->getOpcode(); 1548 if (RetOpcode == PPC::TCRETURNdi) { 1549 MBBI = MBB.getLastNonDebugInstr(); 1550 MachineOperand &JumpTarget = MBBI->getOperand(0); 1551 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). 1552 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 1553 } else if (RetOpcode == PPC::TCRETURNri) { 1554 MBBI = MBB.getLastNonDebugInstr(); 1555 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 1556 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); 1557 } else if (RetOpcode == PPC::TCRETURNai) { 1558 MBBI = MBB.getLastNonDebugInstr(); 1559 MachineOperand &JumpTarget = MBBI->getOperand(0); 1560 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); 1561 } else if (RetOpcode == PPC::TCRETURNdi8) { 1562 MBBI = MBB.getLastNonDebugInstr(); 1563 MachineOperand &JumpTarget = MBBI->getOperand(0); 1564 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). 1565 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 1566 } else if (RetOpcode == PPC::TCRETURNri8) { 1567 MBBI = MBB.getLastNonDebugInstr(); 1568 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 1569 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); 1570 } else if (RetOpcode == PPC::TCRETURNai8) { 1571 MBBI = MBB.getLastNonDebugInstr(); 1572 MachineOperand &JumpTarget = MBBI->getOperand(0); 1573 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); 1574 } 1575 } 1576 1577 void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF, 1578 BitVector &SavedRegs, 1579 RegScavenger *RS) const { 1580 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 1581 1582 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 1583 1584 // Save and clear the LR state. 1585 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 1586 unsigned LR = RegInfo->getRARegister(); 1587 FI->setMustSaveLR(MustSaveLR(MF, LR)); 1588 SavedRegs.reset(LR); 1589 1590 // Save R31 if necessary 1591 int FPSI = FI->getFramePointerSaveIndex(); 1592 bool isPPC64 = Subtarget.isPPC64(); 1593 bool isDarwinABI = Subtarget.isDarwinABI(); 1594 MachineFrameInfo &MFI = MF.getFrameInfo(); 1595 1596 // If the frame pointer save index hasn't been defined yet. 1597 if (!FPSI && needsFP(MF)) { 1598 // Find out what the fix offset of the frame pointer save area. 1599 int FPOffset = getFramePointerSaveOffset(); 1600 // Allocate the frame index for frame pointer save area. 1601 FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 1602 // Save the result. 1603 FI->setFramePointerSaveIndex(FPSI); 1604 } 1605 1606 int BPSI = FI->getBasePointerSaveIndex(); 1607 if (!BPSI && RegInfo->hasBasePointer(MF)) { 1608 int BPOffset = getBasePointerSaveOffset(); 1609 // Allocate the frame index for the base pointer save area. 1610 BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true); 1611 // Save the result. 1612 FI->setBasePointerSaveIndex(BPSI); 1613 } 1614 1615 // Reserve stack space for the PIC Base register (R30). 1616 // Only used in SVR4 32-bit. 1617 if (FI->usesPICBase()) { 1618 int PBPSI = MFI.CreateFixedObject(4, -8, true); 1619 FI->setPICBasePointerSaveIndex(PBPSI); 1620 } 1621 1622 // Make sure we don't explicitly spill r31, because, for example, we have 1623 // some inline asm which explicity clobbers it, when we otherwise have a 1624 // frame pointer and are using r31's spill slot for the prologue/epilogue 1625 // code. Same goes for the base pointer and the PIC base register. 1626 if (needsFP(MF)) 1627 SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31); 1628 if (RegInfo->hasBasePointer(MF)) 1629 SavedRegs.reset(RegInfo->getBaseRegister(MF)); 1630 if (FI->usesPICBase()) 1631 SavedRegs.reset(PPC::R30); 1632 1633 // Reserve stack space to move the linkage area to in case of a tail call. 1634 int TCSPDelta = 0; 1635 if (MF.getTarget().Options.GuaranteedTailCallOpt && 1636 (TCSPDelta = FI->getTailCallSPDelta()) < 0) { 1637 MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); 1638 } 1639 1640 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the 1641 // function uses CR 2, 3, or 4. 1642 if (!isPPC64 && !isDarwinABI && 1643 (SavedRegs.test(PPC::CR2) || 1644 SavedRegs.test(PPC::CR3) || 1645 SavedRegs.test(PPC::CR4))) { 1646 int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true); 1647 FI->setCRSpillFrameIndex(FrameIdx); 1648 } 1649 } 1650 1651 void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, 1652 RegScavenger *RS) const { 1653 // Early exit if not using the SVR4 ABI. 1654 if (!Subtarget.isSVR4ABI()) { 1655 addScavengingSpillSlot(MF, RS); 1656 return; 1657 } 1658 1659 // Get callee saved register information. 1660 MachineFrameInfo &MFI = MF.getFrameInfo(); 1661 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 1662 1663 // If the function is shrink-wrapped, and if the function has a tail call, the 1664 // tail call might not be in the new RestoreBlock, so real branch instruction 1665 // won't be generated by emitEpilogue(), because shrink-wrap has chosen new 1666 // RestoreBlock. So we handle this case here. 1667 if (MFI.getSavePoint() && MFI.hasTailCall()) { 1668 MachineBasicBlock *RestoreBlock = MFI.getRestorePoint(); 1669 for (MachineBasicBlock &MBB : MF) { 1670 if (MBB.isReturnBlock() && (&MBB) != RestoreBlock) 1671 createTailCallBranchInstr(MBB); 1672 } 1673 } 1674 1675 // Early exit if no callee saved registers are modified! 1676 if (CSI.empty() && !needsFP(MF)) { 1677 addScavengingSpillSlot(MF, RS); 1678 return; 1679 } 1680 1681 unsigned MinGPR = PPC::R31; 1682 unsigned MinG8R = PPC::X31; 1683 unsigned MinFPR = PPC::F31; 1684 unsigned MinVR = PPC::V31; 1685 1686 bool HasGPSaveArea = false; 1687 bool HasG8SaveArea = false; 1688 bool HasFPSaveArea = false; 1689 bool HasVRSAVESaveArea = false; 1690 bool HasVRSaveArea = false; 1691 1692 SmallVector<CalleeSavedInfo, 18> GPRegs; 1693 SmallVector<CalleeSavedInfo, 18> G8Regs; 1694 SmallVector<CalleeSavedInfo, 18> FPRegs; 1695 SmallVector<CalleeSavedInfo, 18> VRegs; 1696 1697 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1698 unsigned Reg = CSI[i].getReg(); 1699 if (PPC::GPRCRegClass.contains(Reg)) { 1700 HasGPSaveArea = true; 1701 1702 GPRegs.push_back(CSI[i]); 1703 1704 if (Reg < MinGPR) { 1705 MinGPR = Reg; 1706 } 1707 } else if (PPC::G8RCRegClass.contains(Reg)) { 1708 HasG8SaveArea = true; 1709 1710 G8Regs.push_back(CSI[i]); 1711 1712 if (Reg < MinG8R) { 1713 MinG8R = Reg; 1714 } 1715 } else if (PPC::F8RCRegClass.contains(Reg)) { 1716 HasFPSaveArea = true; 1717 1718 FPRegs.push_back(CSI[i]); 1719 1720 if (Reg < MinFPR) { 1721 MinFPR = Reg; 1722 } 1723 } else if (PPC::CRBITRCRegClass.contains(Reg) || 1724 PPC::CRRCRegClass.contains(Reg)) { 1725 ; // do nothing, as we already know whether CRs are spilled 1726 } else if (PPC::VRSAVERCRegClass.contains(Reg)) { 1727 HasVRSAVESaveArea = true; 1728 } else if (PPC::VRRCRegClass.contains(Reg)) { 1729 HasVRSaveArea = true; 1730 1731 VRegs.push_back(CSI[i]); 1732 1733 if (Reg < MinVR) { 1734 MinVR = Reg; 1735 } 1736 } else { 1737 llvm_unreachable("Unknown RegisterClass!"); 1738 } 1739 } 1740 1741 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); 1742 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); 1743 1744 int64_t LowerBound = 0; 1745 1746 // Take into account stack space reserved for tail calls. 1747 int TCSPDelta = 0; 1748 if (MF.getTarget().Options.GuaranteedTailCallOpt && 1749 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { 1750 LowerBound = TCSPDelta; 1751 } 1752 1753 // The Floating-point register save area is right below the back chain word 1754 // of the previous stack frame. 1755 if (HasFPSaveArea) { 1756 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { 1757 int FI = FPRegs[i].getFrameIdx(); 1758 1759 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1760 } 1761 1762 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8; 1763 } 1764 1765 // Check whether the frame pointer register is allocated. If so, make sure it 1766 // is spilled to the correct offset. 1767 if (needsFP(MF)) { 1768 HasGPSaveArea = true; 1769 1770 int FI = PFI->getFramePointerSaveIndex(); 1771 assert(FI && "No Frame Pointer Save Slot!"); 1772 1773 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1774 } 1775 1776 if (PFI->usesPICBase()) { 1777 HasGPSaveArea = true; 1778 1779 int FI = PFI->getPICBasePointerSaveIndex(); 1780 assert(FI && "No PIC Base Pointer Save Slot!"); 1781 1782 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1783 } 1784 1785 const PPCRegisterInfo *RegInfo = Subtarget.getRegisterInfo(); 1786 if (RegInfo->hasBasePointer(MF)) { 1787 HasGPSaveArea = true; 1788 1789 int FI = PFI->getBasePointerSaveIndex(); 1790 assert(FI && "No Base Pointer Save Slot!"); 1791 1792 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1793 } 1794 1795 // General register save area starts right below the Floating-point 1796 // register save area. 1797 if (HasGPSaveArea || HasG8SaveArea) { 1798 // Move general register save area spill slots down, taking into account 1799 // the size of the Floating-point register save area. 1800 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { 1801 int FI = GPRegs[i].getFrameIdx(); 1802 1803 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1804 } 1805 1806 // Move general register save area spill slots down, taking into account 1807 // the size of the Floating-point register save area. 1808 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { 1809 int FI = G8Regs[i].getFrameIdx(); 1810 1811 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1812 } 1813 1814 unsigned MinReg = 1815 std::min<unsigned>(TRI->getEncodingValue(MinGPR), 1816 TRI->getEncodingValue(MinG8R)); 1817 1818 if (Subtarget.isPPC64()) { 1819 LowerBound -= (31 - MinReg + 1) * 8; 1820 } else { 1821 LowerBound -= (31 - MinReg + 1) * 4; 1822 } 1823 } 1824 1825 // For 32-bit only, the CR save area is below the general register 1826 // save area. For 64-bit SVR4, the CR save area is addressed relative 1827 // to the stack pointer and hence does not need an adjustment here. 1828 // Only CR2 (the first nonvolatile spilled) has an associated frame 1829 // index so that we have a single uniform save area. 1830 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) { 1831 // Adjust the frame index of the CR spill slot. 1832 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1833 unsigned Reg = CSI[i].getReg(); 1834 1835 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2) 1836 // Leave Darwin logic as-is. 1837 || (!Subtarget.isSVR4ABI() && 1838 (PPC::CRBITRCRegClass.contains(Reg) || 1839 PPC::CRRCRegClass.contains(Reg)))) { 1840 int FI = CSI[i].getFrameIdx(); 1841 1842 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1843 } 1844 } 1845 1846 LowerBound -= 4; // The CR save area is always 4 bytes long. 1847 } 1848 1849 if (HasVRSAVESaveArea) { 1850 // FIXME SVR4: Is it actually possible to have multiple elements in CSI 1851 // which have the VRSAVE register class? 1852 // Adjust the frame index of the VRSAVE spill slot. 1853 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1854 unsigned Reg = CSI[i].getReg(); 1855 1856 if (PPC::VRSAVERCRegClass.contains(Reg)) { 1857 int FI = CSI[i].getFrameIdx(); 1858 1859 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1860 } 1861 } 1862 1863 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. 1864 } 1865 1866 if (HasVRSaveArea) { 1867 // Insert alignment padding, we need 16-byte alignment. 1868 LowerBound = (LowerBound - 15) & ~(15); 1869 1870 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { 1871 int FI = VRegs[i].getFrameIdx(); 1872 1873 MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); 1874 } 1875 } 1876 1877 addScavengingSpillSlot(MF, RS); 1878 } 1879 1880 void 1881 PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF, 1882 RegScavenger *RS) const { 1883 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or 1884 // a large stack, which will require scavenging a register to materialize a 1885 // large offset. 1886 1887 // We need to have a scavenger spill slot for spills if the frame size is 1888 // large. In case there is no free register for large-offset addressing, 1889 // this slot is used for the necessary emergency spill. Also, we need the 1890 // slot for dynamic stack allocations. 1891 1892 // The scavenger might be invoked if the frame offset does not fit into 1893 // the 16-bit immediate. We don't know the complete frame size here 1894 // because we've not yet computed callee-saved register spills or the 1895 // needed alignment padding. 1896 unsigned StackSize = determineFrameLayout(MF, false, true); 1897 MachineFrameInfo &MFI = MF.getFrameInfo(); 1898 if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) || 1899 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) { 1900 const TargetRegisterClass &GPRC = PPC::GPRCRegClass; 1901 const TargetRegisterClass &G8RC = PPC::G8RCRegClass; 1902 const TargetRegisterClass &RC = Subtarget.isPPC64() ? G8RC : GPRC; 1903 const TargetRegisterInfo &TRI = *Subtarget.getRegisterInfo(); 1904 unsigned Size = TRI.getSpillSize(RC); 1905 unsigned Align = TRI.getSpillAlignment(RC); 1906 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false)); 1907 1908 // Might we have over-aligned allocas? 1909 bool HasAlVars = MFI.hasVarSizedObjects() && 1910 MFI.getMaxAlignment() > getStackAlignment(); 1911 1912 // These kinds of spills might need two registers. 1913 if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars) 1914 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false)); 1915 1916 } 1917 } 1918 1919 bool 1920 PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1921 MachineBasicBlock::iterator MI, 1922 const std::vector<CalleeSavedInfo> &CSI, 1923 const TargetRegisterInfo *TRI) const { 1924 1925 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 1926 // Return false otherwise to maintain pre-existing behavior. 1927 if (!Subtarget.isSVR4ABI()) 1928 return false; 1929 1930 MachineFunction *MF = MBB.getParent(); 1931 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 1932 DebugLoc DL; 1933 bool CRSpilled = false; 1934 MachineInstrBuilder CRMIB; 1935 1936 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1937 unsigned Reg = CSI[i].getReg(); 1938 // Only Darwin actually uses the VRSAVE register, but it can still appear 1939 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on 1940 // Darwin, ignore it. 1941 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI()) 1942 continue; 1943 1944 // CR2 through CR4 are the nonvolatile CR fields. 1945 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4; 1946 1947 // Add the callee-saved register as live-in; it's killed at the spill. 1948 MBB.addLiveIn(Reg); 1949 1950 if (CRSpilled && IsCRField) { 1951 CRMIB.addReg(Reg, RegState::ImplicitKill); 1952 continue; 1953 } 1954 1955 // Insert the spill to the stack frame. 1956 if (IsCRField) { 1957 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 1958 if (Subtarget.isPPC64()) { 1959 // The actual spill will happen at the start of the prologue. 1960 FuncInfo->addMustSaveCR(Reg); 1961 } else { 1962 CRSpilled = true; 1963 FuncInfo->setSpillsCR(); 1964 1965 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have 1966 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot. 1967 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12) 1968 .addReg(Reg, RegState::ImplicitKill); 1969 1970 MBB.insert(MI, CRMIB); 1971 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW)) 1972 .addReg(PPC::R12, 1973 getKillRegState(true)), 1974 CSI[i].getFrameIdx())); 1975 } 1976 } else { 1977 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 1978 TII.storeRegToStackSlot(MBB, MI, Reg, true, 1979 CSI[i].getFrameIdx(), RC, TRI); 1980 } 1981 } 1982 return true; 1983 } 1984 1985 static void 1986 restoreCRs(bool isPPC64, bool is31, 1987 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled, 1988 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, 1989 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) { 1990 1991 MachineFunction *MF = MBB.getParent(); 1992 const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 1993 DebugLoc DL; 1994 unsigned RestoreOp, MoveReg; 1995 1996 if (isPPC64) 1997 // This is handled during epilogue generation. 1998 return; 1999 else { 2000 // 32-bit: FP-relative 2001 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ), 2002 PPC::R12), 2003 CSI[CSIIndex].getFrameIdx())); 2004 RestoreOp = PPC::MTOCRF; 2005 MoveReg = PPC::R12; 2006 } 2007 2008 if (CR2Spilled) 2009 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2) 2010 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled))); 2011 2012 if (CR3Spilled) 2013 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3) 2014 .addReg(MoveReg, getKillRegState(!CR4Spilled))); 2015 2016 if (CR4Spilled) 2017 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4) 2018 .addReg(MoveReg, getKillRegState(true))); 2019 } 2020 2021 MachineBasicBlock::iterator PPCFrameLowering:: 2022 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 2023 MachineBasicBlock::iterator I) const { 2024 const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); 2025 if (MF.getTarget().Options.GuaranteedTailCallOpt && 2026 I->getOpcode() == PPC::ADJCALLSTACKUP) { 2027 // Add (actually subtract) back the amount the callee popped on return. 2028 if (int CalleeAmt = I->getOperand(1).getImm()) { 2029 bool is64Bit = Subtarget.isPPC64(); 2030 CalleeAmt *= -1; 2031 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1; 2032 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0; 2033 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI; 2034 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4; 2035 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS; 2036 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI; 2037 const DebugLoc &dl = I->getDebugLoc(); 2038 2039 if (isInt<16>(CalleeAmt)) { 2040 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg) 2041 .addReg(StackReg, RegState::Kill) 2042 .addImm(CalleeAmt); 2043 } else { 2044 MachineBasicBlock::iterator MBBI = I; 2045 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 2046 .addImm(CalleeAmt >> 16); 2047 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 2048 .addReg(TmpReg, RegState::Kill) 2049 .addImm(CalleeAmt & 0xFFFF); 2050 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg) 2051 .addReg(StackReg, RegState::Kill) 2052 .addReg(TmpReg); 2053 } 2054 } 2055 } 2056 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions. 2057 return MBB.erase(I); 2058 } 2059 2060 bool 2061 PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 2062 MachineBasicBlock::iterator MI, 2063 const std::vector<CalleeSavedInfo> &CSI, 2064 const TargetRegisterInfo *TRI) const { 2065 2066 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 2067 // Return false otherwise to maintain pre-existing behavior. 2068 if (!Subtarget.isSVR4ABI()) 2069 return false; 2070 2071 MachineFunction *MF = MBB.getParent(); 2072 const PPCInstrInfo &TII = *Subtarget.getInstrInfo(); 2073 bool CR2Spilled = false; 2074 bool CR3Spilled = false; 2075 bool CR4Spilled = false; 2076 unsigned CSIIndex = 0; 2077 2078 // Initialize insertion-point logic; we will be restoring in reverse 2079 // order of spill. 2080 MachineBasicBlock::iterator I = MI, BeforeI = I; 2081 bool AtStart = I == MBB.begin(); 2082 2083 if (!AtStart) 2084 --BeforeI; 2085 2086 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 2087 unsigned Reg = CSI[i].getReg(); 2088 2089 // Only Darwin actually uses the VRSAVE register, but it can still appear 2090 // here if, for example, @llvm.eh.unwind.init() is used. If we're not on 2091 // Darwin, ignore it. 2092 if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI()) 2093 continue; 2094 2095 if (Reg == PPC::CR2) { 2096 CR2Spilled = true; 2097 // The spill slot is associated only with CR2, which is the 2098 // first nonvolatile spilled. Save it here. 2099 CSIIndex = i; 2100 continue; 2101 } else if (Reg == PPC::CR3) { 2102 CR3Spilled = true; 2103 continue; 2104 } else if (Reg == PPC::CR4) { 2105 CR4Spilled = true; 2106 continue; 2107 } else { 2108 // When we first encounter a non-CR register after seeing at 2109 // least one CR register, restore all spilled CRs together. 2110 if ((CR2Spilled || CR3Spilled || CR4Spilled) 2111 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 2112 bool is31 = needsFP(*MF); 2113 restoreCRs(Subtarget.isPPC64(), is31, 2114 CR2Spilled, CR3Spilled, CR4Spilled, 2115 MBB, I, CSI, CSIIndex); 2116 CR2Spilled = CR3Spilled = CR4Spilled = false; 2117 } 2118 2119 // Default behavior for non-CR saves. 2120 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 2121 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), 2122 RC, TRI); 2123 assert(I != MBB.begin() && 2124 "loadRegFromStackSlot didn't insert any code!"); 2125 } 2126 2127 // Insert in reverse order. 2128 if (AtStart) 2129 I = MBB.begin(); 2130 else { 2131 I = BeforeI; 2132 ++I; 2133 } 2134 } 2135 2136 // If we haven't yet spilled the CRs, do so now. 2137 if (CR2Spilled || CR3Spilled || CR4Spilled) { 2138 bool is31 = needsFP(*MF); 2139 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled, 2140 MBB, I, CSI, CSIIndex); 2141 } 2142 2143 return true; 2144 } 2145 2146 bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const { 2147 return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() && 2148 MF.getSubtarget<PPCSubtarget>().isPPC64()); 2149 } 2150