1 //===- AlphaRegisterInfo.cpp - Alpha Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Alpha implementation of the MRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "reginfo"
16 #include "AlphaRegisterInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Type.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/Target/TargetFrameInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/ADT/STLExtras.h"
35 static const int IMM_LOW = -32768;
36 static const int IMM_HIGH = 32767;
37 static const int IMM_MULT = 65536;
39 static long getUpper16(long l)
41 long y = l / IMM_MULT;
42 if (l % IMM_MULT > IMM_HIGH)
47 static long getLower16(long l)
49 long h = getUpper16(l);
50 return l - h * IMM_MULT;
53 AlphaRegisterInfo::AlphaRegisterInfo()
54 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
58 static const TargetRegisterClass *getClass(unsigned SrcReg) {
59 if (Alpha::FPRCRegisterClass->contains(SrcReg))
60 return Alpha::FPRCRegisterClass;
61 assert(Alpha::GPRCRegisterClass->contains(SrcReg) && "Reg not FPR or GPR");
62 return Alpha::GPRCRegisterClass;
66 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
67 MachineBasicBlock::iterator MI,
68 unsigned SrcReg, int FrameIdx) const {
69 //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
70 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
71 if (getClass(SrcReg) == Alpha::FPRCRegisterClass)
72 BuildMI(MBB, MI, Alpha::STT, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
73 else if (getClass(SrcReg) == Alpha::GPRCRegisterClass)
74 BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
80 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
81 MachineBasicBlock::iterator MI,
82 unsigned DestReg, int FrameIdx) const{
83 //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
84 if (getClass(DestReg) == Alpha::FPRCRegisterClass)
85 BuildMI(MBB, MI, Alpha::LDT, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
86 else if (getClass(DestReg) == Alpha::GPRCRegisterClass)
87 BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
92 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
93 MachineBasicBlock::iterator MI,
94 unsigned DestReg, unsigned SrcReg,
95 const TargetRegisterClass *RC) const {
96 // std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
97 if (RC == Alpha::GPRCRegisterClass) {
98 BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
99 } else if (RC == Alpha::FPRCRegisterClass) {
100 BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
102 std::cerr << "Attempt to copy register that is not GPR or FPR";
107 //===----------------------------------------------------------------------===//
108 // Stack Frame Processing methods
109 //===----------------------------------------------------------------------===//
111 // hasFP - Return true if the specified function should have a dedicated frame
112 // pointer register. This is true if the function has variable sized allocas or
113 // if frame pointer elimination is disabled.
115 static bool hasFP(MachineFunction &MF) {
116 MachineFrameInfo *MFI = MF.getFrameInfo();
117 return MFI->hasVarSizedObjects();
120 void AlphaRegisterInfo::
121 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
122 MachineBasicBlock::iterator I) const {
124 // If we have a frame pointer, turn the adjcallstackup instruction into a
125 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
127 MachineInstr *Old = I;
128 unsigned Amount = Old->getOperand(0).getImmedValue();
130 // We need to keep the stack aligned properly. To do this, we round the
131 // amount of space needed for the outgoing arguments up to the next
132 // alignment boundary.
133 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
134 Amount = (Amount+Align-1)/Align*Align;
137 if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
138 New=BuildMI(Alpha::LDA, 2, Alpha::R30)
139 .addImm(-Amount).addReg(Alpha::R30);
141 assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
142 New=BuildMI(Alpha::LDA, 2, Alpha::R30)
143 .addImm(Amount).addReg(Alpha::R30);
146 // Replace the pseudo instruction with a new instruction...
154 //Alpha has a slightly funny stack:
157 //fixed locals (and spills, callee saved, etc)
163 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
165 MachineInstr &MI = *II;
166 MachineBasicBlock &MBB = *MI.getParent();
167 MachineFunction &MF = *MBB.getParent();
170 while (!MI.getOperand(i).isFrameIndex()) {
172 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
175 int FrameIndex = MI.getOperand(i).getFrameIndex();
177 // Add the base register of R30 (SP) or R15 (FP).
178 MI.SetMachineOperandReg(i + 1, FP ? Alpha::R15 : Alpha::R30);
180 // Now add the frame object offset to the offset from the virtual frame index.
181 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
183 DEBUG(std::cerr << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
185 Offset += MF.getFrameInfo()->getStackSize();
187 DEBUG(std::cerr << "Corrected Offset " << Offset <<
188 " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
190 if (Offset > IMM_HIGH || Offset < IMM_LOW) {
191 //so in this case, we need to use a temporary register, and move the original
194 MI.SetMachineOperandReg(i + 1, Alpha::R28);
195 MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed,
198 MachineInstr* nMI=BuildMI(Alpha::LDAH, 2, Alpha::R28)
199 .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
202 MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
207 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
208 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
209 MachineBasicBlock::iterator MBBI = MBB.begin();
210 MachineFrameInfo *MFI = MF.getFrameInfo();
215 MI = BuildMI(Alpha::LDGP, 0);
216 MBB.insert(MBBI, MI);
217 //evil const_cast until MO stuff setup to handle const
218 MI = BuildMI(Alpha::ALTENT, 1).addGlobalAddress(const_cast<Function*>(MF.getFunction()), true);
219 MBB.insert(MBBI, MI);
221 // Get the number of bytes to allocate from the FrameInfo
222 long NumBytes = MFI->getStackSize();
224 if (MFI->hasCalls() && !FP) {
225 // We reserve argument space for call sites in the function immediately on
226 // entry to the current function. This eliminates the need for add/sub
227 // brackets around call sites.
228 //If there is a frame pointer, then we don't do this
229 NumBytes += MFI->getMaxCallFrameSize();
230 DEBUG(std::cerr << "Added " << MFI->getMaxCallFrameSize()
231 << " to the stack due to calls\n");
235 NumBytes += 8; //reserve space for the old FP
237 // Do we need to allocate space on the stack?
238 if (NumBytes == 0) return;
240 // Update frame info to pretend that this is part of the stack...
241 MFI->setStackSize(NumBytes);
243 // adjust stack pointer: r30 -= numbytes
244 NumBytes = -NumBytes;
245 if (NumBytes >= IMM_LOW) {
246 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
247 MBB.insert(MBBI, MI);
248 } else if (getUpper16(NumBytes) >= IMM_LOW) {
249 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
250 MBB.insert(MBBI, MI);
251 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
252 MBB.insert(MBBI, MI);
254 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
258 //now if we need to, save the old FP and set the new
261 MI=BuildMI(Alpha::STQ, 3).addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
262 MBB.insert(MBBI, MI);
263 //this must be the last instr in the prolog
264 MI=BuildMI(Alpha::BIS, 2, Alpha::R15).addReg(Alpha::R30).addReg(Alpha::R30);
265 MBB.insert(MBBI, MI);
270 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
271 MachineBasicBlock &MBB) const {
272 const MachineFrameInfo *MFI = MF.getFrameInfo();
273 MachineBasicBlock::iterator MBBI = prior(MBB.end());
275 assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
276 "Can only insert epilog into returning blocks");
280 // Get the number of bytes allocated from the FrameInfo...
281 long NumBytes = MFI->getStackSize();
283 //now if we need to, restore the old FP
286 //copy the FP into the SP (discards allocas)
287 MI=BuildMI(Alpha::BIS, 2, Alpha::R30).addReg(Alpha::R15).addReg(Alpha::R15);
288 MBB.insert(MBBI, MI);
290 MI=BuildMI(Alpha::LDQ, 2, Alpha::R15).addImm(0).addReg(Alpha::R15);
291 MBB.insert(MBBI, MI);
296 if (NumBytes <= IMM_HIGH) {
297 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
298 MBB.insert(MBBI, MI);
299 } else if (getUpper16(NumBytes) <= IMM_HIGH) {
300 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
301 MBB.insert(MBBI, MI);
302 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
303 MBB.insert(MBBI, MI);
305 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
311 #include "AlphaGenRegisterInfo.inc"
313 const TargetRegisterClass*
314 AlphaRegisterInfo::getRegClassForType(const Type* Ty) const {
315 switch (Ty->getTypeID()) {
316 default: assert(0 && "Invalid type to getClass!");
318 case Type::SByteTyID:
319 case Type::UByteTyID:
320 case Type::ShortTyID:
321 case Type::UShortTyID:
324 case Type::PointerTyID:
326 case Type::ULongTyID: return &GPRCInstance;
328 case Type::FloatTyID:
329 case Type::DoubleTyID: return &FPRCInstance;
333 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
335 std::string s(RegisterDescriptors[reg].Name);