llvm.org GIT mirror llvm / 7b155fb
Added support for framepointer Prologue/Epilogue support fp,ra save/restore and use the stack frame the right way! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@39763 91177308-0d34-0410-b5e6-96231b3b80d8 Bruno Cardoso Lopes 13 years ago
1 changed file(s) with 167 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
1414
1515 #include "Mips.h"
1616 #include "MipsRegisterInfo.h"
17 #include "MipsMachineFunction.h"
1718 #include "llvm/Constants.h"
1819 #include "llvm/Type.h"
1920 #include "llvm/Function.h"
4546 const TargetRegisterClass *RC) const
4647 {
4748 if (RC == Mips::CPURegsRegisterClass)
48 BuildMI(MBB, I, TII.get(Mips::SW)).addFrameIndex(FI)
49 .addImm(0).addReg(SrcReg, false, false, true);
49 BuildMI(MBB, I, TII.get(Mips::SW)).addReg(SrcReg, false, false, true)
50 .addImm(0).addFrameIndex(FI);
5051 else
5152 assert(0 && "Can't store this register to stack slot");
5253 }
153154 }
154155
155156 //===----------------------------------------------------------------------===//
157 //
156158 // Stack Frame Processing methods
159 // +----------------------------+
160 //
161 // Too meet ABI, we construct the frame on the reverse
162 // of natural order.
163 //
164 // The LLVM Frame will look like this:
165 //
166 // As the stack grows down, we start at 0, and the reference
167 // is decrement.
168 //
169 // 0 ----------
170 // -4 Args to pass
171 // . saved "Callee Saved" Registers
172 // . Local Area
173 // . saved FP
174 // . saved RA
175 // -StackSize -----------
176 //
177 // On the EliminateFrameIndex we just negate the address above
178 // and we get the stack frame required by the ABI, which is:
179 //
180 // sp + stacksize -------------
181 // saved $RA (only on non-leaf functions)
182 // saved $FP (only with frame pointer)
183 // saved "Callee Saved" Registers
184 // Local Area
185 // saved $GP (used in PIC - not supported yet)
186 // Args to pass area
187 // sp -------------
188 //
189 // The sp is the stack pointer subtracted/added from the stack size
190 // at the Prologue/Epilogue
191 //
192 // References to the previous stack (to obtain arguments) are done
193 // with fixed location stack frames using positive stack offsets.
194 //
195 // Examples:
196 // - reference to the actual stack frame
197 // for any local area var there is smt like : FI >= 0, StackOffset: -4
198 // sw REGX, 4(REGY)
199 //
200 // - reference to previous stack frame
201 // suppose there's a store to the 5th arguments : FI < 0, StackOffset: 16.
202 // The emitted instruction will be something like:
203 // sw REGX, 16+StackSize (REGY)
204 //
157205 //===----------------------------------------------------------------------===//
158206
159 // True if target has frame pointer
207 // hasFP - Return true if the specified function should have a dedicated frame
208 // pointer register. This is true if the function has variable sized allocas or
209 // if frame pointer elimination is disabled.
160210 bool MipsRegisterInfo::
161211 hasFP(const MachineFunction &MF) const {
162 return false;
212 return (NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects());
163213 }
164214
165215 // This function eliminate ADJCALLSTACKDOWN,
178228 eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
179229 RegScavenger *RS) const
180230 {
181 unsigned i = 0;
182231 MachineInstr &MI = *II;
183232 MachineFunction &MF = *MI.getParent()->getParent();
184233
234 unsigned i = 0;
185235 while (!MI.getOperand(i).isFrameIndex()) {
186236 ++i;
187237 assert(i < MI.getNumOperands() &&
188238 "Instr doesn't have FrameIndex operand!");
189239 }
190240
191 // FrameInfo addressable stack objects are accessed
192 // using neg. offsets, so we must add with the stack
193 // size to obtain $sp relative address.
194241 int FrameIndex = MI.getOperand(i).getFrameIndex();
195242 int stackSize = MF.getFrameInfo()->getStackSize();
196243 int spOffset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
197244
198245 #ifndef NDEBUG
199 DOUT << "\n<--------->\n";
246 DOUT << "\nFunction : " << MF.getFunction()->getName() << "\n";
247 DOUT << "<--------->\n";
200248 MI.print(DOUT);
201249 DOUT << "FrameIndex : " << FrameIndex << "\n";
202250 DOUT << "spOffset : " << spOffset << "\n";
203251 DOUT << "stackSize : " << stackSize << "\n";
204252 #endif
205253
206 // If the FrameIndex points to a positive SPOffset this
207 // means we are inside the callee and getting the arguments
208 // from the caller stack
209 int Offset = (-(stackSize)) + spOffset;
254 int Offset = ( (spOffset >= 0) ? (stackSize + spOffset) : (-spOffset));
210255
211256 #ifndef NDEBUG
212257 DOUT << "Offset : " << Offset << "\n";
214259 #endif
215260
216261 MI.getOperand(i-1).ChangeToImmediate(Offset);
217 MI.getOperand(i).ChangeToRegister(Mips::SP,false);
262 MI.getOperand(i).ChangeToRegister(getFrameRegister(MF),false);
218263 }
219264
220265 void MipsRegisterInfo::
221266 emitPrologue(MachineFunction &MF) const
222267 {
223 MachineBasicBlock &MBB = MF.front();
224 MachineFrameInfo *MFI = MF.getFrameInfo();
268 MachineBasicBlock &MBB = MF.front();
269 MachineFrameInfo *MFI = MF.getFrameInfo();
270 MipsFunctionInfo *MipsFI = MF.getInfo();
271 MachineBasicBlock::iterator MBBI = MBB.begin();
225272
226273 // Get the number of bytes to allocate from the FrameInfo
227274 int NumBytes = (int) MFI->getStackSize();
228275
276 #ifndef NDEBUG
277 DOUT << "\n<--- EMIT PROLOGUE --->";
278 DOUT << "Stack size :" << NumBytes << "\n";
279 #endif
280
229281 // Do we need to allocate space on the stack?
230282 if (NumBytes == 0) return;
231283
232 // FIXME: is Stack Align needed here ?? (maybe it's done before...)
284 int FPOffset, RAOffset;
285
286 // Always allocate space for saved RA and FP,
287 // even if FramePointer is not used. When not
288 // using FP, the last stack slot becomes empty
289 // and RA is saved before it.
290 if ((hasFP(MF)) && (MFI->hasCalls())) {
291 FPOffset = NumBytes;
292 RAOffset = (NumBytes+4);
293 } else if ((!hasFP(MF)) && (MFI->hasCalls())) {
294 FPOffset = 0;
295 RAOffset = NumBytes;
296 } else if ((hasFP(MF)) && (!MFI->hasCalls())) {
297 FPOffset = NumBytes;
298 RAOffset = 0;
299 }
300
301 MFI->setObjectOffset(MFI->CreateStackObject(4,4), -FPOffset);
302 MFI->setObjectOffset(MFI->CreateStackObject(4,4), -RAOffset);
303 MipsFI->setFPStackOffset(FPOffset);
304 MipsFI->setRAStackOffset(RAOffset);
305
306 #ifndef NDEBUG
307 DOUT << "FPOffset :" << FPOffset << "\n";
308 DOUT << "RAOffset :" << RAOffset << "\n";
309 #endif
310
311 // Align stack.
312 NumBytes += 8;
233313 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
234 NumBytes = -((NumBytes+Align-1)/Align*Align);
235
236 // Update frame info to pretend that this is part of the stack...
314 NumBytes = ((NumBytes+Align-1)/Align*Align);
315
316 #ifndef NDEBUG
317 DOUT << "New stack size :" << NumBytes << "\n\n";
318 #endif
319
320 // Update frame info
237321 MFI->setStackSize(NumBytes);
238322
239 // adjust stack : addi sp, sp, (-imm)
240 BuildMI(MBB, MBB.begin(), TII.get(Mips::ADDi), Mips::SP)
241 .addReg(Mips::SP).addImm(NumBytes);
323 // Adjust stack : addi sp, sp, (-imm)
324 BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
325 .addReg(Mips::SP).addImm(-NumBytes);
326
327 // Save the return address only if the function isnt a leaf one.
328 // sw $ra, stack_loc($sp)
329 if (MFI->hasCalls()) {
330 BuildMI(MBB, MBBI, TII.get(Mips::SW))
331 .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
332 }
333
334 // if framepointer enabled, save it and set it
335 // to point to the stack pointer
336 if (hasFP(MF)) {
337 // sw $fp,stack_loc($sp)
338 BuildMI(MBB, MBBI, TII.get(Mips::SW))
339 .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
340
341 // move $fp, $sp
342 BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::FP)
343 .addReg(Mips::SP).addReg(Mips::ZERO);
344 }
242345 }
243346
244347 void MipsRegisterInfo::
245348 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const
246349 {
247350 MachineBasicBlock::iterator MBBI = prior(MBB.end());
248 MachineFrameInfo *MFI = MF.getFrameInfo();
351 MachineFrameInfo *MFI = MF.getFrameInfo();
352 MipsFunctionInfo *MipsFI = MF.getInfo();
249353
250354 // Get the number of bytes from FrameInfo
251355 int NumBytes = (int) MFI->getStackSize();
252356
357 // Get the FI's where RA and FP are saved.
358 int FPOffset = MipsFI->getFPStackOffset();
359 int RAOffset = MipsFI->getRAStackOffset();
360
361 #ifndef NDEBUG
362 DOUT << "\n<--- EMIT EPILOGUE --->" << "\n";
363 DOUT << "Stack size :" << NumBytes << "\n";
364 DOUT << "FPOffset :" << FPOffset << "\n";
365 DOUT << "RAOffset :" << RAOffset << "\n\n";
366 #endif
367
368 // if framepointer enabled, restore it and restore the
369 // stack pointer
370 if (hasFP(MF)) {
371 // move $sp, $fp
372 BuildMI(MBB, MBBI, TII.get(Mips::ADDu), Mips::SP)
373 .addReg(Mips::FP).addReg(Mips::ZERO);
374
375 // lw $fp,stack_loc($sp)
376 BuildMI(MBB, MBBI, TII.get(Mips::LW))
377 .addReg(Mips::FP).addImm(FPOffset).addReg(Mips::SP);
378 }
379
380 // Restore the return address only if the function isnt a leaf one.
381 // lw $ra, stack_loc($sp)
382 if (MFI->hasCalls()) {
383 BuildMI(MBB, MBBI, TII.get(Mips::LW))
384 .addReg(Mips::RA).addImm(RAOffset).addReg(Mips::SP);
385 }
386
253387 // adjust stack : insert addi sp, sp, (imm)
254388 if (NumBytes) {
255 BuildMI(MBB, MBBI, TII.get(Mips::ADDi), Mips::SP)
256 .addReg(Mips::SP).addImm(-NumBytes);
257 }
258 }
259
260 void MipsRegisterInfo::
261 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
389 BuildMI(MBB, MBBI, TII.get(Mips::ADDiu), Mips::SP)
390 .addReg(Mips::SP).addImm(NumBytes);
391 }
392 }
393
394 void MipsRegisterInfo::
395 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
396 }
262397
263398 unsigned MipsRegisterInfo::
264399 getRARegister() const {
267402
268403 unsigned MipsRegisterInfo::
269404 getFrameRegister(MachineFunction &MF) const {
270 assert(0 && "What is the frame register");
271 return Mips::FP;
405 return hasFP(MF) ? Mips::FP : Mips::SP;
272406 }
273407
274408 unsigned MipsRegisterInfo::