Add support to locate local variables in frames (early version.)
[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).addGlobalAddress(const_cast<Function*>(MF.getFunction()), true);
267
268   // Get the number of bytes to allocate from the FrameInfo
269   long NumBytes = MFI->getStackSize();
270
271   if (MFI->hasCalls() && !FP) {
272     // We reserve argument space for call sites in the function immediately on
273     // entry to the current function.  This eliminates the need for add/sub
274     // brackets around call sites.
275     //If there is a frame pointer, then we don't do this
276     NumBytes += MFI->getMaxCallFrameSize();
277     DEBUG(std::cerr << "Added " << MFI->getMaxCallFrameSize()
278           << " to the stack due to calls\n");
279   }
280
281   if (FP)
282     NumBytes += 8; //reserve space for the old FP
283
284   // Do we need to allocate space on the stack?
285   if (NumBytes == 0) return;
286
287   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
288   NumBytes = (NumBytes+Align-1)/Align*Align;
289
290   // Update frame info to pretend that this is part of the stack...
291   MFI->setStackSize(NumBytes);
292
293   // adjust stack pointer: r30 -= numbytes
294   NumBytes = -NumBytes;
295   if (NumBytes >= IMM_LOW) {
296     BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(NumBytes)
297       .addReg(Alpha::R30);
298   } else if (getUpper16(NumBytes) >= IMM_LOW) {
299     BuildMI(MBB, MBBI, Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes))
300       .addReg(Alpha::R30);
301     BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes))
302       .addReg(Alpha::R30);
303   } else {
304     std::cerr << "Too big a stack frame at " << NumBytes << "\n";
305     abort();
306   }
307
308   //now if we need to, save the old FP and set the new
309   if (FP)
310   {
311     BuildMI(MBB, MBBI, Alpha::STQ, 3).addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
312     //this must be the last instr in the prolog
313     BuildMI(MBB, MBBI, Alpha::BIS, 2, Alpha::R15).addReg(Alpha::R30).addReg(Alpha::R30);
314   }
315
316 }
317
318 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
319                                      MachineBasicBlock &MBB) const {
320   const MachineFrameInfo *MFI = MF.getFrameInfo();
321   MachineBasicBlock::iterator MBBI = prior(MBB.end());
322   assert(MBBI->getOpcode() == Alpha::RETDAG
323          && "Can only insert epilog into returning blocks");
324
325   bool FP = hasFP(MF);
326
327   // Get the number of bytes allocated from the FrameInfo...
328   long NumBytes = MFI->getStackSize();
329
330   //now if we need to, restore the old FP
331   if (FP)
332   {
333     //copy the FP into the SP (discards allocas)
334     BuildMI(MBB, MBBI, Alpha::BIS, 2, Alpha::R30).addReg(Alpha::R15)
335       .addReg(Alpha::R15);
336     //restore the FP
337     BuildMI(MBB, MBBI, Alpha::LDQ, 2, Alpha::R15).addImm(0).addReg(Alpha::R15);
338   }
339
340    if (NumBytes != 0)
341      {
342        if (NumBytes <= IMM_HIGH) {
343          BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(NumBytes)
344            .addReg(Alpha::R30);
345        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
346          BuildMI(MBB, MBBI, Alpha::LDAH, 2, Alpha::R30)
347            .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
348          BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30)
349            .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
350        } else {
351          std::cerr << "Too big a stack frame at " << NumBytes << "\n";
352          abort();
353        }
354      }
355 }
356
357 void AlphaRegisterInfo::getLocation(MachineFunction &MF, unsigned Index,
358                                     MachineLocation &ML) const {
359   assert(0 && "Needs to be defined for target");
360   MachineFrameInfo *MFI = MF.getFrameInfo();
361   bool FP = hasFP(MF);
362   
363   // FIXME - Needs to handle register variables.
364   // FIXME - Faking that llvm number is same as gcc numbering.
365   ML.set((FP ? Alpha::R15 : Alpha::R30) - Alpha::R0,
366          MFI->getObjectOffset(Index) + MFI->getStackSize());
367 }
368
369 #include "AlphaGenRegisterInfo.inc"
370
371 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
372 {
373   std::string s(RegisterDescriptors[reg].Name);
374   return s;
375 }