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