blob: a6c52bf398fd7b68459e37867fe115423e181476 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===- PPCRegisterInfo.cpp - PowerPC Register Information -------*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file contains the PowerPC implementation of the MRegisterInfo class.
Misha Brukmanf2ccb772004-08-17 04:55:41 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "reginfo"
Chris Lattner26689592005-10-14 23:51:18 +000015#include "PPC.h"
Chris Lattner26bd0d42005-10-14 23:45:43 +000016#include "PPCInstrBuilder.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000017#include "PPCRegisterInfo.h"
Chris Lattner804e0672006-07-11 00:48:23 +000018#include "PPCSubtarget.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000019#include "llvm/Constants.h"
20#include "llvm/Type.h"
21#include "llvm/CodeGen/ValueTypes.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Jim Laskey41886992006-04-07 16:34:46 +000023#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyf1d78e82006-03-23 18:12:57 +000026#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey41886992006-04-07 16:34:46 +000027#include "llvm/CodeGen/SelectionDAGNodes.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000028#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf9568d82006-04-17 21:48:13 +000029#include "llvm/Target/TargetInstrInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000030#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Nate Begemanae232e72005-11-06 09:00:38 +000034#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/ADT/STLExtras.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000036#include <cstdlib>
37#include <iostream>
38using namespace llvm;
39
Chris Lattner369503f2006-04-17 21:07:20 +000040/// getRegisterNumbering - Given the enum value for some register, e.g.
41/// PPC::F14, return the number that it corresponds to (e.g. 14).
42unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000043 using namespace PPC;
Chris Lattner369503f2006-04-17 21:07:20 +000044 switch (RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000045 case R0 : case X0 : case F0 : case V0 : case CR0: return 0;
46 case R1 : case X1 : case F1 : case V1 : case CR1: return 1;
47 case R2 : case X2 : case F2 : case V2 : case CR2: return 2;
48 case R3 : case X3 : case F3 : case V3 : case CR3: return 3;
49 case R4 : case X4 : case F4 : case V4 : case CR4: return 4;
50 case R5 : case X5 : case F5 : case V5 : case CR5: return 5;
51 case R6 : case X6 : case F6 : case V6 : case CR6: return 6;
52 case R7 : case X7 : case F7 : case V7 : case CR7: return 7;
53 case R8 : case X8 : case F8 : case V8 : return 8;
54 case R9 : case X9 : case F9 : case V9 : return 9;
55 case R10: case X10: case F10: case V10: return 10;
56 case R11: case X11: case F11: case V11: return 11;
57 case R12: case X12: case F12: case V12: return 12;
58 case R13: case X13: case F13: case V13: return 13;
59 case R14: case X14: case F14: case V14: return 14;
60 case R15: case X15: case F15: case V15: return 15;
61 case R16: case X16: case F16: case V16: return 16;
62 case R17: case X17: case F17: case V17: return 17;
63 case R18: case X18: case F18: case V18: return 18;
64 case R19: case X19: case F19: case V19: return 19;
65 case R20: case X20: case F20: case V20: return 20;
66 case R21: case X21: case F21: case V21: return 21;
67 case R22: case X22: case F22: case V22: return 22;
68 case R23: case X23: case F23: case V23: return 23;
69 case R24: case X24: case F24: case V24: return 24;
70 case R25: case X25: case F25: case V25: return 25;
71 case R26: case X26: case F26: case V26: return 26;
72 case R27: case X27: case F27: case V27: return 27;
73 case R28: case X28: case F28: case V28: return 28;
74 case R29: case X29: case F29: case V29: return 29;
75 case R30: case X30: case F30: case V30: return 30;
76 case R31: case X31: case F31: case V31: return 31;
77 default:
78 std::cerr << "Unhandled reg in PPCRegisterInfo::getRegisterNumbering!\n";
79 abort();
Chris Lattner369503f2006-04-17 21:07:20 +000080 }
81}
82
Chris Lattner804e0672006-07-11 00:48:23 +000083PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST)
84 : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
85 Subtarget(ST) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +000086 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000087 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
88 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
89 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
90 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
91 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
92 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
Nate Begeman1d9d7422005-10-18 00:28:58 +000093 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000094}
95
Misha Brukmanb5f662f2005-04-21 23:30:14 +000096void
Nate Begeman21e463b2005-10-16 05:39:50 +000097PPCRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
98 MachineBasicBlock::iterator MI,
99 unsigned SrcReg, int FrameIdx,
100 const TargetRegisterClass *RC) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000101 if (SrcReg == PPC::LR) {
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000102 // FIXME: this spills LR immediately to memory in one step. To do this, we
103 // use R11, which we know cannot be used in the prolog/epilog. This is a
104 // hack.
Chris Lattner3f852b42005-08-18 23:24:50 +0000105 BuildMI(MBB, MI, PPC::MFLR, 1, PPC::R11);
Chris Lattner919c0322005-10-01 01:35:02 +0000106 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R11), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000107 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000108 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnerb47e0892006-06-12 21:50:57 +0000109 // We need to store the CR in the low 4-bits of the saved value. First,
110 // issue a MFCR to save all of the CRBits.
Chris Lattnere67304f2006-06-12 23:59:16 +0000111 BuildMI(MBB, MI, PPC::MFCR, 0, PPC::R0);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000112
113 // If the saved register wasn't CR0, shift the bits left so that they are in
114 // CR0's slot.
115 if (SrcReg != PPC::CR0) {
116 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Chris Lattnere67304f2006-06-12 23:59:16 +0000117 // rlwinm r0, r0, ShiftBits, 0, 31.
118 BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
119 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000120 }
121
Chris Lattnere67304f2006-06-12 23:59:16 +0000122 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R0), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000123 } else if (RC == PPC::GPRCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000124 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(SrcReg),FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000125 } else if (RC == PPC::G8RCRegisterClass) {
126 addFrameReference(BuildMI(MBB, MI, PPC::STD, 3).addReg(SrcReg),FrameIdx);
127 } else if (RC == PPC::F8RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000128 addFrameReference(BuildMI(MBB, MI, PPC::STFD, 3).addReg(SrcReg),FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000129 } else if (RC == PPC::F4RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000130 addFrameReference(BuildMI(MBB, MI, PPC::STFS, 3).addReg(SrcReg),FrameIdx);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000131 } else if (RC == PPC::VRRCRegisterClass) {
132 // We don't have indexed addressing for vector loads. Emit:
133 // R11 = ADDI FI#
134 // Dest = LVX R0, R11
135 //
136 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnere45aa732006-05-04 16:56:45 +0000137 addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000138 BuildMI(MBB, MI, PPC::STVX, 3)
139 .addReg(SrcReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000140 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000141 assert(0 && "Unknown regclass!");
142 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000143 }
144}
145
146void
Nate Begeman21e463b2005-10-16 05:39:50 +0000147PPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000148 MachineBasicBlock::iterator MI,
Chris Lattnerb48d2cf2005-09-30 01:31:52 +0000149 unsigned DestReg, int FrameIdx,
150 const TargetRegisterClass *RC) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000151 if (DestReg == PPC::LR) {
Chris Lattner919c0322005-10-01 01:35:02 +0000152 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R11), FrameIdx);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000153 BuildMI(MBB, MI, PPC::MTLR, 1).addReg(PPC::R11);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000154 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000155 // FIXME: We use R0 here, because it isn't available for RA.
156 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R0), FrameIdx);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000157
158 // If the reloaded register isn't CR0, shift the bits right so that they are
159 // in the right CR's slot.
160 if (DestReg != PPC::CR0) {
161 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
162 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Chris Lattnere67304f2006-06-12 23:59:16 +0000163 BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
164 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000165 }
166
Chris Lattnere67304f2006-06-12 23:59:16 +0000167 BuildMI(MBB, MI, PPC::MTCRF, 1, DestReg).addReg(PPC::R0);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000168 } else if (RC == PPC::GPRCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000169 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, DestReg), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000170 } else if (RC == PPC::G8RCRegisterClass) {
171 addFrameReference(BuildMI(MBB, MI, PPC::LD, 2, DestReg), FrameIdx);
172 } else if (RC == PPC::F8RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000173 addFrameReference(BuildMI(MBB, MI, PPC::LFD, 2, DestReg), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000174 } else if (RC == PPC::F4RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000175 addFrameReference(BuildMI(MBB, MI, PPC::LFS, 2, DestReg), FrameIdx);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000176 } else if (RC == PPC::VRRCRegisterClass) {
177 // We don't have indexed addressing for vector loads. Emit:
178 // R11 = ADDI FI#
179 // Dest = LVX R0, R11
180 //
181 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnere45aa732006-05-04 16:56:45 +0000182 addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000183 BuildMI(MBB, MI, PPC::LVX, 2, DestReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000184 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000185 assert(0 && "Unknown regclass!");
186 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000187 }
188}
189
Nate Begeman21e463b2005-10-16 05:39:50 +0000190void PPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
191 MachineBasicBlock::iterator MI,
192 unsigned DestReg, unsigned SrcReg,
193 const TargetRegisterClass *RC) const {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000194 if (RC == PPC::GPRCRegisterClass) {
Chris Lattnerb410dc92006-06-20 23:18:58 +0000195 BuildMI(MBB, MI, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000196 } else if (RC == PPC::G8RCRegisterClass) {
197 BuildMI(MBB, MI, PPC::OR8, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
198 } else if (RC == PPC::F4RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000199 BuildMI(MBB, MI, PPC::FMRS, 1, DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000200 } else if (RC == PPC::F8RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000201 BuildMI(MBB, MI, PPC::FMRD, 1, DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000202 } else if (RC == PPC::CRRCRegisterClass) {
Nate Begeman7af02482005-04-12 07:04:16 +0000203 BuildMI(MBB, MI, PPC::MCRF, 1, DestReg).addReg(SrcReg);
Chris Lattner335fd3c2006-03-16 20:03:58 +0000204 } else if (RC == PPC::VRRCRegisterClass) {
205 BuildMI(MBB, MI, PPC::VOR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman7af02482005-04-12 07:04:16 +0000206 } else {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000207 std::cerr << "Attempt to copy register that is not GPR or FPR";
208 abort();
209 }
210}
211
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000212const unsigned* PPCRegisterInfo::getCalleeSaveRegs() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000213 // 32-bit Darwin calling convention.
214 static const unsigned Darwin32_CalleeSaveRegs[] = {
215 PPC::R1 , PPC::R13, PPC::R14, PPC::R15,
216 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
217 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
218 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
219 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
220
221 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
222 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
223 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
224 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000225 PPC::F30, PPC::F31,
Chris Lattner804e0672006-07-11 00:48:23 +0000226
227 PPC::CR2, PPC::CR3, PPC::CR4,
228 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
229 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
230 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
231
232 PPC::LR, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000233 };
Chris Lattner804e0672006-07-11 00:48:23 +0000234 // 64-bit Darwin calling convention.
235 static const unsigned Darwin64_CalleeSaveRegs[] = {
236 PPC::X1 , PPC::X13, PPC::X14, PPC::X15,
237 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
238 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
239 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
240 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
241
242 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
243 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
244 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
245 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
246 PPC::F30, PPC::F31,
247
248 PPC::CR2, PPC::CR3, PPC::CR4,
249 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
250 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
251 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
252
253 PPC::LR, 0
254 };
255
256 return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegs :
257 Darwin32_CalleeSaveRegs;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000258}
259
260const TargetRegisterClass* const*
261PPCRegisterInfo::getCalleeSaveRegClasses() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000262 // 32-bit Darwin calling convention.
263 static const TargetRegisterClass * const Darwin32_CalleeSaveRegClasses[] = {
264 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
265 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
266 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
267 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
268 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
269
270 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
271 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
272 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
273 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
274 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
275
276 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
277
278 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
279 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
280 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
281
282 &PPC::GPRCRegClass, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000283 };
Chris Lattner804e0672006-07-11 00:48:23 +0000284
285 // 64-bit Darwin calling convention.
286 static const TargetRegisterClass * const Darwin64_CalleeSaveRegClasses[] = {
287 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
288 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
289 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
290 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
291 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
292
293 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
294 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
295 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
296 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
297 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
298
299 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
300
301 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
302 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
303 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
304
305 &PPC::GPRCRegClass, 0
306 };
307
308 return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegClasses :
309 Darwin32_CalleeSaveRegClasses;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000310}
311
Chris Lattnerf38df042005-09-09 21:46:49 +0000312/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
313/// copy instructions, turning them into load/store instructions.
Nate Begeman21e463b2005-10-16 05:39:50 +0000314MachineInstr *PPCRegisterInfo::foldMemoryOperand(MachineInstr *MI,
315 unsigned OpNum,
316 int FrameIndex) const {
Chris Lattnerf38df042005-09-09 21:46:49 +0000317 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
318 // it takes more than one instruction to store it.
319 unsigned Opc = MI->getOpcode();
320
Chris Lattnerb410dc92006-06-20 23:18:58 +0000321 if ((Opc == PPC::OR &&
Chris Lattnerf38df042005-09-09 21:46:49 +0000322 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
323 if (OpNum == 0) { // move -> store
324 unsigned InReg = MI->getOperand(1).getReg();
325 return addFrameReference(BuildMI(PPC::STW,
326 3).addReg(InReg), FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000327 } else { // move -> load
Chris Lattnerf38df042005-09-09 21:46:49 +0000328 unsigned OutReg = MI->getOperand(0).getReg();
329 return addFrameReference(BuildMI(PPC::LWZ, 2, OutReg), FrameIndex);
330 }
Nate Begeman1d9d7422005-10-18 00:28:58 +0000331 } else if ((Opc == PPC::OR8 &&
332 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
333 if (OpNum == 0) { // move -> store
334 unsigned InReg = MI->getOperand(1).getReg();
335 return addFrameReference(BuildMI(PPC::STD,
336 3).addReg(InReg), FrameIndex);
337 } else { // move -> load
338 unsigned OutReg = MI->getOperand(0).getReg();
339 return addFrameReference(BuildMI(PPC::LD, 2, OutReg), FrameIndex);
340 }
Chris Lattner919c0322005-10-01 01:35:02 +0000341 } else if (Opc == PPC::FMRD) {
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000342 if (OpNum == 0) { // move -> store
343 unsigned InReg = MI->getOperand(1).getReg();
344 return addFrameReference(BuildMI(PPC::STFD,
345 3).addReg(InReg), FrameIndex);
346 } else { // move -> load
347 unsigned OutReg = MI->getOperand(0).getReg();
348 return addFrameReference(BuildMI(PPC::LFD, 2, OutReg), FrameIndex);
349 }
Chris Lattner919c0322005-10-01 01:35:02 +0000350 } else if (Opc == PPC::FMRS) {
351 if (OpNum == 0) { // move -> store
352 unsigned InReg = MI->getOperand(1).getReg();
353 return addFrameReference(BuildMI(PPC::STFS,
354 3).addReg(InReg), FrameIndex);
355 } else { // move -> load
356 unsigned OutReg = MI->getOperand(0).getReg();
357 return addFrameReference(BuildMI(PPC::LFS, 2, OutReg), FrameIndex);
358 }
Chris Lattnerf38df042005-09-09 21:46:49 +0000359 }
360 return 0;
361}
362
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000363//===----------------------------------------------------------------------===//
364// Stack Frame Processing methods
365//===----------------------------------------------------------------------===//
366
367// hasFP - Return true if the specified function should have a dedicated frame
368// pointer register. This is true if the function has variable sized allocas or
369// if frame pointer elimination is disabled.
370//
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000371static bool hasFP(const MachineFunction &MF) {
372 const MachineFrameInfo *MFI = MF.getFrameInfo();
373 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
374
Nate Begeman030514c2006-04-11 19:29:21 +0000375 // If frame pointers are forced, or if there are variable sized stack objects,
376 // use a frame pointer.
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000377 //
Nate Begeman030514c2006-04-11 19:29:21 +0000378 return NoFramePointerElim || MFI->hasVarSizedObjects();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000379}
380
Nate Begeman21e463b2005-10-16 05:39:50 +0000381void PPCRegisterInfo::
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000382eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
383 MachineBasicBlock::iterator I) const {
384 if (hasFP(MF)) {
385 // If we have a frame pointer, convert as follows:
386 // ADJCALLSTACKDOWN -> addi, r1, r1, -amount
387 // ADJCALLSTACKUP -> addi, r1, r1, amount
388 MachineInstr *Old = I;
389 unsigned Amount = Old->getOperand(0).getImmedValue();
390 if (Amount != 0) {
391 // We need to keep the stack aligned properly. To do this, we round the
392 // amount of space needed for the outgoing arguments up to the next
393 // alignment boundary.
394 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
395 Amount = (Amount+Align-1)/Align*Align;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000396
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000397 // Replace the pseudo instruction with a new instruction...
398 if (Old->getOpcode() == PPC::ADJCALLSTACKDOWN) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000399 BuildMI(MBB, I, PPC::ADDI, 2, PPC::R1).addReg(PPC::R1).addImm(-Amount);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000400 } else {
401 assert(Old->getOpcode() == PPC::ADJCALLSTACKUP);
Chris Lattner63b3d712006-05-04 17:21:20 +0000402 BuildMI(MBB, I, PPC::ADDI, 2, PPC::R1).addReg(PPC::R1).addImm(Amount);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000403 }
404 }
405 }
406 MBB.erase(I);
407}
408
409void
Nate Begeman21e463b2005-10-16 05:39:50 +0000410PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000411 unsigned i = 0;
412 MachineInstr &MI = *II;
413 MachineBasicBlock &MBB = *MI.getParent();
414 MachineFunction &MF = *MBB.getParent();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000415
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000416 while (!MI.getOperand(i).isFrameIndex()) {
417 ++i;
418 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
419 }
420
421 int FrameIndex = MI.getOperand(i).getFrameIndex();
422
423 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
Chris Lattnere53f4a02006-05-04 17:52:23 +0000424 MI.getOperand(i).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000425
426 // Take into account whether it's an add or mem instruction
427 unsigned OffIdx = (i == 2) ? 1 : 2;
428
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000429 // Figure out if the offset in the instruction is shifted right two bits. This
430 // is true for instructions like "STD", which the machine implicitly adds two
431 // low zeros to.
432 bool isIXAddr = false;
433 switch (MI.getOpcode()) {
434 case PPC::LWA:
435 case PPC::LD:
436 case PPC::STD:
437 case PPC::STD_32:
438 isIXAddr = true;
439 break;
440 }
441
442
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000443 // Now add the frame object offset to the offset from r1.
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000444 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
445
446 if (!isIXAddr)
447 Offset += MI.getOperand(OffIdx).getImmedValue();
448 else
449 Offset += MI.getOperand(OffIdx).getImmedValue() << 2;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000450
451 // If we're not using a Frame Pointer that has been set to the value of the
452 // SP before having the stack size subtracted from it, then add the stack size
453 // to Offset to get the correct offset.
454 Offset += MF.getFrameInfo()->getStackSize();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000455
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000456 if (Offset > 32767 || Offset < -32768) {
457 // Insert a set of r0 with the full offset value before the ld, st, or add
458 MachineBasicBlock *MBB = MI.getParent();
Chris Lattner63b3d712006-05-04 17:21:20 +0000459 BuildMI(*MBB, II, PPC::LIS, 1, PPC::R0).addImm(Offset >> 16);
Chris Lattnerc6d48d32006-01-11 23:07:57 +0000460 BuildMI(*MBB, II, PPC::ORI, 2, PPC::R0).addReg(PPC::R0).addImm(Offset);
461
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000462 // convert into indexed form of the instruction
463 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
464 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
Chris Lattner14630192005-09-09 20:51:08 +0000465 assert(ImmToIdxMap.count(MI.getOpcode()) &&
466 "No indexed form of load or store available!");
467 unsigned NewOpcode = ImmToIdxMap.find(MI.getOpcode())->second;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000468 MI.setOpcode(NewOpcode);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000469 MI.getOperand(1).ChangeToRegister(MI.getOperand(i).getReg());
470 MI.getOperand(2).ChangeToRegister(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000471 } else {
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000472 if (isIXAddr) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000473 assert((Offset & 3) == 0 && "Invalid frame offset!");
474 Offset >>= 2; // The actual encoded value has the low two bits zero.
Chris Lattner841d12d2005-10-18 16:51:22 +0000475 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000476 MI.getOperand(OffIdx).ChangeToImmediate(Offset);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000477 }
478}
479
Chris Lattnerf7d23722006-04-17 20:59:25 +0000480/// VRRegNo - Map from a numbered VR register to its enum value.
481///
482static const unsigned short VRRegNo[] = {
Chris Lattnerb47e0892006-06-12 21:50:57 +0000483 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
484 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
Chris Lattnerf7d23722006-04-17 20:59:25 +0000485 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
486 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
487};
488
Chris Lattnerf9568d82006-04-17 21:48:13 +0000489/// RemoveVRSaveCode - We have found that this function does not need any code
490/// to manipulate the VRSAVE register, even though it uses vector registers.
491/// This can happen when the only registers used are known to be live in or out
492/// of the function. Remove all of the VRSAVE related code from the function.
493static void RemoveVRSaveCode(MachineInstr *MI) {
494 MachineBasicBlock *Entry = MI->getParent();
495 MachineFunction *MF = Entry->getParent();
496
497 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
498 MachineBasicBlock::iterator MBBI = MI;
499 ++MBBI;
500 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
501 MBBI->eraseFromParent();
502
503 bool RemovedAllMTVRSAVEs = true;
504 // See if we can find and remove the MTVRSAVE instruction from all of the
505 // epilog blocks.
506 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
507 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
508 // If last instruction is a return instruction, add an epilogue
509 if (!I->empty() && TII.isReturn(I->back().getOpcode())) {
510 bool FoundIt = false;
511 for (MBBI = I->end(); MBBI != I->begin(); ) {
512 --MBBI;
513 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
514 MBBI->eraseFromParent(); // remove it.
515 FoundIt = true;
516 break;
517 }
518 }
519 RemovedAllMTVRSAVEs &= FoundIt;
520 }
521 }
522
523 // If we found and removed all MTVRSAVE instructions, remove the read of
524 // VRSAVE as well.
525 if (RemovedAllMTVRSAVEs) {
526 MBBI = MI;
527 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
528 --MBBI;
529 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
530 MBBI->eraseFromParent();
531 }
532
533 // Finally, nuke the UPDATE_VRSAVE.
534 MI->eraseFromParent();
535}
536
Chris Lattner1877ec92006-03-13 21:52:10 +0000537// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
538// instruction selector. Based on the vector registers that have been used,
539// transform this into the appropriate ORI instruction.
540static void HandleVRSaveUpdate(MachineInstr *MI, const bool *UsedRegs) {
541 unsigned UsedRegMask = 0;
Chris Lattnerf7d23722006-04-17 20:59:25 +0000542 for (unsigned i = 0; i != 32; ++i)
543 if (UsedRegs[VRRegNo[i]])
544 UsedRegMask |= 1 << (31-i);
545
Chris Lattner402504b2006-04-17 21:22:06 +0000546 // Live in and live out values already must be in the mask, so don't bother
547 // marking them.
548 MachineFunction *MF = MI->getParent()->getParent();
549 for (MachineFunction::livein_iterator I =
550 MF->livein_begin(), E = MF->livein_end(); I != E; ++I) {
551 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
552 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
553 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
554 }
555 for (MachineFunction::liveout_iterator I =
556 MF->liveout_begin(), E = MF->liveout_end(); I != E; ++I) {
557 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
558 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
559 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
560 }
561
Chris Lattner1877ec92006-03-13 21:52:10 +0000562 unsigned SrcReg = MI->getOperand(1).getReg();
563 unsigned DstReg = MI->getOperand(0).getReg();
564 // If no registers are used, turn this into a copy.
565 if (UsedRegMask == 0) {
Chris Lattnerf9568d82006-04-17 21:48:13 +0000566 // Remove all VRSAVE code.
567 RemoveVRSaveCode(MI);
568 return;
Chris Lattner1877ec92006-03-13 21:52:10 +0000569 } else if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
570 BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
571 .addReg(SrcReg).addImm(UsedRegMask);
572 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
573 BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
574 .addReg(SrcReg).addImm(UsedRegMask >> 16);
575 } else {
576 BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
577 .addReg(SrcReg).addImm(UsedRegMask >> 16);
578 BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
579 .addReg(DstReg).addImm(UsedRegMask & 0xFFFF);
580 }
581
582 // Remove the old UPDATE_VRSAVE instruction.
Chris Lattnerf9568d82006-04-17 21:48:13 +0000583 MI->eraseFromParent();
Chris Lattner1877ec92006-03-13 21:52:10 +0000584}
585
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000586
Nate Begeman21e463b2005-10-16 05:39:50 +0000587void PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000588 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
589 MachineBasicBlock::iterator MBBI = MBB.begin();
590 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Laskey41886992006-04-07 16:34:46 +0000591 MachineDebugInfo *DebugInfo = MFI->getMachineDebugInfo();
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000592
593 // Do we have a frame pointer for this function?
594 bool HasFP = hasFP(MF);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000595
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000596 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
597 // process it.
Chris Lattner8aa777d2006-03-16 21:31:45 +0000598 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000599 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
600 HandleVRSaveUpdate(MBBI, MF.getUsedPhysregs());
601 break;
602 }
603 }
604
605 // Move MBBI back to the beginning of the function.
606 MBBI = MBB.begin();
607
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000608 // Get the number of bytes to allocate from the FrameInfo
609 unsigned NumBytes = MFI->getStackSize();
Nate Begemanae232e72005-11-06 09:00:38 +0000610
611 // Get the alignments provided by the target, and the maximum alignment
612 // (if any) of the fixed frame objects.
613 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
614 unsigned MaxAlign = MFI->getMaxAlignment();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000615
616 // If we have calls, we cannot use the red zone to store callee save registers
617 // and we must set up a stack frame, so calculate the necessary size here.
618 if (MFI->hasCalls()) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000619 // We reserve argument space for call sites in the function immediately on
620 // entry to the current function. This eliminates the need for add/sub
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000621 // brackets around call sites.
622 NumBytes += MFI->getMaxCallFrameSize();
623 }
624
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000625 // If we are a leaf function, and use up to 224 bytes of stack space,
Nate Begeman54eed362005-07-27 06:06:29 +0000626 // and don't have a frame pointer, then we do not need to adjust the stack
627 // pointer (we fit in the Red Zone).
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000628 if ((NumBytes == 0) || (NumBytes <= 224 && !HasFP && !MFI->hasCalls() &&
Nate Begemanae232e72005-11-06 09:00:38 +0000629 MaxAlign <= TargetAlign)) {
Nate Begeman54eed362005-07-27 06:06:29 +0000630 MFI->setStackSize(0);
631 return;
632 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000633
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000634 // Add the size of R1 to NumBytes size for the store of R1 to the bottom
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000635 // of the stack and round the size to a multiple of the alignment.
Nate Begemanae232e72005-11-06 09:00:38 +0000636 unsigned Align = std::max(TargetAlign, MaxAlign);
Chris Lattner5802be12005-09-30 17:16:59 +0000637 unsigned GPRSize = 4;
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000638 unsigned Size = HasFP ? GPRSize + GPRSize : GPRSize;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000639 NumBytes = (NumBytes+Size+Align-1)/Align*Align;
640
641 // Update frame info to pretend that this is part of the stack...
642 MFI->setStackSize(NumBytes);
Jim Laskey41886992006-04-07 16:34:46 +0000643 int NegNumbytes = -NumBytes;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000644
Nate Begeman3dee1752005-07-27 23:11:27 +0000645 // Adjust stack pointer: r1 -= numbytes.
Nate Begeman030514c2006-04-11 19:29:21 +0000646 // If there is a preferred stack alignment, align R1 now
647 if (MaxAlign > TargetAlign) {
648 assert(isPowerOf2_32(MaxAlign) && MaxAlign < 32767 && "Invalid alignment!");
Chris Lattner3e6a3502006-05-10 06:38:32 +0000649 assert(isInt16(0-NumBytes) && "Unhandled stack size and alignment!");
Nate Begeman030514c2006-04-11 19:29:21 +0000650 BuildMI(MBB, MBBI, PPC::RLWINM, 4, PPC::R0)
651 .addReg(PPC::R1).addImm(0).addImm(32-Log2_32(MaxAlign)).addImm(31);
Nate Begeman21862982006-04-11 19:44:43 +0000652 BuildMI(MBB, MBBI, PPC::SUBFIC,2,PPC::R0).addReg(PPC::R0)
Chris Lattner3e6a3502006-05-10 06:38:32 +0000653 .addImm(0-NumBytes);
Nate Begeman030514c2006-04-11 19:29:21 +0000654 BuildMI(MBB, MBBI, PPC::STWUX, 3)
655 .addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0);
656 } else if (NumBytes <= 32768) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000657 BuildMI(MBB, MBBI, PPC::STWU, 3).addReg(PPC::R1).addImm(NegNumbytes)
Nate Begeman030514c2006-04-11 19:29:21 +0000658 .addReg(PPC::R1);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000659 } else {
Chris Lattner63b3d712006-05-04 17:21:20 +0000660 BuildMI(MBB, MBBI, PPC::LIS, 1, PPC::R0).addImm(NegNumbytes >> 16);
Nate Begeman030514c2006-04-11 19:29:21 +0000661 BuildMI(MBB, MBBI, PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
662 .addImm(NegNumbytes & 0xFFFF);
663 BuildMI(MBB, MBBI, PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1)
664 .addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000665 }
Nate Begemanae232e72005-11-06 09:00:38 +0000666
Jim Laskey52fa2442006-04-11 08:11:53 +0000667 if (DebugInfo && DebugInfo->hasInfo()) {
Jim Laskey41886992006-04-07 16:34:46 +0000668 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
669 unsigned LabelID = DebugInfo->NextLabelID();
670
671 // Show update of SP.
672 MachineLocation Dst(MachineLocation::VirtualFP);
673 MachineLocation Src(MachineLocation::VirtualFP, NegNumbytes);
674 Moves.push_back(new MachineMove(LabelID, Dst, Src));
675
Chris Lattner63b3d712006-05-04 17:21:20 +0000676 BuildMI(MBB, MBBI, PPC::DWARF_LABEL, 1).addImm(LabelID);
Jim Laskey41886992006-04-07 16:34:46 +0000677 }
678
Nate Begemanae232e72005-11-06 09:00:38 +0000679 // If there is a frame pointer, copy R1 (SP) into R31 (FP)
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000680 if (HasFP) {
Chris Lattnerc6d48d32006-01-11 23:07:57 +0000681 BuildMI(MBB, MBBI, PPC::STW, 3)
Chris Lattner63b3d712006-05-04 17:21:20 +0000682 .addReg(PPC::R31).addImm(GPRSize).addReg(PPC::R1);
Chris Lattnerb410dc92006-06-20 23:18:58 +0000683 BuildMI(MBB, MBBI, PPC::OR, 2, PPC::R31).addReg(PPC::R1).addReg(PPC::R1);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000684 }
685}
686
Nate Begeman21e463b2005-10-16 05:39:50 +0000687void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
688 MachineBasicBlock &MBB) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000689 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Evan Cheng6da8d992006-01-09 18:28:21 +0000690 assert(MBBI->getOpcode() == PPC::BLR &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000691 "Can only insert epilog into returning blocks");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000692
Nate Begeman030514c2006-04-11 19:29:21 +0000693 // Get alignment info so we know how to restore r1
694 const MachineFrameInfo *MFI = MF.getFrameInfo();
695 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
696
Chris Lattner64da1722006-01-11 23:03:54 +0000697 // Get the number of bytes allocated from the FrameInfo.
Nate Begeman030514c2006-04-11 19:29:21 +0000698 unsigned NumBytes = MFI->getStackSize();
Chris Lattner64da1722006-01-11 23:03:54 +0000699 unsigned GPRSize = 4;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000700
701 if (NumBytes != 0) {
Chris Lattner64da1722006-01-11 23:03:54 +0000702 // If this function has a frame pointer, load the saved stack pointer from
703 // its stack slot.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000704 if (hasFP(MF)) {
Chris Lattner64da1722006-01-11 23:03:54 +0000705 BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R31)
Chris Lattner63b3d712006-05-04 17:21:20 +0000706 .addImm(GPRSize).addReg(PPC::R31);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000707 }
Chris Lattner64da1722006-01-11 23:03:54 +0000708
709 // The loaded (or persistent) stack pointer value is offseted by the 'stwu'
710 // on entry to the function. Add this offset back now.
Nate Begeman030514c2006-04-11 19:29:21 +0000711 if (NumBytes < 32768 && TargetAlign >= MFI->getMaxAlignment()) {
Chris Lattner64da1722006-01-11 23:03:54 +0000712 BuildMI(MBB, MBBI, PPC::ADDI, 2, PPC::R1)
Chris Lattner63b3d712006-05-04 17:21:20 +0000713 .addReg(PPC::R1).addImm(NumBytes);
Chris Lattner64da1722006-01-11 23:03:54 +0000714 } else {
Chris Lattner63b3d712006-05-04 17:21:20 +0000715 BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R1).addImm(0).addReg(PPC::R1);
Chris Lattner64da1722006-01-11 23:03:54 +0000716 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000717 }
718}
719
Jim Laskey41886992006-04-07 16:34:46 +0000720unsigned PPCRegisterInfo::getRARegister() const {
721 return PPC::LR;
722}
723
Jim Laskeya9979182006-03-28 13:48:33 +0000724unsigned PPCRegisterInfo::getFrameRegister(MachineFunction &MF) const {
Jim Laskey41886992006-04-07 16:34:46 +0000725 return hasFP(MF) ? PPC::R31 : PPC::R1;
726}
727
728void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove *> &Moves)
729 const {
730 // Initial state is the frame pointer is R1.
731 MachineLocation Dst(MachineLocation::VirtualFP);
732 MachineLocation Src(PPC::R1, 0);
733 Moves.push_back(new MachineMove(0, Dst, Src));
Jim Laskeyf1d78e82006-03-23 18:12:57 +0000734}
735
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000736#include "PPCGenRegisterInfo.inc"
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000737