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