Long live the exception handling!
[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/CodeGen/MachineLocation.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/ADT/BitVector.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include <cstdlib>
34 using namespace llvm;
35
36 //These describe LDAx
37 static const int IMM_LOW  = -32768;
38 static const int IMM_HIGH = 32767;
39 static const int IMM_MULT = 65536;
40
41 static long getUpper16(long l)
42 {
43   long y = l / IMM_MULT;
44   if (l % IMM_MULT > IMM_HIGH)
45     ++y;
46   return y;
47 }
48
49 static long getLower16(long l)
50 {
51   long h = getUpper16(l);
52   return l - h * IMM_MULT;
53 }
54
55 AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
56   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
57     TII(tii)
58 {
59 }
60
61 void
62 AlphaRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
63                                        MachineBasicBlock::iterator MI,
64                                        unsigned SrcReg, int FrameIdx,
65                                        const TargetRegisterClass *RC) const {
66   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
67   //     << FrameIdx << "\n";
68   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
69   if (RC == Alpha::F4RCRegisterClass)
70     BuildMI(MBB, MI, TII.get(Alpha::STS))
71       .addReg(SrcReg, false, false, true)
72       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
73   else if (RC == Alpha::F8RCRegisterClass)
74     BuildMI(MBB, MI, TII.get(Alpha::STT))
75       .addReg(SrcReg, false, false, true)
76       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
77   else if (RC == Alpha::GPRCRegisterClass)
78     BuildMI(MBB, MI, TII.get(Alpha::STQ))
79       .addReg(SrcReg, false, false, true)
80       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
81   else
82     abort();
83 }
84
85 void
86 AlphaRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
87                                         MachineBasicBlock::iterator MI,
88                                         unsigned DestReg, int FrameIdx,
89                                         const TargetRegisterClass *RC) const {
90   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
91   //     << FrameIdx << "\n";
92   if (RC == Alpha::F4RCRegisterClass)
93     BuildMI(MBB, MI, TII.get(Alpha::LDS), DestReg)
94       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
95   else if (RC == Alpha::F8RCRegisterClass)
96     BuildMI(MBB, MI, TII.get(Alpha::LDT), DestReg)
97       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
98   else if (RC == Alpha::GPRCRegisterClass)
99     BuildMI(MBB, MI, TII.get(Alpha::LDQ), DestReg)
100       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
101   else
102     abort();
103 }
104
105 MachineInstr *AlphaRegisterInfo::foldMemoryOperand(MachineInstr *MI,
106                                                  unsigned OpNum,
107                                                  int FrameIndex) const {
108    // Make sure this is a reg-reg copy.
109    unsigned Opc = MI->getOpcode();
110
111    MachineInstr *NewMI = NULL;
112    switch(Opc) {
113    default:
114      break;
115    case Alpha::BISr:
116    case Alpha::CPYSS:
117    case Alpha::CPYST:
118      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
119        if (OpNum == 0) {  // move -> store
120          unsigned InReg = MI->getOperand(1).getReg();
121          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
122            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
123          NewMI = BuildMI(TII.get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
124            .addReg(Alpha::F31);
125        } else {           // load -> move
126          unsigned OutReg = MI->getOperand(0).getReg();
127          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
128            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
129          NewMI = BuildMI(TII.get(Opc), OutReg).addFrameIndex(FrameIndex)
130            .addReg(Alpha::F31);
131        }
132      }
133      break;
134    }
135   if (NewMI)
136     NewMI->copyKillDeadInfo(MI);
137   return 0;
138 }
139
140
141 void AlphaRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
142                                      MachineBasicBlock::iterator MI,
143                                      unsigned DestReg, unsigned SrcReg,
144                                      const TargetRegisterClass *RC) const {
145   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
146   if (RC == Alpha::GPRCRegisterClass) {
147     BuildMI(MBB, MI, TII.get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
148   } else if (RC == Alpha::F4RCRegisterClass) {
149     BuildMI(MBB, MI, TII.get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
150   } else if (RC == Alpha::F8RCRegisterClass) {
151     BuildMI(MBB, MI, TII.get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
152   } else {
153     cerr << "Attempt to copy register that is not GPR or FPR";
154     abort();
155   }
156 }
157
158 void AlphaRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
159                                       MachineBasicBlock::iterator I,
160                                       unsigned DestReg,
161                                       const MachineInstr *Orig) const {
162   MachineInstr *MI = Orig->clone();
163   MI->getOperand(0).setReg(DestReg);
164   MBB.insert(I, MI);
165 }
166
167 const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
168                                                                          const {
169   static const unsigned CalleeSavedRegs[] = {
170     Alpha::R9, Alpha::R10,
171     Alpha::R11, Alpha::R12,
172     Alpha::R13, Alpha::R14,
173     Alpha::F2, Alpha::F3,
174     Alpha::F4, Alpha::F5,
175     Alpha::F6, Alpha::F7,
176     Alpha::F8, Alpha::F9,  0
177   };
178   return CalleeSavedRegs;
179 }
180
181 const TargetRegisterClass* const*
182 AlphaRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
183   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
184     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
185     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
186     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
187     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
188     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
189     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
190     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,  0
191   };
192   return CalleeSavedRegClasses;
193 }
194
195 BitVector AlphaRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
196   BitVector Reserved(getNumRegs());
197   Reserved.set(Alpha::R15);
198   Reserved.set(Alpha::R30);
199   Reserved.set(Alpha::R31);
200   return Reserved;
201 }
202
203 //===----------------------------------------------------------------------===//
204 // Stack Frame Processing methods
205 //===----------------------------------------------------------------------===//
206
207 // hasFP - Return true if the specified function should have a dedicated frame
208 // pointer register.  This is true if the function has variable sized allocas or
209 // if frame pointer elimination is disabled.
210 //
211 bool AlphaRegisterInfo::hasFP(const MachineFunction &MF) const {
212   MachineFrameInfo *MFI = MF.getFrameInfo();
213   return MFI->hasVarSizedObjects();
214 }
215
216 void AlphaRegisterInfo::
217 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
218                               MachineBasicBlock::iterator I) const {
219   if (hasFP(MF)) {
220     // If we have a frame pointer, turn the adjcallstackup instruction into a
221     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
222     // <amt>'
223     MachineInstr *Old = I;
224     uint64_t Amount = Old->getOperand(0).getImmedValue();
225     if (Amount != 0) {
226       // We need to keep the stack aligned properly.  To do this, we round the
227       // amount of space needed for the outgoing arguments up to the next
228       // alignment boundary.
229       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
230       Amount = (Amount+Align-1)/Align*Align;
231
232       MachineInstr *New;
233       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
234         New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
235           .addImm(-Amount).addReg(Alpha::R30);
236       } else {
237          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
238          New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
239           .addImm(Amount).addReg(Alpha::R30);
240       }
241
242       // Replace the pseudo instruction with a new instruction...
243       MBB.insert(I, New);
244     }
245   }
246
247   MBB.erase(I);
248 }
249
250 //Alpha has a slightly funny stack:
251 //Args
252 //<- incoming SP
253 //fixed locals (and spills, callee saved, etc)
254 //<- FP
255 //variable locals
256 //<- SP
257
258 void AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
259                                             int SPAdj, RegScavenger *RS) const {
260   assert(SPAdj == 0 && "Unexpected");
261
262   unsigned i = 0;
263   MachineInstr &MI = *II;
264   MachineBasicBlock &MBB = *MI.getParent();
265   MachineFunction &MF = *MBB.getParent();
266   bool FP = hasFP(MF);
267
268   while (!MI.getOperand(i).isFrameIndex()) {
269     ++i;
270     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
271   }
272
273   int FrameIndex = MI.getOperand(i).getFrameIndex();
274
275   // Add the base register of R30 (SP) or R15 (FP).
276   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
277
278   // Now add the frame object offset to the offset from the virtual frame index.
279   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
280
281   DOUT << "FI: " << FrameIndex << " Offset: " << Offset << "\n";
282
283   Offset += MF.getFrameInfo()->getStackSize();
284
285   DOUT << "Corrected Offset " << Offset
286        << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n";
287
288   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
289     DOUT << "Unconditionally using R28 for evil purposes Offset: "
290          << Offset << "\n";
291     //so in this case, we need to use a temporary register, and move the
292     //original inst off the SP/FP
293     //fix up the old:
294     MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
295     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
296     //insert the new
297     MachineInstr* nMI=BuildMI(TII.get(Alpha::LDAH), Alpha::R28)
298       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
299     MBB.insert(II, nMI);
300   } else {
301     MI.getOperand(i).ChangeToImmediate(Offset);
302   }
303 }
304
305
306 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
307   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
308   MachineBasicBlock::iterator MBBI = MBB.begin();
309   MachineFrameInfo *MFI = MF.getFrameInfo();
310   bool FP = hasFP(MF);
311
312   static int curgpdist = 0;
313
314   //handle GOP offset
315   BuildMI(MBB, MBBI, TII.get(Alpha::LDAHg), Alpha::R29)
316     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
317     .addReg(Alpha::R27).addImm(++curgpdist);
318   BuildMI(MBB, MBBI, TII.get(Alpha::LDAg), Alpha::R29)
319     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
320     .addReg(Alpha::R29).addImm(curgpdist);
321
322   //evil const_cast until MO stuff setup to handle const
323   BuildMI(MBB, MBBI, TII.get(Alpha::ALTENT))
324     .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
325
326   // Get the number of bytes to allocate from the FrameInfo
327   long NumBytes = MFI->getStackSize();
328
329   if (FP)
330     NumBytes += 8; //reserve space for the old FP
331
332   // Do we need to allocate space on the stack?
333   if (NumBytes == 0) return;
334
335   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
336   NumBytes = (NumBytes+Align-1)/Align*Align;
337
338   // Update frame info to pretend that this is part of the stack...
339   MFI->setStackSize(NumBytes);
340
341   // adjust stack pointer: r30 -= numbytes
342   NumBytes = -NumBytes;
343   if (NumBytes >= IMM_LOW) {
344     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
345       .addReg(Alpha::R30);
346   } else if (getUpper16(NumBytes) >= IMM_LOW) {
347     BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30).addImm(getUpper16(NumBytes))
348       .addReg(Alpha::R30);
349     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(getLower16(NumBytes))
350       .addReg(Alpha::R30);
351   } else {
352     cerr << "Too big a stack frame at " << NumBytes << "\n";
353     abort();
354   }
355
356   //now if we need to, save the old FP and set the new
357   if (FP)
358   {
359     BuildMI(MBB, MBBI, TII.get(Alpha::STQ))
360       .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
361     //this must be the last instr in the prolog
362     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R15)
363       .addReg(Alpha::R30).addReg(Alpha::R30);
364   }
365
366 }
367
368 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
369                                      MachineBasicBlock &MBB) const {
370   const MachineFrameInfo *MFI = MF.getFrameInfo();
371   MachineBasicBlock::iterator MBBI = prior(MBB.end());
372   assert(MBBI->getOpcode() == Alpha::RETDAG ||
373          MBBI->getOpcode() == Alpha::RETDAGp
374          && "Can only insert epilog into returning blocks");
375
376   bool FP = hasFP(MF);
377
378   // Get the number of bytes allocated from the FrameInfo...
379   long NumBytes = MFI->getStackSize();
380
381   //now if we need to, restore the old FP
382   if (FP)
383   {
384     //copy the FP into the SP (discards allocas)
385     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
386       .addReg(Alpha::R15);
387     //restore the FP
388     BuildMI(MBB, MBBI, TII.get(Alpha::LDQ), Alpha::R15).addImm(0).addReg(Alpha::R15);
389   }
390
391    if (NumBytes != 0)
392      {
393        if (NumBytes <= IMM_HIGH) {
394          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
395            .addReg(Alpha::R30);
396        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
397          BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30)
398            .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
399          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30)
400            .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
401        } else {
402          cerr << "Too big a stack frame at " << NumBytes << "\n";
403          abort();
404        }
405      }
406 }
407
408 unsigned AlphaRegisterInfo::getRARegister() const {
409   assert(0 && "What is the return address register");
410   return 0;
411 }
412
413 unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
414   return hasFP(MF) ? Alpha::R15 : Alpha::R30;
415 }
416
417 unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
418   assert(0 && "What is the exception register");
419   return 0;
420 }
421
422 unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
423   assert(0 && "What is the exception handler register");
424   return 0;
425 }
426
427 #include "AlphaGenRegisterInfo.inc"
428
429 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
430 {
431   std::string s(RegisterDescriptors[reg].Name);
432   return s;
433 }