OSDN Git Service

Merge commit 'b3201c5cf1e183d840f7c99ff779d57f1549d8e5' into merge_20130226
[android-x86/external-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     static const SpillSlot Offsets[] = {
148       // Floating-point register save area offsets.
149       {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
168       // General register save area offsets.
169       {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},
187
188       // 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},
193
194       // VRSAVE save area offset.
195       {PPC::VRSAVE, -4},
196
197       // Vector register save area
198       {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}
210     };
211
212     static const SpillSlot Offsets64[] = {
213       // Floating-point register save area offsets.
214       {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},
232
233       // General register save area offsets.
234       {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},
252
253       // VRSAVE save area offset.
254       {PPC::VRSAVE, -4},
255
256       // Vector register save area
257       {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}
269     };
270
271     if (Subtarget.isPPC64()) {
272       NumEntries = array_lengthof(Offsets64);
273
274       return Offsets64;
275     } else {
276       NumEntries = array_lengthof(Offsets);
277
278       return Offsets;
279     }
280   }
281 };
282
283 } // End llvm namespace
284
285 #endif