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