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" |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 19 | #include "PPCTargetMachine.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterScavenging.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetOptions.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 31 | /// VRRegNo - Map from a numbered VR register to its enum value. |
| 32 | /// |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 33 | static const MCPhysReg VRRegNo[] = { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 34 | PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , |
| 35 | PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15, |
| 36 | PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23, |
| 37 | PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31 |
| 38 | }; |
| 39 | |
Eric Christopher | f71609b | 2015-02-13 00:39:27 +0000 | [diff] [blame] | 40 | static unsigned computeReturnSaveOffset(const PPCSubtarget &STI) { |
| 41 | if (STI.isDarwinABI()) |
| 42 | return STI.isPPC64() ? 16 : 8; |
| 43 | // SVR4 ABI: |
| 44 | return STI.isPPC64() ? 16 : 4; |
| 45 | } |
| 46 | |
Eric Christopher | 736d39e | 2015-02-13 00:39:36 +0000 | [diff] [blame] | 47 | static unsigned computeTOCSaveOffset(const PPCSubtarget &STI) { |
| 48 | return STI.isELFv2ABI() ? 24 : 40; |
| 49 | } |
| 50 | |
Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 51 | static unsigned computeFramePointerSaveOffset(const PPCSubtarget &STI) { |
| 52 | // For the Darwin ABI: |
| 53 | // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area |
| 54 | // for saving the frame pointer (if needed.) While the published ABI has |
| 55 | // not used this slot since at least MacOSX 10.2, there is older code |
| 56 | // around that does use it, and that needs to continue to work. |
| 57 | if (STI.isDarwinABI()) |
| 58 | return STI.isPPC64() ? -8U : -4U; |
| 59 | |
| 60 | // SVR4 ABI: First slot in the general register save area. |
| 61 | return STI.isPPC64() ? -8U : -4U; |
| 62 | } |
| 63 | |
Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 64 | static unsigned computeLinkageSize(const PPCSubtarget &STI) { |
| 65 | if (STI.isDarwinABI() || STI.isPPC64()) |
| 66 | return (STI.isELFv2ABI() ? 4 : 6) * (STI.isPPC64() ? 8 : 4); |
| 67 | |
| 68 | // SVR4 ABI: |
| 69 | return 8; |
| 70 | } |
| 71 | |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 72 | static unsigned computeBasePointerSaveOffset(const PPCSubtarget &STI) { |
| 73 | if (STI.isDarwinABI()) |
| 74 | return STI.isPPC64() ? -16U : -8U; |
| 75 | |
| 76 | // SVR4 ABI: First slot in the general register save area. |
| 77 | return STI.isPPC64() |
| 78 | ? -16U |
Rafael Espindola | 248cfb9 | 2016-06-28 12:49:12 +0000 | [diff] [blame] | 79 | : STI.getTargetMachine().isPositionIndependent() ? -12U : -8U; |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Eric Christopher | d104c31 | 2014-06-12 20:54:11 +0000 | [diff] [blame] | 82 | PPCFrameLowering::PPCFrameLowering(const PPCSubtarget &STI) |
| 83 | : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 84 | STI.getPlatformStackAlignment(), 0), |
Eric Christopher | 736d39e | 2015-02-13 00:39:36 +0000 | [diff] [blame] | 85 | Subtarget(STI), ReturnSaveOffset(computeReturnSaveOffset(Subtarget)), |
Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 86 | TOCSaveOffset(computeTOCSaveOffset(Subtarget)), |
Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 87 | FramePointerSaveOffset(computeFramePointerSaveOffset(Subtarget)), |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 88 | LinkageSize(computeLinkageSize(Subtarget)), |
| 89 | BasePointerSaveOffset(computeBasePointerSaveOffset(STI)) {} |
Eric Christopher | d104c31 | 2014-06-12 20:54:11 +0000 | [diff] [blame] | 90 | |
Eric Christopher | d104c31 | 2014-06-12 20:54:11 +0000 | [diff] [blame] | 91 | // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack. |
| 92 | const PPCFrameLowering::SpillSlot *PPCFrameLowering::getCalleeSavedSpillSlots( |
| 93 | unsigned &NumEntries) const { |
| 94 | if (Subtarget.isDarwinABI()) { |
| 95 | NumEntries = 1; |
| 96 | if (Subtarget.isPPC64()) { |
| 97 | static const SpillSlot darwin64Offsets = {PPC::X31, -8}; |
| 98 | return &darwin64Offsets; |
| 99 | } else { |
| 100 | static const SpillSlot darwinOffsets = {PPC::R31, -4}; |
| 101 | return &darwinOffsets; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Early exit if not using the SVR4 ABI. |
| 106 | if (!Subtarget.isSVR4ABI()) { |
| 107 | NumEntries = 0; |
| 108 | return nullptr; |
| 109 | } |
| 110 | |
| 111 | // Note that the offsets here overlap, but this is fixed up in |
| 112 | // processFunctionBeforeFrameFinalized. |
| 113 | |
| 114 | static const SpillSlot Offsets[] = { |
| 115 | // Floating-point register save area offsets. |
| 116 | {PPC::F31, -8}, |
| 117 | {PPC::F30, -16}, |
| 118 | {PPC::F29, -24}, |
| 119 | {PPC::F28, -32}, |
| 120 | {PPC::F27, -40}, |
| 121 | {PPC::F26, -48}, |
| 122 | {PPC::F25, -56}, |
| 123 | {PPC::F24, -64}, |
| 124 | {PPC::F23, -72}, |
| 125 | {PPC::F22, -80}, |
| 126 | {PPC::F21, -88}, |
| 127 | {PPC::F20, -96}, |
| 128 | {PPC::F19, -104}, |
| 129 | {PPC::F18, -112}, |
| 130 | {PPC::F17, -120}, |
| 131 | {PPC::F16, -128}, |
| 132 | {PPC::F15, -136}, |
| 133 | {PPC::F14, -144}, |
| 134 | |
| 135 | // General register save area offsets. |
| 136 | {PPC::R31, -4}, |
| 137 | {PPC::R30, -8}, |
| 138 | {PPC::R29, -12}, |
| 139 | {PPC::R28, -16}, |
| 140 | {PPC::R27, -20}, |
| 141 | {PPC::R26, -24}, |
| 142 | {PPC::R25, -28}, |
| 143 | {PPC::R24, -32}, |
| 144 | {PPC::R23, -36}, |
| 145 | {PPC::R22, -40}, |
| 146 | {PPC::R21, -44}, |
| 147 | {PPC::R20, -48}, |
| 148 | {PPC::R19, -52}, |
| 149 | {PPC::R18, -56}, |
| 150 | {PPC::R17, -60}, |
| 151 | {PPC::R16, -64}, |
| 152 | {PPC::R15, -68}, |
| 153 | {PPC::R14, -72}, |
| 154 | |
| 155 | // CR save area offset. We map each of the nonvolatile CR fields |
| 156 | // to the slot for CR2, which is the first of the nonvolatile CR |
| 157 | // fields to be assigned, so that we only allocate one save slot. |
| 158 | // See PPCRegisterInfo::hasReservedSpillSlot() for more information. |
| 159 | {PPC::CR2, -4}, |
| 160 | |
| 161 | // VRSAVE save area offset. |
| 162 | {PPC::VRSAVE, -4}, |
| 163 | |
| 164 | // Vector register save area |
| 165 | {PPC::V31, -16}, |
| 166 | {PPC::V30, -32}, |
| 167 | {PPC::V29, -48}, |
| 168 | {PPC::V28, -64}, |
| 169 | {PPC::V27, -80}, |
| 170 | {PPC::V26, -96}, |
| 171 | {PPC::V25, -112}, |
| 172 | {PPC::V24, -128}, |
| 173 | {PPC::V23, -144}, |
| 174 | {PPC::V22, -160}, |
| 175 | {PPC::V21, -176}, |
| 176 | {PPC::V20, -192}}; |
| 177 | |
| 178 | static const SpillSlot Offsets64[] = { |
| 179 | // Floating-point register save area offsets. |
| 180 | {PPC::F31, -8}, |
| 181 | {PPC::F30, -16}, |
| 182 | {PPC::F29, -24}, |
| 183 | {PPC::F28, -32}, |
| 184 | {PPC::F27, -40}, |
| 185 | {PPC::F26, -48}, |
| 186 | {PPC::F25, -56}, |
| 187 | {PPC::F24, -64}, |
| 188 | {PPC::F23, -72}, |
| 189 | {PPC::F22, -80}, |
| 190 | {PPC::F21, -88}, |
| 191 | {PPC::F20, -96}, |
| 192 | {PPC::F19, -104}, |
| 193 | {PPC::F18, -112}, |
| 194 | {PPC::F17, -120}, |
| 195 | {PPC::F16, -128}, |
| 196 | {PPC::F15, -136}, |
| 197 | {PPC::F14, -144}, |
| 198 | |
| 199 | // General register save area offsets. |
| 200 | {PPC::X31, -8}, |
| 201 | {PPC::X30, -16}, |
| 202 | {PPC::X29, -24}, |
| 203 | {PPC::X28, -32}, |
| 204 | {PPC::X27, -40}, |
| 205 | {PPC::X26, -48}, |
| 206 | {PPC::X25, -56}, |
| 207 | {PPC::X24, -64}, |
| 208 | {PPC::X23, -72}, |
| 209 | {PPC::X22, -80}, |
| 210 | {PPC::X21, -88}, |
| 211 | {PPC::X20, -96}, |
| 212 | {PPC::X19, -104}, |
| 213 | {PPC::X18, -112}, |
| 214 | {PPC::X17, -120}, |
| 215 | {PPC::X16, -128}, |
| 216 | {PPC::X15, -136}, |
| 217 | {PPC::X14, -144}, |
| 218 | |
| 219 | // VRSAVE save area offset. |
| 220 | {PPC::VRSAVE, -4}, |
| 221 | |
| 222 | // Vector register save area |
| 223 | {PPC::V31, -16}, |
| 224 | {PPC::V30, -32}, |
| 225 | {PPC::V29, -48}, |
| 226 | {PPC::V28, -64}, |
| 227 | {PPC::V27, -80}, |
| 228 | {PPC::V26, -96}, |
| 229 | {PPC::V25, -112}, |
| 230 | {PPC::V24, -128}, |
| 231 | {PPC::V23, -144}, |
| 232 | {PPC::V22, -160}, |
| 233 | {PPC::V21, -176}, |
| 234 | {PPC::V20, -192}}; |
| 235 | |
| 236 | if (Subtarget.isPPC64()) { |
| 237 | NumEntries = array_lengthof(Offsets64); |
| 238 | |
| 239 | return Offsets64; |
| 240 | } else { |
| 241 | NumEntries = array_lengthof(Offsets); |
| 242 | |
| 243 | return Offsets; |
| 244 | } |
| 245 | } |
| 246 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 247 | /// RemoveVRSaveCode - We have found that this function does not need any code |
| 248 | /// to manipulate the VRSAVE register, even though it uses vector registers. |
| 249 | /// This can happen when the only registers used are known to be live in or out |
| 250 | /// 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] | 251 | /// FIXME: The removal of the code results in a compile failure at -O0 when the |
| 252 | /// function contains a function call, as the GPR containing original VRSAVE |
| 253 | /// contents is spilled and reloaded around the call. Without the prolog code, |
| 254 | /// the spill instruction refers to an undefined register. This code needs |
| 255 | /// to account for all uses of that GPR. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 256 | static void RemoveVRSaveCode(MachineInstr &MI) { |
| 257 | MachineBasicBlock *Entry = MI.getParent(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 258 | MachineFunction *MF = Entry->getParent(); |
| 259 | |
| 260 | // We know that the MTVRSAVE instruction immediately follows MI. Remove it. |
| 261 | MachineBasicBlock::iterator MBBI = MI; |
| 262 | ++MBBI; |
| 263 | assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE); |
| 264 | MBBI->eraseFromParent(); |
| 265 | |
| 266 | bool RemovedAllMTVRSAVEs = true; |
| 267 | // See if we can find and remove the MTVRSAVE instruction from all of the |
| 268 | // epilog blocks. |
| 269 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) { |
| 270 | // If last instruction is a return instruction, add an epilogue |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 271 | if (I->isReturnBlock()) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 272 | bool FoundIt = false; |
| 273 | for (MBBI = I->end(); MBBI != I->begin(); ) { |
| 274 | --MBBI; |
| 275 | if (MBBI->getOpcode() == PPC::MTVRSAVE) { |
| 276 | MBBI->eraseFromParent(); // remove it. |
| 277 | FoundIt = true; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | RemovedAllMTVRSAVEs &= FoundIt; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // If we found and removed all MTVRSAVE instructions, remove the read of |
| 286 | // VRSAVE as well. |
| 287 | if (RemovedAllMTVRSAVEs) { |
| 288 | MBBI = MI; |
| 289 | assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?"); |
| 290 | --MBBI; |
| 291 | assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?"); |
| 292 | MBBI->eraseFromParent(); |
| 293 | } |
| 294 | |
| 295 | // Finally, nuke the UPDATE_VRSAVE. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 296 | MI.eraseFromParent(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the |
| 300 | // instruction selector. Based on the vector registers that have been used, |
| 301 | // transform this into the appropriate ORI instruction. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 302 | static void HandleVRSaveUpdate(MachineInstr &MI, const TargetInstrInfo &TII) { |
| 303 | MachineFunction *MF = MI.getParent()->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 304 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 305 | DebugLoc dl = MI.getDebugLoc(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 306 | |
Matthias Braun | 9912bb8 | 2015-07-14 17:52:07 +0000 | [diff] [blame] | 307 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 308 | unsigned UsedRegMask = 0; |
| 309 | for (unsigned i = 0; i != 32; ++i) |
Matthias Braun | 9912bb8 | 2015-07-14 17:52:07 +0000 | [diff] [blame] | 310 | if (MRI.isPhysRegModified(VRRegNo[i])) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 311 | UsedRegMask |= 1 << (31-i); |
| 312 | |
| 313 | // Live in and live out values already must be in the mask, so don't bother |
| 314 | // marking them. |
| 315 | for (MachineRegisterInfo::livein_iterator |
| 316 | I = MF->getRegInfo().livein_begin(), |
| 317 | E = MF->getRegInfo().livein_end(); I != E; ++I) { |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 318 | unsigned RegNo = TRI->getEncodingValue(I->first); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 319 | if (VRRegNo[RegNo] == I->first) // If this really is a vector reg. |
| 320 | UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked. |
| 321 | } |
Jakob Stoklund Olesen | bf034db | 2013-02-05 17:40:36 +0000 | [diff] [blame] | 322 | |
| 323 | // Live out registers appear as use operands on return instructions. |
| 324 | for (MachineFunction::const_iterator BI = MF->begin(), BE = MF->end(); |
| 325 | UsedRegMask != 0 && BI != BE; ++BI) { |
| 326 | const MachineBasicBlock &MBB = *BI; |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 327 | if (!MBB.isReturnBlock()) |
Jakob Stoklund Olesen | bf034db | 2013-02-05 17:40:36 +0000 | [diff] [blame] | 328 | continue; |
| 329 | const MachineInstr &Ret = MBB.back(); |
| 330 | for (unsigned I = 0, E = Ret.getNumOperands(); I != E; ++I) { |
| 331 | const MachineOperand &MO = Ret.getOperand(I); |
| 332 | if (!MO.isReg() || !PPC::VRRCRegClass.contains(MO.getReg())) |
| 333 | continue; |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 334 | unsigned RegNo = TRI->getEncodingValue(MO.getReg()); |
Jakob Stoklund Olesen | bf034db | 2013-02-05 17:40:36 +0000 | [diff] [blame] | 335 | UsedRegMask &= ~(1 << (31-RegNo)); |
| 336 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // If no registers are used, turn this into a copy. |
| 340 | if (UsedRegMask == 0) { |
| 341 | // Remove all VRSAVE code. |
| 342 | RemoveVRSaveCode(MI); |
| 343 | return; |
| 344 | } |
| 345 | |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 346 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 347 | unsigned DstReg = MI.getOperand(0).getReg(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 348 | |
| 349 | if ((UsedRegMask & 0xFFFF) == UsedRegMask) { |
| 350 | if (DstReg != SrcReg) |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 351 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 352 | .addReg(SrcReg) |
| 353 | .addImm(UsedRegMask); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 354 | else |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 355 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 356 | .addReg(SrcReg, RegState::Kill) |
| 357 | .addImm(UsedRegMask); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 358 | } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) { |
| 359 | if (DstReg != SrcReg) |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 360 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 361 | .addReg(SrcReg) |
| 362 | .addImm(UsedRegMask >> 16); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 363 | else |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 364 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 365 | .addReg(SrcReg, RegState::Kill) |
| 366 | .addImm(UsedRegMask >> 16); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 367 | } else { |
| 368 | if (DstReg != SrcReg) |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 369 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 370 | .addReg(SrcReg) |
| 371 | .addImm(UsedRegMask >> 16); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 372 | else |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 373 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORIS), DstReg) |
| 374 | .addReg(SrcReg, RegState::Kill) |
| 375 | .addImm(UsedRegMask >> 16); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 376 | |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 377 | BuildMI(*MI.getParent(), MI, dl, TII.get(PPC::ORI), DstReg) |
| 378 | .addReg(DstReg, RegState::Kill) |
| 379 | .addImm(UsedRegMask & 0xFFFF); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Remove the old UPDATE_VRSAVE instruction. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 383 | MI.eraseFromParent(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 386 | static bool spillsCR(const MachineFunction &MF) { |
| 387 | const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 388 | return FuncInfo->isCRSpilled(); |
| 389 | } |
| 390 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 391 | static bool spillsVRSAVE(const MachineFunction &MF) { |
| 392 | const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 393 | return FuncInfo->isVRSAVESpilled(); |
| 394 | } |
| 395 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 396 | static bool hasSpills(const MachineFunction &MF) { |
| 397 | const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 398 | return FuncInfo->hasSpills(); |
| 399 | } |
| 400 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 401 | static bool hasNonRISpills(const MachineFunction &MF) { |
| 402 | const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 403 | return FuncInfo->hasNonRISpills(); |
| 404 | } |
| 405 | |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 406 | /// MustSaveLR - Return true if this function requires that we save the LR |
| 407 | /// register onto the stack in the prolog and restore it in the epilog of the |
| 408 | /// function. |
| 409 | static bool MustSaveLR(const MachineFunction &MF, unsigned LR) { |
| 410 | const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>(); |
| 411 | |
| 412 | // We need a save/restore of LR if there is any def of LR (which is |
| 413 | // defined by calls, including the PIC setup sequence), or if there is |
| 414 | // some use of the LR stack slot (e.g. for builtin_return_address). |
| 415 | // (LR comes in 32 and 64 bit versions.) |
| 416 | MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR); |
| 417 | return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired(); |
| 418 | } |
| 419 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 420 | /// determineFrameLayout - Determine the size of the frame and maximum call |
| 421 | /// frame size. |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 422 | unsigned PPCFrameLowering::determineFrameLayout(MachineFunction &MF, |
| 423 | bool UpdateMF, |
| 424 | bool UseEstimate) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 425 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 426 | |
| 427 | // Get the number of bytes to allocate from the FrameInfo |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 428 | unsigned FrameSize = |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 429 | UseEstimate ? MFI.estimateStackSize(MF) : MFI.getStackSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 430 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 431 | // Get stack alignments. The frame must be aligned to the greatest of these: |
| 432 | unsigned TargetAlign = getStackAlignment(); // alignment required per the ABI |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 433 | unsigned MaxAlign = MFI.getMaxAlignment(); // algmt required by data in frame |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 434 | unsigned AlignMask = std::max(MaxAlign, TargetAlign) - 1; |
| 435 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 436 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 437 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 438 | |
| 439 | // If we are a leaf function, and use up to 224 bytes of stack space, |
| 440 | // 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] | 441 | // to adjust the stack pointer (we fit in the Red Zone). |
Bill Schmidt | 8ea7af8 | 2013-02-26 21:28:57 +0000 | [diff] [blame] | 442 | // The 32-bit SVR4 ABI has no Red Zone. However, it can still generate |
| 443 | // stackless code if all local vars are reg-allocated. |
Duncan P. N. Exon Smith | 5bedaf93 | 2015-02-14 02:54:07 +0000 | [diff] [blame] | 444 | bool DisableRedZone = MF.getFunction()->hasFnAttribute(Attribute::NoRedZone); |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 445 | unsigned LR = RegInfo->getRARegister(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 446 | if (!DisableRedZone && |
Bill Schmidt | 8ea7af8 | 2013-02-26 21:28:57 +0000 | [diff] [blame] | 447 | (Subtarget.isPPC64() || // 32-bit SVR4, no stack- |
| 448 | !Subtarget.isSVR4ABI() || // allocated locals. |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 449 | FrameSize == 0) && |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 450 | FrameSize <= 224 && // Fits in red zone. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 451 | !MFI.hasVarSizedObjects() && // No dynamic alloca. |
| 452 | !MFI.adjustsStack() && // No calls. |
Bill Schmidt | 82f1c77 | 2015-02-10 19:09:05 +0000 | [diff] [blame] | 453 | !MustSaveLR(MF, LR) && |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 454 | !RegInfo->hasBasePointer(MF)) { // No special alignment. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 455 | // No need for frame |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 456 | if (UpdateMF) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 457 | MFI.setStackSize(0); |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 458 | return 0; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Get the maximum call frame size of all the calls. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 462 | unsigned maxCallFrameSize = MFI.getMaxCallFrameSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 463 | |
Ulrich Weigand | f316e1d | 2014-06-23 13:47:52 +0000 | [diff] [blame] | 464 | // Maximum call frame needs to be at least big enough for linkage area. |
Eric Christopher | a4ae213 | 2015-02-13 22:22:57 +0000 | [diff] [blame] | 465 | unsigned minCallFrameSize = getLinkageSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 466 | maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize); |
| 467 | |
| 468 | // If we have dynamic alloca then maxCallFrameSize needs to be aligned so |
| 469 | // that allocations will be aligned. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 470 | if (MFI.hasVarSizedObjects()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 471 | maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask; |
| 472 | |
| 473 | // Update maximum call frame size. |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 474 | if (UpdateMF) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 475 | MFI.setMaxCallFrameSize(maxCallFrameSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 476 | |
| 477 | // Include call frame size in total. |
| 478 | FrameSize += maxCallFrameSize; |
| 479 | |
| 480 | // Make sure the frame is aligned. |
| 481 | FrameSize = (FrameSize + AlignMask) & ~AlignMask; |
| 482 | |
| 483 | // Update frame info. |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 484 | if (UpdateMF) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 485 | MFI.setStackSize(FrameSize); |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 486 | |
| 487 | return FrameSize; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 490 | // hasFP - Return true if the specified function actually has a dedicated frame |
| 491 | // pointer register. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 492 | bool PPCFrameLowering::hasFP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 493 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 3eb4fed | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 494 | // FIXME: This is pretty much broken by design: hasFP() might be called really |
| 495 | // early, before the stack layout was calculated and thus hasFP() might return |
| 496 | // true or false here depending on the time of call. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 497 | return (MFI.getStackSize()) && needsFP(MF); |
Anton Korobeynikov | 3eb4fed | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // needsFP - Return true if the specified function should have a dedicated frame |
| 501 | // pointer register. This is true if the function has variable sized allocas or |
| 502 | // if frame pointer elimination is disabled. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 503 | bool PPCFrameLowering::needsFP(const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 504 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 505 | |
| 506 | // Naked functions have no stack frame pushed, so we don't have a frame |
| 507 | // pointer. |
Duncan P. N. Exon Smith | 5bedaf93 | 2015-02-14 02:54:07 +0000 | [diff] [blame] | 508 | if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 509 | return false; |
| 510 | |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 511 | return MF.getTarget().Options.DisableFramePointerElim(MF) || |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 512 | MFI.hasVarSizedObjects() || MFI.hasStackMap() || MFI.hasPatchPoint() || |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 513 | (MF.getTarget().Options.GuaranteedTailCallOpt && |
| 514 | MF.getInfo<PPCFunctionInfo>()->hasFastCall()); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 517 | void PPCFrameLowering::replaceFPWithRealFP(MachineFunction &MF) const { |
| 518 | bool is31 = needsFP(MF); |
| 519 | unsigned FPReg = is31 ? PPC::R31 : PPC::R1; |
| 520 | unsigned FP8Reg = is31 ? PPC::X31 : PPC::X1; |
| 521 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 522 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 523 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 524 | bool HasBP = RegInfo->hasBasePointer(MF); |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 525 | unsigned BPReg = HasBP ? (unsigned) RegInfo->getBaseRegister(MF) : FPReg; |
Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 526 | unsigned BP8Reg = HasBP ? (unsigned) PPC::X30 : FPReg; |
| 527 | |
Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 528 | for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); |
| 529 | BI != BE; ++BI) |
| 530 | for (MachineBasicBlock::iterator MBBI = BI->end(); MBBI != BI->begin(); ) { |
| 531 | --MBBI; |
| 532 | for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) { |
| 533 | MachineOperand &MO = MBBI->getOperand(I); |
| 534 | if (!MO.isReg()) |
| 535 | continue; |
| 536 | |
| 537 | switch (MO.getReg()) { |
| 538 | case PPC::FP: |
| 539 | MO.setReg(FPReg); |
| 540 | break; |
| 541 | case PPC::FP8: |
| 542 | MO.setReg(FP8Reg); |
| 543 | break; |
Hal Finkel | f05d6c7 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 544 | case PPC::BP: |
| 545 | MO.setReg(BPReg); |
| 546 | break; |
| 547 | case PPC::BP8: |
| 548 | MO.setReg(BP8Reg); |
| 549 | break; |
| 550 | |
Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | } |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 555 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 556 | /* This function will do the following: |
| 557 | - If MBB is an entry or exit block, set SR1 and SR2 to R0 and R12 |
| 558 | respectively (defaults recommended by the ABI) and return true |
| 559 | - If MBB is not an entry block, initialize the register scavenger and look |
| 560 | for available registers. |
| 561 | - If the defaults (R0/R12) are available, return true |
| 562 | - If TwoUniqueRegsRequired is set to true, it looks for two unique |
| 563 | registers. Otherwise, look for a single available register. |
| 564 | - If the required registers are found, set SR1 and SR2 and return true. |
| 565 | - If the required registers are not found, set SR2 or both SR1 and SR2 to |
| 566 | PPC::NoRegister and return false. |
| 567 | |
| 568 | Note that if both SR1 and SR2 are valid parameters and TwoUniqueRegsRequired |
| 569 | is not set, this function will attempt to find two different registers, but |
| 570 | still return true if only one register is available (and set SR1 == SR2). |
| 571 | */ |
| 572 | bool |
| 573 | PPCFrameLowering::findScratchRegister(MachineBasicBlock *MBB, |
| 574 | bool UseAtEnd, |
| 575 | bool TwoUniqueRegsRequired, |
| 576 | unsigned *SR1, |
| 577 | unsigned *SR2) const { |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 578 | RegScavenger RS; |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 579 | unsigned R0 = Subtarget.isPPC64() ? PPC::X0 : PPC::R0; |
| 580 | unsigned R12 = Subtarget.isPPC64() ? PPC::X12 : PPC::R12; |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 581 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 582 | // Set the defaults for the two scratch registers. |
| 583 | if (SR1) |
| 584 | *SR1 = R0; |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 585 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 586 | if (SR2) { |
| 587 | assert (SR1 && "Asking for the second scratch register but not the first?"); |
| 588 | *SR2 = R12; |
| 589 | } |
| 590 | |
| 591 | // If MBB is an entry or exit block, use R0 and R12 as the scratch registers. |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 592 | if ((UseAtEnd && MBB->isReturnBlock()) || |
| 593 | (!UseAtEnd && (&MBB->getParent()->front() == MBB))) |
| 594 | return true; |
Alexey Samsonov | 39b7d65 | 2015-12-02 21:25:28 +0000 | [diff] [blame] | 595 | |
Matthias Braun | 7dc03f0 | 2016-04-06 02:47:09 +0000 | [diff] [blame] | 596 | RS.enterBasicBlock(*MBB); |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 597 | |
Kit Barton | f4ce2f3 | 2015-11-30 18:59:41 +0000 | [diff] [blame] | 598 | if (UseAtEnd && !MBB->empty()) { |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 599 | // The scratch register will be used at the end of the block, so must |
| 600 | // consider all registers used within the block |
Kit Barton | f4ce2f3 | 2015-11-30 18:59:41 +0000 | [diff] [blame] | 601 | |
| 602 | MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator(); |
| 603 | // If no terminator, back iterator up to previous instruction. |
| 604 | if (MBBI == MBB->end()) |
| 605 | MBBI = std::prev(MBBI); |
| 606 | |
| 607 | if (MBBI != MBB->begin()) |
| 608 | RS.forward(MBBI); |
| 609 | } |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 610 | |
| 611 | // If the two registers are available, we're all good. |
| 612 | // Note that we only return here if both R0 and R12 are available because |
| 613 | // although the function may not require two unique registers, it may benefit |
| 614 | // from having two so we should try to provide them. |
| 615 | if (!RS.isRegUsed(R0) && !RS.isRegUsed(R12)) |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 616 | return true; |
| 617 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 618 | // Get the list of callee-saved registers for the target. |
| 619 | const PPCRegisterInfo *RegInfo = |
| 620 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
| 621 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(MBB->getParent()); |
| 622 | |
| 623 | // Get all the available registers in the block. |
| 624 | BitVector BV = RS.getRegsAvailable(Subtarget.isPPC64() ? &PPC::G8RCRegClass : |
| 625 | &PPC::GPRCRegClass); |
| 626 | |
| 627 | // We shouldn't use callee-saved registers as scratch registers as they may be |
| 628 | // available when looking for a candidate block for shrink wrapping but not |
| 629 | // available when the actual prologue/epilogue is being emitted because they |
| 630 | // were added as live-in to the prologue block by PrologueEpilogueInserter. |
| 631 | for (int i = 0; CSRegs[i]; ++i) |
| 632 | BV.reset(CSRegs[i]); |
| 633 | |
| 634 | // Set the first scratch register to the first available one. |
| 635 | if (SR1) { |
| 636 | int FirstScratchReg = BV.find_first(); |
| 637 | *SR1 = FirstScratchReg == -1 ? (unsigned)PPC::NoRegister : FirstScratchReg; |
| 638 | } |
| 639 | |
| 640 | // If there is another one available, set the second scratch register to that. |
| 641 | // Otherwise, set it to either PPC::NoRegister if this function requires two |
| 642 | // or to whatever SR1 is set to if this function doesn't require two. |
| 643 | if (SR2) { |
| 644 | int SecondScratchReg = BV.find_next(*SR1); |
| 645 | if (SecondScratchReg != -1) |
| 646 | *SR2 = SecondScratchReg; |
| 647 | else |
| 648 | *SR2 = TwoUniqueRegsRequired ? (unsigned)PPC::NoRegister : *SR1; |
| 649 | } |
| 650 | |
| 651 | // Now that we've done our best to provide both registers, double check |
| 652 | // whether we were unable to provide enough. |
Aaron Ballman | 8374c1f | 2016-02-23 15:02:43 +0000 | [diff] [blame] | 653 | if (BV.count() < (TwoUniqueRegsRequired ? 2U : 1U)) |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 654 | return false; |
| 655 | |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 659 | // We need a scratch register for spilling LR and for spilling CR. By default, |
| 660 | // we use two scratch registers to hide latency. However, if only one scratch |
| 661 | // register is available, we can adjust for that by not overlapping the spill |
| 662 | // code. However, if we need to realign the stack (i.e. have a base pointer) |
| 663 | // and the stack frame is large, we need two scratch registers. |
| 664 | bool |
| 665 | PPCFrameLowering::twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const { |
| 666 | const PPCRegisterInfo *RegInfo = |
| 667 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
| 668 | MachineFunction &MF = *(MBB->getParent()); |
| 669 | bool HasBP = RegInfo->hasBasePointer(MF); |
| 670 | unsigned FrameSize = determineFrameLayout(MF, false); |
| 671 | int NegFrameSize = -FrameSize; |
| 672 | bool IsLargeFrame = !isInt<16>(NegFrameSize); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 673 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 674 | unsigned MaxAlign = MFI.getMaxAlignment(); |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 675 | bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI(); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 676 | |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 677 | return (IsLargeFrame || !HasRedZone) && HasBP && MaxAlign > 1; |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 680 | bool PPCFrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const { |
| 681 | MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); |
| 682 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 683 | return findScratchRegister(TmpMBB, false, |
| 684 | twoUniqueScratchRegsRequired(TmpMBB)); |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool PPCFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const { |
| 688 | MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); |
| 689 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 690 | return findScratchRegister(TmpMBB, true); |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 693 | void PPCFrameLowering::emitPrologue(MachineFunction &MF, |
| 694 | MachineBasicBlock &MBB) const { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 695 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 696 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 697 | const PPCInstrInfo &TII = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 698 | *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo()); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 699 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 700 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 701 | |
| 702 | MachineModuleInfo &MMI = MF.getMMI(); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 703 | const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 704 | DebugLoc dl; |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 705 | bool needsCFI = MMI.hasDebugInfo() || |
Rafael Espindola | fc9bae6 | 2011-05-25 03:44:17 +0000 | [diff] [blame] | 706 | MF.getFunction()->needsUnwindTableEntry(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 707 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 708 | // Get processor type. |
| 709 | bool isPPC64 = Subtarget.isPPC64(); |
| 710 | // Get the ABI. |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 711 | bool isSVR4ABI = Subtarget.isSVR4ABI(); |
Ulrich Weigand | be928cc | 2014-07-21 00:03:18 +0000 | [diff] [blame] | 712 | bool isELFv2ABI = Subtarget.isELFv2ABI(); |
Chandler Carruth | 003ed33 | 2015-02-14 09:14:44 +0000 | [diff] [blame] | 713 | assert((Subtarget.isDarwinABI() || isSVR4ABI) && |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 714 | "Currently only Darwin and SVR4 ABIs are supported for PowerPC."); |
| 715 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 716 | // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it, |
| 717 | // process it. |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 718 | if (!isSVR4ABI) |
Bill Schmidt | 38d9458 | 2012-10-10 20:54:15 +0000 | [diff] [blame] | 719 | for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) { |
| 720 | if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) { |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 721 | HandleVRSaveUpdate(*MBBI, TII); |
Bill Schmidt | 38d9458 | 2012-10-10 20:54:15 +0000 | [diff] [blame] | 722 | break; |
| 723 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 724 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 725 | |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 726 | // Move MBBI back to the beginning of the prologue block. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 727 | MBBI = MBB.begin(); |
| 728 | |
| 729 | // Work out frame sizes. |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 730 | unsigned FrameSize = determineFrameLayout(MF); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 731 | int NegFrameSize = -FrameSize; |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 732 | if (!isInt<32>(NegFrameSize)) |
| 733 | llvm_unreachable("Unhandled stack size!"); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 734 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 735 | if (MFI.isFrameAddressTaken()) |
Hal Finkel | aa03c03 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 736 | replaceFPWithRealFP(MF); |
| 737 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 738 | // Check if the link register (LR) must be saved. |
| 739 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 740 | bool MustSaveLR = FI->mustSaveLR(); |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 741 | const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs(); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 742 | bool MustSaveCR = !MustSaveCRs.empty(); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 743 | // 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] | 744 | bool HasFP = hasFP(MF); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 745 | bool HasBP = RegInfo->hasBasePointer(MF); |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 746 | bool HasRedZone = isPPC64 || !isSVR4ABI; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 747 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 748 | unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1; |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 749 | unsigned BPReg = RegInfo->getBaseRegister(MF); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 750 | unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; |
| 751 | unsigned LRReg = isPPC64 ? PPC::LR8 : PPC::LR; |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 752 | unsigned ScratchReg = 0; |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 753 | unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg |
| 754 | // ...(R12/X12 is volatile in both Darwin & SVR4, & can't be a function arg.) |
| 755 | const MCInstrDesc& MFLRInst = TII.get(isPPC64 ? PPC::MFLR8 |
| 756 | : PPC::MFLR ); |
| 757 | const MCInstrDesc& StoreInst = TII.get(isPPC64 ? PPC::STD |
| 758 | : PPC::STW ); |
| 759 | const MCInstrDesc& StoreUpdtInst = TII.get(isPPC64 ? PPC::STDU |
| 760 | : PPC::STWU ); |
| 761 | const MCInstrDesc& StoreUpdtIdxInst = TII.get(isPPC64 ? PPC::STDUX |
| 762 | : PPC::STWUX); |
| 763 | const MCInstrDesc& LoadImmShiftedInst = TII.get(isPPC64 ? PPC::LIS8 |
| 764 | : PPC::LIS ); |
| 765 | const MCInstrDesc& OrImmInst = TII.get(isPPC64 ? PPC::ORI8 |
| 766 | : PPC::ORI ); |
| 767 | const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8 |
| 768 | : PPC::OR ); |
| 769 | const MCInstrDesc& SubtractCarryingInst = TII.get(isPPC64 ? PPC::SUBFC8 |
| 770 | : PPC::SUBFC); |
| 771 | const MCInstrDesc& SubtractImmCarryingInst = TII.get(isPPC64 ? PPC::SUBFIC8 |
| 772 | : PPC::SUBFIC); |
| 773 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 774 | // Regarding this assert: Even though LR is saved in the caller's frame (i.e., |
| 775 | // LROffset is positive), that slot is callee-owned. Because PPC32 SVR4 has no |
| 776 | // Red Zone, an asynchronous event (a form of "callee") could claim a frame & |
| 777 | // overwrite it, so PPC32 SVR4 must claim at least a minimal frame to save LR. |
| 778 | assert((isPPC64 || !isSVR4ABI || !(!FrameSize && (MustSaveLR || HasFP))) && |
| 779 | "FrameSize must be >0 to save/restore the FP or LR for 32-bit SVR4."); |
| 780 | |
Simon Pilgrim | fbd2221 | 2016-11-20 13:10:51 +0000 | [diff] [blame] | 781 | // Using the same bool variable as below to suppress compiler warnings. |
Nemanja Ivanovic | daf0ca2 | 2016-02-20 20:45:37 +0000 | [diff] [blame] | 782 | bool SingleScratchReg = |
| 783 | findScratchRegister(&MBB, false, twoUniqueScratchRegsRequired(&MBB), |
| 784 | &ScratchReg, &TempReg); |
| 785 | assert(SingleScratchReg && |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 786 | "Required number of registers not available in this block"); |
| 787 | |
Nemanja Ivanovic | daf0ca2 | 2016-02-20 20:45:37 +0000 | [diff] [blame] | 788 | SingleScratchReg = ScratchReg == TempReg; |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 789 | |
Eric Christopher | f71609b | 2015-02-13 00:39:27 +0000 | [diff] [blame] | 790 | int LROffset = getReturnSaveOffset(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 791 | |
| 792 | int FPOffset = 0; |
| 793 | if (HasFP) { |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 794 | if (isSVR4ABI) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 795 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 796 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 797 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 798 | FPOffset = MFI.getObjectOffset(FPIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 799 | } else { |
Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 800 | FPOffset = getFramePointerSaveOffset(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 804 | int BPOffset = 0; |
| 805 | if (HasBP) { |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 806 | if (isSVR4ABI) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 807 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 808 | int BPIndex = FI->getBasePointerSaveIndex(); |
| 809 | assert(BPIndex && "No Base Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 810 | BPOffset = MFI.getObjectOffset(BPIndex); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 811 | } else { |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 812 | BPOffset = getBasePointerSaveOffset(); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 816 | int PBPOffset = 0; |
| 817 | if (FI->usesPICBase()) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 818 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 819 | int PBPIndex = FI->getPICBasePointerSaveIndex(); |
| 820 | assert(PBPIndex && "No PIC Base Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 821 | PBPOffset = MFI.getObjectOffset(PBPIndex); |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 824 | // Get stack alignments. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 825 | unsigned MaxAlign = MFI.getMaxAlignment(); |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 826 | if (HasBP && MaxAlign > 1) |
| 827 | assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) && |
| 828 | "Invalid alignment!"); |
| 829 | |
| 830 | // Frames of 32KB & larger require special handling because they cannot be |
| 831 | // indexed into with a simple STDU/STWU/STD/STW immediate offset operand. |
| 832 | bool isLargeFrame = !isInt<16>(NegFrameSize); |
| 833 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 834 | assert((isPPC64 || !MustSaveCR) && |
| 835 | "Prologue CR saving supported only in 64-bit mode"); |
| 836 | |
| 837 | // If we need to spill the CR and the LR but we don't have two separate |
| 838 | // registers available, we must spill them one at a time |
| 839 | if (MustSaveCR && SingleScratchReg && MustSaveLR) { |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 840 | // In the ELFv2 ABI, we are not required to save all CR fields. |
| 841 | // If only one or two CR fields are clobbered, it is more efficient to use |
| 842 | // mfocrf to selectively save just those fields, because mfocrf has short |
| 843 | // latency compares to mfcr. |
| 844 | unsigned MfcrOpcode = PPC::MFCR8; |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 845 | unsigned CrState = RegState::ImplicitKill; |
| 846 | if (isELFv2ABI && MustSaveCRs.size() == 1) { |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 847 | MfcrOpcode = PPC::MFOCRF8; |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 848 | CrState = RegState::Kill; |
| 849 | } |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 850 | MachineInstrBuilder MIB = |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 851 | BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 852 | for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 853 | MIB.addReg(MustSaveCRs[i], CrState); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 854 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8)) |
| 855 | .addReg(TempReg, getKillRegState(true)) |
| 856 | .addImm(8) |
| 857 | .addReg(SPReg); |
| 858 | } |
| 859 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 860 | if (MustSaveLR) |
| 861 | BuildMI(MBB, MBBI, dl, MFLRInst, ScratchReg); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 862 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 863 | if (MustSaveCR && |
| 864 | !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64 |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 865 | // In the ELFv2 ABI, we are not required to save all CR fields. |
| 866 | // If only one or two CR fields are clobbered, it is more efficient to use |
| 867 | // mfocrf to selectively save just those fields, because mfocrf has short |
| 868 | // latency compares to mfcr. |
| 869 | unsigned MfcrOpcode = PPC::MFCR8; |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 870 | unsigned CrState = RegState::ImplicitKill; |
| 871 | if (isELFv2ABI && MustSaveCRs.size() == 1) { |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 872 | MfcrOpcode = PPC::MFOCRF8; |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 873 | CrState = RegState::Kill; |
| 874 | } |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 875 | MachineInstrBuilder MIB = |
Chuang-Yu Cheng | 6efde2f | 2016-04-12 03:04:44 +0000 | [diff] [blame] | 876 | BuildMI(MBB, MBBI, dl, TII.get(MfcrOpcode), TempReg); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 877 | for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) |
Chuang-Yu Cheng | 8676c3d | 2016-04-27 02:59:28 +0000 | [diff] [blame] | 878 | MIB.addReg(MustSaveCRs[i], CrState); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 881 | if (HasRedZone) { |
| 882 | if (HasFP) |
| 883 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 884 | .addReg(FPReg) |
| 885 | .addImm(FPOffset) |
| 886 | .addReg(SPReg); |
| 887 | if (FI->usesPICBase()) |
| 888 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 889 | .addReg(PPC::R30) |
| 890 | .addImm(PBPOffset) |
| 891 | .addReg(SPReg); |
| 892 | if (HasBP) |
| 893 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 894 | .addReg(BPReg) |
| 895 | .addImm(BPOffset) |
| 896 | .addReg(SPReg); |
| 897 | } |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 898 | |
| 899 | if (MustSaveLR) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 900 | BuildMI(MBB, MBBI, dl, StoreInst) |
Nemanja Ivanovic | 62fba48 | 2016-07-15 19:56:32 +0000 | [diff] [blame] | 901 | .addReg(ScratchReg, getKillRegState(true)) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 902 | .addImm(LROffset) |
| 903 | .addReg(SPReg); |
| 904 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 905 | if (MustSaveCR && |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 906 | !(SingleScratchReg && MustSaveLR)) { // will only occur for PPC64 |
| 907 | assert(HasRedZone && "A red zone is always available on PPC64"); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 908 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STW8)) |
| 909 | .addReg(TempReg, getKillRegState(true)) |
| 910 | .addImm(8) |
| 911 | .addReg(SPReg); |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 912 | } |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 913 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 914 | // Skip the rest if this is a leaf function & all spills fit in the Red Zone. |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 915 | if (!FrameSize) |
| 916 | return; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 917 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 918 | // Adjust stack pointer: r1 += NegFrameSize. |
| 919 | // If there is a preferred stack alignment, align R1 now |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 920 | |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 921 | if (HasBP && HasRedZone) { |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 922 | // Save a copy of r1 as the base pointer. |
| 923 | BuildMI(MBB, MBBI, dl, OrInst, BPReg) |
| 924 | .addReg(SPReg) |
| 925 | .addReg(SPReg); |
| 926 | } |
| 927 | |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 928 | // Have we generated a STUX instruction to claim stack frame? If so, |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 929 | // the negated frame size will be placed in ScratchReg. |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 930 | bool HasSTUX = false; |
| 931 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 932 | // This condition must be kept in sync with canUseAsPrologue. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 933 | if (HasBP && MaxAlign > 1) { |
| 934 | if (isPPC64) |
| 935 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), ScratchReg) |
| 936 | .addReg(SPReg) |
| 937 | .addImm(0) |
| 938 | .addImm(64 - Log2_32(MaxAlign)); |
| 939 | else // PPC32... |
| 940 | BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), ScratchReg) |
| 941 | .addReg(SPReg) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 942 | .addImm(0) |
| 943 | .addImm(32 - Log2_32(MaxAlign)) |
| 944 | .addImm(31); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 945 | if (!isLargeFrame) { |
| 946 | BuildMI(MBB, MBBI, dl, SubtractImmCarryingInst, ScratchReg) |
| 947 | .addReg(ScratchReg, RegState::Kill) |
| 948 | .addImm(NegFrameSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 949 | } else { |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 950 | assert(!SingleScratchReg && "Only a single scratch reg available"); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 951 | BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, TempReg) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 952 | .addImm(NegFrameSize >> 16); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 953 | BuildMI(MBB, MBBI, dl, OrImmInst, TempReg) |
| 954 | .addReg(TempReg, RegState::Kill) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 955 | .addImm(NegFrameSize & 0xFFFF); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 956 | BuildMI(MBB, MBBI, dl, SubtractCarryingInst, ScratchReg) |
| 957 | .addReg(ScratchReg, RegState::Kill) |
| 958 | .addReg(TempReg, RegState::Kill); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 959 | } |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 960 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 961 | BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg) |
| 962 | .addReg(SPReg, RegState::Kill) |
| 963 | .addReg(SPReg) |
| 964 | .addReg(ScratchReg); |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 965 | HasSTUX = true; |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 966 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 967 | } else if (!isLargeFrame) { |
| 968 | BuildMI(MBB, MBBI, dl, StoreUpdtInst, SPReg) |
| 969 | .addReg(SPReg) |
| 970 | .addImm(NegFrameSize) |
| 971 | .addReg(SPReg); |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 972 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 973 | } else { |
| 974 | BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) |
| 975 | .addImm(NegFrameSize >> 16); |
| 976 | BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) |
| 977 | .addReg(ScratchReg, RegState::Kill) |
| 978 | .addImm(NegFrameSize & 0xFFFF); |
| 979 | BuildMI(MBB, MBBI, dl, StoreUpdtIdxInst, SPReg) |
| 980 | .addReg(SPReg, RegState::Kill) |
| 981 | .addReg(SPReg) |
| 982 | .addReg(ScratchReg); |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 983 | HasSTUX = true; |
| 984 | } |
| 985 | |
| 986 | if (!HasRedZone) { |
| 987 | assert(!isPPC64 && "A red zone is always available on PPC64"); |
| 988 | if (HasSTUX) { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 989 | // The negated frame size is in ScratchReg, and the SPReg has been |
| 990 | // decremented by the frame size: SPReg = old SPReg + ScratchReg. |
| 991 | // Since FPOffset, PBPOffset, etc. are relative to the beginning of |
| 992 | // the stack frame (i.e. the old SP), ideally, we would put the old |
| 993 | // SP into a register and use it as the base for the stores. The |
| 994 | // problem is that the only available register may be ScratchReg, |
| 995 | // which could be R0, and R0 cannot be used as a base address. |
| 996 | |
| 997 | // First, set ScratchReg to the old SP. This may need to be modified |
| 998 | // later. |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 999 | BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBF), ScratchReg) |
| 1000 | .addReg(ScratchReg, RegState::Kill) |
| 1001 | .addReg(SPReg); |
| 1002 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1003 | if (ScratchReg == PPC::R0) { |
| 1004 | // R0 cannot be used as a base register, but it can be used as an |
| 1005 | // index in a store-indexed. |
| 1006 | int LastOffset = 0; |
| 1007 | if (HasFP) { |
| 1008 | // R0 += (FPOffset-LastOffset). |
| 1009 | // Need addic, since addi treats R0 as 0. |
| 1010 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) |
| 1011 | .addReg(ScratchReg) |
| 1012 | .addImm(FPOffset-LastOffset); |
| 1013 | LastOffset = FPOffset; |
| 1014 | // Store FP into *R0. |
| 1015 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) |
| 1016 | .addReg(FPReg, RegState::Kill) // Save FP. |
| 1017 | .addReg(PPC::ZERO) |
| 1018 | .addReg(ScratchReg); // This will be the index (R0 is ok here). |
| 1019 | } |
| 1020 | if (FI->usesPICBase()) { |
| 1021 | // R0 += (PBPOffset-LastOffset). |
| 1022 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) |
| 1023 | .addReg(ScratchReg) |
| 1024 | .addImm(PBPOffset-LastOffset); |
| 1025 | LastOffset = PBPOffset; |
| 1026 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) |
| 1027 | .addReg(PPC::R30, RegState::Kill) // Save PIC base pointer. |
| 1028 | .addReg(PPC::ZERO) |
| 1029 | .addReg(ScratchReg); // This will be the index (R0 is ok here). |
| 1030 | } |
| 1031 | if (HasBP) { |
| 1032 | // R0 += (BPOffset-LastOffset). |
| 1033 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), ScratchReg) |
| 1034 | .addReg(ScratchReg) |
| 1035 | .addImm(BPOffset-LastOffset); |
| 1036 | LastOffset = BPOffset; |
| 1037 | BuildMI(MBB, MBBI, dl, TII.get(PPC::STWX)) |
| 1038 | .addReg(BPReg, RegState::Kill) // Save BP. |
| 1039 | .addReg(PPC::ZERO) |
| 1040 | .addReg(ScratchReg); // This will be the index (R0 is ok here). |
| 1041 | // BP = R0-LastOffset |
| 1042 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDIC), BPReg) |
| 1043 | .addReg(ScratchReg, RegState::Kill) |
| 1044 | .addImm(-LastOffset); |
| 1045 | } |
| 1046 | } else { |
| 1047 | // ScratchReg is not R0, so use it as the base register. It is |
| 1048 | // already set to the old SP, so we can use the offsets directly. |
| 1049 | |
| 1050 | // Now that the stack frame has been allocated, save all the necessary |
| 1051 | // registers using ScratchReg as the base address. |
| 1052 | if (HasFP) |
| 1053 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1054 | .addReg(FPReg) |
| 1055 | .addImm(FPOffset) |
| 1056 | .addReg(ScratchReg); |
| 1057 | if (FI->usesPICBase()) |
| 1058 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1059 | .addReg(PPC::R30) |
| 1060 | .addImm(PBPOffset) |
| 1061 | .addReg(ScratchReg); |
| 1062 | if (HasBP) { |
| 1063 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1064 | .addReg(BPReg) |
| 1065 | .addImm(BPOffset) |
| 1066 | .addReg(ScratchReg); |
| 1067 | BuildMI(MBB, MBBI, dl, OrInst, BPReg) |
| 1068 | .addReg(ScratchReg, RegState::Kill) |
| 1069 | .addReg(ScratchReg); |
| 1070 | } |
Krzysztof Parzyszek | 020ec29 | 2016-09-06 12:30:00 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } else { |
| 1073 | // The frame size is a known 16-bit constant (fitting in the immediate |
| 1074 | // field of STWU). To be here we have to be compiling for PPC32. |
| 1075 | // Since the SPReg has been decreased by FrameSize, add it back to each |
| 1076 | // offset. |
| 1077 | if (HasFP) |
| 1078 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1079 | .addReg(FPReg) |
| 1080 | .addImm(FrameSize + FPOffset) |
| 1081 | .addReg(SPReg); |
| 1082 | if (FI->usesPICBase()) |
| 1083 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1084 | .addReg(PPC::R30) |
| 1085 | .addImm(FrameSize + PBPOffset) |
| 1086 | .addReg(SPReg); |
| 1087 | if (HasBP) { |
| 1088 | BuildMI(MBB, MBBI, dl, StoreInst) |
| 1089 | .addReg(BPReg) |
| 1090 | .addImm(FrameSize + BPOffset) |
| 1091 | .addReg(SPReg); |
| 1092 | BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), BPReg) |
| 1093 | .addReg(SPReg) |
| 1094 | .addImm(FrameSize); |
| 1095 | } |
| 1096 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1099 | // Add Call Frame Information for the instructions we generated above. |
| 1100 | if (needsCFI) { |
| 1101 | unsigned CFIIndex; |
| 1102 | |
| 1103 | if (HasBP) { |
| 1104 | // Define CFA in terms of BP. Do this in preference to using FP/SP, |
| 1105 | // because if the stack needed aligning then CFA won't be at a fixed |
| 1106 | // offset from FP/SP. |
| 1107 | unsigned Reg = MRI->getDwarfRegNum(BPReg, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1108 | CFIIndex = MF.addFrameInst( |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1109 | MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); |
| 1110 | } else { |
| 1111 | // Adjust the definition of CFA to account for the change in SP. |
| 1112 | assert(NegFrameSize); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1113 | CFIIndex = MF.addFrameInst( |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1114 | MCCFIInstruction::createDefCfaOffset(nullptr, NegFrameSize)); |
| 1115 | } |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1116 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1117 | .addCFIIndex(CFIIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (HasFP) { |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1120 | // Describe where FP was saved, at a fixed offset from CFA. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1121 | unsigned Reg = MRI->getDwarfRegNum(FPReg, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1122 | CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1123 | MCCFIInstruction::createOffset(nullptr, Reg, FPOffset)); |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1124 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1125 | .addCFIIndex(CFIIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1128 | if (FI->usesPICBase()) { |
| 1129 | // Describe where FP was saved, at a fixed offset from CFA. |
| 1130 | unsigned Reg = MRI->getDwarfRegNum(PPC::R30, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1131 | CFIIndex = MF.addFrameInst( |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1132 | MCCFIInstruction::createOffset(nullptr, Reg, PBPOffset)); |
| 1133 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1134 | .addCFIIndex(CFIIndex); |
| 1135 | } |
| 1136 | |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1137 | if (HasBP) { |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1138 | // Describe where BP was saved, at a fixed offset from CFA. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1139 | unsigned Reg = MRI->getDwarfRegNum(BPReg, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1140 | CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1141 | MCCFIInstruction::createOffset(nullptr, Reg, BPOffset)); |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1142 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1143 | .addCFIIndex(CFIIndex); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1146 | if (MustSaveLR) { |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1147 | // Describe where LR was saved, at a fixed offset from CFA. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1148 | unsigned Reg = MRI->getDwarfRegNum(LRReg, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1149 | CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1150 | MCCFIInstruction::createOffset(nullptr, Reg, LROffset)); |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1151 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1152 | .addCFIIndex(CFIIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1156 | // If there is a frame pointer, copy R1 into R31 |
| 1157 | if (HasFP) { |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1158 | BuildMI(MBB, MBBI, dl, OrInst, FPReg) |
| 1159 | .addReg(SPReg) |
| 1160 | .addReg(SPReg); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1161 | |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1162 | if (!HasBP && needsCFI) { |
| 1163 | // Change the definition of CFA from SP+offset to FP+offset, because SP |
| 1164 | // will change at every alloca. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1165 | unsigned Reg = MRI->getDwarfRegNum(FPReg, true); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1166 | unsigned CFIIndex = MF.addFrameInst( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1167 | MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); |
| 1168 | |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1169 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1170 | .addCFIIndex(CFIIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
Jay Foad | 1f0a44e | 2014-12-01 09:42:32 +0000 | [diff] [blame] | 1174 | if (needsCFI) { |
| 1175 | // Describe where callee saved registers were saved, at fixed offsets from |
| 1176 | // CFA. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1177 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1178 | for (unsigned I = 0, E = CSI.size(); I != E; ++I) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1179 | unsigned Reg = CSI[I].getReg(); |
| 1180 | if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue; |
Rafael Espindola | 08600bc | 2011-05-30 20:20:15 +0000 | [diff] [blame] | 1181 | |
| 1182 | // This is a bit of a hack: CR2LT, CR2GT, CR2EQ and CR2UN are just |
| 1183 | // subregisters of CR2. We just need to emit a move of CR2. |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1184 | if (PPC::CRBITRCRegClass.contains(Reg)) |
Rafael Espindola | 08600bc | 2011-05-30 20:20:15 +0000 | [diff] [blame] | 1185 | continue; |
Rafael Espindola | 08600bc | 2011-05-30 20:20:15 +0000 | [diff] [blame] | 1186 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1187 | // For SVR4, don't emit a move for the CR spill slot if we haven't |
| 1188 | // spilled CRs. |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1189 | if (isSVR4ABI && (PPC::CR2 <= Reg && Reg <= PPC::CR4) |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1190 | && !MustSaveCR) |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1191 | continue; |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1192 | |
| 1193 | // For 64-bit SVR4 when we have spilled CRs, the spill location |
| 1194 | // is SP+8, not a frame-relative slot. |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1195 | if (isSVR4ABI && isPPC64 && (PPC::CR2 <= Reg && Reg <= PPC::CR4)) { |
Ulrich Weigand | be928cc | 2014-07-21 00:03:18 +0000 | [diff] [blame] | 1196 | // In the ELFv1 ABI, only CR2 is noted in CFI and stands in for |
| 1197 | // the whole CR word. In the ELFv2 ABI, every CR that was |
| 1198 | // actually saved gets its own CFI record. |
| 1199 | unsigned CRReg = isELFv2ABI? Reg : (unsigned) PPC::CR2; |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1200 | unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( |
Ulrich Weigand | be928cc | 2014-07-21 00:03:18 +0000 | [diff] [blame] | 1201 | nullptr, MRI->getDwarfRegNum(CRReg, true), 8)); |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1202 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1203 | .addCFIIndex(CFIIndex); |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1204 | continue; |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1207 | int Offset = MFI.getObjectOffset(CSI[I].getFrameIdx()); |
Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame^] | 1208 | unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset( |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1209 | nullptr, MRI->getDwarfRegNum(Reg, true), Offset)); |
Eric Christopher | 612bb69 | 2014-04-29 00:16:46 +0000 | [diff] [blame] | 1210 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 1211 | .addCFIIndex(CFIIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1216 | void PPCFrameLowering::emitEpilogue(MachineFunction &MF, |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1217 | MachineBasicBlock &MBB) const { |
| 1218 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 1219 | DebugLoc dl; |
| 1220 | |
| 1221 | if (MBBI != MBB.end()) |
| 1222 | dl = MBBI->getDebugLoc(); |
| 1223 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1224 | const PPCInstrInfo &TII = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1225 | *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo()); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1226 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1227 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1228 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1229 | // Get alignment info so we know how to restore the SP. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1230 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1231 | |
| 1232 | // Get the number of bytes allocated from the FrameInfo. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1233 | int FrameSize = MFI.getStackSize(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Get processor type. |
| 1236 | bool isPPC64 = Subtarget.isPPC64(); |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1237 | // Get the ABI. |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1238 | bool isSVR4ABI = Subtarget.isSVR4ABI(); |
| 1239 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1240 | // Check if the link register (LR) has been saved. |
| 1241 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 1242 | bool MustSaveLR = FI->mustSaveLR(); |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1243 | const SmallVectorImpl<unsigned> &MustSaveCRs = FI->getMustSaveCRs(); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1244 | bool MustSaveCR = !MustSaveCRs.empty(); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1245 | // 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] | 1246 | bool HasFP = hasFP(MF); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1247 | bool HasBP = RegInfo->hasBasePointer(MF); |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1248 | bool HasRedZone = Subtarget.isPPC64() || !Subtarget.isSVR4ABI(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1249 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1250 | unsigned SPReg = isPPC64 ? PPC::X1 : PPC::R1; |
Hal Finkel | 3ee2af7 | 2014-07-18 23:29:49 +0000 | [diff] [blame] | 1251 | unsigned BPReg = RegInfo->getBaseRegister(MF); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1252 | unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31; |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 1253 | unsigned ScratchReg = 0; |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1254 | unsigned TempReg = isPPC64 ? PPC::X12 : PPC::R12; // another scratch reg |
| 1255 | const MCInstrDesc& MTLRInst = TII.get( isPPC64 ? PPC::MTLR8 |
| 1256 | : PPC::MTLR ); |
| 1257 | const MCInstrDesc& LoadInst = TII.get( isPPC64 ? PPC::LD |
| 1258 | : PPC::LWZ ); |
| 1259 | const MCInstrDesc& LoadImmShiftedInst = TII.get( isPPC64 ? PPC::LIS8 |
| 1260 | : PPC::LIS ); |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1261 | const MCInstrDesc& OrInst = TII.get(isPPC64 ? PPC::OR8 |
| 1262 | : PPC::OR ); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1263 | const MCInstrDesc& OrImmInst = TII.get( isPPC64 ? PPC::ORI8 |
| 1264 | : PPC::ORI ); |
| 1265 | const MCInstrDesc& AddImmInst = TII.get( isPPC64 ? PPC::ADDI8 |
| 1266 | : PPC::ADDI ); |
| 1267 | const MCInstrDesc& AddInst = TII.get( isPPC64 ? PPC::ADD8 |
| 1268 | : PPC::ADD4 ); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1269 | |
Eric Christopher | f71609b | 2015-02-13 00:39:27 +0000 | [diff] [blame] | 1270 | int LROffset = getReturnSaveOffset(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1271 | |
| 1272 | int FPOffset = 0; |
Kit Barton | 9c432ae | 2015-11-16 20:22:15 +0000 | [diff] [blame] | 1273 | |
Simon Pilgrim | fbd2221 | 2016-11-20 13:10:51 +0000 | [diff] [blame] | 1274 | // Using the same bool variable as below to suppress compiler warnings. |
Nemanja Ivanovic | daf0ca2 | 2016-02-20 20:45:37 +0000 | [diff] [blame] | 1275 | bool SingleScratchReg = findScratchRegister(&MBB, true, false, &ScratchReg, |
| 1276 | &TempReg); |
| 1277 | assert(SingleScratchReg && |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1278 | "Could not find an available scratch register"); |
| 1279 | |
Nemanja Ivanovic | daf0ca2 | 2016-02-20 20:45:37 +0000 | [diff] [blame] | 1280 | SingleScratchReg = ScratchReg == TempReg; |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1281 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1282 | if (HasFP) { |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1283 | if (isSVR4ABI) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1284 | int FPIndex = FI->getFramePointerSaveIndex(); |
| 1285 | assert(FPIndex && "No Frame Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1286 | FPOffset = MFI.getObjectOffset(FPIndex); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1287 | } else { |
Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 1288 | FPOffset = getFramePointerSaveOffset(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1292 | int BPOffset = 0; |
| 1293 | if (HasBP) { |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1294 | if (isSVR4ABI) { |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1295 | int BPIndex = FI->getBasePointerSaveIndex(); |
| 1296 | assert(BPIndex && "No Base Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1297 | BPOffset = MFI.getObjectOffset(BPIndex); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1298 | } else { |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 1299 | BPOffset = getBasePointerSaveOffset(); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1303 | int PBPOffset = 0; |
| 1304 | if (FI->usesPICBase()) { |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1305 | int PBPIndex = FI->getPICBasePointerSaveIndex(); |
| 1306 | assert(PBPIndex && "No PIC Base Pointer Save Slot!"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1307 | PBPOffset = MFI.getObjectOffset(PBPIndex); |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
NAKAMURA Takumi | 8061e86 | 2015-09-11 08:20:56 +0000 | [diff] [blame] | 1310 | bool IsReturnBlock = (MBBI != MBB.end() && MBBI->isReturn()); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1311 | |
| 1312 | if (IsReturnBlock) { |
| 1313 | unsigned RetOpcode = MBBI->getOpcode(); |
| 1314 | bool UsesTCRet = RetOpcode == PPC::TCRETURNri || |
| 1315 | RetOpcode == PPC::TCRETURNdi || |
| 1316 | RetOpcode == PPC::TCRETURNai || |
| 1317 | RetOpcode == PPC::TCRETURNri8 || |
| 1318 | RetOpcode == PPC::TCRETURNdi8 || |
| 1319 | RetOpcode == PPC::TCRETURNai8; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1320 | |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1321 | if (UsesTCRet) { |
| 1322 | int MaxTCRetDelta = FI->getTailCallSPDelta(); |
| 1323 | MachineOperand &StackAdjust = MBBI->getOperand(1); |
| 1324 | assert(StackAdjust.isImm() && "Expecting immediate value."); |
| 1325 | // Adjust stack pointer. |
| 1326 | int StackAdj = StackAdjust.getImm(); |
| 1327 | int Delta = StackAdj - MaxTCRetDelta; |
| 1328 | assert((Delta >= 0) && "Delta must be positive"); |
| 1329 | if (MaxTCRetDelta>0) |
| 1330 | FrameSize += (StackAdj +Delta); |
| 1331 | else |
| 1332 | FrameSize += StackAdj; |
| 1333 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Bill Schmidt | 8893a3d | 2013-08-16 20:05:04 +0000 | [diff] [blame] | 1336 | // Frames of 32KB & larger require special handling because they cannot be |
| 1337 | // indexed into with a simple LD/LWZ immediate offset operand. |
| 1338 | bool isLargeFrame = !isInt<16>(FrameSize); |
| 1339 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1340 | // On targets without red zone, the SP needs to be restored last, so that |
| 1341 | // all live contents of the stack frame are upwards of the SP. This means |
| 1342 | // that we cannot restore SP just now, since there may be more registers |
| 1343 | // to restore from the stack frame (e.g. R31). If the frame size is not |
| 1344 | // a simple immediate value, we will need a spare register to hold the |
| 1345 | // restored SP. If the frame size is known and small, we can simply adjust |
| 1346 | // the offsets of the registers to be restored, and still use SP to restore |
| 1347 | // them. In such case, the final update of SP will be to add the frame |
| 1348 | // size to it. |
| 1349 | // To simplify the code, set RBReg to the base register used to restore |
| 1350 | // values from the stack, and set SPAdd to the value that needs to be added |
| 1351 | // to the SP at the end. The default values are as if red zone was present. |
| 1352 | unsigned RBReg = SPReg; |
| 1353 | unsigned SPAdd = 0; |
| 1354 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1355 | if (FrameSize) { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1356 | // In the prologue, the loaded (or persistent) stack pointer value is |
| 1357 | // offset by the STDU/STDUX/STWU/STWUX instruction. For targets with red |
| 1358 | // zone add this offset back now. |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1359 | |
| 1360 | // If this function contained a fastcc call and GuaranteedTailCallOpt is |
| 1361 | // enabled (=> hasFastCall()==true) the fastcc call might contain a tail |
| 1362 | // call which invalidates the stack pointer value in SP(0). So we use the |
| 1363 | // value of R31 in this case. |
| 1364 | if (FI->hasFastCall()) { |
| 1365 | assert(HasFP && "Expecting a valid frame pointer."); |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1366 | if (!HasRedZone) |
| 1367 | RBReg = FPReg; |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1368 | if (!isLargeFrame) { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1369 | BuildMI(MBB, MBBI, dl, AddImmInst, RBReg) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1370 | .addReg(FPReg).addImm(FrameSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1371 | } else { |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1372 | BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) |
| 1373 | .addImm(FrameSize >> 16); |
| 1374 | BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) |
| 1375 | .addReg(ScratchReg, RegState::Kill) |
| 1376 | .addImm(FrameSize & 0xFFFF); |
| 1377 | BuildMI(MBB, MBBI, dl, AddInst) |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1378 | .addReg(RBReg) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1379 | .addReg(FPReg) |
| 1380 | .addReg(ScratchReg); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1381 | } |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1382 | } else if (!isLargeFrame && !HasBP && !MFI.hasVarSizedObjects()) { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1383 | if (HasRedZone) { |
| 1384 | BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) |
| 1385 | .addReg(SPReg) |
| 1386 | .addImm(FrameSize); |
| 1387 | } else { |
| 1388 | // Make sure that adding FrameSize will not overflow the max offset |
| 1389 | // size. |
| 1390 | assert(FPOffset <= 0 && BPOffset <= 0 && PBPOffset <= 0 && |
| 1391 | "Local offsets should be negative"); |
| 1392 | SPAdd = FrameSize; |
| 1393 | FPOffset += FrameSize; |
| 1394 | BPOffset += FrameSize; |
| 1395 | PBPOffset += FrameSize; |
| 1396 | } |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1397 | } else { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1398 | // We don't want to use ScratchReg as a base register, because it |
| 1399 | // could happen to be R0. Use FP instead, but make sure to preserve it. |
| 1400 | if (!HasRedZone) { |
| 1401 | // If FP is not saved, copy it to ScratchReg. |
| 1402 | if (!HasFP) |
| 1403 | BuildMI(MBB, MBBI, dl, OrInst, ScratchReg) |
| 1404 | .addReg(FPReg) |
| 1405 | .addReg(FPReg); |
| 1406 | RBReg = FPReg; |
| 1407 | } |
| 1408 | BuildMI(MBB, MBBI, dl, LoadInst, RBReg) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1409 | .addImm(0) |
| 1410 | .addReg(SPReg); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1411 | } |
| 1412 | } |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1413 | assert(RBReg != ScratchReg && "Should have avoided ScratchReg"); |
| 1414 | // If there is no red zone, ScratchReg may be needed for holding a useful |
| 1415 | // value (although not the base register). Make sure it is not overwritten |
| 1416 | // too early. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1417 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1418 | assert((isPPC64 || !MustSaveCR) && |
| 1419 | "Epilogue CR restoring supported only in 64-bit mode"); |
| 1420 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1421 | // If we need to restore both the LR and the CR and we only have one |
| 1422 | // available scratch register, we must do them one at a time. |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1423 | if (MustSaveCR && SingleScratchReg && MustSaveLR) { |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1424 | // Here TempReg == ScratchReg, and in the absence of red zone ScratchReg |
| 1425 | // is live here. |
| 1426 | assert(HasRedZone && "Expecting red zone"); |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1427 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg) |
| 1428 | .addImm(8) |
| 1429 | .addReg(SPReg); |
| 1430 | for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) |
| 1431 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i]) |
| 1432 | .addReg(TempReg, getKillRegState(i == e-1)); |
| 1433 | } |
| 1434 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1435 | // Delay restoring of the LR if ScratchReg is needed. This is ok, since |
| 1436 | // LR is stored in the caller's stack frame. ScratchReg will be needed |
| 1437 | // if RBReg is anything other than SP. We shouldn't use ScratchReg as |
| 1438 | // a base register anyway, because it may happen to be R0. |
| 1439 | bool LoadedLR = false; |
| 1440 | if (MustSaveLR && RBReg == SPReg && isInt<16>(LROffset+SPAdd)) { |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1441 | BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1442 | .addImm(LROffset+SPAdd) |
| 1443 | .addReg(RBReg); |
| 1444 | LoadedLR = true; |
| 1445 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1446 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1447 | if (MustSaveCR && !(SingleScratchReg && MustSaveLR)) { |
| 1448 | // This will only occur for PPC64. |
| 1449 | assert(isPPC64 && "Expecting 64-bit mode"); |
| 1450 | assert(RBReg == SPReg && "Should be using SP as a base register"); |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1451 | BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ8), TempReg) |
| 1452 | .addImm(8) |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1453 | .addReg(RBReg); |
| 1454 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1455 | |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1456 | if (HasFP) { |
| 1457 | // If there is red zone, restore FP directly, since SP has already been |
| 1458 | // restored. Otherwise, restore the value of FP into ScratchReg. |
| 1459 | if (HasRedZone || RBReg == SPReg) |
| 1460 | BuildMI(MBB, MBBI, dl, LoadInst, FPReg) |
| 1461 | .addImm(FPOffset) |
| 1462 | .addReg(SPReg); |
| 1463 | else |
| 1464 | BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) |
| 1465 | .addImm(FPOffset) |
| 1466 | .addReg(RBReg); |
| 1467 | } |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1468 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1469 | if (FI->usesPICBase()) |
Justin Hibbits | 98a532d | 2015-01-08 15:47:19 +0000 | [diff] [blame] | 1470 | BuildMI(MBB, MBBI, dl, LoadInst) |
| 1471 | .addReg(PPC::R30) |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1472 | .addImm(PBPOffset) |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1473 | .addReg(RBReg); |
Justin Hibbits | 98a532d | 2015-01-08 15:47:19 +0000 | [diff] [blame] | 1474 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1475 | if (HasBP) |
| 1476 | BuildMI(MBB, MBBI, dl, LoadInst, BPReg) |
| 1477 | .addImm(BPOffset) |
Krzysztof Parzyszek | b66efb8 | 2016-09-22 17:22:43 +0000 | [diff] [blame] | 1478 | .addReg(RBReg); |
| 1479 | |
| 1480 | // There is nothing more to be loaded from the stack, so now we can |
| 1481 | // restore SP: SP = RBReg + SPAdd. |
| 1482 | if (RBReg != SPReg || SPAdd != 0) { |
| 1483 | assert(!HasRedZone && "This should not happen with red zone"); |
| 1484 | // If SPAdd is 0, generate a copy. |
| 1485 | if (SPAdd == 0) |
| 1486 | BuildMI(MBB, MBBI, dl, OrInst, SPReg) |
| 1487 | .addReg(RBReg) |
| 1488 | .addReg(RBReg); |
| 1489 | else |
| 1490 | BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) |
| 1491 | .addReg(RBReg) |
| 1492 | .addImm(SPAdd); |
| 1493 | |
| 1494 | assert(RBReg != ScratchReg && "Should be using FP or SP as base register"); |
| 1495 | if (RBReg == FPReg) |
| 1496 | BuildMI(MBB, MBBI, dl, OrInst, FPReg) |
| 1497 | .addReg(ScratchReg) |
| 1498 | .addReg(ScratchReg); |
| 1499 | |
| 1500 | // Now load the LR from the caller's stack frame. |
| 1501 | if (MustSaveLR && !LoadedLR) |
| 1502 | BuildMI(MBB, MBBI, dl, LoadInst, ScratchReg) |
| 1503 | .addImm(LROffset) |
| 1504 | .addReg(SPReg); |
| 1505 | } |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 1506 | |
Nemanja Ivanovic | ae22101c | 2016-02-20 18:16:25 +0000 | [diff] [blame] | 1507 | if (MustSaveCR && |
| 1508 | !(SingleScratchReg && MustSaveLR)) // will only occur for PPC64 |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1509 | for (unsigned i = 0, e = MustSaveCRs.size(); i != e; ++i) |
| 1510 | BuildMI(MBB, MBBI, dl, TII.get(PPC::MTOCRF8), MustSaveCRs[i]) |
| 1511 | .addReg(TempReg, getKillRegState(i == e-1)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1512 | |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1513 | if (MustSaveLR) |
| 1514 | BuildMI(MBB, MBBI, dl, MTLRInst).addReg(ScratchReg); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1515 | |
| 1516 | // Callee pop calling convention. Pop parameter/linkage area. Used for tail |
| 1517 | // call optimization |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1518 | if (IsReturnBlock) { |
| 1519 | unsigned RetOpcode = MBBI->getOpcode(); |
| 1520 | if (MF.getTarget().Options.GuaranteedTailCallOpt && |
| 1521 | (RetOpcode == PPC::BLR || RetOpcode == PPC::BLR8) && |
| 1522 | MF.getFunction()->getCallingConv() == CallingConv::Fast) { |
| 1523 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 1524 | unsigned CallerAllocatedAmt = FI->getMinReservedArea(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1525 | |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1526 | if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) { |
| 1527 | BuildMI(MBB, MBBI, dl, AddImmInst, SPReg) |
| 1528 | .addReg(SPReg).addImm(CallerAllocatedAmt); |
| 1529 | } else { |
| 1530 | BuildMI(MBB, MBBI, dl, LoadImmShiftedInst, ScratchReg) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1531 | .addImm(CallerAllocatedAmt >> 16); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1532 | BuildMI(MBB, MBBI, dl, OrImmInst, ScratchReg) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1533 | .addReg(ScratchReg, RegState::Kill) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1534 | .addImm(CallerAllocatedAmt & 0xFFFF); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1535 | BuildMI(MBB, MBBI, dl, AddInst) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1536 | .addReg(SPReg) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1537 | .addReg(FPReg) |
Bill Schmidt | f381afc | 2013-08-20 03:12:23 +0000 | [diff] [blame] | 1538 | .addReg(ScratchReg); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1539 | } |
Chuang-Yu Cheng | f8b592f | 2016-04-01 06:44:32 +0000 | [diff] [blame] | 1540 | } else { |
| 1541 | createTailCallBranchInstr(MBB); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 1542 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
Anton Korobeynikov | 14ee344 | 2010-11-18 23:25:52 +0000 | [diff] [blame] | 1545 | |
Chuang-Yu Cheng | f8b592f | 2016-04-01 06:44:32 +0000 | [diff] [blame] | 1546 | void PPCFrameLowering::createTailCallBranchInstr(MachineBasicBlock &MBB) const { |
| 1547 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 1548 | DebugLoc dl; |
| 1549 | |
| 1550 | if (MBBI != MBB.end()) |
| 1551 | dl = MBBI->getDebugLoc(); |
| 1552 | |
| 1553 | const PPCInstrInfo &TII = |
| 1554 | *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo()); |
| 1555 | |
| 1556 | // Create branch instruction for pseudo tail call return instruction |
| 1557 | unsigned RetOpcode = MBBI->getOpcode(); |
| 1558 | if (RetOpcode == PPC::TCRETURNdi) { |
| 1559 | MBBI = MBB.getLastNonDebugInstr(); |
| 1560 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 1561 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)). |
| 1562 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 1563 | } else if (RetOpcode == PPC::TCRETURNri) { |
| 1564 | MBBI = MBB.getLastNonDebugInstr(); |
| 1565 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 1566 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR)); |
| 1567 | } else if (RetOpcode == PPC::TCRETURNai) { |
| 1568 | MBBI = MBB.getLastNonDebugInstr(); |
| 1569 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 1570 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm()); |
| 1571 | } else if (RetOpcode == PPC::TCRETURNdi8) { |
| 1572 | MBBI = MBB.getLastNonDebugInstr(); |
| 1573 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 1574 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)). |
| 1575 | addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset()); |
| 1576 | } else if (RetOpcode == PPC::TCRETURNri8) { |
| 1577 | MBBI = MBB.getLastNonDebugInstr(); |
| 1578 | assert(MBBI->getOperand(0).isReg() && "Expecting register operand."); |
| 1579 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8)); |
| 1580 | } else if (RetOpcode == PPC::TCRETURNai8) { |
| 1581 | MBBI = MBB.getLastNonDebugInstr(); |
| 1582 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 1583 | BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm()); |
| 1584 | } |
| 1585 | } |
| 1586 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1587 | void PPCFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 1588 | BitVector &SavedRegs, |
| 1589 | RegScavenger *RS) const { |
| 1590 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 1591 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1592 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1593 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1594 | |
| 1595 | // Save and clear the LR state. |
| 1596 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 1597 | unsigned LR = RegInfo->getRARegister(); |
| 1598 | FI->setMustSaveLR(MustSaveLR(MF, LR)); |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1599 | SavedRegs.reset(LR); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Save R31 if necessary |
| 1602 | int FPSI = FI->getFramePointerSaveIndex(); |
| 1603 | bool isPPC64 = Subtarget.isPPC64(); |
| 1604 | bool isDarwinABI = Subtarget.isDarwinABI(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1605 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1606 | |
| 1607 | // If the frame pointer save index hasn't been defined yet. |
Anton Korobeynikov | 3eb4fed | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 1608 | if (!FPSI && needsFP(MF)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1609 | // Find out what the fix offset of the frame pointer save area. |
Eric Christopher | dc3a8a4 | 2015-02-13 00:39:38 +0000 | [diff] [blame] | 1610 | int FPOffset = getFramePointerSaveOffset(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1611 | // Allocate the frame index for frame pointer save area. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1612 | FPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1613 | // Save the result. |
| 1614 | FI->setFramePointerSaveIndex(FPSI); |
| 1615 | } |
| 1616 | |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1617 | int BPSI = FI->getBasePointerSaveIndex(); |
| 1618 | if (!BPSI && RegInfo->hasBasePointer(MF)) { |
Eric Christopher | fcd3d87 | 2015-02-13 22:48:53 +0000 | [diff] [blame] | 1619 | int BPOffset = getBasePointerSaveOffset(); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1620 | // Allocate the frame index for the base pointer save area. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1621 | BPSI = MFI.CreateFixedObject(isPPC64? 8 : 4, BPOffset, true); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1622 | // Save the result. |
| 1623 | FI->setBasePointerSaveIndex(BPSI); |
| 1624 | } |
| 1625 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1626 | // Reserve stack space for the PIC Base register (R30). |
| 1627 | // Only used in SVR4 32-bit. |
| 1628 | if (FI->usesPICBase()) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1629 | int PBPSI = MFI.CreateFixedObject(4, -8, true); |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1630 | FI->setPICBasePointerSaveIndex(PBPSI); |
| 1631 | } |
| 1632 | |
Hal Finkel | 97a189c | 2016-08-31 00:52:03 +0000 | [diff] [blame] | 1633 | // Make sure we don't explicitly spill r31, because, for example, we have |
| 1634 | // some inline asm which explicity clobbers it, when we otherwise have a |
| 1635 | // frame pointer and are using r31's spill slot for the prologue/epilogue |
| 1636 | // code. Same goes for the base pointer and the PIC base register. |
| 1637 | if (needsFP(MF)) |
| 1638 | SavedRegs.reset(isPPC64 ? PPC::X31 : PPC::R31); |
| 1639 | if (RegInfo->hasBasePointer(MF)) |
| 1640 | SavedRegs.reset(RegInfo->getBaseRegister(MF)); |
| 1641 | if (FI->usesPICBase()) |
| 1642 | SavedRegs.reset(PPC::R30); |
| 1643 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1644 | // Reserve stack space to move the linkage area to in case of a tail call. |
| 1645 | int TCSPDelta = 0; |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1646 | if (MF.getTarget().Options.GuaranteedTailCallOpt && |
| 1647 | (TCSPDelta = FI->getTailCallSPDelta()) < 0) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1648 | MFI.CreateFixedObject(-1 * TCSPDelta, TCSPDelta, true); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1651 | // For 32-bit SVR4, allocate the nonvolatile CR spill slot iff the |
Bill Schmidt | c68c6df | 2013-02-24 17:34:50 +0000 | [diff] [blame] | 1652 | // function uses CR 2, 3, or 4. |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1653 | if (!isPPC64 && !isDarwinABI && |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1654 | (SavedRegs.test(PPC::CR2) || |
| 1655 | SavedRegs.test(PPC::CR3) || |
| 1656 | SavedRegs.test(PPC::CR4))) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1657 | int FrameIdx = MFI.CreateFixedObject((uint64_t)4, (int64_t)-4, true); |
Bill Schmidt | c68c6df | 2013-02-24 17:34:50 +0000 | [diff] [blame] | 1658 | FI->setCRSpillFrameIndex(FrameIdx); |
| 1659 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Hal Finkel | 5a765fd | 2013-03-14 20:33:40 +0000 | [diff] [blame] | 1662 | void PPCFrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF, |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1663 | RegScavenger *RS) const { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1664 | // Early exit if not using the SVR4 ABI. |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1665 | if (!Subtarget.isSVR4ABI()) { |
| 1666 | addScavengingSpillSlot(MF, RS); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1667 | return; |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1668 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1669 | |
| 1670 | // Get callee saved register information. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1671 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1672 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1673 | |
Chuang-Yu Cheng | f8b592f | 2016-04-01 06:44:32 +0000 | [diff] [blame] | 1674 | // If the function is shrink-wrapped, and if the function has a tail call, the |
| 1675 | // tail call might not be in the new RestoreBlock, so real branch instruction |
| 1676 | // won't be generated by emitEpilogue(), because shrink-wrap has chosen new |
| 1677 | // RestoreBlock. So we handle this case here. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1678 | if (MFI.getSavePoint() && MFI.hasTailCall()) { |
| 1679 | MachineBasicBlock *RestoreBlock = MFI.getRestorePoint(); |
Chuang-Yu Cheng | f8b592f | 2016-04-01 06:44:32 +0000 | [diff] [blame] | 1680 | for (MachineBasicBlock &MBB : MF) { |
| 1681 | if (MBB.isReturnBlock() && (&MBB) != RestoreBlock) |
| 1682 | createTailCallBranchInstr(MBB); |
| 1683 | } |
| 1684 | } |
| 1685 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1686 | // Early exit if no callee saved registers are modified! |
Anton Korobeynikov | 3eb4fed | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 1687 | if (CSI.empty() && !needsFP(MF)) { |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1688 | addScavengingSpillSlot(MF, RS); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | unsigned MinGPR = PPC::R31; |
| 1693 | unsigned MinG8R = PPC::X31; |
| 1694 | unsigned MinFPR = PPC::F31; |
| 1695 | unsigned MinVR = PPC::V31; |
| 1696 | |
| 1697 | bool HasGPSaveArea = false; |
| 1698 | bool HasG8SaveArea = false; |
| 1699 | bool HasFPSaveArea = false; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1700 | bool HasVRSAVESaveArea = false; |
| 1701 | bool HasVRSaveArea = false; |
| 1702 | |
| 1703 | SmallVector<CalleeSavedInfo, 18> GPRegs; |
| 1704 | SmallVector<CalleeSavedInfo, 18> G8Regs; |
| 1705 | SmallVector<CalleeSavedInfo, 18> FPRegs; |
| 1706 | SmallVector<CalleeSavedInfo, 18> VRegs; |
| 1707 | |
| 1708 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1709 | unsigned Reg = CSI[i].getReg(); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1710 | if (PPC::GPRCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1711 | HasGPSaveArea = true; |
| 1712 | |
| 1713 | GPRegs.push_back(CSI[i]); |
| 1714 | |
| 1715 | if (Reg < MinGPR) { |
| 1716 | MinGPR = Reg; |
| 1717 | } |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1718 | } else if (PPC::G8RCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1719 | HasG8SaveArea = true; |
| 1720 | |
| 1721 | G8Regs.push_back(CSI[i]); |
| 1722 | |
| 1723 | if (Reg < MinG8R) { |
| 1724 | MinG8R = Reg; |
| 1725 | } |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1726 | } else if (PPC::F8RCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1727 | HasFPSaveArea = true; |
| 1728 | |
| 1729 | FPRegs.push_back(CSI[i]); |
| 1730 | |
| 1731 | if (Reg < MinFPR) { |
| 1732 | MinFPR = Reg; |
| 1733 | } |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1734 | } else if (PPC::CRBITRCRegClass.contains(Reg) || |
| 1735 | PPC::CRRCRegClass.contains(Reg)) { |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1736 | ; // do nothing, as we already know whether CRs are spilled |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1737 | } else if (PPC::VRSAVERCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1738 | HasVRSAVESaveArea = true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1739 | } else if (PPC::VRRCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1740 | HasVRSaveArea = true; |
| 1741 | |
| 1742 | VRegs.push_back(CSI[i]); |
| 1743 | |
| 1744 | if (Reg < MinVR) { |
| 1745 | MinVR = Reg; |
| 1746 | } |
| 1747 | } else { |
| 1748 | llvm_unreachable("Unknown RegisterClass!"); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>(); |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1753 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1754 | |
| 1755 | int64_t LowerBound = 0; |
| 1756 | |
| 1757 | // Take into account stack space reserved for tail calls. |
| 1758 | int TCSPDelta = 0; |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1759 | if (MF.getTarget().Options.GuaranteedTailCallOpt && |
| 1760 | (TCSPDelta = PFI->getTailCallSPDelta()) < 0) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1761 | LowerBound = TCSPDelta; |
| 1762 | } |
| 1763 | |
| 1764 | // The Floating-point register save area is right below the back chain word |
| 1765 | // of the previous stack frame. |
| 1766 | if (HasFPSaveArea) { |
| 1767 | for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) { |
| 1768 | int FI = FPRegs[i].getFrameIdx(); |
| 1769 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1770 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 1773 | LowerBound -= (31 - TRI->getEncodingValue(MinFPR) + 1) * 8; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | // Check whether the frame pointer register is allocated. If so, make sure it |
| 1777 | // is spilled to the correct offset. |
Anton Korobeynikov | 3eb4fed | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 1778 | if (needsFP(MF)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1779 | HasGPSaveArea = true; |
| 1780 | |
| 1781 | int FI = PFI->getFramePointerSaveIndex(); |
| 1782 | assert(FI && "No Frame Pointer Save Slot!"); |
| 1783 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1784 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1787 | if (PFI->usesPICBase()) { |
| 1788 | HasGPSaveArea = true; |
| 1789 | |
| 1790 | int FI = PFI->getPICBasePointerSaveIndex(); |
| 1791 | assert(FI && "No PIC Base Pointer Save Slot!"); |
| 1792 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1793 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Justin Hibbits | 654346e | 2015-01-10 01:57:21 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1796 | const PPCRegisterInfo *RegInfo = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1797 | static_cast<const PPCRegisterInfo *>(Subtarget.getRegisterInfo()); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1798 | if (RegInfo->hasBasePointer(MF)) { |
| 1799 | HasGPSaveArea = true; |
| 1800 | |
| 1801 | int FI = PFI->getBasePointerSaveIndex(); |
| 1802 | assert(FI && "No Base Pointer Save Slot!"); |
| 1803 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1804 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Hal Finkel | a7c54e8 | 2013-07-17 00:45:52 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1807 | // General register save area starts right below the Floating-point |
| 1808 | // register save area. |
| 1809 | if (HasGPSaveArea || HasG8SaveArea) { |
| 1810 | // Move general register save area spill slots down, taking into account |
| 1811 | // the size of the Floating-point register save area. |
| 1812 | for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) { |
| 1813 | int FI = GPRegs[i].getFrameIdx(); |
| 1814 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1815 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | // Move general register save area spill slots down, taking into account |
| 1819 | // the size of the Floating-point register save area. |
| 1820 | for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) { |
| 1821 | int FI = G8Regs[i].getFrameIdx(); |
| 1822 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1823 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | unsigned MinReg = |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 1827 | std::min<unsigned>(TRI->getEncodingValue(MinGPR), |
| 1828 | TRI->getEncodingValue(MinG8R)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1829 | |
| 1830 | if (Subtarget.isPPC64()) { |
| 1831 | LowerBound -= (31 - MinReg + 1) * 8; |
| 1832 | } else { |
| 1833 | LowerBound -= (31 - MinReg + 1) * 4; |
| 1834 | } |
| 1835 | } |
| 1836 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1837 | // For 32-bit only, the CR save area is below the general register |
| 1838 | // save area. For 64-bit SVR4, the CR save area is addressed relative |
| 1839 | // to the stack pointer and hence does not need an adjustment here. |
| 1840 | // Only CR2 (the first nonvolatile spilled) has an associated frame |
| 1841 | // index so that we have a single uniform save area. |
| 1842 | if (spillsCR(MF) && !(Subtarget.isPPC64() && Subtarget.isSVR4ABI())) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1843 | // Adjust the frame index of the CR spill slot. |
| 1844 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1845 | unsigned Reg = CSI[i].getReg(); |
| 1846 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1847 | if ((Subtarget.isSVR4ABI() && Reg == PPC::CR2) |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1848 | // Leave Darwin logic as-is. |
| 1849 | || (!Subtarget.isSVR4ABI() && |
| 1850 | (PPC::CRBITRCRegClass.contains(Reg) || |
| 1851 | PPC::CRRCRegClass.contains(Reg)))) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1852 | int FI = CSI[i].getFrameIdx(); |
| 1853 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1854 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | LowerBound -= 4; // The CR save area is always 4 bytes long. |
| 1859 | } |
| 1860 | |
| 1861 | if (HasVRSAVESaveArea) { |
| 1862 | // FIXME SVR4: Is it actually possible to have multiple elements in CSI |
| 1863 | // which have the VRSAVE register class? |
| 1864 | // Adjust the frame index of the VRSAVE spill slot. |
| 1865 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1866 | unsigned Reg = CSI[i].getReg(); |
| 1867 | |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1868 | if (PPC::VRSAVERCRegClass.contains(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1869 | int FI = CSI[i].getFrameIdx(); |
| 1870 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1871 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | LowerBound -= 4; // The VRSAVE save area is always 4 bytes long. |
| 1876 | } |
| 1877 | |
| 1878 | if (HasVRSaveArea) { |
| 1879 | // Insert alignment padding, we need 16-byte alignment. |
| 1880 | LowerBound = (LowerBound - 15) & ~(15); |
| 1881 | |
| 1882 | for (unsigned i = 0, e = VRegs.size(); i != e; ++i) { |
| 1883 | int FI = VRegs[i].getFrameIdx(); |
| 1884 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1885 | MFI.setObjectOffset(FI, LowerBound + MFI.getObjectOffset(FI)); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1888 | |
| 1889 | addScavengingSpillSlot(MF, RS); |
| 1890 | } |
| 1891 | |
| 1892 | void |
| 1893 | PPCFrameLowering::addScavengingSpillSlot(MachineFunction &MF, |
| 1894 | RegScavenger *RS) const { |
| 1895 | // Reserve a slot closest to SP or frame pointer if we have a dynalloc or |
| 1896 | // a large stack, which will require scavenging a register to materialize a |
| 1897 | // large offset. |
| 1898 | |
| 1899 | // We need to have a scavenger spill slot for spills if the frame size is |
| 1900 | // large. In case there is no free register for large-offset addressing, |
| 1901 | // this slot is used for the necessary emergency spill. Also, we need the |
| 1902 | // slot for dynamic stack allocations. |
| 1903 | |
| 1904 | // The scavenger might be invoked if the frame offset does not fit into |
| 1905 | // the 16-bit immediate. We don't know the complete frame size here |
| 1906 | // because we've not yet computed callee-saved register spills or the |
| 1907 | // needed alignment padding. |
| 1908 | unsigned StackSize = determineFrameLayout(MF, false, true); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1909 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1910 | if (MFI.hasVarSizedObjects() || spillsCR(MF) || spillsVRSAVE(MF) || |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 1911 | hasNonRISpills(MF) || (hasSpills(MF) && !isInt<16>(StackSize))) { |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1912 | const TargetRegisterClass *GPRC = &PPC::GPRCRegClass; |
| 1913 | const TargetRegisterClass *G8RC = &PPC::G8RCRegClass; |
| 1914 | const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1915 | RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(), |
| 1916 | RC->getAlignment(), |
| 1917 | false)); |
Hal Finkel | 0dfbb05 | 2013-03-26 18:57:22 +0000 | [diff] [blame] | 1918 | |
Hal Finkel | 1860763 | 2013-07-18 04:28:21 +0000 | [diff] [blame] | 1919 | // Might we have over-aligned allocas? |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1920 | bool HasAlVars = MFI.hasVarSizedObjects() && |
| 1921 | MFI.getMaxAlignment() > getStackAlignment(); |
Hal Finkel | 1860763 | 2013-07-18 04:28:21 +0000 | [diff] [blame] | 1922 | |
Hal Finkel | 0dfbb05 | 2013-03-26 18:57:22 +0000 | [diff] [blame] | 1923 | // These kinds of spills might need two registers. |
Hal Finkel | 1860763 | 2013-07-18 04:28:21 +0000 | [diff] [blame] | 1924 | if (spillsCR(MF) || spillsVRSAVE(MF) || HasAlVars) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1925 | RS->addScavengingFrameIndex(MFI.CreateStackObject(RC->getSize(), |
| 1926 | RC->getAlignment(), |
| 1927 | false)); |
Hal Finkel | 0dfbb05 | 2013-03-26 18:57:22 +0000 | [diff] [blame] | 1928 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 1929 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1930 | } |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1931 | |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1932 | bool |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1933 | PPCFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1934 | MachineBasicBlock::iterator MI, |
| 1935 | const std::vector<CalleeSavedInfo> &CSI, |
| 1936 | const TargetRegisterInfo *TRI) const { |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Currently, this function only handles SVR4 32- and 64-bit ABIs. |
| 1939 | // Return false otherwise to maintain pre-existing behavior. |
| 1940 | if (!Subtarget.isSVR4ABI()) |
| 1941 | return false; |
| 1942 | |
| 1943 | MachineFunction *MF = MBB.getParent(); |
| 1944 | const PPCInstrInfo &TII = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 1945 | *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo()); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1946 | DebugLoc DL; |
| 1947 | bool CRSpilled = false; |
Hal Finkel | 2f29391 | 2013-04-13 23:06:15 +0000 | [diff] [blame] | 1948 | MachineInstrBuilder CRMIB; |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1949 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1950 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 1951 | unsigned Reg = CSI[i].getReg(); |
Hal Finkel | ac1a24b | 2013-06-28 22:29:56 +0000 | [diff] [blame] | 1952 | // Only Darwin actually uses the VRSAVE register, but it can still appear |
| 1953 | // here if, for example, @llvm.eh.unwind.init() is used. If we're not on |
| 1954 | // Darwin, ignore it. |
| 1955 | if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI()) |
| 1956 | continue; |
| 1957 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1958 | // CR2 through CR4 are the nonvolatile CR fields. |
| 1959 | bool IsCRField = PPC::CR2 <= Reg && Reg <= PPC::CR4; |
| 1960 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1961 | // Add the callee-saved register as live-in; it's killed at the spill. |
| 1962 | MBB.addLiveIn(Reg); |
| 1963 | |
Hal Finkel | 2f29391 | 2013-04-13 23:06:15 +0000 | [diff] [blame] | 1964 | if (CRSpilled && IsCRField) { |
| 1965 | CRMIB.addReg(Reg, RegState::ImplicitKill); |
| 1966 | continue; |
| 1967 | } |
| 1968 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1969 | // Insert the spill to the stack frame. |
| 1970 | if (IsCRField) { |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 1971 | PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>(); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1972 | if (Subtarget.isPPC64()) { |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 1973 | // The actual spill will happen at the start of the prologue. |
| 1974 | FuncInfo->addMustSaveCR(Reg); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1975 | } else { |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 1976 | CRSpilled = true; |
Bill Schmidt | ef3d1a2 | 2013-05-14 16:08:32 +0000 | [diff] [blame] | 1977 | FuncInfo->setSpillsCR(); |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 1978 | |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1979 | // 32-bit: FP-relative. Note that we made sure CR2-CR4 all have |
| 1980 | // the same frame index in PPCRegisterInfo::hasReservedSpillSlot. |
| 1981 | CRMIB = BuildMI(*MF, DL, TII.get(PPC::MFCR), PPC::R12) |
Hal Finkel | 2f29391 | 2013-04-13 23:06:15 +0000 | [diff] [blame] | 1982 | .addReg(Reg, RegState::ImplicitKill); |
| 1983 | |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1984 | MBB.insert(MI, CRMIB); |
| 1985 | MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::STW)) |
| 1986 | .addReg(PPC::R12, |
| 1987 | getKillRegState(true)), |
| 1988 | CSI[i].getFrameIdx())); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1989 | } |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1990 | } else { |
| 1991 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
| 1992 | TII.storeRegToStackSlot(MBB, MI, Reg, true, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 1993 | CSI[i].getFrameIdx(), RC, TRI); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | return true; |
| 1997 | } |
| 1998 | |
| 1999 | static void |
Hal Finkel | d85a04b | 2013-04-13 08:09:20 +0000 | [diff] [blame] | 2000 | restoreCRs(bool isPPC64, bool is31, |
| 2001 | bool CR2Spilled, bool CR3Spilled, bool CR4Spilled, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2002 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 2003 | const std::vector<CalleeSavedInfo> &CSI, unsigned CSIIndex) { |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2004 | |
| 2005 | MachineFunction *MF = MBB.getParent(); |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 2006 | const PPCInstrInfo &TII = *MF->getSubtarget<PPCSubtarget>().getInstrInfo(); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2007 | DebugLoc DL; |
| 2008 | unsigned RestoreOp, MoveReg; |
| 2009 | |
Hal Finkel | 6736988 | 2013-04-15 02:07:05 +0000 | [diff] [blame] | 2010 | if (isPPC64) |
| 2011 | // This is handled during epilogue generation. |
| 2012 | return; |
| 2013 | else { |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2014 | // 32-bit: FP-relative |
| 2015 | MBB.insert(MI, addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ), |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2016 | PPC::R12), |
| 2017 | CSI[CSIIndex].getFrameIdx())); |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 2018 | RestoreOp = PPC::MTOCRF; |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2019 | MoveReg = PPC::R12; |
| 2020 | } |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2021 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2022 | if (CR2Spilled) |
| 2023 | MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2) |
Hal Finkel | 035b482 | 2013-03-28 03:38:16 +0000 | [diff] [blame] | 2024 | .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled))); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (CR3Spilled) |
| 2027 | MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3) |
Hal Finkel | 035b482 | 2013-03-28 03:38:16 +0000 | [diff] [blame] | 2028 | .addReg(MoveReg, getKillRegState(!CR4Spilled))); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2029 | |
| 2030 | if (CR4Spilled) |
| 2031 | MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4) |
Hal Finkel | 035b482 | 2013-03-28 03:38:16 +0000 | [diff] [blame] | 2032 | .addReg(MoveReg, getKillRegState(true))); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 2035 | MachineBasicBlock::iterator PPCFrameLowering:: |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 2036 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 2037 | MachineBasicBlock::iterator I) const { |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 2038 | const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 2039 | if (MF.getTarget().Options.GuaranteedTailCallOpt && |
| 2040 | I->getOpcode() == PPC::ADJCALLSTACKUP) { |
| 2041 | // Add (actually subtract) back the amount the callee popped on return. |
| 2042 | if (int CalleeAmt = I->getOperand(1).getImm()) { |
| 2043 | bool is64Bit = Subtarget.isPPC64(); |
| 2044 | CalleeAmt *= -1; |
| 2045 | unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1; |
| 2046 | unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0; |
| 2047 | unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI; |
| 2048 | unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4; |
| 2049 | unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS; |
| 2050 | unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI; |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 2051 | const DebugLoc &dl = I->getDebugLoc(); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (isInt<16>(CalleeAmt)) { |
| 2054 | BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg) |
| 2055 | .addReg(StackReg, RegState::Kill) |
| 2056 | .addImm(CalleeAmt); |
| 2057 | } else { |
| 2058 | MachineBasicBlock::iterator MBBI = I; |
| 2059 | BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg) |
| 2060 | .addImm(CalleeAmt >> 16); |
| 2061 | BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg) |
| 2062 | .addReg(TmpReg, RegState::Kill) |
| 2063 | .addImm(CalleeAmt & 0xFFFF); |
| 2064 | BuildMI(MBB, MBBI, dl, TII.get(ADDInstr), StackReg) |
| 2065 | .addReg(StackReg, RegState::Kill) |
| 2066 | .addReg(TmpReg); |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions. |
Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 2071 | return MBB.erase(I); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2074 | bool |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2075 | PPCFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2076 | MachineBasicBlock::iterator MI, |
| 2077 | const std::vector<CalleeSavedInfo> &CSI, |
| 2078 | const TargetRegisterInfo *TRI) const { |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2079 | |
| 2080 | // Currently, this function only handles SVR4 32- and 64-bit ABIs. |
| 2081 | // Return false otherwise to maintain pre-existing behavior. |
| 2082 | if (!Subtarget.isSVR4ABI()) |
| 2083 | return false; |
| 2084 | |
| 2085 | MachineFunction *MF = MBB.getParent(); |
| 2086 | const PPCInstrInfo &TII = |
Eric Christopher | 38522b8 | 2015-01-30 02:11:26 +0000 | [diff] [blame] | 2087 | *static_cast<const PPCInstrInfo *>(Subtarget.getInstrInfo()); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2088 | bool CR2Spilled = false; |
| 2089 | bool CR3Spilled = false; |
| 2090 | bool CR4Spilled = false; |
| 2091 | unsigned CSIIndex = 0; |
| 2092 | |
| 2093 | // Initialize insertion-point logic; we will be restoring in reverse |
| 2094 | // order of spill. |
| 2095 | MachineBasicBlock::iterator I = MI, BeforeI = I; |
| 2096 | bool AtStart = I == MBB.begin(); |
| 2097 | |
| 2098 | if (!AtStart) |
| 2099 | --BeforeI; |
| 2100 | |
| 2101 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 2102 | unsigned Reg = CSI[i].getReg(); |
| 2103 | |
Hal Finkel | ac1a24b | 2013-06-28 22:29:56 +0000 | [diff] [blame] | 2104 | // Only Darwin actually uses the VRSAVE register, but it can still appear |
| 2105 | // here if, for example, @llvm.eh.unwind.init() is used. If we're not on |
| 2106 | // Darwin, ignore it. |
| 2107 | if (Reg == PPC::VRSAVE && !Subtarget.isDarwinABI()) |
| 2108 | continue; |
| 2109 | |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2110 | if (Reg == PPC::CR2) { |
| 2111 | CR2Spilled = true; |
| 2112 | // The spill slot is associated only with CR2, which is the |
| 2113 | // first nonvolatile spilled. Save it here. |
| 2114 | CSIIndex = i; |
| 2115 | continue; |
| 2116 | } else if (Reg == PPC::CR3) { |
| 2117 | CR3Spilled = true; |
| 2118 | continue; |
| 2119 | } else if (Reg == PPC::CR4) { |
| 2120 | CR4Spilled = true; |
| 2121 | continue; |
| 2122 | } else { |
| 2123 | // When we first encounter a non-CR register after seeing at |
| 2124 | // least one CR register, restore all spilled CRs together. |
| 2125 | if ((CR2Spilled || CR3Spilled || CR4Spilled) |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2126 | && !(PPC::CR2 <= Reg && Reg <= PPC::CR4)) { |
Hal Finkel | d85a04b | 2013-04-13 08:09:20 +0000 | [diff] [blame] | 2127 | bool is31 = needsFP(*MF); |
| 2128 | restoreCRs(Subtarget.isPPC64(), is31, |
| 2129 | CR2Spilled, CR3Spilled, CR4Spilled, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2130 | MBB, I, CSI, CSIIndex); |
| 2131 | CR2Spilled = CR3Spilled = CR4Spilled = false; |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | // Default behavior for non-CR saves. |
| 2135 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); |
| 2136 | TII.loadRegFromStackSlot(MBB, I, Reg, CSI[i].getFrameIdx(), |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2137 | RC, TRI); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2138 | assert(I != MBB.begin() && |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2139 | "loadRegFromStackSlot didn't insert any code!"); |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | // Insert in reverse order. |
| 2143 | if (AtStart) |
| 2144 | I = MBB.begin(); |
| 2145 | else { |
| 2146 | I = BeforeI; |
| 2147 | ++I; |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2148 | } |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | // If we haven't yet spilled the CRs, do so now. |
Hal Finkel | d85a04b | 2013-04-13 08:09:20 +0000 | [diff] [blame] | 2152 | if (CR2Spilled || CR3Spilled || CR4Spilled) { |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2153 | bool is31 = needsFP(*MF); |
Hal Finkel | d85a04b | 2013-04-13 08:09:20 +0000 | [diff] [blame] | 2154 | restoreCRs(Subtarget.isPPC64(), is31, CR2Spilled, CR3Spilled, CR4Spilled, |
Eric Christopher | d173749 | 2014-04-29 00:16:40 +0000 | [diff] [blame] | 2155 | MBB, I, CSI, CSIIndex); |
Hal Finkel | d85a04b | 2013-04-13 08:09:20 +0000 | [diff] [blame] | 2156 | } |
Roman Divacky | c9e23d9 | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 2157 | |
| 2158 | return true; |
| 2159 | } |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 2160 | |
| 2161 | bool PPCFrameLowering::enableShrinkWrapping(const MachineFunction &MF) const { |
Kit Barton | f4ce2f3 | 2015-11-30 18:59:41 +0000 | [diff] [blame] | 2162 | return (MF.getSubtarget<PPCSubtarget>().isSVR4ABI() && |
| 2163 | MF.getSubtarget<PPCSubtarget>().isPPC64()); |
Kit Barton | d3b904d | 2015-09-10 01:55:44 +0000 | [diff] [blame] | 2164 | } |