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