Add a comment about overlapping PPC frame offsets
[oota-llvm.git] / lib / Target / PowerPC / PPCFrameLowering.h
1 //===-- PPCFrameLowering.h - Define frame lowering for PowerPC --*- C++ -*-===//
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 //
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef POWERPC_FRAMEINFO_H
14 #define POWERPC_FRAMEINFO_H
15
16 #include "PPC.h"
17 #include "PPCSubtarget.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Target/TargetFrameLowering.h"
20 #include "llvm/Target/TargetMachine.h"
21
22 namespace llvm {
23   class PPCSubtarget;
24
25 class PPCFrameLowering: public TargetFrameLowering {
26   const PPCSubtarget &Subtarget;
27
28 public:
29   PPCFrameLowering(const PPCSubtarget &sti)
30     : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
31         (sti.hasQPX() || sti.isBGQ()) ? 32 : 16, 0),
32       Subtarget(sti) {
33   }
34
35   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
42   bool hasFP(const MachineFunction &MF) const;
43   bool needsFP(const MachineFunction &MF) const;
44
45   void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
46                                             RegScavenger *RS = NULL) const;
47   void processFunctionBeforeFrameFinalized(MachineFunction &MF) const;
48
49   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
50                                  MachineBasicBlock::iterator MI,
51                                  const std::vector<CalleeSavedInfo> &CSI,
52                                  const TargetRegisterInfo *TRI) const;
53
54   void eliminateCallFramePseudoInstr(MachineFunction &MF,
55                                      MachineBasicBlock &MBB,
56                                      MachineBasicBlock::iterator I) const;
57
58   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
59                                    MachineBasicBlock::iterator MI,
60                                    const std::vector<CalleeSavedInfo> &CSI,
61                                    const TargetRegisterInfo *TRI) const;
62
63   /// 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
68   /// getReturnSaveOffset - Return the previous frame offset to save the
69   /// return address.
70   static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) {
71     if (isDarwinABI)
72       return isPPC64 ? 16 : 8;
73     // SVR4 ABI:
74     return isPPC64 ? 16 : 4;
75   }
76
77   /// getFramePointerSaveOffset - Return the previous frame offset to save the
78   /// frame pointer.
79   static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) {
80     // For the Darwin ABI:
81     // 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.
85     if (isDarwinABI)
86       return isPPC64 ? -8U : -4U;
87
88     // SVR4 ABI: First slot in the general register save area.
89     return isPPC64 ? -8U : -4U;
90   }
91
92   /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
93   ///
94   static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) {
95     if (isDarwinABI || isPPC64)
96       return 6 * (isPPC64 ? 8 : 4);
97
98     // SVR4 ABI:
99     return 8;
100   }
101
102   /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI
103   /// argument area.
104   static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) {
105     // For the Darwin ABI / 64-bit SVR4 ABI:
106     // 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.
111     if (isDarwinABI || isPPC64)
112       return 8 * (isPPC64 ? 8 : 4);
113
114     // 32-bit SVR4 ABI:
115     // There is no default stack allocated for the 8 first GPR arguments.
116     return 0;
117   }
118
119   /// getMinCallFrameSize - Return the minimum size a call frame can be using
120   /// the PowerPC ABI.
121   static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
122     // The call frame needs to be at least big enough for linkage and 8 args.
123     return getLinkageSize(isPPC64, isDarwinABI) +
124            getMinCallArgumentsSize(isPPC64, isDarwinABI);
125   }
126
127   // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
128   const SpillSlot *
129   getCalleeSavedSpillSlots(unsigned &NumEntries) const {
130     if (Subtarget.isDarwinABI()) {
131       NumEntries = 1;
132       if (Subtarget.isPPC64()) {
133         static const SpillSlot darwin64Offsets = {PPC::X31, -8};
134         return &darwin64Offsets;
135       } else {
136         static const SpillSlot darwinOffsets = {PPC::R31, -4};
137         return &darwinOffsets;
138       }
139     }
140
141     // Early exit if not using the SVR4 ABI.
142     if (!Subtarget.isSVR4ABI()) {
143       NumEntries = 0;
144       return 0;
145     }
146
147     // Note that the offsets here overlap, but this is fixed up in
148     // processFunctionBeforeFrameFinalized.
149
150     static const SpillSlot Offsets[] = {
151       // Floating-point register save area offsets.
152       {PPC::F31, -8},
153       {PPC::F30, -16},
154       {PPC::F29, -24},
155       {PPC::F28, -32},
156       {PPC::F27, -40},
157       {PPC::F26, -48},
158       {PPC::F25, -56},
159       {PPC::F24, -64},
160       {PPC::F23, -72},
161       {PPC::F22, -80},
162       {PPC::F21, -88},
163       {PPC::F20, -96},
164       {PPC::F19, -104},
165       {PPC::F18, -112},
166       {PPC::F17, -120},
167       {PPC::F16, -128},
168       {PPC::F15, -136},
169       {PPC::F14, -144},
170
171       // General register save area offsets.
172       {PPC::R31, -4},
173       {PPC::R30, -8},
174       {PPC::R29, -12},
175       {PPC::R28, -16},
176       {PPC::R27, -20},
177       {PPC::R26, -24},
178       {PPC::R25, -28},
179       {PPC::R24, -32},
180       {PPC::R23, -36},
181       {PPC::R22, -40},
182       {PPC::R21, -44},
183       {PPC::R20, -48},
184       {PPC::R19, -52},
185       {PPC::R18, -56},
186       {PPC::R17, -60},
187       {PPC::R16, -64},
188       {PPC::R15, -68},
189       {PPC::R14, -72},
190
191       // CR save area offset.  We map each of the nonvolatile CR fields
192       // to the slot for CR2, which is the first of the nonvolatile CR
193       // fields to be assigned, so that we only allocate one save slot.
194       // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
195       {PPC::CR2, -4},
196
197       // VRSAVE save area offset.
198       {PPC::VRSAVE, -4},
199
200       // Vector register save area
201       {PPC::V31, -16},
202       {PPC::V30, -32},
203       {PPC::V29, -48},
204       {PPC::V28, -64},
205       {PPC::V27, -80},
206       {PPC::V26, -96},
207       {PPC::V25, -112},
208       {PPC::V24, -128},
209       {PPC::V23, -144},
210       {PPC::V22, -160},
211       {PPC::V21, -176},
212       {PPC::V20, -192}
213     };
214
215     static const SpillSlot Offsets64[] = {
216       // Floating-point register save area offsets.
217       {PPC::F31, -8},
218       {PPC::F30, -16},
219       {PPC::F29, -24},
220       {PPC::F28, -32},
221       {PPC::F27, -40},
222       {PPC::F26, -48},
223       {PPC::F25, -56},
224       {PPC::F24, -64},
225       {PPC::F23, -72},
226       {PPC::F22, -80},
227       {PPC::F21, -88},
228       {PPC::F20, -96},
229       {PPC::F19, -104},
230       {PPC::F18, -112},
231       {PPC::F17, -120},
232       {PPC::F16, -128},
233       {PPC::F15, -136},
234       {PPC::F14, -144},
235
236       // General register save area offsets.
237       {PPC::X31, -8},
238       {PPC::X30, -16},
239       {PPC::X29, -24},
240       {PPC::X28, -32},
241       {PPC::X27, -40},
242       {PPC::X26, -48},
243       {PPC::X25, -56},
244       {PPC::X24, -64},
245       {PPC::X23, -72},
246       {PPC::X22, -80},
247       {PPC::X21, -88},
248       {PPC::X20, -96},
249       {PPC::X19, -104},
250       {PPC::X18, -112},
251       {PPC::X17, -120},
252       {PPC::X16, -128},
253       {PPC::X15, -136},
254       {PPC::X14, -144},
255
256       // VRSAVE save area offset.
257       {PPC::VRSAVE, -4},
258
259       // Vector register save area
260       {PPC::V31, -16},
261       {PPC::V30, -32},
262       {PPC::V29, -48},
263       {PPC::V28, -64},
264       {PPC::V27, -80},
265       {PPC::V26, -96},
266       {PPC::V25, -112},
267       {PPC::V24, -128},
268       {PPC::V23, -144},
269       {PPC::V22, -160},
270       {PPC::V21, -176},
271       {PPC::V20, -192}
272     };
273
274     if (Subtarget.isPPC64()) {
275       NumEntries = array_lengthof(Offsets64);
276
277       return Offsets64;
278     } else {
279       NumEntries = array_lengthof(Offsets);
280
281       return Offsets;
282     }
283   }
284 };
285
286 } // End llvm namespace
287
288 #endif