[ARM] MVE big endian bitcasts
[llvm-complete.git] / lib / Target / ARM / ARMFrameLowering.cpp
blob7ae0c6ab3cf8ed38856c2466c55052b2a68a5446
1 //===- ARMFrameLowering.cpp - ARM Frame Information -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the ARM implementation of TargetFrameLowering class.
11 //===----------------------------------------------------------------------===//
13 #include "ARMFrameLowering.h"
14 #include "ARMBaseInstrInfo.h"
15 #include "ARMBaseRegisterInfo.h"
16 #include "ARMConstantPoolValue.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "MCTargetDesc/ARMAddressingModes.h"
20 #include "MCTargetDesc/ARMBaseInfo.h"
21 #include "Utils/ARMBaseInfo.h"
22 #include "llvm/ADT/BitVector.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineJumpTableInfo.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/RegisterScavenging.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetOpcodes.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSubtargetInfo.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/CallingConv.h"
43 #include "llvm/IR/DebugLoc.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/MC/MCContext.h"
46 #include "llvm/MC/MCDwarf.h"
47 #include "llvm/MC/MCInstrDesc.h"
48 #include "llvm/MC/MCRegisterInfo.h"
49 #include "llvm/Support/CodeGen.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Compiler.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/MathExtras.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Target/TargetMachine.h"
57 #include "llvm/Target/TargetOptions.h"
58 #include <algorithm>
59 #include <cassert>
60 #include <cstddef>
61 #include <cstdint>
62 #include <iterator>
63 #include <utility>
64 #include <vector>
66 #define DEBUG_TYPE "arm-frame-lowering"
68 using namespace llvm;
70 static cl::opt<bool>
71 SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true),
72 cl::desc("Align ARM NEON spills in prolog and epilog"));
74 static MachineBasicBlock::iterator
75 skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
76 unsigned NumAlignedDPRCS2Regs);
78 ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti)
79 : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4),
80 STI(sti) {}
82 bool ARMFrameLowering::keepFramePointer(const MachineFunction &MF) const {
83 // iOS always has a FP for backtracking, force other targets to keep their FP
84 // when doing FastISel. The emitted code is currently superior, and in cases
85 // like test-suite's lencod FastISel isn't quite correct when FP is eliminated.
86 return MF.getSubtarget<ARMSubtarget>().useFastISel();
89 /// Returns true if the target can safely skip saving callee-saved registers
90 /// for noreturn nounwind functions.
91 bool ARMFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
92 assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
93 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
94 !MF.getFunction().hasFnAttribute(Attribute::UWTable));
96 // Frame pointer and link register are not treated as normal CSR, thus we
97 // can always skip CSR saves for nonreturning functions.
98 return true;
101 /// hasFP - Return true if the specified function should have a dedicated frame
102 /// pointer register. This is true if the function has variable sized allocas
103 /// or if frame pointer elimination is disabled.
104 bool ARMFrameLowering::hasFP(const MachineFunction &MF) const {
105 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
106 const MachineFrameInfo &MFI = MF.getFrameInfo();
108 // ABI-required frame pointer.
109 if (MF.getTarget().Options.DisableFramePointerElim(MF))
110 return true;
112 // Frame pointer required for use within this function.
113 return (RegInfo->needsStackRealignment(MF) ||
114 MFI.hasVarSizedObjects() ||
115 MFI.isFrameAddressTaken());
118 /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
119 /// not required, we reserve argument space for call sites in the function
120 /// immediately on entry to the current function. This eliminates the need for
121 /// add/sub sp brackets around call sites. Returns true if the call frame is
122 /// included as part of the stack frame.
123 bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
124 const MachineFrameInfo &MFI = MF.getFrameInfo();
125 unsigned CFSize = MFI.getMaxCallFrameSize();
126 // It's not always a good idea to include the call frame as part of the
127 // stack frame. ARM (especially Thumb) has small immediate offset to
128 // address the stack frame. So a large call frame can cause poor codegen
129 // and may even makes it impossible to scavenge a register.
130 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
131 return false;
133 return !MFI.hasVarSizedObjects();
136 /// canSimplifyCallFramePseudos - If there is a reserved call frame, the
137 /// call frame pseudos can be simplified. Unlike most targets, having a FP
138 /// is not sufficient here since we still may reference some objects via SP
139 /// even when FP is available in Thumb2 mode.
140 bool
141 ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const {
142 return hasReservedCallFrame(MF) || MF.getFrameInfo().hasVarSizedObjects();
145 static bool isCSRestore(MachineInstr &MI, const ARMBaseInstrInfo &TII,
146 const MCPhysReg *CSRegs) {
147 // Integer spill area is handled with "pop".
148 if (isPopOpcode(MI.getOpcode())) {
149 // The first two operands are predicates. The last two are
150 // imp-def and imp-use of SP. Check everything in between.
151 for (int i = 5, e = MI.getNumOperands(); i != e; ++i)
152 if (!isCalleeSavedRegister(MI.getOperand(i).getReg(), CSRegs))
153 return false;
154 return true;
156 if ((MI.getOpcode() == ARM::LDR_POST_IMM ||
157 MI.getOpcode() == ARM::LDR_POST_REG ||
158 MI.getOpcode() == ARM::t2LDR_POST) &&
159 isCalleeSavedRegister(MI.getOperand(0).getReg(), CSRegs) &&
160 MI.getOperand(1).getReg() == ARM::SP)
161 return true;
163 return false;
166 static void emitRegPlusImmediate(
167 bool isARM, MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
168 const DebugLoc &dl, const ARMBaseInstrInfo &TII, unsigned DestReg,
169 unsigned SrcReg, int NumBytes, unsigned MIFlags = MachineInstr::NoFlags,
170 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
171 if (isARM)
172 emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
173 Pred, PredReg, TII, MIFlags);
174 else
175 emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes,
176 Pred, PredReg, TII, MIFlags);
179 static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB,
180 MachineBasicBlock::iterator &MBBI, const DebugLoc &dl,
181 const ARMBaseInstrInfo &TII, int NumBytes,
182 unsigned MIFlags = MachineInstr::NoFlags,
183 ARMCC::CondCodes Pred = ARMCC::AL,
184 unsigned PredReg = 0) {
185 emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes,
186 MIFlags, Pred, PredReg);
189 static int sizeOfSPAdjustment(const MachineInstr &MI) {
190 int RegSize;
191 switch (MI.getOpcode()) {
192 case ARM::VSTMDDB_UPD:
193 RegSize = 8;
194 break;
195 case ARM::STMDB_UPD:
196 case ARM::t2STMDB_UPD:
197 RegSize = 4;
198 break;
199 case ARM::t2STR_PRE:
200 case ARM::STR_PRE_IMM:
201 return 4;
202 default:
203 llvm_unreachable("Unknown push or pop like instruction");
206 int count = 0;
207 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
208 // pred) so the list starts at 4.
209 for (int i = MI.getNumOperands() - 1; i >= 4; --i)
210 count += RegSize;
211 return count;
214 static bool WindowsRequiresStackProbe(const MachineFunction &MF,
215 size_t StackSizeInBytes) {
216 const MachineFrameInfo &MFI = MF.getFrameInfo();
217 const Function &F = MF.getFunction();
218 unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
219 if (F.hasFnAttribute("stack-probe-size"))
220 F.getFnAttribute("stack-probe-size")
221 .getValueAsString()
222 .getAsInteger(0, StackProbeSize);
223 return (StackSizeInBytes >= StackProbeSize) &&
224 !F.hasFnAttribute("no-stack-arg-probe");
227 namespace {
229 struct StackAdjustingInsts {
230 struct InstInfo {
231 MachineBasicBlock::iterator I;
232 unsigned SPAdjust;
233 bool BeforeFPSet;
236 SmallVector<InstInfo, 4> Insts;
238 void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust,
239 bool BeforeFPSet = false) {
240 InstInfo Info = {I, SPAdjust, BeforeFPSet};
241 Insts.push_back(Info);
244 void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) {
245 auto Info =
246 llvm::find_if(Insts, [&](InstInfo &Info) { return Info.I == I; });
247 assert(Info != Insts.end() && "invalid sp adjusting instruction");
248 Info->SPAdjust += ExtraBytes;
251 void emitDefCFAOffsets(MachineBasicBlock &MBB, const DebugLoc &dl,
252 const ARMBaseInstrInfo &TII, bool HasFP) {
253 MachineFunction &MF = *MBB.getParent();
254 unsigned CFAOffset = 0;
255 for (auto &Info : Insts) {
256 if (HasFP && !Info.BeforeFPSet)
257 return;
259 CFAOffset -= Info.SPAdjust;
260 unsigned CFIIndex = MF.addFrameInst(
261 MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset));
262 BuildMI(MBB, std::next(Info.I), dl,
263 TII.get(TargetOpcode::CFI_INSTRUCTION))
264 .addCFIIndex(CFIIndex)
265 .setMIFlags(MachineInstr::FrameSetup);
270 } // end anonymous namespace
272 /// Emit an instruction sequence that will align the address in
273 /// register Reg by zero-ing out the lower bits. For versions of the
274 /// architecture that support Neon, this must be done in a single
275 /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a
276 /// single instruction. That function only gets called when optimizing
277 /// spilling of D registers on a core with the Neon instruction set
278 /// present.
279 static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI,
280 const TargetInstrInfo &TII,
281 MachineBasicBlock &MBB,
282 MachineBasicBlock::iterator MBBI,
283 const DebugLoc &DL, const unsigned Reg,
284 const unsigned Alignment,
285 const bool MustBeSingleInstruction) {
286 const ARMSubtarget &AST =
287 static_cast<const ARMSubtarget &>(MF.getSubtarget());
288 const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops();
289 const unsigned AlignMask = Alignment - 1;
290 const unsigned NrBitsToZero = countTrailingZeros(Alignment);
291 assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported");
292 if (!AFI->isThumbFunction()) {
293 // if the BFC instruction is available, use that to zero the lower
294 // bits:
295 // bfc Reg, #0, log2(Alignment)
296 // otherwise use BIC, if the mask to zero the required number of bits
297 // can be encoded in the bic immediate field
298 // bic Reg, Reg, Alignment-1
299 // otherwise, emit
300 // lsr Reg, Reg, log2(Alignment)
301 // lsl Reg, Reg, log2(Alignment)
302 if (CanUseBFC) {
303 BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg)
304 .addReg(Reg, RegState::Kill)
305 .addImm(~AlignMask)
306 .add(predOps(ARMCC::AL));
307 } else if (AlignMask <= 255) {
308 BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg)
309 .addReg(Reg, RegState::Kill)
310 .addImm(AlignMask)
311 .add(predOps(ARMCC::AL))
312 .add(condCodeOp());
313 } else {
314 assert(!MustBeSingleInstruction &&
315 "Shouldn't call emitAligningInstructions demanding a single "
316 "instruction to be emitted for large stack alignment for a target "
317 "without BFC.");
318 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
319 .addReg(Reg, RegState::Kill)
320 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero))
321 .add(predOps(ARMCC::AL))
322 .add(condCodeOp());
323 BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg)
324 .addReg(Reg, RegState::Kill)
325 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero))
326 .add(predOps(ARMCC::AL))
327 .add(condCodeOp());
329 } else {
330 // Since this is only reached for Thumb-2 targets, the BFC instruction
331 // should always be available.
332 assert(CanUseBFC);
333 BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg)
334 .addReg(Reg, RegState::Kill)
335 .addImm(~AlignMask)
336 .add(predOps(ARMCC::AL));
340 /// We need the offset of the frame pointer relative to other MachineFrameInfo
341 /// offsets which are encoded relative to SP at function begin.
342 /// See also emitPrologue() for how the FP is set up.
343 /// Unfortunately we cannot determine this value in determineCalleeSaves() yet
344 /// as assignCalleeSavedSpillSlots() hasn't run at this point. Instead we use
345 /// this to produce a conservative estimate that we check in an assert() later.
346 static int getMaxFPOffset(const Function &F, const ARMFunctionInfo &AFI) {
347 // For Thumb1, push.w isn't available, so the first push will always push
348 // r7 and lr onto the stack first.
349 if (AFI.isThumb1OnlyFunction())
350 return -AFI.getArgRegsSaveSize() - (2 * 4);
351 // This is a conservative estimation: Assume the frame pointer being r7 and
352 // pc("r15") up to r8 getting spilled before (= 8 registers).
353 return -AFI.getArgRegsSaveSize() - (8 * 4);
356 void ARMFrameLowering::emitPrologue(MachineFunction &MF,
357 MachineBasicBlock &MBB) const {
358 MachineBasicBlock::iterator MBBI = MBB.begin();
359 MachineFrameInfo &MFI = MF.getFrameInfo();
360 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
361 MachineModuleInfo &MMI = MF.getMMI();
362 MCContext &Context = MMI.getContext();
363 const TargetMachine &TM = MF.getTarget();
364 const MCRegisterInfo *MRI = Context.getRegisterInfo();
365 const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo();
366 const ARMBaseInstrInfo &TII = *STI.getInstrInfo();
367 assert(!AFI->isThumb1OnlyFunction() &&
368 "This emitPrologue does not support Thumb1!");
369 bool isARM = !AFI->isThumbFunction();
370 unsigned Align = STI.getFrameLowering()->getStackAlignment();
371 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
372 unsigned NumBytes = MFI.getStackSize();
373 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
375 // Debug location must be unknown since the first debug location is used
376 // to determine the end of the prologue.
377 DebugLoc dl;
379 unsigned FramePtr = RegInfo->getFrameRegister(MF);
381 // Determine the sizes of each callee-save spill areas and record which frame
382 // belongs to which callee-save spill areas.
383 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
384 int FramePtrSpillFI = 0;
385 int D8SpillFI = 0;
387 // All calls are tail calls in GHC calling conv, and functions have no
388 // prologue/epilogue.
389 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
390 return;
392 StackAdjustingInsts DefCFAOffsetCandidates;
393 bool HasFP = hasFP(MF);
395 // Allocate the vararg register save area.
396 if (ArgRegsSaveSize) {
397 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize,
398 MachineInstr::FrameSetup);
399 DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true);
402 if (!AFI->hasStackFrame() &&
403 (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) {
404 if (NumBytes - ArgRegsSaveSize != 0) {
405 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize),
406 MachineInstr::FrameSetup);
407 DefCFAOffsetCandidates.addInst(std::prev(MBBI),
408 NumBytes - ArgRegsSaveSize, true);
410 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
411 return;
414 // Determine spill area sizes.
415 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
416 unsigned Reg = CSI[i].getReg();
417 int FI = CSI[i].getFrameIdx();
418 switch (Reg) {
419 case ARM::R8:
420 case ARM::R9:
421 case ARM::R10:
422 case ARM::R11:
423 case ARM::R12:
424 if (STI.splitFramePushPop(MF)) {
425 GPRCS2Size += 4;
426 break;
428 LLVM_FALLTHROUGH;
429 case ARM::R0:
430 case ARM::R1:
431 case ARM::R2:
432 case ARM::R3:
433 case ARM::R4:
434 case ARM::R5:
435 case ARM::R6:
436 case ARM::R7:
437 case ARM::LR:
438 if (Reg == FramePtr)
439 FramePtrSpillFI = FI;
440 GPRCS1Size += 4;
441 break;
442 default:
443 // This is a DPR. Exclude the aligned DPRCS2 spills.
444 if (Reg == ARM::D8)
445 D8SpillFI = FI;
446 if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())
447 DPRCSSize += 8;
451 // Move past area 1.
452 MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push;
453 if (GPRCS1Size > 0) {
454 GPRCS1Push = LastPush = MBBI++;
455 DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true);
458 // Determine starting offsets of spill areas.
459 unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size;
460 unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size;
461 unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U;
462 unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign;
463 unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize;
464 int FramePtrOffsetInPush = 0;
465 if (HasFP) {
466 int FPOffset = MFI.getObjectOffset(FramePtrSpillFI);
467 assert(getMaxFPOffset(MF.getFunction(), *AFI) <= FPOffset &&
468 "Max FP estimation is wrong");
469 FramePtrOffsetInPush = FPOffset + ArgRegsSaveSize;
470 AFI->setFramePtrSpillOffset(MFI.getObjectOffset(FramePtrSpillFI) +
471 NumBytes);
473 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
474 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
475 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
477 // Move past area 2.
478 if (GPRCS2Size > 0) {
479 GPRCS2Push = LastPush = MBBI++;
480 DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size);
483 // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our
484 // .cfi_offset operations will reflect that.
485 if (DPRGapSize) {
486 assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs");
487 if (LastPush != MBB.end() &&
488 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, DPRGapSize))
489 DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize);
490 else {
491 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize,
492 MachineInstr::FrameSetup);
493 DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize);
497 // Move past area 3.
498 if (DPRCSSize > 0) {
499 // Since vpush register list cannot have gaps, there may be multiple vpush
500 // instructions in the prologue.
501 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VSTMDDB_UPD) {
502 DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(*MBBI));
503 LastPush = MBBI++;
507 // Move past the aligned DPRCS2 area.
508 if (AFI->getNumAlignedDPRCS2Regs() > 0) {
509 MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs());
510 // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and
511 // leaves the stack pointer pointing to the DPRCS2 area.
513 // Adjust NumBytes to represent the stack slots below the DPRCS2 area.
514 NumBytes += MFI.getObjectOffset(D8SpillFI);
515 } else
516 NumBytes = DPRCSOffset;
518 if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) {
519 uint32_t NumWords = NumBytes >> 2;
521 if (NumWords < 65536)
522 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4)
523 .addImm(NumWords)
524 .setMIFlags(MachineInstr::FrameSetup)
525 .add(predOps(ARMCC::AL));
526 else
527 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4)
528 .addImm(NumWords)
529 .setMIFlags(MachineInstr::FrameSetup);
531 switch (TM.getCodeModel()) {
532 case CodeModel::Tiny:
533 llvm_unreachable("Tiny code model not available on ARM.");
534 case CodeModel::Small:
535 case CodeModel::Medium:
536 case CodeModel::Kernel:
537 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL))
538 .add(predOps(ARMCC::AL))
539 .addExternalSymbol("__chkstk")
540 .addReg(ARM::R4, RegState::Implicit)
541 .setMIFlags(MachineInstr::FrameSetup);
542 break;
543 case CodeModel::Large:
544 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12)
545 .addExternalSymbol("__chkstk")
546 .setMIFlags(MachineInstr::FrameSetup);
548 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr))
549 .add(predOps(ARMCC::AL))
550 .addReg(ARM::R12, RegState::Kill)
551 .addReg(ARM::R4, RegState::Implicit)
552 .setMIFlags(MachineInstr::FrameSetup);
553 break;
556 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), ARM::SP)
557 .addReg(ARM::SP, RegState::Kill)
558 .addReg(ARM::R4, RegState::Kill)
559 .setMIFlags(MachineInstr::FrameSetup)
560 .add(predOps(ARMCC::AL))
561 .add(condCodeOp());
562 NumBytes = 0;
565 if (NumBytes) {
566 // Adjust SP after all the callee-save spills.
567 if (AFI->getNumAlignedDPRCS2Regs() == 0 &&
568 tryFoldSPUpdateIntoPushPop(STI, MF, &*LastPush, NumBytes))
569 DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes);
570 else {
571 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes,
572 MachineInstr::FrameSetup);
573 DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes);
576 if (HasFP && isARM)
577 // Restore from fp only in ARM mode: e.g. sub sp, r7, #24
578 // Note it's not safe to do this in Thumb2 mode because it would have
579 // taken two instructions:
580 // mov sp, r7
581 // sub sp, #24
582 // If an interrupt is taken between the two instructions, then sp is in
583 // an inconsistent state (pointing to the middle of callee-saved area).
584 // The interrupt handler can end up clobbering the registers.
585 AFI->setShouldRestoreSPFromFP(true);
588 // Set FP to point to the stack slot that contains the previous FP.
589 // For iOS, FP is R7, which has now been stored in spill area 1.
590 // Otherwise, if this is not iOS, all the callee-saved registers go
591 // into spill area 1, including the FP in R11. In either case, it
592 // is in area one and the adjustment needs to take place just after
593 // that push.
594 if (HasFP) {
595 MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push);
596 unsigned PushSize = sizeOfSPAdjustment(*GPRCS1Push);
597 emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush,
598 dl, TII, FramePtr, ARM::SP,
599 PushSize + FramePtrOffsetInPush,
600 MachineInstr::FrameSetup);
601 if (FramePtrOffsetInPush + PushSize != 0) {
602 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa(
603 nullptr, MRI->getDwarfRegNum(FramePtr, true),
604 -(ArgRegsSaveSize - FramePtrOffsetInPush)));
605 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
606 .addCFIIndex(CFIIndex)
607 .setMIFlags(MachineInstr::FrameSetup);
608 } else {
609 unsigned CFIIndex =
610 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
611 nullptr, MRI->getDwarfRegNum(FramePtr, true)));
612 BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
613 .addCFIIndex(CFIIndex)
614 .setMIFlags(MachineInstr::FrameSetup);
618 // Now that the prologue's actual instructions are finalised, we can insert
619 // the necessary DWARF cf instructions to describe the situation. Start by
620 // recording where each register ended up:
621 if (GPRCS1Size > 0) {
622 MachineBasicBlock::iterator Pos = std::next(GPRCS1Push);
623 int CFIIndex;
624 for (const auto &Entry : CSI) {
625 unsigned Reg = Entry.getReg();
626 int FI = Entry.getFrameIdx();
627 switch (Reg) {
628 case ARM::R8:
629 case ARM::R9:
630 case ARM::R10:
631 case ARM::R11:
632 case ARM::R12:
633 if (STI.splitFramePushPop(MF))
634 break;
635 LLVM_FALLTHROUGH;
636 case ARM::R0:
637 case ARM::R1:
638 case ARM::R2:
639 case ARM::R3:
640 case ARM::R4:
641 case ARM::R5:
642 case ARM::R6:
643 case ARM::R7:
644 case ARM::LR:
645 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
646 nullptr, MRI->getDwarfRegNum(Reg, true), MFI.getObjectOffset(FI)));
647 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
648 .addCFIIndex(CFIIndex)
649 .setMIFlags(MachineInstr::FrameSetup);
650 break;
655 if (GPRCS2Size > 0) {
656 MachineBasicBlock::iterator Pos = std::next(GPRCS2Push);
657 for (const auto &Entry : CSI) {
658 unsigned Reg = Entry.getReg();
659 int FI = Entry.getFrameIdx();
660 switch (Reg) {
661 case ARM::R8:
662 case ARM::R9:
663 case ARM::R10:
664 case ARM::R11:
665 case ARM::R12:
666 if (STI.splitFramePushPop(MF)) {
667 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
668 unsigned Offset = MFI.getObjectOffset(FI);
669 unsigned CFIIndex = MF.addFrameInst(
670 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
671 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
672 .addCFIIndex(CFIIndex)
673 .setMIFlags(MachineInstr::FrameSetup);
675 break;
680 if (DPRCSSize > 0) {
681 // Since vpush register list cannot have gaps, there may be multiple vpush
682 // instructions in the prologue.
683 MachineBasicBlock::iterator Pos = std::next(LastPush);
684 for (const auto &Entry : CSI) {
685 unsigned Reg = Entry.getReg();
686 int FI = Entry.getFrameIdx();
687 if ((Reg >= ARM::D0 && Reg <= ARM::D31) &&
688 (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) {
689 unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
690 unsigned Offset = MFI.getObjectOffset(FI);
691 unsigned CFIIndex = MF.addFrameInst(
692 MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset));
693 BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
694 .addCFIIndex(CFIIndex)
695 .setMIFlags(MachineInstr::FrameSetup);
700 // Now we can emit descriptions of where the canonical frame address was
701 // throughout the process. If we have a frame pointer, it takes over the job
702 // half-way through, so only the first few .cfi_def_cfa_offset instructions
703 // actually get emitted.
704 DefCFAOffsetCandidates.emitDefCFAOffsets(MBB, dl, TII, HasFP);
706 if (STI.isTargetELF() && hasFP(MF))
707 MFI.setOffsetAdjustment(MFI.getOffsetAdjustment() -
708 AFI->getFramePtrSpillOffset());
710 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
711 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
712 AFI->setDPRCalleeSavedGapSize(DPRGapSize);
713 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
715 // If we need dynamic stack realignment, do it here. Be paranoid and make
716 // sure if we also have VLAs, we have a base pointer for frame access.
717 // If aligned NEON registers were spilled, the stack has already been
718 // realigned.
719 if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) {
720 unsigned MaxAlign = MFI.getMaxAlignment();
721 assert(!AFI->isThumb1OnlyFunction());
722 if (!AFI->isThumbFunction()) {
723 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign,
724 false);
725 } else {
726 // We cannot use sp as source/dest register here, thus we're using r4 to
727 // perform the calculations. We're emitting the following sequence:
728 // mov r4, sp
729 // -- use emitAligningInstructions to produce best sequence to zero
730 // -- out lower bits in r4
731 // mov sp, r4
732 // FIXME: It will be better just to find spare register here.
733 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4)
734 .addReg(ARM::SP, RegState::Kill)
735 .add(predOps(ARMCC::AL));
736 emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign,
737 false);
738 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
739 .addReg(ARM::R4, RegState::Kill)
740 .add(predOps(ARMCC::AL));
743 AFI->setShouldRestoreSPFromFP(true);
746 // If we need a base pointer, set it up here. It's whatever the value
747 // of the stack pointer is at this point. Any variable size objects
748 // will be allocated after this, so we can still use the base pointer
749 // to reference locals.
750 // FIXME: Clarify FrameSetup flags here.
751 if (RegInfo->hasBasePointer(MF)) {
752 if (isARM)
753 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), RegInfo->getBaseRegister())
754 .addReg(ARM::SP)
755 .add(predOps(ARMCC::AL))
756 .add(condCodeOp());
757 else
758 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), RegInfo->getBaseRegister())
759 .addReg(ARM::SP)
760 .add(predOps(ARMCC::AL));
763 // If the frame has variable sized objects then the epilogue must restore
764 // the sp from fp. We can assume there's an FP here since hasFP already
765 // checks for hasVarSizedObjects.
766 if (MFI.hasVarSizedObjects())
767 AFI->setShouldRestoreSPFromFP(true);
770 void ARMFrameLowering::emitEpilogue(MachineFunction &MF,
771 MachineBasicBlock &MBB) const {
772 MachineFrameInfo &MFI = MF.getFrameInfo();
773 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
774 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
775 const ARMBaseInstrInfo &TII =
776 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
777 assert(!AFI->isThumb1OnlyFunction() &&
778 "This emitEpilogue does not support Thumb1!");
779 bool isARM = !AFI->isThumbFunction();
781 unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize();
782 int NumBytes = (int)MFI.getStackSize();
783 unsigned FramePtr = RegInfo->getFrameRegister(MF);
785 // All calls are tail calls in GHC calling conv, and functions have no
786 // prologue/epilogue.
787 if (MF.getFunction().getCallingConv() == CallingConv::GHC)
788 return;
790 // First put ourselves on the first (from top) terminator instructions.
791 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
792 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
794 if (!AFI->hasStackFrame()) {
795 if (NumBytes - ArgRegsSaveSize != 0)
796 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize);
797 } else {
798 // Unwind MBBI to point to first LDR / VLDRD.
799 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
800 if (MBBI != MBB.begin()) {
801 do {
802 --MBBI;
803 } while (MBBI != MBB.begin() && isCSRestore(*MBBI, TII, CSRegs));
804 if (!isCSRestore(*MBBI, TII, CSRegs))
805 ++MBBI;
808 // Move SP to start of FP callee save spill area.
809 NumBytes -= (ArgRegsSaveSize +
810 AFI->getGPRCalleeSavedArea1Size() +
811 AFI->getGPRCalleeSavedArea2Size() +
812 AFI->getDPRCalleeSavedGapSize() +
813 AFI->getDPRCalleeSavedAreaSize());
815 // Reset SP based on frame pointer only if the stack frame extends beyond
816 // frame pointer stack slot or target is ELF and the function has FP.
817 if (AFI->shouldRestoreSPFromFP()) {
818 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
819 if (NumBytes) {
820 if (isARM)
821 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
822 ARMCC::AL, 0, TII);
823 else {
824 // It's not possible to restore SP from FP in a single instruction.
825 // For iOS, this looks like:
826 // mov sp, r7
827 // sub sp, #24
828 // This is bad, if an interrupt is taken after the mov, sp is in an
829 // inconsistent state.
830 // Use the first callee-saved register as a scratch register.
831 assert(!MFI.getPristineRegs(MF).test(ARM::R4) &&
832 "No scratch register to restore SP from FP!");
833 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes,
834 ARMCC::AL, 0, TII);
835 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
836 .addReg(ARM::R4)
837 .add(predOps(ARMCC::AL));
839 } else {
840 // Thumb2 or ARM.
841 if (isARM)
842 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
843 .addReg(FramePtr)
844 .add(predOps(ARMCC::AL))
845 .add(condCodeOp());
846 else
847 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP)
848 .addReg(FramePtr)
849 .add(predOps(ARMCC::AL));
851 } else if (NumBytes &&
852 !tryFoldSPUpdateIntoPushPop(STI, MF, &*MBBI, NumBytes))
853 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
855 // Increment past our save areas.
856 if (MBBI != MBB.end() && AFI->getDPRCalleeSavedAreaSize()) {
857 MBBI++;
858 // Since vpop register list cannot have gaps, there may be multiple vpop
859 // instructions in the epilogue.
860 while (MBBI != MBB.end() && MBBI->getOpcode() == ARM::VLDMDIA_UPD)
861 MBBI++;
863 if (AFI->getDPRCalleeSavedGapSize()) {
864 assert(AFI->getDPRCalleeSavedGapSize() == 4 &&
865 "unexpected DPR alignment gap");
866 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize());
869 if (AFI->getGPRCalleeSavedArea2Size()) MBBI++;
870 if (AFI->getGPRCalleeSavedArea1Size()) MBBI++;
873 if (ArgRegsSaveSize)
874 emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize);
877 /// getFrameIndexReference - Provide a base+offset reference to an FI slot for
878 /// debug info. It's the same as what we use for resolving the code-gen
879 /// references for now. FIXME: This can go wrong when references are
880 /// SP-relative and simple call frames aren't used.
882 ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
883 unsigned &FrameReg) const {
884 return ResolveFrameIndexReference(MF, FI, FrameReg, 0);
888 ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF,
889 int FI, unsigned &FrameReg,
890 int SPAdj) const {
891 const MachineFrameInfo &MFI = MF.getFrameInfo();
892 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
893 MF.getSubtarget().getRegisterInfo());
894 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
895 int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize();
896 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
897 bool isFixed = MFI.isFixedObjectIndex(FI);
899 FrameReg = ARM::SP;
900 Offset += SPAdj;
902 // SP can move around if there are allocas. We may also lose track of SP
903 // when emergency spilling inside a non-reserved call frame setup.
904 bool hasMovingSP = !hasReservedCallFrame(MF);
906 // When dynamically realigning the stack, use the frame pointer for
907 // parameters, and the stack/base pointer for locals.
908 if (RegInfo->needsStackRealignment(MF)) {
909 assert(hasFP(MF) && "dynamic stack realignment without a FP!");
910 if (isFixed) {
911 FrameReg = RegInfo->getFrameRegister(MF);
912 Offset = FPOffset;
913 } else if (hasMovingSP) {
914 assert(RegInfo->hasBasePointer(MF) &&
915 "VLAs and dynamic stack alignment, but missing base pointer!");
916 FrameReg = RegInfo->getBaseRegister();
917 Offset -= SPAdj;
919 return Offset;
922 // If there is a frame pointer, use it when we can.
923 if (hasFP(MF) && AFI->hasStackFrame()) {
924 // Use frame pointer to reference fixed objects. Use it for locals if
925 // there are VLAs (and thus the SP isn't reliable as a base).
926 if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) {
927 FrameReg = RegInfo->getFrameRegister(MF);
928 return FPOffset;
929 } else if (hasMovingSP) {
930 assert(RegInfo->hasBasePointer(MF) && "missing base pointer!");
931 if (AFI->isThumb2Function()) {
932 // Try to use the frame pointer if we can, else use the base pointer
933 // since it's available. This is handy for the emergency spill slot, in
934 // particular.
935 if (FPOffset >= -255 && FPOffset < 0) {
936 FrameReg = RegInfo->getFrameRegister(MF);
937 return FPOffset;
940 } else if (AFI->isThumbFunction()) {
941 // Prefer SP to base pointer, if the offset is suitably aligned and in
942 // range as the effective range of the immediate offset is bigger when
943 // basing off SP.
944 // Use add <rd>, sp, #<imm8>
945 // ldr <rd>, [sp, #<imm8>]
946 if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020)
947 return Offset;
948 // In Thumb2 mode, the negative offset is very limited. Try to avoid
949 // out of range references. ldr <rt>,[<rn>, #-<imm8>]
950 if (AFI->isThumb2Function() && FPOffset >= -255 && FPOffset < 0) {
951 FrameReg = RegInfo->getFrameRegister(MF);
952 return FPOffset;
954 } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) {
955 // Otherwise, use SP or FP, whichever is closer to the stack slot.
956 FrameReg = RegInfo->getFrameRegister(MF);
957 return FPOffset;
960 // Use the base pointer if we have one.
961 // FIXME: Maybe prefer sp on Thumb1 if it's legal and the offset is cheaper?
962 // That can happen if we forced a base pointer for a large call frame.
963 if (RegInfo->hasBasePointer(MF)) {
964 FrameReg = RegInfo->getBaseRegister();
965 Offset -= SPAdj;
967 return Offset;
970 void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB,
971 MachineBasicBlock::iterator MI,
972 const std::vector<CalleeSavedInfo> &CSI,
973 unsigned StmOpc, unsigned StrOpc,
974 bool NoGap,
975 bool(*Func)(unsigned, bool),
976 unsigned NumAlignedDPRCS2Regs,
977 unsigned MIFlags) const {
978 MachineFunction &MF = *MBB.getParent();
979 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
980 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
982 DebugLoc DL;
984 using RegAndKill = std::pair<unsigned, bool>;
986 SmallVector<RegAndKill, 4> Regs;
987 unsigned i = CSI.size();
988 while (i != 0) {
989 unsigned LastReg = 0;
990 for (; i != 0; --i) {
991 unsigned Reg = CSI[i-1].getReg();
992 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
994 // D-registers in the aligned area DPRCS2 are NOT spilled here.
995 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
996 continue;
998 const MachineRegisterInfo &MRI = MF.getRegInfo();
999 bool isLiveIn = MRI.isLiveIn(Reg);
1000 if (!isLiveIn && !MRI.isReserved(Reg))
1001 MBB.addLiveIn(Reg);
1002 // If NoGap is true, push consecutive registers and then leave the rest
1003 // for other instructions. e.g.
1004 // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11}
1005 if (NoGap && LastReg && LastReg != Reg-1)
1006 break;
1007 LastReg = Reg;
1008 // Do not set a kill flag on values that are also marked as live-in. This
1009 // happens with the @llvm-returnaddress intrinsic and with arguments
1010 // passed in callee saved registers.
1011 // Omitting the kill flags is conservatively correct even if the live-in
1012 // is not used after all.
1013 Regs.push_back(std::make_pair(Reg, /*isKill=*/!isLiveIn));
1016 if (Regs.empty())
1017 continue;
1019 llvm::sort(Regs, [&](const RegAndKill &LHS, const RegAndKill &RHS) {
1020 return TRI.getEncodingValue(LHS.first) < TRI.getEncodingValue(RHS.first);
1023 if (Regs.size() > 1 || StrOpc== 0) {
1024 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP)
1025 .addReg(ARM::SP)
1026 .setMIFlags(MIFlags)
1027 .add(predOps(ARMCC::AL));
1028 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1029 MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second));
1030 } else if (Regs.size() == 1) {
1031 BuildMI(MBB, MI, DL, TII.get(StrOpc), ARM::SP)
1032 .addReg(Regs[0].first, getKillRegState(Regs[0].second))
1033 .addReg(ARM::SP)
1034 .setMIFlags(MIFlags)
1035 .addImm(-4)
1036 .add(predOps(ARMCC::AL));
1038 Regs.clear();
1040 // Put any subsequent vpush instructions before this one: they will refer to
1041 // higher register numbers so need to be pushed first in order to preserve
1042 // monotonicity.
1043 if (MI != MBB.begin())
1044 --MI;
1048 void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB,
1049 MachineBasicBlock::iterator MI,
1050 std::vector<CalleeSavedInfo> &CSI,
1051 unsigned LdmOpc, unsigned LdrOpc,
1052 bool isVarArg, bool NoGap,
1053 bool(*Func)(unsigned, bool),
1054 unsigned NumAlignedDPRCS2Regs) const {
1055 MachineFunction &MF = *MBB.getParent();
1056 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1057 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
1058 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1059 DebugLoc DL;
1060 bool isTailCall = false;
1061 bool isInterrupt = false;
1062 bool isTrap = false;
1063 if (MBB.end() != MI) {
1064 DL = MI->getDebugLoc();
1065 unsigned RetOpcode = MI->getOpcode();
1066 isTailCall = (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri);
1067 isInterrupt =
1068 RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR;
1069 isTrap =
1070 RetOpcode == ARM::TRAP || RetOpcode == ARM::TRAPNaCl ||
1071 RetOpcode == ARM::tTRAP;
1074 SmallVector<unsigned, 4> Regs;
1075 unsigned i = CSI.size();
1076 while (i != 0) {
1077 unsigned LastReg = 0;
1078 bool DeleteRet = false;
1079 for (; i != 0; --i) {
1080 CalleeSavedInfo &Info = CSI[i-1];
1081 unsigned Reg = Info.getReg();
1082 if (!(Func)(Reg, STI.splitFramePushPop(MF))) continue;
1084 // The aligned reloads from area DPRCS2 are not inserted here.
1085 if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs)
1086 continue;
1088 if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt &&
1089 !isTrap && STI.hasV5TOps()) {
1090 if (MBB.succ_empty()) {
1091 Reg = ARM::PC;
1092 // Fold the return instruction into the LDM.
1093 DeleteRet = true;
1094 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET;
1095 // We 'restore' LR into PC so it is not live out of the return block:
1096 // Clear Restored bit.
1097 Info.setRestored(false);
1098 } else
1099 LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
1102 // If NoGap is true, pop consecutive registers and then leave the rest
1103 // for other instructions. e.g.
1104 // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11}
1105 if (NoGap && LastReg && LastReg != Reg-1)
1106 break;
1108 LastReg = Reg;
1109 Regs.push_back(Reg);
1112 if (Regs.empty())
1113 continue;
1115 llvm::sort(Regs, [&](unsigned LHS, unsigned RHS) {
1116 return TRI.getEncodingValue(LHS) < TRI.getEncodingValue(RHS);
1119 if (Regs.size() > 1 || LdrOpc == 0) {
1120 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP)
1121 .addReg(ARM::SP)
1122 .add(predOps(ARMCC::AL));
1123 for (unsigned i = 0, e = Regs.size(); i < e; ++i)
1124 MIB.addReg(Regs[i], getDefRegState(true));
1125 if (DeleteRet) {
1126 if (MI != MBB.end()) {
1127 MIB.copyImplicitOps(*MI);
1128 MI->eraseFromParent();
1131 MI = MIB;
1132 } else if (Regs.size() == 1) {
1133 // If we adjusted the reg to PC from LR above, switch it back here. We
1134 // only do that for LDM.
1135 if (Regs[0] == ARM::PC)
1136 Regs[0] = ARM::LR;
1137 MachineInstrBuilder MIB =
1138 BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0])
1139 .addReg(ARM::SP, RegState::Define)
1140 .addReg(ARM::SP);
1141 // ARM mode needs an extra reg0 here due to addrmode2. Will go away once
1142 // that refactoring is complete (eventually).
1143 if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) {
1144 MIB.addReg(0);
1145 MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift));
1146 } else
1147 MIB.addImm(4);
1148 MIB.add(predOps(ARMCC::AL));
1150 Regs.clear();
1152 // Put any subsequent vpop instructions after this one: they will refer to
1153 // higher register numbers so need to be popped afterwards.
1154 if (MI != MBB.end())
1155 ++MI;
1159 /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers
1160 /// starting from d8. Also insert stack realignment code and leave the stack
1161 /// pointer pointing to the d8 spill slot.
1162 static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB,
1163 MachineBasicBlock::iterator MI,
1164 unsigned NumAlignedDPRCS2Regs,
1165 const std::vector<CalleeSavedInfo> &CSI,
1166 const TargetRegisterInfo *TRI) {
1167 MachineFunction &MF = *MBB.getParent();
1168 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1169 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
1170 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1171 MachineFrameInfo &MFI = MF.getFrameInfo();
1173 // Mark the D-register spill slots as properly aligned. Since MFI computes
1174 // stack slot layout backwards, this can actually mean that the d-reg stack
1175 // slot offsets can be wrong. The offset for d8 will always be correct.
1176 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1177 unsigned DNum = CSI[i].getReg() - ARM::D8;
1178 if (DNum > NumAlignedDPRCS2Regs - 1)
1179 continue;
1180 int FI = CSI[i].getFrameIdx();
1181 // The even-numbered registers will be 16-byte aligned, the odd-numbered
1182 // registers will be 8-byte aligned.
1183 MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16);
1185 // The stack slot for D8 needs to be maximally aligned because this is
1186 // actually the point where we align the stack pointer. MachineFrameInfo
1187 // computes all offsets relative to the incoming stack pointer which is a
1188 // bit weird when realigning the stack. Any extra padding for this
1189 // over-alignment is not realized because the code inserted below adjusts
1190 // the stack pointer by numregs * 8 before aligning the stack pointer.
1191 if (DNum == 0)
1192 MFI.setObjectAlignment(FI, MFI.getMaxAlignment());
1195 // Move the stack pointer to the d8 spill slot, and align it at the same
1196 // time. Leave the stack slot address in the scratch register r4.
1198 // sub r4, sp, #numregs * 8
1199 // bic r4, r4, #align - 1
1200 // mov sp, r4
1202 bool isThumb = AFI->isThumbFunction();
1203 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1204 AFI->setShouldRestoreSPFromFP(true);
1206 // sub r4, sp, #numregs * 8
1207 // The immediate is <= 64, so it doesn't need any special encoding.
1208 unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri;
1209 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1210 .addReg(ARM::SP)
1211 .addImm(8 * NumAlignedDPRCS2Regs)
1212 .add(predOps(ARMCC::AL))
1213 .add(condCodeOp());
1215 unsigned MaxAlign = MF.getFrameInfo().getMaxAlignment();
1216 // We must set parameter MustBeSingleInstruction to true, since
1217 // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform
1218 // stack alignment. Luckily, this can always be done since all ARM
1219 // architecture versions that support Neon also support the BFC
1220 // instruction.
1221 emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true);
1223 // mov sp, r4
1224 // The stack pointer must be adjusted before spilling anything, otherwise
1225 // the stack slots could be clobbered by an interrupt handler.
1226 // Leave r4 live, it is used below.
1227 Opc = isThumb ? ARM::tMOVr : ARM::MOVr;
1228 MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP)
1229 .addReg(ARM::R4)
1230 .add(predOps(ARMCC::AL));
1231 if (!isThumb)
1232 MIB.add(condCodeOp());
1234 // Now spill NumAlignedDPRCS2Regs registers starting from d8.
1235 // r4 holds the stack slot address.
1236 unsigned NextReg = ARM::D8;
1238 // 16-byte aligned vst1.64 with 4 d-regs and address writeback.
1239 // The writeback is only needed when emitting two vst1.64 instructions.
1240 if (NumAlignedDPRCS2Regs >= 6) {
1241 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1242 &ARM::QQPRRegClass);
1243 MBB.addLiveIn(SupReg);
1244 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), ARM::R4)
1245 .addReg(ARM::R4, RegState::Kill)
1246 .addImm(16)
1247 .addReg(NextReg)
1248 .addReg(SupReg, RegState::ImplicitKill)
1249 .add(predOps(ARMCC::AL));
1250 NextReg += 4;
1251 NumAlignedDPRCS2Regs -= 4;
1254 // We won't modify r4 beyond this point. It currently points to the next
1255 // register to be spilled.
1256 unsigned R4BaseReg = NextReg;
1258 // 16-byte aligned vst1.64 with 4 d-regs, no writeback.
1259 if (NumAlignedDPRCS2Regs >= 4) {
1260 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1261 &ARM::QQPRRegClass);
1262 MBB.addLiveIn(SupReg);
1263 BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q))
1264 .addReg(ARM::R4)
1265 .addImm(16)
1266 .addReg(NextReg)
1267 .addReg(SupReg, RegState::ImplicitKill)
1268 .add(predOps(ARMCC::AL));
1269 NextReg += 4;
1270 NumAlignedDPRCS2Regs -= 4;
1273 // 16-byte aligned vst1.64 with 2 d-regs.
1274 if (NumAlignedDPRCS2Regs >= 2) {
1275 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1276 &ARM::QPRRegClass);
1277 MBB.addLiveIn(SupReg);
1278 BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64))
1279 .addReg(ARM::R4)
1280 .addImm(16)
1281 .addReg(SupReg)
1282 .add(predOps(ARMCC::AL));
1283 NextReg += 2;
1284 NumAlignedDPRCS2Regs -= 2;
1287 // Finally, use a vanilla vstr.64 for the odd last register.
1288 if (NumAlignedDPRCS2Regs) {
1289 MBB.addLiveIn(NextReg);
1290 // vstr.64 uses addrmode5 which has an offset scale of 4.
1291 BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD))
1292 .addReg(NextReg)
1293 .addReg(ARM::R4)
1294 .addImm((NextReg - R4BaseReg) * 2)
1295 .add(predOps(ARMCC::AL));
1298 // The last spill instruction inserted should kill the scratch register r4.
1299 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
1302 /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an
1303 /// iterator to the following instruction.
1304 static MachineBasicBlock::iterator
1305 skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI,
1306 unsigned NumAlignedDPRCS2Regs) {
1307 // sub r4, sp, #numregs * 8
1308 // bic r4, r4, #align - 1
1309 // mov sp, r4
1310 ++MI; ++MI; ++MI;
1311 assert(MI->mayStore() && "Expecting spill instruction");
1313 // These switches all fall through.
1314 switch(NumAlignedDPRCS2Regs) {
1315 case 7:
1316 ++MI;
1317 assert(MI->mayStore() && "Expecting spill instruction");
1318 LLVM_FALLTHROUGH;
1319 default:
1320 ++MI;
1321 assert(MI->mayStore() && "Expecting spill instruction");
1322 LLVM_FALLTHROUGH;
1323 case 1:
1324 case 2:
1325 case 4:
1326 assert(MI->killsRegister(ARM::R4) && "Missed kill flag");
1327 ++MI;
1329 return MI;
1332 /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers
1333 /// starting from d8. These instructions are assumed to execute while the
1334 /// stack is still aligned, unlike the code inserted by emitPopInst.
1335 static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB,
1336 MachineBasicBlock::iterator MI,
1337 unsigned NumAlignedDPRCS2Regs,
1338 const std::vector<CalleeSavedInfo> &CSI,
1339 const TargetRegisterInfo *TRI) {
1340 MachineFunction &MF = *MBB.getParent();
1341 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1342 DebugLoc DL = MI != MBB.end() ? MI->getDebugLoc() : DebugLoc();
1343 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1345 // Find the frame index assigned to d8.
1346 int D8SpillFI = 0;
1347 for (unsigned i = 0, e = CSI.size(); i != e; ++i)
1348 if (CSI[i].getReg() == ARM::D8) {
1349 D8SpillFI = CSI[i].getFrameIdx();
1350 break;
1353 // Materialize the address of the d8 spill slot into the scratch register r4.
1354 // This can be fairly complicated if the stack frame is large, so just use
1355 // the normal frame index elimination mechanism to do it. This code runs as
1356 // the initial part of the epilog where the stack and base pointers haven't
1357 // been changed yet.
1358 bool isThumb = AFI->isThumbFunction();
1359 assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1");
1361 unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri;
1362 BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4)
1363 .addFrameIndex(D8SpillFI)
1364 .addImm(0)
1365 .add(predOps(ARMCC::AL))
1366 .add(condCodeOp());
1368 // Now restore NumAlignedDPRCS2Regs registers starting from d8.
1369 unsigned NextReg = ARM::D8;
1371 // 16-byte aligned vld1.64 with 4 d-regs and writeback.
1372 if (NumAlignedDPRCS2Regs >= 6) {
1373 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1374 &ARM::QQPRRegClass);
1375 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg)
1376 .addReg(ARM::R4, RegState::Define)
1377 .addReg(ARM::R4, RegState::Kill)
1378 .addImm(16)
1379 .addReg(SupReg, RegState::ImplicitDefine)
1380 .add(predOps(ARMCC::AL));
1381 NextReg += 4;
1382 NumAlignedDPRCS2Regs -= 4;
1385 // We won't modify r4 beyond this point. It currently points to the next
1386 // register to be spilled.
1387 unsigned R4BaseReg = NextReg;
1389 // 16-byte aligned vld1.64 with 4 d-regs, no writeback.
1390 if (NumAlignedDPRCS2Regs >= 4) {
1391 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1392 &ARM::QQPRRegClass);
1393 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg)
1394 .addReg(ARM::R4)
1395 .addImm(16)
1396 .addReg(SupReg, RegState::ImplicitDefine)
1397 .add(predOps(ARMCC::AL));
1398 NextReg += 4;
1399 NumAlignedDPRCS2Regs -= 4;
1402 // 16-byte aligned vld1.64 with 2 d-regs.
1403 if (NumAlignedDPRCS2Regs >= 2) {
1404 unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0,
1405 &ARM::QPRRegClass);
1406 BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg)
1407 .addReg(ARM::R4)
1408 .addImm(16)
1409 .add(predOps(ARMCC::AL));
1410 NextReg += 2;
1411 NumAlignedDPRCS2Regs -= 2;
1414 // Finally, use a vanilla vldr.64 for the remaining odd register.
1415 if (NumAlignedDPRCS2Regs)
1416 BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg)
1417 .addReg(ARM::R4)
1418 .addImm(2 * (NextReg - R4BaseReg))
1419 .add(predOps(ARMCC::AL));
1421 // Last store kills r4.
1422 std::prev(MI)->addRegisterKilled(ARM::R4, TRI);
1425 bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
1426 MachineBasicBlock::iterator MI,
1427 const std::vector<CalleeSavedInfo> &CSI,
1428 const TargetRegisterInfo *TRI) const {
1429 if (CSI.empty())
1430 return false;
1432 MachineFunction &MF = *MBB.getParent();
1433 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1435 unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD;
1436 unsigned PushOneOpc = AFI->isThumbFunction() ?
1437 ARM::t2STR_PRE : ARM::STR_PRE_IMM;
1438 unsigned FltOpc = ARM::VSTMDDB_UPD;
1439 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1440 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0,
1441 MachineInstr::FrameSetup);
1442 emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0,
1443 MachineInstr::FrameSetup);
1444 emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register,
1445 NumAlignedDPRCS2Regs, MachineInstr::FrameSetup);
1447 // The code above does not insert spill code for the aligned DPRCS2 registers.
1448 // The stack realignment code will be inserted between the push instructions
1449 // and these spills.
1450 if (NumAlignedDPRCS2Regs)
1451 emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
1453 return true;
1456 bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
1457 MachineBasicBlock::iterator MI,
1458 std::vector<CalleeSavedInfo> &CSI,
1459 const TargetRegisterInfo *TRI) const {
1460 if (CSI.empty())
1461 return false;
1463 MachineFunction &MF = *MBB.getParent();
1464 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1465 bool isVarArg = AFI->getArgRegsSaveSize() > 0;
1466 unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs();
1468 // The emitPopInst calls below do not insert reloads for the aligned DPRCS2
1469 // registers. Do that here instead.
1470 if (NumAlignedDPRCS2Regs)
1471 emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI);
1473 unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD;
1474 unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM;
1475 unsigned FltOpc = ARM::VLDMDIA_UPD;
1476 emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register,
1477 NumAlignedDPRCS2Regs);
1478 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
1479 &isARMArea2Register, 0);
1480 emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false,
1481 &isARMArea1Register, 0);
1483 return true;
1486 // FIXME: Make generic?
1487 static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF,
1488 const ARMBaseInstrInfo &TII) {
1489 unsigned FnSize = 0;
1490 for (auto &MBB : MF) {
1491 for (auto &MI : MBB)
1492 FnSize += TII.getInstSizeInBytes(MI);
1494 if (MF.getJumpTableInfo())
1495 for (auto &Table: MF.getJumpTableInfo()->getJumpTables())
1496 FnSize += Table.MBBs.size() * 4;
1497 FnSize += MF.getConstantPool()->getConstants().size() * 4;
1498 return FnSize;
1501 /// estimateRSStackSizeLimit - Look at each instruction that references stack
1502 /// frames and return the stack size limit beyond which some of these
1503 /// instructions will require a scratch register during their expansion later.
1504 // FIXME: Move to TII?
1505 static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
1506 const TargetFrameLowering *TFI) {
1507 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1508 unsigned Limit = (1 << 12) - 1;
1509 for (auto &MBB : MF) {
1510 for (auto &MI : MBB) {
1511 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1512 if (!MI.getOperand(i).isFI())
1513 continue;
1515 // When using ADDri to get the address of a stack object, 255 is the
1516 // largest offset guaranteed to fit in the immediate offset.
1517 if (MI.getOpcode() == ARM::ADDri) {
1518 Limit = std::min(Limit, (1U << 8) - 1);
1519 break;
1522 // Otherwise check the addressing mode.
1523 switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) {
1524 case ARMII::AddrMode3:
1525 case ARMII::AddrModeT2_i8:
1526 Limit = std::min(Limit, (1U << 8) - 1);
1527 break;
1528 case ARMII::AddrMode5:
1529 case ARMII::AddrModeT2_i8s4:
1530 case ARMII::AddrModeT2_ldrex:
1531 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
1532 break;
1533 case ARMII::AddrModeT2_i12:
1534 // i12 supports only positive offset so these will be converted to
1535 // i8 opcodes. See llvm::rewriteT2FrameIndex.
1536 if (TFI->hasFP(MF) && AFI->hasStackFrame())
1537 Limit = std::min(Limit, (1U << 8) - 1);
1538 break;
1539 case ARMII::AddrMode4:
1540 case ARMII::AddrMode6:
1541 // Addressing modes 4 & 6 (load/store) instructions can't encode an
1542 // immediate offset for stack references.
1543 return 0;
1544 default:
1545 break;
1547 break; // At most one FI per instruction
1552 return Limit;
1555 // In functions that realign the stack, it can be an advantage to spill the
1556 // callee-saved vector registers after realigning the stack. The vst1 and vld1
1557 // instructions take alignment hints that can improve performance.
1558 static void
1559 checkNumAlignedDPRCS2Regs(MachineFunction &MF, BitVector &SavedRegs) {
1560 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0);
1561 if (!SpillAlignedNEONRegs)
1562 return;
1564 // Naked functions don't spill callee-saved registers.
1565 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
1566 return;
1568 // We are planning to use NEON instructions vst1 / vld1.
1569 if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON())
1570 return;
1572 // Don't bother if the default stack alignment is sufficiently high.
1573 if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8)
1574 return;
1576 // Aligned spills require stack realignment.
1577 if (!static_cast<const ARMBaseRegisterInfo *>(
1578 MF.getSubtarget().getRegisterInfo())->canRealignStack(MF))
1579 return;
1581 // We always spill contiguous d-registers starting from d8. Count how many
1582 // needs spilling. The register allocator will almost always use the
1583 // callee-saved registers in order, but it can happen that there are holes in
1584 // the range. Registers above the hole will be spilled to the standard DPRCS
1585 // area.
1586 unsigned NumSpills = 0;
1587 for (; NumSpills < 8; ++NumSpills)
1588 if (!SavedRegs.test(ARM::D8 + NumSpills))
1589 break;
1591 // Don't do this for just one d-register. It's not worth it.
1592 if (NumSpills < 2)
1593 return;
1595 // Spill the first NumSpills D-registers after realigning the stack.
1596 MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills);
1598 // A scratch register is required for the vst1 / vld1 instructions.
1599 SavedRegs.set(ARM::R4);
1602 void ARMFrameLowering::determineCalleeSaves(MachineFunction &MF,
1603 BitVector &SavedRegs,
1604 RegScavenger *RS) const {
1605 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
1606 // This tells PEI to spill the FP as if it is any other callee-save register
1607 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1608 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1609 // to combine multiple loads / stores.
1610 bool CanEliminateFrame = true;
1611 bool CS1Spilled = false;
1612 bool LRSpilled = false;
1613 unsigned NumGPRSpills = 0;
1614 unsigned NumFPRSpills = 0;
1615 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1616 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1617 const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>(
1618 MF.getSubtarget().getRegisterInfo());
1619 const ARMBaseInstrInfo &TII =
1620 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
1621 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1622 MachineFrameInfo &MFI = MF.getFrameInfo();
1623 MachineRegisterInfo &MRI = MF.getRegInfo();
1624 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1625 (void)TRI; // Silence unused warning in non-assert builds.
1626 unsigned FramePtr = RegInfo->getFrameRegister(MF);
1628 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
1629 // scratch register. Also spill R4 if Thumb2 function has varsized objects,
1630 // since it's not always possible to restore sp from fp in a single
1631 // instruction.
1632 // FIXME: It will be better just to find spare register here.
1633 if (AFI->isThumb2Function() &&
1634 (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF)))
1635 SavedRegs.set(ARM::R4);
1637 // If a stack probe will be emitted, spill R4 and LR, since they are
1638 // clobbered by the stack probe call.
1639 // This estimate should be a safe, conservative estimate. The actual
1640 // stack probe is enabled based on the size of the local objects;
1641 // this estimate also includes the varargs store size.
1642 if (STI.isTargetWindows() &&
1643 WindowsRequiresStackProbe(MF, MFI.estimateStackSize(MF))) {
1644 SavedRegs.set(ARM::R4);
1645 SavedRegs.set(ARM::LR);
1648 if (AFI->isThumb1OnlyFunction()) {
1649 // Spill LR if Thumb1 function uses variable length argument lists.
1650 if (AFI->getArgRegsSaveSize() > 0)
1651 SavedRegs.set(ARM::LR);
1653 // Spill R4 if Thumb1 epilogue has to restore SP from FP or the function
1654 // requires stack alignment. We don't know for sure what the stack size
1655 // will be, but for this, an estimate is good enough. If there anything
1656 // changes it, it'll be a spill, which implies we've used all the registers
1657 // and so R4 is already used, so not marking it here will be OK.
1658 // FIXME: It will be better just to find spare register here.
1659 if (MFI.hasVarSizedObjects() || RegInfo->needsStackRealignment(MF) ||
1660 MFI.estimateStackSize(MF) > 508)
1661 SavedRegs.set(ARM::R4);
1664 // See if we can spill vector registers to aligned stack.
1665 checkNumAlignedDPRCS2Regs(MF, SavedRegs);
1667 // Spill the BasePtr if it's used.
1668 if (RegInfo->hasBasePointer(MF))
1669 SavedRegs.set(RegInfo->getBaseRegister());
1671 // Don't spill FP if the frame can be eliminated. This is determined
1672 // by scanning the callee-save registers to see if any is modified.
1673 const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF);
1674 for (unsigned i = 0; CSRegs[i]; ++i) {
1675 unsigned Reg = CSRegs[i];
1676 bool Spilled = false;
1677 if (SavedRegs.test(Reg)) {
1678 Spilled = true;
1679 CanEliminateFrame = false;
1682 if (!ARM::GPRRegClass.contains(Reg)) {
1683 if (Spilled) {
1684 if (ARM::SPRRegClass.contains(Reg))
1685 NumFPRSpills++;
1686 else if (ARM::DPRRegClass.contains(Reg))
1687 NumFPRSpills += 2;
1688 else if (ARM::QPRRegClass.contains(Reg))
1689 NumFPRSpills += 4;
1691 continue;
1694 if (Spilled) {
1695 NumGPRSpills++;
1697 if (!STI.splitFramePushPop(MF)) {
1698 if (Reg == ARM::LR)
1699 LRSpilled = true;
1700 CS1Spilled = true;
1701 continue;
1704 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1705 switch (Reg) {
1706 case ARM::LR:
1707 LRSpilled = true;
1708 LLVM_FALLTHROUGH;
1709 case ARM::R0: case ARM::R1:
1710 case ARM::R2: case ARM::R3:
1711 case ARM::R4: case ARM::R5:
1712 case ARM::R6: case ARM::R7:
1713 CS1Spilled = true;
1714 break;
1715 default:
1716 break;
1718 } else {
1719 if (!STI.splitFramePushPop(MF)) {
1720 UnspilledCS1GPRs.push_back(Reg);
1721 continue;
1724 switch (Reg) {
1725 case ARM::R0: case ARM::R1:
1726 case ARM::R2: case ARM::R3:
1727 case ARM::R4: case ARM::R5:
1728 case ARM::R6: case ARM::R7:
1729 case ARM::LR:
1730 UnspilledCS1GPRs.push_back(Reg);
1731 break;
1732 default:
1733 UnspilledCS2GPRs.push_back(Reg);
1734 break;
1739 bool ForceLRSpill = false;
1740 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
1741 unsigned FnSize = EstimateFunctionSizeInBytes(MF, TII);
1742 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1743 // use of BL to implement far jump. If it turns out that it's not needed
1744 // then the branch fix up path will undo it.
1745 if (FnSize >= (1 << 11)) {
1746 CanEliminateFrame = false;
1747 ForceLRSpill = true;
1751 // If any of the stack slot references may be out of range of an immediate
1752 // offset, make sure a register (or a spill slot) is available for the
1753 // register scavenger. Note that if we're indexing off the frame pointer, the
1754 // effective stack size is 4 bytes larger since the FP points to the stack
1755 // slot of the previous FP. Also, if we have variable sized objects in the
1756 // function, stack slot references will often be negative, and some of
1757 // our instructions are positive-offset only, so conservatively consider
1758 // that case to want a spill slot (or register) as well. Similarly, if
1759 // the function adjusts the stack pointer during execution and the
1760 // adjustments aren't already part of our stack size estimate, our offset
1761 // calculations may be off, so be conservative.
1762 // FIXME: We could add logic to be more precise about negative offsets
1763 // and which instructions will need a scratch register for them. Is it
1764 // worth the effort and added fragility?
1765 unsigned EstimatedStackSize =
1766 MFI.estimateStackSize(MF) + 4 * (NumGPRSpills + NumFPRSpills);
1768 // Determine biggest (positive) SP offset in MachineFrameInfo.
1769 int MaxFixedOffset = 0;
1770 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) {
1771 int MaxObjectOffset = MFI.getObjectOffset(I) + MFI.getObjectSize(I);
1772 MaxFixedOffset = std::max(MaxFixedOffset, MaxObjectOffset);
1775 bool HasFP = hasFP(MF);
1776 if (HasFP) {
1777 if (AFI->hasStackFrame())
1778 EstimatedStackSize += 4;
1779 } else {
1780 // If FP is not used, SP will be used to access arguments, so count the
1781 // size of arguments into the estimation.
1782 EstimatedStackSize += MaxFixedOffset;
1784 EstimatedStackSize += 16; // For possible paddings.
1786 unsigned EstimatedRSStackSizeLimit, EstimatedRSFixedSizeLimit;
1787 if (AFI->isThumb1OnlyFunction()) {
1788 // For Thumb1, don't bother to iterate over the function. The only
1789 // instruction that requires an emergency spill slot is a store to a
1790 // frame index.
1792 // tSTRspi, which is used for sp-relative accesses, has an 8-bit unsigned
1793 // immediate. tSTRi, which is used for bp- and fp-relative accesses, has
1794 // a 5-bit unsigned immediate.
1796 // We could try to check if the function actually contains a tSTRspi
1797 // that might need the spill slot, but it's not really important.
1798 // Functions with VLAs or extremely large call frames are rare, and
1799 // if a function is allocating more than 1KB of stack, an extra 4-byte
1800 // slot probably isn't relevant.
1801 if (RegInfo->hasBasePointer(MF))
1802 EstimatedRSStackSizeLimit = (1U << 5) * 4;
1803 else
1804 EstimatedRSStackSizeLimit = (1U << 8) * 4;
1805 EstimatedRSFixedSizeLimit = (1U << 5) * 4;
1806 } else {
1807 EstimatedRSStackSizeLimit = estimateRSStackSizeLimit(MF, this);
1808 EstimatedRSFixedSizeLimit = EstimatedRSStackSizeLimit;
1810 // Final estimate of whether sp or bp-relative accesses might require
1811 // scavenging.
1812 bool HasLargeStack = EstimatedStackSize > EstimatedRSStackSizeLimit;
1814 // If the stack pointer moves and we don't have a base pointer, the
1815 // estimate logic doesn't work. The actual offsets might be larger when
1816 // we're constructing a call frame, or we might need to use negative
1817 // offsets from fp.
1818 bool HasMovingSP = MFI.hasVarSizedObjects() ||
1819 (MFI.adjustsStack() && !canSimplifyCallFramePseudos(MF));
1820 bool HasBPOrFixedSP = RegInfo->hasBasePointer(MF) || !HasMovingSP;
1822 // If we have a frame pointer, we assume arguments will be accessed
1823 // relative to the frame pointer. Check whether fp-relative accesses to
1824 // arguments require scavenging.
1826 // We could do slightly better on Thumb1; in some cases, an sp-relative
1827 // offset would be legal even though an fp-relative offset is not.
1828 int MaxFPOffset = getMaxFPOffset(MF.getFunction(), *AFI);
1829 bool HasLargeArgumentList =
1830 HasFP && (MaxFixedOffset - MaxFPOffset) > (int)EstimatedRSFixedSizeLimit;
1832 bool BigFrameOffsets = HasLargeStack || !HasBPOrFixedSP ||
1833 HasLargeArgumentList;
1834 LLVM_DEBUG(dbgs() << "EstimatedLimit: " << EstimatedRSStackSizeLimit
1835 << "; EstimatedStack" << EstimatedStackSize
1836 << "; EstimatedFPStack" << MaxFixedOffset - MaxFPOffset
1837 << "; BigFrameOffsets: " << BigFrameOffsets
1838 << "\n");
1839 if (BigFrameOffsets ||
1840 !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) {
1841 AFI->setHasStackFrame(true);
1843 if (HasFP) {
1844 SavedRegs.set(FramePtr);
1845 // If the frame pointer is required by the ABI, also spill LR so that we
1846 // emit a complete frame record.
1847 if (MF.getTarget().Options.DisableFramePointerElim(MF) && !LRSpilled) {
1848 SavedRegs.set(ARM::LR);
1849 LRSpilled = true;
1850 NumGPRSpills++;
1851 auto LRPos = llvm::find(UnspilledCS1GPRs, ARM::LR);
1852 if (LRPos != UnspilledCS1GPRs.end())
1853 UnspilledCS1GPRs.erase(LRPos);
1855 auto FPPos = llvm::find(UnspilledCS1GPRs, FramePtr);
1856 if (FPPos != UnspilledCS1GPRs.end())
1857 UnspilledCS1GPRs.erase(FPPos);
1858 NumGPRSpills++;
1859 if (FramePtr == ARM::R7)
1860 CS1Spilled = true;
1863 // This is true when we inserted a spill for a callee-save GPR which is
1864 // not otherwise used by the function. This guaranteees it is possible
1865 // to scavenge a register to hold the address of a stack slot. On Thumb1,
1866 // the register must be a valid operand to tSTRi, i.e. r4-r7. For other
1867 // subtargets, this is any GPR, i.e. r4-r11 or lr.
1869 // If we don't insert a spill, we instead allocate an emergency spill
1870 // slot, which can be used by scavenging to spill an arbitrary register.
1872 // We currently don't try to figure out whether any specific instruction
1873 // requires scavening an additional register.
1874 bool ExtraCSSpill = false;
1876 if (AFI->isThumb1OnlyFunction()) {
1877 // For Thumb1-only targets, we need some low registers when we save and
1878 // restore the high registers (which aren't allocatable, but could be
1879 // used by inline assembly) because the push/pop instructions can not
1880 // access high registers. If necessary, we might need to push more low
1881 // registers to ensure that there is at least one free that can be used
1882 // for the saving & restoring, and preferably we should ensure that as
1883 // many as are needed are available so that fewer push/pop instructions
1884 // are required.
1886 // Low registers which are not currently pushed, but could be (r4-r7).
1887 SmallVector<unsigned, 4> AvailableRegs;
1889 // Unused argument registers (r0-r3) can be clobbered in the prologue for
1890 // free.
1891 int EntryRegDeficit = 0;
1892 for (unsigned Reg : {ARM::R0, ARM::R1, ARM::R2, ARM::R3}) {
1893 if (!MF.getRegInfo().isLiveIn(Reg)) {
1894 --EntryRegDeficit;
1895 LLVM_DEBUG(dbgs()
1896 << printReg(Reg, TRI)
1897 << " is unused argument register, EntryRegDeficit = "
1898 << EntryRegDeficit << "\n");
1902 // Unused return registers can be clobbered in the epilogue for free.
1903 int ExitRegDeficit = AFI->getReturnRegsCount() - 4;
1904 LLVM_DEBUG(dbgs() << AFI->getReturnRegsCount()
1905 << " return regs used, ExitRegDeficit = "
1906 << ExitRegDeficit << "\n");
1908 int RegDeficit = std::max(EntryRegDeficit, ExitRegDeficit);
1909 LLVM_DEBUG(dbgs() << "RegDeficit = " << RegDeficit << "\n");
1911 // r4-r6 can be used in the prologue if they are pushed by the first push
1912 // instruction.
1913 for (unsigned Reg : {ARM::R4, ARM::R5, ARM::R6}) {
1914 if (SavedRegs.test(Reg)) {
1915 --RegDeficit;
1916 LLVM_DEBUG(dbgs() << printReg(Reg, TRI)
1917 << " is saved low register, RegDeficit = "
1918 << RegDeficit << "\n");
1919 } else {
1920 AvailableRegs.push_back(Reg);
1921 LLVM_DEBUG(
1922 dbgs()
1923 << printReg(Reg, TRI)
1924 << " is non-saved low register, adding to AvailableRegs\n");
1928 // r7 can be used if it is not being used as the frame pointer.
1929 if (!HasFP) {
1930 if (SavedRegs.test(ARM::R7)) {
1931 --RegDeficit;
1932 LLVM_DEBUG(dbgs() << "%r7 is saved low register, RegDeficit = "
1933 << RegDeficit << "\n");
1934 } else {
1935 AvailableRegs.push_back(ARM::R7);
1936 LLVM_DEBUG(
1937 dbgs()
1938 << "%r7 is non-saved low register, adding to AvailableRegs\n");
1942 // Each of r8-r11 needs to be copied to a low register, then pushed.
1943 for (unsigned Reg : {ARM::R8, ARM::R9, ARM::R10, ARM::R11}) {
1944 if (SavedRegs.test(Reg)) {
1945 ++RegDeficit;
1946 LLVM_DEBUG(dbgs() << printReg(Reg, TRI)
1947 << " is saved high register, RegDeficit = "
1948 << RegDeficit << "\n");
1952 // LR can only be used by PUSH, not POP, and can't be used at all if the
1953 // llvm.returnaddress intrinsic is used. This is only worth doing if we
1954 // are more limited at function entry than exit.
1955 if ((EntryRegDeficit > ExitRegDeficit) &&
1956 !(MF.getRegInfo().isLiveIn(ARM::LR) &&
1957 MF.getFrameInfo().isReturnAddressTaken())) {
1958 if (SavedRegs.test(ARM::LR)) {
1959 --RegDeficit;
1960 LLVM_DEBUG(dbgs() << "%lr is saved register, RegDeficit = "
1961 << RegDeficit << "\n");
1962 } else {
1963 AvailableRegs.push_back(ARM::LR);
1964 LLVM_DEBUG(dbgs() << "%lr is not saved, adding to AvailableRegs\n");
1968 // If there are more high registers that need pushing than low registers
1969 // available, push some more low registers so that we can use fewer push
1970 // instructions. This might not reduce RegDeficit all the way to zero,
1971 // because we can only guarantee that r4-r6 are available, but r8-r11 may
1972 // need saving.
1973 LLVM_DEBUG(dbgs() << "Final RegDeficit = " << RegDeficit << "\n");
1974 for (; RegDeficit > 0 && !AvailableRegs.empty(); --RegDeficit) {
1975 unsigned Reg = AvailableRegs.pop_back_val();
1976 LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
1977 << " to make up reg deficit\n");
1978 SavedRegs.set(Reg);
1979 NumGPRSpills++;
1980 CS1Spilled = true;
1981 assert(!MRI.isReserved(Reg) && "Should not be reserved");
1982 if (Reg != ARM::LR && !MRI.isPhysRegUsed(Reg))
1983 ExtraCSSpill = true;
1984 UnspilledCS1GPRs.erase(llvm::find(UnspilledCS1GPRs, Reg));
1985 if (Reg == ARM::LR)
1986 LRSpilled = true;
1988 LLVM_DEBUG(dbgs() << "After adding spills, RegDeficit = " << RegDeficit
1989 << "\n");
1992 // Avoid spilling LR in Thumb1 if there's a tail call: it's expensive to
1993 // restore LR in that case.
1994 bool ExpensiveLRRestore = AFI->isThumb1OnlyFunction() && MFI.hasTailCall();
1996 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1997 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1998 if (!LRSpilled && CS1Spilled && !ExpensiveLRRestore) {
1999 SavedRegs.set(ARM::LR);
2000 NumGPRSpills++;
2001 SmallVectorImpl<unsigned>::iterator LRPos;
2002 LRPos = llvm::find(UnspilledCS1GPRs, (unsigned)ARM::LR);
2003 if (LRPos != UnspilledCS1GPRs.end())
2004 UnspilledCS1GPRs.erase(LRPos);
2006 ForceLRSpill = false;
2007 if (!MRI.isReserved(ARM::LR) && !MRI.isPhysRegUsed(ARM::LR) &&
2008 !AFI->isThumb1OnlyFunction())
2009 ExtraCSSpill = true;
2012 // If stack and double are 8-byte aligned and we are spilling an odd number
2013 // of GPRs, spill one extra callee save GPR so we won't have to pad between
2014 // the integer and double callee save areas.
2015 LLVM_DEBUG(dbgs() << "NumGPRSpills = " << NumGPRSpills << "\n");
2016 unsigned TargetAlign = getStackAlignment();
2017 if (TargetAlign >= 8 && (NumGPRSpills & 1)) {
2018 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
2019 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
2020 unsigned Reg = UnspilledCS1GPRs[i];
2021 // Don't spill high register if the function is thumb. In the case of
2022 // Windows on ARM, accept R11 (frame pointer)
2023 if (!AFI->isThumbFunction() ||
2024 (STI.isTargetWindows() && Reg == ARM::R11) ||
2025 isARMLowRegister(Reg) ||
2026 (Reg == ARM::LR && !ExpensiveLRRestore)) {
2027 SavedRegs.set(Reg);
2028 LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
2029 << " to make up alignment\n");
2030 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg) &&
2031 !(Reg == ARM::LR && AFI->isThumb1OnlyFunction()))
2032 ExtraCSSpill = true;
2033 break;
2036 } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) {
2037 unsigned Reg = UnspilledCS2GPRs.front();
2038 SavedRegs.set(Reg);
2039 LLVM_DEBUG(dbgs() << "Spilling " << printReg(Reg, TRI)
2040 << " to make up alignment\n");
2041 if (!MRI.isReserved(Reg) && !MRI.isPhysRegUsed(Reg))
2042 ExtraCSSpill = true;
2046 // Estimate if we might need to scavenge a register at some point in order
2047 // to materialize a stack offset. If so, either spill one additional
2048 // callee-saved register or reserve a special spill slot to facilitate
2049 // register scavenging. Thumb1 needs a spill slot for stack pointer
2050 // adjustments also, even when the frame itself is small.
2051 if (BigFrameOffsets && !ExtraCSSpill) {
2052 // If any non-reserved CS register isn't spilled, just spill one or two
2053 // extra. That should take care of it!
2054 unsigned NumExtras = TargetAlign / 4;
2055 SmallVector<unsigned, 2> Extras;
2056 while (NumExtras && !UnspilledCS1GPRs.empty()) {
2057 unsigned Reg = UnspilledCS1GPRs.back();
2058 UnspilledCS1GPRs.pop_back();
2059 if (!MRI.isReserved(Reg) &&
2060 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg))) {
2061 Extras.push_back(Reg);
2062 NumExtras--;
2065 // For non-Thumb1 functions, also check for hi-reg CS registers
2066 if (!AFI->isThumb1OnlyFunction()) {
2067 while (NumExtras && !UnspilledCS2GPRs.empty()) {
2068 unsigned Reg = UnspilledCS2GPRs.back();
2069 UnspilledCS2GPRs.pop_back();
2070 if (!MRI.isReserved(Reg)) {
2071 Extras.push_back(Reg);
2072 NumExtras--;
2076 if (NumExtras == 0) {
2077 for (unsigned Reg : Extras) {
2078 SavedRegs.set(Reg);
2079 if (!MRI.isPhysRegUsed(Reg))
2080 ExtraCSSpill = true;
2083 if (!ExtraCSSpill) {
2084 // Reserve a slot closest to SP or frame pointer.
2085 assert(RS && "Register scavenging not provided");
2086 LLVM_DEBUG(dbgs() << "Reserving emergency spill slot\n");
2087 const TargetRegisterClass &RC = ARM::GPRRegClass;
2088 unsigned Size = TRI->getSpillSize(RC);
2089 unsigned Align = TRI->getSpillAlignment(RC);
2090 RS->addScavengingFrameIndex(MFI.CreateStackObject(Size, Align, false));
2095 if (ForceLRSpill) {
2096 SavedRegs.set(ARM::LR);
2097 AFI->setLRIsSpilledForFarJump(true);
2099 AFI->setLRIsSpilled(SavedRegs.test(ARM::LR));
2101 // If we have the "returned" parameter attribute which guarantees that we
2102 // return the value which was passed in r0 unmodified (e.g. C++ 'structors),
2103 // record that fact for IPRA.
2104 if (AFI->getPreservesR0())
2105 SavedRegs.set(ARM::R0);
2108 MachineBasicBlock::iterator ARMFrameLowering::eliminateCallFramePseudoInstr(
2109 MachineFunction &MF, MachineBasicBlock &MBB,
2110 MachineBasicBlock::iterator I) const {
2111 const ARMBaseInstrInfo &TII =
2112 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
2113 if (!hasReservedCallFrame(MF)) {
2114 // If we have alloca, convert as follows:
2115 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
2116 // ADJCALLSTACKUP -> add, sp, sp, amount
2117 MachineInstr &Old = *I;
2118 DebugLoc dl = Old.getDebugLoc();
2119 unsigned Amount = TII.getFrameSize(Old);
2120 if (Amount != 0) {
2121 // We need to keep the stack aligned properly. To do this, we round the
2122 // amount of space needed for the outgoing arguments up to the next
2123 // alignment boundary.
2124 Amount = alignSPAdjust(Amount);
2126 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2127 assert(!AFI->isThumb1OnlyFunction() &&
2128 "This eliminateCallFramePseudoInstr does not support Thumb1!");
2129 bool isARM = !AFI->isThumbFunction();
2131 // Replace the pseudo instruction with a new instruction...
2132 unsigned Opc = Old.getOpcode();
2133 int PIdx = Old.findFirstPredOperandIdx();
2134 ARMCC::CondCodes Pred =
2135 (PIdx == -1) ? ARMCC::AL
2136 : (ARMCC::CondCodes)Old.getOperand(PIdx).getImm();
2137 unsigned PredReg = TII.getFramePred(Old);
2138 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
2139 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags,
2140 Pred, PredReg);
2141 } else {
2142 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
2143 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags,
2144 Pred, PredReg);
2148 return MBB.erase(I);
2151 /// Get the minimum constant for ARM that is greater than or equal to the
2152 /// argument. In ARM, constants can have any value that can be produced by
2153 /// rotating an 8-bit value to the right by an even number of bits within a
2154 /// 32-bit word.
2155 static uint32_t alignToARMConstant(uint32_t Value) {
2156 unsigned Shifted = 0;
2158 if (Value == 0)
2159 return 0;
2161 while (!(Value & 0xC0000000)) {
2162 Value = Value << 2;
2163 Shifted += 2;
2166 bool Carry = (Value & 0x00FFFFFF);
2167 Value = ((Value & 0xFF000000) >> 24) + Carry;
2169 if (Value & 0x0000100)
2170 Value = Value & 0x000001FC;
2172 if (Shifted > 24)
2173 Value = Value >> (Shifted - 24);
2174 else
2175 Value = Value << (24 - Shifted);
2177 return Value;
2180 // The stack limit in the TCB is set to this many bytes above the actual
2181 // stack limit.
2182 static const uint64_t kSplitStackAvailable = 256;
2184 // Adjust the function prologue to enable split stacks. This currently only
2185 // supports android and linux.
2187 // The ABI of the segmented stack prologue is a little arbitrarily chosen, but
2188 // must be well defined in order to allow for consistent implementations of the
2189 // __morestack helper function. The ABI is also not a normal ABI in that it
2190 // doesn't follow the normal calling conventions because this allows the
2191 // prologue of each function to be optimized further.
2193 // Currently, the ABI looks like (when calling __morestack)
2195 // * r4 holds the minimum stack size requested for this function call
2196 // * r5 holds the stack size of the arguments to the function
2197 // * the beginning of the function is 3 instructions after the call to
2198 // __morestack
2200 // Implementations of __morestack should use r4 to allocate a new stack, r5 to
2201 // place the arguments on to the new stack, and the 3-instruction knowledge to
2202 // jump directly to the body of the function when working on the new stack.
2204 // An old (and possibly no longer compatible) implementation of __morestack for
2205 // ARM can be found at [1].
2207 // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S
2208 void ARMFrameLowering::adjustForSegmentedStacks(
2209 MachineFunction &MF, MachineBasicBlock &PrologueMBB) const {
2210 unsigned Opcode;
2211 unsigned CFIIndex;
2212 const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>();
2213 bool Thumb = ST->isThumb();
2215 // Sadly, this currently doesn't support varargs, platforms other than
2216 // android/linux. Note that thumb1/thumb2 are support for android/linux.
2217 if (MF.getFunction().isVarArg())
2218 report_fatal_error("Segmented stacks do not support vararg functions.");
2219 if (!ST->isTargetAndroid() && !ST->isTargetLinux())
2220 report_fatal_error("Segmented stacks not supported on this platform.");
2222 MachineFrameInfo &MFI = MF.getFrameInfo();
2223 MachineModuleInfo &MMI = MF.getMMI();
2224 MCContext &Context = MMI.getContext();
2225 const MCRegisterInfo *MRI = Context.getRegisterInfo();
2226 const ARMBaseInstrInfo &TII =
2227 *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
2228 ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>();
2229 DebugLoc DL;
2231 uint64_t StackSize = MFI.getStackSize();
2233 // Do not generate a prologue for leaf functions with a stack of size zero.
2234 // For non-leaf functions we have to allow for the possibility that the
2235 // callis to a non-split function, as in PR37807. This function could also
2236 // take the address of a non-split function. When the linker tries to adjust
2237 // its non-existent prologue, it would fail with an error. Mark the object
2238 // file so that such failures are not errors. See this Go language bug-report
2239 // https://go-review.googlesource.com/c/go/+/148819/
2240 if (StackSize == 0 && !MFI.hasTailCall()) {
2241 MF.getMMI().setHasNosplitStack(true);
2242 return;
2245 // Use R4 and R5 as scratch registers.
2246 // We save R4 and R5 before use and restore them before leaving the function.
2247 unsigned ScratchReg0 = ARM::R4;
2248 unsigned ScratchReg1 = ARM::R5;
2249 uint64_t AlignedStackSize;
2251 MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock();
2252 MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock();
2253 MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock();
2254 MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock();
2255 MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock();
2257 // Grab everything that reaches PrologueMBB to update there liveness as well.
2258 SmallPtrSet<MachineBasicBlock *, 8> BeforePrologueRegion;
2259 SmallVector<MachineBasicBlock *, 2> WalkList;
2260 WalkList.push_back(&PrologueMBB);
2262 do {
2263 MachineBasicBlock *CurMBB = WalkList.pop_back_val();
2264 for (MachineBasicBlock *PredBB : CurMBB->predecessors()) {
2265 if (BeforePrologueRegion.insert(PredBB).second)
2266 WalkList.push_back(PredBB);
2268 } while (!WalkList.empty());
2270 // The order in that list is important.
2271 // The blocks will all be inserted before PrologueMBB using that order.
2272 // Therefore the block that should appear first in the CFG should appear
2273 // first in the list.
2274 MachineBasicBlock *AddedBlocks[] = {PrevStackMBB, McrMBB, GetMBB, AllocMBB,
2275 PostStackMBB};
2277 for (MachineBasicBlock *B : AddedBlocks)
2278 BeforePrologueRegion.insert(B);
2280 for (const auto &LI : PrologueMBB.liveins()) {
2281 for (MachineBasicBlock *PredBB : BeforePrologueRegion)
2282 PredBB->addLiveIn(LI);
2285 // Remove the newly added blocks from the list, since we know
2286 // we do not have to do the following updates for them.
2287 for (MachineBasicBlock *B : AddedBlocks) {
2288 BeforePrologueRegion.erase(B);
2289 MF.insert(PrologueMBB.getIterator(), B);
2292 for (MachineBasicBlock *MBB : BeforePrologueRegion) {
2293 // Make sure the LiveIns are still sorted and unique.
2294 MBB->sortUniqueLiveIns();
2295 // Replace the edges to PrologueMBB by edges to the sequences
2296 // we are about to add.
2297 MBB->ReplaceUsesOfBlockWith(&PrologueMBB, AddedBlocks[0]);
2300 // The required stack size that is aligned to ARM constant criterion.
2301 AlignedStackSize = alignToARMConstant(StackSize);
2303 // When the frame size is less than 256 we just compare the stack
2304 // boundary directly to the value of the stack pointer, per gcc.
2305 bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable;
2307 // We will use two of the callee save registers as scratch registers so we
2308 // need to save those registers onto the stack.
2309 // We will use SR0 to hold stack limit and SR1 to hold the stack size
2310 // requested and arguments for __morestack().
2311 // SR0: Scratch Register #0
2312 // SR1: Scratch Register #1
2313 // push {SR0, SR1}
2314 if (Thumb) {
2315 BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))
2316 .add(predOps(ARMCC::AL))
2317 .addReg(ScratchReg0)
2318 .addReg(ScratchReg1);
2319 } else {
2320 BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD))
2321 .addReg(ARM::SP, RegState::Define)
2322 .addReg(ARM::SP)
2323 .add(predOps(ARMCC::AL))
2324 .addReg(ScratchReg0)
2325 .addReg(ScratchReg1);
2328 // Emit the relevant DWARF information about the change in stack pointer as
2329 // well as where to find both r4 and r5 (the callee-save registers)
2330 CFIIndex =
2331 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8));
2332 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2333 .addCFIIndex(CFIIndex);
2334 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
2335 nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4));
2336 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2337 .addCFIIndex(CFIIndex);
2338 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
2339 nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8));
2340 BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2341 .addCFIIndex(CFIIndex);
2343 // mov SR1, sp
2344 if (Thumb) {
2345 BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1)
2346 .addReg(ARM::SP)
2347 .add(predOps(ARMCC::AL));
2348 } else if (CompareStackPointer) {
2349 BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1)
2350 .addReg(ARM::SP)
2351 .add(predOps(ARMCC::AL))
2352 .add(condCodeOp());
2355 // sub SR1, sp, #StackSize
2356 if (!CompareStackPointer && Thumb) {
2357 BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)
2358 .add(condCodeOp())
2359 .addReg(ScratchReg1)
2360 .addImm(AlignedStackSize)
2361 .add(predOps(ARMCC::AL));
2362 } else if (!CompareStackPointer) {
2363 BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1)
2364 .addReg(ARM::SP)
2365 .addImm(AlignedStackSize)
2366 .add(predOps(ARMCC::AL))
2367 .add(condCodeOp());
2370 if (Thumb && ST->isThumb1Only()) {
2371 unsigned PCLabelId = ARMFI->createPICLabelUId();
2372 ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create(
2373 MF.getFunction().getContext(), "__STACK_LIMIT", PCLabelId, 0);
2374 MachineConstantPool *MCP = MF.getConstantPool();
2375 unsigned CPI = MCP->getConstantPoolIndex(NewCPV, 4);
2377 // ldr SR0, [pc, offset(STACK_LIMIT)]
2378 BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0)
2379 .addConstantPoolIndex(CPI)
2380 .add(predOps(ARMCC::AL));
2382 // ldr SR0, [SR0]
2383 BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0)
2384 .addReg(ScratchReg0)
2385 .addImm(0)
2386 .add(predOps(ARMCC::AL));
2387 } else {
2388 // Get TLS base address from the coprocessor
2389 // mrc p15, #0, SR0, c13, c0, #3
2390 BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0)
2391 .addImm(15)
2392 .addImm(0)
2393 .addImm(13)
2394 .addImm(0)
2395 .addImm(3)
2396 .add(predOps(ARMCC::AL));
2398 // Use the last tls slot on android and a private field of the TCP on linux.
2399 assert(ST->isTargetAndroid() || ST->isTargetLinux());
2400 unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1;
2402 // Get the stack limit from the right offset
2403 // ldr SR0, [sr0, #4 * TlsOffset]
2404 BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0)
2405 .addReg(ScratchReg0)
2406 .addImm(4 * TlsOffset)
2407 .add(predOps(ARMCC::AL));
2410 // Compare stack limit with stack size requested.
2411 // cmp SR0, SR1
2412 Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr;
2413 BuildMI(GetMBB, DL, TII.get(Opcode))
2414 .addReg(ScratchReg0)
2415 .addReg(ScratchReg1)
2416 .add(predOps(ARMCC::AL));
2418 // This jump is taken if StackLimit < SP - stack required.
2419 Opcode = Thumb ? ARM::tBcc : ARM::Bcc;
2420 BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB)
2421 .addImm(ARMCC::LO)
2422 .addReg(ARM::CPSR);
2425 // Calling __morestack(StackSize, Size of stack arguments).
2426 // __morestack knows that the stack size requested is in SR0(r4)
2427 // and amount size of stack arguments is in SR1(r5).
2429 // Pass first argument for the __morestack by Scratch Register #0.
2430 // The amount size of stack required
2431 if (Thumb) {
2432 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg0)
2433 .add(condCodeOp())
2434 .addImm(AlignedStackSize)
2435 .add(predOps(ARMCC::AL));
2436 } else {
2437 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0)
2438 .addImm(AlignedStackSize)
2439 .add(predOps(ARMCC::AL))
2440 .add(condCodeOp());
2442 // Pass second argument for the __morestack by Scratch Register #1.
2443 // The amount size of stack consumed to save function arguments.
2444 if (Thumb) {
2445 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)
2446 .add(condCodeOp())
2447 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2448 .add(predOps(ARMCC::AL));
2449 } else {
2450 BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1)
2451 .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))
2452 .add(predOps(ARMCC::AL))
2453 .add(condCodeOp());
2456 // push {lr} - Save return address of this function.
2457 if (Thumb) {
2458 BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))
2459 .add(predOps(ARMCC::AL))
2460 .addReg(ARM::LR);
2461 } else {
2462 BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD))
2463 .addReg(ARM::SP, RegState::Define)
2464 .addReg(ARM::SP)
2465 .add(predOps(ARMCC::AL))
2466 .addReg(ARM::LR);
2469 // Emit the DWARF info about the change in stack as well as where to find the
2470 // previous link register
2471 CFIIndex =
2472 MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12));
2473 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2474 .addCFIIndex(CFIIndex);
2475 CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
2476 nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12));
2477 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2478 .addCFIIndex(CFIIndex);
2480 // Call __morestack().
2481 if (Thumb) {
2482 BuildMI(AllocMBB, DL, TII.get(ARM::tBL))
2483 .add(predOps(ARMCC::AL))
2484 .addExternalSymbol("__morestack");
2485 } else {
2486 BuildMI(AllocMBB, DL, TII.get(ARM::BL))
2487 .addExternalSymbol("__morestack");
2490 // pop {lr} - Restore return address of this original function.
2491 if (Thumb) {
2492 if (ST->isThumb1Only()) {
2493 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2494 .add(predOps(ARMCC::AL))
2495 .addReg(ScratchReg0);
2496 BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR)
2497 .addReg(ScratchReg0)
2498 .add(predOps(ARMCC::AL));
2499 } else {
2500 BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST))
2501 .addReg(ARM::LR, RegState::Define)
2502 .addReg(ARM::SP, RegState::Define)
2503 .addReg(ARM::SP)
2504 .addImm(4)
2505 .add(predOps(ARMCC::AL));
2507 } else {
2508 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2509 .addReg(ARM::SP, RegState::Define)
2510 .addReg(ARM::SP)
2511 .add(predOps(ARMCC::AL))
2512 .addReg(ARM::LR);
2515 // Restore SR0 and SR1 in case of __morestack() was called.
2516 // __morestack() will skip PostStackMBB block so we need to restore
2517 // scratch registers from here.
2518 // pop {SR0, SR1}
2519 if (Thumb) {
2520 BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))
2521 .add(predOps(ARMCC::AL))
2522 .addReg(ScratchReg0)
2523 .addReg(ScratchReg1);
2524 } else {
2525 BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD))
2526 .addReg(ARM::SP, RegState::Define)
2527 .addReg(ARM::SP)
2528 .add(predOps(ARMCC::AL))
2529 .addReg(ScratchReg0)
2530 .addReg(ScratchReg1);
2533 // Update the CFA offset now that we've popped
2534 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2535 BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2536 .addCFIIndex(CFIIndex);
2538 // Return from this function.
2539 BuildMI(AllocMBB, DL, TII.get(ST->getReturnOpcode())).add(predOps(ARMCC::AL));
2541 // Restore SR0 and SR1 in case of __morestack() was not called.
2542 // pop {SR0, SR1}
2543 if (Thumb) {
2544 BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))
2545 .add(predOps(ARMCC::AL))
2546 .addReg(ScratchReg0)
2547 .addReg(ScratchReg1);
2548 } else {
2549 BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD))
2550 .addReg(ARM::SP, RegState::Define)
2551 .addReg(ARM::SP)
2552 .add(predOps(ARMCC::AL))
2553 .addReg(ScratchReg0)
2554 .addReg(ScratchReg1);
2557 // Update the CFA offset now that we've popped
2558 CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0));
2559 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2560 .addCFIIndex(CFIIndex);
2562 // Tell debuggers that r4 and r5 are now the same as they were in the
2563 // previous function, that they're the "Same Value".
2564 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
2565 nullptr, MRI->getDwarfRegNum(ScratchReg0, true)));
2566 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2567 .addCFIIndex(CFIIndex);
2568 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(
2569 nullptr, MRI->getDwarfRegNum(ScratchReg1, true)));
2570 BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
2571 .addCFIIndex(CFIIndex);
2573 // Organizing MBB lists
2574 PostStackMBB->addSuccessor(&PrologueMBB);
2576 AllocMBB->addSuccessor(PostStackMBB);
2578 GetMBB->addSuccessor(PostStackMBB);
2579 GetMBB->addSuccessor(AllocMBB);
2581 McrMBB->addSuccessor(GetMBB);
2583 PrevStackMBB->addSuccessor(McrMBB);
2585 #ifdef EXPENSIVE_CHECKS
2586 MF.verify();
2587 #endif