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