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