blob: 5edf051924c1487d85a84a3779597c042f591477 [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"
Jim Laskey2f616bf2006-11-16 22:43:37 +000017#include "PPCMachineFunctionInfo.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000018#include "PPCRegisterInfo.h"
Jim Laskey2f616bf2006-11-16 22:43:37 +000019#include "PPCFrameInfo.h"
Chris Lattner804e0672006-07-11 00:48:23 +000020#include "PPCSubtarget.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000021#include "llvm/Constants.h"
22#include "llvm/Type.h"
23#include "llvm/CodeGen/ValueTypes.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Jim Laskey41886992006-04-07 16:34:46 +000025#include "llvm/CodeGen/MachineDebugInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyf1d78e82006-03-23 18:12:57 +000028#include "llvm/CodeGen/MachineLocation.h"
Jim Laskey41886992006-04-07 16:34:46 +000029#include "llvm/CodeGen/SelectionDAGNodes.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000030#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf9568d82006-04-17 21:48:13 +000031#include "llvm/Target/TargetInstrInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Nate Begemanae232e72005-11-06 09:00:38 +000036#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/ADT/STLExtras.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000038#include <cstdlib>
39#include <iostream>
40using namespace llvm;
41
Chris Lattner369503f2006-04-17 21:07:20 +000042/// getRegisterNumbering - Given the enum value for some register, e.g.
43/// PPC::F14, return the number that it corresponds to (e.g. 14).
44unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000045 using namespace PPC;
Chris Lattner369503f2006-04-17 21:07:20 +000046 switch (RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000047 case R0 : case X0 : case F0 : case V0 : case CR0: return 0;
48 case R1 : case X1 : case F1 : case V1 : case CR1: return 1;
49 case R2 : case X2 : case F2 : case V2 : case CR2: return 2;
50 case R3 : case X3 : case F3 : case V3 : case CR3: return 3;
51 case R4 : case X4 : case F4 : case V4 : case CR4: return 4;
52 case R5 : case X5 : case F5 : case V5 : case CR5: return 5;
53 case R6 : case X6 : case F6 : case V6 : case CR6: return 6;
54 case R7 : case X7 : case F7 : case V7 : case CR7: return 7;
55 case R8 : case X8 : case F8 : case V8 : return 8;
56 case R9 : case X9 : case F9 : case V9 : return 9;
57 case R10: case X10: case F10: case V10: return 10;
58 case R11: case X11: case F11: case V11: return 11;
59 case R12: case X12: case F12: case V12: return 12;
60 case R13: case X13: case F13: case V13: return 13;
61 case R14: case X14: case F14: case V14: return 14;
62 case R15: case X15: case F15: case V15: return 15;
63 case R16: case X16: case F16: case V16: return 16;
64 case R17: case X17: case F17: case V17: return 17;
65 case R18: case X18: case F18: case V18: return 18;
66 case R19: case X19: case F19: case V19: return 19;
67 case R20: case X20: case F20: case V20: return 20;
68 case R21: case X21: case F21: case V21: return 21;
69 case R22: case X22: case F22: case V22: return 22;
70 case R23: case X23: case F23: case V23: return 23;
71 case R24: case X24: case F24: case V24: return 24;
72 case R25: case X25: case F25: case V25: return 25;
73 case R26: case X26: case F26: case V26: return 26;
74 case R27: case X27: case F27: case V27: return 27;
75 case R28: case X28: case F28: case V28: return 28;
76 case R29: case X29: case F29: case V29: return 29;
77 case R30: case X30: case F30: case V30: return 30;
78 case R31: case X31: case F31: case V31: return 31;
79 default:
80 std::cerr << "Unhandled reg in PPCRegisterInfo::getRegisterNumbering!\n";
81 abort();
Chris Lattner369503f2006-04-17 21:07:20 +000082 }
83}
84
Evan Cheng7ce45782006-11-13 23:36:35 +000085PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
86 const TargetInstrInfo &tii)
Chris Lattner804e0672006-07-11 00:48:23 +000087 : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Evan Cheng7ce45782006-11-13 23:36:35 +000088 Subtarget(ST), TII(tii) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +000089 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000090 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
91 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
92 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
93 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
94 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
95 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
Nate Begeman1d9d7422005-10-18 00:28:58 +000096 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000097}
98
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099void
Nate Begeman21e463b2005-10-16 05:39:50 +0000100PPCRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
101 MachineBasicBlock::iterator MI,
102 unsigned SrcReg, int FrameIdx,
103 const TargetRegisterClass *RC) const {
Chris Lattner6a5339b2006-11-14 18:44:47 +0000104 if (RC == PPC::GPRCRegisterClass) {
105 if (SrcReg != PPC::LR) {
106 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(SrcReg),FrameIdx);
107 } else {
108 // FIXME: this spills LR immediately to memory in one step. To do this,
109 // we use R11, which we know cannot be used in the prolog/epilog. This is
110 // a hack.
111 BuildMI(MBB, MI, PPC::MFLR, 1, PPC::R11);
112 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R11),
113 FrameIdx);
114 }
115 } else if (RC == PPC::G8RCRegisterClass) {
116 if (SrcReg != PPC::LR8) {
117 addFrameReference(BuildMI(MBB, MI, PPC::STD, 3).addReg(SrcReg), FrameIdx);
118 } else {
119 // FIXME: this spills LR immediately to memory in one step. To do this,
120 // we use R11, which we know cannot be used in the prolog/epilog. This is
121 // a hack.
122 BuildMI(MBB, MI, PPC::MFLR8, 1, PPC::X11);
123 addFrameReference(BuildMI(MBB, MI, PPC::STD, 3).addReg(PPC::X11),
124 FrameIdx);
125 }
126 } else if (RC == PPC::F8RCRegisterClass) {
127 addFrameReference(BuildMI(MBB, MI, PPC::STFD, 3).addReg(SrcReg),FrameIdx);
128 } else if (RC == PPC::F4RCRegisterClass) {
129 addFrameReference(BuildMI(MBB, MI, PPC::STFS, 3).addReg(SrcReg),FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000130 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000131 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnerb47e0892006-06-12 21:50:57 +0000132 // We need to store the CR in the low 4-bits of the saved value. First,
133 // issue a MFCR to save all of the CRBits.
Chris Lattnere67304f2006-06-12 23:59:16 +0000134 BuildMI(MBB, MI, PPC::MFCR, 0, PPC::R0);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000135
136 // If the saved register wasn't CR0, shift the bits left so that they are in
137 // CR0's slot.
138 if (SrcReg != PPC::CR0) {
139 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Chris Lattnere67304f2006-06-12 23:59:16 +0000140 // rlwinm r0, r0, ShiftBits, 0, 31.
141 BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
142 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000143 }
144
Chris Lattnere67304f2006-06-12 23:59:16 +0000145 addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R0), FrameIdx);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000146 } else if (RC == PPC::VRRCRegisterClass) {
147 // We don't have indexed addressing for vector loads. Emit:
148 // R11 = ADDI FI#
149 // Dest = LVX R0, R11
150 //
151 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnere45aa732006-05-04 16:56:45 +0000152 addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000153 BuildMI(MBB, MI, PPC::STVX, 3)
154 .addReg(SrcReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000155 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000156 assert(0 && "Unknown regclass!");
157 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000158 }
159}
160
161void
Nate Begeman21e463b2005-10-16 05:39:50 +0000162PPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Chris Lattner6a5339b2006-11-14 18:44:47 +0000163 MachineBasicBlock::iterator MI,
164 unsigned DestReg, int FrameIdx,
165 const TargetRegisterClass *RC) const {
166 if (RC == PPC::GPRCRegisterClass) {
167 if (DestReg != PPC::LR) {
168 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, DestReg), FrameIdx);
169 } else {
170 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R11), FrameIdx);
171 BuildMI(MBB, MI, PPC::MTLR, 1).addReg(PPC::R11);
172 }
173 } else if (RC == PPC::G8RCRegisterClass) {
174 if (DestReg != PPC::LR8) {
175 addFrameReference(BuildMI(MBB, MI, PPC::LD, 2, DestReg), FrameIdx);
176 } else {
177 addFrameReference(BuildMI(MBB, MI, PPC::LD, 2, PPC::R11), FrameIdx);
178 BuildMI(MBB, MI, PPC::MTLR8, 1).addReg(PPC::R11);
179 }
180 } else if (RC == PPC::F8RCRegisterClass) {
181 addFrameReference(BuildMI(MBB, MI, PPC::LFD, 2, DestReg), FrameIdx);
182 } else if (RC == PPC::F4RCRegisterClass) {
183 addFrameReference(BuildMI(MBB, MI, PPC::LFS, 2, DestReg), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000184 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000185 // FIXME: We use R0 here, because it isn't available for RA.
186 addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R0), FrameIdx);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000187
188 // If the reloaded register isn't CR0, shift the bits right so that they are
189 // in the right CR's slot.
190 if (DestReg != PPC::CR0) {
191 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
192 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Chris Lattnere67304f2006-06-12 23:59:16 +0000193 BuildMI(MBB, MI, PPC::RLWINM, 4, PPC::R0)
194 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000195 }
196
Chris Lattnere67304f2006-06-12 23:59:16 +0000197 BuildMI(MBB, MI, PPC::MTCRF, 1, DestReg).addReg(PPC::R0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000198 } else if (RC == PPC::VRRCRegisterClass) {
199 // We don't have indexed addressing for vector loads. Emit:
200 // R11 = ADDI FI#
201 // Dest = LVX R0, R11
202 //
203 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnere45aa732006-05-04 16:56:45 +0000204 addFrameReference(BuildMI(MBB, MI, PPC::ADDI, 1, PPC::R0), FrameIdx, 0, 0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000205 BuildMI(MBB, MI, PPC::LVX, 2, DestReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000206 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000207 assert(0 && "Unknown regclass!");
208 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000209 }
210}
211
Nate Begeman21e463b2005-10-16 05:39:50 +0000212void PPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
213 MachineBasicBlock::iterator MI,
214 unsigned DestReg, unsigned SrcReg,
215 const TargetRegisterClass *RC) const {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000216 if (RC == PPC::GPRCRegisterClass) {
Chris Lattnerb410dc92006-06-20 23:18:58 +0000217 BuildMI(MBB, MI, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000218 } else if (RC == PPC::G8RCRegisterClass) {
219 BuildMI(MBB, MI, PPC::OR8, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
220 } else if (RC == PPC::F4RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000221 BuildMI(MBB, MI, PPC::FMRS, 1, DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000222 } else if (RC == PPC::F8RCRegisterClass) {
Chris Lattner919c0322005-10-01 01:35:02 +0000223 BuildMI(MBB, MI, PPC::FMRD, 1, DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000224 } else if (RC == PPC::CRRCRegisterClass) {
Nate Begeman7af02482005-04-12 07:04:16 +0000225 BuildMI(MBB, MI, PPC::MCRF, 1, DestReg).addReg(SrcReg);
Chris Lattner335fd3c2006-03-16 20:03:58 +0000226 } else if (RC == PPC::VRRCRegisterClass) {
227 BuildMI(MBB, MI, PPC::VOR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman7af02482005-04-12 07:04:16 +0000228 } else {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000229 std::cerr << "Attempt to copy register that is not GPR or FPR";
230 abort();
231 }
232}
233
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000234const unsigned* PPCRegisterInfo::getCalleeSaveRegs() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000235 // 32-bit Darwin calling convention.
236 static const unsigned Darwin32_CalleeSaveRegs[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000237 PPC::R13, PPC::R14, PPC::R15,
Chris Lattner804e0672006-07-11 00:48:23 +0000238 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
239 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
240 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
241 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
242
243 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
244 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
245 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
246 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000247 PPC::F30, PPC::F31,
Chris Lattner804e0672006-07-11 00:48:23 +0000248
249 PPC::CR2, PPC::CR3, PPC::CR4,
250 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
251 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
252 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
253
254 PPC::LR, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000255 };
Chris Lattner804e0672006-07-11 00:48:23 +0000256 // 64-bit Darwin calling convention.
257 static const unsigned Darwin64_CalleeSaveRegs[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000258 PPC::X13, PPC::X14, PPC::X15,
Chris Lattner804e0672006-07-11 00:48:23 +0000259 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
260 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
261 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
262 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
263
264 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
265 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
266 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
267 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
268 PPC::F30, PPC::F31,
269
270 PPC::CR2, PPC::CR3, PPC::CR4,
271 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
272 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
273 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
274
Chris Lattner6a5339b2006-11-14 18:44:47 +0000275 PPC::LR8, 0
Chris Lattner804e0672006-07-11 00:48:23 +0000276 };
277
278 return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegs :
279 Darwin32_CalleeSaveRegs;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000280}
281
282const TargetRegisterClass* const*
283PPCRegisterInfo::getCalleeSaveRegClasses() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000284 // 32-bit Darwin calling convention.
285 static const TargetRegisterClass * const Darwin32_CalleeSaveRegClasses[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000286 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
Chris Lattner804e0672006-07-11 00:48:23 +0000287 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
288 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
289 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
290 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
291
292 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
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,
297
298 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
299
300 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
301 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
302 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
303
304 &PPC::GPRCRegClass, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000305 };
Chris Lattner804e0672006-07-11 00:48:23 +0000306
307 // 64-bit Darwin calling convention.
308 static const TargetRegisterClass * const Darwin64_CalleeSaveRegClasses[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000309 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
Chris Lattner804e0672006-07-11 00:48:23 +0000310 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
311 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
312 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
313 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
314
315 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
316 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
317 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
318 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
319 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
320
321 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
322
323 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
324 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
325 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
326
Chris Lattner6a5339b2006-11-14 18:44:47 +0000327 &PPC::G8RCRegClass, 0
Chris Lattner804e0672006-07-11 00:48:23 +0000328 };
329
330 return Subtarget.isPPC64() ? Darwin64_CalleeSaveRegClasses :
331 Darwin32_CalleeSaveRegClasses;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000332}
333
Chris Lattnerf38df042005-09-09 21:46:49 +0000334/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
335/// copy instructions, turning them into load/store instructions.
Nate Begeman21e463b2005-10-16 05:39:50 +0000336MachineInstr *PPCRegisterInfo::foldMemoryOperand(MachineInstr *MI,
337 unsigned OpNum,
338 int FrameIndex) const {
Chris Lattnerf38df042005-09-09 21:46:49 +0000339 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
340 // it takes more than one instruction to store it.
341 unsigned Opc = MI->getOpcode();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000342
343 MachineInstr *NewMI = NULL;
Chris Lattnerb410dc92006-06-20 23:18:58 +0000344 if ((Opc == PPC::OR &&
Chris Lattnerf38df042005-09-09 21:46:49 +0000345 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
346 if (OpNum == 0) { // move -> store
347 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000348 NewMI = addFrameReference(BuildMI(TII, PPC::STW,
349 3).addReg(InReg), FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000350 } else { // move -> load
Chris Lattnerf38df042005-09-09 21:46:49 +0000351 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000352 NewMI = addFrameReference(BuildMI(TII, PPC::LWZ, 2, OutReg), FrameIndex);
Chris Lattnerf38df042005-09-09 21:46:49 +0000353 }
Nate Begeman1d9d7422005-10-18 00:28:58 +0000354 } else if ((Opc == PPC::OR8 &&
355 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
356 if (OpNum == 0) { // move -> store
357 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000358 NewMI = addFrameReference(BuildMI(TII, PPC::STD,
359 3).addReg(InReg), FrameIndex);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000360 } else { // move -> load
361 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000362 NewMI = addFrameReference(BuildMI(TII, PPC::LD, 2, OutReg), FrameIndex);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000363 }
Chris Lattner919c0322005-10-01 01:35:02 +0000364 } else if (Opc == PPC::FMRD) {
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000365 if (OpNum == 0) { // move -> store
366 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000367 NewMI = addFrameReference(BuildMI(TII, PPC::STFD,
368 3).addReg(InReg), FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000369 } else { // move -> load
370 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000371 NewMI = addFrameReference(BuildMI(TII, PPC::LFD, 2, OutReg), FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000372 }
Chris Lattner919c0322005-10-01 01:35:02 +0000373 } else if (Opc == PPC::FMRS) {
374 if (OpNum == 0) { // move -> store
375 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000376 NewMI = addFrameReference(BuildMI(TII, PPC::STFS,
Chris Lattner919c0322005-10-01 01:35:02 +0000377 3).addReg(InReg), FrameIndex);
378 } else { // move -> load
379 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000380 NewMI = addFrameReference(BuildMI(TII, PPC::LFS, 2, OutReg), FrameIndex);
Chris Lattner919c0322005-10-01 01:35:02 +0000381 }
Chris Lattnerf38df042005-09-09 21:46:49 +0000382 }
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000383
384 if (NewMI)
385 NewMI->copyKillDeadInfo(MI);
386 return NewMI;
Chris Lattnerf38df042005-09-09 21:46:49 +0000387}
388
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000389//===----------------------------------------------------------------------===//
390// Stack Frame Processing methods
391//===----------------------------------------------------------------------===//
392
Jim Laskey2f616bf2006-11-16 22:43:37 +0000393// needsFP - Return true if the specified function should have a dedicated frame
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000394// pointer register. This is true if the function has variable sized allocas or
395// if frame pointer elimination is disabled.
396//
Jim Laskey2f616bf2006-11-16 22:43:37 +0000397static bool needsFP(const MachineFunction &MF) {
398 const MachineFrameInfo *MFI = MF.getFrameInfo();
399 return NoFramePointerElim || MFI->hasVarSizedObjects();
400}
401
402// hasFP - Return true if the specified function actually has a dedicated frame
403// pointer register. This is true if the function needs a frame pointer and has
404// a non-zero stack size.
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000405static bool hasFP(const MachineFunction &MF) {
406 const MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000407 return MFI->getStackSize() && needsFP(MF);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000408}
409
Nate Begeman21e463b2005-10-16 05:39:50 +0000410void PPCRegisterInfo::
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000411eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator I) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000413 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000414 MBB.erase(I);
415}
416
Jim Laskey2f616bf2006-11-16 22:43:37 +0000417/// LowerDynamicAlloc - Generate the code for allocating an object in the
418/// current frame. The sequence of code with be in the general form
419///
420/// addi R0, SP, #frameSize ; get the address of the previous frame
421/// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
422/// addi Rnew, SP, #maxCalFrameSize ; get the top of the allocation
423///
424void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
425 // Get the instruction.
426 MachineInstr &MI = *II;
427 // Get the instruction's basic block.
428 MachineBasicBlock &MBB = *MI.getParent();
429 // Get the basic block's function.
430 MachineFunction &MF = *MBB.getParent();
431 // Get the frame info.
432 MachineFrameInfo *MFI = MF.getFrameInfo();
433 // Determine whether 64-bit pointers are used.
434 bool LP64 = Subtarget.isPPC64();
435
436 // Determine the maximum call stack size. maxCallFrameSize may be
437 // less than the minimum.
438 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
439 unsigned getMinCallFrameSize =
440 PPCFrameInfo::getMinCallFrameSize(LP64);
441 maxCallFrameSize = std::max(maxCallFrameSize, getMinCallFrameSize);
442 // Get the total frame size.
443 unsigned FrameSize = MFI->getStackSize();
444
445 // Get stack alignments.
446 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
447 unsigned MaxAlign = MFI->getMaxAlignment();
448
449 // Determine the previous frame's address. If FrameSize can't be
450 // represented as 16 bits or we need special alignment, then we load the
451 // previous frame's address from 0(SP). Why not do an addis of the hi?
452 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
453 // Constructing the constant and adding would take 3 instructions.
454 // Fortunately, a frame greater than 32K is rare.
455 if (MaxAlign < TargetAlign && isInt16(FrameSize)) {
456 BuildMI(MBB, II, PPC::ADDI, 2, PPC::R0)
457 .addReg(PPC::R31)
458 .addImm(FrameSize);
459 } else if (LP64) {
460 BuildMI(MBB, II, PPC::LD, 2, PPC::X0)
461 .addImm(0)
462 .addReg(PPC::X1);
463 } else {
464 BuildMI(MBB, II, PPC::LWZ, 2, PPC::R0)
465 .addImm(0)
466 .addReg(PPC::R1);
467 }
468
469 // Grow the stack and update the stack pointer link, then
470 // determine the address of new allocated space.
471 if (LP64) {
472 BuildMI(MBB, II, PPC::STDUX, 3)
473 .addReg(PPC::X0)
474 .addReg(PPC::X1)
475 .addReg(MI.getOperand(1).getReg());
476 BuildMI(MBB, II, PPC::ADDI8, 2, MI.getOperand(0).getReg())
477 .addReg(PPC::X1)
478 .addImm(maxCallFrameSize);
479 } else {
480 BuildMI(MBB, II, PPC::STWUX, 3)
481 .addReg(PPC::R0)
482 .addReg(PPC::R1)
483 .addReg(MI.getOperand(1).getReg());
484 BuildMI(MBB, II, PPC::ADDI, 2, MI.getOperand(0).getReg())
485 .addReg(PPC::R1)
486 .addImm(maxCallFrameSize);
487 }
488
489 // Discard the DYNALLOC instruction.
490 MBB.erase(II);
491}
492
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000493void
Nate Begeman21e463b2005-10-16 05:39:50 +0000494PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000495 // Get the instruction.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000496 MachineInstr &MI = *II;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000497 // Get the instruction's basic block.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000498 MachineBasicBlock &MBB = *MI.getParent();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000499 // Get the basic block's function.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000500 MachineFunction &MF = *MBB.getParent();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000501 // Get the frame info.
502 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000503
Jim Laskey2f616bf2006-11-16 22:43:37 +0000504 // Find out which operand is the frame index.
505 unsigned i = 0;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000506 while (!MI.getOperand(i).isFrameIndex()) {
507 ++i;
508 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
509 }
Jim Laskey2f616bf2006-11-16 22:43:37 +0000510 // Take into account whether it's an add or mem instruction
511 unsigned OffIdx = (i == 2) ? 1 : 2;
512 // Get the frame index.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000513 int FrameIndex = MI.getOperand(i).getFrameIndex();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000514
515 // Get the frame pointer save index. Users of this index are primarily
516 // DYNALLOC instructions.
517 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
518 int FPSI = FI->getFramePointerSaveIndex();
519 // Get the instruction opcode.
520 unsigned OpC = MI.getOpcode();
521
522 // Special case for dynamic alloca.
523 if (FPSI && FrameIndex == FPSI &&
524 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
525 lowerDynamicAlloc(II);
526 return;
527 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000528
529 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
Chris Lattner09e46062006-09-05 02:31:13 +0000530 MI.getOperand(i).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1, false);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000531
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000532 // Figure out if the offset in the instruction is shifted right two bits. This
533 // is true for instructions like "STD", which the machine implicitly adds two
534 // low zeros to.
535 bool isIXAddr = false;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000536 switch (OpC) {
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000537 case PPC::LWA:
538 case PPC::LD:
539 case PPC::STD:
540 case PPC::STD_32:
541 isIXAddr = true;
542 break;
543 }
544
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000545 // Now add the frame object offset to the offset from r1.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000546 int Offset = MFI->getObjectOffset(FrameIndex);
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000547
548 if (!isIXAddr)
549 Offset += MI.getOperand(OffIdx).getImmedValue();
550 else
551 Offset += MI.getOperand(OffIdx).getImmedValue() << 2;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000552
553 // If we're not using a Frame Pointer that has been set to the value of the
554 // SP before having the stack size subtracted from it, then add the stack size
555 // to Offset to get the correct offset.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000556 Offset += MFI->getStackSize();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000557
Jim Laskey2f616bf2006-11-16 22:43:37 +0000558 if (!isInt16(Offset)) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000559 // Insert a set of r0 with the full offset value before the ld, st, or add
Jim Laskey2f616bf2006-11-16 22:43:37 +0000560 BuildMI(MBB, II, PPC::LIS, 1, PPC::R0).addImm(Offset >> 16);
561 BuildMI(MBB, II, PPC::ORI, 2, PPC::R0).addReg(PPC::R0).addImm(Offset);
Chris Lattnerc6d48d32006-01-11 23:07:57 +0000562
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000563 // convert into indexed form of the instruction
564 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
565 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
Jim Laskey2f616bf2006-11-16 22:43:37 +0000566 assert(ImmToIdxMap.count(OpC) &&
Chris Lattner14630192005-09-09 20:51:08 +0000567 "No indexed form of load or store available!");
Jim Laskey2f616bf2006-11-16 22:43:37 +0000568 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000569 MI.setOpcode(NewOpcode);
Chris Lattner09e46062006-09-05 02:31:13 +0000570 MI.getOperand(1).ChangeToRegister(MI.getOperand(i).getReg(), false);
571 MI.getOperand(2).ChangeToRegister(PPC::R0, false);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000572 } else {
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000573 if (isIXAddr) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000574 assert((Offset & 3) == 0 && "Invalid frame offset!");
575 Offset >>= 2; // The actual encoded value has the low two bits zero.
Chris Lattner841d12d2005-10-18 16:51:22 +0000576 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000577 MI.getOperand(OffIdx).ChangeToImmediate(Offset);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000578 }
579}
580
Chris Lattnerf7d23722006-04-17 20:59:25 +0000581/// VRRegNo - Map from a numbered VR register to its enum value.
582///
583static const unsigned short VRRegNo[] = {
Chris Lattnerb47e0892006-06-12 21:50:57 +0000584 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
585 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
Chris Lattnerf7d23722006-04-17 20:59:25 +0000586 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
587 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
588};
589
Chris Lattnerf9568d82006-04-17 21:48:13 +0000590/// RemoveVRSaveCode - We have found that this function does not need any code
591/// to manipulate the VRSAVE register, even though it uses vector registers.
592/// This can happen when the only registers used are known to be live in or out
593/// of the function. Remove all of the VRSAVE related code from the function.
594static void RemoveVRSaveCode(MachineInstr *MI) {
595 MachineBasicBlock *Entry = MI->getParent();
596 MachineFunction *MF = Entry->getParent();
597
598 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
599 MachineBasicBlock::iterator MBBI = MI;
600 ++MBBI;
601 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
602 MBBI->eraseFromParent();
603
604 bool RemovedAllMTVRSAVEs = true;
605 // See if we can find and remove the MTVRSAVE instruction from all of the
606 // epilog blocks.
607 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
608 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
609 // If last instruction is a return instruction, add an epilogue
610 if (!I->empty() && TII.isReturn(I->back().getOpcode())) {
611 bool FoundIt = false;
612 for (MBBI = I->end(); MBBI != I->begin(); ) {
613 --MBBI;
614 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
615 MBBI->eraseFromParent(); // remove it.
616 FoundIt = true;
617 break;
618 }
619 }
620 RemovedAllMTVRSAVEs &= FoundIt;
621 }
622 }
623
624 // If we found and removed all MTVRSAVE instructions, remove the read of
625 // VRSAVE as well.
626 if (RemovedAllMTVRSAVEs) {
627 MBBI = MI;
628 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
629 --MBBI;
630 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
631 MBBI->eraseFromParent();
632 }
633
634 // Finally, nuke the UPDATE_VRSAVE.
635 MI->eraseFromParent();
636}
637
Chris Lattner1877ec92006-03-13 21:52:10 +0000638// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
639// instruction selector. Based on the vector registers that have been used,
640// transform this into the appropriate ORI instruction.
641static void HandleVRSaveUpdate(MachineInstr *MI, const bool *UsedRegs) {
642 unsigned UsedRegMask = 0;
Chris Lattnerf7d23722006-04-17 20:59:25 +0000643 for (unsigned i = 0; i != 32; ++i)
644 if (UsedRegs[VRRegNo[i]])
645 UsedRegMask |= 1 << (31-i);
646
Chris Lattner402504b2006-04-17 21:22:06 +0000647 // Live in and live out values already must be in the mask, so don't bother
648 // marking them.
649 MachineFunction *MF = MI->getParent()->getParent();
650 for (MachineFunction::livein_iterator I =
651 MF->livein_begin(), E = MF->livein_end(); I != E; ++I) {
652 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
653 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
654 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
655 }
656 for (MachineFunction::liveout_iterator I =
657 MF->liveout_begin(), E = MF->liveout_end(); I != E; ++I) {
658 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
659 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
660 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
661 }
662
Chris Lattner1877ec92006-03-13 21:52:10 +0000663 unsigned SrcReg = MI->getOperand(1).getReg();
664 unsigned DstReg = MI->getOperand(0).getReg();
665 // If no registers are used, turn this into a copy.
666 if (UsedRegMask == 0) {
Chris Lattnerf9568d82006-04-17 21:48:13 +0000667 // Remove all VRSAVE code.
668 RemoveVRSaveCode(MI);
669 return;
Chris Lattner1877ec92006-03-13 21:52:10 +0000670 } else if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
671 BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
672 .addReg(SrcReg).addImm(UsedRegMask);
673 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
674 BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
675 .addReg(SrcReg).addImm(UsedRegMask >> 16);
676 } else {
677 BuildMI(*MI->getParent(), MI, PPC::ORIS, 2, DstReg)
678 .addReg(SrcReg).addImm(UsedRegMask >> 16);
679 BuildMI(*MI->getParent(), MI, PPC::ORI, 2, DstReg)
680 .addReg(DstReg).addImm(UsedRegMask & 0xFFFF);
681 }
682
683 // Remove the old UPDATE_VRSAVE instruction.
Chris Lattnerf9568d82006-04-17 21:48:13 +0000684 MI->eraseFromParent();
Chris Lattner1877ec92006-03-13 21:52:10 +0000685}
686
Jim Laskey2f616bf2006-11-16 22:43:37 +0000687/// determineFrameLayout - Determine the size of the frame and maximum call
688/// frame size.
689void PPCRegisterInfo::determineFrameLayout(MachineFunction &MF) const {
690 MachineFrameInfo *MFI = MF.getFrameInfo();
691
692 // Get the number of bytes to allocate from the FrameInfo
693 unsigned FrameSize = MFI->getStackSize();
694
695 // Get the alignments provided by the target, and the maximum alignment
696 // (if any) of the fixed frame objects.
697 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
698 unsigned MaxAlign = MFI->getMaxAlignment();
699 unsigned Align = std::max(TargetAlign, MaxAlign);
700 assert(isPowerOf2_32(Align) && "Alignment is not power of 2");
701 unsigned AlignMask = Align - 1; //
702
703 // If we are a leaf function, and use up to 224 bytes of stack space,
704 // don't have a frame pointer, calls, or dynamic alloca then we do not need
705 // to adjust the stack pointer (we fit in the Red Zone).
706 if (FrameSize <= 224 && // Fits in red zone.
Jim Laskey2ff5cdb2006-11-17 16:09:31 +0000707 !MFI->hasVarSizedObjects() && // No dynamic alloca.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000708 !MFI->hasCalls() && // No calls.
709 MaxAlign <= TargetAlign) { // No special alignment.
710 // No need for frame
711 MFI->setStackSize(0);
712 return;
713 }
714
715 // Get the maximum call frame size of all the calls.
716 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
717
718 // Maximum call frame needs to be at least big enough for linkage and 8 args.
719 unsigned minCallFrameSize =
720 PPCFrameInfo::getMinCallFrameSize(Subtarget.isPPC64());
721 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
722
723 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
724 // that allocations will be aligned.
725 if (MFI->hasVarSizedObjects())
726 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
727
728 // Update maximum call frame size.
729 MFI->setMaxCallFrameSize(maxCallFrameSize);
730
731 // Include call frame size in total.
732 FrameSize += maxCallFrameSize;
733
734 // Make sure the frame is aligned.
735 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
736
737 // Update frame info.
738 MFI->setStackSize(FrameSize);
739}
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000740
Nate Begeman21e463b2005-10-16 05:39:50 +0000741void PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000742 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
743 MachineBasicBlock::iterator MBBI = MBB.begin();
744 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Laskey41886992006-04-07 16:34:46 +0000745 MachineDebugInfo *DebugInfo = MFI->getMachineDebugInfo();
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000746
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000747 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
748 // process it.
Chris Lattner8aa777d2006-03-16 21:31:45 +0000749 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000750 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
751 HandleVRSaveUpdate(MBBI, MF.getUsedPhysregs());
752 break;
753 }
754 }
755
756 // Move MBBI back to the beginning of the function.
757 MBBI = MBB.begin();
758
Jim Laskey2f616bf2006-11-16 22:43:37 +0000759 // Work out frame sizes.
760 determineFrameLayout(MF);
761 unsigned FrameSize = MFI->getStackSize();
Nate Begemanae232e72005-11-06 09:00:38 +0000762
Jim Laskey2f616bf2006-11-16 22:43:37 +0000763 // Skip if a leaf routine.
764 if (!FrameSize) return;
765
766 int NegFrameSize = -FrameSize;
767
768 // Do we have a frame pointer for this function?
769 bool HasFP = hasFP(MF);
770
771 // If there is a frame pointer, copy R31 into TOC(SP)
772 if (HasFP) {
773 int Offset = PPCFrameInfo::getFramePointerSaveOffset(Subtarget.isPPC64());
774
775 if (!Subtarget.isPPC64()) {
776 BuildMI(MBB, MBBI, PPC::STW, 3)
777 .addReg(PPC::R31).addImm(Offset).addReg(PPC::R1);
778 } else {
779 BuildMI(MBB, MBBI, PPC::STD, 3)
780 .addReg(PPC::X31).addImm(Offset/4).addReg(PPC::X1);
781 }
782 }
783
784 // Get stack alignments.
Nate Begemanae232e72005-11-06 09:00:38 +0000785 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
786 unsigned MaxAlign = MFI->getMaxAlignment();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000787
Jim Laskey2f616bf2006-11-16 22:43:37 +0000788 // Adjust stack pointer: r1 += NegFrameSize.
Nate Begeman030514c2006-04-11 19:29:21 +0000789 // If there is a preferred stack alignment, align R1 now
Chris Lattnera94a2032006-11-11 19:05:28 +0000790 if (!Subtarget.isPPC64()) {
791 // PPC32.
792 if (MaxAlign > TargetAlign) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000793 assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
794 assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
Chris Lattnera94a2032006-11-11 19:05:28 +0000795 BuildMI(MBB, MBBI, PPC::RLWINM, 4, PPC::R0)
796 .addReg(PPC::R1).addImm(0).addImm(32-Log2_32(MaxAlign)).addImm(31);
797 BuildMI(MBB, MBBI, PPC::SUBFIC,2,PPC::R0).addReg(PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000798 .addImm(NegFrameSize);
Chris Lattnera94a2032006-11-11 19:05:28 +0000799 BuildMI(MBB, MBBI, PPC::STWUX, 3)
800 .addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000801 } else if (isInt16(NegFrameSize)) {
Chris Lattner5e14b822006-11-15 17:40:51 +0000802 BuildMI(MBB, MBBI, PPC::STWU, 3,
Jim Laskey2f616bf2006-11-16 22:43:37 +0000803 PPC::R1).addReg(PPC::R1).addImm(NegFrameSize).addReg(PPC::R1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000804 } else {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000805 BuildMI(MBB, MBBI, PPC::LIS, 1, PPC::R0).addImm(NegFrameSize >> 16);
Chris Lattnera94a2032006-11-11 19:05:28 +0000806 BuildMI(MBB, MBBI, PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000807 .addImm(NegFrameSize & 0xFFFF);
Chris Lattnera94a2032006-11-11 19:05:28 +0000808 BuildMI(MBB, MBBI, PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1)
809 .addReg(PPC::R0);
810 }
811 } else { // PPC64.
812 if (MaxAlign > TargetAlign) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000813 assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
814 assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
Chris Lattnera94a2032006-11-11 19:05:28 +0000815 BuildMI(MBB, MBBI, PPC::RLDICL, 3, PPC::X0)
816 .addReg(PPC::X1).addImm(0).addImm(64-Log2_32(MaxAlign));
817 BuildMI(MBB, MBBI, PPC::SUBFIC8, 2, PPC::X0).addReg(PPC::X0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000818 .addImm(NegFrameSize);
Chris Lattnera94a2032006-11-11 19:05:28 +0000819 BuildMI(MBB, MBBI, PPC::STDUX, 3)
820 .addReg(PPC::X1).addReg(PPC::X1).addReg(PPC::X0);
Jim Laskey2ff5cdb2006-11-17 16:09:31 +0000821 } else if (isInt16(NegFrameSize)) {
Chris Lattner5e14b822006-11-15 17:40:51 +0000822 BuildMI(MBB, MBBI, PPC::STDU, 3, PPC::X1)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000823 .addReg(PPC::X1).addImm(NegFrameSize/4).addReg(PPC::X1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000824 } else {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000825 BuildMI(MBB, MBBI, PPC::LIS8, 1, PPC::X0).addImm(NegFrameSize >> 16);
Chris Lattnera94a2032006-11-11 19:05:28 +0000826 BuildMI(MBB, MBBI, PPC::ORI8, 2, PPC::X0).addReg(PPC::X0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000827 .addImm(NegFrameSize & 0xFFFF);
Chris Lattnera94a2032006-11-11 19:05:28 +0000828 BuildMI(MBB, MBBI, PPC::STDUX, 3).addReg(PPC::X1).addReg(PPC::X1)
829 .addReg(PPC::X0);
830 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000831 }
Nate Begemanae232e72005-11-06 09:00:38 +0000832
Jim Laskey52fa2442006-04-11 08:11:53 +0000833 if (DebugInfo && DebugInfo->hasInfo()) {
Jim Laskey41886992006-04-07 16:34:46 +0000834 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
835 unsigned LabelID = DebugInfo->NextLabelID();
836
Jim Laskey4c2c9032006-08-25 19:40:59 +0000837 // Mark effective beginning of when frame pointer becomes valid.
Chris Lattner63b3d712006-05-04 17:21:20 +0000838 BuildMI(MBB, MBBI, PPC::DWARF_LABEL, 1).addImm(LabelID);
Jim Laskey4c2c9032006-08-25 19:40:59 +0000839
Jim Laskeyce50a162006-08-29 16:24:26 +0000840 // Show update of SP.
841 MachineLocation SPDst(MachineLocation::VirtualFP);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000842 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
Jim Laskeyce50a162006-08-29 16:24:26 +0000843 Moves.push_back(new MachineMove(LabelID, SPDst, SPSrc));
844
845 // Add callee saved registers to move list.
846 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
847 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
848 MachineLocation CSDst(MachineLocation::VirtualFP,
849 MFI->getObjectOffset(CSI[I].getFrameIdx()));
850 MachineLocation CSSrc(CSI[I].getReg());
851 Moves.push_back(new MachineMove(LabelID, CSDst, CSSrc));
852 }
Jim Laskey41886992006-04-07 16:34:46 +0000853 }
Jim Laskey2f616bf2006-11-16 22:43:37 +0000854
855 // If there is a frame pointer, copy R1 into R31
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000856 if (HasFP) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000857 if (!Subtarget.isPPC64()) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000858 BuildMI(MBB, MBBI, PPC::OR, 2, PPC::R31).addReg(PPC::R1).addReg(PPC::R1);
859 } else {
Chris Lattnera94a2032006-11-11 19:05:28 +0000860 BuildMI(MBB, MBBI, PPC::OR8, 2, PPC::X31).addReg(PPC::X1).addReg(PPC::X1);
861 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000862 }
863}
864
Nate Begeman21e463b2005-10-16 05:39:50 +0000865void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
866 MachineBasicBlock &MBB) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000867 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Evan Cheng6da8d992006-01-09 18:28:21 +0000868 assert(MBBI->getOpcode() == PPC::BLR &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000869 "Can only insert epilog into returning blocks");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000870
Nate Begeman030514c2006-04-11 19:29:21 +0000871 // Get alignment info so we know how to restore r1
872 const MachineFrameInfo *MFI = MF.getFrameInfo();
873 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000874 unsigned MaxAlign = MFI->getMaxAlignment();
Nate Begeman030514c2006-04-11 19:29:21 +0000875
Chris Lattner64da1722006-01-11 23:03:54 +0000876 // Get the number of bytes allocated from the FrameInfo.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000877 unsigned FrameSize = MFI->getStackSize();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000878
Jim Laskey2f616bf2006-11-16 22:43:37 +0000879 if (FrameSize != 0) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000880 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
Chris Lattner64da1722006-01-11 23:03:54 +0000881 // on entry to the function. Add this offset back now.
Chris Lattnera94a2032006-11-11 19:05:28 +0000882 if (!Subtarget.isPPC64()) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000883 if (isInt16(FrameSize) && TargetAlign >= MaxAlign &&
884 !MFI->hasVarSizedObjects()) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000885 BuildMI(MBB, MBBI, PPC::ADDI, 2, PPC::R1)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000886 .addReg(PPC::R1).addImm(FrameSize);
Chris Lattnera94a2032006-11-11 19:05:28 +0000887 } else {
888 BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R1).addImm(0).addReg(PPC::R1);
889 }
Chris Lattner64da1722006-01-11 23:03:54 +0000890 } else {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000891 if (isInt16(FrameSize) && TargetAlign >= MaxAlign &&
892 !MFI->hasVarSizedObjects()) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000893 BuildMI(MBB, MBBI, PPC::ADDI8, 2, PPC::X1)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000894 .addReg(PPC::X1).addImm(FrameSize);
Chris Lattnera94a2032006-11-11 19:05:28 +0000895 } else {
896 BuildMI(MBB, MBBI, PPC::LD, 2, PPC::X1).addImm(0).addReg(PPC::X1);
897 }
Chris Lattner64da1722006-01-11 23:03:54 +0000898 }
Jim Laskey2f616bf2006-11-16 22:43:37 +0000899
900 // If this function has a frame pointer, load the saved frame pointer from
901 // its stack slot.
902 if (hasFP(MF)) {
903 int Offset = PPCFrameInfo::getFramePointerSaveOffset(Subtarget.isPPC64());
904
905 if (!Subtarget.isPPC64()) {
906 BuildMI(MBB, MBBI, PPC::LWZ, 2, PPC::R31)
907 .addImm(Offset).addReg(PPC::R1);
908 } else {
909 BuildMI(MBB, MBBI, PPC::LD, 2, PPC::X31)
910 .addImm(Offset/4).addReg(PPC::X1);
911 }
912 }
913
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000914 }
915}
916
Jim Laskey41886992006-04-07 16:34:46 +0000917unsigned PPCRegisterInfo::getRARegister() const {
Chris Lattner6a5339b2006-11-14 18:44:47 +0000918 return !Subtarget.isPPC64() ? PPC::LR : PPC::LR8;
919
Jim Laskey41886992006-04-07 16:34:46 +0000920}
921
Jim Laskeya9979182006-03-28 13:48:33 +0000922unsigned PPCRegisterInfo::getFrameRegister(MachineFunction &MF) const {
Chris Lattnera94a2032006-11-11 19:05:28 +0000923 if (!Subtarget.isPPC64())
924 return hasFP(MF) ? PPC::R31 : PPC::R1;
925 else
926 return hasFP(MF) ? PPC::X31 : PPC::X1;
Jim Laskey41886992006-04-07 16:34:46 +0000927}
928
929void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove *> &Moves)
930 const {
Jim Laskey4c2c9032006-08-25 19:40:59 +0000931 // Initial state of the frame pointer is R1.
Jim Laskey41886992006-04-07 16:34:46 +0000932 MachineLocation Dst(MachineLocation::VirtualFP);
933 MachineLocation Src(PPC::R1, 0);
934 Moves.push_back(new MachineMove(0, Dst, Src));
Jim Laskeyf1d78e82006-03-23 18:12:57 +0000935}
936
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000937#include "PPCGenRegisterInfo.inc"
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000938