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