blob: 5ad7b1962175f6545e8493c9b4a022ef4807275a [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaRegisterInfo.cpp - Alpha Register Information -------*- C++ -*-===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +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 Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
Andrew Lenharth2d6f0222005-01-24 19:44:07 +000010// This file contains the Alpha implementation of the MRegisterInfo class.
Andrew Lenharth304d0f32005-01-22 23:41:55 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "reginfo"
15#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000016#include "AlphaRegisterInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/Type.h"
Andrew Lenharthc24b5372005-04-13 17:17:28 +000019#include "llvm/Function.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/CodeGen/ValueTypes.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyf1d78e82006-03-23 18:12:57 +000024#include "llvm/CodeGen/MachineLocation.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000025#include "llvm/Target/TargetFrameInfo.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetOptions.h"
Evan Chengc0f64ff2006-11-27 23:37:22 +000028#include "llvm/Target/TargetInstrInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/ADT/STLExtras.h"
32#include <cstdlib>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000035//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +000036static const int IMM_LOW = -32768;
37static const int IMM_HIGH = 32767;
Andrew Lenharth3ee60412005-03-05 15:30:33 +000038static const int IMM_MULT = 65536;
Andrew Lenharth032f2352005-02-22 21:59:48 +000039
40static long getUpper16(long l)
41{
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000042 long y = l / IMM_MULT;
43 if (l % IMM_MULT > IMM_HIGH)
Andrew Lenharth032f2352005-02-22 21:59:48 +000044 ++y;
45 return y;
46}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000048static long getLower16(long l)
49{
50 long h = getUpper16(l);
51 return l - h * IMM_MULT;
52}
53
Evan Cheng7ce45782006-11-13 23:36:35 +000054AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
55 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
56 TII(tii)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057{
58}
59
Misha Brukman4633f1c2005-04-21 23:13:11 +000060void
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
62 MachineBasicBlock::iterator MI,
Chris Lattner97d5e642005-09-30 01:29:42 +000063 unsigned SrcReg, int FrameIdx,
64 const TargetRegisterClass *RC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +000065 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
66 // << FrameIdx << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000068 if (RC == Alpha::F4RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000069 BuildMI(MBB, MI, TII.get(Alpha::STS))
Chris Lattner09e46062006-09-05 02:31:13 +000070 .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000071 else if (RC == Alpha::F8RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000072 BuildMI(MBB, MI, TII.get(Alpha::STT))
Chris Lattner09e46062006-09-05 02:31:13 +000073 .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000074 else if (RC == Alpha::GPRCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000075 BuildMI(MBB, MI, TII.get(Alpha::STQ))
Chris Lattner09e46062006-09-05 02:31:13 +000076 .addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2a8fc232005-02-01 20:35:57 +000077 else
78 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000079}
80
81void
82AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
83 MachineBasicBlock::iterator MI,
Chris Lattner97d5e642005-09-30 01:29:42 +000084 unsigned DestReg, int FrameIdx,
85 const TargetRegisterClass *RC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +000086 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
87 // << FrameIdx << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000088 if (RC == Alpha::F4RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000089 BuildMI(MBB, MI, TII.get(Alpha::LDS), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +000090 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000091 else if (RC == Alpha::F8RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000092 BuildMI(MBB, MI, TII.get(Alpha::LDT), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +000093 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000094 else if (RC == Alpha::GPRCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000095 BuildMI(MBB, MI, TII.get(Alpha::LDQ), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +000096 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2a8fc232005-02-01 20:35:57 +000097 else
98 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099}
100
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000101MachineInstr *AlphaRegisterInfo::foldMemoryOperand(MachineInstr *MI,
102 unsigned OpNum,
103 int FrameIndex) const {
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000104 // Make sure this is a reg-reg copy.
105 unsigned Opc = MI->getOpcode();
106
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000107 MachineInstr *NewMI = NULL;
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000108 switch(Opc) {
109 default:
110 break;
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +0000111 case Alpha::BISr:
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000112 case Alpha::CPYSS:
113 case Alpha::CPYST:
114 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
115 if (OpNum == 0) { // move -> store
116 unsigned InReg = MI->getOperand(1).getReg();
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +0000117 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000118 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000119 NewMI = BuildMI(TII.get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000120 .addReg(Alpha::F31);
121 } else { // load -> move
122 unsigned OutReg = MI->getOperand(0).getReg();
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +0000123 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000124 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000125 NewMI = BuildMI(TII.get(Opc), OutReg).addFrameIndex(FrameIndex)
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000126 .addReg(Alpha::F31);
127 }
128 }
129 break;
130 }
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000131 if (NewMI)
132 NewMI->copyKillDeadInfo(MI);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000133 return 0;
134}
135
136
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
138 MachineBasicBlock::iterator MI,
139 unsigned DestReg, unsigned SrcReg,
140 const TargetRegisterClass *RC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000141 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 if (RC == Alpha::GPRCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000143 BuildMI(MBB, MI, TII.get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000144 } else if (RC == Alpha::F4RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000145 BuildMI(MBB, MI, TII.get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000146 } else if (RC == Alpha::F8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000147 BuildMI(MBB, MI, TII.get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000148 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000149 cerr << "Attempt to copy register that is not GPR or FPR";
150 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151 }
152}
153
Evan Chengc2b861d2007-01-02 21:33:40 +0000154const unsigned* AlphaRegisterInfo::getCalleeSavedRegs() const {
155 static const unsigned CalleeSavedRegs[] = {
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000156 Alpha::R9, Alpha::R10,
157 Alpha::R11, Alpha::R12,
158 Alpha::R13, Alpha::R14,
159 Alpha::F2, Alpha::F3,
160 Alpha::F4, Alpha::F5,
161 Alpha::F6, Alpha::F7,
162 Alpha::F8, Alpha::F9, 0
163 };
Evan Chengc2b861d2007-01-02 21:33:40 +0000164 return CalleeSavedRegs;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000165}
166
167const TargetRegisterClass* const*
Evan Chengc2b861d2007-01-02 21:33:40 +0000168AlphaRegisterInfo::getCalleeSavedRegClasses() const {
169 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000170 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
171 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
172 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
173 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
174 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
175 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
176 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass, 0
177 };
Evan Chengc2b861d2007-01-02 21:33:40 +0000178 return CalleeSavedRegClasses;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000179}
180
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181//===----------------------------------------------------------------------===//
182// Stack Frame Processing methods
183//===----------------------------------------------------------------------===//
184
185// hasFP - Return true if the specified function should have a dedicated frame
186// pointer register. This is true if the function has variable sized allocas or
187// if frame pointer elimination is disabled.
188//
Chris Lattner5ea64fd2006-08-17 22:00:08 +0000189static bool hasFP(const MachineFunction &MF) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000190 MachineFrameInfo *MFI = MF.getFrameInfo();
191 return MFI->hasVarSizedObjects();
192}
193
194void AlphaRegisterInfo::
195eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
196 MachineBasicBlock::iterator I) const {
197 if (hasFP(MF)) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000198 // If we have a frame pointer, turn the adjcallstackup instruction into a
199 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
200 // <amt>'
201 MachineInstr *Old = I;
Andrew Lenharth65b889f2006-05-17 19:24:49 +0000202 uint64_t Amount = Old->getOperand(0).getImmedValue();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000203 if (Amount != 0) {
204 // We need to keep the stack aligned properly. To do this, we round the
205 // amount of space needed for the outgoing arguments up to the next
206 // alignment boundary.
207 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
208 Amount = (Amount+Align-1)/Align*Align;
209
Andrew Lenharth032f2352005-02-22 21:59:48 +0000210 MachineInstr *New;
211 if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000212 New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000213 .addImm(-Amount).addReg(Alpha::R30);
214 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000215 assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000216 New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000217 .addImm(Amount).addReg(Alpha::R30);
218 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000219
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220 // Replace the pseudo instruction with a new instruction...
Andrew Lenharth032f2352005-02-22 21:59:48 +0000221 MBB.insert(I, New);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 }
223 }
224
225 MBB.erase(I);
226}
227
Andrew Lenharth032f2352005-02-22 21:59:48 +0000228//Alpha has a slightly funny stack:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000229//Args
Andrew Lenharth032f2352005-02-22 21:59:48 +0000230//<- incoming SP
231//fixed locals (and spills, callee saved, etc)
232//<- FP
233//variable locals
234//<- SP
235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236void
237AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
Andrew Lenharth684f2292005-01-30 00:35:27 +0000238 unsigned i = 0;
239 MachineInstr &MI = *II;
240 MachineBasicBlock &MBB = *MI.getParent();
241 MachineFunction &MF = *MBB.getParent();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000242 bool FP = hasFP(MF);
243
Andrew Lenharth684f2292005-01-30 00:35:27 +0000244 while (!MI.getOperand(i).isFrameIndex()) {
245 ++i;
246 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
247 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000248
Andrew Lenharth684f2292005-01-30 00:35:27 +0000249 int FrameIndex = MI.getOperand(i).getFrameIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250
Andrew Lenharth684f2292005-01-30 00:35:27 +0000251 // Add the base register of R30 (SP) or R15 (FP).
Chris Lattner09e46062006-09-05 02:31:13 +0000252 MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253
Andrew Lenharth032f2352005-02-22 21:59:48 +0000254 // Now add the frame object offset to the offset from the virtual frame index.
Andrew Lenharth684f2292005-01-30 00:35:27 +0000255 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
256
Bill Wendlingf5da1332006-12-07 22:21:48 +0000257 DOUT << "FI: " << FrameIndex << " Offset: " << Offset << "\n";
Andrew Lenharth684f2292005-01-30 00:35:27 +0000258
Andrew Lenharth032f2352005-02-22 21:59:48 +0000259 Offset += MF.getFrameInfo()->getStackSize();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260
Bill Wendlingf5da1332006-12-07 22:21:48 +0000261 DOUT << "Corrected Offset " << Offset
262 << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n";
Andrew Lenharth032f2352005-02-22 21:59:48 +0000263
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000264 if (Offset > IMM_HIGH || Offset < IMM_LOW) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000265 DOUT << "Unconditionally using R28 for evil purposes Offset: "
266 << Offset << "\n";
Chris Lattner09e46062006-09-05 02:31:13 +0000267 //so in this case, we need to use a temporary register, and move the
268 //original inst off the SP/FP
Andrew Lenharth032f2352005-02-22 21:59:48 +0000269 //fix up the old:
Chris Lattner09e46062006-09-05 02:31:13 +0000270 MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000271 MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
Andrew Lenharth032f2352005-02-22 21:59:48 +0000272 //insert the new
Evan Chengc0f64ff2006-11-27 23:37:22 +0000273 MachineInstr* nMI=BuildMI(TII.get(Alpha::LDAH), Alpha::R28)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000274 .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
Andrew Lenharth84e2dc22005-03-13 00:43:20 +0000275 MBB.insert(II, nMI);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000276 } else {
Chris Lattnere53f4a02006-05-04 17:52:23 +0000277 MI.getOperand(i).ChangeToImmediate(Offset);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000278 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279}
280
281
282void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
283 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
284 MachineBasicBlock::iterator MBBI = MBB.begin();
285 MachineFrameInfo *MFI = MF.getFrameInfo();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000286 bool FP = hasFP(MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000287
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000288 static int curgpdist = 0;
289
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290 //handle GOP offset
Evan Chengc0f64ff2006-11-27 23:37:22 +0000291 BuildMI(MBB, MBBI, TII.get(Alpha::LDAHg), Alpha::R29)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000292 .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
293 .addReg(Alpha::R27).addImm(++curgpdist);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000294 BuildMI(MBB, MBBI, TII.get(Alpha::LDAg), Alpha::R29)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000295 .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
296 .addReg(Alpha::R29).addImm(curgpdist);
297
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000298 //evil const_cast until MO stuff setup to handle const
Evan Chengc0f64ff2006-11-27 23:37:22 +0000299 BuildMI(MBB, MBBI, TII.get(Alpha::ALTENT))
Chris Lattnerea50fab2006-05-04 01:15:02 +0000300 .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000301
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 // Get the number of bytes to allocate from the FrameInfo
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000303 long NumBytes = MFI->getStackSize();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000304
Andrew Lenharth032f2352005-02-22 21:59:48 +0000305 if (MFI->hasCalls() && !FP) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000306 // We reserve argument space for call sites in the function immediately on
307 // entry to the current function. This eliminates the need for add/sub
Andrew Lenharth684f2292005-01-30 00:35:27 +0000308 // brackets around call sites.
Andrew Lenharth032f2352005-02-22 21:59:48 +0000309 //If there is a frame pointer, then we don't do this
Andrew Lenharth684f2292005-01-30 00:35:27 +0000310 NumBytes += MFI->getMaxCallFrameSize();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000311 DOUT << "Added " << MFI->getMaxCallFrameSize()
312 << " to the stack due to calls\n";
Andrew Lenharth684f2292005-01-30 00:35:27 +0000313 }
314
Andrew Lenharth032f2352005-02-22 21:59:48 +0000315 if (FP)
316 NumBytes += 8; //reserve space for the old FP
317
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000318 // Do we need to allocate space on the stack?
319 if (NumBytes == 0) return;
320
Andrew Lenharth23918972006-01-25 01:51:08 +0000321 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
322 NumBytes = (NumBytes+Align-1)/Align*Align;
323
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000324 // Update frame info to pretend that this is part of the stack...
325 MFI->setStackSize(NumBytes);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000326
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000327 // adjust stack pointer: r30 -= numbytes
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000328 NumBytes = -NumBytes;
329 if (NumBytes >= IMM_LOW) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000330 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000331 .addReg(Alpha::R30);
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000332 } else if (getUpper16(NumBytes) >= IMM_LOW) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000333 BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30).addImm(getUpper16(NumBytes))
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000334 .addReg(Alpha::R30);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000335 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(getLower16(NumBytes))
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000336 .addReg(Alpha::R30);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000337 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000338 cerr << "Too big a stack frame at " << NumBytes << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000339 abort();
340 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000341
342 //now if we need to, save the old FP and set the new
343 if (FP)
344 {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000345 BuildMI(MBB, MBBI, TII.get(Alpha::STQ))
Chris Lattner09e46062006-09-05 02:31:13 +0000346 .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000347 //this must be the last instr in the prolog
Evan Chengc0f64ff2006-11-27 23:37:22 +0000348 BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R15)
Chris Lattner09e46062006-09-05 02:31:13 +0000349 .addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000350 }
351
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000352}
353
354void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
355 MachineBasicBlock &MBB) const {
356 const MachineFrameInfo *MFI = MF.getFrameInfo();
357 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Chris Lattner09e46062006-09-05 02:31:13 +0000358 assert(MBBI->getOpcode() == Alpha::RETDAG ||
359 MBBI->getOpcode() == Alpha::RETDAGp
Misha Brukman7847fca2005-04-22 17:54:37 +0000360 && "Can only insert epilog into returning blocks");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000361
Andrew Lenharth032f2352005-02-22 21:59:48 +0000362 bool FP = hasFP(MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000363
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000364 // Get the number of bytes allocated from the FrameInfo...
Andrew Lenharth3ee60412005-03-05 15:30:33 +0000365 long NumBytes = MFI->getStackSize();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000366
Andrew Lenharth032f2352005-02-22 21:59:48 +0000367 //now if we need to, restore the old FP
368 if (FP)
369 {
Andrew Lenharth01694752005-02-24 18:36:32 +0000370 //copy the FP into the SP (discards allocas)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000371 BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000372 .addReg(Alpha::R15);
Andrew Lenharth01694752005-02-24 18:36:32 +0000373 //restore the FP
Evan Chengc0f64ff2006-11-27 23:37:22 +0000374 BuildMI(MBB, MBBI, TII.get(Alpha::LDQ), Alpha::R15).addImm(0).addReg(Alpha::R15);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000375 }
376
Misha Brukman4633f1c2005-04-21 23:13:11 +0000377 if (NumBytes != 0)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000378 {
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000379 if (NumBytes <= IMM_HIGH) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000380 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000381 .addReg(Alpha::R30);
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000382 } else if (getUpper16(NumBytes) <= IMM_HIGH) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000383 BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000384 .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000385 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000386 .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
Andrew Lenharth04c868e2005-01-27 08:31:19 +0000387 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000388 cerr << "Too big a stack frame at " << NumBytes << "\n";
Andrew Lenharth04c868e2005-01-27 08:31:19 +0000389 abort();
390 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000391 }
392}
393
Jim Laskey41886992006-04-07 16:34:46 +0000394unsigned AlphaRegisterInfo::getRARegister() const {
395 assert(0 && "What is the return address register");
396 return 0;
397}
398
Jim Laskeya9979182006-03-28 13:48:33 +0000399unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
Jim Laskey41886992006-04-07 16:34:46 +0000400 return hasFP(MF) ? Alpha::R15 : Alpha::R30;
Jim Laskeyf1d78e82006-03-23 18:12:57 +0000401}
402
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000403#include "AlphaGenRegisterInfo.inc"
404
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000405std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
406{
407 std::string s(RegisterDescriptors[reg].Name);
408 return s;
409}