Teach frame lowering to ignore debug values after the terminators.
[oota-llvm.git] / lib / Target / Mips / MipsFrameLowering.cpp
1 //=======- MipsFrameLowering.cpp - Mips Frame 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 Mips implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsFrameLowering.h"
15 #include "MipsInstrInfo.h"
16 #include "MipsMachineFunction.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Support/CommandLine.h"
26
27 using namespace llvm;
28
29
30 //===----------------------------------------------------------------------===//
31 //
32 // Stack Frame Processing methods
33 // +----------------------------+
34 //
35 // The stack is allocated decrementing the stack pointer on
36 // the first instruction of a function prologue. Once decremented,
37 // all stack references are done thought a positive offset
38 // from the stack/frame pointer, so the stack is considering
39 // to grow up! Otherwise terrible hacks would have to be made
40 // to get this stack ABI compliant :)
41 //
42 //  The stack frame required by the ABI (after call):
43 //  Offset
44 //
45 //  0                 ----------
46 //  4                 Args to pass
47 //  .                 saved $GP  (used in PIC)
48 //  .                 Alloca allocations
49 //  .                 Local Area
50 //  .                 CPU "Callee Saved" Registers
51 //  .                 saved FP
52 //  .                 saved RA
53 //  .                 FPU "Callee Saved" Registers
54 //  StackSize         -----------
55 //
56 // Offset - offset from sp after stack allocation on function prologue
57 //
58 // The sp is the stack pointer subtracted/added from the stack size
59 // at the Prologue/Epilogue
60 //
61 // References to the previous stack (to obtain arguments) are done
62 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
63 //
64 // Examples:
65 // - reference to the actual stack frame
66 //   for any local area var there is smt like : FI >= 0, StackOffset: 4
67 //     sw REGX, 4(SP)
68 //
69 // - reference to previous stack frame
70 //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
71 //   The emitted instruction will be something like:
72 //     lw REGX, 16+StackSize(SP)
73 //
74 // Since the total stack size is unknown on LowerFormalArguments, all
75 // stack references (ObjectOffset) created to reference the function
76 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
77 // possible to detect those references and the offsets are adjusted to
78 // their real location.
79 //
80 //===----------------------------------------------------------------------===//
81
82 // hasFP - Return true if the specified function should have a dedicated frame
83 // pointer register.  This is true if the function has variable sized allocas or
84 // if frame pointer elimination is disabled.
85 bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
86   const MachineFrameInfo *MFI = MF.getFrameInfo();
87   return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
88 }
89
90 void MipsFrameLowering::adjustMipsStackFrame(MachineFunction &MF) const {
91   MachineFrameInfo *MFI = MF.getFrameInfo();
92   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
93   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
94   unsigned StackAlign = getStackAlignment();
95   unsigned RegSize = STI.isGP32bit() ? 4 : 8;
96   bool HasGP = MipsFI->needGPSaveRestore();
97
98   // Min and Max CSI FrameIndex.
99   int MinCSFI = -1, MaxCSFI = -1;
100
101   // See the description at MipsMachineFunction.h
102   int TopCPUSavedRegOff = -1, TopFPUSavedRegOff = -1;
103
104   // Replace the dummy '0' SPOffset by the negative offsets, as explained on
105   // LowerFormalArguments. Leaving '0' for while is necessary to avoid the
106   // approach done by calculateFrameObjectOffsets to the stack frame.
107   MipsFI->adjustLoadArgsFI(MFI);
108   MipsFI->adjustStoreVarArgsFI(MFI);
109
110   // It happens that the default stack frame allocation order does not directly
111   // map to the convention used for mips. So we must fix it. We move the callee
112   // save register slots after the local variables area, as described in the
113   // stack frame above.
114   unsigned CalleeSavedAreaSize = 0;
115   if (!CSI.empty()) {
116     MinCSFI = CSI[0].getFrameIdx();
117     MaxCSFI = CSI[CSI.size()-1].getFrameIdx();
118   }
119   for (unsigned i = 0, e = CSI.size(); i != e; ++i)
120     CalleeSavedAreaSize += MFI->getObjectAlignment(CSI[i].getFrameIdx());
121
122   unsigned StackOffset = HasGP ? (MipsFI->getGPStackOffset()+RegSize)
123                 : (STI.isABI_O32() ? 16 : 0);
124
125   // Adjust local variables. They should come on the stack right
126   // after the arguments.
127   int LastOffsetFI = -1;
128   for (int i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
129     if (i >= MinCSFI && i <= MaxCSFI)
130       continue;
131     if (MFI->isDeadObjectIndex(i))
132       continue;
133     unsigned Offset =
134       StackOffset + MFI->getObjectOffset(i) - CalleeSavedAreaSize;
135     if (LastOffsetFI == -1)
136       LastOffsetFI = i;
137     if (Offset > MFI->getObjectOffset(LastOffsetFI))
138       LastOffsetFI = i;
139     MFI->setObjectOffset(i, Offset);
140   }
141
142   // Adjust CPU Callee Saved Registers Area. Registers RA and FP must
143   // be saved in this CPU Area. This whole area must be aligned to the
144   // default Stack Alignment requirements.
145   if (LastOffsetFI >= 0)
146     StackOffset = MFI->getObjectOffset(LastOffsetFI)+
147                   MFI->getObjectSize(LastOffsetFI);
148   StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
149
150   for (unsigned i = 0, e = CSI.size(); i != e ; ++i) {
151     unsigned Reg = CSI[i].getReg();
152     if (!Mips::CPURegsRegisterClass->contains(Reg))
153       break;
154     MFI->setObjectOffset(CSI[i].getFrameIdx(), StackOffset);
155     TopCPUSavedRegOff = StackOffset;
156     StackOffset += MFI->getObjectAlignment(CSI[i].getFrameIdx());
157   }
158
159   // Stack locations for FP and RA. If only one of them is used,
160   // the space must be allocated for both, otherwise no space at all.
161   if (hasFP(MF) || MFI->adjustsStack()) {
162     // FP stack location
163     MFI->setObjectOffset(MFI->CreateStackObject(RegSize, RegSize, true),
164                          StackOffset);
165     MipsFI->setFPStackOffset(StackOffset);
166     TopCPUSavedRegOff = StackOffset;
167     StackOffset += RegSize;
168
169     // SP stack location
170     MFI->setObjectOffset(MFI->CreateStackObject(RegSize, RegSize, true),
171                          StackOffset);
172     MipsFI->setRAStackOffset(StackOffset);
173     StackOffset += RegSize;
174
175     if (MFI->adjustsStack())
176       TopCPUSavedRegOff += RegSize;
177   }
178
179   StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
180
181   // Adjust FPU Callee Saved Registers Area. This Area must be
182   // aligned to the default Stack Alignment requirements.
183   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
184     unsigned Reg = CSI[i].getReg();
185     if (Mips::CPURegsRegisterClass->contains(Reg))
186       continue;
187     MFI->setObjectOffset(CSI[i].getFrameIdx(), StackOffset);
188     TopFPUSavedRegOff = StackOffset;
189     StackOffset += MFI->getObjectAlignment(CSI[i].getFrameIdx());
190   }
191   StackOffset = ((StackOffset+StackAlign-1)/StackAlign*StackAlign);
192
193   // Update frame info
194   MFI->setStackSize(StackOffset);
195
196   // Recalculate the final tops offset. The final values must be '0'
197   // if there isn't a callee saved register for CPU or FPU, otherwise
198   // a negative offset is needed.
199   if (TopCPUSavedRegOff >= 0)
200     MipsFI->setCPUTopSavedRegOff(TopCPUSavedRegOff-StackOffset);
201
202   if (TopFPUSavedRegOff >= 0)
203     MipsFI->setFPUTopSavedRegOff(TopFPUSavedRegOff-StackOffset);
204 }
205
206 void MipsFrameLowering::emitPrologue(MachineFunction &MF) const {
207   MachineBasicBlock &MBB   = MF.front();
208   MachineFrameInfo *MFI    = MF.getFrameInfo();
209   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
210   const MipsRegisterInfo *RegInfo =
211     static_cast<const MipsRegisterInfo*>(MF.getTarget().getRegisterInfo());
212   const MipsInstrInfo &TII =
213     *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
214   MachineBasicBlock::iterator MBBI = MBB.begin();
215   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
216   bool isPIC = (MF.getTarget().getRelocationModel() == Reloc::PIC_);
217
218   // Get the right frame order for Mips.
219   adjustMipsStackFrame(MF);
220
221   // Get the number of bytes to allocate from the FrameInfo.
222   unsigned StackSize = MFI->getStackSize();
223
224   // No need to allocate space on the stack.
225   if (StackSize == 0 && !MFI->adjustsStack()) return;
226
227   int FPOffset = MipsFI->getFPStackOffset();
228   int RAOffset = MipsFI->getRAStackOffset();
229
230   BuildMI(MBB, MBBI, dl, TII.get(Mips::NOREORDER));
231
232   // TODO: check need from GP here.
233   if (isPIC && STI.isABI_O32())
234     BuildMI(MBB, MBBI, dl, TII.get(Mips::CPLOAD))
235       .addReg(RegInfo->getPICCallReg());
236   BuildMI(MBB, MBBI, dl, TII.get(Mips::NOMACRO));
237
238   // Adjust stack : addi sp, sp, (-imm)
239   BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
240       .addReg(Mips::SP).addImm(-StackSize);
241
242   // Save the return address only if the function isnt a leaf one.
243   // sw  $ra, stack_loc($sp)
244   if (MFI->adjustsStack()) {
245     BuildMI(MBB, MBBI, dl, TII.get(Mips::SW))
246         .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
247   }
248
249   // if framepointer enabled, save it and set it
250   // to point to the stack pointer
251   if (hasFP(MF)) {
252     // sw  $fp,stack_loc($sp)
253     BuildMI(MBB, MBBI, dl, TII.get(Mips::SW))
254       .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
255
256     // move $fp, $sp
257     BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::FP)
258       .addReg(Mips::SP).addReg(Mips::ZERO);
259   }
260
261   // Restore GP from the saved stack location
262   if (MipsFI->needGPSaveRestore())
263     BuildMI(MBB, MBBI, dl, TII.get(Mips::CPRESTORE))
264       .addImm(MipsFI->getGPStackOffset());
265 }
266
267 void MipsFrameLowering::emitEpilogue(MachineFunction &MF,
268                                  MachineBasicBlock &MBB) const {
269   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
270   MachineFrameInfo *MFI            = MF.getFrameInfo();
271   MipsFunctionInfo *MipsFI         = MF.getInfo<MipsFunctionInfo>();
272   const MipsInstrInfo &TII =
273     *static_cast<const MipsInstrInfo*>(MF.getTarget().getInstrInfo());
274   DebugLoc dl = MBBI->getDebugLoc();
275
276   // Get the number of bytes from FrameInfo
277   int NumBytes = (int) MFI->getStackSize();
278
279   // Get the FI's where RA and FP are saved.
280   int FPOffset = MipsFI->getFPStackOffset();
281   int RAOffset = MipsFI->getRAStackOffset();
282
283   // if framepointer enabled, restore it and restore the
284   // stack pointer
285   if (hasFP(MF)) {
286     // move $sp, $fp
287     BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDu), Mips::SP)
288       .addReg(Mips::FP).addReg(Mips::ZERO);
289
290     // lw  $fp,stack_loc($sp)
291     BuildMI(MBB, MBBI, dl, TII.get(Mips::LW), Mips::FP)
292       .addImm(FPOffset).addReg(Mips::SP);
293   }
294
295   // Restore the return address only if the function isnt a leaf one.
296   // lw  $ra, stack_loc($sp)
297   if (MFI->adjustsStack()) {
298     BuildMI(MBB, MBBI, dl, TII.get(Mips::LW), Mips::RA)
299       .addImm(RAOffset).addReg(Mips::SP);
300   }
301
302   // adjust stack  : insert addi sp, sp, (imm)
303   if (NumBytes) {
304     BuildMI(MBB, MBBI, dl, TII.get(Mips::ADDiu), Mips::SP)
305       .addReg(Mips::SP).addImm(NumBytes);
306   }
307 }