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