fix data size stuff for architectures with bit challenged data types
[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 //These describe LDAx
34 static const int64_t IMM_LOW  = 0xffffffffffff8000;
35 static const int IMM_HIGH = 0x0000000000007fff;
36 static const int IMM_MULT = 65536;
37
38 static long getUpper16(long l)
39 {
40   long y = l / IMM_MULT;
41   if (l % IMM_MULT > IMM_HIGH)
42     ++y;
43   return y;
44 }
45
46 static long getLower16(long l)
47 {
48   long h = getUpper16(l);
49   return l - h * IMM_MULT;
50 }
51
52 AlphaRegisterInfo::AlphaRegisterInfo()
53   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP)
54 {
55 }
56
57 static const TargetRegisterClass *getClass(unsigned SrcReg) {
58   if (Alpha::FPRCRegisterClass->contains(SrcReg))
59     return Alpha::FPRCRegisterClass;
60   assert(Alpha::GPRCRegisterClass->contains(SrcReg) && "Reg not FPR or GPR");
61   return Alpha::GPRCRegisterClass;
62 }
63
64 void 
65 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
66                                        MachineBasicBlock::iterator MI,
67                                        unsigned SrcReg, int FrameIdx) const {
68   //std::cerr << "Trying to store " << getPrettyName(SrcReg) << " to " << FrameIdx << "\n";
69   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
70   if (getClass(SrcReg) == Alpha::FPRCRegisterClass)
71     BuildMI(MBB, MI, Alpha::STT, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
72   else if (getClass(SrcReg) == Alpha::GPRCRegisterClass)
73     BuildMI(MBB, MI, Alpha::STQ, 3).addReg(SrcReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
74   else
75     abort();
76 }
77
78 void
79 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
80                                         MachineBasicBlock::iterator MI,
81                                         unsigned DestReg, int FrameIdx) const{
82   //std::cerr << "Trying to load " << getPrettyName(DestReg) << " to " << FrameIdx << "\n";
83   if (getClass(DestReg) == Alpha::FPRCRegisterClass)
84     BuildMI(MBB, MI, Alpha::LDT, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
85   else if (getClass(DestReg) == Alpha::GPRCRegisterClass)
86     BuildMI(MBB, MI, Alpha::LDQ, 2, DestReg).addFrameIndex(FrameIdx).addReg(Alpha::F31);
87   else
88     abort();
89 }
90
91 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
92                                      MachineBasicBlock::iterator MI,
93                                      unsigned DestReg, unsigned SrcReg,
94                                      const TargetRegisterClass *RC) const {
95   //  std::cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
96   if (RC == Alpha::GPRCRegisterClass) {
97     BuildMI(MBB, MI, Alpha::BIS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
98   } else if (RC == Alpha::FPRCRegisterClass) {
99     BuildMI(MBB, MI, Alpha::CPYS, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
100   } else { 
101     std::cerr << "Attempt to copy register that is not GPR or FPR";
102      abort();
103   }
104 }
105
106 //===----------------------------------------------------------------------===//
107 // Stack Frame Processing methods
108 //===----------------------------------------------------------------------===//
109
110 // hasFP - Return true if the specified function should have a dedicated frame
111 // pointer register.  This is true if the function has variable sized allocas or
112 // if frame pointer elimination is disabled.
113 //
114 static bool hasFP(MachineFunction &MF) {
115   MachineFrameInfo *MFI = MF.getFrameInfo();
116   return MFI->hasVarSizedObjects();
117 }
118
119 void AlphaRegisterInfo::
120 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
121                               MachineBasicBlock::iterator I) const {
122   if (hasFP(MF)) {
123     // If we have a frame pointer, turn the adjcallstackup instruction into a
124     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
125     // <amt>'
126     MachineInstr *Old = I;
127     unsigned Amount = Old->getOperand(0).getImmedValue();
128     if (Amount != 0) {
129       // We need to keep the stack aligned properly.  To do this, we round the
130       // amount of space needed for the outgoing arguments up to the next
131       // alignment boundary.
132       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
133       Amount = (Amount+Align-1)/Align*Align;
134
135       MachineInstr *New;
136       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
137         New=BuildMI(Alpha::LDA, 2, Alpha::R30)
138           .addImm(-Amount).addReg(Alpha::R30);
139       } else {
140         assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
141         New=BuildMI(Alpha::LDA, 2, Alpha::R30)
142           .addImm(Amount).addReg(Alpha::R30);
143       }
144       
145       // Replace the pseudo instruction with a new instruction...
146       MBB.insert(I, New);
147     }
148   }
149
150   MBB.erase(I);
151 }
152
153 //Alpha has a slightly funny stack:
154 //Args 
155 //<- incoming SP
156 //fixed locals (and spills, callee saved, etc)
157 //<- FP
158 //variable locals
159 //<- SP
160
161 void
162 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
163   unsigned i = 0;
164   MachineInstr &MI = *II;
165   MachineBasicBlock &MBB = *MI.getParent();
166   MachineFunction &MF = *MBB.getParent();
167   bool FP = hasFP(MF);
168
169   while (!MI.getOperand(i).isFrameIndex()) {
170     ++i;
171     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
172   }
173
174   int FrameIndex = MI.getOperand(i).getFrameIndex();
175
176   // Add the base register of R30 (SP) or R15 (FP).
177   MI.SetMachineOperandReg(i + 1, FP ? Alpha::R15 : Alpha::R30);
178   
179   // Now add the frame object offset to the offset from the virtual frame index.
180   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
181
182   DEBUG(std::cerr << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
183
184   Offset += MF.getFrameInfo()->getStackSize();
185   
186   DEBUG(std::cerr << "Corrected Offset " << Offset << 
187         " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
188
189   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
190     //so in this case, we need to use a temporary register, and move the original
191     //inst off the SP/FP
192     //fix up the old:
193     MI.SetMachineOperandReg(i + 1, Alpha::R28);
194     MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, 
195                               getLower16(Offset));
196     //insert the new
197     MachineInstr* nMI=BuildMI(Alpha::LDAH, 2, Alpha::R28)
198       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
199     MBB.insert(--II, nMI);
200   } else {
201     MI.SetMachineOperandConst(i, MachineOperand::MO_SignExtendedImmed, Offset);
202   }
203 }
204
205
206 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
207   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
208   MachineBasicBlock::iterator MBBI = MBB.begin();
209   MachineFrameInfo *MFI = MF.getFrameInfo();
210   MachineInstr *MI;
211   bool FP = hasFP(MF);
212   
213   //handle GOP offset
214   MI = BuildMI(Alpha::LDGP, 0);
215   MBB.insert(MBBI, MI);
216
217   // Get the number of bytes to allocate from the FrameInfo
218   long NumBytes = MFI->getStackSize();
219
220   if (MFI->hasCalls() && !FP) {
221     // We reserve argument space for call sites in the function immediately on 
222     // entry to the current function.  This eliminates the need for add/sub 
223     // brackets around call sites.
224     //If there is a frame pointer, then we don't do this
225     NumBytes += MFI->getMaxCallFrameSize();
226     DEBUG(std::cerr << "Added " << MFI->getMaxCallFrameSize() 
227           << " to the stack due to calls\n");
228   }
229
230   if (FP)
231     NumBytes += 8; //reserve space for the old FP
232
233   // Do we need to allocate space on the stack?
234   if (NumBytes == 0) return;
235
236   // Update frame info to pretend that this is part of the stack...
237   MFI->setStackSize(NumBytes);
238
239   // adjust stack pointer: r30 -= numbytes
240   NumBytes = -NumBytes;
241   if (NumBytes >= IMM_LOW) {
242     MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
243     MBB.insert(MBBI, MI);
244   } else if (getUpper16(NumBytes) >= IMM_LOW) {
245     MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
246     MBB.insert(MBBI, MI);
247     MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
248     MBB.insert(MBBI, MI);
249   } else {
250     std::cerr << "Too big a stack frame at " << NumBytes << "\n";
251     abort();
252   }
253
254   //now if we need to, save the old FP and set the new
255   if (FP)
256   {
257     MI=BuildMI(Alpha::STQ, 3).addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
258     MBB.insert(MBBI, MI);
259     //this must be the last instr in the prolog
260     MI=BuildMI(Alpha::BIS, 2, Alpha::R15).addReg(Alpha::R30).addReg(Alpha::R30);
261     MBB.insert(MBBI, MI);
262   }
263
264 }
265
266 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
267                                      MachineBasicBlock &MBB) const {
268   const MachineFrameInfo *MFI = MF.getFrameInfo();
269   MachineBasicBlock::iterator MBBI = prior(MBB.end());
270   MachineInstr *MI;
271   assert((MBBI->getOpcode() == Alpha::RET || MBBI->getOpcode() == Alpha::RETURN) &&
272          "Can only insert epilog into returning blocks");
273   
274   bool FP = hasFP(MF);
275  
276   // Get the number of bytes allocated from the FrameInfo...
277   long NumBytes = MFI->getStackSize();
278
279   //now if we need to, restore the old FP
280   if (FP)
281   {
282     //copy the FP into the SP (discards allocas)
283     MI=BuildMI(Alpha::BIS, 2, Alpha::R30).addReg(Alpha::R15).addReg(Alpha::R15);
284     MBB.insert(MBBI, MI);
285     //restore the FP
286     MI=BuildMI(Alpha::LDQ, 2, Alpha::R15).addImm(0).addReg(Alpha::R15);
287     MBB.insert(MBBI, MI);
288   }
289
290    if (NumBytes != 0) 
291      {
292        if (NumBytes <= IMM_HIGH) {
293          MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(NumBytes).addReg(Alpha::R30);
294          MBB.insert(MBBI, MI);
295        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
296          MI=BuildMI(Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
297          MBB.insert(MBBI, MI);
298          MI=BuildMI(Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes)).addReg(Alpha::R30);
299          MBB.insert(MBBI, MI);
300        } else {
301          std::cerr << "Too big a stack frame at " << NumBytes << "\n";
302          abort();
303        }
304      }
305 }
306
307 #include "AlphaGenRegisterInfo.inc"
308
309 const TargetRegisterClass*
310 AlphaRegisterInfo::getRegClassForType(const Type* Ty) const {
311   switch (Ty->getTypeID()) {
312     default:              assert(0 && "Invalid type to getClass!");
313     case Type::BoolTyID:
314     case Type::SByteTyID:
315     case Type::UByteTyID:
316     case Type::ShortTyID:
317     case Type::UShortTyID:
318     case Type::IntTyID:
319     case Type::UIntTyID:
320     case Type::PointerTyID:
321     case Type::LongTyID:
322     case Type::ULongTyID:  return &GPRCInstance;
323      
324   case Type::FloatTyID:
325   case Type::DoubleTyID: return &FPRCInstance;
326   }
327 }
328
329 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
330 {
331   std::string s(RegisterDescriptors[reg].Name);
332   return s;
333 }