blob: a9e74dda76708a2e06e49763c8b6992591ce988a [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===//
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"
Eric Christophere94ac882012-08-03 00:05:53 +000018#include "llvm/CallingConv.h"
19#include "llvm/Function.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +000021#include "llvm/Function.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengab5c7032010-11-22 18:12:04 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000026#include "llvm/CodeGen/RegisterScavenging.h"
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000027#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +000028#include "llvm/Support/CommandLine.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000029
30using namespace llvm;
31
Benjamin Kramer120cfdf2012-02-24 22:09:25 +000032static cl::opt<bool>
Jakob Stoklund Olesenbad1e6b2012-01-06 22:19:37 +000033SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +000034 cl::desc("Align ARM NEON spills in prolog and epilog"));
35
36static MachineBasicBlock::iterator
37skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
38 unsigned NumAlignedDPRCS2Regs);
39
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000040/// hasFP - Return true if the specified function should have a dedicated frame
41/// pointer register. This is true if the function has variable sized allocas
42/// or if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000043bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000044 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
45
Evan Chengafad0fe2012-01-04 01:55:04 +000046 // iOS requires FP not to be clobbered for backtracing purpose.
47 if (STI.isTargetIOS())
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000048 return true;
49
50 const MachineFrameInfo *MFI = MF.getFrameInfo();
51 // Always eliminate non-leaf frame pointers.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000052 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
53 MFI->hasCalls()) ||
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000054 RegInfo->needsStackRealignment(MF) ||
55 MFI->hasVarSizedObjects() ||
56 MFI->isFrameAddressTaken());
57}
58
Bob Wilson42257852011-01-13 21:10:12 +000059/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
60/// not required, we reserve argument space for call sites in the function
61/// immediately on entry to the current function. This eliminates the need for
62/// add/sub sp brackets around call sites. Returns true if the call frame is
63/// included as part of the stack frame.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000064bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000065 const MachineFrameInfo *FFI = MF.getFrameInfo();
66 unsigned CFSize = FFI->getMaxCallFrameSize();
67 // It's not always a good idea to include the call frame as part of the
68 // stack frame. ARM (especially Thumb) has small immediate offset to
69 // address the stack frame. So a large call frame can cause poor codegen
70 // and may even makes it impossible to scavenge a register.
71 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
72 return false;
73
74 return !MF.getFrameInfo()->hasVarSizedObjects();
75}
76
Bob Wilson42257852011-01-13 21:10:12 +000077/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
78/// call frame pseudos can be simplified. Unlike most targets, having a FP
79/// is not sufficient here since we still may reference some objects via SP
80/// even when FP is available in Thumb2 mode.
81bool
82ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000083 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
84}
85
Craig Topper015f2282012-03-04 03:33:22 +000086static bool isCalleeSavedRegister(unsigned Reg, const uint16_t *CSRegs) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000087 for (unsigned i = 0; CSRegs[i]; ++i)
88 if (Reg == CSRegs[i])
89 return true;
90 return false;
91}
92
93static bool isCSRestore(MachineInstr *MI,
94 const ARMBaseInstrInfo &TII,
Craig Topper015f2282012-03-04 03:33:22 +000095 const uint16_t *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000096 // Integer spill area is handled with "pop".
97 if (MI->getOpcode() == ARM::LDMIA_RET ||
98 MI->getOpcode() == ARM::t2LDMIA_RET ||
99 MI->getOpcode() == ARM::LDMIA_UPD ||
100 MI->getOpcode() == ARM::t2LDMIA_UPD ||
101 MI->getOpcode() == ARM::VLDMDIA_UPD) {
102 // The first two operands are predicates. The last two are
103 // imp-def and imp-use of SP. Check everything in between.
104 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
105 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
106 return false;
107 return true;
108 }
Owen Anderson793e7962011-07-26 20:54:26 +0000109 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
110 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbach568f5282010-12-10 18:41:15 +0000111 MI->getOpcode() == ARM::t2LDR_POST) &&
112 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
113 MI->getOperand(1).getReg() == ARM::SP)
114 return true;
Eric Christopher8b3ca622010-11-18 19:40:05 +0000115
116 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000117}
118
119static void
120emitSPUpdate(bool isARM,
121 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
122 DebugLoc dl, const ARMBaseInstrInfo &TII,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000123 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000124 if (isARM)
125 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000126 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000127 else
128 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000129 ARMCC::AL, 0, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000130}
131
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000132void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000133 MachineBasicBlock &MBB = MF.front();
134 MachineBasicBlock::iterator MBBI = MBB.begin();
135 MachineFrameInfo *MFI = MF.getFrameInfo();
136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
137 const ARMBaseRegisterInfo *RegInfo =
138 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
139 const ARMBaseInstrInfo &TII =
140 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
141 assert(!AFI->isThumb1OnlyFunction() &&
142 "This emitPrologue does not support Thumb1!");
143 bool isARM = !AFI->isThumbFunction();
144 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
145 unsigned NumBytes = MFI->getStackSize();
146 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
147 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
148 unsigned FramePtr = RegInfo->getFrameRegister(MF);
149
150 // Determine the sizes of each callee-save spill areas and record which frame
151 // belongs to which callee-save spill areas.
152 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
153 int FramePtrSpillFI = 0;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000154 int D8SpillFI = 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000155
Eric Christophere94ac882012-08-03 00:05:53 +0000156 // All calls are tail calls in GHC calling conv, and functions have no prologue/epilogue.
157 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
158 return;
159
Anton Korobeynikov33464912010-11-15 00:06:54 +0000160 // Allocate the vararg register save area. This is not counted in NumBytes.
161 if (VARegSaveSize)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000162 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize,
163 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000164
165 if (!AFI->hasStackFrame()) {
166 if (NumBytes != 0)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000167 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
168 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000169 return;
170 }
171
172 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
173 unsigned Reg = CSI[i].getReg();
174 int FI = CSI[i].getFrameIdx();
175 switch (Reg) {
176 case ARM::R4:
177 case ARM::R5:
178 case ARM::R6:
179 case ARM::R7:
180 case ARM::LR:
181 if (Reg == FramePtr)
182 FramePtrSpillFI = FI;
183 AFI->addGPRCalleeSavedArea1Frame(FI);
184 GPRCS1Size += 4;
185 break;
186 case ARM::R8:
187 case ARM::R9:
188 case ARM::R10:
189 case ARM::R11:
190 if (Reg == FramePtr)
191 FramePtrSpillFI = FI;
Evan Chengafad0fe2012-01-04 01:55:04 +0000192 if (STI.isTargetIOS()) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000193 AFI->addGPRCalleeSavedArea2Frame(FI);
194 GPRCS2Size += 4;
195 } else {
196 AFI->addGPRCalleeSavedArea1Frame(FI);
197 GPRCS1Size += 4;
198 }
199 break;
200 default:
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000201 // This is a DPR. Exclude the aligned DPRCS2 spills.
202 if (Reg == ARM::D8)
203 D8SpillFI = FI;
204 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) {
205 AFI->addDPRCalleeSavedAreaFrame(FI);
206 DPRCSSize += 8;
207 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000208 }
209 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000210
Eric Christopher8b3ca622010-11-18 19:40:05 +0000211 // Move past area 1.
212 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000213
Anton Korobeynikov33464912010-11-15 00:06:54 +0000214 // Set FP to point to the stack slot that contains the previous FP.
Evan Chengafad0fe2012-01-04 01:55:04 +0000215 // For iOS, FP is R7, which has now been stored in spill area 1.
216 // Otherwise, if this is not iOS, all the callee-saved registers go
Anton Korobeynikov33464912010-11-15 00:06:54 +0000217 // into spill area 1, including the FP in R11. In either case, it is
218 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000219 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000220 if (HasFP) {
221 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
222 MachineInstrBuilder MIB =
223 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000224 .addFrameIndex(FramePtrSpillFI).addImm(0)
225 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000226 AddDefaultCC(AddDefaultPred(MIB));
227 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000228
Eric Christopher8b3ca622010-11-18 19:40:05 +0000229 // Move past area 2.
230 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000231
Anton Korobeynikov33464912010-11-15 00:06:54 +0000232 // Determine starting offsets of spill areas.
233 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
234 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
235 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
236 if (HasFP)
237 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
238 NumBytes);
239 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
240 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
241 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
242
Eric Christopher8b3ca622010-11-18 19:40:05 +0000243 // Move past area 3.
Evan Chengacca09b2011-02-25 00:24:46 +0000244 if (DPRCSSize > 0) {
245 MBBI++;
246 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Cheng9831f2d2011-02-25 01:29:29 +0000247 // instructions in the prologue.
Evan Chengacca09b2011-02-25 00:24:46 +0000248 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
249 MBBI++;
250 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000251
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000252 // Move past the aligned DPRCS2 area.
253 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
254 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
255 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
256 // leaves the stack pointer pointing to the DPRCS2 area.
257 //
258 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
259 NumBytes += MFI->getObjectOffset(D8SpillFI);
260 } else
261 NumBytes = DPRCSOffset;
262
Anton Korobeynikov33464912010-11-15 00:06:54 +0000263 if (NumBytes) {
264 // Adjust SP after all the callee-save spills.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000265 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
266 MachineInstr::FrameSetup);
Evan Chengab5c7032010-11-22 18:12:04 +0000267 if (HasFP && isARM)
268 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
269 // Note it's not safe to do this in Thumb2 mode because it would have
270 // taken two instructions:
271 // mov sp, r7
272 // sub sp, #24
273 // If an interrupt is taken between the two instructions, then sp is in
274 // an inconsistent state (pointing to the middle of callee-saved area).
275 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000276 AFI->setShouldRestoreSPFromFP(true);
277 }
278
Evan Chengab5c7032010-11-22 18:12:04 +0000279 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000280 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
281 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000282
283 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
284 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
285 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
286
287 // If we need dynamic stack realignment, do it here. Be paranoid and make
288 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen43ea32c2011-12-24 04:17:01 +0000289 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000290 // realigned.
291 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000292 unsigned MaxAlign = MFI->getMaxAlignment();
293 assert (!AFI->isThumb1OnlyFunction());
294 if (!AFI->isThumbFunction()) {
295 // Emit bic sp, sp, MaxAlign
296 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
297 TII.get(ARM::BICri), ARM::SP)
298 .addReg(ARM::SP, RegState::Kill)
299 .addImm(MaxAlign-1)));
300 } else {
301 // We cannot use sp as source/dest register here, thus we're emitting the
302 // following sequence:
303 // mov r4, sp
304 // bic r4, r4, MaxAlign
305 // mov sp, r4
306 // FIXME: It will be better just to find spare register here.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000307 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000308 .addReg(ARM::SP, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000309 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
310 TII.get(ARM::t2BICri), ARM::R4)
311 .addReg(ARM::R4, RegState::Kill)
312 .addImm(MaxAlign-1)));
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000313 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000314 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000315 }
316
317 AFI->setShouldRestoreSPFromFP(true);
318 }
319
320 // If we need a base pointer, set it up here. It's whatever the value
321 // of the stack pointer is at this point. Any variable size objects
322 // will be allocated after this, so we can still use the base pointer
323 // to reference locals.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000324 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000325 if (RegInfo->hasBasePointer(MF)) {
326 if (isARM)
327 BuildMI(MBB, MBBI, dl,
328 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
329 .addReg(ARM::SP)
330 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
331 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000332 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000333 RegInfo->getBaseRegister())
334 .addReg(ARM::SP));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000335 }
336
337 // If the frame has variable sized objects then the epilogue must restore
Eric Christopher4dd312f2011-01-10 23:10:59 +0000338 // the sp from fp. We can assume there's an FP here since hasFP already
339 // checks for hasVarSizedObjects.
Evan Chengab5c7032010-11-22 18:12:04 +0000340 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000341 AFI->setShouldRestoreSPFromFP(true);
342}
343
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000344void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000345 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000346 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000347 assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000348 unsigned RetOpcode = MBBI->getOpcode();
349 DebugLoc dl = MBBI->getDebugLoc();
350 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +0000351 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000352 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
353 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
354 const ARMBaseInstrInfo &TII =
355 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
356 assert(!AFI->isThumb1OnlyFunction() &&
357 "This emitEpilogue does not support Thumb1!");
358 bool isARM = !AFI->isThumbFunction();
359
360 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
361 int NumBytes = (int)MFI->getStackSize();
362 unsigned FramePtr = RegInfo->getFrameRegister(MF);
363
Eric Christophere94ac882012-08-03 00:05:53 +0000364 // All calls are tail calls in GHC calling conv, and functions have no prologue/epilogue.
365 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
366 return;
367
Anton Korobeynikov33464912010-11-15 00:06:54 +0000368 if (!AFI->hasStackFrame()) {
369 if (NumBytes != 0)
370 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
371 } else {
372 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper015f2282012-03-04 03:33:22 +0000373 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000374 if (MBBI != MBB.begin()) {
375 do
376 --MBBI;
377 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
378 if (!isCSRestore(MBBI, TII, CSRegs))
379 ++MBBI;
380 }
381
382 // Move SP to start of FP callee save spill area.
383 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
384 AFI->getGPRCalleeSavedArea2Size() +
385 AFI->getDPRCalleeSavedAreaSize());
386
387 // Reset SP based on frame pointer only if the stack frame extends beyond
388 // frame pointer stack slot or target is ELF and the function has FP.
389 if (AFI->shouldRestoreSPFromFP()) {
390 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
391 if (NumBytes) {
392 if (isARM)
393 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
394 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000395 else {
396 // It's not possible to restore SP from FP in a single instruction.
Evan Chengafad0fe2012-01-04 01:55:04 +0000397 // For iOS, this looks like:
Evan Chengab5c7032010-11-22 18:12:04 +0000398 // mov sp, r7
399 // sub sp, #24
400 // This is bad, if an interrupt is taken after the mov, sp is in an
401 // inconsistent state.
402 // Use the first callee-saved register as a scratch register.
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +0000403 assert(MRI.isPhysRegUsed(ARM::R4) &&
Evan Chengab5c7032010-11-22 18:12:04 +0000404 "No scratch register to restore SP from FP!");
405 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000406 ARMCC::AL, 0, TII);
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000407 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000408 ARM::SP)
409 .addReg(ARM::R4));
Evan Chengab5c7032010-11-22 18:12:04 +0000410 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000411 } else {
412 // Thumb2 or ARM.
413 if (isARM)
414 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
415 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
416 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000417 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000418 ARM::SP)
419 .addReg(FramePtr));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000420 }
421 } else if (NumBytes)
422 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
423
Eric Christopher8b3ca622010-11-18 19:40:05 +0000424 // Increment past our save areas.
Evan Chengacca09b2011-02-25 00:24:46 +0000425 if (AFI->getDPRCalleeSavedAreaSize()) {
426 MBBI++;
427 // Since vpop register list cannot have gaps, there may be multiple vpop
428 // instructions in the epilogue.
429 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
430 MBBI++;
431 }
Eric Christopher8b3ca622010-11-18 19:40:05 +0000432 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
433 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000434 }
435
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000436 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000437 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000438 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000439 MachineOperand &JumpTarget = MBBI->getOperand(0);
440
441 // Jump to label or value in register.
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000442 if (RetOpcode == ARM::TCRETURNdi) {
443 unsigned TCOpcode = STI.isThumb() ?
444 (STI.isTargetIOS() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) :
445 ARM::TAILJMPd;
Evan Cheng3d2125c2010-11-30 23:55:39 +0000446 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
447 if (JumpTarget.isGlobal())
448 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
449 JumpTarget.getTargetFlags());
450 else {
451 assert(JumpTarget.isSymbol());
452 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
453 JumpTarget.getTargetFlags());
454 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000455
456 // Add the default predicate in Thumb mode.
457 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000458 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000459 BuildMI(MBB, MBBI, dl,
460 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000461 addReg(JumpTarget.getReg(), RegState::Kill);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000462 }
463
464 MachineInstr *NewMI = prior(MBBI);
465 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
466 NewMI->addOperand(MBBI->getOperand(i));
467
468 // Delete the pseudo instruction TCRETURN.
469 MBB.erase(MBBI);
Cameron Zwarichcd4e0b52011-06-17 02:16:43 +0000470 MBBI = NewMI;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000471 }
472
473 if (VARegSaveSize)
474 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
475}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000476
Bob Wilson42257852011-01-13 21:10:12 +0000477/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
478/// debug info. It's the same as what we use for resolving the code-gen
479/// references for now. FIXME: This can go wrong when references are
480/// SP-relative and simple call frames aren't used.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000481int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000482ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson42257852011-01-13 21:10:12 +0000483 unsigned &FrameReg) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000484 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
485}
486
487int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000488ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengdb6cbe12011-04-22 01:42:52 +0000489 int FI, unsigned &FrameReg,
Bob Wilson42257852011-01-13 21:10:12 +0000490 int SPAdj) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000491 const MachineFrameInfo *MFI = MF.getFrameInfo();
492 const ARMBaseRegisterInfo *RegInfo =
493 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
494 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
495 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
496 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
497 bool isFixed = MFI->isFixedObjectIndex(FI);
498
499 FrameReg = ARM::SP;
500 Offset += SPAdj;
501 if (AFI->isGPRCalleeSavedArea1Frame(FI))
502 return Offset - AFI->getGPRCalleeSavedArea1Offset();
503 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
504 return Offset - AFI->getGPRCalleeSavedArea2Offset();
505 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
506 return Offset - AFI->getDPRCalleeSavedAreaOffset();
507
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000508 // SP can move around if there are allocas. We may also lose track of SP
509 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilson055a8122012-03-20 19:28:22 +0000510 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000511
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000512 // When dynamically realigning the stack, use the frame pointer for
513 // parameters, and the stack/base pointer for locals.
514 if (RegInfo->needsStackRealignment(MF)) {
515 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
516 if (isFixed) {
517 FrameReg = RegInfo->getFrameRegister(MF);
518 Offset = FPOffset;
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000519 } else if (hasMovingSP) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000520 assert(RegInfo->hasBasePointer(MF) &&
521 "VLAs and dynamic stack alignment, but missing base pointer!");
522 FrameReg = RegInfo->getBaseRegister();
523 }
524 return Offset;
525 }
526
527 // If there is a frame pointer, use it when we can.
528 if (hasFP(MF) && AFI->hasStackFrame()) {
529 // Use frame pointer to reference fixed objects. Use it for locals if
530 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000531 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000532 FrameReg = RegInfo->getFrameRegister(MF);
533 return FPOffset;
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000534 } else if (hasMovingSP) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000535 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000536 if (AFI->isThumb2Function()) {
Evan Chengdb6cbe12011-04-22 01:42:52 +0000537 // Try to use the frame pointer if we can, else use the base pointer
538 // since it's available. This is handy for the emergency spill slot, in
539 // particular.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000540 if (FPOffset >= -255 && FPOffset < 0) {
541 FrameReg = RegInfo->getFrameRegister(MF);
542 return FPOffset;
543 }
Evan Chengdb6cbe12011-04-22 01:42:52 +0000544 }
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000545 } else if (AFI->isThumb2Function()) {
Andrew Trick51972da2011-08-25 17:40:54 +0000546 // Use add <rd>, sp, #<imm8>
Evan Chengdb6cbe12011-04-22 01:42:52 +0000547 // ldr <rd>, [sp, #<imm8>]
548 // if at all possible to save space.
549 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
550 return Offset;
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000551 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengdb6cbe12011-04-22 01:42:52 +0000552 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000553 if (FPOffset >= -255 && FPOffset < 0) {
554 FrameReg = RegInfo->getFrameRegister(MF);
555 return FPOffset;
556 }
557 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
558 // Otherwise, use SP or FP, whichever is closer to the stack slot.
559 FrameReg = RegInfo->getFrameRegister(MF);
560 return FPOffset;
561 }
562 }
563 // Use the base pointer if we have one.
564 if (RegInfo->hasBasePointer(MF))
565 FrameReg = RegInfo->getBaseRegister();
566 return Offset;
567}
568
Bob Wilson42257852011-01-13 21:10:12 +0000569int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
570 int FI) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000571 unsigned FrameReg;
572 return getFrameIndexReference(MF, FI, FrameReg);
573}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000574
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000575void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000576 MachineBasicBlock::iterator MI,
577 const std::vector<CalleeSavedInfo> &CSI,
578 unsigned StmOpc, unsigned StrOpc,
579 bool NoGap,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000580 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000581 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000582 unsigned MIFlags) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000583 MachineFunction &MF = *MBB.getParent();
584 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
585
586 DebugLoc DL;
587 if (MI != MBB.end()) DL = MI->getDebugLoc();
588
Evan Cheng9801b5c2010-12-07 19:59:34 +0000589 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng06d65f52010-12-07 23:08:38 +0000590 unsigned i = CSI.size();
591 while (i != 0) {
592 unsigned LastReg = 0;
593 for (; i != 0; --i) {
594 unsigned Reg = CSI[i-1].getReg();
Evan Chengafad0fe2012-01-04 01:55:04 +0000595 if (!(Func)(Reg, STI.isTargetIOS())) continue;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000596
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000597 // D-registers in the aligned area DPRCS2 are NOT spilled here.
598 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
599 continue;
600
Evan Cheng06d65f52010-12-07 23:08:38 +0000601 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000602 // @llvm.returnaddress is called. If LR is returned for
603 // @llvm.returnaddress then it's already added to the function and
604 // entry block live-in sets.
Evan Cheng06d65f52010-12-07 23:08:38 +0000605 bool isKill = true;
606 if (Reg == ARM::LR) {
607 if (MF.getFrameInfo()->isReturnAddressTaken() &&
608 MF.getRegInfo().isLiveIn(Reg))
609 isKill = false;
610 }
611
612 if (isKill)
613 MBB.addLiveIn(Reg);
614
Eric Christopher1a48c032010-12-09 01:57:45 +0000615 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng275bf632010-12-08 06:29:02 +0000616 // for other instructions. e.g.
Eric Christopher1a48c032010-12-09 01:57:45 +0000617 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng275bf632010-12-08 06:29:02 +0000618 if (NoGap && LastReg && LastReg != Reg-1)
619 break;
Evan Cheng06d65f52010-12-07 23:08:38 +0000620 LastReg = Reg;
621 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000622 }
623
Jim Grosbachc6f92612010-12-09 18:31:13 +0000624 if (Regs.empty())
625 continue;
626 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000627 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000628 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000629 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng06d65f52010-12-07 23:08:38 +0000630 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
631 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbachc6f92612010-12-09 18:31:13 +0000632 } else if (Regs.size() == 1) {
633 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
634 ARM::SP)
635 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbach19dec202011-08-05 20:35:44 +0000636 .addReg(ARM::SP).setMIFlags(MIFlags)
637 .addImm(-4);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000638 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000639 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000640 Regs.clear();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000641 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000642}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000643
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000644void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000645 MachineBasicBlock::iterator MI,
646 const std::vector<CalleeSavedInfo> &CSI,
647 unsigned LdmOpc, unsigned LdrOpc,
648 bool isVarArg, bool NoGap,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000649 bool(*Func)(unsigned, bool),
650 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng06d65f52010-12-07 23:08:38 +0000651 MachineFunction &MF = *MBB.getParent();
652 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
654 DebugLoc DL = MI->getDebugLoc();
Evan Cheng7cfa6562011-01-25 01:28:33 +0000655 unsigned RetOpcode = MI->getOpcode();
656 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000657 RetOpcode == ARM::TCRETURNri);
Evan Cheng06d65f52010-12-07 23:08:38 +0000658
659 SmallVector<unsigned, 4> Regs;
660 unsigned i = CSI.size();
661 while (i != 0) {
662 unsigned LastReg = 0;
663 bool DeleteRet = false;
664 for (; i != 0; --i) {
665 unsigned Reg = CSI[i-1].getReg();
Evan Chengafad0fe2012-01-04 01:55:04 +0000666 if (!(Func)(Reg, STI.isTargetIOS())) continue;
Evan Cheng06d65f52010-12-07 23:08:38 +0000667
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000668 // The aligned reloads from area DPRCS2 are not inserted here.
669 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
670 continue;
671
Evan Cheng7cfa6562011-01-25 01:28:33 +0000672 if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000673 Reg = ARM::PC;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000674 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng06d65f52010-12-07 23:08:38 +0000675 // Fold the return instruction into the LDM.
676 DeleteRet = true;
677 }
678
Evan Cheng275bf632010-12-08 06:29:02 +0000679 // If NoGap is true, pop consecutive registers and then leave the rest
680 // for other instructions. e.g.
681 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
682 if (NoGap && LastReg && LastReg != Reg-1)
683 break;
684
Evan Cheng06d65f52010-12-07 23:08:38 +0000685 LastReg = Reg;
686 Regs.push_back(Reg);
687 }
688
Jim Grosbachc6f92612010-12-09 18:31:13 +0000689 if (Regs.empty())
690 continue;
691 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000692 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000693 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000694 .addReg(ARM::SP));
695 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
696 MIB.addReg(Regs[i], getDefRegState(true));
Andrew Trickb9ca5122011-08-25 17:50:53 +0000697 if (DeleteRet) {
698 MIB->copyImplicitOps(&*MI);
Evan Cheng06d65f52010-12-07 23:08:38 +0000699 MI->eraseFromParent();
Andrew Trickb9ca5122011-08-25 17:50:53 +0000700 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000701 MI = MIB;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000702 } else if (Regs.size() == 1) {
703 // If we adjusted the reg to PC from LR above, switch it back here. We
704 // only do that for LDM.
705 if (Regs[0] == ARM::PC)
706 Regs[0] = ARM::LR;
707 MachineInstrBuilder MIB =
708 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
709 .addReg(ARM::SP, RegState::Define)
710 .addReg(ARM::SP);
711 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
712 // that refactoring is complete (eventually).
Owen Anderson793e7962011-07-26 20:54:26 +0000713 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbachc6f92612010-12-09 18:31:13 +0000714 MIB.addReg(0);
715 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
716 } else
717 MIB.addImm(4);
718 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000719 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000720 Regs.clear();
Evan Cheng9801b5c2010-12-07 19:59:34 +0000721 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000722}
723
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000724/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen43ea32c2011-12-24 04:17:01 +0000725/// starting from d8. Also insert stack realignment code and leave the stack
726/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000727static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
728 MachineBasicBlock::iterator MI,
729 unsigned NumAlignedDPRCS2Regs,
730 const std::vector<CalleeSavedInfo> &CSI,
731 const TargetRegisterInfo *TRI) {
732 MachineFunction &MF = *MBB.getParent();
733 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
734 DebugLoc DL = MI->getDebugLoc();
735 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
736 MachineFrameInfo &MFI = *MF.getFrameInfo();
737
738 // Mark the D-register spill slots as properly aligned. Since MFI computes
739 // stack slot layout backwards, this can actually mean that the d-reg stack
740 // slot offsets can be wrong. The offset for d8 will always be correct.
741 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
742 unsigned DNum = CSI[i].getReg() - ARM::D8;
743 if (DNum >= 8)
744 continue;
745 int FI = CSI[i].getFrameIdx();
746 // The even-numbered registers will be 16-byte aligned, the odd-numbered
747 // registers will be 8-byte aligned.
748 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
749
750 // The stack slot for D8 needs to be maximally aligned because this is
751 // actually the point where we align the stack pointer. MachineFrameInfo
752 // computes all offsets relative to the incoming stack pointer which is a
753 // bit weird when realigning the stack. Any extra padding for this
754 // over-alignment is not realized because the code inserted below adjusts
755 // the stack pointer by numregs * 8 before aligning the stack pointer.
756 if (DNum == 0)
757 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
758 }
759
760 // Move the stack pointer to the d8 spill slot, and align it at the same
761 // time. Leave the stack slot address in the scratch register r4.
762 //
763 // sub r4, sp, #numregs * 8
764 // bic r4, r4, #align - 1
765 // mov sp, r4
766 //
767 bool isThumb = AFI->isThumbFunction();
768 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
769 AFI->setShouldRestoreSPFromFP(true);
770
771 // sub r4, sp, #numregs * 8
772 // The immediate is <= 64, so it doesn't need any special encoding.
773 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
774 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
775 .addReg(ARM::SP)
776 .addImm(8 * NumAlignedDPRCS2Regs)));
777
778 // bic r4, r4, #align-1
779 Opc = isThumb ? ARM::t2BICri : ARM::BICri;
780 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
781 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
782 .addReg(ARM::R4, RegState::Kill)
783 .addImm(MaxAlign - 1)));
784
785 // mov sp, r4
786 // The stack pointer must be adjusted before spilling anything, otherwise
787 // the stack slots could be clobbered by an interrupt handler.
788 // Leave r4 live, it is used below.
789 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
790 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
791 .addReg(ARM::R4);
792 MIB = AddDefaultPred(MIB);
793 if (!isThumb)
794 AddDefaultCC(MIB);
795
796 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
797 // r4 holds the stack slot address.
798 unsigned NextReg = ARM::D8;
799
800 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
801 // The writeback is only needed when emitting two vst1.64 instructions.
802 if (NumAlignedDPRCS2Regs >= 6) {
803 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000804 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000805 MBB.addLiveIn(SupReg);
806 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
807 ARM::R4)
808 .addReg(ARM::R4, RegState::Kill).addImm(16)
809 .addReg(NextReg)
810 .addReg(SupReg, RegState::ImplicitKill));
811 NextReg += 4;
812 NumAlignedDPRCS2Regs -= 4;
813 }
814
815 // We won't modify r4 beyond this point. It currently points to the next
816 // register to be spilled.
817 unsigned R4BaseReg = NextReg;
818
819 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
820 if (NumAlignedDPRCS2Regs >= 4) {
821 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000822 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000823 MBB.addLiveIn(SupReg);
824 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
825 .addReg(ARM::R4).addImm(16).addReg(NextReg)
826 .addReg(SupReg, RegState::ImplicitKill));
827 NextReg += 4;
828 NumAlignedDPRCS2Regs -= 4;
829 }
830
831 // 16-byte aligned vst1.64 with 2 d-regs.
832 if (NumAlignedDPRCS2Regs >= 2) {
833 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000834 &ARM::QPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000835 MBB.addLiveIn(SupReg);
836 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbach28f08c92012-03-05 19:33:30 +0000837 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000838 NextReg += 2;
839 NumAlignedDPRCS2Regs -= 2;
840 }
841
842 // Finally, use a vanilla vstr.64 for the odd last register.
843 if (NumAlignedDPRCS2Regs) {
844 MBB.addLiveIn(NextReg);
845 // vstr.64 uses addrmode5 which has an offset scale of 4.
846 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
847 .addReg(NextReg)
848 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
849 }
850
851 // The last spill instruction inserted should kill the scratch register r4.
852 llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI);
853}
854
855/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
856/// iterator to the following instruction.
857static MachineBasicBlock::iterator
858skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
859 unsigned NumAlignedDPRCS2Regs) {
860 // sub r4, sp, #numregs * 8
861 // bic r4, r4, #align - 1
862 // mov sp, r4
863 ++MI; ++MI; ++MI;
864 assert(MI->mayStore() && "Expecting spill instruction");
865
866 // These switches all fall through.
867 switch(NumAlignedDPRCS2Regs) {
868 case 7:
869 ++MI;
870 assert(MI->mayStore() && "Expecting spill instruction");
871 default:
872 ++MI;
873 assert(MI->mayStore() && "Expecting spill instruction");
874 case 1:
875 case 2:
876 case 4:
877 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
878 ++MI;
879 }
880 return MI;
881}
882
883/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
884/// starting from d8. These instructions are assumed to execute while the
885/// stack is still aligned, unlike the code inserted by emitPopInst.
886static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
887 MachineBasicBlock::iterator MI,
888 unsigned NumAlignedDPRCS2Regs,
889 const std::vector<CalleeSavedInfo> &CSI,
890 const TargetRegisterInfo *TRI) {
891 MachineFunction &MF = *MBB.getParent();
892 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
893 DebugLoc DL = MI->getDebugLoc();
894 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
895
896 // Find the frame index assigned to d8.
897 int D8SpillFI = 0;
898 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
899 if (CSI[i].getReg() == ARM::D8) {
900 D8SpillFI = CSI[i].getFrameIdx();
901 break;
902 }
903
904 // Materialize the address of the d8 spill slot into the scratch register r4.
905 // This can be fairly complicated if the stack frame is large, so just use
906 // the normal frame index elimination mechanism to do it. This code runs as
907 // the initial part of the epilog where the stack and base pointers haven't
908 // been changed yet.
909 bool isThumb = AFI->isThumbFunction();
910 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
911
912 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
913 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
914 .addFrameIndex(D8SpillFI).addImm(0)));
915
916 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
917 unsigned NextReg = ARM::D8;
918
919 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
920 if (NumAlignedDPRCS2Regs >= 6) {
921 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000922 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000923 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
924 .addReg(ARM::R4, RegState::Define)
925 .addReg(ARM::R4, RegState::Kill).addImm(16)
926 .addReg(SupReg, RegState::ImplicitDefine));
927 NextReg += 4;
928 NumAlignedDPRCS2Regs -= 4;
929 }
930
931 // We won't modify r4 beyond this point. It currently points to the next
932 // register to be spilled.
933 unsigned R4BaseReg = NextReg;
934
935 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
936 if (NumAlignedDPRCS2Regs >= 4) {
937 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000938 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000939 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
940 .addReg(ARM::R4).addImm(16)
941 .addReg(SupReg, RegState::ImplicitDefine));
942 NextReg += 4;
943 NumAlignedDPRCS2Regs -= 4;
944 }
945
946 // 16-byte aligned vld1.64 with 2 d-regs.
947 if (NumAlignedDPRCS2Regs >= 2) {
948 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000949 &ARM::QPRRegClass);
Jim Grosbach28f08c92012-03-05 19:33:30 +0000950 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
951 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000952 NextReg += 2;
953 NumAlignedDPRCS2Regs -= 2;
954 }
955
956 // Finally, use a vanilla vldr.64 for the remaining odd register.
957 if (NumAlignedDPRCS2Regs)
958 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
959 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
960
961 // Last store kills r4.
962 llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI);
963}
964
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000965bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000966 MachineBasicBlock::iterator MI,
967 const std::vector<CalleeSavedInfo> &CSI,
968 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000969 if (CSI.empty())
970 return false;
971
972 MachineFunction &MF = *MBB.getParent();
973 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000974
975 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000976 unsigned PushOneOpc = AFI->isThumbFunction() ?
977 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000978 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000979 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
980 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000981 MachineInstr::FrameSetup);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000982 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000983 MachineInstr::FrameSetup);
984 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000985 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
986
987 // The code above does not insert spill code for the aligned DPRCS2 registers.
988 // The stack realignment code will be inserted between the push instructions
989 // and these spills.
990 if (NumAlignedDPRCS2Regs)
991 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000992
993 return true;
994}
995
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000996bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000997 MachineBasicBlock::iterator MI,
998 const std::vector<CalleeSavedInfo> &CSI,
999 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001000 if (CSI.empty())
1001 return false;
1002
1003 MachineFunction &MF = *MBB.getParent();
1004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1005 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001006 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1007
1008 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1009 // registers. Do that here instead.
1010 if (NumAlignedDPRCS2Regs)
1011 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001012
1013 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +00001014 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001015 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001016 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1017 NumAlignedDPRCS2Regs);
Jim Grosbachc6f92612010-12-09 18:31:13 +00001018 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001019 &isARMArea2Register, 0);
Jim Grosbachc6f92612010-12-09 18:31:13 +00001020 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001021 &isARMArea1Register, 0);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001022
1023 return true;
1024}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001025
1026// FIXME: Make generic?
1027static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1028 const ARMBaseInstrInfo &TII) {
1029 unsigned FnSize = 0;
1030 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
1031 MBBI != E; ++MBBI) {
1032 const MachineBasicBlock &MBB = *MBBI;
1033 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
1034 I != E; ++I)
1035 FnSize += TII.GetInstSizeInBytes(I);
1036 }
1037 return FnSize;
1038}
1039
1040/// estimateStackSize - Estimate and return the size of the frame.
1041/// FIXME: Make generic?
1042static unsigned estimateStackSize(MachineFunction &MF) {
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001043 const MachineFrameInfo *MFI = MF.getFrameInfo();
1044 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
1045 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
1046 unsigned MaxAlign = MFI->getMaxAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001047 int Offset = 0;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001048
1049 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
1050 // It really should be refactored to share code. Until then, changes
1051 // should keep in mind that there's tight coupling between the two.
1052
1053 for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
1054 int FixedOff = -MFI->getObjectOffset(i);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001055 if (FixedOff > Offset) Offset = FixedOff;
1056 }
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001057 for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
1058 if (MFI->isDeadObjectIndex(i))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001059 continue;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001060 Offset += MFI->getObjectSize(i);
1061 unsigned Align = MFI->getObjectAlignment(i);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001062 // Adjust to alignment boundary
1063 Offset = (Offset+Align-1)/Align*Align;
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001064
1065 MaxAlign = std::max(Align, MaxAlign);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001066 }
Jim Grosbachbc20e4f2011-07-05 16:05:50 +00001067
1068 if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF))
1069 Offset += MFI->getMaxCallFrameSize();
1070
1071 // Round up the size to a multiple of the alignment. If the function has
1072 // any calls or alloca's, align to the target's StackAlignment value to
1073 // ensure that the callee's frame or the alloca data is suitably aligned;
1074 // otherwise, for leaf functions, align to the TransientStackAlignment
1075 // value.
1076 unsigned StackAlign;
1077 if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
1078 (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0))
1079 StackAlign = TFI->getStackAlignment();
1080 else
1081 StackAlign = TFI->getTransientStackAlignment();
1082
1083 // If the frame pointer is eliminated, all frame offsets will be relative to
1084 // SP not FP. Align to MaxAlign so this works.
1085 StackAlign = std::max(StackAlign, MaxAlign);
1086 unsigned AlignMask = StackAlign - 1;
1087 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
1088
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001089 return (unsigned)Offset;
1090}
1091
1092/// estimateRSStackSizeLimit - Look at each instruction that references stack
1093/// frames and return the stack size limit beyond which some of these
1094/// instructions will require a scratch register during their expansion later.
1095// FIXME: Move to TII?
1096static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001097 const TargetFrameLowering *TFI) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001098 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1099 unsigned Limit = (1 << 12) - 1;
1100 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
1101 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1102 I != E; ++I) {
1103 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1104 if (!I->getOperand(i).isFI()) continue;
1105
1106 // When using ADDri to get the address of a stack object, 255 is the
1107 // largest offset guaranteed to fit in the immediate offset.
1108 if (I->getOpcode() == ARM::ADDri) {
1109 Limit = std::min(Limit, (1U << 8) - 1);
1110 break;
1111 }
1112
1113 // Otherwise check the addressing mode.
1114 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
1115 case ARMII::AddrMode3:
1116 case ARMII::AddrModeT2_i8:
1117 Limit = std::min(Limit, (1U << 8) - 1);
1118 break;
1119 case ARMII::AddrMode5:
1120 case ARMII::AddrModeT2_i8s4:
1121 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1122 break;
1123 case ARMII::AddrModeT2_i12:
1124 // i12 supports only positive offset so these will be converted to
1125 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1126 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1127 Limit = std::min(Limit, (1U << 8) - 1);
1128 break;
1129 case ARMII::AddrMode4:
1130 case ARMII::AddrMode6:
1131 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1132 // immediate offset for stack references.
1133 return 0;
1134 default:
1135 break;
1136 }
1137 break; // At most one FI per instruction
1138 }
1139 }
1140 }
1141
1142 return Limit;
1143}
1144
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001145// In functions that realign the stack, it can be an advantage to spill the
1146// callee-saved vector registers after realigning the stack. The vst1 and vld1
1147// instructions take alignment hints that can improve performance.
1148//
1149static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) {
1150 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1151 if (!SpillAlignedNEONRegs)
1152 return;
1153
1154 // Naked functions don't spill callee-saved registers.
Bill Wendling67658342012-10-09 07:45:08 +00001155 if (MF.getFunction()->getFnAttributes().hasAttribute(Attributes::Naked))
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001156 return;
1157
1158 // We are planning to use NEON instructions vst1 / vld1.
1159 if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON())
1160 return;
1161
1162 // Don't bother if the default stack alignment is sufficiently high.
1163 if (MF.getTarget().getFrameLowering()->getStackAlignment() >= 8)
1164 return;
1165
1166 // Aligned spills require stack realignment.
1167 const ARMBaseRegisterInfo *RegInfo =
1168 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1169 if (!RegInfo->canRealignStack(MF))
1170 return;
1171
1172 // We always spill contiguous d-registers starting from d8. Count how many
1173 // needs spilling. The register allocator will almost always use the
1174 // callee-saved registers in order, but it can happen that there are holes in
1175 // the range. Registers above the hole will be spilled to the standard DPRCS
1176 // area.
1177 MachineRegisterInfo &MRI = MF.getRegInfo();
1178 unsigned NumSpills = 0;
1179 for (; NumSpills < 8; ++NumSpills)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +00001180 if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills))
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001181 break;
1182
1183 // Don't do this for just one d-register. It's not worth it.
1184 if (NumSpills < 2)
1185 return;
1186
1187 // Spill the first NumSpills D-registers after realigning the stack.
1188 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1189
1190 // A scratch register is required for the vst1 / vld1 instructions.
1191 MF.getRegInfo().setPhysRegUsed(ARM::R4);
1192}
1193
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001194void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001195ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +00001196 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001197 // This tells PEI to spill the FP as if it is any other callee-save register
1198 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1199 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1200 // to combine multiple loads / stores.
1201 bool CanEliminateFrame = true;
1202 bool CS1Spilled = false;
1203 bool LRSpilled = false;
1204 unsigned NumGPRSpills = 0;
1205 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1206 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1207 const ARMBaseRegisterInfo *RegInfo =
1208 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1209 const ARMBaseInstrInfo &TII =
1210 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
1211 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1212 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001213 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001214 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1215
1216 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1217 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Chengdf55fea2011-01-16 05:14:33 +00001218 // since it's not always possible to restore sp from fp in a single
1219 // instruction.
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001220 // FIXME: It will be better just to find spare register here.
1221 if (AFI->isThumb2Function() &&
1222 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001223 MRI.setPhysRegUsed(ARM::R4);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001224
Evan Chengdf55fea2011-01-16 05:14:33 +00001225 if (AFI->isThumb1OnlyFunction()) {
1226 // Spill LR if Thumb1 function uses variable length argument lists.
1227 if (AFI->getVarArgsRegSaveSize() > 0)
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001228 MRI.setPhysRegUsed(ARM::LR);
Evan Chengdf55fea2011-01-16 05:14:33 +00001229
Jim Grosbach7980f612011-06-13 21:18:25 +00001230 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1231 // for sure what the stack size will be, but for this, an estimate is good
1232 // enough. If there anything changes it, it'll be a spill, which implies
1233 // we've used all the registers and so R4 is already used, so not marking
Chad Rosier6690bca2011-10-20 00:07:12 +00001234 // it here will be OK.
Evan Chengdf55fea2011-01-16 05:14:33 +00001235 // FIXME: It will be better just to find spare register here.
Jim Grosbach7980f612011-06-13 21:18:25 +00001236 unsigned StackSize = estimateStackSize(MF);
Chad Rosier6690bca2011-10-20 00:07:12 +00001237 if (MFI->hasVarSizedObjects() || StackSize > 508)
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001238 MRI.setPhysRegUsed(ARM::R4);
Evan Chengdf55fea2011-01-16 05:14:33 +00001239 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001240
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001241 // See if we can spill vector registers to aligned stack.
1242 checkNumAlignedDPRCS2Regs(MF);
1243
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001244 // Spill the BasePtr if it's used.
1245 if (RegInfo->hasBasePointer(MF))
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001246 MRI.setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001247
1248 // Don't spill FP if the frame can be eliminated. This is determined
1249 // by scanning the callee-save registers to see if any is used.
Craig Topper015f2282012-03-04 03:33:22 +00001250 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001251 for (unsigned i = 0; CSRegs[i]; ++i) {
1252 unsigned Reg = CSRegs[i];
1253 bool Spilled = false;
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001254 if (MRI.isPhysRegUsed(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001255 Spilled = true;
1256 CanEliminateFrame = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001257 }
1258
Craig Topper420761a2012-04-20 07:30:17 +00001259 if (!ARM::GPRRegClass.contains(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001260 continue;
1261
1262 if (Spilled) {
1263 NumGPRSpills++;
1264
Evan Chengafad0fe2012-01-04 01:55:04 +00001265 if (!STI.isTargetIOS()) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001266 if (Reg == ARM::LR)
1267 LRSpilled = true;
1268 CS1Spilled = true;
1269 continue;
1270 }
1271
1272 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1273 switch (Reg) {
1274 case ARM::LR:
1275 LRSpilled = true;
1276 // Fallthrough
1277 case ARM::R4: case ARM::R5:
1278 case ARM::R6: case ARM::R7:
1279 CS1Spilled = true;
1280 break;
1281 default:
1282 break;
1283 }
1284 } else {
Evan Chengafad0fe2012-01-04 01:55:04 +00001285 if (!STI.isTargetIOS()) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001286 UnspilledCS1GPRs.push_back(Reg);
1287 continue;
1288 }
1289
1290 switch (Reg) {
1291 case ARM::R4: case ARM::R5:
1292 case ARM::R6: case ARM::R7:
1293 case ARM::LR:
1294 UnspilledCS1GPRs.push_back(Reg);
1295 break;
1296 default:
1297 UnspilledCS2GPRs.push_back(Reg);
1298 break;
1299 }
1300 }
1301 }
1302
1303 bool ForceLRSpill = false;
1304 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1305 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1306 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1307 // use of BL to implement far jump. If it turns out that it's not needed
1308 // then the branch fix up path will undo it.
1309 if (FnSize >= (1 << 11)) {
1310 CanEliminateFrame = false;
1311 ForceLRSpill = true;
1312 }
1313 }
1314
1315 // If any of the stack slot references may be out of range of an immediate
1316 // offset, make sure a register (or a spill slot) is available for the
1317 // register scavenger. Note that if we're indexing off the frame pointer, the
1318 // effective stack size is 4 bytes larger since the FP points to the stack
1319 // slot of the previous FP. Also, if we have variable sized objects in the
1320 // function, stack slot references will often be negative, and some of
1321 // our instructions are positive-offset only, so conservatively consider
1322 // that case to want a spill slot (or register) as well. Similarly, if
1323 // the function adjusts the stack pointer during execution and the
1324 // adjustments aren't already part of our stack size estimate, our offset
1325 // calculations may be off, so be conservative.
1326 // FIXME: We could add logic to be more precise about negative offsets
1327 // and which instructions will need a scratch register for them. Is it
1328 // worth the effort and added fragility?
1329 bool BigStack =
1330 (RS &&
1331 (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
1332 estimateRSStackSizeLimit(MF, this)))
1333 || MFI->hasVarSizedObjects()
1334 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
1335
1336 bool ExtraCSSpill = false;
1337 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1338 AFI->setHasStackFrame(true);
1339
1340 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1341 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1342 if (!LRSpilled && CS1Spilled) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001343 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001344 NumGPRSpills++;
1345 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1346 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1347 ForceLRSpill = false;
1348 ExtraCSSpill = true;
1349 }
1350
1351 if (hasFP(MF)) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001352 MRI.setPhysRegUsed(FramePtr);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001353 NumGPRSpills++;
1354 }
1355
1356 // If stack and double are 8-byte aligned and we are spilling an odd number
1357 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1358 // the integer and double callee save areas.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001359 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001360 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1361 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1362 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1363 unsigned Reg = UnspilledCS1GPRs[i];
1364 // Don't spill high register if the function is thumb1
1365 if (!AFI->isThumb1OnlyFunction() ||
1366 isARMLowRegister(Reg) || Reg == ARM::LR) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001367 MRI.setPhysRegUsed(Reg);
1368 if (!MRI.isReserved(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001369 ExtraCSSpill = true;
1370 break;
1371 }
1372 }
1373 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1374 unsigned Reg = UnspilledCS2GPRs.front();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001375 MRI.setPhysRegUsed(Reg);
1376 if (!MRI.isReserved(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001377 ExtraCSSpill = true;
1378 }
1379 }
1380
1381 // Estimate if we might need to scavenge a register at some point in order
1382 // to materialize a stack offset. If so, either spill one additional
1383 // callee-saved register or reserve a special spill slot to facilitate
1384 // register scavenging. Thumb1 needs a spill slot for stack pointer
1385 // adjustments also, even when the frame itself is small.
1386 if (BigStack && !ExtraCSSpill) {
1387 // If any non-reserved CS register isn't spilled, just spill one or two
1388 // extra. That should take care of it!
1389 unsigned NumExtras = TargetAlign / 4;
1390 SmallVector<unsigned, 2> Extras;
1391 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1392 unsigned Reg = UnspilledCS1GPRs.back();
1393 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001394 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001395 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1396 Reg == ARM::LR)) {
1397 Extras.push_back(Reg);
1398 NumExtras--;
1399 }
1400 }
1401 // For non-Thumb1 functions, also check for hi-reg CS registers
1402 if (!AFI->isThumb1OnlyFunction()) {
1403 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1404 unsigned Reg = UnspilledCS2GPRs.back();
1405 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001406 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001407 Extras.push_back(Reg);
1408 NumExtras--;
1409 }
1410 }
1411 }
1412 if (Extras.size() && NumExtras == 0) {
1413 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001414 MRI.setPhysRegUsed(Extras[i]);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001415 }
1416 } else if (!AFI->isThumb1OnlyFunction()) {
1417 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1418 // closest to SP or frame pointer.
Craig Topper420761a2012-04-20 07:30:17 +00001419 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001420 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1421 RC->getAlignment(),
1422 false));
1423 }
1424 }
1425 }
1426
1427 if (ForceLRSpill) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001428 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001429 AFI->setLRIsSpilledForFarJump(true);
1430 }
1431}