Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1 | //=======- ARMFrameLowering.cpp - ARM Frame Information --------*- C++ -*-====// |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the ARM implementation of TargetFrameLowering class. |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "ARMFrameLowering.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 15 | #include "ARMBaseInstrInfo.h" |
Evan Cheng | b72d2a9 | 2011-01-11 21:46:47 +0000 | [diff] [blame] | 16 | #include "ARMBaseRegisterInfo.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 17 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | ee04a6d | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/ARMAddressingModes.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/RegisterScavenging.h" |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 28 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 29 | /// pointer register. This is true if the function has variable sized allocas |
| 30 | /// or if frame pointer elimination is disabled. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 31 | bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 32 | const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 33 | |
| 34 | // Mac OS X requires FP not to be clobbered for backtracing purpose. |
| 35 | if (STI.isTargetDarwin()) |
| 36 | return true; |
| 37 | |
| 38 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 39 | // Always eliminate non-leaf frame pointers. |
| 40 | return ((DisableFramePointerElim(MF) && MFI->hasCalls()) || |
| 41 | RegInfo->needsStackRealignment(MF) || |
| 42 | MFI->hasVarSizedObjects() || |
| 43 | MFI->isFrameAddressTaken()); |
| 44 | } |
| 45 | |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 46 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 47 | /// not required, we reserve argument space for call sites in the function |
| 48 | /// immediately on entry to the current function. This eliminates the need for |
| 49 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 50 | /// included as part of the stack frame. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 51 | bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 52 | const MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 53 | unsigned CFSize = FFI->getMaxCallFrameSize(); |
| 54 | // It's not always a good idea to include the call frame as part of the |
| 55 | // stack frame. ARM (especially Thumb) has small immediate offset to |
| 56 | // address the stack frame. So a large call frame can cause poor codegen |
| 57 | // and may even makes it impossible to scavenge a register. |
| 58 | if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 |
| 59 | return false; |
| 60 | |
| 61 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 62 | } |
| 63 | |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 64 | /// canSimplifyCallFramePseudos - If there is a reserved call frame, the |
| 65 | /// call frame pseudos can be simplified. Unlike most targets, having a FP |
| 66 | /// is not sufficient here since we still may reference some objects via SP |
| 67 | /// even when FP is available in Thumb2 mode. |
| 68 | bool |
| 69 | ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 70 | return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); |
| 71 | } |
| 72 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 73 | static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { |
| 74 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 75 | if (Reg == CSRegs[i]) |
| 76 | return true; |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static bool isCSRestore(MachineInstr *MI, |
| 81 | const ARMBaseInstrInfo &TII, |
| 82 | const unsigned *CSRegs) { |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 83 | // Integer spill area is handled with "pop". |
| 84 | if (MI->getOpcode() == ARM::LDMIA_RET || |
| 85 | MI->getOpcode() == ARM::t2LDMIA_RET || |
| 86 | MI->getOpcode() == ARM::LDMIA_UPD || |
| 87 | MI->getOpcode() == ARM::t2LDMIA_UPD || |
| 88 | MI->getOpcode() == ARM::VLDMDIA_UPD) { |
| 89 | // The first two operands are predicates. The last two are |
| 90 | // imp-def and imp-use of SP. Check everything in between. |
| 91 | for (int i = 5, e = MI->getNumOperands(); i != e; ++i) |
| 92 | if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) |
| 93 | return false; |
| 94 | return true; |
| 95 | } |
Owen Anderson | 793e796 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 96 | if ((MI->getOpcode() == ARM::LDR_POST_IMM || |
| 97 | MI->getOpcode() == ARM::LDR_POST_REG || |
Jim Grosbach | 568f528 | 2010-12-10 18:41:15 +0000 | [diff] [blame] | 98 | MI->getOpcode() == ARM::t2LDR_POST) && |
| 99 | isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && |
| 100 | MI->getOperand(1).getReg() == ARM::SP) |
| 101 | return true; |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 102 | |
| 103 | return false; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void |
| 107 | emitSPUpdate(bool isARM, |
| 108 | MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, |
| 109 | DebugLoc dl, const ARMBaseInstrInfo &TII, |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 110 | int NumBytes, unsigned MIFlags = MachineInstr::NoFlags) { |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 111 | if (isARM) |
| 112 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 113 | ARMCC::AL, 0, TII, MIFlags); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 114 | else |
| 115 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes, |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 116 | ARMCC::AL, 0, TII, MIFlags); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 119 | void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 120 | MachineBasicBlock &MBB = MF.front(); |
| 121 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 122 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 123 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 124 | const ARMBaseRegisterInfo *RegInfo = |
| 125 | static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 126 | const ARMBaseInstrInfo &TII = |
| 127 | *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 128 | assert(!AFI->isThumb1OnlyFunction() && |
| 129 | "This emitPrologue does not support Thumb1!"); |
| 130 | bool isARM = !AFI->isThumbFunction(); |
| 131 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 132 | unsigned NumBytes = MFI->getStackSize(); |
| 133 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 134 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 135 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 136 | |
| 137 | // Determine the sizes of each callee-save spill areas and record which frame |
| 138 | // belongs to which callee-save spill areas. |
| 139 | unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; |
| 140 | int FramePtrSpillFI = 0; |
| 141 | |
| 142 | // Allocate the vararg register save area. This is not counted in NumBytes. |
| 143 | if (VARegSaveSize) |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 144 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize, |
| 145 | MachineInstr::FrameSetup); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 146 | |
| 147 | if (!AFI->hasStackFrame()) { |
| 148 | if (NumBytes != 0) |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 149 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, |
| 150 | MachineInstr::FrameSetup); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
| 154 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 155 | unsigned Reg = CSI[i].getReg(); |
| 156 | int FI = CSI[i].getFrameIdx(); |
| 157 | switch (Reg) { |
| 158 | case ARM::R4: |
| 159 | case ARM::R5: |
| 160 | case ARM::R6: |
| 161 | case ARM::R7: |
| 162 | case ARM::LR: |
| 163 | if (Reg == FramePtr) |
| 164 | FramePtrSpillFI = FI; |
| 165 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 166 | GPRCS1Size += 4; |
| 167 | break; |
| 168 | case ARM::R8: |
| 169 | case ARM::R9: |
| 170 | case ARM::R10: |
| 171 | case ARM::R11: |
| 172 | if (Reg == FramePtr) |
| 173 | FramePtrSpillFI = FI; |
| 174 | if (STI.isTargetDarwin()) { |
| 175 | AFI->addGPRCalleeSavedArea2Frame(FI); |
| 176 | GPRCS2Size += 4; |
| 177 | } else { |
| 178 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 179 | GPRCS1Size += 4; |
| 180 | } |
| 181 | break; |
| 182 | default: |
| 183 | AFI->addDPRCalleeSavedAreaFrame(FI); |
| 184 | DPRCSSize += 8; |
| 185 | } |
| 186 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 187 | |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 188 | // Move past area 1. |
| 189 | if (GPRCS1Size > 0) MBBI++; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 190 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 191 | // Set FP to point to the stack slot that contains the previous FP. |
| 192 | // For Darwin, FP is R7, which has now been stored in spill area 1. |
| 193 | // Otherwise, if this is not Darwin, all the callee-saved registers go |
| 194 | // into spill area 1, including the FP in R11. In either case, it is |
| 195 | // now safe to emit this assignment. |
Anton Korobeynikov | d0c3817 | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 196 | bool HasFP = hasFP(MF); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 197 | if (HasFP) { |
| 198 | unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri; |
| 199 | MachineInstrBuilder MIB = |
| 200 | BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr) |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 201 | .addFrameIndex(FramePtrSpillFI).addImm(0) |
| 202 | .setMIFlag(MachineInstr::FrameSetup); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 203 | AddDefaultCC(AddDefaultPred(MIB)); |
| 204 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 205 | |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 206 | // Move past area 2. |
| 207 | if (GPRCS2Size > 0) MBBI++; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 208 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 209 | // Determine starting offsets of spill areas. |
| 210 | unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); |
| 211 | unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; |
| 212 | unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; |
| 213 | if (HasFP) |
| 214 | AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + |
| 215 | NumBytes); |
| 216 | AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); |
| 217 | AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); |
| 218 | AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); |
| 219 | |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 220 | // Move past area 3. |
Evan Cheng | acca09b | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 221 | if (DPRCSSize > 0) { |
| 222 | MBBI++; |
| 223 | // Since vpush register list cannot have gaps, there may be multiple vpush |
Evan Cheng | 9831f2d | 2011-02-25 01:29:29 +0000 | [diff] [blame] | 224 | // instructions in the prologue. |
Evan Cheng | acca09b | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 225 | while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) |
| 226 | MBBI++; |
| 227 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 228 | |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 229 | NumBytes = DPRCSOffset; |
| 230 | if (NumBytes) { |
| 231 | // Adjust SP after all the callee-save spills. |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 232 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, |
| 233 | MachineInstr::FrameSetup); |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 234 | if (HasFP && isARM) |
| 235 | // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 |
| 236 | // Note it's not safe to do this in Thumb2 mode because it would have |
| 237 | // taken two instructions: |
| 238 | // mov sp, r7 |
| 239 | // sub sp, #24 |
| 240 | // If an interrupt is taken between the two instructions, then sp is in |
| 241 | // an inconsistent state (pointing to the middle of callee-saved area). |
| 242 | // The interrupt handler can end up clobbering the registers. |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 243 | AFI->setShouldRestoreSPFromFP(true); |
| 244 | } |
| 245 | |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 246 | if (STI.isTargetELF() && hasFP(MF)) |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 247 | MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - |
| 248 | AFI->getFramePtrSpillOffset()); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 249 | |
| 250 | AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); |
| 251 | AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); |
| 252 | AFI->setDPRCalleeSavedAreaSize(DPRCSSize); |
| 253 | |
| 254 | // If we need dynamic stack realignment, do it here. Be paranoid and make |
| 255 | // sure if we also have VLAs, we have a base pointer for frame access. |
| 256 | if (RegInfo->needsStackRealignment(MF)) { |
| 257 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 258 | assert (!AFI->isThumb1OnlyFunction()); |
| 259 | if (!AFI->isThumbFunction()) { |
| 260 | // Emit bic sp, sp, MaxAlign |
| 261 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 262 | TII.get(ARM::BICri), ARM::SP) |
| 263 | .addReg(ARM::SP, RegState::Kill) |
| 264 | .addImm(MaxAlign-1))); |
| 265 | } else { |
| 266 | // We cannot use sp as source/dest register here, thus we're emitting the |
| 267 | // following sequence: |
| 268 | // mov r4, sp |
| 269 | // bic r4, r4, MaxAlign |
| 270 | // mov sp, r4 |
| 271 | // FIXME: It will be better just to find spare register here. |
Jim Grosbach | 2a7b41b | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 272 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) |
Jim Grosbach | 63b46fa | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 273 | .addReg(ARM::SP, RegState::Kill)); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 274 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 275 | TII.get(ARM::t2BICri), ARM::R4) |
| 276 | .addReg(ARM::R4, RegState::Kill) |
| 277 | .addImm(MaxAlign-1))); |
Jim Grosbach | 2a7b41b | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 278 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) |
Jim Grosbach | 63b46fa | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 279 | .addReg(ARM::R4, RegState::Kill)); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | AFI->setShouldRestoreSPFromFP(true); |
| 283 | } |
| 284 | |
| 285 | // If we need a base pointer, set it up here. It's whatever the value |
| 286 | // of the stack pointer is at this point. Any variable size objects |
| 287 | // will be allocated after this, so we can still use the base pointer |
| 288 | // to reference locals. |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 289 | // FIXME: Clarify FrameSetup flags here. |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 290 | if (RegInfo->hasBasePointer(MF)) { |
| 291 | if (isARM) |
| 292 | BuildMI(MBB, MBBI, dl, |
| 293 | TII.get(ARM::MOVr), RegInfo->getBaseRegister()) |
| 294 | .addReg(ARM::SP) |
| 295 | .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 296 | else |
Jim Grosbach | 2a7b41b | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 297 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | 63b46fa | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 298 | RegInfo->getBaseRegister()) |
| 299 | .addReg(ARM::SP)); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // If the frame has variable sized objects then the epilogue must restore |
Eric Christopher | 4dd312f | 2011-01-10 23:10:59 +0000 | [diff] [blame] | 303 | // the sp from fp. We can assume there's an FP here since hasFP already |
| 304 | // checks for hasVarSizedObjects. |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 305 | if (MFI->hasVarSizedObjects()) |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 306 | AFI->setShouldRestoreSPFromFP(true); |
| 307 | } |
| 308 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 309 | void ARMFrameLowering::emitEpilogue(MachineFunction &MF, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 310 | MachineBasicBlock &MBB) const { |
Jakob Stoklund Olesen | 4f28c1c | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 311 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 312 | assert(MBBI->getDesc().isReturn() && |
| 313 | "Can only insert epilog into returning blocks"); |
| 314 | unsigned RetOpcode = MBBI->getOpcode(); |
| 315 | DebugLoc dl = MBBI->getDebugLoc(); |
| 316 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 317 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 318 | const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 319 | const ARMBaseInstrInfo &TII = |
| 320 | *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 321 | assert(!AFI->isThumb1OnlyFunction() && |
| 322 | "This emitEpilogue does not support Thumb1!"); |
| 323 | bool isARM = !AFI->isThumbFunction(); |
| 324 | |
| 325 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 326 | int NumBytes = (int)MFI->getStackSize(); |
| 327 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 328 | |
| 329 | if (!AFI->hasStackFrame()) { |
| 330 | if (NumBytes != 0) |
| 331 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
| 332 | } else { |
| 333 | // Unwind MBBI to point to first LDR / VLDRD. |
| 334 | const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); |
| 335 | if (MBBI != MBB.begin()) { |
| 336 | do |
| 337 | --MBBI; |
| 338 | while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); |
| 339 | if (!isCSRestore(MBBI, TII, CSRegs)) |
| 340 | ++MBBI; |
| 341 | } |
| 342 | |
| 343 | // Move SP to start of FP callee save spill area. |
| 344 | NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + |
| 345 | AFI->getGPRCalleeSavedArea2Size() + |
| 346 | AFI->getDPRCalleeSavedAreaSize()); |
| 347 | |
| 348 | // Reset SP based on frame pointer only if the stack frame extends beyond |
| 349 | // frame pointer stack slot or target is ELF and the function has FP. |
| 350 | if (AFI->shouldRestoreSPFromFP()) { |
| 351 | NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; |
| 352 | if (NumBytes) { |
| 353 | if (isARM) |
| 354 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, |
| 355 | ARMCC::AL, 0, TII); |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 356 | else { |
| 357 | // It's not possible to restore SP from FP in a single instruction. |
| 358 | // For Darwin, this looks like: |
| 359 | // mov sp, r7 |
| 360 | // sub sp, #24 |
| 361 | // This is bad, if an interrupt is taken after the mov, sp is in an |
| 362 | // inconsistent state. |
| 363 | // Use the first callee-saved register as a scratch register. |
| 364 | assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && |
| 365 | "No scratch register to restore SP from FP!"); |
| 366 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 367 | ARMCC::AL, 0, TII); |
Jim Grosbach | 2a7b41b | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 368 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | 63b46fa | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 369 | ARM::SP) |
| 370 | .addReg(ARM::R4)); |
Evan Cheng | ab5c703 | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 371 | } |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 372 | } else { |
| 373 | // Thumb2 or ARM. |
| 374 | if (isARM) |
| 375 | BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) |
| 376 | .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 377 | else |
Jim Grosbach | 2a7b41b | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 378 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | 63b46fa | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 379 | ARM::SP) |
| 380 | .addReg(FramePtr)); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 381 | } |
| 382 | } else if (NumBytes) |
| 383 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
| 384 | |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 385 | // Increment past our save areas. |
Evan Cheng | acca09b | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 386 | if (AFI->getDPRCalleeSavedAreaSize()) { |
| 387 | MBBI++; |
| 388 | // Since vpop register list cannot have gaps, there may be multiple vpop |
| 389 | // instructions in the epilogue. |
| 390 | while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) |
| 391 | MBBI++; |
| 392 | } |
Eric Christopher | 8b3ca62 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 393 | if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; |
| 394 | if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND || |
| 398 | RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) { |
| 399 | // Tail call return: adjust the stack pointer and jump to callee. |
Jakob Stoklund Olesen | 4f28c1c | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 400 | MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 401 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 402 | |
| 403 | // Jump to label or value in register. |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 404 | if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND) { |
| 405 | unsigned TCOpcode = (RetOpcode == ARM::TCRETURNdi) |
Jim Grosbach | 5edf24e | 2011-03-15 00:30:40 +0000 | [diff] [blame] | 406 | ? (STI.isThumb() ? ARM::tTAILJMPd : ARM::TAILJMPd) |
| 407 | : (STI.isThumb() ? ARM::tTAILJMPdND : ARM::TAILJMPdND); |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 408 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); |
| 409 | if (JumpTarget.isGlobal()) |
| 410 | MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), |
| 411 | JumpTarget.getTargetFlags()); |
| 412 | else { |
| 413 | assert(JumpTarget.isSymbol()); |
| 414 | MIB.addExternalSymbol(JumpTarget.getSymbolName(), |
| 415 | JumpTarget.getTargetFlags()); |
| 416 | } |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 417 | } else if (RetOpcode == ARM::TCRETURNri) { |
Jim Grosbach | 5edf24e | 2011-03-15 00:30:40 +0000 | [diff] [blame] | 418 | BuildMI(MBB, MBBI, dl, |
| 419 | TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 420 | addReg(JumpTarget.getReg(), RegState::Kill); |
| 421 | } else if (RetOpcode == ARM::TCRETURNriND) { |
Jim Grosbach | 5edf24e | 2011-03-15 00:30:40 +0000 | [diff] [blame] | 422 | BuildMI(MBB, MBBI, dl, |
| 423 | TII.get(STI.isThumb() ? ARM::tTAILJMPrND : ARM::TAILJMPrND)). |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 424 | addReg(JumpTarget.getReg(), RegState::Kill); |
| 425 | } |
| 426 | |
| 427 | MachineInstr *NewMI = prior(MBBI); |
| 428 | for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) |
| 429 | NewMI->addOperand(MBBI->getOperand(i)); |
| 430 | |
| 431 | // Delete the pseudo instruction TCRETURN. |
| 432 | MBB.erase(MBBI); |
Cameron Zwarich | cd4e0b5 | 2011-06-17 02:16:43 +0000 | [diff] [blame] | 433 | MBBI = NewMI; |
Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (VARegSaveSize) |
| 437 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize); |
| 438 | } |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 439 | |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 440 | /// getFrameIndexReference - Provide a base+offset reference to an FI slot for |
| 441 | /// debug info. It's the same as what we use for resolving the code-gen |
| 442 | /// references for now. FIXME: This can go wrong when references are |
| 443 | /// SP-relative and simple call frames aren't used. |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 444 | int |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 445 | ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 446 | unsigned &FrameReg) const { |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 447 | return ResolveFrameIndexReference(MF, FI, FrameReg, 0); |
| 448 | } |
| 449 | |
| 450 | int |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 451 | ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, |
Evan Cheng | db6cbe1 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 452 | int FI, unsigned &FrameReg, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 453 | int SPAdj) const { |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 454 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 455 | const ARMBaseRegisterInfo *RegInfo = |
| 456 | static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 457 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 458 | int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); |
| 459 | int FPOffset = Offset - AFI->getFramePtrSpillOffset(); |
| 460 | bool isFixed = MFI->isFixedObjectIndex(FI); |
| 461 | |
| 462 | FrameReg = ARM::SP; |
| 463 | Offset += SPAdj; |
| 464 | if (AFI->isGPRCalleeSavedArea1Frame(FI)) |
| 465 | return Offset - AFI->getGPRCalleeSavedArea1Offset(); |
| 466 | else if (AFI->isGPRCalleeSavedArea2Frame(FI)) |
| 467 | return Offset - AFI->getGPRCalleeSavedArea2Offset(); |
| 468 | else if (AFI->isDPRCalleeSavedAreaFrame(FI)) |
| 469 | return Offset - AFI->getDPRCalleeSavedAreaOffset(); |
| 470 | |
| 471 | // When dynamically realigning the stack, use the frame pointer for |
| 472 | // parameters, and the stack/base pointer for locals. |
| 473 | if (RegInfo->needsStackRealignment(MF)) { |
| 474 | assert (hasFP(MF) && "dynamic stack realignment without a FP!"); |
| 475 | if (isFixed) { |
| 476 | FrameReg = RegInfo->getFrameRegister(MF); |
| 477 | Offset = FPOffset; |
| 478 | } else if (MFI->hasVarSizedObjects()) { |
| 479 | assert(RegInfo->hasBasePointer(MF) && |
| 480 | "VLAs and dynamic stack alignment, but missing base pointer!"); |
| 481 | FrameReg = RegInfo->getBaseRegister(); |
| 482 | } |
| 483 | return Offset; |
| 484 | } |
| 485 | |
| 486 | // If there is a frame pointer, use it when we can. |
| 487 | if (hasFP(MF) && AFI->hasStackFrame()) { |
| 488 | // Use frame pointer to reference fixed objects. Use it for locals if |
| 489 | // there are VLAs (and thus the SP isn't reliable as a base). |
Jim Grosbach | 2a4f098 | 2010-12-09 16:14:46 +0000 | [diff] [blame] | 490 | if (isFixed || (MFI->hasVarSizedObjects() && |
| 491 | !RegInfo->hasBasePointer(MF))) { |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 492 | FrameReg = RegInfo->getFrameRegister(MF); |
| 493 | return FPOffset; |
| 494 | } else if (MFI->hasVarSizedObjects()) { |
| 495 | assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 496 | if (AFI->isThumb2Function()) { |
Evan Cheng | db6cbe1 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 497 | // Try to use the frame pointer if we can, else use the base pointer |
| 498 | // since it's available. This is handy for the emergency spill slot, in |
| 499 | // particular. |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 500 | if (FPOffset >= -255 && FPOffset < 0) { |
| 501 | FrameReg = RegInfo->getFrameRegister(MF); |
| 502 | return FPOffset; |
| 503 | } |
Evan Cheng | db6cbe1 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 504 | } |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 505 | } else if (AFI->isThumb2Function()) { |
Evan Cheng | db6cbe1 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 506 | // Use add <rd>, sp, #<imm8> |
| 507 | // ldr <rd>, [sp, #<imm8>] |
| 508 | // if at all possible to save space. |
| 509 | if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) |
| 510 | return Offset; |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 511 | // In Thumb2 mode, the negative offset is very limited. Try to avoid |
Evan Cheng | db6cbe1 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 512 | // out of range references. ldr <rt>,[<rn>, #-<imm8>] |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 513 | if (FPOffset >= -255 && FPOffset < 0) { |
| 514 | FrameReg = RegInfo->getFrameRegister(MF); |
| 515 | return FPOffset; |
| 516 | } |
| 517 | } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { |
| 518 | // Otherwise, use SP or FP, whichever is closer to the stack slot. |
| 519 | FrameReg = RegInfo->getFrameRegister(MF); |
| 520 | return FPOffset; |
| 521 | } |
| 522 | } |
| 523 | // Use the base pointer if we have one. |
| 524 | if (RegInfo->hasBasePointer(MF)) |
| 525 | FrameReg = RegInfo->getBaseRegister(); |
| 526 | return Offset; |
| 527 | } |
| 528 | |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 529 | int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, |
| 530 | int FI) const { |
Anton Korobeynikov | 82f5874 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 531 | unsigned FrameReg; |
| 532 | return getFrameIndexReference(MF, FI, FrameReg); |
| 533 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 534 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 535 | void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 536 | MachineBasicBlock::iterator MI, |
| 537 | const std::vector<CalleeSavedInfo> &CSI, |
| 538 | unsigned StmOpc, unsigned StrOpc, |
| 539 | bool NoGap, |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 540 | bool(*Func)(unsigned, bool), |
| 541 | unsigned MIFlags) const { |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 542 | MachineFunction &MF = *MBB.getParent(); |
| 543 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
| 544 | |
| 545 | DebugLoc DL; |
| 546 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 547 | |
Evan Cheng | 9801b5c | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 548 | SmallVector<std::pair<unsigned,bool>, 4> Regs; |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 549 | unsigned i = CSI.size(); |
| 550 | while (i != 0) { |
| 551 | unsigned LastReg = 0; |
| 552 | for (; i != 0; --i) { |
| 553 | unsigned Reg = CSI[i-1].getReg(); |
| 554 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 555 | |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 556 | // Add the callee-saved register as live-in unless it's LR and |
Jim Grosbach | 2a4f098 | 2010-12-09 16:14:46 +0000 | [diff] [blame] | 557 | // @llvm.returnaddress is called. If LR is returned for |
| 558 | // @llvm.returnaddress then it's already added to the function and |
| 559 | // entry block live-in sets. |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 560 | bool isKill = true; |
| 561 | if (Reg == ARM::LR) { |
| 562 | if (MF.getFrameInfo()->isReturnAddressTaken() && |
| 563 | MF.getRegInfo().isLiveIn(Reg)) |
| 564 | isKill = false; |
| 565 | } |
| 566 | |
| 567 | if (isKill) |
| 568 | MBB.addLiveIn(Reg); |
| 569 | |
Eric Christopher | 1a48c03 | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 570 | // If NoGap is true, push consecutive registers and then leave the rest |
Evan Cheng | 275bf63 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 571 | // for other instructions. e.g. |
Eric Christopher | 1a48c03 | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 572 | // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} |
Evan Cheng | 275bf63 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 573 | if (NoGap && LastReg && LastReg != Reg-1) |
| 574 | break; |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 575 | LastReg = Reg; |
| 576 | Regs.push_back(std::make_pair(Reg, isKill)); |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 579 | if (Regs.empty()) |
| 580 | continue; |
| 581 | if (Regs.size() > 1 || StrOpc== 0) { |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 582 | MachineInstrBuilder MIB = |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 583 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 584 | .addReg(ARM::SP).setMIFlags(MIFlags)); |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 585 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 586 | MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 587 | } else if (Regs.size() == 1) { |
| 588 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), |
| 589 | ARM::SP) |
| 590 | .addReg(Regs[0].first, getKillRegState(Regs[0].second)) |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 591 | .addReg(ARM::SP).setMIFlags(MIFlags); |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 592 | // ARM mode needs an extra reg0 here due to addrmode2. Will go away once |
| 593 | // that refactoring is complete (eventually). |
Owen Anderson | 793e796 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 594 | if (StrOpc == ARM::STR_PRE_REG || StrOpc == ARM::STR_PRE_IMM) { |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 595 | MIB.addReg(0); |
| 596 | MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::sub, 4, ARM_AM::no_shift)); |
| 597 | } else |
| 598 | MIB.addImm(-4); |
| 599 | AddDefaultPred(MIB); |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 600 | } |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 601 | Regs.clear(); |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 602 | } |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 603 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 604 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 605 | void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 606 | MachineBasicBlock::iterator MI, |
| 607 | const std::vector<CalleeSavedInfo> &CSI, |
| 608 | unsigned LdmOpc, unsigned LdrOpc, |
| 609 | bool isVarArg, bool NoGap, |
| 610 | bool(*Func)(unsigned, bool)) const { |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 611 | MachineFunction &MF = *MBB.getParent(); |
| 612 | const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); |
| 613 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 614 | DebugLoc DL = MI->getDebugLoc(); |
Evan Cheng | 7cfa656 | 2011-01-25 01:28:33 +0000 | [diff] [blame] | 615 | unsigned RetOpcode = MI->getOpcode(); |
| 616 | bool isTailCall = (RetOpcode == ARM::TCRETURNdi || |
| 617 | RetOpcode == ARM::TCRETURNdiND || |
| 618 | RetOpcode == ARM::TCRETURNri || |
| 619 | RetOpcode == ARM::TCRETURNriND); |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 620 | |
| 621 | SmallVector<unsigned, 4> Regs; |
| 622 | unsigned i = CSI.size(); |
| 623 | while (i != 0) { |
| 624 | unsigned LastReg = 0; |
| 625 | bool DeleteRet = false; |
| 626 | for (; i != 0; --i) { |
| 627 | unsigned Reg = CSI[i-1].getReg(); |
| 628 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
| 629 | |
Evan Cheng | 7cfa656 | 2011-01-25 01:28:33 +0000 | [diff] [blame] | 630 | if (Reg == ARM::LR && !isTailCall && !isVarArg && STI.hasV5TOps()) { |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 631 | Reg = ARM::PC; |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 632 | LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 633 | // Fold the return instruction into the LDM. |
| 634 | DeleteRet = true; |
| 635 | } |
| 636 | |
Evan Cheng | 275bf63 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 637 | // If NoGap is true, pop consecutive registers and then leave the rest |
| 638 | // for other instructions. e.g. |
| 639 | // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} |
| 640 | if (NoGap && LastReg && LastReg != Reg-1) |
| 641 | break; |
| 642 | |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 643 | LastReg = Reg; |
| 644 | Regs.push_back(Reg); |
| 645 | } |
| 646 | |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 647 | if (Regs.empty()) |
| 648 | continue; |
| 649 | if (Regs.size() > 1 || LdrOpc == 0) { |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 650 | MachineInstrBuilder MIB = |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 651 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 652 | .addReg(ARM::SP)); |
| 653 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 654 | MIB.addReg(Regs[i], getDefRegState(true)); |
| 655 | if (DeleteRet) |
| 656 | MI->eraseFromParent(); |
| 657 | MI = MIB; |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 658 | } else if (Regs.size() == 1) { |
| 659 | // If we adjusted the reg to PC from LR above, switch it back here. We |
| 660 | // only do that for LDM. |
| 661 | if (Regs[0] == ARM::PC) |
| 662 | Regs[0] = ARM::LR; |
| 663 | MachineInstrBuilder MIB = |
| 664 | BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) |
| 665 | .addReg(ARM::SP, RegState::Define) |
| 666 | .addReg(ARM::SP); |
| 667 | // ARM mode needs an extra reg0 here due to addrmode2. Will go away once |
| 668 | // that refactoring is complete (eventually). |
Owen Anderson | 793e796 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 669 | if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 670 | MIB.addReg(0); |
| 671 | MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); |
| 672 | } else |
| 673 | MIB.addImm(4); |
| 674 | AddDefaultPred(MIB); |
Evan Cheng | 06d65f5 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 675 | } |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 676 | Regs.clear(); |
Evan Cheng | 9801b5c | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 677 | } |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 680 | bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 681 | MachineBasicBlock::iterator MI, |
| 682 | const std::vector<CalleeSavedInfo> &CSI, |
| 683 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 684 | if (CSI.empty()) |
| 685 | return false; |
| 686 | |
| 687 | MachineFunction &MF = *MBB.getParent(); |
| 688 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 689 | |
| 690 | unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; |
Owen Anderson | 793e796 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 691 | unsigned PushOneOpc = AFI->isThumbFunction() ? ARM::t2STR_PRE : ARM::STR_PRE_IMM; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 692 | unsigned FltOpc = ARM::VSTMDDB_UPD; |
Anton Korobeynikov | 57caad7 | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 693 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, |
| 694 | MachineInstr::FrameSetup); |
| 695 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, |
| 696 | MachineInstr::FrameSetup); |
| 697 | emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, |
| 698 | MachineInstr::FrameSetup); |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 699 | |
| 700 | return true; |
| 701 | } |
| 702 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 703 | bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 704 | MachineBasicBlock::iterator MI, |
| 705 | const std::vector<CalleeSavedInfo> &CSI, |
| 706 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 707 | if (CSI.empty()) |
| 708 | return false; |
| 709 | |
| 710 | MachineFunction &MF = *MBB.getParent(); |
| 711 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 712 | bool isVarArg = AFI->getVarArgsRegSaveSize() > 0; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 713 | |
| 714 | unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; |
Owen Anderson | 793e796 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 715 | unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST : ARM::LDR_POST_IMM; |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 716 | unsigned FltOpc = ARM::VLDMDIA_UPD; |
Bob Wilson | 28f1015 | 2011-01-06 19:24:36 +0000 | [diff] [blame] | 717 | emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register); |
Jim Grosbach | c6f9261 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 718 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
| 719 | &isARMArea2Register); |
| 720 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
| 721 | &isARMArea1Register); |
Anton Korobeynikov | cd775ce | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 722 | |
| 723 | return true; |
| 724 | } |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 725 | |
| 726 | // FIXME: Make generic? |
| 727 | static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, |
| 728 | const ARMBaseInstrInfo &TII) { |
| 729 | unsigned FnSize = 0; |
| 730 | for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); |
| 731 | MBBI != E; ++MBBI) { |
| 732 | const MachineBasicBlock &MBB = *MBBI; |
| 733 | for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); |
| 734 | I != E; ++I) |
| 735 | FnSize += TII.GetInstSizeInBytes(I); |
| 736 | } |
| 737 | return FnSize; |
| 738 | } |
| 739 | |
| 740 | /// estimateStackSize - Estimate and return the size of the frame. |
| 741 | /// FIXME: Make generic? |
| 742 | static unsigned estimateStackSize(MachineFunction &MF) { |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 743 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 744 | const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering(); |
| 745 | const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo(); |
| 746 | unsigned MaxAlign = MFI->getMaxAlignment(); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 747 | int Offset = 0; |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 748 | |
| 749 | // This code is very, very similar to PEI::calculateFrameObjectOffsets(). |
| 750 | // It really should be refactored to share code. Until then, changes |
| 751 | // should keep in mind that there's tight coupling between the two. |
| 752 | |
| 753 | for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) { |
| 754 | int FixedOff = -MFI->getObjectOffset(i); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 755 | if (FixedOff > Offset) Offset = FixedOff; |
| 756 | } |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 757 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { |
| 758 | if (MFI->isDeadObjectIndex(i)) |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 759 | continue; |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 760 | Offset += MFI->getObjectSize(i); |
| 761 | unsigned Align = MFI->getObjectAlignment(i); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 762 | // Adjust to alignment boundary |
| 763 | Offset = (Offset+Align-1)/Align*Align; |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 764 | |
| 765 | MaxAlign = std::max(Align, MaxAlign); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 766 | } |
Jim Grosbach | bc20e4f | 2011-07-05 16:05:50 +0000 | [diff] [blame] | 767 | |
| 768 | if (MFI->adjustsStack() && TFI->hasReservedCallFrame(MF)) |
| 769 | Offset += MFI->getMaxCallFrameSize(); |
| 770 | |
| 771 | // Round up the size to a multiple of the alignment. If the function has |
| 772 | // any calls or alloca's, align to the target's StackAlignment value to |
| 773 | // ensure that the callee's frame or the alloca data is suitably aligned; |
| 774 | // otherwise, for leaf functions, align to the TransientStackAlignment |
| 775 | // value. |
| 776 | unsigned StackAlign; |
| 777 | if (MFI->adjustsStack() || MFI->hasVarSizedObjects() || |
| 778 | (RegInfo->needsStackRealignment(MF) && MFI->getObjectIndexEnd() != 0)) |
| 779 | StackAlign = TFI->getStackAlignment(); |
| 780 | else |
| 781 | StackAlign = TFI->getTransientStackAlignment(); |
| 782 | |
| 783 | // If the frame pointer is eliminated, all frame offsets will be relative to |
| 784 | // SP not FP. Align to MaxAlign so this works. |
| 785 | StackAlign = std::max(StackAlign, MaxAlign); |
| 786 | unsigned AlignMask = StackAlign - 1; |
| 787 | Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); |
| 788 | |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 789 | return (unsigned)Offset; |
| 790 | } |
| 791 | |
| 792 | /// estimateRSStackSizeLimit - Look at each instruction that references stack |
| 793 | /// frames and return the stack size limit beyond which some of these |
| 794 | /// instructions will require a scratch register during their expansion later. |
| 795 | // FIXME: Move to TII? |
| 796 | static unsigned estimateRSStackSizeLimit(MachineFunction &MF, |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 797 | const TargetFrameLowering *TFI) { |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 798 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 799 | unsigned Limit = (1 << 12) - 1; |
| 800 | for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) { |
| 801 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 802 | I != E; ++I) { |
| 803 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 804 | if (!I->getOperand(i).isFI()) continue; |
| 805 | |
| 806 | // When using ADDri to get the address of a stack object, 255 is the |
| 807 | // largest offset guaranteed to fit in the immediate offset. |
| 808 | if (I->getOpcode() == ARM::ADDri) { |
| 809 | Limit = std::min(Limit, (1U << 8) - 1); |
| 810 | break; |
| 811 | } |
| 812 | |
| 813 | // Otherwise check the addressing mode. |
| 814 | switch (I->getDesc().TSFlags & ARMII::AddrModeMask) { |
| 815 | case ARMII::AddrMode3: |
| 816 | case ARMII::AddrModeT2_i8: |
| 817 | Limit = std::min(Limit, (1U << 8) - 1); |
| 818 | break; |
| 819 | case ARMII::AddrMode5: |
| 820 | case ARMII::AddrModeT2_i8s4: |
| 821 | Limit = std::min(Limit, ((1U << 8) - 1) * 4); |
| 822 | break; |
| 823 | case ARMII::AddrModeT2_i12: |
| 824 | // i12 supports only positive offset so these will be converted to |
| 825 | // i8 opcodes. See llvm::rewriteT2FrameIndex. |
| 826 | if (TFI->hasFP(MF) && AFI->hasStackFrame()) |
| 827 | Limit = std::min(Limit, (1U << 8) - 1); |
| 828 | break; |
| 829 | case ARMII::AddrMode4: |
| 830 | case ARMII::AddrMode6: |
| 831 | // Addressing modes 4 & 6 (load/store) instructions can't encode an |
| 832 | // immediate offset for stack references. |
| 833 | return 0; |
| 834 | default: |
| 835 | break; |
| 836 | } |
| 837 | break; // At most one FI per instruction |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return Limit; |
| 843 | } |
| 844 | |
| 845 | void |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 846 | ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
Bob Wilson | 4225785 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 847 | RegScavenger *RS) const { |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 848 | // This tells PEI to spill the FP as if it is any other callee-save register |
| 849 | // to take advantage the eliminateFrameIndex machinery. This also ensures it |
| 850 | // is spilled in the order specified by getCalleeSavedRegs() to make it easier |
| 851 | // to combine multiple loads / stores. |
| 852 | bool CanEliminateFrame = true; |
| 853 | bool CS1Spilled = false; |
| 854 | bool LRSpilled = false; |
| 855 | unsigned NumGPRSpills = 0; |
| 856 | SmallVector<unsigned, 4> UnspilledCS1GPRs; |
| 857 | SmallVector<unsigned, 4> UnspilledCS2GPRs; |
| 858 | const ARMBaseRegisterInfo *RegInfo = |
| 859 | static_cast<const ARMBaseRegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 860 | const ARMBaseInstrInfo &TII = |
| 861 | *static_cast<const ARMBaseInstrInfo*>(MF.getTarget().getInstrInfo()); |
| 862 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 863 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 864 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 865 | |
| 866 | // Spill R4 if Thumb2 function requires stack realignment - it will be used as |
| 867 | // scratch register. Also spill R4 if Thumb2 function has varsized objects, |
Evan Cheng | df55fea | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 868 | // since it's not always possible to restore sp from fp in a single |
| 869 | // instruction. |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 870 | // FIXME: It will be better just to find spare register here. |
| 871 | if (AFI->isThumb2Function() && |
| 872 | (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) |
| 873 | MF.getRegInfo().setPhysRegUsed(ARM::R4); |
| 874 | |
Evan Cheng | df55fea | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 875 | if (AFI->isThumb1OnlyFunction()) { |
| 876 | // Spill LR if Thumb1 function uses variable length argument lists. |
| 877 | if (AFI->getVarArgsRegSaveSize() > 0) |
| 878 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
| 879 | |
Jim Grosbach | 7980f61 | 2011-06-13 21:18:25 +0000 | [diff] [blame] | 880 | // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know |
| 881 | // for sure what the stack size will be, but for this, an estimate is good |
| 882 | // enough. If there anything changes it, it'll be a spill, which implies |
| 883 | // we've used all the registers and so R4 is already used, so not marking |
| 884 | // it here will be OK. |
Evan Cheng | df55fea | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 885 | // FIXME: It will be better just to find spare register here. |
Jim Grosbach | 7980f61 | 2011-06-13 21:18:25 +0000 | [diff] [blame] | 886 | unsigned StackSize = estimateStackSize(MF); |
| 887 | if (MFI->hasVarSizedObjects() || StackSize > 508) |
Evan Cheng | df55fea | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 888 | MF.getRegInfo().setPhysRegUsed(ARM::R4); |
| 889 | } |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 890 | |
| 891 | // Spill the BasePtr if it's used. |
| 892 | if (RegInfo->hasBasePointer(MF)) |
| 893 | MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister()); |
| 894 | |
| 895 | // Don't spill FP if the frame can be eliminated. This is determined |
| 896 | // by scanning the callee-save registers to see if any is used. |
| 897 | const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); |
| 898 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 899 | unsigned Reg = CSRegs[i]; |
| 900 | bool Spilled = false; |
| 901 | if (MF.getRegInfo().isPhysRegUsed(Reg)) { |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 902 | Spilled = true; |
| 903 | CanEliminateFrame = false; |
| 904 | } else { |
| 905 | // Check alias registers too. |
| 906 | for (const unsigned *Aliases = |
| 907 | RegInfo->getAliasSet(Reg); *Aliases; ++Aliases) { |
| 908 | if (MF.getRegInfo().isPhysRegUsed(*Aliases)) { |
| 909 | Spilled = true; |
| 910 | CanEliminateFrame = false; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | if (!ARM::GPRRegisterClass->contains(Reg)) |
| 916 | continue; |
| 917 | |
| 918 | if (Spilled) { |
| 919 | NumGPRSpills++; |
| 920 | |
| 921 | if (!STI.isTargetDarwin()) { |
| 922 | if (Reg == ARM::LR) |
| 923 | LRSpilled = true; |
| 924 | CS1Spilled = true; |
| 925 | continue; |
| 926 | } |
| 927 | |
| 928 | // Keep track if LR and any of R4, R5, R6, and R7 is spilled. |
| 929 | switch (Reg) { |
| 930 | case ARM::LR: |
| 931 | LRSpilled = true; |
| 932 | // Fallthrough |
| 933 | case ARM::R4: case ARM::R5: |
| 934 | case ARM::R6: case ARM::R7: |
| 935 | CS1Spilled = true; |
| 936 | break; |
| 937 | default: |
| 938 | break; |
| 939 | } |
| 940 | } else { |
| 941 | if (!STI.isTargetDarwin()) { |
| 942 | UnspilledCS1GPRs.push_back(Reg); |
| 943 | continue; |
| 944 | } |
| 945 | |
| 946 | switch (Reg) { |
| 947 | case ARM::R4: case ARM::R5: |
| 948 | case ARM::R6: case ARM::R7: |
| 949 | case ARM::LR: |
| 950 | UnspilledCS1GPRs.push_back(Reg); |
| 951 | break; |
| 952 | default: |
| 953 | UnspilledCS2GPRs.push_back(Reg); |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | bool ForceLRSpill = false; |
| 960 | if (!LRSpilled && AFI->isThumb1OnlyFunction()) { |
| 961 | unsigned FnSize = GetFunctionSizeInBytes(MF, TII); |
| 962 | // Force LR to be spilled if the Thumb function size is > 2048. This enables |
| 963 | // use of BL to implement far jump. If it turns out that it's not needed |
| 964 | // then the branch fix up path will undo it. |
| 965 | if (FnSize >= (1 << 11)) { |
| 966 | CanEliminateFrame = false; |
| 967 | ForceLRSpill = true; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // If any of the stack slot references may be out of range of an immediate |
| 972 | // offset, make sure a register (or a spill slot) is available for the |
| 973 | // register scavenger. Note that if we're indexing off the frame pointer, the |
| 974 | // effective stack size is 4 bytes larger since the FP points to the stack |
| 975 | // slot of the previous FP. Also, if we have variable sized objects in the |
| 976 | // function, stack slot references will often be negative, and some of |
| 977 | // our instructions are positive-offset only, so conservatively consider |
| 978 | // that case to want a spill slot (or register) as well. Similarly, if |
| 979 | // the function adjusts the stack pointer during execution and the |
| 980 | // adjustments aren't already part of our stack size estimate, our offset |
| 981 | // calculations may be off, so be conservative. |
| 982 | // FIXME: We could add logic to be more precise about negative offsets |
| 983 | // and which instructions will need a scratch register for them. Is it |
| 984 | // worth the effort and added fragility? |
| 985 | bool BigStack = |
| 986 | (RS && |
| 987 | (estimateStackSize(MF) + ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= |
| 988 | estimateRSStackSizeLimit(MF, this))) |
| 989 | || MFI->hasVarSizedObjects() |
| 990 | || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); |
| 991 | |
| 992 | bool ExtraCSSpill = false; |
| 993 | if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { |
| 994 | AFI->setHasStackFrame(true); |
| 995 | |
| 996 | // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. |
| 997 | // Spill LR as well so we can fold BX_RET to the registers restore (LDM). |
| 998 | if (!LRSpilled && CS1Spilled) { |
| 999 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1000 | NumGPRSpills++; |
| 1001 | UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(), |
| 1002 | UnspilledCS1GPRs.end(), (unsigned)ARM::LR)); |
| 1003 | ForceLRSpill = false; |
| 1004 | ExtraCSSpill = true; |
| 1005 | } |
| 1006 | |
| 1007 | if (hasFP(MF)) { |
| 1008 | MF.getRegInfo().setPhysRegUsed(FramePtr); |
| 1009 | NumGPRSpills++; |
| 1010 | } |
| 1011 | |
| 1012 | // If stack and double are 8-byte aligned and we are spilling an odd number |
| 1013 | // of GPRs, spill one extra callee save GPR so we won't have to pad between |
| 1014 | // the integer and double callee save areas. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1015 | unsigned TargetAlign = getStackAlignment(); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1016 | if (TargetAlign == 8 && (NumGPRSpills & 1)) { |
| 1017 | if (CS1Spilled && !UnspilledCS1GPRs.empty()) { |
| 1018 | for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { |
| 1019 | unsigned Reg = UnspilledCS1GPRs[i]; |
| 1020 | // Don't spill high register if the function is thumb1 |
| 1021 | if (!AFI->isThumb1OnlyFunction() || |
| 1022 | isARMLowRegister(Reg) || Reg == ARM::LR) { |
| 1023 | MF.getRegInfo().setPhysRegUsed(Reg); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1024 | if (!RegInfo->isReservedReg(MF, Reg)) |
| 1025 | ExtraCSSpill = true; |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { |
| 1030 | unsigned Reg = UnspilledCS2GPRs.front(); |
| 1031 | MF.getRegInfo().setPhysRegUsed(Reg); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1032 | if (!RegInfo->isReservedReg(MF, Reg)) |
| 1033 | ExtraCSSpill = true; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | // Estimate if we might need to scavenge a register at some point in order |
| 1038 | // to materialize a stack offset. If so, either spill one additional |
| 1039 | // callee-saved register or reserve a special spill slot to facilitate |
| 1040 | // register scavenging. Thumb1 needs a spill slot for stack pointer |
| 1041 | // adjustments also, even when the frame itself is small. |
| 1042 | if (BigStack && !ExtraCSSpill) { |
| 1043 | // If any non-reserved CS register isn't spilled, just spill one or two |
| 1044 | // extra. That should take care of it! |
| 1045 | unsigned NumExtras = TargetAlign / 4; |
| 1046 | SmallVector<unsigned, 2> Extras; |
| 1047 | while (NumExtras && !UnspilledCS1GPRs.empty()) { |
| 1048 | unsigned Reg = UnspilledCS1GPRs.back(); |
| 1049 | UnspilledCS1GPRs.pop_back(); |
| 1050 | if (!RegInfo->isReservedReg(MF, Reg) && |
| 1051 | (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || |
| 1052 | Reg == ARM::LR)) { |
| 1053 | Extras.push_back(Reg); |
| 1054 | NumExtras--; |
| 1055 | } |
| 1056 | } |
| 1057 | // For non-Thumb1 functions, also check for hi-reg CS registers |
| 1058 | if (!AFI->isThumb1OnlyFunction()) { |
| 1059 | while (NumExtras && !UnspilledCS2GPRs.empty()) { |
| 1060 | unsigned Reg = UnspilledCS2GPRs.back(); |
| 1061 | UnspilledCS2GPRs.pop_back(); |
| 1062 | if (!RegInfo->isReservedReg(MF, Reg)) { |
| 1063 | Extras.push_back(Reg); |
| 1064 | NumExtras--; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | if (Extras.size() && NumExtras == 0) { |
| 1069 | for (unsigned i = 0, e = Extras.size(); i != e; ++i) { |
| 1070 | MF.getRegInfo().setPhysRegUsed(Extras[i]); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } else if (!AFI->isThumb1OnlyFunction()) { |
| 1073 | // note: Thumb1 functions spill to R12, not the stack. Reserve a slot |
| 1074 | // closest to SP or frame pointer. |
| 1075 | const TargetRegisterClass *RC = ARM::GPRRegisterClass; |
| 1076 | RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), |
| 1077 | RC->getAlignment(), |
| 1078 | false)); |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (ForceLRSpill) { |
| 1084 | MF.getRegInfo().setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 94c5ae0 | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1085 | AFI->setLRIsSpilledForFarJump(true); |
| 1086 | } |
| 1087 | } |