Correct stack stuff for FP
[oota-llvm.git] / lib / Target / Alpha / AlphaRegisterInfo.cpp
1 //===- AlphaRegisterInfo.cpp - Alpha Register Information ---*- C++ -*-----===//
2 // 
3 //                     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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Alpha implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "reginfo"
15 #include "Alpha.h"
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"
29 #include <cstdlib>
30 #include <iostream>
31 using namespace llvm;
32
33
34 AlphaRegisterInfo::AlphaRegisterInfo()
35   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
36 {
37 }
38
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;
44 }
45
46 void 
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   if (getClass(SrcReg) == Alpha::FPRCRegisterClass)
53     BuildMI(MBB, MI, Alpha::STT, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
54   else if (getClass(SrcReg) == Alpha::GPRCRegisterClass)
55     BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
56   else
57     abort();
58 }
59
60 void
61 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
62                                         MachineBasicBlock::iterator MI,
63                                         unsigned DestReg, int FrameIdx) const{
64   //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
65   if (getClass(DestReg) == Alpha::FPRCRegisterClass)
66     BuildMI(MBB, MI, Alpha::LDT, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
67   else if (getClass(DestReg) == Alpha::GPRCRegisterClass)
68     BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
69   else
70     abort();
71 }
72
73 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
74                                      MachineBasicBlock::iterator MI,
75                                      unsigned DestReg, unsigned SrcReg,
76                                      const TargetRegisterClass *RC) const {
77   //  std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
78   if (RC == Alpha::GPRCRegisterClass) {
79     BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
80   } else if (RC == Alpha::FPRCRegisterClass) {
81     BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
82   } else { 
83     std::cerr << "Attempt to copy register that is not GPR or FPR";
84      abort();
85   }
86 }
87
88 //===----------------------------------------------------------------------===//
89 // Stack Frame Processing methods
90 //===----------------------------------------------------------------------===//
91
92 // hasFP - Return true if the specified function should have a dedicated frame
93 // pointer register.  This is true if the function has variable sized allocas or
94 // if frame pointer elimination is disabled.
95 //
96 static bool hasFP(MachineFunction &MF) {
97   MachineFrameInfo *MFI = MF.getFrameInfo();
98   return MFI->hasVarSizedObjects();
99 }
100
101 void AlphaRegisterInfo::
102 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
103                               MachineBasicBlock::iterator I) const {
104   if (hasFP(MF)) {
105     assert(0 && "TODO");
106     // If we have a frame pointer, turn the adjcallstackup instruction into a
107     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
108     // <amt>'
109     MachineInstr *Old = I;
110     unsigned Amount = Old->getOperand(0).getImmedValue();
111     if (Amount != 0) {
112       // We need to keep the stack aligned properly.  To do this, we round the
113       // amount of space needed for the outgoing arguments up to the next
114       // alignment boundary.
115       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
116       Amount = (Amount+Align-1)/Align*Align;
117
118 //    MachineInstr *New;
119 //       if (Old->getOpcode() == X86::ADJCALLSTACKDOWN) {
120 //      New=BuildMI(X86::SUB32ri, 1, X86::ESP, MachineOperand::UseAndDef)
121 //               .addZImm(Amount);
122 //       } else {
123 //      assert(Old->getOpcode() == X86::ADJCALLSTACKUP);
124 //      New=BuildMI(X86::ADD32ri, 1, X86::ESP, MachineOperand::UseAndDef)
125 //               .addZImm(Amount);
126 //       }
127
128       // Replace the pseudo instruction with a new instruction...
129       //MBB.insert(I, New);
130       abort();
131     }
132   }
133
134   MBB.erase(I);
135 }
136
137 void
138 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
139   unsigned i = 0;
140   MachineInstr &MI = *II;
141   MachineBasicBlock &MBB = *MI.getParent();
142   MachineFunction &MF = *MBB.getParent();
143   
144   while (!MI.getOperand(i).isFrameIndex()) {
145     ++i;
146     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
147   }
148
149   int FrameIndex = MI.getOperand(i).getFrameIndex();
150
151   // Add the base register of R30 (SP) or R15 (FP).
152   MI.SetMachineOperandReg(i + 1, hasFP(MF) ? Alpha::R15 : Alpha::R30);
153   
154   // Now add the frame object offset to the offset from r1.
155   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
156
157   // If we're not using a Frame Pointer that has been set to the value of the
158   // SP before having the stack size subtracted from it, then add the stack size
159   // to Offset to get the correct offset.
160   Offset += MF.getFrameInfo()->getStackSize();
161
162    if (Offset > 32767 || Offset < -32768) {
163      std::cerr << "Offset needs to be " << Offset << "\n";
164      assert(0 && "stack too big");
165    } else {
166      MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
167    }
168 }
169
170
171 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
172   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
173   MachineBasicBlock::iterator MBBI = MBB.begin();
174   MachineFrameInfo *MFI = MF.getFrameInfo();
175   MachineInstr *MI;
176   
177   //handle GOP offset
178   MI = BuildMI(Alpha::LDGP, 0);
179   MBB.insert(MBBI, MI);
180
181   // Get the number of bytes to allocate from the FrameInfo
182   unsigned NumBytes = MFI->getStackSize();
183
184   if (MFI->hasCalls()) {
185     // We reserve argument space for call sites in the function immediately on 
186     // entry to the current function.  This eliminates the need for add/sub 
187     // brackets around call sites.
188     NumBytes += MFI->getMaxCallFrameSize();
189     std::cerr << "Added " << MFI->getMaxCallFrameSize() << " to the stack due to calls\n";
190   }
191
192   // Do we need to allocate space on the stack?
193   if (NumBytes == 0) return;
194
195   // Add the size of R30 to  NumBytes size for the store of R30 to the 
196   // stack
197 //   std::cerr << "Spillsize of R30 is " << getSpillSize(Alpha::R30) << "\n";
198 //   NumBytes = NumBytes + getSpillSize(Alpha::R30)/8;
199
200   // Update frame info to pretend that this is part of the stack...
201   MFI->setStackSize(NumBytes);
202   
203   // adjust stack pointer: r30 -= numbytes
204   
205   if (NumBytes <= 32767) {
206     MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-NumBytes).addReg(Alpha::R30);
207     MBB.insert(MBBI, MI);
208   } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
209     long y = NumBytes / 65536;
210     if (NumBytes % 65536 > 32767)
211       ++y;
212     MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(-y).addReg(Alpha::R30);
213     MBB.insert(MBBI, MI);
214     MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(-(NumBytes - y * 65536)).addReg(Alpha::R30);
215     MBB.insert(MBBI, MI);
216   } else {
217     std::cerr << "Too big a stack frame at " << NumBytes << "\n";
218     abort();
219   }
220 }
221
222 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
223                                      MachineBasicBlock &MBB) const {
224   const MachineFrameInfo *MFI = MF.getFrameInfo();
225   MachineBasicBlock::iterator MBBI = prior(MBB.end());
226   MachineInstr *MI;
227   assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
228          "Can only insert epilog into returning blocks");
229   
230   // Get the number of bytes allocated from the FrameInfo...
231   unsigned NumBytes = MFI->getStackSize();
232
233    if (NumBytes != 0) 
234      {
235        if (NumBytes <= 32767) {
236          MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
237          MBB.insert(MBBI, MI);
238        } else if ((unsigned long)NumBytes <= (unsigned long)32767 * (unsigned long)65536) {
239          long y = NumBytes / 65536;
240          if (NumBytes % 65536 > 32767)
241            ++y;
242          MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(y).addReg(Alpha::R30);
243          MBB.insert(MBBI, MI);
244          MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes - y * 65536).addReg(Alpha::R30);
245          MBB.insert(MBBI, MI);
246        } else {
247          std::cerr << "Too big a stack frame at " << NumBytes << "\n";
248          abort();
249        }
250      }
251 }
252
253 #include "AlphaGenRegisterInfo.inc"
254
255 const TargetRegisterClass*
256 AlphaRegisterInfo::getRegClassForType(const Type* Ty) const {
257   switch (Ty->getTypeID()) {
258     default:              assert(0 && "Invalid type to getClass!");
259     case Type::BoolTyID:
260     case Type::SByteTyID:
261     case Type::UByteTyID:
262     case Type::ShortTyID:
263     case Type::UShortTyID:
264     case Type::IntTyID:
265     case Type::UIntTyID:
266     case Type::PointerTyID:
267     case Type::LongTyID:
268     case Type::ULongTyID:  return &GPRCInstance;
269      
270   case Type::FloatTyID:
271   case Type::DoubleTyID: return &FPRCInstance;
272   }
273 }
274
275 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
276 {
277   std::string s(RegisterDescriptors[reg].Name);
278   return s;
279 }