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/CodeGen/ValueTypes.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/Target/TargetFrameInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/ADT/STLExtras.h"
34 AlphaRegisterInfo::AlphaRegisterInfo()
35 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
39 static const TargetRegisterClass *getClass(unsigned SrcReg) {
40 if (Alpha::FPRCRegisterClass->contains(SrcReg))
41 return Alpha::FPRCRegisterClass;
42 assert(Alpha::GPRCRegisterClass->contains(SrcReg) && "Reg not FPR or GPR");
43 return Alpha::GPRCRegisterClass;
47 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
48 MachineBasicBlock::iterator MI,
49 unsigned SrcReg, int FrameIdx) const {
50 //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
51 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
52 BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
53 // assert(0 && "TODO");
57 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
58 MachineBasicBlock::iterator MI,
59 unsigned DestReg, int FrameIdx) const{
60 //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
61 //BuildMI(MBB, MI, Alpha::WTF, 0, DestReg);
62 BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
63 // assert(0 && "TODO");
66 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
67 MachineBasicBlock::iterator MI,
68 unsigned DestReg, unsigned SrcReg,
69 const TargetRegisterClass *RC) const {
70 // std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
71 if (RC == Alpha::GPRCRegisterClass) {
72 BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
73 } else if (RC == Alpha::FPRCRegisterClass) {
74 BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
76 std::cerr << "Attempt to copy register that is not GPR or FPR";
81 //===----------------------------------------------------------------------===//
82 // Stack Frame Processing methods
83 //===----------------------------------------------------------------------===//
85 // hasFP - Return true if the specified function should have a dedicated frame
86 // pointer register. This is true if the function has variable sized allocas or
87 // if frame pointer elimination is disabled.
89 static bool hasFP(MachineFunction &MF) {
90 MachineFrameInfo *MFI = MF.getFrameInfo();
91 return MFI->hasVarSizedObjects();
94 void AlphaRegisterInfo::
95 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
96 MachineBasicBlock::iterator I) const {
99 // If we have a frame pointer, turn the adjcallstackup instruction into a
100 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
102 MachineInstr *Old = I;
103 unsigned Amount = Old->getOperand(0).getImmedValue();
105 // We need to keep the stack aligned properly. To do this, we round the
106 // amount of space needed for the outgoing arguments up to the next
107 // alignment boundary.
108 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
109 Amount = (Amount+Align-1)/Align*Align;
111 // MachineInstr *New;
112 // if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
113 // New=BuildMI(X86::SUB32ri, 1, X86::ESP, MachineOperand::UseAndDef)
116 // assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
117 // New=BuildMI(X86::ADD32ri, 1, X86::ESP, MachineOperand::UseAndDef)
121 // Replace the pseudo instruction with a new instruction...
122 //MBB.insert(I, New);
131 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
133 MachineInstr &MI = *II;
134 MachineBasicBlock &MBB = *MI.getParent();
135 MachineFunction &MF = *MBB.getParent();
137 while (!MI.getOperand(i).isFrameIndex()) {
139 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
142 int FrameIndex = MI.getOperand(i).getFrameIndex();
144 // Add the base register of R30 (SP) or R15 (FP).
145 MI.SetMachineOperandReg(i + 1, hasFP(MF) ? Alpha::R15 : Alpha::R30);
147 // Now add the frame object offset to the offset from r1.
148 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
150 // If we're not using a Frame Pointer that has been set to the value of the
151 // SP before having the stack size subtracted from it, then add the stack size
152 // to Offset to get the correct offset.
153 Offset += MF.getFrameInfo()->getStackSize();
155 if (Offset > 32767 || Offset < -32768) {
156 std::cerr << "Offset needs to be " << Offset << "\n";
157 assert(0 && "stack too big");
159 MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
164 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
165 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
166 MachineBasicBlock::iterator MBBI = MBB.begin();
167 MachineFrameInfo *MFI = MF.getFrameInfo();
171 MI = BuildMI(Alpha::LDGP, 0);
172 MBB.insert(MBBI, MI);
174 // Get the number of bytes to allocate from the FrameInfo
175 unsigned NumBytes = MFI->getStackSize();
177 if (MFI->hasCalls()) {
178 // We reserve argument space for call sites in the function immediately on
179 // entry to the current function. This eliminates the need for add/sub
180 // brackets around call sites.
181 NumBytes += MFI->getMaxCallFrameSize();
182 std::cerr << "Added " << MFI->getMaxCallFrameSize() << " to the stack due to calls\n";
185 // Do we need to allocate space on the stack?
186 if (NumBytes == 0) return;
188 // Add the size of R30 to NumBytes size for the store of R30 to the
190 // std::cerr << "Spillsize of R30 is " << getSpillSize(Alpha::R30) << "\n";
191 // NumBytes = NumBytes + getSpillSize(Alpha::R30)/8;
193 // Update frame info to pretend that this is part of the stack...
194 MFI->setStackSize(NumBytes);
196 // adjust stack pointer: r30 -= numbytes
198 if (NumBytes <= 32767) {
199 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-NumBytes).addReg(Alpha::R30);
200 MBB.insert(MBBI, MI);
201 } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
202 long y = NumBytes / 65536;
203 if (NumBytes % 65536 > 32767)
205 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(-y).addReg(Alpha::R30);
206 MBB.insert(MBBI, MI);
207 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-(NumBytes - y * 65536)).addReg(Alpha::R30);
208 MBB.insert(MBBI, MI);
210 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
215 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
216 MachineBasicBlock &MBB) const {
217 const MachineFrameInfo *MFI = MF.getFrameInfo();
218 MachineBasicBlock::iterator MBBI = prior(MBB.end());
220 assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
221 "Can only insert epilog into returning blocks");
223 // Get the number of bytes allocated from the FrameInfo...
224 unsigned NumBytes = MFI->getStackSize();
228 if (NumBytes <= 32767) {
229 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
230 MBB.insert(MBBI, MI);
231 } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
232 long y = NumBytes / 65536;
233 if (NumBytes % 65536 > 32767)
235 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(y).addReg(Alpha::R30);
236 MBB.insert(MBBI, MI);
237 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes - y * 65536).addReg(Alpha::R30);
238 MBB.insert(MBBI, MI);
240 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
246 #include "AlphaGenRegisterInfo.inc"
248 const TargetRegisterClass*
249 AlphaRegisterInfo::getRegClassForType(const Type* Ty) const {
250 switch (Ty->getTypeID()) {
251 default: assert(0 && "Invalid type to getClass!");
253 case Type::SByteTyID:
254 case Type::UByteTyID:
255 case Type::ShortTyID:
256 case Type::UShortTyID:
259 case Type::PointerTyID:
261 case Type::ULongTyID: return &GPRCInstance;
263 case Type::FloatTyID:
264 case Type::DoubleTyID: return &FPRCInstance;
268 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
270 std::string s(RegisterDescriptors[reg].Name);