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