blob: 76ade794141bab9307f284c5731c4cdcbe9e236d [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>
Misha Brukmanf2ccb772004-08-17 04:55:41 +000039using namespace llvm;
40
Chris Lattner369503f2006-04-17 21:07:20 +000041/// getRegisterNumbering - Given the enum value for some register, e.g.
42/// PPC::F14, return the number that it corresponds to (e.g. 14).
43unsigned PPCRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000044 using namespace PPC;
Chris Lattner369503f2006-04-17 21:07:20 +000045 switch (RegEnum) {
Chris Lattnerbe6a0392006-07-11 20:53:55 +000046 case R0 : case X0 : case F0 : case V0 : case CR0: return 0;
47 case R1 : case X1 : case F1 : case V1 : case CR1: return 1;
48 case R2 : case X2 : case F2 : case V2 : case CR2: return 2;
49 case R3 : case X3 : case F3 : case V3 : case CR3: return 3;
50 case R4 : case X4 : case F4 : case V4 : case CR4: return 4;
51 case R5 : case X5 : case F5 : case V5 : case CR5: return 5;
52 case R6 : case X6 : case F6 : case V6 : case CR6: return 6;
53 case R7 : case X7 : case F7 : case V7 : case CR7: return 7;
54 case R8 : case X8 : case F8 : case V8 : return 8;
55 case R9 : case X9 : case F9 : case V9 : return 9;
56 case R10: case X10: case F10: case V10: return 10;
57 case R11: case X11: case F11: case V11: return 11;
58 case R12: case X12: case F12: case V12: return 12;
59 case R13: case X13: case F13: case V13: return 13;
60 case R14: case X14: case F14: case V14: return 14;
61 case R15: case X15: case F15: case V15: return 15;
62 case R16: case X16: case F16: case V16: return 16;
63 case R17: case X17: case F17: case V17: return 17;
64 case R18: case X18: case F18: case V18: return 18;
65 case R19: case X19: case F19: case V19: return 19;
66 case R20: case X20: case F20: case V20: return 20;
67 case R21: case X21: case F21: case V21: return 21;
68 case R22: case X22: case F22: case V22: return 22;
69 case R23: case X23: case F23: case V23: return 23;
70 case R24: case X24: case F24: case V24: return 24;
71 case R25: case X25: case F25: case V25: return 25;
72 case R26: case X26: case F26: case V26: return 26;
73 case R27: case X27: case F27: case V27: return 27;
74 case R28: case X28: case F28: case V28: return 28;
75 case R29: case X29: case F29: case V29: return 29;
76 case R30: case X30: case F30: case V30: return 30;
77 case R31: case X31: case F31: case V31: return 31;
78 default:
Bill Wendlingf5da1332006-12-07 22:21:48 +000079 cerr << "Unhandled reg in PPCRegisterInfo::getRegisterNumbering!\n";
Chris Lattnerbe6a0392006-07-11 20:53:55 +000080 abort();
Chris Lattner369503f2006-04-17 21:07:20 +000081 }
82}
83
Evan Cheng7ce45782006-11-13 23:36:35 +000084PPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
85 const TargetInstrInfo &tii)
Chris Lattner804e0672006-07-11 00:48:23 +000086 : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Evan Cheng7ce45782006-11-13 23:36:35 +000087 Subtarget(ST), TII(tii) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +000088 ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000089 ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX;
90 ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX;
91 ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX;
92 ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX;
93 ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX;
94 ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX;
Nate Begeman1d9d7422005-10-18 00:28:58 +000095 ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
Chris Lattnerc88fa742006-12-07 22:15:58 +000096 ImmToIdxMap[PPC::ADDI8] = PPC::ADD8;
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) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000106 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STW)).addReg(SrcReg),
107 FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000108 } else {
109 // FIXME: this spills LR immediately to memory in one step. To do this,
110 // we use R11, which we know cannot be used in the prolog/epilog. This is
111 // a hack.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000112 BuildMI(MBB, MI, TII.get(PPC::MFLR), PPC::R11);
113 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STW)).addReg(PPC::R11),
Chris Lattner6a5339b2006-11-14 18:44:47 +0000114 FrameIdx);
115 }
116 } else if (RC == PPC::G8RCRegisterClass) {
117 if (SrcReg != PPC::LR8) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000118 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STD)).addReg(SrcReg),
119 FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000120 } else {
121 // FIXME: this spills LR immediately to memory in one step. To do this,
122 // we use R11, which we know cannot be used in the prolog/epilog. This is
123 // a hack.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000124 BuildMI(MBB, MI, TII.get(PPC::MFLR8), PPC::X11);
125 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STD)).addReg(PPC::X11),
Chris Lattner6a5339b2006-11-14 18:44:47 +0000126 FrameIdx);
127 }
128 } else if (RC == PPC::F8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000129 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STFD)).addReg(SrcReg),
130 FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000131 } else if (RC == PPC::F4RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000132 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STFS)).addReg(SrcReg),
133 FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000134 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000135 // FIXME: We use R0 here, because it isn't available for RA.
Chris Lattnerb47e0892006-06-12 21:50:57 +0000136 // We need to store the CR in the low 4-bits of the saved value. First,
137 // issue a MFCR to save all of the CRBits.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000138 BuildMI(MBB, MI, TII.get(PPC::MFCR), PPC::R0);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000139
140 // If the saved register wasn't CR0, shift the bits left so that they are in
141 // CR0's slot.
142 if (SrcReg != PPC::CR0) {
143 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Chris Lattnere67304f2006-06-12 23:59:16 +0000144 // rlwinm r0, r0, ShiftBits, 0, 31.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000145 BuildMI(MBB, MI, TII.get(PPC::RLWINM), PPC::R0)
Chris Lattnere67304f2006-06-12 23:59:16 +0000146 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000147 }
148
Evan Chengc0f64ff2006-11-27 23:37:22 +0000149 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::STW)).addReg(PPC::R0),
150 FrameIdx);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000151 } else if (RC == PPC::VRRCRegisterClass) {
152 // We don't have indexed addressing for vector loads. Emit:
153 // R11 = ADDI FI#
154 // Dest = LVX R0, R11
155 //
156 // FIXME: We use R0 here, because it isn't available for RA.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000157 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::ADDI), PPC::R0),
158 FrameIdx, 0, 0);
159 BuildMI(MBB, MI, TII.get(PPC::STVX))
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000160 .addReg(SrcReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000161 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000162 assert(0 && "Unknown regclass!");
163 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000164 }
165}
166
167void
Nate Begeman21e463b2005-10-16 05:39:50 +0000168PPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Chris Lattner6a5339b2006-11-14 18:44:47 +0000169 MachineBasicBlock::iterator MI,
170 unsigned DestReg, int FrameIdx,
171 const TargetRegisterClass *RC) const {
172 if (RC == PPC::GPRCRegisterClass) {
173 if (DestReg != PPC::LR) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000174 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LWZ), DestReg), FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000175 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000176 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LWZ), PPC::R11),FrameIdx);
177 BuildMI(MBB, MI, TII.get(PPC::MTLR)).addReg(PPC::R11);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000178 }
179 } else if (RC == PPC::G8RCRegisterClass) {
180 if (DestReg != PPC::LR8) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000181 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LD), DestReg), FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000182 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000183 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LD), PPC::R11), FrameIdx);
184 BuildMI(MBB, MI, TII.get(PPC::MTLR8)).addReg(PPC::R11);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000185 }
186 } else if (RC == PPC::F8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000187 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LFD), DestReg), FrameIdx);
Chris Lattner6a5339b2006-11-14 18:44:47 +0000188 } else if (RC == PPC::F4RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000189 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LFS), DestReg), FrameIdx);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000190 } else if (RC == PPC::CRRCRegisterClass) {
Chris Lattnere67304f2006-06-12 23:59:16 +0000191 // FIXME: We use R0 here, because it isn't available for RA.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000192 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::LWZ), PPC::R0), FrameIdx);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000193
194 // If the reloaded register isn't CR0, shift the bits right so that they are
195 // in the right CR's slot.
196 if (DestReg != PPC::CR0) {
197 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
198 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000199 BuildMI(MBB, MI, TII.get(PPC::RLWINM), PPC::R0)
Chris Lattnere67304f2006-06-12 23:59:16 +0000200 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31);
Chris Lattnerb47e0892006-06-12 21:50:57 +0000201 }
202
Evan Chengc0f64ff2006-11-27 23:37:22 +0000203 BuildMI(MBB, MI, TII.get(PPC::MTCRF), DestReg).addReg(PPC::R0);
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000204 } else if (RC == PPC::VRRCRegisterClass) {
205 // We don't have indexed addressing for vector loads. Emit:
206 // R11 = ADDI FI#
207 // Dest = LVX R0, R11
208 //
209 // FIXME: We use R0 here, because it isn't available for RA.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000210 addFrameReference(BuildMI(MBB, MI, TII.get(PPC::ADDI), PPC::R0),
211 FrameIdx, 0, 0);
212 BuildMI(MBB, MI, TII.get(PPC::LVX),DestReg).addReg(PPC::R0).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000213 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000214 assert(0 && "Unknown regclass!");
215 abort();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000216 }
217}
218
Nate Begeman21e463b2005-10-16 05:39:50 +0000219void PPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
220 MachineBasicBlock::iterator MI,
221 unsigned DestReg, unsigned SrcReg,
222 const TargetRegisterClass *RC) const {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000223 if (RC == PPC::GPRCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000224 BuildMI(MBB, MI, TII.get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000225 } else if (RC == PPC::G8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000226 BuildMI(MBB, MI, TII.get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000227 } else if (RC == PPC::F4RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000228 BuildMI(MBB, MI, TII.get(PPC::FMRS), DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000229 } else if (RC == PPC::F8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000230 BuildMI(MBB, MI, TII.get(PPC::FMRD), DestReg).addReg(SrcReg);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000231 } else if (RC == PPC::CRRCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000232 BuildMI(MBB, MI, TII.get(PPC::MCRF), DestReg).addReg(SrcReg);
Chris Lattner335fd3c2006-03-16 20:03:58 +0000233 } else if (RC == PPC::VRRCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000234 BuildMI(MBB, MI, TII.get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nate Begeman7af02482005-04-12 07:04:16 +0000235 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000236 cerr << "Attempt to copy register that is not GPR or FPR";
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000237 abort();
238 }
239}
240
Evan Chengc2b861d2007-01-02 21:33:40 +0000241const unsigned* PPCRegisterInfo::getCalleeSavedRegs() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000242 // 32-bit Darwin calling convention.
Evan Chengc2b861d2007-01-02 21:33:40 +0000243 static const unsigned Darwin32_CalleeSavedRegs[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000244 PPC::R13, PPC::R14, PPC::R15,
Chris Lattner804e0672006-07-11 00:48:23 +0000245 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
246 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
247 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
248 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
249
250 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
251 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
252 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
253 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000254 PPC::F30, PPC::F31,
Chris Lattner804e0672006-07-11 00:48:23 +0000255
256 PPC::CR2, PPC::CR3, PPC::CR4,
257 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
258 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
259 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
260
261 PPC::LR, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000262 };
Chris Lattner804e0672006-07-11 00:48:23 +0000263 // 64-bit Darwin calling convention.
Evan Chengc2b861d2007-01-02 21:33:40 +0000264 static const unsigned Darwin64_CalleeSavedRegs[] = {
Chris Lattnerbdc571b2006-11-20 19:33:51 +0000265 PPC::X14, PPC::X15,
Chris Lattner804e0672006-07-11 00:48:23 +0000266 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
267 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
268 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
269 PPC::X28, PPC::X29, PPC::X30, PPC::X31,
270
271 PPC::F14, PPC::F15, PPC::F16, PPC::F17,
272 PPC::F18, PPC::F19, PPC::F20, PPC::F21,
273 PPC::F22, PPC::F23, PPC::F24, PPC::F25,
274 PPC::F26, PPC::F27, PPC::F28, PPC::F29,
275 PPC::F30, PPC::F31,
276
277 PPC::CR2, PPC::CR3, PPC::CR4,
278 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
279 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
280 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
281
Chris Lattner6a5339b2006-11-14 18:44:47 +0000282 PPC::LR8, 0
Chris Lattner804e0672006-07-11 00:48:23 +0000283 };
284
Evan Chengc2b861d2007-01-02 21:33:40 +0000285 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
286 Darwin32_CalleeSavedRegs;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000287}
288
289const TargetRegisterClass* const*
Evan Chengc2b861d2007-01-02 21:33:40 +0000290PPCRegisterInfo::getCalleeSavedRegClasses() const {
Chris Lattner804e0672006-07-11 00:48:23 +0000291 // 32-bit Darwin calling convention.
Evan Chengc2b861d2007-01-02 21:33:40 +0000292 static const TargetRegisterClass * const Darwin32_CalleeSavedRegClasses[] = {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000293 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
Chris Lattner804e0672006-07-11 00:48:23 +0000294 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
295 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
296 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
297 &PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,&PPC::GPRCRegClass,
298
299 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
300 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
301 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
302 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
303 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
304
305 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
306
307 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
308 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
309 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
310
311 &PPC::GPRCRegClass, 0
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000312 };
Chris Lattner804e0672006-07-11 00:48:23 +0000313
314 // 64-bit Darwin calling convention.
Evan Chengc2b861d2007-01-02 21:33:40 +0000315 static const TargetRegisterClass * const Darwin64_CalleeSavedRegClasses[] = {
Chris Lattnerbdc571b2006-11-20 19:33:51 +0000316 &PPC::G8RCRegClass,&PPC::G8RCRegClass,
Chris Lattner804e0672006-07-11 00:48:23 +0000317 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
318 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
319 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
320 &PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,&PPC::G8RCRegClass,
321
322 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
323 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
324 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
325 &PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,&PPC::F8RCRegClass,
326 &PPC::F8RCRegClass,&PPC::F8RCRegClass,
327
328 &PPC::CRRCRegClass,&PPC::CRRCRegClass,&PPC::CRRCRegClass,
329
330 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
331 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
332 &PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,&PPC::VRRCRegClass,
333
Chris Lattner6a5339b2006-11-14 18:44:47 +0000334 &PPC::G8RCRegClass, 0
Chris Lattner804e0672006-07-11 00:48:23 +0000335 };
336
Evan Chengc2b861d2007-01-02 21:33:40 +0000337 return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegClasses :
338 Darwin32_CalleeSavedRegClasses;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000339}
340
Chris Lattnerf38df042005-09-09 21:46:49 +0000341/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
342/// copy instructions, turning them into load/store instructions.
Nate Begeman21e463b2005-10-16 05:39:50 +0000343MachineInstr *PPCRegisterInfo::foldMemoryOperand(MachineInstr *MI,
344 unsigned OpNum,
345 int FrameIndex) const {
Chris Lattnerf38df042005-09-09 21:46:49 +0000346 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
347 // it takes more than one instruction to store it.
348 unsigned Opc = MI->getOpcode();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000349
350 MachineInstr *NewMI = NULL;
Chris Lattnerb410dc92006-06-20 23:18:58 +0000351 if ((Opc == PPC::OR &&
Chris Lattnerf38df042005-09-09 21:46:49 +0000352 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
353 if (OpNum == 0) { // move -> store
354 unsigned InReg = MI->getOperand(1).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000355 NewMI = addFrameReference(BuildMI(TII.get(PPC::STW)).addReg(InReg),
356 FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000357 } else { // move -> load
Chris Lattnerf38df042005-09-09 21:46:49 +0000358 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000359 NewMI = addFrameReference(BuildMI(TII.get(PPC::LWZ), OutReg),
360 FrameIndex);
Chris Lattnerf38df042005-09-09 21:46:49 +0000361 }
Nate Begeman1d9d7422005-10-18 00:28:58 +0000362 } else if ((Opc == PPC::OR8 &&
363 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
364 if (OpNum == 0) { // move -> store
365 unsigned InReg = MI->getOperand(1).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000366 NewMI = addFrameReference(BuildMI(TII.get(PPC::STD)).addReg(InReg),
367 FrameIndex);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000368 } else { // move -> load
369 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000370 NewMI = addFrameReference(BuildMI(TII.get(PPC::LD), OutReg), FrameIndex);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000371 }
Chris Lattner919c0322005-10-01 01:35:02 +0000372 } else if (Opc == PPC::FMRD) {
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000373 if (OpNum == 0) { // move -> store
374 unsigned InReg = MI->getOperand(1).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000375 NewMI = addFrameReference(BuildMI(TII.get(PPC::STFD)).addReg(InReg),
376 FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000377 } else { // move -> load
378 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000379 NewMI = addFrameReference(BuildMI(TII.get(PPC::LFD), OutReg), FrameIndex);
Chris Lattnerc9fe7502005-09-09 21:59:44 +0000380 }
Chris Lattner919c0322005-10-01 01:35:02 +0000381 } else if (Opc == PPC::FMRS) {
382 if (OpNum == 0) { // move -> store
383 unsigned InReg = MI->getOperand(1).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000384 NewMI = addFrameReference(BuildMI(TII.get(PPC::STFS)).addReg(InReg),
385 FrameIndex);
Chris Lattner919c0322005-10-01 01:35:02 +0000386 } else { // move -> load
387 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chengc0f64ff2006-11-27 23:37:22 +0000388 NewMI = addFrameReference(BuildMI(TII.get(PPC::LFS), OutReg), FrameIndex);
Chris Lattner919c0322005-10-01 01:35:02 +0000389 }
Chris Lattnerf38df042005-09-09 21:46:49 +0000390 }
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000391
392 if (NewMI)
393 NewMI->copyKillDeadInfo(MI);
394 return NewMI;
Chris Lattnerf38df042005-09-09 21:46:49 +0000395}
396
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000397//===----------------------------------------------------------------------===//
398// Stack Frame Processing methods
399//===----------------------------------------------------------------------===//
400
Jim Laskey2f616bf2006-11-16 22:43:37 +0000401// needsFP - Return true if the specified function should have a dedicated frame
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000402// pointer register. This is true if the function has variable sized allocas or
403// if frame pointer elimination is disabled.
404//
Jim Laskey2f616bf2006-11-16 22:43:37 +0000405static bool needsFP(const MachineFunction &MF) {
406 const MachineFrameInfo *MFI = MF.getFrameInfo();
407 return NoFramePointerElim || MFI->hasVarSizedObjects();
408}
409
410// hasFP - Return true if the specified function actually has a dedicated frame
411// pointer register. This is true if the function needs a frame pointer and has
412// a non-zero stack size.
Evan Chengdc775402007-01-23 00:57:47 +0000413bool PPCRegisterInfo::hasFP(const MachineFunction &MF) const {
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000414 const MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000415 return MFI->getStackSize() && needsFP(MF);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000416}
417
Jim Laskey51fe9d92006-12-06 17:42:06 +0000418/// usesLR - Returns if the link registers (LR) has been used in the function.
419///
420bool PPCRegisterInfo::usesLR(MachineFunction &MF) const {
421 const bool *PhysRegsUsed = MF.getUsedPhysregs();
422 return PhysRegsUsed[getRARegister()];
423}
424
Nate Begeman21e463b2005-10-16 05:39:50 +0000425void PPCRegisterInfo::
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000426eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
427 MachineBasicBlock::iterator I) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000428 // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000429 MBB.erase(I);
430}
431
Jim Laskey2f616bf2006-11-16 22:43:37 +0000432/// LowerDynamicAlloc - Generate the code for allocating an object in the
433/// current frame. The sequence of code with be in the general form
434///
435/// addi R0, SP, #frameSize ; get the address of the previous frame
436/// stwxu R0, SP, Rnegsize ; add and update the SP with the negated size
437/// addi Rnew, SP, #maxCalFrameSize ; get the top of the allocation
438///
439void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const {
440 // Get the instruction.
441 MachineInstr &MI = *II;
442 // Get the instruction's basic block.
443 MachineBasicBlock &MBB = *MI.getParent();
444 // Get the basic block's function.
445 MachineFunction &MF = *MBB.getParent();
446 // Get the frame info.
447 MachineFrameInfo *MFI = MF.getFrameInfo();
448 // Determine whether 64-bit pointers are used.
449 bool LP64 = Subtarget.isPPC64();
450
451 // Determine the maximum call stack size. maxCallFrameSize may be
452 // less than the minimum.
453 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000454 // Get the total frame size.
455 unsigned FrameSize = MFI->getStackSize();
456
457 // Get stack alignments.
458 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
459 unsigned MaxAlign = MFI->getMaxAlignment();
Jim Laskeyd6fa8c12006-11-17 18:49:39 +0000460 assert(MaxAlign <= TargetAlign &&
461 "Dynamic alloca with large aligns not supported");
Jim Laskey2f616bf2006-11-16 22:43:37 +0000462
463 // Determine the previous frame's address. If FrameSize can't be
464 // represented as 16 bits or we need special alignment, then we load the
465 // previous frame's address from 0(SP). Why not do an addis of the hi?
466 // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
467 // Constructing the constant and adding would take 3 instructions.
468 // Fortunately, a frame greater than 32K is rare.
469 if (MaxAlign < TargetAlign && isInt16(FrameSize)) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000470 BuildMI(MBB, II, TII.get(PPC::ADDI), PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000471 .addReg(PPC::R31)
472 .addImm(FrameSize);
473 } else if (LP64) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000474 BuildMI(MBB, II, TII.get(PPC::LD), PPC::X0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000475 .addImm(0)
476 .addReg(PPC::X1);
477 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000478 BuildMI(MBB, II, TII.get(PPC::LWZ), PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000479 .addImm(0)
480 .addReg(PPC::R1);
481 }
482
483 // Grow the stack and update the stack pointer link, then
484 // determine the address of new allocated space.
485 if (LP64) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000486 BuildMI(MBB, II, TII.get(PPC::STDUX))
Jim Laskey2f616bf2006-11-16 22:43:37 +0000487 .addReg(PPC::X0)
488 .addReg(PPC::X1)
489 .addReg(MI.getOperand(1).getReg());
Evan Chengc0f64ff2006-11-27 23:37:22 +0000490 BuildMI(MBB, II, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
Jim Laskey2f616bf2006-11-16 22:43:37 +0000491 .addReg(PPC::X1)
492 .addImm(maxCallFrameSize);
493 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000494 BuildMI(MBB, II, TII.get(PPC::STWUX))
Jim Laskey2f616bf2006-11-16 22:43:37 +0000495 .addReg(PPC::R0)
496 .addReg(PPC::R1)
497 .addReg(MI.getOperand(1).getReg());
Evan Chengc0f64ff2006-11-27 23:37:22 +0000498 BuildMI(MBB, II, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
Jim Laskey2f616bf2006-11-16 22:43:37 +0000499 .addReg(PPC::R1)
500 .addImm(maxCallFrameSize);
501 }
502
503 // Discard the DYNALLOC instruction.
504 MBB.erase(II);
505}
506
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000507void
Nate Begeman21e463b2005-10-16 05:39:50 +0000508PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000509 // Get the instruction.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000510 MachineInstr &MI = *II;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000511 // Get the instruction's basic block.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000512 MachineBasicBlock &MBB = *MI.getParent();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000513 // Get the basic block's function.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000514 MachineFunction &MF = *MBB.getParent();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000515 // Get the frame info.
516 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000517
Jim Laskey2f616bf2006-11-16 22:43:37 +0000518 // Find out which operand is the frame index.
519 unsigned i = 0;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000520 while (!MI.getOperand(i).isFrameIndex()) {
521 ++i;
522 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
523 }
Jim Laskey2f616bf2006-11-16 22:43:37 +0000524 // Take into account whether it's an add or mem instruction
525 unsigned OffIdx = (i == 2) ? 1 : 2;
526 // Get the frame index.
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000527 int FrameIndex = MI.getOperand(i).getFrameIndex();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000528
529 // Get the frame pointer save index. Users of this index are primarily
530 // DYNALLOC instructions.
531 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
532 int FPSI = FI->getFramePointerSaveIndex();
533 // Get the instruction opcode.
534 unsigned OpC = MI.getOpcode();
535
536 // Special case for dynamic alloca.
537 if (FPSI && FrameIndex == FPSI &&
538 (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
539 lowerDynamicAlloc(II);
540 return;
541 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000542
543 // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
Chris Lattner09e46062006-09-05 02:31:13 +0000544 MI.getOperand(i).ChangeToRegister(hasFP(MF) ? PPC::R31 : PPC::R1, false);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000545
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000546 // Figure out if the offset in the instruction is shifted right two bits. This
547 // is true for instructions like "STD", which the machine implicitly adds two
548 // low zeros to.
549 bool isIXAddr = false;
Jim Laskey2f616bf2006-11-16 22:43:37 +0000550 switch (OpC) {
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000551 case PPC::LWA:
552 case PPC::LD:
553 case PPC::STD:
554 case PPC::STD_32:
555 isIXAddr = true;
556 break;
557 }
558
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000559 // Now add the frame object offset to the offset from r1.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000560 int Offset = MFI->getObjectOffset(FrameIndex);
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000561
562 if (!isIXAddr)
563 Offset += MI.getOperand(OffIdx).getImmedValue();
564 else
565 Offset += MI.getOperand(OffIdx).getImmedValue() << 2;
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000566
567 // If we're not using a Frame Pointer that has been set to the value of the
568 // SP before having the stack size subtracted from it, then add the stack size
569 // to Offset to get the correct offset.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000570 Offset += MFI->getStackSize();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000571
Jim Laskey2f616bf2006-11-16 22:43:37 +0000572 if (!isInt16(Offset)) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000573 // Insert a set of r0 with the full offset value before the ld, st, or add
Evan Chengc0f64ff2006-11-27 23:37:22 +0000574 BuildMI(MBB, II, TII.get(PPC::LIS), PPC::R0).addImm(Offset >> 16);
575 BuildMI(MBB, II, TII.get(PPC::ORI), PPC::R0).addReg(PPC::R0).addImm(Offset);
Chris Lattnerc6d48d32006-01-11 23:07:57 +0000576
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000577 // convert into indexed form of the instruction
578 // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
579 // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
Jim Laskey2f616bf2006-11-16 22:43:37 +0000580 assert(ImmToIdxMap.count(OpC) &&
Chris Lattner14630192005-09-09 20:51:08 +0000581 "No indexed form of load or store available!");
Jim Laskey2f616bf2006-11-16 22:43:37 +0000582 unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
Evan Cheng12a44782006-11-30 07:12:03 +0000583 MI.setInstrDescriptor(TII.get(NewOpcode));
Chris Lattner09e46062006-09-05 02:31:13 +0000584 MI.getOperand(1).ChangeToRegister(MI.getOperand(i).getReg(), false);
585 MI.getOperand(2).ChangeToRegister(PPC::R0, false);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000586 } else {
Chris Lattner7ffa9ab2006-06-27 18:55:49 +0000587 if (isIXAddr) {
Chris Lattner841d12d2005-10-18 16:51:22 +0000588 assert((Offset & 3) == 0 && "Invalid frame offset!");
589 Offset >>= 2; // The actual encoded value has the low two bits zero.
Chris Lattner841d12d2005-10-18 16:51:22 +0000590 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000591 MI.getOperand(OffIdx).ChangeToImmediate(Offset);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000592 }
593}
594
Chris Lattnerf7d23722006-04-17 20:59:25 +0000595/// VRRegNo - Map from a numbered VR register to its enum value.
596///
597static const unsigned short VRRegNo[] = {
Chris Lattnerb47e0892006-06-12 21:50:57 +0000598 PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
599 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, PPC::V12, PPC::V13, PPC::V14, PPC::V15,
Chris Lattnerf7d23722006-04-17 20:59:25 +0000600 PPC::V16, PPC::V17, PPC::V18, PPC::V19, PPC::V20, PPC::V21, PPC::V22, PPC::V23,
601 PPC::V24, PPC::V25, PPC::V26, PPC::V27, PPC::V28, PPC::V29, PPC::V30, PPC::V31
602};
603
Chris Lattnerf9568d82006-04-17 21:48:13 +0000604/// RemoveVRSaveCode - We have found that this function does not need any code
605/// to manipulate the VRSAVE register, even though it uses vector registers.
606/// This can happen when the only registers used are known to be live in or out
607/// of the function. Remove all of the VRSAVE related code from the function.
608static void RemoveVRSaveCode(MachineInstr *MI) {
609 MachineBasicBlock *Entry = MI->getParent();
610 MachineFunction *MF = Entry->getParent();
611
612 // We know that the MTVRSAVE instruction immediately follows MI. Remove it.
613 MachineBasicBlock::iterator MBBI = MI;
614 ++MBBI;
615 assert(MBBI != Entry->end() && MBBI->getOpcode() == PPC::MTVRSAVE);
616 MBBI->eraseFromParent();
617
618 bool RemovedAllMTVRSAVEs = true;
619 // See if we can find and remove the MTVRSAVE instruction from all of the
620 // epilog blocks.
621 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
622 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) {
623 // If last instruction is a return instruction, add an epilogue
624 if (!I->empty() && TII.isReturn(I->back().getOpcode())) {
625 bool FoundIt = false;
626 for (MBBI = I->end(); MBBI != I->begin(); ) {
627 --MBBI;
628 if (MBBI->getOpcode() == PPC::MTVRSAVE) {
629 MBBI->eraseFromParent(); // remove it.
630 FoundIt = true;
631 break;
632 }
633 }
634 RemovedAllMTVRSAVEs &= FoundIt;
635 }
636 }
637
638 // If we found and removed all MTVRSAVE instructions, remove the read of
639 // VRSAVE as well.
640 if (RemovedAllMTVRSAVEs) {
641 MBBI = MI;
642 assert(MBBI != Entry->begin() && "UPDATE_VRSAVE is first instr in block?");
643 --MBBI;
644 assert(MBBI->getOpcode() == PPC::MFVRSAVE && "VRSAVE instrs wandered?");
645 MBBI->eraseFromParent();
646 }
647
648 // Finally, nuke the UPDATE_VRSAVE.
649 MI->eraseFromParent();
650}
651
Chris Lattner1877ec92006-03-13 21:52:10 +0000652// HandleVRSaveUpdate - MI is the UPDATE_VRSAVE instruction introduced by the
653// instruction selector. Based on the vector registers that have been used,
654// transform this into the appropriate ORI instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000655static void HandleVRSaveUpdate(MachineInstr *MI, const bool *UsedRegs,
656 const TargetInstrInfo &TII) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000657 unsigned UsedRegMask = 0;
Chris Lattnerf7d23722006-04-17 20:59:25 +0000658 for (unsigned i = 0; i != 32; ++i)
659 if (UsedRegs[VRRegNo[i]])
660 UsedRegMask |= 1 << (31-i);
661
Chris Lattner402504b2006-04-17 21:22:06 +0000662 // Live in and live out values already must be in the mask, so don't bother
663 // marking them.
664 MachineFunction *MF = MI->getParent()->getParent();
665 for (MachineFunction::livein_iterator I =
666 MF->livein_begin(), E = MF->livein_end(); I != E; ++I) {
667 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(I->first);
668 if (VRRegNo[RegNo] == I->first) // If this really is a vector reg.
669 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
670 }
671 for (MachineFunction::liveout_iterator I =
672 MF->liveout_begin(), E = MF->liveout_end(); I != E; ++I) {
673 unsigned RegNo = PPCRegisterInfo::getRegisterNumbering(*I);
674 if (VRRegNo[RegNo] == *I) // If this really is a vector reg.
675 UsedRegMask &= ~(1 << (31-RegNo)); // Doesn't need to be marked.
676 }
677
Chris Lattner1877ec92006-03-13 21:52:10 +0000678 unsigned SrcReg = MI->getOperand(1).getReg();
679 unsigned DstReg = MI->getOperand(0).getReg();
680 // If no registers are used, turn this into a copy.
681 if (UsedRegMask == 0) {
Chris Lattnerf9568d82006-04-17 21:48:13 +0000682 // Remove all VRSAVE code.
683 RemoveVRSaveCode(MI);
684 return;
Chris Lattner1877ec92006-03-13 21:52:10 +0000685 } else if ((UsedRegMask & 0xFFFF) == UsedRegMask) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000686 BuildMI(*MI->getParent(), MI, TII.get(PPC::ORI), DstReg)
Chris Lattner1877ec92006-03-13 21:52:10 +0000687 .addReg(SrcReg).addImm(UsedRegMask);
688 } else if ((UsedRegMask & 0xFFFF0000) == UsedRegMask) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000689 BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
Chris Lattner1877ec92006-03-13 21:52:10 +0000690 .addReg(SrcReg).addImm(UsedRegMask >> 16);
691 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000692 BuildMI(*MI->getParent(), MI, TII.get(PPC::ORIS), DstReg)
Chris Lattner1877ec92006-03-13 21:52:10 +0000693 .addReg(SrcReg).addImm(UsedRegMask >> 16);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000694 BuildMI(*MI->getParent(), MI, TII.get(PPC::ORI), DstReg)
Chris Lattner1877ec92006-03-13 21:52:10 +0000695 .addReg(DstReg).addImm(UsedRegMask & 0xFFFF);
696 }
697
698 // Remove the old UPDATE_VRSAVE instruction.
Chris Lattnerf9568d82006-04-17 21:48:13 +0000699 MI->eraseFromParent();
Chris Lattner1877ec92006-03-13 21:52:10 +0000700}
701
Jim Laskey2f616bf2006-11-16 22:43:37 +0000702/// determineFrameLayout - Determine the size of the frame and maximum call
703/// frame size.
704void PPCRegisterInfo::determineFrameLayout(MachineFunction &MF) const {
705 MachineFrameInfo *MFI = MF.getFrameInfo();
706
707 // Get the number of bytes to allocate from the FrameInfo
708 unsigned FrameSize = MFI->getStackSize();
709
710 // Get the alignments provided by the target, and the maximum alignment
711 // (if any) of the fixed frame objects.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000712 unsigned MaxAlign = MFI->getMaxAlignment();
Evan Cheng99403b62007-01-25 22:25:04 +0000713 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
714 unsigned AlignMask = TargetAlign - 1; //
Jim Laskey2f616bf2006-11-16 22:43:37 +0000715
716 // If we are a leaf function, and use up to 224 bytes of stack space,
717 // don't have a frame pointer, calls, or dynamic alloca then we do not need
718 // to adjust the stack pointer (we fit in the Red Zone).
719 if (FrameSize <= 224 && // Fits in red zone.
Jim Laskey2ff5cdb2006-11-17 16:09:31 +0000720 !MFI->hasVarSizedObjects() && // No dynamic alloca.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000721 !MFI->hasCalls() && // No calls.
722 MaxAlign <= TargetAlign) { // No special alignment.
723 // No need for frame
724 MFI->setStackSize(0);
725 return;
726 }
727
728 // Get the maximum call frame size of all the calls.
729 unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
730
731 // Maximum call frame needs to be at least big enough for linkage and 8 args.
732 unsigned minCallFrameSize =
733 PPCFrameInfo::getMinCallFrameSize(Subtarget.isPPC64());
734 maxCallFrameSize = std::max(maxCallFrameSize, minCallFrameSize);
735
736 // If we have dynamic alloca then maxCallFrameSize needs to be aligned so
737 // that allocations will be aligned.
738 if (MFI->hasVarSizedObjects())
739 maxCallFrameSize = (maxCallFrameSize + AlignMask) & ~AlignMask;
740
741 // Update maximum call frame size.
742 MFI->setMaxCallFrameSize(maxCallFrameSize);
743
744 // Include call frame size in total.
745 FrameSize += maxCallFrameSize;
746
747 // Make sure the frame is aligned.
748 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
749
750 // Update frame info.
751 MFI->setStackSize(FrameSize);
752}
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000753
Nate Begeman21e463b2005-10-16 05:39:50 +0000754void PPCRegisterInfo::emitPrologue(MachineFunction &MF) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000755 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
756 MachineBasicBlock::iterator MBBI = MBB.begin();
757 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Laskey41886992006-04-07 16:34:46 +0000758 MachineDebugInfo *DebugInfo = MFI->getMachineDebugInfo();
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000759
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000760 // Prepare for debug frame info.
761 bool hasInfo = DebugInfo && DebugInfo->hasInfo();
762 unsigned FrameLabelId = 0;
763
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000764 // Scan the prolog, looking for an UPDATE_VRSAVE instruction. If we find it,
765 // process it.
Chris Lattner8aa777d2006-03-16 21:31:45 +0000766 for (unsigned i = 0; MBBI != MBB.end(); ++i, ++MBBI) {
Chris Lattner1877ec92006-03-13 21:52:10 +0000767 if (MBBI->getOpcode() == PPC::UPDATE_VRSAVE) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000768 HandleVRSaveUpdate(MBBI, MF.getUsedPhysregs(), TII);
Chris Lattner1877ec92006-03-13 21:52:10 +0000769 break;
770 }
771 }
772
773 // Move MBBI back to the beginning of the function.
774 MBBI = MBB.begin();
775
Jim Laskey2f616bf2006-11-16 22:43:37 +0000776 // Work out frame sizes.
777 determineFrameLayout(MF);
778 unsigned FrameSize = MFI->getStackSize();
Nate Begemanae232e72005-11-06 09:00:38 +0000779
Jim Laskey2f616bf2006-11-16 22:43:37 +0000780 // Skip if a leaf routine.
781 if (!FrameSize) return;
782
783 int NegFrameSize = -FrameSize;
Jim Laskey51fe9d92006-12-06 17:42:06 +0000784
785 // Get processor type.
786 bool IsPPC64 = Subtarget.isPPC64();
787 // Check if the link register (LR) has been used.
788 bool UsesLR = MFI->hasCalls() || usesLR(MF);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000789 // Do we have a frame pointer for this function?
790 bool HasFP = hasFP(MF);
Jim Laskey51fe9d92006-12-06 17:42:06 +0000791
792 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64);
793 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64);
794
795 if (IsPPC64) {
796 if (UsesLR)
797 BuildMI(MBB, MBBI, TII.get(PPC::MFLR8), PPC::X0);
798
799 if (HasFP)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000800 BuildMI(MBB, MBBI, TII.get(PPC::STD))
Jim Laskey51fe9d92006-12-06 17:42:06 +0000801 .addReg(PPC::X31).addImm(FPOffset/4).addReg(PPC::X1);
802
803 if (UsesLR)
804 BuildMI(MBB, MBBI, TII.get(PPC::STD))
805 .addReg(PPC::X0).addImm(LROffset/4).addReg(PPC::X1);
806 } else {
807 if (UsesLR)
808 BuildMI(MBB, MBBI, TII.get(PPC::MFLR), PPC::R0);
809
810 if (HasFP)
811 BuildMI(MBB, MBBI, TII.get(PPC::STW))
812 .addReg(PPC::R31).addImm(FPOffset).addReg(PPC::R1);
813
814 if (UsesLR)
815 BuildMI(MBB, MBBI, TII.get(PPC::STW))
816 .addReg(PPC::R0).addImm(LROffset).addReg(PPC::R1);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000817 }
818
819 // Get stack alignments.
Nate Begemanae232e72005-11-06 09:00:38 +0000820 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
821 unsigned MaxAlign = MFI->getMaxAlignment();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000822
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000823 if (hasInfo) {
824 // Mark effective beginning of when frame pointer becomes valid.
825 FrameLabelId = DebugInfo->NextLabelID();
826 BuildMI(MBB, MBBI, TII.get(PPC::DWARF_LABEL)).addImm(FrameLabelId);
827 }
828
Jim Laskey2f616bf2006-11-16 22:43:37 +0000829 // Adjust stack pointer: r1 += NegFrameSize.
Nate Begeman030514c2006-04-11 19:29:21 +0000830 // If there is a preferred stack alignment, align R1 now
Jim Laskey51fe9d92006-12-06 17:42:06 +0000831 if (!IsPPC64) {
Chris Lattnera94a2032006-11-11 19:05:28 +0000832 // PPC32.
833 if (MaxAlign > TargetAlign) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000834 assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
835 assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000836 BuildMI(MBB, MBBI, TII.get(PPC::RLWINM), PPC::R0)
Chris Lattnera94a2032006-11-11 19:05:28 +0000837 .addReg(PPC::R1).addImm(0).addImm(32-Log2_32(MaxAlign)).addImm(31);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000838 BuildMI(MBB, MBBI, TII.get(PPC::SUBFIC) ,PPC::R0).addReg(PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000839 .addImm(NegFrameSize);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000840 BuildMI(MBB, MBBI, TII.get(PPC::STWUX))
Chris Lattnera94a2032006-11-11 19:05:28 +0000841 .addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000842 } else if (isInt16(NegFrameSize)) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000843 BuildMI(MBB, MBBI, TII.get(PPC::STWU),
Jim Laskey2f616bf2006-11-16 22:43:37 +0000844 PPC::R1).addReg(PPC::R1).addImm(NegFrameSize).addReg(PPC::R1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000845 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000846 BuildMI(MBB, MBBI, TII.get(PPC::LIS), PPC::R0).addImm(NegFrameSize >> 16);
847 BuildMI(MBB, MBBI, TII.get(PPC::ORI), PPC::R0).addReg(PPC::R0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000848 .addImm(NegFrameSize & 0xFFFF);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000849 BuildMI(MBB, MBBI, TII.get(PPC::STWUX)).addReg(PPC::R1).addReg(PPC::R1)
Chris Lattnera94a2032006-11-11 19:05:28 +0000850 .addReg(PPC::R0);
851 }
852 } else { // PPC64.
853 if (MaxAlign > TargetAlign) {
Jim Laskey2f616bf2006-11-16 22:43:37 +0000854 assert(isPowerOf2_32(MaxAlign)&&isInt16(MaxAlign)&&"Invalid alignment!");
855 assert(isInt16(NegFrameSize) && "Unhandled stack size and alignment!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000856 BuildMI(MBB, MBBI, TII.get(PPC::RLDICL), PPC::X0)
Chris Lattnera94a2032006-11-11 19:05:28 +0000857 .addReg(PPC::X1).addImm(0).addImm(64-Log2_32(MaxAlign));
Evan Chengc0f64ff2006-11-27 23:37:22 +0000858 BuildMI(MBB, MBBI, TII.get(PPC::SUBFIC8), PPC::X0).addReg(PPC::X0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000859 .addImm(NegFrameSize);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000860 BuildMI(MBB, MBBI, TII.get(PPC::STDUX))
Chris Lattnera94a2032006-11-11 19:05:28 +0000861 .addReg(PPC::X1).addReg(PPC::X1).addReg(PPC::X0);
Jim Laskey2ff5cdb2006-11-17 16:09:31 +0000862 } else if (isInt16(NegFrameSize)) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000863 BuildMI(MBB, MBBI, TII.get(PPC::STDU), PPC::X1)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000864 .addReg(PPC::X1).addImm(NegFrameSize/4).addReg(PPC::X1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000865 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000866 BuildMI(MBB, MBBI, TII.get(PPC::LIS8), PPC::X0).addImm(NegFrameSize >>16);
867 BuildMI(MBB, MBBI, TII.get(PPC::ORI8), PPC::X0).addReg(PPC::X0)
Jim Laskey2f616bf2006-11-16 22:43:37 +0000868 .addImm(NegFrameSize & 0xFFFF);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000869 BuildMI(MBB, MBBI, TII.get(PPC::STDUX)).addReg(PPC::X1).addReg(PPC::X1)
Chris Lattnera94a2032006-11-11 19:05:28 +0000870 .addReg(PPC::X0);
871 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000872 }
Nate Begemanae232e72005-11-06 09:00:38 +0000873
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000874 if (hasInfo) {
875 std::vector<MachineMove> &Moves = DebugInfo->getFrameMoves();
Jim Laskey41886992006-04-07 16:34:46 +0000876
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000877 if (NegFrameSize) {
878 // Show update of SP.
879 MachineLocation SPDst(MachineLocation::VirtualFP);
880 MachineLocation SPSrc(MachineLocation::VirtualFP, NegFrameSize);
881 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
882 } else {
883 MachineLocation SP(IsPPC64 ? PPC::X31 : PPC::R31);
884 Moves.push_back(MachineMove(FrameLabelId, SP, SP));
885 }
Jim Laskey4c2c9032006-08-25 19:40:59 +0000886
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000887 if (HasFP) {
888 MachineLocation FPDst(MachineLocation::VirtualFP, FPOffset);
889 MachineLocation FPSrc(IsPPC64 ? PPC::X31 : PPC::R31);
890 Moves.push_back(MachineMove(FrameLabelId, FPDst, FPSrc));
891 }
Jim Laskeyce50a162006-08-29 16:24:26 +0000892
893 // Add callee saved registers to move list.
894 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
895 for (unsigned I = 0, E = CSI.size(); I != E; ++I) {
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000896 int Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
897 unsigned Reg = CSI[I].getReg();
898 if (Reg == PPC::LR || Reg == PPC::LR8) continue;
899 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
900 MachineLocation CSSrc(Reg);
901 Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc));
Jim Laskeyce50a162006-08-29 16:24:26 +0000902 }
Jim Laskey5e73d5b2007-01-24 18:45:13 +0000903
904 // Mark effective beginning of when frame pointer is ready.
905 unsigned ReadyLabelId = DebugInfo->NextLabelID();
906 BuildMI(MBB, MBBI, TII.get(PPC::DWARF_LABEL)).addImm(ReadyLabelId);
907
908 MachineLocation FPDst(HasFP ? (IsPPC64 ? PPC::X31 : PPC::R31) :
909 (IsPPC64 ? PPC::X1 : PPC::R1));
910 MachineLocation FPSrc(MachineLocation::VirtualFP);
911 Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
Jim Laskey41886992006-04-07 16:34:46 +0000912 }
Jim Laskey2f616bf2006-11-16 22:43:37 +0000913
914 // If there is a frame pointer, copy R1 into R31
Chris Lattner4f91a4c2006-04-03 22:03:29 +0000915 if (HasFP) {
Jim Laskey51fe9d92006-12-06 17:42:06 +0000916 if (!IsPPC64) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000917 BuildMI(MBB, MBBI, TII.get(PPC::OR), PPC::R31).addReg(PPC::R1)
918 .addReg(PPC::R1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000919 } else {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000920 BuildMI(MBB, MBBI, TII.get(PPC::OR8), PPC::X31).addReg(PPC::X1)
921 .addReg(PPC::X1);
Chris Lattnera94a2032006-11-11 19:05:28 +0000922 }
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000923 }
924}
925
Nate Begeman21e463b2005-10-16 05:39:50 +0000926void PPCRegisterInfo::emitEpilogue(MachineFunction &MF,
927 MachineBasicBlock &MBB) const {
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000928 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Evan Cheng6da8d992006-01-09 18:28:21 +0000929 assert(MBBI->getOpcode() == PPC::BLR &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000930 "Can only insert epilog into returning blocks");
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000931
Nate Begeman030514c2006-04-11 19:29:21 +0000932 // Get alignment info so we know how to restore r1
933 const MachineFrameInfo *MFI = MF.getFrameInfo();
934 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
Jim Laskey2f616bf2006-11-16 22:43:37 +0000935 unsigned MaxAlign = MFI->getMaxAlignment();
Nate Begeman030514c2006-04-11 19:29:21 +0000936
Chris Lattner64da1722006-01-11 23:03:54 +0000937 // Get the number of bytes allocated from the FrameInfo.
Jim Laskey2f616bf2006-11-16 22:43:37 +0000938 unsigned FrameSize = MFI->getStackSize();
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000939
Jim Laskey51fe9d92006-12-06 17:42:06 +0000940 if (!FrameSize) return;
941
942 // Get processor type.
943 bool IsPPC64 = Subtarget.isPPC64();
944 // Check if the link register (LR) has been used.
945 bool UsesLR = MFI->hasCalls() || usesLR(MF);
946 // Do we have a frame pointer for this function?
947 bool HasFP = hasFP(MF);
948
949 // The loaded (or persistent) stack pointer value is offset by the 'stwu'
950 // on entry to the function. Add this offset back now.
951 if (!Subtarget.isPPC64()) {
952 if (isInt16(FrameSize) && TargetAlign >= MaxAlign &&
953 !MFI->hasVarSizedObjects()) {
954 BuildMI(MBB, MBBI, TII.get(PPC::ADDI), PPC::R1)
955 .addReg(PPC::R1).addImm(FrameSize);
Chris Lattner64da1722006-01-11 23:03:54 +0000956 } else {
Jim Laskey51fe9d92006-12-06 17:42:06 +0000957 BuildMI(MBB, MBBI, TII.get(PPC::LWZ),PPC::R1).addImm(0).addReg(PPC::R1);
Chris Lattner64da1722006-01-11 23:03:54 +0000958 }
Jim Laskey51fe9d92006-12-06 17:42:06 +0000959 } else {
960 if (isInt16(FrameSize) && TargetAlign >= MaxAlign &&
961 !MFI->hasVarSizedObjects()) {
962 BuildMI(MBB, MBBI, TII.get(PPC::ADDI8), PPC::X1)
963 .addReg(PPC::X1).addImm(FrameSize);
964 } else {
965 BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X1).addImm(0).addReg(PPC::X1);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000966 }
Jim Laskey51fe9d92006-12-06 17:42:06 +0000967 }
968
969 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64);
970 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64);
971
972 if (IsPPC64) {
973 if (UsesLR)
974 BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X0)
975 .addImm(LROffset/4).addReg(PPC::X1);
976
977 if (HasFP)
978 BuildMI(MBB, MBBI, TII.get(PPC::LD), PPC::X31)
979 .addImm(FPOffset/4).addReg(PPC::X1);
980
981 if (UsesLR)
982 BuildMI(MBB, MBBI, TII.get(PPC::MTLR8)).addReg(PPC::X0);
983 } else {
984 if (UsesLR)
985 BuildMI(MBB, MBBI, TII.get(PPC::LWZ), PPC::R0)
986 .addImm(LROffset).addReg(PPC::R1);
987
988 if (HasFP)
989 BuildMI(MBB, MBBI, TII.get(PPC::LWZ), PPC::R31)
990 .addImm(FPOffset).addReg(PPC::R1);
991
992 if (UsesLR)
993 BuildMI(MBB, MBBI, TII.get(PPC::MTLR)).addReg(PPC::R0);
Misha Brukmanf2ccb772004-08-17 04:55:41 +0000994 }
995}
996
Jim Laskey41886992006-04-07 16:34:46 +0000997unsigned PPCRegisterInfo::getRARegister() const {
Chris Lattner6a5339b2006-11-14 18:44:47 +0000998 return !Subtarget.isPPC64() ? PPC::LR : PPC::LR8;
999
Jim Laskey41886992006-04-07 16:34:46 +00001000}
1001
Jim Laskeya9979182006-03-28 13:48:33 +00001002unsigned PPCRegisterInfo::getFrameRegister(MachineFunction &MF) const {
Chris Lattnera94a2032006-11-11 19:05:28 +00001003 if (!Subtarget.isPPC64())
1004 return hasFP(MF) ? PPC::R31 : PPC::R1;
1005 else
1006 return hasFP(MF) ? PPC::X31 : PPC::X1;
Jim Laskey41886992006-04-07 16:34:46 +00001007}
1008
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001009void PPCRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
Jim Laskey41886992006-04-07 16:34:46 +00001010 const {
Jim Laskey4c2c9032006-08-25 19:40:59 +00001011 // Initial state of the frame pointer is R1.
Jim Laskey41886992006-04-07 16:34:46 +00001012 MachineLocation Dst(MachineLocation::VirtualFP);
1013 MachineLocation Src(PPC::R1, 0);
Jim Laskey5e73d5b2007-01-24 18:45:13 +00001014 Moves.push_back(MachineMove(0, Dst, Src));
Jim Laskeyf1d78e82006-03-23 18:12:57 +00001015}
1016
Chris Lattner4c7b43b2005-10-14 23:37:35 +00001017#include "PPCGenRegisterInfo.inc"
Misha Brukmanf2ccb772004-08-17 04:55:41 +00001018