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 "AlphaInstrBuilder.h"
17 #include "AlphaRegisterInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Type.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 AlphaRegisterInfo::AlphaRegisterInfo()
36 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
40 static const TargetRegisterClass *getClass(unsigned SrcReg) {
41 if (Alpha::FPRCRegisterClass->contains(SrcReg))
42 return Alpha::FPRCRegisterClass;
43 assert(Alpha::GPRCRegisterClass->contains(SrcReg) && "Reg not FPR or GPR");
44 return Alpha::GPRCRegisterClass;
48 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
49 MachineBasicBlock::iterator MI,
50 unsigned SrcReg, int FrameIdx) const {
51 //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
52 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
53 BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addImm(FrameIdx * 8).addReg(Alpha::R30);
54 // assert(0 && "TODO");
58 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
59 MachineBasicBlock::iterator MI,
60 unsigned DestReg, int FrameIdx) const{
61 //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
62 //BuildMI(MBB, MI, Alpha::WTF, 0, DestReg);
63 BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addImm(FrameIdx * 8).addReg(Alpha::R30);
64 // assert(0 && "TODO");
67 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
68 MachineBasicBlock::iterator MI,
69 unsigned DestReg, unsigned SrcReg,
70 const TargetRegisterClass *RC) const {
71 // std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
72 if (RC == Alpha::GPRCRegisterClass) {
73 BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
74 } else if (RC == Alpha::FPRCRegisterClass) {
75 BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
77 std::cerr << "Attempt to copy register that is not GPR or FPR";
82 //===----------------------------------------------------------------------===//
83 // Stack Frame Processing methods
84 //===----------------------------------------------------------------------===//
86 // hasFP - Return true if the specified function should have a dedicated frame
87 // pointer register. This is true if the function has variable sized allocas or
88 // if frame pointer elimination is disabled.
90 static bool hasFP(MachineFunction &MF) {
91 MachineFrameInfo *MFI = MF.getFrameInfo();
92 return MFI->hasVarSizedObjects();
95 void AlphaRegisterInfo::
96 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
97 MachineBasicBlock::iterator I) const {
100 // If we have a frame pointer, turn the adjcallstackup instruction into a
101 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
103 MachineInstr *Old = I;
104 unsigned Amount = Old->getOperand(0).getImmedValue();
106 // We need to keep the stack aligned properly. To do this, we round the
107 // amount of space needed for the outgoing arguments up to the next
108 // alignment boundary.
109 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
110 Amount = (Amount+Align-1)/Align*Align;
113 // if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
114 // New=BuildMI(X86::SUB32ri, 1, X86::ESP, MachineOperand::UseAndDef)
117 // assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
118 // New=BuildMI(X86::ADD32ri, 1, X86::ESP, MachineOperand::UseAndDef)
122 // Replace the pseudo instruction with a new instruction...
131 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
134 // MachineInstr &MI = *II;
135 // MachineBasicBlock &MBB = *MI.getParent();
136 // MachineFunction &MF = *MBB.getParent();
138 // while (!MI.getOperand(i).isFrameIndex()) {
140 // assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
143 // int FrameIndex = MI.getOperand(i).getFrameIndex();
145 // // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
146 // MI.SetMachineOperandReg(i, hasFP(MF) ? PPC::R31 : PPC::R1);
148 // // Take into account whether it's an add or mem instruction
149 // unsigned OffIdx = (i == 2) ? 1 : 2;
151 // // Now add the frame object offset to the offset from r1.
152 // int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
153 // MI.getOperand(OffIdx).getImmedValue();
155 // // If we're not using a Frame Pointer that has been set to the value of the
156 // // SP before having the stack size subtracted from it, then add the stack size
157 // // to Offset to get the correct offset.
158 // Offset += MF.getFrameInfo()->getStackSize();
160 // if (Offset > 32767 || Offset < -32768) {
161 // // Insert a set of r0 with the full offset value before the ld, st, or add
162 // MachineBasicBlock *MBB = MI.getParent();
163 // MBB->insert(II, BuildMI(PPC::LIS, 1, PPC::R0).addSImm(Offset >> 16));
164 // MBB->insert(II, BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0)
166 // // convert into indexed form of the instruction
167 // // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
168 // // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
169 // unsigned NewOpcode =
170 // const_cast<std::map<unsigned, unsigned>& >(ImmToIdxMap)[MI.getOpcode()];
171 // assert(NewOpcode && "No indexed form of load or store available!");
172 // MI.setOpcode(NewOpcode);
173 // MI.SetMachineOperandReg(1, MI.getOperand(i).getReg());
174 // MI.SetMachineOperandReg(2, PPC::R0);
176 // MI.SetMachineOperandConst(OffIdx, MachineOperand::MO_SignExtendedImmed,
182 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
183 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
184 MachineBasicBlock::iterator MBBI = MBB.begin();
185 MachineFrameInfo *MFI = MF.getFrameInfo();
189 MI = BuildMI(Alpha::LDGP, 0);
190 MBB.insert(MBBI, MI);
192 // Get the number of bytes to allocate from the FrameInfo
193 unsigned NumBytes = MFI->getStackSize();
195 // Do we need to allocate space on the stack?
196 if (NumBytes == 0) return;
198 // Add the size of R30 to NumBytes size for the store of R30 to the
200 // std::cerr << "Spillsize of R30 is " << getSpillSize(Alpha::R30) << "\n";
201 // NumBytes = NumBytes + getSpillSize(Alpha::R30)/8;
203 // Update frame info to pretend that this is part of the stack...
204 MFI->setStackSize(NumBytes);
206 // adjust stack pointer: r30 -= numbytes
208 if (NumBytes <= 32767) {
209 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-NumBytes).addReg(Alpha::R30);
210 MBB.insert(MBBI, MI);
211 } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
212 long y = NumBytes / 65536;
213 if (NumBytes % 65536 > 32767)
215 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(-y).addReg(Alpha::R30);
216 MBB.insert(MBBI, MI);
217 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-(NumBytes - y * 65536)).addReg(Alpha::R30);
218 MBB.insert(MBBI, MI);
220 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
225 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
226 MachineBasicBlock &MBB) const {
227 const MachineFrameInfo *MFI = MF.getFrameInfo();
228 MachineBasicBlock::iterator MBBI = prior(MBB.end());
230 assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
231 "Can only insert epilog into returning blocks");
233 // Get the number of bytes allocated from the FrameInfo...
234 unsigned NumBytes = MFI->getStackSize();
238 if (NumBytes <= 32767) {
239 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
240 MBB.insert(MBBI, MI);
241 } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
242 long y = NumBytes / 65536;
243 if (NumBytes % 65536 > 32767)
245 MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(y).addReg(Alpha::R30);
246 MBB.insert(MBBI, MI);
247 MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes - y * 65536).addReg(Alpha::R30);
248 MBB.insert(MBBI, MI);
250 std::cerr << "Too big a stack frame at " << NumBytes << "\n";
256 #include "AlphaGenRegisterInfo.inc"
258 const TargetRegisterClass*
259 AlphaRegisterInfo::getRegClassForType(const Type* Ty) const {
260 switch (Ty->getTypeID()) {
261 default: assert(0 && "Invalid type to getClass!");
263 case Type::SByteTyID:
264 case Type::UByteTyID:
265 case Type::ShortTyID:
266 case Type::UShortTyID:
269 case Type::PointerTyID:
271 case Type::ULongTyID: return &GPRCInstance;
273 case Type::FloatTyID:
274 case Type::DoubleTyID: return &FPRCInstance;
278 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
280 std::string s(RegisterDescriptors[reg].Name);