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