blob: 340f49ffeadcd225b6a6c63c350168c4e93a9413 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===//
Anton Korobeynikovf7183ed2010-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 Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the ARM implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "ARMFrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "ARMBaseInstrInfo.h"
Evan Chenge45d6852011-01-11 21:46:47 +000016#include "ARMBaseRegisterInfo.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000018#include "MCTargetDesc/ARMAddressingModes.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000024#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/CallingConv.h"
26#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000027#include "llvm/MC/MCContext.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000028#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000030
31using namespace llvm;
32
Benjamin Kramer9fceb902012-02-24 22:09:25 +000033static cl::opt<bool>
Jakob Stoklund Olesen68a922c2012-01-06 22:19:37 +000034SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000035 cl::desc("Align ARM NEON spills in prolog and epilog"));
36
37static MachineBasicBlock::iterator
38skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
39 unsigned NumAlignedDPRCS2Regs);
40
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000041/// hasFP - Return true if the specified function should have a dedicated frame
42/// pointer register. This is true if the function has variable sized allocas
43/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000044bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000045 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
46
Evan Cheng801d98b2012-01-04 01:55:04 +000047 // iOS requires FP not to be clobbered for backtracing purpose.
48 if (STI.isTargetIOS())
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000049 return true;
50
51 const MachineFrameInfo *MFI = MF.getFrameInfo();
52 // Always eliminate non-leaf frame pointers.
Nick Lewycky50f02cb2011-12-02 22:16:29 +000053 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
54 MFI->hasCalls()) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000055 RegInfo->needsStackRealignment(MF) ||
56 MFI->hasVarSizedObjects() ||
57 MFI->isFrameAddressTaken());
58}
59
Bob Wilson657f2272011-01-13 21:10:12 +000060/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
61/// not required, we reserve argument space for call sites in the function
62/// immediately on entry to the current function. This eliminates the need for
63/// add/sub sp brackets around call sites. Returns true if the call frame is
64/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000065bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000066 const MachineFrameInfo *FFI = MF.getFrameInfo();
67 unsigned CFSize = FFI->getMaxCallFrameSize();
68 // It's not always a good idea to include the call frame as part of the
69 // stack frame. ARM (especially Thumb) has small immediate offset to
70 // address the stack frame. So a large call frame can cause poor codegen
71 // and may even makes it impossible to scavenge a register.
72 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
73 return false;
74
75 return !MF.getFrameInfo()->hasVarSizedObjects();
76}
77
Bob Wilson657f2272011-01-13 21:10:12 +000078/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
79/// call frame pseudos can be simplified. Unlike most targets, having a FP
80/// is not sufficient here since we still may reference some objects via SP
81/// even when FP is available in Thumb2 mode.
82bool
83ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000084 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
85}
86
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000087static bool isCSRestore(MachineInstr *MI,
88 const ARMBaseInstrInfo &TII,
Craig Topper420525c2012-03-04 03:33:22 +000089 const uint16_t *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +000090 // Integer spill area is handled with "pop".
Tim Northover93bcc662013-11-08 17:18:07 +000091 if (isPopOpcode(MI->getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +000092 // The first two operands are predicates. The last two are
93 // imp-def and imp-use of SP. Check everything in between.
94 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
95 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
96 return false;
97 return true;
98 }
Owen Anderson2aedba62011-07-26 20:54:26 +000099 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
100 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000101 MI->getOpcode() == ARM::t2LDR_POST) &&
102 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
103 MI->getOperand(1).getReg() == ARM::SP)
104 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000105
106 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000107}
108
Tim Northoverc9432eb2013-11-04 23:04:15 +0000109static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB,
110 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
111 const ARMBaseInstrInfo &TII, unsigned DestReg,
112 unsigned SrcReg, int NumBytes,
113 unsigned MIFlags = MachineInstr::NoFlags,
114 ARMCC::CondCodes Pred = ARMCC::AL,
115 unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000116 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000117 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000118 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000119 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000120 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000121 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000122}
123
Tim Northoverc9432eb2013-11-04 23:04:15 +0000124static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
126 const ARMBaseInstrInfo &TII, int NumBytes,
127 unsigned MIFlags = MachineInstr::NoFlags,
128 ARMCC::CondCodes Pred = ARMCC::AL,
129 unsigned PredReg = 0) {
130 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
131 MIFlags, Pred, PredReg);
132}
133
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000134static int sizeOfSPAdjustment(const MachineInstr *MI) {
135 assert(MI->getOpcode() == ARM::VSTMDDB_UPD);
136 int count = 0;
137 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
138 // pred) so the list starts at 4.
139 for (int i = MI->getNumOperands() - 1; i >= 4; --i)
140 count += 8;
141 return count;
142}
143
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000144void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000145 MachineBasicBlock &MBB = MF.front();
146 MachineBasicBlock::iterator MBBI = MBB.begin();
147 MachineFrameInfo *MFI = MF.getFrameInfo();
148 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000149 MachineModuleInfo &MMI = MF.getMMI();
150 MCContext &Context = MMI.getContext();
151 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000152 const ARMBaseRegisterInfo *RegInfo =
153 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
154 const ARMBaseInstrInfo &TII =
155 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
156 assert(!AFI->isThumb1OnlyFunction() &&
157 "This emitPrologue does not support Thumb1!");
158 bool isARM = !AFI->isThumbFunction();
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000159 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
160 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000161 unsigned NumBytes = MFI->getStackSize();
162 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
163 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
164 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000165 int CFAOffset = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000166
167 // Determine the sizes of each callee-save spill areas and record which frame
168 // belongs to which callee-save spill areas.
169 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
170 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000171 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000172
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000173 // All calls are tail calls in GHC calling conv, and functions have no
174 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000175 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
176 return;
177
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000178 // Allocate the vararg register save area. This is not counted in NumBytes.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000179 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000180 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000181 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000182 MCSymbol *SPLabel = Context.CreateTempSymbol();
183 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
184 .addSym(SPLabel);
185 CFAOffset -= ArgRegsSaveSize;
186 MMI.addFrameInst(
187 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
188 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000189
190 if (!AFI->hasStackFrame()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000191 if (NumBytes != 0) {
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000192 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
193 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000194 MCSymbol *SPLabel = Context.CreateTempSymbol();
195 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
196 .addSym(SPLabel);
197 CFAOffset -= NumBytes;
198 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(SPLabel,
199 CFAOffset));
200 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000201 return;
202 }
203
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000204 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000205 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
206 unsigned Reg = CSI[i].getReg();
207 int FI = CSI[i].getFrameIdx();
208 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000209 case ARM::R8:
210 case ARM::R9:
211 case ARM::R10:
212 case ARM::R11:
213 case ARM::R12:
214 if (STI.isTargetMachO()) {
215 GPRCS2Size += 4;
216 break;
217 }
218 // fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +0000219 case ARM::R0:
220 case ARM::R1:
221 case ARM::R2:
222 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000223 case ARM::R4:
224 case ARM::R5:
225 case ARM::R6:
226 case ARM::R7:
227 case ARM::LR:
228 if (Reg == FramePtr)
229 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000230 GPRCS1Size += 4;
231 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000232 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000233 // This is a DPR. Exclude the aligned DPRCS2 spills.
234 if (Reg == ARM::D8)
235 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000236 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000237 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000238 }
239 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000240
Eric Christopherb006fc92010-11-18 19:40:05 +0000241 // Move past area 1.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000242 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push,
243 DPRCSPush;
Tim Northover93bcc662013-11-08 17:18:07 +0000244 if (GPRCS1Size > 0)
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000245 GPRCS1Push = LastPush = MBBI++;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000246
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000247 // Determine starting offsets of spill areas.
Tim Northoverc9432eb2013-11-04 23:04:15 +0000248 bool HasFP = hasFP(MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000249 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
250 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
251 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
Tim Northover93bcc662013-11-08 17:18:07 +0000252 int FramePtrOffsetInPush = 0;
253 if (HasFP) {
254 FramePtrOffsetInPush = MFI->getObjectOffset(FramePtrSpillFI) + GPRCS1Size;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000255 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
256 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000257 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000258 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
259 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
260 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
261
Tim Northoverc9432eb2013-11-04 23:04:15 +0000262 // Move past area 2.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000263 if (GPRCS2Size > 0)
264 GPRCS2Push = LastPush = MBBI++;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000265
Eric Christopherb006fc92010-11-18 19:40:05 +0000266 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000267 if (DPRCSSize > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000268 DPRCSPush = MBBI;
Evan Cheng70d29632011-02-25 00:24:46 +0000269 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000270 // instructions in the prologue.
Evan Cheng70d29632011-02-25 00:24:46 +0000271 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
Tim Northover93bcc662013-11-08 17:18:07 +0000272 LastPush = MBBI++;
Evan Cheng70d29632011-02-25 00:24:46 +0000273 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000274
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000275 // Move past the aligned DPRCS2 area.
276 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
277 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
278 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
279 // leaves the stack pointer pointing to the DPRCS2 area.
280 //
281 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
282 NumBytes += MFI->getObjectOffset(D8SpillFI);
283 } else
284 NumBytes = DPRCSOffset;
285
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000286 unsigned adjustedGPRCS1Size = GPRCS1Size;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000287 if (NumBytes) {
288 // Adjust SP after all the callee-save spills.
Tim Northovera4173712013-12-08 15:56:50 +0000289 if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000290 if (LastPush == GPRCS1Push) {
Tim Northovera4173712013-12-08 15:56:50 +0000291 FramePtrOffsetInPush += NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000292 adjustedGPRCS1Size += NumBytes;
293 NumBytes = 0;
294 }
Tim Northovera4173712013-12-08 15:56:50 +0000295 } else
Tim Northover93bcc662013-11-08 17:18:07 +0000296 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
297 MachineInstr::FrameSetup);
298
Evan Chengeb56dca2010-11-22 18:12:04 +0000299 if (HasFP && isARM)
300 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
301 // Note it's not safe to do this in Thumb2 mode because it would have
302 // taken two instructions:
303 // mov sp, r7
304 // sub sp, #24
305 // If an interrupt is taken between the two instructions, then sp is in
306 // an inconsistent state (pointing to the middle of callee-saved area).
307 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000308 AFI->setShouldRestoreSPFromFP(true);
309 }
310
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000311 if (adjustedGPRCS1Size > 0) {
312 MCSymbol *SPLabel = Context.CreateTempSymbol();
313 BuildMI(MBB, ++GPRCS1Push, dl, TII.get(TargetOpcode::PROLOG_LABEL))
314 .addSym(SPLabel);
315 CFAOffset -= adjustedGPRCS1Size;
316 MMI.addFrameInst(
317 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
318 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
319 E = CSI.end(); I != E; ++I) {
320 unsigned Reg = I->getReg();
321 int FI = I->getFrameIdx();
322 switch (Reg) {
323 case ARM::R8:
324 case ARM::R9:
325 case ARM::R10:
326 case ARM::R11:
327 case ARM::R12:
328 if (STI.isTargetMachO())
329 break;
330 // fallthrough
331 case ARM::R0:
332 case ARM::R1:
333 case ARM::R2:
334 case ARM::R3:
335 case ARM::R4:
336 case ARM::R5:
337 case ARM::R6:
338 case ARM::R7:
339 case ARM::LR:
340 MMI.addFrameInst(MCCFIInstruction::createOffset(SPLabel,
341 MRI->getDwarfRegNum(Reg, true),
342 MFI->getObjectOffset(FI) - ArgRegsSaveSize));
343 break;
344 }
345 }
346 }
347
Tim Northover93bcc662013-11-08 17:18:07 +0000348 // Set FP to point to the stack slot that contains the previous FP.
349 // For iOS, FP is R7, which has now been stored in spill area 1.
350 // Otherwise, if this is not iOS, all the callee-saved registers go
351 // into spill area 1, including the FP in R11. In either case, it
352 // is in area one and the adjustment needs to take place just after
353 // that push.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000354 if (HasFP) {
355 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, GPRCS1Push, dl, TII,
Tim Northover93bcc662013-11-08 17:18:07 +0000356 FramePtr, ARM::SP, FramePtrOffsetInPush,
357 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000358 MCSymbol *SPLabel = Context.CreateTempSymbol();
359 BuildMI(MBB, GPRCS1Push, dl, TII.get(TargetOpcode::PROLOG_LABEL))
360 .addSym(SPLabel);
361 if (FramePtrOffsetInPush) {
362 CFAOffset += FramePtrOffsetInPush;
363 MMI.addFrameInst(
364 MCCFIInstruction::createDefCfa(SPLabel,
365 MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
366 } else
367 MMI.addFrameInst(
368 MCCFIInstruction::createDefCfaRegister(SPLabel,
369 MRI->getDwarfRegNum(FramePtr, true)));
370 }
Tim Northover93bcc662013-11-08 17:18:07 +0000371
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000372 if (GPRCS2Size > 0) {
373 MCSymbol *SPLabel = Context.CreateTempSymbol();
374 BuildMI(MBB, ++GPRCS2Push, dl, TII.get(TargetOpcode::PROLOG_LABEL))
375 .addSym(SPLabel);
376 if (!HasFP) {
377 CFAOffset -= GPRCS2Size;
378 MMI.addFrameInst(
379 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
380 }
381 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
382 E = CSI.end(); I != E; ++I) {
383 unsigned Reg = I->getReg();
384 int FI = I->getFrameIdx();
385 switch (Reg) {
386 case ARM::R8:
387 case ARM::R9:
388 case ARM::R10:
389 case ARM::R11:
390 case ARM::R12:
391 if (STI.isTargetMachO()) {
392 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
393 unsigned Offset = MFI->getObjectOffset(FI) - ArgRegsSaveSize;
394 MMI.addFrameInst(
395 MCCFIInstruction::createOffset(SPLabel, DwarfReg, Offset));
396 }
397 break;
398 }
399 }
400 }
401
402 if (DPRCSSize > 0) {
403 // Since vpush register list cannot have gaps, there may be multiple vpush
404 // instructions in the prologue.
405 MCSymbol *SPLabel = NULL;
406 do {
407 MachineBasicBlock::iterator Push = DPRCSPush++;
408 if (!HasFP) {
409 SPLabel = Context.CreateTempSymbol();
410 BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::PROLOG_LABEL))
411 .addSym(SPLabel);
412 CFAOffset -= sizeOfSPAdjustment(Push);;
413 MMI.addFrameInst(
414 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
415 }
416 } while (DPRCSPush->getOpcode() == ARM::VSTMDDB_UPD);
417
418 if (!SPLabel) {
419 SPLabel = Context.CreateTempSymbol();
420 BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::PROLOG_LABEL))
421 .addSym(SPLabel);
422 }
423 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
424 E = CSI.end(); I != E; ++I) {
425 unsigned Reg = I->getReg();
426 int FI = I->getFrameIdx();
427 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
428 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
429 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
430 unsigned Offset = MFI->getObjectOffset(FI);
431 MMI.addFrameInst(MCCFIInstruction::createOffset(SPLabel, DwarfReg,
432 Offset));
433 }
434 }
435 }
436
437 if (NumBytes) {
438 if (!HasFP) {
439 MCSymbol *SPLabel = Context.CreateTempSymbol();
440 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::PROLOG_LABEL))
441 .addSym(SPLabel);
442 CFAOffset -= NumBytes;
443 MMI.addFrameInst(
444 MCCFIInstruction::createDefCfaOffset(SPLabel, CFAOffset));
445 }
446 }
Tim Northover93bcc662013-11-08 17:18:07 +0000447
Evan Chengeb56dca2010-11-22 18:12:04 +0000448 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000449 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
450 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000451
452 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
453 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
454 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
455
456 // If we need dynamic stack realignment, do it here. Be paranoid and make
457 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000458 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000459 // realigned.
460 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000461 unsigned MaxAlign = MFI->getMaxAlignment();
462 assert (!AFI->isThumb1OnlyFunction());
463 if (!AFI->isThumbFunction()) {
464 // Emit bic sp, sp, MaxAlign
465 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
466 TII.get(ARM::BICri), ARM::SP)
467 .addReg(ARM::SP, RegState::Kill)
468 .addImm(MaxAlign-1)));
469 } else {
470 // We cannot use sp as source/dest register here, thus we're emitting the
471 // following sequence:
472 // mov r4, sp
473 // bic r4, r4, MaxAlign
474 // mov sp, r4
475 // FIXME: It will be better just to find spare register here.
Jim Grosbache9cc9012011-06-30 23:38:17 +0000476 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000477 .addReg(ARM::SP, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000478 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
479 TII.get(ARM::t2BICri), ARM::R4)
480 .addReg(ARM::R4, RegState::Kill)
481 .addImm(MaxAlign-1)));
Jim Grosbache9cc9012011-06-30 23:38:17 +0000482 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000483 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000484 }
485
486 AFI->setShouldRestoreSPFromFP(true);
487 }
488
489 // If we need a base pointer, set it up here. It's whatever the value
490 // of the stack pointer is at this point. Any variable size objects
491 // will be allocated after this, so we can still use the base pointer
492 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000493 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000494 if (RegInfo->hasBasePointer(MF)) {
495 if (isARM)
496 BuildMI(MBB, MBBI, dl,
497 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
498 .addReg(ARM::SP)
499 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
500 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000501 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000502 RegInfo->getBaseRegister())
503 .addReg(ARM::SP));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000504 }
505
506 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000507 // the sp from fp. We can assume there's an FP here since hasFP already
508 // checks for hasVarSizedObjects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000509 if (MFI->hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000510 AFI->setShouldRestoreSPFromFP(true);
511}
512
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000513void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000514 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000515 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000516 assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000517 unsigned RetOpcode = MBBI->getOpcode();
518 DebugLoc dl = MBBI->getDebugLoc();
519 MachineFrameInfo *MFI = MF.getFrameInfo();
520 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
521 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
522 const ARMBaseInstrInfo &TII =
523 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
524 assert(!AFI->isThumb1OnlyFunction() &&
525 "This emitEpilogue does not support Thumb1!");
526 bool isARM = !AFI->isThumbFunction();
527
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000528 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
529 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000530 int NumBytes = (int)MFI->getStackSize();
531 unsigned FramePtr = RegInfo->getFrameRegister(MF);
532
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000533 // All calls are tail calls in GHC calling conv, and functions have no
534 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000535 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
536 return;
537
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000538 if (!AFI->hasStackFrame()) {
539 if (NumBytes != 0)
540 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
541 } else {
542 // Unwind MBBI to point to first LDR / VLDRD.
Tim Northoverd8407452013-10-01 14:33:28 +0000543 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000544 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000545 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000546 --MBBI;
Tim Northover93bcc662013-11-08 17:18:07 +0000547 } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000548 if (!isCSRestore(MBBI, TII, CSRegs))
549 ++MBBI;
550 }
551
552 // Move SP to start of FP callee save spill area.
553 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
554 AFI->getGPRCalleeSavedArea2Size() +
555 AFI->getDPRCalleeSavedAreaSize());
556
557 // Reset SP based on frame pointer only if the stack frame extends beyond
558 // frame pointer stack slot or target is ELF and the function has FP.
559 if (AFI->shouldRestoreSPFromFP()) {
560 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
561 if (NumBytes) {
562 if (isARM)
563 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
564 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000565 else {
566 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000567 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000568 // mov sp, r7
569 // sub sp, #24
570 // This is bad, if an interrupt is taken after the mov, sp is in an
571 // inconsistent state.
572 // Use the first callee-saved register as a scratch register.
Kaelyn Uhrain271fbb62012-10-26 23:28:41 +0000573 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000574 "No scratch register to restore SP from FP!");
575 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000576 ARMCC::AL, 0, TII);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000577 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000578 ARM::SP)
579 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000580 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000581 } else {
582 // Thumb2 or ARM.
583 if (isARM)
584 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
585 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
586 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000587 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000588 ARM::SP)
589 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000590 }
Tim Northoverdee86042013-12-02 14:46:26 +0000591 } else if (NumBytes &&
Tim Northovere4def5e2013-12-05 11:02:02 +0000592 !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000593 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000594
Eric Christopherb006fc92010-11-18 19:40:05 +0000595 // Increment past our save areas.
Evan Cheng70d29632011-02-25 00:24:46 +0000596 if (AFI->getDPRCalleeSavedAreaSize()) {
597 MBBI++;
598 // Since vpop register list cannot have gaps, there may be multiple vpop
599 // instructions in the epilogue.
600 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
601 MBBI++;
602 }
Eric Christopherb006fc92010-11-18 19:40:05 +0000603 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
604 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000605 }
606
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000607 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000608 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000609 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000610 MachineOperand &JumpTarget = MBBI->getOperand(0);
611
612 // Jump to label or value in register.
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000613 if (RetOpcode == ARM::TCRETURNdi) {
614 unsigned TCOpcode = STI.isThumb() ?
Tim Northoverd6a729b2014-01-06 14:28:05 +0000615 (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) :
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000616 ARM::TAILJMPd;
Evan Chengd4b08732010-11-30 23:55:39 +0000617 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
618 if (JumpTarget.isGlobal())
619 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
620 JumpTarget.getTargetFlags());
621 else {
622 assert(JumpTarget.isSymbol());
623 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
624 JumpTarget.getTargetFlags());
625 }
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000626
627 // Add the default predicate in Thumb mode.
628 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000629 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach3af6fe62011-03-15 00:30:40 +0000630 BuildMI(MBB, MBBI, dl,
631 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000632 addReg(JumpTarget.getReg(), RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000633 }
634
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000635 MachineInstr *NewMI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000636 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
637 NewMI->addOperand(MBBI->getOperand(i));
638
639 // Delete the pseudo instruction TCRETURN.
640 MBB.erase(MBBI);
Cameron Zwarich033026f2011-06-17 02:16:43 +0000641 MBBI = NewMI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000642 }
643
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000644 if (ArgRegsSaveSize)
645 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000646}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000647
Bob Wilson657f2272011-01-13 21:10:12 +0000648/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
649/// debug info. It's the same as what we use for resolving the code-gen
650/// references for now. FIXME: This can go wrong when references are
651/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000652int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000653ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000654 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000655 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
656}
657
658int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000659ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000660 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000661 int SPAdj) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000662 const MachineFrameInfo *MFI = MF.getFrameInfo();
663 const ARMBaseRegisterInfo *RegInfo =
664 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
665 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
666 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
667 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
668 bool isFixed = MFI->isFixedObjectIndex(FI);
669
670 FrameReg = ARM::SP;
671 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000672
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000673 // SP can move around if there are allocas. We may also lose track of SP
674 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000675 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000676
Anton Korobeynikov46877782010-11-20 15:59:32 +0000677 // When dynamically realigning the stack, use the frame pointer for
678 // parameters, and the stack/base pointer for locals.
679 if (RegInfo->needsStackRealignment(MF)) {
680 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
681 if (isFixed) {
682 FrameReg = RegInfo->getFrameRegister(MF);
683 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000684 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000685 assert(RegInfo->hasBasePointer(MF) &&
686 "VLAs and dynamic stack alignment, but missing base pointer!");
687 FrameReg = RegInfo->getBaseRegister();
688 }
689 return Offset;
690 }
691
692 // If there is a frame pointer, use it when we can.
693 if (hasFP(MF) && AFI->hasStackFrame()) {
694 // Use frame pointer to reference fixed objects. Use it for locals if
695 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000696 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000697 FrameReg = RegInfo->getFrameRegister(MF);
698 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000699 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000700 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000701 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000702 // Try to use the frame pointer if we can, else use the base pointer
703 // since it's available. This is handy for the emergency spill slot, in
704 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000705 if (FPOffset >= -255 && FPOffset < 0) {
706 FrameReg = RegInfo->getFrameRegister(MF);
707 return FPOffset;
708 }
Evan Chengc0d20042011-04-22 01:42:52 +0000709 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000710 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000711 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000712 // ldr <rd>, [sp, #<imm8>]
713 // if at all possible to save space.
714 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
715 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000716 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000717 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000718 if (FPOffset >= -255 && FPOffset < 0) {
719 FrameReg = RegInfo->getFrameRegister(MF);
720 return FPOffset;
721 }
722 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
723 // Otherwise, use SP or FP, whichever is closer to the stack slot.
724 FrameReg = RegInfo->getFrameRegister(MF);
725 return FPOffset;
726 }
727 }
728 // Use the base pointer if we have one.
729 if (RegInfo->hasBasePointer(MF))
730 FrameReg = RegInfo->getBaseRegister();
731 return Offset;
732}
733
Bob Wilson657f2272011-01-13 21:10:12 +0000734int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
735 int FI) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000736 unsigned FrameReg;
737 return getFrameIndexReference(MF, FI, FrameReg);
738}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000739
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000740void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000741 MachineBasicBlock::iterator MI,
742 const std::vector<CalleeSavedInfo> &CSI,
743 unsigned StmOpc, unsigned StrOpc,
744 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000745 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000746 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000747 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000748 MachineFunction &MF = *MBB.getParent();
749 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
750
751 DebugLoc DL;
752 if (MI != MBB.end()) DL = MI->getDebugLoc();
753
Evan Chengc27c9562010-12-07 19:59:34 +0000754 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng775ead32010-12-07 23:08:38 +0000755 unsigned i = CSI.size();
756 while (i != 0) {
757 unsigned LastReg = 0;
758 for (; i != 0; --i) {
759 unsigned Reg = CSI[i-1].getReg();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000760 if (!(Func)(Reg, STI.isTargetMachO())) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000761
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000762 // D-registers in the aligned area DPRCS2 are NOT spilled here.
763 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
764 continue;
765
Evan Cheng775ead32010-12-07 23:08:38 +0000766 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbachc0b669f2010-12-09 16:14:46 +0000767 // @llvm.returnaddress is called. If LR is returned for
768 // @llvm.returnaddress then it's already added to the function and
769 // entry block live-in sets.
Evan Cheng775ead32010-12-07 23:08:38 +0000770 bool isKill = true;
771 if (Reg == ARM::LR) {
772 if (MF.getFrameInfo()->isReturnAddressTaken() &&
773 MF.getRegInfo().isLiveIn(Reg))
774 isKill = false;
775 }
776
777 if (isKill)
778 MBB.addLiveIn(Reg);
779
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000780 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000781 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000782 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000783 if (NoGap && LastReg && LastReg != Reg-1)
784 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000785 LastReg = Reg;
786 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000787 }
788
Jim Grosbach5fccad82010-12-09 18:31:13 +0000789 if (Regs.empty())
790 continue;
791 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000792 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000793 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000794 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng775ead32010-12-07 23:08:38 +0000795 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
796 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +0000797 } else if (Regs.size() == 1) {
798 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
799 ARM::SP)
800 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000801 .addReg(ARM::SP).setMIFlags(MIFlags)
802 .addImm(-4);
Jim Grosbach5fccad82010-12-09 18:31:13 +0000803 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000804 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000805 Regs.clear();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000806 }
Evan Cheng775ead32010-12-07 23:08:38 +0000807}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000808
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000809void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000810 MachineBasicBlock::iterator MI,
811 const std::vector<CalleeSavedInfo> &CSI,
812 unsigned LdmOpc, unsigned LdrOpc,
813 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000814 bool(*Func)(unsigned, bool),
815 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +0000816 MachineFunction &MF = *MBB.getParent();
817 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
818 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
819 DebugLoc DL = MI->getDebugLoc();
Evan Chengd6093ff2011-01-25 01:28:33 +0000820 unsigned RetOpcode = MI->getOpcode();
821 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000822 RetOpcode == ARM::TCRETURNri);
Tim Northoverd8407452013-10-01 14:33:28 +0000823 bool isInterrupt =
824 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Evan Cheng775ead32010-12-07 23:08:38 +0000825
826 SmallVector<unsigned, 4> Regs;
827 unsigned i = CSI.size();
828 while (i != 0) {
829 unsigned LastReg = 0;
830 bool DeleteRet = false;
831 for (; i != 0; --i) {
832 unsigned Reg = CSI[i-1].getReg();
Tim Northoverd6a729b2014-01-06 14:28:05 +0000833 if (!(Func)(Reg, STI.isTargetMachO())) continue;
Evan Cheng775ead32010-12-07 23:08:38 +0000834
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000835 // The aligned reloads from area DPRCS2 are not inserted here.
836 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
837 continue;
838
Tim Northoverd8407452013-10-01 14:33:28 +0000839 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
840 STI.hasV5TOps()) {
Evan Cheng775ead32010-12-07 23:08:38 +0000841 Reg = ARM::PC;
Jim Grosbach5fccad82010-12-09 18:31:13 +0000842 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng775ead32010-12-07 23:08:38 +0000843 // Fold the return instruction into the LDM.
844 DeleteRet = true;
845 }
846
Evan Cheng9d54ae62010-12-08 06:29:02 +0000847 // If NoGap is true, pop consecutive registers and then leave the rest
848 // for other instructions. e.g.
849 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
850 if (NoGap && LastReg && LastReg != Reg-1)
851 break;
852
Evan Cheng775ead32010-12-07 23:08:38 +0000853 LastReg = Reg;
854 Regs.push_back(Reg);
855 }
856
Jim Grosbach5fccad82010-12-09 18:31:13 +0000857 if (Regs.empty())
858 continue;
859 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000860 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000861 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng775ead32010-12-07 23:08:38 +0000862 .addReg(ARM::SP));
863 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
864 MIB.addReg(Regs[i], getDefRegState(true));
Andrew Trick6446bf72011-08-25 17:50:53 +0000865 if (DeleteRet) {
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000866 MIB.copyImplicitOps(&*MI);
Evan Cheng775ead32010-12-07 23:08:38 +0000867 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +0000868 }
Evan Cheng775ead32010-12-07 23:08:38 +0000869 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +0000870 } else if (Regs.size() == 1) {
871 // If we adjusted the reg to PC from LR above, switch it back here. We
872 // only do that for LDM.
873 if (Regs[0] == ARM::PC)
874 Regs[0] = ARM::LR;
875 MachineInstrBuilder MIB =
876 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
877 .addReg(ARM::SP, RegState::Define)
878 .addReg(ARM::SP);
879 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
880 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +0000881 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +0000882 MIB.addReg(0);
883 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
884 } else
885 MIB.addImm(4);
886 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000887 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000888 Regs.clear();
Evan Chengc27c9562010-12-07 19:59:34 +0000889 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000890}
891
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000892/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000893/// starting from d8. Also insert stack realignment code and leave the stack
894/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000895static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
896 MachineBasicBlock::iterator MI,
897 unsigned NumAlignedDPRCS2Regs,
898 const std::vector<CalleeSavedInfo> &CSI,
899 const TargetRegisterInfo *TRI) {
900 MachineFunction &MF = *MBB.getParent();
901 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
902 DebugLoc DL = MI->getDebugLoc();
903 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
904 MachineFrameInfo &MFI = *MF.getFrameInfo();
905
906 // Mark the D-register spill slots as properly aligned. Since MFI computes
907 // stack slot layout backwards, this can actually mean that the d-reg stack
908 // slot offsets can be wrong. The offset for d8 will always be correct.
909 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
910 unsigned DNum = CSI[i].getReg() - ARM::D8;
911 if (DNum >= 8)
912 continue;
913 int FI = CSI[i].getFrameIdx();
914 // The even-numbered registers will be 16-byte aligned, the odd-numbered
915 // registers will be 8-byte aligned.
916 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
917
918 // The stack slot for D8 needs to be maximally aligned because this is
919 // actually the point where we align the stack pointer. MachineFrameInfo
920 // computes all offsets relative to the incoming stack pointer which is a
921 // bit weird when realigning the stack. Any extra padding for this
922 // over-alignment is not realized because the code inserted below adjusts
923 // the stack pointer by numregs * 8 before aligning the stack pointer.
924 if (DNum == 0)
925 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
926 }
927
928 // Move the stack pointer to the d8 spill slot, and align it at the same
929 // time. Leave the stack slot address in the scratch register r4.
930 //
931 // sub r4, sp, #numregs * 8
932 // bic r4, r4, #align - 1
933 // mov sp, r4
934 //
935 bool isThumb = AFI->isThumbFunction();
936 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
937 AFI->setShouldRestoreSPFromFP(true);
938
939 // sub r4, sp, #numregs * 8
940 // The immediate is <= 64, so it doesn't need any special encoding.
941 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
942 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
943 .addReg(ARM::SP)
944 .addImm(8 * NumAlignedDPRCS2Regs)));
945
946 // bic r4, r4, #align-1
947 Opc = isThumb ? ARM::t2BICri : ARM::BICri;
948 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
949 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
950 .addReg(ARM::R4, RegState::Kill)
951 .addImm(MaxAlign - 1)));
952
953 // mov sp, r4
954 // The stack pointer must be adjusted before spilling anything, otherwise
955 // the stack slots could be clobbered by an interrupt handler.
956 // Leave r4 live, it is used below.
957 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
958 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
959 .addReg(ARM::R4);
960 MIB = AddDefaultPred(MIB);
961 if (!isThumb)
962 AddDefaultCC(MIB);
963
964 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
965 // r4 holds the stack slot address.
966 unsigned NextReg = ARM::D8;
967
968 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
969 // The writeback is only needed when emitting two vst1.64 instructions.
970 if (NumAlignedDPRCS2Regs >= 6) {
971 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +0000972 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000973 MBB.addLiveIn(SupReg);
974 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
975 ARM::R4)
976 .addReg(ARM::R4, RegState::Kill).addImm(16)
977 .addReg(NextReg)
978 .addReg(SupReg, RegState::ImplicitKill));
979 NextReg += 4;
980 NumAlignedDPRCS2Regs -= 4;
981 }
982
983 // We won't modify r4 beyond this point. It currently points to the next
984 // register to be spilled.
985 unsigned R4BaseReg = NextReg;
986
987 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
988 if (NumAlignedDPRCS2Regs >= 4) {
989 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +0000990 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000991 MBB.addLiveIn(SupReg);
992 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
993 .addReg(ARM::R4).addImm(16).addReg(NextReg)
994 .addReg(SupReg, RegState::ImplicitKill));
995 NextReg += 4;
996 NumAlignedDPRCS2Regs -= 4;
997 }
998
999 // 16-byte aligned vst1.64 with 2 d-regs.
1000 if (NumAlignedDPRCS2Regs >= 2) {
1001 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001002 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001003 MBB.addLiveIn(SupReg);
1004 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001005 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001006 NextReg += 2;
1007 NumAlignedDPRCS2Regs -= 2;
1008 }
1009
1010 // Finally, use a vanilla vstr.64 for the odd last register.
1011 if (NumAlignedDPRCS2Regs) {
1012 MBB.addLiveIn(NextReg);
1013 // vstr.64 uses addrmode5 which has an offset scale of 4.
1014 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1015 .addReg(NextReg)
1016 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1017 }
1018
1019 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001020 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001021}
1022
1023/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1024/// iterator to the following instruction.
1025static MachineBasicBlock::iterator
1026skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1027 unsigned NumAlignedDPRCS2Regs) {
1028 // sub r4, sp, #numregs * 8
1029 // bic r4, r4, #align - 1
1030 // mov sp, r4
1031 ++MI; ++MI; ++MI;
1032 assert(MI->mayStore() && "Expecting spill instruction");
1033
1034 // These switches all fall through.
1035 switch(NumAlignedDPRCS2Regs) {
1036 case 7:
1037 ++MI;
1038 assert(MI->mayStore() && "Expecting spill instruction");
1039 default:
1040 ++MI;
1041 assert(MI->mayStore() && "Expecting spill instruction");
1042 case 1:
1043 case 2:
1044 case 4:
1045 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1046 ++MI;
1047 }
1048 return MI;
1049}
1050
1051/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1052/// starting from d8. These instructions are assumed to execute while the
1053/// stack is still aligned, unlike the code inserted by emitPopInst.
1054static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1055 MachineBasicBlock::iterator MI,
1056 unsigned NumAlignedDPRCS2Regs,
1057 const std::vector<CalleeSavedInfo> &CSI,
1058 const TargetRegisterInfo *TRI) {
1059 MachineFunction &MF = *MBB.getParent();
1060 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1061 DebugLoc DL = MI->getDebugLoc();
1062 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
1063
1064 // Find the frame index assigned to d8.
1065 int D8SpillFI = 0;
1066 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1067 if (CSI[i].getReg() == ARM::D8) {
1068 D8SpillFI = CSI[i].getFrameIdx();
1069 break;
1070 }
1071
1072 // Materialize the address of the d8 spill slot into the scratch register r4.
1073 // This can be fairly complicated if the stack frame is large, so just use
1074 // the normal frame index elimination mechanism to do it. This code runs as
1075 // the initial part of the epilog where the stack and base pointers haven't
1076 // been changed yet.
1077 bool isThumb = AFI->isThumbFunction();
1078 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1079
1080 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1081 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1082 .addFrameIndex(D8SpillFI).addImm(0)));
1083
1084 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1085 unsigned NextReg = ARM::D8;
1086
1087 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1088 if (NumAlignedDPRCS2Regs >= 6) {
1089 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001090 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001091 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1092 .addReg(ARM::R4, RegState::Define)
1093 .addReg(ARM::R4, RegState::Kill).addImm(16)
1094 .addReg(SupReg, RegState::ImplicitDefine));
1095 NextReg += 4;
1096 NumAlignedDPRCS2Regs -= 4;
1097 }
1098
1099 // We won't modify r4 beyond this point. It currently points to the next
1100 // register to be spilled.
1101 unsigned R4BaseReg = NextReg;
1102
1103 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1104 if (NumAlignedDPRCS2Regs >= 4) {
1105 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001106 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001107 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1108 .addReg(ARM::R4).addImm(16)
1109 .addReg(SupReg, RegState::ImplicitDefine));
1110 NextReg += 4;
1111 NumAlignedDPRCS2Regs -= 4;
1112 }
1113
1114 // 16-byte aligned vld1.64 with 2 d-regs.
1115 if (NumAlignedDPRCS2Regs >= 2) {
1116 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001117 &ARM::QPRRegClass);
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001118 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1119 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001120 NextReg += 2;
1121 NumAlignedDPRCS2Regs -= 2;
1122 }
1123
1124 // Finally, use a vanilla vldr.64 for the remaining odd register.
1125 if (NumAlignedDPRCS2Regs)
1126 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1127 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1128
1129 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001130 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001131}
1132
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001133bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001134 MachineBasicBlock::iterator MI,
1135 const std::vector<CalleeSavedInfo> &CSI,
1136 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001137 if (CSI.empty())
1138 return false;
1139
1140 MachineFunction &MF = *MBB.getParent();
1141 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001142
1143 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001144 unsigned PushOneOpc = AFI->isThumbFunction() ?
1145 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001146 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001147 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1148 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001149 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001150 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001151 MachineInstr::FrameSetup);
1152 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001153 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1154
1155 // The code above does not insert spill code for the aligned DPRCS2 registers.
1156 // The stack realignment code will be inserted between the push instructions
1157 // and these spills.
1158 if (NumAlignedDPRCS2Regs)
1159 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001160
1161 return true;
1162}
1163
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001164bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001165 MachineBasicBlock::iterator MI,
1166 const std::vector<CalleeSavedInfo> &CSI,
1167 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001168 if (CSI.empty())
1169 return false;
1170
1171 MachineFunction &MF = *MBB.getParent();
1172 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001173 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001174 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1175
1176 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1177 // registers. Do that here instead.
1178 if (NumAlignedDPRCS2Regs)
1179 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001180
1181 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001182 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001183 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001184 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1185 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001186 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001187 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001188 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001189 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001190
1191 return true;
1192}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001193
1194// FIXME: Make generic?
1195static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1196 const ARMBaseInstrInfo &TII) {
1197 unsigned FnSize = 0;
1198 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
1199 MBBI != E; ++MBBI) {
1200 const MachineBasicBlock &MBB = *MBBI;
1201 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
1202 I != E; ++I)
1203 FnSize += TII.GetInstSizeInBytes(I);
1204 }
1205 return FnSize;
1206}
1207
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001208/// estimateRSStackSizeLimit - Look at each instruction that references stack
1209/// frames and return the stack size limit beyond which some of these
1210/// instructions will require a scratch register during their expansion later.
1211// FIXME: Move to TII?
1212static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001213 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001214 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1215 unsigned Limit = (1 << 12) - 1;
1216 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
1217 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1218 I != E; ++I) {
1219 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1220 if (!I->getOperand(i).isFI()) continue;
1221
1222 // When using ADDri to get the address of a stack object, 255 is the
1223 // largest offset guaranteed to fit in the immediate offset.
1224 if (I->getOpcode() == ARM::ADDri) {
1225 Limit = std::min(Limit, (1U << 8) - 1);
1226 break;
1227 }
1228
1229 // Otherwise check the addressing mode.
1230 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
1231 case ARMII::AddrMode3:
1232 case ARMII::AddrModeT2_i8:
1233 Limit = std::min(Limit, (1U << 8) - 1);
1234 break;
1235 case ARMII::AddrMode5:
1236 case ARMII::AddrModeT2_i8s4:
1237 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1238 break;
1239 case ARMII::AddrModeT2_i12:
1240 // i12 supports only positive offset so these will be converted to
1241 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1242 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1243 Limit = std::min(Limit, (1U << 8) - 1);
1244 break;
1245 case ARMII::AddrMode4:
1246 case ARMII::AddrMode6:
1247 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1248 // immediate offset for stack references.
1249 return 0;
1250 default:
1251 break;
1252 }
1253 break; // At most one FI per instruction
1254 }
1255 }
1256 }
1257
1258 return Limit;
1259}
1260
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001261// In functions that realign the stack, it can be an advantage to spill the
1262// callee-saved vector registers after realigning the stack. The vst1 and vld1
1263// instructions take alignment hints that can improve performance.
1264//
1265static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) {
1266 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1267 if (!SpillAlignedNEONRegs)
1268 return;
1269
1270 // Naked functions don't spill callee-saved registers.
Bill Wendling698e84f2012-12-30 10:32:01 +00001271 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1272 Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001273 return;
1274
1275 // We are planning to use NEON instructions vst1 / vld1.
1276 if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON())
1277 return;
1278
1279 // Don't bother if the default stack alignment is sufficiently high.
1280 if (MF.getTarget().getFrameLowering()->getStackAlignment() >= 8)
1281 return;
1282
1283 // Aligned spills require stack realignment.
1284 const ARMBaseRegisterInfo *RegInfo =
1285 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1286 if (!RegInfo->canRealignStack(MF))
1287 return;
1288
1289 // We always spill contiguous d-registers starting from d8. Count how many
1290 // needs spilling. The register allocator will almost always use the
1291 // callee-saved registers in order, but it can happen that there are holes in
1292 // the range. Registers above the hole will be spilled to the standard DPRCS
1293 // area.
1294 MachineRegisterInfo &MRI = MF.getRegInfo();
1295 unsigned NumSpills = 0;
1296 for (; NumSpills < 8; ++NumSpills)
Jakob Stoklund Olesen07364422012-10-17 18:44:18 +00001297 if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001298 break;
1299
1300 // Don't do this for just one d-register. It's not worth it.
1301 if (NumSpills < 2)
1302 return;
1303
1304 // Spill the first NumSpills D-registers after realigning the stack.
1305 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1306
1307 // A scratch register is required for the vst1 / vld1 instructions.
1308 MF.getRegInfo().setPhysRegUsed(ARM::R4);
1309}
1310
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001311void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001312ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +00001313 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001314 // This tells PEI to spill the FP as if it is any other callee-save register
1315 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1316 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1317 // to combine multiple loads / stores.
1318 bool CanEliminateFrame = true;
1319 bool CS1Spilled = false;
1320 bool LRSpilled = false;
1321 unsigned NumGPRSpills = 0;
1322 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1323 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1324 const ARMBaseRegisterInfo *RegInfo =
1325 static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo());
1326 const ARMBaseInstrInfo &TII =
1327 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
1328 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1329 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001330 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001331 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1332
1333 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1334 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001335 // since it's not always possible to restore sp from fp in a single
1336 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001337 // FIXME: It will be better just to find spare register here.
1338 if (AFI->isThumb2Function() &&
1339 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001340 MRI.setPhysRegUsed(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001341
Evan Cheng572756a2011-01-16 05:14:33 +00001342 if (AFI->isThumb1OnlyFunction()) {
1343 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001344 if (AFI->getArgRegsSaveSize() > 0)
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001345 MRI.setPhysRegUsed(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001346
Jim Grosbachdca85312011-06-13 21:18:25 +00001347 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1348 // for sure what the stack size will be, but for this, an estimate is good
1349 // enough. If there anything changes it, it'll be a spill, which implies
1350 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001351 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001352 // FIXME: It will be better just to find spare register here.
Hal Finkel628ba122013-03-14 21:15:20 +00001353 unsigned StackSize = MFI->estimateStackSize(MF);
Chad Rosieradd38c12011-10-20 00:07:12 +00001354 if (MFI->hasVarSizedObjects() || StackSize > 508)
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001355 MRI.setPhysRegUsed(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001356 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001357
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001358 // See if we can spill vector registers to aligned stack.
1359 checkNumAlignedDPRCS2Regs(MF);
1360
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001361 // Spill the BasePtr if it's used.
1362 if (RegInfo->hasBasePointer(MF))
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001363 MRI.setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001364
1365 // Don't spill FP if the frame can be eliminated. This is determined
1366 // by scanning the callee-save registers to see if any is used.
Tim Northoverd8407452013-10-01 14:33:28 +00001367 const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001368 for (unsigned i = 0; CSRegs[i]; ++i) {
1369 unsigned Reg = CSRegs[i];
1370 bool Spilled = false;
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001371 if (MRI.isPhysRegUsed(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001372 Spilled = true;
1373 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001374 }
1375
Craig Topperc7242e02012-04-20 07:30:17 +00001376 if (!ARM::GPRRegClass.contains(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001377 continue;
1378
1379 if (Spilled) {
1380 NumGPRSpills++;
1381
Tim Northoverd6a729b2014-01-06 14:28:05 +00001382 if (!STI.isTargetMachO()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001383 if (Reg == ARM::LR)
1384 LRSpilled = true;
1385 CS1Spilled = true;
1386 continue;
1387 }
1388
1389 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1390 switch (Reg) {
1391 case ARM::LR:
1392 LRSpilled = true;
1393 // Fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +00001394 case ARM::R0: case ARM::R1:
1395 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001396 case ARM::R4: case ARM::R5:
1397 case ARM::R6: case ARM::R7:
1398 CS1Spilled = true;
1399 break;
1400 default:
1401 break;
1402 }
1403 } else {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001404 if (!STI.isTargetMachO()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001405 UnspilledCS1GPRs.push_back(Reg);
1406 continue;
1407 }
1408
1409 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001410 case ARM::R0: case ARM::R1:
1411 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001412 case ARM::R4: case ARM::R5:
1413 case ARM::R6: case ARM::R7:
1414 case ARM::LR:
1415 UnspilledCS1GPRs.push_back(Reg);
1416 break;
1417 default:
1418 UnspilledCS2GPRs.push_back(Reg);
1419 break;
1420 }
1421 }
1422 }
1423
1424 bool ForceLRSpill = false;
1425 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1426 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1427 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1428 // use of BL to implement far jump. If it turns out that it's not needed
1429 // then the branch fix up path will undo it.
1430 if (FnSize >= (1 << 11)) {
1431 CanEliminateFrame = false;
1432 ForceLRSpill = true;
1433 }
1434 }
1435
1436 // If any of the stack slot references may be out of range of an immediate
1437 // offset, make sure a register (or a spill slot) is available for the
1438 // register scavenger. Note that if we're indexing off the frame pointer, the
1439 // effective stack size is 4 bytes larger since the FP points to the stack
1440 // slot of the previous FP. Also, if we have variable sized objects in the
1441 // function, stack slot references will often be negative, and some of
1442 // our instructions are positive-offset only, so conservatively consider
1443 // that case to want a spill slot (or register) as well. Similarly, if
1444 // the function adjusts the stack pointer during execution and the
1445 // adjustments aren't already part of our stack size estimate, our offset
1446 // calculations may be off, so be conservative.
1447 // FIXME: We could add logic to be more precise about negative offsets
1448 // and which instructions will need a scratch register for them. Is it
1449 // worth the effort and added fragility?
1450 bool BigStack =
1451 (RS &&
Hal Finkel628ba122013-03-14 21:15:20 +00001452 (MFI->estimateStackSize(MF) +
1453 ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001454 estimateRSStackSizeLimit(MF, this)))
1455 || MFI->hasVarSizedObjects()
1456 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
1457
1458 bool ExtraCSSpill = false;
1459 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1460 AFI->setHasStackFrame(true);
1461
1462 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1463 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1464 if (!LRSpilled && CS1Spilled) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001465 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001466 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001467 SmallVectorImpl<unsigned>::iterator LRPos;
1468 LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1469 (unsigned)ARM::LR);
1470 if (LRPos != UnspilledCS1GPRs.end())
1471 UnspilledCS1GPRs.erase(LRPos);
1472
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001473 ForceLRSpill = false;
1474 ExtraCSSpill = true;
1475 }
1476
1477 if (hasFP(MF)) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001478 MRI.setPhysRegUsed(FramePtr);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001479 NumGPRSpills++;
1480 }
1481
1482 // If stack and double are 8-byte aligned and we are spilling an odd number
1483 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1484 // the integer and double callee save areas.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001485 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001486 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1487 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1488 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1489 unsigned Reg = UnspilledCS1GPRs[i];
1490 // Don't spill high register if the function is thumb1
1491 if (!AFI->isThumb1OnlyFunction() ||
1492 isARMLowRegister(Reg) || Reg == ARM::LR) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001493 MRI.setPhysRegUsed(Reg);
1494 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001495 ExtraCSSpill = true;
1496 break;
1497 }
1498 }
1499 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1500 unsigned Reg = UnspilledCS2GPRs.front();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001501 MRI.setPhysRegUsed(Reg);
1502 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001503 ExtraCSSpill = true;
1504 }
1505 }
1506
1507 // Estimate if we might need to scavenge a register at some point in order
1508 // to materialize a stack offset. If so, either spill one additional
1509 // callee-saved register or reserve a special spill slot to facilitate
1510 // register scavenging. Thumb1 needs a spill slot for stack pointer
1511 // adjustments also, even when the frame itself is small.
1512 if (BigStack && !ExtraCSSpill) {
1513 // If any non-reserved CS register isn't spilled, just spill one or two
1514 // extra. That should take care of it!
1515 unsigned NumExtras = TargetAlign / 4;
1516 SmallVector<unsigned, 2> Extras;
1517 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1518 unsigned Reg = UnspilledCS1GPRs.back();
1519 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001520 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001521 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1522 Reg == ARM::LR)) {
1523 Extras.push_back(Reg);
1524 NumExtras--;
1525 }
1526 }
1527 // For non-Thumb1 functions, also check for hi-reg CS registers
1528 if (!AFI->isThumb1OnlyFunction()) {
1529 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1530 unsigned Reg = UnspilledCS2GPRs.back();
1531 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001532 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001533 Extras.push_back(Reg);
1534 NumExtras--;
1535 }
1536 }
1537 }
1538 if (Extras.size() && NumExtras == 0) {
1539 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001540 MRI.setPhysRegUsed(Extras[i]);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001541 }
1542 } else if (!AFI->isThumb1OnlyFunction()) {
1543 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1544 // closest to SP or frame pointer.
Craig Topperc7242e02012-04-20 07:30:17 +00001545 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Hal Finkel9e331c22013-03-22 23:32:27 +00001546 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001547 RC->getAlignment(),
1548 false));
1549 }
1550 }
1551 }
1552
1553 if (ForceLRSpill) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001554 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001555 AFI->setLRIsSpilledForFarJump(true);
1556 }
1557}
Eli Bendersky8da87162013-02-21 20:05:00 +00001558
1559
1560void ARMFrameLowering::
1561eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1562 MachineBasicBlock::iterator I) const {
1563 const ARMBaseInstrInfo &TII =
1564 *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo());
1565 if (!hasReservedCallFrame(MF)) {
1566 // If we have alloca, convert as follows:
1567 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1568 // ADJCALLSTACKUP -> add, sp, sp, amount
1569 MachineInstr *Old = I;
1570 DebugLoc dl = Old->getDebugLoc();
1571 unsigned Amount = Old->getOperand(0).getImm();
1572 if (Amount != 0) {
1573 // We need to keep the stack aligned properly. To do this, we round the
1574 // amount of space needed for the outgoing arguments up to the next
1575 // alignment boundary.
1576 unsigned Align = getStackAlignment();
1577 Amount = (Amount+Align-1)/Align*Align;
1578
1579 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1580 assert(!AFI->isThumb1OnlyFunction() &&
1581 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1582 bool isARM = !AFI->isThumbFunction();
1583
1584 // Replace the pseudo instruction with a new instruction...
1585 unsigned Opc = Old->getOpcode();
1586 int PIdx = Old->findFirstPredOperandIdx();
1587 ARMCC::CondCodes Pred = (PIdx == -1)
1588 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1589 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1590 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1591 unsigned PredReg = Old->getOperand(2).getReg();
1592 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1593 Pred, PredReg);
1594 } else {
1595 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1596 unsigned PredReg = Old->getOperand(3).getReg();
1597 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1598 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1599 Pred, PredReg);
1600 }
1601 }
1602 }
1603 MBB.erase(I);
1604}
1605