Anton Korobeynikov | 3346491 | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 1 | //=======- Thumb1FrameInfo.cpp - Thumb1 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 Thumb1 implementation of TargetFrameInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Thumb1FrameInfo.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" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | static void emitSPUpdate(MachineBasicBlock &MBB, |
| 24 | MachineBasicBlock::iterator &MBBI, |
| 25 | const TargetInstrInfo &TII, DebugLoc dl, |
| 26 | const Thumb1RegisterInfo &MRI, |
| 27 | int NumBytes) { |
| 28 | emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII, |
| 29 | MRI, dl); |
| 30 | } |
| 31 | |
| 32 | void Thumb1FrameInfo::emitPrologue(MachineFunction &MF) const { |
| 33 | MachineBasicBlock &MBB = MF.front(); |
| 34 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 35 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 36 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 37 | const Thumb1RegisterInfo *RegInfo = |
| 38 | static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 39 | const Thumb1InstrInfo &TII = |
| 40 | *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo()); |
| 41 | |
| 42 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 43 | unsigned NumBytes = MFI->getStackSize(); |
| 44 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 45 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 46 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 47 | unsigned BasePtr = RegInfo->getBaseRegister(); |
| 48 | |
| 49 | // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4. |
| 50 | NumBytes = (NumBytes + 3) & ~3; |
| 51 | MFI->setStackSize(NumBytes); |
| 52 | |
| 53 | // Determine the sizes of each callee-save spill areas and record which frame |
| 54 | // belongs to which callee-save spill areas. |
| 55 | unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; |
| 56 | int FramePtrSpillFI = 0; |
| 57 | |
| 58 | if (VARegSaveSize) |
| 59 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -VARegSaveSize); |
| 60 | |
| 61 | if (!AFI->hasStackFrame()) { |
| 62 | if (NumBytes != 0) |
| 63 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 68 | unsigned Reg = CSI[i].getReg(); |
| 69 | int FI = CSI[i].getFrameIdx(); |
| 70 | switch (Reg) { |
| 71 | case ARM::R4: |
| 72 | case ARM::R5: |
| 73 | case ARM::R6: |
| 74 | case ARM::R7: |
| 75 | case ARM::LR: |
| 76 | if (Reg == FramePtr) |
| 77 | FramePtrSpillFI = FI; |
| 78 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 79 | GPRCS1Size += 4; |
| 80 | break; |
| 81 | case ARM::R8: |
| 82 | case ARM::R9: |
| 83 | case ARM::R10: |
| 84 | case ARM::R11: |
| 85 | if (Reg == FramePtr) |
| 86 | FramePtrSpillFI = FI; |
| 87 | if (STI.isTargetDarwin()) { |
| 88 | AFI->addGPRCalleeSavedArea2Frame(FI); |
| 89 | GPRCS2Size += 4; |
| 90 | } else { |
| 91 | AFI->addGPRCalleeSavedArea1Frame(FI); |
| 92 | GPRCS1Size += 4; |
| 93 | } |
| 94 | break; |
| 95 | default: |
| 96 | AFI->addDPRCalleeSavedAreaFrame(FI); |
| 97 | DPRCSSize += 8; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) { |
| 102 | ++MBBI; |
| 103 | if (MBBI != MBB.end()) |
| 104 | dl = MBBI->getDebugLoc(); |
| 105 | } |
| 106 | |
| 107 | // Adjust FP so it point to the stack slot that contains the previous FP. |
| 108 | if (RegInfo->hasFP(MF)) { |
| 109 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr) |
| 110 | .addFrameIndex(FramePtrSpillFI).addImm(0); |
| 111 | AFI->setShouldRestoreSPFromFP(true); |
| 112 | } |
| 113 | |
| 114 | // Determine starting offsets of spill areas. |
| 115 | unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize); |
| 116 | unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize; |
| 117 | unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size; |
| 118 | AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes); |
| 119 | AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); |
| 120 | AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); |
| 121 | AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); |
| 122 | |
| 123 | NumBytes = DPRCSOffset; |
| 124 | if (NumBytes) { |
| 125 | // Insert it after all the callee-save spills. |
| 126 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, -NumBytes); |
| 127 | } |
| 128 | |
| 129 | if (STI.isTargetELF() && RegInfo->hasFP(MF)) |
| 130 | MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - |
| 131 | AFI->getFramePtrSpillOffset()); |
| 132 | |
| 133 | AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); |
| 134 | AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); |
| 135 | AFI->setDPRCalleeSavedAreaSize(DPRCSSize); |
| 136 | |
| 137 | // If we need a base pointer, set it up here. It's whatever the value |
| 138 | // of the stack pointer is at this point. Any variable size objects |
| 139 | // will be allocated after this, so we can still use the base pointer |
| 140 | // to reference locals. |
| 141 | if (RegInfo->hasBasePointer(MF)) |
| 142 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), BasePtr).addReg(ARM::SP); |
| 143 | } |
| 144 | |
| 145 | static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) { |
| 146 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 147 | if (Reg == CSRegs[i]) |
| 148 | return true; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) { |
| 153 | if (MI->getOpcode() == ARM::tRestore && |
| 154 | MI->getOperand(1).isFI() && |
| 155 | isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs)) |
| 156 | return true; |
| 157 | else if (MI->getOpcode() == ARM::tPOP) { |
| 158 | // The first two operands are predicates. The last two are |
| 159 | // imp-def and imp-use of SP. Check everything in between. |
| 160 | for (int i = 2, e = MI->getNumOperands() - 2; i != e; ++i) |
| 161 | if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) |
| 162 | return false; |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | void Thumb1FrameInfo::emitEpilogue(MachineFunction &MF, |
| 169 | MachineBasicBlock &MBB) const { |
| 170 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
| 171 | assert((MBBI->getOpcode() == ARM::tBX_RET || |
| 172 | MBBI->getOpcode() == ARM::tPOP_RET) && |
| 173 | "Can only insert epilog into returning blocks"); |
| 174 | DebugLoc dl = MBBI->getDebugLoc(); |
| 175 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 176 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 177 | const Thumb1RegisterInfo *RegInfo = |
| 178 | static_cast<const Thumb1RegisterInfo*>(MF.getTarget().getRegisterInfo()); |
| 179 | const Thumb1InstrInfo &TII = |
| 180 | *static_cast<const Thumb1InstrInfo*>(MF.getTarget().getInstrInfo()); |
| 181 | |
| 182 | unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize(); |
| 183 | int NumBytes = (int)MFI->getStackSize(); |
| 184 | const unsigned *CSRegs = RegInfo->getCalleeSavedRegs(); |
| 185 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 186 | |
| 187 | if (!AFI->hasStackFrame()) { |
| 188 | if (NumBytes != 0) |
| 189 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes); |
| 190 | } else { |
| 191 | // Unwind MBBI to point to first LDR / VLDRD. |
| 192 | if (MBBI != MBB.begin()) { |
| 193 | do |
| 194 | --MBBI; |
| 195 | while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs)); |
| 196 | if (!isCSRestore(MBBI, CSRegs)) |
| 197 | ++MBBI; |
| 198 | } |
| 199 | |
| 200 | // Move SP to start of FP callee save spill area. |
| 201 | NumBytes -= (AFI->getGPRCalleeSavedArea1Size() + |
| 202 | AFI->getGPRCalleeSavedArea2Size() + |
| 203 | AFI->getDPRCalleeSavedAreaSize()); |
| 204 | |
| 205 | if (AFI->shouldRestoreSPFromFP()) { |
| 206 | NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; |
| 207 | // Reset SP based on frame pointer only if the stack frame extends beyond |
| 208 | // frame pointer stack slot or target is ELF and the function has FP. |
| 209 | if (NumBytes) |
| 210 | emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, |
| 211 | TII, *RegInfo, dl); |
| 212 | else |
| 213 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP) |
| 214 | .addReg(FramePtr); |
| 215 | } else { |
| 216 | if (MBBI->getOpcode() == ARM::tBX_RET && |
| 217 | &MBB.front() != MBBI && |
| 218 | prior(MBBI)->getOpcode() == ARM::tPOP) { |
| 219 | MachineBasicBlock::iterator PMBBI = prior(MBBI); |
| 220 | emitSPUpdate(MBB, PMBBI, TII, dl, *RegInfo, NumBytes); |
| 221 | } else |
| 222 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, NumBytes); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (VARegSaveSize) { |
| 227 | // Unlike T2 and ARM mode, the T1 pop instruction cannot restore |
| 228 | // to LR, and we can't pop the value directly to the PC since |
| 229 | // we need to update the SP after popping the value. Therefore, we |
| 230 | // pop the old LR into R3 as a temporary. |
| 231 | |
| 232 | // Move back past the callee-saved register restoration |
| 233 | while (MBBI != MBB.end() && isCSRestore(MBBI, CSRegs)) |
| 234 | ++MBBI; |
| 235 | // Epilogue for vararg functions: pop LR to R3 and branch off it. |
| 236 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))) |
| 237 | .addReg(ARM::R3, RegState::Define); |
| 238 | |
| 239 | emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, VARegSaveSize); |
| 240 | |
| 241 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg)) |
| 242 | .addReg(ARM::R3, RegState::Kill); |
| 243 | // erase the old tBX_RET instruction |
| 244 | MBB.erase(MBBI); |
| 245 | } |
| 246 | } |