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