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