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