LLVM API Documentation
00001 //===-- PPCFrameLowering.cpp - PPC Frame Information ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the PPC implementation of TargetFrameLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "PPCFrameLowering.h" 00015 #include "PPCInstrBuilder.h" 00016 #include "PPCInstrInfo.h" 00017 #include "PPCMachineFunctionInfo.h" 00018 #include "llvm/CodeGen/MachineFrameInfo.h" 00019 #include "llvm/CodeGen/MachineFunction.h" 00020 #include "llvm/CodeGen/MachineInstrBuilder.h" 00021 #include "llvm/CodeGen/MachineModuleInfo.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/RegisterScavenging.h" 00024 #include "llvm/IR/Function.h" 00025 #include "llvm/Target/TargetOptions.h" 00026 00027 using namespace llvm; 00028 00029 // FIXME This disables some code that aligns the stack to a boundary bigger than 00030 // the default (16 bytes on Darwin) when there is a stack local of greater 00031 // alignment. This does not currently work, because the delta between old and 00032 // new stack pointers is added to offsets that reference incoming parameters 00033 // after the prolog is generated, and the code that does that doesn't handle a 00034 // variable delta. You don't want to do that anyway; a better approach is to 00035 // reserve another register that retains to the incoming stack pointer, and 00036 // reference parameters relative to that. 00037 #define ALIGN_STACK 0 00038 00039 00040 /// VRRegNo - Map from a numbered VR register to its enum value. 00041 /// 00042 static const uint16_t VRRegNo[] = { 00043 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , 00044 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, 00045 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, 00046 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 00047 }; 00048 00049 /// RemoveVRSaveCode - We have found that this function does not need any code 00050 /// to manipulate the VRSAVE register, even though it uses vector registers. 00051 /// This can happen when the only registers used are known to be live in or out 00052 /// of the function. Remove all of the VRSAVE related code from the function. 00053 /// FIXME: The removal of the code results in a compile failure at -O0 when the 00054 /// function contains a function call, as the GPR containing original VRSAVE 00055 /// contents is spilled and reloaded around the call. Without the prolog code, 00056 /// the spill instruction refers to an undefined register. This code needs 00057 /// to account for all uses of that GPR. 00058 static void RemoveVRSaveCode(MachineInstr *MI) { 00059 MachineBasicBlock *Entry = MI->getParent(); 00060 MachineFunction *MF = Entry->getParent(); 00061 00062 // We know that the MTVRSAVE instruction immediately follows MI. Remove it. 00063 MachineBasicBlock::iterator MBBI = MI; 00064 ++MBBI; 00065 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); 00066 MBBI->eraseFromParent(); 00067 00068 bool RemovedAllMTVRSAVEs = true; 00069 // See if we can find and remove the MTVRSAVE instruction from all of the 00070 // epilog blocks. 00071 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { 00072 // If last instruction is a return instruction, add an epilogue 00073 if (!I->empty() && I->back().isReturn()) { 00074 bool FoundIt = false; 00075 for (MBBI = I->end(); MBBI != I->begin(); ) { 00076 --MBBI; 00077 if (MBBI->getOpcode() == PPC::MTVRSAVE) { 00078 MBBI->eraseFromParent(); // remove it. 00079 FoundIt = true; 00080 break; 00081 } 00082 } 00083 RemovedAllMTVRSAVEs &= FoundIt; 00084 } 00085 } 00086 00087 // If we found and removed all MTVRSAVE instructions, remove the read of 00088 // VRSAVE as well. 00089 if (RemovedAllMTVRSAVEs) { 00090 MBBI = MI; 00091 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); 00092 --MBBI; 00093 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); 00094 MBBI->eraseFromParent(); 00095 } 00096 00097 // Finally, nuke the UPDATE_VRSAVE. 00098 MI->eraseFromParent(); 00099 } 00100 00101 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the 00102 // instruction selector. Based on the vector registers that have been used, 00103 // transform this into the appropriate ORI instruction. 00104 static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) { 00105 MachineFunction *MF = MI->getParent()->getParent(); 00106 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 00107 DebugLoc dl = MI->getDebugLoc(); 00108 00109 unsigned UsedRegMask = 0; 00110 for (unsigned i = 0; i != 32; ++i) 00111 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i])) 00112 UsedRegMask |= 1 << (31-i); 00113 00114 // Live in and live out values already must be in the mask, so don't bother 00115 // marking them. 00116 for (MachineRegisterInfo::livein_iterator 00117 I = MF->getRegInfo().livein_begin(), 00118 E = MF->getRegInfo().livein_end(); I != E; ++I) { 00119 unsigned RegNo = TRI->getEncodingValue(I->first); 00120 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. 00121 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. 00122 } 00123 00124 // Live out registers appear as use operands on return instructions. 00125 for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end(); 00126 UsedRegMask != 0 && BI != BE; ++BI) { 00127 const MachineBasicBlock &MBB = *BI; 00128 if (MBB.empty() || !MBB.back().isReturn()) 00129 continue; 00130 const MachineInstr &Ret = MBB.back(); 00131 for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) { 00132 const MachineOperand &MO = Ret.getOperand(I); 00133 if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg())) 00134 continue; 00135 unsigned RegNo = TRI->getEncodingValue(MO.getReg()); 00136 UsedRegMask &= ~(1 << (31-RegNo)); 00137 } 00138 } 00139 00140 // If no registers are used, turn this into a copy. 00141 if (UsedRegMask == 0) { 00142 // Remove all VRSAVE code. 00143 RemoveVRSaveCode(MI); 00144 return; 00145 } 00146 00147 unsigned SrcReg = MI->getOperand(1).getReg(); 00148 unsigned DstReg = MI->getOperand(0).getReg(); 00149 00150 if ((UsedRegMask & 0xFFFF) == UsedRegMask) { 00151 if (DstReg != SrcReg) 00152 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 00153 .addReg(SrcReg) 00154 .addImm(UsedRegMask); 00155 else 00156 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 00157 .addReg(SrcReg, RegState::Kill) 00158 .addImm(UsedRegMask); 00159 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { 00160 if (DstReg != SrcReg) 00161 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 00162 .addReg(SrcReg) 00163 .addImm(UsedRegMask >> 16); 00164 else 00165 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 00166 .addReg(SrcReg, RegState::Kill) 00167 .addImm(UsedRegMask >> 16); 00168 } else { 00169 if (DstReg != SrcReg) 00170 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 00171 .addReg(SrcReg) 00172 .addImm(UsedRegMask >> 16); 00173 else 00174 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) 00175 .addReg(SrcReg, RegState::Kill) 00176 .addImm(UsedRegMask >> 16); 00177 00178 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) 00179 .addReg(DstReg, RegState::Kill) 00180 .addImm(UsedRegMask & 0xFFFF); 00181 } 00182 00183 // Remove the old UPDATE_VRSAVE instruction. 00184 MI->eraseFromParent(); 00185 } 00186 00187 static bool spillsCR(const MachineFunction &MF) { 00188 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 00189 return FuncInfo->isCRSpilled(); 00190 } 00191 00192 static bool spillsVRSAVE(const MachineFunction &MF) { 00193 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 00194 return FuncInfo->isVRSAVESpilled(); 00195 } 00196 00197 static bool hasSpills(const MachineFunction &MF) { 00198 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 00199 return FuncInfo->hasSpills(); 00200 } 00201 00202 static bool hasNonRISpills(const MachineFunction &MF) { 00203 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); 00204 return FuncInfo->hasNonRISpills(); 00205 } 00206 00207 /// determineFrameLayout - Determine the size of the frame and maximum call 00208 /// frame size. 00209 unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF, 00210 bool UpdateMF, 00211 bool UseEstimate) const { 00212 MachineFrameInfo *MFI = MF.getFrameInfo(); 00213 00214 // Get the number of bytes to allocate from the FrameInfo 00215 unsigned FrameSize = 00216 UseEstimate ? MFI->estimateStackSize(MF) : MFI->getStackSize(); 00217 00218 // Get the alignments provided by the target, and the maximum alignment 00219 // (if any) of the fixed frame objects. 00220 unsigned MaxAlign = MFI->getMaxAlignment(); 00221 unsigned TargetAlign = getStackAlignment(); 00222 unsigned AlignMask = TargetAlign - 1; // 00223 00224 // If we are a leaf function, and use up to 224 bytes of stack space, 00225 // don't have a frame pointer, calls, or dynamic alloca then we do not need 00226 // to adjust the stack pointer (we fit in the Red Zone). 00227 // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate 00228 // stackless code if all local vars are reg-allocated. 00229 bool DisableRedZone = MF.getFunction()->getAttributes(). 00230 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoRedZone); 00231 if (!DisableRedZone && 00232 (Subtarget.isPPC64() || // 32-bit SVR4, no stack- 00233 !Subtarget.isSVR4ABI() || // allocated locals. 00234 FrameSize == 0) && 00235 FrameSize <= 224 && // Fits in red zone. 00236 !MFI->hasVarSizedObjects() && // No dynamic alloca. 00237 !MFI->adjustsStack() && // No calls. 00238 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment. 00239 // No need for frame 00240 if (UpdateMF) 00241 MFI->setStackSize(0); 00242 return 0; 00243 } 00244 00245 // Get the maximum call frame size of all the calls. 00246 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); 00247 00248 // Maximum call frame needs to be at least big enough for linkage and 8 args. 00249 unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(), 00250 Subtarget.isDarwinABI()); 00251 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); 00252 00253 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so 00254 // that allocations will be aligned. 00255 if (MFI->hasVarSizedObjects()) 00256 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; 00257 00258 // Update maximum call frame size. 00259 if (UpdateMF) 00260 MFI->setMaxCallFrameSize(maxCallFrameSize); 00261 00262 // Include call frame size in total. 00263 FrameSize += maxCallFrameSize; 00264 00265 // Make sure the frame is aligned. 00266 FrameSize = (FrameSize + AlignMask) & ~AlignMask; 00267 00268 // Update frame info. 00269 if (UpdateMF) 00270 MFI->setStackSize(FrameSize); 00271 00272 return FrameSize; 00273 } 00274 00275 // hasFP - Return true if the specified function actually has a dedicated frame 00276 // pointer register. 00277 bool PPCFrameLowering::hasFP(const MachineFunction &MF) const { 00278 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00279 // FIXME: This is pretty much broken by design: hasFP() might be called really 00280 // early, before the stack layout was calculated and thus hasFP() might return 00281 // true or false here depending on the time of call. 00282 return (MFI->getStackSize()) && needsFP(MF); 00283 } 00284 00285 // needsFP - Return true if the specified function should have a dedicated frame 00286 // pointer register. This is true if the function has variable sized allocas or 00287 // if frame pointer elimination is disabled. 00288 bool PPCFrameLowering::needsFP(const MachineFunction &MF) const { 00289 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00290 00291 // Naked functions have no stack frame pushed, so we don't have a frame 00292 // pointer. 00293 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 00294 Attribute::Naked)) 00295 return false; 00296 00297 return MF.getTarget().Options.DisableFramePointerElim(MF) || 00298 MFI->hasVarSizedObjects() || 00299 (MF.getTarget().Options.GuaranteedTailCallOpt && 00300 MF.getInfo<PPCFunctionInfo>()->hasFastCall()); 00301 } 00302 00303 void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const { 00304 bool is31 = needsFP(MF); 00305 unsigned FPReg = is31 ? PPC::R31 : PPC::R1; 00306 unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1; 00307 00308 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); 00309 BI != BE; ++BI) 00310 for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) { 00311 --MBBI; 00312 for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) { 00313 MachineOperand &MO = MBBI->getOperand(I); 00314 if (!MO.isReg()) 00315 continue; 00316 00317 switch (MO.getReg()) { 00318 case PPC::FP: 00319 MO.setReg(FPReg); 00320 break; 00321 case PPC::FP8: 00322 MO.setReg(FP8Reg); 00323 break; 00324 } 00325 } 00326 } 00327 } 00328 00329 void PPCFrameLowering::emitPrologue(MachineFunction &MF) const { 00330 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB 00331 MachineBasicBlock::iterator MBBI = MBB.begin(); 00332 MachineFrameInfo *MFI = MF.getFrameInfo(); 00333 const PPCInstrInfo &TII = 00334 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 00335 00336 MachineModuleInfo &MMI = MF.getMMI(); 00337 const MCRegisterInfo &MRI = MMI.getContext().getRegisterInfo(); 00338 DebugLoc dl; 00339 bool needsFrameMoves = MMI.hasDebugInfo() || 00340 MF.getFunction()->needsUnwindTableEntry(); 00341 00342 // Prepare for frame info. 00343 MCSymbol *FrameLabel = 0; 00344 00345 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, 00346 // process it. 00347 if (!Subtarget.isSVR4ABI()) 00348 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { 00349 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { 00350 HandleVRSaveUpdate(MBBI, TII); 00351 break; 00352 } 00353 } 00354 00355 // Move MBBI back to the beginning of the function. 00356 MBBI = MBB.begin(); 00357 00358 // Work out frame sizes. 00359 unsigned FrameSize = determineFrameLayout(MF); 00360 int NegFrameSize = -FrameSize; 00361 00362 if (MFI->isFrameAddressTaken()) 00363 replaceFPWithRealFP(MF); 00364 00365 // Get processor type. 00366 bool isPPC64 = Subtarget.isPPC64(); 00367 // Get operating system 00368 bool isDarwinABI = Subtarget.isDarwinABI(); 00369 // Check if the link register (LR) must be saved. 00370 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 00371 bool MustSaveLR = FI->mustSaveLR(); 00372 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs(); 00373 // Do we have a frame pointer for this function? 00374 bool HasFP = hasFP(MF); 00375 00376 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 00377 00378 int FPOffset = 0; 00379 if (HasFP) { 00380 if (Subtarget.isSVR4ABI()) { 00381 MachineFrameInfo *FFI = MF.getFrameInfo(); 00382 int FPIndex = FI->getFramePointerSaveIndex(); 00383 assert(FPIndex && "No Frame Pointer Save Slot!"); 00384 FPOffset = FFI->getObjectOffset(FPIndex); 00385 } else { 00386 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 00387 } 00388 } 00389 00390 if (isPPC64) { 00391 if (MustSaveLR) 00392 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0); 00393 00394 if (!MustSaveCRs.empty()) { 00395 MachineInstrBuilder MIB = 00396 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFCR8), PPC::X12); 00397 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 00398 MIB.addReg(MustSaveCRs[i], RegState::ImplicitKill); 00399 } 00400 00401 if (HasFP) 00402 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 00403 .addReg(PPC::X31) 00404 .addImm(FPOffset) 00405 .addReg(PPC::X1); 00406 00407 if (MustSaveLR) 00408 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) 00409 .addReg(PPC::X0) 00410 .addImm(LROffset) 00411 .addReg(PPC::X1); 00412 00413 if (!MustSaveCRs.empty()) 00414 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8)) 00415 .addReg(PPC::X12, getKillRegState(true)) 00416 .addImm(8) 00417 .addReg(PPC::X1); 00418 } else { 00419 if (MustSaveLR) 00420 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0); 00421 00422 if (HasFP) 00423 // FIXME: On PPC32 SVR4, FPOffset is negative and access to negative 00424 // offsets of R1 is not allowed. 00425 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 00426 .addReg(PPC::R31) 00427 .addImm(FPOffset) 00428 .addReg(PPC::R1); 00429 00430 assert(MustSaveCRs.empty() && 00431 "Prologue CR saving supported only in 64-bit mode"); 00432 00433 if (MustSaveLR) 00434 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) 00435 .addReg(PPC::R0) 00436 .addImm(LROffset) 00437 .addReg(PPC::R1); 00438 } 00439 00440 // Skip if a leaf routine. 00441 if (!FrameSize) return; 00442 00443 // Get stack alignments. 00444 unsigned TargetAlign = getStackAlignment(); 00445 unsigned MaxAlign = MFI->getMaxAlignment(); 00446 00447 // Adjust stack pointer: r1 += NegFrameSize. 00448 // If there is a preferred stack alignment, align R1 now 00449 if (!isPPC64) { 00450 // PPC32. 00451 if (ALIGN_STACK && MaxAlign > TargetAlign) { 00452 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 00453 "Invalid alignment!"); 00454 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 00455 00456 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0) 00457 .addReg(PPC::R1) 00458 .addImm(0) 00459 .addImm(32 - Log2_32(MaxAlign)) 00460 .addImm(31); 00461 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0) 00462 .addReg(PPC::R0, RegState::Kill) 00463 .addImm(NegFrameSize); 00464 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1) 00465 .addReg(PPC::R1, RegState::Kill) 00466 .addReg(PPC::R1) 00467 .addReg(PPC::R0); 00468 } else if (isInt<16>(NegFrameSize)) { 00469 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1) 00470 .addReg(PPC::R1) 00471 .addImm(NegFrameSize) 00472 .addReg(PPC::R1); 00473 } else { 00474 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 00475 .addImm(NegFrameSize >> 16); 00476 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 00477 .addReg(PPC::R0, RegState::Kill) 00478 .addImm(NegFrameSize & 0xFFFF); 00479 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX), PPC::R1) 00480 .addReg(PPC::R1, RegState::Kill) 00481 .addReg(PPC::R1) 00482 .addReg(PPC::R0); 00483 } 00484 } else { // PPC64. 00485 if (ALIGN_STACK && MaxAlign > TargetAlign) { 00486 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && 00487 "Invalid alignment!"); 00488 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); 00489 00490 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0) 00491 .addReg(PPC::X1) 00492 .addImm(0) 00493 .addImm(64 - Log2_32(MaxAlign)); 00494 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0) 00495 .addReg(PPC::X0) 00496 .addImm(NegFrameSize); 00497 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1) 00498 .addReg(PPC::X1, RegState::Kill) 00499 .addReg(PPC::X1) 00500 .addReg(PPC::X0); 00501 } else if (isInt<16>(NegFrameSize)) { 00502 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1) 00503 .addReg(PPC::X1) 00504 .addImm(NegFrameSize) 00505 .addReg(PPC::X1); 00506 } else { 00507 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 00508 .addImm(NegFrameSize >> 16); 00509 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 00510 .addReg(PPC::X0, RegState::Kill) 00511 .addImm(NegFrameSize & 0xFFFF); 00512 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX), PPC::X1) 00513 .addReg(PPC::X1, RegState::Kill) 00514 .addReg(PPC::X1) 00515 .addReg(PPC::X0); 00516 } 00517 } 00518 00519 // Add the "machine moves" for the instructions we generated above, but in 00520 // reverse order. 00521 if (needsFrameMoves) { 00522 // Mark effective beginning of when frame pointer becomes valid. 00523 FrameLabel = MMI.getContext().CreateTempSymbol(); 00524 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel); 00525 00526 // Show update of SP. 00527 assert(NegFrameSize); 00528 MMI.addFrameInst( 00529 MCCFIInstruction::createDefCfaOffset(FrameLabel, NegFrameSize)); 00530 00531 if (HasFP) { 00532 unsigned Reg = isPPC64 ? PPC::X31 : PPC::R31; 00533 Reg = MRI.getDwarfRegNum(Reg, true); 00534 MMI.addFrameInst( 00535 MCCFIInstruction::createOffset(FrameLabel, Reg, FPOffset)); 00536 } 00537 00538 if (MustSaveLR) { 00539 unsigned Reg = isPPC64 ? PPC::LR8 : PPC::LR; 00540 Reg = MRI.getDwarfRegNum(Reg, true); 00541 MMI.addFrameInst( 00542 MCCFIInstruction::createOffset(FrameLabel, Reg, LROffset)); 00543 } 00544 } 00545 00546 MCSymbol *ReadyLabel = 0; 00547 00548 // If there is a frame pointer, copy R1 into R31 00549 if (HasFP) { 00550 if (!isPPC64) { 00551 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31) 00552 .addReg(PPC::R1) 00553 .addReg(PPC::R1); 00554 } else { 00555 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31) 00556 .addReg(PPC::X1) 00557 .addReg(PPC::X1); 00558 } 00559 00560 if (needsFrameMoves) { 00561 ReadyLabel = MMI.getContext().CreateTempSymbol(); 00562 00563 // Mark effective beginning of when frame pointer is ready. 00564 BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel); 00565 00566 unsigned Reg = HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) 00567 : (isPPC64 ? PPC::X1 : PPC::R1); 00568 Reg = MRI.getDwarfRegNum(Reg, true); 00569 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(ReadyLabel, Reg)); 00570 } 00571 } 00572 00573 if (needsFrameMoves) { 00574 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel; 00575 00576 // Add callee saved registers to move list. 00577 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 00578 for (unsigned I = 0, E = CSI.size(); I != E; ++I) { 00579 unsigned Reg = CSI[I].getReg(); 00580 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; 00581 00582 // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just 00583 // subregisters of CR2. We just need to emit a move of CR2. 00584 if (PPC::CRBITRCRegClass.contains(Reg)) 00585 continue; 00586 00587 // For SVR4, don't emit a move for the CR spill slot if we haven't 00588 // spilled CRs. 00589 if (Subtarget.isSVR4ABI() 00590 && (PPC::CR2 <= Reg && Reg <= PPC::CR4) 00591 && MustSaveCRs.empty()) 00592 continue; 00593 00594 // For 64-bit SVR4 when we have spilled CRs, the spill location 00595 // is SP+8, not a frame-relative slot. 00596 if (Subtarget.isSVR4ABI() 00597 && Subtarget.isPPC64() 00598 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 00599 MMI.addFrameInst(MCCFIInstruction::createOffset( 00600 Label, MRI.getDwarfRegNum(PPC::CR2, true), 8)); 00601 continue; 00602 } 00603 00604 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); 00605 MMI.addFrameInst(MCCFIInstruction::createOffset( 00606 Label, MRI.getDwarfRegNum(Reg, true), Offset)); 00607 } 00608 } 00609 } 00610 00611 void PPCFrameLowering::emitEpilogue(MachineFunction &MF, 00612 MachineBasicBlock &MBB) const { 00613 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 00614 assert(MBBI != MBB.end() && "Returning block has no terminator"); 00615 const PPCInstrInfo &TII = 00616 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 00617 00618 unsigned RetOpcode = MBBI->getOpcode(); 00619 DebugLoc dl; 00620 00621 assert((RetOpcode == PPC::BLR || 00622 RetOpcode == PPC::TCRETURNri || 00623 RetOpcode == PPC::TCRETURNdi || 00624 RetOpcode == PPC::TCRETURNai || 00625 RetOpcode == PPC::TCRETURNri8 || 00626 RetOpcode == PPC::TCRETURNdi8 || 00627 RetOpcode == PPC::TCRETURNai8) && 00628 "Can only insert epilog into returning blocks"); 00629 00630 // Get alignment info so we know how to restore r1 00631 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00632 unsigned TargetAlign = getStackAlignment(); 00633 unsigned MaxAlign = MFI->getMaxAlignment(); 00634 00635 // Get the number of bytes allocated from the FrameInfo. 00636 int FrameSize = MFI->getStackSize(); 00637 00638 // Get processor type. 00639 bool isPPC64 = Subtarget.isPPC64(); 00640 // Get operating system 00641 bool isDarwinABI = Subtarget.isDarwinABI(); 00642 // Check if the link register (LR) has been saved. 00643 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 00644 bool MustSaveLR = FI->mustSaveLR(); 00645 const SmallVector<unsigned, 3> &MustSaveCRs = FI->getMustSaveCRs(); 00646 // Do we have a frame pointer for this function? 00647 bool HasFP = hasFP(MF); 00648 00649 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); 00650 00651 int FPOffset = 0; 00652 if (HasFP) { 00653 if (Subtarget.isSVR4ABI()) { 00654 MachineFrameInfo *FFI = MF.getFrameInfo(); 00655 int FPIndex = FI->getFramePointerSaveIndex(); 00656 assert(FPIndex && "No Frame Pointer Save Slot!"); 00657 FPOffset = FFI->getObjectOffset(FPIndex); 00658 } else { 00659 FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); 00660 } 00661 } 00662 00663 bool UsesTCRet = RetOpcode == PPC::TCRETURNri || 00664 RetOpcode == PPC::TCRETURNdi || 00665 RetOpcode == PPC::TCRETURNai || 00666 RetOpcode == PPC::TCRETURNri8 || 00667 RetOpcode == PPC::TCRETURNdi8 || 00668 RetOpcode == PPC::TCRETURNai8; 00669 00670 if (UsesTCRet) { 00671 int MaxTCRetDelta = FI->getTailCallSPDelta(); 00672 MachineOperand &StackAdjust = MBBI->getOperand(1); 00673 assert(StackAdjust.isImm() && "Expecting immediate value."); 00674 // Adjust stack pointer. 00675 int StackAdj = StackAdjust.getImm(); 00676 int Delta = StackAdj - MaxTCRetDelta; 00677 assert((Delta >= 0) && "Delta must be positive"); 00678 if (MaxTCRetDelta>0) 00679 FrameSize += (StackAdj +Delta); 00680 else 00681 FrameSize += StackAdj; 00682 } 00683 00684 if (FrameSize) { 00685 // The loaded (or persistent) stack pointer value is offset by the 'stwu' 00686 // on entry to the function. Add this offset back now. 00687 if (!isPPC64) { 00688 // If this function contained a fastcc call and GuaranteedTailCallOpt is 00689 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail 00690 // call which invalidates the stack pointer value in SP(0). So we use the 00691 // value of R31 in this case. 00692 if (FI->hasFastCall() && isInt<16>(FrameSize)) { 00693 assert(hasFP(MF) && "Expecting a valid the frame pointer."); 00694 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 00695 .addReg(PPC::R31).addImm(FrameSize); 00696 } else if(FI->hasFastCall()) { 00697 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) 00698 .addImm(FrameSize >> 16); 00699 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) 00700 .addReg(PPC::R0, RegState::Kill) 00701 .addImm(FrameSize & 0xFFFF); 00702 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4)) 00703 .addReg(PPC::R1) 00704 .addReg(PPC::R31) 00705 .addReg(PPC::R0); 00706 } else if (isInt<16>(FrameSize) && 00707 (!ALIGN_STACK || TargetAlign >= MaxAlign) && 00708 !MFI->hasVarSizedObjects()) { 00709 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) 00710 .addReg(PPC::R1).addImm(FrameSize); 00711 } else { 00712 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1) 00713 .addImm(0).addReg(PPC::R1); 00714 } 00715 } else { 00716 if (FI->hasFastCall() && isInt<16>(FrameSize)) { 00717 assert(hasFP(MF) && "Expecting a valid the frame pointer."); 00718 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 00719 .addReg(PPC::X31).addImm(FrameSize); 00720 } else if(FI->hasFastCall()) { 00721 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) 00722 .addImm(FrameSize >> 16); 00723 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) 00724 .addReg(PPC::X0, RegState::Kill) 00725 .addImm(FrameSize & 0xFFFF); 00726 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8)) 00727 .addReg(PPC::X1) 00728 .addReg(PPC::X31) 00729 .addReg(PPC::X0); 00730 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign && 00731 !MFI->hasVarSizedObjects()) { 00732 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) 00733 .addReg(PPC::X1).addImm(FrameSize); 00734 } else { 00735 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1) 00736 .addImm(0).addReg(PPC::X1); 00737 } 00738 } 00739 } 00740 00741 if (isPPC64) { 00742 if (MustSaveLR) 00743 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0) 00744 .addImm(LROffset).addReg(PPC::X1); 00745 00746 if (!MustSaveCRs.empty()) 00747 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), PPC::X12) 00748 .addImm(8).addReg(PPC::X1); 00749 00750 if (HasFP) 00751 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31) 00752 .addImm(FPOffset).addReg(PPC::X1); 00753 00754 if (!MustSaveCRs.empty()) 00755 for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) 00756 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTCRF8), MustSaveCRs[i]) 00757 .addReg(PPC::X12, getKillRegState(i == e-1)); 00758 00759 if (MustSaveLR) 00760 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0); 00761 } else { 00762 if (MustSaveLR) 00763 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0) 00764 .addImm(LROffset).addReg(PPC::R1); 00765 00766 assert(MustSaveCRs.empty() && 00767 "Epilogue CR restoring supported only in 64-bit mode"); 00768 00769 if (HasFP) 00770 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31) 00771 .addImm(FPOffset).addReg(PPC::R1); 00772 00773 if (MustSaveLR) 00774 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0); 00775 } 00776 00777 // Callee pop calling convention. Pop parameter/linkage area. Used for tail 00778 // call optimization 00779 if (MF.getTarget().Options.GuaranteedTailCallOpt && RetOpcode == PPC::BLR && 00780 MF.getFunction()->getCallingConv() == CallingConv::Fast) { 00781 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 00782 unsigned CallerAllocatedAmt = FI->getMinReservedArea(); 00783 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1; 00784 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; 00785 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0; 00786 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI; 00787 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4; 00788 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS; 00789 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI; 00790 00791 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { 00792 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg) 00793 .addReg(StackReg).addImm(CallerAllocatedAmt); 00794 } else { 00795 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 00796 .addImm(CallerAllocatedAmt >> 16); 00797 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 00798 .addReg(TmpReg, RegState::Kill) 00799 .addImm(CallerAllocatedAmt & 0xFFFF); 00800 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr)) 00801 .addReg(StackReg) 00802 .addReg(FPReg) 00803 .addReg(TmpReg); 00804 } 00805 } else if (RetOpcode == PPC::TCRETURNdi) { 00806 MBBI = MBB.getLastNonDebugInstr(); 00807 MachineOperand &JumpTarget = MBBI->getOperand(0); 00808 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). 00809 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 00810 } else if (RetOpcode == PPC::TCRETURNri) { 00811 MBBI = MBB.getLastNonDebugInstr(); 00812 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 00813 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); 00814 } else if (RetOpcode == PPC::TCRETURNai) { 00815 MBBI = MBB.getLastNonDebugInstr(); 00816 MachineOperand &JumpTarget = MBBI->getOperand(0); 00817 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); 00818 } else if (RetOpcode == PPC::TCRETURNdi8) { 00819 MBBI = MBB.getLastNonDebugInstr(); 00820 MachineOperand &JumpTarget = MBBI->getOperand(0); 00821 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). 00822 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); 00823 } else if (RetOpcode == PPC::TCRETURNri8) { 00824 MBBI = MBB.getLastNonDebugInstr(); 00825 assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); 00826 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); 00827 } else if (RetOpcode == PPC::TCRETURNai8) { 00828 MBBI = MBB.getLastNonDebugInstr(); 00829 MachineOperand &JumpTarget = MBBI->getOperand(0); 00830 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); 00831 } 00832 } 00833 00834 /// MustSaveLR - Return true if this function requires that we save the LR 00835 /// register onto the stack in the prolog and restore it in the epilog of the 00836 /// function. 00837 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) { 00838 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); 00839 00840 // We need a save/restore of LR if there is any def of LR (which is 00841 // defined by calls, including the PIC setup sequence), or if there is 00842 // some use of the LR stack slot (e.g. for builtin_return_address). 00843 // (LR comes in 32 and 64 bit versions.) 00844 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); 00845 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); 00846 } 00847 00848 void 00849 PPCFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 00850 RegScavenger *) const { 00851 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 00852 00853 // Save and clear the LR state. 00854 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); 00855 unsigned LR = RegInfo->getRARegister(); 00856 FI->setMustSaveLR(MustSaveLR(MF, LR)); 00857 MachineRegisterInfo &MRI = MF.getRegInfo(); 00858 MRI.setPhysRegUnused(LR); 00859 00860 // Save R31 if necessary 00861 int FPSI = FI->getFramePointerSaveIndex(); 00862 bool isPPC64 = Subtarget.isPPC64(); 00863 bool isDarwinABI = Subtarget.isDarwinABI(); 00864 MachineFrameInfo *MFI = MF.getFrameInfo(); 00865 00866 // If the frame pointer save index hasn't been defined yet. 00867 if (!FPSI && needsFP(MF)) { 00868 // Find out what the fix offset of the frame pointer save area. 00869 int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI); 00870 // Allocate the frame index for frame pointer save area. 00871 FPSI = MFI->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); 00872 // Save the result. 00873 FI->setFramePointerSaveIndex(FPSI); 00874 } 00875 00876 // Reserve stack space to move the linkage area to in case of a tail call. 00877 int TCSPDelta = 0; 00878 if (MF.getTarget().Options.GuaranteedTailCallOpt && 00879 (TCSPDelta = FI->getTailCallSPDelta()) < 0) { 00880 MFI->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); 00881 } 00882 00883 // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the 00884 // function uses CR 2, 3, or 4. 00885 if (!isPPC64 && !isDarwinABI && 00886 (MRI.isPhysRegUsed(PPC::CR2) || 00887 MRI.isPhysRegUsed(PPC::CR3) || 00888 MRI.isPhysRegUsed(PPC::CR4))) { 00889 int FrameIdx = MFI->CreateFixedObject((uint64_t)4, (int64_t)-4, true); 00890 FI->setCRSpillFrameIndex(FrameIdx); 00891 } 00892 } 00893 00894 void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, 00895 RegScavenger *RS) const { 00896 // Early exit if not using the SVR4 ABI. 00897 if (!Subtarget.isSVR4ABI()) { 00898 addScavengingSpillSlot(MF, RS); 00899 return; 00900 } 00901 00902 // Get callee saved register information. 00903 MachineFrameInfo *FFI = MF.getFrameInfo(); 00904 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo(); 00905 00906 // Early exit if no callee saved registers are modified! 00907 if (CSI.empty() && !needsFP(MF)) { 00908 addScavengingSpillSlot(MF, RS); 00909 return; 00910 } 00911 00912 unsigned MinGPR = PPC::R31; 00913 unsigned MinG8R = PPC::X31; 00914 unsigned MinFPR = PPC::F31; 00915 unsigned MinVR = PPC::V31; 00916 00917 bool HasGPSaveArea = false; 00918 bool HasG8SaveArea = false; 00919 bool HasFPSaveArea = false; 00920 bool HasVRSAVESaveArea = false; 00921 bool HasVRSaveArea = false; 00922 00923 SmallVector<CalleeSavedInfo, 18> GPRegs; 00924 SmallVector<CalleeSavedInfo, 18> G8Regs; 00925 SmallVector<CalleeSavedInfo, 18> FPRegs; 00926 SmallVector<CalleeSavedInfo, 18> VRegs; 00927 00928 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 00929 unsigned Reg = CSI[i].getReg(); 00930 if (PPC::GPRCRegClass.contains(Reg)) { 00931 HasGPSaveArea = true; 00932 00933 GPRegs.push_back(CSI[i]); 00934 00935 if (Reg < MinGPR) { 00936 MinGPR = Reg; 00937 } 00938 } else if (PPC::G8RCRegClass.contains(Reg)) { 00939 HasG8SaveArea = true; 00940 00941 G8Regs.push_back(CSI[i]); 00942 00943 if (Reg < MinG8R) { 00944 MinG8R = Reg; 00945 } 00946 } else if (PPC::F8RCRegClass.contains(Reg)) { 00947 HasFPSaveArea = true; 00948 00949 FPRegs.push_back(CSI[i]); 00950 00951 if (Reg < MinFPR) { 00952 MinFPR = Reg; 00953 } 00954 } else if (PPC::CRBITRCRegClass.contains(Reg) || 00955 PPC::CRRCRegClass.contains(Reg)) { 00956 ; // do nothing, as we already know whether CRs are spilled 00957 } else if (PPC::VRSAVERCRegClass.contains(Reg)) { 00958 HasVRSAVESaveArea = true; 00959 } else if (PPC::VRRCRegClass.contains(Reg)) { 00960 HasVRSaveArea = true; 00961 00962 VRegs.push_back(CSI[i]); 00963 00964 if (Reg < MinVR) { 00965 MinVR = Reg; 00966 } 00967 } else { 00968 llvm_unreachable("Unknown RegisterClass!"); 00969 } 00970 } 00971 00972 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); 00973 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); 00974 00975 int64_t LowerBound = 0; 00976 00977 // Take into account stack space reserved for tail calls. 00978 int TCSPDelta = 0; 00979 if (MF.getTarget().Options.GuaranteedTailCallOpt && 00980 (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { 00981 LowerBound = TCSPDelta; 00982 } 00983 00984 // The Floating-point register save area is right below the back chain word 00985 // of the previous stack frame. 00986 if (HasFPSaveArea) { 00987 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { 00988 int FI = FPRegs[i].getFrameIdx(); 00989 00990 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 00991 } 00992 00993 LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8; 00994 } 00995 00996 // Check whether the frame pointer register is allocated. If so, make sure it 00997 // is spilled to the correct offset. 00998 if (needsFP(MF)) { 00999 HasGPSaveArea = true; 01000 01001 int FI = PFI->getFramePointerSaveIndex(); 01002 assert(FI && "No Frame Pointer Save Slot!"); 01003 01004 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01005 } 01006 01007 // General register save area starts right below the Floating-point 01008 // register save area. 01009 if (HasGPSaveArea || HasG8SaveArea) { 01010 // Move general register save area spill slots down, taking into account 01011 // the size of the Floating-point register save area. 01012 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { 01013 int FI = GPRegs[i].getFrameIdx(); 01014 01015 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01016 } 01017 01018 // Move general register save area spill slots down, taking into account 01019 // the size of the Floating-point register save area. 01020 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { 01021 int FI = G8Regs[i].getFrameIdx(); 01022 01023 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01024 } 01025 01026 unsigned MinReg = 01027 std::min<unsigned>(TRI->getEncodingValue(MinGPR), 01028 TRI->getEncodingValue(MinG8R)); 01029 01030 if (Subtarget.isPPC64()) { 01031 LowerBound -= (31 - MinReg + 1) * 8; 01032 } else { 01033 LowerBound -= (31 - MinReg + 1) * 4; 01034 } 01035 } 01036 01037 // For 32-bit only, the CR save area is below the general register 01038 // save area. For 64-bit SVR4, the CR save area is addressed relative 01039 // to the stack pointer and hence does not need an adjustment here. 01040 // Only CR2 (the first nonvolatile spilled) has an associated frame 01041 // index so that we have a single uniform save area. 01042 if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) { 01043 // Adjust the frame index of the CR spill slot. 01044 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 01045 unsigned Reg = CSI[i].getReg(); 01046 01047 if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2) 01048 // Leave Darwin logic as-is. 01049 || (!Subtarget.isSVR4ABI() && 01050 (PPC::CRBITRCRegClass.contains(Reg) || 01051 PPC::CRRCRegClass.contains(Reg)))) { 01052 int FI = CSI[i].getFrameIdx(); 01053 01054 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01055 } 01056 } 01057 01058 LowerBound -= 4; // The CR save area is always 4 bytes long. 01059 } 01060 01061 if (HasVRSAVESaveArea) { 01062 // FIXME SVR4: Is it actually possible to have multiple elements in CSI 01063 // which have the VRSAVE register class? 01064 // Adjust the frame index of the VRSAVE spill slot. 01065 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 01066 unsigned Reg = CSI[i].getReg(); 01067 01068 if (PPC::VRSAVERCRegClass.contains(Reg)) { 01069 int FI = CSI[i].getFrameIdx(); 01070 01071 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01072 } 01073 } 01074 01075 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. 01076 } 01077 01078 if (HasVRSaveArea) { 01079 // Insert alignment padding, we need 16-byte alignment. 01080 LowerBound = (LowerBound - 15) & ~(15); 01081 01082 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { 01083 int FI = VRegs[i].getFrameIdx(); 01084 01085 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); 01086 } 01087 } 01088 01089 addScavengingSpillSlot(MF, RS); 01090 } 01091 01092 void 01093 PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF, 01094 RegScavenger *RS) const { 01095 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or 01096 // a large stack, which will require scavenging a register to materialize a 01097 // large offset. 01098 01099 // We need to have a scavenger spill slot for spills if the frame size is 01100 // large. In case there is no free register for large-offset addressing, 01101 // this slot is used for the necessary emergency spill. Also, we need the 01102 // slot for dynamic stack allocations. 01103 01104 // The scavenger might be invoked if the frame offset does not fit into 01105 // the 16-bit immediate. We don't know the complete frame size here 01106 // because we've not yet computed callee-saved register spills or the 01107 // needed alignment padding. 01108 unsigned StackSize = determineFrameLayout(MF, false, true); 01109 MachineFrameInfo *MFI = MF.getFrameInfo(); 01110 if (MFI->hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) || 01111 hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) { 01112 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; 01113 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; 01114 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC; 01115 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 01116 RC->getAlignment(), 01117 false)); 01118 01119 // These kinds of spills might need two registers. 01120 if (spillsCR(MF) || spillsVRSAVE(MF)) 01121 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 01122 RC->getAlignment(), 01123 false)); 01124 01125 } 01126 } 01127 01128 bool 01129 PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 01130 MachineBasicBlock::iterator MI, 01131 const std::vector<CalleeSavedInfo> &CSI, 01132 const TargetRegisterInfo *TRI) const { 01133 01134 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 01135 // Return false otherwise to maintain pre-existing behavior. 01136 if (!Subtarget.isSVR4ABI()) 01137 return false; 01138 01139 MachineFunction *MF = MBB.getParent(); 01140 const PPCInstrInfo &TII = 01141 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 01142 DebugLoc DL; 01143 bool CRSpilled = false; 01144 MachineInstrBuilder CRMIB; 01145 01146 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 01147 unsigned Reg = CSI[i].getReg(); 01148 // CR2 through CR4 are the nonvolatile CR fields. 01149 bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4; 01150 01151 // Add the callee-saved register as live-in; it's killed at the spill. 01152 MBB.addLiveIn(Reg); 01153 01154 if (CRSpilled && IsCRField) { 01155 CRMIB.addReg(Reg, RegState::ImplicitKill); 01156 continue; 01157 } 01158 01159 // Insert the spill to the stack frame. 01160 if (IsCRField) { 01161 PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); 01162 if (Subtarget.isPPC64()) { 01163 // The actual spill will happen at the start of the prologue. 01164 FuncInfo->addMustSaveCR(Reg); 01165 } else { 01166 CRSpilled = true; 01167 FuncInfo->setSpillsCR(); 01168 01169 // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have 01170 // the same frame index in PPCRegisterInfo::hasReservedSpillSlot. 01171 CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12) 01172 .addReg(Reg, RegState::ImplicitKill); 01173 01174 MBB.insert(MI, CRMIB); 01175 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW)) 01176 .addReg(PPC::R12, 01177 getKillRegState(true)), 01178 CSI[i].getFrameIdx())); 01179 } 01180 } else { 01181 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 01182 TII.storeRegToStackSlot(MBB, MI, Reg, true, 01183 CSI[i].getFrameIdx(), RC, TRI); 01184 } 01185 } 01186 return true; 01187 } 01188 01189 static void 01190 restoreCRs(bool isPPC64, bool is31, 01191 bool CR2Spilled, bool CR3Spilled, bool CR4Spilled, 01192 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, 01193 const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) { 01194 01195 MachineFunction *MF = MBB.getParent(); 01196 const PPCInstrInfo &TII = 01197 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 01198 DebugLoc DL; 01199 unsigned RestoreOp, MoveReg; 01200 01201 if (isPPC64) 01202 // This is handled during epilogue generation. 01203 return; 01204 else { 01205 // 32-bit: FP-relative 01206 MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ), 01207 PPC::R12), 01208 CSI[CSIIndex].getFrameIdx())); 01209 RestoreOp = PPC::MTCRF; 01210 MoveReg = PPC::R12; 01211 } 01212 01213 if (CR2Spilled) 01214 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2) 01215 .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled))); 01216 01217 if (CR3Spilled) 01218 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3) 01219 .addReg(MoveReg, getKillRegState(!CR4Spilled))); 01220 01221 if (CR4Spilled) 01222 MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4) 01223 .addReg(MoveReg, getKillRegState(true))); 01224 } 01225 01226 void PPCFrameLowering:: 01227 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 01228 MachineBasicBlock::iterator I) const { 01229 const PPCInstrInfo &TII = 01230 *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); 01231 if (MF.getTarget().Options.GuaranteedTailCallOpt && 01232 I->getOpcode() == PPC::ADJCALLSTACKUP) { 01233 // Add (actually subtract) back the amount the callee popped on return. 01234 if (int CalleeAmt = I->getOperand(1).getImm()) { 01235 bool is64Bit = Subtarget.isPPC64(); 01236 CalleeAmt *= -1; 01237 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1; 01238 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0; 01239 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI; 01240 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4; 01241 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS; 01242 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI; 01243 MachineInstr *MI = I; 01244 DebugLoc dl = MI->getDebugLoc(); 01245 01246 if (isInt<16>(CalleeAmt)) { 01247 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg) 01248 .addReg(StackReg, RegState::Kill) 01249 .addImm(CalleeAmt); 01250 } else { 01251 MachineBasicBlock::iterator MBBI = I; 01252 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) 01253 .addImm(CalleeAmt >> 16); 01254 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) 01255 .addReg(TmpReg, RegState::Kill) 01256 .addImm(CalleeAmt & 0xFFFF); 01257 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg) 01258 .addReg(StackReg, RegState::Kill) 01259 .addReg(TmpReg); 01260 } 01261 } 01262 } 01263 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions. 01264 MBB.erase(I); 01265 } 01266 01267 bool 01268 PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 01269 MachineBasicBlock::iterator MI, 01270 const std::vector<CalleeSavedInfo> &CSI, 01271 const TargetRegisterInfo *TRI) const { 01272 01273 // Currently, this function only handles SVR4 32- and 64-bit ABIs. 01274 // Return false otherwise to maintain pre-existing behavior. 01275 if (!Subtarget.isSVR4ABI()) 01276 return false; 01277 01278 MachineFunction *MF = MBB.getParent(); 01279 const PPCInstrInfo &TII = 01280 *static_cast<const PPCInstrInfo*>(MF->getTarget().getInstrInfo()); 01281 bool CR2Spilled = false; 01282 bool CR3Spilled = false; 01283 bool CR4Spilled = false; 01284 unsigned CSIIndex = 0; 01285 01286 // Initialize insertion-point logic; we will be restoring in reverse 01287 // order of spill. 01288 MachineBasicBlock::iterator I = MI, BeforeI = I; 01289 bool AtStart = I == MBB.begin(); 01290 01291 if (!AtStart) 01292 --BeforeI; 01293 01294 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 01295 unsigned Reg = CSI[i].getReg(); 01296 01297 if (Reg == PPC::CR2) { 01298 CR2Spilled = true; 01299 // The spill slot is associated only with CR2, which is the 01300 // first nonvolatile spilled. Save it here. 01301 CSIIndex = i; 01302 continue; 01303 } else if (Reg == PPC::CR3) { 01304 CR3Spilled = true; 01305 continue; 01306 } else if (Reg == PPC::CR4) { 01307 CR4Spilled = true; 01308 continue; 01309 } else { 01310 // When we first encounter a non-CR register after seeing at 01311 // least one CR register, restore all spilled CRs together. 01312 if ((CR2Spilled || CR3Spilled || CR4Spilled) 01313 && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) { 01314 bool is31 = needsFP(*MF); 01315 restoreCRs(Subtarget.isPPC64(), is31, 01316 CR2Spilled, CR3Spilled, CR4Spilled, 01317 MBB, I, CSI, CSIIndex); 01318 CR2Spilled = CR3Spilled = CR4Spilled = false; 01319 } 01320 01321 // Default behavior for non-CR saves. 01322 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 01323 TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), 01324 RC, TRI); 01325 assert(I != MBB.begin() && 01326 "loadRegFromStackSlot didn't insert any code!"); 01327 } 01328 01329 // Insert in reverse order. 01330 if (AtStart) 01331 I = MBB.begin(); 01332 else { 01333 I = BeforeI; 01334 ++I; 01335 } 01336 } 01337 01338 // If we haven't yet spilled the CRs, do so now. 01339 if (CR2Spilled || CR3Spilled || CR4Spilled) { 01340 bool is31 = needsFP(*MF); 01341 restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled, 01342 MBB, I, CSI, CSIIndex); 01343 } 01344 01345 return true; 01346 } 01347