blob: e2e95d47b37b90b2d3cdd595f8759651d0a185db [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"
Evan Chengb72d2a92011-01-11 21:46:47 +000017#include "ARMBaseRegisterInfo.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000018#include "ARMMachineFunctionInfo.h"
19#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.
40 return ((DisableFramePointerElim(MF) && MFI->hasCalls()) ||
41 RegInfo->needsStackRealignment(MF) ||
42 MFI->hasVarSizedObjects() ||
43 MFI->isFrameAddressTaken());
44}
45
Bob Wilson42257852011-01-13 21:10:12 +000046/// 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
Bob Wilson42257852011-01-13 21:10:12 +000064/// 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.
68bool
69ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000070 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
71}
72
Anton Korobeynikov33464912010-11-15 00:06:54 +000073static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
74 for (unsigned i = 0; CSRegs[i]; ++i)
75 if (Reg == CSRegs[i])
76 return true;
77 return false;
78}
79
80static bool isCSRestore(MachineInstr *MI,
81 const ARMBaseInstrInfo &TII,
82 const unsigned *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000083 // Integer spill area is handled with "pop".
84 if (MI->getOpcode() == ARM::LDMIA_RET ||
85 MI->getOpcode() == ARM::t2LDMIA_RET ||
86 MI->getOpcode() == ARM::LDMIA_UPD ||
87 MI->getOpcode() == ARM::t2LDMIA_UPD ||
88 MI->getOpcode() == ARM::VLDMDIA_UPD) {
89 // The first two operands are predicates. The last two are
90 // imp-def and imp-use of SP. Check everything in between.
91 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
92 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
93 return false;
94 return true;
95 }
Jim Grosbach568f5282010-12-10 18:41:15 +000096 if ((MI->getOpcode() == ARM::LDR_POST ||
97 MI->getOpcode() == ARM::t2LDR_POST) &&
98 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
99 MI->getOperand(1).getReg() == ARM::SP)
100 return true;
Eric Christopher8b3ca622010-11-18 19:40:05 +0000101
102 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000103}
104
105static void
106emitSPUpdate(bool isARM,
107 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
108 DebugLoc dl, const ARMBaseInstrInfo &TII,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000109 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000110 if (isARM)
111 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000112 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000113 else
114 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000115 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000116}
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)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000143 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize,
144 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000145
146 if (!AFI->hasStackFrame()) {
147 if (NumBytes != 0)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000148 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
149 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000150 return;
151 }
152
153 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
154 unsigned Reg = CSI[i].getReg();
155 int FI = CSI[i].getFrameIdx();
156 switch (Reg) {
157 case ARM::R4:
158 case ARM::R5:
159 case ARM::R6:
160 case ARM::R7:
161 case ARM::LR:
162 if (Reg == FramePtr)
163 FramePtrSpillFI = FI;
164 AFI->addGPRCalleeSavedArea1Frame(FI);
165 GPRCS1Size += 4;
166 break;
167 case ARM::R8:
168 case ARM::R9:
169 case ARM::R10:
170 case ARM::R11:
171 if (Reg == FramePtr)
172 FramePtrSpillFI = FI;
173 if (STI.isTargetDarwin()) {
174 AFI->addGPRCalleeSavedArea2Frame(FI);
175 GPRCS2Size += 4;
176 } else {
177 AFI->addGPRCalleeSavedArea1Frame(FI);
178 GPRCS1Size += 4;
179 }
180 break;
181 default:
182 AFI->addDPRCalleeSavedAreaFrame(FI);
183 DPRCSSize += 8;
184 }
185 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000186
Eric Christopher8b3ca622010-11-18 19:40:05 +0000187 // Move past area 1.
188 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000189
Anton Korobeynikov33464912010-11-15 00:06:54 +0000190 // Set FP to point to the stack slot that contains the previous FP.
191 // For Darwin, FP is R7, which has now been stored in spill area 1.
192 // Otherwise, if this is not Darwin, all the callee-saved registers go
193 // into spill area 1, including the FP in R11. In either case, it is
194 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000195 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000196 if (HasFP) {
197 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
198 MachineInstrBuilder MIB =
199 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000200 .addFrameIndex(FramePtrSpillFI).addImm(0)
201 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000202 AddDefaultCC(AddDefaultPred(MIB));
203 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000204
Eric Christopher8b3ca622010-11-18 19:40:05 +0000205 // Move past area 2.
206 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000207
Anton Korobeynikov33464912010-11-15 00:06:54 +0000208 // Determine starting offsets of spill areas.
209 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
210 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
211 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
212 if (HasFP)
213 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
214 NumBytes);
215 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
216 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
217 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
218
Eric Christopher8b3ca622010-11-18 19:40:05 +0000219 // Move past area 3.
Evan Chengacca09b2011-02-25 00:24:46 +0000220 if (DPRCSSize > 0) {
221 MBBI++;
222 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Cheng9831f2d2011-02-25 01:29:29 +0000223 // instructions in the prologue.
Evan Chengacca09b2011-02-25 00:24:46 +0000224 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
225 MBBI++;
226 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000227
Anton Korobeynikov33464912010-11-15 00:06:54 +0000228 NumBytes = DPRCSOffset;
229 if (NumBytes) {
230 // Adjust SP after all the callee-save spills.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000231 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
232 MachineInstr::FrameSetup);
Evan Chengab5c7032010-11-22 18:12:04 +0000233 if (HasFP && isARM)
234 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
235 // Note it's not safe to do this in Thumb2 mode because it would have
236 // taken two instructions:
237 // mov sp, r7
238 // sub sp, #24
239 // If an interrupt is taken between the two instructions, then sp is in
240 // an inconsistent state (pointing to the middle of callee-saved area).
241 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000242 AFI->setShouldRestoreSPFromFP(true);
243 }
244
Evan Chengab5c7032010-11-22 18:12:04 +0000245 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000246 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
247 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000248
249 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
250 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
251 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
252
253 // If we need dynamic stack realignment, do it here. Be paranoid and make
254 // sure if we also have VLAs, we have a base pointer for frame access.
255 if (RegInfo->needsStackRealignment(MF)) {
256 unsigned MaxAlign = MFI->getMaxAlignment();
257 assert (!AFI->isThumb1OnlyFunction());
258 if (!AFI->isThumbFunction()) {
259 // Emit bic sp, sp, MaxAlign
260 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
261 TII.get(ARM::BICri), ARM::SP)
262 .addReg(ARM::SP, RegState::Kill)
263 .addImm(MaxAlign-1)));
264 } else {
265 // We cannot use sp as source/dest register here, thus we're emitting the
266 // following sequence:
267 // mov r4, sp
268 // bic r4, r4, MaxAlign
269 // mov sp, r4
270 // FIXME: It will be better just to find spare register here.
271 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
272 .addReg(ARM::SP, RegState::Kill);
273 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
274 TII.get(ARM::t2BICri), ARM::R4)
275 .addReg(ARM::R4, RegState::Kill)
276 .addImm(MaxAlign-1)));
277 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
278 .addReg(ARM::R4, RegState::Kill);
279 }
280
281 AFI->setShouldRestoreSPFromFP(true);
282 }
283
284 // If we need a base pointer, set it up here. It's whatever the value
285 // of the stack pointer is at this point. Any variable size objects
286 // will be allocated after this, so we can still use the base pointer
287 // to reference locals.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000288 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000289 if (RegInfo->hasBasePointer(MF)) {
290 if (isARM)
291 BuildMI(MBB, MBBI, dl,
292 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
293 .addReg(ARM::SP)
294 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
295 else
296 BuildMI(MBB, MBBI, dl,
297 TII.get(ARM::tMOVgpr2gpr), RegInfo->getBaseRegister())
298 .addReg(ARM::SP);
299 }
300
301 // If the frame has variable sized objects then the epilogue must restore
Eric Christopher4dd312f2011-01-10 23:10:59 +0000302 // the sp from fp. We can assume there's an FP here since hasFP already
303 // checks for hasVarSizedObjects.
Evan Chengab5c7032010-11-22 18:12:04 +0000304 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000305 AFI->setShouldRestoreSPFromFP(true);
306}
307
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000308void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000309 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000310 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000311 assert(MBBI->getDesc().isReturn() &&
312 "Can only insert epilog into returning blocks");
313 unsigned RetOpcode = MBBI->getOpcode();
314 DebugLoc dl = MBBI->getDebugLoc();
315 MachineFrameInfo *MFI = MF.getFrameInfo();
316 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
317 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
318 const ARMBaseInstrInfo &TII =
319 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
320 assert(!AFI->isThumb1OnlyFunction() &&
321 "This emitEpilogue does not support Thumb1!");
322 bool isARM = !AFI->isThumbFunction();
323
324 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
325 int NumBytes = (int)MFI->getStackSize();
326 unsigned FramePtr = RegInfo->getFrameRegister(MF);
327
328 if (!AFI->hasStackFrame()) {
329 if (NumBytes != 0)
330 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
331 } else {
332 // Unwind MBBI to point to first LDR / VLDRD.
333 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
334 if (MBBI != MBB.begin()) {
335 do
336 --MBBI;
337 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
338 if (!isCSRestore(MBBI, TII, CSRegs))
339 ++MBBI;
340 }
341
342 // Move SP to start of FP callee save spill area.
343 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
344 AFI->getGPRCalleeSavedArea2Size() +
345 AFI->getDPRCalleeSavedAreaSize());
346
347 // Reset SP based on frame pointer only if the stack frame extends beyond
348 // frame pointer stack slot or target is ELF and the function has FP.
349 if (AFI->shouldRestoreSPFromFP()) {
350 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
351 if (NumBytes) {
352 if (isARM)
353 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
354 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000355 else {
356 // It's not possible to restore SP from FP in a single instruction.
357 // For Darwin, this looks like:
358 // mov sp, r7
359 // sub sp, #24
360 // This is bad, if an interrupt is taken after the mov, sp is in an
361 // inconsistent state.
362 // Use the first callee-saved register as a scratch register.
363 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
364 "No scratch register to restore SP from FP!");
365 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000366 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000367 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
368 .addReg(ARM::R4);
369 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000370 } else {
371 // Thumb2 or ARM.
372 if (isARM)
373 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
374 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
375 else
376 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
377 .addReg(FramePtr);
378 }
379 } else if (NumBytes)
380 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
381
Eric Christopher8b3ca622010-11-18 19:40:05 +0000382 // Increment past our save areas.
Evan Chengacca09b2011-02-25 00:24:46 +0000383 if (AFI->getDPRCalleeSavedAreaSize()) {
384 MBBI++;
385 // Since vpop register list cannot have gaps, there may be multiple vpop
386 // instructions in the epilogue.
387 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
388 MBBI++;
389 }
Eric Christopher8b3ca622010-11-18 19:40:05 +0000390 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
391 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000392 }
393
394 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
395 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
396 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000397 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000398 MachineOperand &JumpTarget = MBBI->getOperand(0);
399
400 // Jump to label or value in register.
Evan Cheng3d2125c2010-11-30 23:55:39 +0000401 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) {
402 unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi)
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000403 ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd)
404 : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND);
Evan Cheng3d2125c2010-11-30 23:55:39 +0000405 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
406 if (JumpTarget.isGlobal())
407 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
408 JumpTarget.getTargetFlags());
409 else {
410 assert(JumpTarget.isSymbol());
411 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
412 JumpTarget.getTargetFlags());
413 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000414 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000415 BuildMI(MBB, MBBI, dl,
416 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000417 addReg(JumpTarget.getReg(), RegState::Kill);
418 } else if (RetOpcode == ARM::TCRETURNriND) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000419 BuildMI(MBB, MBBI, dl,
420 TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000421 addReg(JumpTarget.getReg(), RegState::Kill);
422 }
423
424 MachineInstr *NewMI = prior(MBBI);
425 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
426 NewMI->addOperand(MBBI->getOperand(i));
427
428 // Delete the pseudo instruction TCRETURN.
429 MBB.erase(MBBI);
430 }
431
432 if (VARegSaveSize)
433 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
434}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000435
Bob Wilson42257852011-01-13 21:10:12 +0000436/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
437/// debug info. It's the same as what we use for resolving the code-gen
438/// references for now. FIXME: This can go wrong when references are
439/// SP-relative and simple call frames aren't used.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000440int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000441ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson42257852011-01-13 21:10:12 +0000442 unsigned &FrameReg) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000443 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
444}
445
446int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000447ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengdb6cbe12011-04-22 01:42:52 +0000448 int FI, unsigned &FrameReg,
Bob Wilson42257852011-01-13 21:10:12 +0000449 int SPAdj) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000450 const MachineFrameInfo *MFI = MF.getFrameInfo();
451 const ARMBaseRegisterInfo *RegInfo =
452 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
453 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
454 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
455 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
456 bool isFixed = MFI->isFixedObjectIndex(FI);
457
458 FrameReg = ARM::SP;
459 Offset += SPAdj;
460 if (AFI->isGPRCalleeSavedArea1Frame(FI))
461 return Offset - AFI->getGPRCalleeSavedArea1Offset();
462 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
463 return Offset - AFI->getGPRCalleeSavedArea2Offset();
464 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
465 return Offset - AFI->getDPRCalleeSavedAreaOffset();
466
467 // When dynamically realigning the stack, use the frame pointer for
468 // parameters, and the stack/base pointer for locals.
469 if (RegInfo->needsStackRealignment(MF)) {
470 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
471 if (isFixed) {
472 FrameReg = RegInfo->getFrameRegister(MF);
473 Offset = FPOffset;
474 } else if (MFI->hasVarSizedObjects()) {
475 assert(RegInfo->hasBasePointer(MF) &&
476 "VLAs and dynamic stack alignment, but missing base pointer!");
477 FrameReg = RegInfo->getBaseRegister();
478 }
479 return Offset;
480 }
481
482 // If there is a frame pointer, use it when we can.
483 if (hasFP(MF) && AFI->hasStackFrame()) {
484 // Use frame pointer to reference fixed objects. Use it for locals if
485 // there are VLAs (and thus the SP isn't reliable as a base).
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000486 if (isFixed || (MFI->hasVarSizedObjects() &&
487 !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000488 FrameReg = RegInfo->getFrameRegister(MF);
489 return FPOffset;
490 } else if (MFI->hasVarSizedObjects()) {
491 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000492 if (AFI->isThumb2Function()) {
Evan Chengdb6cbe12011-04-22 01:42:52 +0000493 // Try to use the frame pointer if we can, else use the base pointer
494 // since it's available. This is handy for the emergency spill slot, in
495 // particular.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000496 if (FPOffset >= -255 && FPOffset < 0) {
497 FrameReg = RegInfo->getFrameRegister(MF);
498 return FPOffset;
499 }
Evan Chengdb6cbe12011-04-22 01:42:52 +0000500 }
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000501 } else if (AFI->isThumb2Function()) {
Evan Chengdb6cbe12011-04-22 01:42:52 +0000502 // Use add <rd>, sp, #<imm8>
503 // ldr <rd>, [sp, #<imm8>]
504 // if at all possible to save space.
505 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
506 return Offset;
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000507 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengdb6cbe12011-04-22 01:42:52 +0000508 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000509 if (FPOffset >= -255 && FPOffset < 0) {
510 FrameReg = RegInfo->getFrameRegister(MF);
511 return FPOffset;
512 }
513 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
514 // Otherwise, use SP or FP, whichever is closer to the stack slot.
515 FrameReg = RegInfo->getFrameRegister(MF);
516 return FPOffset;
517 }
518 }
519 // Use the base pointer if we have one.
520 if (RegInfo->hasBasePointer(MF))
521 FrameReg = RegInfo->getBaseRegister();
522 return Offset;
523}
524
Bob Wilson42257852011-01-13 21:10:12 +0000525int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
526 int FI) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000527 unsigned FrameReg;
528 return getFrameIndexReference(MF, FI, FrameReg);
529}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000530
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000531void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000532 MachineBasicBlock::iterator MI,
533 const std::vector<CalleeSavedInfo> &CSI,
534 unsigned StmOpc, unsigned StrOpc,
535 bool NoGap,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000536 bool(*Func)(unsigned, bool),
537 unsigned MIFlags) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000538 MachineFunction &MF = *MBB.getParent();
539 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
540
541 DebugLoc DL;
542 if (MI != MBB.end()) DL = MI->getDebugLoc();
543
Evan Cheng9801b5c2010-12-07 19:59:34 +0000544 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng06d65f52010-12-07 23:08:38 +0000545 unsigned i = CSI.size();
546 while (i != 0) {
547 unsigned LastReg = 0;
548 for (; i != 0; --i) {
549 unsigned Reg = CSI[i-1].getReg();
550 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000551
Evan Cheng06d65f52010-12-07 23:08:38 +0000552 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000553 // @llvm.returnaddress is called. If LR is returned for
554 // @llvm.returnaddress then it's already added to the function and
555 // entry block live-in sets.
Evan Cheng06d65f52010-12-07 23:08:38 +0000556 bool isKill = true;
557 if (Reg == ARM::LR) {
558 if (MF.getFrameInfo()->isReturnAddressTaken() &&
559 MF.getRegInfo().isLiveIn(Reg))
560 isKill = false;
561 }
562
563 if (isKill)
564 MBB.addLiveIn(Reg);
565
Eric Christopher1a48c032010-12-09 01:57:45 +0000566 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng275bf632010-12-08 06:29:02 +0000567 // for other instructions. e.g.
Eric Christopher1a48c032010-12-09 01:57:45 +0000568 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng275bf632010-12-08 06:29:02 +0000569 if (NoGap && LastReg && LastReg != Reg-1)
570 break;
Evan Cheng06d65f52010-12-07 23:08:38 +0000571 LastReg = Reg;
572 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000573 }
574
Jim Grosbachc6f92612010-12-09 18:31:13 +0000575 if (Regs.empty())
576 continue;
577 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000578 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000579 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000580 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng06d65f52010-12-07 23:08:38 +0000581 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
582 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbachc6f92612010-12-09 18:31:13 +0000583 } else if (Regs.size() == 1) {
584 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
585 ARM::SP)
586 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000587 .addReg(ARM::SP).setMIFlags(MIFlags);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000588 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
589 // that refactoring is complete (eventually).
590 if (StrOpc == ARM::STR_PRE) {
591 MIB.addReg(0);
592 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::sub, 4, ARM_AM::no_shift));
593 } else
594 MIB.addImm(-4);
595 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000596 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000597 Regs.clear();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000598 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000599}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000600
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000601void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000602 MachineBasicBlock::iterator MI,
603 const std::vector<CalleeSavedInfo> &CSI,
604 unsigned LdmOpc, unsigned LdrOpc,
605 bool isVarArg, bool NoGap,
606 bool(*Func)(unsigned, bool)) const {
Evan Cheng06d65f52010-12-07 23:08:38 +0000607 MachineFunction &MF = *MBB.getParent();
608 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
609 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
610 DebugLoc DL = MI->getDebugLoc();
Evan Cheng7cfa6562011-01-25 01:28:33 +0000611 unsigned RetOpcode = MI->getOpcode();
612 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
613 RetOpcode == ARM::TCRETURNdiND ||
614 RetOpcode == ARM::TCRETURNri ||
615 RetOpcode == ARM::TCRETURNriND);
Evan Cheng06d65f52010-12-07 23:08:38 +0000616
617 SmallVector<unsigned, 4> Regs;
618 unsigned i = CSI.size();
619 while (i != 0) {
620 unsigned LastReg = 0;
621 bool DeleteRet = false;
622 for (; i != 0; --i) {
623 unsigned Reg = CSI[i-1].getReg();
624 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
625
Evan Cheng7cfa6562011-01-25 01:28:33 +0000626 if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000627 Reg = ARM::PC;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000628 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng06d65f52010-12-07 23:08:38 +0000629 // Fold the return instruction into the LDM.
630 DeleteRet = true;
631 }
632
Evan Cheng275bf632010-12-08 06:29:02 +0000633 // If NoGap is true, pop consecutive registers and then leave the rest
634 // for other instructions. e.g.
635 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
636 if (NoGap && LastReg && LastReg != Reg-1)
637 break;
638
Evan Cheng06d65f52010-12-07 23:08:38 +0000639 LastReg = Reg;
640 Regs.push_back(Reg);
641 }
642
Jim Grosbachc6f92612010-12-09 18:31:13 +0000643 if (Regs.empty())
644 continue;
645 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000646 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000647 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000648 .addReg(ARM::SP));
649 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
650 MIB.addReg(Regs[i], getDefRegState(true));
651 if (DeleteRet)
652 MI->eraseFromParent();
653 MI = MIB;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000654 } else if (Regs.size() == 1) {
655 // If we adjusted the reg to PC from LR above, switch it back here. We
656 // only do that for LDM.
657 if (Regs[0] == ARM::PC)
658 Regs[0] = ARM::LR;
659 MachineInstrBuilder MIB =
660 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
661 .addReg(ARM::SP, RegState::Define)
662 .addReg(ARM::SP);
663 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
664 // that refactoring is complete (eventually).
665 if (LdrOpc == ARM::LDR_POST) {
666 MIB.addReg(0);
667 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
668 } else
669 MIB.addImm(4);
670 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000671 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000672 Regs.clear();
Evan Cheng9801b5c2010-12-07 19:59:34 +0000673 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000674}
675
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000676bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000677 MachineBasicBlock::iterator MI,
678 const std::vector<CalleeSavedInfo> &CSI,
679 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000680 if (CSI.empty())
681 return false;
682
683 MachineFunction &MF = *MBB.getParent();
684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000685
686 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000687 unsigned PushOneOpc = AFI->isThumbFunction() ? ARM::t2STR_PRE : ARM::STR_PRE;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000688 unsigned FltOpc = ARM::VSTMDDB_UPD;
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000689 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register,
690 MachineInstr::FrameSetup);
691 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register,
692 MachineInstr::FrameSetup);
693 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
694 MachineInstr::FrameSetup);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000695
696 return true;
697}
698
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000699bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000700 MachineBasicBlock::iterator MI,
701 const std::vector<CalleeSavedInfo> &CSI,
702 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000703 if (CSI.empty())
704 return false;
705
706 MachineFunction &MF = *MBB.getParent();
707 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
708 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000709
710 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000711 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST : ARM::LDR_POST;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000712 unsigned FltOpc = ARM::VLDMDIA_UPD;
Bob Wilson28f10152011-01-06 19:24:36 +0000713 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000714 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
715 &isARMArea2Register);
716 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
717 &isARMArea1Register);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000718
719 return true;
720}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000721
722// FIXME: Make generic?
723static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
724 const ARMBaseInstrInfo &TII) {
725 unsigned FnSize = 0;
726 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
727 MBBI != E; ++MBBI) {
728 const MachineBasicBlock &MBB = *MBBI;
729 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
730 I != E; ++I)
731 FnSize += TII.GetInstSizeInBytes(I);
732 }
733 return FnSize;
734}
735
736/// estimateStackSize - Estimate and return the size of the frame.
737/// FIXME: Make generic?
738static unsigned estimateStackSize(MachineFunction &MF) {
739 const MachineFrameInfo *FFI = MF.getFrameInfo();
740 int Offset = 0;
741 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
742 int FixedOff = -FFI->getObjectOffset(i);
743 if (FixedOff > Offset) Offset = FixedOff;
744 }
745 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
746 if (FFI->isDeadObjectIndex(i))
747 continue;
748 Offset += FFI->getObjectSize(i);
749 unsigned Align = FFI->getObjectAlignment(i);
750 // Adjust to alignment boundary
751 Offset = (Offset+Align-1)/Align*Align;
752 }
753 return (unsigned)Offset;
754}
755
756/// estimateRSStackSizeLimit - Look at each instruction that references stack
757/// frames and return the stack size limit beyond which some of these
758/// instructions will require a scratch register during their expansion later.
759// FIXME: Move to TII?
760static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000761 const TargetFrameLowering *TFI) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000762 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
763 unsigned Limit = (1 << 12) - 1;
764 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
765 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
766 I != E; ++I) {
767 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
768 if (!I->getOperand(i).isFI()) continue;
769
770 // When using ADDri to get the address of a stack object, 255 is the
771 // largest offset guaranteed to fit in the immediate offset.
772 if (I->getOpcode() == ARM::ADDri) {
773 Limit = std::min(Limit, (1U << 8) - 1);
774 break;
775 }
776
777 // Otherwise check the addressing mode.
778 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
779 case ARMII::AddrMode3:
780 case ARMII::AddrModeT2_i8:
781 Limit = std::min(Limit, (1U << 8) - 1);
782 break;
783 case ARMII::AddrMode5:
784 case ARMII::AddrModeT2_i8s4:
785 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
786 break;
787 case ARMII::AddrModeT2_i12:
788 // i12 supports only positive offset so these will be converted to
789 // i8 opcodes. See llvm::rewriteT2FrameIndex.
790 if (TFI->hasFP(MF) && AFI->hasStackFrame())
791 Limit = std::min(Limit, (1U << 8) - 1);
792 break;
793 case ARMII::AddrMode4:
794 case ARMII::AddrMode6:
795 // Addressing modes 4 & 6 (load/store) instructions can't encode an
796 // immediate offset for stack references.
797 return 0;
798 default:
799 break;
800 }
801 break; // At most one FI per instruction
802 }
803 }
804 }
805
806 return Limit;
807}
808
809void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000810ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000811 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000812 // This tells PEI to spill the FP as if it is any other callee-save register
813 // to take advantage the eliminateFrameIndex machinery. This also ensures it
814 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
815 // to combine multiple loads / stores.
816 bool CanEliminateFrame = true;
817 bool CS1Spilled = false;
818 bool LRSpilled = false;
819 unsigned NumGPRSpills = 0;
820 SmallVector<unsigned, 4> UnspilledCS1GPRs;
821 SmallVector<unsigned, 4> UnspilledCS2GPRs;
822 const ARMBaseRegisterInfo *RegInfo =
823 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
824 const ARMBaseInstrInfo &TII =
825 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
826 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
827 MachineFrameInfo *MFI = MF.getFrameInfo();
828 unsigned FramePtr = RegInfo->getFrameRegister(MF);
829
830 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
831 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Chengdf55fea2011-01-16 05:14:33 +0000832 // since it's not always possible to restore sp from fp in a single
833 // instruction.
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000834 // FIXME: It will be better just to find spare register here.
835 if (AFI->isThumb2Function() &&
836 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
837 MF.getRegInfo().setPhysRegUsed(ARM::R4);
838
Evan Chengdf55fea2011-01-16 05:14:33 +0000839 if (AFI->isThumb1OnlyFunction()) {
840 // Spill LR if Thumb1 function uses variable length argument lists.
841 if (AFI->getVarArgsRegSaveSize() > 0)
842 MF.getRegInfo().setPhysRegUsed(ARM::LR);
843
844 // Spill R4 if Thumb1 epilogue has to restore SP from FP since
845 // FIXME: It will be better just to find spare register here.
846 if (MFI->hasVarSizedObjects())
847 MF.getRegInfo().setPhysRegUsed(ARM::R4);
848 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000849
850 // Spill the BasePtr if it's used.
851 if (RegInfo->hasBasePointer(MF))
852 MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister());
853
854 // Don't spill FP if the frame can be eliminated. This is determined
855 // by scanning the callee-save registers to see if any is used.
856 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
857 for (unsigned i = 0; CSRegs[i]; ++i) {
858 unsigned Reg = CSRegs[i];
859 bool Spilled = false;
860 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000861 Spilled = true;
862 CanEliminateFrame = false;
863 } else {
864 // Check alias registers too.
865 for (const unsigned *Aliases =
866 RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) {
867 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
868 Spilled = true;
869 CanEliminateFrame = false;
870 }
871 }
872 }
873
874 if (!ARM::GPRRegisterClass->contains(Reg))
875 continue;
876
877 if (Spilled) {
878 NumGPRSpills++;
879
880 if (!STI.isTargetDarwin()) {
881 if (Reg == ARM::LR)
882 LRSpilled = true;
883 CS1Spilled = true;
884 continue;
885 }
886
887 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
888 switch (Reg) {
889 case ARM::LR:
890 LRSpilled = true;
891 // Fallthrough
892 case ARM::R4: case ARM::R5:
893 case ARM::R6: case ARM::R7:
894 CS1Spilled = true;
895 break;
896 default:
897 break;
898 }
899 } else {
900 if (!STI.isTargetDarwin()) {
901 UnspilledCS1GPRs.push_back(Reg);
902 continue;
903 }
904
905 switch (Reg) {
906 case ARM::R4: case ARM::R5:
907 case ARM::R6: case ARM::R7:
908 case ARM::LR:
909 UnspilledCS1GPRs.push_back(Reg);
910 break;
911 default:
912 UnspilledCS2GPRs.push_back(Reg);
913 break;
914 }
915 }
916 }
917
918 bool ForceLRSpill = false;
919 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
920 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
921 // Force LR to be spilled if the Thumb function size is > 2048. This enables
922 // use of BL to implement far jump. If it turns out that it's not needed
923 // then the branch fix up path will undo it.
924 if (FnSize >= (1 << 11)) {
925 CanEliminateFrame = false;
926 ForceLRSpill = true;
927 }
928 }
929
930 // If any of the stack slot references may be out of range of an immediate
931 // offset, make sure a register (or a spill slot) is available for the
932 // register scavenger. Note that if we're indexing off the frame pointer, the
933 // effective stack size is 4 bytes larger since the FP points to the stack
934 // slot of the previous FP. Also, if we have variable sized objects in the
935 // function, stack slot references will often be negative, and some of
936 // our instructions are positive-offset only, so conservatively consider
937 // that case to want a spill slot (or register) as well. Similarly, if
938 // the function adjusts the stack pointer during execution and the
939 // adjustments aren't already part of our stack size estimate, our offset
940 // calculations may be off, so be conservative.
941 // FIXME: We could add logic to be more precise about negative offsets
942 // and which instructions will need a scratch register for them. Is it
943 // worth the effort and added fragility?
944 bool BigStack =
945 (RS &&
946 (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
947 estimateRSStackSizeLimit(MF, this)))
948 || MFI->hasVarSizedObjects()
949 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
950
951 bool ExtraCSSpill = false;
952 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
953 AFI->setHasStackFrame(true);
954
955 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
956 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
957 if (!LRSpilled && CS1Spilled) {
958 MF.getRegInfo().setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000959 NumGPRSpills++;
960 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
961 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
962 ForceLRSpill = false;
963 ExtraCSSpill = true;
964 }
965
966 if (hasFP(MF)) {
967 MF.getRegInfo().setPhysRegUsed(FramePtr);
968 NumGPRSpills++;
969 }
970
971 // If stack and double are 8-byte aligned and we are spilling an odd number
972 // of GPRs, spill one extra callee save GPR so we won't have to pad between
973 // the integer and double callee save areas.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000974 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000975 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
976 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
977 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
978 unsigned Reg = UnspilledCS1GPRs[i];
979 // Don't spill high register if the function is thumb1
980 if (!AFI->isThumb1OnlyFunction() ||
981 isARMLowRegister(Reg) || Reg == ARM::LR) {
982 MF.getRegInfo().setPhysRegUsed(Reg);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000983 if (!RegInfo->isReservedReg(MF, Reg))
984 ExtraCSSpill = true;
985 break;
986 }
987 }
988 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
989 unsigned Reg = UnspilledCS2GPRs.front();
990 MF.getRegInfo().setPhysRegUsed(Reg);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +0000991 if (!RegInfo->isReservedReg(MF, Reg))
992 ExtraCSSpill = true;
993 }
994 }
995
996 // Estimate if we might need to scavenge a register at some point in order
997 // to materialize a stack offset. If so, either spill one additional
998 // callee-saved register or reserve a special spill slot to facilitate
999 // register scavenging. Thumb1 needs a spill slot for stack pointer
1000 // adjustments also, even when the frame itself is small.
1001 if (BigStack && !ExtraCSSpill) {
1002 // If any non-reserved CS register isn't spilled, just spill one or two
1003 // extra. That should take care of it!
1004 unsigned NumExtras = TargetAlign / 4;
1005 SmallVector<unsigned, 2> Extras;
1006 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1007 unsigned Reg = UnspilledCS1GPRs.back();
1008 UnspilledCS1GPRs.pop_back();
1009 if (!RegInfo->isReservedReg(MF, Reg) &&
1010 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1011 Reg == ARM::LR)) {
1012 Extras.push_back(Reg);
1013 NumExtras--;
1014 }
1015 }
1016 // For non-Thumb1 functions, also check for hi-reg CS registers
1017 if (!AFI->isThumb1OnlyFunction()) {
1018 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1019 unsigned Reg = UnspilledCS2GPRs.back();
1020 UnspilledCS2GPRs.pop_back();
1021 if (!RegInfo->isReservedReg(MF, Reg)) {
1022 Extras.push_back(Reg);
1023 NumExtras--;
1024 }
1025 }
1026 }
1027 if (Extras.size() && NumExtras == 0) {
1028 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1029 MF.getRegInfo().setPhysRegUsed(Extras[i]);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001030 }
1031 } else if (!AFI->isThumb1OnlyFunction()) {
1032 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1033 // closest to SP or frame pointer.
1034 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
1035 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1036 RC->getAlignment(),
1037 false));
1038 }
1039 }
1040 }
1041
1042 if (ForceLRSpill) {
1043 MF.getRegInfo().setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001044 AFI->setLRIsSpilledForFarJump(true);
1045 }
1046}