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