Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1 | //=======- PPCFrameInfo.cpp - PPC Frame Information ------------*- C++ -*-====// |
| 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 TargetFrameInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PPCFrameInfo.h" |
| 15 | #include "PPCInstrInfo.h" |
| 16 | #include "PPCMachineFunctionInfo.h" |
| 17 | #include "llvm/Function.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" |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/RegisterScavenging.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | // FIXME This disables some code that aligns the stack to a boundary bigger than |
| 29 | // the default (16 bytes on Darwin) when there is a stack local of greater |
| 30 | // alignment. This does not currently work, because the delta between old and |
| 31 | // new stack pointers is added to offsets that reference incoming parameters |
| 32 | // after the prolog is generated, and the code that does that doesn't handle a |
| 33 | // variable delta. You don't want to do that anyway; a better approach is to |
| 34 | // reserve another register that retains to the incoming stack pointer, and |
| 35 | // reference parameters relative to that. |
| 36 | #define ALIGN_STACK 0 |
| 37 | |
| 38 | |
| 39 | /// VRRegNo - Map from a numbered VR register to its enum value. |
| 40 | /// |
| 41 | static const unsigned short VRRegNo[] = { |
| 42 | PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , |
| 43 | PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, |
| 44 | PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, |
| 45 | PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 |
| 46 | }; |
| 47 | |
| 48 | /// RemoveVRSaveCode - We have found that this function does not need any code |
| 49 | /// to manipulate the VRSAVE register, even though it uses vector registers. |
| 50 | /// This can happen when the only registers used are known to be live in or out |
| 51 | /// of the function. Remove all of the VRSAVE related code from the function. |
| 52 | static void RemoveVRSaveCode(MachineInstr *MI) { |
| 53 | MachineBasicBlock *Entry = MI->getParent(); |
| 54 | MachineFunction *MF = Entry->getParent(); |
| 55 | |
| 56 | // We know that the MTVRSAVE instruction immediately follows MI. Remove it. |
| 57 | MachineBasicBlock::iterator MBBI = MI; |
| 58 | ++MBBI; |
| 59 | assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); |
| 60 | MBBI->eraseFromParent(); |
| 61 | |
| 62 | bool RemovedAllMTVRSAVEs = true; |
| 63 | // See if we can find and remove the MTVRSAVE instruction from all of the |
| 64 | // epilog blocks. |
| 65 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { |
| 66 | // If last instruction is a return instruction, add an epilogue |
| 67 | if (!I->empty() && I->back().getDesc().isReturn()) { |
| 68 | bool FoundIt = false; |
| 69 | for (MBBI = I->end(); MBBI != I->begin(); ) { |
| 70 | --MBBI; |
| 71 | if (MBBI->getOpcode() == PPC::MTVRSAVE) { |
| 72 | MBBI->eraseFromParent(); // remove it. |
| 73 | FoundIt = true; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | RemovedAllMTVRSAVEs &= FoundIt; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // If we found and removed all MTVRSAVE instructions, remove the read of |
| 82 | // VRSAVE as well. |
| 83 | if (RemovedAllMTVRSAVEs) { |
| 84 | MBBI = MI; |
| 85 | assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); |
| 86 | --MBBI; |
| 87 | assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); |
| 88 | MBBI->eraseFromParent(); |
| 89 | } |
| 90 | |
| 91 | // Finally, nuke the UPDATE_VRSAVE. |
| 92 | MI->eraseFromParent(); |
| 93 | } |
| 94 | |
| 95 | // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the |
| 96 | // instruction selector. Based on the vector registers that have been used, |
| 97 | // transform this into the appropriate ORI instruction. |
| 98 | static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) { |
| 99 | MachineFunction *MF = MI->getParent()->getParent(); |
| 100 | DebugLoc dl = MI->getDebugLoc(); |
| 101 | |
| 102 | unsigned UsedRegMask = 0; |
| 103 | for (unsigned i = 0; i != 32; ++i) |
| 104 | if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i])) |
| 105 | UsedRegMask |= 1 << (31-i); |
| 106 | |
| 107 | // Live in and live out values already must be in the mask, so don't bother |
| 108 | // marking them. |
| 109 | for (MachineRegisterInfo::livein_iterator |
| 110 | I = MF->getRegInfo().livein_begin(), |
| 111 | E = MF->getRegInfo().livein_end(); I != E; ++I) { |
| 112 | unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first); |
| 113 | if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. |
| 114 | UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. |
| 115 | } |
| 116 | for (MachineRegisterInfo::liveout_iterator |
| 117 | I = MF->getRegInfo().liveout_begin(), |
| 118 | E = MF->getRegInfo().liveout_end(); I != E; ++I) { |
| 119 | unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I); |
| 120 | if (VRRegNo[RegNo] == *I) // If this really is a vector reg. |
| 121 | UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. |
| 122 | } |
| 123 | |
| 124 | // If no registers are used, turn this into a copy. |
| 125 | if (UsedRegMask == 0) { |
| 126 | // Remove all VRSAVE code. |
| 127 | RemoveVRSaveCode(MI); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 132 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 133 | |
| 134 | if ((UsedRegMask & 0xFFFF) == UsedRegMask) { |
| 135 | if (DstReg != SrcReg) |
| 136 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 137 | .addReg(SrcReg) |
| 138 | .addImm(UsedRegMask); |
| 139 | else |
| 140 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 141 | .addReg(SrcReg, RegState::Kill) |
| 142 | .addImm(UsedRegMask); |
| 143 | } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { |
| 144 | if (DstReg != SrcReg) |
| 145 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 146 | .addReg(SrcReg) |
| 147 | .addImm(UsedRegMask >> 16); |
| 148 | else |
| 149 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 150 | .addReg(SrcReg, RegState::Kill) |
| 151 | .addImm(UsedRegMask >> 16); |
| 152 | } else { |
| 153 | if (DstReg != SrcReg) |
| 154 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 155 | .addReg(SrcReg) |
| 156 | .addImm(UsedRegMask >> 16); |
| 157 | else |
| 158 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 159 | .addReg(SrcReg, RegState::Kill) |
| 160 | .addImm(UsedRegMask >> 16); |
| 161 | |
| 162 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 163 | .addReg(DstReg, RegState::Kill) |
| 164 | .addImm(UsedRegMask & 0xFFFF); |
| 165 | } |
| 166 | |
| 167 | // Remove the old UPDATE_VRSAVE instruction. |
| 168 | MI->eraseFromParent(); |
| 169 | } |
| 170 | |
| 171 | /// determineFrameLayout - Determine the size of the frame and maximum call |
| 172 | /// frame size. |
| 173 | void PPCFrameInfo::determineFrameLayout(MachineFunction &MF) const { |
| 174 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 175 | |
| 176 | // Get the number of bytes to allocate from the FrameInfo |
| 177 | unsigned FrameSize = MFI->getStackSize(); |
| 178 | |
| 179 | // Get the alignments provided by the target, and the maximum alignment |
| 180 | // (if any) of the fixed frame objects. |
| 181 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 182 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 183 | unsigned AlignMask = TargetAlign - 1; // |
| 184 | |
| 185 | // If we are a leaf function, and use up to 224 bytes of stack space, |
| 186 | // don't have a frame pointer, calls, or dynamic alloca then we do not need |
| 187 | // to adjust the stack pointer (we fit in the Red Zone). |
| 188 | bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone); |
| 189 | // FIXME SVR4 The 32-bit SVR4 ABI has no red zone. |
| 190 | if (!DisableRedZone && |
| 191 | FrameSize <= 224 && // Fits in red zone. |
| 192 | !MFI->hasVarSizedObjects() && // No dynamic alloca. |
| 193 | !MFI->adjustsStack() && // No calls. |
| 194 | (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment. |
| 195 | // No need for frame |
| 196 | MFI->setStackSize(0); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | // Get the maximum call frame size of all the calls. |
| 201 | unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); |
| 202 | |
| 203 | // Maximum call frame needs to be at least big enough for linkage and 8 args. |
| 204 | unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(), |
| 205 | Subtarget.isDarwinABI()); |
| 206 | maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); |
| 207 | |
| 208 | // If we have dynamic alloca then maxCallFrameSize needs to be aligned so |
| 209 | // that allocations will be aligned. |
| 210 | if (MFI->hasVarSizedObjects()) |
| 211 | maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; |
| 212 | |
| 213 | // Update maximum call frame size. |
| 214 | MFI->setMaxCallFrameSize(maxCallFrameSize); |
| 215 | |
| 216 | // Include call frame size in total. |
| 217 | FrameSize += maxCallFrameSize; |
| 218 | |
| 219 | // Make sure the frame is aligned. |
| 220 | FrameSize = (FrameSize + AlignMask) & ~AlignMask; |
| 221 | |
| 222 | // Update frame info. |
| 223 | MFI->setStackSize(FrameSize); |
| 224 | } |
| 225 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 226 | // hasFP - Return true if the specified function actually has a dedicated frame |
| 227 | // pointer register. |
| 228 | bool PPCFrameInfo::hasFP(const MachineFunction &MF) const { |
| 229 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 230 | |
| 231 | // Naked functions have no stack frame pushed, so we don't have a frame |
| 232 | // pointer. |
| 233 | if (MF.getFunction()->hasFnAttr(Attribute::Naked)) |
| 234 | return false; |
| 235 | |
| 236 | return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects() || |
| 237 | (GuaranteedTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall()); |
| 238 | } |
| 239 | |
| 240 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 241 | void PPCFrameInfo::emitPrologue(MachineFunction &MF) const { |
| 242 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 243 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 244 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 245 | const PPCInstrInfo &TII = |
| 246 | *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 247 | |
| 248 | MachineModuleInfo &MMI = MF.getMMI(); |
| 249 | DebugLoc dl; |
| 250 | bool needsFrameMoves = MMI.hasDebugInfo() || |
| 251 | !MF.getFunction()->doesNotThrow() || |
| 252 | UnwindTablesMandatory; |
| 253 | |
| 254 | // Prepare for frame info. |
| 255 | MCSymbol *FrameLabel = 0; |
| 256 | |
| 257 | // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, |
| 258 | // process it. |
| 259 | for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { |
| 260 | if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { |
| 261 | HandleVRSaveUpdate(MBBI, TII); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Move MBBI back to the beginning of the function. |
| 267 | MBBI = MBB.begin(); |
| 268 | |
| 269 | // Work out frame sizes. |
| 270 | determineFrameLayout(MF); |
| 271 | unsigned FrameSize = MFI->getStackSize(); |
| 272 | |
| 273 | int NegFrameSize = -FrameSize; |
| 274 | |
| 275 | // Get processor type. |
| 276 | bool isPPC64 = Subtarget.isPPC64(); |
| 277 | // Get operating system |
| 278 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| 279 | // Check if the link register (LR) must be saved. |
| 280 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 281 | bool MustSaveLR = FI->mustSaveLR(); |
| 282 | // Do we have a frame pointer for this function? |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 283 | bool HasFP = hasFP(MF) && FrameSize; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 284 | |
| 285 | int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI); |
| 286 | |
| 287 | int FPOffset = 0; |
| 288 | if (HasFP) { |
| 289 | if (Subtarget.isSVR4ABI()) { |
| 290 | MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 291 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 292 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
| 293 | FPOffset = FFI->getObjectOffset(FPIndex); |
| 294 | } else { |
| 295 | FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (isPPC64) { |
| 300 | if (MustSaveLR) |
| 301 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0); |
| 302 | |
| 303 | if (HasFP) |
| 304 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) |
| 305 | .addReg(PPC::X31) |
| 306 | .addImm(FPOffset/4) |
| 307 | .addReg(PPC::X1); |
| 308 | |
| 309 | if (MustSaveLR) |
| 310 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) |
| 311 | .addReg(PPC::X0) |
| 312 | .addImm(LROffset / 4) |
| 313 | .addReg(PPC::X1); |
| 314 | } else { |
| 315 | if (MustSaveLR) |
| 316 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0); |
| 317 | |
| 318 | if (HasFP) |
| 319 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) |
| 320 | .addReg(PPC::R31) |
| 321 | .addImm(FPOffset) |
| 322 | .addReg(PPC::R1); |
| 323 | |
| 324 | if (MustSaveLR) |
| 325 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) |
| 326 | .addReg(PPC::R0) |
| 327 | .addImm(LROffset) |
| 328 | .addReg(PPC::R1); |
| 329 | } |
| 330 | |
| 331 | // Skip if a leaf routine. |
| 332 | if (!FrameSize) return; |
| 333 | |
| 334 | // Get stack alignments. |
| 335 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 336 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 337 | |
| 338 | // Adjust stack pointer: r1 += NegFrameSize. |
| 339 | // If there is a preferred stack alignment, align R1 now |
| 340 | if (!isPPC64) { |
| 341 | // PPC32. |
| 342 | if (ALIGN_STACK && MaxAlign > TargetAlign) { |
| 343 | assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && |
| 344 | "Invalid alignment!"); |
| 345 | assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); |
| 346 | |
| 347 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0) |
| 348 | .addReg(PPC::R1) |
| 349 | .addImm(0) |
| 350 | .addImm(32 - Log2_32(MaxAlign)) |
| 351 | .addImm(31); |
| 352 | BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0) |
| 353 | .addReg(PPC::R0, RegState::Kill) |
| 354 | .addImm(NegFrameSize); |
| 355 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) |
| 356 | .addReg(PPC::R1) |
| 357 | .addReg(PPC::R1) |
| 358 | .addReg(PPC::R0); |
| 359 | } else if (isInt<16>(NegFrameSize)) { |
| 360 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1) |
| 361 | .addReg(PPC::R1) |
| 362 | .addImm(NegFrameSize) |
| 363 | .addReg(PPC::R1); |
| 364 | } else { |
| 365 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) |
| 366 | .addImm(NegFrameSize >> 16); |
| 367 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) |
| 368 | .addReg(PPC::R0, RegState::Kill) |
| 369 | .addImm(NegFrameSize & 0xFFFF); |
| 370 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) |
| 371 | .addReg(PPC::R1) |
| 372 | .addReg(PPC::R1) |
| 373 | .addReg(PPC::R0); |
| 374 | } |
| 375 | } else { // PPC64. |
| 376 | if (ALIGN_STACK && MaxAlign > TargetAlign) { |
| 377 | assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && |
| 378 | "Invalid alignment!"); |
| 379 | assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); |
| 380 | |
| 381 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0) |
| 382 | .addReg(PPC::X1) |
| 383 | .addImm(0) |
| 384 | .addImm(64 - Log2_32(MaxAlign)); |
| 385 | BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0) |
| 386 | .addReg(PPC::X0) |
| 387 | .addImm(NegFrameSize); |
| 388 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) |
| 389 | .addReg(PPC::X1) |
| 390 | .addReg(PPC::X1) |
| 391 | .addReg(PPC::X0); |
| 392 | } else if (isInt<16>(NegFrameSize)) { |
| 393 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1) |
| 394 | .addReg(PPC::X1) |
| 395 | .addImm(NegFrameSize / 4) |
| 396 | .addReg(PPC::X1); |
| 397 | } else { |
| 398 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) |
| 399 | .addImm(NegFrameSize >> 16); |
| 400 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) |
| 401 | .addReg(PPC::X0, RegState::Kill) |
| 402 | .addImm(NegFrameSize & 0xFFFF); |
| 403 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) |
| 404 | .addReg(PPC::X1) |
| 405 | .addReg(PPC::X1) |
| 406 | .addReg(PPC::X0); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | std::vector<MachineMove> &Moves = MMI.getFrameMoves(); |
| 411 | |
| 412 | // Add the "machine moves" for the instructions we generated above, but in |
| 413 | // reverse order. |
| 414 | if (needsFrameMoves) { |
| 415 | // Mark effective beginning of when frame pointer becomes valid. |
| 416 | FrameLabel = MMI.getContext().CreateTempSymbol(); |
| 417 | BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel); |
| 418 | |
| 419 | // Show update of SP. |
| 420 | if (NegFrameSize) { |
| 421 | MachineLocation SPDst(MachineLocation::VirtualFP); |
| 422 | MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize); |
| 423 | Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc)); |
| 424 | } else { |
| 425 | MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31); |
| 426 | Moves.push_back(MachineMove(FrameLabel, SP, SP)); |
| 427 | } |
| 428 | |
| 429 | if (HasFP) { |
| 430 | MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset); |
| 431 | MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31); |
| 432 | Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc)); |
| 433 | } |
| 434 | |
| 435 | if (MustSaveLR) { |
| 436 | MachineLocation LRDst(MachineLocation::VirtualFP, LROffset); |
| 437 | MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR); |
| 438 | Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc)); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | MCSymbol *ReadyLabel = 0; |
| 443 | |
| 444 | // If there is a frame pointer, copy R1 into R31 |
| 445 | if (HasFP) { |
| 446 | if (!isPPC64) { |
| 447 | BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31) |
| 448 | .addReg(PPC::R1) |
| 449 | .addReg(PPC::R1); |
| 450 | } else { |
| 451 | BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31) |
| 452 | .addReg(PPC::X1) |
| 453 | .addReg(PPC::X1); |
| 454 | } |
| 455 | |
| 456 | if (needsFrameMoves) { |
| 457 | ReadyLabel = MMI.getContext().CreateTempSymbol(); |
| 458 | |
| 459 | // Mark effective beginning of when frame pointer is ready. |
| 460 | BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel); |
| 461 | |
| 462 | MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) : |
| 463 | (isPPC64 ? PPC::X1 : PPC::R1)); |
| 464 | MachineLocation FPSrc(MachineLocation::VirtualFP); |
| 465 | Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc)); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if (needsFrameMoves) { |
| 470 | MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel; |
| 471 | |
| 472 | // Add callee saved registers to move list. |
| 473 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 474 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 475 | int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); |
| 476 | unsigned Reg = CSI[I].getReg(); |
| 477 | if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; |
| 478 | MachineLocation CSDst(MachineLocation::VirtualFP, Offset); |
| 479 | MachineLocation CSSrc(Reg); |
| 480 | Moves.push_back(MachineMove(Label, CSDst, CSSrc)); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void PPCFrameInfo::emitEpilogue(MachineFunction &MF, |
| 486 | MachineBasicBlock &MBB) const { |
| 487 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 488 | const PPCInstrInfo &TII = |
| 489 | *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 490 | |
| 491 | unsigned RetOpcode = MBBI->getOpcode(); |
| 492 | DebugLoc dl; |
| 493 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 494 | assert((RetOpcode == PPC::BLR || |
| 495 | RetOpcode == PPC::TCRETURNri || |
| 496 | RetOpcode == PPC::TCRETURNdi || |
| 497 | RetOpcode == PPC::TCRETURNai || |
| 498 | RetOpcode == PPC::TCRETURNri8 || |
| 499 | RetOpcode == PPC::TCRETURNdi8 || |
| 500 | RetOpcode == PPC::TCRETURNai8) && |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 501 | "Can only insert epilog into returning blocks"); |
| 502 | |
| 503 | // Get alignment info so we know how to restore r1 |
| 504 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 505 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 506 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 507 | |
| 508 | // Get the number of bytes allocated from the FrameInfo. |
| 509 | int FrameSize = MFI->getStackSize(); |
| 510 | |
| 511 | // Get processor type. |
| 512 | bool isPPC64 = Subtarget.isPPC64(); |
| 513 | // Get operating system |
| 514 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| 515 | // Check if the link register (LR) has been saved. |
| 516 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 517 | bool MustSaveLR = FI->mustSaveLR(); |
| 518 | // Do we have a frame pointer for this function? |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 519 | bool HasFP = hasFP(MF) && FrameSize; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 520 | |
| 521 | int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI); |
| 522 | |
| 523 | int FPOffset = 0; |
| 524 | if (HasFP) { |
| 525 | if (Subtarget.isSVR4ABI()) { |
| 526 | MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 527 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 528 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
| 529 | FPOffset = FFI->getObjectOffset(FPIndex); |
| 530 | } else { |
| 531 | FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | bool UsesTCRet = RetOpcode == PPC::TCRETURNri || |
| 536 | RetOpcode == PPC::TCRETURNdi || |
| 537 | RetOpcode == PPC::TCRETURNai || |
| 538 | RetOpcode == PPC::TCRETURNri8 || |
| 539 | RetOpcode == PPC::TCRETURNdi8 || |
| 540 | RetOpcode == PPC::TCRETURNai8; |
| 541 | |
| 542 | if (UsesTCRet) { |
| 543 | int MaxTCRetDelta = FI->getTailCallSPDelta(); |
| 544 | MachineOperand &StackAdjust = MBBI->getOperand(1); |
| 545 | assert(StackAdjust.isImm() && "Expecting immediate value."); |
| 546 | // Adjust stack pointer. |
| 547 | int StackAdj = StackAdjust.getImm(); |
| 548 | int Delta = StackAdj - MaxTCRetDelta; |
| 549 | assert((Delta >= 0) && "Delta must be positive"); |
| 550 | if (MaxTCRetDelta>0) |
| 551 | FrameSize += (StackAdj +Delta); |
| 552 | else |
| 553 | FrameSize += StackAdj; |
| 554 | } |
| 555 | |
| 556 | if (FrameSize) { |
| 557 | // The loaded (or persistent) stack pointer value is offset by the 'stwu' |
| 558 | // on entry to the function. Add this offset back now. |
| 559 | if (!isPPC64) { |
| 560 | // If this function contained a fastcc call and GuaranteedTailCallOpt is |
| 561 | // enabled (=> hasFastCall()==true) the fastcc call might contain a tail |
| 562 | // call which invalidates the stack pointer value in SP(0). So we use the |
| 563 | // value of R31 in this case. |
| 564 | if (FI->hasFastCall() && isInt<16>(FrameSize)) { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 565 | assert(hasFP(MF) && "Expecting a valid the frame pointer."); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 566 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) |
| 567 | .addReg(PPC::R31).addImm(FrameSize); |
| 568 | } else if(FI->hasFastCall()) { |
| 569 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) |
| 570 | .addImm(FrameSize >> 16); |
| 571 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) |
| 572 | .addReg(PPC::R0, RegState::Kill) |
| 573 | .addImm(FrameSize & 0xFFFF); |
| 574 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4)) |
| 575 | .addReg(PPC::R1) |
| 576 | .addReg(PPC::R31) |
| 577 | .addReg(PPC::R0); |
| 578 | } else if (isInt<16>(FrameSize) && |
| 579 | (!ALIGN_STACK || TargetAlign >= MaxAlign) && |
| 580 | !MFI->hasVarSizedObjects()) { |
| 581 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) |
| 582 | .addReg(PPC::R1).addImm(FrameSize); |
| 583 | } else { |
| 584 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1) |
| 585 | .addImm(0).addReg(PPC::R1); |
| 586 | } |
| 587 | } else { |
| 588 | if (FI->hasFastCall() && isInt<16>(FrameSize)) { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 589 | assert(hasFP(MF) && "Expecting a valid the frame pointer."); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 590 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) |
| 591 | .addReg(PPC::X31).addImm(FrameSize); |
| 592 | } else if(FI->hasFastCall()) { |
| 593 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) |
| 594 | .addImm(FrameSize >> 16); |
| 595 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) |
| 596 | .addReg(PPC::X0, RegState::Kill) |
| 597 | .addImm(FrameSize & 0xFFFF); |
| 598 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8)) |
| 599 | .addReg(PPC::X1) |
| 600 | .addReg(PPC::X31) |
| 601 | .addReg(PPC::X0); |
| 602 | } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign && |
| 603 | !MFI->hasVarSizedObjects()) { |
| 604 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) |
| 605 | .addReg(PPC::X1).addImm(FrameSize); |
| 606 | } else { |
| 607 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1) |
| 608 | .addImm(0).addReg(PPC::X1); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | if (isPPC64) { |
| 614 | if (MustSaveLR) |
| 615 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0) |
| 616 | .addImm(LROffset/4).addReg(PPC::X1); |
| 617 | |
| 618 | if (HasFP) |
| 619 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31) |
| 620 | .addImm(FPOffset/4).addReg(PPC::X1); |
| 621 | |
| 622 | if (MustSaveLR) |
| 623 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0); |
| 624 | } else { |
| 625 | if (MustSaveLR) |
| 626 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0) |
| 627 | .addImm(LROffset).addReg(PPC::R1); |
| 628 | |
| 629 | if (HasFP) |
| 630 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31) |
| 631 | .addImm(FPOffset).addReg(PPC::R1); |
| 632 | |
| 633 | if (MustSaveLR) |
| 634 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0); |
| 635 | } |
| 636 | |
| 637 | // Callee pop calling convention. Pop parameter/linkage area. Used for tail |
| 638 | // call optimization |
| 639 | if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR && |
| 640 | MF.getFunction()->getCallingConv() == CallingConv::Fast) { |
| 641 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 642 | unsigned CallerAllocatedAmt = FI->getMinReservedArea(); |
| 643 | unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1; |
| 644 | unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; |
| 645 | unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0; |
| 646 | unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI; |
| 647 | unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4; |
| 648 | unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS; |
| 649 | unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI; |
| 650 | |
| 651 | if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { |
| 652 | BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg) |
| 653 | .addReg(StackReg).addImm(CallerAllocatedAmt); |
| 654 | } else { |
| 655 | BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) |
| 656 | .addImm(CallerAllocatedAmt >> 16); |
| 657 | BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) |
| 658 | .addReg(TmpReg, RegState::Kill) |
| 659 | .addImm(CallerAllocatedAmt & 0xFFFF); |
| 660 | BuildMI(MBB, MBBI, dl, TII.get(ADDInstr)) |
| 661 | .addReg(StackReg) |
| 662 | .addReg(FPReg) |
| 663 | .addReg(TmpReg); |
| 664 | } |
| 665 | } else if (RetOpcode == PPC::TCRETURNdi) { |
| 666 | MBBI = prior(MBB.end()); |
| 667 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 668 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). |
| 669 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 670 | } else if (RetOpcode == PPC::TCRETURNri) { |
| 671 | MBBI = prior(MBB.end()); |
| 672 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 673 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); |
| 674 | } else if (RetOpcode == PPC::TCRETURNai) { |
| 675 | MBBI = prior(MBB.end()); |
| 676 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 677 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); |
| 678 | } else if (RetOpcode == PPC::TCRETURNdi8) { |
| 679 | MBBI = prior(MBB.end()); |
| 680 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 681 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). |
| 682 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 683 | } else if (RetOpcode == PPC::TCRETURNri8) { |
| 684 | MBBI = prior(MBB.end()); |
| 685 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 686 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); |
| 687 | } else if (RetOpcode == PPC::TCRETURNai8) { |
| 688 | MBBI = prior(MBB.end()); |
| 689 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 690 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); |
| 691 | } |
| 692 | } |
Anton Korobeynikov | d9e3385 | 2010-11-18 23:25:52 +0000 | [diff] [blame] | 693 | |
| 694 | void PPCFrameInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const { |
| 695 | // Initial state of the frame pointer is R1. |
| 696 | MachineLocation Dst(MachineLocation::VirtualFP); |
| 697 | MachineLocation Src(PPC::R1, 0); |
| 698 | Moves.push_back(MachineMove(0, Dst, Src)); |
| 699 | } |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 700 | |
| 701 | static bool spillsCR(const MachineFunction &MF) { |
| 702 | const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 703 | return FuncInfo->isCRSpilled(); |
| 704 | } |
| 705 | |
| 706 | /// MustSaveLR - Return true if this function requires that we save the LR |
| 707 | /// register onto the stack in the prolog and restore it in the epilog of the |
| 708 | /// function. |
| 709 | static bool MustSaveLR(const MachineFunction &MF, unsigned LR) { |
| 710 | const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); |
| 711 | |
| 712 | // We need a save/restore of LR if there is any def of LR (which is |
| 713 | // defined by calls, including the PIC setup sequence), or if there is |
| 714 | // some use of the LR stack slot (e.g. for builtin_return_address). |
| 715 | // (LR comes in 32 and 64 bit versions.) |
| 716 | MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); |
| 717 | return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); |
| 718 | } |
| 719 | |
| 720 | void |
| 721 | PPCFrameInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
| 722 | RegScavenger *RS) const { |
| 723 | const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 724 | |
| 725 | // Save and clear the LR state. |
| 726 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 727 | unsigned LR = RegInfo->getRARegister(); |
| 728 | FI->setMustSaveLR(MustSaveLR(MF, LR)); |
| 729 | MF.getRegInfo().setPhysRegUnused(LR); |
| 730 | |
| 731 | // Save R31 if necessary |
| 732 | int FPSI = FI->getFramePointerSaveIndex(); |
| 733 | bool isPPC64 = Subtarget.isPPC64(); |
| 734 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| 735 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 736 | |
| 737 | // If the frame pointer save index hasn't been defined yet. |
| 738 | if (!FPSI && hasFP(MF)) { |
| 739 | // Find out what the fix offset of the frame pointer save area. |
| 740 | int FPOffset = getFramePointerSaveOffset(isPPC64, isDarwinABI); |
| 741 | // Allocate the frame index for frame pointer save area. |
| 742 | FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); |
| 743 | // Save the result. |
| 744 | FI->setFramePointerSaveIndex(FPSI); |
| 745 | } |
| 746 | |
| 747 | // Reserve stack space to move the linkage area to in case of a tail call. |
| 748 | int TCSPDelta = 0; |
| 749 | if (GuaranteedTailCallOpt && (TCSPDelta = FI->getTailCallSPDelta()) < 0) { |
| 750 | MF.getFrameInfo()->CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); |
| 751 | } |
| 752 | |
| 753 | // Reserve a slot closest to SP or frame pointer if we have a dynalloc or |
| 754 | // a large stack, which will require scavenging a register to materialize a |
| 755 | // large offset. |
| 756 | // FIXME: this doesn't actually check stack size, so is a bit pessimistic |
| 757 | // FIXME: doesn't detect whether or not we need to spill vXX, which requires |
| 758 | // r0 for now. |
| 759 | |
| 760 | if (RegInfo->requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable. |
| 761 | if (hasFP(MF) || spillsCR(MF)) { |
| 762 | const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; |
| 763 | const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; |
| 764 | const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC; |
| 765 | RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), |
| 766 | RC->getAlignment(), |
| 767 | false)); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void PPCFrameInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) |
| 772 | const { |
| 773 | // Early exit if not using the SVR4 ABI. |
| 774 | if (!Subtarget.isSVR4ABI()) |
| 775 | return; |
| 776 | |
| 777 | // Get callee saved register information. |
| 778 | MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 779 | const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo(); |
| 780 | |
| 781 | // Early exit if no callee saved registers are modified! |
| 782 | if (CSI.empty() && !hasFP(MF)) { |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | unsigned MinGPR = PPC::R31; |
| 787 | unsigned MinG8R = PPC::X31; |
| 788 | unsigned MinFPR = PPC::F31; |
| 789 | unsigned MinVR = PPC::V31; |
| 790 | |
| 791 | bool HasGPSaveArea = false; |
| 792 | bool HasG8SaveArea = false; |
| 793 | bool HasFPSaveArea = false; |
| 794 | bool HasCRSaveArea = false; |
| 795 | bool HasVRSAVESaveArea = false; |
| 796 | bool HasVRSaveArea = false; |
| 797 | |
| 798 | SmallVector<CalleeSavedInfo, 18> GPRegs; |
| 799 | SmallVector<CalleeSavedInfo, 18> G8Regs; |
| 800 | SmallVector<CalleeSavedInfo, 18> FPRegs; |
| 801 | SmallVector<CalleeSavedInfo, 18> VRegs; |
| 802 | |
| 803 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 804 | unsigned Reg = CSI[i].getReg(); |
| 805 | if (PPC::GPRCRegisterClass->contains(Reg)) { |
| 806 | HasGPSaveArea = true; |
| 807 | |
| 808 | GPRegs.push_back(CSI[i]); |
| 809 | |
| 810 | if (Reg < MinGPR) { |
| 811 | MinGPR = Reg; |
| 812 | } |
| 813 | } else if (PPC::G8RCRegisterClass->contains(Reg)) { |
| 814 | HasG8SaveArea = true; |
| 815 | |
| 816 | G8Regs.push_back(CSI[i]); |
| 817 | |
| 818 | if (Reg < MinG8R) { |
| 819 | MinG8R = Reg; |
| 820 | } |
| 821 | } else if (PPC::F8RCRegisterClass->contains(Reg)) { |
| 822 | HasFPSaveArea = true; |
| 823 | |
| 824 | FPRegs.push_back(CSI[i]); |
| 825 | |
| 826 | if (Reg < MinFPR) { |
| 827 | MinFPR = Reg; |
| 828 | } |
| 829 | // FIXME SVR4: Disable CR save area for now. |
| 830 | } else if (PPC::CRBITRCRegisterClass->contains(Reg) |
| 831 | || PPC::CRRCRegisterClass->contains(Reg)) { |
| 832 | // HasCRSaveArea = true; |
| 833 | } else if (PPC::VRSAVERCRegisterClass->contains(Reg)) { |
| 834 | HasVRSAVESaveArea = true; |
| 835 | } else if (PPC::VRRCRegisterClass->contains(Reg)) { |
| 836 | HasVRSaveArea = true; |
| 837 | |
| 838 | VRegs.push_back(CSI[i]); |
| 839 | |
| 840 | if (Reg < MinVR) { |
| 841 | MinVR = Reg; |
| 842 | } |
| 843 | } else { |
| 844 | llvm_unreachable("Unknown RegisterClass!"); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); |
| 849 | |
| 850 | int64_t LowerBound = 0; |
| 851 | |
| 852 | // Take into account stack space reserved for tail calls. |
| 853 | int TCSPDelta = 0; |
| 854 | if (GuaranteedTailCallOpt && (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { |
| 855 | LowerBound = TCSPDelta; |
| 856 | } |
| 857 | |
| 858 | // The Floating-point register save area is right below the back chain word |
| 859 | // of the previous stack frame. |
| 860 | if (HasFPSaveArea) { |
| 861 | for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { |
| 862 | int FI = FPRegs[i].getFrameIdx(); |
| 863 | |
| 864 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 865 | } |
| 866 | |
| 867 | LowerBound -= (31 - PPCRegisterInfo::getRegisterNumbering(MinFPR) + 1) * 8; |
| 868 | } |
| 869 | |
| 870 | // Check whether the frame pointer register is allocated. If so, make sure it |
| 871 | // is spilled to the correct offset. |
| 872 | if (hasFP(MF)) { |
| 873 | HasGPSaveArea = true; |
| 874 | |
| 875 | int FI = PFI->getFramePointerSaveIndex(); |
| 876 | assert(FI && "No Frame Pointer Save Slot!"); |
| 877 | |
| 878 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 879 | } |
| 880 | |
| 881 | // General register save area starts right below the Floating-point |
| 882 | // register save area. |
| 883 | if (HasGPSaveArea || HasG8SaveArea) { |
| 884 | // Move general register save area spill slots down, taking into account |
| 885 | // the size of the Floating-point register save area. |
| 886 | for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { |
| 887 | int FI = GPRegs[i].getFrameIdx(); |
| 888 | |
| 889 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 890 | } |
| 891 | |
| 892 | // Move general register save area spill slots down, taking into account |
| 893 | // the size of the Floating-point register save area. |
| 894 | for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { |
| 895 | int FI = G8Regs[i].getFrameIdx(); |
| 896 | |
| 897 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 898 | } |
| 899 | |
| 900 | unsigned MinReg = |
| 901 | std::min<unsigned>(PPCRegisterInfo::getRegisterNumbering(MinGPR), |
| 902 | PPCRegisterInfo::getRegisterNumbering(MinG8R)); |
| 903 | |
| 904 | if (Subtarget.isPPC64()) { |
| 905 | LowerBound -= (31 - MinReg + 1) * 8; |
| 906 | } else { |
| 907 | LowerBound -= (31 - MinReg + 1) * 4; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // The CR save area is below the general register save area. |
| 912 | if (HasCRSaveArea) { |
| 913 | // FIXME SVR4: Is it actually possible to have multiple elements in CSI |
| 914 | // which have the CR/CRBIT register class? |
| 915 | // Adjust the frame index of the CR spill slot. |
| 916 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 917 | unsigned Reg = CSI[i].getReg(); |
| 918 | |
| 919 | if (PPC::CRBITRCRegisterClass->contains(Reg) || |
| 920 | PPC::CRRCRegisterClass->contains(Reg)) { |
| 921 | int FI = CSI[i].getFrameIdx(); |
| 922 | |
| 923 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | LowerBound -= 4; // The CR save area is always 4 bytes long. |
| 928 | } |
| 929 | |
| 930 | if (HasVRSAVESaveArea) { |
| 931 | // FIXME SVR4: Is it actually possible to have multiple elements in CSI |
| 932 | // which have the VRSAVE register class? |
| 933 | // Adjust the frame index of the VRSAVE spill slot. |
| 934 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 935 | unsigned Reg = CSI[i].getReg(); |
| 936 | |
| 937 | if (PPC::VRSAVERCRegisterClass->contains(Reg)) { |
| 938 | int FI = CSI[i].getFrameIdx(); |
| 939 | |
| 940 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. |
| 945 | } |
| 946 | |
| 947 | if (HasVRSaveArea) { |
| 948 | // Insert alignment padding, we need 16-byte alignment. |
| 949 | LowerBound = (LowerBound - 15) & ~(15); |
| 950 | |
| 951 | for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { |
| 952 | int FI = VRegs[i].getFrameIdx(); |
| 953 | |
| 954 | FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI)); |
| 955 | } |
| 956 | } |
| 957 | } |