blob: 9acf129be5b59b6eaeca6d6fafd03f2ab29fe503 [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"
Chandler Carrutha1514e22012-12-04 07:12:27 +000018#include "llvm/ADT/STLExtras.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000019#include "llvm/Target/TargetFrameLowering.h"
Nate Begemanca068e82004-08-14 22:16:36 +000020#include "llvm/Target/TargetMachine.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)
Hal Finkel9a79b322013-01-30 23:43:27 +000030 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
31 (sti.hasQPX() || sti.isBGQ()) ? 32 : 16, 0),
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000032 Subtarget(sti) {
Nate Begemanca068e82004-08-14 22:16:36 +000033 }
34
Hal Finkel0cfb42a2013-03-15 05:06:04 +000035 unsigned determineFrameLayout(MachineFunction &MF,
36 bool UpdateMF = true,
37 bool UseEstimate = false) const;
Anton Korobeynikov33464912010-11-15 00:06:54 +000038
39 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
40 /// the function.
41 void emitPrologue(MachineFunction &MF) const;
42 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const;
43
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000044 bool hasFP(const MachineFunction &MF) const;
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +000045 bool needsFP(const MachineFunction &MF) const;
Hal Finkele9cc0a02013-03-21 19:03:19 +000046 void replaceFPWithRealFP(MachineFunction &MF) const;
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000047
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000048 void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
49 RegScavenger *RS = NULL) const;
Hal Finkel3080d232013-03-14 20:33:40 +000050 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
51 RegScavenger *RS = NULL) const;
Hal Finkel0cfb42a2013-03-15 05:06:04 +000052 void addScavengingSpillSlot(MachineFunction &MF, RegScavenger *RS) const;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000053
Roman Divacky9d760ae2012-09-12 14:47:47 +000054 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
55 MachineBasicBlock::iterator MI,
56 const std::vector<CalleeSavedInfo> &CSI,
57 const TargetRegisterInfo *TRI) const;
58
Eli Bendersky700ed802013-02-21 20:05:00 +000059 void eliminateCallFramePseudoInstr(MachineFunction &MF,
60 MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator I) const;
62
Roman Divacky9d760ae2012-09-12 14:47:47 +000063 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
64 MachineBasicBlock::iterator MI,
65 const std::vector<CalleeSavedInfo> &CSI,
66 const TargetRegisterInfo *TRI) const;
67
Anton Korobeynikov33464912010-11-15 00:06:54 +000068 /// targetHandlesStackFrameRounding - Returns true if the target is
69 /// responsible for rounding up the stack frame (probably at emitPrologue
70 /// time).
71 bool targetHandlesStackFrameRounding() const { return true; }
72
Jim Laskey51fe9d92006-12-06 17:42:06 +000073 /// getReturnSaveOffset - Return the previous frame offset to save the
74 /// return address.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000075 static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000076 if (isDarwinABI)
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000077 return isPPC64 ? 16 : 8;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000078 // SVR4 ABI:
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000079 return isPPC64 ? 16 : 4;
Nate Begemanca068e82004-08-14 22:16:36 +000080 }
Jim Laskey51fe9d92006-12-06 17:42:06 +000081
Jim Laskey2f616bf2006-11-16 22:43:37 +000082 /// getFramePointerSaveOffset - Return the previous frame offset to save the
83 /// frame pointer.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000084 static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000085 // For the Darwin ABI:
Dale Johannesenf7801b42009-11-24 22:59:02 +000086 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
87 // for saving the frame pointer (if needed.) While the published ABI has
88 // not used this slot since at least MacOSX 10.2, there is older code
89 // around that does use it, and that needs to continue to work.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000090 if (isDarwinABI)
Dale Johannesenf7801b42009-11-24 22:59:02 +000091 return isPPC64 ? -8U : -4U;
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000092
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000093 // SVR4 ABI: First slot in the general register save area.
Tilmann Schellercfcb7992009-12-18 13:00:34 +000094 return isPPC64 ? -8U : -4U;
Jim Laskey2f616bf2006-11-16 22:43:37 +000095 }
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000096
Hal Finkelfe47bf82013-07-17 00:45:52 +000097 /// getBasePointerSaveOffset - Return the previous frame offset to save the
98 /// base pointer.
99 static unsigned getBasePointerSaveOffset(bool isPPC64, bool isDarwinABI,
100 bool hasFP) {
101 if (!hasFP)
102 return getFramePointerSaveOffset(isPPC64, isDarwinABI);
103
104 if (isDarwinABI)
105 return isPPC64 ? -16U : -8U;
106
107 // SVR4 ABI: First slot in the general register save area.
108 return isPPC64 ? -16U : -8U;
109 }
110
Jim Laskey2f616bf2006-11-16 22:43:37 +0000111 /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
112 ///
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000113 static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) {
114 if (isDarwinABI || isPPC64)
115 return 6 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +0000116
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000117 // SVR4 ABI:
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +0000118 return 8;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000119 }
120
121 /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI
122 /// argument area.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000123 static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) {
124 // For the Darwin ABI / 64-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000125 // The prolog code of the callee may store up to 8 GPR argument registers to
126 // the stack, allowing va_start to index over them in memory if its varargs.
127 // Because we cannot tell if this is needed on the caller side, we have to
128 // conservatively assume that it is needed. As such, make sure we have at
129 // least enough stack space for the caller to store the 8 GPRs.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000130 if (isDarwinABI || isPPC64)
131 return 8 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +0000132
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000133 // 32-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000134 // There is no default stack allocated for the 8 first GPR arguments.
135 return 0;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000136 }
137
138 /// getMinCallFrameSize - Return the minimum size a call frame can be using
139 /// the PowerPC ABI.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000140 static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000141 // The call frame needs to be at least big enough for linkage and 8 args.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000142 return getLinkageSize(isPPC64, isDarwinABI) +
143 getMinCallArgumentsSize(isPPC64, isDarwinABI);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000144 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000145
146 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000147 const SpillSlot *
Tilmann Schellerffd02002009-07-03 06:45:56 +0000148 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000149 if (Subtarget.isDarwinABI()) {
Dale Johannesenf7801b42009-11-24 22:59:02 +0000150 NumEntries = 1;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000151 if (Subtarget.isPPC64()) {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000152 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
153 return &darwin64Offsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000154 } else {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000155 static const SpillSlot darwinOffsets = {PPC::R31, -4};
156 return &darwinOffsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000157 }
158 }
159
Tilmann Schellerffd02002009-07-03 06:45:56 +0000160 // Early exit if not using the SVR4 ABI.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000161 if (!Subtarget.isSVR4ABI()) {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000162 NumEntries = 0;
163 return 0;
164 }
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000165
Hal Finkel6bc99602013-03-14 18:38:31 +0000166 // Note that the offsets here overlap, but this is fixed up in
167 // processFunctionBeforeFrameFinalized.
168
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000169 static const SpillSlot Offsets[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000170 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000171 {PPC::F31, -8},
172 {PPC::F30, -16},
173 {PPC::F29, -24},
174 {PPC::F28, -32},
175 {PPC::F27, -40},
176 {PPC::F26, -48},
177 {PPC::F25, -56},
178 {PPC::F24, -64},
179 {PPC::F23, -72},
180 {PPC::F22, -80},
181 {PPC::F21, -88},
182 {PPC::F20, -96},
183 {PPC::F19, -104},
184 {PPC::F18, -112},
185 {PPC::F17, -120},
186 {PPC::F16, -128},
187 {PPC::F15, -136},
188 {PPC::F14, -144},
189
Tilmann Schellerffd02002009-07-03 06:45:56 +0000190 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000191 {PPC::R31, -4},
192 {PPC::R30, -8},
193 {PPC::R29, -12},
194 {PPC::R28, -16},
195 {PPC::R27, -20},
196 {PPC::R26, -24},
197 {PPC::R25, -28},
198 {PPC::R24, -32},
199 {PPC::R23, -36},
200 {PPC::R22, -40},
201 {PPC::R21, -44},
202 {PPC::R20, -48},
203 {PPC::R19, -52},
204 {PPC::R18, -56},
205 {PPC::R17, -60},
206 {PPC::R16, -64},
207 {PPC::R15, -68},
208 {PPC::R14, -72},
Tilmann Schellerffd02002009-07-03 06:45:56 +0000209
Roman Divacky9d760ae2012-09-12 14:47:47 +0000210 // CR save area offset. We map each of the nonvolatile CR fields
211 // to the slot for CR2, which is the first of the nonvolatile CR
212 // fields to be assigned, so that we only allocate one save slot.
213 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
214 {PPC::CR2, -4},
Tilmann Schellerffd02002009-07-03 06:45:56 +0000215
216 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000217 {PPC::VRSAVE, -4},
218
Tilmann Schellerffd02002009-07-03 06:45:56 +0000219 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000220 {PPC::V31, -16},
221 {PPC::V30, -32},
222 {PPC::V29, -48},
223 {PPC::V28, -64},
224 {PPC::V27, -80},
225 {PPC::V26, -96},
226 {PPC::V25, -112},
227 {PPC::V24, -128},
228 {PPC::V23, -144},
229 {PPC::V22, -160},
230 {PPC::V21, -176},
231 {PPC::V20, -192}
Tilmann Schellerffd02002009-07-03 06:45:56 +0000232 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000233
234 static const SpillSlot Offsets64[] = {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000235 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000236 {PPC::F31, -8},
237 {PPC::F30, -16},
238 {PPC::F29, -24},
239 {PPC::F28, -32},
240 {PPC::F27, -40},
241 {PPC::F26, -48},
242 {PPC::F25, -56},
243 {PPC::F24, -64},
244 {PPC::F23, -72},
245 {PPC::F22, -80},
246 {PPC::F21, -88},
247 {PPC::F20, -96},
248 {PPC::F19, -104},
249 {PPC::F18, -112},
250 {PPC::F17, -120},
251 {PPC::F16, -128},
252 {PPC::F15, -136},
253 {PPC::F14, -144},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000254
255 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000256 {PPC::X31, -8},
257 {PPC::X30, -16},
258 {PPC::X29, -24},
259 {PPC::X28, -32},
260 {PPC::X27, -40},
261 {PPC::X26, -48},
262 {PPC::X25, -56},
263 {PPC::X24, -64},
264 {PPC::X23, -72},
265 {PPC::X22, -80},
266 {PPC::X21, -88},
267 {PPC::X20, -96},
268 {PPC::X19, -104},
269 {PPC::X18, -112},
270 {PPC::X17, -120},
271 {PPC::X16, -128},
272 {PPC::X15, -136},
273 {PPC::X14, -144},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000274
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000275 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000276 {PPC::VRSAVE, -4},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000277
278 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000279 {PPC::V31, -16},
280 {PPC::V30, -32},
281 {PPC::V29, -48},
282 {PPC::V28, -64},
283 {PPC::V27, -80},
284 {PPC::V26, -96},
285 {PPC::V25, -112},
286 {PPC::V24, -128},
287 {PPC::V23, -144},
288 {PPC::V22, -160},
289 {PPC::V21, -176},
290 {PPC::V20, -192}
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000291 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000292
Anton Korobeynikov33464912010-11-15 00:06:54 +0000293 if (Subtarget.isPPC64()) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000294 NumEntries = array_lengthof(Offsets64);
295
296 return Offsets64;
297 } else {
298 NumEntries = array_lengthof(Offsets);
299
300 return Offsets;
301 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000302 }
Nate Begemanca068e82004-08-14 22:16:36 +0000303};
304
305} // End llvm namespace
306
307#endif