blob: 4d957b91c7bb3f8cf02ea2c97bb18807435b8900 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.h - Define frame lowering for PowerPC --*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Nate Begemanca068e82004-08-14 22:16:36 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemanca068e82004-08-14 22:16:36 +00008//===----------------------------------------------------------------------===//
9//
10//
Nate Begeman21e463b2005-10-16 05:39:50 +000011//===----------------------------------------------------------------------===//
Nate Begemanca068e82004-08-14 22:16:36 +000012
13#ifndef POWERPC_FRAMEINFO_H
14#define POWERPC_FRAMEINFO_H
15
Chris Lattner26689592005-10-14 23:51:18 +000016#include "PPC.h"
Tilmann Schellerffd02002009-07-03 06:45:56 +000017#include "PPCSubtarget.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000018#include "llvm/Target/TargetFrameLowering.h"
Nate Begemanca068e82004-08-14 22:16:36 +000019#include "llvm/Target/TargetMachine.h"
Tilmann Schellerffd02002009-07-03 06:45:56 +000020#include "llvm/ADT/STLExtras.h"
Nate Begemanca068e82004-08-14 22:16:36 +000021
22namespace llvm {
Anton Korobeynikov33464912010-11-15 00:06:54 +000023 class PPCSubtarget;
Nate Begemanca068e82004-08-14 22:16:36 +000024
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000025class PPCFrameLowering: public TargetFrameLowering {
Anton Korobeynikov33464912010-11-15 00:06:54 +000026 const PPCSubtarget &Subtarget;
Misha Brukmanb5f662f2005-04-21 23:30:14 +000027
Nate Begemanca068e82004-08-14 22:16:36 +000028public:
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000029 PPCFrameLowering(const PPCSubtarget &sti)
30 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 16, 0),
31 Subtarget(sti) {
Nate Begemanca068e82004-08-14 22:16:36 +000032 }
33
Anton Korobeynikov33464912010-11-15 00:06:54 +000034 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 Korobeynikovd0c38172010-11-18 21:19:35 +000041 bool hasFP(const MachineFunction &MF) const;
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +000042 bool needsFP(const MachineFunction &MF) const;
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000043
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000044 void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
45 RegScavenger *RS = NULL) const;
46 void processFunctionBeforeFrameFinalized(MachineFunction &MF) const;
47
Roman Divacky9d760ae2012-09-12 14:47:47 +000048 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 Korobeynikov33464912010-11-15 00:06:54 +000058 /// 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 Laskey51fe9d92006-12-06 17:42:06 +000063 /// getReturnSaveOffset - Return the previous frame offset to save the
64 /// return address.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000065 static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000066 if (isDarwinABI)
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000067 return isPPC64 ? 16 : 8;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000068 // SVR4 ABI:
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000069 return isPPC64 ? 16 : 4;
Nate Begemanca068e82004-08-14 22:16:36 +000070 }
Jim Laskey51fe9d92006-12-06 17:42:06 +000071
Jim Laskey2f616bf2006-11-16 22:43:37 +000072 /// getFramePointerSaveOffset - Return the previous frame offset to save the
73 /// frame pointer.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000074 static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000075 // For the Darwin ABI:
Dale Johannesenf7801b42009-11-24 22:59:02 +000076 // 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 Scheller2a9ddfb2009-07-03 06:47:08 +000080 if (isDarwinABI)
Dale Johannesenf7801b42009-11-24 22:59:02 +000081 return isPPC64 ? -8U : -4U;
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000082
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000083 // SVR4 ABI: First slot in the general register save area.
Tilmann Schellercfcb7992009-12-18 13:00:34 +000084 return isPPC64 ? -8U : -4U;
Jim Laskey2f616bf2006-11-16 22:43:37 +000085 }
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000086
Jim Laskey2f616bf2006-11-16 22:43:37 +000087 /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
88 ///
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000089 static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) {
90 if (isDarwinABI || isPPC64)
91 return 6 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000092
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000093 // SVR4 ABI:
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +000094 return 8;
Jim Laskey2f616bf2006-11-16 22:43:37 +000095 }
96
97 /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI
98 /// argument area.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000099 static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) {
100 // For the Darwin ABI / 64-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000101 // 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 Scheller6b16eff2009-08-15 11:54:46 +0000106 if (isDarwinABI || isPPC64)
107 return 8 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +0000108
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000109 // 32-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000110 // There is no default stack allocated for the 8 first GPR arguments.
111 return 0;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000112 }
113
114 /// getMinCallFrameSize - Return the minimum size a call frame can be using
115 /// the PowerPC ABI.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000116 static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000117 // The call frame needs to be at least big enough for linkage and 8 args.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000118 return getLinkageSize(isPPC64, isDarwinABI) +
119 getMinCallArgumentsSize(isPPC64, isDarwinABI);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000120 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000121
122 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000123 const SpillSlot *
Tilmann Schellerffd02002009-07-03 06:45:56 +0000124 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000125 if (Subtarget.isDarwinABI()) {
Dale Johannesenf7801b42009-11-24 22:59:02 +0000126 NumEntries = 1;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000127 if (Subtarget.isPPC64()) {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000128 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
129 return &darwin64Offsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000130 } else {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000131 static const SpillSlot darwinOffsets = {PPC::R31, -4};
132 return &darwinOffsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000133 }
134 }
135
Tilmann Schellerffd02002009-07-03 06:45:56 +0000136 // Early exit if not using the SVR4 ABI.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000137 if (!Subtarget.isSVR4ABI()) {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000138 NumEntries = 0;
139 return 0;
140 }
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000141
142 static const SpillSlot Offsets[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000143 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000144 {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 Schellerffd02002009-07-03 06:45:56 +0000163 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000164 {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 Schellerffd02002009-07-03 06:45:56 +0000182
Roman Divacky9d760ae2012-09-12 14:47:47 +0000183 // 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 Schellerffd02002009-07-03 06:45:56 +0000188
189 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000190 {PPC::VRSAVE, -4},
191
Tilmann Schellerffd02002009-07-03 06:45:56 +0000192 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000193 {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 Schellerffd02002009-07-03 06:45:56 +0000205 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000206
207 static const SpillSlot Offsets64[] = {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000208 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000209 {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 Scheller6b16eff2009-08-15 11:54:46 +0000227
228 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000229 {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 Scheller6b16eff2009-08-15 11:54:46 +0000247
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000248 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000249 {PPC::VRSAVE, -4},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000250
251 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000252 {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 Scheller6b16eff2009-08-15 11:54:46 +0000264 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000265
Anton Korobeynikov33464912010-11-15 00:06:54 +0000266 if (Subtarget.isPPC64()) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000267 NumEntries = array_lengthof(Offsets64);
268
269 return Offsets64;
270 } else {
271 NumEntries = array_lengthof(Offsets);
272
273 return Offsets;
274 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000275 }
Nate Begemanca068e82004-08-14 22:16:36 +0000276};
277
278} // End llvm namespace
279
280#endif