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