1 //===- PPCRegisterInfo.cpp - PowerPC Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the PowerPC implementation of the TargetRegisterInfo
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "reginfo"
17 #include "PPCInstrBuilder.h"
18 #include "PPCMachineFunctionInfo.h"
19 #include "PPCRegisterInfo.h"
20 #include "PPCFrameInfo.h"
21 #include "PPCSubtarget.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Function.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/ValueTypes.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineLocation.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/RegisterScavenging.h"
34 #include "llvm/Target/TargetFrameInfo.h"
35 #include "llvm/Target/TargetInstrInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/ADT/BitVector.h"
44 #include "llvm/ADT/STLExtras.h"
47 // FIXME This disables some code that aligns the stack to a boundary
48 // bigger than the default (16 bytes on Darwin) when there is a stack local
49 // of greater alignment. This does not currently work, because the delta
50 // between old and new stack pointers is added to offsets that reference
51 // incoming parameters after the prolog is generated, and the code that
52 // does that doesn't handle a variable delta. You don't want to do that
53 // anyway; a better approach is to reserve another register that retains
54 // to the incoming stack pointer, and reference parameters relative to that.
57 // FIXME (64-bit): Eventually enable by default.
59 cl::opt<bool> EnablePPC32RS("enable-ppc32-regscavenger",
61 cl::desc("Enable PPC32 register scavenger"),
63 cl::opt<bool> EnablePPC64RS("enable-ppc64-regscavenger",
65 cl::desc("Enable PPC64 register scavenger"),
71 #define EnableRegisterScavenging \
72 ((EnablePPC32RS && !Subtarget.isPPC64()) || \
73 (EnablePPC64RS && Subtarget.isPPC64()))
75 // FIXME (64-bit): Should be inlined.
77 PPCRegisterInfo::requiresRegisterScavenging(const MachineFunction &) const {
78 return EnableRegisterScavenging;
81 /// getRegisterNumbering - Given the enum value for some register, e.g.
82 /// PPC::F14, return the number that it corresponds to (e.g. 14).
83 unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
87 case R0 : case X0 : case F0 : case V0 : case CR0: case CR0LT: return 0;
88 case R1 : case X1 : case F1 : case V1 : case CR1: case CR0GT: return 1;
89 case R2 : case X2 : case F2 : case V2 : case CR2: case CR0EQ: return 2;
90 case R3 : case X3 : case F3 : case V3 : case CR3: case CR0UN: return 3;
91 case R4 : case X4 : case F4 : case V4 : case CR4: case CR1LT: return 4;
92 case R5 : case X5 : case F5 : case V5 : case CR5: case CR1GT: return 5;
93 case R6 : case X6 : case F6 : case V6 : case CR6: case CR1EQ: return 6;
94 case R7 : case X7 : case F7 : case V7 : case CR7: case CR1UN: return 7;
95 case R8 : case X8 : case F8 : case V8 : case CR2LT: return 8;
96 case R9 : case X9 : case F9 : case V9 : case CR2GT: return 9;
97 case R10: case X10: case F10: case V10: case CR2EQ: return 10;
98 case R11: case X11: case F11: case V11: case CR2UN: return 11;
99 case R12: case X12: case F12: case V12: case CR3LT: return 12;
100 case R13: case X13: case F13: case V13: case CR3GT: return 13;
101 case R14: case X14: case F14: case V14: case CR3EQ: return 14;
102 case R15: case X15: case F15: case V15: case CR3UN: return 15;
103 case R16: case X16: case F16: case V16: case CR4LT: return 16;
104 case R17: case X17: case F17: case V17: case CR4GT: return 17;
105 case R18: case X18: case F18: case V18: case CR4EQ: return 18;
106 case R19: case X19: case F19: case V19: case CR4UN: return 19;
107 case R20: case X20: case F20: case V20: case CR5LT: return 20;
108 case R21: case X21: case F21: case V21: case CR5GT: return 21;
109 case R22: case X22: case F22: case V22: case CR5EQ: return 22;
110 case R23: case X23: case F23: case V23: case CR5UN: return 23;
111 case R24: case X24: case F24: case V24: case CR6LT: return 24;
112 case R25: case X25: case F25: case V25: case CR6GT: return 25;
113 case R26: case X26: case F26: case V26: case CR6EQ: return 26;
114 case R27: case X27: case F27: case V27: case CR6UN: return 27;
115 case R28: case X28: case F28: case V28: case CR7LT: return 28;
116 case R29: case X29: case F29: case V29: case CR7GT: return 29;
117 case R30: case X30: case F30: case V30: case CR7EQ: return 30;
118 case R31: case X31: case F31: case V31: case CR7UN: return 31;
120 llvm_unreachable("Unhandled reg in PPCRegisterInfo::getRegisterNumbering!");
124 PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
125 const TargetInstrInfo &tii)
126 : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
127 Subtarget(ST), TII(tii) {
128 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
129 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
130 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
131 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
132 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
133 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
134 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
135 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
138 ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
139 ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
140 ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
141 ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
142 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32;
145 /// getPointerRegClass - Return the register class to use to hold pointers.
146 /// This is used for addressing modes.
147 const TargetRegisterClass *
148 PPCRegisterInfo::getPointerRegClass(unsigned Kind) const {
149 if (Subtarget.isPPC64())
150 return &PPC::G8RCRegClass;
151 return &PPC::GPRCRegClass;
155 PPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
156 // 32-bit Darwin calling convention.
157 static const unsigned Darwin32_CalleeSavedRegs[] = {
158 PPC::R13, PPC::R14, PPC::R15,
159 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
160 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
161 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
162 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
164 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
165 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
166 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
167 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
170 PPC::CR2, PPC::CR3, PPC::CR4,
171 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
172 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
173 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
175 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
176 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
177 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
182 // 32-bit SVR4 calling convention.
183 static const unsigned SVR4_CalleeSavedRegs[] = {
185 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
186 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
187 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
188 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
190 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
191 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
192 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
193 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
196 PPC::CR2, PPC::CR3, PPC::CR4,
200 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
201 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
202 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
204 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
205 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
206 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
210 // 64-bit Darwin calling convention.
211 static const unsigned Darwin64_CalleeSavedRegs[] = {
213 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
214 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
215 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
216 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
218 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
219 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
220 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
221 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
224 PPC::CR2, PPC::CR3, PPC::CR4,
225 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
226 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
227 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
229 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
230 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
231 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
236 // 64-bit SVR4 calling convention.
237 static const unsigned SVR4_64_CalleeSavedRegs[] = {
239 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
240 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
241 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
242 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
244 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
245 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
246 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
247 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
250 PPC::CR2, PPC::CR3, PPC::CR4,
254 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
255 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
256 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
258 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
259 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
260 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
265 if (Subtarget.isDarwinABI())
266 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
267 Darwin32_CalleeSavedRegs;
269 return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegs : SVR4_CalleeSavedRegs;
272 const TargetRegisterClass* const*
273 PPCRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
274 // 32-bit Darwin calling convention.
275 static const TargetRegisterClass * const Darwin32_CalleeSavedRegClasses[] = {
276 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
277 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
278 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
279 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
280 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
282 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
283 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
284 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
285 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
286 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
288 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
290 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
291 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
292 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
294 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
295 &PPC::CRBITRCRegClass,
296 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
297 &PPC::CRBITRCRegClass,
298 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
299 &PPC::CRBITRCRegClass,
301 &PPC::GPRCRegClass, 0
304 // 32-bit SVR4 calling convention.
305 static const TargetRegisterClass * const SVR4_CalleeSavedRegClasses[] = {
306 &PPC::GPRCRegClass,&PPC::GPRCRegClass,
307 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
308 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
309 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
310 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
312 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
313 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
314 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
315 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
316 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
318 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
320 &PPC::VRSAVERCRegClass,
322 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
323 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
324 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
326 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
327 &PPC::CRBITRCRegClass,
328 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
329 &PPC::CRBITRCRegClass,
330 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
331 &PPC::CRBITRCRegClass,
336 // 64-bit Darwin calling convention.
337 static const TargetRegisterClass * const Darwin64_CalleeSavedRegClasses[] = {
338 &PPC::G8RCRegClass,&PPC::G8RCRegClass,
339 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
340 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
341 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
342 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
344 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
345 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
346 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
347 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
348 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
350 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
352 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
353 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
354 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
356 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
357 &PPC::CRBITRCRegClass,
358 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
359 &PPC::CRBITRCRegClass,
360 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
361 &PPC::CRBITRCRegClass,
363 &PPC::G8RCRegClass, 0
366 // 64-bit SVR4 calling convention.
367 static const TargetRegisterClass * const SVR4_64_CalleeSavedRegClasses[] = {
368 &PPC::G8RCRegClass,&PPC::G8RCRegClass,
369 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
370 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
371 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
372 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
374 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
375 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
376 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
377 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
378 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
380 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
382 &PPC::VRSAVERCRegClass,
384 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
385 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
386 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
388 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
389 &PPC::CRBITRCRegClass,
390 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
391 &PPC::CRBITRCRegClass,
392 &PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,&PPC::CRBITRCRegClass,
393 &PPC::CRBITRCRegClass,
398 if (Subtarget.isDarwinABI())
399 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegClasses :
400 Darwin32_CalleeSavedRegClasses;
402 return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegClasses
403 : SVR4_CalleeSavedRegClasses;
406 // needsFP - Return true if the specified function should have a dedicated frame
407 // pointer register. This is true if the function has variable sized allocas or
408 // if frame pointer elimination is disabled.
410 static bool needsFP(const MachineFunction &MF) {
411 const MachineFrameInfo *MFI = MF.getFrameInfo();
412 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects() ||
413 (GuaranteedTailCallOpt && MF.getInfo<PPCFunctionInfo>()->hasFastCall());
416 static bool spillsCR(const MachineFunction &MF) {
417 const PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
418 return FuncInfo->isCRSpilled();
421 BitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
422 BitVector Reserved(getNumRegs());
423 Reserved.set(PPC::R0);
424 Reserved.set(PPC::R1);
425 Reserved.set(PPC::LR);
426 Reserved.set(PPC::LR8);
427 Reserved.set(PPC::RM);
429 // The SVR4 ABI reserves r2 and r13
430 if (Subtarget.isSVR4ABI()) {
431 Reserved.set(PPC::R2); // System-reserved register
432 Reserved.set(PPC::R13); // Small Data Area pointer register
434 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
435 // when the stack frame is too big to address directly; we need two regs.
437 if (Subtarget.isDarwinABI()) {
438 Reserved.set(PPC::R2);
441 // On PPC64, r13 is the thread pointer. Never allocate this register.
442 // Note that this is over conservative, as it also prevents allocation of R31
443 // when the FP is not needed.
444 if (Subtarget.isPPC64()) {
445 Reserved.set(PPC::R13);
446 Reserved.set(PPC::R31);
448 if (!EnableRegisterScavenging)
449 Reserved.set(PPC::R0); // FIXME (64-bit): Remove
451 Reserved.set(PPC::X0);
452 Reserved.set(PPC::X1);
453 Reserved.set(PPC::X13);
454 Reserved.set(PPC::X31);
456 // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
457 if (Subtarget.isSVR4ABI()) {
458 Reserved.set(PPC::X2);
460 // Reserve R2 on Darwin to hack around the problem of save/restore of CR
461 // when the stack frame is too big to address directly; we need two regs.
463 if (Subtarget.isDarwinABI()) {
464 Reserved.set(PPC::X2);
469 Reserved.set(PPC::R31);
474 //===----------------------------------------------------------------------===//
475 // Stack Frame Processing methods
476 //===----------------------------------------------------------------------===//
478 // hasFP - Return true if the specified function actually has a dedicated frame
479 // pointer register. This is true if the function needs a frame pointer and has
480 // a non-zero stack size.
481 bool PPCRegisterInfo::hasFP(const MachineFunction &MF) const {
482 const MachineFrameInfo *MFI = MF.getFrameInfo();
483 return MFI->getStackSize() && needsFP(MF);
486 /// MustSaveLR - Return true if this function requires that we save the LR
487 /// register onto the stack in the prolog and restore it in the epilog of the
489 static bool MustSaveLR(const MachineFunction &MF, unsigned LR) {
490 const PPCFunctionInfo *MFI = MF.getInfo<PPCFunctionInfo>();
492 // We need a save/restore of LR if there is any def of LR (which is
493 // defined by calls, including the PIC setup sequence), or if there is
494 // some use of the LR stack slot (e.g. for builtin_return_address).
495 // (LR comes in 32 and 64 bit versions.)
496 MachineRegisterInfo::def_iterator RI = MF.getRegInfo().def_begin(LR);
497 return RI !=MF.getRegInfo().def_end() || MFI->isLRStoreRequired();
502 void PPCRegisterInfo::
503 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
504 MachineBasicBlock::iterator I) const {
505 if (GuaranteedTailCallOpt && I->getOpcode() == PPC::ADJCALLSTACKUP) {
506 // Add (actually subtract) back the amount the callee popped on return.
507 if (int CalleeAmt = I->getOperand(1).getImm()) {
508 bool is64Bit = Subtarget.isPPC64();
510 unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
511 unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
512 unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
513 unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
514 unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
515 unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
516 MachineInstr *MI = I;
517 DebugLoc dl = MI->getDebugLoc();
519 if (isInt<16>(CalleeAmt)) {
520 BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg).addReg(StackReg).
523 MachineBasicBlock::iterator MBBI = I;
524 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
525 .addImm(CalleeAmt >> 16);
526 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
527 .addReg(TmpReg, RegState::Kill)
528 .addImm(CalleeAmt & 0xFFFF);
529 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
536 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
540 /// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
541 /// register first and then a spilled callee-saved register if that fails.
543 unsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
544 const TargetRegisterClass *RC, int SPAdj) {
545 assert(RS && "Register scavenging must be on");
546 unsigned Reg = RS->FindUnusedReg(RC);
547 // FIXME: move ARM callee-saved reg scan to target independent code, then
548 // search for already spilled CS register here.
550 Reg = RS->scavengeRegister(RC, II, SPAdj);
554 /// lowerDynamicAlloc - Generate the code for allocating an object in the
555 /// current frame. The sequence of code with be in the general form
557 /// addi R0, SP, \#frameSize ; get the address of the previous frame
558 /// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
559 /// addi Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
561 void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
562 int SPAdj, RegScavenger *RS) const {
563 // Get the instruction.
564 MachineInstr &MI = *II;
565 // Get the instruction's basic block.
566 MachineBasicBlock &MBB = *MI.getParent();
567 // Get the basic block's function.
568 MachineFunction &MF = *MBB.getParent();
569 // Get the frame info.
570 MachineFrameInfo *MFI = MF.getFrameInfo();
571 // Determine whether 64-bit pointers are used.
572 bool LP64 = Subtarget.isPPC64();
573 DebugLoc dl = MI.getDebugLoc();
575 // Get the maximum call stack size.
576 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
577 // Get the total frame size.
578 unsigned FrameSize = MFI->getStackSize();
580 // Get stack alignments.
581 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
582 unsigned MaxAlign = MFI->getMaxAlignment();
583 assert(MaxAlign <= TargetAlign &&
584 "Dynamic alloca with large aligns not supported");
586 // Determine the previous frame's address. If FrameSize can't be
587 // represented as 16 bits or we need special alignment, then we load the
588 // previous frame's address from 0(SP). Why not do an addis of the hi?
589 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
590 // Constructing the constant and adding would take 3 instructions.
591 // Fortunately, a frame greater than 32K is rare.
592 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
593 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
594 const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
596 // FIXME (64-bit): Use "findScratchRegister"
598 if (EnableRegisterScavenging)
599 Reg = findScratchRegister(II, RS, RC, SPAdj);
603 if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
604 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
608 if (EnableRegisterScavenging) // FIXME (64-bit): Use "true" part.
609 BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
613 BuildMI(MBB, II, dl, TII.get(PPC::LD), PPC::X0)
617 BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
622 // Grow the stack and update the stack pointer link, then determine the
623 // address of new allocated space.
625 if (EnableRegisterScavenging) // FIXME (64-bit): Use "true" part.
626 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
627 .addReg(Reg, RegState::Kill)
629 .addReg(MI.getOperand(1).getReg());
631 BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
632 .addReg(PPC::X0, RegState::Kill)
634 .addReg(MI.getOperand(1).getReg());
636 if (!MI.getOperand(1).isKill())
637 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
639 .addImm(maxCallFrameSize);
641 // Implicitly kill the register.
642 BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
644 .addImm(maxCallFrameSize)
645 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
647 BuildMI(MBB, II, dl, TII.get(PPC::STWUX))
648 .addReg(Reg, RegState::Kill)
650 .addReg(MI.getOperand(1).getReg());
652 if (!MI.getOperand(1).isKill())
653 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
655 .addImm(maxCallFrameSize);
657 // Implicitly kill the register.
658 BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
660 .addImm(maxCallFrameSize)
661 .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
664 // Discard the DYNALLOC instruction.
668 /// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
669 /// reserving a whole register (R0), we scrounge for one here. This generates
672 /// mfcr rA ; Move the conditional register into GPR rA.
673 /// rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
674 /// stw rA, FI ; Store rA to the frame.
676 void PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
677 unsigned FrameIndex, int SPAdj,
678 RegScavenger *RS) const {
679 // Get the instruction.
680 MachineInstr &MI = *II; // ; SPILL_CR <SrcReg>, <offset>, <FI>
681 // Get the instruction's basic block.
682 MachineBasicBlock &MBB = *MI.getParent();
683 DebugLoc dl = MI.getDebugLoc();
685 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
686 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
687 const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
688 unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
690 // We need to store the CR in the low 4-bits of the saved value. First, issue
691 // an MFCR to save all of the CRBits. Add an implicit kill of the CR.
692 if (!MI.getOperand(0).isKill())
693 BuildMI(MBB, II, dl, TII.get(PPC::MFCR), Reg);
695 // Implicitly kill the CR register.
696 BuildMI(MBB, II, dl, TII.get(PPC::MFCR), Reg)
697 .addReg(MI.getOperand(0).getReg(), RegState::ImplicitKill);
699 // If the saved register wasn't CR0, shift the bits left so that they are in
701 unsigned SrcReg = MI.getOperand(0).getReg();
702 if (SrcReg != PPC::CR0)
703 // rlwinm rA, rA, ShiftBits, 0, 31.
704 BuildMI(MBB, II, dl, TII.get(PPC::RLWINM), Reg)
705 .addReg(Reg, RegState::Kill)
706 .addImm(PPCRegisterInfo::getRegisterNumbering(SrcReg) * 4)
710 addFrameReference(BuildMI(MBB, II, dl, TII.get(PPC::STW))
711 .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())),
714 // Discard the pseudo instruction.
719 PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
720 int SPAdj, FrameIndexValue *Value,
721 RegScavenger *RS) const {
722 assert(SPAdj == 0 && "Unexpected");
724 // Get the instruction.
725 MachineInstr &MI = *II;
726 // Get the instruction's basic block.
727 MachineBasicBlock &MBB = *MI.getParent();
728 // Get the basic block's function.
729 MachineFunction &MF = *MBB.getParent();
730 // Get the frame info.
731 MachineFrameInfo *MFI = MF.getFrameInfo();
732 DebugLoc dl = MI.getDebugLoc();
734 // Find out which operand is the frame index.
735 unsigned FIOperandNo = 0;
736 while (!MI.getOperand(FIOperandNo).isFI()) {
738 assert(FIOperandNo != MI.getNumOperands() &&
739 "Instr doesn't have FrameIndex operand!");
741 // Take into account whether it's an add or mem instruction
742 unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
743 if (MI.isInlineAsm())
744 OffsetOperandNo = FIOperandNo-1;
746 // Get the frame index.
747 int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
749 // Get the frame pointer save index. Users of this index are primarily
750 // DYNALLOC instructions.
751 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
752 int FPSI = FI->getFramePointerSaveIndex();
753 // Get the instruction opcode.
754 unsigned OpC = MI.getOpcode();
756 // Special case for dynamic alloca.
757 if (FPSI && FrameIndex == FPSI &&
758 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
759 lowerDynamicAlloc(II, SPAdj, RS);
763 // Special case for pseudo-op SPILL_CR.
764 if (EnableRegisterScavenging) // FIXME (64-bit): Enable by default.
765 if (OpC == PPC::SPILL_CR) {
766 lowerCRSpilling(II, FrameIndex, SPAdj, RS);
770 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
771 MI.getOperand(FIOperandNo).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1,
774 // Figure out if the offset in the instruction is shifted right two bits. This
775 // is true for instructions like "STD", which the machine implicitly adds two
777 bool isIXAddr = false;
787 // Now add the frame object offset to the offset from r1.
788 int Offset = MFI->getObjectOffset(FrameIndex);
790 Offset += MI.getOperand(OffsetOperandNo).getImm();
792 Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
794 // If we're not using a Frame Pointer that has been set to the value of the
795 // SP before having the stack size subtracted from it, then add the stack size
796 // to Offset to get the correct offset.
797 Offset += MFI->getStackSize();
799 // If we can, encode the offset directly into the instruction. If this is a
800 // normal PPC "ri" instruction, any 16-bit value can be safely encoded. If
801 // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
802 // clear can be encoded. This is extremely uncommon, because normally you
803 // only "std" to a stack slot that is at least 4-byte aligned, but it can
804 // happen in invalid code.
805 if (isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
807 Offset >>= 2; // The actual encoded value has the low two bits zero.
808 MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
812 // The offset doesn't fit into a single register, scavenge one to build the
814 // FIXME: figure out what SPAdj is doing here.
816 // FIXME (64-bit): Use "findScratchRegister".
818 if (EnableRegisterScavenging)
819 SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
823 // Insert a set of rA with the full offset value before the ld, st, or add
824 BuildMI(MBB, II, dl, TII.get(PPC::LIS), SReg)
825 .addImm(Offset >> 16);
826 BuildMI(MBB, II, dl, TII.get(PPC::ORI), SReg)
827 .addReg(SReg, RegState::Kill)
830 // Convert into indexed form of the instruction:
832 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
833 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
834 unsigned OperandBase;
836 if (OpC != TargetOpcode::INLINEASM) {
837 assert(ImmToIdxMap.count(OpC) &&
838 "No indexed form of load or store available!");
839 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
840 MI.setDesc(TII.get(NewOpcode));
843 OperandBase = OffsetOperandNo;
846 unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
847 MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
848 MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
852 /// VRRegNo - Map from a numbered VR register to its enum value.
854 static const unsigned short VRRegNo[] = {
855 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
856 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
857 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
858 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
861 /// RemoveVRSaveCode - We have found that this function does not need any code
862 /// to manipulate the VRSAVE register, even though it uses vector registers.
863 /// This can happen when the only registers used are known to be live in or out
864 /// of the function. Remove all of the VRSAVE related code from the function.
865 static void RemoveVRSaveCode(MachineInstr *MI) {
866 MachineBasicBlock *Entry = MI->getParent();
867 MachineFunction *MF = Entry->getParent();
869 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
870 MachineBasicBlock::iterator MBBI = MI;
872 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
873 MBBI->eraseFromParent();
875 bool RemovedAllMTVRSAVEs = true;
876 // See if we can find and remove the MTVRSAVE instruction from all of the
878 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
879 // If last instruction is a return instruction, add an epilogue
880 if (!I->empty() && I->back().getDesc().isReturn()) {
881 bool FoundIt = false;
882 for (MBBI = I->end(); MBBI != I->begin(); ) {
884 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
885 MBBI->eraseFromParent(); // remove it.
890 RemovedAllMTVRSAVEs &= FoundIt;
894 // If we found and removed all MTVRSAVE instructions, remove the read of
896 if (RemovedAllMTVRSAVEs) {
898 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
900 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
901 MBBI->eraseFromParent();
904 // Finally, nuke the UPDATE_VRSAVE.
905 MI->eraseFromParent();
908 // HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
909 // instruction selector. Based on the vector registers that have been used,
910 // transform this into the appropriate ORI instruction.
911 static void HandleVRSaveUpdate(MachineInstr *MI, const TargetInstrInfo &TII) {
912 MachineFunction *MF = MI->getParent()->getParent();
913 DebugLoc dl = MI->getDebugLoc();
915 unsigned UsedRegMask = 0;
916 for (unsigned i = 0; i != 32; ++i)
917 if (MF->getRegInfo().isPhysRegUsed(VRRegNo[i]))
918 UsedRegMask |= 1 << (31-i);
920 // Live in and live out values already must be in the mask, so don't bother
922 for (MachineRegisterInfo::livein_iterator
923 I = MF->getRegInfo().livein_begin(),
924 E = MF->getRegInfo().livein_end(); I != E; ++I) {
925 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
926 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
927 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
929 for (MachineRegisterInfo::liveout_iterator
930 I = MF->getRegInfo().liveout_begin(),
931 E = MF->getRegInfo().liveout_end(); I != E; ++I) {
932 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
933 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
934 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
937 // If no registers are used, turn this into a copy.
938 if (UsedRegMask == 0) {
939 // Remove all VRSAVE code.
940 RemoveVRSaveCode(MI);
944 unsigned SrcReg = MI->getOperand(1).getReg();
945 unsigned DstReg = MI->getOperand(0).getReg();
947 if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
948 if (DstReg != SrcReg)
949 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
951 .addImm(UsedRegMask);
953 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
954 .addReg(SrcReg, RegState::Kill)
955 .addImm(UsedRegMask);
956 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
957 if (DstReg != SrcReg)
958 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
960 .addImm(UsedRegMask >> 16);
962 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
963 .addReg(SrcReg, RegState::Kill)
964 .addImm(UsedRegMask >> 16);
966 if (DstReg != SrcReg)
967 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
969 .addImm(UsedRegMask >> 16);
971 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORIS), DstReg)
972 .addReg(SrcReg, RegState::Kill)
973 .addImm(UsedRegMask >> 16);
975 BuildMI(*MI->getParent(), MI, dl, TII.get(PPC::ORI), DstReg)
976 .addReg(DstReg, RegState::Kill)
977 .addImm(UsedRegMask & 0xFFFF);
980 // Remove the old UPDATE_VRSAVE instruction.
981 MI->eraseFromParent();
984 /// determineFrameLayout - Determine the size of the frame and maximum call
986 void PPCRegisterInfo::determineFrameLayout(MachineFunction &MF) const {
987 MachineFrameInfo *MFI = MF.getFrameInfo();
989 // Get the number of bytes to allocate from the FrameInfo
990 unsigned FrameSize = MFI->getStackSize();
992 // Get the alignments provided by the target, and the maximum alignment
993 // (if any) of the fixed frame objects.
994 unsigned MaxAlign = MFI->getMaxAlignment();
995 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
996 unsigned AlignMask = TargetAlign - 1; //
998 // If we are a leaf function, and use up to 224 bytes of stack space,
999 // don't have a frame pointer, calls, or dynamic alloca then we do not need
1000 // to adjust the stack pointer (we fit in the Red Zone).
1001 bool DisableRedZone = MF.getFunction()->hasFnAttr(Attribute::NoRedZone);
1002 // FIXME SVR4 The 32-bit SVR4 ABI has no red zone.
1003 if (!DisableRedZone &&
1004 FrameSize <= 224 && // Fits in red zone.
1005 !MFI->hasVarSizedObjects() && // No dynamic alloca.
1006 !MFI->hasCalls() && // No calls.
1007 (!ALIGN_STACK || MaxAlign <= TargetAlign)) { // No special alignment.
1008 // No need for frame
1009 MFI->setStackSize(0);
1013 // Get the maximum call frame size of all the calls.
1014 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
1016 // Maximum call frame needs to be at least big enough for linkage and 8 args.
1017 unsigned minCallFrameSize =
1018 PPCFrameInfo::getMinCallFrameSize(Subtarget.isPPC64(),
1019 Subtarget.isDarwinABI());
1020 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
1022 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
1023 // that allocations will be aligned.
1024 if (MFI->hasVarSizedObjects())
1025 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
1027 // Update maximum call frame size.
1028 MFI->setMaxCallFrameSize(maxCallFrameSize);
1030 // Include call frame size in total.
1031 FrameSize += maxCallFrameSize;
1033 // Make sure the frame is aligned.
1034 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
1036 // Update frame info.
1037 MFI->setStackSize(FrameSize);
1041 PPCRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
1042 RegScavenger *RS) const {
1043 // Save and clear the LR state.
1044 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1045 unsigned LR = getRARegister();
1046 FI->setMustSaveLR(MustSaveLR(MF, LR));
1047 MF.getRegInfo().setPhysRegUnused(LR);
1049 // Save R31 if necessary
1050 int FPSI = FI->getFramePointerSaveIndex();
1051 bool isPPC64 = Subtarget.isPPC64();
1052 bool isDarwinABI = Subtarget.isDarwinABI();
1053 MachineFrameInfo *MFI = MF.getFrameInfo();
1055 // If the frame pointer save index hasn't been defined yet.
1056 if (!FPSI && needsFP(MF)) {
1057 // Find out what the fix offset of the frame pointer save area.
1058 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64,
1060 // Allocate the frame index for frame pointer save area.
1061 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset,
1064 FI->setFramePointerSaveIndex(FPSI);
1067 // Reserve stack space to move the linkage area to in case of a tail call.
1069 if (GuaranteedTailCallOpt && (TCSPDelta = FI->getTailCallSPDelta()) < 0) {
1070 MF.getFrameInfo()->CreateFixedObject(-1 * TCSPDelta, TCSPDelta,
1074 // Reserve a slot closest to SP or frame pointer if we have a dynalloc or
1075 // a large stack, which will require scavenging a register to materialize a
1077 // FIXME: this doesn't actually check stack size, so is a bit pessimistic
1078 // FIXME: doesn't detect whether or not we need to spill vXX, which requires
1081 if (EnableRegisterScavenging) // FIXME (64-bit): Enable.
1082 if (needsFP(MF) || spillsCR(MF)) {
1083 const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
1084 const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
1085 const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC;
1086 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1093 PPCRegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF)
1095 // Early exit if not using the SVR4 ABI.
1096 if (!Subtarget.isSVR4ABI()) {
1100 // Get callee saved register information.
1101 MachineFrameInfo *FFI = MF.getFrameInfo();
1102 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
1104 // Early exit if no callee saved registers are modified!
1105 if (CSI.empty() && !needsFP(MF)) {
1109 unsigned MinGPR = PPC::R31;
1110 unsigned MinG8R = PPC::X31;
1111 unsigned MinFPR = PPC::F31;
1112 unsigned MinVR = PPC::V31;
1114 bool HasGPSaveArea = false;
1115 bool HasG8SaveArea = false;
1116 bool HasFPSaveArea = false;
1117 bool HasCRSaveArea = false;
1118 bool HasVRSAVESaveArea = false;
1119 bool HasVRSaveArea = false;
1121 SmallVector<CalleeSavedInfo, 18> GPRegs;
1122 SmallVector<CalleeSavedInfo, 18> G8Regs;
1123 SmallVector<CalleeSavedInfo, 18> FPRegs;
1124 SmallVector<CalleeSavedInfo, 18> VRegs;
1126 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1127 unsigned Reg = CSI[i].getReg();
1128 const TargetRegisterClass *RC = CSI[i].getRegClass();
1130 if (RC == PPC::GPRCRegisterClass) {
1131 HasGPSaveArea = true;
1133 GPRegs.push_back(CSI[i]);
1138 } else if (RC == PPC::G8RCRegisterClass) {
1139 HasG8SaveArea = true;
1141 G8Regs.push_back(CSI[i]);
1146 } else if (RC == PPC::F8RCRegisterClass) {
1147 HasFPSaveArea = true;
1149 FPRegs.push_back(CSI[i]);
1154 // FIXME SVR4: Disable CR save area for now.
1155 } else if ( RC == PPC::CRBITRCRegisterClass
1156 || RC == PPC::CRRCRegisterClass) {
1157 // HasCRSaveArea = true;
1158 } else if (RC == PPC::VRSAVERCRegisterClass) {
1159 HasVRSAVESaveArea = true;
1160 } else if (RC == PPC::VRRCRegisterClass) {
1161 HasVRSaveArea = true;
1163 VRegs.push_back(CSI[i]);
1169 llvm_unreachable("Unknown RegisterClass!");
1173 PPCFunctionInfo *PFI = MF.getInfo<PPCFunctionInfo>();
1175 int64_t LowerBound = 0;
1177 // Take into account stack space reserved for tail calls.
1179 if (GuaranteedTailCallOpt && (TCSPDelta = PFI->getTailCallSPDelta()) < 0) {
1180 LowerBound = TCSPDelta;
1183 // The Floating-point register save area is right below the back chain word
1184 // of the previous stack frame.
1185 if (HasFPSaveArea) {
1186 for (unsigned i = 0, e = FPRegs.size(); i != e; ++i) {
1187 int FI = FPRegs[i].getFrameIdx();
1189 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1192 LowerBound -= (31 - getRegisterNumbering(MinFPR) + 1) * 8;
1195 // Check whether the frame pointer register is allocated. If so, make sure it
1196 // is spilled to the correct offset.
1198 HasGPSaveArea = true;
1200 int FI = PFI->getFramePointerSaveIndex();
1201 assert(FI && "No Frame Pointer Save Slot!");
1203 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1206 // General register save area starts right below the Floating-point
1207 // register save area.
1208 if (HasGPSaveArea || HasG8SaveArea) {
1209 // Move general register save area spill slots down, taking into account
1210 // the size of the Floating-point register save area.
1211 for (unsigned i = 0, e = GPRegs.size(); i != e; ++i) {
1212 int FI = GPRegs[i].getFrameIdx();
1214 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1217 // Move general register save area spill slots down, taking into account
1218 // the size of the Floating-point register save area.
1219 for (unsigned i = 0, e = G8Regs.size(); i != e; ++i) {
1220 int FI = G8Regs[i].getFrameIdx();
1222 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1225 unsigned MinReg = std::min<unsigned>(getRegisterNumbering(MinGPR),
1226 getRegisterNumbering(MinG8R));
1228 if (Subtarget.isPPC64()) {
1229 LowerBound -= (31 - MinReg + 1) * 8;
1231 LowerBound -= (31 - MinReg + 1) * 4;
1235 // The CR save area is below the general register save area.
1236 if (HasCRSaveArea) {
1237 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1238 // which have the CR/CRBIT register class?
1239 // Adjust the frame index of the CR spill slot.
1240 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1241 const TargetRegisterClass *RC = CSI[i].getRegClass();
1243 if (RC == PPC::CRBITRCRegisterClass || RC == PPC::CRRCRegisterClass) {
1244 int FI = CSI[i].getFrameIdx();
1246 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1250 LowerBound -= 4; // The CR save area is always 4 bytes long.
1253 if (HasVRSAVESaveArea) {
1254 // FIXME SVR4: Is it actually possible to have multiple elements in CSI
1255 // which have the VRSAVE register class?
1256 // Adjust the frame index of the VRSAVE spill slot.
1257 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1258 const TargetRegisterClass *RC = CSI[i].getRegClass();
1260 if (RC == PPC::VRSAVERCRegisterClass) {
1261 int FI = CSI[i].getFrameIdx();
1263 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1267 LowerBound -= 4; // The VRSAVE save area is always 4 bytes long.
1270 if (HasVRSaveArea) {
1271 // Insert alignment padding, we need 16-byte alignment.
1272 LowerBound = (LowerBound - 15) & ~(15);
1274 for (unsigned i = 0, e = VRegs.size(); i != e; ++i) {
1275 int FI = VRegs[i].getFrameIdx();
1277 FFI->setObjectOffset(FI, LowerBound + FFI->getObjectOffset(FI));
1283 PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
1284 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
1285 MachineBasicBlock::iterator MBBI = MBB.begin();
1286 MachineFrameInfo *MFI = MF.getFrameInfo();
1287 MachineModuleInfo &MMI = MF.getMMI();
1289 bool needsFrameMoves = MMI.hasDebugInfo() ||
1290 !MF.getFunction()->doesNotThrow() ||
1291 UnwindTablesMandatory;
1293 // Prepare for frame info.
1294 MCSymbol *FrameLabel = 0;
1296 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
1298 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
1299 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
1300 HandleVRSaveUpdate(MBBI, TII);
1305 // Move MBBI back to the beginning of the function.
1308 // Work out frame sizes.
1309 determineFrameLayout(MF);
1310 unsigned FrameSize = MFI->getStackSize();
1312 int NegFrameSize = -FrameSize;
1314 // Get processor type.
1315 bool isPPC64 = Subtarget.isPPC64();
1316 // Get operating system
1317 bool isDarwinABI = Subtarget.isDarwinABI();
1318 // Check if the link register (LR) must be saved.
1319 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1320 bool MustSaveLR = FI->mustSaveLR();
1321 // Do we have a frame pointer for this function?
1322 bool HasFP = hasFP(MF) && FrameSize;
1324 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
1328 if (Subtarget.isSVR4ABI()) {
1329 MachineFrameInfo *FFI = MF.getFrameInfo();
1330 int FPIndex = FI->getFramePointerSaveIndex();
1331 assert(FPIndex && "No Frame Pointer Save Slot!");
1332 FPOffset = FFI->getObjectOffset(FPIndex);
1334 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
1340 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR8), PPC::X0);
1343 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
1349 BuildMI(MBB, MBBI, dl, TII.get(PPC::STD))
1351 .addImm(LROffset / 4)
1355 BuildMI(MBB, MBBI, dl, TII.get(PPC::MFLR), PPC::R0);
1358 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
1364 BuildMI(MBB, MBBI, dl, TII.get(PPC::STW))
1370 // Skip if a leaf routine.
1371 if (!FrameSize) return;
1373 // Get stack alignments.
1374 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1375 unsigned MaxAlign = MFI->getMaxAlignment();
1377 // Adjust stack pointer: r1 += NegFrameSize.
1378 // If there is a preferred stack alignment, align R1 now
1381 if (ALIGN_STACK && MaxAlign > TargetAlign) {
1382 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
1383 "Invalid alignment!");
1384 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
1386 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLWINM), PPC::R0)
1389 .addImm(32 - Log2_32(MaxAlign))
1391 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC) ,PPC::R0)
1392 .addReg(PPC::R0, RegState::Kill)
1393 .addImm(NegFrameSize);
1394 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
1398 } else if (isInt<16>(NegFrameSize)) {
1399 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWU), PPC::R1)
1401 .addImm(NegFrameSize)
1404 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
1405 .addImm(NegFrameSize >> 16);
1406 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
1407 .addReg(PPC::R0, RegState::Kill)
1408 .addImm(NegFrameSize & 0xFFFF);
1409 BuildMI(MBB, MBBI, dl, TII.get(PPC::STWUX))
1415 if (ALIGN_STACK && MaxAlign > TargetAlign) {
1416 assert(isPowerOf2_32(MaxAlign) && isInt<16>(MaxAlign) &&
1417 "Invalid alignment!");
1418 assert(isInt<16>(NegFrameSize) && "Unhandled stack size and alignment!");
1420 BuildMI(MBB, MBBI, dl, TII.get(PPC::RLDICL), PPC::X0)
1423 .addImm(64 - Log2_32(MaxAlign));
1424 BuildMI(MBB, MBBI, dl, TII.get(PPC::SUBFIC8), PPC::X0)
1426 .addImm(NegFrameSize);
1427 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
1431 } else if (isInt<16>(NegFrameSize)) {
1432 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDU), PPC::X1)
1434 .addImm(NegFrameSize / 4)
1437 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
1438 .addImm(NegFrameSize >> 16);
1439 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
1440 .addReg(PPC::X0, RegState::Kill)
1441 .addImm(NegFrameSize & 0xFFFF);
1442 BuildMI(MBB, MBBI, dl, TII.get(PPC::STDUX))
1449 std::vector<MachineMove> &Moves = MMI.getFrameMoves();
1451 // Add the "machine moves" for the instructions we generated above, but in
1453 if (needsFrameMoves) {
1454 // Mark effective beginning of when frame pointer becomes valid.
1455 FrameLabel = MMI.getContext().CreateTempSymbol();
1456 BuildMI(MBB, MBBI, dl, TII.get(PPC::DBG_LABEL)).addSym(FrameLabel);
1458 // Show update of SP.
1460 MachineLocation SPDst(MachineLocation::VirtualFP);
1461 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
1462 Moves.push_back(MachineMove(FrameLabel, SPDst, SPSrc));
1464 MachineLocation SP(isPPC64 ? PPC::X31 : PPC::R31);
1465 Moves.push_back(MachineMove(FrameLabel, SP, SP));
1469 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
1470 MachineLocation FPSrc(isPPC64 ? PPC::X31 : PPC::R31);
1471 Moves.push_back(MachineMove(FrameLabel, FPDst, FPSrc));
1475 MachineLocation LRDst(MachineLocation::VirtualFP, LROffset);
1476 MachineLocation LRSrc(isPPC64 ? PPC::LR8 : PPC::LR);
1477 Moves.push_back(MachineMove(FrameLabel, LRDst, LRSrc));
1481 MCSymbol *ReadyLabel = 0;
1483 // If there is a frame pointer, copy R1 into R31
1486 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR), PPC::R31)
1490 BuildMI(MBB, MBBI, dl, TII.get(PPC::OR8), PPC::X31)
1495 if (needsFrameMoves) {
1496 ReadyLabel = MMI.getContext().CreateTempSymbol();
1498 // Mark effective beginning of when frame pointer is ready.
1499 BuildMI(MBB, MBBI, dl, TII.get(PPC::DBG_LABEL)).addSym(ReadyLabel);
1501 MachineLocation FPDst(HasFP ? (isPPC64 ? PPC::X31 : PPC::R31) :
1502 (isPPC64 ? PPC::X1 : PPC::R1));
1503 MachineLocation FPSrc(MachineLocation::VirtualFP);
1504 Moves.push_back(MachineMove(ReadyLabel, FPDst, FPSrc));
1508 if (needsFrameMoves) {
1509 MCSymbol *Label = HasFP ? ReadyLabel : FrameLabel;
1511 // Add callee saved registers to move list.
1512 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1513 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
1514 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
1515 unsigned Reg = CSI[I].getReg();
1516 if (Reg == PPC::LR || Reg == PPC::LR8 || Reg == PPC::RM) continue;
1517 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
1518 MachineLocation CSSrc(Reg);
1519 Moves.push_back(MachineMove(Label, CSDst, CSSrc));
1524 void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
1525 MachineBasicBlock &MBB) const {
1526 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1527 unsigned RetOpcode = MBBI->getOpcode();
1530 assert( (RetOpcode == PPC::BLR ||
1531 RetOpcode == PPC::TCRETURNri ||
1532 RetOpcode == PPC::TCRETURNdi ||
1533 RetOpcode == PPC::TCRETURNai ||
1534 RetOpcode == PPC::TCRETURNri8 ||
1535 RetOpcode == PPC::TCRETURNdi8 ||
1536 RetOpcode == PPC::TCRETURNai8) &&
1537 "Can only insert epilog into returning blocks");
1539 // Get alignment info so we know how to restore r1
1540 const MachineFrameInfo *MFI = MF.getFrameInfo();
1541 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1542 unsigned MaxAlign = MFI->getMaxAlignment();
1544 // Get the number of bytes allocated from the FrameInfo.
1545 int FrameSize = MFI->getStackSize();
1547 // Get processor type.
1548 bool isPPC64 = Subtarget.isPPC64();
1549 // Get operating system
1550 bool isDarwinABI = Subtarget.isDarwinABI();
1551 // Check if the link register (LR) has been saved.
1552 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1553 bool MustSaveLR = FI->mustSaveLR();
1554 // Do we have a frame pointer for this function?
1555 bool HasFP = hasFP(MF) && FrameSize;
1557 int LROffset = PPCFrameInfo::getReturnSaveOffset(isPPC64, isDarwinABI);
1561 if (Subtarget.isSVR4ABI()) {
1562 MachineFrameInfo *FFI = MF.getFrameInfo();
1563 int FPIndex = FI->getFramePointerSaveIndex();
1564 assert(FPIndex && "No Frame Pointer Save Slot!");
1565 FPOffset = FFI->getObjectOffset(FPIndex);
1567 FPOffset = PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
1571 bool UsesTCRet = RetOpcode == PPC::TCRETURNri ||
1572 RetOpcode == PPC::TCRETURNdi ||
1573 RetOpcode == PPC::TCRETURNai ||
1574 RetOpcode == PPC::TCRETURNri8 ||
1575 RetOpcode == PPC::TCRETURNdi8 ||
1576 RetOpcode == PPC::TCRETURNai8;
1579 int MaxTCRetDelta = FI->getTailCallSPDelta();
1580 MachineOperand &StackAdjust = MBBI->getOperand(1);
1581 assert(StackAdjust.isImm() && "Expecting immediate value.");
1582 // Adjust stack pointer.
1583 int StackAdj = StackAdjust.getImm();
1584 int Delta = StackAdj - MaxTCRetDelta;
1585 assert((Delta >= 0) && "Delta must be positive");
1586 if (MaxTCRetDelta>0)
1587 FrameSize += (StackAdj +Delta);
1589 FrameSize += StackAdj;
1593 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
1594 // on entry to the function. Add this offset back now.
1596 // If this function contained a fastcc call and GuaranteedTailCallOpt is
1597 // enabled (=> hasFastCall()==true) the fastcc call might contain a tail
1598 // call which invalidates the stack pointer value in SP(0). So we use the
1599 // value of R31 in this case.
1600 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
1601 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
1602 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
1603 .addReg(PPC::R31).addImm(FrameSize);
1604 } else if(FI->hasFastCall()) {
1605 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS), PPC::R0)
1606 .addImm(FrameSize >> 16);
1607 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI), PPC::R0)
1608 .addReg(PPC::R0, RegState::Kill)
1609 .addImm(FrameSize & 0xFFFF);
1610 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD4))
1614 } else if (isInt<16>(FrameSize) &&
1615 (!ALIGN_STACK || TargetAlign >= MaxAlign) &&
1616 !MFI->hasVarSizedObjects()) {
1617 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI), PPC::R1)
1618 .addReg(PPC::R1).addImm(FrameSize);
1620 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ),PPC::R1)
1621 .addImm(0).addReg(PPC::R1);
1624 if (FI->hasFastCall() && isInt<16>(FrameSize)) {
1625 assert(hasFP(MF) && "Expecting a valid the frame pointer.");
1626 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
1627 .addReg(PPC::X31).addImm(FrameSize);
1628 } else if(FI->hasFastCall()) {
1629 BuildMI(MBB, MBBI, dl, TII.get(PPC::LIS8), PPC::X0)
1630 .addImm(FrameSize >> 16);
1631 BuildMI(MBB, MBBI, dl, TII.get(PPC::ORI8), PPC::X0)
1632 .addReg(PPC::X0, RegState::Kill)
1633 .addImm(FrameSize & 0xFFFF);
1634 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADD8))
1638 } else if (isInt<16>(FrameSize) && TargetAlign >= MaxAlign &&
1639 !MFI->hasVarSizedObjects()) {
1640 BuildMI(MBB, MBBI, dl, TII.get(PPC::ADDI8), PPC::X1)
1641 .addReg(PPC::X1).addImm(FrameSize);
1643 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X1)
1644 .addImm(0).addReg(PPC::X1);
1651 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X0)
1652 .addImm(LROffset/4).addReg(PPC::X1);
1655 BuildMI(MBB, MBBI, dl, TII.get(PPC::LD), PPC::X31)
1656 .addImm(FPOffset/4).addReg(PPC::X1);
1659 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR8)).addReg(PPC::X0);
1662 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R0)
1663 .addImm(LROffset).addReg(PPC::R1);
1666 BuildMI(MBB, MBBI, dl, TII.get(PPC::LWZ), PPC::R31)
1667 .addImm(FPOffset).addReg(PPC::R1);
1670 BuildMI(MBB, MBBI, dl, TII.get(PPC::MTLR)).addReg(PPC::R0);
1673 // Callee pop calling convention. Pop parameter/linkage area. Used for tail
1674 // call optimization
1675 if (GuaranteedTailCallOpt && RetOpcode == PPC::BLR &&
1676 MF.getFunction()->getCallingConv() == CallingConv::Fast) {
1677 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1678 unsigned CallerAllocatedAmt = FI->getMinReservedArea();
1679 unsigned StackReg = isPPC64 ? PPC::X1 : PPC::R1;
1680 unsigned FPReg = isPPC64 ? PPC::X31 : PPC::R31;
1681 unsigned TmpReg = isPPC64 ? PPC::X0 : PPC::R0;
1682 unsigned ADDIInstr = isPPC64 ? PPC::ADDI8 : PPC::ADDI;
1683 unsigned ADDInstr = isPPC64 ? PPC::ADD8 : PPC::ADD4;
1684 unsigned LISInstr = isPPC64 ? PPC::LIS8 : PPC::LIS;
1685 unsigned ORIInstr = isPPC64 ? PPC::ORI8 : PPC::ORI;
1687 if (CallerAllocatedAmt && isInt<16>(CallerAllocatedAmt)) {
1688 BuildMI(MBB, MBBI, dl, TII.get(ADDIInstr), StackReg)
1689 .addReg(StackReg).addImm(CallerAllocatedAmt);
1691 BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
1692 .addImm(CallerAllocatedAmt >> 16);
1693 BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
1694 .addReg(TmpReg, RegState::Kill)
1695 .addImm(CallerAllocatedAmt & 0xFFFF);
1696 BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
1701 } else if (RetOpcode == PPC::TCRETURNdi) {
1702 MBBI = prior(MBB.end());
1703 MachineOperand &JumpTarget = MBBI->getOperand(0);
1704 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB)).
1705 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1706 } else if (RetOpcode == PPC::TCRETURNri) {
1707 MBBI = prior(MBB.end());
1708 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1709 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR));
1710 } else if (RetOpcode == PPC::TCRETURNai) {
1711 MBBI = prior(MBB.end());
1712 MachineOperand &JumpTarget = MBBI->getOperand(0);
1713 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA)).addImm(JumpTarget.getImm());
1714 } else if (RetOpcode == PPC::TCRETURNdi8) {
1715 MBBI = prior(MBB.end());
1716 MachineOperand &JumpTarget = MBBI->getOperand(0);
1717 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILB8)).
1718 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1719 } else if (RetOpcode == PPC::TCRETURNri8) {
1720 MBBI = prior(MBB.end());
1721 assert(MBBI->getOperand(0).isReg() && "Expecting register operand.");
1722 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBCTR8));
1723 } else if (RetOpcode == PPC::TCRETURNai8) {
1724 MBBI = prior(MBB.end());
1725 MachineOperand &JumpTarget = MBBI->getOperand(0);
1726 BuildMI(MBB, MBBI, dl, TII.get(PPC::TAILBA8)).addImm(JumpTarget.getImm());
1730 unsigned PPCRegisterInfo::getRARegister() const {
1731 return !Subtarget.isPPC64() ? PPC::LR : PPC::LR8;
1734 unsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
1735 if (!Subtarget.isPPC64())
1736 return hasFP(MF) ? PPC::R31 : PPC::R1;
1738 return hasFP(MF) ? PPC::X31 : PPC::X1;
1741 void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
1743 // Initial state of the frame pointer is R1.
1744 MachineLocation Dst(MachineLocation::VirtualFP);
1745 MachineLocation Src(PPC::R1, 0);
1746 Moves.push_back(MachineMove(0, Dst, Src));
1749 unsigned PPCRegisterInfo::getEHExceptionRegister() const {
1750 return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
1753 unsigned PPCRegisterInfo::getEHHandlerRegister() const {
1754 return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;
1757 int PPCRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
1758 // FIXME: Most probably dwarf numbers differs for Linux and Darwin
1759 return PPCGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
1762 #include "PPCGenRegisterInfo.inc"