blob: d09e47fafd1e9d6b979acdd62699b31a713bfb12 [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
Anton Korobeynikov33464912010-11-15 00:06:54 +000035 void determineFrameLayout(MachineFunction &MF) const;
36
37 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
38 /// the function.
39 void emitPrologue(MachineFunction &MF) const;
40 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const;
41
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000042 bool hasFP(const MachineFunction &MF) const;
Anton Korobeynikovc8bd78c2010-12-18 19:53:14 +000043 bool needsFP(const MachineFunction &MF) const;
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000044
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000045 void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
46 RegScavenger *RS = NULL) const;
47 void processFunctionBeforeFrameFinalized(MachineFunction &MF) const;
48
Roman Divacky9d760ae2012-09-12 14:47:47 +000049 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
50 MachineBasicBlock::iterator MI,
51 const std::vector<CalleeSavedInfo> &CSI,
52 const TargetRegisterInfo *TRI) const;
53
Eli Bendersky700ed802013-02-21 20:05:00 +000054 void eliminateCallFramePseudoInstr(MachineFunction &MF,
55 MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator I) const;
57
Roman Divacky9d760ae2012-09-12 14:47:47 +000058 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
59 MachineBasicBlock::iterator MI,
60 const std::vector<CalleeSavedInfo> &CSI,
61 const TargetRegisterInfo *TRI) const;
62
Anton Korobeynikov33464912010-11-15 00:06:54 +000063 /// targetHandlesStackFrameRounding - Returns true if the target is
64 /// responsible for rounding up the stack frame (probably at emitPrologue
65 /// time).
66 bool targetHandlesStackFrameRounding() const { return true; }
67
Jim Laskey51fe9d92006-12-06 17:42:06 +000068 /// getReturnSaveOffset - Return the previous frame offset to save the
69 /// return address.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000070 static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000071 if (isDarwinABI)
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000072 return isPPC64 ? 16 : 8;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000073 // SVR4 ABI:
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000074 return isPPC64 ? 16 : 4;
Nate Begemanca068e82004-08-14 22:16:36 +000075 }
Jim Laskey51fe9d92006-12-06 17:42:06 +000076
Jim Laskey2f616bf2006-11-16 22:43:37 +000077 /// getFramePointerSaveOffset - Return the previous frame offset to save the
78 /// frame pointer.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000079 static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000080 // For the Darwin ABI:
Dale Johannesenf7801b42009-11-24 22:59:02 +000081 // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
82 // for saving the frame pointer (if needed.) While the published ABI has
83 // not used this slot since at least MacOSX 10.2, there is older code
84 // around that does use it, and that needs to continue to work.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000085 if (isDarwinABI)
Dale Johannesenf7801b42009-11-24 22:59:02 +000086 return isPPC64 ? -8U : -4U;
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000087
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000088 // SVR4 ABI: First slot in the general register save area.
Tilmann Schellercfcb7992009-12-18 13:00:34 +000089 return isPPC64 ? -8U : -4U;
Jim Laskey2f616bf2006-11-16 22:43:37 +000090 }
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000091
Jim Laskey2f616bf2006-11-16 22:43:37 +000092 /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
93 ///
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000094 static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) {
95 if (isDarwinABI || isPPC64)
96 return 6 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +000097
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +000098 // SVR4 ABI:
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +000099 return 8;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000100 }
101
102 /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI
103 /// argument area.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000104 static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) {
105 // For the Darwin ABI / 64-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000106 // The prolog code of the callee may store up to 8 GPR argument registers to
107 // the stack, allowing va_start to index over them in memory if its varargs.
108 // Because we cannot tell if this is needed on the caller side, we have to
109 // conservatively assume that it is needed. As such, make sure we have at
110 // least enough stack space for the caller to store the 8 GPRs.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000111 if (isDarwinABI || isPPC64)
112 return 8 * (isPPC64 ? 8 : 4);
Anton Korobeynikov78b4fee2010-11-15 00:06:05 +0000113
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000114 // 32-bit SVR4 ABI:
Chris Lattner9f0bc652007-02-25 05:34:32 +0000115 // There is no default stack allocated for the 8 first GPR arguments.
116 return 0;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000117 }
118
119 /// getMinCallFrameSize - Return the minimum size a call frame can be using
120 /// the PowerPC ABI.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000121 static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000122 // The call frame needs to be at least big enough for linkage and 8 args.
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000123 return getLinkageSize(isPPC64, isDarwinABI) +
124 getMinCallArgumentsSize(isPPC64, isDarwinABI);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000125 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000126
127 // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000128 const SpillSlot *
Tilmann Schellerffd02002009-07-03 06:45:56 +0000129 getCalleeSavedSpillSlots(unsigned &NumEntries) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000130 if (Subtarget.isDarwinABI()) {
Dale Johannesenf7801b42009-11-24 22:59:02 +0000131 NumEntries = 1;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000132 if (Subtarget.isPPC64()) {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000133 static const SpillSlot darwin64Offsets = {PPC::X31, -8};
134 return &darwin64Offsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000135 } else {
Dale Johannesen0106a0a2009-11-25 00:58:21 +0000136 static const SpillSlot darwinOffsets = {PPC::R31, -4};
137 return &darwinOffsets;
Dale Johannesenf7801b42009-11-24 22:59:02 +0000138 }
139 }
140
Tilmann Schellerffd02002009-07-03 06:45:56 +0000141 // Early exit if not using the SVR4 ABI.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000142 if (!Subtarget.isSVR4ABI()) {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000143 NumEntries = 0;
144 return 0;
145 }
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000146
147 static const SpillSlot Offsets[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +0000148 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000149 {PPC::F31, -8},
150 {PPC::F30, -16},
151 {PPC::F29, -24},
152 {PPC::F28, -32},
153 {PPC::F27, -40},
154 {PPC::F26, -48},
155 {PPC::F25, -56},
156 {PPC::F24, -64},
157 {PPC::F23, -72},
158 {PPC::F22, -80},
159 {PPC::F21, -88},
160 {PPC::F20, -96},
161 {PPC::F19, -104},
162 {PPC::F18, -112},
163 {PPC::F17, -120},
164 {PPC::F16, -128},
165 {PPC::F15, -136},
166 {PPC::F14, -144},
167
Tilmann Schellerffd02002009-07-03 06:45:56 +0000168 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000169 {PPC::R31, -4},
170 {PPC::R30, -8},
171 {PPC::R29, -12},
172 {PPC::R28, -16},
173 {PPC::R27, -20},
174 {PPC::R26, -24},
175 {PPC::R25, -28},
176 {PPC::R24, -32},
177 {PPC::R23, -36},
178 {PPC::R22, -40},
179 {PPC::R21, -44},
180 {PPC::R20, -48},
181 {PPC::R19, -52},
182 {PPC::R18, -56},
183 {PPC::R17, -60},
184 {PPC::R16, -64},
185 {PPC::R15, -68},
186 {PPC::R14, -72},
Tilmann Schellerffd02002009-07-03 06:45:56 +0000187
Roman Divacky9d760ae2012-09-12 14:47:47 +0000188 // CR save area offset. We map each of the nonvolatile CR fields
189 // to the slot for CR2, which is the first of the nonvolatile CR
190 // fields to be assigned, so that we only allocate one save slot.
191 // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
192 {PPC::CR2, -4},
Tilmann Schellerffd02002009-07-03 06:45:56 +0000193
194 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000195 {PPC::VRSAVE, -4},
196
Tilmann Schellerffd02002009-07-03 06:45:56 +0000197 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000198 {PPC::V31, -16},
199 {PPC::V30, -32},
200 {PPC::V29, -48},
201 {PPC::V28, -64},
202 {PPC::V27, -80},
203 {PPC::V26, -96},
204 {PPC::V25, -112},
205 {PPC::V24, -128},
206 {PPC::V23, -144},
207 {PPC::V22, -160},
208 {PPC::V21, -176},
209 {PPC::V20, -192}
Tilmann Schellerffd02002009-07-03 06:45:56 +0000210 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000211
212 static const SpillSlot Offsets64[] = {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000213 // Floating-point register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000214 {PPC::F31, -8},
215 {PPC::F30, -16},
216 {PPC::F29, -24},
217 {PPC::F28, -32},
218 {PPC::F27, -40},
219 {PPC::F26, -48},
220 {PPC::F25, -56},
221 {PPC::F24, -64},
222 {PPC::F23, -72},
223 {PPC::F22, -80},
224 {PPC::F21, -88},
225 {PPC::F20, -96},
226 {PPC::F19, -104},
227 {PPC::F18, -112},
228 {PPC::F17, -120},
229 {PPC::F16, -128},
230 {PPC::F15, -136},
231 {PPC::F14, -144},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000232
233 // General register save area offsets.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000234 {PPC::X31, -8},
235 {PPC::X30, -16},
236 {PPC::X29, -24},
237 {PPC::X28, -32},
238 {PPC::X27, -40},
239 {PPC::X26, -48},
240 {PPC::X25, -56},
241 {PPC::X24, -64},
242 {PPC::X23, -72},
243 {PPC::X22, -80},
244 {PPC::X21, -88},
245 {PPC::X20, -96},
246 {PPC::X19, -104},
247 {PPC::X18, -112},
248 {PPC::X17, -120},
249 {PPC::X16, -128},
250 {PPC::X15, -136},
251 {PPC::X14, -144},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000252
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000253 // VRSAVE save area offset.
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000254 {PPC::VRSAVE, -4},
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000255
256 // Vector register save area
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000257 {PPC::V31, -16},
258 {PPC::V30, -32},
259 {PPC::V29, -48},
260 {PPC::V28, -64},
261 {PPC::V27, -80},
262 {PPC::V26, -96},
263 {PPC::V25, -112},
264 {PPC::V24, -128},
265 {PPC::V23, -144},
266 {PPC::V22, -160},
267 {PPC::V21, -176},
268 {PPC::V20, -192}
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000269 };
Tilmann Scheller8ff95de2009-09-27 17:58:47 +0000270
Anton Korobeynikov33464912010-11-15 00:06:54 +0000271 if (Subtarget.isPPC64()) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000272 NumEntries = array_lengthof(Offsets64);
273
274 return Offsets64;
275 } else {
276 NumEntries = array_lengthof(Offsets);
277
278 return Offsets;
279 }
Tilmann Schellerffd02002009-07-03 06:45:56 +0000280 }
Nate Begemanca068e82004-08-14 22:16:36 +0000281};
282
283} // End llvm namespace
284
285#endif