Allow copyRegToReg to emit cross register classes copies.
[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 *DestRC,
145                                      const TargetRegisterClass *SrcRC) const {
146   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
147   if (DestRC != SrcRC) {
148     cerr << "Not yet supported!";
149     abort();
150   }
151
152   if (DestRC == Alpha::GPRCRegisterClass) {
153     BuildMI(MBB, MI, TII.get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
154   } else if (DestRC == Alpha::F4RCRegisterClass) {
155     BuildMI(MBB, MI, TII.get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
156   } else if (DestRC == Alpha::F8RCRegisterClass) {
157     BuildMI(MBB, MI, TII.get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
158   } else {
159     cerr << "Attempt to copy register that is not GPR or FPR";
160     abort();
161   }
162 }
163
164 void AlphaRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
165                                       MachineBasicBlock::iterator I,
166                                       unsigned DestReg,
167                                       const MachineInstr *Orig) const {
168   MachineInstr *MI = Orig->clone();
169   MI->getOperand(0).setReg(DestReg);
170   MBB.insert(I, MI);
171 }
172
173 const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
174                                                                          const {
175   static const unsigned CalleeSavedRegs[] = {
176     Alpha::R9, Alpha::R10,
177     Alpha::R11, Alpha::R12,
178     Alpha::R13, Alpha::R14,
179     Alpha::F2, Alpha::F3,
180     Alpha::F4, Alpha::F5,
181     Alpha::F6, Alpha::F7,
182     Alpha::F8, Alpha::F9,  0
183   };
184   return CalleeSavedRegs;
185 }
186
187 const TargetRegisterClass* const*
188 AlphaRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
189   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
190     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
191     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
192     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
193     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
194     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
195     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
196     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,  0
197   };
198   return CalleeSavedRegClasses;
199 }
200
201 BitVector AlphaRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
202   BitVector Reserved(getNumRegs());
203   Reserved.set(Alpha::R15);
204   Reserved.set(Alpha::R30);
205   Reserved.set(Alpha::R31);
206   return Reserved;
207 }
208
209 //===----------------------------------------------------------------------===//
210 // Stack Frame Processing methods
211 //===----------------------------------------------------------------------===//
212
213 // hasFP - Return true if the specified function should have a dedicated frame
214 // pointer register.  This is true if the function has variable sized allocas or
215 // if frame pointer elimination is disabled.
216 //
217 bool AlphaRegisterInfo::hasFP(const MachineFunction &MF) const {
218   MachineFrameInfo *MFI = MF.getFrameInfo();
219   return MFI->hasVarSizedObjects();
220 }
221
222 void AlphaRegisterInfo::
223 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
224                               MachineBasicBlock::iterator I) const {
225   if (hasFP(MF)) {
226     // If we have a frame pointer, turn the adjcallstackup instruction into a
227     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
228     // <amt>'
229     MachineInstr *Old = I;
230     uint64_t Amount = Old->getOperand(0).getImmedValue();
231     if (Amount != 0) {
232       // We need to keep the stack aligned properly.  To do this, we round the
233       // amount of space needed for the outgoing arguments up to the next
234       // alignment boundary.
235       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
236       Amount = (Amount+Align-1)/Align*Align;
237
238       MachineInstr *New;
239       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
240         New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
241           .addImm(-Amount).addReg(Alpha::R30);
242       } else {
243          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
244          New=BuildMI(TII.get(Alpha::LDA), Alpha::R30)
245           .addImm(Amount).addReg(Alpha::R30);
246       }
247
248       // Replace the pseudo instruction with a new instruction...
249       MBB.insert(I, New);
250     }
251   }
252
253   MBB.erase(I);
254 }
255
256 //Alpha has a slightly funny stack:
257 //Args
258 //<- incoming SP
259 //fixed locals (and spills, callee saved, etc)
260 //<- FP
261 //variable locals
262 //<- SP
263
264 void AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
265                                             int SPAdj, RegScavenger *RS) const {
266   assert(SPAdj == 0 && "Unexpected");
267
268   unsigned i = 0;
269   MachineInstr &MI = *II;
270   MachineBasicBlock &MBB = *MI.getParent();
271   MachineFunction &MF = *MBB.getParent();
272   bool FP = hasFP(MF);
273
274   while (!MI.getOperand(i).isFrameIndex()) {
275     ++i;
276     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
277   }
278
279   int FrameIndex = MI.getOperand(i).getFrameIndex();
280
281   // Add the base register of R30 (SP) or R15 (FP).
282   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
283
284   // Now add the frame object offset to the offset from the virtual frame index.
285   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
286
287   DOUT << "FI: " << FrameIndex << " Offset: " << Offset << "\n";
288
289   Offset += MF.getFrameInfo()->getStackSize();
290
291   DOUT << "Corrected Offset " << Offset
292        << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n";
293
294   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
295     DOUT << "Unconditionally using R28 for evil purposes Offset: "
296          << Offset << "\n";
297     //so in this case, we need to use a temporary register, and move the
298     //original inst off the SP/FP
299     //fix up the old:
300     MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
301     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
302     //insert the new
303     MachineInstr* nMI=BuildMI(TII.get(Alpha::LDAH), Alpha::R28)
304       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
305     MBB.insert(II, nMI);
306   } else {
307     MI.getOperand(i).ChangeToImmediate(Offset);
308   }
309 }
310
311
312 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
313   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
314   MachineBasicBlock::iterator MBBI = MBB.begin();
315   MachineFrameInfo *MFI = MF.getFrameInfo();
316   bool FP = hasFP(MF);
317
318   static int curgpdist = 0;
319
320   //handle GOP offset
321   BuildMI(MBB, MBBI, TII.get(Alpha::LDAHg), Alpha::R29)
322     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
323     .addReg(Alpha::R27).addImm(++curgpdist);
324   BuildMI(MBB, MBBI, TII.get(Alpha::LDAg), Alpha::R29)
325     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
326     .addReg(Alpha::R29).addImm(curgpdist);
327
328   //evil const_cast until MO stuff setup to handle const
329   BuildMI(MBB, MBBI, TII.get(Alpha::ALTENT))
330     .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
331
332   // Get the number of bytes to allocate from the FrameInfo
333   long NumBytes = MFI->getStackSize();
334
335   if (FP)
336     NumBytes += 8; //reserve space for the old FP
337
338   // Do we need to allocate space on the stack?
339   if (NumBytes == 0) return;
340
341   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
342   NumBytes = (NumBytes+Align-1)/Align*Align;
343
344   // Update frame info to pretend that this is part of the stack...
345   MFI->setStackSize(NumBytes);
346
347   // adjust stack pointer: r30 -= numbytes
348   NumBytes = -NumBytes;
349   if (NumBytes >= IMM_LOW) {
350     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
351       .addReg(Alpha::R30);
352   } else if (getUpper16(NumBytes) >= IMM_LOW) {
353     BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30).addImm(getUpper16(NumBytes))
354       .addReg(Alpha::R30);
355     BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(getLower16(NumBytes))
356       .addReg(Alpha::R30);
357   } else {
358     cerr << "Too big a stack frame at " << NumBytes << "\n";
359     abort();
360   }
361
362   //now if we need to, save the old FP and set the new
363   if (FP)
364   {
365     BuildMI(MBB, MBBI, TII.get(Alpha::STQ))
366       .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
367     //this must be the last instr in the prolog
368     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R15)
369       .addReg(Alpha::R30).addReg(Alpha::R30);
370   }
371
372 }
373
374 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
375                                      MachineBasicBlock &MBB) const {
376   const MachineFrameInfo *MFI = MF.getFrameInfo();
377   MachineBasicBlock::iterator MBBI = prior(MBB.end());
378   assert(MBBI->getOpcode() == Alpha::RETDAG ||
379          MBBI->getOpcode() == Alpha::RETDAGp
380          && "Can only insert epilog into returning blocks");
381
382   bool FP = hasFP(MF);
383
384   // Get the number of bytes allocated from the FrameInfo...
385   long NumBytes = MFI->getStackSize();
386
387   //now if we need to, restore the old FP
388   if (FP)
389   {
390     //copy the FP into the SP (discards allocas)
391     BuildMI(MBB, MBBI, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
392       .addReg(Alpha::R15);
393     //restore the FP
394     BuildMI(MBB, MBBI, TII.get(Alpha::LDQ), Alpha::R15).addImm(0).addReg(Alpha::R15);
395   }
396
397    if (NumBytes != 0)
398      {
399        if (NumBytes <= IMM_HIGH) {
400          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
401            .addReg(Alpha::R30);
402        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
403          BuildMI(MBB, MBBI, TII.get(Alpha::LDAH), Alpha::R30)
404            .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
405          BuildMI(MBB, MBBI, TII.get(Alpha::LDA), Alpha::R30)
406            .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
407        } else {
408          cerr << "Too big a stack frame at " << NumBytes << "\n";
409          abort();
410        }
411      }
412 }
413
414 unsigned AlphaRegisterInfo::getRARegister() const {
415   assert(0 && "What is the return address register");
416   return 0;
417 }
418
419 unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
420   return hasFP(MF) ? Alpha::R15 : Alpha::R30;
421 }
422
423 unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
424   assert(0 && "What is the exception register");
425   return 0;
426 }
427
428 unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
429   assert(0 && "What is the exception handler register");
430   return 0;
431 }
432
433 #include "AlphaGenRegisterInfo.inc"
434
435 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
436 {
437   std::string s(RegisterDescriptors[reg].Name);
438   return s;
439 }