blob: d379bf0e9cb1cd77b09d3389e92945e33488144d [file] [log] [blame]
Anton Korobeynikov33464912010-11-15 00:06:54 +00001//=======- ARMFrameInfo.cpp - ARM 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 ARM implementation of TargetFrameInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMFrameInfo.h"
15#include "ARMBaseInstrInfo.h"
16#include "ARMMachineFunctionInfo.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengab5c7032010-11-22 18:12:04 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000021#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000022
23using namespace llvm;
24
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000025/// hasFP - Return true if the specified function should have a dedicated frame
26/// pointer register. This is true if the function has variable sized allocas
27/// or if frame pointer elimination is disabled.
28///
29bool ARMFrameInfo::hasFP(const MachineFunction &MF) const {
30 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
31
32 // Mac OS X requires FP not to be clobbered for backtracing purpose.
33 if (STI.isTargetDarwin())
34 return true;
35
36 const MachineFrameInfo *MFI = MF.getFrameInfo();
37 // Always eliminate non-leaf frame pointers.
38 return ((DisableFramePointerElim(MF) && MFI->hasCalls()) ||
39 RegInfo->needsStackRealignment(MF) ||
40 MFI->hasVarSizedObjects() ||
41 MFI->isFrameAddressTaken());
42}
43
44// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
45// not required, we reserve argument space for call sites in the function
46// immediately on entry to the current function. This eliminates the need for
47// add/sub sp brackets around call sites. Returns true if the call frame is
48// included as part of the stack frame.
49bool ARMFrameInfo::hasReservedCallFrame(const MachineFunction &MF) const {
50 const MachineFrameInfo *FFI = MF.getFrameInfo();
51 unsigned CFSize = FFI->getMaxCallFrameSize();
52 // It's not always a good idea to include the call frame as part of the
53 // stack frame. ARM (especially Thumb) has small immediate offset to
54 // address the stack frame. So a large call frame can cause poor codegen
55 // and may even makes it impossible to scavenge a register.
56 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
57 return false;
58
59 return !MF.getFrameInfo()->hasVarSizedObjects();
60}
61
62// canSimplifyCallFramePseudos - If there is a reserved call frame, the
63// call frame pseudos can be simplified. Unlike most targets, having a FP
64// is not sufficient here since we still may reference some objects via SP
65// even when FP is available in Thumb2 mode.
66bool ARMFrameInfo::canSimplifyCallFramePseudos(const MachineFunction &MF)const {
67 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
68}
69
Anton Korobeynikov33464912010-11-15 00:06:54 +000070static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
71 for (unsigned i = 0; CSRegs[i]; ++i)
72 if (Reg == CSRegs[i])
73 return true;
74 return false;
75}
76
77static bool isCSRestore(MachineInstr *MI,
78 const ARMBaseInstrInfo &TII,
79 const unsigned *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000080 // Integer spill area is handled with "pop".
81 if (MI->getOpcode() == ARM::LDMIA_RET ||
82 MI->getOpcode() == ARM::t2LDMIA_RET ||
83 MI->getOpcode() == ARM::LDMIA_UPD ||
84 MI->getOpcode() == ARM::t2LDMIA_UPD ||
85 MI->getOpcode() == ARM::VLDMDIA_UPD) {
86 // The first two operands are predicates. The last two are
87 // imp-def and imp-use of SP. Check everything in between.
88 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
89 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
90 return false;
91 return true;
92 }
93
94 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +000095}
96
97static void
98emitSPUpdate(bool isARM,
99 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
100 DebugLoc dl, const ARMBaseInstrInfo &TII,
101 int NumBytes,
102 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
103 if (isARM)
104 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
105 Pred, PredReg, TII);
106 else
107 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
108 Pred, PredReg, TII);
109}
110
111void ARMFrameInfo::emitPrologue(MachineFunction &MF) const {
112 MachineBasicBlock &MBB = MF.front();
113 MachineBasicBlock::iterator MBBI = MBB.begin();
114 MachineFrameInfo *MFI = MF.getFrameInfo();
115 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
116 const ARMBaseRegisterInfo *RegInfo =
117 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
118 const ARMBaseInstrInfo &TII =
119 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
120 assert(!AFI->isThumb1OnlyFunction() &&
121 "This emitPrologue does not support Thumb1!");
122 bool isARM = !AFI->isThumbFunction();
123 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
124 unsigned NumBytes = MFI->getStackSize();
125 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
126 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
127 unsigned FramePtr = RegInfo->getFrameRegister(MF);
128
129 // Determine the sizes of each callee-save spill areas and record which frame
130 // belongs to which callee-save spill areas.
131 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
132 int FramePtrSpillFI = 0;
133
134 // Allocate the vararg register save area. This is not counted in NumBytes.
135 if (VARegSaveSize)
136 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
137
138 if (!AFI->hasStackFrame()) {
139 if (NumBytes != 0)
140 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
141 return;
142 }
143
144 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
145 unsigned Reg = CSI[i].getReg();
146 int FI = CSI[i].getFrameIdx();
147 switch (Reg) {
148 case ARM::R4:
149 case ARM::R5:
150 case ARM::R6:
151 case ARM::R7:
152 case ARM::LR:
153 if (Reg == FramePtr)
154 FramePtrSpillFI = FI;
155 AFI->addGPRCalleeSavedArea1Frame(FI);
156 GPRCS1Size += 4;
157 break;
158 case ARM::R8:
159 case ARM::R9:
160 case ARM::R10:
161 case ARM::R11:
162 if (Reg == FramePtr)
163 FramePtrSpillFI = FI;
164 if (STI.isTargetDarwin()) {
165 AFI->addGPRCalleeSavedArea2Frame(FI);
166 GPRCS2Size += 4;
167 } else {
168 AFI->addGPRCalleeSavedArea1Frame(FI);
169 GPRCS1Size += 4;
170 }
171 break;
172 default:
173 AFI->addDPRCalleeSavedAreaFrame(FI);
174 DPRCSSize += 8;
175 }
176 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000177
Eric Christopher8b3ca622010-11-18 19:40:05 +0000178 // Move past area 1.
179 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000180
Anton Korobeynikov33464912010-11-15 00:06:54 +0000181 // Set FP to point to the stack slot that contains the previous FP.
182 // For Darwin, FP is R7, which has now been stored in spill area 1.
183 // Otherwise, if this is not Darwin, all the callee-saved registers go
184 // into spill area 1, including the FP in R11. In either case, it is
185 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000186 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000187 if (HasFP) {
188 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
189 MachineInstrBuilder MIB =
190 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
191 .addFrameIndex(FramePtrSpillFI).addImm(0);
192 AddDefaultCC(AddDefaultPred(MIB));
193 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000194
Eric Christopher8b3ca622010-11-18 19:40:05 +0000195 // Move past area 2.
196 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000197
Anton Korobeynikov33464912010-11-15 00:06:54 +0000198 // Determine starting offsets of spill areas.
199 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
200 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
201 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
202 if (HasFP)
203 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
204 NumBytes);
205 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
206 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
207 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
208
Eric Christopher8b3ca622010-11-18 19:40:05 +0000209 // Move past area 3.
210 if (DPRCSSize > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000211
Anton Korobeynikov33464912010-11-15 00:06:54 +0000212 NumBytes = DPRCSOffset;
213 if (NumBytes) {
214 // Adjust SP after all the callee-save spills.
215 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
Evan Chengab5c7032010-11-22 18:12:04 +0000216 if (HasFP && isARM)
217 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
218 // Note it's not safe to do this in Thumb2 mode because it would have
219 // taken two instructions:
220 // mov sp, r7
221 // sub sp, #24
222 // If an interrupt is taken between the two instructions, then sp is in
223 // an inconsistent state (pointing to the middle of callee-saved area).
224 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000225 AFI->setShouldRestoreSPFromFP(true);
226 }
227
Evan Chengab5c7032010-11-22 18:12:04 +0000228 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000229 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
230 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000231
232 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
233 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
234 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
235
236 // If we need dynamic stack realignment, do it here. Be paranoid and make
237 // sure if we also have VLAs, we have a base pointer for frame access.
238 if (RegInfo->needsStackRealignment(MF)) {
239 unsigned MaxAlign = MFI->getMaxAlignment();
240 assert (!AFI->isThumb1OnlyFunction());
241 if (!AFI->isThumbFunction()) {
242 // Emit bic sp, sp, MaxAlign
243 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
244 TII.get(ARM::BICri), ARM::SP)
245 .addReg(ARM::SP, RegState::Kill)
246 .addImm(MaxAlign-1)));
247 } else {
248 // We cannot use sp as source/dest register here, thus we're emitting the
249 // following sequence:
250 // mov r4, sp
251 // bic r4, r4, MaxAlign
252 // mov sp, r4
253 // FIXME: It will be better just to find spare register here.
254 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
255 .addReg(ARM::SP, RegState::Kill);
256 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
257 TII.get(ARM::t2BICri), ARM::R4)
258 .addReg(ARM::R4, RegState::Kill)
259 .addImm(MaxAlign-1)));
260 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
261 .addReg(ARM::R4, RegState::Kill);
262 }
263
264 AFI->setShouldRestoreSPFromFP(true);
265 }
266
267 // If we need a base pointer, set it up here. It's whatever the value
268 // of the stack pointer is at this point. Any variable size objects
269 // will be allocated after this, so we can still use the base pointer
270 // to reference locals.
271 if (RegInfo->hasBasePointer(MF)) {
272 if (isARM)
273 BuildMI(MBB, MBBI, dl,
274 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
275 .addReg(ARM::SP)
276 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
277 else
278 BuildMI(MBB, MBBI, dl,
279 TII.get(ARM::tMOVgpr2gpr), RegInfo->getBaseRegister())
280 .addReg(ARM::SP);
281 }
282
283 // If the frame has variable sized objects then the epilogue must restore
284 // the sp from fp.
Evan Chengab5c7032010-11-22 18:12:04 +0000285 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000286 AFI->setShouldRestoreSPFromFP(true);
287}
288
289void ARMFrameInfo::emitEpilogue(MachineFunction &MF,
290 MachineBasicBlock &MBB) const {
291 MachineBasicBlock::iterator MBBI = prior(MBB.end());
292 assert(MBBI->getDesc().isReturn() &&
293 "Can only insert epilog into returning blocks");
294 unsigned RetOpcode = MBBI->getOpcode();
295 DebugLoc dl = MBBI->getDebugLoc();
296 MachineFrameInfo *MFI = MF.getFrameInfo();
297 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
298 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
299 const ARMBaseInstrInfo &TII =
300 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
301 assert(!AFI->isThumb1OnlyFunction() &&
302 "This emitEpilogue does not support Thumb1!");
303 bool isARM = !AFI->isThumbFunction();
304
305 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
306 int NumBytes = (int)MFI->getStackSize();
307 unsigned FramePtr = RegInfo->getFrameRegister(MF);
308
309 if (!AFI->hasStackFrame()) {
310 if (NumBytes != 0)
311 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
312 } else {
313 // Unwind MBBI to point to first LDR / VLDRD.
314 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
315 if (MBBI != MBB.begin()) {
316 do
317 --MBBI;
318 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
319 if (!isCSRestore(MBBI, TII, CSRegs))
320 ++MBBI;
321 }
322
323 // Move SP to start of FP callee save spill area.
324 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
325 AFI->getGPRCalleeSavedArea2Size() +
326 AFI->getDPRCalleeSavedAreaSize());
327
328 // Reset SP based on frame pointer only if the stack frame extends beyond
329 // frame pointer stack slot or target is ELF and the function has FP.
330 if (AFI->shouldRestoreSPFromFP()) {
331 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
332 if (NumBytes) {
333 if (isARM)
334 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
335 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000336 else {
337 // It's not possible to restore SP from FP in a single instruction.
338 // For Darwin, this looks like:
339 // mov sp, r7
340 // sub sp, #24
341 // This is bad, if an interrupt is taken after the mov, sp is in an
342 // inconsistent state.
343 // Use the first callee-saved register as a scratch register.
344 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
345 "No scratch register to restore SP from FP!");
346 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000347 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000348 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
349 .addReg(ARM::R4);
350 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000351 } else {
352 // Thumb2 or ARM.
353 if (isARM)
354 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
355 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
356 else
357 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
358 .addReg(FramePtr);
359 }
360 } else if (NumBytes)
361 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
362
Eric Christopher8b3ca622010-11-18 19:40:05 +0000363 // Increment past our save areas.
364 if (AFI->getDPRCalleeSavedAreaSize()) MBBI++;
365 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
366 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000367 }
368
369 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
370 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
371 // Tail call return: adjust the stack pointer and jump to callee.
372 MBBI = prior(MBB.end());
373 MachineOperand &JumpTarget = MBBI->getOperand(0);
374
375 // Jump to label or value in register.
376 if (RetOpcode == ARM::TCRETURNdi) {
377 BuildMI(MBB, MBBI, dl,
378 TII.get(STI.isThumb() ? ARM::TAILJMPdt : ARM::TAILJMPd)).
379 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
380 JumpTarget.getTargetFlags());
381 } else if (RetOpcode == ARM::TCRETURNdiND) {
382 BuildMI(MBB, MBBI, dl,
383 TII.get(STI.isThumb() ? ARM::TAILJMPdNDt : ARM::TAILJMPdND)).
384 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
385 JumpTarget.getTargetFlags());
386 } else if (RetOpcode == ARM::TCRETURNri) {
387 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPr)).
388 addReg(JumpTarget.getReg(), RegState::Kill);
389 } else if (RetOpcode == ARM::TCRETURNriND) {
390 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPrND)).
391 addReg(JumpTarget.getReg(), RegState::Kill);
392 }
393
394 MachineInstr *NewMI = prior(MBBI);
395 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
396 NewMI->addOperand(MBBI->getOperand(i));
397
398 // Delete the pseudo instruction TCRETURN.
399 MBB.erase(MBBI);
400 }
401
402 if (VARegSaveSize)
403 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
404}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000405
406// Provide a base+offset reference to an FI slot for debug info. It's the
407// same as what we use for resolving the code-gen references for now.
408// FIXME: This can go wrong when references are SP-relative and simple call
409// frames aren't used.
410int
411ARMFrameInfo::getFrameIndexReference(const MachineFunction &MF, int FI,
412 unsigned &FrameReg) const {
413 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
414}
415
416int
417ARMFrameInfo::ResolveFrameIndexReference(const MachineFunction &MF,
418 int FI,
419 unsigned &FrameReg,
420 int SPAdj) const {
421 const MachineFrameInfo *MFI = MF.getFrameInfo();
422 const ARMBaseRegisterInfo *RegInfo =
423 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
424 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
425 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
426 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
427 bool isFixed = MFI->isFixedObjectIndex(FI);
428
429 FrameReg = ARM::SP;
430 Offset += SPAdj;
431 if (AFI->isGPRCalleeSavedArea1Frame(FI))
432 return Offset - AFI->getGPRCalleeSavedArea1Offset();
433 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
434 return Offset - AFI->getGPRCalleeSavedArea2Offset();
435 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
436 return Offset - AFI->getDPRCalleeSavedAreaOffset();
437
438 // When dynamically realigning the stack, use the frame pointer for
439 // parameters, and the stack/base pointer for locals.
440 if (RegInfo->needsStackRealignment(MF)) {
441 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
442 if (isFixed) {
443 FrameReg = RegInfo->getFrameRegister(MF);
444 Offset = FPOffset;
445 } else if (MFI->hasVarSizedObjects()) {
446 assert(RegInfo->hasBasePointer(MF) &&
447 "VLAs and dynamic stack alignment, but missing base pointer!");
448 FrameReg = RegInfo->getBaseRegister();
449 }
450 return Offset;
451 }
452
453 // If there is a frame pointer, use it when we can.
454 if (hasFP(MF) && AFI->hasStackFrame()) {
455 // Use frame pointer to reference fixed objects. Use it for locals if
456 // there are VLAs (and thus the SP isn't reliable as a base).
457 if (isFixed || (MFI->hasVarSizedObjects() && !RegInfo->hasBasePointer(MF))) {
458 FrameReg = RegInfo->getFrameRegister(MF);
459 return FPOffset;
460 } else if (MFI->hasVarSizedObjects()) {
461 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
462 // Try to use the frame pointer if we can, else use the base pointer
463 // since it's available. This is handy for the emergency spill slot, in
464 // particular.
465 if (AFI->isThumb2Function()) {
466 if (FPOffset >= -255 && FPOffset < 0) {
467 FrameReg = RegInfo->getFrameRegister(MF);
468 return FPOffset;
469 }
470 } else
471 FrameReg = RegInfo->getBaseRegister();
472 } else if (AFI->isThumb2Function()) {
473 // In Thumb2 mode, the negative offset is very limited. Try to avoid
474 // out of range references.
475 if (FPOffset >= -255 && FPOffset < 0) {
476 FrameReg = RegInfo->getFrameRegister(MF);
477 return FPOffset;
478 }
479 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
480 // Otherwise, use SP or FP, whichever is closer to the stack slot.
481 FrameReg = RegInfo->getFrameRegister(MF);
482 return FPOffset;
483 }
484 }
485 // Use the base pointer if we have one.
486 if (RegInfo->hasBasePointer(MF))
487 FrameReg = RegInfo->getBaseRegister();
488 return Offset;
489}
490
491int ARMFrameInfo::getFrameIndexOffset(const MachineFunction &MF, int FI) const {
492 unsigned FrameReg;
493 return getFrameIndexReference(MF, FI, FrameReg);
494}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000495
496void ARMFrameInfo::emitPushInst(MachineBasicBlock &MBB,
497 MachineBasicBlock::iterator MI,
498 const std::vector<CalleeSavedInfo> &CSI,
499 unsigned Opc,
500 bool(*Func)(unsigned, bool)) const {
501 MachineFunction &MF = *MBB.getParent();
502 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
503
504 DebugLoc DL;
505 if (MI != MBB.end()) DL = MI->getDebugLoc();
506
507 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(Opc));
508 MIB.addReg(ARM::SP, getDefRegState(true));
509 MIB.addReg(ARM::SP);
510 AddDefaultPred(MIB);
511 bool NumRegs = false;
512 for (unsigned i = CSI.size(); i != 0; --i) {
513 unsigned Reg = CSI[i-1].getReg();
514 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
515
516 // Add the callee-saved register as live-in unless it's LR and
517 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
518 // then it's already added to the function and entry block live-in sets.
519 bool isKill = true;
520 if (Reg == ARM::LR) {
521 if (MF.getFrameInfo()->isReturnAddressTaken() &&
522 MF.getRegInfo().isLiveIn(Reg))
523 isKill = false;
524 }
525
526 if (isKill)
527 MBB.addLiveIn(Reg);
528
529 NumRegs = true;
530 MIB.addReg(Reg, getKillRegState(isKill));
531 }
532
533 // It's illegal to emit push instruction without operands.
534 if (NumRegs)
535 MBB.insert(MI, &*MIB);
536 else
537 MF.DeleteMachineInstr(MIB);
538}
539
540bool ARMFrameInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
541 MachineBasicBlock::iterator MI,
542 const std::vector<CalleeSavedInfo> &CSI,
543 const TargetRegisterInfo *TRI) const {
544 if (CSI.empty())
545 return false;
546
547 MachineFunction &MF = *MBB.getParent();
548 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
549 DebugLoc DL = MI->getDebugLoc();
550
551 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
552 unsigned FltOpc = ARM::VSTMDDB_UPD;
553 emitPushInst(MBB, MI, CSI, PushOpc, &isARMArea1Register);
554 emitPushInst(MBB, MI, CSI, PushOpc, &isARMArea2Register);
555 emitPushInst(MBB, MI, CSI, FltOpc, &isARMArea3Register);
556
557 return true;
558}
559
560void ARMFrameInfo::emitPopInst(MachineBasicBlock &MBB,
561 MachineBasicBlock::iterator MI,
562 const std::vector<CalleeSavedInfo> &CSI,
563 unsigned Opc, bool isVarArg,
564 bool(*Func)(unsigned, bool)) const {
565 MachineFunction &MF = *MBB.getParent();
566 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
567 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
568 DebugLoc DL = MI->getDebugLoc();
569
570 MachineInstrBuilder MIB = BuildMI(MF, DL, TII.get(Opc));
571 MIB.addReg(ARM::SP, getDefRegState(true));
572 MIB.addReg(ARM::SP);
573 AddDefaultPred(MIB);
574 bool NumRegs = false;
575 for (unsigned i = CSI.size(); i != 0; --i) {
576 unsigned Reg = CSI[i-1].getReg();
577 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
578
579 if (Reg == ARM::LR && !isVarArg) {
580 Reg = ARM::PC;
581 unsigned Opc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
582 (*MIB).setDesc(TII.get(Opc));
583 MI = MBB.erase(MI);
584 }
585
586 MIB.addReg(Reg, RegState::Define);
587 NumRegs = true;
588 }
589
590 // It's illegal to emit pop instruction without operands.
591 if (NumRegs)
592 MBB.insert(MI, &*MIB);
593 else
594 MF.DeleteMachineInstr(MIB);
595}
596
597bool ARMFrameInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
598 MachineBasicBlock::iterator MI,
599 const std::vector<CalleeSavedInfo> &CSI,
600 const TargetRegisterInfo *TRI) const {
601 if (CSI.empty())
602 return false;
603
604 MachineFunction &MF = *MBB.getParent();
605 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
606 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
607 DebugLoc DL = MI->getDebugLoc();
608
609 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
610 unsigned FltOpc = ARM::VLDMDIA_UPD;
611 emitPopInst(MBB, MI, CSI, FltOpc, isVarArg, &isARMArea3Register);
612 emitPopInst(MBB, MI, CSI, PopOpc, isVarArg, &isARMArea2Register);
613 emitPopInst(MBB, MI, CSI, PopOpc, isVarArg, &isARMArea1Register);
614
615 return true;
616}