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