blob: 824538ccd79dd4cf8ded64dc7014ea50b5d02c11 [file] [log] [blame]
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001//=======- ARMFrameLowering.cpp - ARM Frame Information --------*- C++ -*-====//
Anton Korobeynikov33464912010-11-15 00:06:54 +00002//
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//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the ARM implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "ARMFrameLowering.h"
Jim Grosbachc6f92612010-12-09 18:31:13 +000015#include "ARMAddressingModes.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000016#include "ARMBaseInstrInfo.h"
17#include "ARMMachineFunctionInfo.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengab5c7032010-11-22 18:12:04 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000022#include "llvm/CodeGen/RegisterScavenging.h"
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000023#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000024
25using namespace llvm;
26
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000027/// hasFP - Return true if the specified function should have a dedicated frame
28/// pointer register. This is true if the function has variable sized allocas
29/// or if frame pointer elimination is disabled.
30///
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000031bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000032 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
33
34 // Mac OS X requires FP not to be clobbered for backtracing purpose.
35 if (STI.isTargetDarwin())
36 return true;
37
38 const MachineFrameInfo *MFI = MF.getFrameInfo();
39 // Always eliminate non-leaf frame pointers.
40 return ((DisableFramePointerElim(MF) && MFI->hasCalls()) ||
41 RegInfo->needsStackRealignment(MF) ||
42 MFI->hasVarSizedObjects() ||
43 MFI->isFrameAddressTaken());
44}
45
46// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
47// not required, we reserve argument space for call sites in the function
48// immediately on entry to the current function. This eliminates the need for
49// add/sub sp brackets around call sites. Returns true if the call frame is
50// included as part of the stack frame.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000051bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000052 const MachineFrameInfo *FFI = MF.getFrameInfo();
53 unsigned CFSize = FFI->getMaxCallFrameSize();
54 // It's not always a good idea to include the call frame as part of the
55 // stack frame. ARM (especially Thumb) has small immediate offset to
56 // address the stack frame. So a large call frame can cause poor codegen
57 // and may even makes it impossible to scavenge a register.
58 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
59 return false;
60
61 return !MF.getFrameInfo()->hasVarSizedObjects();
62}
63
64// canSimplifyCallFramePseudos - If there is a reserved call frame, the
65// call frame pseudos can be simplified. Unlike most targets, having a FP
66// is not sufficient here since we still may reference some objects via SP
67// even when FP is available in Thumb2 mode.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000068bool ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF)const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000069 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
70}
71
Anton Korobeynikov33464912010-11-15 00:06:54 +000072static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
73 for (unsigned i = 0; CSRegs[i]; ++i)
74 if (Reg == CSRegs[i])
75 return true;
76 return false;
77}
78
79static bool isCSRestore(MachineInstr *MI,
80 const ARMBaseInstrInfo &TII,
81 const unsigned *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000082 // Integer spill area is handled with "pop".
83 if (MI->getOpcode() == ARM::LDMIA_RET ||
84 MI->getOpcode() == ARM::t2LDMIA_RET ||
85 MI->getOpcode() == ARM::LDMIA_UPD ||
86 MI->getOpcode() == ARM::t2LDMIA_UPD ||
87 MI->getOpcode() == ARM::VLDMDIA_UPD) {
88 // The first two operands are predicates. The last two are
89 // imp-def and imp-use of SP. Check everything in between.
90 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
91 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
92 return false;
93 return true;
94 }
Jim Grosbach568f5282010-12-10 18:41:15 +000095 if ((MI->getOpcode() == ARM::LDR_POST ||
96 MI->getOpcode() == ARM::t2LDR_POST) &&
97 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
98 MI->getOperand(1).getReg() == ARM::SP)
99 return true;
Eric Christopher8b3ca622010-11-18 19:40:05 +0000100
101 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000102}
103
104static void
105emitSPUpdate(bool isARM,
106 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
107 DebugLoc dl, const ARMBaseInstrInfo &TII,
108 int NumBytes,
109 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
110 if (isARM)
111 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
112 Pred, PredReg, TII);
113 else
114 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
115 Pred, PredReg, TII);
116}
117
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000118void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000119 MachineBasicBlock &MBB = MF.front();
120 MachineBasicBlock::iterator MBBI = MBB.begin();
121 MachineFrameInfo *MFI = MF.getFrameInfo();
122 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
123 const ARMBaseRegisterInfo *RegInfo =
124 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
125 const ARMBaseInstrInfo &TII =
126 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
127 assert(!AFI->isThumb1OnlyFunction() &&
128 "This emitPrologue does not support Thumb1!");
129 bool isARM = !AFI->isThumbFunction();
130 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
131 unsigned NumBytes = MFI->getStackSize();
132 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
133 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
134 unsigned FramePtr = RegInfo->getFrameRegister(MF);
135
136 // Determine the sizes of each callee-save spill areas and record which frame
137 // belongs to which callee-save spill areas.
138 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
139 int FramePtrSpillFI = 0;
140
141 // Allocate the vararg register save area. This is not counted in NumBytes.
142 if (VARegSaveSize)
143 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
144
145 if (!AFI->hasStackFrame()) {
146 if (NumBytes != 0)
147 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
148 return;
149 }
150
151 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
152 unsigned Reg = CSI[i].getReg();
153 int FI = CSI[i].getFrameIdx();
154 switch (Reg) {
155 case ARM::R4:
156 case ARM::R5:
157 case ARM::R6:
158 case ARM::R7:
159 case ARM::LR:
160 if (Reg == FramePtr)
161 FramePtrSpillFI = FI;
162 AFI->addGPRCalleeSavedArea1Frame(FI);
163 GPRCS1Size += 4;
164 break;
165 case ARM::R8:
166 case ARM::R9:
167 case ARM::R10:
168 case ARM::R11:
169 if (Reg == FramePtr)
170 FramePtrSpillFI = FI;
171 if (STI.isTargetDarwin()) {
172 AFI->addGPRCalleeSavedArea2Frame(FI);
173 GPRCS2Size += 4;
174 } else {
175 AFI->addGPRCalleeSavedArea1Frame(FI);
176 GPRCS1Size += 4;
177 }
178 break;
179 default:
180 AFI->addDPRCalleeSavedAreaFrame(FI);
181 DPRCSSize += 8;
182 }
183 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000184
Eric Christopher8b3ca622010-11-18 19:40:05 +0000185 // Move past area 1.
186 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000187
Anton Korobeynikov33464912010-11-15 00:06:54 +0000188 // Set FP to point to the stack slot that contains the previous FP.
189 // For Darwin, FP is R7, which has now been stored in spill area 1.
190 // Otherwise, if this is not Darwin, all the callee-saved registers go
191 // into spill area 1, including the FP in R11. In either case, it is
192 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000193 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000194 if (HasFP) {
195 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
196 MachineInstrBuilder MIB =
197 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
198 .addFrameIndex(FramePtrSpillFI).addImm(0);
199 AddDefaultCC(AddDefaultPred(MIB));
200 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000201
Eric Christopher8b3ca622010-11-18 19:40:05 +0000202 // Move past area 2.
203 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000204
Anton Korobeynikov33464912010-11-15 00:06:54 +0000205 // Determine starting offsets of spill areas.
206 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
207 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
208 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
209 if (HasFP)
210 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
211 NumBytes);
212 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
213 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
214 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
215
Eric Christopher8b3ca622010-11-18 19:40:05 +0000216 // Move past area 3.
217 if (DPRCSSize > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000218
Anton Korobeynikov33464912010-11-15 00:06:54 +0000219 NumBytes = DPRCSOffset;
220 if (NumBytes) {
221 // Adjust SP after all the callee-save spills.
222 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
Evan Chengab5c7032010-11-22 18:12:04 +0000223 if (HasFP && isARM)
224 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
225 // Note it's not safe to do this in Thumb2 mode because it would have
226 // taken two instructions:
227 // mov sp, r7
228 // sub sp, #24
229 // If an interrupt is taken between the two instructions, then sp is in
230 // an inconsistent state (pointing to the middle of callee-saved area).
231 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000232 AFI->setShouldRestoreSPFromFP(true);
233 }
234
Evan Chengab5c7032010-11-22 18:12:04 +0000235 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000236 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
237 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000238
239 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
240 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
241 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
242
243 // If we need dynamic stack realignment, do it here. Be paranoid and make
244 // sure if we also have VLAs, we have a base pointer for frame access.
245 if (RegInfo->needsStackRealignment(MF)) {
246 unsigned MaxAlign = MFI->getMaxAlignment();
247 assert (!AFI->isThumb1OnlyFunction());
248 if (!AFI->isThumbFunction()) {
249 // Emit bic sp, sp, MaxAlign
250 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
251 TII.get(ARM::BICri), ARM::SP)
252 .addReg(ARM::SP, RegState::Kill)
253 .addImm(MaxAlign-1)));
254 } else {
255 // We cannot use sp as source/dest register here, thus we're emitting the
256 // following sequence:
257 // mov r4, sp
258 // bic r4, r4, MaxAlign
259 // mov sp, r4
260 // FIXME: It will be better just to find spare register here.
261 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
262 .addReg(ARM::SP, RegState::Kill);
263 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
264 TII.get(ARM::t2BICri), ARM::R4)
265 .addReg(ARM::R4, RegState::Kill)
266 .addImm(MaxAlign-1)));
267 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
268 .addReg(ARM::R4, RegState::Kill);
269 }
270
271 AFI->setShouldRestoreSPFromFP(true);
272 }
273
274 // If we need a base pointer, set it up here. It's whatever the value
275 // of the stack pointer is at this point. Any variable size objects
276 // will be allocated after this, so we can still use the base pointer
277 // to reference locals.
278 if (RegInfo->hasBasePointer(MF)) {
279 if (isARM)
280 BuildMI(MBB, MBBI, dl,
281 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
282 .addReg(ARM::SP)
283 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
284 else
285 BuildMI(MBB, MBBI, dl,
286 TII.get(ARM::tMOVgpr2gpr), RegInfo->getBaseRegister())
287 .addReg(ARM::SP);
288 }
289
290 // If the frame has variable sized objects then the epilogue must restore
291 // the sp from fp.
Evan Chengab5c7032010-11-22 18:12:04 +0000292 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000293 AFI->setShouldRestoreSPFromFP(true);
294}
295
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000296void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000297 MachineBasicBlock &MBB) const {
298 MachineBasicBlock::iterator MBBI = prior(MBB.end());
299 assert(MBBI->getDesc().isReturn() &&
300 "Can only insert epilog into returning blocks");
301 unsigned RetOpcode = MBBI->getOpcode();
302 DebugLoc dl = MBBI->getDebugLoc();
303 MachineFrameInfo *MFI = MF.getFrameInfo();
304 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
305 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
306 const ARMBaseInstrInfo &TII =
307 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
308 assert(!AFI->isThumb1OnlyFunction() &&
309 "This emitEpilogue does not support Thumb1!");
310 bool isARM = !AFI->isThumbFunction();
311
312 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
313 int NumBytes = (int)MFI->getStackSize();
314 unsigned FramePtr = RegInfo->getFrameRegister(MF);
315
316 if (!AFI->hasStackFrame()) {
317 if (NumBytes != 0)
318 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
319 } else {
320 // Unwind MBBI to point to first LDR / VLDRD.
321 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
322 if (MBBI != MBB.begin()) {
323 do
324 --MBBI;
325 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
326 if (!isCSRestore(MBBI, TII, CSRegs))
327 ++MBBI;
328 }
329
330 // Move SP to start of FP callee save spill area.
331 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
332 AFI->getGPRCalleeSavedArea2Size() +
333 AFI->getDPRCalleeSavedAreaSize());
334
335 // Reset SP based on frame pointer only if the stack frame extends beyond
336 // frame pointer stack slot or target is ELF and the function has FP.
337 if (AFI->shouldRestoreSPFromFP()) {
338 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
339 if (NumBytes) {
340 if (isARM)
341 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
342 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000343 else {
344 // It's not possible to restore SP from FP in a single instruction.
345 // For Darwin, this looks like:
346 // mov sp, r7
347 // sub sp, #24
348 // This is bad, if an interrupt is taken after the mov, sp is in an
349 // inconsistent state.
350 // Use the first callee-saved register as a scratch register.
351 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
352 "No scratch register to restore SP from FP!");
353 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000354 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000355 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
356 .addReg(ARM::R4);
357 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000358 } else {
359 // Thumb2 or ARM.
360 if (isARM)
361 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
362 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
363 else
364 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
365 .addReg(FramePtr);
366 }
367 } else if (NumBytes)
368 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
369
Eric Christopher8b3ca622010-11-18 19:40:05 +0000370 // Increment past our save areas.
371 if (AFI->getDPRCalleeSavedAreaSize()) MBBI++;
372 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
373 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000374 }
375
376 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
377 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
378 // Tail call return: adjust the stack pointer and jump to callee.
379 MBBI = prior(MBB.end());
380 MachineOperand &JumpTarget = MBBI->getOperand(0);
381
382 // Jump to label or value in register.
Evan Cheng3d2125c2010-11-30 23:55:39 +0000383 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) {
384 unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi)
385 ? (STI.isThumb() ? ARM::TAILJMPdt : ARM::TAILJMPd)
386 : (STI.isThumb() ? ARM::TAILJMPdNDt : ARM::TAILJMPdND);
387 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
388 if (JumpTarget.isGlobal())
389 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
390 JumpTarget.getTargetFlags());
391 else {
392 assert(JumpTarget.isSymbol());
393 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
394 JumpTarget.getTargetFlags());
395 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000396 } else if (RetOpcode == ARM::TCRETURNri) {
397 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPr)).
398 addReg(JumpTarget.getReg(), RegState::Kill);
399 } else if (RetOpcode == ARM::TCRETURNriND) {
400 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPrND)).
401 addReg(JumpTarget.getReg(), RegState::Kill);
402 }
403
404 MachineInstr *NewMI = prior(MBBI);
405 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
406 NewMI->addOperand(MBBI->getOperand(i));
407
408 // Delete the pseudo instruction TCRETURN.
409 MBB.erase(MBBI);
410 }
411
412 if (VARegSaveSize)
413 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
414}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000415
416// Provide a base+offset reference to an FI slot for debug info. It's the
417// same as what we use for resolving the code-gen references for now.
418// FIXME: This can go wrong when references are SP-relative and simple call
419// frames aren't used.
420int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000421ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000422 unsigned &FrameReg) const {
423 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
424}
425
426int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000427ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000428 int FI,
429 unsigned &FrameReg,
430 int SPAdj) const {
431 const MachineFrameInfo *MFI = MF.getFrameInfo();
432 const ARMBaseRegisterInfo *RegInfo =
433 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
434 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
435 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
436 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
437 bool isFixed = MFI->isFixedObjectIndex(FI);
438
439 FrameReg = ARM::SP;
440 Offset += SPAdj;
441 if (AFI->isGPRCalleeSavedArea1Frame(FI))
442 return Offset - AFI->getGPRCalleeSavedArea1Offset();
443 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
444 return Offset - AFI->getGPRCalleeSavedArea2Offset();
445 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
446 return Offset - AFI->getDPRCalleeSavedAreaOffset();
447
448 // When dynamically realigning the stack, use the frame pointer for
449 // parameters, and the stack/base pointer for locals.
450 if (RegInfo->needsStackRealignment(MF)) {
451 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
452 if (isFixed) {
453 FrameReg = RegInfo->getFrameRegister(MF);
454 Offset = FPOffset;
455 } else if (MFI->hasVarSizedObjects()) {
456 assert(RegInfo->hasBasePointer(MF) &&
457 "VLAs and dynamic stack alignment, but missing base pointer!");
458 FrameReg = RegInfo->getBaseRegister();
459 }
460 return Offset;
461 }
462
463 // If there is a frame pointer, use it when we can.
464 if (hasFP(MF) && AFI->hasStackFrame()) {
465 // Use frame pointer to reference fixed objects. Use it for locals if
466 // there are VLAs (and thus the SP isn't reliable as a base).
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000467 if (isFixed || (MFI->hasVarSizedObjects() &&
468 !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000469 FrameReg = RegInfo->getFrameRegister(MF);
470 return FPOffset;
471 } else if (MFI->hasVarSizedObjects()) {
472 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
473 // Try to use the frame pointer if we can, else use the base pointer
474 // since it's available. This is handy for the emergency spill slot, in
475 // particular.
476 if (AFI->isThumb2Function()) {
477 if (FPOffset >= -255 && FPOffset < 0) {
478 FrameReg = RegInfo->getFrameRegister(MF);
479 return FPOffset;
480 }
481 } else
482 FrameReg = RegInfo->getBaseRegister();
483 } else if (AFI->isThumb2Function()) {
484 // In Thumb2 mode, the negative offset is very limited. Try to avoid
485 // out of range references.
486 if (FPOffset >= -255 && FPOffset < 0) {
487 FrameReg = RegInfo->getFrameRegister(MF);
488 return FPOffset;
489 }
490 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
491 // Otherwise, use SP or FP, whichever is closer to the stack slot.
492 FrameReg = RegInfo->getFrameRegister(MF);
493 return FPOffset;
494 }
495 }
496 // Use the base pointer if we have one.
497 if (RegInfo->hasBasePointer(MF))
498 FrameReg = RegInfo->getBaseRegister();
499 return Offset;
500}
501
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000502int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, int FI) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000503 unsigned FrameReg;
504 return getFrameIndexReference(MF, FI, FrameReg);
505}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000506
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000507void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000508 MachineBasicBlock::iterator MI,
509 const std::vector<CalleeSavedInfo> &CSI,
Jim Grosbachc6f92612010-12-09 18:31:13 +0000510 unsigned StmOpc, unsigned StrOpc, bool NoGap,
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000511 bool(*Func)(unsigned, bool)) const {
512 MachineFunction &MF = *MBB.getParent();
513 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
514
515 DebugLoc DL;
516 if (MI != MBB.end()) DL = MI->getDebugLoc();
517
Evan Cheng9801b5c2010-12-07 19:59:34 +0000518 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng06d65f52010-12-07 23:08:38 +0000519 unsigned i = CSI.size();
520 while (i != 0) {
521 unsigned LastReg = 0;
522 for (; i != 0; --i) {
523 unsigned Reg = CSI[i-1].getReg();
524 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000525
Evan Cheng06d65f52010-12-07 23:08:38 +0000526 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000527 // @llvm.returnaddress is called. If LR is returned for
528 // @llvm.returnaddress then it's already added to the function and
529 // entry block live-in sets.
Evan Cheng06d65f52010-12-07 23:08:38 +0000530 bool isKill = true;
531 if (Reg == ARM::LR) {
532 if (MF.getFrameInfo()->isReturnAddressTaken() &&
533 MF.getRegInfo().isLiveIn(Reg))
534 isKill = false;
535 }
536
537 if (isKill)
538 MBB.addLiveIn(Reg);
539
Eric Christopher1a48c032010-12-09 01:57:45 +0000540 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng275bf632010-12-08 06:29:02 +0000541 // for other instructions. e.g.
Eric Christopher1a48c032010-12-09 01:57:45 +0000542 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng275bf632010-12-08 06:29:02 +0000543 if (NoGap && LastReg && LastReg != Reg-1)
544 break;
Evan Cheng06d65f52010-12-07 23:08:38 +0000545 LastReg = Reg;
546 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000547 }
548
Jim Grosbachc6f92612010-12-09 18:31:13 +0000549 if (Regs.empty())
550 continue;
551 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000552 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000553 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000554 .addReg(ARM::SP));
555 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
556 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbachc6f92612010-12-09 18:31:13 +0000557 } else if (Regs.size() == 1) {
558 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
559 ARM::SP)
560 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
561 .addReg(ARM::SP);
562 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
563 // that refactoring is complete (eventually).
564 if (StrOpc == ARM::STR_PRE) {
565 MIB.addReg(0);
566 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::sub, 4, ARM_AM::no_shift));
567 } else
568 MIB.addImm(-4);
569 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000570 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000571 Regs.clear();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000572 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000573}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000574
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000575void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Evan Cheng06d65f52010-12-07 23:08:38 +0000576 MachineBasicBlock::iterator MI,
577 const std::vector<CalleeSavedInfo> &CSI,
Jim Grosbachc6f92612010-12-09 18:31:13 +0000578 unsigned LdmOpc, unsigned LdrOpc,
579 bool isVarArg, bool NoGap,
Evan Cheng06d65f52010-12-07 23:08:38 +0000580 bool(*Func)(unsigned, bool)) const {
581 MachineFunction &MF = *MBB.getParent();
582 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
583 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
584 DebugLoc DL = MI->getDebugLoc();
585
586 SmallVector<unsigned, 4> Regs;
587 unsigned i = CSI.size();
588 while (i != 0) {
589 unsigned LastReg = 0;
590 bool DeleteRet = false;
591 for (; i != 0; --i) {
592 unsigned Reg = CSI[i-1].getReg();
593 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
594
Bob Wilson6819dbb2011-01-06 19:24:41 +0000595 if (Reg == ARM::LR && !isVarArg && STI.hasV5TOps()) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000596 Reg = ARM::PC;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000597 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng06d65f52010-12-07 23:08:38 +0000598 // Fold the return instruction into the LDM.
599 DeleteRet = true;
600 }
601
Evan Cheng275bf632010-12-08 06:29:02 +0000602 // If NoGap is true, pop consecutive registers and then leave the rest
603 // for other instructions. e.g.
604 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
605 if (NoGap && LastReg && LastReg != Reg-1)
606 break;
607
Evan Cheng06d65f52010-12-07 23:08:38 +0000608 LastReg = Reg;
609 Regs.push_back(Reg);
610 }
611
Jim Grosbachc6f92612010-12-09 18:31:13 +0000612 if (Regs.empty())
613 continue;
614 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000615 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000616 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000617 .addReg(ARM::SP));
618 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
619 MIB.addReg(Regs[i], getDefRegState(true));
620 if (DeleteRet)
621 MI->eraseFromParent();
622 MI = MIB;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000623 } else if (Regs.size() == 1) {
624 // If we adjusted the reg to PC from LR above, switch it back here. We
625 // only do that for LDM.
626 if (Regs[0] == ARM::PC)
627 Regs[0] = ARM::LR;
628 MachineInstrBuilder MIB =
629 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
630 .addReg(ARM::SP, RegState::Define)
631 .addReg(ARM::SP);
632 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
633 // that refactoring is complete (eventually).
634 if (LdrOpc == ARM::LDR_POST) {
635 MIB.addReg(0);
636 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
637 } else
638 MIB.addImm(4);
639 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000640 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000641 Regs.clear();
Evan Cheng9801b5c2010-12-07 19:59:34 +0000642 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000643}
644
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000645bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000646 MachineBasicBlock::iterator MI,
647 const std::vector<CalleeSavedInfo> &CSI,
648 const TargetRegisterInfo *TRI) const {
649 if (CSI.empty())
650 return false;
651
652 MachineFunction &MF = *MBB.getParent();
653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
654 DebugLoc DL = MI->getDebugLoc();
655
656 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000657 unsigned PushOneOpc = AFI->isThumbFunction() ? ARM::t2STR_PRE : ARM::STR_PRE;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000658 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000659 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register);
660 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register);
Bob Wilson28f10152011-01-06 19:24:36 +0000661 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000662
663 return true;
664}
665
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000666bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000667 MachineBasicBlock::iterator MI,
668 const std::vector<CalleeSavedInfo> &CSI,
669 const TargetRegisterInfo *TRI) const {
670 if (CSI.empty())
671 return false;
672
673 MachineFunction &MF = *MBB.getParent();
674 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
675 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
676 DebugLoc DL = MI->getDebugLoc();
677
678 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000679 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST : ARM::LDR_POST;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000680 unsigned FltOpc = ARM::VLDMDIA_UPD;
Bob Wilson28f10152011-01-06 19:24:36 +0000681 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000682 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
683 &isARMArea2Register);
684 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
685 &isARMArea1Register);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000686
687 return true;
688}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000689
690// FIXME: Make generic?
691static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
692 const ARMBaseInstrInfo &TII) {
693 unsigned FnSize = 0;
694 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
695 MBBI != E; ++MBBI) {
696 const MachineBasicBlock &MBB = *MBBI;
697 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
698 I != E; ++I)
699 FnSize += TII.GetInstSizeInBytes(I);
700 }
701 return FnSize;
702}
703
704/// estimateStackSize - Estimate and return the size of the frame.
705/// FIXME: Make generic?
706static unsigned estimateStackSize(MachineFunction &MF) {
707 const MachineFrameInfo *FFI = MF.getFrameInfo();
708 int Offset = 0;
709 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
710 int FixedOff = -FFI->getObjectOffset(i);
711 if (FixedOff > Offset) Offset = FixedOff;
712 }
713 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
714 if (FFI->isDeadObjectIndex(i))
715 continue;
716 Offset += FFI->getObjectSize(i);
717 unsigned Align = FFI->getObjectAlignment(i);
718 // Adjust to alignment boundary
719 Offset = (Offset+Align-1)/Align*Align;
720 }
721 return (unsigned)Offset;
722}
723
724/// estimateRSStackSizeLimit - Look at each instruction that references stack
725/// frames and return the stack size limit beyond which some of these
726/// instructions will require a scratch register during their expansion later.
727// FIXME: Move to TII?
728static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000729 const TargetFrameLowering *TFI) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000730 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
731 unsigned Limit = (1 << 12) - 1;
732 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
733 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
734 I != E; ++I) {
735 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
736 if (!I->getOperand(i).isFI()) continue;
737
738 // When using ADDri to get the address of a stack object, 255 is the
739 // largest offset guaranteed to fit in the immediate offset.
740 if (I->getOpcode() == ARM::ADDri) {
741 Limit = std::min(Limit, (1U << 8) - 1);
742 break;
743 }
744
745 // Otherwise check the addressing mode.
746 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
747 case ARMII::AddrMode3:
748 case ARMII::AddrModeT2_i8:
749 Limit = std::min(Limit, (1U << 8) - 1);
750 break;
751 case ARMII::AddrMode5:
752 case ARMII::AddrModeT2_i8s4:
753 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
754 break;
755 case ARMII::AddrModeT2_i12:
756 // i12 supports only positive offset so these will be converted to
757 // i8 opcodes. See llvm::rewriteT2FrameIndex.
758 if (TFI->hasFP(MF) && AFI->hasStackFrame())
759 Limit = std::min(Limit, (1U << 8) - 1);
760 break;
761 case ARMII::AddrMode4:
762 case ARMII::AddrMode6:
763 // Addressing modes 4 & 6 (load/store) instructions can't encode an
764 // immediate offset for stack references.
765 return 0;
766 default:
767 break;
768 }
769 break; // At most one FI per instruction
770 }
771 }
772 }
773
774 return Limit;
775}
776
777void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000778ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000779 RegScavenger *RS) const {
780 // This tells PEI to spill the FP as if it is any other callee-save register
781 // to take advantage the eliminateFrameIndex machinery. This also ensures it
782 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
783 // to combine multiple loads / stores.
784 bool CanEliminateFrame = true;
785 bool CS1Spilled = false;
786 bool LRSpilled = false;
787 unsigned NumGPRSpills = 0;
788 SmallVector<unsigned, 4> UnspilledCS1GPRs;
789 SmallVector<unsigned, 4> UnspilledCS2GPRs;
790 const ARMBaseRegisterInfo *RegInfo =
791 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
792 const ARMBaseInstrInfo &TII =
793 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
794 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
795 MachineFrameInfo *MFI = MF.getFrameInfo();
796 unsigned FramePtr = RegInfo->getFrameRegister(MF);
797
798 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
799 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
800 // since it's always posible to restore sp from fp in a single instruction.
801 // FIXME: It will be better just to find spare register here.
802 if (AFI->isThumb2Function() &&
803 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
804 MF.getRegInfo().setPhysRegUsed(ARM::R4);
805
806 // Spill LR if Thumb1 function uses variable length argument lists.
807 if (AFI->isThumb1OnlyFunction() && AFI->getVarArgsRegSaveSize() > 0)
808 MF.getRegInfo().setPhysRegUsed(ARM::LR);
809
810 // Spill the BasePtr if it's used.
811 if (RegInfo->hasBasePointer(MF))
812 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
813
814 // Don't spill FP if the frame can be eliminated. This is determined
815 // by scanning the callee-save registers to see if any is used.
816 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
817 for (unsigned i = 0; CSRegs[i]; ++i) {
818 unsigned Reg = CSRegs[i];
819 bool Spilled = false;
820 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
821 AFI->setCSRegisterIsSpilled(Reg);
822 Spilled = true;
823 CanEliminateFrame = false;
824 } else {
825 // Check alias registers too.
826 for (const unsigned *Aliases =
827 RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) {
828 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
829 Spilled = true;
830 CanEliminateFrame = false;
831 }
832 }
833 }
834
835 if (!ARM::GPRRegisterClass->contains(Reg))
836 continue;
837
838 if (Spilled) {
839 NumGPRSpills++;
840
841 if (!STI.isTargetDarwin()) {
842 if (Reg == ARM::LR)
843 LRSpilled = true;
844 CS1Spilled = true;
845 continue;
846 }
847
848 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
849 switch (Reg) {
850 case ARM::LR:
851 LRSpilled = true;
852 // Fallthrough
853 case ARM::R4: case ARM::R5:
854 case ARM::R6: case ARM::R7:
855 CS1Spilled = true;
856 break;
857 default:
858 break;
859 }
860 } else {
861 if (!STI.isTargetDarwin()) {
862 UnspilledCS1GPRs.push_back(Reg);
863 continue;
864 }
865
866 switch (Reg) {
867 case ARM::R4: case ARM::R5:
868 case ARM::R6: case ARM::R7:
869 case ARM::LR:
870 UnspilledCS1GPRs.push_back(Reg);
871 break;
872 default:
873 UnspilledCS2GPRs.push_back(Reg);
874 break;
875 }
876 }
877 }
878
879 bool ForceLRSpill = false;
880 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
881 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
882 // Force LR to be spilled if the Thumb function size is > 2048. This enables
883 // use of BL to implement far jump. If it turns out that it's not needed
884 // then the branch fix up path will undo it.
885 if (FnSize >= (1 << 11)) {
886 CanEliminateFrame = false;
887 ForceLRSpill = true;
888 }
889 }
890
891 // If any of the stack slot references may be out of range of an immediate
892 // offset, make sure a register (or a spill slot) is available for the
893 // register scavenger. Note that if we're indexing off the frame pointer, the
894 // effective stack size is 4 bytes larger since the FP points to the stack
895 // slot of the previous FP. Also, if we have variable sized objects in the
896 // function, stack slot references will often be negative, and some of
897 // our instructions are positive-offset only, so conservatively consider
898 // that case to want a spill slot (or register) as well. Similarly, if
899 // the function adjusts the stack pointer during execution and the
900 // adjustments aren't already part of our stack size estimate, our offset
901 // calculations may be off, so be conservative.
902 // FIXME: We could add logic to be more precise about negative offsets
903 // and which instructions will need a scratch register for them. Is it
904 // worth the effort and added fragility?
905 bool BigStack =
906 (RS &&
907 (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
908 estimateRSStackSizeLimit(MF, this)))
909 || MFI->hasVarSizedObjects()
910 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
911
912 bool ExtraCSSpill = false;
913 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
914 AFI->setHasStackFrame(true);
915
916 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
917 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
918 if (!LRSpilled && CS1Spilled) {
919 MF.getRegInfo().setPhysRegUsed(ARM::LR);
920 AFI->setCSRegisterIsSpilled(ARM::LR);
921 NumGPRSpills++;
922 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
923 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
924 ForceLRSpill = false;
925 ExtraCSSpill = true;
926 }
927
928 if (hasFP(MF)) {
929 MF.getRegInfo().setPhysRegUsed(FramePtr);
930 NumGPRSpills++;
931 }
932
933 // If stack and double are 8-byte aligned and we are spilling an odd number
934 // of GPRs, spill one extra callee save GPR so we won't have to pad between
935 // the integer and double callee save areas.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000936 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000937 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
938 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
939 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
940 unsigned Reg = UnspilledCS1GPRs[i];
941 // Don't spill high register if the function is thumb1
942 if (!AFI->isThumb1OnlyFunction() ||
943 isARMLowRegister(Reg) || Reg == ARM::LR) {
944 MF.getRegInfo().setPhysRegUsed(Reg);
945 AFI->setCSRegisterIsSpilled(Reg);
946 if (!RegInfo->isReservedReg(MF, Reg))
947 ExtraCSSpill = true;
948 break;
949 }
950 }
951 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
952 unsigned Reg = UnspilledCS2GPRs.front();
953 MF.getRegInfo().setPhysRegUsed(Reg);
954 AFI->setCSRegisterIsSpilled(Reg);
955 if (!RegInfo->isReservedReg(MF, Reg))
956 ExtraCSSpill = true;
957 }
958 }
959
960 // Estimate if we might need to scavenge a register at some point in order
961 // to materialize a stack offset. If so, either spill one additional
962 // callee-saved register or reserve a special spill slot to facilitate
963 // register scavenging. Thumb1 needs a spill slot for stack pointer
964 // adjustments also, even when the frame itself is small.
965 if (BigStack && !ExtraCSSpill) {
966 // If any non-reserved CS register isn't spilled, just spill one or two
967 // extra. That should take care of it!
968 unsigned NumExtras = TargetAlign / 4;
969 SmallVector<unsigned, 2> Extras;
970 while (NumExtras && !UnspilledCS1GPRs.empty()) {
971 unsigned Reg = UnspilledCS1GPRs.back();
972 UnspilledCS1GPRs.pop_back();
973 if (!RegInfo->isReservedReg(MF, Reg) &&
974 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
975 Reg == ARM::LR)) {
976 Extras.push_back(Reg);
977 NumExtras--;
978 }
979 }
980 // For non-Thumb1 functions, also check for hi-reg CS registers
981 if (!AFI->isThumb1OnlyFunction()) {
982 while (NumExtras && !UnspilledCS2GPRs.empty()) {
983 unsigned Reg = UnspilledCS2GPRs.back();
984 UnspilledCS2GPRs.pop_back();
985 if (!RegInfo->isReservedReg(MF, Reg)) {
986 Extras.push_back(Reg);
987 NumExtras--;
988 }
989 }
990 }
991 if (Extras.size() && NumExtras == 0) {
992 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
993 MF.getRegInfo().setPhysRegUsed(Extras[i]);
994 AFI->setCSRegisterIsSpilled(Extras[i]);
995 }
996 } else if (!AFI->isThumb1OnlyFunction()) {
997 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
998 // closest to SP or frame pointer.
999 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
1000 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1001 RC->getAlignment(),
1002 false));
1003 }
1004 }
1005 }
1006
1007 if (ForceLRSpill) {
1008 MF.getRegInfo().setPhysRegUsed(ARM::LR);
1009 AFI->setCSRegisterIsSpilled(ARM::LR);
1010 AFI->setLRIsSpilledForFarJump(true);
1011 }
1012}