Let the alpha breakage begin. First Formals and RET. next Calls
[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 const unsigned* AlphaRegisterInfo::getCalleeSaveRegs() const {
149   static const unsigned CalleeSaveRegs[] = {
150     Alpha::R9, Alpha::R10,
151     Alpha::R11, Alpha::R12,
152     Alpha::R13, Alpha::R14,
153     Alpha::F2, Alpha::F3,
154     Alpha::F4, Alpha::F5,
155     Alpha::F6, Alpha::F7,
156     Alpha::F8, Alpha::F9,  0
157   };
158   return CalleeSaveRegs;
159 }
160
161 const TargetRegisterClass* const*
162 AlphaRegisterInfo::getCalleeSaveRegClasses() const {
163   static const TargetRegisterClass * const CalleeSaveRegClasses[] = {
164     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
165     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
166     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
167     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
168     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
169     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
170     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,  0
171   };
172   return CalleeSaveRegClasses;
173 }
174
175 //===----------------------------------------------------------------------===//
176 // Stack Frame Processing methods
177 //===----------------------------------------------------------------------===//
178
179 // hasFP - Return true if the specified function should have a dedicated frame
180 // pointer register.  This is true if the function has variable sized allocas or
181 // if frame pointer elimination is disabled.
182 //
183 static bool hasFP(MachineFunction &MF) {
184   MachineFrameInfo *MFI = MF.getFrameInfo();
185   return MFI->hasVarSizedObjects();
186 }
187
188 void AlphaRegisterInfo::
189 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
190                               MachineBasicBlock::iterator I) const {
191   if (hasFP(MF)) {
192     // If we have a frame pointer, turn the adjcallstackup instruction into a
193     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
194     // <amt>'
195     MachineInstr *Old = I;
196     uint64_t Amount = Old->getOperand(0).getImmedValue();
197     if (Amount != 0) {
198       // We need to keep the stack aligned properly.  To do this, we round the
199       // amount of space needed for the outgoing arguments up to the next
200       // alignment boundary.
201       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
202       Amount = (Amount+Align-1)/Align*Align;
203
204       MachineInstr *New;
205       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
206          New=BuildMI(Alpha::LDA, 2, Alpha::R30)
207           .addImm(-Amount).addReg(Alpha::R30);
208       } else {
209          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
210          New=BuildMI(Alpha::LDA, 2, Alpha::R30)
211           .addImm(Amount).addReg(Alpha::R30);
212       }
213
214       // Replace the pseudo instruction with a new instruction...
215       MBB.insert(I, New);
216     }
217   }
218
219   MBB.erase(I);
220 }
221
222 //Alpha has a slightly funny stack:
223 //Args
224 //<- incoming SP
225 //fixed locals (and spills, callee saved, etc)
226 //<- FP
227 //variable locals
228 //<- SP
229
230 void
231 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const {
232   unsigned i = 0;
233   MachineInstr &MI = *II;
234   MachineBasicBlock &MBB = *MI.getParent();
235   MachineFunction &MF = *MBB.getParent();
236   bool FP = hasFP(MF);
237
238   while (!MI.getOperand(i).isFrameIndex()) {
239     ++i;
240     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
241   }
242
243   int FrameIndex = MI.getOperand(i).getFrameIndex();
244
245   // Add the base register of R30 (SP) or R15 (FP).
246   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30);
247
248   // Now add the frame object offset to the offset from the virtual frame index.
249   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
250
251   DEBUG(std::cerr << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
252
253   Offset += MF.getFrameInfo()->getStackSize();
254
255   DEBUG(std::cerr << "Corrected Offset " << Offset <<
256         " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
257
258   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
259     DEBUG(std::cerr << "Unconditionally using R28 for evil purposes Offset: " << Offset << "\n");
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.getOperand(i + 1).ChangeToRegister(Alpha::R28);
264     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
265     //insert the new
266     MachineInstr* nMI=BuildMI(Alpha::LDAH, 2, Alpha::R28)
267       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
268     MBB.insert(II, nMI);
269   } else {
270     MI.getOperand(i).ChangeToImmediate(Offset);
271   }
272 }
273
274
275 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
276   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
277   MachineBasicBlock::iterator MBBI = MBB.begin();
278   MachineFrameInfo *MFI = MF.getFrameInfo();
279   bool FP = hasFP(MF);
280
281   static int curgpdist = 0;
282
283   //handle GOP offset
284   BuildMI(MBB, MBBI, Alpha::LDAHg, 3, Alpha::R29)
285     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
286     .addReg(Alpha::R27).addImm(++curgpdist);
287   BuildMI(MBB, MBBI, Alpha::LDAg, 3, Alpha::R29)
288     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
289     .addReg(Alpha::R29).addImm(curgpdist);
290
291   //evil const_cast until MO stuff setup to handle const
292   BuildMI(MBB, MBBI, Alpha::ALTENT, 1)
293     .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
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   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
315   NumBytes = (NumBytes+Align-1)/Align*Align;
316
317   // Update frame info to pretend that this is part of the stack...
318   MFI->setStackSize(NumBytes);
319
320   // adjust stack pointer: r30 -= numbytes
321   NumBytes = -NumBytes;
322   if (NumBytes >= IMM_LOW) {
323     BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(NumBytes)
324       .addReg(Alpha::R30);
325   } else if (getUpper16(NumBytes) >= IMM_LOW) {
326     BuildMI(MBB, MBBI, Alpha::LDAH, 2, Alpha::R30).addImm(getUpper16(NumBytes))
327       .addReg(Alpha::R30);
328     BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(getLower16(NumBytes))
329       .addReg(Alpha::R30);
330   } else {
331     std::cerr << "Too big a stack frame at " << NumBytes << "\n";
332     abort();
333   }
334
335   //now if we need to, save the old FP and set the new
336   if (FP)
337   {
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::RETDAG || MBBI->getOpcode() == Alpha::RETDAGp
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     BuildMI(MBB, MBBI, Alpha::LDQ, 2, Alpha::R15).addImm(0).addReg(Alpha::R15);
365   }
366
367    if (NumBytes != 0)
368      {
369        if (NumBytes <= IMM_HIGH) {
370          BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30).addImm(NumBytes)
371            .addReg(Alpha::R30);
372        } else if (getUpper16(NumBytes) <= IMM_HIGH) {
373          BuildMI(MBB, MBBI, Alpha::LDAH, 2, Alpha::R30)
374            .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
375          BuildMI(MBB, MBBI, Alpha::LDA, 2, Alpha::R30)
376            .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
377        } else {
378          std::cerr << "Too big a stack frame at " << NumBytes << "\n";
379          abort();
380        }
381      }
382 }
383
384 unsigned AlphaRegisterInfo::getRARegister() const {
385   assert(0 && "What is the return address register");
386   return 0;
387 }
388
389 unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
390   return hasFP(MF) ? Alpha::R15 : Alpha::R30;
391 }
392
393 #include "AlphaGenRegisterInfo.inc"
394
395 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
396 {
397   std::string s(RegisterDescriptors[reg].Name);
398   return s;
399 }