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 "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineInstrBuilder.h" 21 #include "llvm/CodeGen/MachineModuleInfo.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/RegisterScavenging.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/Target/TargetOptions.h" 26 27 using namespace llvm; 28 29 // FIXME This disables some code that aligns the stack to a boundary bigger than 30 // the default (16 bytes on Darwin) when there is a stack local of greater 31 // alignment. This does not currently work, because the delta between old and 32 // new stack pointers is added to offsets that reference incoming parameters 33 // after the prolog is generated, and the code that does that doesn't handle a 34 // variable delta. You don't want to do that anyway; a better approach is to 35 // reserve another register that retains to the incoming stack pointer, and 36 // reference parameters relative to that. 37 #define ALIGN_STACK 0 38 39 40 /// VRRegNo - Map from a numbered VR register to its enum value. 41 /// 42 static const uint16_t VRRegNo[] = { 43 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , 44 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, 45 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, 46 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 47 }; 48 49 /// RemoveVRSaveCode - We have found that this function does not need any code 50 /// to manipulate the VRSAVE register, even though it uses vector registers. 51 /// This can happen when the only registers used are known to be live in or out 52 /// of the function. Remove all of the VRSAVE related code from the function. 53 /// FIXME: The removal of the code results in a compile failure at -O0 when the 54 /// function contains a function call, as the GPR containing original VRSAVE 55 /// contents is spilled and reloaded around the call. Without the prolog code, 56 /// the spill instruction refers to an undefined register. This code needs 57 /// to account for all uses of that GPR. 58 static void RemoveVRSaveCode(MachineInstr *MI) { 59 MachineBasicBlock *Entry = MI->getParent(); 60 MachineFunction *MF = Entry->getParent(); 61 62 // We know that the MTVRSAVE instruction immediately follows MI. Remove it. 63 MachineBasicBlock::iterator MBBI = MI; 64 ++MBBI; 65 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); 66 MBBI->eraseFromParent(); 67 68 bool RemovedAllMTVRSAVEs = true; 69 // See if we can find and remove the MTVRSAVE instruction from all of the 70 // epilog blocks. 71 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { 72 // If last instruction is a return instruction, add an epilogue 73 if (!I->empty() && I->back().isReturn()) { 74 bool FoundIt = false; 75 for (MBBI = I->end(); MBBI != I->begin(); ) { 76 --MBBI; 77 if (MBBI->getOpcode() == PPC::MTVRSAVE) { 78 MBBI->eraseFromParent(); // remove it. 79 FoundIt = true; 80 break; 81 } 82 } 83 RemovedAllMTVRSAVEs &= FoundIt; 84 } 85 } 86 87 // If we found and removed all MTVRSAVE instructions, remove the read of 88 // VRSAVE as well. 89 if (RemovedAllMTVRSAVEs) { 90 MBBI = MI; 91 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); 92 --MBBI; 93 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); 94 MBBI->eraseFromParent(); 95 } 96 97 // Finally, nuke the UPDATE_VRSAVE. 98 MI->eraseFromParent(); 99 } 100 101 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the 102 // instruction selector. Based on the vector registers that have been used, 103 // transform this into the appropriate ORI instruction. 104 static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) { 105 MachineFunction *MF = MI->getParent()->getParent(); 106 DebugLoc dl = MI->getDebugLoc(); 107 108 unsigned UsedRegMask = 0; 109 for (unsigned i = 0; i != 32; ++i) 110 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i])) 111 UsedRegMask |= 1 << (31-i); 112 113 // Live in and live out values already must be in the mask, so don't bother 114 // marking them. 115 for (MachineRegisterInfo::livein_iterator 116 I = MF->getRegInfo().livein_begin(), 117 E = MF->getRegInfo().livein_end(); I != E; ++I) { 118 unsigned RegNo = getPPCRegisterNumbering(I->first); 119 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. 120 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. 121 } 122 123 // Live out registers appear as use operands on return instructions. 124 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end(); 125 UsedRegMask != 0 && BI != BE; ++BI) { 126 const MachineBasicBlock &MBB = *BI; 127 if (MBB.empty() || !MBB.back().isReturn()) 128 continue; 129 const MachineInstr &Ret = MBB.back(); 130 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) { 131 const MachineOperand &MO = Ret.getOperand(I); 132 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg())) 133 continue; 134 unsigned RegNo = getPPCRegisterNumbering(MO.getReg()); 135 UsedRegMask &= ~(1 << (31-RegNo)); 136 } 137 } 138 139 // If no registers are used, turn this into a copy. 140 if (UsedRegMask == 0) { 141 // Remove all VRSAVE code. 142 RemoveVRSaveCode(MI); 143 return; 144 } 145 146 unsigned SrcReg = MI->getOperand(1).getReg(); 147 unsigned DstReg = MI->getOperand(0).getReg(); 148 149 if ((UsedRegMask & 0xFFFF) == UsedRegMask) { 150 if (DstReg != SrcReg) 151 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 152 .addReg(SrcReg) 153 .addImm(UsedRegMask); 154 else 155 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 156 .addReg(SrcReg, RegState::Kill) 157 .addImm(UsedRegMask); 158 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { 159 if (DstReg != SrcReg) 160 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 161 .addReg(SrcReg) 162 .addImm(UsedRegMask >> 16); 163 else 164 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 165 .addReg(SrcReg, RegState::Kill) 166 .addImm(UsedRegMask >> 16); 167 } else { 168 if (DstReg != SrcReg) 169 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 170 .addReg(SrcReg) 171 .addImm(UsedRegMask >> 16); 172 else 173 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 174 .addReg(SrcReg, RegState::Kill) 175 .addImm(UsedRegMask >> 16); 176 177 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 178 .addReg(DstReg, RegState::Kill) 179 .addImm(UsedRegMask & 0xFFFF); 180 } 181 182 // Remove the old UPDATE_VRSAVE instruction. 183 MI->eraseFromParent(); 184 } 185 186 static bool spillsCR(const MachineFunction &MF) { 187 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 188 return FuncInfo->isCRSpilled(); 189 } 190 191 static bool spillsVRSAVE(const MachineFunction &MF) { 192 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 193 return FuncInfo->isVRSAVESpilled(); 194 } 195 196 static bool hasSpills(const MachineFunction &MF) { 197 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 198 return FuncInfo->hasSpills(); 199 } 200 201 static bool hasNonRISpills(const MachineFunction &MF) { 202 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 203 return FuncInfo->hasNonRISpills(); 204 } 205 206 /// determineFrameLayout - Determine the size of the frame and maximum call 207 /// frame size. 208 unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF, 209 bool UpdateMF, 210 bool UseEstimate) const { 211 MachineFrameInfo *MFI = MF.getFrameInfo(); 212 213 // Get the number of bytes to allocate from the FrameInfo 214 unsigned FrameSize = 215 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize(); 216 217 // Get the alignments provided by the target, and the maximum alignment 218 // (if any) of the fixed frame objects. 219 unsigned MaxAlign = MFI->getMaxAlignment(); 220 unsigned TargetAlign = getStackAlignment(); 221 unsigned AlignMask = TargetAlign - 1; // 222 223 // If we are a leaf function, and use up to 224 bytes of stack space, 224 // don't have a frame pointer, calls, or dynamic alloca then we do not need 225 // to adjust the stack pointer (we fit in the Red Zone). For 64-bit 226 // SVR4, we also require a stack frame if we need to spill the CR, 227 // since this spill area is addressed relative to the stack pointer. 228 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate 229 // stackless code if all local vars are reg-allocated. 230 bool DisableRedZone = MF.getFunction()->getAttributes(). 231 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone); 232 if (!DisableRedZone && 233 (Subtarget.isPPC64() || // 32-bit SVR4, no stack- 234 !Subtarget.isSVR4ABI() || // allocated locals. 235 FrameSize == 0) && 236 FrameSize <= 224 && // Fits in red zone. 237 !MFI->hasVarSizedObjects() && // No dynamic alloca. 238 !MFI->adjustsStack() && // No calls. 239 !(Subtarget.isPPC64() && // No 64-bit SVR4 CRsave. 240 Subtarget.isSVR4ABI() 241 && spillsCR(MF)) && 242 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment. 243 // No need for frame 244 if (UpdateMF) 245 MFI->setStackSize(0); 246 return 0; 247 } 248 249 // Get the maximum call frame size of all the calls. 250 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); 251 252 // Maximum call frame needs to be at least big enough for linkage and 8 args. 253 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(), 254 Subtarget.isDarwinABI()); 255 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); 256 257 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so 258 // that allocations will be aligned. 259 if (MFI->hasVarSizedObjects()) 260 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; 261 262 // Update maximum call frame size. 263 if (UpdateMF) 264 MFI->setMaxCallFrameSize(maxCallFrameSize); 265 266 // Include call frame size in total. 267 FrameSize += maxCallFrameSize; 268 269 // Make sure the frame is aligned. 270 FrameSize = (FrameSize + AlignMask) & ~AlignMask; 271 272 // Update frame info. 273 if (UpdateMF) 274 MFI->setStackSize(FrameSize); 275 276 return FrameSize; 277 } 278 279 // hasFP - Return true if the specified function actually has a dedicated frame 280 // pointer register. 281 bool PPCFrameLowering::hasFP(const MachineFunction &MF) const { 282 const MachineFrameInfo *MFI = MF.getFrameInfo(); 283 // FIXME: This is pretty much broken by design: hasFP() might be called really 284 // early, before the stack layout was calculated and thus hasFP() might return 285 // true or false here depending on the time of call. 286 return (MFI->getStackSize()) && needsFP(MF); 287 } 288 289 // needsFP - Return true if the specified function should have a dedicated frame 290 // pointer register. This is true if the function has variable sized allocas or 291 // if frame pointer elimination is disabled. 292 bool PPCFrameLowering::needsFP(const MachineFunction &MF) const { 293 const MachineFrameInfo *MFI = MF.getFrameInfo(); 294 295 // Naked functions have no stack frame pushed, so we don't have a frame 296 // pointer. 297 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 298 Attribute::Naked)) 299 return false; 300 301 return MF.getTarget().Options.DisableFramePointerElim(MF) || 302 MFI->hasVarSizedObjects() || 303 (MF.getTarget().Options.GuaranteedTailCallOpt && 304 MF.getInfo<PPCFunctionInfo>()->hasFastCall()); 305 } 306 307 void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const { 308 bool is31 = needsFP(MF); 309 unsigned FPReg = is31 ? PPC::R31 : PPC::R1; 310 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1; 311 312 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); 313 BI != BE; ++BI) 314 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) { 315 --MBBI; 316 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) { 317 MachineOperand &MO = MBBI->getOperand(I); 318 if (!MO.isReg()) 319 continue; 320 321 switch (MO.getReg()) { 322 case PPC::FP: 323 MO.setReg(FPReg); 324 break; 325 case PPC::FP8: 326 MO.setReg(FP8Reg); 327 break; 328 } 329 } 330 } 331 } 332 333 void PPCFrameLowering::emitPrologue(MachineFunction &MF) const { 334 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 335 MachineBasicBlock::iterator MBBI = MBB.begin(); 336 MachineFrameInfo *MFI = MF.getFrameInfo(); 337 const PPCInstrInfo &TII = 338 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 339 340 MachineModuleInfo &MMI = MF.getMMI(); 341 DebugLoc dl; 342 bool needsFrameMoves = MMI.hasDebugInfo() || 343 MF.getFunction()->needsUnwindTableEntry(); 344 345 // Prepare for frame info. 346 MCSymbol *FrameLabel = 0; 347 348 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, 349 // process it. 350 if (!Subtarget.isSVR4ABI()) 351 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { 352 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { 353 HandleVRSaveUpdate(MBBI, TII); 354 break; 355 } 356 } 357 358 // Move MBBI back to the beginning of the function. 359 MBBI = MBB.begin(); 360 361 // Work out frame sizes. 362 unsigned FrameSize = determineFrameLayout(MF); 363 int NegFrameSize = -FrameSize; 364 365 if (MFI->isFrameAddressTaken()) 366 replaceFPWithRealFP(MF); 367 368 // Get processor type. 369 bool isPPC64 = Subtarget.isPPC64(); 370 // Get operating system 371 bool isDarwinABI = Subtarget.isDarwinABI(); 372 // Check if the link register (LR) must be saved. 373 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 374 bool MustSaveLR = FI->mustSaveLR(); 375 // Do we have a frame pointer for this function? 376 bool HasFP = hasFP(MF); 377 378 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 379 380 int FPOffset = 0; 381 if (HasFP) { 382 if (Subtarget.isSVR4ABI()) { 383 MachineFrameInfo *FFI = MF.getFrameInfo(); 384 int FPIndex = FI->getFramePointerSaveIndex(); 385 assert(FPIndex && "No Frame Pointer Save Slot!"); 386 FPOffset = FFI->getObjectOffset(FPIndex); 387 } else { 388 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 389 } 390 } 391 392 if (isPPC64) { 393 if (MustSaveLR) 394 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0); 395 396 if (HasFP) 397 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 398 .addReg(PPC::X31) 399 .addImm(FPOffset/4) 400 .addReg(PPC::X1); 401 402 if (MustSaveLR) 403 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 404 .addReg(PPC::X0) 405 .addImm(LROffset / 4) 406 .addReg(PPC::X1); 407 } else { 408 if (MustSaveLR) 409 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0); 410 411 if (HasFP) 412 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative 413 // offsets of R1 is not allowed. 414 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 415 .addReg(PPC::R31) 416 .addImm(FPOffset) 417 .addReg(PPC::R1); 418 419 if (MustSaveLR) 420 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 421 .addReg(PPC::R0) 422 .addImm(LROffset) 423 .addReg(PPC::R1); 424 } 425 426 // Skip if a leaf routine. 427 if (!FrameSize) return; 428 429 // Get stack alignments. 430 unsigned TargetAlign = getStackAlignment(); 431 unsigned MaxAlign = MFI->getMaxAlignment(); 432 433 // Adjust stack pointer: r1 += NegFrameSize. 434 // If there is a preferred stack alignment, align R1 now 435 if (!isPPC64) { 436 // PPC32. 437 if (ALIGN_STACK && MaxAlign > TargetAlign) { 438 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 439 "Invalid alignment!"); 440 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 441 442 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0) 443 .addReg(PPC::R1) 444 .addImm(0) 445 .addImm(32 - Log2_32(MaxAlign)) 446 .addImm(31); 447 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0) 448 .addReg(PPC::R0, RegState::Kill) 449 .addImm(NegFrameSize); 450 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1) 451 .addReg(PPC::R1, RegState::Kill) 452 .addReg(PPC::R1) 453 .addReg(PPC::R0); 454 } else if (isInt<16>(NegFrameSize)) { 455 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1) 456 .addReg(PPC::R1) 457 .addImm(NegFrameSize) 458 .addReg(PPC::R1); 459 } else { 460 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 461 .addImm(NegFrameSize >> 16); 462 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 463 .addReg(PPC::R0, RegState::Kill) 464 .addImm(NegFrameSize & 0xFFFF); 465 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1) 466 .addReg(PPC::R1, RegState::Kill) 467 .addReg(PPC::R1) 468 .addReg(PPC::R0); 469 } 470 } else { // PPC64. 471 if (ALIGN_STACK && MaxAlign > TargetAlign) { 472 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 473 "Invalid alignment!"); 474 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 475 476 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0) 477 .addReg(PPC::X1) 478 .addImm(0) 479 .addImm(64 - Log2_32(MaxAlign)); 480 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0) 481 .addReg(PPC::X0) 482 .addImm(NegFrameSize); 483 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1) 484 .addReg(PPC::X1, RegState::Kill) 485 .addReg(PPC::X1) 486 .addReg(PPC::X0); 487 } else if (isInt<16>(NegFrameSize)) { 488 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1) 489 .addReg(PPC::X1) 490 .addImm(NegFrameSize / 4) 491 .addReg(PPC::X1); 492 } else { 493 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 494 .addImm(NegFrameSize >> 16); 495 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 496 .addReg(PPC::X0, RegState::Kill) 497 .addImm(NegFrameSize & 0xFFFF); 498 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1) 499 .addReg(PPC::X1, RegState::Kill) 500 .addReg(PPC::X1) 501 .addReg(PPC::X0); 502 } 503 } 504 505 std::vector<MachineMove> &Moves = MMI.getFrameMoves(); 506 507 // Add the "machine moves" for the instructions we generated above, but in 508 // reverse order. 509 if (needsFrameMoves) { 510 // Mark effective beginning of when frame pointer becomes valid. 511 FrameLabel = MMI.getContext().CreateTempSymbol(); 512 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel); 513 514 // Show update of SP. 515 if (NegFrameSize) { 516 MachineLocation SPDst(MachineLocation::VirtualFP); 517 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize); 518 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc)); 519 } else { 520 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31); 521 Moves.push_back(MachineMove(FrameLabel, SP, SP)); 522 } 523 524 if (HasFP) { 525 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset); 526 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31); 527 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc)); 528 } 529 530 if (MustSaveLR) { 531 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset); 532 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR); 533 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc)); 534 } 535 } 536 537 MCSymbol *ReadyLabel = 0; 538 539 // If there is a frame pointer, copy R1 into R31 540 if (HasFP) { 541 if (!isPPC64) { 542 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31) 543 .addReg(PPC::R1) 544 .addReg(PPC::R1); 545 } else { 546 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31) 547 .addReg(PPC::X1) 548 .addReg(PPC::X1); 549 } 550 551 if (needsFrameMoves) { 552 ReadyLabel = MMI.getContext().CreateTempSymbol(); 553 554 // Mark effective beginning of when frame pointer is ready. 555 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel); 556 557 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) : 558 (isPPC64 ? PPC::X1 : PPC::R1)); 559 MachineLocation FPSrc(MachineLocation::VirtualFP); 560 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc)); 561 } 562 } 563 564 if (needsFrameMoves) { 565 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel; 566 567 // Add callee saved registers to move list. 568 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 569 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 570 unsigned Reg = CSI[I].getReg(); 571 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; 572 573 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just 574 // subregisters of CR2. We just need to emit a move of CR2. 575 if (PPC::CRBITRCRegClass.contains(Reg)) 576 continue; 577 578 // For SVR4, don't emit a move for the CR spill slot if we haven't 579 // spilled CRs. 580 if (Subtarget.isSVR4ABI() 581 && (PPC::CR2 <= Reg && Reg <= PPC::CR4) 582 && !spillsCR(MF)) 583 continue; 584 585 // For 64-bit SVR4 when we have spilled CRs, the spill location 586 // is SP+8, not a frame-relative slot. 587 if (Subtarget.isSVR4ABI() 588 && Subtarget.isPPC64() 589 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 590 MachineLocation CSDst(PPC::X1, 8); 591 MachineLocation CSSrc(PPC::CR2); 592 Moves.push_back(MachineMove(Label, CSDst, CSSrc)); 593 continue; 594 } 595 596 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); 597 MachineLocation CSDst(MachineLocation::VirtualFP, Offset); 598 MachineLocation CSSrc(Reg); 599 Moves.push_back(MachineMove(Label, CSDst, CSSrc)); 600 } 601 } 602 } 603 604 void PPCFrameLowering::emitEpilogue(MachineFunction &MF, 605 MachineBasicBlock &MBB) const { 606 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 607 assert(MBBI != MBB.end() && "Returning block has no terminator"); 608 const PPCInstrInfo &TII = 609 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 610 611 unsigned RetOpcode = MBBI->getOpcode(); 612 DebugLoc dl; 613 614 assert((RetOpcode == PPC::BLR || 615 RetOpcode == PPC::TCRETURNri || 616 RetOpcode == PPC::TCRETURNdi || 617 RetOpcode == PPC::TCRETURNai || 618 RetOpcode == PPC::TCRETURNri8 || 619 RetOpcode == PPC::TCRETURNdi8 || 620 RetOpcode == PPC::TCRETURNai8) && 621 "Can only insert epilog into returning blocks"); 622 623 // Get alignment info so we know how to restore r1 624 const MachineFrameInfo *MFI = MF.getFrameInfo(); 625 unsigned TargetAlign = getStackAlignment(); 626 unsigned MaxAlign = MFI->getMaxAlignment(); 627 628 // Get the number of bytes allocated from the FrameInfo. 629 int FrameSize = MFI->getStackSize(); 630 631 // Get processor type. 632 bool isPPC64 = Subtarget.isPPC64(); 633 // Get operating system 634 bool isDarwinABI = Subtarget.isDarwinABI(); 635 // Check if the link register (LR) has been saved. 636 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 637 bool MustSaveLR = FI->mustSaveLR(); 638 // Do we have a frame pointer for this function? 639 bool HasFP = hasFP(MF); 640 641 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 642 643 int FPOffset = 0; 644 if (HasFP) { 645 if (Subtarget.isSVR4ABI()) { 646 MachineFrameInfo *FFI = MF.getFrameInfo(); 647 int FPIndex = FI->getFramePointerSaveIndex(); 648 assert(FPIndex && "No Frame Pointer Save Slot!"); 649 FPOffset = FFI->getObjectOffset(FPIndex); 650 } else { 651 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 652 } 653 } 654 655 bool UsesTCRet = RetOpcode == PPC::TCRETURNri || 656 RetOpcode == PPC::TCRETURNdi || 657 RetOpcode == PPC::TCRETURNai || 658 RetOpcode == PPC::TCRETURNri8 || 659 RetOpcode == PPC::TCRETURNdi8 || 660 RetOpcode == PPC::TCRETURNai8; 661 662 if (UsesTCRet) { 663 int MaxTCRetDelta = FI->getTailCallSPDelta(); 664 MachineOperand &StackAdjust = MBBI->getOperand(1); 665 assert(StackAdjust.isImm() && "Expecting immediate value."); 666 // Adjust stack pointer. 667 int StackAdj = StackAdjust.getImm(); 668 int Delta = StackAdj - MaxTCRetDelta; 669 assert((Delta >= 0) && "Delta must be positive"); 670 if (MaxTCRetDelta>0) 671 FrameSize += (StackAdj +Delta); 672 else 673 FrameSize += StackAdj; 674 } 675 676 if (FrameSize) { 677 // The loaded (or persistent) stack pointer value is offset by the 'stwu' 678 // on entry to the function. Add this offset back now. 679 if (!isPPC64) { 680 // If this function contained a fastcc call and GuaranteedTailCallOpt is 681 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail 682 // call which invalidates the stack pointer value in SP(0). So we use the 683 // value of R31 in this case. 684 if (FI->hasFastCall() && isInt<16>(FrameSize)) { 685 assert(hasFP(MF) && "Expecting a valid the frame pointer."); 686 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 687 .addReg(PPC::R31).addImm(FrameSize); 688 } else if(FI->hasFastCall()) { 689 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 690 .addImm(FrameSize >> 16); 691 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 692 .addReg(PPC::R0, RegState::Kill) 693 .addImm(FrameSize & 0xFFFF); 694 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4)) 695 .addReg(PPC::R1) 696 .addReg(PPC::R31) 697 .addReg(PPC::R0); 698 } else if (isInt<16>(FrameSize) && 699 (!ALIGN_STACK || TargetAlign >= MaxAlign) && 700 !MFI->hasVarSizedObjects()) { 701 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 702 .addReg(PPC::R1).addImm(FrameSize); 703 } else { 704 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1) 705 .addImm(0).addReg(PPC::R1); 706 } 707 } else { 708 if (FI->hasFastCall() && isInt<16>(FrameSize)) { 709 assert(hasFP(MF) && "Expecting a valid the frame pointer."); 710 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 711 .addReg(PPC::X31).addImm(FrameSize); 712 } else if(FI->hasFastCall()) { 713 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 714 .addImm(FrameSize >> 16); 715 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 716 .addReg(PPC::X0, RegState::Kill) 717 .addImm(FrameSize & 0xFFFF); 718 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8)) 719 .addReg(PPC::X1) 720 .addReg(PPC::X31) 721 .addReg(PPC::X0); 722 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign && 723 !MFI->hasVarSizedObjects()) { 724 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 725 .addReg(PPC::X1).addImm(FrameSize); 726 } else { 727 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1) 728 .addImm(0).addReg(PPC::X1); 729 } 730 } 731 } 732 733 if (isPPC64) { 734 if (MustSaveLR) 735 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0) 736 .addImm(LROffset/4).addReg(PPC::X1); 737 738 if (HasFP) 739 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31) 740 .addImm(FPOffset/4).addReg(PPC::X1); 741 742 if (MustSaveLR) 743 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0); 744 } else { 745 if (MustSaveLR) 746 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0) 747 .addImm(LROffset).addReg(PPC::R1); 748 749 if (HasFP) 750 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31) 751 .addImm(FPOffset).addReg(PPC::R1); 752 753 if (MustSaveLR) 754 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0); 755 } 756 757 // Callee pop calling convention. Pop parameter/linkage area. Used for tail 758 // call optimization 759 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR && 760 MF.getFunction()->getCallingConv() == CallingConv::Fast) { 761 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 762 unsigned CallerAllocatedAmt = FI->getMinReservedArea(); 763 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1; 764 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; 765 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0; 766 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI; 767 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4; 768 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS; 769 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI; 770 771 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { 772 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg) 773 .addReg(StackReg).addImm(CallerAllocatedAmt); 774 } else { 775 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 776 .addImm(CallerAllocatedAmt >> 16); 777 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 778 .addReg(TmpReg, RegState::Kill) 779 .addImm(CallerAllocatedAmt & 0xFFFF); 780 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr)) 781 .addReg(StackReg) 782 .addReg(FPReg) 783 .addReg(TmpReg); 784 } 785 } else if (RetOpcode == PPC::TCRETURNdi) { 786 MBBI = MBB.getLastNonDebugInstr(); 787 MachineOperand &JumpTarget = MBBI->getOperand(0); 788 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). 789 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 790 } else if (RetOpcode == PPC::TCRETURNri) { 791 MBBI = MBB.getLastNonDebugInstr(); 792 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 793 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); 794 } else if (RetOpcode == PPC::TCRETURNai) { 795 MBBI = MBB.getLastNonDebugInstr(); 796 MachineOperand &JumpTarget = MBBI->getOperand(0); 797 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); 798 } else if (RetOpcode == PPC::TCRETURNdi8) { 799 MBBI = MBB.getLastNonDebugInstr(); 800 MachineOperand &JumpTarget = MBBI->getOperand(0); 801 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). 802 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 803 } else if (RetOpcode == PPC::TCRETURNri8) { 804 MBBI = MBB.getLastNonDebugInstr(); 805 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 806 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); 807 } else if (RetOpcode == PPC::TCRETURNai8) { 808 MBBI = MBB.getLastNonDebugInstr(); 809 MachineOperand &JumpTarget = MBBI->getOperand(0); 810 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); 811 } 812 } 813 814 /// MustSaveLR - Return true if this function requires that we save the LR 815 /// register onto the stack in the prolog and restore it in the epilog of the 816 /// function. 817 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) { 818 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); 819 820 // We need a save/restore of LR if there is any def of LR (which is 821 // defined by calls, including the PIC setup sequence), or if there is 822 // some use of the LR stack slot (e.g. for builtin_return_address). 823 // (LR comes in 32 and 64 bit versions.) 824 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); 825 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); 826 } 827 828 void 829 PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 830 RegScavenger *) const { 831 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 832 833 // Save and clear the LR state. 834 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 835 unsigned LR = RegInfo->getRARegister(); 836 FI->setMustSaveLR(MustSaveLR(MF, LR)); 837 MachineRegisterInfo &MRI = MF.getRegInfo(); 838 MRI.setPhysRegUnused(LR); 839 840 // Save R31 if necessary 841 int FPSI = FI->getFramePointerSaveIndex(); 842 bool isPPC64 = Subtarget.isPPC64(); 843 bool isDarwinABI = Subtarget.isDarwinABI(); 844 MachineFrameInfo *MFI = MF.getFrameInfo(); 845 846 // If the frame pointer save index hasn't been defined yet. 847 if (!FPSI && needsFP(MF)) { 848 // Find out what the fix offset of the frame pointer save area. 849 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI); 850 // Allocate the frame index for frame pointer save area. 851 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 852 // Save the result. 853 FI->setFramePointerSaveIndex(FPSI); 854 } 855 856 // Reserve stack space to move the linkage area to in case of a tail call. 857 int TCSPDelta = 0; 858 if (MF.getTarget().Options.GuaranteedTailCallOpt && 859 (TCSPDelta = FI->getTailCallSPDelta()) < 0) { 860 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); 861 } 862 863 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the 864 // function uses CR 2, 3, or 4. 865 if (!isPPC64 && !isDarwinABI && 866 (MRI.isPhysRegUsed(PPC::CR2) || 867 MRI.isPhysRegUsed(PPC::CR3) || 868 MRI.isPhysRegUsed(PPC::CR4))) { 869 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true); 870 FI->setCRSpillFrameIndex(FrameIdx); 871 } 872 } 873 874 void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, 875 RegScavenger *RS) const { 876 // Early exit if not using the SVR4 ABI. 877 if (!Subtarget.isSVR4ABI()) { 878 addScavengingSpillSlot(MF, RS); 879 return; 880 } 881 882 // Get callee saved register information. 883 MachineFrameInfo *FFI = MF.getFrameInfo(); 884 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo(); 885 886 // Early exit if no callee saved registers are modified! 887 if (CSI.empty() && !needsFP(MF)) { 888 addScavengingSpillSlot(MF, RS); 889 return; 890 } 891 892 unsigned MinGPR = PPC::R31; 893 unsigned MinG8R = PPC::X31; 894 unsigned MinFPR = PPC::F31; 895 unsigned MinVR = PPC::V31; 896 897 bool HasGPSaveArea = false; 898 bool HasG8SaveArea = false; 899 bool HasFPSaveArea = false; 900 bool HasVRSAVESaveArea = false; 901 bool HasVRSaveArea = false; 902 903 SmallVector<CalleeSavedInfo, 18> GPRegs; 904 SmallVector<CalleeSavedInfo, 18> G8Regs; 905 SmallVector<CalleeSavedInfo, 18> FPRegs; 906 SmallVector<CalleeSavedInfo, 18> VRegs; 907 908 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 909 unsigned Reg = CSI[i].getReg(); 910 if (PPC::GPRCRegClass.contains(Reg)) { 911 HasGPSaveArea = true; 912 913 GPRegs.push_back(CSI[i]); 914 915 if (Reg < MinGPR) { 916 MinGPR = Reg; 917 } 918 } else if (PPC::G8RCRegClass.contains(Reg)) { 919 HasG8SaveArea = true; 920 921 G8Regs.push_back(CSI[i]); 922 923 if (Reg < MinG8R) { 924 MinG8R = Reg; 925 } 926 } else if (PPC::F8RCRegClass.contains(Reg)) { 927 HasFPSaveArea = true; 928 929 FPRegs.push_back(CSI[i]); 930 931 if (Reg < MinFPR) { 932 MinFPR = Reg; 933 } 934 } else if (PPC::CRBITRCRegClass.contains(Reg) || 935 PPC::CRRCRegClass.contains(Reg)) { 936 ; // do nothing, as we already know whether CRs are spilled 937 } else if (PPC::VRSAVERCRegClass.contains(Reg)) { 938 HasVRSAVESaveArea = true; 939 } else if (PPC::VRRCRegClass.contains(Reg)) { 940 HasVRSaveArea = true; 941 942 VRegs.push_back(CSI[i]); 943 944 if (Reg < MinVR) { 945 MinVR = Reg; 946 } 947 } else { 948 llvm_unreachable("Unknown RegisterClass!"); 949 } 950 } 951 952 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); 953 954 int64_t LowerBound = 0; 955 956 // Take into account stack space reserved for tail calls. 957 int TCSPDelta = 0; 958 if (MF.getTarget().Options.GuaranteedTailCallOpt && 959 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { 960 LowerBound = TCSPDelta; 961 } 962 963 // The Floating-point register save area is right below the back chain word 964 // of the previous stack frame. 965 if (HasFPSaveArea) { 966 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { 967 int FI = FPRegs[i].getFrameIdx(); 968 969 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 970 } 971 972 LowerBound -= (31 - getPPCRegisterNumbering(MinFPR) + 1) * 8; 973 } 974 975 // Check whether the frame pointer register is allocated. If so, make sure it 976 // is spilled to the correct offset. 977 if (needsFP(MF)) { 978 HasGPSaveArea = true; 979 980 int FI = PFI->getFramePointerSaveIndex(); 981 assert(FI && "No Frame Pointer Save Slot!"); 982 983 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 984 } 985 986 // General register save area starts right below the Floating-point 987 // register save area. 988 if (HasGPSaveArea || HasG8SaveArea) { 989 // Move general register save area spill slots down, taking into account 990 // the size of the Floating-point register save area. 991 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { 992 int FI = GPRegs[i].getFrameIdx(); 993 994 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 995 } 996 997 // Move general register save area spill slots down, taking into account 998 // the size of the Floating-point register save area. 999 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { 1000 int FI = G8Regs[i].getFrameIdx(); 1001 1002 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 1003 } 1004 1005 unsigned MinReg = 1006 std::min<unsigned>(getPPCRegisterNumbering(MinGPR), 1007 getPPCRegisterNumbering(MinG8R)); 1008 1009 if (Subtarget.isPPC64()) { 1010 LowerBound -= (31 - MinReg + 1) * 8; 1011 } else { 1012 LowerBound -= (31 - MinReg + 1) * 4; 1013 } 1014 } 1015 1016 // For 32-bit only, the CR save area is below the general register 1017 // save area. For 64-bit SVR4, the CR save area is addressed relative 1018 // to the stack pointer and hence does not need an adjustment here. 1019 // Only CR2 (the first nonvolatile spilled) has an associated frame 1020 // index so that we have a single uniform save area. 1021 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) { 1022 // Adjust the frame index of the CR spill slot. 1023 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1024 unsigned Reg = CSI[i].getReg(); 1025 1026 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2) 1027 // Leave Darwin logic as-is. 1028 || (!Subtarget.isSVR4ABI() && 1029 (PPC::CRBITRCRegClass.contains(Reg) || 1030 PPC::CRRCRegClass.contains(Reg)))) { 1031 int FI = CSI[i].getFrameIdx(); 1032 1033 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 1034 } 1035 } 1036 1037 LowerBound -= 4; // The CR save area is always 4 bytes long. 1038 } 1039 1040 if (HasVRSAVESaveArea) { 1041 // FIXME SVR4: Is it actually possible to have multiple elements in CSI 1042 // which have the VRSAVE register class? 1043 // Adjust the frame index of the VRSAVE spill slot. 1044 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1045 unsigned Reg = CSI[i].getReg(); 1046 1047 if (PPC::VRSAVERCRegClass.contains(Reg)) { 1048 int FI = CSI[i].getFrameIdx(); 1049 1050 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 1051 } 1052 } 1053 1054 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. 1055 } 1056 1057 if (HasVRSaveArea) { 1058 // Insert alignment padding, we need 16-byte alignment. 1059 LowerBound = (LowerBound - 15) & ~(15); 1060 1061 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { 1062 int FI = VRegs[i].getFrameIdx(); 1063 1064 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 1065 } 1066 } 1067 1068 addScavengingSpillSlot(MF, RS); 1069 } 1070 1071 void 1072 PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF, 1073 RegScavenger *RS) const { 1074 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or 1075 // a large stack, which will require scavenging a register to materialize a 1076 // large offset. 1077 1078 // We need to have a scavenger spill slot for spills if the frame size is 1079 // large. In case there is no free register for large-offset addressing, 1080 // this slot is used for the necessary emergency spill. Also, we need the 1081 // slot for dynamic stack allocations. 1082 1083 // The scavenger might be invoked if the frame offset does not fit into 1084 // the 16-bit immediate. We don't know the complete frame size here 1085 // because we've not yet computed callee-saved register spills or the 1086 // needed alignment padding. 1087 unsigned StackSize = determineFrameLayout(MF, false, true); 1088 MachineFrameInfo *MFI = MF.getFrameInfo(); 1089 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) || 1090 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) { 1091 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 1092 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 1093 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC; 1094 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 1095 RC->getAlignment(), 1096 false)); 1097 1098 // These kinds of spills might need two registers. 1099 if (spillsCR(MF) || spillsVRSAVE(MF)) 1100 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 1101 RC->getAlignment(), 1102 false)); 1103 1104 } 1105 } 1106 1107 bool 1108 PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1109 MachineBasicBlock::iterator MI, 1110 const std::vector<CalleeSavedInfo> &CSI, 1111 const TargetRegisterInfo *TRI) const { 1112 1113 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 1114 // Return false otherwise to maintain pre-existing behavior. 1115 if (!Subtarget.isSVR4ABI()) 1116 return false; 1117 1118 MachineFunction *MF = MBB.getParent(); 1119 const PPCInstrInfo &TII = 1120 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 1121 DebugLoc DL; 1122 bool CRSpilled = false; 1123 1124 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1125 unsigned Reg = CSI[i].getReg(); 1126 // CR2 through CR4 are the nonvolatile CR fields. 1127 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4; 1128 1129 if (CRSpilled && IsCRField) 1130 continue; 1131 1132 // Add the callee-saved register as live-in; it's killed at the spill. 1133 MBB.addLiveIn(Reg); 1134 1135 // Insert the spill to the stack frame. 1136 if (IsCRField) { 1137 CRSpilled = true; 1138 // The first time we see a CR field, store the whole CR into the 1139 // save slot via GPR12 (available in the prolog for 32- and 64-bit). 1140 if (Subtarget.isPPC64()) { 1141 // 64-bit: SP+8 1142 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::X12)); 1143 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::STW)) 1144 .addReg(PPC::X12, 1145 getKillRegState(true)) 1146 .addImm(8) 1147 .addReg(PPC::X1)); 1148 } else { 1149 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have 1150 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot. 1151 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12)); 1152 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW)) 1153 .addReg(PPC::R12, 1154 getKillRegState(true)), 1155 CSI[i].getFrameIdx())); 1156 } 1157 1158 // Record that we spill the CR in this function. 1159 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 1160 FuncInfo->setSpillsCR(); 1161 } else { 1162 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 1163 TII.storeRegToStackSlot(MBB, MI, Reg, true, 1164 CSI[i].getFrameIdx(), RC, TRI); 1165 } 1166 } 1167 return true; 1168 } 1169 1170 static void 1171 restoreCRs(bool isPPC64, bool CR2Spilled, bool CR3Spilled, bool CR4Spilled, 1172 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, 1173 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) { 1174 1175 MachineFunction *MF = MBB.getParent(); 1176 const PPCInstrInfo &TII = 1177 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 1178 DebugLoc DL; 1179 unsigned RestoreOp, MoveReg; 1180 1181 if (isPPC64) { 1182 // 64-bit: SP+8 1183 MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::LWZ), PPC::X12) 1184 .addImm(8) 1185 .addReg(PPC::X1)); 1186 RestoreOp = PPC::MTCRF8; 1187 MoveReg = PPC::X12; 1188 } else { 1189 // 32-bit: FP-relative 1190 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ), 1191 PPC::R12), 1192 CSI[CSIIndex].getFrameIdx())); 1193 RestoreOp = PPC::MTCRF; 1194 MoveReg = PPC::R12; 1195 } 1196 1197 if (CR2Spilled) 1198 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2) 1199 .addReg(MoveReg)); 1200 1201 if (CR3Spilled) 1202 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3) 1203 .addReg(MoveReg)); 1204 1205 if (CR4Spilled) 1206 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4) 1207 .addReg(MoveReg)); 1208 } 1209 1210 void PPCFrameLowering:: 1211 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1212 MachineBasicBlock::iterator I) const { 1213 const PPCInstrInfo &TII = 1214 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 1215 if (MF.getTarget().Options.GuaranteedTailCallOpt && 1216 I->getOpcode() == PPC::ADJCALLSTACKUP) { 1217 // Add (actually subtract) back the amount the callee popped on return. 1218 if (int CalleeAmt = I->getOperand(1).getImm()) { 1219 bool is64Bit = Subtarget.isPPC64(); 1220 CalleeAmt *= -1; 1221 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1; 1222 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0; 1223 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI; 1224 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4; 1225 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS; 1226 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI; 1227 MachineInstr *MI = I; 1228 DebugLoc dl = MI->getDebugLoc(); 1229 1230 if (isInt<16>(CalleeAmt)) { 1231 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg) 1232 .addReg(StackReg, RegState::Kill) 1233 .addImm(CalleeAmt); 1234 } else { 1235 MachineBasicBlock::iterator MBBI = I; 1236 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 1237 .addImm(CalleeAmt >> 16); 1238 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 1239 .addReg(TmpReg, RegState::Kill) 1240 .addImm(CalleeAmt & 0xFFFF); 1241 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg) 1242 .addReg(StackReg, RegState::Kill) 1243 .addReg(TmpReg); 1244 } 1245 } 1246 } 1247 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions. 1248 MBB.erase(I); 1249 } 1250 1251 bool 1252 PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 1253 MachineBasicBlock::iterator MI, 1254 const std::vector<CalleeSavedInfo> &CSI, 1255 const TargetRegisterInfo *TRI) const { 1256 1257 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 1258 // Return false otherwise to maintain pre-existing behavior. 1259 if (!Subtarget.isSVR4ABI()) 1260 return false; 1261 1262 MachineFunction *MF = MBB.getParent(); 1263 const PPCInstrInfo &TII = 1264 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 1265 bool CR2Spilled = false; 1266 bool CR3Spilled = false; 1267 bool CR4Spilled = false; 1268 unsigned CSIIndex = 0; 1269 1270 // Initialize insertion-point logic; we will be restoring in reverse 1271 // order of spill. 1272 MachineBasicBlock::iterator I = MI, BeforeI = I; 1273 bool AtStart = I == MBB.begin(); 1274 1275 if (!AtStart) 1276 --BeforeI; 1277 1278 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1279 unsigned Reg = CSI[i].getReg(); 1280 1281 if (Reg == PPC::CR2) { 1282 CR2Spilled = true; 1283 // The spill slot is associated only with CR2, which is the 1284 // first nonvolatile spilled. Save it here. 1285 CSIIndex = i; 1286 continue; 1287 } else if (Reg == PPC::CR3) { 1288 CR3Spilled = true; 1289 continue; 1290 } else if (Reg == PPC::CR4) { 1291 CR4Spilled = true; 1292 continue; 1293 } else { 1294 // When we first encounter a non-CR register after seeing at 1295 // least one CR register, restore all spilled CRs together. 1296 if ((CR2Spilled || CR3Spilled || CR4Spilled) 1297 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 1298 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled, 1299 MBB, I, CSI, CSIIndex); 1300 CR2Spilled = CR3Spilled = CR4Spilled = false; 1301 } 1302 1303 // Default behavior for non-CR saves. 1304 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 1305 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), 1306 RC, TRI); 1307 assert(I != MBB.begin() && 1308 "loadRegFromStackSlot didn't insert any code!"); 1309 } 1310 1311 // Insert in reverse order. 1312 if (AtStart) 1313 I = MBB.begin(); 1314 else { 1315 I = BeforeI; 1316 ++I; 1317 } 1318 } 1319 1320 // If we haven't yet spilled the CRs, do so now. 1321 if (CR2Spilled || CR3Spilled || CR4Spilled) 1322 restoreCRs(Subtarget.isPPC64(), CR2Spilled, CR3Spilled, CR4Spilled, 1323 MBB, I, CSI, CSIIndex); 1324 1325 return true; 1326 } 1327 1328