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" |
| 23 | #include "llvm/Target/TargetOptions.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | // FIXME This disables some code that aligns the stack to a boundary bigger than |
| 28 | // the default (16 bytes on Darwin) when there is a stack local of greater |
| 29 | // alignment. This does not currently work, because the delta between old and |
| 30 | // new stack pointers is added to offsets that reference incoming parameters |
| 31 | // after the prolog is generated, and the code that does that doesn't handle a |
| 32 | // variable delta. You don't want to do that anyway; a better approach is to |
| 33 | // reserve another register that retains to the incoming stack pointer, and |
| 34 | // reference parameters relative to that. |
| 35 | #define ALIGN_STACK 0 |
| 36 | |
| 37 | |
| 38 | /// VRRegNo - Map from a numbered VR register to its enum value. |
| 39 | /// |
| 40 | static const unsigned short VRRegNo[] = { |
| 41 | PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , |
| 42 | PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, |
| 43 | PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, |
| 44 | PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 |
| 45 | }; |
| 46 | |
| 47 | /// RemoveVRSaveCode - We have found that this function does not need any code |
| 48 | /// to manipulate the VRSAVE register, even though it uses vector registers. |
| 49 | /// This can happen when the only registers used are known to be live in or out |
| 50 | /// of the function. Remove all of the VRSAVE related code from the function. |
| 51 | static void RemoveVRSaveCode(MachineInstr *MI) { |
| 52 | MachineBasicBlock *Entry = MI->getParent(); |
| 53 | MachineFunction *MF = Entry->getParent(); |
| 54 | |
| 55 | // We know that the MTVRSAVE instruction immediately follows MI. Remove it. |
| 56 | MachineBasicBlock::iterator MBBI = MI; |
| 57 | ++MBBI; |
| 58 | assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); |
| 59 | MBBI->eraseFromParent(); |
| 60 | |
| 61 | bool RemovedAllMTVRSAVEs = true; |
| 62 | // See if we can find and remove the MTVRSAVE instruction from all of the |
| 63 | // epilog blocks. |
| 64 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { |
| 65 | // If last instruction is a return instruction, add an epilogue |
| 66 | if (!I->empty() && I->back().getDesc().isReturn()) { |
| 67 | bool FoundIt = false; |
| 68 | for (MBBI = I->end(); MBBI != I->begin(); ) { |
| 69 | --MBBI; |
| 70 | if (MBBI->getOpcode() == PPC::MTVRSAVE) { |
| 71 | MBBI->eraseFromParent(); // remove it. |
| 72 | FoundIt = true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | RemovedAllMTVRSAVEs &= FoundIt; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // If we found and removed all MTVRSAVE instructions, remove the read of |
| 81 | // VRSAVE as well. |
| 82 | if (RemovedAllMTVRSAVEs) { |
| 83 | MBBI = MI; |
| 84 | assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); |
| 85 | --MBBI; |
| 86 | assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); |
| 87 | MBBI->eraseFromParent(); |
| 88 | } |
| 89 | |
| 90 | // Finally, nuke the UPDATE_VRSAVE. |
| 91 | MI->eraseFromParent(); |
| 92 | } |
| 93 | |
| 94 | // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the |
| 95 | // instruction selector. Based on the vector registers that have been used, |
| 96 | // transform this into the appropriate ORI instruction. |
| 97 | static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) { |
| 98 | MachineFunction *MF = MI->getParent()->getParent(); |
| 99 | DebugLoc dl = MI->getDebugLoc(); |
| 100 | |
| 101 | unsigned UsedRegMask = 0; |
| 102 | for (unsigned i = 0; i != 32; ++i) |
| 103 | if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i])) |
| 104 | UsedRegMask |= 1 << (31-i); |
| 105 | |
| 106 | // Live in and live out values already must be in the mask, so don't bother |
| 107 | // marking them. |
| 108 | for (MachineRegisterInfo::livein_iterator |
| 109 | I = MF->getRegInfo().livein_begin(), |
| 110 | E = MF->getRegInfo().livein_end(); I != E; ++I) { |
| 111 | unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first); |
| 112 | if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. |
| 113 | UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. |
| 114 | } |
| 115 | for (MachineRegisterInfo::liveout_iterator |
| 116 | I = MF->getRegInfo().liveout_begin(), |
| 117 | E = MF->getRegInfo().liveout_end(); I != E; ++I) { |
| 118 | unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I); |
| 119 | if (VRRegNo[RegNo] == *I) // If this really is a vector reg. |
| 120 | UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. |
| 121 | } |
| 122 | |
| 123 | // If no registers are used, turn this into a copy. |
| 124 | if (UsedRegMask == 0) { |
| 125 | // Remove all VRSAVE code. |
| 126 | RemoveVRSaveCode(MI); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 131 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 132 | |
| 133 | if ((UsedRegMask & 0xFFFF) == UsedRegMask) { |
| 134 | if (DstReg != SrcReg) |
| 135 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 136 | .addReg(SrcReg) |
| 137 | .addImm(UsedRegMask); |
| 138 | else |
| 139 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 140 | .addReg(SrcReg, RegState::Kill) |
| 141 | .addImm(UsedRegMask); |
| 142 | } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { |
| 143 | if (DstReg != SrcReg) |
| 144 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 145 | .addReg(SrcReg) |
| 146 | .addImm(UsedRegMask >> 16); |
| 147 | else |
| 148 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 149 | .addReg(SrcReg, RegState::Kill) |
| 150 | .addImm(UsedRegMask >> 16); |
| 151 | } else { |
| 152 | if (DstReg != SrcReg) |
| 153 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 154 | .addReg(SrcReg) |
| 155 | .addImm(UsedRegMask >> 16); |
| 156 | else |
| 157 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 158 | .addReg(SrcReg, RegState::Kill) |
| 159 | .addImm(UsedRegMask >> 16); |
| 160 | |
| 161 | BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 162 | .addReg(DstReg, RegState::Kill) |
| 163 | .addImm(UsedRegMask & 0xFFFF); |
| 164 | } |
| 165 | |
| 166 | // Remove the old UPDATE_VRSAVE instruction. |
| 167 | MI->eraseFromParent(); |
| 168 | } |
| 169 | |
| 170 | /// determineFrameLayout - Determine the size of the frame and maximum call |
| 171 | /// frame size. |
| 172 | void PPCFrameInfo::determineFrameLayout(MachineFunction &MF) const { |
| 173 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 174 | |
| 175 | // Get the number of bytes to allocate from the FrameInfo |
| 176 | unsigned FrameSize = MFI->getStackSize(); |
| 177 | |
| 178 | // Get the alignments provided by the target, and the maximum alignment |
| 179 | // (if any) of the fixed frame objects. |
| 180 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 181 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 182 | unsigned AlignMask = TargetAlign - 1; // |
| 183 | |
| 184 | // If we are a leaf function, and use up to 224 bytes of stack space, |
| 185 | // don't have a frame pointer, calls, or dynamic alloca then we do not need |
| 186 | // to adjust the stack pointer (we fit in the Red Zone). |
| 187 | bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone); |
| 188 | // FIXME SVR4 The 32-bit SVR4 ABI has no red zone. |
| 189 | if (!DisableRedZone && |
| 190 | FrameSize <= 224 && // Fits in red zone. |
| 191 | !MFI->hasVarSizedObjects() && // No dynamic alloca. |
| 192 | !MFI->adjustsStack() && // No calls. |
| 193 | (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment. |
| 194 | // No need for frame |
| 195 | MFI->setStackSize(0); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | // Get the maximum call frame size of all the calls. |
| 200 | unsigned maxCallFrameSize = MFI->getMaxCallFrameSize(); |
| 201 | |
| 202 | // Maximum call frame needs to be at least big enough for linkage and 8 args. |
| 203 | unsigned minCallFrameSize = getMinCallFrameSize(Subtarget.isPPC64(), |
| 204 | Subtarget.isDarwinABI()); |
| 205 | maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); |
| 206 | |
| 207 | // If we have dynamic alloca then maxCallFrameSize needs to be aligned so |
| 208 | // that allocations will be aligned. |
| 209 | if (MFI->hasVarSizedObjects()) |
| 210 | maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; |
| 211 | |
| 212 | // Update maximum call frame size. |
| 213 | MFI->setMaxCallFrameSize(maxCallFrameSize); |
| 214 | |
| 215 | // Include call frame size in total. |
| 216 | FrameSize += maxCallFrameSize; |
| 217 | |
| 218 | // Make sure the frame is aligned. |
| 219 | FrameSize = (FrameSize + AlignMask) & ~AlignMask; |
| 220 | |
| 221 | // Update frame info. |
| 222 | MFI->setStackSize(FrameSize); |
| 223 | } |
| 224 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 225 | // hasFP - Return true if the specified function actually has a dedicated frame |
| 226 | // pointer register. |
| 227 | bool PPCFrameInfo::hasFP(const MachineFunction &MF) const { |
| 228 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 229 | |
| 230 | // Naked functions have no stack frame pushed, so we don't have a frame |
| 231 | // pointer. |
| 232 | if (MF.getFunction()->hasFnAttr(Attribute::Naked)) |
| 233 | return false; |
| 234 | |
| 235 | return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects() || |
| 236 | (GuaranteedTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall()); |
| 237 | } |
| 238 | |
| 239 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 240 | void PPCFrameInfo::emitPrologue(MachineFunction &MF) const { |
| 241 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 242 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 243 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 244 | const PPCInstrInfo &TII = |
| 245 | *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 246 | |
| 247 | MachineModuleInfo &MMI = MF.getMMI(); |
| 248 | DebugLoc dl; |
| 249 | bool needsFrameMoves = MMI.hasDebugInfo() || |
| 250 | !MF.getFunction()->doesNotThrow() || |
| 251 | UnwindTablesMandatory; |
| 252 | |
| 253 | // Prepare for frame info. |
| 254 | MCSymbol *FrameLabel = 0; |
| 255 | |
| 256 | // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, |
| 257 | // process it. |
| 258 | for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { |
| 259 | if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { |
| 260 | HandleVRSaveUpdate(MBBI, TII); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Move MBBI back to the beginning of the function. |
| 266 | MBBI = MBB.begin(); |
| 267 | |
| 268 | // Work out frame sizes. |
| 269 | determineFrameLayout(MF); |
| 270 | unsigned FrameSize = MFI->getStackSize(); |
| 271 | |
| 272 | int NegFrameSize = -FrameSize; |
| 273 | |
| 274 | // Get processor type. |
| 275 | bool isPPC64 = Subtarget.isPPC64(); |
| 276 | // Get operating system |
| 277 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| 278 | // Check if the link register (LR) must be saved. |
| 279 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 280 | bool MustSaveLR = FI->mustSaveLR(); |
| 281 | // Do we have a frame pointer for this function? |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 282 | bool HasFP = hasFP(MF) && FrameSize; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 283 | |
| 284 | int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI); |
| 285 | |
| 286 | int FPOffset = 0; |
| 287 | if (HasFP) { |
| 288 | if (Subtarget.isSVR4ABI()) { |
| 289 | MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 290 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 291 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
| 292 | FPOffset = FFI->getObjectOffset(FPIndex); |
| 293 | } else { |
| 294 | FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (isPPC64) { |
| 299 | if (MustSaveLR) |
| 300 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0); |
| 301 | |
| 302 | if (HasFP) |
| 303 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) |
| 304 | .addReg(PPC::X31) |
| 305 | .addImm(FPOffset/4) |
| 306 | .addReg(PPC::X1); |
| 307 | |
| 308 | if (MustSaveLR) |
| 309 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STD)) |
| 310 | .addReg(PPC::X0) |
| 311 | .addImm(LROffset / 4) |
| 312 | .addReg(PPC::X1); |
| 313 | } else { |
| 314 | if (MustSaveLR) |
| 315 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0); |
| 316 | |
| 317 | if (HasFP) |
| 318 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) |
| 319 | .addReg(PPC::R31) |
| 320 | .addImm(FPOffset) |
| 321 | .addReg(PPC::R1); |
| 322 | |
| 323 | if (MustSaveLR) |
| 324 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW)) |
| 325 | .addReg(PPC::R0) |
| 326 | .addImm(LROffset) |
| 327 | .addReg(PPC::R1); |
| 328 | } |
| 329 | |
| 330 | // Skip if a leaf routine. |
| 331 | if (!FrameSize) return; |
| 332 | |
| 333 | // Get stack alignments. |
| 334 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 335 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 336 | |
| 337 | // Adjust stack pointer: r1 += NegFrameSize. |
| 338 | // If there is a preferred stack alignment, align R1 now |
| 339 | if (!isPPC64) { |
| 340 | // PPC32. |
| 341 | if (ALIGN_STACK && MaxAlign > TargetAlign) { |
| 342 | assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && |
| 343 | "Invalid alignment!"); |
| 344 | assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); |
| 345 | |
| 346 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0) |
| 347 | .addReg(PPC::R1) |
| 348 | .addImm(0) |
| 349 | .addImm(32 - Log2_32(MaxAlign)) |
| 350 | .addImm(31); |
| 351 | BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0) |
| 352 | .addReg(PPC::R0, RegState::Kill) |
| 353 | .addImm(NegFrameSize); |
| 354 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) |
| 355 | .addReg(PPC::R1) |
| 356 | .addReg(PPC::R1) |
| 357 | .addReg(PPC::R0); |
| 358 | } else if (isInt<16>(NegFrameSize)) { |
| 359 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1) |
| 360 | .addReg(PPC::R1) |
| 361 | .addImm(NegFrameSize) |
| 362 | .addReg(PPC::R1); |
| 363 | } else { |
| 364 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) |
| 365 | .addImm(NegFrameSize >> 16); |
| 366 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) |
| 367 | .addReg(PPC::R0, RegState::Kill) |
| 368 | .addImm(NegFrameSize & 0xFFFF); |
| 369 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX)) |
| 370 | .addReg(PPC::R1) |
| 371 | .addReg(PPC::R1) |
| 372 | .addReg(PPC::R0); |
| 373 | } |
| 374 | } else { // PPC64. |
| 375 | if (ALIGN_STACK && MaxAlign > TargetAlign) { |
| 376 | assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && |
| 377 | "Invalid alignment!"); |
| 378 | assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!"); |
| 379 | |
| 380 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0) |
| 381 | .addReg(PPC::X1) |
| 382 | .addImm(0) |
| 383 | .addImm(64 - Log2_32(MaxAlign)); |
| 384 | BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0) |
| 385 | .addReg(PPC::X0) |
| 386 | .addImm(NegFrameSize); |
| 387 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) |
| 388 | .addReg(PPC::X1) |
| 389 | .addReg(PPC::X1) |
| 390 | .addReg(PPC::X0); |
| 391 | } else if (isInt<16>(NegFrameSize)) { |
| 392 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1) |
| 393 | .addReg(PPC::X1) |
| 394 | .addImm(NegFrameSize / 4) |
| 395 | .addReg(PPC::X1); |
| 396 | } else { |
| 397 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) |
| 398 | .addImm(NegFrameSize >> 16); |
| 399 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) |
| 400 | .addReg(PPC::X0, RegState::Kill) |
| 401 | .addImm(NegFrameSize & 0xFFFF); |
| 402 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX)) |
| 403 | .addReg(PPC::X1) |
| 404 | .addReg(PPC::X1) |
| 405 | .addReg(PPC::X0); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | std::vector<MachineMove> &Moves = MMI.getFrameMoves(); |
| 410 | |
| 411 | // Add the "machine moves" for the instructions we generated above, but in |
| 412 | // reverse order. |
| 413 | if (needsFrameMoves) { |
| 414 | // Mark effective beginning of when frame pointer becomes valid. |
| 415 | FrameLabel = MMI.getContext().CreateTempSymbol(); |
| 416 | BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(FrameLabel); |
| 417 | |
| 418 | // Show update of SP. |
| 419 | if (NegFrameSize) { |
| 420 | MachineLocation SPDst(MachineLocation::VirtualFP); |
| 421 | MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize); |
| 422 | Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc)); |
| 423 | } else { |
| 424 | MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31); |
| 425 | Moves.push_back(MachineMove(FrameLabel, SP, SP)); |
| 426 | } |
| 427 | |
| 428 | if (HasFP) { |
| 429 | MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset); |
| 430 | MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31); |
| 431 | Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc)); |
| 432 | } |
| 433 | |
| 434 | if (MustSaveLR) { |
| 435 | MachineLocation LRDst(MachineLocation::VirtualFP, LROffset); |
| 436 | MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR); |
| 437 | Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc)); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | MCSymbol *ReadyLabel = 0; |
| 442 | |
| 443 | // If there is a frame pointer, copy R1 into R31 |
| 444 | if (HasFP) { |
| 445 | if (!isPPC64) { |
| 446 | BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31) |
| 447 | .addReg(PPC::R1) |
| 448 | .addReg(PPC::R1); |
| 449 | } else { |
| 450 | BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31) |
| 451 | .addReg(PPC::X1) |
| 452 | .addReg(PPC::X1); |
| 453 | } |
| 454 | |
| 455 | if (needsFrameMoves) { |
| 456 | ReadyLabel = MMI.getContext().CreateTempSymbol(); |
| 457 | |
| 458 | // Mark effective beginning of when frame pointer is ready. |
| 459 | BuildMI(MBB, MBBI, dl, TII.get(PPC::PROLOG_LABEL)).addSym(ReadyLabel); |
| 460 | |
| 461 | MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) : |
| 462 | (isPPC64 ? PPC::X1 : PPC::R1)); |
| 463 | MachineLocation FPSrc(MachineLocation::VirtualFP); |
| 464 | Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc)); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if (needsFrameMoves) { |
| 469 | MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel; |
| 470 | |
| 471 | // Add callee saved registers to move list. |
| 472 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 473 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
| 474 | int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx()); |
| 475 | unsigned Reg = CSI[I].getReg(); |
| 476 | if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; |
| 477 | MachineLocation CSDst(MachineLocation::VirtualFP, Offset); |
| 478 | MachineLocation CSSrc(Reg); |
| 479 | Moves.push_back(MachineMove(Label, CSDst, CSSrc)); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | void PPCFrameInfo::emitEpilogue(MachineFunction &MF, |
| 485 | MachineBasicBlock &MBB) const { |
| 486 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 487 | const PPCInstrInfo &TII = |
| 488 | *static_cast<const PPCInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 489 | |
| 490 | unsigned RetOpcode = MBBI->getOpcode(); |
| 491 | DebugLoc dl; |
| 492 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 493 | assert((RetOpcode == PPC::BLR || |
| 494 | RetOpcode == PPC::TCRETURNri || |
| 495 | RetOpcode == PPC::TCRETURNdi || |
| 496 | RetOpcode == PPC::TCRETURNai || |
| 497 | RetOpcode == PPC::TCRETURNri8 || |
| 498 | RetOpcode == PPC::TCRETURNdi8 || |
| 499 | RetOpcode == PPC::TCRETURNai8) && |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 500 | "Can only insert epilog into returning blocks"); |
| 501 | |
| 502 | // Get alignment info so we know how to restore r1 |
| 503 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 504 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 505 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 506 | |
| 507 | // Get the number of bytes allocated from the FrameInfo. |
| 508 | int FrameSize = MFI->getStackSize(); |
| 509 | |
| 510 | // Get processor type. |
| 511 | bool isPPC64 = Subtarget.isPPC64(); |
| 512 | // Get operating system |
| 513 | bool isDarwinABI = Subtarget.isDarwinABI(); |
| 514 | // Check if the link register (LR) has been saved. |
| 515 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 516 | bool MustSaveLR = FI->mustSaveLR(); |
| 517 | // Do we have a frame pointer for this function? |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 518 | bool HasFP = hasFP(MF) && FrameSize; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 519 | |
| 520 | int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI); |
| 521 | |
| 522 | int FPOffset = 0; |
| 523 | if (HasFP) { |
| 524 | if (Subtarget.isSVR4ABI()) { |
| 525 | MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 526 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 527 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
| 528 | FPOffset = FFI->getObjectOffset(FPIndex); |
| 529 | } else { |
| 530 | FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | bool UsesTCRet = RetOpcode == PPC::TCRETURNri || |
| 535 | RetOpcode == PPC::TCRETURNdi || |
| 536 | RetOpcode == PPC::TCRETURNai || |
| 537 | RetOpcode == PPC::TCRETURNri8 || |
| 538 | RetOpcode == PPC::TCRETURNdi8 || |
| 539 | RetOpcode == PPC::TCRETURNai8; |
| 540 | |
| 541 | if (UsesTCRet) { |
| 542 | int MaxTCRetDelta = FI->getTailCallSPDelta(); |
| 543 | MachineOperand &StackAdjust = MBBI->getOperand(1); |
| 544 | assert(StackAdjust.isImm() && "Expecting immediate value."); |
| 545 | // Adjust stack pointer. |
| 546 | int StackAdj = StackAdjust.getImm(); |
| 547 | int Delta = StackAdj - MaxTCRetDelta; |
| 548 | assert((Delta >= 0) && "Delta must be positive"); |
| 549 | if (MaxTCRetDelta>0) |
| 550 | FrameSize += (StackAdj +Delta); |
| 551 | else |
| 552 | FrameSize += StackAdj; |
| 553 | } |
| 554 | |
| 555 | if (FrameSize) { |
| 556 | // The loaded (or persistent) stack pointer value is offset by the 'stwu' |
| 557 | // on entry to the function. Add this offset back now. |
| 558 | if (!isPPC64) { |
| 559 | // If this function contained a fastcc call and GuaranteedTailCallOpt is |
| 560 | // enabled (=> hasFastCall()==true) the fastcc call might contain a tail |
| 561 | // call which invalidates the stack pointer value in SP(0). So we use the |
| 562 | // value of R31 in this case. |
| 563 | if (FI->hasFastCall() && isInt<16>(FrameSize)) { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 564 | assert(hasFP(MF) && "Expecting a valid the frame pointer."); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 565 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) |
| 566 | .addReg(PPC::R31).addImm(FrameSize); |
| 567 | } else if(FI->hasFastCall()) { |
| 568 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0) |
| 569 | .addImm(FrameSize >> 16); |
| 570 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0) |
| 571 | .addReg(PPC::R0, RegState::Kill) |
| 572 | .addImm(FrameSize & 0xFFFF); |
| 573 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4)) |
| 574 | .addReg(PPC::R1) |
| 575 | .addReg(PPC::R31) |
| 576 | .addReg(PPC::R0); |
| 577 | } else if (isInt<16>(FrameSize) && |
| 578 | (!ALIGN_STACK || TargetAlign >= MaxAlign) && |
| 579 | !MFI->hasVarSizedObjects()) { |
| 580 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1) |
| 581 | .addReg(PPC::R1).addImm(FrameSize); |
| 582 | } else { |
| 583 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1) |
| 584 | .addImm(0).addReg(PPC::R1); |
| 585 | } |
| 586 | } else { |
| 587 | if (FI->hasFastCall() && isInt<16>(FrameSize)) { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame^] | 588 | assert(hasFP(MF) && "Expecting a valid the frame pointer."); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 589 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) |
| 590 | .addReg(PPC::X31).addImm(FrameSize); |
| 591 | } else if(FI->hasFastCall()) { |
| 592 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0) |
| 593 | .addImm(FrameSize >> 16); |
| 594 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0) |
| 595 | .addReg(PPC::X0, RegState::Kill) |
| 596 | .addImm(FrameSize & 0xFFFF); |
| 597 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8)) |
| 598 | .addReg(PPC::X1) |
| 599 | .addReg(PPC::X31) |
| 600 | .addReg(PPC::X0); |
| 601 | } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign && |
| 602 | !MFI->hasVarSizedObjects()) { |
| 603 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1) |
| 604 | .addReg(PPC::X1).addImm(FrameSize); |
| 605 | } else { |
| 606 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1) |
| 607 | .addImm(0).addReg(PPC::X1); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (isPPC64) { |
| 613 | if (MustSaveLR) |
| 614 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0) |
| 615 | .addImm(LROffset/4).addReg(PPC::X1); |
| 616 | |
| 617 | if (HasFP) |
| 618 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31) |
| 619 | .addImm(FPOffset/4).addReg(PPC::X1); |
| 620 | |
| 621 | if (MustSaveLR) |
| 622 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0); |
| 623 | } else { |
| 624 | if (MustSaveLR) |
| 625 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0) |
| 626 | .addImm(LROffset).addReg(PPC::R1); |
| 627 | |
| 628 | if (HasFP) |
| 629 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31) |
| 630 | .addImm(FPOffset).addReg(PPC::R1); |
| 631 | |
| 632 | if (MustSaveLR) |
| 633 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0); |
| 634 | } |
| 635 | |
| 636 | // Callee pop calling convention. Pop parameter/linkage area. Used for tail |
| 637 | // call optimization |
| 638 | if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR && |
| 639 | MF.getFunction()->getCallingConv() == CallingConv::Fast) { |
| 640 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 641 | unsigned CallerAllocatedAmt = FI->getMinReservedArea(); |
| 642 | unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1; |
| 643 | unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; |
| 644 | unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0; |
| 645 | unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI; |
| 646 | unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4; |
| 647 | unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS; |
| 648 | unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI; |
| 649 | |
| 650 | if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { |
| 651 | BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg) |
| 652 | .addReg(StackReg).addImm(CallerAllocatedAmt); |
| 653 | } else { |
| 654 | BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) |
| 655 | .addImm(CallerAllocatedAmt >> 16); |
| 656 | BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) |
| 657 | .addReg(TmpReg, RegState::Kill) |
| 658 | .addImm(CallerAllocatedAmt & 0xFFFF); |
| 659 | BuildMI(MBB, MBBI, dl, TII.get(ADDInstr)) |
| 660 | .addReg(StackReg) |
| 661 | .addReg(FPReg) |
| 662 | .addReg(TmpReg); |
| 663 | } |
| 664 | } else if (RetOpcode == PPC::TCRETURNdi) { |
| 665 | MBBI = prior(MBB.end()); |
| 666 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 667 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). |
| 668 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 669 | } else if (RetOpcode == PPC::TCRETURNri) { |
| 670 | MBBI = prior(MBB.end()); |
| 671 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 672 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); |
| 673 | } else if (RetOpcode == PPC::TCRETURNai) { |
| 674 | MBBI = prior(MBB.end()); |
| 675 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 676 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); |
| 677 | } else if (RetOpcode == PPC::TCRETURNdi8) { |
| 678 | MBBI = prior(MBB.end()); |
| 679 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 680 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). |
| 681 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 682 | } else if (RetOpcode == PPC::TCRETURNri8) { |
| 683 | MBBI = prior(MBB.end()); |
| 684 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 685 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); |
| 686 | } else if (RetOpcode == PPC::TCRETURNai8) { |
| 687 | MBBI = prior(MBB.end()); |
| 688 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 689 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); |
| 690 | } |
| 691 | } |