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