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