blob: 53d8df1bb5a9c54c7f38382451d41645e6058d42 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Evan Chengb371f452007-02-19 21:49:54 +000031#include "llvm/ADT/BitVector.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include "llvm/ADT/STLExtras.h"
33#include <cstdlib>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000036//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +000037static const int IMM_LOW = -32768;
38static const int IMM_HIGH = 32767;
Andrew Lenharth3ee60412005-03-05 15:30:33 +000039static const int IMM_MULT = 65536;
Andrew Lenharth032f2352005-02-22 21:59:48 +000040
41static long getUpper16(long l)
42{
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000043 long y = l / IMM_MULT;
44 if (l % IMM_MULT > IMM_HIGH)
Andrew Lenharth032f2352005-02-22 21:59:48 +000045 ++y;
46 return y;
47}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000048
Andrew Lenharthf69a98c2005-03-04 21:40:02 +000049static long getLower16(long l)
50{
51 long h = getUpper16(l);
52 return l - h * IMM_MULT;
53}
54
Evan Cheng7ce45782006-11-13 23:36:35 +000055AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
56 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
57 TII(tii)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000058{
59}
60
Misha Brukman4633f1c2005-04-21 23:13:11 +000061void
Andrew Lenharth304d0f32005-01-22 23:41:55 +000062AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MI,
Evan Chengd64b5c82007-12-05 03:14:33 +000064 unsigned SrcReg, bool isKill, int FrameIdx,
65 const TargetRegisterClass *RC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +000066 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
67 // << FrameIdx << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +000068 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000069 if (RC == Alpha::F4RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000070 BuildMI(MBB, MI, TII.get(Alpha::STS))
Evan Chengd64b5c82007-12-05 03:14:33 +000071 .addReg(SrcReg, false, false, isKill)
Evan Cheng0fa1b6d2007-02-23 01:10:04 +000072 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000073 else if (RC == Alpha::F8RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000074 BuildMI(MBB, MI, TII.get(Alpha::STT))
Evan Chengd64b5c82007-12-05 03:14:33 +000075 .addReg(SrcReg, false, false, isKill)
Evan Cheng0fa1b6d2007-02-23 01:10:04 +000076 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000077 else if (RC == Alpha::GPRCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +000078 BuildMI(MBB, MI, TII.get(Alpha::STQ))
Evan Chengd64b5c82007-12-05 03:14:33 +000079 .addReg(SrcReg, false, false, isKill)
Evan Cheng0fa1b6d2007-02-23 01:10:04 +000080 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2a8fc232005-02-01 20:35:57 +000081 else
82 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083}
84
Evan Cheng66f0f642007-10-05 01:32:41 +000085void AlphaRegisterInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Evan Chengd64b5c82007-12-05 03:14:33 +000086 bool isKill,
Evan Chengf0a0cdd2007-10-18 22:40:57 +000087 SmallVectorImpl<MachineOperand> &Addr,
Evan Cheng66f0f642007-10-05 01:32:41 +000088 const TargetRegisterClass *RC,
Evan Cheng58184e62007-10-18 21:29:24 +000089 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng66f0f642007-10-05 01:32:41 +000090 unsigned Opc = 0;
91 if (RC == Alpha::F4RCRegisterClass)
92 Opc = Alpha::STS;
93 else if (RC == Alpha::F8RCRegisterClass)
94 Opc = Alpha::STT;
95 else if (RC == Alpha::GPRCRegisterClass)
96 Opc = Alpha::STQ;
97 else
98 abort();
99 MachineInstrBuilder MIB =
Evan Chengd64b5c82007-12-05 03:14:33 +0000100 BuildMI(TII.get(Opc)).addReg(SrcReg, false, false, isKill);
Evan Cheng66f0f642007-10-05 01:32:41 +0000101 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
102 MachineOperand &MO = Addr[i];
103 if (MO.isRegister())
104 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
105 else
106 MIB.addImm(MO.getImm());
107 }
108 NewMIs.push_back(MIB);
109}
110
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111void
112AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
113 MachineBasicBlock::iterator MI,
Chris Lattner97d5e642005-09-30 01:29:42 +0000114 unsigned DestReg, int FrameIdx,
115 const TargetRegisterClass *RC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000116 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
117 // << FrameIdx << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000118 if (RC == Alpha::F4RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000119 BuildMI(MBB, MI, TII.get(Alpha::LDS), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +0000120 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000121 else if (RC == Alpha::F8RCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000122 BuildMI(MBB, MI, TII.get(Alpha::LDT), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +0000123 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000124 else if (RC == Alpha::GPRCRegisterClass)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000125 BuildMI(MBB, MI, TII.get(Alpha::LDQ), DestReg)
Chris Lattner09e46062006-09-05 02:31:13 +0000126 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2a8fc232005-02-01 20:35:57 +0000127 else
128 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129}
130
Evan Cheng66f0f642007-10-05 01:32:41 +0000131void AlphaRegisterInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Evan Chengf0a0cdd2007-10-18 22:40:57 +0000132 SmallVectorImpl<MachineOperand> &Addr,
Evan Cheng66f0f642007-10-05 01:32:41 +0000133 const TargetRegisterClass *RC,
Evan Cheng58184e62007-10-18 21:29:24 +0000134 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng66f0f642007-10-05 01:32:41 +0000135 unsigned Opc = 0;
136 if (RC == Alpha::F4RCRegisterClass)
137 Opc = Alpha::LDS;
138 else if (RC == Alpha::F8RCRegisterClass)
139 Opc = Alpha::LDT;
140 else if (RC == Alpha::GPRCRegisterClass)
141 Opc = Alpha::LDQ;
142 else
143 abort();
144 MachineInstrBuilder MIB =
145 BuildMI(TII.get(Opc), DestReg);
146 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
147 MachineOperand &MO = Addr[i];
148 if (MO.isRegister())
149 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
150 else
151 MIB.addImm(MO.getImm());
152 }
153 NewMIs.push_back(MIB);
154}
155
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000156MachineInstr *AlphaRegisterInfo::foldMemoryOperand(MachineInstr *MI,
Evan Chengaee4af62007-12-02 08:30:39 +0000157 SmallVectorImpl<unsigned> &Ops,
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000158 int FrameIndex) const {
Evan Chengaee4af62007-12-02 08:30:39 +0000159 if (Ops.size() != 1) return NULL;
160
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000161 // Make sure this is a reg-reg copy.
162 unsigned Opc = MI->getOpcode();
163
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000164 MachineInstr *NewMI = NULL;
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000165 switch(Opc) {
166 default:
167 break;
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +0000168 case Alpha::BISr:
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000169 case Alpha::CPYSS:
170 case Alpha::CPYST:
171 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Evan Chengaee4af62007-12-02 08:30:39 +0000172 if (Ops[0] == 0) { // move -> store
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000173 unsigned InReg = MI->getOperand(1).getReg();
174 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
175 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
176 NewMI = BuildMI(TII.get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
177 .addReg(Alpha::F31);
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000178 } else { // load -> move
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000179 unsigned OutReg = MI->getOperand(0).getReg();
180 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
181 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
182 NewMI = BuildMI(TII.get(Opc), OutReg).addFrameIndex(FrameIndex)
183 .addReg(Alpha::F31);
Andrew Lenharth7a832da2006-01-01 22:13:54 +0000184 }
185 }
186 break;
187 }
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000188 if (NewMI)
189 NewMI->copyKillDeadInfo(MI);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000190 return 0;
191}
192
193
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000194void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
195 MachineBasicBlock::iterator MI,
196 unsigned DestReg, unsigned SrcReg,
Evan Cheng9efce632007-09-26 06:25:56 +0000197 const TargetRegisterClass *DestRC,
198 const TargetRegisterClass *SrcRC) const {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000199 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
Evan Cheng9efce632007-09-26 06:25:56 +0000200 if (DestRC != SrcRC) {
201 cerr << "Not yet supported!";
202 abort();
203 }
204
205 if (DestRC == Alpha::GPRCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000206 BuildMI(MBB, MI, TII.get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
Evan Cheng9efce632007-09-26 06:25:56 +0000207 } else if (DestRC == Alpha::F4RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000208 BuildMI(MBB, MI, TII.get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
Evan Cheng9efce632007-09-26 06:25:56 +0000209 } else if (DestRC == Alpha::F8RCRegisterClass) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000210 BuildMI(MBB, MI, TII.get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000211 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000212 cerr << "Attempt to copy register that is not GPR or FPR";
213 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214 }
215}
216
Evan Chengbf2c8b32007-03-20 08:09:38 +0000217void AlphaRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
218 MachineBasicBlock::iterator I,
219 unsigned DestReg,
220 const MachineInstr *Orig) const {
221 MachineInstr *MI = Orig->clone();
222 MI->getOperand(0).setReg(DestReg);
223 MBB.insert(I, MI);
224}
225
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000226const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
227 const {
Evan Chengc2b861d2007-01-02 21:33:40 +0000228 static const unsigned CalleeSavedRegs[] = {
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000229 Alpha::R9, Alpha::R10,
230 Alpha::R11, Alpha::R12,
231 Alpha::R13, Alpha::R14,
232 Alpha::F2, Alpha::F3,
233 Alpha::F4, Alpha::F5,
234 Alpha::F6, Alpha::F7,
235 Alpha::F8, Alpha::F9, 0
236 };
Evan Chengc2b861d2007-01-02 21:33:40 +0000237 return CalleeSavedRegs;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000238}
239
240const TargetRegisterClass* const*
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000241AlphaRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
Evan Chengc2b861d2007-01-02 21:33:40 +0000242 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000243 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
244 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
245 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
246 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
247 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
248 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
249 &Alpha::F8RCRegClass, &Alpha::F8RCRegClass, 0
250 };
Evan Chengc2b861d2007-01-02 21:33:40 +0000251 return CalleeSavedRegClasses;
Evan Cheng0f3ac8d2006-05-18 00:12:58 +0000252}
253
Evan Chengb371f452007-02-19 21:49:54 +0000254BitVector AlphaRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
255 BitVector Reserved(getNumRegs());
256 Reserved.set(Alpha::R15);
257 Reserved.set(Alpha::R30);
258 Reserved.set(Alpha::R31);
259 return Reserved;
260}
261
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262//===----------------------------------------------------------------------===//
263// Stack Frame Processing methods
264//===----------------------------------------------------------------------===//
265
266// hasFP - Return true if the specified function should have a dedicated frame
267// pointer register. This is true if the function has variable sized allocas or
268// if frame pointer elimination is disabled.
269//
Evan Chengdc775402007-01-23 00:57:47 +0000270bool AlphaRegisterInfo::hasFP(const MachineFunction &MF) const {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 MachineFrameInfo *MFI = MF.getFrameInfo();
272 return MFI->hasVarSizedObjects();
273}
274
275void AlphaRegisterInfo::
276eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
277 MachineBasicBlock::iterator I) const {
278 if (hasFP(MF)) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279 // If we have a frame pointer, turn the adjcallstackup instruction into a
280 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
281 // <amt>'
282 MachineInstr *Old = I;
Andrew Lenharth65b889f2006-05-17 19:24:49 +0000283 uint64_t Amount = Old->getOperand(0).getImmedValue();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000284 if (Amount != 0) {
285 // We need to keep the stack aligned properly. To do this, we round the
286 // amount of space needed for the outgoing arguments up to the next
287 // alignment boundary.
288 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
289 Amount = (Amount+Align-1)/Align*Align;
290
Andrew Lenharth032f2352005-02-22 21:59:48 +0000291 MachineInstr *New;
292 if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000293 New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000294 .addImm(-Amount).addReg(Alpha::R30);
295 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000296 assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000297 New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000298 .addImm(Amount).addReg(Alpha::R30);
299 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000300
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 // Replace the pseudo instruction with a new instruction...
Andrew Lenharth032f2352005-02-22 21:59:48 +0000302 MBB.insert(I, New);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000303 }
304 }
305
306 MBB.erase(I);
307}
308
Andrew Lenharth032f2352005-02-22 21:59:48 +0000309//Alpha has a slightly funny stack:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000310//Args
Andrew Lenharth032f2352005-02-22 21:59:48 +0000311//<- incoming SP
312//fixed locals (and spills, callee saved, etc)
313//<- FP
314//variable locals
315//<- SP
316
Evan Cheng5e6df462007-02-28 00:21:17 +0000317void AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
Evan Cheng97de9132007-05-01 09:13:03 +0000318 int SPAdj, RegScavenger *RS) const {
319 assert(SPAdj == 0 && "Unexpected");
320
Andrew Lenharth684f2292005-01-30 00:35:27 +0000321 unsigned i = 0;
322 MachineInstr &MI = *II;
323 MachineBasicBlock &MBB = *MI.getParent();
324 MachineFunction &MF = *MBB.getParent();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000325 bool FP = hasFP(MF);
326
Andrew Lenharth684f2292005-01-30 00:35:27 +0000327 while (!MI.getOperand(i).isFrameIndex()) {
328 ++i;
329 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
330 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000331
Andrew Lenharth684f2292005-01-30 00:35:27 +0000332 int FrameIndex = MI.getOperand(i).getFrameIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000333
Andrew Lenharth684f2292005-01-30 00:35:27 +0000334 // Add the base register of R30 (SP) or R15 (FP).
Chris Lattner09e46062006-09-05 02:31:13 +0000335 MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000336
Andrew Lenharth032f2352005-02-22 21:59:48 +0000337 // Now add the frame object offset to the offset from the virtual frame index.
Andrew Lenharth684f2292005-01-30 00:35:27 +0000338 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
339
Bill Wendlingf5da1332006-12-07 22:21:48 +0000340 DOUT << "FI: " << FrameIndex << " Offset: " << Offset << "\n";
Andrew Lenharth684f2292005-01-30 00:35:27 +0000341
Andrew Lenharth032f2352005-02-22 21:59:48 +0000342 Offset += MF.getFrameInfo()->getStackSize();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000343
Bill Wendlingf5da1332006-12-07 22:21:48 +0000344 DOUT << "Corrected Offset " << Offset
345 << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n";
Andrew Lenharth032f2352005-02-22 21:59:48 +0000346
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000347 if (Offset > IMM_HIGH || Offset < IMM_LOW) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000348 DOUT << "Unconditionally using R28 for evil purposes Offset: "
349 << Offset << "\n";
Chris Lattner09e46062006-09-05 02:31:13 +0000350 //so in this case, we need to use a temporary register, and move the
351 //original inst off the SP/FP
Andrew Lenharth032f2352005-02-22 21:59:48 +0000352 //fix up the old:
Chris Lattner09e46062006-09-05 02:31:13 +0000353 MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000354 MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
Andrew Lenharth032f2352005-02-22 21:59:48 +0000355 //insert the new
Evan Chengc0f64ff2006-11-27 23:37:22 +0000356 MachineInstr* nMI=BuildMI(TII.get(Alpha::LDAH), Alpha::R28)
Andrew Lenharth032f2352005-02-22 21:59:48 +0000357 .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
Andrew Lenharth84e2dc22005-03-13 00:43:20 +0000358 MBB.insert(II, nMI);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000359 } else {
Chris Lattnere53f4a02006-05-04 17:52:23 +0000360 MI.getOperand(i).ChangeToImmediate(Offset);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000361 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000362}
363
364
365void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
366 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
367 MachineBasicBlock::iterator MBBI = MBB.begin();
368 MachineFrameInfo *MFI = MF.getFrameInfo();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000369 bool FP = hasFP(MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000370
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000371 static int curgpdist = 0;
372
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000373 //handle GOP offset
Evan Chengc0f64ff2006-11-27 23:37:22 +0000374 BuildMI(MBB, MBBI, TII.get(Alpha::LDAHg), Alpha::R29)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000375 .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
376 .addReg(Alpha::R27).addImm(++curgpdist);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000377 BuildMI(MBB, MBBI, TII.get(Alpha::LDAg), Alpha::R29)
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000378 .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
379 .addReg(Alpha::R29).addImm(curgpdist);
380
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000381 //evil const_cast until MO stuff setup to handle const
Evan Chengc0f64ff2006-11-27 23:37:22 +0000382 BuildMI(MBB, MBBI, TII.get(Alpha::ALTENT))
Chris Lattnerea50fab2006-05-04 01:15:02 +0000383 .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000384
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000385 // Get the number of bytes to allocate from the FrameInfo
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000386 long NumBytes = MFI->getStackSize();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000387
Andrew Lenharth032f2352005-02-22 21:59:48 +0000388 if (FP)
389 NumBytes += 8; //reserve space for the old FP
390
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000391 // Do we need to allocate space on the stack?
392 if (NumBytes == 0) return;
393
Andrew Lenharth23918972006-01-25 01:51:08 +0000394 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
395 NumBytes = (NumBytes+Align-1)/Align*Align;
396
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000397 // Update frame info to pretend that this is part of the stack...
398 MFI->setStackSize(NumBytes);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000399
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000400 // adjust stack pointer: r30 -= numbytes
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000401 NumBytes = -NumBytes;
402 if (NumBytes >= IMM_LOW) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000403 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000404 .addReg(Alpha::R30);
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000405 } else if (getUpper16(NumBytes) >= IMM_LOW) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000406 BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30).addImm(getUpper16(NumBytes))
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000407 .addReg(Alpha::R30);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000408 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(getLower16(NumBytes))
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000409 .addReg(Alpha::R30);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000410 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000411 cerr << "Too big a stack frame at " << NumBytes << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000412 abort();
413 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000414
415 //now if we need to, save the old FP and set the new
416 if (FP)
417 {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000418 BuildMI(MBB, MBBI, TII.get(Alpha::STQ))
Chris Lattner09e46062006-09-05 02:31:13 +0000419 .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000420 //this must be the last instr in the prolog
Evan Chengc0f64ff2006-11-27 23:37:22 +0000421 BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R15)
Chris Lattner09e46062006-09-05 02:31:13 +0000422 .addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000423 }
424
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000425}
426
427void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
428 MachineBasicBlock &MBB) const {
429 const MachineFrameInfo *MFI = MF.getFrameInfo();
430 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Chris Lattner09e46062006-09-05 02:31:13 +0000431 assert(MBBI->getOpcode() == Alpha::RETDAG ||
432 MBBI->getOpcode() == Alpha::RETDAGp
Misha Brukman7847fca2005-04-22 17:54:37 +0000433 && "Can only insert epilog into returning blocks");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000434
Andrew Lenharth032f2352005-02-22 21:59:48 +0000435 bool FP = hasFP(MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000436
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000437 // Get the number of bytes allocated from the FrameInfo...
Andrew Lenharth3ee60412005-03-05 15:30:33 +0000438 long NumBytes = MFI->getStackSize();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000439
Andrew Lenharth032f2352005-02-22 21:59:48 +0000440 //now if we need to, restore the old FP
441 if (FP)
442 {
Andrew Lenharth01694752005-02-24 18:36:32 +0000443 //copy the FP into the SP (discards allocas)
Evan Chengc0f64ff2006-11-27 23:37:22 +0000444 BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000445 .addReg(Alpha::R15);
Andrew Lenharth01694752005-02-24 18:36:32 +0000446 //restore the FP
Evan Chengc0f64ff2006-11-27 23:37:22 +0000447 BuildMI(MBB, MBBI, TII.get(Alpha::LDQ), Alpha::R15).addImm(0).addReg(Alpha::R15);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000448 }
449
Misha Brukman4633f1c2005-04-21 23:13:11 +0000450 if (NumBytes != 0)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000451 {
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000452 if (NumBytes <= IMM_HIGH) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000453 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000454 .addReg(Alpha::R30);
Andrew Lenharthf69a98c2005-03-04 21:40:02 +0000455 } else if (getUpper16(NumBytes) <= IMM_HIGH) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000456 BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000457 .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000458 BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30)
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000459 .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
Andrew Lenharth04c868e2005-01-27 08:31:19 +0000460 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000461 cerr << "Too big a stack frame at " << NumBytes << "\n";
Andrew Lenharth04c868e2005-01-27 08:31:19 +0000462 abort();
463 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000464 }
465}
466
Jim Laskey41886992006-04-07 16:34:46 +0000467unsigned AlphaRegisterInfo::getRARegister() const {
468 assert(0 && "What is the return address register");
469 return 0;
470}
471
Jim Laskeya9979182006-03-28 13:48:33 +0000472unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
Jim Laskey41886992006-04-07 16:34:46 +0000473 return hasFP(MF) ? Alpha::R15 : Alpha::R30;
Jim Laskeyf1d78e82006-03-23 18:12:57 +0000474}
475
Jim Laskey62819f32007-02-21 22:54:50 +0000476unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
477 assert(0 && "What is the exception register");
478 return 0;
479}
480
481unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
482 assert(0 && "What is the exception handler register");
483 return 0;
484}
485
Dale Johannesenb97aec62007-11-13 19:13:01 +0000486int AlphaRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
Anton Korobeynikovf191c802007-11-11 19:50:10 +0000487 assert(0 && "What is the dwarf register number");
488 return -1;
489}
490
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000491#include "AlphaGenRegisterInfo.inc"
492
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000493std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
494{
495 std::string s(RegisterDescriptors[reg].Name);
496 return s;
497}