1dff0c46cSDimitry Andric //===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===// 22754fe60SDimitry Andric // 32754fe60SDimitry Andric // The LLVM Compiler Infrastructure 42754fe60SDimitry Andric // 52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source 62754fe60SDimitry Andric // License. See LICENSE.TXT for details. 72754fe60SDimitry Andric // 82754fe60SDimitry Andric //===----------------------------------------------------------------------===// 92754fe60SDimitry Andric // 102754fe60SDimitry Andric // This file contains the ARM implementation of TargetFrameLowering class. 112754fe60SDimitry Andric // 122754fe60SDimitry Andric //===----------------------------------------------------------------------===// 132754fe60SDimitry Andric 142754fe60SDimitry Andric #include "ARMFrameLowering.h" 152754fe60SDimitry Andric #include "ARMBaseInstrInfo.h" 162754fe60SDimitry Andric #include "ARMBaseRegisterInfo.h" 1791bc56edSDimitry Andric #include "ARMConstantPoolValue.h" 182754fe60SDimitry Andric #include "ARMMachineFunctionInfo.h" 196122f3e6SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h" 202754fe60SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 212754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 222754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 2391bc56edSDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 242754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 252754fe60SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 26139f7f9bSDimitry Andric #include "llvm/IR/CallingConv.h" 27139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 2891bc56edSDimitry Andric #include "llvm/MC/MCContext.h" 29dff0c46cSDimitry Andric #include "llvm/Support/CommandLine.h" 30139f7f9bSDimitry Andric #include "llvm/Target/TargetOptions.h" 312754fe60SDimitry Andric 322754fe60SDimitry Andric using namespace llvm; 332754fe60SDimitry Andric 34dff0c46cSDimitry Andric static cl::opt<bool> 35dff0c46cSDimitry Andric SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true), 36dff0c46cSDimitry Andric cl::desc("Align ARM NEON spills in prolog and epilog")); 37dff0c46cSDimitry Andric 38dff0c46cSDimitry Andric static MachineBasicBlock::iterator 39dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 40dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs); 41dff0c46cSDimitry Andric 4291bc56edSDimitry Andric ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti) 4391bc56edSDimitry Andric : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4), 4491bc56edSDimitry Andric STI(sti) {} 4591bc56edSDimitry Andric 462754fe60SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 472754fe60SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 482754fe60SDimitry Andric /// or if frame pointer elimination is disabled. 492754fe60SDimitry Andric bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 5039d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 512754fe60SDimitry Andric 52dff0c46cSDimitry Andric // iOS requires FP not to be clobbered for backtracing purpose. 53dff0c46cSDimitry Andric if (STI.isTargetIOS()) 542754fe60SDimitry Andric return true; 552754fe60SDimitry Andric 562754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 572754fe60SDimitry Andric // Always eliminate non-leaf frame pointers. 58dff0c46cSDimitry Andric return ((MF.getTarget().Options.DisableFramePointerElim(MF) && 59dff0c46cSDimitry Andric MFI->hasCalls()) || 602754fe60SDimitry Andric RegInfo->needsStackRealignment(MF) || 612754fe60SDimitry Andric MFI->hasVarSizedObjects() || 622754fe60SDimitry Andric MFI->isFrameAddressTaken()); 632754fe60SDimitry Andric } 642754fe60SDimitry Andric 652754fe60SDimitry Andric /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 662754fe60SDimitry Andric /// not required, we reserve argument space for call sites in the function 672754fe60SDimitry Andric /// immediately on entry to the current function. This eliminates the need for 682754fe60SDimitry Andric /// add/sub sp brackets around call sites. Returns true if the call frame is 692754fe60SDimitry Andric /// included as part of the stack frame. 702754fe60SDimitry Andric bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 712754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 722754fe60SDimitry Andric unsigned CFSize = FFI->getMaxCallFrameSize(); 732754fe60SDimitry Andric // It's not always a good idea to include the call frame as part of the 742754fe60SDimitry Andric // stack frame. ARM (especially Thumb) has small immediate offset to 752754fe60SDimitry Andric // address the stack frame. So a large call frame can cause poor codegen 762754fe60SDimitry Andric // and may even makes it impossible to scavenge a register. 772754fe60SDimitry Andric if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 782754fe60SDimitry Andric return false; 792754fe60SDimitry Andric 802754fe60SDimitry Andric return !MF.getFrameInfo()->hasVarSizedObjects(); 812754fe60SDimitry Andric } 822754fe60SDimitry Andric 832754fe60SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 842754fe60SDimitry Andric /// call frame pseudos can be simplified. Unlike most targets, having a FP 852754fe60SDimitry Andric /// is not sufficient here since we still may reference some objects via SP 862754fe60SDimitry Andric /// even when FP is available in Thumb2 mode. 872754fe60SDimitry Andric bool 882754fe60SDimitry Andric ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 892754fe60SDimitry Andric return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 902754fe60SDimitry Andric } 912754fe60SDimitry Andric 922754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 932754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 9491bc56edSDimitry Andric const MCPhysReg *CSRegs) { 952754fe60SDimitry Andric // Integer spill area is handled with "pop". 96f785676fSDimitry Andric if (isPopOpcode(MI->getOpcode())) { 972754fe60SDimitry Andric // The first two operands are predicates. The last two are 982754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 992754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 1002754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 1012754fe60SDimitry Andric return false; 1022754fe60SDimitry Andric return true; 1032754fe60SDimitry Andric } 1046122f3e6SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST_IMM || 1056122f3e6SDimitry Andric MI->getOpcode() == ARM::LDR_POST_REG || 1062754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 1072754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 1082754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1092754fe60SDimitry Andric return true; 1102754fe60SDimitry Andric 1112754fe60SDimitry Andric return false; 1122754fe60SDimitry Andric } 1132754fe60SDimitry Andric 114f785676fSDimitry Andric static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB, 115f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 116f785676fSDimitry Andric const ARMBaseInstrInfo &TII, unsigned DestReg, 117f785676fSDimitry Andric unsigned SrcReg, int NumBytes, 118f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 119f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 120f785676fSDimitry Andric unsigned PredReg = 0) { 1212754fe60SDimitry Andric if (isARM) 122f785676fSDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 123139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1242754fe60SDimitry Andric else 125f785676fSDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 126139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1272754fe60SDimitry Andric } 1282754fe60SDimitry Andric 129f785676fSDimitry Andric static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB, 130f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 131f785676fSDimitry Andric const ARMBaseInstrInfo &TII, int NumBytes, 132f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 133f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 134f785676fSDimitry Andric unsigned PredReg = 0) { 135f785676fSDimitry Andric emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes, 136f785676fSDimitry Andric MIFlags, Pred, PredReg); 137f785676fSDimitry Andric } 138f785676fSDimitry Andric 13991bc56edSDimitry Andric static int sizeOfSPAdjustment(const MachineInstr *MI) { 14039d628a0SDimitry Andric int RegSize; 14139d628a0SDimitry Andric switch (MI->getOpcode()) { 14239d628a0SDimitry Andric case ARM::VSTMDDB_UPD: 14339d628a0SDimitry Andric RegSize = 8; 14439d628a0SDimitry Andric break; 14539d628a0SDimitry Andric case ARM::STMDB_UPD: 14639d628a0SDimitry Andric case ARM::t2STMDB_UPD: 14739d628a0SDimitry Andric RegSize = 4; 14839d628a0SDimitry Andric break; 14939d628a0SDimitry Andric case ARM::t2STR_PRE: 15039d628a0SDimitry Andric case ARM::STR_PRE_IMM: 15139d628a0SDimitry Andric return 4; 15239d628a0SDimitry Andric default: 15339d628a0SDimitry Andric llvm_unreachable("Unknown push or pop like instruction"); 15439d628a0SDimitry Andric } 15539d628a0SDimitry Andric 15691bc56edSDimitry Andric int count = 0; 15791bc56edSDimitry Andric // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ 15891bc56edSDimitry Andric // pred) so the list starts at 4. 15991bc56edSDimitry Andric for (int i = MI->getNumOperands() - 1; i >= 4; --i) 16039d628a0SDimitry Andric count += RegSize; 16191bc56edSDimitry Andric return count; 16291bc56edSDimitry Andric } 16391bc56edSDimitry Andric 16491bc56edSDimitry Andric static bool WindowsRequiresStackProbe(const MachineFunction &MF, 16591bc56edSDimitry Andric size_t StackSizeInBytes) { 16691bc56edSDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 16791bc56edSDimitry Andric if (MFI->getStackProtectorIndex() > 0) 16891bc56edSDimitry Andric return StackSizeInBytes >= 4080; 16991bc56edSDimitry Andric return StackSizeInBytes >= 4096; 17091bc56edSDimitry Andric } 17191bc56edSDimitry Andric 17239d628a0SDimitry Andric namespace { 17339d628a0SDimitry Andric struct StackAdjustingInsts { 17439d628a0SDimitry Andric struct InstInfo { 17539d628a0SDimitry Andric MachineBasicBlock::iterator I; 17639d628a0SDimitry Andric unsigned SPAdjust; 17739d628a0SDimitry Andric bool BeforeFPSet; 17839d628a0SDimitry Andric }; 17939d628a0SDimitry Andric 18039d628a0SDimitry Andric SmallVector<InstInfo, 4> Insts; 18139d628a0SDimitry Andric 18239d628a0SDimitry Andric void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust, 18339d628a0SDimitry Andric bool BeforeFPSet = false) { 18439d628a0SDimitry Andric InstInfo Info = {I, SPAdjust, BeforeFPSet}; 18539d628a0SDimitry Andric Insts.push_back(Info); 18639d628a0SDimitry Andric } 18739d628a0SDimitry Andric 18839d628a0SDimitry Andric void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) { 18939d628a0SDimitry Andric auto Info = std::find_if(Insts.begin(), Insts.end(), 19039d628a0SDimitry Andric [&](InstInfo &Info) { return Info.I == I; }); 19139d628a0SDimitry Andric assert(Info != Insts.end() && "invalid sp adjusting instruction"); 19239d628a0SDimitry Andric Info->SPAdjust += ExtraBytes; 19339d628a0SDimitry Andric } 19439d628a0SDimitry Andric 19539d628a0SDimitry Andric void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB, 19639d628a0SDimitry Andric DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) { 19739d628a0SDimitry Andric unsigned CFAOffset = 0; 19839d628a0SDimitry Andric for (auto &Info : Insts) { 19939d628a0SDimitry Andric if (HasFP && !Info.BeforeFPSet) 20039d628a0SDimitry Andric return; 20139d628a0SDimitry Andric 20239d628a0SDimitry Andric CFAOffset -= Info.SPAdjust; 20339d628a0SDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 20439d628a0SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 20539d628a0SDimitry Andric BuildMI(MBB, std::next(Info.I), dl, 20639d628a0SDimitry Andric TII.get(TargetOpcode::CFI_INSTRUCTION)) 20739d628a0SDimitry Andric .addCFIIndex(CFIIndex) 20839d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 20939d628a0SDimitry Andric } 21039d628a0SDimitry Andric } 21139d628a0SDimitry Andric }; 21239d628a0SDimitry Andric } 21339d628a0SDimitry Andric 21439d628a0SDimitry Andric /// Emit an instruction sequence that will align the address in 21539d628a0SDimitry Andric /// register Reg by zero-ing out the lower bits. For versions of the 21639d628a0SDimitry Andric /// architecture that support Neon, this must be done in a single 21739d628a0SDimitry Andric /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a 21839d628a0SDimitry Andric /// single instruction. That function only gets called when optimizing 21939d628a0SDimitry Andric /// spilling of D registers on a core with the Neon instruction set 22039d628a0SDimitry Andric /// present. 22139d628a0SDimitry Andric static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI, 22239d628a0SDimitry Andric const TargetInstrInfo &TII, 22339d628a0SDimitry Andric MachineBasicBlock &MBB, 22439d628a0SDimitry Andric MachineBasicBlock::iterator MBBI, 22539d628a0SDimitry Andric DebugLoc DL, const unsigned Reg, 22639d628a0SDimitry Andric const unsigned Alignment, 22739d628a0SDimitry Andric const bool MustBeSingleInstruction) { 22839d628a0SDimitry Andric const ARMSubtarget &AST = MF.getTarget().getSubtarget<ARMSubtarget>(); 22939d628a0SDimitry Andric const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops(); 23039d628a0SDimitry Andric const unsigned AlignMask = Alignment - 1; 23139d628a0SDimitry Andric const unsigned NrBitsToZero = countTrailingZeros(Alignment); 23239d628a0SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported"); 23339d628a0SDimitry Andric if (!AFI->isThumbFunction()) { 23439d628a0SDimitry Andric // if the BFC instruction is available, use that to zero the lower 23539d628a0SDimitry Andric // bits: 23639d628a0SDimitry Andric // bfc Reg, #0, log2(Alignment) 23739d628a0SDimitry Andric // otherwise use BIC, if the mask to zero the required number of bits 23839d628a0SDimitry Andric // can be encoded in the bic immediate field 23939d628a0SDimitry Andric // bic Reg, Reg, Alignment-1 24039d628a0SDimitry Andric // otherwise, emit 24139d628a0SDimitry Andric // lsr Reg, Reg, log2(Alignment) 24239d628a0SDimitry Andric // lsl Reg, Reg, log2(Alignment) 24339d628a0SDimitry Andric if (CanUseBFC) { 24439d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg) 24539d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 24639d628a0SDimitry Andric .addImm(~AlignMask)); 24739d628a0SDimitry Andric } else if (AlignMask <= 255) { 24839d628a0SDimitry Andric AddDefaultCC( 24939d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg) 25039d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 25139d628a0SDimitry Andric .addImm(AlignMask))); 25239d628a0SDimitry Andric } else { 25339d628a0SDimitry Andric assert(!MustBeSingleInstruction && 25439d628a0SDimitry Andric "Shouldn't call emitAligningInstructions demanding a single " 25539d628a0SDimitry Andric "instruction to be emitted for large stack alignment for a target " 25639d628a0SDimitry Andric "without BFC."); 25739d628a0SDimitry Andric AddDefaultCC(AddDefaultPred( 25839d628a0SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) 25939d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 26039d628a0SDimitry Andric .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero)))); 26139d628a0SDimitry Andric AddDefaultCC(AddDefaultPred( 26239d628a0SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) 26339d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 26439d628a0SDimitry Andric .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero)))); 26539d628a0SDimitry Andric } 26639d628a0SDimitry Andric } else { 26739d628a0SDimitry Andric // Since this is only reached for Thumb-2 targets, the BFC instruction 26839d628a0SDimitry Andric // should always be available. 26939d628a0SDimitry Andric assert(CanUseBFC); 27039d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg) 27139d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 27239d628a0SDimitry Andric .addImm(~AlignMask)); 27339d628a0SDimitry Andric } 27439d628a0SDimitry Andric } 27539d628a0SDimitry Andric 2762754fe60SDimitry Andric void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { 2772754fe60SDimitry Andric MachineBasicBlock &MBB = MF.front(); 2782754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 2792754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 2802754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 28191bc56edSDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 28291bc56edSDimitry Andric MCContext &Context = MMI.getContext(); 28391bc56edSDimitry Andric const TargetMachine &TM = MF.getTarget(); 28491bc56edSDimitry Andric const MCRegisterInfo *MRI = Context.getRegisterInfo(); 28539d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 28639d628a0SDimitry Andric TM.getSubtargetImpl()->getRegisterInfo()); 28739d628a0SDimitry Andric const ARMBaseInstrInfo &TII = *static_cast<const ARMBaseInstrInfo *>( 28839d628a0SDimitry Andric TM.getSubtargetImpl()->getInstrInfo()); 2892754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 2902754fe60SDimitry Andric "This emitPrologue does not support Thumb1!"); 2912754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 29239d628a0SDimitry Andric unsigned Align = 29339d628a0SDimitry Andric TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment(); 294f785676fSDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); 2952754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 2962754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 2972754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 2982754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 2992754fe60SDimitry Andric 3002754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 3012754fe60SDimitry Andric // belongs to which callee-save spill areas. 3022754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 3032754fe60SDimitry Andric int FramePtrSpillFI = 0; 304dff0c46cSDimitry Andric int D8SpillFI = 0; 305f8254f43SDimitry Andric 3063861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 3073861d79fSDimitry Andric // prologue/epilogue. 3087ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 3097ae0e2c9SDimitry Andric return; 3107ae0e2c9SDimitry Andric 31139d628a0SDimitry Andric StackAdjustingInsts DefCFAOffsetCandidates; 31239d628a0SDimitry Andric 31391bc56edSDimitry Andric // Allocate the vararg register save area. 31491bc56edSDimitry Andric if (ArgRegsSaveSize) { 315284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, 3163b0f4066SDimitry Andric MachineInstr::FrameSetup); 31739d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true); 31891bc56edSDimitry Andric } 3192754fe60SDimitry Andric 32091bc56edSDimitry Andric if (!AFI->hasStackFrame() && 32191bc56edSDimitry Andric (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) { 32291bc56edSDimitry Andric if (NumBytes - ArgRegsSaveSize != 0) { 32391bc56edSDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize), 3243b0f4066SDimitry Andric MachineInstr::FrameSetup); 32539d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), 32639d628a0SDimitry Andric NumBytes - ArgRegsSaveSize, true); 32791bc56edSDimitry Andric } 3282754fe60SDimitry Andric return; 3292754fe60SDimitry Andric } 3302754fe60SDimitry Andric 33191bc56edSDimitry Andric // Determine spill area sizes. 3322754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 3332754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 3342754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 3352754fe60SDimitry Andric switch (Reg) { 33691bc56edSDimitry Andric case ARM::R8: 33791bc56edSDimitry Andric case ARM::R9: 33891bc56edSDimitry Andric case ARM::R10: 33991bc56edSDimitry Andric case ARM::R11: 34091bc56edSDimitry Andric case ARM::R12: 34191bc56edSDimitry Andric if (STI.isTargetDarwin()) { 34291bc56edSDimitry Andric GPRCS2Size += 4; 34391bc56edSDimitry Andric break; 34491bc56edSDimitry Andric } 34591bc56edSDimitry Andric // fallthrough 346f785676fSDimitry Andric case ARM::R0: 347f785676fSDimitry Andric case ARM::R1: 348f785676fSDimitry Andric case ARM::R2: 349f785676fSDimitry Andric case ARM::R3: 3502754fe60SDimitry Andric case ARM::R4: 3512754fe60SDimitry Andric case ARM::R5: 3522754fe60SDimitry Andric case ARM::R6: 3532754fe60SDimitry Andric case ARM::R7: 3542754fe60SDimitry Andric case ARM::LR: 3552754fe60SDimitry Andric if (Reg == FramePtr) 3562754fe60SDimitry Andric FramePtrSpillFI = FI; 3572754fe60SDimitry Andric GPRCS1Size += 4; 3582754fe60SDimitry Andric break; 3592754fe60SDimitry Andric default: 360dff0c46cSDimitry Andric // This is a DPR. Exclude the aligned DPRCS2 spills. 361dff0c46cSDimitry Andric if (Reg == ARM::D8) 362dff0c46cSDimitry Andric D8SpillFI = FI; 363f785676fSDimitry Andric if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) 3642754fe60SDimitry Andric DPRCSSize += 8; 3652754fe60SDimitry Andric } 3662754fe60SDimitry Andric } 3672754fe60SDimitry Andric 3682754fe60SDimitry Andric // Move past area 1. 36939d628a0SDimitry Andric MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push; 37039d628a0SDimitry Andric if (GPRCS1Size > 0) { 37191bc56edSDimitry Andric GPRCS1Push = LastPush = MBBI++; 37239d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true); 37339d628a0SDimitry Andric } 3742754fe60SDimitry Andric 3752754fe60SDimitry Andric // Determine starting offsets of spill areas. 376f785676fSDimitry Andric bool HasFP = hasFP(MF); 37739d628a0SDimitry Andric unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size; 37839d628a0SDimitry Andric unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size; 37939d628a0SDimitry Andric unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U; 38039d628a0SDimitry Andric unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign; 38139d628a0SDimitry Andric unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize; 382f785676fSDimitry Andric int FramePtrOffsetInPush = 0; 383f785676fSDimitry Andric if (HasFP) { 38439d628a0SDimitry Andric FramePtrOffsetInPush = 38539d628a0SDimitry Andric MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize; 3862754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 3872754fe60SDimitry Andric NumBytes); 388f785676fSDimitry Andric } 3892754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 3902754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 3912754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 3922754fe60SDimitry Andric 393f785676fSDimitry Andric // Move past area 2. 39439d628a0SDimitry Andric if (GPRCS2Size > 0) { 39591bc56edSDimitry Andric GPRCS2Push = LastPush = MBBI++; 39639d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size); 39739d628a0SDimitry Andric } 39839d628a0SDimitry Andric 39939d628a0SDimitry Andric // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our 40039d628a0SDimitry Andric // .cfi_offset operations will reflect that. 40139d628a0SDimitry Andric if (DPRGapSize) { 40239d628a0SDimitry Andric assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs"); 40339d628a0SDimitry Andric if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize)) 40439d628a0SDimitry Andric DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize); 40539d628a0SDimitry Andric else { 40639d628a0SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize, 40739d628a0SDimitry Andric MachineInstr::FrameSetup); 40839d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize); 40939d628a0SDimitry Andric } 41039d628a0SDimitry Andric } 411f785676fSDimitry Andric 4122754fe60SDimitry Andric // Move past area 3. 413dd6029ffSDimitry Andric if (DPRCSSize > 0) { 414dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 415dd6029ffSDimitry Andric // instructions in the prologue. 41639d628a0SDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) { 41739d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI)); 418f785676fSDimitry Andric LastPush = MBBI++; 419dd6029ffSDimitry Andric } 42039d628a0SDimitry Andric } 4212754fe60SDimitry Andric 422dff0c46cSDimitry Andric // Move past the aligned DPRCS2 area. 423dff0c46cSDimitry Andric if (AFI->getNumAlignedDPRCS2Regs() > 0) { 424dff0c46cSDimitry Andric MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); 425dff0c46cSDimitry Andric // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and 426dff0c46cSDimitry Andric // leaves the stack pointer pointing to the DPRCS2 area. 427dff0c46cSDimitry Andric // 428dff0c46cSDimitry Andric // Adjust NumBytes to represent the stack slots below the DPRCS2 area. 429dff0c46cSDimitry Andric NumBytes += MFI->getObjectOffset(D8SpillFI); 430dff0c46cSDimitry Andric } else 4312754fe60SDimitry Andric NumBytes = DPRCSOffset; 432dff0c46cSDimitry Andric 43391bc56edSDimitry Andric if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) { 43491bc56edSDimitry Andric uint32_t NumWords = NumBytes >> 2; 43591bc56edSDimitry Andric 43691bc56edSDimitry Andric if (NumWords < 65536) 43791bc56edSDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4) 43891bc56edSDimitry Andric .addImm(NumWords) 43991bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup)); 44091bc56edSDimitry Andric else 44191bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4) 44291bc56edSDimitry Andric .addImm(NumWords) 44391bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 44491bc56edSDimitry Andric 44591bc56edSDimitry Andric switch (TM.getCodeModel()) { 44691bc56edSDimitry Andric case CodeModel::Small: 44791bc56edSDimitry Andric case CodeModel::Medium: 44891bc56edSDimitry Andric case CodeModel::Default: 44991bc56edSDimitry Andric case CodeModel::Kernel: 45091bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL)) 45191bc56edSDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0) 45291bc56edSDimitry Andric .addExternalSymbol("__chkstk") 45391bc56edSDimitry Andric .addReg(ARM::R4, RegState::Implicit) 45491bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 45591bc56edSDimitry Andric break; 45691bc56edSDimitry Andric case CodeModel::Large: 45791bc56edSDimitry Andric case CodeModel::JITDefault: 45891bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12) 45991bc56edSDimitry Andric .addExternalSymbol("__chkstk") 46091bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 46191bc56edSDimitry Andric 46291bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr)) 46391bc56edSDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0) 46491bc56edSDimitry Andric .addReg(ARM::R12, RegState::Kill) 46591bc56edSDimitry Andric .addReg(ARM::R4, RegState::Implicit) 46691bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 46791bc56edSDimitry Andric break; 46891bc56edSDimitry Andric } 46991bc56edSDimitry Andric 47091bc56edSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), 47191bc56edSDimitry Andric ARM::SP) 47291bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 47391bc56edSDimitry Andric .addReg(ARM::R4, RegState::Kill) 47491bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup))); 47591bc56edSDimitry Andric NumBytes = 0; 47691bc56edSDimitry Andric } 47791bc56edSDimitry Andric 4782754fe60SDimitry Andric if (NumBytes) { 4792754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 48039d628a0SDimitry Andric if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) 48139d628a0SDimitry Andric DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes); 48239d628a0SDimitry Andric else { 4833b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 4843b0f4066SDimitry Andric MachineInstr::FrameSetup); 48539d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes); 48639d628a0SDimitry Andric } 487f785676fSDimitry Andric 4882754fe60SDimitry Andric if (HasFP && isARM) 4892754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 4902754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 4912754fe60SDimitry Andric // taken two instructions: 4922754fe60SDimitry Andric // mov sp, r7 4932754fe60SDimitry Andric // sub sp, #24 4942754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 4952754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 4962754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 4972754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 4982754fe60SDimitry Andric } 4992754fe60SDimitry Andric 50039d628a0SDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 50139d628a0SDimitry Andric // For iOS, FP is R7, which has now been stored in spill area 1. 50239d628a0SDimitry Andric // Otherwise, if this is not iOS, all the callee-saved registers go 50339d628a0SDimitry Andric // into spill area 1, including the FP in R11. In either case, it 50439d628a0SDimitry Andric // is in area one and the adjustment needs to take place just after 50539d628a0SDimitry Andric // that push. 50639d628a0SDimitry Andric if (HasFP) { 50739d628a0SDimitry Andric MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push); 50839d628a0SDimitry Andric unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push); 50939d628a0SDimitry Andric emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush, 51039d628a0SDimitry Andric dl, TII, FramePtr, ARM::SP, 51139d628a0SDimitry Andric PushSize + FramePtrOffsetInPush, 51239d628a0SDimitry Andric MachineInstr::FrameSetup); 51339d628a0SDimitry Andric if (FramePtrOffsetInPush + PushSize != 0) { 51439d628a0SDimitry Andric unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa( 51539d628a0SDimitry Andric nullptr, MRI->getDwarfRegNum(FramePtr, true), 51639d628a0SDimitry Andric -(ArgRegsSaveSize - FramePtrOffsetInPush))); 51739d628a0SDimitry Andric BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 51839d628a0SDimitry Andric .addCFIIndex(CFIIndex) 51939d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 52039d628a0SDimitry Andric } else { 52139d628a0SDimitry Andric unsigned CFIIndex = 52239d628a0SDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( 52339d628a0SDimitry Andric nullptr, MRI->getDwarfRegNum(FramePtr, true))); 52439d628a0SDimitry Andric BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 52539d628a0SDimitry Andric .addCFIIndex(CFIIndex) 52639d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 52739d628a0SDimitry Andric } 52839d628a0SDimitry Andric } 52939d628a0SDimitry Andric 53039d628a0SDimitry Andric // Now that the prologue's actual instructions are finalised, we can insert 53139d628a0SDimitry Andric // the necessary DWARF cf instructions to describe the situation. Start by 53239d628a0SDimitry Andric // recording where each register ended up: 53339d628a0SDimitry Andric if (GPRCS1Size > 0) { 53439d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(GPRCS1Push); 53539d628a0SDimitry Andric int CFIIndex; 53691bc56edSDimitry Andric for (const auto &Entry : CSI) { 53791bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 53891bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 53991bc56edSDimitry Andric switch (Reg) { 54091bc56edSDimitry Andric case ARM::R8: 54191bc56edSDimitry Andric case ARM::R9: 54291bc56edSDimitry Andric case ARM::R10: 54391bc56edSDimitry Andric case ARM::R11: 54491bc56edSDimitry Andric case ARM::R12: 54591bc56edSDimitry Andric if (STI.isTargetDarwin()) 54691bc56edSDimitry Andric break; 54791bc56edSDimitry Andric // fallthrough 54891bc56edSDimitry Andric case ARM::R0: 54991bc56edSDimitry Andric case ARM::R1: 55091bc56edSDimitry Andric case ARM::R2: 55191bc56edSDimitry Andric case ARM::R3: 55291bc56edSDimitry Andric case ARM::R4: 55391bc56edSDimitry Andric case ARM::R5: 55491bc56edSDimitry Andric case ARM::R6: 55591bc56edSDimitry Andric case ARM::R7: 55691bc56edSDimitry Andric case ARM::LR: 55791bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 55891bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI))); 55991bc56edSDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 56039d628a0SDimitry Andric .addCFIIndex(CFIIndex) 56139d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 56291bc56edSDimitry Andric break; 56391bc56edSDimitry Andric } 56491bc56edSDimitry Andric } 56591bc56edSDimitry Andric } 56691bc56edSDimitry Andric 56791bc56edSDimitry Andric if (GPRCS2Size > 0) { 56839d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(GPRCS2Push); 56991bc56edSDimitry Andric for (const auto &Entry : CSI) { 57091bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 57191bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 57291bc56edSDimitry Andric switch (Reg) { 57391bc56edSDimitry Andric case ARM::R8: 57491bc56edSDimitry Andric case ARM::R9: 57591bc56edSDimitry Andric case ARM::R10: 57691bc56edSDimitry Andric case ARM::R11: 57791bc56edSDimitry Andric case ARM::R12: 57891bc56edSDimitry Andric if (STI.isTargetDarwin()) { 57991bc56edSDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 58091bc56edSDimitry Andric unsigned Offset = MFI->getObjectOffset(FI); 58191bc56edSDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 58291bc56edSDimitry Andric MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); 58391bc56edSDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 58439d628a0SDimitry Andric .addCFIIndex(CFIIndex) 58539d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 58691bc56edSDimitry Andric } 58791bc56edSDimitry Andric break; 58891bc56edSDimitry Andric } 58991bc56edSDimitry Andric } 59091bc56edSDimitry Andric } 59191bc56edSDimitry Andric 59291bc56edSDimitry Andric if (DPRCSSize > 0) { 59391bc56edSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 59491bc56edSDimitry Andric // instructions in the prologue. 59539d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(LastPush); 59691bc56edSDimitry Andric for (const auto &Entry : CSI) { 59791bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 59891bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 59991bc56edSDimitry Andric if ((Reg >= ARM::D0 && Reg <= ARM::D31) && 60091bc56edSDimitry Andric (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) { 60191bc56edSDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 60291bc56edSDimitry Andric unsigned Offset = MFI->getObjectOffset(FI); 60391bc56edSDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 60491bc56edSDimitry Andric MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); 60539d628a0SDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 60639d628a0SDimitry Andric .addCFIIndex(CFIIndex) 60739d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 60891bc56edSDimitry Andric } 60991bc56edSDimitry Andric } 61091bc56edSDimitry Andric } 61191bc56edSDimitry Andric 61239d628a0SDimitry Andric // Now we can emit descriptions of where the canonical frame address was 61339d628a0SDimitry Andric // throughout the process. If we have a frame pointer, it takes over the job 61439d628a0SDimitry Andric // half-way through, so only the first few .cfi_def_cfa_offset instructions 61539d628a0SDimitry Andric // actually get emitted. 61639d628a0SDimitry Andric DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP); 617f785676fSDimitry Andric 6182754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 6192754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 6202754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 6212754fe60SDimitry Andric 6222754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 6232754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 62439d628a0SDimitry Andric AFI->setDPRCalleeSavedGapSize(DPRGapSize); 6252754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 6262754fe60SDimitry Andric 6272754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 6282754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 629dff0c46cSDimitry Andric // If aligned NEON registers were spilled, the stack has already been 630dff0c46cSDimitry Andric // realigned. 631dff0c46cSDimitry Andric if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { 6322754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 6332754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction()); 6342754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 63539d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign, 63639d628a0SDimitry Andric false); 6372754fe60SDimitry Andric } else { 63839d628a0SDimitry Andric // We cannot use sp as source/dest register here, thus we're using r4 to 63939d628a0SDimitry Andric // perform the calculations. We're emitting the following sequence: 6402754fe60SDimitry Andric // mov r4, sp 64139d628a0SDimitry Andric // -- use emitAligningInstructions to produce best sequence to zero 64239d628a0SDimitry Andric // -- out lower bits in r4 6432754fe60SDimitry Andric // mov sp, r4 6442754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 64517a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 64617a519f9SDimitry Andric .addReg(ARM::SP, RegState::Kill)); 64739d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign, 64839d628a0SDimitry Andric false); 64917a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 65017a519f9SDimitry Andric .addReg(ARM::R4, RegState::Kill)); 6512754fe60SDimitry Andric } 6522754fe60SDimitry Andric 6532754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 6542754fe60SDimitry Andric } 6552754fe60SDimitry Andric 6562754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 6572754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 6582754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 6592754fe60SDimitry Andric // to reference locals. 6603b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 6612754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 6622754fe60SDimitry Andric if (isARM) 6632754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 6642754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 6652754fe60SDimitry Andric .addReg(ARM::SP) 6662754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 6672754fe60SDimitry Andric else 66817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 66917a519f9SDimitry Andric RegInfo->getBaseRegister()) 67017a519f9SDimitry Andric .addReg(ARM::SP)); 6712754fe60SDimitry Andric } 6722754fe60SDimitry Andric 6732754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 6742754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 6752754fe60SDimitry Andric // checks for hasVarSizedObjects. 6762754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 6772754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 6782754fe60SDimitry Andric } 6792754fe60SDimitry Andric 6809cac79b3SDimitry Andric // Resolve TCReturn pseudo-instruction 6819cac79b3SDimitry Andric void ARMFrameLowering::fixTCReturn(MachineFunction &MF, 6822754fe60SDimitry Andric MachineBasicBlock &MBB) const { 6832754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 684dff0c46cSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 6852754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 6862754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 6879cac79b3SDimitry Andric const ARMBaseInstrInfo &TII = 68839d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 6899cac79b3SDimitry Andric 6909cac79b3SDimitry Andric if (!(RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri)) 6919cac79b3SDimitry Andric return; 6929cac79b3SDimitry Andric 6939cac79b3SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 6949cac79b3SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 6959cac79b3SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 6969cac79b3SDimitry Andric 6979cac79b3SDimitry Andric // Jump to label or value in register. 6989cac79b3SDimitry Andric if (RetOpcode == ARM::TCRETURNdi) { 6999cac79b3SDimitry Andric unsigned TCOpcode = STI.isThumb() ? 7009cac79b3SDimitry Andric (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : 7019cac79b3SDimitry Andric ARM::TAILJMPd; 7029cac79b3SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 7039cac79b3SDimitry Andric if (JumpTarget.isGlobal()) 7049cac79b3SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 7059cac79b3SDimitry Andric JumpTarget.getTargetFlags()); 7069cac79b3SDimitry Andric else { 7079cac79b3SDimitry Andric assert(JumpTarget.isSymbol()); 7089cac79b3SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 7099cac79b3SDimitry Andric JumpTarget.getTargetFlags()); 7109cac79b3SDimitry Andric } 7119cac79b3SDimitry Andric 7129cac79b3SDimitry Andric // Add the default predicate in Thumb mode. 7139cac79b3SDimitry Andric if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 7149cac79b3SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 7159cac79b3SDimitry Andric BuildMI(MBB, MBBI, dl, 7169cac79b3SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 7179cac79b3SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 7189cac79b3SDimitry Andric } 7199cac79b3SDimitry Andric 7209cac79b3SDimitry Andric MachineInstr *NewMI = std::prev(MBBI); 7219cac79b3SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 7229cac79b3SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 7239cac79b3SDimitry Andric 7249cac79b3SDimitry Andric // Delete the pseudo instruction TCRETURN. 7259cac79b3SDimitry Andric MBB.erase(MBBI); 7269cac79b3SDimitry Andric MBBI = NewMI; 7279cac79b3SDimitry Andric } 7289cac79b3SDimitry Andric 7299cac79b3SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 7309cac79b3SDimitry Andric MachineBasicBlock &MBB) const { 7319cac79b3SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 7329cac79b3SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 7339cac79b3SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 7342754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 7352754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 73639d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 7372754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 73839d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 7392754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 7402754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 7412754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 7422754fe60SDimitry Andric 74339d628a0SDimitry Andric unsigned Align = MF.getTarget() 74439d628a0SDimitry Andric .getSubtargetImpl() 74539d628a0SDimitry Andric ->getFrameLowering() 74639d628a0SDimitry Andric ->getStackAlignment(); 747f785676fSDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); 7482754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 7492754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 7502754fe60SDimitry Andric 7513861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 7523861d79fSDimitry Andric // prologue/epilogue. 7539cac79b3SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) { 7549cac79b3SDimitry Andric fixTCReturn(MF, MBB); 7557ae0e2c9SDimitry Andric return; 7569cac79b3SDimitry Andric } 7577ae0e2c9SDimitry Andric 7582754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 75991bc56edSDimitry Andric if (NumBytes - ArgRegsSaveSize != 0) 76091bc56edSDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize); 7612754fe60SDimitry Andric } else { 7622754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 76391bc56edSDimitry Andric const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 7642754fe60SDimitry Andric if (MBBI != MBB.begin()) { 765f785676fSDimitry Andric do { 7662754fe60SDimitry Andric --MBBI; 767f785676fSDimitry Andric } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 7682754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 7692754fe60SDimitry Andric ++MBBI; 7702754fe60SDimitry Andric } 7712754fe60SDimitry Andric 7722754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 77391bc56edSDimitry Andric NumBytes -= (ArgRegsSaveSize + 77491bc56edSDimitry Andric AFI->getGPRCalleeSavedArea1Size() + 7752754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 77639d628a0SDimitry Andric AFI->getDPRCalleeSavedGapSize() + 7772754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 7782754fe60SDimitry Andric 7792754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 7802754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 7812754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 7822754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 7832754fe60SDimitry Andric if (NumBytes) { 7842754fe60SDimitry Andric if (isARM) 7852754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 7862754fe60SDimitry Andric ARMCC::AL, 0, TII); 7872754fe60SDimitry Andric else { 7882754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 789dff0c46cSDimitry Andric // For iOS, this looks like: 7902754fe60SDimitry Andric // mov sp, r7 7912754fe60SDimitry Andric // sub sp, #24 7922754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 7932754fe60SDimitry Andric // inconsistent state. 7942754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 7952754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 7962754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 7972754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 7982754fe60SDimitry Andric ARMCC::AL, 0, TII); 79917a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 80017a519f9SDimitry Andric ARM::SP) 80117a519f9SDimitry Andric .addReg(ARM::R4)); 8022754fe60SDimitry Andric } 8032754fe60SDimitry Andric } else { 8042754fe60SDimitry Andric // Thumb2 or ARM. 8052754fe60SDimitry Andric if (isARM) 8062754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 8072754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 8082754fe60SDimitry Andric else 80917a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 81017a519f9SDimitry Andric ARM::SP) 81117a519f9SDimitry Andric .addReg(FramePtr)); 8122754fe60SDimitry Andric } 81391bc56edSDimitry Andric } else if (NumBytes && 81491bc56edSDimitry Andric !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes)) 8152754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 8162754fe60SDimitry Andric 8172754fe60SDimitry Andric // Increment past our save areas. 818dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 819dd6029ffSDimitry Andric MBBI++; 820dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 821dd6029ffSDimitry Andric // instructions in the epilogue. 822dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 823dd6029ffSDimitry Andric MBBI++; 824dd6029ffSDimitry Andric } 82539d628a0SDimitry Andric if (AFI->getDPRCalleeSavedGapSize()) { 82639d628a0SDimitry Andric assert(AFI->getDPRCalleeSavedGapSize() == 4 && 82739d628a0SDimitry Andric "unexpected DPR alignment gap"); 82839d628a0SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize()); 82939d628a0SDimitry Andric } 83039d628a0SDimitry Andric 8312754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 8322754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 8332754fe60SDimitry Andric } 8342754fe60SDimitry Andric 8359cac79b3SDimitry Andric fixTCReturn(MF, MBB); 8362754fe60SDimitry Andric 837284c1978SDimitry Andric if (ArgRegsSaveSize) 838284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); 8392754fe60SDimitry Andric } 8402754fe60SDimitry Andric 8412754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 8422754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 8432754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 8442754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 8452754fe60SDimitry Andric int 8462754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 8472754fe60SDimitry Andric unsigned &FrameReg) const { 8482754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 8492754fe60SDimitry Andric } 8502754fe60SDimitry Andric 8512754fe60SDimitry Andric int 8522754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 8533b0f4066SDimitry Andric int FI, unsigned &FrameReg, 8542754fe60SDimitry Andric int SPAdj) const { 8552754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 85639d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 85739d628a0SDimitry Andric MF.getSubtarget().getRegisterInfo()); 8582754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 8592754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 8602754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 8612754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 8622754fe60SDimitry Andric 8632754fe60SDimitry Andric FrameReg = ARM::SP; 8642754fe60SDimitry Andric Offset += SPAdj; 8652754fe60SDimitry Andric 866dff0c46cSDimitry Andric // SP can move around if there are allocas. We may also lose track of SP 867dff0c46cSDimitry Andric // when emergency spilling inside a non-reserved call frame setup. 868dff0c46cSDimitry Andric bool hasMovingSP = !hasReservedCallFrame(MF); 869dff0c46cSDimitry Andric 8702754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 8712754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 8722754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 8732754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 8742754fe60SDimitry Andric if (isFixed) { 8752754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 8762754fe60SDimitry Andric Offset = FPOffset; 877dff0c46cSDimitry Andric } else if (hasMovingSP) { 8782754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 8792754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 8802754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 8812754fe60SDimitry Andric } 8822754fe60SDimitry Andric return Offset; 8832754fe60SDimitry Andric } 8842754fe60SDimitry Andric 8852754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 8862754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 8872754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 8882754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 889dff0c46cSDimitry Andric if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { 8902754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 8912754fe60SDimitry Andric return FPOffset; 892dff0c46cSDimitry Andric } else if (hasMovingSP) { 8932754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 8943b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 8952754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 8962754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 8972754fe60SDimitry Andric // particular. 8982754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 8992754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9002754fe60SDimitry Andric return FPOffset; 9012754fe60SDimitry Andric } 9023b0f4066SDimitry Andric } 9032754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 9043b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 9053b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 9063b0f4066SDimitry Andric // if at all possible to save space. 9073b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 9083b0f4066SDimitry Andric return Offset; 9092754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 9103b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 9112754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 9122754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9132754fe60SDimitry Andric return FPOffset; 9142754fe60SDimitry Andric } 9152754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 9162754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 9172754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9182754fe60SDimitry Andric return FPOffset; 9192754fe60SDimitry Andric } 9202754fe60SDimitry Andric } 9212754fe60SDimitry Andric // Use the base pointer if we have one. 9222754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 9232754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 9242754fe60SDimitry Andric return Offset; 9252754fe60SDimitry Andric } 9262754fe60SDimitry Andric 9272754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 9282754fe60SDimitry Andric int FI) const { 9292754fe60SDimitry Andric unsigned FrameReg; 9302754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 9312754fe60SDimitry Andric } 9322754fe60SDimitry Andric 9332754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 9342754fe60SDimitry Andric MachineBasicBlock::iterator MI, 9352754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 9362754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 9372754fe60SDimitry Andric bool NoGap, 9383b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 939dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 9403b0f4066SDimitry Andric unsigned MIFlags) const { 9412754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 94239d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 9432754fe60SDimitry Andric 9442754fe60SDimitry Andric DebugLoc DL; 9452754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 9462754fe60SDimitry Andric 9472754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 9482754fe60SDimitry Andric unsigned i = CSI.size(); 9492754fe60SDimitry Andric while (i != 0) { 9502754fe60SDimitry Andric unsigned LastReg = 0; 9512754fe60SDimitry Andric for (; i != 0; --i) { 9522754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 95391bc56edSDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 954dff0c46cSDimitry Andric 955dff0c46cSDimitry Andric // D-registers in the aligned area DPRCS2 are NOT spilled here. 956dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 957dff0c46cSDimitry Andric continue; 9582754fe60SDimitry Andric 9592754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 9602754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 9612754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 9622754fe60SDimitry Andric // entry block live-in sets. 9632754fe60SDimitry Andric bool isKill = true; 9642754fe60SDimitry Andric if (Reg == ARM::LR) { 9652754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 9662754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 9672754fe60SDimitry Andric isKill = false; 9682754fe60SDimitry Andric } 9692754fe60SDimitry Andric 9702754fe60SDimitry Andric if (isKill) 9712754fe60SDimitry Andric MBB.addLiveIn(Reg); 9722754fe60SDimitry Andric 9732754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 9742754fe60SDimitry Andric // for other instructions. e.g. 9752754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 9762754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 9772754fe60SDimitry Andric break; 9782754fe60SDimitry Andric LastReg = Reg; 9792754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 9802754fe60SDimitry Andric } 9812754fe60SDimitry Andric 9822754fe60SDimitry Andric if (Regs.empty()) 9832754fe60SDimitry Andric continue; 9842754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 9852754fe60SDimitry Andric MachineInstrBuilder MIB = 9862754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 9873b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 9882754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 9892754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 9902754fe60SDimitry Andric } else if (Regs.size() == 1) { 9912754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 9922754fe60SDimitry Andric ARM::SP) 9932754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 9946122f3e6SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags) 9956122f3e6SDimitry Andric .addImm(-4); 9962754fe60SDimitry Andric AddDefaultPred(MIB); 9972754fe60SDimitry Andric } 9982754fe60SDimitry Andric Regs.clear(); 99991bc56edSDimitry Andric 100091bc56edSDimitry Andric // Put any subsequent vpush instructions before this one: they will refer to 100191bc56edSDimitry Andric // higher register numbers so need to be pushed first in order to preserve 100291bc56edSDimitry Andric // monotonicity. 100391bc56edSDimitry Andric --MI; 10042754fe60SDimitry Andric } 10052754fe60SDimitry Andric } 10062754fe60SDimitry Andric 10072754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 10082754fe60SDimitry Andric MachineBasicBlock::iterator MI, 10092754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 10102754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 10112754fe60SDimitry Andric bool isVarArg, bool NoGap, 1012dff0c46cSDimitry Andric bool(*Func)(unsigned, bool), 1013dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) const { 10142754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 101539d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 10162754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 10172754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 10182754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 10192754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 1020dff0c46cSDimitry Andric RetOpcode == ARM::TCRETURNri); 1021f785676fSDimitry Andric bool isInterrupt = 1022f785676fSDimitry Andric RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR; 10232754fe60SDimitry Andric 10242754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 10252754fe60SDimitry Andric unsigned i = CSI.size(); 10262754fe60SDimitry Andric while (i != 0) { 10272754fe60SDimitry Andric unsigned LastReg = 0; 10282754fe60SDimitry Andric bool DeleteRet = false; 10292754fe60SDimitry Andric for (; i != 0; --i) { 10302754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 103191bc56edSDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 1032dff0c46cSDimitry Andric 1033dff0c46cSDimitry Andric // The aligned reloads from area DPRCS2 are not inserted here. 1034dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 1035dff0c46cSDimitry Andric continue; 10362754fe60SDimitry Andric 1037f785676fSDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt && 1038f785676fSDimitry Andric STI.hasV5TOps()) { 10392754fe60SDimitry Andric Reg = ARM::PC; 10402754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 10412754fe60SDimitry Andric // Fold the return instruction into the LDM. 10422754fe60SDimitry Andric DeleteRet = true; 10432754fe60SDimitry Andric } 10442754fe60SDimitry Andric 10452754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 10462754fe60SDimitry Andric // for other instructions. e.g. 10472754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 10482754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 10492754fe60SDimitry Andric break; 10502754fe60SDimitry Andric 10512754fe60SDimitry Andric LastReg = Reg; 10522754fe60SDimitry Andric Regs.push_back(Reg); 10532754fe60SDimitry Andric } 10542754fe60SDimitry Andric 10552754fe60SDimitry Andric if (Regs.empty()) 10562754fe60SDimitry Andric continue; 10572754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 10582754fe60SDimitry Andric MachineInstrBuilder MIB = 10592754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 10602754fe60SDimitry Andric .addReg(ARM::SP)); 10612754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 10622754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 10636122f3e6SDimitry Andric if (DeleteRet) { 1064139f7f9bSDimitry Andric MIB.copyImplicitOps(&*MI); 10652754fe60SDimitry Andric MI->eraseFromParent(); 10666122f3e6SDimitry Andric } 10672754fe60SDimitry Andric MI = MIB; 10682754fe60SDimitry Andric } else if (Regs.size() == 1) { 10692754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 10702754fe60SDimitry Andric // only do that for LDM. 10712754fe60SDimitry Andric if (Regs[0] == ARM::PC) 10722754fe60SDimitry Andric Regs[0] = ARM::LR; 10732754fe60SDimitry Andric MachineInstrBuilder MIB = 10742754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 10752754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 10762754fe60SDimitry Andric .addReg(ARM::SP); 10772754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 10782754fe60SDimitry Andric // that refactoring is complete (eventually). 10796122f3e6SDimitry Andric if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 10802754fe60SDimitry Andric MIB.addReg(0); 10812754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 10822754fe60SDimitry Andric } else 10832754fe60SDimitry Andric MIB.addImm(4); 10842754fe60SDimitry Andric AddDefaultPred(MIB); 10852754fe60SDimitry Andric } 10862754fe60SDimitry Andric Regs.clear(); 108791bc56edSDimitry Andric 108891bc56edSDimitry Andric // Put any subsequent vpop instructions after this one: they will refer to 108991bc56edSDimitry Andric // higher register numbers so need to be popped afterwards. 109091bc56edSDimitry Andric ++MI; 10912754fe60SDimitry Andric } 10922754fe60SDimitry Andric } 10932754fe60SDimitry Andric 1094dff0c46cSDimitry Andric /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers 1095dff0c46cSDimitry Andric /// starting from d8. Also insert stack realignment code and leave the stack 1096dff0c46cSDimitry Andric /// pointer pointing to the d8 spill slot. 1097dff0c46cSDimitry Andric static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, 1098dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 1099dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 1100dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 1101dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 1102dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 1103dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1104dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 110539d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1106dff0c46cSDimitry Andric MachineFrameInfo &MFI = *MF.getFrameInfo(); 1107dff0c46cSDimitry Andric 1108dff0c46cSDimitry Andric // Mark the D-register spill slots as properly aligned. Since MFI computes 1109dff0c46cSDimitry Andric // stack slot layout backwards, this can actually mean that the d-reg stack 1110dff0c46cSDimitry Andric // slot offsets can be wrong. The offset for d8 will always be correct. 1111dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1112dff0c46cSDimitry Andric unsigned DNum = CSI[i].getReg() - ARM::D8; 1113dff0c46cSDimitry Andric if (DNum >= 8) 1114dff0c46cSDimitry Andric continue; 1115dff0c46cSDimitry Andric int FI = CSI[i].getFrameIdx(); 1116dff0c46cSDimitry Andric // The even-numbered registers will be 16-byte aligned, the odd-numbered 1117dff0c46cSDimitry Andric // registers will be 8-byte aligned. 1118dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); 1119dff0c46cSDimitry Andric 1120dff0c46cSDimitry Andric // The stack slot for D8 needs to be maximally aligned because this is 1121dff0c46cSDimitry Andric // actually the point where we align the stack pointer. MachineFrameInfo 1122dff0c46cSDimitry Andric // computes all offsets relative to the incoming stack pointer which is a 1123dff0c46cSDimitry Andric // bit weird when realigning the stack. Any extra padding for this 1124dff0c46cSDimitry Andric // over-alignment is not realized because the code inserted below adjusts 1125dff0c46cSDimitry Andric // the stack pointer by numregs * 8 before aligning the stack pointer. 1126dff0c46cSDimitry Andric if (DNum == 0) 1127dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); 1128dff0c46cSDimitry Andric } 1129dff0c46cSDimitry Andric 1130dff0c46cSDimitry Andric // Move the stack pointer to the d8 spill slot, and align it at the same 1131dff0c46cSDimitry Andric // time. Leave the stack slot address in the scratch register r4. 1132dff0c46cSDimitry Andric // 1133dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1134dff0c46cSDimitry Andric // bic r4, r4, #align - 1 1135dff0c46cSDimitry Andric // mov sp, r4 1136dff0c46cSDimitry Andric // 1137dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 1138dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 1139dff0c46cSDimitry Andric AFI->setShouldRestoreSPFromFP(true); 1140dff0c46cSDimitry Andric 1141dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1142dff0c46cSDimitry Andric // The immediate is <= 64, so it doesn't need any special encoding. 1143dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; 1144dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 1145dff0c46cSDimitry Andric .addReg(ARM::SP) 1146dff0c46cSDimitry Andric .addImm(8 * NumAlignedDPRCS2Regs))); 1147dff0c46cSDimitry Andric 1148dff0c46cSDimitry Andric unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); 114939d628a0SDimitry Andric // We must set parameter MustBeSingleInstruction to true, since 115039d628a0SDimitry Andric // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform 115139d628a0SDimitry Andric // stack alignment. Luckily, this can always be done since all ARM 115239d628a0SDimitry Andric // architecture versions that support Neon also support the BFC 115339d628a0SDimitry Andric // instruction. 115439d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true); 1155dff0c46cSDimitry Andric 1156dff0c46cSDimitry Andric // mov sp, r4 1157dff0c46cSDimitry Andric // The stack pointer must be adjusted before spilling anything, otherwise 1158dff0c46cSDimitry Andric // the stack slots could be clobbered by an interrupt handler. 1159dff0c46cSDimitry Andric // Leave r4 live, it is used below. 1160dff0c46cSDimitry Andric Opc = isThumb ? ARM::tMOVr : ARM::MOVr; 1161dff0c46cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) 1162dff0c46cSDimitry Andric .addReg(ARM::R4); 1163dff0c46cSDimitry Andric MIB = AddDefaultPred(MIB); 1164dff0c46cSDimitry Andric if (!isThumb) 1165dff0c46cSDimitry Andric AddDefaultCC(MIB); 1166dff0c46cSDimitry Andric 1167dff0c46cSDimitry Andric // Now spill NumAlignedDPRCS2Regs registers starting from d8. 1168dff0c46cSDimitry Andric // r4 holds the stack slot address. 1169dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 1170dff0c46cSDimitry Andric 1171dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs and address writeback. 1172dff0c46cSDimitry Andric // The writeback is only needed when emitting two vst1.64 instructions. 1173dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 1174dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 11757ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1176dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1177dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), 1178dff0c46cSDimitry Andric ARM::R4) 1179dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 1180dff0c46cSDimitry Andric .addReg(NextReg) 1181dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 1182dff0c46cSDimitry Andric NextReg += 4; 1183dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1184dff0c46cSDimitry Andric } 1185dff0c46cSDimitry Andric 1186dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 1187dff0c46cSDimitry Andric // register to be spilled. 1188dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 1189dff0c46cSDimitry Andric 1190dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs, no writeback. 1191dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 1192dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 11937ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1194dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1195dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) 1196dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(NextReg) 1197dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 1198dff0c46cSDimitry Andric NextReg += 4; 1199dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1200dff0c46cSDimitry Andric } 1201dff0c46cSDimitry Andric 1202dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 2 d-regs. 1203dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 1204dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 12057ae0e2c9SDimitry Andric &ARM::QPRRegClass); 1206dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1207dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) 1208dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(SupReg)); 1209dff0c46cSDimitry Andric NextReg += 2; 1210dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 1211dff0c46cSDimitry Andric } 1212dff0c46cSDimitry Andric 1213dff0c46cSDimitry Andric // Finally, use a vanilla vstr.64 for the odd last register. 1214dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) { 1215dff0c46cSDimitry Andric MBB.addLiveIn(NextReg); 1216dff0c46cSDimitry Andric // vstr.64 uses addrmode5 which has an offset scale of 4. 1217dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) 1218dff0c46cSDimitry Andric .addReg(NextReg) 1219dff0c46cSDimitry Andric .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); 1220dff0c46cSDimitry Andric } 1221dff0c46cSDimitry Andric 1222dff0c46cSDimitry Andric // The last spill instruction inserted should kill the scratch register r4. 122391bc56edSDimitry Andric std::prev(MI)->addRegisterKilled(ARM::R4, TRI); 1224dff0c46cSDimitry Andric } 1225dff0c46cSDimitry Andric 1226dff0c46cSDimitry Andric /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an 1227dff0c46cSDimitry Andric /// iterator to the following instruction. 1228dff0c46cSDimitry Andric static MachineBasicBlock::iterator 1229dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 1230dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) { 1231dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1232dff0c46cSDimitry Andric // bic r4, r4, #align - 1 1233dff0c46cSDimitry Andric // mov sp, r4 1234dff0c46cSDimitry Andric ++MI; ++MI; ++MI; 1235dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1236dff0c46cSDimitry Andric 1237dff0c46cSDimitry Andric // These switches all fall through. 1238dff0c46cSDimitry Andric switch(NumAlignedDPRCS2Regs) { 1239dff0c46cSDimitry Andric case 7: 1240dff0c46cSDimitry Andric ++MI; 1241dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1242dff0c46cSDimitry Andric default: 1243dff0c46cSDimitry Andric ++MI; 1244dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1245dff0c46cSDimitry Andric case 1: 1246dff0c46cSDimitry Andric case 2: 1247dff0c46cSDimitry Andric case 4: 1248dff0c46cSDimitry Andric assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); 1249dff0c46cSDimitry Andric ++MI; 1250dff0c46cSDimitry Andric } 1251dff0c46cSDimitry Andric return MI; 1252dff0c46cSDimitry Andric } 1253dff0c46cSDimitry Andric 1254dff0c46cSDimitry Andric /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers 1255dff0c46cSDimitry Andric /// starting from d8. These instructions are assumed to execute while the 1256dff0c46cSDimitry Andric /// stack is still aligned, unlike the code inserted by emitPopInst. 1257dff0c46cSDimitry Andric static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, 1258dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 1259dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 1260dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 1261dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 1262dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 1263dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1264dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 126539d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1266dff0c46cSDimitry Andric 1267dff0c46cSDimitry Andric // Find the frame index assigned to d8. 1268dff0c46cSDimitry Andric int D8SpillFI = 0; 1269dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) 1270dff0c46cSDimitry Andric if (CSI[i].getReg() == ARM::D8) { 1271dff0c46cSDimitry Andric D8SpillFI = CSI[i].getFrameIdx(); 1272dff0c46cSDimitry Andric break; 1273dff0c46cSDimitry Andric } 1274dff0c46cSDimitry Andric 1275dff0c46cSDimitry Andric // Materialize the address of the d8 spill slot into the scratch register r4. 1276dff0c46cSDimitry Andric // This can be fairly complicated if the stack frame is large, so just use 1277dff0c46cSDimitry Andric // the normal frame index elimination mechanism to do it. This code runs as 1278dff0c46cSDimitry Andric // the initial part of the epilog where the stack and base pointers haven't 1279dff0c46cSDimitry Andric // been changed yet. 1280dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 1281dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 1282dff0c46cSDimitry Andric 1283dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; 1284dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 1285dff0c46cSDimitry Andric .addFrameIndex(D8SpillFI).addImm(0))); 1286dff0c46cSDimitry Andric 1287dff0c46cSDimitry Andric // Now restore NumAlignedDPRCS2Regs registers starting from d8. 1288dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 1289dff0c46cSDimitry Andric 1290dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs and writeback. 1291dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 1292dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 12937ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1294dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) 1295dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Define) 1296dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 1297dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 1298dff0c46cSDimitry Andric NextReg += 4; 1299dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1300dff0c46cSDimitry Andric } 1301dff0c46cSDimitry Andric 1302dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 1303dff0c46cSDimitry Andric // register to be spilled. 1304dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 1305dff0c46cSDimitry Andric 1306dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs, no writeback. 1307dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 1308dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 13097ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1310dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) 1311dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16) 1312dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 1313dff0c46cSDimitry Andric NextReg += 4; 1314dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1315dff0c46cSDimitry Andric } 1316dff0c46cSDimitry Andric 1317dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 2 d-regs. 1318dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 1319dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 13207ae0e2c9SDimitry Andric &ARM::QPRRegClass); 1321dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) 1322dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16)); 1323dff0c46cSDimitry Andric NextReg += 2; 1324dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 1325dff0c46cSDimitry Andric } 1326dff0c46cSDimitry Andric 1327dff0c46cSDimitry Andric // Finally, use a vanilla vldr.64 for the remaining odd register. 1328dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1329dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) 1330dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); 1331dff0c46cSDimitry Andric 1332dff0c46cSDimitry Andric // Last store kills r4. 133391bc56edSDimitry Andric std::prev(MI)->addRegisterKilled(ARM::R4, TRI); 1334dff0c46cSDimitry Andric } 1335dff0c46cSDimitry Andric 13362754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 13372754fe60SDimitry Andric MachineBasicBlock::iterator MI, 13382754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 13392754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 13402754fe60SDimitry Andric if (CSI.empty()) 13412754fe60SDimitry Andric return false; 13422754fe60SDimitry Andric 13432754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13442754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 13452754fe60SDimitry Andric 13462754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 13476122f3e6SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? 13486122f3e6SDimitry Andric ARM::t2STR_PRE : ARM::STR_PRE_IMM; 13492754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 1350dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1351dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, 13523b0f4066SDimitry Andric MachineInstr::FrameSetup); 1353dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, 13543b0f4066SDimitry Andric MachineInstr::FrameSetup); 13553b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 1356dff0c46cSDimitry Andric NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); 1357dff0c46cSDimitry Andric 1358dff0c46cSDimitry Andric // The code above does not insert spill code for the aligned DPRCS2 registers. 1359dff0c46cSDimitry Andric // The stack realignment code will be inserted between the push instructions 1360dff0c46cSDimitry Andric // and these spills. 1361dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1362dff0c46cSDimitry Andric emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 13632754fe60SDimitry Andric 13642754fe60SDimitry Andric return true; 13652754fe60SDimitry Andric } 13662754fe60SDimitry Andric 13672754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 13682754fe60SDimitry Andric MachineBasicBlock::iterator MI, 13692754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 13702754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 13712754fe60SDimitry Andric if (CSI.empty()) 13722754fe60SDimitry Andric return false; 13732754fe60SDimitry Andric 13742754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13752754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1376284c1978SDimitry Andric bool isVarArg = AFI->getArgRegsSaveSize() > 0; 1377dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1378dff0c46cSDimitry Andric 1379dff0c46cSDimitry Andric // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 1380dff0c46cSDimitry Andric // registers. Do that here instead. 1381dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1382dff0c46cSDimitry Andric emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 13832754fe60SDimitry Andric 13842754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 13856122f3e6SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 13862754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 1387dff0c46cSDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, 1388dff0c46cSDimitry Andric NumAlignedDPRCS2Regs); 13892754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1390dff0c46cSDimitry Andric &isARMArea2Register, 0); 13912754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1392dff0c46cSDimitry Andric &isARMArea1Register, 0); 13932754fe60SDimitry Andric 13942754fe60SDimitry Andric return true; 13952754fe60SDimitry Andric } 13962754fe60SDimitry Andric 13972754fe60SDimitry Andric // FIXME: Make generic? 13982754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 13992754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 14002754fe60SDimitry Andric unsigned FnSize = 0; 140191bc56edSDimitry Andric for (auto &MBB : MF) { 140291bc56edSDimitry Andric for (auto &MI : MBB) 140391bc56edSDimitry Andric FnSize += TII.GetInstSizeInBytes(&MI); 14042754fe60SDimitry Andric } 14052754fe60SDimitry Andric return FnSize; 14062754fe60SDimitry Andric } 14072754fe60SDimitry Andric 14082754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 14092754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 14102754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 14112754fe60SDimitry Andric // FIXME: Move to TII? 14122754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 14132754fe60SDimitry Andric const TargetFrameLowering *TFI) { 14142754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 14152754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 141691bc56edSDimitry Andric for (auto &MBB : MF) { 141791bc56edSDimitry Andric for (auto &MI : MBB) { 141891bc56edSDimitry Andric for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 141991bc56edSDimitry Andric if (!MI.getOperand(i).isFI()) 142091bc56edSDimitry Andric continue; 14212754fe60SDimitry Andric 14222754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 14232754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 142491bc56edSDimitry Andric if (MI.getOpcode() == ARM::ADDri) { 14252754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14262754fe60SDimitry Andric break; 14272754fe60SDimitry Andric } 14282754fe60SDimitry Andric 14292754fe60SDimitry Andric // Otherwise check the addressing mode. 143091bc56edSDimitry Andric switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) { 14312754fe60SDimitry Andric case ARMII::AddrMode3: 14322754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 14332754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14342754fe60SDimitry Andric break; 14352754fe60SDimitry Andric case ARMII::AddrMode5: 14362754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 14372754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 14382754fe60SDimitry Andric break; 14392754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 14402754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 14412754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 14422754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 14432754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14442754fe60SDimitry Andric break; 14452754fe60SDimitry Andric case ARMII::AddrMode4: 14462754fe60SDimitry Andric case ARMII::AddrMode6: 14472754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 14482754fe60SDimitry Andric // immediate offset for stack references. 14492754fe60SDimitry Andric return 0; 14502754fe60SDimitry Andric default: 14512754fe60SDimitry Andric break; 14522754fe60SDimitry Andric } 14532754fe60SDimitry Andric break; // At most one FI per instruction 14542754fe60SDimitry Andric } 14552754fe60SDimitry Andric } 14562754fe60SDimitry Andric } 14572754fe60SDimitry Andric 14582754fe60SDimitry Andric return Limit; 14592754fe60SDimitry Andric } 14602754fe60SDimitry Andric 1461dff0c46cSDimitry Andric // In functions that realign the stack, it can be an advantage to spill the 1462dff0c46cSDimitry Andric // callee-saved vector registers after realigning the stack. The vst1 and vld1 1463dff0c46cSDimitry Andric // instructions take alignment hints that can improve performance. 1464dff0c46cSDimitry Andric // 1465dff0c46cSDimitry Andric static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { 1466dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); 1467dff0c46cSDimitry Andric if (!SpillAlignedNEONRegs) 1468dff0c46cSDimitry Andric return; 1469dff0c46cSDimitry Andric 1470dff0c46cSDimitry Andric // Naked functions don't spill callee-saved registers. 1471139f7f9bSDimitry Andric if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 1472139f7f9bSDimitry Andric Attribute::Naked)) 1473dff0c46cSDimitry Andric return; 1474dff0c46cSDimitry Andric 1475dff0c46cSDimitry Andric // We are planning to use NEON instructions vst1 / vld1. 1476dff0c46cSDimitry Andric if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON()) 1477dff0c46cSDimitry Andric return; 1478dff0c46cSDimitry Andric 1479dff0c46cSDimitry Andric // Don't bother if the default stack alignment is sufficiently high. 148039d628a0SDimitry Andric if (MF.getTarget() 148139d628a0SDimitry Andric .getSubtargetImpl() 148239d628a0SDimitry Andric ->getFrameLowering() 148339d628a0SDimitry Andric ->getStackAlignment() >= 8) 1484dff0c46cSDimitry Andric return; 1485dff0c46cSDimitry Andric 1486dff0c46cSDimitry Andric // Aligned spills require stack realignment. 148739d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 148839d628a0SDimitry Andric MF.getSubtarget().getRegisterInfo()); 1489dff0c46cSDimitry Andric if (!RegInfo->canRealignStack(MF)) 1490dff0c46cSDimitry Andric return; 1491dff0c46cSDimitry Andric 1492dff0c46cSDimitry Andric // We always spill contiguous d-registers starting from d8. Count how many 1493dff0c46cSDimitry Andric // needs spilling. The register allocator will almost always use the 1494dff0c46cSDimitry Andric // callee-saved registers in order, but it can happen that there are holes in 1495dff0c46cSDimitry Andric // the range. Registers above the hole will be spilled to the standard DPRCS 1496dff0c46cSDimitry Andric // area. 1497dff0c46cSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1498dff0c46cSDimitry Andric unsigned NumSpills = 0; 1499dff0c46cSDimitry Andric for (; NumSpills < 8; ++NumSpills) 15003861d79fSDimitry Andric if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) 1501dff0c46cSDimitry Andric break; 1502dff0c46cSDimitry Andric 1503dff0c46cSDimitry Andric // Don't do this for just one d-register. It's not worth it. 1504dff0c46cSDimitry Andric if (NumSpills < 2) 1505dff0c46cSDimitry Andric return; 1506dff0c46cSDimitry Andric 1507dff0c46cSDimitry Andric // Spill the first NumSpills D-registers after realigning the stack. 1508dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); 1509dff0c46cSDimitry Andric 1510dff0c46cSDimitry Andric // A scratch register is required for the vst1 / vld1 instructions. 1511dff0c46cSDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 1512dff0c46cSDimitry Andric } 1513dff0c46cSDimitry Andric 15142754fe60SDimitry Andric void 15152754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 15162754fe60SDimitry Andric RegScavenger *RS) const { 15172754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 15182754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 15192754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 15202754fe60SDimitry Andric // to combine multiple loads / stores. 15212754fe60SDimitry Andric bool CanEliminateFrame = true; 15222754fe60SDimitry Andric bool CS1Spilled = false; 15232754fe60SDimitry Andric bool LRSpilled = false; 15242754fe60SDimitry Andric unsigned NumGPRSpills = 0; 15252754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 15262754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 152739d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 152839d628a0SDimitry Andric MF.getSubtarget().getRegisterInfo()); 15292754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 153039d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 15312754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 15322754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 15333861d79fSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 15342754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 15352754fe60SDimitry Andric 15362754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 15372754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 15382754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 15392754fe60SDimitry Andric // instruction. 15402754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 15412754fe60SDimitry Andric if (AFI->isThumb2Function() && 15422754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 15433861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 15442754fe60SDimitry Andric 15452754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 15462754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 1547284c1978SDimitry Andric if (AFI->getArgRegsSaveSize() > 0) 15483861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 15492754fe60SDimitry Andric 155017a519f9SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 155117a519f9SDimitry Andric // for sure what the stack size will be, but for this, an estimate is good 155217a519f9SDimitry Andric // enough. If there anything changes it, it'll be a spill, which implies 155317a519f9SDimitry Andric // we've used all the registers and so R4 is already used, so not marking 155417a519f9SDimitry Andric // it here will be OK. 15552754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 1556139f7f9bSDimitry Andric unsigned StackSize = MFI->estimateStackSize(MF); 155717a519f9SDimitry Andric if (MFI->hasVarSizedObjects() || StackSize > 508) 15583861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 15592754fe60SDimitry Andric } 15602754fe60SDimitry Andric 1561dff0c46cSDimitry Andric // See if we can spill vector registers to aligned stack. 1562dff0c46cSDimitry Andric checkNumAlignedDPRCS2Regs(MF); 1563dff0c46cSDimitry Andric 15642754fe60SDimitry Andric // Spill the BasePtr if it's used. 15652754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 15663861d79fSDimitry Andric MRI.setPhysRegUsed(RegInfo->getBaseRegister()); 15672754fe60SDimitry Andric 15682754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 15692754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 157091bc56edSDimitry Andric const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 15712754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 15722754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 15732754fe60SDimitry Andric bool Spilled = false; 15743861d79fSDimitry Andric if (MRI.isPhysRegUsed(Reg)) { 15752754fe60SDimitry Andric Spilled = true; 15762754fe60SDimitry Andric CanEliminateFrame = false; 15772754fe60SDimitry Andric } 15782754fe60SDimitry Andric 15797ae0e2c9SDimitry Andric if (!ARM::GPRRegClass.contains(Reg)) 15802754fe60SDimitry Andric continue; 15812754fe60SDimitry Andric 15822754fe60SDimitry Andric if (Spilled) { 15832754fe60SDimitry Andric NumGPRSpills++; 15842754fe60SDimitry Andric 158591bc56edSDimitry Andric if (!STI.isTargetDarwin()) { 15862754fe60SDimitry Andric if (Reg == ARM::LR) 15872754fe60SDimitry Andric LRSpilled = true; 15882754fe60SDimitry Andric CS1Spilled = true; 15892754fe60SDimitry Andric continue; 15902754fe60SDimitry Andric } 15912754fe60SDimitry Andric 15922754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 15932754fe60SDimitry Andric switch (Reg) { 15942754fe60SDimitry Andric case ARM::LR: 15952754fe60SDimitry Andric LRSpilled = true; 15962754fe60SDimitry Andric // Fallthrough 1597f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1598f785676fSDimitry Andric case ARM::R2: case ARM::R3: 15992754fe60SDimitry Andric case ARM::R4: case ARM::R5: 16002754fe60SDimitry Andric case ARM::R6: case ARM::R7: 16012754fe60SDimitry Andric CS1Spilled = true; 16022754fe60SDimitry Andric break; 16032754fe60SDimitry Andric default: 16042754fe60SDimitry Andric break; 16052754fe60SDimitry Andric } 16062754fe60SDimitry Andric } else { 160791bc56edSDimitry Andric if (!STI.isTargetDarwin()) { 16082754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 16092754fe60SDimitry Andric continue; 16102754fe60SDimitry Andric } 16112754fe60SDimitry Andric 16122754fe60SDimitry Andric switch (Reg) { 1613f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1614f785676fSDimitry Andric case ARM::R2: case ARM::R3: 16152754fe60SDimitry Andric case ARM::R4: case ARM::R5: 16162754fe60SDimitry Andric case ARM::R6: case ARM::R7: 16172754fe60SDimitry Andric case ARM::LR: 16182754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 16192754fe60SDimitry Andric break; 16202754fe60SDimitry Andric default: 16212754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 16222754fe60SDimitry Andric break; 16232754fe60SDimitry Andric } 16242754fe60SDimitry Andric } 16252754fe60SDimitry Andric } 16262754fe60SDimitry Andric 16272754fe60SDimitry Andric bool ForceLRSpill = false; 16282754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 16292754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 16302754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 16312754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 16322754fe60SDimitry Andric // then the branch fix up path will undo it. 16332754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 16342754fe60SDimitry Andric CanEliminateFrame = false; 16352754fe60SDimitry Andric ForceLRSpill = true; 16362754fe60SDimitry Andric } 16372754fe60SDimitry Andric } 16382754fe60SDimitry Andric 16392754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 16402754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 16412754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 16422754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 16432754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 16442754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 16452754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 16462754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 16472754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 16482754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 16492754fe60SDimitry Andric // calculations may be off, so be conservative. 16502754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 16512754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 16522754fe60SDimitry Andric // worth the effort and added fragility? 16532754fe60SDimitry Andric bool BigStack = 16542754fe60SDimitry Andric (RS && 1655139f7f9bSDimitry Andric (MFI->estimateStackSize(MF) + 1656139f7f9bSDimitry Andric ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 16572754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 16582754fe60SDimitry Andric || MFI->hasVarSizedObjects() 16592754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 16602754fe60SDimitry Andric 16612754fe60SDimitry Andric bool ExtraCSSpill = false; 16622754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 16632754fe60SDimitry Andric AFI->setHasStackFrame(true); 16642754fe60SDimitry Andric 16652754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 16662754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 16672754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 16683861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 16692754fe60SDimitry Andric NumGPRSpills++; 1670f785676fSDimitry Andric SmallVectorImpl<unsigned>::iterator LRPos; 1671f785676fSDimitry Andric LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), 1672f785676fSDimitry Andric (unsigned)ARM::LR); 1673f785676fSDimitry Andric if (LRPos != UnspilledCS1GPRs.end()) 1674f785676fSDimitry Andric UnspilledCS1GPRs.erase(LRPos); 1675f785676fSDimitry Andric 16762754fe60SDimitry Andric ForceLRSpill = false; 16772754fe60SDimitry Andric ExtraCSSpill = true; 16782754fe60SDimitry Andric } 16792754fe60SDimitry Andric 16802754fe60SDimitry Andric if (hasFP(MF)) { 16813861d79fSDimitry Andric MRI.setPhysRegUsed(FramePtr); 168291bc56edSDimitry Andric auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), 168391bc56edSDimitry Andric FramePtr); 168491bc56edSDimitry Andric if (FPPos != UnspilledCS1GPRs.end()) 168591bc56edSDimitry Andric UnspilledCS1GPRs.erase(FPPos); 16862754fe60SDimitry Andric NumGPRSpills++; 16872754fe60SDimitry Andric } 16882754fe60SDimitry Andric 16892754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 16902754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 16912754fe60SDimitry Andric // the integer and double callee save areas. 16922754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 169339d628a0SDimitry Andric if (TargetAlign >= 8 && (NumGPRSpills & 1)) { 16942754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 16952754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 16962754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 16972754fe60SDimitry Andric // Don't spill high register if the function is thumb1 16982754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction() || 16992754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 17003861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 17013861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 17022754fe60SDimitry Andric ExtraCSSpill = true; 17032754fe60SDimitry Andric break; 17042754fe60SDimitry Andric } 17052754fe60SDimitry Andric } 17062754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 17072754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 17083861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 17093861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 17102754fe60SDimitry Andric ExtraCSSpill = true; 17112754fe60SDimitry Andric } 17122754fe60SDimitry Andric } 17132754fe60SDimitry Andric 17142754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 17152754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 17162754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 17172754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 17182754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 17192754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 17202754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 17212754fe60SDimitry Andric // extra. That should take care of it! 17222754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 17232754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 17242754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 17252754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 17262754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 17273861d79fSDimitry Andric if (!MRI.isReserved(Reg) && 17282754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 17292754fe60SDimitry Andric Reg == ARM::LR)) { 17302754fe60SDimitry Andric Extras.push_back(Reg); 17312754fe60SDimitry Andric NumExtras--; 17322754fe60SDimitry Andric } 17332754fe60SDimitry Andric } 17342754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 17352754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 17362754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 17372754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 17382754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 17393861d79fSDimitry Andric if (!MRI.isReserved(Reg)) { 17402754fe60SDimitry Andric Extras.push_back(Reg); 17412754fe60SDimitry Andric NumExtras--; 17422754fe60SDimitry Andric } 17432754fe60SDimitry Andric } 17442754fe60SDimitry Andric } 17452754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 17462754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 17473861d79fSDimitry Andric MRI.setPhysRegUsed(Extras[i]); 17482754fe60SDimitry Andric } 17492754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 17502754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 17512754fe60SDimitry Andric // closest to SP or frame pointer. 17527ae0e2c9SDimitry Andric const TargetRegisterClass *RC = &ARM::GPRRegClass; 1753139f7f9bSDimitry Andric RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 17542754fe60SDimitry Andric RC->getAlignment(), 17552754fe60SDimitry Andric false)); 17562754fe60SDimitry Andric } 17572754fe60SDimitry Andric } 17582754fe60SDimitry Andric } 17592754fe60SDimitry Andric 17602754fe60SDimitry Andric if (ForceLRSpill) { 17613861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 17622754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 17632754fe60SDimitry Andric } 17642754fe60SDimitry Andric } 1765139f7f9bSDimitry Andric 1766139f7f9bSDimitry Andric 1767139f7f9bSDimitry Andric void ARMFrameLowering:: 1768139f7f9bSDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1769139f7f9bSDimitry Andric MachineBasicBlock::iterator I) const { 1770139f7f9bSDimitry Andric const ARMBaseInstrInfo &TII = 177139d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 1772139f7f9bSDimitry Andric if (!hasReservedCallFrame(MF)) { 1773139f7f9bSDimitry Andric // If we have alloca, convert as follows: 1774139f7f9bSDimitry Andric // ADJCALLSTACKDOWN -> sub, sp, sp, amount 1775139f7f9bSDimitry Andric // ADJCALLSTACKUP -> add, sp, sp, amount 1776139f7f9bSDimitry Andric MachineInstr *Old = I; 1777139f7f9bSDimitry Andric DebugLoc dl = Old->getDebugLoc(); 1778139f7f9bSDimitry Andric unsigned Amount = Old->getOperand(0).getImm(); 1779139f7f9bSDimitry Andric if (Amount != 0) { 1780139f7f9bSDimitry Andric // We need to keep the stack aligned properly. To do this, we round the 1781139f7f9bSDimitry Andric // amount of space needed for the outgoing arguments up to the next 1782139f7f9bSDimitry Andric // alignment boundary. 1783139f7f9bSDimitry Andric unsigned Align = getStackAlignment(); 1784139f7f9bSDimitry Andric Amount = (Amount+Align-1)/Align*Align; 1785139f7f9bSDimitry Andric 1786139f7f9bSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1787139f7f9bSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1788139f7f9bSDimitry Andric "This eliminateCallFramePseudoInstr does not support Thumb1!"); 1789139f7f9bSDimitry Andric bool isARM = !AFI->isThumbFunction(); 1790139f7f9bSDimitry Andric 1791139f7f9bSDimitry Andric // Replace the pseudo instruction with a new instruction... 1792139f7f9bSDimitry Andric unsigned Opc = Old->getOpcode(); 1793139f7f9bSDimitry Andric int PIdx = Old->findFirstPredOperandIdx(); 1794139f7f9bSDimitry Andric ARMCC::CondCodes Pred = (PIdx == -1) 1795139f7f9bSDimitry Andric ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); 1796139f7f9bSDimitry Andric if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { 1797139f7f9bSDimitry Andric // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. 1798139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(2).getReg(); 1799139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, 1800139f7f9bSDimitry Andric Pred, PredReg); 1801139f7f9bSDimitry Andric } else { 1802139f7f9bSDimitry Andric // Note: PredReg is operand 3 for ADJCALLSTACKUP. 1803139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(3).getReg(); 1804139f7f9bSDimitry Andric assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); 1805139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, 1806139f7f9bSDimitry Andric Pred, PredReg); 1807139f7f9bSDimitry Andric } 1808139f7f9bSDimitry Andric } 1809139f7f9bSDimitry Andric } 1810139f7f9bSDimitry Andric MBB.erase(I); 1811139f7f9bSDimitry Andric } 1812139f7f9bSDimitry Andric 181391bc56edSDimitry Andric /// Get the minimum constant for ARM that is greater than or equal to the 181491bc56edSDimitry Andric /// argument. In ARM, constants can have any value that can be produced by 181591bc56edSDimitry Andric /// rotating an 8-bit value to the right by an even number of bits within a 181691bc56edSDimitry Andric /// 32-bit word. 181791bc56edSDimitry Andric static uint32_t alignToARMConstant(uint32_t Value) { 181891bc56edSDimitry Andric unsigned Shifted = 0; 181991bc56edSDimitry Andric 182091bc56edSDimitry Andric if (Value == 0) 182191bc56edSDimitry Andric return 0; 182291bc56edSDimitry Andric 182391bc56edSDimitry Andric while (!(Value & 0xC0000000)) { 182491bc56edSDimitry Andric Value = Value << 2; 182591bc56edSDimitry Andric Shifted += 2; 182691bc56edSDimitry Andric } 182791bc56edSDimitry Andric 182891bc56edSDimitry Andric bool Carry = (Value & 0x00FFFFFF); 182991bc56edSDimitry Andric Value = ((Value & 0xFF000000) >> 24) + Carry; 183091bc56edSDimitry Andric 183191bc56edSDimitry Andric if (Value & 0x0000100) 183291bc56edSDimitry Andric Value = Value & 0x000001FC; 183391bc56edSDimitry Andric 183491bc56edSDimitry Andric if (Shifted > 24) 183591bc56edSDimitry Andric Value = Value >> (Shifted - 24); 183691bc56edSDimitry Andric else 183791bc56edSDimitry Andric Value = Value << (24 - Shifted); 183891bc56edSDimitry Andric 183991bc56edSDimitry Andric return Value; 184091bc56edSDimitry Andric } 184191bc56edSDimitry Andric 184291bc56edSDimitry Andric // The stack limit in the TCB is set to this many bytes above the actual 184391bc56edSDimitry Andric // stack limit. 184491bc56edSDimitry Andric static const uint64_t kSplitStackAvailable = 256; 184591bc56edSDimitry Andric 184691bc56edSDimitry Andric // Adjust the function prologue to enable split stacks. This currently only 184791bc56edSDimitry Andric // supports android and linux. 184891bc56edSDimitry Andric // 184991bc56edSDimitry Andric // The ABI of the segmented stack prologue is a little arbitrarily chosen, but 185091bc56edSDimitry Andric // must be well defined in order to allow for consistent implementations of the 185191bc56edSDimitry Andric // __morestack helper function. The ABI is also not a normal ABI in that it 185291bc56edSDimitry Andric // doesn't follow the normal calling conventions because this allows the 185391bc56edSDimitry Andric // prologue of each function to be optimized further. 185491bc56edSDimitry Andric // 185591bc56edSDimitry Andric // Currently, the ABI looks like (when calling __morestack) 185691bc56edSDimitry Andric // 185791bc56edSDimitry Andric // * r4 holds the minimum stack size requested for this function call 185891bc56edSDimitry Andric // * r5 holds the stack size of the arguments to the function 185991bc56edSDimitry Andric // * the beginning of the function is 3 instructions after the call to 186091bc56edSDimitry Andric // __morestack 186191bc56edSDimitry Andric // 186291bc56edSDimitry Andric // Implementations of __morestack should use r4 to allocate a new stack, r5 to 186391bc56edSDimitry Andric // place the arguments on to the new stack, and the 3-instruction knowledge to 186491bc56edSDimitry Andric // jump directly to the body of the function when working on the new stack. 186591bc56edSDimitry Andric // 186691bc56edSDimitry Andric // An old (and possibly no longer compatible) implementation of __morestack for 186791bc56edSDimitry Andric // ARM can be found at [1]. 186891bc56edSDimitry Andric // 186991bc56edSDimitry Andric // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S 187091bc56edSDimitry Andric void ARMFrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const { 187191bc56edSDimitry Andric unsigned Opcode; 187291bc56edSDimitry Andric unsigned CFIIndex; 187391bc56edSDimitry Andric const ARMSubtarget *ST = &MF.getTarget().getSubtarget<ARMSubtarget>(); 187491bc56edSDimitry Andric bool Thumb = ST->isThumb(); 187591bc56edSDimitry Andric 187691bc56edSDimitry Andric // Sadly, this currently doesn't support varargs, platforms other than 187791bc56edSDimitry Andric // android/linux. Note that thumb1/thumb2 are support for android/linux. 187891bc56edSDimitry Andric if (MF.getFunction()->isVarArg()) 187991bc56edSDimitry Andric report_fatal_error("Segmented stacks do not support vararg functions."); 188091bc56edSDimitry Andric if (!ST->isTargetAndroid() && !ST->isTargetLinux()) 188191bc56edSDimitry Andric report_fatal_error("Segmented stacks not supported on this platform."); 188291bc56edSDimitry Andric 188391bc56edSDimitry Andric MachineBasicBlock &prologueMBB = MF.front(); 188491bc56edSDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 188591bc56edSDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 188691bc56edSDimitry Andric MCContext &Context = MMI.getContext(); 188791bc56edSDimitry Andric const MCRegisterInfo *MRI = Context.getRegisterInfo(); 188891bc56edSDimitry Andric const ARMBaseInstrInfo &TII = 188939d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 189091bc56edSDimitry Andric ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>(); 189191bc56edSDimitry Andric DebugLoc DL; 189291bc56edSDimitry Andric 189391bc56edSDimitry Andric uint64_t StackSize = MFI->getStackSize(); 189491bc56edSDimitry Andric 189591bc56edSDimitry Andric // Do not generate a prologue for functions with a stack of size zero 189691bc56edSDimitry Andric if (StackSize == 0) 189791bc56edSDimitry Andric return; 189891bc56edSDimitry Andric 189991bc56edSDimitry Andric // Use R4 and R5 as scratch registers. 190091bc56edSDimitry Andric // We save R4 and R5 before use and restore them before leaving the function. 190191bc56edSDimitry Andric unsigned ScratchReg0 = ARM::R4; 190291bc56edSDimitry Andric unsigned ScratchReg1 = ARM::R5; 190391bc56edSDimitry Andric uint64_t AlignedStackSize; 190491bc56edSDimitry Andric 190591bc56edSDimitry Andric MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock(); 190691bc56edSDimitry Andric MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock(); 190791bc56edSDimitry Andric MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock(); 190891bc56edSDimitry Andric MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock(); 190991bc56edSDimitry Andric MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock(); 191091bc56edSDimitry Andric 191191bc56edSDimitry Andric for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(), 191291bc56edSDimitry Andric e = prologueMBB.livein_end(); 191391bc56edSDimitry Andric i != e; ++i) { 191491bc56edSDimitry Andric AllocMBB->addLiveIn(*i); 191591bc56edSDimitry Andric GetMBB->addLiveIn(*i); 191691bc56edSDimitry Andric McrMBB->addLiveIn(*i); 191791bc56edSDimitry Andric PrevStackMBB->addLiveIn(*i); 191891bc56edSDimitry Andric PostStackMBB->addLiveIn(*i); 191991bc56edSDimitry Andric } 192091bc56edSDimitry Andric 192191bc56edSDimitry Andric MF.push_front(PostStackMBB); 192291bc56edSDimitry Andric MF.push_front(AllocMBB); 192391bc56edSDimitry Andric MF.push_front(GetMBB); 192491bc56edSDimitry Andric MF.push_front(McrMBB); 192591bc56edSDimitry Andric MF.push_front(PrevStackMBB); 192691bc56edSDimitry Andric 192791bc56edSDimitry Andric // The required stack size that is aligned to ARM constant criterion. 192891bc56edSDimitry Andric AlignedStackSize = alignToARMConstant(StackSize); 192991bc56edSDimitry Andric 193091bc56edSDimitry Andric // When the frame size is less than 256 we just compare the stack 193191bc56edSDimitry Andric // boundary directly to the value of the stack pointer, per gcc. 193291bc56edSDimitry Andric bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable; 193391bc56edSDimitry Andric 193491bc56edSDimitry Andric // We will use two of the callee save registers as scratch registers so we 193591bc56edSDimitry Andric // need to save those registers onto the stack. 193691bc56edSDimitry Andric // We will use SR0 to hold stack limit and SR1 to hold the stack size 193791bc56edSDimitry Andric // requested and arguments for __morestack(). 193891bc56edSDimitry Andric // SR0: Scratch Register #0 193991bc56edSDimitry Andric // SR1: Scratch Register #1 194091bc56edSDimitry Andric // push {SR0, SR1} 194191bc56edSDimitry Andric if (Thumb) { 194291bc56edSDimitry Andric AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))) 194391bc56edSDimitry Andric .addReg(ScratchReg0).addReg(ScratchReg1); 194491bc56edSDimitry Andric } else { 194591bc56edSDimitry Andric AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD)) 194691bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define).addReg(ARM::SP)) 194791bc56edSDimitry Andric .addReg(ScratchReg0).addReg(ScratchReg1); 194891bc56edSDimitry Andric } 194991bc56edSDimitry Andric 195091bc56edSDimitry Andric // Emit the relevant DWARF information about the change in stack pointer as 195191bc56edSDimitry Andric // well as where to find both r4 and r5 (the callee-save registers) 195291bc56edSDimitry Andric CFIIndex = 195391bc56edSDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8)); 195491bc56edSDimitry Andric BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 195591bc56edSDimitry Andric .addCFIIndex(CFIIndex); 195691bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 195791bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4)); 195891bc56edSDimitry Andric BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 195991bc56edSDimitry Andric .addCFIIndex(CFIIndex); 196091bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 196191bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8)); 196291bc56edSDimitry Andric BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 196391bc56edSDimitry Andric .addCFIIndex(CFIIndex); 196491bc56edSDimitry Andric 196591bc56edSDimitry Andric // mov SR1, sp 196691bc56edSDimitry Andric if (Thumb) { 196791bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1) 196891bc56edSDimitry Andric .addReg(ARM::SP)); 196991bc56edSDimitry Andric } else if (CompareStackPointer) { 197091bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1) 197191bc56edSDimitry Andric .addReg(ARM::SP)).addReg(0); 197291bc56edSDimitry Andric } 197391bc56edSDimitry Andric 197491bc56edSDimitry Andric // sub SR1, sp, #StackSize 197591bc56edSDimitry Andric if (!CompareStackPointer && Thumb) { 197691bc56edSDimitry Andric AddDefaultPred( 197791bc56edSDimitry Andric AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)) 197891bc56edSDimitry Andric .addReg(ScratchReg1).addImm(AlignedStackSize)); 197991bc56edSDimitry Andric } else if (!CompareStackPointer) { 198091bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1) 198191bc56edSDimitry Andric .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0); 198291bc56edSDimitry Andric } 198391bc56edSDimitry Andric 198491bc56edSDimitry Andric if (Thumb && ST->isThumb1Only()) { 198591bc56edSDimitry Andric unsigned PCLabelId = ARMFI->createPICLabelUId(); 198691bc56edSDimitry Andric ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create( 198791bc56edSDimitry Andric MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0); 198891bc56edSDimitry Andric MachineConstantPool *MCP = MF.getConstantPool(); 198991bc56edSDimitry Andric unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment()); 199091bc56edSDimitry Andric 199191bc56edSDimitry Andric // ldr SR0, [pc, offset(STACK_LIMIT)] 199291bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0) 199391bc56edSDimitry Andric .addConstantPoolIndex(CPI)); 199491bc56edSDimitry Andric 199591bc56edSDimitry Andric // ldr SR0, [SR0] 199691bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0) 199791bc56edSDimitry Andric .addReg(ScratchReg0).addImm(0)); 199891bc56edSDimitry Andric } else { 199991bc56edSDimitry Andric // Get TLS base address from the coprocessor 200091bc56edSDimitry Andric // mrc p15, #0, SR0, c13, c0, #3 200191bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0) 200291bc56edSDimitry Andric .addImm(15) 200391bc56edSDimitry Andric .addImm(0) 200491bc56edSDimitry Andric .addImm(13) 200591bc56edSDimitry Andric .addImm(0) 200691bc56edSDimitry Andric .addImm(3)); 200791bc56edSDimitry Andric 200891bc56edSDimitry Andric // Use the last tls slot on android and a private field of the TCP on linux. 200991bc56edSDimitry Andric assert(ST->isTargetAndroid() || ST->isTargetLinux()); 201091bc56edSDimitry Andric unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1; 201191bc56edSDimitry Andric 201291bc56edSDimitry Andric // Get the stack limit from the right offset 201391bc56edSDimitry Andric // ldr SR0, [sr0, #4 * TlsOffset] 201491bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0) 201591bc56edSDimitry Andric .addReg(ScratchReg0).addImm(4 * TlsOffset)); 201691bc56edSDimitry Andric } 201791bc56edSDimitry Andric 201891bc56edSDimitry Andric // Compare stack limit with stack size requested. 201991bc56edSDimitry Andric // cmp SR0, SR1 202091bc56edSDimitry Andric Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr; 202191bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode)) 202291bc56edSDimitry Andric .addReg(ScratchReg0) 202391bc56edSDimitry Andric .addReg(ScratchReg1)); 202491bc56edSDimitry Andric 202591bc56edSDimitry Andric // This jump is taken if StackLimit < SP - stack required. 202691bc56edSDimitry Andric Opcode = Thumb ? ARM::tBcc : ARM::Bcc; 202791bc56edSDimitry Andric BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB) 202891bc56edSDimitry Andric .addImm(ARMCC::LO) 202991bc56edSDimitry Andric .addReg(ARM::CPSR); 203091bc56edSDimitry Andric 203191bc56edSDimitry Andric 203291bc56edSDimitry Andric // Calling __morestack(StackSize, Size of stack arguments). 203391bc56edSDimitry Andric // __morestack knows that the stack size requested is in SR0(r4) 203491bc56edSDimitry Andric // and amount size of stack arguments is in SR1(r5). 203591bc56edSDimitry Andric 203691bc56edSDimitry Andric // Pass first argument for the __morestack by Scratch Register #0. 203791bc56edSDimitry Andric // The amount size of stack required 203891bc56edSDimitry Andric if (Thumb) { 203991bc56edSDimitry Andric AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), 204091bc56edSDimitry Andric ScratchReg0)).addImm(AlignedStackSize)); 204191bc56edSDimitry Andric } else { 204291bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0) 204391bc56edSDimitry Andric .addImm(AlignedStackSize)).addReg(0); 204491bc56edSDimitry Andric } 204591bc56edSDimitry Andric // Pass second argument for the __morestack by Scratch Register #1. 204691bc56edSDimitry Andric // The amount size of stack consumed to save function arguments. 204791bc56edSDimitry Andric if (Thumb) { 204891bc56edSDimitry Andric AddDefaultPred( 204991bc56edSDimitry Andric AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)) 205091bc56edSDimitry Andric .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))); 205191bc56edSDimitry Andric } else { 205291bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1) 205391bc56edSDimitry Andric .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))) 205491bc56edSDimitry Andric .addReg(0); 205591bc56edSDimitry Andric } 205691bc56edSDimitry Andric 205791bc56edSDimitry Andric // push {lr} - Save return address of this function. 205891bc56edSDimitry Andric if (Thumb) { 205991bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))) 206091bc56edSDimitry Andric .addReg(ARM::LR); 206191bc56edSDimitry Andric } else { 206291bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD)) 206391bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 206491bc56edSDimitry Andric .addReg(ARM::SP)) 206591bc56edSDimitry Andric .addReg(ARM::LR); 206691bc56edSDimitry Andric } 206791bc56edSDimitry Andric 206891bc56edSDimitry Andric // Emit the DWARF info about the change in stack as well as where to find the 206991bc56edSDimitry Andric // previous link register 207091bc56edSDimitry Andric CFIIndex = 207191bc56edSDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12)); 207291bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 207391bc56edSDimitry Andric .addCFIIndex(CFIIndex); 207491bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 207591bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12)); 207691bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 207791bc56edSDimitry Andric .addCFIIndex(CFIIndex); 207891bc56edSDimitry Andric 207991bc56edSDimitry Andric // Call __morestack(). 208091bc56edSDimitry Andric if (Thumb) { 208191bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL))) 208291bc56edSDimitry Andric .addExternalSymbol("__morestack"); 208391bc56edSDimitry Andric } else { 208491bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(ARM::BL)) 208591bc56edSDimitry Andric .addExternalSymbol("__morestack"); 208691bc56edSDimitry Andric } 208791bc56edSDimitry Andric 208891bc56edSDimitry Andric // pop {lr} - Restore return address of this original function. 208991bc56edSDimitry Andric if (Thumb) { 209091bc56edSDimitry Andric if (ST->isThumb1Only()) { 209191bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) 209291bc56edSDimitry Andric .addReg(ScratchReg0); 209391bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR) 209491bc56edSDimitry Andric .addReg(ScratchReg0)); 209591bc56edSDimitry Andric } else { 209691bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST)) 209791bc56edSDimitry Andric .addReg(ARM::LR, RegState::Define) 209891bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 209991bc56edSDimitry Andric .addReg(ARM::SP) 210091bc56edSDimitry Andric .addImm(4)); 210191bc56edSDimitry Andric } 210291bc56edSDimitry Andric } else { 210391bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) 210491bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 210591bc56edSDimitry Andric .addReg(ARM::SP)) 210691bc56edSDimitry Andric .addReg(ARM::LR); 210791bc56edSDimitry Andric } 210891bc56edSDimitry Andric 210991bc56edSDimitry Andric // Restore SR0 and SR1 in case of __morestack() was called. 211091bc56edSDimitry Andric // __morestack() will skip PostStackMBB block so we need to restore 211191bc56edSDimitry Andric // scratch registers from here. 211291bc56edSDimitry Andric // pop {SR0, SR1} 211391bc56edSDimitry Andric if (Thumb) { 211491bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) 211591bc56edSDimitry Andric .addReg(ScratchReg0) 211691bc56edSDimitry Andric .addReg(ScratchReg1); 211791bc56edSDimitry Andric } else { 211891bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) 211991bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 212091bc56edSDimitry Andric .addReg(ARM::SP)) 212191bc56edSDimitry Andric .addReg(ScratchReg0) 212291bc56edSDimitry Andric .addReg(ScratchReg1); 212391bc56edSDimitry Andric } 212491bc56edSDimitry Andric 212591bc56edSDimitry Andric // Update the CFA offset now that we've popped 212691bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); 212791bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 212891bc56edSDimitry Andric .addCFIIndex(CFIIndex); 212991bc56edSDimitry Andric 213091bc56edSDimitry Andric // bx lr - Return from this function. 213191bc56edSDimitry Andric Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET; 213291bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode))); 213391bc56edSDimitry Andric 213491bc56edSDimitry Andric // Restore SR0 and SR1 in case of __morestack() was not called. 213591bc56edSDimitry Andric // pop {SR0, SR1} 213691bc56edSDimitry Andric if (Thumb) { 213791bc56edSDimitry Andric AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))) 213891bc56edSDimitry Andric .addReg(ScratchReg0) 213991bc56edSDimitry Andric .addReg(ScratchReg1); 214091bc56edSDimitry Andric } else { 214191bc56edSDimitry Andric AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD)) 214291bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 214391bc56edSDimitry Andric .addReg(ARM::SP)) 214491bc56edSDimitry Andric .addReg(ScratchReg0) 214591bc56edSDimitry Andric .addReg(ScratchReg1); 214691bc56edSDimitry Andric } 214791bc56edSDimitry Andric 214891bc56edSDimitry Andric // Update the CFA offset now that we've popped 214991bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); 215091bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 215191bc56edSDimitry Andric .addCFIIndex(CFIIndex); 215291bc56edSDimitry Andric 215391bc56edSDimitry Andric // Tell debuggers that r4 and r5 are now the same as they were in the 215491bc56edSDimitry Andric // previous function, that they're the "Same Value". 215591bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( 215691bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg0, true))); 215791bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 215891bc56edSDimitry Andric .addCFIIndex(CFIIndex); 215991bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( 216091bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg1, true))); 216191bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 216291bc56edSDimitry Andric .addCFIIndex(CFIIndex); 216391bc56edSDimitry Andric 216491bc56edSDimitry Andric // Organizing MBB lists 216591bc56edSDimitry Andric PostStackMBB->addSuccessor(&prologueMBB); 216691bc56edSDimitry Andric 216791bc56edSDimitry Andric AllocMBB->addSuccessor(PostStackMBB); 216891bc56edSDimitry Andric 216991bc56edSDimitry Andric GetMBB->addSuccessor(PostStackMBB); 217091bc56edSDimitry Andric GetMBB->addSuccessor(AllocMBB); 217191bc56edSDimitry Andric 217291bc56edSDimitry Andric McrMBB->addSuccessor(GetMBB); 217391bc56edSDimitry Andric 217491bc56edSDimitry Andric PrevStackMBB->addSuccessor(McrMBB); 217591bc56edSDimitry Andric 217691bc56edSDimitry Andric #ifdef XDEBUG 217791bc56edSDimitry Andric MF.verify(); 217891bc56edSDimitry Andric #endif 217991bc56edSDimitry Andric } 2180