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