Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCFrameLowering.h - Define frame lowering for PowerPC --*- C++ -*-===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 12 | |
| 13 | #ifndef POWERPC_FRAMEINFO_H |
| 14 | #define POWERPC_FRAMEINFO_H |
| 15 | |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 17 | #include "PPCSubtarget.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetFrameLowering.h" |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.h" |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 23 | class PPCSubtarget; |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 24 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 25 | class PPCFrameLowering: public TargetFrameLowering { |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 26 | const PPCSubtarget &Subtarget; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 27 | |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 28 | public: |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 29 | PPCFrameLowering(const PPCSubtarget &sti) |
| 30 | : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 16, 0), |
| 31 | Subtarget(sti) { |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 34 | void determineFrameLayout(MachineFunction &MF) const; |
| 35 | |
| 36 | /// emitProlog/emitEpilog - These methods insert prolog and epilog code into |
| 37 | /// the function. |
| 38 | void emitPrologue(MachineFunction &MF) const; |
| 39 | void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const; |
| 40 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 41 | bool hasFP(const MachineFunction &MF) const; |
Anton Korobeynikov | c8bd78c | 2010-12-18 19:53:14 +0000 | [diff] [blame] | 42 | bool needsFP(const MachineFunction &MF) const; |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 43 | |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 44 | void processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
| 45 | RegScavenger *RS = NULL) const; |
| 46 | void processFunctionBeforeFrameFinalized(MachineFunction &MF) const; |
| 47 | |
Roman Divacky | 9d760ae | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 48 | bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 49 | MachineBasicBlock::iterator MI, |
| 50 | const std::vector<CalleeSavedInfo> &CSI, |
| 51 | const TargetRegisterInfo *TRI) const; |
| 52 | |
| 53 | bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
| 54 | MachineBasicBlock::iterator MI, |
| 55 | const std::vector<CalleeSavedInfo> &CSI, |
| 56 | const TargetRegisterInfo *TRI) const; |
| 57 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 58 | /// targetHandlesStackFrameRounding - Returns true if the target is |
| 59 | /// responsible for rounding up the stack frame (probably at emitPrologue |
| 60 | /// time). |
| 61 | bool targetHandlesStackFrameRounding() const { return true; } |
| 62 | |
Jim Laskey | 51fe9d9 | 2006-12-06 17:42:06 +0000 | [diff] [blame] | 63 | /// getReturnSaveOffset - Return the previous frame offset to save the |
| 64 | /// return address. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 65 | static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 66 | if (isDarwinABI) |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 67 | return isPPC64 ? 16 : 8; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 68 | // SVR4 ABI: |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 69 | return isPPC64 ? 16 : 4; |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 70 | } |
Jim Laskey | 51fe9d9 | 2006-12-06 17:42:06 +0000 | [diff] [blame] | 71 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 72 | /// getFramePointerSaveOffset - Return the previous frame offset to save the |
| 73 | /// frame pointer. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 74 | static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 75 | // For the Darwin ABI: |
Dale Johannesen | f7801b4 | 2009-11-24 22:59:02 +0000 | [diff] [blame] | 76 | // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area |
| 77 | // for saving the frame pointer (if needed.) While the published ABI has |
| 78 | // not used this slot since at least MacOSX 10.2, there is older code |
| 79 | // around that does use it, and that needs to continue to work. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 80 | if (isDarwinABI) |
Dale Johannesen | f7801b4 | 2009-11-24 22:59:02 +0000 | [diff] [blame] | 81 | return isPPC64 ? -8U : -4U; |
Anton Korobeynikov | 78b4fee | 2010-11-15 00:06:05 +0000 | [diff] [blame] | 82 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 83 | // SVR4 ABI: First slot in the general register save area. |
Tilmann Scheller | cfcb799 | 2009-12-18 13:00:34 +0000 | [diff] [blame] | 84 | return isPPC64 ? -8U : -4U; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 85 | } |
Anton Korobeynikov | 78b4fee | 2010-11-15 00:06:05 +0000 | [diff] [blame] | 86 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 87 | /// getLinkageSize - Return the size of the PowerPC ABI linkage area. |
| 88 | /// |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 89 | static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) { |
| 90 | if (isDarwinABI || isPPC64) |
| 91 | return 6 * (isPPC64 ? 8 : 4); |
Anton Korobeynikov | 78b4fee | 2010-11-15 00:06:05 +0000 | [diff] [blame] | 92 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 93 | // SVR4 ABI: |
Nicolas Geoffray | ec58d9f | 2007-04-03 12:35:28 +0000 | [diff] [blame] | 94 | return 8; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI |
| 98 | /// argument area. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 99 | static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) { |
| 100 | // For the Darwin ABI / 64-bit SVR4 ABI: |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 101 | // The prolog code of the callee may store up to 8 GPR argument registers to |
| 102 | // the stack, allowing va_start to index over them in memory if its varargs. |
| 103 | // Because we cannot tell if this is needed on the caller side, we have to |
| 104 | // conservatively assume that it is needed. As such, make sure we have at |
| 105 | // least enough stack space for the caller to store the 8 GPRs. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 106 | if (isDarwinABI || isPPC64) |
| 107 | return 8 * (isPPC64 ? 8 : 4); |
Anton Korobeynikov | 78b4fee | 2010-11-15 00:06:05 +0000 | [diff] [blame] | 108 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 109 | // 32-bit SVR4 ABI: |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 110 | // There is no default stack allocated for the 8 first GPR arguments. |
| 111 | return 0; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /// getMinCallFrameSize - Return the minimum size a call frame can be using |
| 115 | /// the PowerPC ABI. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 116 | static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) { |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 117 | // The call frame needs to be at least big enough for linkage and 8 args. |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 118 | return getLinkageSize(isPPC64, isDarwinABI) + |
| 119 | getMinCallArgumentsSize(isPPC64, isDarwinABI); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 120 | } |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 121 | |
| 122 | // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 123 | const SpillSlot * |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 124 | getCalleeSavedSpillSlots(unsigned &NumEntries) const { |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 125 | if (Subtarget.isDarwinABI()) { |
Dale Johannesen | f7801b4 | 2009-11-24 22:59:02 +0000 | [diff] [blame] | 126 | NumEntries = 1; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 127 | if (Subtarget.isPPC64()) { |
Dale Johannesen | 0106a0a | 2009-11-25 00:58:21 +0000 | [diff] [blame] | 128 | static const SpillSlot darwin64Offsets = {PPC::X31, -8}; |
| 129 | return &darwin64Offsets; |
Dale Johannesen | f7801b4 | 2009-11-24 22:59:02 +0000 | [diff] [blame] | 130 | } else { |
Dale Johannesen | 0106a0a | 2009-11-25 00:58:21 +0000 | [diff] [blame] | 131 | static const SpillSlot darwinOffsets = {PPC::R31, -4}; |
| 132 | return &darwinOffsets; |
Dale Johannesen | f7801b4 | 2009-11-24 22:59:02 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 136 | // Early exit if not using the SVR4 ABI. |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 137 | if (!Subtarget.isSVR4ABI()) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 138 | NumEntries = 0; |
| 139 | return 0; |
| 140 | } |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 141 | |
| 142 | static const SpillSlot Offsets[] = { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 143 | // Floating-point register save area offsets. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 144 | {PPC::F31, -8}, |
| 145 | {PPC::F30, -16}, |
| 146 | {PPC::F29, -24}, |
| 147 | {PPC::F28, -32}, |
| 148 | {PPC::F27, -40}, |
| 149 | {PPC::F26, -48}, |
| 150 | {PPC::F25, -56}, |
| 151 | {PPC::F24, -64}, |
| 152 | {PPC::F23, -72}, |
| 153 | {PPC::F22, -80}, |
| 154 | {PPC::F21, -88}, |
| 155 | {PPC::F20, -96}, |
| 156 | {PPC::F19, -104}, |
| 157 | {PPC::F18, -112}, |
| 158 | {PPC::F17, -120}, |
| 159 | {PPC::F16, -128}, |
| 160 | {PPC::F15, -136}, |
| 161 | {PPC::F14, -144}, |
| 162 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 163 | // General register save area offsets. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 164 | {PPC::R31, -4}, |
| 165 | {PPC::R30, -8}, |
| 166 | {PPC::R29, -12}, |
| 167 | {PPC::R28, -16}, |
| 168 | {PPC::R27, -20}, |
| 169 | {PPC::R26, -24}, |
| 170 | {PPC::R25, -28}, |
| 171 | {PPC::R24, -32}, |
| 172 | {PPC::R23, -36}, |
| 173 | {PPC::R22, -40}, |
| 174 | {PPC::R21, -44}, |
| 175 | {PPC::R20, -48}, |
| 176 | {PPC::R19, -52}, |
| 177 | {PPC::R18, -56}, |
| 178 | {PPC::R17, -60}, |
| 179 | {PPC::R16, -64}, |
| 180 | {PPC::R15, -68}, |
| 181 | {PPC::R14, -72}, |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 182 | |
Roman Divacky | 9d760ae | 2012-09-12 14:47:47 +0000 | [diff] [blame] | 183 | // CR save area offset. We map each of the nonvolatile CR fields |
| 184 | // to the slot for CR2, which is the first of the nonvolatile CR |
| 185 | // fields to be assigned, so that we only allocate one save slot. |
| 186 | // See PPCRegisterInfo::hasReservedSpillSlot() for more information. |
| 187 | {PPC::CR2, -4}, |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 188 | |
| 189 | // VRSAVE save area offset. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 190 | {PPC::VRSAVE, -4}, |
| 191 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 192 | // Vector register save area |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 193 | {PPC::V31, -16}, |
| 194 | {PPC::V30, -32}, |
| 195 | {PPC::V29, -48}, |
| 196 | {PPC::V28, -64}, |
| 197 | {PPC::V27, -80}, |
| 198 | {PPC::V26, -96}, |
| 199 | {PPC::V25, -112}, |
| 200 | {PPC::V24, -128}, |
| 201 | {PPC::V23, -144}, |
| 202 | {PPC::V22, -160}, |
| 203 | {PPC::V21, -176}, |
| 204 | {PPC::V20, -192} |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 205 | }; |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 206 | |
| 207 | static const SpillSlot Offsets64[] = { |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 208 | // Floating-point register save area offsets. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 209 | {PPC::F31, -8}, |
| 210 | {PPC::F30, -16}, |
| 211 | {PPC::F29, -24}, |
| 212 | {PPC::F28, -32}, |
| 213 | {PPC::F27, -40}, |
| 214 | {PPC::F26, -48}, |
| 215 | {PPC::F25, -56}, |
| 216 | {PPC::F24, -64}, |
| 217 | {PPC::F23, -72}, |
| 218 | {PPC::F22, -80}, |
| 219 | {PPC::F21, -88}, |
| 220 | {PPC::F20, -96}, |
| 221 | {PPC::F19, -104}, |
| 222 | {PPC::F18, -112}, |
| 223 | {PPC::F17, -120}, |
| 224 | {PPC::F16, -128}, |
| 225 | {PPC::F15, -136}, |
| 226 | {PPC::F14, -144}, |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 227 | |
| 228 | // General register save area offsets. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 229 | {PPC::X31, -8}, |
| 230 | {PPC::X30, -16}, |
| 231 | {PPC::X29, -24}, |
| 232 | {PPC::X28, -32}, |
| 233 | {PPC::X27, -40}, |
| 234 | {PPC::X26, -48}, |
| 235 | {PPC::X25, -56}, |
| 236 | {PPC::X24, -64}, |
| 237 | {PPC::X23, -72}, |
| 238 | {PPC::X22, -80}, |
| 239 | {PPC::X21, -88}, |
| 240 | {PPC::X20, -96}, |
| 241 | {PPC::X19, -104}, |
| 242 | {PPC::X18, -112}, |
| 243 | {PPC::X17, -120}, |
| 244 | {PPC::X16, -128}, |
| 245 | {PPC::X15, -136}, |
| 246 | {PPC::X14, -144}, |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 247 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 248 | // VRSAVE save area offset. |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 249 | {PPC::VRSAVE, -4}, |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 250 | |
| 251 | // Vector register save area |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 252 | {PPC::V31, -16}, |
| 253 | {PPC::V30, -32}, |
| 254 | {PPC::V29, -48}, |
| 255 | {PPC::V28, -64}, |
| 256 | {PPC::V27, -80}, |
| 257 | {PPC::V26, -96}, |
| 258 | {PPC::V25, -112}, |
| 259 | {PPC::V24, -128}, |
| 260 | {PPC::V23, -144}, |
| 261 | {PPC::V22, -160}, |
| 262 | {PPC::V21, -176}, |
| 263 | {PPC::V20, -192} |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 264 | }; |
Tilmann Scheller | 8ff95de | 2009-09-27 17:58:47 +0000 | [diff] [blame] | 265 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 266 | if (Subtarget.isPPC64()) { |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 267 | NumEntries = array_lengthof(Offsets64); |
| 268 | |
| 269 | return Offsets64; |
| 270 | } else { |
| 271 | NumEntries = array_lengthof(Offsets); |
| 272 | |
| 273 | return Offsets; |
| 274 | } |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 275 | } |
Nate Begeman | ca068e8 | 2004-08-14 22:16:36 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | } // End llvm namespace |
| 279 | |
| 280 | #endif |