blob: 4589799933b8b5630d30f0cf6b1b7130dd8bb7da [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"
Oliver Stannardb14c6252014-04-02 16:10:33 +000017#include "ARMConstantPoolValue.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000018#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000019#include "MCTargetDesc/ARMAddressingModes.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Chengeb56dca2010-11-22 18:12:04 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +000025#include "llvm/CodeGen/RegisterScavenging.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Function.h"
Artyom Skrobovf6830f42014-02-14 17:19:07 +000028#include "llvm/MC/MCContext.h"
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000029#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000031
32using namespace llvm;
33
Benjamin Kramer9fceb902012-02-24 22:09:25 +000034static cl::opt<bool>
Jakob Stoklund Olesen68a922c2012-01-06 22:19:37 +000035SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +000036 cl::desc("Align ARM NEON spills in prolog and epilog"));
37
38static MachineBasicBlock::iterator
39skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
40 unsigned NumAlignedDPRCS2Regs);
41
Eric Christopher45fb7b62014-06-26 19:29:59 +000042ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
43 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
44 STI(sti) {}
45
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000046/// hasFP - Return true if the specified function should have a dedicated frame
47/// pointer register. This is true if the function has variable sized allocas
48/// or if frame pointer elimination is disabled.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000049bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +000050 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000051
Evan Cheng801d98b2012-01-04 01:55:04 +000052 // iOS requires FP not to be clobbered for backtracing purpose.
53 if (STI.isTargetIOS())
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000054 return true;
55
56 const MachineFrameInfo *MFI = MF.getFrameInfo();
57 // Always eliminate non-leaf frame pointers.
Nick Lewycky50f02cb2011-12-02 22:16:29 +000058 return ((MF.getTarget().Options.DisableFramePointerElim(MF) &&
59 MFI->hasCalls()) ||
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000060 RegInfo->needsStackRealignment(MF) ||
61 MFI->hasVarSizedObjects() ||
62 MFI->isFrameAddressTaken());
63}
64
Bob Wilson657f2272011-01-13 21:10:12 +000065/// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
66/// not required, we reserve argument space for call sites in the function
67/// immediately on entry to the current function. This eliminates the need for
68/// add/sub sp brackets around call sites. Returns true if the call frame is
69/// included as part of the stack frame.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000070bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000071 const MachineFrameInfo *FFI = MF.getFrameInfo();
72 unsigned CFSize = FFI->getMaxCallFrameSize();
73 // It's not always a good idea to include the call frame as part of the
74 // stack frame. ARM (especially Thumb) has small immediate offset to
75 // address the stack frame. So a large call frame can cause poor codegen
76 // and may even makes it impossible to scavenge a register.
77 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
78 return false;
79
80 return !MF.getFrameInfo()->hasVarSizedObjects();
81}
82
Bob Wilson657f2272011-01-13 21:10:12 +000083/// canSimplifyCallFramePseudos - If there is a reserved call frame, the
84/// call frame pseudos can be simplified. Unlike most targets, having a FP
85/// is not sufficient here since we still may reference some objects via SP
86/// even when FP is available in Thumb2 mode.
87bool
88ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +000089 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
90}
91
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000092static bool isCSRestore(MachineInstr *MI,
93 const ARMBaseInstrInfo &TII,
Craig Topper840beec2014-04-04 05:16:06 +000094 const MCPhysReg *CSRegs) {
Eric Christopherb006fc92010-11-18 19:40:05 +000095 // Integer spill area is handled with "pop".
Tim Northover93bcc662013-11-08 17:18:07 +000096 if (isPopOpcode(MI->getOpcode())) {
Eric Christopherb006fc92010-11-18 19:40:05 +000097 // The first two operands are predicates. The last two are
98 // imp-def and imp-use of SP. Check everything in between.
99 for (int i = 5, e = MI->getNumOperands(); i != e; ++i)
100 if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs))
101 return false;
102 return true;
103 }
Owen Anderson2aedba62011-07-26 20:54:26 +0000104 if ((MI->getOpcode() == ARM::LDR_POST_IMM ||
105 MI->getOpcode() == ARM::LDR_POST_REG ||
Jim Grosbachbdb7ed12010-12-10 18:41:15 +0000106 MI->getOpcode() == ARM::t2LDR_POST) &&
107 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) &&
108 MI->getOperand(1).getReg() == ARM::SP)
109 return true;
Eric Christopherb006fc92010-11-18 19:40:05 +0000110
111 return false;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000112}
113
Tim Northoverc9432eb2013-11-04 23:04:15 +0000114static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
116 const ARMBaseInstrInfo &TII, unsigned DestReg,
117 unsigned SrcReg, int NumBytes,
118 unsigned MIFlags = MachineInstr::NoFlags,
119 ARMCC::CondCodes Pred = ARMCC::AL,
120 unsigned PredReg = 0) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000121 if (isARM)
Tim Northoverc9432eb2013-11-04 23:04:15 +0000122 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000123 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000124 else
Tim Northoverc9432eb2013-11-04 23:04:15 +0000125 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
Eli Bendersky8da87162013-02-21 20:05:00 +0000126 Pred, PredReg, TII, MIFlags);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000127}
128
Tim Northoverc9432eb2013-11-04 23:04:15 +0000129static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
130 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
131 const ARMBaseInstrInfo &TII, int NumBytes,
132 unsigned MIFlags = MachineInstr::NoFlags,
133 ARMCC::CondCodes Pred = ARMCC::AL,
134 unsigned PredReg = 0) {
135 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
136 MIFlags, Pred, PredReg);
137}
138
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000139static int sizeOfSPAdjustment(const MachineInstr *MI) {
140 assert(MI->getOpcode() == ARM::VSTMDDB_UPD);
141 int count = 0;
142 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
143 // pred) so the list starts at 4.
144 for (int i = MI->getNumOperands() - 1; i >= 4; --i)
145 count += 8;
146 return count;
147}
148
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000149static bool WindowsRequiresStackProbe(const MachineFunction &MF,
150 size_t StackSizeInBytes) {
151 const MachineFrameInfo *MFI = MF.getFrameInfo();
152 if (MFI->getStackProtectorIndex() > 0)
153 return StackSizeInBytes >= 4080;
154 return StackSizeInBytes >= 4096;
155}
156
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000157void ARMFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000158 MachineBasicBlock &MBB = MF.front();
159 MachineBasicBlock::iterator MBBI = MBB.begin();
160 MachineFrameInfo *MFI = MF.getFrameInfo();
161 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000162 MachineModuleInfo &MMI = MF.getMMI();
163 MCContext &Context = MMI.getContext();
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000164 const TargetMachine &TM = MF.getTarget();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000165 const MCRegisterInfo *MRI = Context.getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000166 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
167 TM.getSubtargetImpl()->getRegisterInfo());
168 const ARMBaseInstrInfo &TII = *static_cast<const ARMBaseInstrInfo *>(
169 TM.getSubtargetImpl()->getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000170 assert(!AFI->isThumb1OnlyFunction() &&
171 "This emitPrologue does not support Thumb1!");
172 bool isARM = !AFI->isThumbFunction();
Eric Christopherd9134482014-08-04 21:25:23 +0000173 unsigned Align =
174 TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000175 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000176 unsigned NumBytes = MFI->getStackSize();
177 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
178 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
179 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000180 int CFAOffset = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000181
182 // Determine the sizes of each callee-save spill areas and record which frame
183 // belongs to which callee-save spill areas.
184 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
185 int FramePtrSpillFI = 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000186 int D8SpillFI = 0;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000187
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000188 // All calls are tail calls in GHC calling conv, and functions have no
189 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000190 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
191 return;
192
Oliver Stannardd55e1152014-03-05 15:25:27 +0000193 // Allocate the vararg register save area.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000194 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000195 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000196 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000197 CFAOffset -= ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000198 unsigned CFIIndex = MMI.addFrameInst(
199 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
200 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
201 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000202 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000203
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000204 if (!AFI->hasStackFrame() &&
205 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000206 if (NumBytes - ArgRegsSaveSize != 0) {
207 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000208 MachineInstr::FrameSetup);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000209 CFAOffset -= NumBytes - ArgRegsSaveSize;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000210 unsigned CFIIndex = MMI.addFrameInst(
211 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
212 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
213 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000214 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000215 return;
216 }
217
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000218 // Determine spill area sizes.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000219 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
220 unsigned Reg = CSI[i].getReg();
221 int FI = CSI[i].getFrameIdx();
222 switch (Reg) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000223 case ARM::R8:
224 case ARM::R9:
225 case ARM::R10:
226 case ARM::R11:
227 case ARM::R12:
Tim Northover86f60b72014-05-30 13:23:06 +0000228 if (STI.isTargetDarwin()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000229 GPRCS2Size += 4;
230 break;
231 }
232 // fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +0000233 case ARM::R0:
234 case ARM::R1:
235 case ARM::R2:
236 case ARM::R3:
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000237 case ARM::R4:
238 case ARM::R5:
239 case ARM::R6:
240 case ARM::R7:
241 case ARM::LR:
242 if (Reg == FramePtr)
243 FramePtrSpillFI = FI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000244 GPRCS1Size += 4;
245 break;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000246 default:
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000247 // This is a DPR. Exclude the aligned DPRCS2 spills.
248 if (Reg == ARM::D8)
249 D8SpillFI = FI;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000250 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000251 DPRCSSize += 8;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000252 }
253 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000254
Eric Christopherb006fc92010-11-18 19:40:05 +0000255 // Move past area 1.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000256 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push,
257 DPRCSPush;
Tim Northover93bcc662013-11-08 17:18:07 +0000258 if (GPRCS1Size > 0)
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000259 GPRCS1Push = LastPush = MBBI++;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000260
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000261 // Determine starting offsets of spill areas.
Tim Northoverc9432eb2013-11-04 23:04:15 +0000262 bool HasFP = hasFP(MF);
Tim Northover228c9432014-11-05 00:27:13 +0000263 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
264 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
265 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
266 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
267 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
Tim Northover93bcc662013-11-08 17:18:07 +0000268 int FramePtrOffsetInPush = 0;
269 if (HasFP) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000270 FramePtrOffsetInPush = MFI->getObjectOffset(FramePtrSpillFI)
271 + GPRCS1Size + ArgRegsSaveSize;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000272 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
273 NumBytes);
Tim Northover93bcc662013-11-08 17:18:07 +0000274 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000275 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
276 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
277 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
278
Tim Northoverc9432eb2013-11-04 23:04:15 +0000279 // Move past area 2.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000280 if (GPRCS2Size > 0)
281 GPRCS2Push = LastPush = MBBI++;
Tim Northoverc9432eb2013-11-04 23:04:15 +0000282
Tim Northover228c9432014-11-05 00:27:13 +0000283 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
284 // .cfi_offset operations will reflect that.
285 if (DPRGapSize) {
286 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
287 if (!tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize))
288 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
289 MachineInstr::FrameSetup);
290 }
291
Eric Christopherb006fc92010-11-18 19:40:05 +0000292 // Move past area 3.
Evan Cheng70d29632011-02-25 00:24:46 +0000293 if (DPRCSSize > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000294 DPRCSPush = MBBI;
Evan Cheng70d29632011-02-25 00:24:46 +0000295 // Since vpush register list cannot have gaps, there may be multiple vpush
Evan Chenga921dc52011-02-25 01:29:29 +0000296 // instructions in the prologue.
Evan Cheng70d29632011-02-25 00:24:46 +0000297 while (MBBI->getOpcode() == ARM::VSTMDDB_UPD)
Tim Northover93bcc662013-11-08 17:18:07 +0000298 LastPush = MBBI++;
Evan Cheng70d29632011-02-25 00:24:46 +0000299 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000300
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000301 // Move past the aligned DPRCS2 area.
302 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
303 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
304 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
305 // leaves the stack pointer pointing to the DPRCS2 area.
306 //
307 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
308 NumBytes += MFI->getObjectOffset(D8SpillFI);
309 } else
310 NumBytes = DPRCSOffset;
311
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000312 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
313 uint32_t NumWords = NumBytes >> 2;
314
315 if (NumWords < 65536)
316 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000317 .addImm(NumWords)
318 .setMIFlags(MachineInstr::FrameSetup));
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000319 else
320 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000321 .addImm(NumWords)
322 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000323
324 switch (TM.getCodeModel()) {
325 case CodeModel::Small:
326 case CodeModel::Medium:
327 case CodeModel::Default:
328 case CodeModel::Kernel:
329 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
330 .addImm((unsigned)ARMCC::AL).addReg(0)
331 .addExternalSymbol("__chkstk")
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000332 .addReg(ARM::R4, RegState::Implicit)
333 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000334 break;
335 case CodeModel::Large:
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000336 case CodeModel::JITDefault:
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000337 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000338 .addExternalSymbol("__chkstk")
339 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool71583032014-05-01 04:19:59 +0000340
Saleem Abdulrasoolacd03382014-05-07 03:03:27 +0000341 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
342 .addImm((unsigned)ARMCC::AL).addReg(0)
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000343 .addReg(ARM::R12, RegState::Kill)
Saleem Abdulrasool985dcf12014-05-07 03:03:31 +0000344 .addReg(ARM::R4, RegState::Implicit)
345 .setMIFlags(MachineInstr::FrameSetup);
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000346 break;
347 }
Saleem Abdulrasool25947c32014-04-30 07:05:07 +0000348
349 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr),
350 ARM::SP)
351 .addReg(ARM::SP, RegState::Define)
352 .addReg(ARM::R4, RegState::Kill)
353 .setMIFlags(MachineInstr::FrameSetup)));
354 NumBytes = 0;
355 }
356
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000357 unsigned adjustedGPRCS1Size = GPRCS1Size;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000358 if (NumBytes) {
359 // Adjust SP after all the callee-save spills.
Tim Northovera4173712013-12-08 15:56:50 +0000360 if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000361 if (LastPush == GPRCS1Push) {
Tim Northovera4173712013-12-08 15:56:50 +0000362 FramePtrOffsetInPush += NumBytes;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000363 adjustedGPRCS1Size += NumBytes;
364 NumBytes = 0;
365 }
Tim Northovera4173712013-12-08 15:56:50 +0000366 } else
Tim Northover93bcc662013-11-08 17:18:07 +0000367 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
368 MachineInstr::FrameSetup);
369
Evan Chengeb56dca2010-11-22 18:12:04 +0000370 if (HasFP && isARM)
371 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
372 // Note it's not safe to do this in Thumb2 mode because it would have
373 // taken two instructions:
374 // mov sp, r7
375 // sub sp, #24
376 // If an interrupt is taken between the two instructions, then sp is in
377 // an inconsistent state (pointing to the middle of callee-saved area).
378 // The interrupt handler can end up clobbering the registers.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000379 AFI->setShouldRestoreSPFromFP(true);
380 }
381
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000382 if (adjustedGPRCS1Size > 0) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000383 CFAOffset -= adjustedGPRCS1Size;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000384 unsigned CFIIndex = MMI.addFrameInst(
385 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
386 MachineBasicBlock::iterator Pos = ++GPRCS1Push;
387 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
388 .addCFIIndex(CFIIndex);
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000389 for (const auto &Entry : CSI) {
390 unsigned Reg = Entry.getReg();
391 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000392 switch (Reg) {
393 case ARM::R8:
394 case ARM::R9:
395 case ARM::R10:
396 case ARM::R11:
397 case ARM::R12:
Tim Northover86f60b72014-05-30 13:23:06 +0000398 if (STI.isTargetDarwin())
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000399 break;
400 // fallthrough
401 case ARM::R0:
402 case ARM::R1:
403 case ARM::R2:
404 case ARM::R3:
405 case ARM::R4:
406 case ARM::R5:
407 case ARM::R6:
408 case ARM::R7:
409 case ARM::LR:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000410 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
411 nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI)));
412 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
413 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000414 break;
415 }
416 }
417 }
418
Tim Northover93bcc662013-11-08 17:18:07 +0000419 // Set FP to point to the stack slot that contains the previous FP.
420 // For iOS, FP is R7, which has now been stored in spill area 1.
421 // Otherwise, if this is not iOS, all the callee-saved registers go
422 // into spill area 1, including the FP in R11. In either case, it
423 // is in area one and the adjustment needs to take place just after
424 // that push.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000425 if (HasFP) {
426 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, GPRCS1Push, dl, TII,
Tim Northover93bcc662013-11-08 17:18:07 +0000427 FramePtr, ARM::SP, FramePtrOffsetInPush,
428 MachineInstr::FrameSetup);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000429 if (FramePtrOffsetInPush) {
430 CFAOffset += FramePtrOffsetInPush;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000431 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa(
432 nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset));
433 BuildMI(MBB, GPRCS1Push, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
434 .addCFIIndex(CFIIndex);
435
436 } else {
437 unsigned CFIIndex =
438 MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
439 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
440 BuildMI(MBB, GPRCS1Push, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
441 .addCFIIndex(CFIIndex);
442 }
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000443 }
Tim Northover93bcc662013-11-08 17:18:07 +0000444
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000445 if (GPRCS2Size > 0) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000446 MachineBasicBlock::iterator Pos = ++GPRCS2Push;
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000447 if (!HasFP) {
448 CFAOffset -= GPRCS2Size;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000449 unsigned CFIIndex = MMI.addFrameInst(
450 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
451 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
452 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000453 }
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000454 for (const auto &Entry : CSI) {
455 unsigned Reg = Entry.getReg();
456 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000457 switch (Reg) {
458 case ARM::R8:
459 case ARM::R9:
460 case ARM::R10:
461 case ARM::R11:
462 case ARM::R12:
Tim Northover86f60b72014-05-30 13:23:06 +0000463 if (STI.isTargetDarwin()) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000464 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
Oliver Stannardd55e1152014-03-05 15:25:27 +0000465 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000466 unsigned CFIIndex = MMI.addFrameInst(
467 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
468 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
469 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000470 }
471 break;
472 }
473 }
474 }
475
476 if (DPRCSSize > 0) {
477 // Since vpush register list cannot have gaps, there may be multiple vpush
478 // instructions in the prologue.
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000479 do {
480 MachineBasicBlock::iterator Push = DPRCSPush++;
481 if (!HasFP) {
Alp Toker98444342014-04-19 23:56:35 +0000482 CFAOffset -= sizeOfSPAdjustment(Push);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000483 unsigned CFIIndex = MMI.addFrameInst(
484 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
485 BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
486 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000487 }
488 } while (DPRCSPush->getOpcode() == ARM::VSTMDDB_UPD);
489
Jim Grosbachf92e8f52014-04-04 02:10:55 +0000490 for (const auto &Entry : CSI) {
491 unsigned Reg = Entry.getReg();
492 int FI = Entry.getFrameIdx();
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000493 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
494 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
495 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
496 unsigned Offset = MFI->getObjectOffset(FI);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000497 unsigned CFIIndex = MMI.addFrameInst(
498 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
499 BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
500 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000501 }
502 }
503 }
504
505 if (NumBytes) {
506 if (!HasFP) {
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000507 CFAOffset -= NumBytes;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000508 unsigned CFIIndex = MMI.addFrameInst(
509 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
510 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
511 .addCFIIndex(CFIIndex);
Artyom Skrobovf6830f42014-02-14 17:19:07 +0000512 }
513 }
Tim Northover93bcc662013-11-08 17:18:07 +0000514
Evan Chengeb56dca2010-11-22 18:12:04 +0000515 if (STI.isTargetELF() && hasFP(MF))
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000516 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
517 AFI->getFramePtrSpillOffset());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000518
519 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
520 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
Tim Northover228c9432014-11-05 00:27:13 +0000521 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000522 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
523
524 // If we need dynamic stack realignment, do it here. Be paranoid and make
525 // sure if we also have VLAs, we have a base pointer for frame access.
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000526 // If aligned NEON registers were spilled, the stack has already been
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000527 // realigned.
528 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000529 unsigned MaxAlign = MFI->getMaxAlignment();
530 assert (!AFI->isThumb1OnlyFunction());
531 if (!AFI->isThumbFunction()) {
532 // Emit bic sp, sp, MaxAlign
533 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
534 TII.get(ARM::BICri), ARM::SP)
535 .addReg(ARM::SP, RegState::Kill)
536 .addImm(MaxAlign-1)));
537 } else {
538 // We cannot use sp as source/dest register here, thus we're emitting the
539 // following sequence:
540 // mov r4, sp
541 // bic r4, r4, MaxAlign
542 // mov sp, r4
543 // FIXME: It will be better just to find spare register here.
Jim Grosbache9cc9012011-06-30 23:38:17 +0000544 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000545 .addReg(ARM::SP, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000546 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
547 TII.get(ARM::t2BICri), ARM::R4)
548 .addReg(ARM::R4, RegState::Kill)
549 .addImm(MaxAlign-1)));
Jim Grosbache9cc9012011-06-30 23:38:17 +0000550 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
Jim Grosbachb98ab912011-06-30 22:10:46 +0000551 .addReg(ARM::R4, RegState::Kill));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000552 }
553
554 AFI->setShouldRestoreSPFromFP(true);
555 }
556
557 // If we need a base pointer, set it up here. It's whatever the value
558 // of the stack pointer is at this point. Any variable size objects
559 // will be allocated after this, so we can still use the base pointer
560 // to reference locals.
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000561 // FIXME: Clarify FrameSetup flags here.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000562 if (RegInfo->hasBasePointer(MF)) {
563 if (isARM)
564 BuildMI(MBB, MBBI, dl,
565 TII.get(ARM::MOVr), RegInfo->getBaseRegister())
566 .addReg(ARM::SP)
567 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
568 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000569 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000570 RegInfo->getBaseRegister())
571 .addReg(ARM::SP));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000572 }
573
574 // If the frame has variable sized objects then the epilogue must restore
Eric Christopherd5bbeba2011-01-10 23:10:59 +0000575 // the sp from fp. We can assume there's an FP here since hasFP already
576 // checks for hasVarSizedObjects.
Evan Chengeb56dca2010-11-22 18:12:04 +0000577 if (MFI->hasVarSizedObjects())
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000578 AFI->setShouldRestoreSPFromFP(true);
579}
580
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000581void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +0000582 MachineBasicBlock &MBB) const {
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000583 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000584 assert(MBBI->isReturn() && "Can only insert epilog into returning blocks");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000585 unsigned RetOpcode = MBBI->getOpcode();
586 DebugLoc dl = MBBI->getDebugLoc();
587 MachineFrameInfo *MFI = MF.getFrameInfo();
588 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Eric Christopherfc6de422014-08-05 02:39:49 +0000589 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000590 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000591 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000592 assert(!AFI->isThumb1OnlyFunction() &&
593 "This emitEpilogue does not support Thumb1!");
594 bool isARM = !AFI->isThumbFunction();
595
Eric Christopherd9134482014-08-04 21:25:23 +0000596 unsigned Align = MF.getTarget()
597 .getSubtargetImpl()
598 ->getFrameLowering()
599 ->getStackAlignment();
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +0000600 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000601 int NumBytes = (int)MFI->getStackSize();
602 unsigned FramePtr = RegInfo->getFrameRegister(MF);
603
Jakob Stoklund Olesene3801832012-10-26 21:46:57 +0000604 // All calls are tail calls in GHC calling conv, and functions have no
605 // prologue/epilogue.
Eric Christopherb3322362012-08-03 00:05:53 +0000606 if (MF.getFunction()->getCallingConv() == CallingConv::GHC)
607 return;
608
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000609 if (!AFI->hasStackFrame()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +0000610 if (NumBytes - ArgRegsSaveSize != 0)
611 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000612 } else {
613 // Unwind MBBI to point to first LDR / VLDRD.
Craig Topper840beec2014-04-04 05:16:06 +0000614 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000615 if (MBBI != MBB.begin()) {
Tim Northover93bcc662013-11-08 17:18:07 +0000616 do {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000617 --MBBI;
Tim Northover93bcc662013-11-08 17:18:07 +0000618 } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000619 if (!isCSRestore(MBBI, TII, CSRegs))
620 ++MBBI;
621 }
622
623 // Move SP to start of FP callee save spill area.
Oliver Stannardd55e1152014-03-05 15:25:27 +0000624 NumBytes -= (ArgRegsSaveSize +
625 AFI->getGPRCalleeSavedArea1Size() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000626 AFI->getGPRCalleeSavedArea2Size() +
Tim Northover228c9432014-11-05 00:27:13 +0000627 AFI->getDPRCalleeSavedGapSize() +
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000628 AFI->getDPRCalleeSavedAreaSize());
629
630 // Reset SP based on frame pointer only if the stack frame extends beyond
631 // frame pointer stack slot or target is ELF and the function has FP.
632 if (AFI->shouldRestoreSPFromFP()) {
633 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
634 if (NumBytes) {
635 if (isARM)
636 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
637 ARMCC::AL, 0, TII);
Evan Chengeb56dca2010-11-22 18:12:04 +0000638 else {
639 // It's not possible to restore SP from FP in a single instruction.
Evan Cheng801d98b2012-01-04 01:55:04 +0000640 // For iOS, this looks like:
Evan Chengeb56dca2010-11-22 18:12:04 +0000641 // mov sp, r7
642 // sub sp, #24
643 // This is bad, if an interrupt is taken after the mov, sp is in an
644 // inconsistent state.
645 // Use the first callee-saved register as a scratch register.
Kaelyn Uhrain271fbb62012-10-26 23:28:41 +0000646 assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) &&
Evan Chengeb56dca2010-11-22 18:12:04 +0000647 "No scratch register to restore SP from FP!");
648 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000649 ARMCC::AL, 0, TII);
Jim Grosbache9cc9012011-06-30 23:38:17 +0000650 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000651 ARM::SP)
652 .addReg(ARM::R4));
Evan Chengeb56dca2010-11-22 18:12:04 +0000653 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000654 } else {
655 // Thumb2 or ARM.
656 if (isARM)
657 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
658 .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
659 else
Jim Grosbache9cc9012011-06-30 23:38:17 +0000660 AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr),
Jim Grosbachb98ab912011-06-30 22:10:46 +0000661 ARM::SP)
662 .addReg(FramePtr));
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000663 }
Tim Northoverdee86042013-12-02 14:46:26 +0000664 } else if (NumBytes &&
Tim Northovere4def5e2013-12-05 11:02:02 +0000665 !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes))
Tim Northover93bcc662013-11-08 17:18:07 +0000666 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000667
Eric Christopherb006fc92010-11-18 19:40:05 +0000668 // Increment past our save areas.
Evan Cheng70d29632011-02-25 00:24:46 +0000669 if (AFI->getDPRCalleeSavedAreaSize()) {
670 MBBI++;
671 // Since vpop register list cannot have gaps, there may be multiple vpop
672 // instructions in the epilogue.
673 while (MBBI->getOpcode() == ARM::VLDMDIA_UPD)
674 MBBI++;
675 }
Tim Northover228c9432014-11-05 00:27:13 +0000676 if (AFI->getDPRCalleeSavedGapSize()) {
677 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
678 "unexpected DPR alignment gap");
679 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
680 }
681
Eric Christopherb006fc92010-11-18 19:40:05 +0000682 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
683 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000684 }
685
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000686 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000687 // Tail call return: adjust the stack pointer and jump to callee.
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000688 MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000689 MachineOperand &JumpTarget = MBBI->getOperand(0);
690
691 // Jump to label or value in register.
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000692 if (RetOpcode == ARM::TCRETURNdi) {
693 unsigned TCOpcode = STI.isThumb() ?
Tim Northoverd6a729b2014-01-06 14:28:05 +0000694 (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) :
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000695 ARM::TAILJMPd;
Evan Chengd4b08732010-11-30 23:55:39 +0000696 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode));
697 if (JumpTarget.isGlobal())
698 MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
699 JumpTarget.getTargetFlags());
700 else {
701 assert(JumpTarget.isSymbol());
702 MIB.addExternalSymbol(JumpTarget.getSymbolName(),
703 JumpTarget.getTargetFlags());
704 }
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000705
706 // Add the default predicate in Thumb mode.
707 if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000708 } else if (RetOpcode == ARM::TCRETURNri) {
Jim Grosbach3af6fe62011-03-15 00:30:40 +0000709 BuildMI(MBB, MBBI, dl,
710 TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)).
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000711 addReg(JumpTarget.getReg(), RegState::Kill);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000712 }
713
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000714 MachineInstr *NewMI = std::prev(MBBI);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000715 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
716 NewMI->addOperand(MBBI->getOperand(i));
717
718 // Delete the pseudo instruction TCRETURN.
719 MBB.erase(MBBI);
Cameron Zwarich033026f2011-06-17 02:16:43 +0000720 MBBI = NewMI;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000721 }
722
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +0000723 if (ArgRegsSaveSize)
724 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000725}
Anton Korobeynikov46877782010-11-20 15:59:32 +0000726
Bob Wilson657f2272011-01-13 21:10:12 +0000727/// getFrameIndexReference - Provide a base+offset reference to an FI slot for
728/// debug info. It's the same as what we use for resolving the code-gen
729/// references for now. FIXME: This can go wrong when references are
730/// SP-relative and simple call frames aren't used.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000731int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000732ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
Bob Wilson657f2272011-01-13 21:10:12 +0000733 unsigned &FrameReg) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000734 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
735}
736
737int
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000738ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
Evan Chengc0d20042011-04-22 01:42:52 +0000739 int FI, unsigned &FrameReg,
Bob Wilson657f2272011-01-13 21:10:12 +0000740 int SPAdj) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000741 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000742 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000743 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov46877782010-11-20 15:59:32 +0000744 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
745 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
746 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
747 bool isFixed = MFI->isFixedObjectIndex(FI);
748
749 FrameReg = ARM::SP;
750 Offset += SPAdj;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000751
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000752 // SP can move around if there are allocas. We may also lose track of SP
753 // when emergency spilling inside a non-reserved call frame setup.
Bob Wilsonca690322012-03-20 19:28:22 +0000754 bool hasMovingSP = !hasReservedCallFrame(MF);
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000755
Anton Korobeynikov46877782010-11-20 15:59:32 +0000756 // When dynamically realigning the stack, use the frame pointer for
757 // parameters, and the stack/base pointer for locals.
758 if (RegInfo->needsStackRealignment(MF)) {
759 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
760 if (isFixed) {
761 FrameReg = RegInfo->getFrameRegister(MF);
762 Offset = FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000763 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000764 assert(RegInfo->hasBasePointer(MF) &&
765 "VLAs and dynamic stack alignment, but missing base pointer!");
766 FrameReg = RegInfo->getBaseRegister();
767 }
768 return Offset;
769 }
770
771 // If there is a frame pointer, use it when we can.
772 if (hasFP(MF) && AFI->hasStackFrame()) {
773 // Use frame pointer to reference fixed objects. Use it for locals if
774 // there are VLAs (and thus the SP isn't reliable as a base).
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000775 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000776 FrameReg = RegInfo->getFrameRegister(MF);
777 return FPOffset;
Jakob Stoklund Olesen92c15b22012-02-28 01:15:01 +0000778 } else if (hasMovingSP) {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000779 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
Anton Korobeynikov46877782010-11-20 15:59:32 +0000780 if (AFI->isThumb2Function()) {
Evan Chengc0d20042011-04-22 01:42:52 +0000781 // Try to use the frame pointer if we can, else use the base pointer
782 // since it's available. This is handy for the emergency spill slot, in
783 // particular.
Anton Korobeynikov46877782010-11-20 15:59:32 +0000784 if (FPOffset >= -255 && FPOffset < 0) {
785 FrameReg = RegInfo->getFrameRegister(MF);
786 return FPOffset;
787 }
Evan Chengc0d20042011-04-22 01:42:52 +0000788 }
Anton Korobeynikov46877782010-11-20 15:59:32 +0000789 } else if (AFI->isThumb2Function()) {
Andrew Trickf7ecc162011-08-25 17:40:54 +0000790 // Use add <rd>, sp, #<imm8>
Evan Chengc0d20042011-04-22 01:42:52 +0000791 // ldr <rd>, [sp, #<imm8>]
792 // if at all possible to save space.
793 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
794 return Offset;
Anton Korobeynikov46877782010-11-20 15:59:32 +0000795 // In Thumb2 mode, the negative offset is very limited. Try to avoid
Evan Chengc0d20042011-04-22 01:42:52 +0000796 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
Anton Korobeynikov46877782010-11-20 15:59:32 +0000797 if (FPOffset >= -255 && FPOffset < 0) {
798 FrameReg = RegInfo->getFrameRegister(MF);
799 return FPOffset;
800 }
801 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
802 // Otherwise, use SP or FP, whichever is closer to the stack slot.
803 FrameReg = RegInfo->getFrameRegister(MF);
804 return FPOffset;
805 }
806 }
807 // Use the base pointer if we have one.
808 if (RegInfo->hasBasePointer(MF))
809 FrameReg = RegInfo->getBaseRegister();
810 return Offset;
811}
812
Bob Wilson657f2272011-01-13 21:10:12 +0000813int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
814 int FI) const {
Anton Korobeynikov46877782010-11-20 15:59:32 +0000815 unsigned FrameReg;
816 return getFrameIndexReference(MF, FI, FrameReg);
817}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000818
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000819void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000820 MachineBasicBlock::iterator MI,
821 const std::vector<CalleeSavedInfo> &CSI,
822 unsigned StmOpc, unsigned StrOpc,
823 bool NoGap,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000824 bool(*Func)(unsigned, bool),
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000825 unsigned NumAlignedDPRCS2Regs,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000826 unsigned MIFlags) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000827 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000828 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000829
830 DebugLoc DL;
831 if (MI != MBB.end()) DL = MI->getDebugLoc();
832
Evan Chengc27c9562010-12-07 19:59:34 +0000833 SmallVector<std::pair<unsigned,bool>, 4> Regs;
Evan Cheng775ead32010-12-07 23:08:38 +0000834 unsigned i = CSI.size();
835 while (i != 0) {
836 unsigned LastReg = 0;
837 for (; i != 0; --i) {
838 unsigned Reg = CSI[i-1].getReg();
Tim Northover86f60b72014-05-30 13:23:06 +0000839 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000840
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000841 // D-registers in the aligned area DPRCS2 are NOT spilled here.
842 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
843 continue;
844
Evan Cheng775ead32010-12-07 23:08:38 +0000845 // Add the callee-saved register as live-in unless it's LR and
Jim Grosbachc0b669f2010-12-09 16:14:46 +0000846 // @llvm.returnaddress is called. If LR is returned for
847 // @llvm.returnaddress then it's already added to the function and
848 // entry block live-in sets.
Evan Cheng775ead32010-12-07 23:08:38 +0000849 bool isKill = true;
850 if (Reg == ARM::LR) {
851 if (MF.getFrameInfo()->isReturnAddressTaken() &&
852 MF.getRegInfo().isLiveIn(Reg))
853 isKill = false;
854 }
855
856 if (isKill)
857 MBB.addLiveIn(Reg);
858
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000859 // If NoGap is true, push consecutive registers and then leave the rest
Evan Cheng9d54ae62010-12-08 06:29:02 +0000860 // for other instructions. e.g.
Eric Christopher2a2e65c2010-12-09 01:57:45 +0000861 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
Evan Cheng9d54ae62010-12-08 06:29:02 +0000862 if (NoGap && LastReg && LastReg != Reg-1)
863 break;
Evan Cheng775ead32010-12-07 23:08:38 +0000864 LastReg = Reg;
865 Regs.push_back(std::make_pair(Reg, isKill));
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000866 }
867
Jim Grosbach5fccad82010-12-09 18:31:13 +0000868 if (Regs.empty())
869 continue;
870 if (Regs.size() > 1 || StrOpc== 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000871 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000872 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +0000873 .addReg(ARM::SP).setMIFlags(MIFlags));
Evan Cheng775ead32010-12-07 23:08:38 +0000874 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
875 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
Jim Grosbach5fccad82010-12-09 18:31:13 +0000876 } else if (Regs.size() == 1) {
877 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc),
878 ARM::SP)
879 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +0000880 .addReg(ARM::SP).setMIFlags(MIFlags)
881 .addImm(-4);
Jim Grosbach5fccad82010-12-09 18:31:13 +0000882 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000883 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000884 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +0000885
886 // Put any subsequent vpush instructions before this one: they will refer to
887 // higher register numbers so need to be pushed first in order to preserve
888 // monotonicity.
889 --MI;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000890 }
Evan Cheng775ead32010-12-07 23:08:38 +0000891}
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000892
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000893void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +0000894 MachineBasicBlock::iterator MI,
895 const std::vector<CalleeSavedInfo> &CSI,
896 unsigned LdmOpc, unsigned LdrOpc,
897 bool isVarArg, bool NoGap,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000898 bool(*Func)(unsigned, bool),
899 unsigned NumAlignedDPRCS2Regs) const {
Evan Cheng775ead32010-12-07 23:08:38 +0000900 MachineFunction &MF = *MBB.getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +0000901 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Evan Cheng775ead32010-12-07 23:08:38 +0000902 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
903 DebugLoc DL = MI->getDebugLoc();
Evan Chengd6093ff2011-01-25 01:28:33 +0000904 unsigned RetOpcode = MI->getOpcode();
905 bool isTailCall = (RetOpcode == ARM::TCRETURNdi ||
Jakob Stoklund Olesenb4bd3882012-04-06 21:17:42 +0000906 RetOpcode == ARM::TCRETURNri);
Tim Northoverd8407452013-10-01 14:33:28 +0000907 bool isInterrupt =
908 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
Evan Cheng775ead32010-12-07 23:08:38 +0000909
910 SmallVector<unsigned, 4> Regs;
911 unsigned i = CSI.size();
912 while (i != 0) {
913 unsigned LastReg = 0;
914 bool DeleteRet = false;
915 for (; i != 0; --i) {
916 unsigned Reg = CSI[i-1].getReg();
Tim Northover86f60b72014-05-30 13:23:06 +0000917 if (!(Func)(Reg, STI.isTargetDarwin())) continue;
Evan Cheng775ead32010-12-07 23:08:38 +0000918
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000919 // The aligned reloads from area DPRCS2 are not inserted here.
920 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
921 continue;
922
Tim Northoverd8407452013-10-01 14:33:28 +0000923 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
924 STI.hasV5TOps()) {
Evan Cheng775ead32010-12-07 23:08:38 +0000925 Reg = ARM::PC;
Jim Grosbach5fccad82010-12-09 18:31:13 +0000926 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
Evan Cheng775ead32010-12-07 23:08:38 +0000927 // Fold the return instruction into the LDM.
928 DeleteRet = true;
929 }
930
Evan Cheng9d54ae62010-12-08 06:29:02 +0000931 // If NoGap is true, pop consecutive registers and then leave the rest
932 // for other instructions. e.g.
933 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
934 if (NoGap && LastReg && LastReg != Reg-1)
935 break;
936
Evan Cheng775ead32010-12-07 23:08:38 +0000937 LastReg = Reg;
938 Regs.push_back(Reg);
939 }
940
Jim Grosbach5fccad82010-12-09 18:31:13 +0000941 if (Regs.empty())
942 continue;
943 if (Regs.size() > 1 || LdrOpc == 0) {
Evan Cheng775ead32010-12-07 23:08:38 +0000944 MachineInstrBuilder MIB =
Jim Grosbach5fccad82010-12-09 18:31:13 +0000945 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
Evan Cheng775ead32010-12-07 23:08:38 +0000946 .addReg(ARM::SP));
947 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
948 MIB.addReg(Regs[i], getDefRegState(true));
Andrew Trick6446bf72011-08-25 17:50:53 +0000949 if (DeleteRet) {
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +0000950 MIB.copyImplicitOps(&*MI);
Evan Cheng775ead32010-12-07 23:08:38 +0000951 MI->eraseFromParent();
Andrew Trick6446bf72011-08-25 17:50:53 +0000952 }
Evan Cheng775ead32010-12-07 23:08:38 +0000953 MI = MIB;
Jim Grosbach5fccad82010-12-09 18:31:13 +0000954 } else if (Regs.size() == 1) {
955 // If we adjusted the reg to PC from LR above, switch it back here. We
956 // only do that for LDM.
957 if (Regs[0] == ARM::PC)
958 Regs[0] = ARM::LR;
959 MachineInstrBuilder MIB =
960 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
961 .addReg(ARM::SP, RegState::Define)
962 .addReg(ARM::SP);
963 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
964 // that refactoring is complete (eventually).
Owen Anderson2aedba62011-07-26 20:54:26 +0000965 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
Jim Grosbach5fccad82010-12-09 18:31:13 +0000966 MIB.addReg(0);
967 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
968 } else
969 MIB.addImm(4);
970 AddDefaultPred(MIB);
Evan Cheng775ead32010-12-07 23:08:38 +0000971 }
Jim Grosbach5fccad82010-12-09 18:31:13 +0000972 Regs.clear();
Tim Northover3cccc452014-03-12 11:29:23 +0000973
974 // Put any subsequent vpop instructions after this one: they will refer to
975 // higher register numbers so need to be popped afterwards.
976 ++MI;
Evan Chengc27c9562010-12-07 19:59:34 +0000977 }
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +0000978}
979
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000980/// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
Jakob Stoklund Olesen103318e2011-12-24 04:17:01 +0000981/// starting from d8. Also insert stack realignment code and leave the stack
982/// pointer pointing to the d8 spill slot.
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000983static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
984 MachineBasicBlock::iterator MI,
985 unsigned NumAlignedDPRCS2Regs,
986 const std::vector<CalleeSavedInfo> &CSI,
987 const TargetRegisterInfo *TRI) {
988 MachineFunction &MF = *MBB.getParent();
989 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
990 DebugLoc DL = MI->getDebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +0000991 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +0000992 MachineFrameInfo &MFI = *MF.getFrameInfo();
993
994 // Mark the D-register spill slots as properly aligned. Since MFI computes
995 // stack slot layout backwards, this can actually mean that the d-reg stack
996 // slot offsets can be wrong. The offset for d8 will always be correct.
997 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
998 unsigned DNum = CSI[i].getReg() - ARM::D8;
999 if (DNum >= 8)
1000 continue;
1001 int FI = CSI[i].getFrameIdx();
1002 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1003 // registers will be 8-byte aligned.
1004 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1005
1006 // The stack slot for D8 needs to be maximally aligned because this is
1007 // actually the point where we align the stack pointer. MachineFrameInfo
1008 // computes all offsets relative to the incoming stack pointer which is a
1009 // bit weird when realigning the stack. Any extra padding for this
1010 // over-alignment is not realized because the code inserted below adjusts
1011 // the stack pointer by numregs * 8 before aligning the stack pointer.
1012 if (DNum == 0)
1013 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1014 }
1015
1016 // Move the stack pointer to the d8 spill slot, and align it at the same
1017 // time. Leave the stack slot address in the scratch register r4.
1018 //
1019 // sub r4, sp, #numregs * 8
1020 // bic r4, r4, #align - 1
1021 // mov sp, r4
1022 //
1023 bool isThumb = AFI->isThumbFunction();
1024 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1025 AFI->setShouldRestoreSPFromFP(true);
1026
1027 // sub r4, sp, #numregs * 8
1028 // The immediate is <= 64, so it doesn't need any special encoding.
1029 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1030 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1031 .addReg(ARM::SP)
1032 .addImm(8 * NumAlignedDPRCS2Regs)));
1033
1034 // bic r4, r4, #align-1
1035 Opc = isThumb ? ARM::t2BICri : ARM::BICri;
1036 unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment();
1037 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1038 .addReg(ARM::R4, RegState::Kill)
1039 .addImm(MaxAlign - 1)));
1040
1041 // mov sp, r4
1042 // The stack pointer must be adjusted before spilling anything, otherwise
1043 // the stack slots could be clobbered by an interrupt handler.
1044 // Leave r4 live, it is used below.
1045 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1046 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1047 .addReg(ARM::R4);
1048 MIB = AddDefaultPred(MIB);
1049 if (!isThumb)
1050 AddDefaultCC(MIB);
1051
1052 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1053 // r4 holds the stack slot address.
1054 unsigned NextReg = ARM::D8;
1055
1056 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1057 // The writeback is only needed when emitting two vst1.64 instructions.
1058 if (NumAlignedDPRCS2Regs >= 6) {
1059 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001060 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001061 MBB.addLiveIn(SupReg);
1062 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed),
1063 ARM::R4)
1064 .addReg(ARM::R4, RegState::Kill).addImm(16)
1065 .addReg(NextReg)
1066 .addReg(SupReg, RegState::ImplicitKill));
1067 NextReg += 4;
1068 NumAlignedDPRCS2Regs -= 4;
1069 }
1070
1071 // We won't modify r4 beyond this point. It currently points to the next
1072 // register to be spilled.
1073 unsigned R4BaseReg = NextReg;
1074
1075 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1076 if (NumAlignedDPRCS2Regs >= 4) {
1077 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001078 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001079 MBB.addLiveIn(SupReg);
1080 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1081 .addReg(ARM::R4).addImm(16).addReg(NextReg)
1082 .addReg(SupReg, RegState::ImplicitKill));
1083 NextReg += 4;
1084 NumAlignedDPRCS2Regs -= 4;
1085 }
1086
1087 // 16-byte aligned vst1.64 with 2 d-regs.
1088 if (NumAlignedDPRCS2Regs >= 2) {
1089 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001090 &ARM::QPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001091 MBB.addLiveIn(SupReg);
1092 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001093 .addReg(ARM::R4).addImm(16).addReg(SupReg));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001094 NextReg += 2;
1095 NumAlignedDPRCS2Regs -= 2;
1096 }
1097
1098 // Finally, use a vanilla vstr.64 for the odd last register.
1099 if (NumAlignedDPRCS2Regs) {
1100 MBB.addLiveIn(NextReg);
1101 // vstr.64 uses addrmode5 which has an offset scale of 4.
1102 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1103 .addReg(NextReg)
1104 .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2));
1105 }
1106
1107 // The last spill instruction inserted should kill the scratch register r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001108 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001109}
1110
1111/// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1112/// iterator to the following instruction.
1113static MachineBasicBlock::iterator
1114skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1115 unsigned NumAlignedDPRCS2Regs) {
1116 // sub r4, sp, #numregs * 8
1117 // bic r4, r4, #align - 1
1118 // mov sp, r4
1119 ++MI; ++MI; ++MI;
1120 assert(MI->mayStore() && "Expecting spill instruction");
1121
1122 // These switches all fall through.
1123 switch(NumAlignedDPRCS2Regs) {
1124 case 7:
1125 ++MI;
1126 assert(MI->mayStore() && "Expecting spill instruction");
1127 default:
1128 ++MI;
1129 assert(MI->mayStore() && "Expecting spill instruction");
1130 case 1:
1131 case 2:
1132 case 4:
1133 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1134 ++MI;
1135 }
1136 return MI;
1137}
1138
1139/// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1140/// starting from d8. These instructions are assumed to execute while the
1141/// stack is still aligned, unlike the code inserted by emitPopInst.
1142static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1143 MachineBasicBlock::iterator MI,
1144 unsigned NumAlignedDPRCS2Regs,
1145 const std::vector<CalleeSavedInfo> &CSI,
1146 const TargetRegisterInfo *TRI) {
1147 MachineFunction &MF = *MBB.getParent();
1148 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1149 DebugLoc DL = MI->getDebugLoc();
Eric Christopherfc6de422014-08-05 02:39:49 +00001150 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001151
1152 // Find the frame index assigned to d8.
1153 int D8SpillFI = 0;
1154 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1155 if (CSI[i].getReg() == ARM::D8) {
1156 D8SpillFI = CSI[i].getFrameIdx();
1157 break;
1158 }
1159
1160 // Materialize the address of the d8 spill slot into the scratch register r4.
1161 // This can be fairly complicated if the stack frame is large, so just use
1162 // the normal frame index elimination mechanism to do it. This code runs as
1163 // the initial part of the epilog where the stack and base pointers haven't
1164 // been changed yet.
1165 bool isThumb = AFI->isThumbFunction();
1166 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1167
1168 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1169 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1170 .addFrameIndex(D8SpillFI).addImm(0)));
1171
1172 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1173 unsigned NextReg = ARM::D8;
1174
1175 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1176 if (NumAlignedDPRCS2Regs >= 6) {
1177 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001178 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001179 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1180 .addReg(ARM::R4, RegState::Define)
1181 .addReg(ARM::R4, RegState::Kill).addImm(16)
1182 .addReg(SupReg, RegState::ImplicitDefine));
1183 NextReg += 4;
1184 NumAlignedDPRCS2Regs -= 4;
1185 }
1186
1187 // We won't modify r4 beyond this point. It currently points to the next
1188 // register to be spilled.
1189 unsigned R4BaseReg = NextReg;
1190
1191 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1192 if (NumAlignedDPRCS2Regs >= 4) {
1193 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001194 &ARM::QQPRRegClass);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001195 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1196 .addReg(ARM::R4).addImm(16)
1197 .addReg(SupReg, RegState::ImplicitDefine));
1198 NextReg += 4;
1199 NumAlignedDPRCS2Regs -= 4;
1200 }
1201
1202 // 16-byte aligned vld1.64 with 2 d-regs.
1203 if (NumAlignedDPRCS2Regs >= 2) {
1204 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
Craig Topperc7242e02012-04-20 07:30:17 +00001205 &ARM::QPRRegClass);
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001206 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1207 .addReg(ARM::R4).addImm(16));
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001208 NextReg += 2;
1209 NumAlignedDPRCS2Regs -= 2;
1210 }
1211
1212 // Finally, use a vanilla vldr.64 for the remaining odd register.
1213 if (NumAlignedDPRCS2Regs)
1214 AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1215 .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg)));
1216
1217 // Last store kills r4.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001218 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001219}
1220
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001221bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001222 MachineBasicBlock::iterator MI,
1223 const std::vector<CalleeSavedInfo> &CSI,
1224 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001225 if (CSI.empty())
1226 return false;
1227
1228 MachineFunction &MF = *MBB.getParent();
1229 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001230
1231 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001232 unsigned PushOneOpc = AFI->isThumbFunction() ?
1233 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001234 unsigned FltOpc = ARM::VSTMDDB_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001235 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1236 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001237 MachineInstr::FrameSetup);
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001238 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001239 MachineInstr::FrameSetup);
1240 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001241 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1242
1243 // The code above does not insert spill code for the aligned DPRCS2 registers.
1244 // The stack realignment code will be inserted between the push instructions
1245 // and these spills.
1246 if (NumAlignedDPRCS2Regs)
1247 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001248
1249 return true;
1250}
1251
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001252bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bob Wilson657f2272011-01-13 21:10:12 +00001253 MachineBasicBlock::iterator MI,
1254 const std::vector<CalleeSavedInfo> &CSI,
1255 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001256 if (CSI.empty())
1257 return false;
1258
1259 MachineFunction &MF = *MBB.getParent();
1260 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001261 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001262 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1263
1264 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1265 // registers. Do that here instead.
1266 if (NumAlignedDPRCS2Regs)
1267 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001268
1269 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
Jim Grosbach05dec8b12011-09-02 18:46:15 +00001270 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001271 unsigned FltOpc = ARM::VLDMDIA_UPD;
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001272 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1273 NumAlignedDPRCS2Regs);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001274 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001275 &isARMArea2Register, 0);
Jim Grosbach5fccad82010-12-09 18:31:13 +00001276 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001277 &isARMArea1Register, 0);
Anton Korobeynikovd08fbd12010-11-27 23:05:03 +00001278
1279 return true;
1280}
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001281
1282// FIXME: Make generic?
1283static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
1284 const ARMBaseInstrInfo &TII) {
1285 unsigned FnSize = 0;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001286 for (auto &MBB : MF) {
1287 for (auto &MI : MBB)
1288 FnSize += TII.GetInstSizeInBytes(&MI);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001289 }
1290 return FnSize;
1291}
1292
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001293/// estimateRSStackSizeLimit - Look at each instruction that references stack
1294/// frames and return the stack size limit beyond which some of these
1295/// instructions will require a scratch register during their expansion later.
1296// FIXME: Move to TII?
1297static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001298 const TargetFrameLowering *TFI) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001299 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1300 unsigned Limit = (1 << 12) - 1;
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001301 for (auto &MBB : MF) {
1302 for (auto &MI : MBB) {
1303 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1304 if (!MI.getOperand(i).isFI())
1305 continue;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001306
1307 // When using ADDri to get the address of a stack object, 255 is the
1308 // largest offset guaranteed to fit in the immediate offset.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001309 if (MI.getOpcode() == ARM::ADDri) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001310 Limit = std::min(Limit, (1U << 8) - 1);
1311 break;
1312 }
1313
1314 // Otherwise check the addressing mode.
Jim Grosbachf92e8f52014-04-04 02:10:55 +00001315 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001316 case ARMII::AddrMode3:
1317 case ARMII::AddrModeT2_i8:
1318 Limit = std::min(Limit, (1U << 8) - 1);
1319 break;
1320 case ARMII::AddrMode5:
1321 case ARMII::AddrModeT2_i8s4:
1322 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1323 break;
1324 case ARMII::AddrModeT2_i12:
1325 // i12 supports only positive offset so these will be converted to
1326 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1327 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1328 Limit = std::min(Limit, (1U << 8) - 1);
1329 break;
1330 case ARMII::AddrMode4:
1331 case ARMII::AddrMode6:
1332 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1333 // immediate offset for stack references.
1334 return 0;
1335 default:
1336 break;
1337 }
1338 break; // At most one FI per instruction
1339 }
1340 }
1341 }
1342
1343 return Limit;
1344}
1345
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001346// In functions that realign the stack, it can be an advantage to spill the
1347// callee-saved vector registers after realigning the stack. The vst1 and vld1
1348// instructions take alignment hints that can improve performance.
1349//
1350static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) {
1351 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1352 if (!SpillAlignedNEONRegs)
1353 return;
1354
1355 // Naked functions don't spill callee-saved registers.
Bill Wendling698e84f2012-12-30 10:32:01 +00001356 if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1357 Attribute::Naked))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001358 return;
1359
1360 // We are planning to use NEON instructions vst1 / vld1.
1361 if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON())
1362 return;
1363
1364 // Don't bother if the default stack alignment is sufficiently high.
Eric Christopherd9134482014-08-04 21:25:23 +00001365 if (MF.getTarget()
1366 .getSubtargetImpl()
1367 ->getFrameLowering()
1368 ->getStackAlignment() >= 8)
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001369 return;
1370
1371 // Aligned spills require stack realignment.
Eric Christopherd9134482014-08-04 21:25:23 +00001372 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001373 MF.getSubtarget().getRegisterInfo());
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001374 if (!RegInfo->canRealignStack(MF))
1375 return;
1376
1377 // We always spill contiguous d-registers starting from d8. Count how many
1378 // needs spilling. The register allocator will almost always use the
1379 // callee-saved registers in order, but it can happen that there are holes in
1380 // the range. Registers above the hole will be spilled to the standard DPRCS
1381 // area.
1382 MachineRegisterInfo &MRI = MF.getRegInfo();
1383 unsigned NumSpills = 0;
1384 for (; NumSpills < 8; ++NumSpills)
Jakob Stoklund Olesen07364422012-10-17 18:44:18 +00001385 if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills))
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001386 break;
1387
1388 // Don't do this for just one d-register. It's not worth it.
1389 if (NumSpills < 2)
1390 return;
1391
1392 // Spill the first NumSpills D-registers after realigning the stack.
1393 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1394
1395 // A scratch register is required for the vst1 / vld1 instructions.
1396 MF.getRegInfo().setPhysRegUsed(ARM::R4);
1397}
1398
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001399void
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001400ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
Bob Wilson657f2272011-01-13 21:10:12 +00001401 RegScavenger *RS) const {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001402 // This tells PEI to spill the FP as if it is any other callee-save register
1403 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1404 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1405 // to combine multiple loads / stores.
1406 bool CanEliminateFrame = true;
1407 bool CS1Spilled = false;
1408 bool LRSpilled = false;
1409 unsigned NumGPRSpills = 0;
1410 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1411 SmallVector<unsigned, 4> UnspilledCS2GPRs;
Eric Christopherd9134482014-08-04 21:25:23 +00001412 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +00001413 MF.getSubtarget().getRegisterInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001414 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001415 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001416 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1417 MachineFrameInfo *MFI = MF.getFrameInfo();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001418 MachineRegisterInfo &MRI = MF.getRegInfo();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001419 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1420
1421 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1422 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
Evan Cheng572756a2011-01-16 05:14:33 +00001423 // since it's not always possible to restore sp from fp in a single
1424 // instruction.
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001425 // FIXME: It will be better just to find spare register here.
1426 if (AFI->isThumb2Function() &&
1427 (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001428 MRI.setPhysRegUsed(ARM::R4);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001429
Evan Cheng572756a2011-01-16 05:14:33 +00001430 if (AFI->isThumb1OnlyFunction()) {
1431 // Spill LR if Thumb1 function uses variable length argument lists.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001432 if (AFI->getArgRegsSaveSize() > 0)
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001433 MRI.setPhysRegUsed(ARM::LR);
Evan Cheng572756a2011-01-16 05:14:33 +00001434
Jim Grosbachdca85312011-06-13 21:18:25 +00001435 // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know
1436 // for sure what the stack size will be, but for this, an estimate is good
1437 // enough. If there anything changes it, it'll be a spill, which implies
1438 // we've used all the registers and so R4 is already used, so not marking
Chad Rosieradd38c12011-10-20 00:07:12 +00001439 // it here will be OK.
Evan Cheng572756a2011-01-16 05:14:33 +00001440 // FIXME: It will be better just to find spare register here.
Hal Finkel628ba122013-03-14 21:15:20 +00001441 unsigned StackSize = MFI->estimateStackSize(MF);
Chad Rosieradd38c12011-10-20 00:07:12 +00001442 if (MFI->hasVarSizedObjects() || StackSize > 508)
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001443 MRI.setPhysRegUsed(ARM::R4);
Evan Cheng572756a2011-01-16 05:14:33 +00001444 }
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001445
Jakob Stoklund Olesen09655852011-12-23 00:36:18 +00001446 // See if we can spill vector registers to aligned stack.
1447 checkNumAlignedDPRCS2Regs(MF);
1448
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001449 // Spill the BasePtr if it's used.
1450 if (RegInfo->hasBasePointer(MF))
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001451 MRI.setPhysRegUsed(RegInfo->getBaseRegister());
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001452
1453 // Don't spill FP if the frame can be eliminated. This is determined
1454 // by scanning the callee-save registers to see if any is used.
Craig Topper840beec2014-04-04 05:16:06 +00001455 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001456 for (unsigned i = 0; CSRegs[i]; ++i) {
1457 unsigned Reg = CSRegs[i];
1458 bool Spilled = false;
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001459 if (MRI.isPhysRegUsed(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001460 Spilled = true;
1461 CanEliminateFrame = false;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001462 }
1463
Craig Topperc7242e02012-04-20 07:30:17 +00001464 if (!ARM::GPRRegClass.contains(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001465 continue;
1466
1467 if (Spilled) {
1468 NumGPRSpills++;
1469
Tim Northover86f60b72014-05-30 13:23:06 +00001470 if (!STI.isTargetDarwin()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001471 if (Reg == ARM::LR)
1472 LRSpilled = true;
1473 CS1Spilled = true;
1474 continue;
1475 }
1476
1477 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1478 switch (Reg) {
1479 case ARM::LR:
1480 LRSpilled = true;
1481 // Fallthrough
Tim Northoverd8407452013-10-01 14:33:28 +00001482 case ARM::R0: case ARM::R1:
1483 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001484 case ARM::R4: case ARM::R5:
1485 case ARM::R6: case ARM::R7:
1486 CS1Spilled = true;
1487 break;
1488 default:
1489 break;
1490 }
1491 } else {
Tim Northover86f60b72014-05-30 13:23:06 +00001492 if (!STI.isTargetDarwin()) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001493 UnspilledCS1GPRs.push_back(Reg);
1494 continue;
1495 }
1496
1497 switch (Reg) {
Tim Northoverd8407452013-10-01 14:33:28 +00001498 case ARM::R0: case ARM::R1:
1499 case ARM::R2: case ARM::R3:
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001500 case ARM::R4: case ARM::R5:
1501 case ARM::R6: case ARM::R7:
1502 case ARM::LR:
1503 UnspilledCS1GPRs.push_back(Reg);
1504 break;
1505 default:
1506 UnspilledCS2GPRs.push_back(Reg);
1507 break;
1508 }
1509 }
1510 }
1511
1512 bool ForceLRSpill = false;
1513 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1514 unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
1515 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1516 // use of BL to implement far jump. If it turns out that it's not needed
1517 // then the branch fix up path will undo it.
1518 if (FnSize >= (1 << 11)) {
1519 CanEliminateFrame = false;
1520 ForceLRSpill = true;
1521 }
1522 }
1523
1524 // If any of the stack slot references may be out of range of an immediate
1525 // offset, make sure a register (or a spill slot) is available for the
1526 // register scavenger. Note that if we're indexing off the frame pointer, the
1527 // effective stack size is 4 bytes larger since the FP points to the stack
1528 // slot of the previous FP. Also, if we have variable sized objects in the
1529 // function, stack slot references will often be negative, and some of
1530 // our instructions are positive-offset only, so conservatively consider
1531 // that case to want a spill slot (or register) as well. Similarly, if
1532 // the function adjusts the stack pointer during execution and the
1533 // adjustments aren't already part of our stack size estimate, our offset
1534 // calculations may be off, so be conservative.
1535 // FIXME: We could add logic to be more precise about negative offsets
1536 // and which instructions will need a scratch register for them. Is it
1537 // worth the effort and added fragility?
1538 bool BigStack =
1539 (RS &&
Hal Finkel628ba122013-03-14 21:15:20 +00001540 (MFI->estimateStackSize(MF) +
1541 ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >=
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001542 estimateRSStackSizeLimit(MF, this)))
1543 || MFI->hasVarSizedObjects()
1544 || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF));
1545
1546 bool ExtraCSSpill = false;
1547 if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1548 AFI->setHasStackFrame(true);
1549
1550 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1551 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1552 if (!LRSpilled && CS1Spilled) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001553 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001554 NumGPRSpills++;
Tim Northoverd8407452013-10-01 14:33:28 +00001555 SmallVectorImpl<unsigned>::iterator LRPos;
1556 LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1557 (unsigned)ARM::LR);
1558 if (LRPos != UnspilledCS1GPRs.end())
1559 UnspilledCS1GPRs.erase(LRPos);
1560
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001561 ForceLRSpill = false;
1562 ExtraCSSpill = true;
1563 }
1564
1565 if (hasFP(MF)) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001566 MRI.setPhysRegUsed(FramePtr);
Joerg Sonnenberger818e7252014-05-06 20:43:01 +00001567 auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(),
1568 FramePtr);
1569 if (FPPos != UnspilledCS1GPRs.end())
1570 UnspilledCS1GPRs.erase(FPPos);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001571 NumGPRSpills++;
1572 }
1573
1574 // If stack and double are 8-byte aligned and we are spilling an odd number
1575 // of GPRs, spill one extra callee save GPR so we won't have to pad between
1576 // the integer and double callee save areas.
Anton Korobeynikov2f931282011-01-10 12:39:04 +00001577 unsigned TargetAlign = getStackAlignment();
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001578 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1579 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1580 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1581 unsigned Reg = UnspilledCS1GPRs[i];
1582 // Don't spill high register if the function is thumb1
1583 if (!AFI->isThumb1OnlyFunction() ||
1584 isARMLowRegister(Reg) || Reg == ARM::LR) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001585 MRI.setPhysRegUsed(Reg);
1586 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001587 ExtraCSSpill = true;
1588 break;
1589 }
1590 }
1591 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
1592 unsigned Reg = UnspilledCS2GPRs.front();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001593 MRI.setPhysRegUsed(Reg);
1594 if (!MRI.isReserved(Reg))
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001595 ExtraCSSpill = true;
1596 }
1597 }
1598
1599 // Estimate if we might need to scavenge a register at some point in order
1600 // to materialize a stack offset. If so, either spill one additional
1601 // callee-saved register or reserve a special spill slot to facilitate
1602 // register scavenging. Thumb1 needs a spill slot for stack pointer
1603 // adjustments also, even when the frame itself is small.
1604 if (BigStack && !ExtraCSSpill) {
1605 // If any non-reserved CS register isn't spilled, just spill one or two
1606 // extra. That should take care of it!
1607 unsigned NumExtras = TargetAlign / 4;
1608 SmallVector<unsigned, 2> Extras;
1609 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1610 unsigned Reg = UnspilledCS1GPRs.back();
1611 UnspilledCS1GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001612 if (!MRI.isReserved(Reg) &&
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001613 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
1614 Reg == ARM::LR)) {
1615 Extras.push_back(Reg);
1616 NumExtras--;
1617 }
1618 }
1619 // For non-Thumb1 functions, also check for hi-reg CS registers
1620 if (!AFI->isThumb1OnlyFunction()) {
1621 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1622 unsigned Reg = UnspilledCS2GPRs.back();
1623 UnspilledCS2GPRs.pop_back();
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001624 if (!MRI.isReserved(Reg)) {
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001625 Extras.push_back(Reg);
1626 NumExtras--;
1627 }
1628 }
1629 }
1630 if (Extras.size() && NumExtras == 0) {
1631 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001632 MRI.setPhysRegUsed(Extras[i]);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001633 }
1634 } else if (!AFI->isThumb1OnlyFunction()) {
1635 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
1636 // closest to SP or frame pointer.
Craig Topperc7242e02012-04-20 07:30:17 +00001637 const TargetRegisterClass *RC = &ARM::GPRRegClass;
Hal Finkel9e331c22013-03-22 23:32:27 +00001638 RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001639 RC->getAlignment(),
1640 false));
1641 }
1642 }
1643 }
1644
1645 if (ForceLRSpill) {
Jakob Stoklund Olesen410eae52012-10-26 21:43:05 +00001646 MRI.setPhysRegUsed(ARM::LR);
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +00001647 AFI->setLRIsSpilledForFarJump(true);
1648 }
1649}
Eli Bendersky8da87162013-02-21 20:05:00 +00001650
1651
1652void ARMFrameLowering::
1653eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1654 MachineBasicBlock::iterator I) const {
1655 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001656 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Eli Bendersky8da87162013-02-21 20:05:00 +00001657 if (!hasReservedCallFrame(MF)) {
1658 // If we have alloca, convert as follows:
1659 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1660 // ADJCALLSTACKUP -> add, sp, sp, amount
1661 MachineInstr *Old = I;
1662 DebugLoc dl = Old->getDebugLoc();
1663 unsigned Amount = Old->getOperand(0).getImm();
1664 if (Amount != 0) {
1665 // We need to keep the stack aligned properly. To do this, we round the
1666 // amount of space needed for the outgoing arguments up to the next
1667 // alignment boundary.
1668 unsigned Align = getStackAlignment();
1669 Amount = (Amount+Align-1)/Align*Align;
1670
1671 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1672 assert(!AFI->isThumb1OnlyFunction() &&
1673 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1674 bool isARM = !AFI->isThumbFunction();
1675
1676 // Replace the pseudo instruction with a new instruction...
1677 unsigned Opc = Old->getOpcode();
1678 int PIdx = Old->findFirstPredOperandIdx();
1679 ARMCC::CondCodes Pred = (PIdx == -1)
1680 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1681 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1682 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1683 unsigned PredReg = Old->getOperand(2).getReg();
1684 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
1685 Pred, PredReg);
1686 } else {
1687 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1688 unsigned PredReg = Old->getOperand(3).getReg();
1689 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1690 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
1691 Pred, PredReg);
1692 }
1693 }
1694 }
1695 MBB.erase(I);
1696}
1697
Oliver Stannardb14c6252014-04-02 16:10:33 +00001698/// Get the minimum constant for ARM that is greater than or equal to the
1699/// argument. In ARM, constants can have any value that can be produced by
1700/// rotating an 8-bit value to the right by an even number of bits within a
1701/// 32-bit word.
1702static uint32_t alignToARMConstant(uint32_t Value) {
1703 unsigned Shifted = 0;
1704
1705 if (Value == 0)
1706 return 0;
1707
1708 while (!(Value & 0xC0000000)) {
1709 Value = Value << 2;
1710 Shifted += 2;
1711 }
1712
1713 bool Carry = (Value & 0x00FFFFFF);
1714 Value = ((Value & 0xFF000000) >> 24) + Carry;
1715
1716 if (Value & 0x0000100)
1717 Value = Value & 0x000001FC;
1718
1719 if (Shifted > 24)
1720 Value = Value >> (Shifted - 24);
1721 else
1722 Value = Value << (24 - Shifted);
1723
1724 return Value;
1725}
1726
1727// The stack limit in the TCB is set to this many bytes above the actual
1728// stack limit.
1729static const uint64_t kSplitStackAvailable = 256;
1730
1731// Adjust the function prologue to enable split stacks. This currently only
1732// supports android and linux.
1733//
1734// The ABI of the segmented stack prologue is a little arbitrarily chosen, but
1735// must be well defined in order to allow for consistent implementations of the
1736// __morestack helper function. The ABI is also not a normal ABI in that it
1737// doesn't follow the normal calling conventions because this allows the
1738// prologue of each function to be optimized further.
1739//
1740// Currently, the ABI looks like (when calling __morestack)
1741//
1742// * r4 holds the minimum stack size requested for this function call
1743// * r5 holds the stack size of the arguments to the function
1744// * the beginning of the function is 3 instructions after the call to
1745// __morestack
1746//
1747// Implementations of __morestack should use r4 to allocate a new stack, r5 to
1748// place the arguments on to the new stack, and the 3-instruction knowledge to
1749// jump directly to the body of the function when working on the new stack.
1750//
1751// An old (and possibly no longer compatible) implementation of __morestack for
1752// ARM can be found at [1].
1753//
1754// [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
1755void ARMFrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const {
1756 unsigned Opcode;
1757 unsigned CFIIndex;
1758 const ARMSubtarget *ST = &MF.getTarget().getSubtarget<ARMSubtarget>();
1759 bool Thumb = ST->isThumb();
1760
1761 // Sadly, this currently doesn't support varargs, platforms other than
1762 // android/linux. Note that thumb1/thumb2 are support for android/linux.
1763 if (MF.getFunction()->isVarArg())
1764 report_fatal_error("Segmented stacks do not support vararg functions.");
1765 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
Alp Toker16f98b22014-04-09 14:47:27 +00001766 report_fatal_error("Segmented stacks not supported on this platform.");
Oliver Stannardb14c6252014-04-02 16:10:33 +00001767
1768 MachineBasicBlock &prologueMBB = MF.front();
1769 MachineFrameInfo *MFI = MF.getFrameInfo();
1770 MachineModuleInfo &MMI = MF.getMMI();
1771 MCContext &Context = MMI.getContext();
1772 const MCRegisterInfo *MRI = Context.getRegisterInfo();
1773 const ARMBaseInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +00001774 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
Oliver Stannardb14c6252014-04-02 16:10:33 +00001775 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
1776 DebugLoc DL;
1777
Tim Northoverf9e798b2014-05-22 13:03:43 +00001778 uint64_t StackSize = MFI->getStackSize();
1779
1780 // Do not generate a prologue for functions with a stack of size zero
1781 if (StackSize == 0)
1782 return;
1783
Oliver Stannardb14c6252014-04-02 16:10:33 +00001784 // Use R4 and R5 as scratch registers.
1785 // We save R4 and R5 before use and restore them before leaving the function.
1786 unsigned ScratchReg0 = ARM::R4;
1787 unsigned ScratchReg1 = ARM::R5;
1788 uint64_t AlignedStackSize;
1789
1790 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
1791 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
1792 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
1793 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
1794 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
1795
1796 for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(),
1797 e = prologueMBB.livein_end();
1798 i != e; ++i) {
1799 AllocMBB->addLiveIn(*i);
1800 GetMBB->addLiveIn(*i);
1801 McrMBB->addLiveIn(*i);
1802 PrevStackMBB->addLiveIn(*i);
1803 PostStackMBB->addLiveIn(*i);
1804 }
1805
1806 MF.push_front(PostStackMBB);
1807 MF.push_front(AllocMBB);
1808 MF.push_front(GetMBB);
1809 MF.push_front(McrMBB);
1810 MF.push_front(PrevStackMBB);
1811
1812 // The required stack size that is aligned to ARM constant criterion.
Oliver Stannardb14c6252014-04-02 16:10:33 +00001813 AlignedStackSize = alignToARMConstant(StackSize);
1814
1815 // When the frame size is less than 256 we just compare the stack
1816 // boundary directly to the value of the stack pointer, per gcc.
1817 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
1818
1819 // We will use two of the callee save registers as scratch registers so we
1820 // need to save those registers onto the stack.
1821 // We will use SR0 to hold stack limit and SR1 to hold the stack size
1822 // requested and arguments for __morestack().
1823 // SR0: Scratch Register #0
1824 // SR1: Scratch Register #1
1825 // push {SR0, SR1}
1826 if (Thumb) {
1827 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH)))
1828 .addReg(ScratchReg0).addReg(ScratchReg1);
1829 } else {
1830 AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
1831 .addReg(ARM::SP, RegState::Define).addReg(ARM::SP))
1832 .addReg(ScratchReg0).addReg(ScratchReg1);
1833 }
1834
1835 // Emit the relevant DWARF information about the change in stack pointer as
1836 // well as where to find both r4 and r5 (the callee-save registers)
1837 CFIIndex =
1838 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
1839 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1840 .addCFIIndex(CFIIndex);
1841 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1842 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
1843 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1844 .addCFIIndex(CFIIndex);
1845 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1846 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
1847 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1848 .addCFIIndex(CFIIndex);
1849
1850 // mov SR1, sp
1851 if (Thumb) {
1852 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
1853 .addReg(ARM::SP));
1854 } else if (CompareStackPointer) {
1855 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
1856 .addReg(ARM::SP)).addReg(0);
1857 }
1858
1859 // sub SR1, sp, #StackSize
1860 if (!CompareStackPointer && Thumb) {
1861 AddDefaultPred(
1862 AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1))
1863 .addReg(ScratchReg1).addImm(AlignedStackSize));
1864 } else if (!CompareStackPointer) {
1865 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
1866 .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0);
1867 }
1868
1869 if (Thumb && ST->isThumb1Only()) {
1870 unsigned PCLabelId = ARMFI->createPICLabelUId();
1871 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
Oliver Stannard92e0fc02014-04-03 08:45:16 +00001872 MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0);
Oliver Stannardb14c6252014-04-02 16:10:33 +00001873 MachineConstantPool *MCP = MF.getConstantPool();
1874 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment());
1875
1876 // ldr SR0, [pc, offset(STACK_LIMIT)]
1877 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
1878 .addConstantPoolIndex(CPI));
1879
1880 // ldr SR0, [SR0]
1881 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
1882 .addReg(ScratchReg0).addImm(0));
1883 } else {
1884 // Get TLS base address from the coprocessor
1885 // mrc p15, #0, SR0, c13, c0, #3
1886 AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
1887 .addImm(15)
1888 .addImm(0)
1889 .addImm(13)
1890 .addImm(0)
1891 .addImm(3));
1892
1893 // Use the last tls slot on android and a private field of the TCP on linux.
1894 assert(ST->isTargetAndroid() || ST->isTargetLinux());
1895 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
1896
1897 // Get the stack limit from the right offset
1898 // ldr SR0, [sr0, #4 * TlsOffset]
1899 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
1900 .addReg(ScratchReg0).addImm(4 * TlsOffset));
1901 }
1902
1903 // Compare stack limit with stack size requested.
1904 // cmp SR0, SR1
1905 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
1906 AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode))
1907 .addReg(ScratchReg0)
1908 .addReg(ScratchReg1));
1909
1910 // This jump is taken if StackLimit < SP - stack required.
1911 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
1912 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
1913 .addImm(ARMCC::LO)
1914 .addReg(ARM::CPSR);
1915
1916
1917 // Calling __morestack(StackSize, Size of stack arguments).
1918 // __morestack knows that the stack size requested is in SR0(r4)
1919 // and amount size of stack arguments is in SR1(r5).
1920
1921 // Pass first argument for the __morestack by Scratch Register #0.
1922 // The amount size of stack required
1923 if (Thumb) {
1924 AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8),
1925 ScratchReg0)).addImm(AlignedStackSize));
1926 } else {
1927 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
1928 .addImm(AlignedStackSize)).addReg(0);
1929 }
1930 // Pass second argument for the __morestack by Scratch Register #1.
1931 // The amount size of stack consumed to save function arguments.
1932 if (Thumb) {
1933 AddDefaultPred(
1934 AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1))
1935 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())));
1936 } else {
1937 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
1938 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize())))
1939 .addReg(0);
1940 }
1941
1942 // push {lr} - Save return address of this function.
1943 if (Thumb) {
1944 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH)))
1945 .addReg(ARM::LR);
1946 } else {
1947 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
1948 .addReg(ARM::SP, RegState::Define)
1949 .addReg(ARM::SP))
1950 .addReg(ARM::LR);
1951 }
1952
1953 // Emit the DWARF info about the change in stack as well as where to find the
1954 // previous link register
1955 CFIIndex =
1956 MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
1957 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1958 .addCFIIndex(CFIIndex);
1959 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
1960 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
1961 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
1962 .addCFIIndex(CFIIndex);
1963
1964 // Call __morestack().
1965 if (Thumb) {
1966 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL)))
1967 .addExternalSymbol("__morestack");
1968 } else {
1969 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
1970 .addExternalSymbol("__morestack");
1971 }
1972
1973 // pop {lr} - Restore return address of this original function.
1974 if (Thumb) {
1975 if (ST->isThumb1Only()) {
1976 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
1977 .addReg(ScratchReg0);
1978 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
1979 .addReg(ScratchReg0));
1980 } else {
1981 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
1982 .addReg(ARM::LR, RegState::Define)
1983 .addReg(ARM::SP, RegState::Define)
1984 .addReg(ARM::SP)
1985 .addImm(4));
1986 }
1987 } else {
1988 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
1989 .addReg(ARM::SP, RegState::Define)
1990 .addReg(ARM::SP))
1991 .addReg(ARM::LR);
1992 }
1993
1994 // Restore SR0 and SR1 in case of __morestack() was called.
1995 // __morestack() will skip PostStackMBB block so we need to restore
1996 // scratch registers from here.
1997 // pop {SR0, SR1}
1998 if (Thumb) {
1999 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP)))
2000 .addReg(ScratchReg0)
2001 .addReg(ScratchReg1);
2002 } else {
2003 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2004 .addReg(ARM::SP, RegState::Define)
2005 .addReg(ARM::SP))
2006 .addReg(ScratchReg0)
2007 .addReg(ScratchReg1);
2008 }
2009
2010 // Update the CFA offset now that we've popped
2011 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2012 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2013 .addCFIIndex(CFIIndex);
2014
2015 // bx lr - Return from this function.
2016 Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET;
2017 AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode)));
2018
2019 // Restore SR0 and SR1 in case of __morestack() was not called.
2020 // pop {SR0, SR1}
2021 if (Thumb) {
2022 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP)))
2023 .addReg(ScratchReg0)
2024 .addReg(ScratchReg1);
2025 } else {
2026 AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2027 .addReg(ARM::SP, RegState::Define)
2028 .addReg(ARM::SP))
2029 .addReg(ScratchReg0)
2030 .addReg(ScratchReg1);
2031 }
2032
2033 // Update the CFA offset now that we've popped
2034 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2035 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2036 .addCFIIndex(CFIIndex);
2037
2038 // Tell debuggers that r4 and r5 are now the same as they were in the
2039 // previous function, that they're the "Same Value".
2040 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2041 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2042 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2043 .addCFIIndex(CFIIndex);
2044 CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue(
2045 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2046 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2047 .addCFIIndex(CFIIndex);
2048
2049 // Organizing MBB lists
2050 PostStackMBB->addSuccessor(&prologueMBB);
2051
2052 AllocMBB->addSuccessor(PostStackMBB);
2053
2054 GetMBB->addSuccessor(PostStackMBB);
2055 GetMBB->addSuccessor(AllocMBB);
2056
2057 McrMBB->addSuccessor(GetMBB);
2058
2059 PrevStackMBB->addSuccessor(McrMBB);
2060
2061#ifdef XDEBUG
2062 MF.verify();
2063#endif
2064}