blob: c8637be2bddfb3d958ba1553c755f867fd7c1d76 [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"
Evan Chengee04a6d2011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMAddressingModes.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chengab5c7032010-11-22 18:12:04 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/Function.h"
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +000026#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000028
29using namespace llvm;
30
Benjamin Kramer120cfdf2012-02-24 22:09:25 +000031static cl::opt<bool>
Jakob Stoklund Olesenbad1e6b2012-01-06 22:19:37 +000032SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +000033 cl::desc("Align ARM NEON spills in prolog and epilog"));
34
35static MachineBasicBlock::iterator
36skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
37 unsigned NumAlignedDPRCS2Regs);
38
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000039/// hasFP - Return true if the specified function should have a dedicated frame
40/// pointer register. This is true if the function has variable sized allocas
41/// or if frame pointer elimination is disabled.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000042bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000043 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
44
Evan Chengafad0fe2012-01-04 01:55:04 +000045 // iOS requires FP not to be clobbered for backtracing purpose.
46 if (STI.isTargetIOS())
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000047 return true;
48
49 const MachineFrameInfo *MFI = MF.getFrameInfo();
50 // Always eliminate non-leaf frame pointers.
Nick Lewycky8a8d4792011-12-02 22:16:29 +000051 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
52 MFI->hasCalls()) ||
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000053 RegInfo->needsStackRealignment(MF) ||
54 MFI->hasVarSizedObjects() ||
55 MFI->isFrameAddressTaken());
56}
57
Bob Wilson42257852011-01-13 21:10:12 +000058/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
59/// not required, we reserve argument space for call sites in the function
60/// immediately on entry to the current function. This eliminates the need for
61/// add/sub sp brackets around call sites. Returns true if the call frame is
62/// included as part of the stack frame.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000063bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000064 const MachineFrameInfo *FFI = MF.getFrameInfo();
65 unsigned CFSize = FFI->getMaxCallFrameSize();
66 // It's not always a good idea to include the call frame as part of the
67 // stack frame. ARM (especially Thumb) has small immediate offset to
68 // address the stack frame. So a large call frame can cause poor codegen
69 // and may even makes it impossible to scavenge a register.
70 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
71 return false;
72
73 return !MF.getFrameInfo()->hasVarSizedObjects();
74}
75
Bob Wilson42257852011-01-13 21:10:12 +000076/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
77/// call frame pseudos can be simplified. Unlike most targets, having a FP
78/// is not sufficient here since we still may reference some objects via SP
79/// even when FP is available in Thumb2 mode.
80bool
81ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000082 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
83}
84
Craig Topper015f2282012-03-04 03:33:22 +000085static bool isCalleeSavedRegister(unsigned Reg, const uint16_t *CSRegs) {
Anton Korobeynikov33464912010-11-15 00:06:54 +000086 for (unsigned i = 0; CSRegs[i]; ++i)
87 if (Reg == CSRegs[i])
88 return true;
89 return false;
90}
91
92static bool isCSRestore(MachineInstr *MI,
93 const ARMBaseInstrInfo &TII,
Craig Topper015f2282012-03-04 03:33:22 +000094 const uint16_t *CSRegs) {
Eric Christopher8b3ca622010-11-18 19:40:05 +000095 // Integer spill area is handled with "pop".
96 if (MI->getOpcode() == ARM::LDMIA_RET ||
97 MI->getOpcode() == ARM::t2LDMIA_RET ||
98 MI->getOpcode() == ARM::LDMIA_UPD ||
99 MI->getOpcode() == ARM::t2LDMIA_UPD ||
100 MI->getOpcode() == ARM::VLDMDIA_UPD) {
101 // The first two operands are predicates. The last two are
102 // imp-def and imp-use of SP. Check everything in between.
103 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
104 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
105 return false;
106 return true;
107 }
Owen Anderson793e7962011-07-26 20:54:26 +0000108 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
109 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbach568f5282010-12-10 18:41:15 +0000110 MI->getOpcode() == ARM::t2LDR_POST) &&
111 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
112 MI->getOperand(1).getReg() == ARM::SP)
113 return true;
Eric Christopher8b3ca622010-11-18 19:40:05 +0000114
115 return false;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000116}
117
118static void
119emitSPUpdate(bool isARM,
120 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
121 DebugLoc dl, const ARMBaseInstrInfo &TII,
Eli Bendersky700ed802013-02-21 20:05:00 +0000122 int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
123 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000124 if (isARM)
125 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Eli Bendersky700ed802013-02-21 20:05:00 +0000126 Pred, PredReg, TII, MIFlags);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000127 else
128 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
Eli Bendersky700ed802013-02-21 20:05:00 +0000129 Pred, PredReg, 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();
Stepan Dyatkovskiy083bc972013-05-20 08:01:34 +0000144 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
145 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000146 unsigned NumBytes = MFI->getStackSize();
147 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
148 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
149 unsigned FramePtr = RegInfo->getFrameRegister(MF);
150
151 // Determine the sizes of each callee-save spill areas and record which frame
152 // belongs to which callee-save spill areas.
153 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
154 int FramePtrSpillFI = 0;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000155 int D8SpillFI = 0;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000156
Jakob Stoklund Olesenbe5ec8c2012-10-26 21:46:57 +0000157 // All calls are tail calls in GHC calling conv, and functions have no
158 // prologue/epilogue.
Eric Christophere94ac882012-08-03 00:05:53 +0000159 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
160 return;
161
Anton Korobeynikov33464912010-11-15 00:06:54 +0000162 // Allocate the vararg register save area. This is not counted in NumBytes.
Stepan Dyatkovskiyf65e4932013-04-30 07:19:58 +0000163 if (ArgRegsSaveSize)
164 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000165 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000166
167 if (!AFI->hasStackFrame()) {
168 if (NumBytes != 0)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000169 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
170 MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000171 return;
172 }
173
174 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
175 unsigned Reg = CSI[i].getReg();
176 int FI = CSI[i].getFrameIdx();
177 switch (Reg) {
178 case ARM::R4:
179 case ARM::R5:
180 case ARM::R6:
181 case ARM::R7:
182 case ARM::LR:
183 if (Reg == FramePtr)
184 FramePtrSpillFI = FI;
185 AFI->addGPRCalleeSavedArea1Frame(FI);
186 GPRCS1Size += 4;
187 break;
188 case ARM::R8:
189 case ARM::R9:
190 case ARM::R10:
191 case ARM::R11:
192 if (Reg == FramePtr)
193 FramePtrSpillFI = FI;
Evan Chengafad0fe2012-01-04 01:55:04 +0000194 if (STI.isTargetIOS()) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000195 AFI->addGPRCalleeSavedArea2Frame(FI);
196 GPRCS2Size += 4;
197 } else {
198 AFI->addGPRCalleeSavedArea1Frame(FI);
199 GPRCS1Size += 4;
200 }
201 break;
202 default:
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000203 // This is a DPR. Exclude the aligned DPRCS2 spills.
204 if (Reg == ARM::D8)
205 D8SpillFI = FI;
206 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) {
207 AFI->addDPRCalleeSavedAreaFrame(FI);
208 DPRCSSize += 8;
209 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000210 }
211 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000212
Eric Christopher8b3ca622010-11-18 19:40:05 +0000213 // Move past area 1.
214 if (GPRCS1Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000215
Anton Korobeynikov33464912010-11-15 00:06:54 +0000216 // Set FP to point to the stack slot that contains the previous FP.
Evan Chengafad0fe2012-01-04 01:55:04 +0000217 // For iOS, FP is R7, which has now been stored in spill area 1.
218 // Otherwise, if this is not iOS, all the callee-saved registers go
Anton Korobeynikov33464912010-11-15 00:06:54 +0000219 // into spill area 1, including the FP in R11. In either case, it is
220 // now safe to emit this assignment.
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000221 bool HasFP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000222 if (HasFP) {
223 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
224 MachineInstrBuilder MIB =
225 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000226 .addFrameIndex(FramePtrSpillFI).addImm(0)
227 .setMIFlag(MachineInstr::FrameSetup);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000228 AddDefaultCC(AddDefaultPred(MIB));
229 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000230
Eric Christopher8b3ca622010-11-18 19:40:05 +0000231 // Move past area 2.
232 if (GPRCS2Size > 0) MBBI++;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000233
Anton Korobeynikov33464912010-11-15 00:06:54 +0000234 // Determine starting offsets of spill areas.
235 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
236 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
237 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
238 if (HasFP)
239 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
240 NumBytes);
241 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
242 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
243 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
244
Eric Christopher8b3ca622010-11-18 19:40:05 +0000245 // Move past area 3.
Evan Chengacca09b2011-02-25 00:24:46 +0000246 if (DPRCSSize > 0) {
247 MBBI++;
248 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Cheng9831f2d2011-02-25 01:29:29 +0000249 // instructions in the prologue.
Evan Chengacca09b2011-02-25 00:24:46 +0000250 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
251 MBBI++;
252 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000253
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000254 // Move past the aligned DPRCS2 area.
255 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
256 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
257 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
258 // leaves the stack pointer pointing to the DPRCS2 area.
259 //
260 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
261 NumBytes += MFI->getObjectOffset(D8SpillFI);
262 } else
263 NumBytes = DPRCSOffset;
264
Anton Korobeynikov33464912010-11-15 00:06:54 +0000265 if (NumBytes) {
266 // Adjust SP after all the callee-save spills.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000267 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
268 MachineInstr::FrameSetup);
Evan Chengab5c7032010-11-22 18:12:04 +0000269 if (HasFP && isARM)
270 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
271 // Note it's not safe to do this in Thumb2 mode because it would have
272 // taken two instructions:
273 // mov sp, r7
274 // sub sp, #24
275 // If an interrupt is taken between the two instructions, then sp is in
276 // an inconsistent state (pointing to the middle of callee-saved area).
277 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000278 AFI->setShouldRestoreSPFromFP(true);
279 }
280
Evan Chengab5c7032010-11-22 18:12:04 +0000281 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikov33464912010-11-15 00:06:54 +0000282 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
283 AFI->getFramePtrSpillOffset());
Anton Korobeynikov33464912010-11-15 00:06:54 +0000284
285 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
286 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
287 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
288
289 // If we need dynamic stack realignment, do it here. Be paranoid and make
290 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen43ea32c2011-12-24 04:17:01 +0000291 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000292 // realigned.
293 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000294 unsigned MaxAlign = MFI->getMaxAlignment();
295 assert (!AFI->isThumb1OnlyFunction());
296 if (!AFI->isThumbFunction()) {
297 // Emit bic sp, sp, MaxAlign
298 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
299 TII.get(ARM::BICri), ARM::SP)
300 .addReg(ARM::SP, RegState::Kill)
301 .addImm(MaxAlign-1)));
302 } else {
303 // We cannot use sp as source/dest register here, thus we're emitting the
304 // following sequence:
305 // mov r4, sp
306 // bic r4, r4, MaxAlign
307 // mov sp, r4
308 // FIXME: It will be better just to find spare register here.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000309 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000310 .addReg(ARM::SP, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000311 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
312 TII.get(ARM::t2BICri), ARM::R4)
313 .addReg(ARM::R4, RegState::Kill)
314 .addImm(MaxAlign-1)));
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000315 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000316 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000317 }
318
319 AFI->setShouldRestoreSPFromFP(true);
320 }
321
322 // If we need a base pointer, set it up here. It's whatever the value
323 // of the stack pointer is at this point. Any variable size objects
324 // will be allocated after this, so we can still use the base pointer
325 // to reference locals.
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000326 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikov33464912010-11-15 00:06:54 +0000327 if (RegInfo->hasBasePointer(MF)) {
328 if (isARM)
329 BuildMI(MBB, MBBI, dl,
330 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
331 .addReg(ARM::SP)
332 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
333 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000334 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000335 RegInfo->getBaseRegister())
336 .addReg(ARM::SP));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000337 }
338
339 // If the frame has variable sized objects then the epilogue must restore
Eric Christopher4dd312f2011-01-10 23:10:59 +0000340 // the sp from fp. We can assume there's an FP here since hasFP already
341 // checks for hasVarSizedObjects.
Evan Chengab5c7032010-11-22 18:12:04 +0000342 if (MFI->hasVarSizedObjects())
Anton Korobeynikov33464912010-11-15 00:06:54 +0000343 AFI->setShouldRestoreSPFromFP(true);
344}
345
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000346void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +0000347 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000348 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000349 assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
Anton Korobeynikov33464912010-11-15 00:06:54 +0000350 unsigned RetOpcode = MBBI->getOpcode();
351 DebugLoc dl = MBBI->getDebugLoc();
352 MachineFrameInfo *MFI = MF.getFrameInfo();
353 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
354 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
355 const ARMBaseInstrInfo &TII =
356 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
357 assert(!AFI->isThumb1OnlyFunction() &&
358 "This emitEpilogue does not support Thumb1!");
359 bool isARM = !AFI->isThumbFunction();
360
Stepan Dyatkovskiy083bc972013-05-20 08:01:34 +0000361 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
362 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000363 int NumBytes = (int)MFI->getStackSize();
364 unsigned FramePtr = RegInfo->getFrameRegister(MF);
365
Jakob Stoklund Olesenbe5ec8c2012-10-26 21:46:57 +0000366 // All calls are tail calls in GHC calling conv, and functions have no
367 // prologue/epilogue.
Eric Christophere94ac882012-08-03 00:05:53 +0000368 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
369 return;
370
Anton Korobeynikov33464912010-11-15 00:06:54 +0000371 if (!AFI->hasStackFrame()) {
372 if (NumBytes != 0)
373 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
374 } else {
375 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper015f2282012-03-04 03:33:22 +0000376 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000377 if (MBBI != MBB.begin()) {
378 do
379 --MBBI;
380 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
381 if (!isCSRestore(MBBI, TII, CSRegs))
382 ++MBBI;
383 }
384
385 // Move SP to start of FP callee save spill area.
386 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
387 AFI->getGPRCalleeSavedArea2Size() +
388 AFI->getDPRCalleeSavedAreaSize());
389
390 // Reset SP based on frame pointer only if the stack frame extends beyond
391 // frame pointer stack slot or target is ELF and the function has FP.
392 if (AFI->shouldRestoreSPFromFP()) {
393 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
394 if (NumBytes) {
395 if (isARM)
396 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
397 ARMCC::AL, 0, TII);
Evan Chengab5c7032010-11-22 18:12:04 +0000398 else {
399 // It's not possible to restore SP from FP in a single instruction.
Evan Chengafad0fe2012-01-04 01:55:04 +0000400 // For iOS, this looks like:
Evan Chengab5c7032010-11-22 18:12:04 +0000401 // mov sp, r7
402 // sub sp, #24
403 // This is bad, if an interrupt is taken after the mov, sp is in an
404 // inconsistent state.
405 // Use the first callee-saved register as a scratch register.
Kaelyn Uhrain61fac682012-10-26 23:28:41 +0000406 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
Evan Chengab5c7032010-11-22 18:12:04 +0000407 "No scratch register to restore SP from FP!");
408 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000409 ARMCC::AL, 0, TII);
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000410 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000411 ARM::SP)
412 .addReg(ARM::R4));
Evan Chengab5c7032010-11-22 18:12:04 +0000413 }
Anton Korobeynikov33464912010-11-15 00:06:54 +0000414 } else {
415 // Thumb2 or ARM.
416 if (isARM)
417 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
418 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
419 else
Jim Grosbach2a7b41b2011-06-30 23:38:17 +0000420 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbach63b46fa2011-06-30 22:10:46 +0000421 ARM::SP)
422 .addReg(FramePtr));
Anton Korobeynikov33464912010-11-15 00:06:54 +0000423 }
424 } else if (NumBytes)
425 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
426
Eric Christopher8b3ca622010-11-18 19:40:05 +0000427 // Increment past our save areas.
Evan Chengacca09b2011-02-25 00:24:46 +0000428 if (AFI->getDPRCalleeSavedAreaSize()) {
429 MBBI++;
430 // Since vpop register list cannot have gaps, there may be multiple vpop
431 // instructions in the epilogue.
432 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
433 MBBI++;
434 }
Eric Christopher8b3ca622010-11-18 19:40:05 +0000435 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
436 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000437 }
438
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000439 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000440 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000441 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000442 MachineOperand &JumpTarget = MBBI->getOperand(0);
443
444 // Jump to label or value in register.
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000445 if (RetOpcode == ARM::TCRETURNdi) {
446 unsigned TCOpcode = STI.isThumb() ?
447 (STI.isTargetIOS() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) :
448 ARM::TAILJMPd;
Evan Cheng3d2125c2010-11-30 23:55:39 +0000449 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
450 if (JumpTarget.isGlobal())
451 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
452 JumpTarget.getTargetFlags());
453 else {
454 assert(JumpTarget.isSymbol());
455 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
456 JumpTarget.getTargetFlags());
457 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000458
459 // Add the default predicate in Thumb mode.
460 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000461 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach5edf24e2011-03-15 00:30:40 +0000462 BuildMI(MBB, MBBI, dl,
463 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikov33464912010-11-15 00:06:54 +0000464 addReg(JumpTarget.getReg(), RegState::Kill);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000465 }
466
467 MachineInstr *NewMI = prior(MBBI);
468 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
469 NewMI->addOperand(MBBI->getOperand(i));
470
471 // Delete the pseudo instruction TCRETURN.
472 MBB.erase(MBBI);
Cameron Zwarichcd4e0b52011-06-17 02:16:43 +0000473 MBBI = NewMI;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000474 }
475
Stepan Dyatkovskiyf65e4932013-04-30 07:19:58 +0000476 if (ArgRegsSaveSize)
477 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000478}
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000479
Bob Wilson42257852011-01-13 21:10:12 +0000480/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
481/// debug info. It's the same as what we use for resolving the code-gen
482/// references for now. FIXME: This can go wrong when references are
483/// SP-relative and simple call frames aren't used.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000484int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000485ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson42257852011-01-13 21:10:12 +0000486 unsigned &FrameReg) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000487 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
488}
489
490int
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000491ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengdb6cbe12011-04-22 01:42:52 +0000492 int FI, unsigned &FrameReg,
Bob Wilson42257852011-01-13 21:10:12 +0000493 int SPAdj) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000494 const MachineFrameInfo *MFI = MF.getFrameInfo();
495 const ARMBaseRegisterInfo *RegInfo =
496 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
497 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
498 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
499 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
500 bool isFixed = MFI->isFixedObjectIndex(FI);
501
502 FrameReg = ARM::SP;
503 Offset += SPAdj;
504 if (AFI->isGPRCalleeSavedArea1Frame(FI))
505 return Offset - AFI->getGPRCalleeSavedArea1Offset();
506 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
507 return Offset - AFI->getGPRCalleeSavedArea2Offset();
508 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
509 return Offset - AFI->getDPRCalleeSavedAreaOffset();
510
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000511 // SP can move around if there are allocas. We may also lose track of SP
512 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilson055a8122012-03-20 19:28:22 +0000513 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000514
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000515 // When dynamically realigning the stack, use the frame pointer for
516 // parameters, and the stack/base pointer for locals.
517 if (RegInfo->needsStackRealignment(MF)) {
518 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
519 if (isFixed) {
520 FrameReg = RegInfo->getFrameRegister(MF);
521 Offset = FPOffset;
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000522 } else if (hasMovingSP) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000523 assert(RegInfo->hasBasePointer(MF) &&
524 "VLAs and dynamic stack alignment, but missing base pointer!");
525 FrameReg = RegInfo->getBaseRegister();
526 }
527 return Offset;
528 }
529
530 // If there is a frame pointer, use it when we can.
531 if (hasFP(MF) && AFI->hasStackFrame()) {
532 // Use frame pointer to reference fixed objects. Use it for locals if
533 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000534 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000535 FrameReg = RegInfo->getFrameRegister(MF);
536 return FPOffset;
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000537 } else if (hasMovingSP) {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000538 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000539 if (AFI->isThumb2Function()) {
Evan Chengdb6cbe12011-04-22 01:42:52 +0000540 // Try to use the frame pointer if we can, else use the base pointer
541 // since it's available. This is handy for the emergency spill slot, in
542 // particular.
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000543 if (FPOffset >= -255 && FPOffset < 0) {
544 FrameReg = RegInfo->getFrameRegister(MF);
545 return FPOffset;
546 }
Evan Chengdb6cbe12011-04-22 01:42:52 +0000547 }
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000548 } else if (AFI->isThumb2Function()) {
Andrew Trick51972da2011-08-25 17:40:54 +0000549 // Use add <rd>, sp, #<imm8>
Evan Chengdb6cbe12011-04-22 01:42:52 +0000550 // ldr <rd>, [sp, #<imm8>]
551 // if at all possible to save space.
552 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
553 return Offset;
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000554 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengdb6cbe12011-04-22 01:42:52 +0000555 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000556 if (FPOffset >= -255 && FPOffset < 0) {
557 FrameReg = RegInfo->getFrameRegister(MF);
558 return FPOffset;
559 }
560 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
561 // Otherwise, use SP or FP, whichever is closer to the stack slot.
562 FrameReg = RegInfo->getFrameRegister(MF);
563 return FPOffset;
564 }
565 }
566 // Use the base pointer if we have one.
567 if (RegInfo->hasBasePointer(MF))
568 FrameReg = RegInfo->getBaseRegister();
569 return Offset;
570}
571
Bob Wilson42257852011-01-13 21:10:12 +0000572int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
573 int FI) const {
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000574 unsigned FrameReg;
575 return getFrameIndexReference(MF, FI, FrameReg);
576}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000577
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000578void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000579 MachineBasicBlock::iterator MI,
580 const std::vector<CalleeSavedInfo> &CSI,
581 unsigned StmOpc, unsigned StrOpc,
582 bool NoGap,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000583 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000584 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000585 unsigned MIFlags) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000586 MachineFunction &MF = *MBB.getParent();
587 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
588
589 DebugLoc DL;
590 if (MI != MBB.end()) DL = MI->getDebugLoc();
591
Evan Cheng9801b5c2010-12-07 19:59:34 +0000592 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng06d65f52010-12-07 23:08:38 +0000593 unsigned i = CSI.size();
594 while (i != 0) {
595 unsigned LastReg = 0;
596 for (; i != 0; --i) {
597 unsigned Reg = CSI[i-1].getReg();
Evan Chengafad0fe2012-01-04 01:55:04 +0000598 if (!(Func)(Reg, STI.isTargetIOS())) continue;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000599
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000600 // D-registers in the aligned area DPRCS2 are NOT spilled here.
601 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
602 continue;
603
Evan Cheng06d65f52010-12-07 23:08:38 +0000604 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbach2a4f0982010-12-09 16:14:46 +0000605 // @llvm.returnaddress is called. If LR is returned for
606 // @llvm.returnaddress then it's already added to the function and
607 // entry block live-in sets.
Evan Cheng06d65f52010-12-07 23:08:38 +0000608 bool isKill = true;
609 if (Reg == ARM::LR) {
610 if (MF.getFrameInfo()->isReturnAddressTaken() &&
611 MF.getRegInfo().isLiveIn(Reg))
612 isKill = false;
613 }
614
615 if (isKill)
616 MBB.addLiveIn(Reg);
617
Eric Christopher1a48c032010-12-09 01:57:45 +0000618 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng275bf632010-12-08 06:29:02 +0000619 // for other instructions. e.g.
Eric Christopher1a48c032010-12-09 01:57:45 +0000620 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng275bf632010-12-08 06:29:02 +0000621 if (NoGap && LastReg && LastReg != Reg-1)
622 break;
Evan Cheng06d65f52010-12-07 23:08:38 +0000623 LastReg = Reg;
624 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000625 }
626
Jim Grosbachc6f92612010-12-09 18:31:13 +0000627 if (Regs.empty())
628 continue;
629 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000630 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000631 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000632 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng06d65f52010-12-07 23:08:38 +0000633 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
634 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbachc6f92612010-12-09 18:31:13 +0000635 } else if (Regs.size() == 1) {
636 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
637 ARM::SP)
638 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbach19dec202011-08-05 20:35:44 +0000639 .addReg(ARM::SP).setMIFlags(MIFlags)
640 .addImm(-4);
Jim Grosbachc6f92612010-12-09 18:31:13 +0000641 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000642 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000643 Regs.clear();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000644 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000645}
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000646
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000647void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000648 MachineBasicBlock::iterator MI,
649 const std::vector<CalleeSavedInfo> &CSI,
650 unsigned LdmOpc, unsigned LdrOpc,
651 bool isVarArg, bool NoGap,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000652 bool(*Func)(unsigned, bool),
653 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng06d65f52010-12-07 23:08:38 +0000654 MachineFunction &MF = *MBB.getParent();
655 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
656 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
657 DebugLoc DL = MI->getDebugLoc();
Evan Cheng7cfa6562011-01-25 01:28:33 +0000658 unsigned RetOpcode = MI->getOpcode();
659 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
Jakob Stoklund Olesenaa395e82012-04-06 21:17:42 +0000660 RetOpcode == ARM::TCRETURNri);
Evan Cheng06d65f52010-12-07 23:08:38 +0000661
662 SmallVector<unsigned, 4> Regs;
663 unsigned i = CSI.size();
664 while (i != 0) {
665 unsigned LastReg = 0;
666 bool DeleteRet = false;
667 for (; i != 0; --i) {
668 unsigned Reg = CSI[i-1].getReg();
Evan Chengafad0fe2012-01-04 01:55:04 +0000669 if (!(Func)(Reg, STI.isTargetIOS())) continue;
Evan Cheng06d65f52010-12-07 23:08:38 +0000670
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000671 // The aligned reloads from area DPRCS2 are not inserted here.
672 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
673 continue;
674
Evan Cheng7cfa6562011-01-25 01:28:33 +0000675 if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000676 Reg = ARM::PC;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000677 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng06d65f52010-12-07 23:08:38 +0000678 // Fold the return instruction into the LDM.
679 DeleteRet = true;
680 }
681
Evan Cheng275bf632010-12-08 06:29:02 +0000682 // If NoGap is true, pop consecutive registers and then leave the rest
683 // for other instructions. e.g.
684 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
685 if (NoGap && LastReg && LastReg != Reg-1)
686 break;
687
Evan Cheng06d65f52010-12-07 23:08:38 +0000688 LastReg = Reg;
689 Regs.push_back(Reg);
690 }
691
Jim Grosbachc6f92612010-12-09 18:31:13 +0000692 if (Regs.empty())
693 continue;
694 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng06d65f52010-12-07 23:08:38 +0000695 MachineInstrBuilder MIB =
Jim Grosbachc6f92612010-12-09 18:31:13 +0000696 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng06d65f52010-12-07 23:08:38 +0000697 .addReg(ARM::SP));
698 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
699 MIB.addReg(Regs[i], getDefRegState(true));
Andrew Trickb9ca5122011-08-25 17:50:53 +0000700 if (DeleteRet) {
Jakob Stoklund Olesenbe06aac2012-12-20 22:54:02 +0000701 MIB.copyImplicitOps(&*MI);
Evan Cheng06d65f52010-12-07 23:08:38 +0000702 MI->eraseFromParent();
Andrew Trickb9ca5122011-08-25 17:50:53 +0000703 }
Evan Cheng06d65f52010-12-07 23:08:38 +0000704 MI = MIB;
Jim Grosbachc6f92612010-12-09 18:31:13 +0000705 } else if (Regs.size() == 1) {
706 // If we adjusted the reg to PC from LR above, switch it back here. We
707 // only do that for LDM.
708 if (Regs[0] == ARM::PC)
709 Regs[0] = ARM::LR;
710 MachineInstrBuilder MIB =
711 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
712 .addReg(ARM::SP, RegState::Define)
713 .addReg(ARM::SP);
714 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
715 // that refactoring is complete (eventually).
Owen Anderson793e7962011-07-26 20:54:26 +0000716 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbachc6f92612010-12-09 18:31:13 +0000717 MIB.addReg(0);
718 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
719 } else
720 MIB.addImm(4);
721 AddDefaultPred(MIB);
Evan Cheng06d65f52010-12-07 23:08:38 +0000722 }
Jim Grosbachc6f92612010-12-09 18:31:13 +0000723 Regs.clear();
Evan Cheng9801b5c2010-12-07 19:59:34 +0000724 }
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000725}
726
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000727/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen43ea32c2011-12-24 04:17:01 +0000728/// starting from d8. Also insert stack realignment code and leave the stack
729/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000730static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
731 MachineBasicBlock::iterator MI,
732 unsigned NumAlignedDPRCS2Regs,
733 const std::vector<CalleeSavedInfo> &CSI,
734 const TargetRegisterInfo *TRI) {
735 MachineFunction &MF = *MBB.getParent();
736 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
737 DebugLoc DL = MI->getDebugLoc();
738 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
739 MachineFrameInfo &MFI = *MF.getFrameInfo();
740
741 // Mark the D-register spill slots as properly aligned. Since MFI computes
742 // stack slot layout backwards, this can actually mean that the d-reg stack
743 // slot offsets can be wrong. The offset for d8 will always be correct.
744 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
745 unsigned DNum = CSI[i].getReg() - ARM::D8;
746 if (DNum >= 8)
747 continue;
748 int FI = CSI[i].getFrameIdx();
749 // The even-numbered registers will be 16-byte aligned, the odd-numbered
750 // registers will be 8-byte aligned.
751 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
752
753 // The stack slot for D8 needs to be maximally aligned because this is
754 // actually the point where we align the stack pointer. MachineFrameInfo
755 // computes all offsets relative to the incoming stack pointer which is a
756 // bit weird when realigning the stack. Any extra padding for this
757 // over-alignment is not realized because the code inserted below adjusts
758 // the stack pointer by numregs * 8 before aligning the stack pointer.
759 if (DNum == 0)
760 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
761 }
762
763 // Move the stack pointer to the d8 spill slot, and align it at the same
764 // time. Leave the stack slot address in the scratch register r4.
765 //
766 // sub r4, sp, #numregs * 8
767 // bic r4, r4, #align - 1
768 // mov sp, r4
769 //
770 bool isThumb = AFI->isThumbFunction();
771 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
772 AFI->setShouldRestoreSPFromFP(true);
773
774 // sub r4, sp, #numregs * 8
775 // The immediate is <= 64, so it doesn't need any special encoding.
776 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
777 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
778 .addReg(ARM::SP)
779 .addImm(8 * NumAlignedDPRCS2Regs)));
780
781 // bic r4, r4, #align-1
782 Opc = isThumb ? ARM::t2BICri : ARM::BICri;
783 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
784 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
785 .addReg(ARM::R4, RegState::Kill)
786 .addImm(MaxAlign - 1)));
787
788 // mov sp, r4
789 // The stack pointer must be adjusted before spilling anything, otherwise
790 // the stack slots could be clobbered by an interrupt handler.
791 // Leave r4 live, it is used below.
792 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
793 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
794 .addReg(ARM::R4);
795 MIB = AddDefaultPred(MIB);
796 if (!isThumb)
797 AddDefaultCC(MIB);
798
799 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
800 // r4 holds the stack slot address.
801 unsigned NextReg = ARM::D8;
802
803 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
804 // The writeback is only needed when emitting two vst1.64 instructions.
805 if (NumAlignedDPRCS2Regs >= 6) {
806 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000807 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000808 MBB.addLiveIn(SupReg);
809 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
810 ARM::R4)
811 .addReg(ARM::R4, RegState::Kill).addImm(16)
812 .addReg(NextReg)
813 .addReg(SupReg, RegState::ImplicitKill));
814 NextReg += 4;
815 NumAlignedDPRCS2Regs -= 4;
816 }
817
818 // We won't modify r4 beyond this point. It currently points to the next
819 // register to be spilled.
820 unsigned R4BaseReg = NextReg;
821
822 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
823 if (NumAlignedDPRCS2Regs >= 4) {
824 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000825 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000826 MBB.addLiveIn(SupReg);
827 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
828 .addReg(ARM::R4).addImm(16).addReg(NextReg)
829 .addReg(SupReg, RegState::ImplicitKill));
830 NextReg += 4;
831 NumAlignedDPRCS2Regs -= 4;
832 }
833
834 // 16-byte aligned vst1.64 with 2 d-regs.
835 if (NumAlignedDPRCS2Regs >= 2) {
836 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000837 &ARM::QPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000838 MBB.addLiveIn(SupReg);
839 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbach28f08c92012-03-05 19:33:30 +0000840 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000841 NextReg += 2;
842 NumAlignedDPRCS2Regs -= 2;
843 }
844
845 // Finally, use a vanilla vstr.64 for the odd last register.
846 if (NumAlignedDPRCS2Regs) {
847 MBB.addLiveIn(NextReg);
848 // vstr.64 uses addrmode5 which has an offset scale of 4.
849 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
850 .addReg(NextReg)
851 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
852 }
853
854 // The last spill instruction inserted should kill the scratch register r4.
855 llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI);
856}
857
858/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
859/// iterator to the following instruction.
860static MachineBasicBlock::iterator
861skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
862 unsigned NumAlignedDPRCS2Regs) {
863 // sub r4, sp, #numregs * 8
864 // bic r4, r4, #align - 1
865 // mov sp, r4
866 ++MI; ++MI; ++MI;
867 assert(MI->mayStore() && "Expecting spill instruction");
868
869 // These switches all fall through.
870 switch(NumAlignedDPRCS2Regs) {
871 case 7:
872 ++MI;
873 assert(MI->mayStore() && "Expecting spill instruction");
874 default:
875 ++MI;
876 assert(MI->mayStore() && "Expecting spill instruction");
877 case 1:
878 case 2:
879 case 4:
880 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
881 ++MI;
882 }
883 return MI;
884}
885
886/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
887/// starting from d8. These instructions are assumed to execute while the
888/// stack is still aligned, unlike the code inserted by emitPopInst.
889static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
890 MachineBasicBlock::iterator MI,
891 unsigned NumAlignedDPRCS2Regs,
892 const std::vector<CalleeSavedInfo> &CSI,
893 const TargetRegisterInfo *TRI) {
894 MachineFunction &MF = *MBB.getParent();
895 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
896 DebugLoc DL = MI->getDebugLoc();
897 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
898
899 // Find the frame index assigned to d8.
900 int D8SpillFI = 0;
901 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
902 if (CSI[i].getReg() == ARM::D8) {
903 D8SpillFI = CSI[i].getFrameIdx();
904 break;
905 }
906
907 // Materialize the address of the d8 spill slot into the scratch register r4.
908 // This can be fairly complicated if the stack frame is large, so just use
909 // the normal frame index elimination mechanism to do it. This code runs as
910 // the initial part of the epilog where the stack and base pointers haven't
911 // been changed yet.
912 bool isThumb = AFI->isThumbFunction();
913 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
914
915 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
916 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
917 .addFrameIndex(D8SpillFI).addImm(0)));
918
919 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
920 unsigned NextReg = ARM::D8;
921
922 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
923 if (NumAlignedDPRCS2Regs >= 6) {
924 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000925 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000926 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
927 .addReg(ARM::R4, RegState::Define)
928 .addReg(ARM::R4, RegState::Kill).addImm(16)
929 .addReg(SupReg, RegState::ImplicitDefine));
930 NextReg += 4;
931 NumAlignedDPRCS2Regs -= 4;
932 }
933
934 // We won't modify r4 beyond this point. It currently points to the next
935 // register to be spilled.
936 unsigned R4BaseReg = NextReg;
937
938 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
939 if (NumAlignedDPRCS2Regs >= 4) {
940 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000941 &ARM::QQPRRegClass);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000942 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
943 .addReg(ARM::R4).addImm(16)
944 .addReg(SupReg, RegState::ImplicitDefine));
945 NextReg += 4;
946 NumAlignedDPRCS2Regs -= 4;
947 }
948
949 // 16-byte aligned vld1.64 with 2 d-regs.
950 if (NumAlignedDPRCS2Regs >= 2) {
951 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topper420761a2012-04-20 07:30:17 +0000952 &ARM::QPRRegClass);
Jim Grosbach28f08c92012-03-05 19:33:30 +0000953 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
954 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000955 NextReg += 2;
956 NumAlignedDPRCS2Regs -= 2;
957 }
958
959 // Finally, use a vanilla vldr.64 for the remaining odd register.
960 if (NumAlignedDPRCS2Regs)
961 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
962 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
963
964 // Last store kills r4.
965 llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI);
966}
967
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000968bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +0000969 MachineBasicBlock::iterator MI,
970 const std::vector<CalleeSavedInfo> &CSI,
971 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000972 if (CSI.empty())
973 return false;
974
975 MachineFunction &MF = *MBB.getParent();
976 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000977
978 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000979 unsigned PushOneOpc = AFI->isThumbFunction() ?
980 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000981 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000982 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
983 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000984 MachineInstr::FrameSetup);
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000985 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikov57caad72011-03-05 18:43:32 +0000986 MachineInstr::FrameSetup);
987 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +0000988 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
989
990 // The code above does not insert spill code for the aligned DPRCS2 registers.
991 // The stack realignment code will be inserted between the push instructions
992 // and these spills.
993 if (NumAlignedDPRCS2Regs)
994 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +0000995
996 return true;
997}
998
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000999bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson42257852011-01-13 21:10:12 +00001000 MachineBasicBlock::iterator MI,
1001 const std::vector<CalleeSavedInfo> &CSI,
1002 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001003 if (CSI.empty())
1004 return false;
1005
1006 MachineFunction &MF = *MBB.getParent();
1007 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf65e4932013-04-30 07:19:58 +00001008 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001009 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1010
1011 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1012 // registers. Do that here instead.
1013 if (NumAlignedDPRCS2Regs)
1014 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001015
1016 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach8e0c7692011-09-02 18:46:15 +00001017 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001018 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001019 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1020 NumAlignedDPRCS2Regs);
Jim Grosbachc6f92612010-12-09 18:31:13 +00001021 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001022 &isARMArea2Register, 0);
Jim Grosbachc6f92612010-12-09 18:31:13 +00001023 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001024 &isARMArea1Register, 0);
Anton Korobeynikovcd775ce2010-11-27 23:05:03 +00001025
1026 return true;
1027}
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001028
1029// FIXME: Make generic?
1030static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1031 const ARMBaseInstrInfo &TII) {
1032 unsigned FnSize = 0;
1033 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
1034 MBBI != E; ++MBBI) {
1035 const MachineBasicBlock &MBB = *MBBI;
1036 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
1037 I != E; ++I)
1038 FnSize += TII.GetInstSizeInBytes(I);
1039 }
1040 return FnSize;
1041}
1042
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001043/// estimateRSStackSizeLimit - Look at each instruction that references stack
1044/// frames and return the stack size limit beyond which some of these
1045/// instructions will require a scratch register during their expansion later.
1046// FIXME: Move to TII?
1047static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001048 const TargetFrameLowering *TFI) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001049 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1050 unsigned Limit = (1 << 12) - 1;
1051 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
1052 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1053 I != E; ++I) {
1054 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1055 if (!I->getOperand(i).isFI()) continue;
1056
1057 // When using ADDri to get the address of a stack object, 255 is the
1058 // largest offset guaranteed to fit in the immediate offset.
1059 if (I->getOpcode() == ARM::ADDri) {
1060 Limit = std::min(Limit, (1U << 8) - 1);
1061 break;
1062 }
1063
1064 // Otherwise check the addressing mode.
1065 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
1066 case ARMII::AddrMode3:
1067 case ARMII::AddrModeT2_i8:
1068 Limit = std::min(Limit, (1U << 8) - 1);
1069 break;
1070 case ARMII::AddrMode5:
1071 case ARMII::AddrModeT2_i8s4:
1072 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1073 break;
1074 case ARMII::AddrModeT2_i12:
1075 // i12 supports only positive offset so these will be converted to
1076 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1077 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1078 Limit = std::min(Limit, (1U << 8) - 1);
1079 break;
1080 case ARMII::AddrMode4:
1081 case ARMII::AddrMode6:
1082 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1083 // immediate offset for stack references.
1084 return 0;
1085 default:
1086 break;
1087 }
1088 break; // At most one FI per instruction
1089 }
1090 }
1091 }
1092
1093 return Limit;
1094}
1095
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001096// In functions that realign the stack, it can be an advantage to spill the
1097// callee-saved vector registers after realigning the stack. The vst1 and vld1
1098// instructions take alignment hints that can improve performance.
1099//
1100static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) {
1101 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1102 if (!SpillAlignedNEONRegs)
1103 return;
1104
1105 // Naked functions don't spill callee-saved registers.
Bill Wendling831737d2012-12-30 10:32:01 +00001106 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1107 Attribute::Naked))
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001108 return;
1109
1110 // We are planning to use NEON instructions vst1 / vld1.
1111 if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON())
1112 return;
1113
1114 // Don't bother if the default stack alignment is sufficiently high.
1115 if (MF.getTarget().getFrameLowering()->getStackAlignment() >= 8)
1116 return;
1117
1118 // Aligned spills require stack realignment.
1119 const ARMBaseRegisterInfo *RegInfo =
1120 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1121 if (!RegInfo->canRealignStack(MF))
1122 return;
1123
1124 // We always spill contiguous d-registers starting from d8. Count how many
1125 // needs spilling. The register allocator will almost always use the
1126 // callee-saved registers in order, but it can happen that there are holes in
1127 // the range. Registers above the hole will be spilled to the standard DPRCS
1128 // area.
1129 MachineRegisterInfo &MRI = MF.getRegInfo();
1130 unsigned NumSpills = 0;
1131 for (; NumSpills < 8; ++NumSpills)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +00001132 if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills))
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001133 break;
1134
1135 // Don't do this for just one d-register. It's not worth it.
1136 if (NumSpills < 2)
1137 return;
1138
1139 // Spill the first NumSpills D-registers after realigning the stack.
1140 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1141
1142 // A scratch register is required for the vst1 / vld1 instructions.
1143 MF.getRegInfo().setPhysRegUsed(ARM::R4);
1144}
1145
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001146void
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001147ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson42257852011-01-13 21:10:12 +00001148 RegScavenger *RS) const {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001149 // This tells PEI to spill the FP as if it is any other callee-save register
1150 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1151 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1152 // to combine multiple loads / stores.
1153 bool CanEliminateFrame = true;
1154 bool CS1Spilled = false;
1155 bool LRSpilled = false;
1156 unsigned NumGPRSpills = 0;
1157 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1158 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1159 const ARMBaseRegisterInfo *RegInfo =
1160 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1161 const ARMBaseInstrInfo &TII =
1162 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
1163 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1164 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001165 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001166 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1167
1168 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1169 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Chengdf55fea2011-01-16 05:14:33 +00001170 // since it's not always possible to restore sp from fp in a single
1171 // instruction.
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001172 // FIXME: It will be better just to find spare register here.
1173 if (AFI->isThumb2Function() &&
1174 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001175 MRI.setPhysRegUsed(ARM::R4);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001176
Evan Chengdf55fea2011-01-16 05:14:33 +00001177 if (AFI->isThumb1OnlyFunction()) {
1178 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf65e4932013-04-30 07:19:58 +00001179 if (AFI->getArgRegsSaveSize() > 0)
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001180 MRI.setPhysRegUsed(ARM::LR);
Evan Chengdf55fea2011-01-16 05:14:33 +00001181
Jim Grosbach7980f612011-06-13 21:18:25 +00001182 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1183 // for sure what the stack size will be, but for this, an estimate is good
1184 // enough. If there anything changes it, it'll be a spill, which implies
1185 // we've used all the registers and so R4 is already used, so not marking
Chad Rosier6690bca2011-10-20 00:07:12 +00001186 // it here will be OK.
Evan Chengdf55fea2011-01-16 05:14:33 +00001187 // FIXME: It will be better just to find spare register here.
Hal Finkel0cc52c62013-03-14 21:15:20 +00001188 unsigned StackSize = MFI->estimateStackSize(MF);
Chad Rosier6690bca2011-10-20 00:07:12 +00001189 if (MFI->hasVarSizedObjects() || StackSize > 508)
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001190 MRI.setPhysRegUsed(ARM::R4);
Evan Chengdf55fea2011-01-16 05:14:33 +00001191 }
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001192
Jakob Stoklund Olesenf06f6f52011-12-23 00:36:18 +00001193 // See if we can spill vector registers to aligned stack.
1194 checkNumAlignedDPRCS2Regs(MF);
1195
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001196 // Spill the BasePtr if it's used.
1197 if (RegInfo->hasBasePointer(MF))
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001198 MRI.setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001199
1200 // Don't spill FP if the frame can be eliminated. This is determined
1201 // by scanning the callee-save registers to see if any is used.
Craig Topper015f2282012-03-04 03:33:22 +00001202 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001203 for (unsigned i = 0; CSRegs[i]; ++i) {
1204 unsigned Reg = CSRegs[i];
1205 bool Spilled = false;
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001206 if (MRI.isPhysRegUsed(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001207 Spilled = true;
1208 CanEliminateFrame = false;
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001209 }
1210
Craig Topper420761a2012-04-20 07:30:17 +00001211 if (!ARM::GPRRegClass.contains(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001212 continue;
1213
1214 if (Spilled) {
1215 NumGPRSpills++;
1216
Evan Chengafad0fe2012-01-04 01:55:04 +00001217 if (!STI.isTargetIOS()) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001218 if (Reg == ARM::LR)
1219 LRSpilled = true;
1220 CS1Spilled = true;
1221 continue;
1222 }
1223
1224 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1225 switch (Reg) {
1226 case ARM::LR:
1227 LRSpilled = true;
1228 // Fallthrough
1229 case ARM::R4: case ARM::R5:
1230 case ARM::R6: case ARM::R7:
1231 CS1Spilled = true;
1232 break;
1233 default:
1234 break;
1235 }
1236 } else {
Evan Chengafad0fe2012-01-04 01:55:04 +00001237 if (!STI.isTargetIOS()) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001238 UnspilledCS1GPRs.push_back(Reg);
1239 continue;
1240 }
1241
1242 switch (Reg) {
1243 case ARM::R4: case ARM::R5:
1244 case ARM::R6: case ARM::R7:
1245 case ARM::LR:
1246 UnspilledCS1GPRs.push_back(Reg);
1247 break;
1248 default:
1249 UnspilledCS2GPRs.push_back(Reg);
1250 break;
1251 }
1252 }
1253 }
1254
1255 bool ForceLRSpill = false;
1256 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1257 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1258 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1259 // use of BL to implement far jump. If it turns out that it's not needed
1260 // then the branch fix up path will undo it.
1261 if (FnSize >= (1 << 11)) {
1262 CanEliminateFrame = false;
1263 ForceLRSpill = true;
1264 }
1265 }
1266
1267 // If any of the stack slot references may be out of range of an immediate
1268 // offset, make sure a register (or a spill slot) is available for the
1269 // register scavenger. Note that if we're indexing off the frame pointer, the
1270 // effective stack size is 4 bytes larger since the FP points to the stack
1271 // slot of the previous FP. Also, if we have variable sized objects in the
1272 // function, stack slot references will often be negative, and some of
1273 // our instructions are positive-offset only, so conservatively consider
1274 // that case to want a spill slot (or register) as well. Similarly, if
1275 // the function adjusts the stack pointer during execution and the
1276 // adjustments aren't already part of our stack size estimate, our offset
1277 // calculations may be off, so be conservative.
1278 // FIXME: We could add logic to be more precise about negative offsets
1279 // and which instructions will need a scratch register for them. Is it
1280 // worth the effort and added fragility?
1281 bool BigStack =
1282 (RS &&
Hal Finkel0cc52c62013-03-14 21:15:20 +00001283 (MFI->estimateStackSize(MF) +
1284 ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001285 estimateRSStackSizeLimit(MF, this)))
1286 || MFI->hasVarSizedObjects()
1287 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
1288
1289 bool ExtraCSSpill = false;
1290 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1291 AFI->setHasStackFrame(true);
1292
1293 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1294 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1295 if (!LRSpilled && CS1Spilled) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001296 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001297 NumGPRSpills++;
1298 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1299 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1300 ForceLRSpill = false;
1301 ExtraCSSpill = true;
1302 }
1303
1304 if (hasFP(MF)) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001305 MRI.setPhysRegUsed(FramePtr);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001306 NumGPRSpills++;
1307 }
1308
1309 // If stack and double are 8-byte aligned and we are spilling an odd number
1310 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1311 // the integer and double callee save areas.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001312 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001313 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1314 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1315 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1316 unsigned Reg = UnspilledCS1GPRs[i];
1317 // Don't spill high register if the function is thumb1
1318 if (!AFI->isThumb1OnlyFunction() ||
1319 isARMLowRegister(Reg) || Reg == ARM::LR) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001320 MRI.setPhysRegUsed(Reg);
1321 if (!MRI.isReserved(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001322 ExtraCSSpill = true;
1323 break;
1324 }
1325 }
1326 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1327 unsigned Reg = UnspilledCS2GPRs.front();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001328 MRI.setPhysRegUsed(Reg);
1329 if (!MRI.isReserved(Reg))
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001330 ExtraCSSpill = true;
1331 }
1332 }
1333
1334 // Estimate if we might need to scavenge a register at some point in order
1335 // to materialize a stack offset. If so, either spill one additional
1336 // callee-saved register or reserve a special spill slot to facilitate
1337 // register scavenging. Thumb1 needs a spill slot for stack pointer
1338 // adjustments also, even when the frame itself is small.
1339 if (BigStack && !ExtraCSSpill) {
1340 // If any non-reserved CS register isn't spilled, just spill one or two
1341 // extra. That should take care of it!
1342 unsigned NumExtras = TargetAlign / 4;
1343 SmallVector<unsigned, 2> Extras;
1344 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1345 unsigned Reg = UnspilledCS1GPRs.back();
1346 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001347 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001348 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1349 Reg == ARM::LR)) {
1350 Extras.push_back(Reg);
1351 NumExtras--;
1352 }
1353 }
1354 // For non-Thumb1 functions, also check for hi-reg CS registers
1355 if (!AFI->isThumb1OnlyFunction()) {
1356 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1357 unsigned Reg = UnspilledCS2GPRs.back();
1358 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001359 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001360 Extras.push_back(Reg);
1361 NumExtras--;
1362 }
1363 }
1364 }
1365 if (Extras.size() && NumExtras == 0) {
1366 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001367 MRI.setPhysRegUsed(Extras[i]);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001368 }
1369 } else if (!AFI->isThumb1OnlyFunction()) {
1370 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1371 // closest to SP or frame pointer.
Craig Topper420761a2012-04-20 07:30:17 +00001372 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Hal Finkeldc3beb92013-03-22 23:32:27 +00001373 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001374 RC->getAlignment(),
1375 false));
1376 }
1377 }
1378 }
1379
1380 if (ForceLRSpill) {
Jakob Stoklund Olesenb1f994a2012-10-26 21:43:05 +00001381 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov94c5ae02010-11-27 23:05:25 +00001382 AFI->setLRIsSpilledForFarJump(true);
1383 }
1384}
Eli Bendersky700ed802013-02-21 20:05:00 +00001385
1386
1387void ARMFrameLowering::
1388eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1389 MachineBasicBlock::iterator I) const {
1390 const ARMBaseInstrInfo &TII =
1391 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
1392 if (!hasReservedCallFrame(MF)) {
1393 // If we have alloca, convert as follows:
1394 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1395 // ADJCALLSTACKUP -> add, sp, sp, amount
1396 MachineInstr *Old = I;
1397 DebugLoc dl = Old->getDebugLoc();
1398 unsigned Amount = Old->getOperand(0).getImm();
1399 if (Amount != 0) {
1400 // We need to keep the stack aligned properly. To do this, we round the
1401 // amount of space needed for the outgoing arguments up to the next
1402 // alignment boundary.
1403 unsigned Align = getStackAlignment();
1404 Amount = (Amount+Align-1)/Align*Align;
1405
1406 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1407 assert(!AFI->isThumb1OnlyFunction() &&
1408 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1409 bool isARM = !AFI->isThumbFunction();
1410
1411 // Replace the pseudo instruction with a new instruction...
1412 unsigned Opc = Old->getOpcode();
1413 int PIdx = Old->findFirstPredOperandIdx();
1414 ARMCC::CondCodes Pred = (PIdx == -1)
1415 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1416 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1417 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1418 unsigned PredReg = Old->getOperand(2).getReg();
1419 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1420 Pred, PredReg);
1421 } else {
1422 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1423 unsigned PredReg = Old->getOperand(3).getReg();
1424 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1425 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1426 Pred, PredReg);
1427 }
1428 }
1429 }
1430 MBB.erase(I);
1431}
1432