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