blob: f2eacdc6324f4571698330e31f0fbd58fdf21492 [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"
Anton Korobeynikov33464912010-11-15 00:06:54 +000015#include "ARMBaseInstrInfo.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000016#include "ARMBaseRegisterInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000017#include "ARMMachineFunctionInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMAddressingModes.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengab5c7032010-11-22 18:12:04 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000024#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000025
26using namespace llvm;
27
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000028/// hasFP - Return true if the specified function should have a dedicated frame
29/// pointer register. This is true if the function has variable sized allocas
30/// or if frame pointer elimination is disabled.
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.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000040 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
41 MFI->hasCalls()) ||
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000042 RegInfo->needsStackRealignment(MF) ||
43 MFI->hasVarSizedObjects() ||
44 MFI->isFrameAddressTaken());
45}
46
Bob Wilson42257852011-01-13 21:10:12 +000047/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
48/// not required, we reserve argument space for call sites in the function
49/// immediately on entry to the current function. This eliminates the need for
50/// add/sub sp brackets around call sites. Returns true if the call frame is
51/// included as part of the stack frame.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000052bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000053 const MachineFrameInfo *FFI = MF.getFrameInfo();
54 unsigned CFSize = FFI->getMaxCallFrameSize();
55 // It's not always a good idea to include the call frame as part of the
56 // stack frame. ARM (especially Thumb) has small immediate offset to
57 // address the stack frame. So a large call frame can cause poor codegen
58 // and may even makes it impossible to scavenge a register.
59 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
60 return false;
61
62 return !MF.getFrameInfo()->hasVarSizedObjects();
63}
64
Bob Wilson42257852011-01-13 21:10:12 +000065/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
66/// call frame pseudos can be simplified. Unlike most targets, having a FP
67/// is not sufficient here since we still may reference some objects via SP
68/// even when FP is available in Thumb2 mode.
69bool
70ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000071 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
72}
73
Anton Korobeynikov33464912010-11-15 00:06:54 +000074static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
75 for (unsigned i = 0; CSRegs[i]; ++i)
76 if (Reg == CSRegs[i])
77 return true;
78 return false;
79}
80
81static bool isCSRestore(MachineInstr *MI,
82 const ARMBaseInstrInfo &TII,
83 const unsigned *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000084 // Integer spill area is handled with "pop".
85 if (MI->getOpcode() == ARM::LDMIA_RET ||
86 MI->getOpcode() == ARM::t2LDMIA_RET ||
87 MI->getOpcode() == ARM::LDMIA_UPD ||
88 MI->getOpcode() == ARM::t2LDMIA_UPD ||
89 MI->getOpcode() == ARM::VLDMDIA_UPD) {
90 // The first two operands are predicates. The last two are
91 // imp-def and imp-use of SP. Check everything in between.
92 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
93 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
94 return false;
95 return true;
96 }
Owen Anderson793e7962011-07-26 20:54:26 +000097 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
98 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbach568f5282010-12-10 18:41:15 +000099 MI->getOpcode() == ARM::t2LDR_POST) &&
100 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
101 MI->getOperand(1).getReg() == ARM::SP)
102 return true;
Eric Christopher8b3ca622010-11-18 19:40:05 +0000103
104 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000105}
106
107static void
108emitSPUpdate(bool isARM,
109 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
110 DebugLoc dl, const ARMBaseInstrInfo &TII,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000111 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000112 if (isARM)
113 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000114 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000115 else
116 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000117 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000118}
119
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000120void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000121 MachineBasicBlock &MBB = MF.front();
122 MachineBasicBlock::iterator MBBI = MBB.begin();
123 MachineFrameInfo *MFI = MF.getFrameInfo();
124 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
125 const ARMBaseRegisterInfo *RegInfo =
126 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
127 const ARMBaseInstrInfo &TII =
128 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
129 assert(!AFI->isThumb1OnlyFunction() &&
130 "This emitPrologue does not support Thumb1!");
131 bool isARM = !AFI->isThumbFunction();
132 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
133 unsigned NumBytes = MFI->getStackSize();
134 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
135 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
136 unsigned FramePtr = RegInfo->getFrameRegister(MF);
137
138 // Determine the sizes of each callee-save spill areas and record which frame
139 // belongs to which callee-save spill areas.
140 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
141 int FramePtrSpillFI = 0;
142
143 // Allocate the vararg register save area. This is not counted in NumBytes.
144 if (VARegSaveSize)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000145 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize,
146 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000147
148 if (!AFI->hasStackFrame()) {
149 if (NumBytes != 0)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000150 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
151 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000152 return;
153 }
154
155 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
156 unsigned Reg = CSI[i].getReg();
157 int FI = CSI[i].getFrameIdx();
158 switch (Reg) {
159 case ARM::R4:
160 case ARM::R5:
161 case ARM::R6:
162 case ARM::R7:
163 case ARM::LR:
164 if (Reg == FramePtr)
165 FramePtrSpillFI = FI;
166 AFI->addGPRCalleeSavedArea1Frame(FI);
167 GPRCS1Size += 4;
168 break;
169 case ARM::R8:
170 case ARM::R9:
171 case ARM::R10:
172 case ARM::R11:
173 if (Reg == FramePtr)
174 FramePtrSpillFI = FI;
175 if (STI.isTargetDarwin()) {
176 AFI->addGPRCalleeSavedArea2Frame(FI);
177 GPRCS2Size += 4;
178 } else {
179 AFI->addGPRCalleeSavedArea1Frame(FI);
180 GPRCS1Size += 4;
181 }
182 break;
183 default:
184 AFI->addDPRCalleeSavedAreaFrame(FI);
185 DPRCSSize += 8;
186 }
187 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000188
Eric Christopher8b3ca622010-11-18 19:40:05 +0000189 // Move past area 1.
190 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000191
Anton Korobeynikov33464912010-11-15 00:06:54 +0000192 // Set FP to point to the stack slot that contains the previous FP.
193 // For Darwin, FP is R7, which has now been stored in spill area 1.
194 // Otherwise, if this is not Darwin, all the callee-saved registers go
195 // into spill area 1, including the FP in R11. In either case, it is
196 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000197 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000198 if (HasFP) {
199 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
200 MachineInstrBuilder MIB =
201 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000202 .addFrameIndex(FramePtrSpillFI).addImm(0)
203 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000204 AddDefaultCC(AddDefaultPred(MIB));
205 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000206
Eric Christopher8b3ca622010-11-18 19:40:05 +0000207 // Move past area 2.
208 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000209
Anton Korobeynikov33464912010-11-15 00:06:54 +0000210 // Determine starting offsets of spill areas.
211 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
212 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
213 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
214 if (HasFP)
215 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
216 NumBytes);
217 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
218 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
219 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
220
Eric Christopher8b3ca622010-11-18 19:40:05 +0000221 // Move past area 3.
Evan Chengacca09b2011-02-25 00:24:46 +0000222 if (DPRCSSize > 0) {
223 MBBI++;
224 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Cheng9831f2d2011-02-25 01:29:29 +0000225 // instructions in the prologue.
Evan Chengacca09b2011-02-25 00:24:46 +0000226 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
227 MBBI++;
228 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000229
Anton Korobeynikov33464912010-11-15 00:06:54 +0000230 NumBytes = DPRCSOffset;
231 if (NumBytes) {
232 // Adjust SP after all the callee-save spills.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000233 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
234 MachineInstr::FrameSetup);
Evan Chengab5c7032010-11-22 18:12:04 +0000235 if (HasFP && isARM)
236 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
237 // Note it's not safe to do this in Thumb2 mode because it would have
238 // taken two instructions:
239 // mov sp, r7
240 // sub sp, #24
241 // If an interrupt is taken between the two instructions, then sp is in
242 // an inconsistent state (pointing to the middle of callee-saved area).
243 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000244 AFI->setShouldRestoreSPFromFP(true);
245 }
246
Evan Chengab5c7032010-11-22 18:12:04 +0000247 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000248 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
249 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000250
251 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
252 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
253 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
254
255 // If we need dynamic stack realignment, do it here. Be paranoid and make
256 // sure if we also have VLAs, we have a base pointer for frame access.
257 if (RegInfo->needsStackRealignment(MF)) {
258 unsigned MaxAlign = MFI->getMaxAlignment();
259 assert (!AFI->isThumb1OnlyFunction());
260 if (!AFI->isThumbFunction()) {
261 // Emit bic sp, sp, MaxAlign
262 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
263 TII.get(ARM::BICri), ARM::SP)
264 .addReg(ARM::SP, RegState::Kill)
265 .addImm(MaxAlign-1)));
266 } else {
267 // We cannot use sp as source/dest register here, thus we're emitting the
268 // following sequence:
269 // mov r4, sp
270 // bic r4, r4, MaxAlign
271 // mov sp, r4
272 // FIXME: It will be better just to find spare register here.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000273 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000274 .addReg(ARM::SP, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000275 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
276 TII.get(ARM::t2BICri), ARM::R4)
277 .addReg(ARM::R4, RegState::Kill)
278 .addImm(MaxAlign-1)));
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000279 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000280 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000281 }
282
283 AFI->setShouldRestoreSPFromFP(true);
284 }
285
286 // If we need a base pointer, set it up here. It's whatever the value
287 // of the stack pointer is at this point. Any variable size objects
288 // will be allocated after this, so we can still use the base pointer
289 // to reference locals.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000290 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000291 if (RegInfo->hasBasePointer(MF)) {
292 if (isARM)
293 BuildMI(MBB, MBBI, dl,
294 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
295 .addReg(ARM::SP)
296 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
297 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000298 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000299 RegInfo->getBaseRegister())
300 .addReg(ARM::SP));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000301 }
302
303 // If the frame has variable sized objects then the epilogue must restore
Eric Christopher4dd312f2011-01-10 23:10:59 +0000304 // the sp from fp. We can assume there's an FP here since hasFP already
305 // checks for hasVarSizedObjects.
Evan Chengab5c7032010-11-22 18:12:04 +0000306 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000307 AFI->setShouldRestoreSPFromFP(true);
308}
309
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000310void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000311 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000312 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000313 assert(MBBI->getDesc().isReturn() &&
314 "Can only insert epilog into returning blocks");
315 unsigned RetOpcode = MBBI->getOpcode();
316 DebugLoc dl = MBBI->getDebugLoc();
317 MachineFrameInfo *MFI = MF.getFrameInfo();
318 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
319 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
320 const ARMBaseInstrInfo &TII =
321 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
322 assert(!AFI->isThumb1OnlyFunction() &&
323 "This emitEpilogue does not support Thumb1!");
324 bool isARM = !AFI->isThumbFunction();
325
326 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
327 int NumBytes = (int)MFI->getStackSize();
328 unsigned FramePtr = RegInfo->getFrameRegister(MF);
329
330 if (!AFI->hasStackFrame()) {
331 if (NumBytes != 0)
332 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
333 } else {
334 // Unwind MBBI to point to first LDR / VLDRD.
335 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
336 if (MBBI != MBB.begin()) {
337 do
338 --MBBI;
339 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
340 if (!isCSRestore(MBBI, TII, CSRegs))
341 ++MBBI;
342 }
343
344 // Move SP to start of FP callee save spill area.
345 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
346 AFI->getGPRCalleeSavedArea2Size() +
347 AFI->getDPRCalleeSavedAreaSize());
348
349 // Reset SP based on frame pointer only if the stack frame extends beyond
350 // frame pointer stack slot or target is ELF and the function has FP.
351 if (AFI->shouldRestoreSPFromFP()) {
352 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
353 if (NumBytes) {
354 if (isARM)
355 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
356 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000357 else {
358 // It's not possible to restore SP from FP in a single instruction.
359 // For Darwin, this looks like:
360 // mov sp, r7
361 // sub sp, #24
362 // This is bad, if an interrupt is taken after the mov, sp is in an
363 // inconsistent state.
364 // Use the first callee-saved register as a scratch register.
365 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
366 "No scratch register to restore SP from FP!");
367 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000368 ARMCC::AL, 0, TII);
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000369 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000370 ARM::SP)
371 .addReg(ARM::R4));
Evan Chengab5c7032010-11-22 18:12:04 +0000372 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000373 } else {
374 // Thumb2 or ARM.
375 if (isARM)
376 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
377 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
378 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000379 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000380 ARM::SP)
381 .addReg(FramePtr));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000382 }
383 } else if (NumBytes)
384 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
385
Eric Christopher8b3ca622010-11-18 19:40:05 +0000386 // Increment past our save areas.
Evan Chengacca09b2011-02-25 00:24:46 +0000387 if (AFI->getDPRCalleeSavedAreaSize()) {
388 MBBI++;
389 // Since vpop register list cannot have gaps, there may be multiple vpop
390 // instructions in the epilogue.
391 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
392 MBBI++;
393 }
Eric Christopher8b3ca622010-11-18 19:40:05 +0000394 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
395 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000396 }
397
398 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
399 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
400 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000401 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000402 MachineOperand &JumpTarget = MBBI->getOperand(0);
403
404 // Jump to label or value in register.
Evan Cheng3d2125c2010-11-30 23:55:39 +0000405 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) {
406 unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi)
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000407 ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd)
408 : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND);
Evan Cheng3d2125c2010-11-30 23:55:39 +0000409 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
410 if (JumpTarget.isGlobal())
411 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
412 JumpTarget.getTargetFlags());
413 else {
414 assert(JumpTarget.isSymbol());
415 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
416 JumpTarget.getTargetFlags());
417 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000418
419 // Add the default predicate in Thumb mode.
420 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000421 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000422 BuildMI(MBB, MBBI, dl,
423 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000424 addReg(JumpTarget.getReg(), RegState::Kill);
425 } else if (RetOpcode == ARM::TCRETURNriND) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000426 BuildMI(MBB, MBBI, dl,
427 TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000428 addReg(JumpTarget.getReg(), RegState::Kill);
429 }
430
431 MachineInstr *NewMI = prior(MBBI);
432 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
433 NewMI->addOperand(MBBI->getOperand(i));
434
435 // Delete the pseudo instruction TCRETURN.
436 MBB.erase(MBBI);
Cameron Zwarichcd4e0b52011-06-17 02:16:43 +0000437 MBBI = NewMI;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000438 }
439
440 if (VARegSaveSize)
441 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
442}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000443
Bob Wilson42257852011-01-13 21:10:12 +0000444/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
445/// debug info. It's the same as what we use for resolving the code-gen
446/// references for now. FIXME: This can go wrong when references are
447/// SP-relative and simple call frames aren't used.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000448int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000449ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson42257852011-01-13 21:10:12 +0000450 unsigned &FrameReg) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000451 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
452}
453
454int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000455ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengdb6cbe12011-04-22 01:42:52 +0000456 int FI, unsigned &FrameReg,
Bob Wilson42257852011-01-13 21:10:12 +0000457 int SPAdj) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000458 const MachineFrameInfo *MFI = MF.getFrameInfo();
459 const ARMBaseRegisterInfo *RegInfo =
460 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
461 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
462 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
463 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
464 bool isFixed = MFI->isFixedObjectIndex(FI);
465
466 FrameReg = ARM::SP;
467 Offset += SPAdj;
468 if (AFI->isGPRCalleeSavedArea1Frame(FI))
469 return Offset - AFI->getGPRCalleeSavedArea1Offset();
470 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
471 return Offset - AFI->getGPRCalleeSavedArea2Offset();
472 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
473 return Offset - AFI->getDPRCalleeSavedAreaOffset();
474
475 // When dynamically realigning the stack, use the frame pointer for
476 // parameters, and the stack/base pointer for locals.
477 if (RegInfo->needsStackRealignment(MF)) {
478 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
479 if (isFixed) {
480 FrameReg = RegInfo->getFrameRegister(MF);
481 Offset = FPOffset;
482 } else if (MFI->hasVarSizedObjects()) {
483 assert(RegInfo->hasBasePointer(MF) &&
484 "VLAs and dynamic stack alignment, but missing base pointer!");
485 FrameReg = RegInfo->getBaseRegister();
486 }
487 return Offset;
488 }
489
490 // If there is a frame pointer, use it when we can.
491 if (hasFP(MF) && AFI->hasStackFrame()) {
492 // Use frame pointer to reference fixed objects. Use it for locals if
493 // there are VLAs (and thus the SP isn't reliable as a base).
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000494 if (isFixed || (MFI->hasVarSizedObjects() &&
495 !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000496 FrameReg = RegInfo->getFrameRegister(MF);
497 return FPOffset;
498 } else if (MFI->hasVarSizedObjects()) {
499 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000500 if (AFI->isThumb2Function()) {
Evan Chengdb6cbe12011-04-22 01:42:52 +0000501 // Try to use the frame pointer if we can, else use the base pointer
502 // since it's available. This is handy for the emergency spill slot, in
503 // particular.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000504 if (FPOffset >= -255 && FPOffset < 0) {
505 FrameReg = RegInfo->getFrameRegister(MF);
506 return FPOffset;
507 }
Evan Chengdb6cbe12011-04-22 01:42:52 +0000508 }
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000509 } else if (AFI->isThumb2Function()) {
Andrew Trick51972da2011-08-25 17:40:54 +0000510 // Use add <rd>, sp, #<imm8>
Evan Chengdb6cbe12011-04-22 01:42:52 +0000511 // ldr <rd>, [sp, #<imm8>]
512 // if at all possible to save space.
513 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
514 return Offset;
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000515 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengdb6cbe12011-04-22 01:42:52 +0000516 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000517 if (FPOffset >= -255 && FPOffset < 0) {
518 FrameReg = RegInfo->getFrameRegister(MF);
519 return FPOffset;
520 }
521 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
522 // Otherwise, use SP or FP, whichever is closer to the stack slot.
523 FrameReg = RegInfo->getFrameRegister(MF);
524 return FPOffset;
525 }
526 }
527 // Use the base pointer if we have one.
528 if (RegInfo->hasBasePointer(MF))
529 FrameReg = RegInfo->getBaseRegister();
530 return Offset;
531}
532
Bob Wilson42257852011-01-13 21:10:12 +0000533int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
534 int FI) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000535 unsigned FrameReg;
536 return getFrameIndexReference(MF, FI, FrameReg);
537}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000538
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000539void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000540 MachineBasicBlock::iterator MI,
541 const std::vector<CalleeSavedInfo> &CSI,
542 unsigned StmOpc, unsigned StrOpc,
543 bool NoGap,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000544 bool(*Func)(unsigned, bool),
545 unsigned MIFlags) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000546 MachineFunction &MF = *MBB.getParent();
547 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
548
549 DebugLoc DL;
550 if (MI != MBB.end()) DL = MI->getDebugLoc();
551
Evan Cheng9801b5c2010-12-07 19:59:34 +0000552 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng06d65f52010-12-07 23:08:38 +0000553 unsigned i = CSI.size();
554 while (i != 0) {
555 unsigned LastReg = 0;
556 for (; i != 0; --i) {
557 unsigned Reg = CSI[i-1].getReg();
558 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000559
Evan Cheng06d65f52010-12-07 23:08:38 +0000560 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000561 // @llvm.returnaddress is called. If LR is returned for
562 // @llvm.returnaddress then it's already added to the function and
563 // entry block live-in sets.
Evan Cheng06d65f52010-12-07 23:08:38 +0000564 bool isKill = true;
565 if (Reg == ARM::LR) {
566 if (MF.getFrameInfo()->isReturnAddressTaken() &&
567 MF.getRegInfo().isLiveIn(Reg))
568 isKill = false;
569 }
570
571 if (isKill)
572 MBB.addLiveIn(Reg);
573
Eric Christopher1a48c032010-12-09 01:57:45 +0000574 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng275bf632010-12-08 06:29:02 +0000575 // for other instructions. e.g.
Eric Christopher1a48c032010-12-09 01:57:45 +0000576 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng275bf632010-12-08 06:29:02 +0000577 if (NoGap && LastReg && LastReg != Reg-1)
578 break;
Evan Cheng06d65f52010-12-07 23:08:38 +0000579 LastReg = Reg;
580 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000581 }
582
Jim Grosbachc6f92612010-12-09 18:31:13 +0000583 if (Regs.empty())
584 continue;
585 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000586 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000587 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000588 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng06d65f52010-12-07 23:08:38 +0000589 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
590 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbachc6f92612010-12-09 18:31:13 +0000591 } else if (Regs.size() == 1) {
592 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
593 ARM::SP)
594 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbach19dec202011-08-05 20:35:44 +0000595 .addReg(ARM::SP).setMIFlags(MIFlags)
596 .addImm(-4);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000597 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000598 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000599 Regs.clear();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000600 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000601}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000602
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000603void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000604 MachineBasicBlock::iterator MI,
605 const std::vector<CalleeSavedInfo> &CSI,
606 unsigned LdmOpc, unsigned LdrOpc,
607 bool isVarArg, bool NoGap,
608 bool(*Func)(unsigned, bool)) const {
Evan Cheng06d65f52010-12-07 23:08:38 +0000609 MachineFunction &MF = *MBB.getParent();
610 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
611 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
612 DebugLoc DL = MI->getDebugLoc();
Evan Cheng7cfa6562011-01-25 01:28:33 +0000613 unsigned RetOpcode = MI->getOpcode();
614 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
615 RetOpcode == ARM::TCRETURNdiND ||
616 RetOpcode == ARM::TCRETURNri ||
617 RetOpcode == ARM::TCRETURNriND);
Evan Cheng06d65f52010-12-07 23:08:38 +0000618
619 SmallVector<unsigned, 4> Regs;
620 unsigned i = CSI.size();
621 while (i != 0) {
622 unsigned LastReg = 0;
623 bool DeleteRet = false;
624 for (; i != 0; --i) {
625 unsigned Reg = CSI[i-1].getReg();
626 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
627
Evan Cheng7cfa6562011-01-25 01:28:33 +0000628 if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000629 Reg = ARM::PC;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000630 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng06d65f52010-12-07 23:08:38 +0000631 // Fold the return instruction into the LDM.
632 DeleteRet = true;
633 }
634
Evan Cheng275bf632010-12-08 06:29:02 +0000635 // If NoGap is true, pop consecutive registers and then leave the rest
636 // for other instructions. e.g.
637 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
638 if (NoGap && LastReg && LastReg != Reg-1)
639 break;
640
Evan Cheng06d65f52010-12-07 23:08:38 +0000641 LastReg = Reg;
642 Regs.push_back(Reg);
643 }
644
Jim Grosbachc6f92612010-12-09 18:31:13 +0000645 if (Regs.empty())
646 continue;
647 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000648 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000649 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000650 .addReg(ARM::SP));
651 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
652 MIB.addReg(Regs[i], getDefRegState(true));
Andrew Trickb9ca5122011-08-25 17:50:53 +0000653 if (DeleteRet) {
654 MIB->copyImplicitOps(&*MI);
Evan Cheng06d65f52010-12-07 23:08:38 +0000655 MI->eraseFromParent();
Andrew Trickb9ca5122011-08-25 17:50:53 +0000656 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000657 MI = MIB;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000658 } else if (Regs.size() == 1) {
659 // If we adjusted the reg to PC from LR above, switch it back here. We
660 // only do that for LDM.
661 if (Regs[0] == ARM::PC)
662 Regs[0] = ARM::LR;
663 MachineInstrBuilder MIB =
664 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
665 .addReg(ARM::SP, RegState::Define)
666 .addReg(ARM::SP);
667 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
668 // that refactoring is complete (eventually).
Owen Anderson793e7962011-07-26 20:54:26 +0000669 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbachc6f92612010-12-09 18:31:13 +0000670 MIB.addReg(0);
671 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
672 } else
673 MIB.addImm(4);
674 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000675 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000676 Regs.clear();
Evan Cheng9801b5c2010-12-07 19:59:34 +0000677 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000678}
679
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000680bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000681 MachineBasicBlock::iterator MI,
682 const std::vector<CalleeSavedInfo> &CSI,
683 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000684 if (CSI.empty())
685 return false;
686
687 MachineFunction &MF = *MBB.getParent();
688 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000689
690 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000691 unsigned PushOneOpc = AFI->isThumbFunction() ?
692 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000693 unsigned FltOpc = ARM::VSTMDDB_UPD;
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000694 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register,
695 MachineInstr::FrameSetup);
696 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register,
697 MachineInstr::FrameSetup);
698 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
699 MachineInstr::FrameSetup);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000700
701 return true;
702}
703
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000704bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000705 MachineBasicBlock::iterator MI,
706 const std::vector<CalleeSavedInfo> &CSI,
707 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000708 if (CSI.empty())
709 return false;
710
711 MachineFunction &MF = *MBB.getParent();
712 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
713 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000714
715 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000716 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000717 unsigned FltOpc = ARM::VLDMDIA_UPD;
Bob Wilson28f10152011-01-06 19:24:36 +0000718 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000719 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
720 &isARMArea2Register);
721 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
722 &isARMArea1Register);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000723
724 return true;
725}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000726
727// FIXME: Make generic?
728static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
729 const ARMBaseInstrInfo &TII) {
730 unsigned FnSize = 0;
731 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
732 MBBI != E; ++MBBI) {
733 const MachineBasicBlock &MBB = *MBBI;
734 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
735 I != E; ++I)
736 FnSize += TII.GetInstSizeInBytes(I);
737 }
738 return FnSize;
739}
740
741/// estimateStackSize - Estimate and return the size of the frame.
742/// FIXME: Make generic?
743static unsigned estimateStackSize(MachineFunction &MF) {
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000744 const MachineFrameInfo *MFI = MF.getFrameInfo();
745 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
746 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
747 unsigned MaxAlign = MFI->getMaxAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000748 int Offset = 0;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000749
750 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
751 // It really should be refactored to share code. Until then, changes
752 // should keep in mind that there's tight coupling between the two.
753
754 for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
755 int FixedOff = -MFI->getObjectOffset(i);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000756 if (FixedOff > Offset) Offset = FixedOff;
757 }
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000758 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
759 if (MFI->isDeadObjectIndex(i))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000760 continue;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000761 Offset += MFI->getObjectSize(i);
762 unsigned Align = MFI->getObjectAlignment(i);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000763 // Adjust to alignment boundary
764 Offset = (Offset+Align-1)/Align*Align;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000765
766 MaxAlign = std::max(Align, MaxAlign);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000767 }
Jim Grosbachbc20e4f2011-07-05 16:05:50 +0000768
769 if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF))
770 Offset += MFI->getMaxCallFrameSize();
771
772 // Round up the size to a multiple of the alignment. If the function has
773 // any calls or alloca's, align to the target's StackAlignment value to
774 // ensure that the callee's frame or the alloca data is suitably aligned;
775 // otherwise, for leaf functions, align to the TransientStackAlignment
776 // value.
777 unsigned StackAlign;
778 if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
779 (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0))
780 StackAlign = TFI->getStackAlignment();
781 else
782 StackAlign = TFI->getTransientStackAlignment();
783
784 // If the frame pointer is eliminated, all frame offsets will be relative to
785 // SP not FP. Align to MaxAlign so this works.
786 StackAlign = std::max(StackAlign, MaxAlign);
787 unsigned AlignMask = StackAlign - 1;
788 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
789
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000790 return (unsigned)Offset;
791}
792
793/// estimateRSStackSizeLimit - Look at each instruction that references stack
794/// frames and return the stack size limit beyond which some of these
795/// instructions will require a scratch register during their expansion later.
796// FIXME: Move to TII?
797static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000798 const TargetFrameLowering *TFI) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000799 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
800 unsigned Limit = (1 << 12) - 1;
801 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
802 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
803 I != E; ++I) {
804 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
805 if (!I->getOperand(i).isFI()) continue;
806
807 // When using ADDri to get the address of a stack object, 255 is the
808 // largest offset guaranteed to fit in the immediate offset.
809 if (I->getOpcode() == ARM::ADDri) {
810 Limit = std::min(Limit, (1U << 8) - 1);
811 break;
812 }
813
814 // Otherwise check the addressing mode.
815 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
816 case ARMII::AddrMode3:
817 case ARMII::AddrModeT2_i8:
818 Limit = std::min(Limit, (1U << 8) - 1);
819 break;
820 case ARMII::AddrMode5:
821 case ARMII::AddrModeT2_i8s4:
822 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
823 break;
824 case ARMII::AddrModeT2_i12:
825 // i12 supports only positive offset so these will be converted to
826 // i8 opcodes. See llvm::rewriteT2FrameIndex.
827 if (TFI->hasFP(MF) && AFI->hasStackFrame())
828 Limit = std::min(Limit, (1U << 8) - 1);
829 break;
830 case ARMII::AddrMode4:
831 case ARMII::AddrMode6:
832 // Addressing modes 4 & 6 (load/store) instructions can't encode an
833 // immediate offset for stack references.
834 return 0;
835 default:
836 break;
837 }
838 break; // At most one FI per instruction
839 }
840 }
841 }
842
843 return Limit;
844}
845
846void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000847ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000848 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000849 // This tells PEI to spill the FP as if it is any other callee-save register
850 // to take advantage the eliminateFrameIndex machinery. This also ensures it
851 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
852 // to combine multiple loads / stores.
853 bool CanEliminateFrame = true;
854 bool CS1Spilled = false;
855 bool LRSpilled = false;
856 unsigned NumGPRSpills = 0;
857 SmallVector<unsigned, 4> UnspilledCS1GPRs;
858 SmallVector<unsigned, 4> UnspilledCS2GPRs;
859 const ARMBaseRegisterInfo *RegInfo =
860 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
861 const ARMBaseInstrInfo &TII =
862 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
863 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
864 MachineFrameInfo *MFI = MF.getFrameInfo();
865 unsigned FramePtr = RegInfo->getFrameRegister(MF);
866
867 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
868 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Chengdf55fea2011-01-16 05:14:33 +0000869 // since it's not always possible to restore sp from fp in a single
870 // instruction.
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000871 // FIXME: It will be better just to find spare register here.
872 if (AFI->isThumb2Function() &&
873 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
874 MF.getRegInfo().setPhysRegUsed(ARM::R4);
875
Evan Chengdf55fea2011-01-16 05:14:33 +0000876 if (AFI->isThumb1OnlyFunction()) {
877 // Spill LR if Thumb1 function uses variable length argument lists.
878 if (AFI->getVarArgsRegSaveSize() > 0)
879 MF.getRegInfo().setPhysRegUsed(ARM::LR);
880
Jim Grosbach7980f612011-06-13 21:18:25 +0000881 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
882 // for sure what the stack size will be, but for this, an estimate is good
883 // enough. If there anything changes it, it'll be a spill, which implies
884 // we've used all the registers and so R4 is already used, so not marking
Chad Rosier6690bca2011-10-20 00:07:12 +0000885 // it here will be OK.
Evan Chengdf55fea2011-01-16 05:14:33 +0000886 // FIXME: It will be better just to find spare register here.
Jim Grosbach7980f612011-06-13 21:18:25 +0000887 unsigned StackSize = estimateStackSize(MF);
Chad Rosier6690bca2011-10-20 00:07:12 +0000888 if (MFI->hasVarSizedObjects() || StackSize > 508)
Evan Chengdf55fea2011-01-16 05:14:33 +0000889 MF.getRegInfo().setPhysRegUsed(ARM::R4);
890 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000891
892 // Spill the BasePtr if it's used.
893 if (RegInfo->hasBasePointer(MF))
894 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
895
896 // Don't spill FP if the frame can be eliminated. This is determined
897 // by scanning the callee-save registers to see if any is used.
898 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
899 for (unsigned i = 0; CSRegs[i]; ++i) {
900 unsigned Reg = CSRegs[i];
901 bool Spilled = false;
902 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000903 Spilled = true;
904 CanEliminateFrame = false;
905 } else {
906 // Check alias registers too.
907 for (const unsigned *Aliases =
908 RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) {
909 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
910 Spilled = true;
911 CanEliminateFrame = false;
912 }
913 }
914 }
915
916 if (!ARM::GPRRegisterClass->contains(Reg))
917 continue;
918
919 if (Spilled) {
920 NumGPRSpills++;
921
922 if (!STI.isTargetDarwin()) {
923 if (Reg == ARM::LR)
924 LRSpilled = true;
925 CS1Spilled = true;
926 continue;
927 }
928
929 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
930 switch (Reg) {
931 case ARM::LR:
932 LRSpilled = true;
933 // Fallthrough
934 case ARM::R4: case ARM::R5:
935 case ARM::R6: case ARM::R7:
936 CS1Spilled = true;
937 break;
938 default:
939 break;
940 }
941 } else {
942 if (!STI.isTargetDarwin()) {
943 UnspilledCS1GPRs.push_back(Reg);
944 continue;
945 }
946
947 switch (Reg) {
948 case ARM::R4: case ARM::R5:
949 case ARM::R6: case ARM::R7:
950 case ARM::LR:
951 UnspilledCS1GPRs.push_back(Reg);
952 break;
953 default:
954 UnspilledCS2GPRs.push_back(Reg);
955 break;
956 }
957 }
958 }
959
960 bool ForceLRSpill = false;
961 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
962 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
963 // Force LR to be spilled if the Thumb function size is > 2048. This enables
964 // use of BL to implement far jump. If it turns out that it's not needed
965 // then the branch fix up path will undo it.
966 if (FnSize >= (1 << 11)) {
967 CanEliminateFrame = false;
968 ForceLRSpill = true;
969 }
970 }
971
972 // If any of the stack slot references may be out of range of an immediate
973 // offset, make sure a register (or a spill slot) is available for the
974 // register scavenger. Note that if we're indexing off the frame pointer, the
975 // effective stack size is 4 bytes larger since the FP points to the stack
976 // slot of the previous FP. Also, if we have variable sized objects in the
977 // function, stack slot references will often be negative, and some of
978 // our instructions are positive-offset only, so conservatively consider
979 // that case to want a spill slot (or register) as well. Similarly, if
980 // the function adjusts the stack pointer during execution and the
981 // adjustments aren't already part of our stack size estimate, our offset
982 // calculations may be off, so be conservative.
983 // FIXME: We could add logic to be more precise about negative offsets
984 // and which instructions will need a scratch register for them. Is it
985 // worth the effort and added fragility?
986 bool BigStack =
987 (RS &&
988 (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
989 estimateRSStackSizeLimit(MF, this)))
990 || MFI->hasVarSizedObjects()
991 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
992
993 bool ExtraCSSpill = false;
994 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
995 AFI->setHasStackFrame(true);
996
997 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
998 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
999 if (!LRSpilled && CS1Spilled) {
1000 MF.getRegInfo().setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001001 NumGPRSpills++;
1002 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1003 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1004 ForceLRSpill = false;
1005 ExtraCSSpill = true;
1006 }
1007
1008 if (hasFP(MF)) {
1009 MF.getRegInfo().setPhysRegUsed(FramePtr);
1010 NumGPRSpills++;
1011 }
1012
1013 // If stack and double are 8-byte aligned and we are spilling an odd number
1014 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1015 // the integer and double callee save areas.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001016 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001017 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1018 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1019 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1020 unsigned Reg = UnspilledCS1GPRs[i];
1021 // Don't spill high register if the function is thumb1
1022 if (!AFI->isThumb1OnlyFunction() ||
1023 isARMLowRegister(Reg) || Reg == ARM::LR) {
1024 MF.getRegInfo().setPhysRegUsed(Reg);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001025 if (!RegInfo->isReservedReg(MF, Reg))
1026 ExtraCSSpill = true;
1027 break;
1028 }
1029 }
1030 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1031 unsigned Reg = UnspilledCS2GPRs.front();
1032 MF.getRegInfo().setPhysRegUsed(Reg);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001033 if (!RegInfo->isReservedReg(MF, Reg))
1034 ExtraCSSpill = true;
1035 }
1036 }
1037
1038 // Estimate if we might need to scavenge a register at some point in order
1039 // to materialize a stack offset. If so, either spill one additional
1040 // callee-saved register or reserve a special spill slot to facilitate
1041 // register scavenging. Thumb1 needs a spill slot for stack pointer
1042 // adjustments also, even when the frame itself is small.
1043 if (BigStack && !ExtraCSSpill) {
1044 // If any non-reserved CS register isn't spilled, just spill one or two
1045 // extra. That should take care of it!
1046 unsigned NumExtras = TargetAlign / 4;
1047 SmallVector<unsigned, 2> Extras;
1048 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1049 unsigned Reg = UnspilledCS1GPRs.back();
1050 UnspilledCS1GPRs.pop_back();
1051 if (!RegInfo->isReservedReg(MF, Reg) &&
1052 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1053 Reg == ARM::LR)) {
1054 Extras.push_back(Reg);
1055 NumExtras--;
1056 }
1057 }
1058 // For non-Thumb1 functions, also check for hi-reg CS registers
1059 if (!AFI->isThumb1OnlyFunction()) {
1060 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1061 unsigned Reg = UnspilledCS2GPRs.back();
1062 UnspilledCS2GPRs.pop_back();
1063 if (!RegInfo->isReservedReg(MF, Reg)) {
1064 Extras.push_back(Reg);
1065 NumExtras--;
1066 }
1067 }
1068 }
1069 if (Extras.size() && NumExtras == 0) {
1070 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1071 MF.getRegInfo().setPhysRegUsed(Extras[i]);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001072 }
1073 } else if (!AFI->isThumb1OnlyFunction()) {
1074 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1075 // closest to SP or frame pointer.
1076 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
1077 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1078 RC->getAlignment(),
1079 false));
1080 }
1081 }
1082 }
1083
1084 if (ForceLRSpill) {
1085 MF.getRegInfo().setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001086 AFI->setLRIsSpilledForFarJump(true);
1087 }
1088}