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