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" 172754fe60SDimitry Andric #include "ARMMachineFunctionInfo.h" 186122f3e6SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h" 192754fe60SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 202754fe60SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 212754fe60SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 222754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 232754fe60SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h" 24139f7f9bSDimitry Andric #include "llvm/IR/CallingConv.h" 25139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 26dff0c46cSDimitry Andric #include "llvm/Support/CommandLine.h" 27139f7f9bSDimitry Andric #include "llvm/Target/TargetOptions.h" 282754fe60SDimitry Andric 292754fe60SDimitry Andric using namespace llvm; 302754fe60SDimitry Andric 31dff0c46cSDimitry Andric static cl::opt<bool> 32dff0c46cSDimitry Andric SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true), 33dff0c46cSDimitry Andric cl::desc("Align ARM NEON spills in prolog and epilog")); 34dff0c46cSDimitry Andric 35dff0c46cSDimitry Andric static MachineBasicBlock::iterator 36dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 37dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs); 38dff0c46cSDimitry Andric 392754fe60SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 402754fe60SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 412754fe60SDimitry Andric /// or if frame pointer elimination is disabled. 422754fe60SDimitry Andric bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { 432754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 442754fe60SDimitry Andric 45dff0c46cSDimitry Andric // iOS requires FP not to be clobbered for backtracing purpose. 46dff0c46cSDimitry Andric if (STI.isTargetIOS()) 472754fe60SDimitry Andric return true; 482754fe60SDimitry Andric 492754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 502754fe60SDimitry Andric // Always eliminate non-leaf frame pointers. 51dff0c46cSDimitry Andric return ((MF.getTarget().Options.DisableFramePointerElim(MF) && 52dff0c46cSDimitry Andric MFI->hasCalls()) || 532754fe60SDimitry Andric RegInfo->needsStackRealignment(MF) || 542754fe60SDimitry Andric MFI->hasVarSizedObjects() || 552754fe60SDimitry Andric MFI->isFrameAddressTaken()); 562754fe60SDimitry Andric } 572754fe60SDimitry Andric 582754fe60SDimitry Andric /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is 592754fe60SDimitry Andric /// not required, we reserve argument space for call sites in the function 602754fe60SDimitry Andric /// immediately on entry to the current function. This eliminates the need for 612754fe60SDimitry Andric /// add/sub sp brackets around call sites. Returns true if the call frame is 622754fe60SDimitry Andric /// included as part of the stack frame. 632754fe60SDimitry Andric bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 642754fe60SDimitry Andric const MachineFrameInfo *FFI = MF.getFrameInfo(); 652754fe60SDimitry Andric unsigned CFSize = FFI->getMaxCallFrameSize(); 662754fe60SDimitry Andric // It's not always a good idea to include the call frame as part of the 672754fe60SDimitry Andric // stack frame. ARM (especially Thumb) has small immediate offset to 682754fe60SDimitry Andric // address the stack frame. So a large call frame can cause poor codegen 692754fe60SDimitry Andric // and may even makes it impossible to scavenge a register. 702754fe60SDimitry Andric if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 712754fe60SDimitry Andric return false; 722754fe60SDimitry Andric 732754fe60SDimitry Andric return !MF.getFrameInfo()->hasVarSizedObjects(); 742754fe60SDimitry Andric } 752754fe60SDimitry Andric 762754fe60SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 772754fe60SDimitry Andric /// call frame pseudos can be simplified. Unlike most targets, having a FP 782754fe60SDimitry Andric /// is not sufficient here since we still may reference some objects via SP 792754fe60SDimitry Andric /// even when FP is available in Thumb2 mode. 802754fe60SDimitry Andric bool 812754fe60SDimitry Andric ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 822754fe60SDimitry Andric return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); 832754fe60SDimitry Andric } 842754fe60SDimitry Andric 852754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 862754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 87dff0c46cSDimitry Andric const uint16_t *CSRegs) { 882754fe60SDimitry Andric // Integer spill area is handled with "pop". 89f785676fSDimitry Andric if (isPopOpcode(MI->getOpcode())) { 902754fe60SDimitry Andric // The first two operands are predicates. The last two are 912754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 922754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 932754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 942754fe60SDimitry Andric return false; 952754fe60SDimitry Andric return true; 962754fe60SDimitry Andric } 976122f3e6SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST_IMM || 986122f3e6SDimitry Andric MI->getOpcode() == ARM::LDR_POST_REG || 992754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 1002754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 1012754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1022754fe60SDimitry Andric return true; 1032754fe60SDimitry Andric 1042754fe60SDimitry Andric return false; 1052754fe60SDimitry Andric } 1062754fe60SDimitry Andric 107f785676fSDimitry Andric static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB, 108f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 109f785676fSDimitry Andric const ARMBaseInstrInfo &TII, unsigned DestReg, 110f785676fSDimitry Andric unsigned SrcReg, int NumBytes, 111f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 112f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 113f785676fSDimitry Andric unsigned PredReg = 0) { 1142754fe60SDimitry Andric if (isARM) 115f785676fSDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 116139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1172754fe60SDimitry Andric else 118f785676fSDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, 119139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1202754fe60SDimitry Andric } 1212754fe60SDimitry Andric 122f785676fSDimitry Andric static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB, 123f785676fSDimitry Andric MachineBasicBlock::iterator &MBBI, DebugLoc dl, 124f785676fSDimitry Andric const ARMBaseInstrInfo &TII, int NumBytes, 125f785676fSDimitry Andric unsigned MIFlags = MachineInstr::NoFlags, 126f785676fSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, 127f785676fSDimitry Andric unsigned PredReg = 0) { 128f785676fSDimitry Andric emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes, 129f785676fSDimitry Andric MIFlags, Pred, PredReg); 130f785676fSDimitry Andric } 131f785676fSDimitry Andric 1322754fe60SDimitry Andric void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { 1332754fe60SDimitry Andric MachineBasicBlock &MBB = MF.front(); 1342754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 1352754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 1362754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1372754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 1382754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 1392754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 1402754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 1412754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1422754fe60SDimitry Andric "This emitPrologue does not support Thumb1!"); 1432754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 144f785676fSDimitry Andric unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment(); 145f785676fSDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); 1462754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 1472754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 1482754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1492754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 1502754fe60SDimitry Andric 1512754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 1522754fe60SDimitry Andric // belongs to which callee-save spill areas. 1532754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 1542754fe60SDimitry Andric int FramePtrSpillFI = 0; 155dff0c46cSDimitry Andric int D8SpillFI = 0; 156f8254f43SDimitry Andric 1573861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 1583861d79fSDimitry Andric // prologue/epilogue. 1597ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 1607ae0e2c9SDimitry Andric return; 1617ae0e2c9SDimitry Andric 1622754fe60SDimitry Andric // Allocate the vararg register save area. This is not counted in NumBytes. 163284c1978SDimitry Andric if (ArgRegsSaveSize) 164284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, 1653b0f4066SDimitry Andric MachineInstr::FrameSetup); 1662754fe60SDimitry Andric 1672754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 1682754fe60SDimitry Andric if (NumBytes != 0) 1693b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 1703b0f4066SDimitry Andric MachineInstr::FrameSetup); 1712754fe60SDimitry Andric return; 1722754fe60SDimitry Andric } 1732754fe60SDimitry Andric 1742754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1752754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 1762754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 1772754fe60SDimitry Andric switch (Reg) { 178f785676fSDimitry Andric case ARM::R0: 179f785676fSDimitry Andric case ARM::R1: 180f785676fSDimitry Andric case ARM::R2: 181f785676fSDimitry Andric case ARM::R3: 1822754fe60SDimitry Andric case ARM::R4: 1832754fe60SDimitry Andric case ARM::R5: 1842754fe60SDimitry Andric case ARM::R6: 1852754fe60SDimitry Andric case ARM::R7: 1862754fe60SDimitry Andric case ARM::LR: 1872754fe60SDimitry Andric if (Reg == FramePtr) 1882754fe60SDimitry Andric FramePtrSpillFI = FI; 1892754fe60SDimitry Andric GPRCS1Size += 4; 1902754fe60SDimitry Andric break; 1912754fe60SDimitry Andric case ARM::R8: 1922754fe60SDimitry Andric case ARM::R9: 1932754fe60SDimitry Andric case ARM::R10: 1942754fe60SDimitry Andric case ARM::R11: 195f785676fSDimitry Andric case ARM::R12: 1962754fe60SDimitry Andric if (Reg == FramePtr) 1972754fe60SDimitry Andric FramePtrSpillFI = FI; 198f785676fSDimitry Andric if (STI.isTargetIOS()) 1992754fe60SDimitry Andric GPRCS2Size += 4; 200f785676fSDimitry Andric else 2012754fe60SDimitry Andric GPRCS1Size += 4; 2022754fe60SDimitry Andric break; 2032754fe60SDimitry Andric default: 204dff0c46cSDimitry Andric // This is a DPR. Exclude the aligned DPRCS2 spills. 205dff0c46cSDimitry Andric if (Reg == ARM::D8) 206dff0c46cSDimitry Andric D8SpillFI = FI; 207f785676fSDimitry Andric if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) 2082754fe60SDimitry Andric DPRCSSize += 8; 2092754fe60SDimitry Andric } 2102754fe60SDimitry Andric } 2112754fe60SDimitry Andric 2122754fe60SDimitry Andric // Move past area 1. 213f785676fSDimitry Andric MachineBasicBlock::iterator LastPush = MBB.end(), FramePtrPush; 214f785676fSDimitry Andric if (GPRCS1Size > 0) 215f785676fSDimitry Andric FramePtrPush = LastPush = MBBI++; 2162754fe60SDimitry Andric 2172754fe60SDimitry Andric // Determine starting offsets of spill areas. 218f785676fSDimitry Andric bool HasFP = hasFP(MF); 2192754fe60SDimitry Andric unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); 2202754fe60SDimitry Andric unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 2212754fe60SDimitry Andric unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 222f785676fSDimitry Andric int FramePtrOffsetInPush = 0; 223f785676fSDimitry Andric if (HasFP) { 224f785676fSDimitry Andric FramePtrOffsetInPush = MFI->getObjectOffset(FramePtrSpillFI) + GPRCS1Size; 2252754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 2262754fe60SDimitry Andric NumBytes); 227f785676fSDimitry Andric } 2282754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 2292754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 2302754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 2312754fe60SDimitry Andric 232f785676fSDimitry Andric // Move past area 2. 233f785676fSDimitry Andric if (GPRCS2Size > 0) { 234f785676fSDimitry Andric LastPush = MBBI++; 235f785676fSDimitry Andric } 236f785676fSDimitry Andric 2372754fe60SDimitry Andric // Move past area 3. 238dd6029ffSDimitry Andric if (DPRCSSize > 0) { 239f785676fSDimitry Andric LastPush = MBBI++; 240dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 241dd6029ffSDimitry Andric // instructions in the prologue. 242dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) 243f785676fSDimitry Andric LastPush = MBBI++; 244dd6029ffSDimitry Andric } 2452754fe60SDimitry Andric 246dff0c46cSDimitry Andric // Move past the aligned DPRCS2 area. 247dff0c46cSDimitry Andric if (AFI->getNumAlignedDPRCS2Regs() > 0) { 248dff0c46cSDimitry Andric MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); 249dff0c46cSDimitry Andric // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and 250dff0c46cSDimitry Andric // leaves the stack pointer pointing to the DPRCS2 area. 251dff0c46cSDimitry Andric // 252dff0c46cSDimitry Andric // Adjust NumBytes to represent the stack slots below the DPRCS2 area. 253dff0c46cSDimitry Andric NumBytes += MFI->getObjectOffset(D8SpillFI); 254dff0c46cSDimitry Andric } else 2552754fe60SDimitry Andric NumBytes = DPRCSOffset; 256dff0c46cSDimitry Andric 2572754fe60SDimitry Andric if (NumBytes) { 2582754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 259f785676fSDimitry Andric if (tryFoldSPUpdateIntoPushPop(MF, LastPush, NumBytes)) { 260f785676fSDimitry Andric if (LastPush == FramePtrPush) 261f785676fSDimitry Andric FramePtrOffsetInPush += NumBytes; 262f785676fSDimitry Andric } else 2633b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 2643b0f4066SDimitry Andric MachineInstr::FrameSetup); 265f785676fSDimitry Andric 2662754fe60SDimitry Andric if (HasFP && isARM) 2672754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 2682754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 2692754fe60SDimitry Andric // taken two instructions: 2702754fe60SDimitry Andric // mov sp, r7 2712754fe60SDimitry Andric // sub sp, #24 2722754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 2732754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 2742754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 2752754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2762754fe60SDimitry Andric } 2772754fe60SDimitry Andric 278f785676fSDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 279f785676fSDimitry Andric // For iOS, FP is R7, which has now been stored in spill area 1. 280f785676fSDimitry Andric // Otherwise, if this is not iOS, all the callee-saved registers go 281f785676fSDimitry Andric // into spill area 1, including the FP in R11. In either case, it 282f785676fSDimitry Andric // is in area one and the adjustment needs to take place just after 283f785676fSDimitry Andric // that push. 284f785676fSDimitry Andric if (HasFP) 285f785676fSDimitry Andric emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, ++FramePtrPush, dl, TII, 286f785676fSDimitry Andric FramePtr, ARM::SP, FramePtrOffsetInPush, 287f785676fSDimitry Andric MachineInstr::FrameSetup); 288f785676fSDimitry Andric 289f785676fSDimitry Andric 2902754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 2912754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 2922754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 2932754fe60SDimitry Andric 2942754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 2952754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 2962754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 2972754fe60SDimitry Andric 2982754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 2992754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 300dff0c46cSDimitry Andric // If aligned NEON registers were spilled, the stack has already been 301dff0c46cSDimitry Andric // realigned. 302dff0c46cSDimitry Andric if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { 3032754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 3042754fe60SDimitry Andric assert (!AFI->isThumb1OnlyFunction()); 3052754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 3062754fe60SDimitry Andric // Emit bic sp, sp, MaxAlign 3072754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 3082754fe60SDimitry Andric TII.get(ARM::BICri), ARM::SP) 3092754fe60SDimitry Andric .addReg(ARM::SP, RegState::Kill) 3102754fe60SDimitry Andric .addImm(MaxAlign-1))); 3112754fe60SDimitry Andric } else { 3122754fe60SDimitry Andric // We cannot use sp as source/dest register here, thus we're emitting the 3132754fe60SDimitry Andric // following sequence: 3142754fe60SDimitry Andric // mov r4, sp 3152754fe60SDimitry Andric // bic r4, r4, MaxAlign 3162754fe60SDimitry Andric // mov sp, r4 3172754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 31817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 31917a519f9SDimitry Andric .addReg(ARM::SP, RegState::Kill)); 3202754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 3212754fe60SDimitry Andric TII.get(ARM::t2BICri), ARM::R4) 3222754fe60SDimitry Andric .addReg(ARM::R4, RegState::Kill) 3232754fe60SDimitry Andric .addImm(MaxAlign-1))); 32417a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 32517a519f9SDimitry Andric .addReg(ARM::R4, RegState::Kill)); 3262754fe60SDimitry Andric } 3272754fe60SDimitry Andric 3282754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3292754fe60SDimitry Andric } 3302754fe60SDimitry Andric 3312754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 3322754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 3332754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 3342754fe60SDimitry Andric // to reference locals. 3353b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 3362754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 3372754fe60SDimitry Andric if (isARM) 3382754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 3392754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 3402754fe60SDimitry Andric .addReg(ARM::SP) 3412754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 3422754fe60SDimitry Andric else 34317a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 34417a519f9SDimitry Andric RegInfo->getBaseRegister()) 34517a519f9SDimitry Andric .addReg(ARM::SP)); 3462754fe60SDimitry Andric } 3472754fe60SDimitry Andric 3482754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 3492754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 3502754fe60SDimitry Andric // checks for hasVarSizedObjects. 3512754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 3522754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3532754fe60SDimitry Andric } 3542754fe60SDimitry Andric 3552754fe60SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 3562754fe60SDimitry Andric MachineBasicBlock &MBB) const { 3572754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 358dff0c46cSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 3592754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 3602754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 3612754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 3622754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3632754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 3642754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 3652754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 3662754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 3672754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 3682754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 3692754fe60SDimitry Andric 370f785676fSDimitry Andric unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment(); 371f785676fSDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); 3722754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 3732754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 3742754fe60SDimitry Andric 3753861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 3763861d79fSDimitry Andric // prologue/epilogue. 3777ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 3787ae0e2c9SDimitry Andric return; 3797ae0e2c9SDimitry Andric 3802754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 3812754fe60SDimitry Andric if (NumBytes != 0) 3822754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3832754fe60SDimitry Andric } else { 3842754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 385f785676fSDimitry Andric const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 3862754fe60SDimitry Andric if (MBBI != MBB.begin()) { 387f785676fSDimitry Andric do { 3882754fe60SDimitry Andric --MBBI; 389f785676fSDimitry Andric } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 3902754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 3912754fe60SDimitry Andric ++MBBI; 3922754fe60SDimitry Andric } 3932754fe60SDimitry Andric 3942754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 3952754fe60SDimitry Andric NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 3962754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 3972754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 3982754fe60SDimitry Andric 3992754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 4002754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 4012754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 4022754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 4032754fe60SDimitry Andric if (NumBytes) { 4042754fe60SDimitry Andric if (isARM) 4052754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 4062754fe60SDimitry Andric ARMCC::AL, 0, TII); 4072754fe60SDimitry Andric else { 4082754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 409dff0c46cSDimitry Andric // For iOS, this looks like: 4102754fe60SDimitry Andric // mov sp, r7 4112754fe60SDimitry Andric // sub sp, #24 4122754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 4132754fe60SDimitry Andric // inconsistent state. 4142754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 4152754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 4162754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 4172754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 4182754fe60SDimitry Andric ARMCC::AL, 0, TII); 41917a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 42017a519f9SDimitry Andric ARM::SP) 42117a519f9SDimitry Andric .addReg(ARM::R4)); 4222754fe60SDimitry Andric } 4232754fe60SDimitry Andric } else { 4242754fe60SDimitry Andric // Thumb2 or ARM. 4252754fe60SDimitry Andric if (isARM) 4262754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 4272754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 4282754fe60SDimitry Andric else 42917a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 43017a519f9SDimitry Andric ARM::SP) 43117a519f9SDimitry Andric .addReg(FramePtr)); 4322754fe60SDimitry Andric } 433f785676fSDimitry Andric } else if (NumBytes && !tryFoldSPUpdateIntoPushPop(MF, MBBI, NumBytes)) 4342754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 4352754fe60SDimitry Andric 4362754fe60SDimitry Andric // Increment past our save areas. 437dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 438dd6029ffSDimitry Andric MBBI++; 439dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 440dd6029ffSDimitry Andric // instructions in the epilogue. 441dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 442dd6029ffSDimitry Andric MBBI++; 443dd6029ffSDimitry Andric } 4442754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 4452754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 4462754fe60SDimitry Andric } 4472754fe60SDimitry Andric 448dff0c46cSDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) { 4492754fe60SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 4502754fe60SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 4512754fe60SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 4522754fe60SDimitry Andric 4532754fe60SDimitry Andric // Jump to label or value in register. 454dff0c46cSDimitry Andric if (RetOpcode == ARM::TCRETURNdi) { 455dff0c46cSDimitry Andric unsigned TCOpcode = STI.isThumb() ? 456dff0c46cSDimitry Andric (STI.isTargetIOS() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : 457dff0c46cSDimitry Andric ARM::TAILJMPd; 4582754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 4592754fe60SDimitry Andric if (JumpTarget.isGlobal()) 4602754fe60SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 4612754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4622754fe60SDimitry Andric else { 4632754fe60SDimitry Andric assert(JumpTarget.isSymbol()); 4642754fe60SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 4652754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4662754fe60SDimitry Andric } 4676122f3e6SDimitry Andric 4686122f3e6SDimitry Andric // Add the default predicate in Thumb mode. 4696122f3e6SDimitry Andric if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 4702754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 4713b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4723b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 4732754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4742754fe60SDimitry Andric } 4752754fe60SDimitry Andric 4762754fe60SDimitry Andric MachineInstr *NewMI = prior(MBBI); 4772754fe60SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 4782754fe60SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 4792754fe60SDimitry Andric 4802754fe60SDimitry Andric // Delete the pseudo instruction TCRETURN. 4812754fe60SDimitry Andric MBB.erase(MBBI); 48217a519f9SDimitry Andric MBBI = NewMI; 4832754fe60SDimitry Andric } 4842754fe60SDimitry Andric 485284c1978SDimitry Andric if (ArgRegsSaveSize) 486284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); 4872754fe60SDimitry Andric } 4882754fe60SDimitry Andric 4892754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 4902754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 4912754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 4922754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 4932754fe60SDimitry Andric int 4942754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 4952754fe60SDimitry Andric unsigned &FrameReg) const { 4962754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 4972754fe60SDimitry Andric } 4982754fe60SDimitry Andric 4992754fe60SDimitry Andric int 5002754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 5013b0f4066SDimitry Andric int FI, unsigned &FrameReg, 5022754fe60SDimitry Andric int SPAdj) const { 5032754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 5042754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 5052754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 5062754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 5072754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 5082754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 5092754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 5102754fe60SDimitry Andric 5112754fe60SDimitry Andric FrameReg = ARM::SP; 5122754fe60SDimitry Andric Offset += SPAdj; 5132754fe60SDimitry Andric 514dff0c46cSDimitry Andric // SP can move around if there are allocas. We may also lose track of SP 515dff0c46cSDimitry Andric // when emergency spilling inside a non-reserved call frame setup. 516dff0c46cSDimitry Andric bool hasMovingSP = !hasReservedCallFrame(MF); 517dff0c46cSDimitry Andric 5182754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 5192754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 5202754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 5212754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 5222754fe60SDimitry Andric if (isFixed) { 5232754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5242754fe60SDimitry Andric Offset = FPOffset; 525dff0c46cSDimitry Andric } else if (hasMovingSP) { 5262754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 5272754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 5282754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5292754fe60SDimitry Andric } 5302754fe60SDimitry Andric return Offset; 5312754fe60SDimitry Andric } 5322754fe60SDimitry Andric 5332754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 5342754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 5352754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 5362754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 537dff0c46cSDimitry Andric if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { 5382754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5392754fe60SDimitry Andric return FPOffset; 540dff0c46cSDimitry Andric } else if (hasMovingSP) { 5412754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 5423b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 5432754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 5442754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 5452754fe60SDimitry Andric // particular. 5462754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5472754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5482754fe60SDimitry Andric return FPOffset; 5492754fe60SDimitry Andric } 5503b0f4066SDimitry Andric } 5512754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 5523b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 5533b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 5543b0f4066SDimitry Andric // if at all possible to save space. 5553b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 5563b0f4066SDimitry Andric return Offset; 5572754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 5583b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 5592754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5602754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5612754fe60SDimitry Andric return FPOffset; 5622754fe60SDimitry Andric } 5632754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 5642754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 5652754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5662754fe60SDimitry Andric return FPOffset; 5672754fe60SDimitry Andric } 5682754fe60SDimitry Andric } 5692754fe60SDimitry Andric // Use the base pointer if we have one. 5702754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 5712754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5722754fe60SDimitry Andric return Offset; 5732754fe60SDimitry Andric } 5742754fe60SDimitry Andric 5752754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 5762754fe60SDimitry Andric int FI) const { 5772754fe60SDimitry Andric unsigned FrameReg; 5782754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 5792754fe60SDimitry Andric } 5802754fe60SDimitry Andric 5812754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 5822754fe60SDimitry Andric MachineBasicBlock::iterator MI, 5832754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 5842754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 5852754fe60SDimitry Andric bool NoGap, 5863b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 587dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 5883b0f4066SDimitry Andric unsigned MIFlags) const { 5892754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 5902754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 5912754fe60SDimitry Andric 5922754fe60SDimitry Andric DebugLoc DL; 5932754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 5942754fe60SDimitry Andric 5952754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 5962754fe60SDimitry Andric unsigned i = CSI.size(); 5972754fe60SDimitry Andric while (i != 0) { 5982754fe60SDimitry Andric unsigned LastReg = 0; 5992754fe60SDimitry Andric for (; i != 0; --i) { 6002754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 601dff0c46cSDimitry Andric if (!(Func)(Reg, STI.isTargetIOS())) continue; 602dff0c46cSDimitry Andric 603dff0c46cSDimitry Andric // D-registers in the aligned area DPRCS2 are NOT spilled here. 604dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 605dff0c46cSDimitry Andric continue; 6062754fe60SDimitry Andric 6072754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 6082754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 6092754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 6102754fe60SDimitry Andric // entry block live-in sets. 6112754fe60SDimitry Andric bool isKill = true; 6122754fe60SDimitry Andric if (Reg == ARM::LR) { 6132754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 6142754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 6152754fe60SDimitry Andric isKill = false; 6162754fe60SDimitry Andric } 6172754fe60SDimitry Andric 6182754fe60SDimitry Andric if (isKill) 6192754fe60SDimitry Andric MBB.addLiveIn(Reg); 6202754fe60SDimitry Andric 6212754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 6222754fe60SDimitry Andric // for other instructions. e.g. 6232754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 6242754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6252754fe60SDimitry Andric break; 6262754fe60SDimitry Andric LastReg = Reg; 6272754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 6282754fe60SDimitry Andric } 6292754fe60SDimitry Andric 6302754fe60SDimitry Andric if (Regs.empty()) 6312754fe60SDimitry Andric continue; 6322754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 6332754fe60SDimitry Andric MachineInstrBuilder MIB = 6342754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 6353b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 6362754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 6372754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 6382754fe60SDimitry Andric } else if (Regs.size() == 1) { 6392754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 6402754fe60SDimitry Andric ARM::SP) 6412754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 6426122f3e6SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags) 6436122f3e6SDimitry Andric .addImm(-4); 6442754fe60SDimitry Andric AddDefaultPred(MIB); 6452754fe60SDimitry Andric } 6462754fe60SDimitry Andric Regs.clear(); 6472754fe60SDimitry Andric } 6482754fe60SDimitry Andric } 6492754fe60SDimitry Andric 6502754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 6512754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6522754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6532754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 6542754fe60SDimitry Andric bool isVarArg, bool NoGap, 655dff0c46cSDimitry Andric bool(*Func)(unsigned, bool), 656dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) const { 6572754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6582754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 6592754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6602754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 6612754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 6622754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 663dff0c46cSDimitry Andric RetOpcode == ARM::TCRETURNri); 664f785676fSDimitry Andric bool isInterrupt = 665f785676fSDimitry Andric RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR; 6662754fe60SDimitry Andric 6672754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 6682754fe60SDimitry Andric unsigned i = CSI.size(); 6692754fe60SDimitry Andric while (i != 0) { 6702754fe60SDimitry Andric unsigned LastReg = 0; 6712754fe60SDimitry Andric bool DeleteRet = false; 6722754fe60SDimitry Andric for (; i != 0; --i) { 6732754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 674dff0c46cSDimitry Andric if (!(Func)(Reg, STI.isTargetIOS())) continue; 675dff0c46cSDimitry Andric 676dff0c46cSDimitry Andric // The aligned reloads from area DPRCS2 are not inserted here. 677dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 678dff0c46cSDimitry Andric continue; 6792754fe60SDimitry Andric 680f785676fSDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt && 681f785676fSDimitry Andric STI.hasV5TOps()) { 6822754fe60SDimitry Andric Reg = ARM::PC; 6832754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 6842754fe60SDimitry Andric // Fold the return instruction into the LDM. 6852754fe60SDimitry Andric DeleteRet = true; 6862754fe60SDimitry Andric } 6872754fe60SDimitry Andric 6882754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 6892754fe60SDimitry Andric // for other instructions. e.g. 6902754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 6912754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6922754fe60SDimitry Andric break; 6932754fe60SDimitry Andric 6942754fe60SDimitry Andric LastReg = Reg; 6952754fe60SDimitry Andric Regs.push_back(Reg); 6962754fe60SDimitry Andric } 6972754fe60SDimitry Andric 6982754fe60SDimitry Andric if (Regs.empty()) 6992754fe60SDimitry Andric continue; 7002754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 7012754fe60SDimitry Andric MachineInstrBuilder MIB = 7022754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 7032754fe60SDimitry Andric .addReg(ARM::SP)); 7042754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 7052754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 7066122f3e6SDimitry Andric if (DeleteRet) { 707139f7f9bSDimitry Andric MIB.copyImplicitOps(&*MI); 7082754fe60SDimitry Andric MI->eraseFromParent(); 7096122f3e6SDimitry Andric } 7102754fe60SDimitry Andric MI = MIB; 7112754fe60SDimitry Andric } else if (Regs.size() == 1) { 7122754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 7132754fe60SDimitry Andric // only do that for LDM. 7142754fe60SDimitry Andric if (Regs[0] == ARM::PC) 7152754fe60SDimitry Andric Regs[0] = ARM::LR; 7162754fe60SDimitry Andric MachineInstrBuilder MIB = 7172754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 7182754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 7192754fe60SDimitry Andric .addReg(ARM::SP); 7202754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 7212754fe60SDimitry Andric // that refactoring is complete (eventually). 7226122f3e6SDimitry Andric if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 7232754fe60SDimitry Andric MIB.addReg(0); 7242754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 7252754fe60SDimitry Andric } else 7262754fe60SDimitry Andric MIB.addImm(4); 7272754fe60SDimitry Andric AddDefaultPred(MIB); 7282754fe60SDimitry Andric } 7292754fe60SDimitry Andric Regs.clear(); 7302754fe60SDimitry Andric } 7312754fe60SDimitry Andric } 7322754fe60SDimitry Andric 733dff0c46cSDimitry Andric /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers 734dff0c46cSDimitry Andric /// starting from d8. Also insert stack realignment code and leave the stack 735dff0c46cSDimitry Andric /// pointer pointing to the d8 spill slot. 736dff0c46cSDimitry Andric static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, 737dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 738dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 739dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 740dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 741dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 742dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 743dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 744dff0c46cSDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 745dff0c46cSDimitry Andric MachineFrameInfo &MFI = *MF.getFrameInfo(); 746dff0c46cSDimitry Andric 747dff0c46cSDimitry Andric // Mark the D-register spill slots as properly aligned. Since MFI computes 748dff0c46cSDimitry Andric // stack slot layout backwards, this can actually mean that the d-reg stack 749dff0c46cSDimitry Andric // slot offsets can be wrong. The offset for d8 will always be correct. 750dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 751dff0c46cSDimitry Andric unsigned DNum = CSI[i].getReg() - ARM::D8; 752dff0c46cSDimitry Andric if (DNum >= 8) 753dff0c46cSDimitry Andric continue; 754dff0c46cSDimitry Andric int FI = CSI[i].getFrameIdx(); 755dff0c46cSDimitry Andric // The even-numbered registers will be 16-byte aligned, the odd-numbered 756dff0c46cSDimitry Andric // registers will be 8-byte aligned. 757dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); 758dff0c46cSDimitry Andric 759dff0c46cSDimitry Andric // The stack slot for D8 needs to be maximally aligned because this is 760dff0c46cSDimitry Andric // actually the point where we align the stack pointer. MachineFrameInfo 761dff0c46cSDimitry Andric // computes all offsets relative to the incoming stack pointer which is a 762dff0c46cSDimitry Andric // bit weird when realigning the stack. Any extra padding for this 763dff0c46cSDimitry Andric // over-alignment is not realized because the code inserted below adjusts 764dff0c46cSDimitry Andric // the stack pointer by numregs * 8 before aligning the stack pointer. 765dff0c46cSDimitry Andric if (DNum == 0) 766dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); 767dff0c46cSDimitry Andric } 768dff0c46cSDimitry Andric 769dff0c46cSDimitry Andric // Move the stack pointer to the d8 spill slot, and align it at the same 770dff0c46cSDimitry Andric // time. Leave the stack slot address in the scratch register r4. 771dff0c46cSDimitry Andric // 772dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 773dff0c46cSDimitry Andric // bic r4, r4, #align - 1 774dff0c46cSDimitry Andric // mov sp, r4 775dff0c46cSDimitry Andric // 776dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 777dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 778dff0c46cSDimitry Andric AFI->setShouldRestoreSPFromFP(true); 779dff0c46cSDimitry Andric 780dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 781dff0c46cSDimitry Andric // The immediate is <= 64, so it doesn't need any special encoding. 782dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; 783dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 784dff0c46cSDimitry Andric .addReg(ARM::SP) 785dff0c46cSDimitry Andric .addImm(8 * NumAlignedDPRCS2Regs))); 786dff0c46cSDimitry Andric 787dff0c46cSDimitry Andric // bic r4, r4, #align-1 788dff0c46cSDimitry Andric Opc = isThumb ? ARM::t2BICri : ARM::BICri; 789dff0c46cSDimitry Andric unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); 790dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 791dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill) 792dff0c46cSDimitry Andric .addImm(MaxAlign - 1))); 793dff0c46cSDimitry Andric 794dff0c46cSDimitry Andric // mov sp, r4 795dff0c46cSDimitry Andric // The stack pointer must be adjusted before spilling anything, otherwise 796dff0c46cSDimitry Andric // the stack slots could be clobbered by an interrupt handler. 797dff0c46cSDimitry Andric // Leave r4 live, it is used below. 798dff0c46cSDimitry Andric Opc = isThumb ? ARM::tMOVr : ARM::MOVr; 799dff0c46cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) 800dff0c46cSDimitry Andric .addReg(ARM::R4); 801dff0c46cSDimitry Andric MIB = AddDefaultPred(MIB); 802dff0c46cSDimitry Andric if (!isThumb) 803dff0c46cSDimitry Andric AddDefaultCC(MIB); 804dff0c46cSDimitry Andric 805dff0c46cSDimitry Andric // Now spill NumAlignedDPRCS2Regs registers starting from d8. 806dff0c46cSDimitry Andric // r4 holds the stack slot address. 807dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 808dff0c46cSDimitry Andric 809dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs and address writeback. 810dff0c46cSDimitry Andric // The writeback is only needed when emitting two vst1.64 instructions. 811dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 812dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8137ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 814dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 815dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), 816dff0c46cSDimitry Andric ARM::R4) 817dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 818dff0c46cSDimitry Andric .addReg(NextReg) 819dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 820dff0c46cSDimitry Andric NextReg += 4; 821dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 822dff0c46cSDimitry Andric } 823dff0c46cSDimitry Andric 824dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 825dff0c46cSDimitry Andric // register to be spilled. 826dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 827dff0c46cSDimitry Andric 828dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs, no writeback. 829dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 830dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8317ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 832dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 833dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) 834dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(NextReg) 835dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 836dff0c46cSDimitry Andric NextReg += 4; 837dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 838dff0c46cSDimitry Andric } 839dff0c46cSDimitry Andric 840dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 2 d-regs. 841dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 842dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8437ae0e2c9SDimitry Andric &ARM::QPRRegClass); 844dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 845dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) 846dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(SupReg)); 847dff0c46cSDimitry Andric NextReg += 2; 848dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 849dff0c46cSDimitry Andric } 850dff0c46cSDimitry Andric 851dff0c46cSDimitry Andric // Finally, use a vanilla vstr.64 for the odd last register. 852dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) { 853dff0c46cSDimitry Andric MBB.addLiveIn(NextReg); 854dff0c46cSDimitry Andric // vstr.64 uses addrmode5 which has an offset scale of 4. 855dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) 856dff0c46cSDimitry Andric .addReg(NextReg) 857dff0c46cSDimitry Andric .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); 858dff0c46cSDimitry Andric } 859dff0c46cSDimitry Andric 860dff0c46cSDimitry Andric // The last spill instruction inserted should kill the scratch register r4. 861dff0c46cSDimitry Andric llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI); 862dff0c46cSDimitry Andric } 863dff0c46cSDimitry Andric 864dff0c46cSDimitry Andric /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an 865dff0c46cSDimitry Andric /// iterator to the following instruction. 866dff0c46cSDimitry Andric static MachineBasicBlock::iterator 867dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 868dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) { 869dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 870dff0c46cSDimitry Andric // bic r4, r4, #align - 1 871dff0c46cSDimitry Andric // mov sp, r4 872dff0c46cSDimitry Andric ++MI; ++MI; ++MI; 873dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 874dff0c46cSDimitry Andric 875dff0c46cSDimitry Andric // These switches all fall through. 876dff0c46cSDimitry Andric switch(NumAlignedDPRCS2Regs) { 877dff0c46cSDimitry Andric case 7: 878dff0c46cSDimitry Andric ++MI; 879dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 880dff0c46cSDimitry Andric default: 881dff0c46cSDimitry Andric ++MI; 882dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 883dff0c46cSDimitry Andric case 1: 884dff0c46cSDimitry Andric case 2: 885dff0c46cSDimitry Andric case 4: 886dff0c46cSDimitry Andric assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); 887dff0c46cSDimitry Andric ++MI; 888dff0c46cSDimitry Andric } 889dff0c46cSDimitry Andric return MI; 890dff0c46cSDimitry Andric } 891dff0c46cSDimitry Andric 892dff0c46cSDimitry Andric /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers 893dff0c46cSDimitry Andric /// starting from d8. These instructions are assumed to execute while the 894dff0c46cSDimitry Andric /// stack is still aligned, unlike the code inserted by emitPopInst. 895dff0c46cSDimitry Andric static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, 896dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 897dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 898dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 899dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 900dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 901dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 902dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 903dff0c46cSDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 904dff0c46cSDimitry Andric 905dff0c46cSDimitry Andric // Find the frame index assigned to d8. 906dff0c46cSDimitry Andric int D8SpillFI = 0; 907dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) 908dff0c46cSDimitry Andric if (CSI[i].getReg() == ARM::D8) { 909dff0c46cSDimitry Andric D8SpillFI = CSI[i].getFrameIdx(); 910dff0c46cSDimitry Andric break; 911dff0c46cSDimitry Andric } 912dff0c46cSDimitry Andric 913dff0c46cSDimitry Andric // Materialize the address of the d8 spill slot into the scratch register r4. 914dff0c46cSDimitry Andric // This can be fairly complicated if the stack frame is large, so just use 915dff0c46cSDimitry Andric // the normal frame index elimination mechanism to do it. This code runs as 916dff0c46cSDimitry Andric // the initial part of the epilog where the stack and base pointers haven't 917dff0c46cSDimitry Andric // been changed yet. 918dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 919dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 920dff0c46cSDimitry Andric 921dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; 922dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 923dff0c46cSDimitry Andric .addFrameIndex(D8SpillFI).addImm(0))); 924dff0c46cSDimitry Andric 925dff0c46cSDimitry Andric // Now restore NumAlignedDPRCS2Regs registers starting from d8. 926dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 927dff0c46cSDimitry Andric 928dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs and writeback. 929dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 930dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9317ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 932dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) 933dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Define) 934dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 935dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 936dff0c46cSDimitry Andric NextReg += 4; 937dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 938dff0c46cSDimitry Andric } 939dff0c46cSDimitry Andric 940dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 941dff0c46cSDimitry Andric // register to be spilled. 942dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 943dff0c46cSDimitry Andric 944dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs, no writeback. 945dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 946dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9477ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 948dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) 949dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16) 950dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 951dff0c46cSDimitry Andric NextReg += 4; 952dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 953dff0c46cSDimitry Andric } 954dff0c46cSDimitry Andric 955dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 2 d-regs. 956dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 957dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9587ae0e2c9SDimitry Andric &ARM::QPRRegClass); 959dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) 960dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16)); 961dff0c46cSDimitry Andric NextReg += 2; 962dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 963dff0c46cSDimitry Andric } 964dff0c46cSDimitry Andric 965dff0c46cSDimitry Andric // Finally, use a vanilla vldr.64 for the remaining odd register. 966dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 967dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) 968dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); 969dff0c46cSDimitry Andric 970dff0c46cSDimitry Andric // Last store kills r4. 971dff0c46cSDimitry Andric llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI); 972dff0c46cSDimitry Andric } 973dff0c46cSDimitry Andric 9742754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 9752754fe60SDimitry Andric MachineBasicBlock::iterator MI, 9762754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 9772754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 9782754fe60SDimitry Andric if (CSI.empty()) 9792754fe60SDimitry Andric return false; 9802754fe60SDimitry Andric 9812754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9822754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 9832754fe60SDimitry Andric 9842754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 9856122f3e6SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? 9866122f3e6SDimitry Andric ARM::t2STR_PRE : ARM::STR_PRE_IMM; 9872754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 988dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 989dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, 9903b0f4066SDimitry Andric MachineInstr::FrameSetup); 991dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, 9923b0f4066SDimitry Andric MachineInstr::FrameSetup); 9933b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 994dff0c46cSDimitry Andric NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); 995dff0c46cSDimitry Andric 996dff0c46cSDimitry Andric // The code above does not insert spill code for the aligned DPRCS2 registers. 997dff0c46cSDimitry Andric // The stack realignment code will be inserted between the push instructions 998dff0c46cSDimitry Andric // and these spills. 999dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1000dff0c46cSDimitry Andric emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 10012754fe60SDimitry Andric 10022754fe60SDimitry Andric return true; 10032754fe60SDimitry Andric } 10042754fe60SDimitry Andric 10052754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 10062754fe60SDimitry Andric MachineBasicBlock::iterator MI, 10072754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 10082754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 10092754fe60SDimitry Andric if (CSI.empty()) 10102754fe60SDimitry Andric return false; 10112754fe60SDimitry Andric 10122754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10132754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1014284c1978SDimitry Andric bool isVarArg = AFI->getArgRegsSaveSize() > 0; 1015dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1016dff0c46cSDimitry Andric 1017dff0c46cSDimitry Andric // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 1018dff0c46cSDimitry Andric // registers. Do that here instead. 1019dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1020dff0c46cSDimitry Andric emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 10212754fe60SDimitry Andric 10222754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 10236122f3e6SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 10242754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 1025dff0c46cSDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, 1026dff0c46cSDimitry Andric NumAlignedDPRCS2Regs); 10272754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1028dff0c46cSDimitry Andric &isARMArea2Register, 0); 10292754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1030dff0c46cSDimitry Andric &isARMArea1Register, 0); 10312754fe60SDimitry Andric 10322754fe60SDimitry Andric return true; 10332754fe60SDimitry Andric } 10342754fe60SDimitry Andric 10352754fe60SDimitry Andric // FIXME: Make generic? 10362754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 10372754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 10382754fe60SDimitry Andric unsigned FnSize = 0; 10392754fe60SDimitry Andric for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 10402754fe60SDimitry Andric MBBI != E; ++MBBI) { 10412754fe60SDimitry Andric const MachineBasicBlock &MBB = *MBBI; 10422754fe60SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 10432754fe60SDimitry Andric I != E; ++I) 10442754fe60SDimitry Andric FnSize += TII.GetInstSizeInBytes(I); 10452754fe60SDimitry Andric } 10462754fe60SDimitry Andric return FnSize; 10472754fe60SDimitry Andric } 10482754fe60SDimitry Andric 10492754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 10502754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 10512754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 10522754fe60SDimitry Andric // FIXME: Move to TII? 10532754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 10542754fe60SDimitry Andric const TargetFrameLowering *TFI) { 10552754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 10562754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 10572754fe60SDimitry Andric for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { 10582754fe60SDimitry Andric for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); 10592754fe60SDimitry Andric I != E; ++I) { 10602754fe60SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 10612754fe60SDimitry Andric if (!I->getOperand(i).isFI()) continue; 10622754fe60SDimitry Andric 10632754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 10642754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 10652754fe60SDimitry Andric if (I->getOpcode() == ARM::ADDri) { 10662754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10672754fe60SDimitry Andric break; 10682754fe60SDimitry Andric } 10692754fe60SDimitry Andric 10702754fe60SDimitry Andric // Otherwise check the addressing mode. 10712754fe60SDimitry Andric switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { 10722754fe60SDimitry Andric case ARMII::AddrMode3: 10732754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 10742754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10752754fe60SDimitry Andric break; 10762754fe60SDimitry Andric case ARMII::AddrMode5: 10772754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 10782754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 10792754fe60SDimitry Andric break; 10802754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 10812754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 10822754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 10832754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 10842754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10852754fe60SDimitry Andric break; 10862754fe60SDimitry Andric case ARMII::AddrMode4: 10872754fe60SDimitry Andric case ARMII::AddrMode6: 10882754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 10892754fe60SDimitry Andric // immediate offset for stack references. 10902754fe60SDimitry Andric return 0; 10912754fe60SDimitry Andric default: 10922754fe60SDimitry Andric break; 10932754fe60SDimitry Andric } 10942754fe60SDimitry Andric break; // At most one FI per instruction 10952754fe60SDimitry Andric } 10962754fe60SDimitry Andric } 10972754fe60SDimitry Andric } 10982754fe60SDimitry Andric 10992754fe60SDimitry Andric return Limit; 11002754fe60SDimitry Andric } 11012754fe60SDimitry Andric 1102dff0c46cSDimitry Andric // In functions that realign the stack, it can be an advantage to spill the 1103dff0c46cSDimitry Andric // callee-saved vector registers after realigning the stack. The vst1 and vld1 1104dff0c46cSDimitry Andric // instructions take alignment hints that can improve performance. 1105dff0c46cSDimitry Andric // 1106dff0c46cSDimitry Andric static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { 1107dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); 1108dff0c46cSDimitry Andric if (!SpillAlignedNEONRegs) 1109dff0c46cSDimitry Andric return; 1110dff0c46cSDimitry Andric 1111dff0c46cSDimitry Andric // Naked functions don't spill callee-saved registers. 1112139f7f9bSDimitry Andric if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 1113139f7f9bSDimitry Andric Attribute::Naked)) 1114dff0c46cSDimitry Andric return; 1115dff0c46cSDimitry Andric 1116dff0c46cSDimitry Andric // We are planning to use NEON instructions vst1 / vld1. 1117dff0c46cSDimitry Andric if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON()) 1118dff0c46cSDimitry Andric return; 1119dff0c46cSDimitry Andric 1120dff0c46cSDimitry Andric // Don't bother if the default stack alignment is sufficiently high. 1121dff0c46cSDimitry Andric if (MF.getTarget().getFrameLowering()->getStackAlignment() >= 8) 1122dff0c46cSDimitry Andric return; 1123dff0c46cSDimitry Andric 1124dff0c46cSDimitry Andric // Aligned spills require stack realignment. 1125dff0c46cSDimitry Andric const ARMBaseRegisterInfo *RegInfo = 1126dff0c46cSDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 1127dff0c46cSDimitry Andric if (!RegInfo->canRealignStack(MF)) 1128dff0c46cSDimitry Andric return; 1129dff0c46cSDimitry Andric 1130dff0c46cSDimitry Andric // We always spill contiguous d-registers starting from d8. Count how many 1131dff0c46cSDimitry Andric // needs spilling. The register allocator will almost always use the 1132dff0c46cSDimitry Andric // callee-saved registers in order, but it can happen that there are holes in 1133dff0c46cSDimitry Andric // the range. Registers above the hole will be spilled to the standard DPRCS 1134dff0c46cSDimitry Andric // area. 1135dff0c46cSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1136dff0c46cSDimitry Andric unsigned NumSpills = 0; 1137dff0c46cSDimitry Andric for (; NumSpills < 8; ++NumSpills) 11383861d79fSDimitry Andric if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) 1139dff0c46cSDimitry Andric break; 1140dff0c46cSDimitry Andric 1141dff0c46cSDimitry Andric // Don't do this for just one d-register. It's not worth it. 1142dff0c46cSDimitry Andric if (NumSpills < 2) 1143dff0c46cSDimitry Andric return; 1144dff0c46cSDimitry Andric 1145dff0c46cSDimitry Andric // Spill the first NumSpills D-registers after realigning the stack. 1146dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); 1147dff0c46cSDimitry Andric 1148dff0c46cSDimitry Andric // A scratch register is required for the vst1 / vld1 instructions. 1149dff0c46cSDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 1150dff0c46cSDimitry Andric } 1151dff0c46cSDimitry Andric 11522754fe60SDimitry Andric void 11532754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 11542754fe60SDimitry Andric RegScavenger *RS) const { 11552754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 11562754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 11572754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 11582754fe60SDimitry Andric // to combine multiple loads / stores. 11592754fe60SDimitry Andric bool CanEliminateFrame = true; 11602754fe60SDimitry Andric bool CS1Spilled = false; 11612754fe60SDimitry Andric bool LRSpilled = false; 11622754fe60SDimitry Andric unsigned NumGPRSpills = 0; 11632754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 11642754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 11652754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 11662754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 11672754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 11682754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 11692754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 11702754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 11713861d79fSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 11722754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 11732754fe60SDimitry Andric 11742754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 11752754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 11762754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 11772754fe60SDimitry Andric // instruction. 11782754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 11792754fe60SDimitry Andric if (AFI->isThumb2Function() && 11802754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 11813861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 11822754fe60SDimitry Andric 11832754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 11842754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 1185284c1978SDimitry Andric if (AFI->getArgRegsSaveSize() > 0) 11863861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 11872754fe60SDimitry Andric 118817a519f9SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 118917a519f9SDimitry Andric // for sure what the stack size will be, but for this, an estimate is good 119017a519f9SDimitry Andric // enough. If there anything changes it, it'll be a spill, which implies 119117a519f9SDimitry Andric // we've used all the registers and so R4 is already used, so not marking 119217a519f9SDimitry Andric // it here will be OK. 11932754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 1194139f7f9bSDimitry Andric unsigned StackSize = MFI->estimateStackSize(MF); 119517a519f9SDimitry Andric if (MFI->hasVarSizedObjects() || StackSize > 508) 11963861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 11972754fe60SDimitry Andric } 11982754fe60SDimitry Andric 1199dff0c46cSDimitry Andric // See if we can spill vector registers to aligned stack. 1200dff0c46cSDimitry Andric checkNumAlignedDPRCS2Regs(MF); 1201dff0c46cSDimitry Andric 12022754fe60SDimitry Andric // Spill the BasePtr if it's used. 12032754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 12043861d79fSDimitry Andric MRI.setPhysRegUsed(RegInfo->getBaseRegister()); 12052754fe60SDimitry Andric 12062754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 12072754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 1208f785676fSDimitry Andric const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(&MF); 12092754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 12102754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 12112754fe60SDimitry Andric bool Spilled = false; 12123861d79fSDimitry Andric if (MRI.isPhysRegUsed(Reg)) { 12132754fe60SDimitry Andric Spilled = true; 12142754fe60SDimitry Andric CanEliminateFrame = false; 12152754fe60SDimitry Andric } 12162754fe60SDimitry Andric 12177ae0e2c9SDimitry Andric if (!ARM::GPRRegClass.contains(Reg)) 12182754fe60SDimitry Andric continue; 12192754fe60SDimitry Andric 12202754fe60SDimitry Andric if (Spilled) { 12212754fe60SDimitry Andric NumGPRSpills++; 12222754fe60SDimitry Andric 1223dff0c46cSDimitry Andric if (!STI.isTargetIOS()) { 12242754fe60SDimitry Andric if (Reg == ARM::LR) 12252754fe60SDimitry Andric LRSpilled = true; 12262754fe60SDimitry Andric CS1Spilled = true; 12272754fe60SDimitry Andric continue; 12282754fe60SDimitry Andric } 12292754fe60SDimitry Andric 12302754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 12312754fe60SDimitry Andric switch (Reg) { 12322754fe60SDimitry Andric case ARM::LR: 12332754fe60SDimitry Andric LRSpilled = true; 12342754fe60SDimitry Andric // Fallthrough 1235f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1236f785676fSDimitry Andric case ARM::R2: case ARM::R3: 12372754fe60SDimitry Andric case ARM::R4: case ARM::R5: 12382754fe60SDimitry Andric case ARM::R6: case ARM::R7: 12392754fe60SDimitry Andric CS1Spilled = true; 12402754fe60SDimitry Andric break; 12412754fe60SDimitry Andric default: 12422754fe60SDimitry Andric break; 12432754fe60SDimitry Andric } 12442754fe60SDimitry Andric } else { 1245dff0c46cSDimitry Andric if (!STI.isTargetIOS()) { 12462754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 12472754fe60SDimitry Andric continue; 12482754fe60SDimitry Andric } 12492754fe60SDimitry Andric 12502754fe60SDimitry Andric switch (Reg) { 1251f785676fSDimitry Andric case ARM::R0: case ARM::R1: 1252f785676fSDimitry Andric case ARM::R2: case ARM::R3: 12532754fe60SDimitry Andric case ARM::R4: case ARM::R5: 12542754fe60SDimitry Andric case ARM::R6: case ARM::R7: 12552754fe60SDimitry Andric case ARM::LR: 12562754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 12572754fe60SDimitry Andric break; 12582754fe60SDimitry Andric default: 12592754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 12602754fe60SDimitry Andric break; 12612754fe60SDimitry Andric } 12622754fe60SDimitry Andric } 12632754fe60SDimitry Andric } 12642754fe60SDimitry Andric 12652754fe60SDimitry Andric bool ForceLRSpill = false; 12662754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 12672754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 12682754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 12692754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 12702754fe60SDimitry Andric // then the branch fix up path will undo it. 12712754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 12722754fe60SDimitry Andric CanEliminateFrame = false; 12732754fe60SDimitry Andric ForceLRSpill = true; 12742754fe60SDimitry Andric } 12752754fe60SDimitry Andric } 12762754fe60SDimitry Andric 12772754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 12782754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 12792754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 12802754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 12812754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 12822754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 12832754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 12842754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 12852754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 12862754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 12872754fe60SDimitry Andric // calculations may be off, so be conservative. 12882754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 12892754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 12902754fe60SDimitry Andric // worth the effort and added fragility? 12912754fe60SDimitry Andric bool BigStack = 12922754fe60SDimitry Andric (RS && 1293139f7f9bSDimitry Andric (MFI->estimateStackSize(MF) + 1294139f7f9bSDimitry Andric ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 12952754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 12962754fe60SDimitry Andric || MFI->hasVarSizedObjects() 12972754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 12982754fe60SDimitry Andric 12992754fe60SDimitry Andric bool ExtraCSSpill = false; 13002754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 13012754fe60SDimitry Andric AFI->setHasStackFrame(true); 13022754fe60SDimitry Andric 13032754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 13042754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 13052754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 13063861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 13072754fe60SDimitry Andric NumGPRSpills++; 1308f785676fSDimitry Andric SmallVectorImpl<unsigned>::iterator LRPos; 1309f785676fSDimitry Andric LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), 1310f785676fSDimitry Andric (unsigned)ARM::LR); 1311f785676fSDimitry Andric if (LRPos != UnspilledCS1GPRs.end()) 1312f785676fSDimitry Andric UnspilledCS1GPRs.erase(LRPos); 1313f785676fSDimitry Andric 13142754fe60SDimitry Andric ForceLRSpill = false; 13152754fe60SDimitry Andric ExtraCSSpill = true; 13162754fe60SDimitry Andric } 13172754fe60SDimitry Andric 13182754fe60SDimitry Andric if (hasFP(MF)) { 13193861d79fSDimitry Andric MRI.setPhysRegUsed(FramePtr); 13202754fe60SDimitry Andric NumGPRSpills++; 13212754fe60SDimitry Andric } 13222754fe60SDimitry Andric 13232754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 13242754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 13252754fe60SDimitry Andric // the integer and double callee save areas. 13262754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 13272754fe60SDimitry Andric if (TargetAlign == 8 && (NumGPRSpills & 1)) { 13282754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 13292754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 13302754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 13312754fe60SDimitry Andric // Don't spill high register if the function is thumb1 13322754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction() || 13332754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 13343861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 13353861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 13362754fe60SDimitry Andric ExtraCSSpill = true; 13372754fe60SDimitry Andric break; 13382754fe60SDimitry Andric } 13392754fe60SDimitry Andric } 13402754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 13412754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 13423861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 13433861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 13442754fe60SDimitry Andric ExtraCSSpill = true; 13452754fe60SDimitry Andric } 13462754fe60SDimitry Andric } 13472754fe60SDimitry Andric 13482754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 13492754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 13502754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 13512754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 13522754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 13532754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 13542754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 13552754fe60SDimitry Andric // extra. That should take care of it! 13562754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 13572754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 13582754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 13592754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 13602754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 13613861d79fSDimitry Andric if (!MRI.isReserved(Reg) && 13622754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 13632754fe60SDimitry Andric Reg == ARM::LR)) { 13642754fe60SDimitry Andric Extras.push_back(Reg); 13652754fe60SDimitry Andric NumExtras--; 13662754fe60SDimitry Andric } 13672754fe60SDimitry Andric } 13682754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 13692754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 13702754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 13712754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 13722754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 13733861d79fSDimitry Andric if (!MRI.isReserved(Reg)) { 13742754fe60SDimitry Andric Extras.push_back(Reg); 13752754fe60SDimitry Andric NumExtras--; 13762754fe60SDimitry Andric } 13772754fe60SDimitry Andric } 13782754fe60SDimitry Andric } 13792754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 13802754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 13813861d79fSDimitry Andric MRI.setPhysRegUsed(Extras[i]); 13822754fe60SDimitry Andric } 13832754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 13842754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 13852754fe60SDimitry Andric // closest to SP or frame pointer. 13867ae0e2c9SDimitry Andric const TargetRegisterClass *RC = &ARM::GPRRegClass; 1387139f7f9bSDimitry Andric RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 13882754fe60SDimitry Andric RC->getAlignment(), 13892754fe60SDimitry Andric false)); 13902754fe60SDimitry Andric } 13912754fe60SDimitry Andric } 13922754fe60SDimitry Andric } 13932754fe60SDimitry Andric 13942754fe60SDimitry Andric if (ForceLRSpill) { 13953861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 13962754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 13972754fe60SDimitry Andric } 13982754fe60SDimitry Andric } 1399139f7f9bSDimitry Andric 1400139f7f9bSDimitry Andric 1401139f7f9bSDimitry Andric void ARMFrameLowering:: 1402139f7f9bSDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1403139f7f9bSDimitry Andric MachineBasicBlock::iterator I) const { 1404139f7f9bSDimitry Andric const ARMBaseInstrInfo &TII = 1405139f7f9bSDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 1406139f7f9bSDimitry Andric if (!hasReservedCallFrame(MF)) { 1407139f7f9bSDimitry Andric // If we have alloca, convert as follows: 1408139f7f9bSDimitry Andric // ADJCALLSTACKDOWN -> sub, sp, sp, amount 1409139f7f9bSDimitry Andric // ADJCALLSTACKUP -> add, sp, sp, amount 1410139f7f9bSDimitry Andric MachineInstr *Old = I; 1411139f7f9bSDimitry Andric DebugLoc dl = Old->getDebugLoc(); 1412139f7f9bSDimitry Andric unsigned Amount = Old->getOperand(0).getImm(); 1413139f7f9bSDimitry Andric if (Amount != 0) { 1414139f7f9bSDimitry Andric // We need to keep the stack aligned properly. To do this, we round the 1415139f7f9bSDimitry Andric // amount of space needed for the outgoing arguments up to the next 1416139f7f9bSDimitry Andric // alignment boundary. 1417139f7f9bSDimitry Andric unsigned Align = getStackAlignment(); 1418139f7f9bSDimitry Andric Amount = (Amount+Align-1)/Align*Align; 1419139f7f9bSDimitry Andric 1420139f7f9bSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1421139f7f9bSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1422139f7f9bSDimitry Andric "This eliminateCallFramePseudoInstr does not support Thumb1!"); 1423139f7f9bSDimitry Andric bool isARM = !AFI->isThumbFunction(); 1424139f7f9bSDimitry Andric 1425139f7f9bSDimitry Andric // Replace the pseudo instruction with a new instruction... 1426139f7f9bSDimitry Andric unsigned Opc = Old->getOpcode(); 1427139f7f9bSDimitry Andric int PIdx = Old->findFirstPredOperandIdx(); 1428139f7f9bSDimitry Andric ARMCC::CondCodes Pred = (PIdx == -1) 1429139f7f9bSDimitry Andric ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); 1430139f7f9bSDimitry Andric if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { 1431139f7f9bSDimitry Andric // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. 1432139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(2).getReg(); 1433139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, 1434139f7f9bSDimitry Andric Pred, PredReg); 1435139f7f9bSDimitry Andric } else { 1436139f7f9bSDimitry Andric // Note: PredReg is operand 3 for ADJCALLSTACKUP. 1437139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(3).getReg(); 1438139f7f9bSDimitry Andric assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); 1439139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, 1440139f7f9bSDimitry Andric Pred, PredReg); 1441139f7f9bSDimitry Andric } 1442139f7f9bSDimitry Andric } 1443139f7f9bSDimitry Andric } 1444139f7f9bSDimitry Andric MBB.erase(I); 1445139f7f9bSDimitry Andric } 1446139f7f9bSDimitry Andric 1447