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 85dff0c46cSDimitry Andric static bool isCalleeSavedRegister(unsigned Reg, const uint16_t *CSRegs) { 862754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) 872754fe60SDimitry Andric if (Reg == CSRegs[i]) 882754fe60SDimitry Andric return true; 892754fe60SDimitry Andric return false; 902754fe60SDimitry Andric } 912754fe60SDimitry Andric 922754fe60SDimitry Andric static bool isCSRestore(MachineInstr *MI, 932754fe60SDimitry Andric const ARMBaseInstrInfo &TII, 94dff0c46cSDimitry Andric const uint16_t *CSRegs) { 952754fe60SDimitry Andric // Integer spill area is handled with "pop". 962754fe60SDimitry Andric if (MI->getOpcode() == ARM::LDMIA_RET || 972754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_RET || 982754fe60SDimitry Andric MI->getOpcode() == ARM::LDMIA_UPD || 992754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDMIA_UPD || 1002754fe60SDimitry Andric MI->getOpcode() == ARM::VLDMDIA_UPD) { 1012754fe60SDimitry Andric // The first two operands are predicates. The last two are 1022754fe60SDimitry Andric // imp-def and imp-use of SP. Check everything in between. 1032754fe60SDimitry Andric for (int i = 5, e = MI->getNumOperands(); i != e; ++i) 1042754fe60SDimitry Andric if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) 1052754fe60SDimitry Andric return false; 1062754fe60SDimitry Andric return true; 1072754fe60SDimitry Andric } 1086122f3e6SDimitry Andric if ((MI->getOpcode() == ARM::LDR_POST_IMM || 1096122f3e6SDimitry Andric MI->getOpcode() == ARM::LDR_POST_REG || 1102754fe60SDimitry Andric MI->getOpcode() == ARM::t2LDR_POST) && 1112754fe60SDimitry Andric isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && 1122754fe60SDimitry Andric MI->getOperand(1).getReg() == ARM::SP) 1132754fe60SDimitry Andric return true; 1142754fe60SDimitry Andric 1152754fe60SDimitry Andric return false; 1162754fe60SDimitry Andric } 1172754fe60SDimitry Andric 1182754fe60SDimitry Andric static void 1192754fe60SDimitry Andric emitSPUpdate(bool isARM, 1202754fe60SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1212754fe60SDimitry Andric DebugLoc dl, const ARMBaseInstrInfo &TII, 122139f7f9bSDimitry Andric int NumBytes, unsigned MIFlags = MachineInstr::NoFlags, 123139f7f9bSDimitry Andric ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) { 1242754fe60SDimitry Andric if (isARM) 1252754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 126139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1272754fe60SDimitry Andric else 1282754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, 129139f7f9bSDimitry Andric Pred, PredReg, TII, MIFlags); 1302754fe60SDimitry Andric } 1312754fe60SDimitry 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(); 144284c1978SDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 1452754fe60SDimitry Andric unsigned NumBytes = MFI->getStackSize(); 1462754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 1472754fe60SDimitry Andric DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 1482754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 1492754fe60SDimitry Andric 1502754fe60SDimitry Andric // Determine the sizes of each callee-save spill areas and record which frame 1512754fe60SDimitry Andric // belongs to which callee-save spill areas. 1522754fe60SDimitry Andric unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; 1532754fe60SDimitry Andric int FramePtrSpillFI = 0; 154dff0c46cSDimitry Andric int D8SpillFI = 0; 155f8254f43SDimitry Andric 1563861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 1573861d79fSDimitry Andric // prologue/epilogue. 1587ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 1597ae0e2c9SDimitry Andric return; 1607ae0e2c9SDimitry Andric 1612754fe60SDimitry Andric // Allocate the vararg register save area. This is not counted in NumBytes. 162284c1978SDimitry Andric if (ArgRegsSaveSize) 163284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, 1643b0f4066SDimitry Andric MachineInstr::FrameSetup); 1652754fe60SDimitry Andric 1662754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 1672754fe60SDimitry Andric if (NumBytes != 0) 1683b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 1693b0f4066SDimitry Andric MachineInstr::FrameSetup); 1702754fe60SDimitry Andric return; 1712754fe60SDimitry Andric } 1722754fe60SDimitry Andric 1732754fe60SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1742754fe60SDimitry Andric unsigned Reg = CSI[i].getReg(); 1752754fe60SDimitry Andric int FI = CSI[i].getFrameIdx(); 1762754fe60SDimitry Andric switch (Reg) { 1772754fe60SDimitry Andric case ARM::R4: 1782754fe60SDimitry Andric case ARM::R5: 1792754fe60SDimitry Andric case ARM::R6: 1802754fe60SDimitry Andric case ARM::R7: 1812754fe60SDimitry Andric case ARM::LR: 1822754fe60SDimitry Andric if (Reg == FramePtr) 1832754fe60SDimitry Andric FramePtrSpillFI = FI; 1842754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1852754fe60SDimitry Andric GPRCS1Size += 4; 1862754fe60SDimitry Andric break; 1872754fe60SDimitry Andric case ARM::R8: 1882754fe60SDimitry Andric case ARM::R9: 1892754fe60SDimitry Andric case ARM::R10: 1902754fe60SDimitry Andric case ARM::R11: 1912754fe60SDimitry Andric if (Reg == FramePtr) 1922754fe60SDimitry Andric FramePtrSpillFI = FI; 193dff0c46cSDimitry Andric if (STI.isTargetIOS()) { 1942754fe60SDimitry Andric AFI->addGPRCalleeSavedArea2Frame(FI); 1952754fe60SDimitry Andric GPRCS2Size += 4; 1962754fe60SDimitry Andric } else { 1972754fe60SDimitry Andric AFI->addGPRCalleeSavedArea1Frame(FI); 1982754fe60SDimitry Andric GPRCS1Size += 4; 1992754fe60SDimitry Andric } 2002754fe60SDimitry Andric break; 2012754fe60SDimitry Andric default: 202dff0c46cSDimitry Andric // This is a DPR. Exclude the aligned DPRCS2 spills. 203dff0c46cSDimitry Andric if (Reg == ARM::D8) 204dff0c46cSDimitry Andric D8SpillFI = FI; 205dff0c46cSDimitry Andric if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) { 2062754fe60SDimitry Andric AFI->addDPRCalleeSavedAreaFrame(FI); 2072754fe60SDimitry Andric DPRCSSize += 8; 2082754fe60SDimitry Andric } 2092754fe60SDimitry Andric } 210dff0c46cSDimitry Andric } 2112754fe60SDimitry Andric 2122754fe60SDimitry Andric // Move past area 1. 2132754fe60SDimitry Andric if (GPRCS1Size > 0) MBBI++; 2142754fe60SDimitry Andric 2152754fe60SDimitry Andric // Set FP to point to the stack slot that contains the previous FP. 216dff0c46cSDimitry Andric // For iOS, FP is R7, which has now been stored in spill area 1. 217dff0c46cSDimitry Andric // Otherwise, if this is not iOS, all the callee-saved registers go 2182754fe60SDimitry Andric // into spill area 1, including the FP in R11. In either case, it is 2192754fe60SDimitry Andric // now safe to emit this assignment. 2202754fe60SDimitry Andric bool HasFP = hasFP(MF); 2212754fe60SDimitry Andric if (HasFP) { 2222754fe60SDimitry Andric unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; 2232754fe60SDimitry Andric MachineInstrBuilder MIB = 2242754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) 2253b0f4066SDimitry Andric .addFrameIndex(FramePtrSpillFI).addImm(0) 2263b0f4066SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2272754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(MIB)); 2282754fe60SDimitry Andric } 2292754fe60SDimitry Andric 2302754fe60SDimitry Andric // Move past area 2. 2312754fe60SDimitry Andric if (GPRCS2Size > 0) MBBI++; 2322754fe60SDimitry Andric 2332754fe60SDimitry Andric // Determine starting offsets of spill areas. 2342754fe60SDimitry Andric unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); 2352754fe60SDimitry Andric unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; 2362754fe60SDimitry Andric unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; 2372754fe60SDimitry Andric if (HasFP) 2382754fe60SDimitry Andric AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + 2392754fe60SDimitry Andric NumBytes); 2402754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); 2412754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); 2422754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); 2432754fe60SDimitry Andric 2442754fe60SDimitry Andric // Move past area 3. 245dd6029ffSDimitry Andric if (DPRCSSize > 0) { 246dd6029ffSDimitry Andric MBBI++; 247dd6029ffSDimitry Andric // Since vpush register list cannot have gaps, there may be multiple vpush 248dd6029ffSDimitry Andric // instructions in the prologue. 249dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) 250dd6029ffSDimitry Andric MBBI++; 251dd6029ffSDimitry Andric } 2522754fe60SDimitry Andric 253dff0c46cSDimitry Andric // Move past the aligned DPRCS2 area. 254dff0c46cSDimitry Andric if (AFI->getNumAlignedDPRCS2Regs() > 0) { 255dff0c46cSDimitry Andric MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); 256dff0c46cSDimitry Andric // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and 257dff0c46cSDimitry Andric // leaves the stack pointer pointing to the DPRCS2 area. 258dff0c46cSDimitry Andric // 259dff0c46cSDimitry Andric // Adjust NumBytes to represent the stack slots below the DPRCS2 area. 260dff0c46cSDimitry Andric NumBytes += MFI->getObjectOffset(D8SpillFI); 261dff0c46cSDimitry Andric } else 2622754fe60SDimitry Andric NumBytes = DPRCSOffset; 263dff0c46cSDimitry Andric 2642754fe60SDimitry Andric if (NumBytes) { 2652754fe60SDimitry Andric // Adjust SP after all the callee-save spills. 2663b0f4066SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, 2673b0f4066SDimitry Andric MachineInstr::FrameSetup); 2682754fe60SDimitry Andric if (HasFP && isARM) 2692754fe60SDimitry Andric // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 2702754fe60SDimitry Andric // Note it's not safe to do this in Thumb2 mode because it would have 2712754fe60SDimitry Andric // taken two instructions: 2722754fe60SDimitry Andric // mov sp, r7 2732754fe60SDimitry Andric // sub sp, #24 2742754fe60SDimitry Andric // If an interrupt is taken between the two instructions, then sp is in 2752754fe60SDimitry Andric // an inconsistent state (pointing to the middle of callee-saved area). 2762754fe60SDimitry Andric // The interrupt handler can end up clobbering the registers. 2772754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 2782754fe60SDimitry Andric } 2792754fe60SDimitry Andric 2802754fe60SDimitry Andric if (STI.isTargetELF() && hasFP(MF)) 2812754fe60SDimitry Andric MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - 2822754fe60SDimitry Andric AFI->getFramePtrSpillOffset()); 2832754fe60SDimitry Andric 2842754fe60SDimitry Andric AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); 2852754fe60SDimitry Andric AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); 2862754fe60SDimitry Andric AFI->setDPRCalleeSavedAreaSize(DPRCSSize); 2872754fe60SDimitry Andric 2882754fe60SDimitry Andric // If we need dynamic stack realignment, do it here. Be paranoid and make 2892754fe60SDimitry Andric // sure if we also have VLAs, we have a base pointer for frame access. 290dff0c46cSDimitry Andric // If aligned NEON registers were spilled, the stack has already been 291dff0c46cSDimitry Andric // realigned. 292dff0c46cSDimitry Andric if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { 2932754fe60SDimitry Andric unsigned MaxAlign = MFI->getMaxAlignment(); 2942754fe60SDimitry Andric assert (!AFI->isThumb1OnlyFunction()); 2952754fe60SDimitry Andric if (!AFI->isThumbFunction()) { 2962754fe60SDimitry Andric // Emit bic sp, sp, MaxAlign 2972754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 2982754fe60SDimitry Andric TII.get(ARM::BICri), ARM::SP) 2992754fe60SDimitry Andric .addReg(ARM::SP, RegState::Kill) 3002754fe60SDimitry Andric .addImm(MaxAlign-1))); 3012754fe60SDimitry Andric } else { 3022754fe60SDimitry Andric // We cannot use sp as source/dest register here, thus we're emitting the 3032754fe60SDimitry Andric // following sequence: 3042754fe60SDimitry Andric // mov r4, sp 3052754fe60SDimitry Andric // bic r4, r4, MaxAlign 3062754fe60SDimitry Andric // mov sp, r4 3072754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 30817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) 30917a519f9SDimitry Andric .addReg(ARM::SP, RegState::Kill)); 3102754fe60SDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, 3112754fe60SDimitry Andric TII.get(ARM::t2BICri), ARM::R4) 3122754fe60SDimitry Andric .addReg(ARM::R4, RegState::Kill) 3132754fe60SDimitry Andric .addImm(MaxAlign-1))); 31417a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) 31517a519f9SDimitry Andric .addReg(ARM::R4, RegState::Kill)); 3162754fe60SDimitry Andric } 3172754fe60SDimitry Andric 3182754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3192754fe60SDimitry Andric } 3202754fe60SDimitry Andric 3212754fe60SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 3222754fe60SDimitry Andric // of the stack pointer is at this point. Any variable size objects 3232754fe60SDimitry Andric // will be allocated after this, so we can still use the base pointer 3242754fe60SDimitry Andric // to reference locals. 3253b0f4066SDimitry Andric // FIXME: Clarify FrameSetup flags here. 3262754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) { 3272754fe60SDimitry Andric if (isARM) 3282754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, 3292754fe60SDimitry Andric TII.get(ARM::MOVr), RegInfo->getBaseRegister()) 3302754fe60SDimitry Andric .addReg(ARM::SP) 3312754fe60SDimitry Andric .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 3322754fe60SDimitry Andric else 33317a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 33417a519f9SDimitry Andric RegInfo->getBaseRegister()) 33517a519f9SDimitry Andric .addReg(ARM::SP)); 3362754fe60SDimitry Andric } 3372754fe60SDimitry Andric 3382754fe60SDimitry Andric // If the frame has variable sized objects then the epilogue must restore 3392754fe60SDimitry Andric // the sp from fp. We can assume there's an FP here since hasFP already 3402754fe60SDimitry Andric // checks for hasVarSizedObjects. 3412754fe60SDimitry Andric if (MFI->hasVarSizedObjects()) 3422754fe60SDimitry Andric AFI->setShouldRestoreSPFromFP(true); 3432754fe60SDimitry Andric } 3442754fe60SDimitry Andric 3452754fe60SDimitry Andric void ARMFrameLowering::emitEpilogue(MachineFunction &MF, 3462754fe60SDimitry Andric MachineBasicBlock &MBB) const { 3472754fe60SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 348dff0c46cSDimitry Andric assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); 3492754fe60SDimitry Andric unsigned RetOpcode = MBBI->getOpcode(); 3502754fe60SDimitry Andric DebugLoc dl = MBBI->getDebugLoc(); 3512754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 3522754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 3532754fe60SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); 3542754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 3552754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 3562754fe60SDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 3572754fe60SDimitry Andric "This emitEpilogue does not support Thumb1!"); 3582754fe60SDimitry Andric bool isARM = !AFI->isThumbFunction(); 3592754fe60SDimitry Andric 360284c1978SDimitry Andric unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(); 3612754fe60SDimitry Andric int NumBytes = (int)MFI->getStackSize(); 3622754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 3632754fe60SDimitry Andric 3643861d79fSDimitry Andric // All calls are tail calls in GHC calling conv, and functions have no 3653861d79fSDimitry Andric // prologue/epilogue. 3667ae0e2c9SDimitry Andric if (MF.getFunction()->getCallingConv() == CallingConv::GHC) 3677ae0e2c9SDimitry Andric return; 3687ae0e2c9SDimitry Andric 3692754fe60SDimitry Andric if (!AFI->hasStackFrame()) { 3702754fe60SDimitry Andric if (NumBytes != 0) 3712754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 3722754fe60SDimitry Andric } else { 3732754fe60SDimitry Andric // Unwind MBBI to point to first LDR / VLDRD. 374dff0c46cSDimitry Andric const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(); 3752754fe60SDimitry Andric if (MBBI != MBB.begin()) { 3762754fe60SDimitry Andric do 3772754fe60SDimitry Andric --MBBI; 3782754fe60SDimitry Andric while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); 3792754fe60SDimitry Andric if (!isCSRestore(MBBI, TII, CSRegs)) 3802754fe60SDimitry Andric ++MBBI; 3812754fe60SDimitry Andric } 3822754fe60SDimitry Andric 3832754fe60SDimitry Andric // Move SP to start of FP callee save spill area. 3842754fe60SDimitry Andric NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + 3852754fe60SDimitry Andric AFI->getGPRCalleeSavedArea2Size() + 3862754fe60SDimitry Andric AFI->getDPRCalleeSavedAreaSize()); 3872754fe60SDimitry Andric 3882754fe60SDimitry Andric // Reset SP based on frame pointer only if the stack frame extends beyond 3892754fe60SDimitry Andric // frame pointer stack slot or target is ELF and the function has FP. 3902754fe60SDimitry Andric if (AFI->shouldRestoreSPFromFP()) { 3912754fe60SDimitry Andric NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; 3922754fe60SDimitry Andric if (NumBytes) { 3932754fe60SDimitry Andric if (isARM) 3942754fe60SDimitry Andric emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, 3952754fe60SDimitry Andric ARMCC::AL, 0, TII); 3962754fe60SDimitry Andric else { 3972754fe60SDimitry Andric // It's not possible to restore SP from FP in a single instruction. 398dff0c46cSDimitry Andric // For iOS, this looks like: 3992754fe60SDimitry Andric // mov sp, r7 4002754fe60SDimitry Andric // sub sp, #24 4012754fe60SDimitry Andric // This is bad, if an interrupt is taken after the mov, sp is in an 4022754fe60SDimitry Andric // inconsistent state. 4032754fe60SDimitry Andric // Use the first callee-saved register as a scratch register. 4042754fe60SDimitry Andric assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && 4052754fe60SDimitry Andric "No scratch register to restore SP from FP!"); 4062754fe60SDimitry Andric emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, 4072754fe60SDimitry Andric ARMCC::AL, 0, TII); 40817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 40917a519f9SDimitry Andric ARM::SP) 41017a519f9SDimitry Andric .addReg(ARM::R4)); 4112754fe60SDimitry Andric } 4122754fe60SDimitry Andric } else { 4132754fe60SDimitry Andric // Thumb2 or ARM. 4142754fe60SDimitry Andric if (isARM) 4152754fe60SDimitry Andric BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) 4162754fe60SDimitry Andric .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); 4172754fe60SDimitry Andric else 41817a519f9SDimitry Andric AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), 41917a519f9SDimitry Andric ARM::SP) 42017a519f9SDimitry Andric .addReg(FramePtr)); 4212754fe60SDimitry Andric } 4222754fe60SDimitry Andric } else if (NumBytes) 4232754fe60SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); 4242754fe60SDimitry Andric 4252754fe60SDimitry Andric // Increment past our save areas. 426dd6029ffSDimitry Andric if (AFI->getDPRCalleeSavedAreaSize()) { 427dd6029ffSDimitry Andric MBBI++; 428dd6029ffSDimitry Andric // Since vpop register list cannot have gaps, there may be multiple vpop 429dd6029ffSDimitry Andric // instructions in the epilogue. 430dd6029ffSDimitry Andric while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) 431dd6029ffSDimitry Andric MBBI++; 432dd6029ffSDimitry Andric } 4332754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; 4342754fe60SDimitry Andric if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; 4352754fe60SDimitry Andric } 4362754fe60SDimitry Andric 437dff0c46cSDimitry Andric if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) { 4382754fe60SDimitry Andric // Tail call return: adjust the stack pointer and jump to callee. 4392754fe60SDimitry Andric MBBI = MBB.getLastNonDebugInstr(); 4402754fe60SDimitry Andric MachineOperand &JumpTarget = MBBI->getOperand(0); 4412754fe60SDimitry Andric 4422754fe60SDimitry Andric // Jump to label or value in register. 443dff0c46cSDimitry Andric if (RetOpcode == ARM::TCRETURNdi) { 444dff0c46cSDimitry Andric unsigned TCOpcode = STI.isThumb() ? 445dff0c46cSDimitry Andric (STI.isTargetIOS() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : 446dff0c46cSDimitry Andric ARM::TAILJMPd; 4472754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); 4482754fe60SDimitry Andric if (JumpTarget.isGlobal()) 4492754fe60SDimitry Andric MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 4502754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4512754fe60SDimitry Andric else { 4522754fe60SDimitry Andric assert(JumpTarget.isSymbol()); 4532754fe60SDimitry Andric MIB.addExternalSymbol(JumpTarget.getSymbolName(), 4542754fe60SDimitry Andric JumpTarget.getTargetFlags()); 4552754fe60SDimitry Andric } 4566122f3e6SDimitry Andric 4576122f3e6SDimitry Andric // Add the default predicate in Thumb mode. 4586122f3e6SDimitry Andric if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); 4592754fe60SDimitry Andric } else if (RetOpcode == ARM::TCRETURNri) { 4603b0f4066SDimitry Andric BuildMI(MBB, MBBI, dl, 4613b0f4066SDimitry Andric TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). 4622754fe60SDimitry Andric addReg(JumpTarget.getReg(), RegState::Kill); 4632754fe60SDimitry Andric } 4642754fe60SDimitry Andric 4652754fe60SDimitry Andric MachineInstr *NewMI = prior(MBBI); 4662754fe60SDimitry Andric for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) 4672754fe60SDimitry Andric NewMI->addOperand(MBBI->getOperand(i)); 4682754fe60SDimitry Andric 4692754fe60SDimitry Andric // Delete the pseudo instruction TCRETURN. 4702754fe60SDimitry Andric MBB.erase(MBBI); 47117a519f9SDimitry Andric MBBI = NewMI; 4722754fe60SDimitry Andric } 4732754fe60SDimitry Andric 474284c1978SDimitry Andric if (ArgRegsSaveSize) 475284c1978SDimitry Andric emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); 4762754fe60SDimitry Andric } 4772754fe60SDimitry Andric 4782754fe60SDimitry Andric /// getFrameIndexReference - Provide a base+offset reference to an FI slot for 4792754fe60SDimitry Andric /// debug info. It's the same as what we use for resolving the code-gen 4802754fe60SDimitry Andric /// references for now. FIXME: This can go wrong when references are 4812754fe60SDimitry Andric /// SP-relative and simple call frames aren't used. 4822754fe60SDimitry Andric int 4832754fe60SDimitry Andric ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 4842754fe60SDimitry Andric unsigned &FrameReg) const { 4852754fe60SDimitry Andric return ResolveFrameIndexReference(MF, FI, FrameReg, 0); 4862754fe60SDimitry Andric } 4872754fe60SDimitry Andric 4882754fe60SDimitry Andric int 4892754fe60SDimitry Andric ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, 4903b0f4066SDimitry Andric int FI, unsigned &FrameReg, 4912754fe60SDimitry Andric int SPAdj) const { 4922754fe60SDimitry Andric const MachineFrameInfo *MFI = MF.getFrameInfo(); 4932754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 4942754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 4952754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 4962754fe60SDimitry Andric int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); 4972754fe60SDimitry Andric int FPOffset = Offset - AFI->getFramePtrSpillOffset(); 4982754fe60SDimitry Andric bool isFixed = MFI->isFixedObjectIndex(FI); 4992754fe60SDimitry Andric 5002754fe60SDimitry Andric FrameReg = ARM::SP; 5012754fe60SDimitry Andric Offset += SPAdj; 5022754fe60SDimitry Andric if (AFI->isGPRCalleeSavedArea1Frame(FI)) 5032754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea1Offset(); 5042754fe60SDimitry Andric else if (AFI->isGPRCalleeSavedArea2Frame(FI)) 5052754fe60SDimitry Andric return Offset - AFI->getGPRCalleeSavedArea2Offset(); 5062754fe60SDimitry Andric else if (AFI->isDPRCalleeSavedAreaFrame(FI)) 5072754fe60SDimitry Andric return Offset - AFI->getDPRCalleeSavedAreaOffset(); 5082754fe60SDimitry Andric 509dff0c46cSDimitry Andric // SP can move around if there are allocas. We may also lose track of SP 510dff0c46cSDimitry Andric // when emergency spilling inside a non-reserved call frame setup. 511dff0c46cSDimitry Andric bool hasMovingSP = !hasReservedCallFrame(MF); 512dff0c46cSDimitry Andric 5132754fe60SDimitry Andric // When dynamically realigning the stack, use the frame pointer for 5142754fe60SDimitry Andric // parameters, and the stack/base pointer for locals. 5152754fe60SDimitry Andric if (RegInfo->needsStackRealignment(MF)) { 5162754fe60SDimitry Andric assert (hasFP(MF) && "dynamic stack realignment without a FP!"); 5172754fe60SDimitry Andric if (isFixed) { 5182754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5192754fe60SDimitry Andric Offset = FPOffset; 520dff0c46cSDimitry Andric } else if (hasMovingSP) { 5212754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && 5222754fe60SDimitry Andric "VLAs and dynamic stack alignment, but missing base pointer!"); 5232754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5242754fe60SDimitry Andric } 5252754fe60SDimitry Andric return Offset; 5262754fe60SDimitry Andric } 5272754fe60SDimitry Andric 5282754fe60SDimitry Andric // If there is a frame pointer, use it when we can. 5292754fe60SDimitry Andric if (hasFP(MF) && AFI->hasStackFrame()) { 5302754fe60SDimitry Andric // Use frame pointer to reference fixed objects. Use it for locals if 5312754fe60SDimitry Andric // there are VLAs (and thus the SP isn't reliable as a base). 532dff0c46cSDimitry Andric if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { 5332754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5342754fe60SDimitry Andric return FPOffset; 535dff0c46cSDimitry Andric } else if (hasMovingSP) { 5362754fe60SDimitry Andric assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); 5373b0f4066SDimitry Andric if (AFI->isThumb2Function()) { 5382754fe60SDimitry Andric // Try to use the frame pointer if we can, else use the base pointer 5392754fe60SDimitry Andric // since it's available. This is handy for the emergency spill slot, in 5402754fe60SDimitry Andric // particular. 5412754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5422754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5432754fe60SDimitry Andric return FPOffset; 5442754fe60SDimitry Andric } 5453b0f4066SDimitry Andric } 5462754fe60SDimitry Andric } else if (AFI->isThumb2Function()) { 5473b0f4066SDimitry Andric // Use add <rd>, sp, #<imm8> 5483b0f4066SDimitry Andric // ldr <rd>, [sp, #<imm8>] 5493b0f4066SDimitry Andric // if at all possible to save space. 5503b0f4066SDimitry Andric if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) 5513b0f4066SDimitry Andric return Offset; 5522754fe60SDimitry Andric // In Thumb2 mode, the negative offset is very limited. Try to avoid 5533b0f4066SDimitry Andric // out of range references. ldr <rt>,[<rn>, #-<imm8>] 5542754fe60SDimitry Andric if (FPOffset >= -255 && FPOffset < 0) { 5552754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5562754fe60SDimitry Andric return FPOffset; 5572754fe60SDimitry Andric } 5582754fe60SDimitry Andric } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { 5592754fe60SDimitry Andric // Otherwise, use SP or FP, whichever is closer to the stack slot. 5602754fe60SDimitry Andric FrameReg = RegInfo->getFrameRegister(MF); 5612754fe60SDimitry Andric return FPOffset; 5622754fe60SDimitry Andric } 5632754fe60SDimitry Andric } 5642754fe60SDimitry Andric // Use the base pointer if we have one. 5652754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 5662754fe60SDimitry Andric FrameReg = RegInfo->getBaseRegister(); 5672754fe60SDimitry Andric return Offset; 5682754fe60SDimitry Andric } 5692754fe60SDimitry Andric 5702754fe60SDimitry Andric int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, 5712754fe60SDimitry Andric int FI) const { 5722754fe60SDimitry Andric unsigned FrameReg; 5732754fe60SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 5742754fe60SDimitry Andric } 5752754fe60SDimitry Andric 5762754fe60SDimitry Andric void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, 5772754fe60SDimitry Andric MachineBasicBlock::iterator MI, 5782754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 5792754fe60SDimitry Andric unsigned StmOpc, unsigned StrOpc, 5802754fe60SDimitry Andric bool NoGap, 5813b0f4066SDimitry Andric bool(*Func)(unsigned, bool), 582dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 5833b0f4066SDimitry Andric unsigned MIFlags) const { 5842754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 5852754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 5862754fe60SDimitry Andric 5872754fe60SDimitry Andric DebugLoc DL; 5882754fe60SDimitry Andric if (MI != MBB.end()) DL = MI->getDebugLoc(); 5892754fe60SDimitry Andric 5902754fe60SDimitry Andric SmallVector<std::pair<unsigned,bool>, 4> Regs; 5912754fe60SDimitry Andric unsigned i = CSI.size(); 5922754fe60SDimitry Andric while (i != 0) { 5932754fe60SDimitry Andric unsigned LastReg = 0; 5942754fe60SDimitry Andric for (; i != 0; --i) { 5952754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 596dff0c46cSDimitry Andric if (!(Func)(Reg, STI.isTargetIOS())) continue; 597dff0c46cSDimitry Andric 598dff0c46cSDimitry Andric // D-registers in the aligned area DPRCS2 are NOT spilled here. 599dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 600dff0c46cSDimitry Andric continue; 6012754fe60SDimitry Andric 6022754fe60SDimitry Andric // Add the callee-saved register as live-in unless it's LR and 6032754fe60SDimitry Andric // @llvm.returnaddress is called. If LR is returned for 6042754fe60SDimitry Andric // @llvm.returnaddress then it's already added to the function and 6052754fe60SDimitry Andric // entry block live-in sets. 6062754fe60SDimitry Andric bool isKill = true; 6072754fe60SDimitry Andric if (Reg == ARM::LR) { 6082754fe60SDimitry Andric if (MF.getFrameInfo()->isReturnAddressTaken() && 6092754fe60SDimitry Andric MF.getRegInfo().isLiveIn(Reg)) 6102754fe60SDimitry Andric isKill = false; 6112754fe60SDimitry Andric } 6122754fe60SDimitry Andric 6132754fe60SDimitry Andric if (isKill) 6142754fe60SDimitry Andric MBB.addLiveIn(Reg); 6152754fe60SDimitry Andric 6162754fe60SDimitry Andric // If NoGap is true, push consecutive registers and then leave the rest 6172754fe60SDimitry Andric // for other instructions. e.g. 6182754fe60SDimitry Andric // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} 6192754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6202754fe60SDimitry Andric break; 6212754fe60SDimitry Andric LastReg = Reg; 6222754fe60SDimitry Andric Regs.push_back(std::make_pair(Reg, isKill)); 6232754fe60SDimitry Andric } 6242754fe60SDimitry Andric 6252754fe60SDimitry Andric if (Regs.empty()) 6262754fe60SDimitry Andric continue; 6272754fe60SDimitry Andric if (Regs.size() > 1 || StrOpc== 0) { 6282754fe60SDimitry Andric MachineInstrBuilder MIB = 6292754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) 6303b0f4066SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags)); 6312754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 6322754fe60SDimitry Andric MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); 6332754fe60SDimitry Andric } else if (Regs.size() == 1) { 6342754fe60SDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), 6352754fe60SDimitry Andric ARM::SP) 6362754fe60SDimitry Andric .addReg(Regs[0].first, getKillRegState(Regs[0].second)) 6376122f3e6SDimitry Andric .addReg(ARM::SP).setMIFlags(MIFlags) 6386122f3e6SDimitry Andric .addImm(-4); 6392754fe60SDimitry Andric AddDefaultPred(MIB); 6402754fe60SDimitry Andric } 6412754fe60SDimitry Andric Regs.clear(); 6422754fe60SDimitry Andric } 6432754fe60SDimitry Andric } 6442754fe60SDimitry Andric 6452754fe60SDimitry Andric void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, 6462754fe60SDimitry Andric MachineBasicBlock::iterator MI, 6472754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 6482754fe60SDimitry Andric unsigned LdmOpc, unsigned LdrOpc, 6492754fe60SDimitry Andric bool isVarArg, bool NoGap, 650dff0c46cSDimitry Andric bool(*Func)(unsigned, bool), 651dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) const { 6522754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 6532754fe60SDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 6542754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 6552754fe60SDimitry Andric DebugLoc DL = MI->getDebugLoc(); 6562754fe60SDimitry Andric unsigned RetOpcode = MI->getOpcode(); 6572754fe60SDimitry Andric bool isTailCall = (RetOpcode == ARM::TCRETURNdi || 658dff0c46cSDimitry Andric RetOpcode == ARM::TCRETURNri); 6592754fe60SDimitry Andric 6602754fe60SDimitry Andric SmallVector<unsigned, 4> Regs; 6612754fe60SDimitry Andric unsigned i = CSI.size(); 6622754fe60SDimitry Andric while (i != 0) { 6632754fe60SDimitry Andric unsigned LastReg = 0; 6642754fe60SDimitry Andric bool DeleteRet = false; 6652754fe60SDimitry Andric for (; i != 0; --i) { 6662754fe60SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 667dff0c46cSDimitry Andric if (!(Func)(Reg, STI.isTargetIOS())) continue; 668dff0c46cSDimitry Andric 669dff0c46cSDimitry Andric // The aligned reloads from area DPRCS2 are not inserted here. 670dff0c46cSDimitry Andric if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) 671dff0c46cSDimitry Andric continue; 6722754fe60SDimitry Andric 6732754fe60SDimitry Andric if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) { 6742754fe60SDimitry Andric Reg = ARM::PC; 6752754fe60SDimitry Andric LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; 6762754fe60SDimitry Andric // Fold the return instruction into the LDM. 6772754fe60SDimitry Andric DeleteRet = true; 6782754fe60SDimitry Andric } 6792754fe60SDimitry Andric 6802754fe60SDimitry Andric // If NoGap is true, pop consecutive registers and then leave the rest 6812754fe60SDimitry Andric // for other instructions. e.g. 6822754fe60SDimitry Andric // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} 6832754fe60SDimitry Andric if (NoGap && LastReg && LastReg != Reg-1) 6842754fe60SDimitry Andric break; 6852754fe60SDimitry Andric 6862754fe60SDimitry Andric LastReg = Reg; 6872754fe60SDimitry Andric Regs.push_back(Reg); 6882754fe60SDimitry Andric } 6892754fe60SDimitry Andric 6902754fe60SDimitry Andric if (Regs.empty()) 6912754fe60SDimitry Andric continue; 6922754fe60SDimitry Andric if (Regs.size() > 1 || LdrOpc == 0) { 6932754fe60SDimitry Andric MachineInstrBuilder MIB = 6942754fe60SDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) 6952754fe60SDimitry Andric .addReg(ARM::SP)); 6962754fe60SDimitry Andric for (unsigned i = 0, e = Regs.size(); i < e; ++i) 6972754fe60SDimitry Andric MIB.addReg(Regs[i], getDefRegState(true)); 6986122f3e6SDimitry Andric if (DeleteRet) { 699139f7f9bSDimitry Andric MIB.copyImplicitOps(&*MI); 7002754fe60SDimitry Andric MI->eraseFromParent(); 7016122f3e6SDimitry Andric } 7022754fe60SDimitry Andric MI = MIB; 7032754fe60SDimitry Andric } else if (Regs.size() == 1) { 7042754fe60SDimitry Andric // If we adjusted the reg to PC from LR above, switch it back here. We 7052754fe60SDimitry Andric // only do that for LDM. 7062754fe60SDimitry Andric if (Regs[0] == ARM::PC) 7072754fe60SDimitry Andric Regs[0] = ARM::LR; 7082754fe60SDimitry Andric MachineInstrBuilder MIB = 7092754fe60SDimitry Andric BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) 7102754fe60SDimitry Andric .addReg(ARM::SP, RegState::Define) 7112754fe60SDimitry Andric .addReg(ARM::SP); 7122754fe60SDimitry Andric // ARM mode needs an extra reg0 here due to addrmode2. Will go away once 7132754fe60SDimitry Andric // that refactoring is complete (eventually). 7146122f3e6SDimitry Andric if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { 7152754fe60SDimitry Andric MIB.addReg(0); 7162754fe60SDimitry Andric MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); 7172754fe60SDimitry Andric } else 7182754fe60SDimitry Andric MIB.addImm(4); 7192754fe60SDimitry Andric AddDefaultPred(MIB); 7202754fe60SDimitry Andric } 7212754fe60SDimitry Andric Regs.clear(); 7222754fe60SDimitry Andric } 7232754fe60SDimitry Andric } 7242754fe60SDimitry Andric 725dff0c46cSDimitry Andric /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers 726dff0c46cSDimitry Andric /// starting from d8. Also insert stack realignment code and leave the stack 727dff0c46cSDimitry Andric /// pointer pointing to the d8 spill slot. 728dff0c46cSDimitry Andric static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, 729dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 730dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 731dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 732dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 733dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 734dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 735dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 736dff0c46cSDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 737dff0c46cSDimitry Andric MachineFrameInfo &MFI = *MF.getFrameInfo(); 738dff0c46cSDimitry Andric 739dff0c46cSDimitry Andric // Mark the D-register spill slots as properly aligned. Since MFI computes 740dff0c46cSDimitry Andric // stack slot layout backwards, this can actually mean that the d-reg stack 741dff0c46cSDimitry Andric // slot offsets can be wrong. The offset for d8 will always be correct. 742dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 743dff0c46cSDimitry Andric unsigned DNum = CSI[i].getReg() - ARM::D8; 744dff0c46cSDimitry Andric if (DNum >= 8) 745dff0c46cSDimitry Andric continue; 746dff0c46cSDimitry Andric int FI = CSI[i].getFrameIdx(); 747dff0c46cSDimitry Andric // The even-numbered registers will be 16-byte aligned, the odd-numbered 748dff0c46cSDimitry Andric // registers will be 8-byte aligned. 749dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); 750dff0c46cSDimitry Andric 751dff0c46cSDimitry Andric // The stack slot for D8 needs to be maximally aligned because this is 752dff0c46cSDimitry Andric // actually the point where we align the stack pointer. MachineFrameInfo 753dff0c46cSDimitry Andric // computes all offsets relative to the incoming stack pointer which is a 754dff0c46cSDimitry Andric // bit weird when realigning the stack. Any extra padding for this 755dff0c46cSDimitry Andric // over-alignment is not realized because the code inserted below adjusts 756dff0c46cSDimitry Andric // the stack pointer by numregs * 8 before aligning the stack pointer. 757dff0c46cSDimitry Andric if (DNum == 0) 758dff0c46cSDimitry Andric MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); 759dff0c46cSDimitry Andric } 760dff0c46cSDimitry Andric 761dff0c46cSDimitry Andric // Move the stack pointer to the d8 spill slot, and align it at the same 762dff0c46cSDimitry Andric // time. Leave the stack slot address in the scratch register r4. 763dff0c46cSDimitry Andric // 764dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 765dff0c46cSDimitry Andric // bic r4, r4, #align - 1 766dff0c46cSDimitry Andric // mov sp, r4 767dff0c46cSDimitry Andric // 768dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 769dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 770dff0c46cSDimitry Andric AFI->setShouldRestoreSPFromFP(true); 771dff0c46cSDimitry Andric 772dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 773dff0c46cSDimitry Andric // The immediate is <= 64, so it doesn't need any special encoding. 774dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; 775dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 776dff0c46cSDimitry Andric .addReg(ARM::SP) 777dff0c46cSDimitry Andric .addImm(8 * NumAlignedDPRCS2Regs))); 778dff0c46cSDimitry Andric 779dff0c46cSDimitry Andric // bic r4, r4, #align-1 780dff0c46cSDimitry Andric Opc = isThumb ? ARM::t2BICri : ARM::BICri; 781dff0c46cSDimitry Andric unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); 782dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 783dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill) 784dff0c46cSDimitry Andric .addImm(MaxAlign - 1))); 785dff0c46cSDimitry Andric 786dff0c46cSDimitry Andric // mov sp, r4 787dff0c46cSDimitry Andric // The stack pointer must be adjusted before spilling anything, otherwise 788dff0c46cSDimitry Andric // the stack slots could be clobbered by an interrupt handler. 789dff0c46cSDimitry Andric // Leave r4 live, it is used below. 790dff0c46cSDimitry Andric Opc = isThumb ? ARM::tMOVr : ARM::MOVr; 791dff0c46cSDimitry Andric MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) 792dff0c46cSDimitry Andric .addReg(ARM::R4); 793dff0c46cSDimitry Andric MIB = AddDefaultPred(MIB); 794dff0c46cSDimitry Andric if (!isThumb) 795dff0c46cSDimitry Andric AddDefaultCC(MIB); 796dff0c46cSDimitry Andric 797dff0c46cSDimitry Andric // Now spill NumAlignedDPRCS2Regs registers starting from d8. 798dff0c46cSDimitry Andric // r4 holds the stack slot address. 799dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 800dff0c46cSDimitry Andric 801dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs and address writeback. 802dff0c46cSDimitry Andric // The writeback is only needed when emitting two vst1.64 instructions. 803dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 804dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8057ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 806dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 807dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), 808dff0c46cSDimitry Andric ARM::R4) 809dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 810dff0c46cSDimitry Andric .addReg(NextReg) 811dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 812dff0c46cSDimitry Andric NextReg += 4; 813dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 814dff0c46cSDimitry Andric } 815dff0c46cSDimitry Andric 816dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 817dff0c46cSDimitry Andric // register to be spilled. 818dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 819dff0c46cSDimitry Andric 820dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 4 d-regs, no writeback. 821dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 822dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8237ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 824dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 825dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) 826dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(NextReg) 827dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitKill)); 828dff0c46cSDimitry Andric NextReg += 4; 829dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 830dff0c46cSDimitry Andric } 831dff0c46cSDimitry Andric 832dff0c46cSDimitry Andric // 16-byte aligned vst1.64 with 2 d-regs. 833dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 834dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 8357ae0e2c9SDimitry Andric &ARM::QPRRegClass); 836dff0c46cSDimitry Andric MBB.addLiveIn(SupReg); 837dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) 838dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16).addReg(SupReg)); 839dff0c46cSDimitry Andric NextReg += 2; 840dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 841dff0c46cSDimitry Andric } 842dff0c46cSDimitry Andric 843dff0c46cSDimitry Andric // Finally, use a vanilla vstr.64 for the odd last register. 844dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) { 845dff0c46cSDimitry Andric MBB.addLiveIn(NextReg); 846dff0c46cSDimitry Andric // vstr.64 uses addrmode5 which has an offset scale of 4. 847dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) 848dff0c46cSDimitry Andric .addReg(NextReg) 849dff0c46cSDimitry Andric .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); 850dff0c46cSDimitry Andric } 851dff0c46cSDimitry Andric 852dff0c46cSDimitry Andric // The last spill instruction inserted should kill the scratch register r4. 853dff0c46cSDimitry Andric llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI); 854dff0c46cSDimitry Andric } 855dff0c46cSDimitry Andric 856dff0c46cSDimitry Andric /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an 857dff0c46cSDimitry Andric /// iterator to the following instruction. 858dff0c46cSDimitry Andric static MachineBasicBlock::iterator 859dff0c46cSDimitry Andric skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, 860dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs) { 861dff0c46cSDimitry Andric // sub r4, sp, #numregs * 8 862dff0c46cSDimitry Andric // bic r4, r4, #align - 1 863dff0c46cSDimitry Andric // mov sp, r4 864dff0c46cSDimitry Andric ++MI; ++MI; ++MI; 865dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 866dff0c46cSDimitry Andric 867dff0c46cSDimitry Andric // These switches all fall through. 868dff0c46cSDimitry Andric switch(NumAlignedDPRCS2Regs) { 869dff0c46cSDimitry Andric case 7: 870dff0c46cSDimitry Andric ++MI; 871dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 872dff0c46cSDimitry Andric default: 873dff0c46cSDimitry Andric ++MI; 874dff0c46cSDimitry Andric assert(MI->mayStore() && "Expecting spill instruction"); 875dff0c46cSDimitry Andric case 1: 876dff0c46cSDimitry Andric case 2: 877dff0c46cSDimitry Andric case 4: 878dff0c46cSDimitry Andric assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); 879dff0c46cSDimitry Andric ++MI; 880dff0c46cSDimitry Andric } 881dff0c46cSDimitry Andric return MI; 882dff0c46cSDimitry Andric } 883dff0c46cSDimitry Andric 884dff0c46cSDimitry Andric /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers 885dff0c46cSDimitry Andric /// starting from d8. These instructions are assumed to execute while the 886dff0c46cSDimitry Andric /// stack is still aligned, unlike the code inserted by emitPopInst. 887dff0c46cSDimitry Andric static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, 888dff0c46cSDimitry Andric MachineBasicBlock::iterator MI, 889dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs, 890dff0c46cSDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 891dff0c46cSDimitry Andric const TargetRegisterInfo *TRI) { 892dff0c46cSDimitry Andric MachineFunction &MF = *MBB.getParent(); 893dff0c46cSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 894dff0c46cSDimitry Andric DebugLoc DL = MI->getDebugLoc(); 895dff0c46cSDimitry Andric const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 896dff0c46cSDimitry Andric 897dff0c46cSDimitry Andric // Find the frame index assigned to d8. 898dff0c46cSDimitry Andric int D8SpillFI = 0; 899dff0c46cSDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) 900dff0c46cSDimitry Andric if (CSI[i].getReg() == ARM::D8) { 901dff0c46cSDimitry Andric D8SpillFI = CSI[i].getFrameIdx(); 902dff0c46cSDimitry Andric break; 903dff0c46cSDimitry Andric } 904dff0c46cSDimitry Andric 905dff0c46cSDimitry Andric // Materialize the address of the d8 spill slot into the scratch register r4. 906dff0c46cSDimitry Andric // This can be fairly complicated if the stack frame is large, so just use 907dff0c46cSDimitry Andric // the normal frame index elimination mechanism to do it. This code runs as 908dff0c46cSDimitry Andric // the initial part of the epilog where the stack and base pointers haven't 909dff0c46cSDimitry Andric // been changed yet. 910dff0c46cSDimitry Andric bool isThumb = AFI->isThumbFunction(); 911dff0c46cSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); 912dff0c46cSDimitry Andric 913dff0c46cSDimitry Andric unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; 914dff0c46cSDimitry Andric AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) 915dff0c46cSDimitry Andric .addFrameIndex(D8SpillFI).addImm(0))); 916dff0c46cSDimitry Andric 917dff0c46cSDimitry Andric // Now restore NumAlignedDPRCS2Regs registers starting from d8. 918dff0c46cSDimitry Andric unsigned NextReg = ARM::D8; 919dff0c46cSDimitry Andric 920dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs and writeback. 921dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 6) { 922dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9237ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 924dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) 925dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Define) 926dff0c46cSDimitry Andric .addReg(ARM::R4, RegState::Kill).addImm(16) 927dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 928dff0c46cSDimitry Andric NextReg += 4; 929dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 930dff0c46cSDimitry Andric } 931dff0c46cSDimitry Andric 932dff0c46cSDimitry Andric // We won't modify r4 beyond this point. It currently points to the next 933dff0c46cSDimitry Andric // register to be spilled. 934dff0c46cSDimitry Andric unsigned R4BaseReg = NextReg; 935dff0c46cSDimitry Andric 936dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 4 d-regs, no writeback. 937dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 4) { 938dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9397ae0e2c9SDimitry Andric &ARM::QQPRRegClass); 940dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) 941dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16) 942dff0c46cSDimitry Andric .addReg(SupReg, RegState::ImplicitDefine)); 943dff0c46cSDimitry Andric NextReg += 4; 944dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 4; 945dff0c46cSDimitry Andric } 946dff0c46cSDimitry Andric 947dff0c46cSDimitry Andric // 16-byte aligned vld1.64 with 2 d-regs. 948dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs >= 2) { 949dff0c46cSDimitry Andric unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, 9507ae0e2c9SDimitry Andric &ARM::QPRRegClass); 951dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) 952dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(16)); 953dff0c46cSDimitry Andric NextReg += 2; 954dff0c46cSDimitry Andric NumAlignedDPRCS2Regs -= 2; 955dff0c46cSDimitry Andric } 956dff0c46cSDimitry Andric 957dff0c46cSDimitry Andric // Finally, use a vanilla vldr.64 for the remaining odd register. 958dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 959dff0c46cSDimitry Andric AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) 960dff0c46cSDimitry Andric .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); 961dff0c46cSDimitry Andric 962dff0c46cSDimitry Andric // Last store kills r4. 963dff0c46cSDimitry Andric llvm::prior(MI)->addRegisterKilled(ARM::R4, TRI); 964dff0c46cSDimitry Andric } 965dff0c46cSDimitry Andric 9662754fe60SDimitry Andric bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 9672754fe60SDimitry Andric MachineBasicBlock::iterator MI, 9682754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 9692754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 9702754fe60SDimitry Andric if (CSI.empty()) 9712754fe60SDimitry Andric return false; 9722754fe60SDimitry Andric 9732754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 9742754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 9752754fe60SDimitry Andric 9762754fe60SDimitry Andric unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; 9776122f3e6SDimitry Andric unsigned PushOneOpc = AFI->isThumbFunction() ? 9786122f3e6SDimitry Andric ARM::t2STR_PRE : ARM::STR_PRE_IMM; 9792754fe60SDimitry Andric unsigned FltOpc = ARM::VSTMDDB_UPD; 980dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 981dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, 9823b0f4066SDimitry Andric MachineInstr::FrameSetup); 983dff0c46cSDimitry Andric emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, 9843b0f4066SDimitry Andric MachineInstr::FrameSetup); 9853b0f4066SDimitry Andric emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, 986dff0c46cSDimitry Andric NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); 987dff0c46cSDimitry Andric 988dff0c46cSDimitry Andric // The code above does not insert spill code for the aligned DPRCS2 registers. 989dff0c46cSDimitry Andric // The stack realignment code will be inserted between the push instructions 990dff0c46cSDimitry Andric // and these spills. 991dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 992dff0c46cSDimitry Andric emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 9932754fe60SDimitry Andric 9942754fe60SDimitry Andric return true; 9952754fe60SDimitry Andric } 9962754fe60SDimitry Andric 9972754fe60SDimitry Andric bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 9982754fe60SDimitry Andric MachineBasicBlock::iterator MI, 9992754fe60SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 10002754fe60SDimitry Andric const TargetRegisterInfo *TRI) const { 10012754fe60SDimitry Andric if (CSI.empty()) 10022754fe60SDimitry Andric return false; 10032754fe60SDimitry Andric 10042754fe60SDimitry Andric MachineFunction &MF = *MBB.getParent(); 10052754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1006284c1978SDimitry Andric bool isVarArg = AFI->getArgRegsSaveSize() > 0; 1007dff0c46cSDimitry Andric unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); 1008dff0c46cSDimitry Andric 1009dff0c46cSDimitry Andric // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 1010dff0c46cSDimitry Andric // registers. Do that here instead. 1011dff0c46cSDimitry Andric if (NumAlignedDPRCS2Regs) 1012dff0c46cSDimitry Andric emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); 10132754fe60SDimitry Andric 10142754fe60SDimitry Andric unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; 10156122f3e6SDimitry Andric unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; 10162754fe60SDimitry Andric unsigned FltOpc = ARM::VLDMDIA_UPD; 1017dff0c46cSDimitry Andric emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, 1018dff0c46cSDimitry Andric NumAlignedDPRCS2Regs); 10192754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1020dff0c46cSDimitry Andric &isARMArea2Register, 0); 10212754fe60SDimitry Andric emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, 1022dff0c46cSDimitry Andric &isARMArea1Register, 0); 10232754fe60SDimitry Andric 10242754fe60SDimitry Andric return true; 10252754fe60SDimitry Andric } 10262754fe60SDimitry Andric 10272754fe60SDimitry Andric // FIXME: Make generic? 10282754fe60SDimitry Andric static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, 10292754fe60SDimitry Andric const ARMBaseInstrInfo &TII) { 10302754fe60SDimitry Andric unsigned FnSize = 0; 10312754fe60SDimitry Andric for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 10322754fe60SDimitry Andric MBBI != E; ++MBBI) { 10332754fe60SDimitry Andric const MachineBasicBlock &MBB = *MBBI; 10342754fe60SDimitry Andric for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 10352754fe60SDimitry Andric I != E; ++I) 10362754fe60SDimitry Andric FnSize += TII.GetInstSizeInBytes(I); 10372754fe60SDimitry Andric } 10382754fe60SDimitry Andric return FnSize; 10392754fe60SDimitry Andric } 10402754fe60SDimitry Andric 10412754fe60SDimitry Andric /// estimateRSStackSizeLimit - Look at each instruction that references stack 10422754fe60SDimitry Andric /// frames and return the stack size limit beyond which some of these 10432754fe60SDimitry Andric /// instructions will require a scratch register during their expansion later. 10442754fe60SDimitry Andric // FIXME: Move to TII? 10452754fe60SDimitry Andric static unsigned estimateRSStackSizeLimit(MachineFunction &MF, 10462754fe60SDimitry Andric const TargetFrameLowering *TFI) { 10472754fe60SDimitry Andric const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 10482754fe60SDimitry Andric unsigned Limit = (1 << 12) - 1; 10492754fe60SDimitry Andric for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { 10502754fe60SDimitry Andric for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); 10512754fe60SDimitry Andric I != E; ++I) { 10522754fe60SDimitry Andric for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 10532754fe60SDimitry Andric if (!I->getOperand(i).isFI()) continue; 10542754fe60SDimitry Andric 10552754fe60SDimitry Andric // When using ADDri to get the address of a stack object, 255 is the 10562754fe60SDimitry Andric // largest offset guaranteed to fit in the immediate offset. 10572754fe60SDimitry Andric if (I->getOpcode() == ARM::ADDri) { 10582754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10592754fe60SDimitry Andric break; 10602754fe60SDimitry Andric } 10612754fe60SDimitry Andric 10622754fe60SDimitry Andric // Otherwise check the addressing mode. 10632754fe60SDimitry Andric switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { 10642754fe60SDimitry Andric case ARMII::AddrMode3: 10652754fe60SDimitry Andric case ARMII::AddrModeT2_i8: 10662754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10672754fe60SDimitry Andric break; 10682754fe60SDimitry Andric case ARMII::AddrMode5: 10692754fe60SDimitry Andric case ARMII::AddrModeT2_i8s4: 10702754fe60SDimitry Andric Limit = std::min(Limit, ((1U << 8) - 1) * 4); 10712754fe60SDimitry Andric break; 10722754fe60SDimitry Andric case ARMII::AddrModeT2_i12: 10732754fe60SDimitry Andric // i12 supports only positive offset so these will be converted to 10742754fe60SDimitry Andric // i8 opcodes. See llvm::rewriteT2FrameIndex. 10752754fe60SDimitry Andric if (TFI->hasFP(MF) && AFI->hasStackFrame()) 10762754fe60SDimitry Andric Limit = std::min(Limit, (1U << 8) - 1); 10772754fe60SDimitry Andric break; 10782754fe60SDimitry Andric case ARMII::AddrMode4: 10792754fe60SDimitry Andric case ARMII::AddrMode6: 10802754fe60SDimitry Andric // Addressing modes 4 & 6 (load/store) instructions can't encode an 10812754fe60SDimitry Andric // immediate offset for stack references. 10822754fe60SDimitry Andric return 0; 10832754fe60SDimitry Andric default: 10842754fe60SDimitry Andric break; 10852754fe60SDimitry Andric } 10862754fe60SDimitry Andric break; // At most one FI per instruction 10872754fe60SDimitry Andric } 10882754fe60SDimitry Andric } 10892754fe60SDimitry Andric } 10902754fe60SDimitry Andric 10912754fe60SDimitry Andric return Limit; 10922754fe60SDimitry Andric } 10932754fe60SDimitry Andric 1094dff0c46cSDimitry Andric // In functions that realign the stack, it can be an advantage to spill the 1095dff0c46cSDimitry Andric // callee-saved vector registers after realigning the stack. The vst1 and vld1 1096dff0c46cSDimitry Andric // instructions take alignment hints that can improve performance. 1097dff0c46cSDimitry Andric // 1098dff0c46cSDimitry Andric static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { 1099dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); 1100dff0c46cSDimitry Andric if (!SpillAlignedNEONRegs) 1101dff0c46cSDimitry Andric return; 1102dff0c46cSDimitry Andric 1103dff0c46cSDimitry Andric // Naked functions don't spill callee-saved registers. 1104139f7f9bSDimitry Andric if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 1105139f7f9bSDimitry Andric Attribute::Naked)) 1106dff0c46cSDimitry Andric return; 1107dff0c46cSDimitry Andric 1108dff0c46cSDimitry Andric // We are planning to use NEON instructions vst1 / vld1. 1109dff0c46cSDimitry Andric if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON()) 1110dff0c46cSDimitry Andric return; 1111dff0c46cSDimitry Andric 1112dff0c46cSDimitry Andric // Don't bother if the default stack alignment is sufficiently high. 1113dff0c46cSDimitry Andric if (MF.getTarget().getFrameLowering()->getStackAlignment() >= 8) 1114dff0c46cSDimitry Andric return; 1115dff0c46cSDimitry Andric 1116dff0c46cSDimitry Andric // Aligned spills require stack realignment. 1117dff0c46cSDimitry Andric const ARMBaseRegisterInfo *RegInfo = 1118dff0c46cSDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 1119dff0c46cSDimitry Andric if (!RegInfo->canRealignStack(MF)) 1120dff0c46cSDimitry Andric return; 1121dff0c46cSDimitry Andric 1122dff0c46cSDimitry Andric // We always spill contiguous d-registers starting from d8. Count how many 1123dff0c46cSDimitry Andric // needs spilling. The register allocator will almost always use the 1124dff0c46cSDimitry Andric // callee-saved registers in order, but it can happen that there are holes in 1125dff0c46cSDimitry Andric // the range. Registers above the hole will be spilled to the standard DPRCS 1126dff0c46cSDimitry Andric // area. 1127dff0c46cSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 1128dff0c46cSDimitry Andric unsigned NumSpills = 0; 1129dff0c46cSDimitry Andric for (; NumSpills < 8; ++NumSpills) 11303861d79fSDimitry Andric if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) 1131dff0c46cSDimitry Andric break; 1132dff0c46cSDimitry Andric 1133dff0c46cSDimitry Andric // Don't do this for just one d-register. It's not worth it. 1134dff0c46cSDimitry Andric if (NumSpills < 2) 1135dff0c46cSDimitry Andric return; 1136dff0c46cSDimitry Andric 1137dff0c46cSDimitry Andric // Spill the first NumSpills D-registers after realigning the stack. 1138dff0c46cSDimitry Andric MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); 1139dff0c46cSDimitry Andric 1140dff0c46cSDimitry Andric // A scratch register is required for the vst1 / vld1 instructions. 1141dff0c46cSDimitry Andric MF.getRegInfo().setPhysRegUsed(ARM::R4); 1142dff0c46cSDimitry Andric } 1143dff0c46cSDimitry Andric 11442754fe60SDimitry Andric void 11452754fe60SDimitry Andric ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 11462754fe60SDimitry Andric RegScavenger *RS) const { 11472754fe60SDimitry Andric // This tells PEI to spill the FP as if it is any other callee-save register 11482754fe60SDimitry Andric // to take advantage the eliminateFrameIndex machinery. This also ensures it 11492754fe60SDimitry Andric // is spilled in the order specified by getCalleeSavedRegs() to make it easier 11502754fe60SDimitry Andric // to combine multiple loads / stores. 11512754fe60SDimitry Andric bool CanEliminateFrame = true; 11522754fe60SDimitry Andric bool CS1Spilled = false; 11532754fe60SDimitry Andric bool LRSpilled = false; 11542754fe60SDimitry Andric unsigned NumGPRSpills = 0; 11552754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS1GPRs; 11562754fe60SDimitry Andric SmallVector<unsigned, 4> UnspilledCS2GPRs; 11572754fe60SDimitry Andric const ARMBaseRegisterInfo *RegInfo = 11582754fe60SDimitry Andric static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); 11592754fe60SDimitry Andric const ARMBaseInstrInfo &TII = 11602754fe60SDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 11612754fe60SDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 11622754fe60SDimitry Andric MachineFrameInfo *MFI = MF.getFrameInfo(); 11633861d79fSDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 11642754fe60SDimitry Andric unsigned FramePtr = RegInfo->getFrameRegister(MF); 11652754fe60SDimitry Andric 11662754fe60SDimitry Andric // Spill R4 if Thumb2 function requires stack realignment - it will be used as 11672754fe60SDimitry Andric // scratch register. Also spill R4 if Thumb2 function has varsized objects, 11682754fe60SDimitry Andric // since it's not always possible to restore sp from fp in a single 11692754fe60SDimitry Andric // instruction. 11702754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 11712754fe60SDimitry Andric if (AFI->isThumb2Function() && 11722754fe60SDimitry Andric (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) 11733861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 11742754fe60SDimitry Andric 11752754fe60SDimitry Andric if (AFI->isThumb1OnlyFunction()) { 11762754fe60SDimitry Andric // Spill LR if Thumb1 function uses variable length argument lists. 1177284c1978SDimitry Andric if (AFI->getArgRegsSaveSize() > 0) 11783861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 11792754fe60SDimitry Andric 118017a519f9SDimitry Andric // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know 118117a519f9SDimitry Andric // for sure what the stack size will be, but for this, an estimate is good 118217a519f9SDimitry Andric // enough. If there anything changes it, it'll be a spill, which implies 118317a519f9SDimitry Andric // we've used all the registers and so R4 is already used, so not marking 118417a519f9SDimitry Andric // it here will be OK. 11852754fe60SDimitry Andric // FIXME: It will be better just to find spare register here. 1186139f7f9bSDimitry Andric unsigned StackSize = MFI->estimateStackSize(MF); 118717a519f9SDimitry Andric if (MFI->hasVarSizedObjects() || StackSize > 508) 11883861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::R4); 11892754fe60SDimitry Andric } 11902754fe60SDimitry Andric 1191dff0c46cSDimitry Andric // See if we can spill vector registers to aligned stack. 1192dff0c46cSDimitry Andric checkNumAlignedDPRCS2Regs(MF); 1193dff0c46cSDimitry Andric 11942754fe60SDimitry Andric // Spill the BasePtr if it's used. 11952754fe60SDimitry Andric if (RegInfo->hasBasePointer(MF)) 11963861d79fSDimitry Andric MRI.setPhysRegUsed(RegInfo->getBaseRegister()); 11972754fe60SDimitry Andric 11982754fe60SDimitry Andric // Don't spill FP if the frame can be eliminated. This is determined 11992754fe60SDimitry Andric // by scanning the callee-save registers to see if any is used. 1200dff0c46cSDimitry Andric const uint16_t *CSRegs = RegInfo->getCalleeSavedRegs(); 12012754fe60SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 12022754fe60SDimitry Andric unsigned Reg = CSRegs[i]; 12032754fe60SDimitry Andric bool Spilled = false; 12043861d79fSDimitry Andric if (MRI.isPhysRegUsed(Reg)) { 12052754fe60SDimitry Andric Spilled = true; 12062754fe60SDimitry Andric CanEliminateFrame = false; 12072754fe60SDimitry Andric } 12082754fe60SDimitry Andric 12097ae0e2c9SDimitry Andric if (!ARM::GPRRegClass.contains(Reg)) 12102754fe60SDimitry Andric continue; 12112754fe60SDimitry Andric 12122754fe60SDimitry Andric if (Spilled) { 12132754fe60SDimitry Andric NumGPRSpills++; 12142754fe60SDimitry Andric 1215dff0c46cSDimitry Andric if (!STI.isTargetIOS()) { 12162754fe60SDimitry Andric if (Reg == ARM::LR) 12172754fe60SDimitry Andric LRSpilled = true; 12182754fe60SDimitry Andric CS1Spilled = true; 12192754fe60SDimitry Andric continue; 12202754fe60SDimitry Andric } 12212754fe60SDimitry Andric 12222754fe60SDimitry Andric // Keep track if LR and any of R4, R5, R6, and R7 is spilled. 12232754fe60SDimitry Andric switch (Reg) { 12242754fe60SDimitry Andric case ARM::LR: 12252754fe60SDimitry Andric LRSpilled = true; 12262754fe60SDimitry Andric // Fallthrough 12272754fe60SDimitry Andric case ARM::R4: case ARM::R5: 12282754fe60SDimitry Andric case ARM::R6: case ARM::R7: 12292754fe60SDimitry Andric CS1Spilled = true; 12302754fe60SDimitry Andric break; 12312754fe60SDimitry Andric default: 12322754fe60SDimitry Andric break; 12332754fe60SDimitry Andric } 12342754fe60SDimitry Andric } else { 1235dff0c46cSDimitry Andric if (!STI.isTargetIOS()) { 12362754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 12372754fe60SDimitry Andric continue; 12382754fe60SDimitry Andric } 12392754fe60SDimitry Andric 12402754fe60SDimitry Andric switch (Reg) { 12412754fe60SDimitry Andric case ARM::R4: case ARM::R5: 12422754fe60SDimitry Andric case ARM::R6: case ARM::R7: 12432754fe60SDimitry Andric case ARM::LR: 12442754fe60SDimitry Andric UnspilledCS1GPRs.push_back(Reg); 12452754fe60SDimitry Andric break; 12462754fe60SDimitry Andric default: 12472754fe60SDimitry Andric UnspilledCS2GPRs.push_back(Reg); 12482754fe60SDimitry Andric break; 12492754fe60SDimitry Andric } 12502754fe60SDimitry Andric } 12512754fe60SDimitry Andric } 12522754fe60SDimitry Andric 12532754fe60SDimitry Andric bool ForceLRSpill = false; 12542754fe60SDimitry Andric if (!LRSpilled && AFI->isThumb1OnlyFunction()) { 12552754fe60SDimitry Andric unsigned FnSize = GetFunctionSizeInBytes(MF, TII); 12562754fe60SDimitry Andric // Force LR to be spilled if the Thumb function size is > 2048. This enables 12572754fe60SDimitry Andric // use of BL to implement far jump. If it turns out that it's not needed 12582754fe60SDimitry Andric // then the branch fix up path will undo it. 12592754fe60SDimitry Andric if (FnSize >= (1 << 11)) { 12602754fe60SDimitry Andric CanEliminateFrame = false; 12612754fe60SDimitry Andric ForceLRSpill = true; 12622754fe60SDimitry Andric } 12632754fe60SDimitry Andric } 12642754fe60SDimitry Andric 12652754fe60SDimitry Andric // If any of the stack slot references may be out of range of an immediate 12662754fe60SDimitry Andric // offset, make sure a register (or a spill slot) is available for the 12672754fe60SDimitry Andric // register scavenger. Note that if we're indexing off the frame pointer, the 12682754fe60SDimitry Andric // effective stack size is 4 bytes larger since the FP points to the stack 12692754fe60SDimitry Andric // slot of the previous FP. Also, if we have variable sized objects in the 12702754fe60SDimitry Andric // function, stack slot references will often be negative, and some of 12712754fe60SDimitry Andric // our instructions are positive-offset only, so conservatively consider 12722754fe60SDimitry Andric // that case to want a spill slot (or register) as well. Similarly, if 12732754fe60SDimitry Andric // the function adjusts the stack pointer during execution and the 12742754fe60SDimitry Andric // adjustments aren't already part of our stack size estimate, our offset 12752754fe60SDimitry Andric // calculations may be off, so be conservative. 12762754fe60SDimitry Andric // FIXME: We could add logic to be more precise about negative offsets 12772754fe60SDimitry Andric // and which instructions will need a scratch register for them. Is it 12782754fe60SDimitry Andric // worth the effort and added fragility? 12792754fe60SDimitry Andric bool BigStack = 12802754fe60SDimitry Andric (RS && 1281139f7f9bSDimitry Andric (MFI->estimateStackSize(MF) + 1282139f7f9bSDimitry Andric ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= 12832754fe60SDimitry Andric estimateRSStackSizeLimit(MF, this))) 12842754fe60SDimitry Andric || MFI->hasVarSizedObjects() 12852754fe60SDimitry Andric || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); 12862754fe60SDimitry Andric 12872754fe60SDimitry Andric bool ExtraCSSpill = false; 12882754fe60SDimitry Andric if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { 12892754fe60SDimitry Andric AFI->setHasStackFrame(true); 12902754fe60SDimitry Andric 12912754fe60SDimitry Andric // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. 12922754fe60SDimitry Andric // Spill LR as well so we can fold BX_RET to the registers restore (LDM). 12932754fe60SDimitry Andric if (!LRSpilled && CS1Spilled) { 12943861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 12952754fe60SDimitry Andric NumGPRSpills++; 12962754fe60SDimitry Andric UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), 12972754fe60SDimitry Andric UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); 12982754fe60SDimitry Andric ForceLRSpill = false; 12992754fe60SDimitry Andric ExtraCSSpill = true; 13002754fe60SDimitry Andric } 13012754fe60SDimitry Andric 13022754fe60SDimitry Andric if (hasFP(MF)) { 13033861d79fSDimitry Andric MRI.setPhysRegUsed(FramePtr); 13042754fe60SDimitry Andric NumGPRSpills++; 13052754fe60SDimitry Andric } 13062754fe60SDimitry Andric 13072754fe60SDimitry Andric // If stack and double are 8-byte aligned and we are spilling an odd number 13082754fe60SDimitry Andric // of GPRs, spill one extra callee save GPR so we won't have to pad between 13092754fe60SDimitry Andric // the integer and double callee save areas. 13102754fe60SDimitry Andric unsigned TargetAlign = getStackAlignment(); 13112754fe60SDimitry Andric if (TargetAlign == 8 && (NumGPRSpills & 1)) { 13122754fe60SDimitry Andric if (CS1Spilled && !UnspilledCS1GPRs.empty()) { 13132754fe60SDimitry Andric for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { 13142754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs[i]; 13152754fe60SDimitry Andric // Don't spill high register if the function is thumb1 13162754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction() || 13172754fe60SDimitry Andric isARMLowRegister(Reg) || Reg == ARM::LR) { 13183861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 13193861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 13202754fe60SDimitry Andric ExtraCSSpill = true; 13212754fe60SDimitry Andric break; 13222754fe60SDimitry Andric } 13232754fe60SDimitry Andric } 13242754fe60SDimitry Andric } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { 13252754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.front(); 13263861d79fSDimitry Andric MRI.setPhysRegUsed(Reg); 13273861d79fSDimitry Andric if (!MRI.isReserved(Reg)) 13282754fe60SDimitry Andric ExtraCSSpill = true; 13292754fe60SDimitry Andric } 13302754fe60SDimitry Andric } 13312754fe60SDimitry Andric 13322754fe60SDimitry Andric // Estimate if we might need to scavenge a register at some point in order 13332754fe60SDimitry Andric // to materialize a stack offset. If so, either spill one additional 13342754fe60SDimitry Andric // callee-saved register or reserve a special spill slot to facilitate 13352754fe60SDimitry Andric // register scavenging. Thumb1 needs a spill slot for stack pointer 13362754fe60SDimitry Andric // adjustments also, even when the frame itself is small. 13372754fe60SDimitry Andric if (BigStack && !ExtraCSSpill) { 13382754fe60SDimitry Andric // If any non-reserved CS register isn't spilled, just spill one or two 13392754fe60SDimitry Andric // extra. That should take care of it! 13402754fe60SDimitry Andric unsigned NumExtras = TargetAlign / 4; 13412754fe60SDimitry Andric SmallVector<unsigned, 2> Extras; 13422754fe60SDimitry Andric while (NumExtras && !UnspilledCS1GPRs.empty()) { 13432754fe60SDimitry Andric unsigned Reg = UnspilledCS1GPRs.back(); 13442754fe60SDimitry Andric UnspilledCS1GPRs.pop_back(); 13453861d79fSDimitry Andric if (!MRI.isReserved(Reg) && 13462754fe60SDimitry Andric (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || 13472754fe60SDimitry Andric Reg == ARM::LR)) { 13482754fe60SDimitry Andric Extras.push_back(Reg); 13492754fe60SDimitry Andric NumExtras--; 13502754fe60SDimitry Andric } 13512754fe60SDimitry Andric } 13522754fe60SDimitry Andric // For non-Thumb1 functions, also check for hi-reg CS registers 13532754fe60SDimitry Andric if (!AFI->isThumb1OnlyFunction()) { 13542754fe60SDimitry Andric while (NumExtras && !UnspilledCS2GPRs.empty()) { 13552754fe60SDimitry Andric unsigned Reg = UnspilledCS2GPRs.back(); 13562754fe60SDimitry Andric UnspilledCS2GPRs.pop_back(); 13573861d79fSDimitry Andric if (!MRI.isReserved(Reg)) { 13582754fe60SDimitry Andric Extras.push_back(Reg); 13592754fe60SDimitry Andric NumExtras--; 13602754fe60SDimitry Andric } 13612754fe60SDimitry Andric } 13622754fe60SDimitry Andric } 13632754fe60SDimitry Andric if (Extras.size() && NumExtras == 0) { 13642754fe60SDimitry Andric for (unsigned i = 0, e = Extras.size(); i != e; ++i) { 13653861d79fSDimitry Andric MRI.setPhysRegUsed(Extras[i]); 13662754fe60SDimitry Andric } 13672754fe60SDimitry Andric } else if (!AFI->isThumb1OnlyFunction()) { 13682754fe60SDimitry Andric // note: Thumb1 functions spill to R12, not the stack. Reserve a slot 13692754fe60SDimitry Andric // closest to SP or frame pointer. 13707ae0e2c9SDimitry Andric const TargetRegisterClass *RC = &ARM::GPRRegClass; 1371139f7f9bSDimitry Andric RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), 13722754fe60SDimitry Andric RC->getAlignment(), 13732754fe60SDimitry Andric false)); 13742754fe60SDimitry Andric } 13752754fe60SDimitry Andric } 13762754fe60SDimitry Andric } 13772754fe60SDimitry Andric 13782754fe60SDimitry Andric if (ForceLRSpill) { 13793861d79fSDimitry Andric MRI.setPhysRegUsed(ARM::LR); 13802754fe60SDimitry Andric AFI->setLRIsSpilledForFarJump(true); 13812754fe60SDimitry Andric } 13822754fe60SDimitry Andric } 1383139f7f9bSDimitry Andric 1384139f7f9bSDimitry Andric 1385139f7f9bSDimitry Andric void ARMFrameLowering:: 1386139f7f9bSDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1387139f7f9bSDimitry Andric MachineBasicBlock::iterator I) const { 1388139f7f9bSDimitry Andric const ARMBaseInstrInfo &TII = 1389139f7f9bSDimitry Andric *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); 1390139f7f9bSDimitry Andric if (!hasReservedCallFrame(MF)) { 1391139f7f9bSDimitry Andric // If we have alloca, convert as follows: 1392139f7f9bSDimitry Andric // ADJCALLSTACKDOWN -> sub, sp, sp, amount 1393139f7f9bSDimitry Andric // ADJCALLSTACKUP -> add, sp, sp, amount 1394139f7f9bSDimitry Andric MachineInstr *Old = I; 1395139f7f9bSDimitry Andric DebugLoc dl = Old->getDebugLoc(); 1396139f7f9bSDimitry Andric unsigned Amount = Old->getOperand(0).getImm(); 1397139f7f9bSDimitry Andric if (Amount != 0) { 1398139f7f9bSDimitry Andric // We need to keep the stack aligned properly. To do this, we round the 1399139f7f9bSDimitry Andric // amount of space needed for the outgoing arguments up to the next 1400139f7f9bSDimitry Andric // alignment boundary. 1401139f7f9bSDimitry Andric unsigned Align = getStackAlignment(); 1402139f7f9bSDimitry Andric Amount = (Amount+Align-1)/Align*Align; 1403139f7f9bSDimitry Andric 1404139f7f9bSDimitry Andric ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); 1405139f7f9bSDimitry Andric assert(!AFI->isThumb1OnlyFunction() && 1406139f7f9bSDimitry Andric "This eliminateCallFramePseudoInstr does not support Thumb1!"); 1407139f7f9bSDimitry Andric bool isARM = !AFI->isThumbFunction(); 1408139f7f9bSDimitry Andric 1409139f7f9bSDimitry Andric // Replace the pseudo instruction with a new instruction... 1410139f7f9bSDimitry Andric unsigned Opc = Old->getOpcode(); 1411139f7f9bSDimitry Andric int PIdx = Old->findFirstPredOperandIdx(); 1412139f7f9bSDimitry Andric ARMCC::CondCodes Pred = (PIdx == -1) 1413139f7f9bSDimitry Andric ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); 1414139f7f9bSDimitry Andric if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { 1415139f7f9bSDimitry Andric // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. 1416139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(2).getReg(); 1417139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, 1418139f7f9bSDimitry Andric Pred, PredReg); 1419139f7f9bSDimitry Andric } else { 1420139f7f9bSDimitry Andric // Note: PredReg is operand 3 for ADJCALLSTACKUP. 1421139f7f9bSDimitry Andric unsigned PredReg = Old->getOperand(3).getReg(); 1422139f7f9bSDimitry Andric assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); 1423139f7f9bSDimitry Andric emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, 1424139f7f9bSDimitry Andric Pred, PredReg); 1425139f7f9bSDimitry Andric } 1426139f7f9bSDimitry Andric } 1427139f7f9bSDimitry Andric } 1428139f7f9bSDimitry Andric MBB.erase(I); 1429139f7f9bSDimitry Andric } 1430139f7f9bSDimitry Andric 1431