assert(0) -> LLVM_UNREACHABLE.
[oota-llvm.git] / lib / Target / IA64 / IA64RegisterInfo.cpp
1 //===- IA64RegisterInfo.cpp - IA64 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 IA64 implementation of the TargetRegisterInfo class.
11 // This file is responsible for the frame pointer elimination optimization
12 // on IA64.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "IA64.h"
17 #include "IA64RegisterInfo.h"
18 #include "IA64InstrBuilder.h"
19 #include "IA64MachineFunctionInfo.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Type.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 using namespace llvm;
36
37 IA64RegisterInfo::IA64RegisterInfo(const TargetInstrInfo &tii)
38   : IA64GenRegisterInfo(IA64::ADJUSTCALLSTACKDOWN, IA64::ADJUSTCALLSTACKUP),
39     TII(tii) {}
40
41 const unsigned* IA64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
42                                                                          const {
43   static const unsigned CalleeSavedRegs[] = {
44     IA64::r5,  0
45   };
46   return CalleeSavedRegs;
47 }
48
49 const TargetRegisterClass* const*
50 IA64RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
51   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
52     &IA64::GRRegClass,  0
53   };
54   return CalleeSavedRegClasses;
55 }
56
57 BitVector IA64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
58   BitVector Reserved(getNumRegs());
59   Reserved.set(IA64::r0);
60   Reserved.set(IA64::r1);
61   Reserved.set(IA64::r2);
62   Reserved.set(IA64::r5);
63   Reserved.set(IA64::r12);
64   Reserved.set(IA64::r13);
65   Reserved.set(IA64::r22);
66   Reserved.set(IA64::rp);
67   return Reserved;
68 }
69
70 //===----------------------------------------------------------------------===//
71 // Stack Frame Processing methods
72 //===----------------------------------------------------------------------===//
73
74 // hasFP - Return true if the specified function should have a dedicated frame
75 // pointer register.  This is true if the function has variable sized allocas or
76 // if frame pointer elimination is disabled.
77 //
78 bool IA64RegisterInfo::hasFP(const MachineFunction &MF) const {
79   const MachineFrameInfo *MFI = MF.getFrameInfo();
80   return NoFramePointerElim || MFI->hasVarSizedObjects();
81 }
82
83 void IA64RegisterInfo::
84 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
85                               MachineBasicBlock::iterator I) const {
86   if (hasFP(MF)) {
87     // If we have a frame pointer, turn the adjcallstackup instruction into a
88     // 'sub SP, <amt>' and the adjcallstackdown instruction into 'add SP,
89     // <amt>'
90     MachineInstr *Old = I;
91     unsigned Amount = Old->getOperand(0).getImm();
92     DebugLoc dl = Old->getDebugLoc();
93     if (Amount != 0) {
94       // We need to keep the stack aligned properly.  To do this, we round the
95       // amount of space needed for the outgoing arguments up to the next
96       // alignment boundary.
97       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
98       Amount = (Amount+Align-1)/Align*Align;
99
100       // Replace the pseudo instruction with a new instruction...
101       if (Old->getOpcode() == IA64::ADJUSTCALLSTACKDOWN) {
102         BuildMI(MBB, I, dl, TII.get(IA64::ADDIMM22), IA64::r12)
103           .addReg(IA64::r12).addImm(-Amount);
104       } else {
105         assert(Old->getOpcode() == IA64::ADJUSTCALLSTACKUP);
106         BuildMI(MBB, I, dl, TII.get(IA64::ADDIMM22), IA64::r12)
107           .addReg(IA64::r12).addImm(Amount);
108       }
109     }
110   }
111
112   MBB.erase(I);
113 }
114
115 void IA64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
116                                            int SPAdj, RegScavenger *RS)const{
117   assert(SPAdj == 0 && "Unexpected");
118
119   unsigned i = 0;
120   MachineInstr &MI = *II;
121   MachineBasicBlock &MBB = *MI.getParent();
122   MachineFunction &MF = *MBB.getParent();
123   DebugLoc dl = MI.getDebugLoc();
124
125   bool FP = hasFP(MF);
126
127   while (!MI.getOperand(i).isFI()) {
128     ++i;
129     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
130   }
131
132   int FrameIndex = MI.getOperand(i).getIndex();
133
134   // choose a base register: ( hasFP? framepointer : stack pointer )
135   unsigned BaseRegister = FP ? IA64::r5 : IA64::r12;
136   // Add the base register
137   MI.getOperand(i).ChangeToRegister(BaseRegister, false);
138
139   // Now add the frame object offset to the offset from r1.
140   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
141
142   // If we're not using a Frame Pointer that has been set to the value of the
143   // SP before having the stack size subtracted from it, then add the stack size
144   // to Offset to get the correct offset.
145   Offset += MF.getFrameInfo()->getStackSize();
146
147   // XXX: we use 'r22' as another hack+slash temporary register here :(
148   if (Offset <= 8191 && Offset >= -8192) { // smallish offset
149     // Fix up the old:
150     MI.getOperand(i).ChangeToRegister(IA64::r22, false);
151     //insert the new
152     BuildMI(MBB, II, dl, TII.get(IA64::ADDIMM22), IA64::r22)
153       .addReg(BaseRegister).addImm(Offset);
154   } else { // it's big
155     //fix up the old:
156     MI.getOperand(i).ChangeToRegister(IA64::r22, false);
157     BuildMI(MBB, II, dl, TII.get(IA64::MOVLIMM64), IA64::r22).addImm(Offset);
158     BuildMI(MBB, II, dl, TII.get(IA64::ADD), IA64::r22).addReg(BaseRegister)
159       .addReg(IA64::r22);
160   }
161
162 }
163
164 void IA64RegisterInfo::emitPrologue(MachineFunction &MF) const {
165   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
166   MachineBasicBlock::iterator MBBI = MBB.begin();
167   MachineFrameInfo *MFI = MF.getFrameInfo();
168   bool FP = hasFP(MF);
169   DebugLoc dl = (MBBI != MBB.end() ?
170                  MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
171
172   // first, we handle the 'alloc' instruction, that should be right up the
173   // top of any function
174   static const unsigned RegsInOrder[96] = { // there are 96 GPRs the
175                                             // RSE worries about
176         IA64::r32, IA64::r33, IA64::r34, IA64::r35,
177         IA64::r36, IA64::r37, IA64::r38, IA64::r39, IA64::r40, IA64::r41,
178         IA64::r42, IA64::r43, IA64::r44, IA64::r45, IA64::r46, IA64::r47,
179         IA64::r48, IA64::r49, IA64::r50, IA64::r51, IA64::r52, IA64::r53,
180         IA64::r54, IA64::r55, IA64::r56, IA64::r57, IA64::r58, IA64::r59,
181         IA64::r60, IA64::r61, IA64::r62, IA64::r63, IA64::r64, IA64::r65,
182         IA64::r66, IA64::r67, IA64::r68, IA64::r69, IA64::r70, IA64::r71,
183         IA64::r72, IA64::r73, IA64::r74, IA64::r75, IA64::r76, IA64::r77,
184         IA64::r78, IA64::r79, IA64::r80, IA64::r81, IA64::r82, IA64::r83,
185         IA64::r84, IA64::r85, IA64::r86, IA64::r87, IA64::r88, IA64::r89,
186         IA64::r90, IA64::r91, IA64::r92, IA64::r93, IA64::r94, IA64::r95,
187         IA64::r96, IA64::r97, IA64::r98, IA64::r99, IA64::r100, IA64::r101,
188         IA64::r102, IA64::r103, IA64::r104, IA64::r105, IA64::r106, IA64::r107,
189         IA64::r108, IA64::r109, IA64::r110, IA64::r111, IA64::r112, IA64::r113,
190         IA64::r114, IA64::r115, IA64::r116, IA64::r117, IA64::r118, IA64::r119,
191         IA64::r120, IA64::r121, IA64::r122, IA64::r123, IA64::r124, IA64::r125,
192         IA64::r126, IA64::r127 };
193
194   unsigned numStackedGPRsUsed=0;
195   for (int i=0; i != 96; i++) {
196     if (MF.getRegInfo().isPhysRegUsed(RegsInOrder[i]))
197       numStackedGPRsUsed=i+1; // (i+1 and not ++ - consider fn(fp, fp, int)
198   }
199
200   unsigned numOutRegsUsed=MF.getInfo<IA64FunctionInfo>()->outRegsUsed;
201
202   // XXX FIXME : this code should be a bit more reliable (in case there _isn't_
203   // a pseudo_alloc in the MBB)
204   unsigned dstRegOfPseudoAlloc;
205   for(MBBI = MBB.begin(); /*MBBI->getOpcode() != IA64::PSEUDO_ALLOC*/; ++MBBI) {
206     assert(MBBI != MBB.end());
207     if(MBBI->getOpcode() == IA64::PSEUDO_ALLOC) {
208       dstRegOfPseudoAlloc=MBBI->getOperand(0).getReg();
209       break;
210     }
211   }
212
213   if (MBBI != MBB.end()) dl = MBBI->getDebugLoc();
214
215   BuildMI(MBB, MBBI, dl, TII.get(IA64::ALLOC)).
216      addReg(dstRegOfPseudoAlloc).addImm(0).
217      addImm(numStackedGPRsUsed).addImm(numOutRegsUsed).addImm(0);
218
219   // Get the number of bytes to allocate from the FrameInfo
220   unsigned NumBytes = MFI->getStackSize();
221
222   if(FP)
223     NumBytes += 8; // reserve space for the old FP
224
225   // Do we need to allocate space on the stack?
226   if (NumBytes == 0)
227     return;
228
229   // Add 16 bytes at the bottom of the stack (scratch area)
230   // and round the size to a multiple of the alignment.
231   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
232   unsigned Size = 16 + (FP ? 8 : 0);
233   NumBytes = (NumBytes+Size+Align-1)/Align*Align;
234
235   // Update frame info to pretend that this is part of the stack...
236   MFI->setStackSize(NumBytes);
237
238   // adjust stack pointer: r12 -= numbytes
239   if (NumBytes <= 8191) {
240     BuildMI(MBB, MBBI, dl, TII.get(IA64::ADDIMM22),IA64::r12).addReg(IA64::r12).
241       addImm(-NumBytes);
242   } else { // we use r22 as a scratch register here
243     // first load the decrement into r22
244     BuildMI(MBB, MBBI, dl, TII.get(IA64::MOVLIMM64), IA64::r22).
245       addImm(-NumBytes);
246     // FIXME: MOVLSI32 expects a _u_32imm
247     // then add (subtract) it to r12 (stack ptr)
248     BuildMI(MBB, MBBI, dl, TII.get(IA64::ADD), IA64::r12)
249       .addReg(IA64::r12).addReg(IA64::r22);
250     
251   }
252
253   // now if we need to, save the old FP and set the new
254   if (FP) {
255     BuildMI(MBB, MBBI,dl,TII.get(IA64::ST8)).addReg(IA64::r12).addReg(IA64::r5);
256     // this must be the last instr in the prolog ?  (XXX: why??)
257     BuildMI(MBB, MBBI, dl, TII.get(IA64::MOV), IA64::r5).addReg(IA64::r12);
258   }
259
260 }
261
262 void IA64RegisterInfo::emitEpilogue(MachineFunction &MF,
263                                    MachineBasicBlock &MBB) const {
264   const MachineFrameInfo *MFI = MF.getFrameInfo();
265   MachineBasicBlock::iterator MBBI = prior(MBB.end());
266   assert(MBBI->getOpcode() == IA64::RET &&
267          "Can only insert epilog into returning blocks");
268   DebugLoc dl = MBBI->getDebugLoc();
269   bool FP = hasFP(MF);
270
271   // Get the number of bytes allocated from the FrameInfo...
272   unsigned NumBytes = MFI->getStackSize();
273
274   //now if we need to, restore the old FP
275   if (FP) {
276     //copy the FP into the SP (discards allocas)
277     BuildMI(MBB, MBBI, dl, TII.get(IA64::MOV), IA64::r12).addReg(IA64::r5);
278     //restore the FP
279     BuildMI(MBB, MBBI, dl, TII.get(IA64::LD8), IA64::r5).addReg(IA64::r5);
280   }
281
282   if (NumBytes != 0) {
283     if (NumBytes <= 8191) {
284       BuildMI(MBB, MBBI, dl, TII.get(IA64::ADDIMM22),IA64::r12).
285         addReg(IA64::r12).addImm(NumBytes);
286     } else {
287       BuildMI(MBB, MBBI, dl, TII.get(IA64::MOVLIMM64), IA64::r22).
288         addImm(NumBytes);
289       BuildMI(MBB, MBBI, dl, TII.get(IA64::ADD), IA64::r12).addReg(IA64::r12).
290         addReg(IA64::r22);
291     }
292   }
293 }
294
295 unsigned IA64RegisterInfo::getRARegister() const {
296   LLVM_UNREACHABLE("What is the return address register");
297   return 0;
298 }
299
300 unsigned IA64RegisterInfo::getFrameRegister(MachineFunction &MF) const {
301   return hasFP(MF) ? IA64::r5 : IA64::r12;
302 }
303
304 unsigned IA64RegisterInfo::getEHExceptionRegister() const {
305   LLVM_UNREACHABLE("What is the exception register");
306   return 0;
307 }
308
309 unsigned IA64RegisterInfo::getEHHandlerRegister() const {
310   LLVM_UNREACHABLE("What is the exception handler register");
311   return 0;
312 }
313
314 int IA64RegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
315   LLVM_UNREACHABLE("What is the dwarf register number");
316   return -1;
317 }
318
319 #include "IA64GenRegisterInfo.inc"
320