Avoid using MRI::liveout_iterator for computing VRSAVEs.
[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   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
55                                    MachineBasicBlock::iterator MI,
56                                    const std::vector<CalleeSavedInfo> &CSI,
57                                    const TargetRegisterInfo *TRI) const;
58
59   /// targetHandlesStackFrameRounding - Returns true if the target is
60   /// responsible for rounding up the stack frame (probably at emitPrologue
61   /// time).
62   bool targetHandlesStackFrameRounding() const { return true; }
63
64   /// getReturnSaveOffset - Return the previous frame offset to save the
65   /// return address.
66   static unsigned getReturnSaveOffset(bool isPPC64, bool isDarwinABI) {
67     if (isDarwinABI)
68       return isPPC64 ? 16 : 8;
69     // SVR4 ABI:
70     return isPPC64 ? 16 : 4;
71   }
72
73   /// getFramePointerSaveOffset - Return the previous frame offset to save the
74   /// frame pointer.
75   static unsigned getFramePointerSaveOffset(bool isPPC64, bool isDarwinABI) {
76     // For the Darwin ABI:
77     // We cannot use the TOC save slot (offset +20) in the PowerPC linkage area
78     // for saving the frame pointer (if needed.)  While the published ABI has
79     // not used this slot since at least MacOSX 10.2, there is older code
80     // around that does use it, and that needs to continue to work.
81     if (isDarwinABI)
82       return isPPC64 ? -8U : -4U;
83
84     // SVR4 ABI: First slot in the general register save area.
85     return isPPC64 ? -8U : -4U;
86   }
87
88   /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
89   ///
90   static unsigned getLinkageSize(bool isPPC64, bool isDarwinABI) {
91     if (isDarwinABI || isPPC64)
92       return 6 * (isPPC64 ? 8 : 4);
93
94     // SVR4 ABI:
95     return 8;
96   }
97
98   /// getMinCallArgumentsSize - Return the size of the minium PowerPC ABI
99   /// argument area.
100   static unsigned getMinCallArgumentsSize(bool isPPC64, bool isDarwinABI) {
101     // For the Darwin ABI / 64-bit SVR4 ABI:
102     // The prolog code of the callee may store up to 8 GPR argument registers to
103     // the stack, allowing va_start to index over them in memory if its varargs.
104     // Because we cannot tell if this is needed on the caller side, we have to
105     // conservatively assume that it is needed.  As such, make sure we have at
106     // least enough stack space for the caller to store the 8 GPRs.
107     if (isDarwinABI || isPPC64)
108       return 8 * (isPPC64 ? 8 : 4);
109
110     // 32-bit SVR4 ABI:
111     // There is no default stack allocated for the 8 first GPR arguments.
112     return 0;
113   }
114
115   /// getMinCallFrameSize - Return the minimum size a call frame can be using
116   /// the PowerPC ABI.
117   static unsigned getMinCallFrameSize(bool isPPC64, bool isDarwinABI) {
118     // The call frame needs to be at least big enough for linkage and 8 args.
119     return getLinkageSize(isPPC64, isDarwinABI) +
120            getMinCallArgumentsSize(isPPC64, isDarwinABI);
121   }
122
123   // With the SVR4 ABI, callee-saved registers have fixed offsets on the stack.
124   const SpillSlot *
125   getCalleeSavedSpillSlots(unsigned &NumEntries) const {
126     if (Subtarget.isDarwinABI()) {
127       NumEntries = 1;
128       if (Subtarget.isPPC64()) {
129         static const SpillSlot darwin64Offsets = {PPC::X31, -8};
130         return &darwin64Offsets;
131       } else {
132         static const SpillSlot darwinOffsets = {PPC::R31, -4};
133         return &darwinOffsets;
134       }
135     }
136
137     // Early exit if not using the SVR4 ABI.
138     if (!Subtarget.isSVR4ABI()) {
139       NumEntries = 0;
140       return 0;
141     }
142
143     static const SpillSlot Offsets[] = {
144       // Floating-point register save area offsets.
145       {PPC::F31, -8},
146       {PPC::F30, -16},
147       {PPC::F29, -24},
148       {PPC::F28, -32},
149       {PPC::F27, -40},
150       {PPC::F26, -48},
151       {PPC::F25, -56},
152       {PPC::F24, -64},
153       {PPC::F23, -72},
154       {PPC::F22, -80},
155       {PPC::F21, -88},
156       {PPC::F20, -96},
157       {PPC::F19, -104},
158       {PPC::F18, -112},
159       {PPC::F17, -120},
160       {PPC::F16, -128},
161       {PPC::F15, -136},
162       {PPC::F14, -144},
163
164       // General register save area offsets.
165       {PPC::R31, -4},
166       {PPC::R30, -8},
167       {PPC::R29, -12},
168       {PPC::R28, -16},
169       {PPC::R27, -20},
170       {PPC::R26, -24},
171       {PPC::R25, -28},
172       {PPC::R24, -32},
173       {PPC::R23, -36},
174       {PPC::R22, -40},
175       {PPC::R21, -44},
176       {PPC::R20, -48},
177       {PPC::R19, -52},
178       {PPC::R18, -56},
179       {PPC::R17, -60},
180       {PPC::R16, -64},
181       {PPC::R15, -68},
182       {PPC::R14, -72},
183
184       // CR save area offset.  We map each of the nonvolatile CR fields
185       // to the slot for CR2, which is the first of the nonvolatile CR
186       // fields to be assigned, so that we only allocate one save slot.
187       // See PPCRegisterInfo::hasReservedSpillSlot() for more information.
188       {PPC::CR2, -4},
189
190       // VRSAVE save area offset.
191       {PPC::VRSAVE, -4},
192
193       // Vector register save area
194       {PPC::V31, -16},
195       {PPC::V30, -32},
196       {PPC::V29, -48},
197       {PPC::V28, -64},
198       {PPC::V27, -80},
199       {PPC::V26, -96},
200       {PPC::V25, -112},
201       {PPC::V24, -128},
202       {PPC::V23, -144},
203       {PPC::V22, -160},
204       {PPC::V21, -176},
205       {PPC::V20, -192}
206     };
207
208     static const SpillSlot Offsets64[] = {
209       // Floating-point register save area offsets.
210       {PPC::F31, -8},
211       {PPC::F30, -16},
212       {PPC::F29, -24},
213       {PPC::F28, -32},
214       {PPC::F27, -40},
215       {PPC::F26, -48},
216       {PPC::F25, -56},
217       {PPC::F24, -64},
218       {PPC::F23, -72},
219       {PPC::F22, -80},
220       {PPC::F21, -88},
221       {PPC::F20, -96},
222       {PPC::F19, -104},
223       {PPC::F18, -112},
224       {PPC::F17, -120},
225       {PPC::F16, -128},
226       {PPC::F15, -136},
227       {PPC::F14, -144},
228
229       // General register save area offsets.
230       {PPC::X31, -8},
231       {PPC::X30, -16},
232       {PPC::X29, -24},
233       {PPC::X28, -32},
234       {PPC::X27, -40},
235       {PPC::X26, -48},
236       {PPC::X25, -56},
237       {PPC::X24, -64},
238       {PPC::X23, -72},
239       {PPC::X22, -80},
240       {PPC::X21, -88},
241       {PPC::X20, -96},
242       {PPC::X19, -104},
243       {PPC::X18, -112},
244       {PPC::X17, -120},
245       {PPC::X16, -128},
246       {PPC::X15, -136},
247       {PPC::X14, -144},
248
249       // VRSAVE save area offset.
250       {PPC::VRSAVE, -4},
251
252       // Vector register save area
253       {PPC::V31, -16},
254       {PPC::V30, -32},
255       {PPC::V29, -48},
256       {PPC::V28, -64},
257       {PPC::V27, -80},
258       {PPC::V26, -96},
259       {PPC::V25, -112},
260       {PPC::V24, -128},
261       {PPC::V23, -144},
262       {PPC::V22, -160},
263       {PPC::V21, -176},
264       {PPC::V20, -192}
265     };
266
267     if (Subtarget.isPPC64()) {
268       NumEntries = array_lengthof(Offsets64);
269
270       return Offsets64;
271     } else {
272       NumEntries = array_lengthof(Offsets);
273
274       return Offsets;
275     }
276   }
277 };
278
279 } // End llvm namespace
280
281 #endif