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 46ff0cc061SDimitry Andric bool ARMFrameLowering::noFramePointerElim(const MachineFunction &MF) const { 47ff0cc061SDimitry Andric // iOS always has a FP for backtracking, force other targets to keep their FP 48ff0cc061SDimitry Andric // when doing FastISel. The emitted code is currently superior, and in cases 49ff0cc061SDimitry Andric // like test-suite's lencod FastISel isn't quite correct when FP is eliminated. 50ff0cc061SDimitry Andric return TargetFrameLowering::noFramePointerElim(MF) || 51ff0cc061SDimitry Andric MF.getSubtarget<ARMSubtarget>().useFastISel(); 52ff0cc061SDimitry Andric } 53ff0cc061SDimitry Andric 542754fe60SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 552754fe60SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 562754fe60SDimitry Andric /// or if frame pointer elimination is disabled. 572754fe60SDimitry Andric bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 5839d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 592754fe60SDimitry Andric 60dff0c46cSDimitry Andric // iOS requires FP not to be clobbered for backtracing purpose. 61dff0c46cSDimitry Andric if (STI.isTargetIOS()) 622754fe60SDimitry Andric return true; 632754fe60SDimitry Andric 642754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 652754fe60SDimitry Andric // Always eliminate non-leaf frame pointers. 66dff0c46cSDimitry Andric return ((MF.getTarget().Options.DisableFramePointerElim(MF) && 67dff0c46cSDimitry Andric MFI->hasCalls()) || 682754fe60SDimitry Andric RegInfo->needsStackRealignment(MF) || 692754fe60SDimitry Andric MFI->hasVarSizedObjects() || 702754fe60SDimitry Andric MFI->isFrameAddressTaken()); 712754fe60SDimitry Andric } 722754fe60SDimitry Andric 732754fe60SDimitry Andric /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 742754fe60SDimitry Andric /// not required, we reserve argument space for call sites in the function 752754fe60SDimitry Andric /// immediately on entry to the current function. This eliminates the need for 762754fe60SDimitry Andric /// add/sub sp brackets around call sites. Returns true if the call frame is 772754fe60SDimitry Andric /// included as part of the stack frame. 782754fe60SDimitry Andric bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 792754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 802754fe60SDimitry Andric unsigned CFSize = FFI->getMaxCallFrameSize(); 812754fe60SDimitry Andric // It's not always a good idea to include the call frame as part of the 822754fe60SDimitry Andric // stack frame. ARM (especially Thumb) has small immediate offset to 832754fe60SDimitry Andric // address the stack frame. So a large call frame can cause poor codegen 842754fe60SDimitry Andric // and may even makes it impossible to scavenge a register. 852754fe60SDimitry Andric if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 862754fe60SDimitry Andric return false; 872754fe60SDimitry Andric 882754fe60SDimitry Andric return !MF.getFrameInfo()->hasVarSizedObjects(); 892754fe60SDimitry Andric } 902754fe60SDimitry Andric 912754fe60SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 922754fe60SDimitry Andric /// call frame pseudos can be simplified. Unlike most targets, having a FP 932754fe60SDimitry Andric /// is not sufficient here since we still may reference some objects via SP 942754fe60SDimitry Andric /// even when FP is available in Thumb2 mode. 952754fe60SDimitry Andric bool 962754fe60SDimitry Andric ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 972754fe60SDimitry Andric return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 982754fe60SDimitry Andric } 992754fe60SDimitry Andric 1002754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 1012754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 10291bc56edSDimitry Andric const MCPhysReg *CSRegs) { 1032754fe60SDimitry Andric // Integer spill area is handled with "pop". 104f785676fSDimitry Andric if (isPopOpcode(MI->getOpcode())) { 1052754fe60SDimitry Andric // The first two operands are predicates. The last two are 1062754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 1072754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 1082754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 1092754fe60SDimitry Andric return false; 1102754fe60SDimitry Andric return true; 1112754fe60SDimitry Andric } 1126122f3e6SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST_IMM || 1136122f3e6SDimitry Andric MI->getOpcode() == ARM::LDR_POST_REG || 1142754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 1152754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 1162754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1172754fe60SDimitry Andric return true; 1182754fe60SDimitry Andric 1192754fe60SDimitry Andric return false; 1202754fe60SDimitry Andric } 1212754fe60SDimitry Andric 122f785676fSDimitry Andric static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB, 123f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 124f785676fSDimitry Andric const ARMBaseInstrInfo &TII, unsigned DestReg, 125f785676fSDimitry Andric unsigned SrcReg, int NumBytes, 126f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 127f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 128f785676fSDimitry Andric unsigned PredReg = 0) { 1292754fe60SDimitry Andric if (isARM) 130f785676fSDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 131139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1322754fe60SDimitry Andric else 133f785676fSDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 134139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1352754fe60SDimitry Andric } 1362754fe60SDimitry Andric 137f785676fSDimitry Andric static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB, 138f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 139f785676fSDimitry Andric const ARMBaseInstrInfo &TII, int NumBytes, 140f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 141f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 142f785676fSDimitry Andric unsigned PredReg = 0) { 143f785676fSDimitry Andric emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes, 144f785676fSDimitry Andric MIFlags, Pred, PredReg); 145f785676fSDimitry Andric } 146f785676fSDimitry Andric 14791bc56edSDimitry Andric static int sizeOfSPAdjustment(const MachineInstr *MI) { 14839d628a0SDimitry Andric int RegSize; 14939d628a0SDimitry Andric switch (MI->getOpcode()) { 15039d628a0SDimitry Andric case ARM::VSTMDDB_UPD: 15139d628a0SDimitry Andric RegSize = 8; 15239d628a0SDimitry Andric break; 15339d628a0SDimitry Andric case ARM::STMDB_UPD: 15439d628a0SDimitry Andric case ARM::t2STMDB_UPD: 15539d628a0SDimitry Andric RegSize = 4; 15639d628a0SDimitry Andric break; 15739d628a0SDimitry Andric case ARM::t2STR_PRE: 15839d628a0SDimitry Andric case ARM::STR_PRE_IMM: 15939d628a0SDimitry Andric return 4; 16039d628a0SDimitry Andric default: 16139d628a0SDimitry Andric llvm_unreachable("Unknown push or pop like instruction"); 16239d628a0SDimitry Andric } 16339d628a0SDimitry Andric 16491bc56edSDimitry Andric int count = 0; 16591bc56edSDimitry Andric // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ 16691bc56edSDimitry Andric // pred) so the list starts at 4. 16791bc56edSDimitry Andric for (int i = MI->getNumOperands() - 1; i >= 4; --i) 16839d628a0SDimitry Andric count += RegSize; 16991bc56edSDimitry Andric return count; 17091bc56edSDimitry Andric } 17191bc56edSDimitry Andric 17291bc56edSDimitry Andric static bool WindowsRequiresStackProbe(const MachineFunction &MF, 17391bc56edSDimitry Andric size_t StackSizeInBytes) { 17491bc56edSDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 175ff0cc061SDimitry Andric const Function *F = MF.getFunction(); 176ff0cc061SDimitry Andric unsigned StackProbeSize = (MFI->getStackProtectorIndex() > 0) ? 4080 : 4096; 177ff0cc061SDimitry Andric if (F->hasFnAttribute("stack-probe-size")) 178ff0cc061SDimitry Andric F->getFnAttribute("stack-probe-size") 179ff0cc061SDimitry Andric .getValueAsString() 180ff0cc061SDimitry Andric .getAsInteger(0, StackProbeSize); 181ff0cc061SDimitry Andric return StackSizeInBytes >= StackProbeSize; 18291bc56edSDimitry Andric } 18391bc56edSDimitry Andric 18439d628a0SDimitry Andric namespace { 18539d628a0SDimitry Andric struct StackAdjustingInsts { 18639d628a0SDimitry Andric struct InstInfo { 18739d628a0SDimitry Andric MachineBasicBlock::iterator I; 18839d628a0SDimitry Andric unsigned SPAdjust; 18939d628a0SDimitry Andric bool BeforeFPSet; 19039d628a0SDimitry Andric }; 19139d628a0SDimitry Andric 19239d628a0SDimitry Andric SmallVector<InstInfo, 4> Insts; 19339d628a0SDimitry Andric 19439d628a0SDimitry Andric void addInst(MachineBasicBlock::iterator I, unsigned SPAdjust, 19539d628a0SDimitry Andric bool BeforeFPSet = false) { 19639d628a0SDimitry Andric InstInfo Info = {I, SPAdjust, BeforeFPSet}; 19739d628a0SDimitry Andric Insts.push_back(Info); 19839d628a0SDimitry Andric } 19939d628a0SDimitry Andric 20039d628a0SDimitry Andric void addExtraBytes(const MachineBasicBlock::iterator I, unsigned ExtraBytes) { 20139d628a0SDimitry Andric auto Info = std::find_if(Insts.begin(), Insts.end(), 20239d628a0SDimitry Andric [&](InstInfo &Info) { return Info.I == I; }); 20339d628a0SDimitry Andric assert(Info != Insts.end() && "invalid sp adjusting instruction"); 20439d628a0SDimitry Andric Info->SPAdjust += ExtraBytes; 20539d628a0SDimitry Andric } 20639d628a0SDimitry Andric 20739d628a0SDimitry Andric void emitDefCFAOffsets(MachineModuleInfo &MMI, MachineBasicBlock &MBB, 20839d628a0SDimitry Andric DebugLoc dl, const ARMBaseInstrInfo &TII, bool HasFP) { 20939d628a0SDimitry Andric unsigned CFAOffset = 0; 21039d628a0SDimitry Andric for (auto &Info : Insts) { 21139d628a0SDimitry Andric if (HasFP && !Info.BeforeFPSet) 21239d628a0SDimitry Andric return; 21339d628a0SDimitry Andric 21439d628a0SDimitry Andric CFAOffset -= Info.SPAdjust; 21539d628a0SDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 21639d628a0SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); 21739d628a0SDimitry Andric BuildMI(MBB, std::next(Info.I), dl, 21839d628a0SDimitry Andric TII.get(TargetOpcode::CFI_INSTRUCTION)) 21939d628a0SDimitry Andric .addCFIIndex(CFIIndex) 22039d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 22139d628a0SDimitry Andric } 22239d628a0SDimitry Andric } 22339d628a0SDimitry Andric }; 2243dac3a9bSDimitry Andric } 22539d628a0SDimitry Andric 22639d628a0SDimitry Andric /// Emit an instruction sequence that will align the address in 22739d628a0SDimitry Andric /// register Reg by zero-ing out the lower bits. For versions of the 22839d628a0SDimitry Andric /// architecture that support Neon, this must be done in a single 22939d628a0SDimitry Andric /// instruction, since skipAlignedDPRCS2Spills assumes it is done in a 23039d628a0SDimitry Andric /// single instruction. That function only gets called when optimizing 23139d628a0SDimitry Andric /// spilling of D registers on a core with the Neon instruction set 23239d628a0SDimitry Andric /// present. 23339d628a0SDimitry Andric static void emitAligningInstructions(MachineFunction &MF, ARMFunctionInfo *AFI, 23439d628a0SDimitry Andric const TargetInstrInfo &TII, 23539d628a0SDimitry Andric MachineBasicBlock &MBB, 23639d628a0SDimitry Andric MachineBasicBlock::iterator MBBI, 23739d628a0SDimitry Andric DebugLoc DL, const unsigned Reg, 23839d628a0SDimitry Andric const unsigned Alignment, 23939d628a0SDimitry Andric const bool MustBeSingleInstruction) { 240ff0cc061SDimitry Andric const ARMSubtarget &AST = 241ff0cc061SDimitry Andric static_cast<const ARMSubtarget &>(MF.getSubtarget()); 24239d628a0SDimitry Andric const bool CanUseBFC = AST.hasV6T2Ops() || AST.hasV7Ops(); 24339d628a0SDimitry Andric const unsigned AlignMask = Alignment - 1; 24439d628a0SDimitry Andric const unsigned NrBitsToZero = countTrailingZeros(Alignment); 24539d628a0SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Thumb1 not supported"); 24639d628a0SDimitry Andric if (!AFI->isThumbFunction()) { 24739d628a0SDimitry Andric // if the BFC instruction is available, use that to zero the lower 24839d628a0SDimitry Andric // bits: 24939d628a0SDimitry Andric // bfc Reg, #0, log2(Alignment) 25039d628a0SDimitry Andric // otherwise use BIC, if the mask to zero the required number of bits 25139d628a0SDimitry Andric // can be encoded in the bic immediate field 25239d628a0SDimitry Andric // bic Reg, Reg, Alignment-1 25339d628a0SDimitry Andric // otherwise, emit 25439d628a0SDimitry Andric // lsr Reg, Reg, log2(Alignment) 25539d628a0SDimitry Andric // lsl Reg, Reg, log2(Alignment) 25639d628a0SDimitry Andric if (CanUseBFC) { 25739d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BFC), Reg) 25839d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 25939d628a0SDimitry Andric .addImm(~AlignMask)); 26039d628a0SDimitry Andric } else if (AlignMask <= 255) { 26139d628a0SDimitry Andric AddDefaultCC( 26239d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::BICri), Reg) 26339d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 26439d628a0SDimitry Andric .addImm(AlignMask))); 26539d628a0SDimitry Andric } else { 26639d628a0SDimitry Andric assert(!MustBeSingleInstruction && 26739d628a0SDimitry Andric "Shouldn't call emitAligningInstructions demanding a single " 26839d628a0SDimitry Andric "instruction to be emitted for large stack alignment for a target " 26939d628a0SDimitry Andric "without BFC."); 27039d628a0SDimitry Andric AddDefaultCC(AddDefaultPred( 27139d628a0SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) 27239d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 27339d628a0SDimitry Andric .addImm(ARM_AM::getSORegOpc(ARM_AM::lsr, NrBitsToZero)))); 27439d628a0SDimitry Andric AddDefaultCC(AddDefaultPred( 27539d628a0SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(ARM::MOVsi), Reg) 27639d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 27739d628a0SDimitry Andric .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, NrBitsToZero)))); 27839d628a0SDimitry Andric } 27939d628a0SDimitry Andric } else { 28039d628a0SDimitry Andric // Since this is only reached for Thumb-2 targets, the BFC instruction 28139d628a0SDimitry Andric // should always be available. 28239d628a0SDimitry Andric assert(CanUseBFC); 28339d628a0SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, DL, TII.get(ARM::t2BFC), Reg) 28439d628a0SDimitry Andric .addReg(Reg, RegState::Kill) 28539d628a0SDimitry Andric .addImm(~AlignMask)); 28639d628a0SDimitry Andric } 28739d628a0SDimitry Andric } 28839d628a0SDimitry Andric 289ff0cc061SDimitry Andric void ARMFrameLowering::emitPrologue(MachineFunction &MF, 290ff0cc061SDimitry Andric MachineBasicBlock &MBB) const { 291ff0cc061SDimitry Andric assert(&MBB == &MF.front() && "Shrink-wrapping not yet implemented"); 2922754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 2932754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 2942754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 29591bc56edSDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 29691bc56edSDimitry Andric MCContext &Context = MMI.getContext(); 29791bc56edSDimitry Andric const TargetMachine &TM = MF.getTarget(); 29891bc56edSDimitry Andric const MCRegisterInfo *MRI = Context.getRegisterInfo(); 299ff0cc061SDimitry Andric const ARMBaseRegisterInfo *RegInfo = STI.getRegisterInfo(); 300ff0cc061SDimitry Andric const ARMBaseInstrInfo &TII = *STI.getInstrInfo(); 3012754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 3022754fe60SDimitry Andric "This emitPrologue does not support Thumb1!"); 3032754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 304ff0cc061SDimitry Andric unsigned Align = STI.getFrameLowering()->getStackAlignment(); 305ff0cc061SDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 3062754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 3072754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 3082754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 3092754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 3102754fe60SDimitry Andric 3112754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 3122754fe60SDimitry Andric // belongs to which callee-save spill areas. 3132754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 3142754fe60SDimitry Andric int FramePtrSpillFI = 0; 315dff0c46cSDimitry Andric int D8SpillFI = 0; 316f8254f43SDimitry Andric 3173861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 3183861d79fSDimitry Andric // prologue/epilogue. 3197ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 3207ae0e2c9SDimitry Andric return; 3217ae0e2c9SDimitry Andric 32239d628a0SDimitry Andric StackAdjustingInsts DefCFAOffsetCandidates; 323ff0cc061SDimitry Andric bool HasFP = hasFP(MF); 32439d628a0SDimitry Andric 32591bc56edSDimitry Andric // Allocate the vararg register save area. 32691bc56edSDimitry Andric if (ArgRegsSaveSize) { 327284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, 3283b0f4066SDimitry Andric MachineInstr::FrameSetup); 32939d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), ArgRegsSaveSize, true); 33091bc56edSDimitry Andric } 3312754fe60SDimitry Andric 33291bc56edSDimitry Andric if (!AFI->hasStackFrame() && 33391bc56edSDimitry Andric (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) { 33491bc56edSDimitry Andric if (NumBytes - ArgRegsSaveSize != 0) { 33591bc56edSDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize), 3363b0f4066SDimitry Andric MachineInstr::FrameSetup); 33739d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), 33839d628a0SDimitry Andric NumBytes - ArgRegsSaveSize, true); 33991bc56edSDimitry Andric } 340ff0cc061SDimitry Andric DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP); 3412754fe60SDimitry Andric return; 3422754fe60SDimitry Andric } 3432754fe60SDimitry Andric 34491bc56edSDimitry Andric // Determine spill area sizes. 3452754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 3462754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 3472754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 3482754fe60SDimitry Andric switch (Reg) { 34991bc56edSDimitry Andric case ARM::R8: 35091bc56edSDimitry Andric case ARM::R9: 35191bc56edSDimitry Andric case ARM::R10: 35291bc56edSDimitry Andric case ARM::R11: 35391bc56edSDimitry Andric case ARM::R12: 35491bc56edSDimitry Andric if (STI.isTargetDarwin()) { 35591bc56edSDimitry Andric GPRCS2Size += 4; 35691bc56edSDimitry Andric break; 35791bc56edSDimitry Andric } 35891bc56edSDimitry Andric // fallthrough 359f785676fSDimitry Andric case ARM::R0: 360f785676fSDimitry Andric case ARM::R1: 361f785676fSDimitry Andric case ARM::R2: 362f785676fSDimitry Andric case ARM::R3: 3632754fe60SDimitry Andric case ARM::R4: 3642754fe60SDimitry Andric case ARM::R5: 3652754fe60SDimitry Andric case ARM::R6: 3662754fe60SDimitry Andric case ARM::R7: 3672754fe60SDimitry Andric case ARM::LR: 3682754fe60SDimitry Andric if (Reg == FramePtr) 3692754fe60SDimitry Andric FramePtrSpillFI = FI; 3702754fe60SDimitry Andric GPRCS1Size += 4; 3712754fe60SDimitry Andric break; 3722754fe60SDimitry Andric default: 373dff0c46cSDimitry Andric // This is a DPR. Exclude the aligned DPRCS2 spills. 374dff0c46cSDimitry Andric if (Reg == ARM::D8) 375dff0c46cSDimitry Andric D8SpillFI = FI; 376f785676fSDimitry Andric if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) 3772754fe60SDimitry Andric DPRCSSize += 8; 3782754fe60SDimitry Andric } 3792754fe60SDimitry Andric } 3802754fe60SDimitry Andric 3812754fe60SDimitry Andric // Move past area 1. 38239d628a0SDimitry Andric MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push; 38339d628a0SDimitry Andric if (GPRCS1Size > 0) { 38491bc56edSDimitry Andric GPRCS1Push = LastPush = MBBI++; 38539d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(LastPush, GPRCS1Size, true); 38639d628a0SDimitry Andric } 3872754fe60SDimitry Andric 3882754fe60SDimitry Andric // Determine starting offsets of spill areas. 38939d628a0SDimitry Andric unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size; 39039d628a0SDimitry Andric unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size; 39139d628a0SDimitry Andric unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U; 39239d628a0SDimitry Andric unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign; 39339d628a0SDimitry Andric unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize; 394f785676fSDimitry Andric int FramePtrOffsetInPush = 0; 395f785676fSDimitry Andric if (HasFP) { 39639d628a0SDimitry Andric FramePtrOffsetInPush = 39739d628a0SDimitry Andric MFI->getObjectOffset(FramePtrSpillFI) + ArgRegsSaveSize; 3982754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 3992754fe60SDimitry Andric NumBytes); 400f785676fSDimitry Andric } 4012754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 4022754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 4032754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 4042754fe60SDimitry Andric 405f785676fSDimitry Andric // Move past area 2. 40639d628a0SDimitry Andric if (GPRCS2Size > 0) { 40791bc56edSDimitry Andric GPRCS2Push = LastPush = MBBI++; 40839d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(LastPush, GPRCS2Size); 40939d628a0SDimitry Andric } 41039d628a0SDimitry Andric 41139d628a0SDimitry Andric // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our 41239d628a0SDimitry Andric // .cfi_offset operations will reflect that. 41339d628a0SDimitry Andric if (DPRGapSize) { 41439d628a0SDimitry Andric assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs"); 41539d628a0SDimitry Andric if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize)) 41639d628a0SDimitry Andric DefCFAOffsetCandidates.addExtraBytes(LastPush, DPRGapSize); 41739d628a0SDimitry Andric else { 41839d628a0SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize, 41939d628a0SDimitry Andric MachineInstr::FrameSetup); 42039d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), DPRGapSize); 42139d628a0SDimitry Andric } 42239d628a0SDimitry Andric } 423f785676fSDimitry Andric 4242754fe60SDimitry Andric // Move past area 3. 425dd6029ffSDimitry Andric if (DPRCSSize > 0) { 426dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 427dd6029ffSDimitry Andric // instructions in the prologue. 42839d628a0SDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) { 42939d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(MBBI, sizeOfSPAdjustment(MBBI)); 430f785676fSDimitry Andric LastPush = MBBI++; 431dd6029ffSDimitry Andric } 43239d628a0SDimitry Andric } 4332754fe60SDimitry Andric 434dff0c46cSDimitry Andric // Move past the aligned DPRCS2 area. 435dff0c46cSDimitry Andric if (AFI->getNumAlignedDPRCS2Regs() > 0) { 436dff0c46cSDimitry Andric MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); 437dff0c46cSDimitry Andric // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and 438dff0c46cSDimitry Andric // leaves the stack pointer pointing to the DPRCS2 area. 439dff0c46cSDimitry Andric // 440dff0c46cSDimitry Andric // Adjust NumBytes to represent the stack slots below the DPRCS2 area. 441dff0c46cSDimitry Andric NumBytes += MFI->getObjectOffset(D8SpillFI); 442dff0c46cSDimitry Andric } else 4432754fe60SDimitry Andric NumBytes = DPRCSOffset; 444dff0c46cSDimitry Andric 44591bc56edSDimitry Andric if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) { 44691bc56edSDimitry Andric uint32_t NumWords = NumBytes >> 2; 44791bc56edSDimitry Andric 44891bc56edSDimitry Andric if (NumWords < 65536) 44991bc56edSDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4) 45091bc56edSDimitry Andric .addImm(NumWords) 45191bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup)); 45291bc56edSDimitry Andric else 45391bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4) 45491bc56edSDimitry Andric .addImm(NumWords) 45591bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 45691bc56edSDimitry Andric 45791bc56edSDimitry Andric switch (TM.getCodeModel()) { 45891bc56edSDimitry Andric case CodeModel::Small: 45991bc56edSDimitry Andric case CodeModel::Medium: 46091bc56edSDimitry Andric case CodeModel::Default: 46191bc56edSDimitry Andric case CodeModel::Kernel: 46291bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL)) 46391bc56edSDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0) 46491bc56edSDimitry Andric .addExternalSymbol("__chkstk") 46591bc56edSDimitry Andric .addReg(ARM::R4, RegState::Implicit) 46691bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 46791bc56edSDimitry Andric break; 46891bc56edSDimitry Andric case CodeModel::Large: 46991bc56edSDimitry Andric case CodeModel::JITDefault: 47091bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12) 47191bc56edSDimitry Andric .addExternalSymbol("__chkstk") 47291bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 47391bc56edSDimitry Andric 47491bc56edSDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr)) 47591bc56edSDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0) 47691bc56edSDimitry Andric .addReg(ARM::R12, RegState::Kill) 47791bc56edSDimitry Andric .addReg(ARM::R4, RegState::Implicit) 47891bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 47991bc56edSDimitry Andric break; 48091bc56edSDimitry Andric } 48191bc56edSDimitry Andric 48291bc56edSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), 48391bc56edSDimitry Andric ARM::SP) 48491bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 48591bc56edSDimitry Andric .addReg(ARM::R4, RegState::Kill) 48691bc56edSDimitry Andric .setMIFlags(MachineInstr::FrameSetup))); 48791bc56edSDimitry Andric NumBytes = 0; 48891bc56edSDimitry Andric } 48991bc56edSDimitry Andric 4902754fe60SDimitry Andric if (NumBytes) { 4912754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 49239d628a0SDimitry Andric if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) 49339d628a0SDimitry Andric DefCFAOffsetCandidates.addExtraBytes(LastPush, NumBytes); 49439d628a0SDimitry Andric else { 4953b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 4963b0f4066SDimitry Andric MachineInstr::FrameSetup); 49739d628a0SDimitry Andric DefCFAOffsetCandidates.addInst(std::prev(MBBI), NumBytes); 49839d628a0SDimitry Andric } 499f785676fSDimitry Andric 5002754fe60SDimitry Andric if (HasFP && isARM) 5012754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 5022754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 5032754fe60SDimitry Andric // taken two instructions: 5042754fe60SDimitry Andric // mov sp, r7 5052754fe60SDimitry Andric // sub sp, #24 5062754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 5072754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 5082754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 5092754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 5102754fe60SDimitry Andric } 5112754fe60SDimitry Andric 51239d628a0SDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 51339d628a0SDimitry Andric // For iOS, FP is R7, which has now been stored in spill area 1. 51439d628a0SDimitry Andric // Otherwise, if this is not iOS, all the callee-saved registers go 51539d628a0SDimitry Andric // into spill area 1, including the FP in R11. In either case, it 51639d628a0SDimitry Andric // is in area one and the adjustment needs to take place just after 51739d628a0SDimitry Andric // that push. 51839d628a0SDimitry Andric if (HasFP) { 51939d628a0SDimitry Andric MachineBasicBlock::iterator AfterPush = std::next(GPRCS1Push); 52039d628a0SDimitry Andric unsigned PushSize = sizeOfSPAdjustment(GPRCS1Push); 52139d628a0SDimitry Andric emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, AfterPush, 52239d628a0SDimitry Andric dl, TII, FramePtr, ARM::SP, 52339d628a0SDimitry Andric PushSize + FramePtrOffsetInPush, 52439d628a0SDimitry Andric MachineInstr::FrameSetup); 52539d628a0SDimitry Andric if (FramePtrOffsetInPush + PushSize != 0) { 52639d628a0SDimitry Andric unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa( 52739d628a0SDimitry Andric nullptr, MRI->getDwarfRegNum(FramePtr, true), 52839d628a0SDimitry Andric -(ArgRegsSaveSize - FramePtrOffsetInPush))); 52939d628a0SDimitry Andric BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 53039d628a0SDimitry Andric .addCFIIndex(CFIIndex) 53139d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 53239d628a0SDimitry Andric } else { 53339d628a0SDimitry Andric unsigned CFIIndex = 53439d628a0SDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( 53539d628a0SDimitry Andric nullptr, MRI->getDwarfRegNum(FramePtr, true))); 53639d628a0SDimitry Andric BuildMI(MBB, AfterPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 53739d628a0SDimitry Andric .addCFIIndex(CFIIndex) 53839d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 53939d628a0SDimitry Andric } 54039d628a0SDimitry Andric } 54139d628a0SDimitry Andric 54239d628a0SDimitry Andric // Now that the prologue's actual instructions are finalised, we can insert 54339d628a0SDimitry Andric // the necessary DWARF cf instructions to describe the situation. Start by 54439d628a0SDimitry Andric // recording where each register ended up: 54539d628a0SDimitry Andric if (GPRCS1Size > 0) { 54639d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(GPRCS1Push); 54739d628a0SDimitry Andric int CFIIndex; 54891bc56edSDimitry Andric for (const auto &Entry : CSI) { 54991bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 55091bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 55191bc56edSDimitry Andric switch (Reg) { 55291bc56edSDimitry Andric case ARM::R8: 55391bc56edSDimitry Andric case ARM::R9: 55491bc56edSDimitry Andric case ARM::R10: 55591bc56edSDimitry Andric case ARM::R11: 55691bc56edSDimitry Andric case ARM::R12: 55791bc56edSDimitry Andric if (STI.isTargetDarwin()) 55891bc56edSDimitry Andric break; 55991bc56edSDimitry Andric // fallthrough 56091bc56edSDimitry Andric case ARM::R0: 56191bc56edSDimitry Andric case ARM::R1: 56291bc56edSDimitry Andric case ARM::R2: 56391bc56edSDimitry Andric case ARM::R3: 56491bc56edSDimitry Andric case ARM::R4: 56591bc56edSDimitry Andric case ARM::R5: 56691bc56edSDimitry Andric case ARM::R6: 56791bc56edSDimitry Andric case ARM::R7: 56891bc56edSDimitry Andric case ARM::LR: 56991bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 57091bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI))); 57191bc56edSDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 57239d628a0SDimitry Andric .addCFIIndex(CFIIndex) 57339d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 57491bc56edSDimitry Andric break; 57591bc56edSDimitry Andric } 57691bc56edSDimitry Andric } 57791bc56edSDimitry Andric } 57891bc56edSDimitry Andric 57991bc56edSDimitry Andric if (GPRCS2Size > 0) { 58039d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(GPRCS2Push); 58191bc56edSDimitry Andric for (const auto &Entry : CSI) { 58291bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 58391bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 58491bc56edSDimitry Andric switch (Reg) { 58591bc56edSDimitry Andric case ARM::R8: 58691bc56edSDimitry Andric case ARM::R9: 58791bc56edSDimitry Andric case ARM::R10: 58891bc56edSDimitry Andric case ARM::R11: 58991bc56edSDimitry Andric case ARM::R12: 59091bc56edSDimitry Andric if (STI.isTargetDarwin()) { 59191bc56edSDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 59291bc56edSDimitry Andric unsigned Offset = MFI->getObjectOffset(FI); 59391bc56edSDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 59491bc56edSDimitry Andric MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); 59591bc56edSDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 59639d628a0SDimitry Andric .addCFIIndex(CFIIndex) 59739d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 59891bc56edSDimitry Andric } 59991bc56edSDimitry Andric break; 60091bc56edSDimitry Andric } 60191bc56edSDimitry Andric } 60291bc56edSDimitry Andric } 60391bc56edSDimitry Andric 60491bc56edSDimitry Andric if (DPRCSSize > 0) { 60591bc56edSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 60691bc56edSDimitry Andric // instructions in the prologue. 60739d628a0SDimitry Andric MachineBasicBlock::iterator Pos = std::next(LastPush); 60891bc56edSDimitry Andric for (const auto &Entry : CSI) { 60991bc56edSDimitry Andric unsigned Reg = Entry.getReg(); 61091bc56edSDimitry Andric int FI = Entry.getFrameIdx(); 61191bc56edSDimitry Andric if ((Reg >= ARM::D0 && Reg <= ARM::D31) && 61291bc56edSDimitry Andric (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) { 61391bc56edSDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 61491bc56edSDimitry Andric unsigned Offset = MFI->getObjectOffset(FI); 61591bc56edSDimitry Andric unsigned CFIIndex = MMI.addFrameInst( 61691bc56edSDimitry Andric MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); 61739d628a0SDimitry Andric BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 61839d628a0SDimitry Andric .addCFIIndex(CFIIndex) 61939d628a0SDimitry Andric .setMIFlags(MachineInstr::FrameSetup); 62091bc56edSDimitry Andric } 62191bc56edSDimitry Andric } 62291bc56edSDimitry Andric } 62391bc56edSDimitry Andric 62439d628a0SDimitry Andric // Now we can emit descriptions of where the canonical frame address was 62539d628a0SDimitry Andric // throughout the process. If we have a frame pointer, it takes over the job 62639d628a0SDimitry Andric // half-way through, so only the first few .cfi_def_cfa_offset instructions 62739d628a0SDimitry Andric // actually get emitted. 62839d628a0SDimitry Andric DefCFAOffsetCandidates.emitDefCFAOffsets(MMI, MBB, dl, TII, HasFP); 629f785676fSDimitry Andric 6302754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 6312754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 6322754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 6332754fe60SDimitry Andric 6342754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 6352754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 63639d628a0SDimitry Andric AFI->setDPRCalleeSavedGapSize(DPRGapSize); 6372754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 6382754fe60SDimitry Andric 6392754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 6402754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 641dff0c46cSDimitry Andric // If aligned NEON registers were spilled, the stack has already been 642dff0c46cSDimitry Andric // realigned. 643dff0c46cSDimitry Andric if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { 6442754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 6452754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction()); 6462754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 64739d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::SP, MaxAlign, 64839d628a0SDimitry Andric false); 6492754fe60SDimitry Andric } else { 65039d628a0SDimitry Andric // We cannot use sp as source/dest register here, thus we're using r4 to 65139d628a0SDimitry Andric // perform the calculations. We're emitting the following sequence: 6522754fe60SDimitry Andric // mov r4, sp 65339d628a0SDimitry Andric // -- use emitAligningInstructions to produce best sequence to zero 65439d628a0SDimitry Andric // -- out lower bits in r4 6552754fe60SDimitry Andric // mov sp, r4 6562754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 65717a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 65817a519f9SDimitry Andric .addReg(ARM::SP, RegState::Kill)); 65939d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MBBI, dl, ARM::R4, MaxAlign, 66039d628a0SDimitry Andric false); 66117a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 66217a519f9SDimitry Andric .addReg(ARM::R4, RegState::Kill)); 6632754fe60SDimitry Andric } 6642754fe60SDimitry Andric 6652754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 6662754fe60SDimitry Andric } 6672754fe60SDimitry Andric 6682754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 6692754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 6702754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 6712754fe60SDimitry Andric // to reference locals. 6723b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 6732754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 6742754fe60SDimitry Andric if (isARM) 6752754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 6762754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 6772754fe60SDimitry Andric .addReg(ARM::SP) 6782754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 6792754fe60SDimitry Andric else 68017a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 68117a519f9SDimitry Andric RegInfo->getBaseRegister()) 68217a519f9SDimitry Andric .addReg(ARM::SP)); 6832754fe60SDimitry Andric } 6842754fe60SDimitry Andric 6852754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 6862754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 6872754fe60SDimitry Andric // checks for hasVarSizedObjects. 6882754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 6892754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 6902754fe60SDimitry Andric } 6912754fe60SDimitry Andric 6929cac79b3SDimitry Andric // Resolve TCReturn pseudo-instruction 6939cac79b3SDimitry Andric void ARMFrameLowering::fixTCReturn(MachineFunction &MF, 6942754fe60SDimitry Andric MachineBasicBlock &MBB) const { 6952754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 696dff0c46cSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 6972754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 6982754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 6999cac79b3SDimitry Andric const ARMBaseInstrInfo &TII = 70039d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 7019cac79b3SDimitry Andric 7029cac79b3SDimitry Andric if (!(RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri)) 7039cac79b3SDimitry Andric return; 7049cac79b3SDimitry Andric 7059cac79b3SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 7069cac79b3SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 7079cac79b3SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 7089cac79b3SDimitry Andric 7099cac79b3SDimitry Andric // Jump to label or value in register. 7109cac79b3SDimitry Andric if (RetOpcode == ARM::TCRETURNdi) { 7119cac79b3SDimitry Andric unsigned TCOpcode = STI.isThumb() ? 7129cac79b3SDimitry Andric (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : 7139cac79b3SDimitry Andric ARM::TAILJMPd; 7149cac79b3SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 7159cac79b3SDimitry Andric if (JumpTarget.isGlobal()) 7169cac79b3SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 7179cac79b3SDimitry Andric JumpTarget.getTargetFlags()); 7189cac79b3SDimitry Andric else { 7199cac79b3SDimitry Andric assert(JumpTarget.isSymbol()); 7209cac79b3SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 7219cac79b3SDimitry Andric JumpTarget.getTargetFlags()); 7229cac79b3SDimitry Andric } 7239cac79b3SDimitry Andric 7249cac79b3SDimitry Andric // Add the default predicate in Thumb mode. 7259cac79b3SDimitry Andric if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 7269cac79b3SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 7279cac79b3SDimitry Andric BuildMI(MBB, MBBI, dl, 7289cac79b3SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 7299cac79b3SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 7309cac79b3SDimitry Andric } 7319cac79b3SDimitry Andric 7329cac79b3SDimitry Andric MachineInstr *NewMI = std::prev(MBBI); 7339cac79b3SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 7349cac79b3SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 7359cac79b3SDimitry Andric 7369cac79b3SDimitry Andric // Delete the pseudo instruction TCRETURN. 7379cac79b3SDimitry Andric MBB.erase(MBBI); 7389cac79b3SDimitry Andric MBBI = NewMI; 7399cac79b3SDimitry Andric } 7409cac79b3SDimitry Andric 7419cac79b3SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 7429cac79b3SDimitry Andric MachineBasicBlock &MBB) const { 7439cac79b3SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 7449cac79b3SDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 7459cac79b3SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 7462754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 7472754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 74839d628a0SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 7492754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 75039d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 7512754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 7522754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 7532754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 7542754fe60SDimitry Andric 755ff0cc061SDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 7562754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 7572754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 7582754fe60SDimitry Andric 7593861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 7603861d79fSDimitry Andric // prologue/epilogue. 7619cac79b3SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) { 7629cac79b3SDimitry Andric fixTCReturn(MF, MBB); 7637ae0e2c9SDimitry Andric return; 7649cac79b3SDimitry Andric } 7657ae0e2c9SDimitry Andric 7662754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 76791bc56edSDimitry Andric if (NumBytes - ArgRegsSaveSize != 0) 76891bc56edSDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize); 7692754fe60SDimitry Andric } else { 7702754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 77191bc56edSDimitry Andric const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 7722754fe60SDimitry Andric if (MBBI != MBB.begin()) { 773f785676fSDimitry Andric do { 7742754fe60SDimitry Andric --MBBI; 775f785676fSDimitry Andric } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 7762754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 7772754fe60SDimitry Andric ++MBBI; 7782754fe60SDimitry Andric } 7792754fe60SDimitry Andric 7802754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 78191bc56edSDimitry Andric NumBytes -= (ArgRegsSaveSize + 78291bc56edSDimitry Andric AFI->getGPRCalleeSavedArea1Size() + 7832754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 78439d628a0SDimitry Andric AFI->getDPRCalleeSavedGapSize() + 7852754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 7862754fe60SDimitry Andric 7872754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 7882754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 7892754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 7902754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 7912754fe60SDimitry Andric if (NumBytes) { 7922754fe60SDimitry Andric if (isARM) 7932754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 7942754fe60SDimitry Andric ARMCC::AL, 0, TII); 7952754fe60SDimitry Andric else { 7962754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 797dff0c46cSDimitry Andric // For iOS, this looks like: 7982754fe60SDimitry Andric // mov sp, r7 7992754fe60SDimitry Andric // sub sp, #24 8002754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 8012754fe60SDimitry Andric // inconsistent state. 8022754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 8032754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 8042754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 8052754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 8062754fe60SDimitry Andric ARMCC::AL, 0, TII); 80717a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 80817a519f9SDimitry Andric ARM::SP) 80917a519f9SDimitry Andric .addReg(ARM::R4)); 8102754fe60SDimitry Andric } 8112754fe60SDimitry Andric } else { 8122754fe60SDimitry Andric // Thumb2 or ARM. 8132754fe60SDimitry Andric if (isARM) 8142754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 8152754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 8162754fe60SDimitry Andric else 81717a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 81817a519f9SDimitry Andric ARM::SP) 81917a519f9SDimitry Andric .addReg(FramePtr)); 8202754fe60SDimitry Andric } 82191bc56edSDimitry Andric } else if (NumBytes && 82291bc56edSDimitry Andric !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes)) 8232754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 8242754fe60SDimitry Andric 8252754fe60SDimitry Andric // Increment past our save areas. 826dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 827dd6029ffSDimitry Andric MBBI++; 828dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 829dd6029ffSDimitry Andric // instructions in the epilogue. 830dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 831dd6029ffSDimitry Andric MBBI++; 832dd6029ffSDimitry Andric } 83339d628a0SDimitry Andric if (AFI->getDPRCalleeSavedGapSize()) { 83439d628a0SDimitry Andric assert(AFI->getDPRCalleeSavedGapSize() == 4 && 83539d628a0SDimitry Andric "unexpected DPR alignment gap"); 83639d628a0SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize()); 83739d628a0SDimitry Andric } 83839d628a0SDimitry Andric 8392754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 8402754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 8412754fe60SDimitry Andric } 8422754fe60SDimitry Andric 8439cac79b3SDimitry Andric fixTCReturn(MF, MBB); 8442754fe60SDimitry Andric 845284c1978SDimitry Andric if (ArgRegsSaveSize) 846284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); 8472754fe60SDimitry Andric } 8482754fe60SDimitry Andric 8492754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 8502754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 8512754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 8522754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 8532754fe60SDimitry Andric int 8542754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 8552754fe60SDimitry Andric unsigned &FrameReg) const { 8562754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 8572754fe60SDimitry Andric } 8582754fe60SDimitry Andric 8592754fe60SDimitry Andric int 8602754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 8613b0f4066SDimitry Andric int FI, unsigned &FrameReg, 8622754fe60SDimitry Andric int SPAdj) const { 8632754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 86439d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 86539d628a0SDimitry Andric MF.getSubtarget().getRegisterInfo()); 8662754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 8672754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 8682754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 8692754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 8702754fe60SDimitry Andric 8712754fe60SDimitry Andric FrameReg = ARM::SP; 8722754fe60SDimitry Andric Offset += SPAdj; 8732754fe60SDimitry Andric 874dff0c46cSDimitry Andric // SP can move around if there are allocas. We may also lose track of SP 875dff0c46cSDimitry Andric // when emergency spilling inside a non-reserved call frame setup. 876dff0c46cSDimitry Andric bool hasMovingSP = !hasReservedCallFrame(MF); 877dff0c46cSDimitry Andric 8782754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 8792754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 8802754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 8812754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 8822754fe60SDimitry Andric if (isFixed) { 8832754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 8842754fe60SDimitry Andric Offset = FPOffset; 885dff0c46cSDimitry Andric } else if (hasMovingSP) { 8862754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 8872754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 8882754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 8892754fe60SDimitry Andric } 8902754fe60SDimitry Andric return Offset; 8912754fe60SDimitry Andric } 8922754fe60SDimitry Andric 8932754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 8942754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 8952754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 8962754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 897dff0c46cSDimitry Andric if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { 8982754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 8992754fe60SDimitry Andric return FPOffset; 900dff0c46cSDimitry Andric } else if (hasMovingSP) { 9012754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 9023b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 9032754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 9042754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 9052754fe60SDimitry Andric // particular. 9062754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 9072754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9082754fe60SDimitry Andric return FPOffset; 9092754fe60SDimitry Andric } 9103b0f4066SDimitry Andric } 9112754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 9123b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 9133b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 9143b0f4066SDimitry Andric // if at all possible to save space. 9153b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 9163b0f4066SDimitry Andric return Offset; 9172754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 9183b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 9192754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 9202754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9212754fe60SDimitry Andric return FPOffset; 9222754fe60SDimitry Andric } 9232754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 9242754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 9252754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 9262754fe60SDimitry Andric return FPOffset; 9272754fe60SDimitry Andric } 9282754fe60SDimitry Andric } 9292754fe60SDimitry Andric // Use the base pointer if we have one. 9302754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 9312754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 9322754fe60SDimitry Andric return Offset; 9332754fe60SDimitry Andric } 9342754fe60SDimitry Andric 9352754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 9362754fe60SDimitry Andric int FI) const { 9372754fe60SDimitry Andric unsigned FrameReg; 9382754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 9392754fe60SDimitry Andric } 9402754fe60SDimitry Andric 9412754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 9422754fe60SDimitry Andric MachineBasicBlock::iterator MI, 9432754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 9442754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 9452754fe60SDimitry Andric bool NoGap, 9463b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 947dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 9483b0f4066SDimitry Andric unsigned MIFlags) const { 9492754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 95039d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 9512754fe60SDimitry Andric 9522754fe60SDimitry Andric DebugLoc DL; 9532754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 9542754fe60SDimitry Andric 9552754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 9562754fe60SDimitry Andric unsigned i = CSI.size(); 9572754fe60SDimitry Andric while (i != 0) { 9582754fe60SDimitry Andric unsigned LastReg = 0; 9592754fe60SDimitry Andric for (; i != 0; --i) { 9602754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 96191bc56edSDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 962dff0c46cSDimitry Andric 963dff0c46cSDimitry Andric // D-registers in the aligned area DPRCS2 are NOT spilled here. 964dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 965dff0c46cSDimitry Andric continue; 9662754fe60SDimitry Andric 9672754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 9682754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 9692754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 9702754fe60SDimitry Andric // entry block live-in sets. 9712754fe60SDimitry Andric bool isKill = true; 9722754fe60SDimitry Andric if (Reg == ARM::LR) { 9732754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 9742754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 9752754fe60SDimitry Andric isKill = false; 9762754fe60SDimitry Andric } 9772754fe60SDimitry Andric 9782754fe60SDimitry Andric if (isKill) 9792754fe60SDimitry Andric MBB.addLiveIn(Reg); 9802754fe60SDimitry Andric 9812754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 9822754fe60SDimitry Andric // for other instructions. e.g. 9832754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 9842754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 9852754fe60SDimitry Andric break; 9862754fe60SDimitry Andric LastReg = Reg; 9872754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 9882754fe60SDimitry Andric } 9892754fe60SDimitry Andric 9902754fe60SDimitry Andric if (Regs.empty()) 9912754fe60SDimitry Andric continue; 9922754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 9932754fe60SDimitry Andric MachineInstrBuilder MIB = 9942754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 9953b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 9962754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 9972754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 9982754fe60SDimitry Andric } else if (Regs.size() == 1) { 9992754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 10002754fe60SDimitry Andric ARM::SP) 10012754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 10026122f3e6SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags) 10036122f3e6SDimitry Andric .addImm(-4); 10042754fe60SDimitry Andric AddDefaultPred(MIB); 10052754fe60SDimitry Andric } 10062754fe60SDimitry Andric Regs.clear(); 100791bc56edSDimitry Andric 100891bc56edSDimitry Andric // Put any subsequent vpush instructions before this one: they will refer to 100991bc56edSDimitry Andric // higher register numbers so need to be pushed first in order to preserve 101091bc56edSDimitry Andric // monotonicity. 101191bc56edSDimitry Andric --MI; 10122754fe60SDimitry Andric } 10132754fe60SDimitry Andric } 10142754fe60SDimitry Andric 10152754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 10162754fe60SDimitry Andric MachineBasicBlock::iterator MI, 10172754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 10182754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 10192754fe60SDimitry Andric bool isVarArg, bool NoGap, 1020dff0c46cSDimitry Andric bool(*Func)(unsigned, bool), 1021dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) const { 10222754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 102339d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 10242754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 10252754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 10262754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 10272754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 1028dff0c46cSDimitry Andric RetOpcode == ARM::TCRETURNri); 1029f785676fSDimitry Andric bool isInterrupt = 1030f785676fSDimitry Andric RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR; 10312754fe60SDimitry Andric 10322754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 10332754fe60SDimitry Andric unsigned i = CSI.size(); 10342754fe60SDimitry Andric while (i != 0) { 10352754fe60SDimitry Andric unsigned LastReg = 0; 10362754fe60SDimitry Andric bool DeleteRet = false; 10372754fe60SDimitry Andric for (; i != 0; --i) { 10382754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 103991bc56edSDimitry Andric if (!(Func)(Reg, STI.isTargetDarwin())) continue; 1040dff0c46cSDimitry Andric 1041dff0c46cSDimitry Andric // The aligned reloads from area DPRCS2 are not inserted here. 1042dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 1043dff0c46cSDimitry Andric continue; 10442754fe60SDimitry Andric 1045f785676fSDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt && 1046f785676fSDimitry Andric STI.hasV5TOps()) { 10472754fe60SDimitry Andric Reg = ARM::PC; 10482754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 10492754fe60SDimitry Andric // Fold the return instruction into the LDM. 10502754fe60SDimitry Andric DeleteRet = true; 10512754fe60SDimitry Andric } 10522754fe60SDimitry Andric 10532754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 10542754fe60SDimitry Andric // for other instructions. e.g. 10552754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 10562754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 10572754fe60SDimitry Andric break; 10582754fe60SDimitry Andric 10592754fe60SDimitry Andric LastReg = Reg; 10602754fe60SDimitry Andric Regs.push_back(Reg); 10612754fe60SDimitry Andric } 10622754fe60SDimitry Andric 10632754fe60SDimitry Andric if (Regs.empty()) 10642754fe60SDimitry Andric continue; 10652754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 10662754fe60SDimitry Andric MachineInstrBuilder MIB = 10672754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 10682754fe60SDimitry Andric .addReg(ARM::SP)); 10692754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 10702754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 10716122f3e6SDimitry Andric if (DeleteRet) { 1072139f7f9bSDimitry Andric MIB.copyImplicitOps(&*MI); 10732754fe60SDimitry Andric MI->eraseFromParent(); 10746122f3e6SDimitry Andric } 10752754fe60SDimitry Andric MI = MIB; 10762754fe60SDimitry Andric } else if (Regs.size() == 1) { 10772754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 10782754fe60SDimitry Andric // only do that for LDM. 10792754fe60SDimitry Andric if (Regs[0] == ARM::PC) 10802754fe60SDimitry Andric Regs[0] = ARM::LR; 10812754fe60SDimitry Andric MachineInstrBuilder MIB = 10822754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 10832754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 10842754fe60SDimitry Andric .addReg(ARM::SP); 10852754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 10862754fe60SDimitry Andric // that refactoring is complete (eventually). 10876122f3e6SDimitry Andric if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 10882754fe60SDimitry Andric MIB.addReg(0); 10892754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 10902754fe60SDimitry Andric } else 10912754fe60SDimitry Andric MIB.addImm(4); 10922754fe60SDimitry Andric AddDefaultPred(MIB); 10932754fe60SDimitry Andric } 10942754fe60SDimitry Andric Regs.clear(); 109591bc56edSDimitry Andric 109691bc56edSDimitry Andric // Put any subsequent vpop instructions after this one: they will refer to 109791bc56edSDimitry Andric // higher register numbers so need to be popped afterwards. 109891bc56edSDimitry Andric ++MI; 10992754fe60SDimitry Andric } 11002754fe60SDimitry Andric } 11012754fe60SDimitry Andric 1102dff0c46cSDimitry Andric /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers 1103dff0c46cSDimitry Andric /// starting from d8. Also insert stack realignment code and leave the stack 1104dff0c46cSDimitry Andric /// pointer pointing to the d8 spill slot. 1105dff0c46cSDimitry Andric static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, 1106dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 1107dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 1108dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 1109dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 1110dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 1111dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1112dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 111339d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1114dff0c46cSDimitry Andric MachineFrameInfo &MFI = *MF.getFrameInfo(); 1115dff0c46cSDimitry Andric 1116dff0c46cSDimitry Andric // Mark the D-register spill slots as properly aligned. Since MFI computes 1117dff0c46cSDimitry Andric // stack slot layout backwards, this can actually mean that the d-reg stack 1118dff0c46cSDimitry Andric // slot offsets can be wrong. The offset for d8 will always be correct. 1119dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1120dff0c46cSDimitry Andric unsigned DNum = CSI[i].getReg() - ARM::D8; 1121dff0c46cSDimitry Andric if (DNum >= 8) 1122dff0c46cSDimitry Andric continue; 1123dff0c46cSDimitry Andric int FI = CSI[i].getFrameIdx(); 1124dff0c46cSDimitry Andric // The even-numbered registers will be 16-byte aligned, the odd-numbered 1125dff0c46cSDimitry Andric // registers will be 8-byte aligned. 1126dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); 1127dff0c46cSDimitry Andric 1128dff0c46cSDimitry Andric // The stack slot for D8 needs to be maximally aligned because this is 1129dff0c46cSDimitry Andric // actually the point where we align the stack pointer. MachineFrameInfo 1130dff0c46cSDimitry Andric // computes all offsets relative to the incoming stack pointer which is a 1131dff0c46cSDimitry Andric // bit weird when realigning the stack. Any extra padding for this 1132dff0c46cSDimitry Andric // over-alignment is not realized because the code inserted below adjusts 1133dff0c46cSDimitry Andric // the stack pointer by numregs * 8 before aligning the stack pointer. 1134dff0c46cSDimitry Andric if (DNum == 0) 1135dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); 1136dff0c46cSDimitry Andric } 1137dff0c46cSDimitry Andric 1138dff0c46cSDimitry Andric // Move the stack pointer to the d8 spill slot, and align it at the same 1139dff0c46cSDimitry Andric // time. Leave the stack slot address in the scratch register r4. 1140dff0c46cSDimitry Andric // 1141dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1142dff0c46cSDimitry Andric // bic r4, r4, #align - 1 1143dff0c46cSDimitry Andric // mov sp, r4 1144dff0c46cSDimitry Andric // 1145dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 1146dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 1147dff0c46cSDimitry Andric AFI->setShouldRestoreSPFromFP(true); 1148dff0c46cSDimitry Andric 1149dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1150dff0c46cSDimitry Andric // The immediate is <= 64, so it doesn't need any special encoding. 1151dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; 1152dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 1153dff0c46cSDimitry Andric .addReg(ARM::SP) 1154dff0c46cSDimitry Andric .addImm(8 * NumAlignedDPRCS2Regs))); 1155dff0c46cSDimitry Andric 1156dff0c46cSDimitry Andric unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); 115739d628a0SDimitry Andric // We must set parameter MustBeSingleInstruction to true, since 115839d628a0SDimitry Andric // skipAlignedDPRCS2Spills expects exactly 3 instructions to perform 115939d628a0SDimitry Andric // stack alignment. Luckily, this can always be done since all ARM 116039d628a0SDimitry Andric // architecture versions that support Neon also support the BFC 116139d628a0SDimitry Andric // instruction. 116239d628a0SDimitry Andric emitAligningInstructions(MF, AFI, TII, MBB, MI, DL, ARM::R4, MaxAlign, true); 1163dff0c46cSDimitry Andric 1164dff0c46cSDimitry Andric // mov sp, r4 1165dff0c46cSDimitry Andric // The stack pointer must be adjusted before spilling anything, otherwise 1166dff0c46cSDimitry Andric // the stack slots could be clobbered by an interrupt handler. 1167dff0c46cSDimitry Andric // Leave r4 live, it is used below. 1168dff0c46cSDimitry Andric Opc = isThumb ? ARM::tMOVr : ARM::MOVr; 1169dff0c46cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) 1170dff0c46cSDimitry Andric .addReg(ARM::R4); 1171dff0c46cSDimitry Andric MIB = AddDefaultPred(MIB); 1172dff0c46cSDimitry Andric if (!isThumb) 1173dff0c46cSDimitry Andric AddDefaultCC(MIB); 1174dff0c46cSDimitry Andric 1175dff0c46cSDimitry Andric // Now spill NumAlignedDPRCS2Regs registers starting from d8. 1176dff0c46cSDimitry Andric // r4 holds the stack slot address. 1177dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 1178dff0c46cSDimitry Andric 1179dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs and address writeback. 1180dff0c46cSDimitry Andric // The writeback is only needed when emitting two vst1.64 instructions. 1181dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 1182dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 11837ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1184dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1185dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), 1186dff0c46cSDimitry Andric ARM::R4) 1187dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 1188dff0c46cSDimitry Andric .addReg(NextReg) 1189dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 1190dff0c46cSDimitry Andric NextReg += 4; 1191dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1192dff0c46cSDimitry Andric } 1193dff0c46cSDimitry Andric 1194dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 1195dff0c46cSDimitry Andric // register to be spilled. 1196dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 1197dff0c46cSDimitry Andric 1198dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs, no writeback. 1199dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 1200dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 12017ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1202dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1203dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) 1204dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(NextReg) 1205dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 1206dff0c46cSDimitry Andric NextReg += 4; 1207dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1208dff0c46cSDimitry Andric } 1209dff0c46cSDimitry Andric 1210dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 2 d-regs. 1211dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 1212dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 12137ae0e2c9SDimitry Andric &ARM::QPRRegClass); 1214dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 1215dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) 1216dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(SupReg)); 1217dff0c46cSDimitry Andric NextReg += 2; 1218dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 1219dff0c46cSDimitry Andric } 1220dff0c46cSDimitry Andric 1221dff0c46cSDimitry Andric // Finally, use a vanilla vstr.64 for the odd last register. 1222dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) { 1223dff0c46cSDimitry Andric MBB.addLiveIn(NextReg); 1224dff0c46cSDimitry Andric // vstr.64 uses addrmode5 which has an offset scale of 4. 1225dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) 1226dff0c46cSDimitry Andric .addReg(NextReg) 1227dff0c46cSDimitry Andric .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); 1228dff0c46cSDimitry Andric } 1229dff0c46cSDimitry Andric 1230dff0c46cSDimitry Andric // The last spill instruction inserted should kill the scratch register r4. 123191bc56edSDimitry Andric std::prev(MI)->addRegisterKilled(ARM::R4, TRI); 1232dff0c46cSDimitry Andric } 1233dff0c46cSDimitry Andric 1234dff0c46cSDimitry Andric /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an 1235dff0c46cSDimitry Andric /// iterator to the following instruction. 1236dff0c46cSDimitry Andric static MachineBasicBlock::iterator 1237dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 1238dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) { 1239dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 1240dff0c46cSDimitry Andric // bic r4, r4, #align - 1 1241dff0c46cSDimitry Andric // mov sp, r4 1242dff0c46cSDimitry Andric ++MI; ++MI; ++MI; 1243dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1244dff0c46cSDimitry Andric 1245dff0c46cSDimitry Andric // These switches all fall through. 1246dff0c46cSDimitry Andric switch(NumAlignedDPRCS2Regs) { 1247dff0c46cSDimitry Andric case 7: 1248dff0c46cSDimitry Andric ++MI; 1249dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1250dff0c46cSDimitry Andric default: 1251dff0c46cSDimitry Andric ++MI; 1252dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 1253dff0c46cSDimitry Andric case 1: 1254dff0c46cSDimitry Andric case 2: 1255dff0c46cSDimitry Andric case 4: 1256dff0c46cSDimitry Andric assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); 1257dff0c46cSDimitry Andric ++MI; 1258dff0c46cSDimitry Andric } 1259dff0c46cSDimitry Andric return MI; 1260dff0c46cSDimitry Andric } 1261dff0c46cSDimitry Andric 1262dff0c46cSDimitry Andric /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers 1263dff0c46cSDimitry Andric /// starting from d8. These instructions are assumed to execute while the 1264dff0c46cSDimitry Andric /// stack is still aligned, unlike the code inserted by emitPopInst. 1265dff0c46cSDimitry Andric static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, 1266dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 1267dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 1268dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 1269dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 1270dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 1271dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1272dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 127339d628a0SDimitry Andric const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 1274dff0c46cSDimitry Andric 1275dff0c46cSDimitry Andric // Find the frame index assigned to d8. 1276dff0c46cSDimitry Andric int D8SpillFI = 0; 1277dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) 1278dff0c46cSDimitry Andric if (CSI[i].getReg() == ARM::D8) { 1279dff0c46cSDimitry Andric D8SpillFI = CSI[i].getFrameIdx(); 1280dff0c46cSDimitry Andric break; 1281dff0c46cSDimitry Andric } 1282dff0c46cSDimitry Andric 1283dff0c46cSDimitry Andric // Materialize the address of the d8 spill slot into the scratch register r4. 1284dff0c46cSDimitry Andric // This can be fairly complicated if the stack frame is large, so just use 1285dff0c46cSDimitry Andric // the normal frame index elimination mechanism to do it. This code runs as 1286dff0c46cSDimitry Andric // the initial part of the epilog where the stack and base pointers haven't 1287dff0c46cSDimitry Andric // been changed yet. 1288dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 1289dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 1290dff0c46cSDimitry Andric 1291dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; 1292dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 1293dff0c46cSDimitry Andric .addFrameIndex(D8SpillFI).addImm(0))); 1294dff0c46cSDimitry Andric 1295dff0c46cSDimitry Andric // Now restore NumAlignedDPRCS2Regs registers starting from d8. 1296dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 1297dff0c46cSDimitry Andric 1298dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs and writeback. 1299dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 1300dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 13017ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1302dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) 1303dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Define) 1304dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 1305dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 1306dff0c46cSDimitry Andric NextReg += 4; 1307dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1308dff0c46cSDimitry Andric } 1309dff0c46cSDimitry Andric 1310dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 1311dff0c46cSDimitry Andric // register to be spilled. 1312dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 1313dff0c46cSDimitry Andric 1314dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs, no writeback. 1315dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 1316dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 13177ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 1318dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) 1319dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16) 1320dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 1321dff0c46cSDimitry Andric NextReg += 4; 1322dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 1323dff0c46cSDimitry Andric } 1324dff0c46cSDimitry Andric 1325dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 2 d-regs. 1326dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 1327dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 13287ae0e2c9SDimitry Andric &ARM::QPRRegClass); 1329dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) 1330dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16)); 1331dff0c46cSDimitry Andric NextReg += 2; 1332dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 1333dff0c46cSDimitry Andric } 1334dff0c46cSDimitry Andric 1335dff0c46cSDimitry Andric // Finally, use a vanilla vldr.64 for the remaining odd register. 1336dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1337dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) 1338dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); 1339dff0c46cSDimitry Andric 1340dff0c46cSDimitry Andric // Last store kills r4. 134191bc56edSDimitry Andric std::prev(MI)->addRegisterKilled(ARM::R4, TRI); 1342dff0c46cSDimitry Andric } 1343dff0c46cSDimitry Andric 13442754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 13452754fe60SDimitry Andric MachineBasicBlock::iterator MI, 13462754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 13472754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 13482754fe60SDimitry Andric if (CSI.empty()) 13492754fe60SDimitry Andric return false; 13502754fe60SDimitry Andric 13512754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13522754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 13532754fe60SDimitry Andric 13542754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 13556122f3e6SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? 13566122f3e6SDimitry Andric ARM::t2STR_PRE : ARM::STR_PRE_IMM; 13572754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 1358dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1359dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, 13603b0f4066SDimitry Andric MachineInstr::FrameSetup); 1361dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, 13623b0f4066SDimitry Andric MachineInstr::FrameSetup); 13633b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 1364dff0c46cSDimitry Andric NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); 1365dff0c46cSDimitry Andric 1366dff0c46cSDimitry Andric // The code above does not insert spill code for the aligned DPRCS2 registers. 1367dff0c46cSDimitry Andric // The stack realignment code will be inserted between the push instructions 1368dff0c46cSDimitry Andric // and these spills. 1369dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1370dff0c46cSDimitry Andric emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 13712754fe60SDimitry Andric 13722754fe60SDimitry Andric return true; 13732754fe60SDimitry Andric } 13742754fe60SDimitry Andric 13752754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 13762754fe60SDimitry Andric MachineBasicBlock::iterator MI, 13772754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 13782754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 13792754fe60SDimitry Andric if (CSI.empty()) 13802754fe60SDimitry Andric return false; 13812754fe60SDimitry Andric 13822754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 13832754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1384284c1978SDimitry Andric bool isVarArg = AFI->getArgRegsSaveSize() > 0; 1385dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1386dff0c46cSDimitry Andric 1387dff0c46cSDimitry Andric // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 1388dff0c46cSDimitry Andric // registers. Do that here instead. 1389dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1390dff0c46cSDimitry Andric emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 13912754fe60SDimitry Andric 13922754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 13936122f3e6SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 13942754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 1395dff0c46cSDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, 1396dff0c46cSDimitry Andric NumAlignedDPRCS2Regs); 13972754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1398dff0c46cSDimitry Andric &isARMArea2Register, 0); 13992754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1400dff0c46cSDimitry Andric &isARMArea1Register, 0); 14012754fe60SDimitry Andric 14022754fe60SDimitry Andric return true; 14032754fe60SDimitry Andric } 14042754fe60SDimitry Andric 14052754fe60SDimitry Andric // FIXME: Make generic? 14062754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 14072754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 14082754fe60SDimitry Andric unsigned FnSize = 0; 140991bc56edSDimitry Andric for (auto &MBB : MF) { 141091bc56edSDimitry Andric for (auto &MI : MBB) 141191bc56edSDimitry Andric FnSize += TII.GetInstSizeInBytes(&MI); 14122754fe60SDimitry Andric } 14132754fe60SDimitry Andric return FnSize; 14142754fe60SDimitry Andric } 14152754fe60SDimitry Andric 14162754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 14172754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 14182754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 14192754fe60SDimitry Andric // FIXME: Move to TII? 14202754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 14212754fe60SDimitry Andric const TargetFrameLowering *TFI) { 14222754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 14232754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 142491bc56edSDimitry Andric for (auto &MBB : MF) { 142591bc56edSDimitry Andric for (auto &MI : MBB) { 142691bc56edSDimitry Andric for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 142791bc56edSDimitry Andric if (!MI.getOperand(i).isFI()) 142891bc56edSDimitry Andric continue; 14292754fe60SDimitry Andric 14302754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 14312754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 143291bc56edSDimitry Andric if (MI.getOpcode() == ARM::ADDri) { 14332754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14342754fe60SDimitry Andric break; 14352754fe60SDimitry Andric } 14362754fe60SDimitry Andric 14372754fe60SDimitry Andric // Otherwise check the addressing mode. 143891bc56edSDimitry Andric switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) { 14392754fe60SDimitry Andric case ARMII::AddrMode3: 14402754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 14412754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14422754fe60SDimitry Andric break; 14432754fe60SDimitry Andric case ARMII::AddrMode5: 14442754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 14452754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 14462754fe60SDimitry Andric break; 14472754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 14482754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 14492754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 14502754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 14512754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 14522754fe60SDimitry Andric break; 14532754fe60SDimitry Andric case ARMII::AddrMode4: 14542754fe60SDimitry Andric case ARMII::AddrMode6: 14552754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 14562754fe60SDimitry Andric // immediate offset for stack references. 14572754fe60SDimitry Andric return 0; 14582754fe60SDimitry Andric default: 14592754fe60SDimitry Andric break; 14602754fe60SDimitry Andric } 14612754fe60SDimitry Andric break; // At most one FI per instruction 14622754fe60SDimitry Andric } 14632754fe60SDimitry Andric } 14642754fe60SDimitry Andric } 14652754fe60SDimitry Andric 14662754fe60SDimitry Andric return Limit; 14672754fe60SDimitry Andric } 14682754fe60SDimitry Andric 1469dff0c46cSDimitry Andric // In functions that realign the stack, it can be an advantage to spill the 1470dff0c46cSDimitry Andric // callee-saved vector registers after realigning the stack. The vst1 and vld1 1471dff0c46cSDimitry Andric // instructions take alignment hints that can improve performance. 1472dff0c46cSDimitry Andric // 1473dff0c46cSDimitry Andric static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { 1474dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); 1475dff0c46cSDimitry Andric if (!SpillAlignedNEONRegs) 1476dff0c46cSDimitry Andric return; 1477dff0c46cSDimitry Andric 1478dff0c46cSDimitry Andric // Naked functions don't spill callee-saved registers. 1479ff0cc061SDimitry Andric if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) 1480dff0c46cSDimitry Andric return; 1481dff0c46cSDimitry Andric 1482dff0c46cSDimitry Andric // We are planning to use NEON instructions vst1 / vld1. 1483ff0cc061SDimitry Andric if (!static_cast<const ARMSubtarget &>(MF.getSubtarget()).hasNEON()) 1484dff0c46cSDimitry Andric return; 1485dff0c46cSDimitry Andric 1486dff0c46cSDimitry Andric // Don't bother if the default stack alignment is sufficiently high. 1487ff0cc061SDimitry Andric if (MF.getSubtarget().getFrameLowering()->getStackAlignment() >= 8) 1488dff0c46cSDimitry Andric return; 1489dff0c46cSDimitry Andric 1490dff0c46cSDimitry Andric // Aligned spills require stack realignment. 1491ff0cc061SDimitry Andric if (!static_cast<const ARMBaseRegisterInfo *>( 1492ff0cc061SDimitry Andric MF.getSubtarget().getRegisterInfo())->canRealignStack(MF)) 1493dff0c46cSDimitry Andric return; 1494dff0c46cSDimitry Andric 1495dff0c46cSDimitry Andric // We always spill contiguous d-registers starting from d8. Count how many 1496dff0c46cSDimitry Andric // needs spilling. The register allocator will almost always use the 1497dff0c46cSDimitry Andric // callee-saved registers in order, but it can happen that there are holes in 1498dff0c46cSDimitry Andric // the range. Registers above the hole will be spilled to the standard DPRCS 1499dff0c46cSDimitry Andric // area. 1500dff0c46cSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1501dff0c46cSDimitry Andric unsigned NumSpills = 0; 1502dff0c46cSDimitry Andric for (; NumSpills < 8; ++NumSpills) 15033861d79fSDimitry Andric if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) 1504dff0c46cSDimitry Andric break; 1505dff0c46cSDimitry Andric 1506dff0c46cSDimitry Andric // Don't do this for just one d-register. It's not worth it. 1507dff0c46cSDimitry Andric if (NumSpills < 2) 1508dff0c46cSDimitry Andric return; 1509dff0c46cSDimitry Andric 1510dff0c46cSDimitry Andric // Spill the first NumSpills D-registers after realigning the stack. 1511dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); 1512dff0c46cSDimitry Andric 1513dff0c46cSDimitry Andric // A scratch register is required for the vst1 / vld1 instructions. 1514dff0c46cSDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 1515dff0c46cSDimitry Andric } 1516dff0c46cSDimitry Andric 15172754fe60SDimitry Andric void 15182754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 15192754fe60SDimitry Andric RegScavenger *RS) const { 15202754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 15212754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 15222754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 15232754fe60SDimitry Andric // to combine multiple loads / stores. 15242754fe60SDimitry Andric bool CanEliminateFrame = true; 15252754fe60SDimitry Andric bool CS1Spilled = false; 15262754fe60SDimitry Andric bool LRSpilled = false; 15272754fe60SDimitry Andric unsigned NumGPRSpills = 0; 15282754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 15292754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 153039d628a0SDimitry Andric const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( 153139d628a0SDimitry Andric MF.getSubtarget().getRegisterInfo()); 15322754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 153339d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 15342754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 15352754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 15363861d79fSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 15372754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 15382754fe60SDimitry Andric 15392754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 15402754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 15412754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 15422754fe60SDimitry Andric // instruction. 15432754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 15442754fe60SDimitry Andric if (AFI->isThumb2Function() && 15452754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 15463861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 15472754fe60SDimitry Andric 15482754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 15492754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 1550284c1978SDimitry Andric if (AFI->getArgRegsSaveSize() > 0) 15513861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 15522754fe60SDimitry Andric 155317a519f9SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 155417a519f9SDimitry Andric // for sure what the stack size will be, but for this, an estimate is good 155517a519f9SDimitry Andric // enough. If there anything changes it, it'll be a spill, which implies 155617a519f9SDimitry Andric // we've used all the registers and so R4 is already used, so not marking 155717a519f9SDimitry Andric // it here will be OK. 15582754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 1559139f7f9bSDimitry Andric unsigned StackSize = MFI->estimateStackSize(MF); 156017a519f9SDimitry Andric if (MFI->hasVarSizedObjects() || StackSize > 508) 15613861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 15622754fe60SDimitry Andric } 15632754fe60SDimitry Andric 1564dff0c46cSDimitry Andric // See if we can spill vector registers to aligned stack. 1565dff0c46cSDimitry Andric checkNumAlignedDPRCS2Regs(MF); 1566dff0c46cSDimitry Andric 15672754fe60SDimitry Andric // Spill the BasePtr if it's used. 15682754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 15693861d79fSDimitry Andric MRI.setPhysRegUsed(RegInfo->getBaseRegister()); 15702754fe60SDimitry Andric 15712754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 15722754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 157391bc56edSDimitry Andric const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 15742754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 15752754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 15762754fe60SDimitry Andric bool Spilled = false; 15773861d79fSDimitry Andric if (MRI.isPhysRegUsed(Reg)) { 15782754fe60SDimitry Andric Spilled = true; 15792754fe60SDimitry Andric CanEliminateFrame = false; 15802754fe60SDimitry Andric } 15812754fe60SDimitry Andric 15827ae0e2c9SDimitry Andric if (!ARM::GPRRegClass.contains(Reg)) 15832754fe60SDimitry Andric continue; 15842754fe60SDimitry Andric 15852754fe60SDimitry Andric if (Spilled) { 15862754fe60SDimitry Andric NumGPRSpills++; 15872754fe60SDimitry Andric 158891bc56edSDimitry Andric if (!STI.isTargetDarwin()) { 15892754fe60SDimitry Andric if (Reg == ARM::LR) 15902754fe60SDimitry Andric LRSpilled = true; 15912754fe60SDimitry Andric CS1Spilled = true; 15922754fe60SDimitry Andric continue; 15932754fe60SDimitry Andric } 15942754fe60SDimitry Andric 15952754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 15962754fe60SDimitry Andric switch (Reg) { 15972754fe60SDimitry Andric case ARM::LR: 15982754fe60SDimitry Andric LRSpilled = true; 15992754fe60SDimitry Andric // Fallthrough 1600f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1601f785676fSDimitry Andric case ARM::R2: case ARM::R3: 16022754fe60SDimitry Andric case ARM::R4: case ARM::R5: 16032754fe60SDimitry Andric case ARM::R6: case ARM::R7: 16042754fe60SDimitry Andric CS1Spilled = true; 16052754fe60SDimitry Andric break; 16062754fe60SDimitry Andric default: 16072754fe60SDimitry Andric break; 16082754fe60SDimitry Andric } 16092754fe60SDimitry Andric } else { 161091bc56edSDimitry Andric if (!STI.isTargetDarwin()) { 16112754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 16122754fe60SDimitry Andric continue; 16132754fe60SDimitry Andric } 16142754fe60SDimitry Andric 16152754fe60SDimitry Andric switch (Reg) { 1616f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1617f785676fSDimitry Andric case ARM::R2: case ARM::R3: 16182754fe60SDimitry Andric case ARM::R4: case ARM::R5: 16192754fe60SDimitry Andric case ARM::R6: case ARM::R7: 16202754fe60SDimitry Andric case ARM::LR: 16212754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 16222754fe60SDimitry Andric break; 16232754fe60SDimitry Andric default: 16242754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 16252754fe60SDimitry Andric break; 16262754fe60SDimitry Andric } 16272754fe60SDimitry Andric } 16282754fe60SDimitry Andric } 16292754fe60SDimitry Andric 16302754fe60SDimitry Andric bool ForceLRSpill = false; 16312754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 16322754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 16332754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 16342754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 16352754fe60SDimitry Andric // then the branch fix up path will undo it. 16362754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 16372754fe60SDimitry Andric CanEliminateFrame = false; 16382754fe60SDimitry Andric ForceLRSpill = true; 16392754fe60SDimitry Andric } 16402754fe60SDimitry Andric } 16412754fe60SDimitry Andric 16422754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 16432754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 16442754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 16452754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 16462754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 16472754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 16482754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 16492754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 16502754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 16512754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 16522754fe60SDimitry Andric // calculations may be off, so be conservative. 16532754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 16542754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 16552754fe60SDimitry Andric // worth the effort and added fragility? 16562754fe60SDimitry Andric bool BigStack = 16572754fe60SDimitry Andric (RS && 1658139f7f9bSDimitry Andric (MFI->estimateStackSize(MF) + 1659139f7f9bSDimitry Andric ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 16602754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 16612754fe60SDimitry Andric || MFI->hasVarSizedObjects() 16622754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 16632754fe60SDimitry Andric 16642754fe60SDimitry Andric bool ExtraCSSpill = false; 16652754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 16662754fe60SDimitry Andric AFI->setHasStackFrame(true); 16672754fe60SDimitry Andric 16682754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 16692754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 16702754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 16713861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 16722754fe60SDimitry Andric NumGPRSpills++; 1673f785676fSDimitry Andric SmallVectorImpl<unsigned>::iterator LRPos; 1674f785676fSDimitry Andric LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), 1675f785676fSDimitry Andric (unsigned)ARM::LR); 1676f785676fSDimitry Andric if (LRPos != UnspilledCS1GPRs.end()) 1677f785676fSDimitry Andric UnspilledCS1GPRs.erase(LRPos); 1678f785676fSDimitry Andric 16792754fe60SDimitry Andric ForceLRSpill = false; 16802754fe60SDimitry Andric ExtraCSSpill = true; 16812754fe60SDimitry Andric } 16822754fe60SDimitry Andric 16832754fe60SDimitry Andric if (hasFP(MF)) { 16843861d79fSDimitry Andric MRI.setPhysRegUsed(FramePtr); 168591bc56edSDimitry Andric auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), 168691bc56edSDimitry Andric FramePtr); 168791bc56edSDimitry Andric if (FPPos != UnspilledCS1GPRs.end()) 168891bc56edSDimitry Andric UnspilledCS1GPRs.erase(FPPos); 16892754fe60SDimitry Andric NumGPRSpills++; 16902754fe60SDimitry Andric } 16912754fe60SDimitry Andric 16922754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 16932754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 16942754fe60SDimitry Andric // the integer and double callee save areas. 16952754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 169639d628a0SDimitry Andric if (TargetAlign >= 8 && (NumGPRSpills & 1)) { 16972754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 16982754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 16992754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 1700ff0cc061SDimitry Andric // Don't spill high register if the function is thumb 1701ff0cc061SDimitry Andric if (!AFI->isThumbFunction() || 17022754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 17033861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 17043861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 17052754fe60SDimitry Andric ExtraCSSpill = true; 17062754fe60SDimitry Andric break; 17072754fe60SDimitry Andric } 17082754fe60SDimitry Andric } 17092754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 17102754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 17113861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 17123861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 17132754fe60SDimitry Andric ExtraCSSpill = true; 17142754fe60SDimitry Andric } 17152754fe60SDimitry Andric } 17162754fe60SDimitry Andric 17172754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 17182754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 17192754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 17202754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 17212754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 17222754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 17232754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 17242754fe60SDimitry Andric // extra. That should take care of it! 17252754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 17262754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 17272754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 17282754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 17292754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 17303861d79fSDimitry Andric if (!MRI.isReserved(Reg) && 17312754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 17322754fe60SDimitry Andric Reg == ARM::LR)) { 17332754fe60SDimitry Andric Extras.push_back(Reg); 17342754fe60SDimitry Andric NumExtras--; 17352754fe60SDimitry Andric } 17362754fe60SDimitry Andric } 17372754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 17382754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 17392754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 17402754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 17412754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 17423861d79fSDimitry Andric if (!MRI.isReserved(Reg)) { 17432754fe60SDimitry Andric Extras.push_back(Reg); 17442754fe60SDimitry Andric NumExtras--; 17452754fe60SDimitry Andric } 17462754fe60SDimitry Andric } 17472754fe60SDimitry Andric } 17482754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 17492754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 17503861d79fSDimitry Andric MRI.setPhysRegUsed(Extras[i]); 17512754fe60SDimitry Andric } 17522754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 17532754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 17542754fe60SDimitry Andric // closest to SP or frame pointer. 17557ae0e2c9SDimitry Andric const TargetRegisterClass *RC = &ARM::GPRRegClass; 1756139f7f9bSDimitry Andric RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 17572754fe60SDimitry Andric RC->getAlignment(), 17582754fe60SDimitry Andric false)); 17592754fe60SDimitry Andric } 17602754fe60SDimitry Andric } 17612754fe60SDimitry Andric } 17622754fe60SDimitry Andric 17632754fe60SDimitry Andric if (ForceLRSpill) { 17643861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 17652754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 17662754fe60SDimitry Andric } 17672754fe60SDimitry Andric } 1768139f7f9bSDimitry Andric 1769139f7f9bSDimitry Andric 1770139f7f9bSDimitry Andric void ARMFrameLowering:: 1771139f7f9bSDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1772139f7f9bSDimitry Andric MachineBasicBlock::iterator I) const { 1773139f7f9bSDimitry Andric const ARMBaseInstrInfo &TII = 177439d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 1775139f7f9bSDimitry Andric if (!hasReservedCallFrame(MF)) { 1776139f7f9bSDimitry Andric // If we have alloca, convert as follows: 1777139f7f9bSDimitry Andric // ADJCALLSTACKDOWN -> sub, sp, sp, amount 1778139f7f9bSDimitry Andric // ADJCALLSTACKUP -> add, sp, sp, amount 1779139f7f9bSDimitry Andric MachineInstr *Old = I; 1780139f7f9bSDimitry Andric DebugLoc dl = Old->getDebugLoc(); 1781139f7f9bSDimitry Andric unsigned Amount = Old->getOperand(0).getImm(); 1782139f7f9bSDimitry Andric if (Amount != 0) { 1783139f7f9bSDimitry Andric // We need to keep the stack aligned properly. To do this, we round the 1784139f7f9bSDimitry Andric // amount of space needed for the outgoing arguments up to the next 1785139f7f9bSDimitry Andric // alignment boundary. 1786139f7f9bSDimitry Andric unsigned Align = getStackAlignment(); 1787139f7f9bSDimitry Andric Amount = (Amount+Align-1)/Align*Align; 1788139f7f9bSDimitry Andric 1789139f7f9bSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1790139f7f9bSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1791139f7f9bSDimitry Andric "This eliminateCallFramePseudoInstr does not support Thumb1!"); 1792139f7f9bSDimitry Andric bool isARM = !AFI->isThumbFunction(); 1793139f7f9bSDimitry Andric 1794139f7f9bSDimitry Andric // Replace the pseudo instruction with a new instruction... 1795139f7f9bSDimitry Andric unsigned Opc = Old->getOpcode(); 1796139f7f9bSDimitry Andric int PIdx = Old->findFirstPredOperandIdx(); 1797139f7f9bSDimitry Andric ARMCC::CondCodes Pred = (PIdx == -1) 1798139f7f9bSDimitry Andric ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); 1799139f7f9bSDimitry Andric if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { 1800139f7f9bSDimitry Andric // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. 1801139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(2).getReg(); 1802139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, 1803139f7f9bSDimitry Andric Pred, PredReg); 1804139f7f9bSDimitry Andric } else { 1805139f7f9bSDimitry Andric // Note: PredReg is operand 3 for ADJCALLSTACKUP. 1806139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(3).getReg(); 1807139f7f9bSDimitry Andric assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); 1808139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, 1809139f7f9bSDimitry Andric Pred, PredReg); 1810139f7f9bSDimitry Andric } 1811139f7f9bSDimitry Andric } 1812139f7f9bSDimitry Andric } 1813139f7f9bSDimitry Andric MBB.erase(I); 1814139f7f9bSDimitry Andric } 1815139f7f9bSDimitry Andric 181691bc56edSDimitry Andric /// Get the minimum constant for ARM that is greater than or equal to the 181791bc56edSDimitry Andric /// argument. In ARM, constants can have any value that can be produced by 181891bc56edSDimitry Andric /// rotating an 8-bit value to the right by an even number of bits within a 181991bc56edSDimitry Andric /// 32-bit word. 182091bc56edSDimitry Andric static uint32_t alignToARMConstant(uint32_t Value) { 182191bc56edSDimitry Andric unsigned Shifted = 0; 182291bc56edSDimitry Andric 182391bc56edSDimitry Andric if (Value == 0) 182491bc56edSDimitry Andric return 0; 182591bc56edSDimitry Andric 182691bc56edSDimitry Andric while (!(Value & 0xC0000000)) { 182791bc56edSDimitry Andric Value = Value << 2; 182891bc56edSDimitry Andric Shifted += 2; 182991bc56edSDimitry Andric } 183091bc56edSDimitry Andric 183191bc56edSDimitry Andric bool Carry = (Value & 0x00FFFFFF); 183291bc56edSDimitry Andric Value = ((Value & 0xFF000000) >> 24) + Carry; 183391bc56edSDimitry Andric 183491bc56edSDimitry Andric if (Value & 0x0000100) 183591bc56edSDimitry Andric Value = Value & 0x000001FC; 183691bc56edSDimitry Andric 183791bc56edSDimitry Andric if (Shifted > 24) 183891bc56edSDimitry Andric Value = Value >> (Shifted - 24); 183991bc56edSDimitry Andric else 184091bc56edSDimitry Andric Value = Value << (24 - Shifted); 184191bc56edSDimitry Andric 184291bc56edSDimitry Andric return Value; 184391bc56edSDimitry Andric } 184491bc56edSDimitry Andric 184591bc56edSDimitry Andric // The stack limit in the TCB is set to this many bytes above the actual 184691bc56edSDimitry Andric // stack limit. 184791bc56edSDimitry Andric static const uint64_t kSplitStackAvailable = 256; 184891bc56edSDimitry Andric 184991bc56edSDimitry Andric // Adjust the function prologue to enable split stacks. This currently only 185091bc56edSDimitry Andric // supports android and linux. 185191bc56edSDimitry Andric // 185291bc56edSDimitry Andric // The ABI of the segmented stack prologue is a little arbitrarily chosen, but 185391bc56edSDimitry Andric // must be well defined in order to allow for consistent implementations of the 185491bc56edSDimitry Andric // __morestack helper function. The ABI is also not a normal ABI in that it 185591bc56edSDimitry Andric // doesn't follow the normal calling conventions because this allows the 185691bc56edSDimitry Andric // prologue of each function to be optimized further. 185791bc56edSDimitry Andric // 185891bc56edSDimitry Andric // Currently, the ABI looks like (when calling __morestack) 185991bc56edSDimitry Andric // 186091bc56edSDimitry Andric // * r4 holds the minimum stack size requested for this function call 186191bc56edSDimitry Andric // * r5 holds the stack size of the arguments to the function 186291bc56edSDimitry Andric // * the beginning of the function is 3 instructions after the call to 186391bc56edSDimitry Andric // __morestack 186491bc56edSDimitry Andric // 186591bc56edSDimitry Andric // Implementations of __morestack should use r4 to allocate a new stack, r5 to 186691bc56edSDimitry Andric // place the arguments on to the new stack, and the 3-instruction knowledge to 186791bc56edSDimitry Andric // jump directly to the body of the function when working on the new stack. 186891bc56edSDimitry Andric // 186991bc56edSDimitry Andric // An old (and possibly no longer compatible) implementation of __morestack for 187091bc56edSDimitry Andric // ARM can be found at [1]. 187191bc56edSDimitry Andric // 187291bc56edSDimitry Andric // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S 1873ff0cc061SDimitry Andric void ARMFrameLowering::adjustForSegmentedStacks( 1874ff0cc061SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologueMBB) const { 187591bc56edSDimitry Andric unsigned Opcode; 187691bc56edSDimitry Andric unsigned CFIIndex; 1877ff0cc061SDimitry Andric const ARMSubtarget *ST = &MF.getSubtarget<ARMSubtarget>(); 187891bc56edSDimitry Andric bool Thumb = ST->isThumb(); 187991bc56edSDimitry Andric 188091bc56edSDimitry Andric // Sadly, this currently doesn't support varargs, platforms other than 188191bc56edSDimitry Andric // android/linux. Note that thumb1/thumb2 are support for android/linux. 188291bc56edSDimitry Andric if (MF.getFunction()->isVarArg()) 188391bc56edSDimitry Andric report_fatal_error("Segmented stacks do not support vararg functions."); 188491bc56edSDimitry Andric if (!ST->isTargetAndroid() && !ST->isTargetLinux()) 188591bc56edSDimitry Andric report_fatal_error("Segmented stacks not supported on this platform."); 188691bc56edSDimitry Andric 1887ff0cc061SDimitry Andric assert(&PrologueMBB == &MF.front() && "Shrink-wrapping not yet implemented"); 188891bc56edSDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 188991bc56edSDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 189091bc56edSDimitry Andric MCContext &Context = MMI.getContext(); 189191bc56edSDimitry Andric const MCRegisterInfo *MRI = Context.getRegisterInfo(); 189291bc56edSDimitry Andric const ARMBaseInstrInfo &TII = 189339d628a0SDimitry Andric *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); 189491bc56edSDimitry Andric ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>(); 189591bc56edSDimitry Andric DebugLoc DL; 189691bc56edSDimitry Andric 189791bc56edSDimitry Andric uint64_t StackSize = MFI->getStackSize(); 189891bc56edSDimitry Andric 189991bc56edSDimitry Andric // Do not generate a prologue for functions with a stack of size zero 190091bc56edSDimitry Andric if (StackSize == 0) 190191bc56edSDimitry Andric return; 190291bc56edSDimitry Andric 190391bc56edSDimitry Andric // Use R4 and R5 as scratch registers. 190491bc56edSDimitry Andric // We save R4 and R5 before use and restore them before leaving the function. 190591bc56edSDimitry Andric unsigned ScratchReg0 = ARM::R4; 190691bc56edSDimitry Andric unsigned ScratchReg1 = ARM::R5; 190791bc56edSDimitry Andric uint64_t AlignedStackSize; 190891bc56edSDimitry Andric 190991bc56edSDimitry Andric MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock(); 191091bc56edSDimitry Andric MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock(); 191191bc56edSDimitry Andric MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock(); 191291bc56edSDimitry Andric MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock(); 191391bc56edSDimitry Andric MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock(); 191491bc56edSDimitry Andric 1915ff0cc061SDimitry Andric for (MachineBasicBlock::livein_iterator i = PrologueMBB.livein_begin(), 1916ff0cc061SDimitry Andric e = PrologueMBB.livein_end(); 191791bc56edSDimitry Andric i != e; ++i) { 191891bc56edSDimitry Andric AllocMBB->addLiveIn(*i); 191991bc56edSDimitry Andric GetMBB->addLiveIn(*i); 192091bc56edSDimitry Andric McrMBB->addLiveIn(*i); 192191bc56edSDimitry Andric PrevStackMBB->addLiveIn(*i); 192291bc56edSDimitry Andric PostStackMBB->addLiveIn(*i); 192391bc56edSDimitry Andric } 192491bc56edSDimitry Andric 192591bc56edSDimitry Andric MF.push_front(PostStackMBB); 192691bc56edSDimitry Andric MF.push_front(AllocMBB); 192791bc56edSDimitry Andric MF.push_front(GetMBB); 192891bc56edSDimitry Andric MF.push_front(McrMBB); 192991bc56edSDimitry Andric MF.push_front(PrevStackMBB); 193091bc56edSDimitry Andric 193191bc56edSDimitry Andric // The required stack size that is aligned to ARM constant criterion. 193291bc56edSDimitry Andric AlignedStackSize = alignToARMConstant(StackSize); 193391bc56edSDimitry Andric 193491bc56edSDimitry Andric // When the frame size is less than 256 we just compare the stack 193591bc56edSDimitry Andric // boundary directly to the value of the stack pointer, per gcc. 193691bc56edSDimitry Andric bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable; 193791bc56edSDimitry Andric 193891bc56edSDimitry Andric // We will use two of the callee save registers as scratch registers so we 193991bc56edSDimitry Andric // need to save those registers onto the stack. 194091bc56edSDimitry Andric // We will use SR0 to hold stack limit and SR1 to hold the stack size 194191bc56edSDimitry Andric // requested and arguments for __morestack(). 194291bc56edSDimitry Andric // SR0: Scratch Register #0 194391bc56edSDimitry Andric // SR1: Scratch Register #1 194491bc56edSDimitry Andric // push {SR0, SR1} 194591bc56edSDimitry Andric if (Thumb) { 194691bc56edSDimitry Andric AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))) 194791bc56edSDimitry Andric .addReg(ScratchReg0).addReg(ScratchReg1); 194891bc56edSDimitry Andric } else { 194991bc56edSDimitry Andric AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD)) 195091bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define).addReg(ARM::SP)) 195191bc56edSDimitry Andric .addReg(ScratchReg0).addReg(ScratchReg1); 195291bc56edSDimitry Andric } 195391bc56edSDimitry Andric 195491bc56edSDimitry Andric // Emit the relevant DWARF information about the change in stack pointer as 195591bc56edSDimitry Andric // well as where to find both r4 and r5 (the callee-save registers) 195691bc56edSDimitry Andric CFIIndex = 195791bc56edSDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8)); 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(ScratchReg1, true), -4)); 196291bc56edSDimitry Andric BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 196391bc56edSDimitry Andric .addCFIIndex(CFIIndex); 196491bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 196591bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8)); 196691bc56edSDimitry Andric BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 196791bc56edSDimitry Andric .addCFIIndex(CFIIndex); 196891bc56edSDimitry Andric 196991bc56edSDimitry Andric // mov SR1, sp 197091bc56edSDimitry Andric if (Thumb) { 197191bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1) 197291bc56edSDimitry Andric .addReg(ARM::SP)); 197391bc56edSDimitry Andric } else if (CompareStackPointer) { 197491bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1) 197591bc56edSDimitry Andric .addReg(ARM::SP)).addReg(0); 197691bc56edSDimitry Andric } 197791bc56edSDimitry Andric 197891bc56edSDimitry Andric // sub SR1, sp, #StackSize 197991bc56edSDimitry Andric if (!CompareStackPointer && Thumb) { 198091bc56edSDimitry Andric AddDefaultPred( 198191bc56edSDimitry Andric AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)) 198291bc56edSDimitry Andric .addReg(ScratchReg1).addImm(AlignedStackSize)); 198391bc56edSDimitry Andric } else if (!CompareStackPointer) { 198491bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1) 198591bc56edSDimitry Andric .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0); 198691bc56edSDimitry Andric } 198791bc56edSDimitry Andric 198891bc56edSDimitry Andric if (Thumb && ST->isThumb1Only()) { 198991bc56edSDimitry Andric unsigned PCLabelId = ARMFI->createPICLabelUId(); 199091bc56edSDimitry Andric ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create( 199191bc56edSDimitry Andric MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0); 199291bc56edSDimitry Andric MachineConstantPool *MCP = MF.getConstantPool(); 199391bc56edSDimitry Andric unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment()); 199491bc56edSDimitry Andric 199591bc56edSDimitry Andric // ldr SR0, [pc, offset(STACK_LIMIT)] 199691bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0) 199791bc56edSDimitry Andric .addConstantPoolIndex(CPI)); 199891bc56edSDimitry Andric 199991bc56edSDimitry Andric // ldr SR0, [SR0] 200091bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0) 200191bc56edSDimitry Andric .addReg(ScratchReg0).addImm(0)); 200291bc56edSDimitry Andric } else { 200391bc56edSDimitry Andric // Get TLS base address from the coprocessor 200491bc56edSDimitry Andric // mrc p15, #0, SR0, c13, c0, #3 200591bc56edSDimitry Andric AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0) 200691bc56edSDimitry Andric .addImm(15) 200791bc56edSDimitry Andric .addImm(0) 200891bc56edSDimitry Andric .addImm(13) 200991bc56edSDimitry Andric .addImm(0) 201091bc56edSDimitry Andric .addImm(3)); 201191bc56edSDimitry Andric 201291bc56edSDimitry Andric // Use the last tls slot on android and a private field of the TCP on linux. 201391bc56edSDimitry Andric assert(ST->isTargetAndroid() || ST->isTargetLinux()); 201491bc56edSDimitry Andric unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1; 201591bc56edSDimitry Andric 201691bc56edSDimitry Andric // Get the stack limit from the right offset 201791bc56edSDimitry Andric // ldr SR0, [sr0, #4 * TlsOffset] 201891bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0) 201991bc56edSDimitry Andric .addReg(ScratchReg0).addImm(4 * TlsOffset)); 202091bc56edSDimitry Andric } 202191bc56edSDimitry Andric 202291bc56edSDimitry Andric // Compare stack limit with stack size requested. 202391bc56edSDimitry Andric // cmp SR0, SR1 202491bc56edSDimitry Andric Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr; 202591bc56edSDimitry Andric AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode)) 202691bc56edSDimitry Andric .addReg(ScratchReg0) 202791bc56edSDimitry Andric .addReg(ScratchReg1)); 202891bc56edSDimitry Andric 202991bc56edSDimitry Andric // This jump is taken if StackLimit < SP - stack required. 203091bc56edSDimitry Andric Opcode = Thumb ? ARM::tBcc : ARM::Bcc; 203191bc56edSDimitry Andric BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB) 203291bc56edSDimitry Andric .addImm(ARMCC::LO) 203391bc56edSDimitry Andric .addReg(ARM::CPSR); 203491bc56edSDimitry Andric 203591bc56edSDimitry Andric 203691bc56edSDimitry Andric // Calling __morestack(StackSize, Size of stack arguments). 203791bc56edSDimitry Andric // __morestack knows that the stack size requested is in SR0(r4) 203891bc56edSDimitry Andric // and amount size of stack arguments is in SR1(r5). 203991bc56edSDimitry Andric 204091bc56edSDimitry Andric // Pass first argument for the __morestack by Scratch Register #0. 204191bc56edSDimitry Andric // The amount size of stack required 204291bc56edSDimitry Andric if (Thumb) { 204391bc56edSDimitry Andric AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), 204491bc56edSDimitry Andric ScratchReg0)).addImm(AlignedStackSize)); 204591bc56edSDimitry Andric } else { 204691bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0) 204791bc56edSDimitry Andric .addImm(AlignedStackSize)).addReg(0); 204891bc56edSDimitry Andric } 204991bc56edSDimitry Andric // Pass second argument for the __morestack by Scratch Register #1. 205091bc56edSDimitry Andric // The amount size of stack consumed to save function arguments. 205191bc56edSDimitry Andric if (Thumb) { 205291bc56edSDimitry Andric AddDefaultPred( 205391bc56edSDimitry Andric AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)) 205491bc56edSDimitry Andric .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))); 205591bc56edSDimitry Andric } else { 205691bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1) 205791bc56edSDimitry Andric .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))) 205891bc56edSDimitry Andric .addReg(0); 205991bc56edSDimitry Andric } 206091bc56edSDimitry Andric 206191bc56edSDimitry Andric // push {lr} - Save return address of this function. 206291bc56edSDimitry Andric if (Thumb) { 206391bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))) 206491bc56edSDimitry Andric .addReg(ARM::LR); 206591bc56edSDimitry Andric } else { 206691bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD)) 206791bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 206891bc56edSDimitry Andric .addReg(ARM::SP)) 206991bc56edSDimitry Andric .addReg(ARM::LR); 207091bc56edSDimitry Andric } 207191bc56edSDimitry Andric 207291bc56edSDimitry Andric // Emit the DWARF info about the change in stack as well as where to find the 207391bc56edSDimitry Andric // previous link register 207491bc56edSDimitry Andric CFIIndex = 207591bc56edSDimitry Andric MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12)); 207691bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 207791bc56edSDimitry Andric .addCFIIndex(CFIIndex); 207891bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 207991bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12)); 208091bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 208191bc56edSDimitry Andric .addCFIIndex(CFIIndex); 208291bc56edSDimitry Andric 208391bc56edSDimitry Andric // Call __morestack(). 208491bc56edSDimitry Andric if (Thumb) { 208591bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL))) 208691bc56edSDimitry Andric .addExternalSymbol("__morestack"); 208791bc56edSDimitry Andric } else { 208891bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(ARM::BL)) 208991bc56edSDimitry Andric .addExternalSymbol("__morestack"); 209091bc56edSDimitry Andric } 209191bc56edSDimitry Andric 209291bc56edSDimitry Andric // pop {lr} - Restore return address of this original function. 209391bc56edSDimitry Andric if (Thumb) { 209491bc56edSDimitry Andric if (ST->isThumb1Only()) { 209591bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) 209691bc56edSDimitry Andric .addReg(ScratchReg0); 209791bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR) 209891bc56edSDimitry Andric .addReg(ScratchReg0)); 209991bc56edSDimitry Andric } else { 210091bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST)) 210191bc56edSDimitry Andric .addReg(ARM::LR, RegState::Define) 210291bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 210391bc56edSDimitry Andric .addReg(ARM::SP) 210491bc56edSDimitry Andric .addImm(4)); 210591bc56edSDimitry Andric } 210691bc56edSDimitry Andric } else { 210791bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) 210891bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 210991bc56edSDimitry Andric .addReg(ARM::SP)) 211091bc56edSDimitry Andric .addReg(ARM::LR); 211191bc56edSDimitry Andric } 211291bc56edSDimitry Andric 211391bc56edSDimitry Andric // Restore SR0 and SR1 in case of __morestack() was called. 211491bc56edSDimitry Andric // __morestack() will skip PostStackMBB block so we need to restore 211591bc56edSDimitry Andric // scratch registers from here. 211691bc56edSDimitry Andric // pop {SR0, SR1} 211791bc56edSDimitry Andric if (Thumb) { 211891bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) 211991bc56edSDimitry Andric .addReg(ScratchReg0) 212091bc56edSDimitry Andric .addReg(ScratchReg1); 212191bc56edSDimitry Andric } else { 212291bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) 212391bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 212491bc56edSDimitry Andric .addReg(ARM::SP)) 212591bc56edSDimitry Andric .addReg(ScratchReg0) 212691bc56edSDimitry Andric .addReg(ScratchReg1); 212791bc56edSDimitry Andric } 212891bc56edSDimitry Andric 212991bc56edSDimitry Andric // Update the CFA offset now that we've popped 213091bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); 213191bc56edSDimitry Andric BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 213291bc56edSDimitry Andric .addCFIIndex(CFIIndex); 213391bc56edSDimitry Andric 213491bc56edSDimitry Andric // bx lr - Return from this function. 213591bc56edSDimitry Andric Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET; 213691bc56edSDimitry Andric AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode))); 213791bc56edSDimitry Andric 213891bc56edSDimitry Andric // Restore SR0 and SR1 in case of __morestack() was not called. 213991bc56edSDimitry Andric // pop {SR0, SR1} 214091bc56edSDimitry Andric if (Thumb) { 214191bc56edSDimitry Andric AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))) 214291bc56edSDimitry Andric .addReg(ScratchReg0) 214391bc56edSDimitry Andric .addReg(ScratchReg1); 214491bc56edSDimitry Andric } else { 214591bc56edSDimitry Andric AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD)) 214691bc56edSDimitry Andric .addReg(ARM::SP, RegState::Define) 214791bc56edSDimitry Andric .addReg(ARM::SP)) 214891bc56edSDimitry Andric .addReg(ScratchReg0) 214991bc56edSDimitry Andric .addReg(ScratchReg1); 215091bc56edSDimitry Andric } 215191bc56edSDimitry Andric 215291bc56edSDimitry Andric // Update the CFA offset now that we've popped 215391bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); 215491bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 215591bc56edSDimitry Andric .addCFIIndex(CFIIndex); 215691bc56edSDimitry Andric 215791bc56edSDimitry Andric // Tell debuggers that r4 and r5 are now the same as they were in the 215891bc56edSDimitry Andric // previous function, that they're the "Same Value". 215991bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( 216091bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg0, true))); 216191bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 216291bc56edSDimitry Andric .addCFIIndex(CFIIndex); 216391bc56edSDimitry Andric CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( 216491bc56edSDimitry Andric nullptr, MRI->getDwarfRegNum(ScratchReg1, true))); 216591bc56edSDimitry Andric BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 216691bc56edSDimitry Andric .addCFIIndex(CFIIndex); 216791bc56edSDimitry Andric 216891bc56edSDimitry Andric // Organizing MBB lists 2169ff0cc061SDimitry Andric PostStackMBB->addSuccessor(&PrologueMBB); 217091bc56edSDimitry Andric 217191bc56edSDimitry Andric AllocMBB->addSuccessor(PostStackMBB); 217291bc56edSDimitry Andric 217391bc56edSDimitry Andric GetMBB->addSuccessor(PostStackMBB); 217491bc56edSDimitry Andric GetMBB->addSuccessor(AllocMBB); 217591bc56edSDimitry Andric 217691bc56edSDimitry Andric McrMBB->addSuccessor(GetMBB); 217791bc56edSDimitry Andric 217891bc56edSDimitry Andric PrevStackMBB->addSuccessor(McrMBB); 217991bc56edSDimitry Andric 218091bc56edSDimitry Andric #ifdef XDEBUG 218191bc56edSDimitry Andric MF.verify(); 218291bc56edSDimitry Andric #endif 218391bc56edSDimitry Andric } 2184