Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- ARMFrameLowering.cpp - ARM Frame Information ----------------------===// |
Anton Korobeynikov | f7183ed | 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 | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 10 | // This file contains the ARM implementation of TargetFrameLowering class. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 14 | #include "ARMFrameLowering.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 15 | #include "ARMBaseInstrInfo.h" |
Evan Cheng | e45d685 | 2011-01-11 21:46:47 +0000 | [diff] [blame] | 16 | #include "ARMBaseRegisterInfo.h" |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 17 | #include "ARMConstantPoolValue.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | a20cde3 | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/ARMAddressingModes.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterScavenging.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CallingConv.h" |
| 27 | #include "llvm/IR/Function.h" |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Benjamin Kramer | 9fceb90 | 2012-02-24 22:09:25 +0000 | [diff] [blame] | 34 | static cl::opt<bool> |
Jakob Stoklund Olesen | 68a922c | 2012-01-06 22:19:37 +0000 | [diff] [blame] | 35 | SpillAlignedNEONRegs("align-neon-spills", cl::Hidden, cl::init(true), |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 36 | cl::desc("Align ARM NEON spills in prolog and epilog")); |
| 37 | |
| 38 | static MachineBasicBlock::iterator |
| 39 | skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, |
| 40 | unsigned NumAlignedDPRCS2Regs); |
| 41 | |
Eric Christopher | 45fb7b6 | 2014-06-26 19:29:59 +0000 | [diff] [blame] | 42 | ARMFrameLowering::ARMFrameLowering(const ARMSubtarget &sti) |
| 43 | : TargetFrameLowering(StackGrowsDown, sti.getStackAlignment(), 0, 4), |
| 44 | STI(sti) {} |
| 45 | |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 46 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 47 | /// pointer register. This is true if the function has variable sized allocas |
| 48 | /// or if frame pointer elimination is disabled. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 49 | bool ARMFrameLowering::hasFP(const MachineFunction &MF) const { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 50 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 51 | |
Evan Cheng | 801d98b | 2012-01-04 01:55:04 +0000 | [diff] [blame] | 52 | // iOS requires FP not to be clobbered for backtracing purpose. |
| 53 | if (STI.isTargetIOS()) |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 54 | return true; |
| 55 | |
| 56 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 57 | // Always eliminate non-leaf frame pointers. |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 58 | return ((MF.getTarget().Options.DisableFramePointerElim(MF) && |
| 59 | MFI->hasCalls()) || |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 60 | RegInfo->needsStackRealignment(MF) || |
| 61 | MFI->hasVarSizedObjects() || |
| 62 | MFI->isFrameAddressTaken()); |
| 63 | } |
| 64 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 65 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 66 | /// not required, we reserve argument space for call sites in the function |
| 67 | /// immediately on entry to the current function. This eliminates the need for |
| 68 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 69 | /// included as part of the stack frame. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 70 | bool ARMFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 71 | const MachineFrameInfo *FFI = MF.getFrameInfo(); |
| 72 | unsigned CFSize = FFI->getMaxCallFrameSize(); |
| 73 | // It's not always a good idea to include the call frame as part of the |
| 74 | // stack frame. ARM (especially Thumb) has small immediate offset to |
| 75 | // address the stack frame. So a large call frame can cause poor codegen |
| 76 | // and may even makes it impossible to scavenge a register. |
| 77 | if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12 |
| 78 | return false; |
| 79 | |
| 80 | return !MF.getFrameInfo()->hasVarSizedObjects(); |
| 81 | } |
| 82 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 83 | /// canSimplifyCallFramePseudos - If there is a reserved call frame, the |
| 84 | /// call frame pseudos can be simplified. Unlike most targets, having a FP |
| 85 | /// is not sufficient here since we still may reference some objects via SP |
| 86 | /// even when FP is available in Thumb2 mode. |
| 87 | bool |
| 88 | ARMFrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { |
Anton Korobeynikov | 0eecf5d | 2010-11-18 21:19:35 +0000 | [diff] [blame] | 89 | return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects(); |
| 90 | } |
| 91 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 92 | static bool isCSRestore(MachineInstr *MI, |
| 93 | const ARMBaseInstrInfo &TII, |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 94 | const MCPhysReg *CSRegs) { |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 95 | // Integer spill area is handled with "pop". |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 96 | if (isPopOpcode(MI->getOpcode())) { |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 97 | // The first two operands are predicates. The last two are |
| 98 | // imp-def and imp-use of SP. Check everything in between. |
| 99 | for (int i = 5, e = MI->getNumOperands(); i != e; ++i) |
| 100 | if (!isCalleeSavedRegister(MI->getOperand(i).getReg(), CSRegs)) |
| 101 | return false; |
| 102 | return true; |
| 103 | } |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 104 | if ((MI->getOpcode() == ARM::LDR_POST_IMM || |
| 105 | MI->getOpcode() == ARM::LDR_POST_REG || |
Jim Grosbach | bdb7ed1 | 2010-12-10 18:41:15 +0000 | [diff] [blame] | 106 | MI->getOpcode() == ARM::t2LDR_POST) && |
| 107 | isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs) && |
| 108 | MI->getOperand(1).getReg() == ARM::SP) |
| 109 | return true; |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 110 | |
| 111 | return false; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 114 | static void emitRegPlusImmediate(bool isARM, MachineBasicBlock &MBB, |
| 115 | MachineBasicBlock::iterator &MBBI, DebugLoc dl, |
| 116 | const ARMBaseInstrInfo &TII, unsigned DestReg, |
| 117 | unsigned SrcReg, int NumBytes, |
| 118 | unsigned MIFlags = MachineInstr::NoFlags, |
| 119 | ARMCC::CondCodes Pred = ARMCC::AL, |
| 120 | unsigned PredReg = 0) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 121 | if (isARM) |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 122 | emitARMRegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 123 | Pred, PredReg, TII, MIFlags); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 124 | else |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 125 | emitT2RegPlusImmediate(MBB, MBBI, dl, DestReg, SrcReg, NumBytes, |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 126 | Pred, PredReg, TII, MIFlags); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 129 | static void emitSPUpdate(bool isARM, MachineBasicBlock &MBB, |
| 130 | MachineBasicBlock::iterator &MBBI, DebugLoc dl, |
| 131 | const ARMBaseInstrInfo &TII, int NumBytes, |
| 132 | unsigned MIFlags = MachineInstr::NoFlags, |
| 133 | ARMCC::CondCodes Pred = ARMCC::AL, |
| 134 | unsigned PredReg = 0) { |
| 135 | emitRegPlusImmediate(isARM, MBB, MBBI, dl, TII, ARM::SP, ARM::SP, NumBytes, |
| 136 | MIFlags, Pred, PredReg); |
| 137 | } |
| 138 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 139 | static int sizeOfSPAdjustment(const MachineInstr *MI) { |
| 140 | assert(MI->getOpcode() == ARM::VSTMDDB_UPD); |
| 141 | int count = 0; |
| 142 | // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+ |
| 143 | // pred) so the list starts at 4. |
| 144 | for (int i = MI->getNumOperands() - 1; i >= 4; --i) |
| 145 | count += 8; |
| 146 | return count; |
| 147 | } |
| 148 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 149 | static bool WindowsRequiresStackProbe(const MachineFunction &MF, |
| 150 | size_t StackSizeInBytes) { |
| 151 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 152 | if (MFI->getStackProtectorIndex() > 0) |
| 153 | return StackSizeInBytes >= 4080; |
| 154 | return StackSizeInBytes >= 4096; |
| 155 | } |
| 156 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 157 | void ARMFrameLowering::emitPrologue(MachineFunction &MF) const { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 158 | MachineBasicBlock &MBB = MF.front(); |
| 159 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 160 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 161 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 162 | MachineModuleInfo &MMI = MF.getMMI(); |
| 163 | MCContext &Context = MMI.getContext(); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 164 | const TargetMachine &TM = MF.getTarget(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 165 | const MCRegisterInfo *MRI = Context.getRegisterInfo(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 166 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
| 167 | TM.getSubtargetImpl()->getRegisterInfo()); |
| 168 | const ARMBaseInstrInfo &TII = *static_cast<const ARMBaseInstrInfo *>( |
| 169 | TM.getSubtargetImpl()->getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 170 | assert(!AFI->isThumb1OnlyFunction() && |
| 171 | "This emitPrologue does not support Thumb1!"); |
| 172 | bool isARM = !AFI->isThumbFunction(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 173 | unsigned Align = |
| 174 | TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment(); |
Stepan Dyatkovskiy | d0e34a2 | 2013-05-20 08:01:34 +0000 | [diff] [blame] | 175 | unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 176 | unsigned NumBytes = MFI->getStackSize(); |
| 177 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 178 | DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 179 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 180 | int CFAOffset = 0; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 181 | |
| 182 | // Determine the sizes of each callee-save spill areas and record which frame |
| 183 | // belongs to which callee-save spill areas. |
| 184 | unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0; |
| 185 | int FramePtrSpillFI = 0; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 186 | int D8SpillFI = 0; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 187 | |
Jakob Stoklund Olesen | e380183 | 2012-10-26 21:46:57 +0000 | [diff] [blame] | 188 | // All calls are tail calls in GHC calling conv, and functions have no |
| 189 | // prologue/epilogue. |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 190 | if (MF.getFunction()->getCallingConv() == CallingConv::GHC) |
| 191 | return; |
| 192 | |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 193 | // Allocate the vararg register save area. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 194 | if (ArgRegsSaveSize) { |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 195 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -ArgRegsSaveSize, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 196 | MachineInstr::FrameSetup); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 197 | CFAOffset -= ArgRegsSaveSize; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 198 | unsigned CFIIndex = MMI.addFrameInst( |
| 199 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 200 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 201 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 202 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 203 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 204 | if (!AFI->hasStackFrame() && |
| 205 | (!STI.isTargetWindows() || !WindowsRequiresStackProbe(MF, NumBytes))) { |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 206 | if (NumBytes - ArgRegsSaveSize != 0) { |
| 207 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -(NumBytes - ArgRegsSaveSize), |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 208 | MachineInstr::FrameSetup); |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 209 | CFAOffset -= NumBytes - ArgRegsSaveSize; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 210 | unsigned CFIIndex = MMI.addFrameInst( |
| 211 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 212 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 213 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 214 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 218 | // Determine spill area sizes. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 219 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 220 | unsigned Reg = CSI[i].getReg(); |
| 221 | int FI = CSI[i].getFrameIdx(); |
| 222 | switch (Reg) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 223 | case ARM::R8: |
| 224 | case ARM::R9: |
| 225 | case ARM::R10: |
| 226 | case ARM::R11: |
| 227 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 228 | if (STI.isTargetDarwin()) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 229 | GPRCS2Size += 4; |
| 230 | break; |
| 231 | } |
| 232 | // fallthrough |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 233 | case ARM::R0: |
| 234 | case ARM::R1: |
| 235 | case ARM::R2: |
| 236 | case ARM::R3: |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 237 | case ARM::R4: |
| 238 | case ARM::R5: |
| 239 | case ARM::R6: |
| 240 | case ARM::R7: |
| 241 | case ARM::LR: |
| 242 | if (Reg == FramePtr) |
| 243 | FramePtrSpillFI = FI; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 244 | GPRCS1Size += 4; |
| 245 | break; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 246 | default: |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 247 | // This is a DPR. Exclude the aligned DPRCS2 spills. |
| 248 | if (Reg == ARM::D8) |
| 249 | D8SpillFI = FI; |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 250 | if (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs()) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 251 | DPRCSSize += 8; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 254 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 255 | // Move past area 1. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 256 | MachineBasicBlock::iterator LastPush = MBB.end(), GPRCS1Push, GPRCS2Push, |
| 257 | DPRCSPush; |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 258 | if (GPRCS1Size > 0) |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 259 | GPRCS1Push = LastPush = MBBI++; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 260 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 261 | // Determine starting offsets of spill areas. |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 262 | bool HasFP = hasFP(MF); |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame^] | 263 | unsigned GPRCS1Offset = NumBytes - ArgRegsSaveSize - GPRCS1Size; |
| 264 | unsigned GPRCS2Offset = GPRCS1Offset - GPRCS2Size; |
| 265 | unsigned DPRAlign = DPRCSSize ? std::min(8U, Align) : 4U; |
| 266 | unsigned DPRGapSize = (GPRCS1Size + GPRCS2Size + ArgRegsSaveSize) % DPRAlign; |
| 267 | unsigned DPRCSOffset = GPRCS2Offset - DPRGapSize - DPRCSSize; |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 268 | int FramePtrOffsetInPush = 0; |
| 269 | if (HasFP) { |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 270 | FramePtrOffsetInPush = MFI->getObjectOffset(FramePtrSpillFI) |
| 271 | + GPRCS1Size + ArgRegsSaveSize; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 272 | AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + |
| 273 | NumBytes); |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 274 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 275 | AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset); |
| 276 | AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset); |
| 277 | AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset); |
| 278 | |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 279 | // Move past area 2. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 280 | if (GPRCS2Size > 0) |
| 281 | GPRCS2Push = LastPush = MBBI++; |
Tim Northover | c9432eb | 2013-11-04 23:04:15 +0000 | [diff] [blame] | 282 | |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame^] | 283 | // Prolog/epilog inserter assumes we correctly align DPRs on the stack, so our |
| 284 | // .cfi_offset operations will reflect that. |
| 285 | if (DPRGapSize) { |
| 286 | assert(DPRGapSize == 4 && "unexpected alignment requirements for DPRs"); |
| 287 | if (!tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, DPRGapSize)) |
| 288 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRGapSize, |
| 289 | MachineInstr::FrameSetup); |
| 290 | } |
| 291 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 292 | // Move past area 3. |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 293 | if (DPRCSSize > 0) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 294 | DPRCSPush = MBBI; |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 295 | // Since vpush register list cannot have gaps, there may be multiple vpush |
Evan Cheng | a921dc5 | 2011-02-25 01:29:29 +0000 | [diff] [blame] | 296 | // instructions in the prologue. |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 297 | while (MBBI->getOpcode() == ARM::VSTMDDB_UPD) |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 298 | LastPush = MBBI++; |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 299 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 300 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 301 | // Move past the aligned DPRCS2 area. |
| 302 | if (AFI->getNumAlignedDPRCS2Regs() > 0) { |
| 303 | MBBI = skipAlignedDPRCS2Spills(MBBI, AFI->getNumAlignedDPRCS2Regs()); |
| 304 | // The code inserted by emitAlignedDPRCS2Spills realigns the stack, and |
| 305 | // leaves the stack pointer pointing to the DPRCS2 area. |
| 306 | // |
| 307 | // Adjust NumBytes to represent the stack slots below the DPRCS2 area. |
| 308 | NumBytes += MFI->getObjectOffset(D8SpillFI); |
| 309 | } else |
| 310 | NumBytes = DPRCSOffset; |
| 311 | |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 312 | if (STI.isTargetWindows() && WindowsRequiresStackProbe(MF, NumBytes)) { |
| 313 | uint32_t NumWords = NumBytes >> 2; |
| 314 | |
| 315 | if (NumWords < 65536) |
| 316 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), ARM::R4) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 317 | .addImm(NumWords) |
| 318 | .setMIFlags(MachineInstr::FrameSetup)); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 319 | else |
| 320 | BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R4) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 321 | .addImm(NumWords) |
| 322 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 323 | |
| 324 | switch (TM.getCodeModel()) { |
| 325 | case CodeModel::Small: |
| 326 | case CodeModel::Medium: |
| 327 | case CodeModel::Default: |
| 328 | case CodeModel::Kernel: |
| 329 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tBL)) |
| 330 | .addImm((unsigned)ARMCC::AL).addReg(0) |
| 331 | .addExternalSymbol("__chkstk") |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 332 | .addReg(ARM::R4, RegState::Implicit) |
| 333 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 334 | break; |
| 335 | case CodeModel::Large: |
Saleem Abdulrasool | 7158303 | 2014-05-01 04:19:59 +0000 | [diff] [blame] | 336 | case CodeModel::JITDefault: |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 337 | BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi32imm), ARM::R12) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 338 | .addExternalSymbol("__chkstk") |
| 339 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 7158303 | 2014-05-01 04:19:59 +0000 | [diff] [blame] | 340 | |
Saleem Abdulrasool | acd0338 | 2014-05-07 03:03:27 +0000 | [diff] [blame] | 341 | BuildMI(MBB, MBBI, dl, TII.get(ARM::tBLXr)) |
| 342 | .addImm((unsigned)ARMCC::AL).addReg(0) |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 343 | .addReg(ARM::R12, RegState::Kill) |
Saleem Abdulrasool | 985dcf1 | 2014-05-07 03:03:31 +0000 | [diff] [blame] | 344 | .addReg(ARM::R4, RegState::Implicit) |
| 345 | .setMIFlags(MachineInstr::FrameSetup); |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 346 | break; |
| 347 | } |
Saleem Abdulrasool | 25947c3 | 2014-04-30 07:05:07 +0000 | [diff] [blame] | 348 | |
| 349 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), |
| 350 | ARM::SP) |
| 351 | .addReg(ARM::SP, RegState::Define) |
| 352 | .addReg(ARM::R4, RegState::Kill) |
| 353 | .setMIFlags(MachineInstr::FrameSetup))); |
| 354 | NumBytes = 0; |
| 355 | } |
| 356 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 357 | unsigned adjustedGPRCS1Size = GPRCS1Size; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 358 | if (NumBytes) { |
| 359 | // Adjust SP after all the callee-save spills. |
Tim Northover | a417371 | 2013-12-08 15:56:50 +0000 | [diff] [blame] | 360 | if (tryFoldSPUpdateIntoPushPop(STI, MF, LastPush, NumBytes)) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 361 | if (LastPush == GPRCS1Push) { |
Tim Northover | a417371 | 2013-12-08 15:56:50 +0000 | [diff] [blame] | 362 | FramePtrOffsetInPush += NumBytes; |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 363 | adjustedGPRCS1Size += NumBytes; |
| 364 | NumBytes = 0; |
| 365 | } |
Tim Northover | a417371 | 2013-12-08 15:56:50 +0000 | [diff] [blame] | 366 | } else |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 367 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes, |
| 368 | MachineInstr::FrameSetup); |
| 369 | |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 370 | if (HasFP && isARM) |
| 371 | // Restore from fp only in ARM mode: e.g. sub sp, r7, #24 |
| 372 | // Note it's not safe to do this in Thumb2 mode because it would have |
| 373 | // taken two instructions: |
| 374 | // mov sp, r7 |
| 375 | // sub sp, #24 |
| 376 | // If an interrupt is taken between the two instructions, then sp is in |
| 377 | // an inconsistent state (pointing to the middle of callee-saved area). |
| 378 | // The interrupt handler can end up clobbering the registers. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 379 | AFI->setShouldRestoreSPFromFP(true); |
| 380 | } |
| 381 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 382 | if (adjustedGPRCS1Size > 0) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 383 | CFAOffset -= adjustedGPRCS1Size; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 384 | unsigned CFIIndex = MMI.addFrameInst( |
| 385 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 386 | MachineBasicBlock::iterator Pos = ++GPRCS1Push; |
| 387 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 388 | .addCFIIndex(CFIIndex); |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 389 | for (const auto &Entry : CSI) { |
| 390 | unsigned Reg = Entry.getReg(); |
| 391 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 392 | switch (Reg) { |
| 393 | case ARM::R8: |
| 394 | case ARM::R9: |
| 395 | case ARM::R10: |
| 396 | case ARM::R11: |
| 397 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 398 | if (STI.isTargetDarwin()) |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 399 | break; |
| 400 | // fallthrough |
| 401 | case ARM::R0: |
| 402 | case ARM::R1: |
| 403 | case ARM::R2: |
| 404 | case ARM::R3: |
| 405 | case ARM::R4: |
| 406 | case ARM::R5: |
| 407 | case ARM::R6: |
| 408 | case ARM::R7: |
| 409 | case ARM::LR: |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 410 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 411 | nullptr, MRI->getDwarfRegNum(Reg, true), MFI->getObjectOffset(FI))); |
| 412 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 413 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 419 | // Set FP to point to the stack slot that contains the previous FP. |
| 420 | // For iOS, FP is R7, which has now been stored in spill area 1. |
| 421 | // Otherwise, if this is not iOS, all the callee-saved registers go |
| 422 | // into spill area 1, including the FP in R11. In either case, it |
| 423 | // is in area one and the adjustment needs to take place just after |
| 424 | // that push. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 425 | if (HasFP) { |
| 426 | emitRegPlusImmediate(!AFI->isThumbFunction(), MBB, GPRCS1Push, dl, TII, |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 427 | FramePtr, ARM::SP, FramePtrOffsetInPush, |
| 428 | MachineInstr::FrameSetup); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 429 | if (FramePtrOffsetInPush) { |
| 430 | CFAOffset += FramePtrOffsetInPush; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 431 | unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfa( |
| 432 | nullptr, MRI->getDwarfRegNum(FramePtr, true), CFAOffset)); |
| 433 | BuildMI(MBB, GPRCS1Push, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 434 | .addCFIIndex(CFIIndex); |
| 435 | |
| 436 | } else { |
| 437 | unsigned CFIIndex = |
| 438 | MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( |
| 439 | nullptr, MRI->getDwarfRegNum(FramePtr, true))); |
| 440 | BuildMI(MBB, GPRCS1Push, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 441 | .addCFIIndex(CFIIndex); |
| 442 | } |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 443 | } |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 444 | |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 445 | if (GPRCS2Size > 0) { |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 446 | MachineBasicBlock::iterator Pos = ++GPRCS2Push; |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 447 | if (!HasFP) { |
| 448 | CFAOffset -= GPRCS2Size; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 449 | unsigned CFIIndex = MMI.addFrameInst( |
| 450 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 451 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 452 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 453 | } |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 454 | for (const auto &Entry : CSI) { |
| 455 | unsigned Reg = Entry.getReg(); |
| 456 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 457 | switch (Reg) { |
| 458 | case ARM::R8: |
| 459 | case ARM::R9: |
| 460 | case ARM::R10: |
| 461 | case ARM::R11: |
| 462 | case ARM::R12: |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 463 | if (STI.isTargetDarwin()) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 464 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 465 | unsigned Offset = MFI->getObjectOffset(FI); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 466 | unsigned CFIIndex = MMI.addFrameInst( |
| 467 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
| 468 | BuildMI(MBB, Pos, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 469 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 470 | } |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (DPRCSSize > 0) { |
| 477 | // Since vpush register list cannot have gaps, there may be multiple vpush |
| 478 | // instructions in the prologue. |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 479 | do { |
| 480 | MachineBasicBlock::iterator Push = DPRCSPush++; |
| 481 | if (!HasFP) { |
Alp Toker | 9844434 | 2014-04-19 23:56:35 +0000 | [diff] [blame] | 482 | CFAOffset -= sizeOfSPAdjustment(Push); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 483 | unsigned CFIIndex = MMI.addFrameInst( |
| 484 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 485 | BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 486 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 487 | } |
| 488 | } while (DPRCSPush->getOpcode() == ARM::VSTMDDB_UPD); |
| 489 | |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 490 | for (const auto &Entry : CSI) { |
| 491 | unsigned Reg = Entry.getReg(); |
| 492 | int FI = Entry.getFrameIdx(); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 493 | if ((Reg >= ARM::D0 && Reg <= ARM::D31) && |
| 494 | (Reg < ARM::D8 || Reg >= ARM::D8 + AFI->getNumAlignedDPRCS2Regs())) { |
| 495 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
| 496 | unsigned Offset = MFI->getObjectOffset(FI); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 497 | unsigned CFIIndex = MMI.addFrameInst( |
| 498 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
| 499 | BuildMI(MBB, DPRCSPush, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 500 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (NumBytes) { |
| 506 | if (!HasFP) { |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 507 | CFAOffset -= NumBytes; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 508 | unsigned CFIIndex = MMI.addFrameInst( |
| 509 | MCCFIInstruction::createDefCfaOffset(nullptr, CFAOffset)); |
| 510 | BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 511 | .addCFIIndex(CFIIndex); |
Artyom Skrobov | f6830f4 | 2014-02-14 17:19:07 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 514 | |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 515 | if (STI.isTargetELF() && hasFP(MF)) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 516 | MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() - |
| 517 | AFI->getFramePtrSpillOffset()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 518 | |
| 519 | AFI->setGPRCalleeSavedArea1Size(GPRCS1Size); |
| 520 | AFI->setGPRCalleeSavedArea2Size(GPRCS2Size); |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame^] | 521 | AFI->setDPRCalleeSavedGapSize(DPRGapSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 522 | AFI->setDPRCalleeSavedAreaSize(DPRCSSize); |
| 523 | |
| 524 | // If we need dynamic stack realignment, do it here. Be paranoid and make |
| 525 | // sure if we also have VLAs, we have a base pointer for frame access. |
Jakob Stoklund Olesen | 103318e | 2011-12-24 04:17:01 +0000 | [diff] [blame] | 526 | // If aligned NEON registers were spilled, the stack has already been |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 527 | // realigned. |
| 528 | if (!AFI->getNumAlignedDPRCS2Regs() && RegInfo->needsStackRealignment(MF)) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 529 | unsigned MaxAlign = MFI->getMaxAlignment(); |
| 530 | assert (!AFI->isThumb1OnlyFunction()); |
| 531 | if (!AFI->isThumbFunction()) { |
| 532 | // Emit bic sp, sp, MaxAlign |
| 533 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 534 | TII.get(ARM::BICri), ARM::SP) |
| 535 | .addReg(ARM::SP, RegState::Kill) |
| 536 | .addImm(MaxAlign-1))); |
| 537 | } else { |
| 538 | // We cannot use sp as source/dest register here, thus we're emitting the |
| 539 | // following sequence: |
| 540 | // mov r4, sp |
| 541 | // bic r4, r4, MaxAlign |
| 542 | // mov sp, r4 |
| 543 | // FIXME: It will be better just to find spare register here. |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 544 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::R4) |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 545 | .addReg(ARM::SP, RegState::Kill)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 546 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl, |
| 547 | TII.get(ARM::t2BICri), ARM::R4) |
| 548 | .addReg(ARM::R4, RegState::Kill) |
| 549 | .addImm(MaxAlign-1))); |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 550 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), ARM::SP) |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 551 | .addReg(ARM::R4, RegState::Kill)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | AFI->setShouldRestoreSPFromFP(true); |
| 555 | } |
| 556 | |
| 557 | // If we need a base pointer, set it up here. It's whatever the value |
| 558 | // of the stack pointer is at this point. Any variable size objects |
| 559 | // will be allocated after this, so we can still use the base pointer |
| 560 | // to reference locals. |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 561 | // FIXME: Clarify FrameSetup flags here. |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 562 | if (RegInfo->hasBasePointer(MF)) { |
| 563 | if (isARM) |
| 564 | BuildMI(MBB, MBBI, dl, |
| 565 | TII.get(ARM::MOVr), RegInfo->getBaseRegister()) |
| 566 | .addReg(ARM::SP) |
| 567 | .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 568 | else |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 569 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 570 | RegInfo->getBaseRegister()) |
| 571 | .addReg(ARM::SP)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // If the frame has variable sized objects then the epilogue must restore |
Eric Christopher | d5bbeba | 2011-01-10 23:10:59 +0000 | [diff] [blame] | 575 | // the sp from fp. We can assume there's an FP here since hasFP already |
| 576 | // checks for hasVarSizedObjects. |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 577 | if (MFI->hasVarSizedObjects()) |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 578 | AFI->setShouldRestoreSPFromFP(true); |
| 579 | } |
| 580 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 581 | void ARMFrameLowering::emitEpilogue(MachineFunction &MF, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 582 | MachineBasicBlock &MBB) const { |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 583 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 584 | assert(MBBI->isReturn() && "Can only insert epilog into returning blocks"); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 585 | unsigned RetOpcode = MBBI->getOpcode(); |
| 586 | DebugLoc dl = MBBI->getDebugLoc(); |
| 587 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 588 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 589 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 590 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 591 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 592 | assert(!AFI->isThumb1OnlyFunction() && |
| 593 | "This emitEpilogue does not support Thumb1!"); |
| 594 | bool isARM = !AFI->isThumbFunction(); |
| 595 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 596 | unsigned Align = MF.getTarget() |
| 597 | .getSubtargetImpl() |
| 598 | ->getFrameLowering() |
| 599 | ->getStackAlignment(); |
Stepan Dyatkovskiy | d0e34a2 | 2013-05-20 08:01:34 +0000 | [diff] [blame] | 600 | unsigned ArgRegsSaveSize = AFI->getArgRegsSaveSize(Align); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 601 | int NumBytes = (int)MFI->getStackSize(); |
| 602 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 603 | |
Jakob Stoklund Olesen | e380183 | 2012-10-26 21:46:57 +0000 | [diff] [blame] | 604 | // All calls are tail calls in GHC calling conv, and functions have no |
| 605 | // prologue/epilogue. |
Eric Christopher | b332236 | 2012-08-03 00:05:53 +0000 | [diff] [blame] | 606 | if (MF.getFunction()->getCallingConv() == CallingConv::GHC) |
| 607 | return; |
| 608 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 609 | if (!AFI->hasStackFrame()) { |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 610 | if (NumBytes - ArgRegsSaveSize != 0) |
| 611 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes - ArgRegsSaveSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 612 | } else { |
| 613 | // Unwind MBBI to point to first LDR / VLDRD. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 614 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 615 | if (MBBI != MBB.begin()) { |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 616 | do { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 617 | --MBBI; |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 618 | } while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 619 | if (!isCSRestore(MBBI, TII, CSRegs)) |
| 620 | ++MBBI; |
| 621 | } |
| 622 | |
| 623 | // Move SP to start of FP callee save spill area. |
Oliver Stannard | d55e115 | 2014-03-05 15:25:27 +0000 | [diff] [blame] | 624 | NumBytes -= (ArgRegsSaveSize + |
| 625 | AFI->getGPRCalleeSavedArea1Size() + |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 626 | AFI->getGPRCalleeSavedArea2Size() + |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame^] | 627 | AFI->getDPRCalleeSavedGapSize() + |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 628 | AFI->getDPRCalleeSavedAreaSize()); |
| 629 | |
| 630 | // Reset SP based on frame pointer only if the stack frame extends beyond |
| 631 | // frame pointer stack slot or target is ELF and the function has FP. |
| 632 | if (AFI->shouldRestoreSPFromFP()) { |
| 633 | NumBytes = AFI->getFramePtrSpillOffset() - NumBytes; |
| 634 | if (NumBytes) { |
| 635 | if (isARM) |
| 636 | emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes, |
| 637 | ARMCC::AL, 0, TII); |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 638 | else { |
| 639 | // It's not possible to restore SP from FP in a single instruction. |
Evan Cheng | 801d98b | 2012-01-04 01:55:04 +0000 | [diff] [blame] | 640 | // For iOS, this looks like: |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 641 | // mov sp, r7 |
| 642 | // sub sp, #24 |
| 643 | // This is bad, if an interrupt is taken after the mov, sp is in an |
| 644 | // inconsistent state. |
| 645 | // Use the first callee-saved register as a scratch register. |
Kaelyn Uhrain | 271fbb6 | 2012-10-26 23:28:41 +0000 | [diff] [blame] | 646 | assert(MF.getRegInfo().isPhysRegUsed(ARM::R4) && |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 647 | "No scratch register to restore SP from FP!"); |
| 648 | emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::R4, FramePtr, -NumBytes, |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 649 | ARMCC::AL, 0, TII); |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 650 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 651 | ARM::SP) |
| 652 | .addReg(ARM::R4)); |
Evan Cheng | eb56dca | 2010-11-22 18:12:04 +0000 | [diff] [blame] | 653 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 654 | } else { |
| 655 | // Thumb2 or ARM. |
| 656 | if (isARM) |
| 657 | BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP) |
| 658 | .addReg(FramePtr).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0); |
| 659 | else |
Jim Grosbach | e9cc901 | 2011-06-30 23:38:17 +0000 | [diff] [blame] | 660 | AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), |
Jim Grosbach | b98ab91 | 2011-06-30 22:10:46 +0000 | [diff] [blame] | 661 | ARM::SP) |
| 662 | .addReg(FramePtr)); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 663 | } |
Tim Northover | dee8604 | 2013-12-02 14:46:26 +0000 | [diff] [blame] | 664 | } else if (NumBytes && |
Tim Northover | e4def5e | 2013-12-05 11:02:02 +0000 | [diff] [blame] | 665 | !tryFoldSPUpdateIntoPushPop(STI, MF, MBBI, NumBytes)) |
Tim Northover | 93bcc66 | 2013-11-08 17:18:07 +0000 | [diff] [blame] | 666 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 667 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 668 | // Increment past our save areas. |
Evan Cheng | 70d2963 | 2011-02-25 00:24:46 +0000 | [diff] [blame] | 669 | if (AFI->getDPRCalleeSavedAreaSize()) { |
| 670 | MBBI++; |
| 671 | // Since vpop register list cannot have gaps, there may be multiple vpop |
| 672 | // instructions in the epilogue. |
| 673 | while (MBBI->getOpcode() == ARM::VLDMDIA_UPD) |
| 674 | MBBI++; |
| 675 | } |
Tim Northover | 228c943 | 2014-11-05 00:27:13 +0000 | [diff] [blame^] | 676 | if (AFI->getDPRCalleeSavedGapSize()) { |
| 677 | assert(AFI->getDPRCalleeSavedGapSize() == 4 && |
| 678 | "unexpected DPR alignment gap"); |
| 679 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedGapSize()); |
| 680 | } |
| 681 | |
Eric Christopher | b006fc9 | 2010-11-18 19:40:05 +0000 | [diff] [blame] | 682 | if (AFI->getGPRCalleeSavedArea2Size()) MBBI++; |
| 683 | if (AFI->getGPRCalleeSavedArea1Size()) MBBI++; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Jakob Stoklund Olesen | b4bd388 | 2012-04-06 21:17:42 +0000 | [diff] [blame] | 686 | if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNri) { |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 687 | // Tail call return: adjust the stack pointer and jump to callee. |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 688 | MBBI = MBB.getLastNonDebugInstr(); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 689 | MachineOperand &JumpTarget = MBBI->getOperand(0); |
| 690 | |
| 691 | // Jump to label or value in register. |
Jakob Stoklund Olesen | b4bd388 | 2012-04-06 21:17:42 +0000 | [diff] [blame] | 692 | if (RetOpcode == ARM::TCRETURNdi) { |
| 693 | unsigned TCOpcode = STI.isThumb() ? |
Tim Northover | d6a729b | 2014-01-06 14:28:05 +0000 | [diff] [blame] | 694 | (STI.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND) : |
Jakob Stoklund Olesen | b4bd388 | 2012-04-06 21:17:42 +0000 | [diff] [blame] | 695 | ARM::TAILJMPd; |
Evan Cheng | d4b0873 | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 696 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(TCOpcode)); |
| 697 | if (JumpTarget.isGlobal()) |
| 698 | MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), |
| 699 | JumpTarget.getTargetFlags()); |
| 700 | else { |
| 701 | assert(JumpTarget.isSymbol()); |
| 702 | MIB.addExternalSymbol(JumpTarget.getSymbolName(), |
| 703 | JumpTarget.getTargetFlags()); |
| 704 | } |
Owen Anderson | 29cfe6c | 2011-09-09 21:48:23 +0000 | [diff] [blame] | 705 | |
| 706 | // Add the default predicate in Thumb mode. |
| 707 | if (STI.isThumb()) MIB.addImm(ARMCC::AL).addReg(0); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 708 | } else if (RetOpcode == ARM::TCRETURNri) { |
Jim Grosbach | 3af6fe6 | 2011-03-15 00:30:40 +0000 | [diff] [blame] | 709 | BuildMI(MBB, MBBI, dl, |
| 710 | TII.get(STI.isThumb() ? ARM::tTAILJMPr : ARM::TAILJMPr)). |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 711 | addReg(JumpTarget.getReg(), RegState::Kill); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 714 | MachineInstr *NewMI = std::prev(MBBI); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 715 | for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i) |
| 716 | NewMI->addOperand(MBBI->getOperand(i)); |
| 717 | |
| 718 | // Delete the pseudo instruction TCRETURN. |
| 719 | MBB.erase(MBBI); |
Cameron Zwarich | 033026f | 2011-06-17 02:16:43 +0000 | [diff] [blame] | 720 | MBBI = NewMI; |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 723 | if (ArgRegsSaveSize) |
| 724 | emitSPUpdate(isARM, MBB, MBBI, dl, TII, ArgRegsSaveSize); |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 725 | } |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 726 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 727 | /// getFrameIndexReference - Provide a base+offset reference to an FI slot for |
| 728 | /// debug info. It's the same as what we use for resolving the code-gen |
| 729 | /// references for now. FIXME: This can go wrong when references are |
| 730 | /// SP-relative and simple call frames aren't used. |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 731 | int |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 732 | ARMFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 733 | unsigned &FrameReg) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 734 | return ResolveFrameIndexReference(MF, FI, FrameReg, 0); |
| 735 | } |
| 736 | |
| 737 | int |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 738 | ARMFrameLowering::ResolveFrameIndexReference(const MachineFunction &MF, |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 739 | int FI, unsigned &FrameReg, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 740 | int SPAdj) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 741 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 742 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 743 | MF.getSubtarget().getRegisterInfo()); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 744 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 745 | int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize(); |
| 746 | int FPOffset = Offset - AFI->getFramePtrSpillOffset(); |
| 747 | bool isFixed = MFI->isFixedObjectIndex(FI); |
| 748 | |
| 749 | FrameReg = ARM::SP; |
| 750 | Offset += SPAdj; |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 751 | |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 752 | // SP can move around if there are allocas. We may also lose track of SP |
| 753 | // when emergency spilling inside a non-reserved call frame setup. |
Bob Wilson | ca69032 | 2012-03-20 19:28:22 +0000 | [diff] [blame] | 754 | bool hasMovingSP = !hasReservedCallFrame(MF); |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 755 | |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 756 | // When dynamically realigning the stack, use the frame pointer for |
| 757 | // parameters, and the stack/base pointer for locals. |
| 758 | if (RegInfo->needsStackRealignment(MF)) { |
| 759 | assert (hasFP(MF) && "dynamic stack realignment without a FP!"); |
| 760 | if (isFixed) { |
| 761 | FrameReg = RegInfo->getFrameRegister(MF); |
| 762 | Offset = FPOffset; |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 763 | } else if (hasMovingSP) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 764 | assert(RegInfo->hasBasePointer(MF) && |
| 765 | "VLAs and dynamic stack alignment, but missing base pointer!"); |
| 766 | FrameReg = RegInfo->getBaseRegister(); |
| 767 | } |
| 768 | return Offset; |
| 769 | } |
| 770 | |
| 771 | // If there is a frame pointer, use it when we can. |
| 772 | if (hasFP(MF) && AFI->hasStackFrame()) { |
| 773 | // Use frame pointer to reference fixed objects. Use it for locals if |
| 774 | // there are VLAs (and thus the SP isn't reliable as a base). |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 775 | if (isFixed || (hasMovingSP && !RegInfo->hasBasePointer(MF))) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 776 | FrameReg = RegInfo->getFrameRegister(MF); |
| 777 | return FPOffset; |
Jakob Stoklund Olesen | 92c15b2 | 2012-02-28 01:15:01 +0000 | [diff] [blame] | 778 | } else if (hasMovingSP) { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 779 | assert(RegInfo->hasBasePointer(MF) && "missing base pointer!"); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 780 | if (AFI->isThumb2Function()) { |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 781 | // Try to use the frame pointer if we can, else use the base pointer |
| 782 | // since it's available. This is handy for the emergency spill slot, in |
| 783 | // particular. |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 784 | if (FPOffset >= -255 && FPOffset < 0) { |
| 785 | FrameReg = RegInfo->getFrameRegister(MF); |
| 786 | return FPOffset; |
| 787 | } |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 788 | } |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 789 | } else if (AFI->isThumb2Function()) { |
Andrew Trick | f7ecc16 | 2011-08-25 17:40:54 +0000 | [diff] [blame] | 790 | // Use add <rd>, sp, #<imm8> |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 791 | // ldr <rd>, [sp, #<imm8>] |
| 792 | // if at all possible to save space. |
| 793 | if (Offset >= 0 && (Offset & 3) == 0 && Offset <= 1020) |
| 794 | return Offset; |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 795 | // In Thumb2 mode, the negative offset is very limited. Try to avoid |
Evan Cheng | c0d2004 | 2011-04-22 01:42:52 +0000 | [diff] [blame] | 796 | // out of range references. ldr <rt>,[<rn>, #-<imm8>] |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 797 | if (FPOffset >= -255 && FPOffset < 0) { |
| 798 | FrameReg = RegInfo->getFrameRegister(MF); |
| 799 | return FPOffset; |
| 800 | } |
| 801 | } else if (Offset > (FPOffset < 0 ? -FPOffset : FPOffset)) { |
| 802 | // Otherwise, use SP or FP, whichever is closer to the stack slot. |
| 803 | FrameReg = RegInfo->getFrameRegister(MF); |
| 804 | return FPOffset; |
| 805 | } |
| 806 | } |
| 807 | // Use the base pointer if we have one. |
| 808 | if (RegInfo->hasBasePointer(MF)) |
| 809 | FrameReg = RegInfo->getBaseRegister(); |
| 810 | return Offset; |
| 811 | } |
| 812 | |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 813 | int ARMFrameLowering::getFrameIndexOffset(const MachineFunction &MF, |
| 814 | int FI) const { |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 815 | unsigned FrameReg; |
| 816 | return getFrameIndexReference(MF, FI, FrameReg); |
| 817 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 818 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 819 | void ARMFrameLowering::emitPushInst(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 820 | MachineBasicBlock::iterator MI, |
| 821 | const std::vector<CalleeSavedInfo> &CSI, |
| 822 | unsigned StmOpc, unsigned StrOpc, |
| 823 | bool NoGap, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 824 | bool(*Func)(unsigned, bool), |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 825 | unsigned NumAlignedDPRCS2Regs, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 826 | unsigned MIFlags) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 827 | MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 828 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 829 | |
| 830 | DebugLoc DL; |
| 831 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 832 | |
Evan Cheng | c27c956 | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 833 | SmallVector<std::pair<unsigned,bool>, 4> Regs; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 834 | unsigned i = CSI.size(); |
| 835 | while (i != 0) { |
| 836 | unsigned LastReg = 0; |
| 837 | for (; i != 0; --i) { |
| 838 | unsigned Reg = CSI[i-1].getReg(); |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 839 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 840 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 841 | // D-registers in the aligned area DPRCS2 are NOT spilled here. |
| 842 | if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) |
| 843 | continue; |
| 844 | |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 845 | // Add the callee-saved register as live-in unless it's LR and |
Jim Grosbach | c0b669f | 2010-12-09 16:14:46 +0000 | [diff] [blame] | 846 | // @llvm.returnaddress is called. If LR is returned for |
| 847 | // @llvm.returnaddress then it's already added to the function and |
| 848 | // entry block live-in sets. |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 849 | bool isKill = true; |
| 850 | if (Reg == ARM::LR) { |
| 851 | if (MF.getFrameInfo()->isReturnAddressTaken() && |
| 852 | MF.getRegInfo().isLiveIn(Reg)) |
| 853 | isKill = false; |
| 854 | } |
| 855 | |
| 856 | if (isKill) |
| 857 | MBB.addLiveIn(Reg); |
| 858 | |
Eric Christopher | 2a2e65c | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 859 | // If NoGap is true, push consecutive registers and then leave the rest |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 860 | // for other instructions. e.g. |
Eric Christopher | 2a2e65c | 2010-12-09 01:57:45 +0000 | [diff] [blame] | 861 | // vpush {d8, d10, d11} -> vpush {d8}, vpush {d10, d11} |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 862 | if (NoGap && LastReg && LastReg != Reg-1) |
| 863 | break; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 864 | LastReg = Reg; |
| 865 | Regs.push_back(std::make_pair(Reg, isKill)); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 868 | if (Regs.empty()) |
| 869 | continue; |
| 870 | if (Regs.size() > 1 || StrOpc== 0) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 871 | MachineInstrBuilder MIB = |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 872 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(StmOpc), ARM::SP) |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 873 | .addReg(ARM::SP).setMIFlags(MIFlags)); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 874 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 875 | MIB.addReg(Regs[i].first, getKillRegState(Regs[i].second)); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 876 | } else if (Regs.size() == 1) { |
| 877 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc), |
| 878 | ARM::SP) |
| 879 | .addReg(Regs[0].first, getKillRegState(Regs[0].second)) |
Jim Grosbach | f0c95ca | 2011-08-05 20:35:44 +0000 | [diff] [blame] | 880 | .addReg(ARM::SP).setMIFlags(MIFlags) |
| 881 | .addImm(-4); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 882 | AddDefaultPred(MIB); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 883 | } |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 884 | Regs.clear(); |
Tim Northover | 3cccc45 | 2014-03-12 11:29:23 +0000 | [diff] [blame] | 885 | |
| 886 | // Put any subsequent vpush instructions before this one: they will refer to |
| 887 | // higher register numbers so need to be pushed first in order to preserve |
| 888 | // monotonicity. |
| 889 | --MI; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 890 | } |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 891 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 892 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 893 | void ARMFrameLowering::emitPopInst(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 894 | MachineBasicBlock::iterator MI, |
| 895 | const std::vector<CalleeSavedInfo> &CSI, |
| 896 | unsigned LdmOpc, unsigned LdrOpc, |
| 897 | bool isVarArg, bool NoGap, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 898 | bool(*Func)(unsigned, bool), |
| 899 | unsigned NumAlignedDPRCS2Regs) const { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 900 | MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 901 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 902 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 903 | DebugLoc DL = MI->getDebugLoc(); |
Evan Cheng | d6093ff | 2011-01-25 01:28:33 +0000 | [diff] [blame] | 904 | unsigned RetOpcode = MI->getOpcode(); |
| 905 | bool isTailCall = (RetOpcode == ARM::TCRETURNdi || |
Jakob Stoklund Olesen | b4bd388 | 2012-04-06 21:17:42 +0000 | [diff] [blame] | 906 | RetOpcode == ARM::TCRETURNri); |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 907 | bool isInterrupt = |
| 908 | RetOpcode == ARM::SUBS_PC_LR || RetOpcode == ARM::t2SUBS_PC_LR; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 909 | |
| 910 | SmallVector<unsigned, 4> Regs; |
| 911 | unsigned i = CSI.size(); |
| 912 | while (i != 0) { |
| 913 | unsigned LastReg = 0; |
| 914 | bool DeleteRet = false; |
| 915 | for (; i != 0; --i) { |
| 916 | unsigned Reg = CSI[i-1].getReg(); |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 917 | if (!(Func)(Reg, STI.isTargetDarwin())) continue; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 918 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 919 | // The aligned reloads from area DPRCS2 are not inserted here. |
| 920 | if (Reg >= ARM::D8 && Reg < ARM::D8 + NumAlignedDPRCS2Regs) |
| 921 | continue; |
| 922 | |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 923 | if (Reg == ARM::LR && !isTailCall && !isVarArg && !isInterrupt && |
| 924 | STI.hasV5TOps()) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 925 | Reg = ARM::PC; |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 926 | LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_RET : ARM::LDMIA_RET; |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 927 | // Fold the return instruction into the LDM. |
| 928 | DeleteRet = true; |
| 929 | } |
| 930 | |
Evan Cheng | 9d54ae6 | 2010-12-08 06:29:02 +0000 | [diff] [blame] | 931 | // If NoGap is true, pop consecutive registers and then leave the rest |
| 932 | // for other instructions. e.g. |
| 933 | // vpop {d8, d10, d11} -> vpop {d8}, vpop {d10, d11} |
| 934 | if (NoGap && LastReg && LastReg != Reg-1) |
| 935 | break; |
| 936 | |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 937 | LastReg = Reg; |
| 938 | Regs.push_back(Reg); |
| 939 | } |
| 940 | |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 941 | if (Regs.empty()) |
| 942 | continue; |
| 943 | if (Regs.size() > 1 || LdrOpc == 0) { |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 944 | MachineInstrBuilder MIB = |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 945 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(LdmOpc), ARM::SP) |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 946 | .addReg(ARM::SP)); |
| 947 | for (unsigned i = 0, e = Regs.size(); i < e; ++i) |
| 948 | MIB.addReg(Regs[i], getDefRegState(true)); |
Andrew Trick | 6446bf7 | 2011-08-25 17:50:53 +0000 | [diff] [blame] | 949 | if (DeleteRet) { |
Jakob Stoklund Olesen | 33f5d14 | 2012-12-20 22:54:02 +0000 | [diff] [blame] | 950 | MIB.copyImplicitOps(&*MI); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 951 | MI->eraseFromParent(); |
Andrew Trick | 6446bf7 | 2011-08-25 17:50:53 +0000 | [diff] [blame] | 952 | } |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 953 | MI = MIB; |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 954 | } else if (Regs.size() == 1) { |
| 955 | // If we adjusted the reg to PC from LR above, switch it back here. We |
| 956 | // only do that for LDM. |
| 957 | if (Regs[0] == ARM::PC) |
| 958 | Regs[0] = ARM::LR; |
| 959 | MachineInstrBuilder MIB = |
| 960 | BuildMI(MBB, MI, DL, TII.get(LdrOpc), Regs[0]) |
| 961 | .addReg(ARM::SP, RegState::Define) |
| 962 | .addReg(ARM::SP); |
| 963 | // ARM mode needs an extra reg0 here due to addrmode2. Will go away once |
| 964 | // that refactoring is complete (eventually). |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 965 | if (LdrOpc == ARM::LDR_POST_REG || LdrOpc == ARM::LDR_POST_IMM) { |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 966 | MIB.addReg(0); |
| 967 | MIB.addImm(ARM_AM::getAM2Opc(ARM_AM::add, 4, ARM_AM::no_shift)); |
| 968 | } else |
| 969 | MIB.addImm(4); |
| 970 | AddDefaultPred(MIB); |
Evan Cheng | 775ead3 | 2010-12-07 23:08:38 +0000 | [diff] [blame] | 971 | } |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 972 | Regs.clear(); |
Tim Northover | 3cccc45 | 2014-03-12 11:29:23 +0000 | [diff] [blame] | 973 | |
| 974 | // Put any subsequent vpop instructions after this one: they will refer to |
| 975 | // higher register numbers so need to be popped afterwards. |
| 976 | ++MI; |
Evan Cheng | c27c956 | 2010-12-07 19:59:34 +0000 | [diff] [blame] | 977 | } |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 980 | /// Emit aligned spill instructions for NumAlignedDPRCS2Regs D-registers |
Jakob Stoklund Olesen | 103318e | 2011-12-24 04:17:01 +0000 | [diff] [blame] | 981 | /// starting from d8. Also insert stack realignment code and leave the stack |
| 982 | /// pointer pointing to the d8 spill slot. |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 983 | static void emitAlignedDPRCS2Spills(MachineBasicBlock &MBB, |
| 984 | MachineBasicBlock::iterator MI, |
| 985 | unsigned NumAlignedDPRCS2Regs, |
| 986 | const std::vector<CalleeSavedInfo> &CSI, |
| 987 | const TargetRegisterInfo *TRI) { |
| 988 | MachineFunction &MF = *MBB.getParent(); |
| 989 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 990 | DebugLoc DL = MI->getDebugLoc(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 991 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 992 | MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 993 | |
| 994 | // Mark the D-register spill slots as properly aligned. Since MFI computes |
| 995 | // stack slot layout backwards, this can actually mean that the d-reg stack |
| 996 | // slot offsets can be wrong. The offset for d8 will always be correct. |
| 997 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { |
| 998 | unsigned DNum = CSI[i].getReg() - ARM::D8; |
| 999 | if (DNum >= 8) |
| 1000 | continue; |
| 1001 | int FI = CSI[i].getFrameIdx(); |
| 1002 | // The even-numbered registers will be 16-byte aligned, the odd-numbered |
| 1003 | // registers will be 8-byte aligned. |
| 1004 | MFI.setObjectAlignment(FI, DNum % 2 ? 8 : 16); |
| 1005 | |
| 1006 | // The stack slot for D8 needs to be maximally aligned because this is |
| 1007 | // actually the point where we align the stack pointer. MachineFrameInfo |
| 1008 | // computes all offsets relative to the incoming stack pointer which is a |
| 1009 | // bit weird when realigning the stack. Any extra padding for this |
| 1010 | // over-alignment is not realized because the code inserted below adjusts |
| 1011 | // the stack pointer by numregs * 8 before aligning the stack pointer. |
| 1012 | if (DNum == 0) |
| 1013 | MFI.setObjectAlignment(FI, MFI.getMaxAlignment()); |
| 1014 | } |
| 1015 | |
| 1016 | // Move the stack pointer to the d8 spill slot, and align it at the same |
| 1017 | // time. Leave the stack slot address in the scratch register r4. |
| 1018 | // |
| 1019 | // sub r4, sp, #numregs * 8 |
| 1020 | // bic r4, r4, #align - 1 |
| 1021 | // mov sp, r4 |
| 1022 | // |
| 1023 | bool isThumb = AFI->isThumbFunction(); |
| 1024 | assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); |
| 1025 | AFI->setShouldRestoreSPFromFP(true); |
| 1026 | |
| 1027 | // sub r4, sp, #numregs * 8 |
| 1028 | // The immediate is <= 64, so it doesn't need any special encoding. |
| 1029 | unsigned Opc = isThumb ? ARM::t2SUBri : ARM::SUBri; |
| 1030 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) |
| 1031 | .addReg(ARM::SP) |
| 1032 | .addImm(8 * NumAlignedDPRCS2Regs))); |
| 1033 | |
| 1034 | // bic r4, r4, #align-1 |
| 1035 | Opc = isThumb ? ARM::t2BICri : ARM::BICri; |
| 1036 | unsigned MaxAlign = MF.getFrameInfo()->getMaxAlignment(); |
| 1037 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) |
| 1038 | .addReg(ARM::R4, RegState::Kill) |
| 1039 | .addImm(MaxAlign - 1))); |
| 1040 | |
| 1041 | // mov sp, r4 |
| 1042 | // The stack pointer must be adjusted before spilling anything, otherwise |
| 1043 | // the stack slots could be clobbered by an interrupt handler. |
| 1044 | // Leave r4 live, it is used below. |
| 1045 | Opc = isThumb ? ARM::tMOVr : ARM::MOVr; |
| 1046 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(Opc), ARM::SP) |
| 1047 | .addReg(ARM::R4); |
| 1048 | MIB = AddDefaultPred(MIB); |
| 1049 | if (!isThumb) |
| 1050 | AddDefaultCC(MIB); |
| 1051 | |
| 1052 | // Now spill NumAlignedDPRCS2Regs registers starting from d8. |
| 1053 | // r4 holds the stack slot address. |
| 1054 | unsigned NextReg = ARM::D8; |
| 1055 | |
| 1056 | // 16-byte aligned vst1.64 with 4 d-regs and address writeback. |
| 1057 | // The writeback is only needed when emitting two vst1.64 instructions. |
| 1058 | if (NumAlignedDPRCS2Regs >= 6) { |
| 1059 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1060 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1061 | MBB.addLiveIn(SupReg); |
| 1062 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Qwb_fixed), |
| 1063 | ARM::R4) |
| 1064 | .addReg(ARM::R4, RegState::Kill).addImm(16) |
| 1065 | .addReg(NextReg) |
| 1066 | .addReg(SupReg, RegState::ImplicitKill)); |
| 1067 | NextReg += 4; |
| 1068 | NumAlignedDPRCS2Regs -= 4; |
| 1069 | } |
| 1070 | |
| 1071 | // We won't modify r4 beyond this point. It currently points to the next |
| 1072 | // register to be spilled. |
| 1073 | unsigned R4BaseReg = NextReg; |
| 1074 | |
| 1075 | // 16-byte aligned vst1.64 with 4 d-regs, no writeback. |
| 1076 | if (NumAlignedDPRCS2Regs >= 4) { |
| 1077 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1078 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1079 | MBB.addLiveIn(SupReg); |
| 1080 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1d64Q)) |
| 1081 | .addReg(ARM::R4).addImm(16).addReg(NextReg) |
| 1082 | .addReg(SupReg, RegState::ImplicitKill)); |
| 1083 | NextReg += 4; |
| 1084 | NumAlignedDPRCS2Regs -= 4; |
| 1085 | } |
| 1086 | |
| 1087 | // 16-byte aligned vst1.64 with 2 d-regs. |
| 1088 | if (NumAlignedDPRCS2Regs >= 2) { |
| 1089 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1090 | &ARM::QPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1091 | MBB.addLiveIn(SupReg); |
| 1092 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VST1q64)) |
Jim Grosbach | c988e0c | 2012-03-05 19:33:30 +0000 | [diff] [blame] | 1093 | .addReg(ARM::R4).addImm(16).addReg(SupReg)); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1094 | NextReg += 2; |
| 1095 | NumAlignedDPRCS2Regs -= 2; |
| 1096 | } |
| 1097 | |
| 1098 | // Finally, use a vanilla vstr.64 for the odd last register. |
| 1099 | if (NumAlignedDPRCS2Regs) { |
| 1100 | MBB.addLiveIn(NextReg); |
| 1101 | // vstr.64 uses addrmode5 which has an offset scale of 4. |
| 1102 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VSTRD)) |
| 1103 | .addReg(NextReg) |
| 1104 | .addReg(ARM::R4).addImm((NextReg-R4BaseReg)*2)); |
| 1105 | } |
| 1106 | |
| 1107 | // The last spill instruction inserted should kill the scratch register r4. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1108 | std::prev(MI)->addRegisterKilled(ARM::R4, TRI); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /// Skip past the code inserted by emitAlignedDPRCS2Spills, and return an |
| 1112 | /// iterator to the following instruction. |
| 1113 | static MachineBasicBlock::iterator |
| 1114 | skipAlignedDPRCS2Spills(MachineBasicBlock::iterator MI, |
| 1115 | unsigned NumAlignedDPRCS2Regs) { |
| 1116 | // sub r4, sp, #numregs * 8 |
| 1117 | // bic r4, r4, #align - 1 |
| 1118 | // mov sp, r4 |
| 1119 | ++MI; ++MI; ++MI; |
| 1120 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1121 | |
| 1122 | // These switches all fall through. |
| 1123 | switch(NumAlignedDPRCS2Regs) { |
| 1124 | case 7: |
| 1125 | ++MI; |
| 1126 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1127 | default: |
| 1128 | ++MI; |
| 1129 | assert(MI->mayStore() && "Expecting spill instruction"); |
| 1130 | case 1: |
| 1131 | case 2: |
| 1132 | case 4: |
| 1133 | assert(MI->killsRegister(ARM::R4) && "Missed kill flag"); |
| 1134 | ++MI; |
| 1135 | } |
| 1136 | return MI; |
| 1137 | } |
| 1138 | |
| 1139 | /// Emit aligned reload instructions for NumAlignedDPRCS2Regs D-registers |
| 1140 | /// starting from d8. These instructions are assumed to execute while the |
| 1141 | /// stack is still aligned, unlike the code inserted by emitPopInst. |
| 1142 | static void emitAlignedDPRCS2Restores(MachineBasicBlock &MBB, |
| 1143 | MachineBasicBlock::iterator MI, |
| 1144 | unsigned NumAlignedDPRCS2Regs, |
| 1145 | const std::vector<CalleeSavedInfo> &CSI, |
| 1146 | const TargetRegisterInfo *TRI) { |
| 1147 | MachineFunction &MF = *MBB.getParent(); |
| 1148 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1149 | DebugLoc DL = MI->getDebugLoc(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1150 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Find the frame index assigned to d8. |
| 1153 | int D8SpillFI = 0; |
| 1154 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) |
| 1155 | if (CSI[i].getReg() == ARM::D8) { |
| 1156 | D8SpillFI = CSI[i].getFrameIdx(); |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | // Materialize the address of the d8 spill slot into the scratch register r4. |
| 1161 | // This can be fairly complicated if the stack frame is large, so just use |
| 1162 | // the normal frame index elimination mechanism to do it. This code runs as |
| 1163 | // the initial part of the epilog where the stack and base pointers haven't |
| 1164 | // been changed yet. |
| 1165 | bool isThumb = AFI->isThumbFunction(); |
| 1166 | assert(!AFI->isThumb1OnlyFunction() && "Can't realign stack for thumb1"); |
| 1167 | |
| 1168 | unsigned Opc = isThumb ? ARM::t2ADDri : ARM::ADDri; |
| 1169 | AddDefaultCC(AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(Opc), ARM::R4) |
| 1170 | .addFrameIndex(D8SpillFI).addImm(0))); |
| 1171 | |
| 1172 | // Now restore NumAlignedDPRCS2Regs registers starting from d8. |
| 1173 | unsigned NextReg = ARM::D8; |
| 1174 | |
| 1175 | // 16-byte aligned vld1.64 with 4 d-regs and writeback. |
| 1176 | if (NumAlignedDPRCS2Regs >= 6) { |
| 1177 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1178 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1179 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Qwb_fixed), NextReg) |
| 1180 | .addReg(ARM::R4, RegState::Define) |
| 1181 | .addReg(ARM::R4, RegState::Kill).addImm(16) |
| 1182 | .addReg(SupReg, RegState::ImplicitDefine)); |
| 1183 | NextReg += 4; |
| 1184 | NumAlignedDPRCS2Regs -= 4; |
| 1185 | } |
| 1186 | |
| 1187 | // We won't modify r4 beyond this point. It currently points to the next |
| 1188 | // register to be spilled. |
| 1189 | unsigned R4BaseReg = NextReg; |
| 1190 | |
| 1191 | // 16-byte aligned vld1.64 with 4 d-regs, no writeback. |
| 1192 | if (NumAlignedDPRCS2Regs >= 4) { |
| 1193 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1194 | &ARM::QQPRRegClass); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1195 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1d64Q), NextReg) |
| 1196 | .addReg(ARM::R4).addImm(16) |
| 1197 | .addReg(SupReg, RegState::ImplicitDefine)); |
| 1198 | NextReg += 4; |
| 1199 | NumAlignedDPRCS2Regs -= 4; |
| 1200 | } |
| 1201 | |
| 1202 | // 16-byte aligned vld1.64 with 2 d-regs. |
| 1203 | if (NumAlignedDPRCS2Regs >= 2) { |
| 1204 | unsigned SupReg = TRI->getMatchingSuperReg(NextReg, ARM::dsub_0, |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1205 | &ARM::QPRRegClass); |
Jim Grosbach | c988e0c | 2012-03-05 19:33:30 +0000 | [diff] [blame] | 1206 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLD1q64), SupReg) |
| 1207 | .addReg(ARM::R4).addImm(16)); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1208 | NextReg += 2; |
| 1209 | NumAlignedDPRCS2Regs -= 2; |
| 1210 | } |
| 1211 | |
| 1212 | // Finally, use a vanilla vldr.64 for the remaining odd register. |
| 1213 | if (NumAlignedDPRCS2Regs) |
| 1214 | AddDefaultPred(BuildMI(MBB, MI, DL, TII.get(ARM::VLDRD), NextReg) |
| 1215 | .addReg(ARM::R4).addImm(2*(NextReg-R4BaseReg))); |
| 1216 | |
| 1217 | // Last store kills r4. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1218 | std::prev(MI)->addRegisterKilled(ARM::R4, TRI); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1221 | bool ARMFrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1222 | MachineBasicBlock::iterator MI, |
| 1223 | const std::vector<CalleeSavedInfo> &CSI, |
| 1224 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1225 | if (CSI.empty()) |
| 1226 | return false; |
| 1227 | |
| 1228 | MachineFunction &MF = *MBB.getParent(); |
| 1229 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1230 | |
| 1231 | unsigned PushOpc = AFI->isThumbFunction() ? ARM::t2STMDB_UPD : ARM::STMDB_UPD; |
Jim Grosbach | 05dec8b1 | 2011-09-02 18:46:15 +0000 | [diff] [blame] | 1232 | unsigned PushOneOpc = AFI->isThumbFunction() ? |
| 1233 | ARM::t2STR_PRE : ARM::STR_PRE_IMM; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1234 | unsigned FltOpc = ARM::VSTMDDB_UPD; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1235 | unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); |
| 1236 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea1Register, 0, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 1237 | MachineInstr::FrameSetup); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1238 | emitPushInst(MBB, MI, CSI, PushOpc, PushOneOpc, false, &isARMArea2Register, 0, |
Anton Korobeynikov | e7410dd | 2011-03-05 18:43:32 +0000 | [diff] [blame] | 1239 | MachineInstr::FrameSetup); |
| 1240 | emitPushInst(MBB, MI, CSI, FltOpc, 0, true, &isARMArea3Register, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1241 | NumAlignedDPRCS2Regs, MachineInstr::FrameSetup); |
| 1242 | |
| 1243 | // The code above does not insert spill code for the aligned DPRCS2 registers. |
| 1244 | // The stack realignment code will be inserted between the push instructions |
| 1245 | // and these spills. |
| 1246 | if (NumAlignedDPRCS2Regs) |
| 1247 | emitAlignedDPRCS2Spills(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1248 | |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1252 | bool ARMFrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1253 | MachineBasicBlock::iterator MI, |
| 1254 | const std::vector<CalleeSavedInfo> &CSI, |
| 1255 | const TargetRegisterInfo *TRI) const { |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1256 | if (CSI.empty()) |
| 1257 | return false; |
| 1258 | |
| 1259 | MachineFunction &MF = *MBB.getParent(); |
| 1260 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 1261 | bool isVarArg = AFI->getArgRegsSaveSize() > 0; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1262 | unsigned NumAlignedDPRCS2Regs = AFI->getNumAlignedDPRCS2Regs(); |
| 1263 | |
| 1264 | // The emitPopInst calls below do not insert reloads for the aligned DPRCS2 |
| 1265 | // registers. Do that here instead. |
| 1266 | if (NumAlignedDPRCS2Regs) |
| 1267 | emitAlignedDPRCS2Restores(MBB, MI, NumAlignedDPRCS2Regs, CSI, TRI); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1268 | |
| 1269 | unsigned PopOpc = AFI->isThumbFunction() ? ARM::t2LDMIA_UPD : ARM::LDMIA_UPD; |
Jim Grosbach | 05dec8b1 | 2011-09-02 18:46:15 +0000 | [diff] [blame] | 1270 | unsigned LdrOpc = AFI->isThumbFunction() ? ARM::t2LDR_POST :ARM::LDR_POST_IMM; |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1271 | unsigned FltOpc = ARM::VLDMDIA_UPD; |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1272 | emitPopInst(MBB, MI, CSI, FltOpc, 0, isVarArg, true, &isARMArea3Register, |
| 1273 | NumAlignedDPRCS2Regs); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1274 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1275 | &isARMArea2Register, 0); |
Jim Grosbach | 5fccad8 | 2010-12-09 18:31:13 +0000 | [diff] [blame] | 1276 | emitPopInst(MBB, MI, CSI, PopOpc, LdrOpc, isVarArg, false, |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1277 | &isARMArea1Register, 0); |
Anton Korobeynikov | d08fbd1 | 2010-11-27 23:05:03 +0000 | [diff] [blame] | 1278 | |
| 1279 | return true; |
| 1280 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1281 | |
| 1282 | // FIXME: Make generic? |
| 1283 | static unsigned GetFunctionSizeInBytes(const MachineFunction &MF, |
| 1284 | const ARMBaseInstrInfo &TII) { |
| 1285 | unsigned FnSize = 0; |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1286 | for (auto &MBB : MF) { |
| 1287 | for (auto &MI : MBB) |
| 1288 | FnSize += TII.GetInstSizeInBytes(&MI); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1289 | } |
| 1290 | return FnSize; |
| 1291 | } |
| 1292 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1293 | /// estimateRSStackSizeLimit - Look at each instruction that references stack |
| 1294 | /// frames and return the stack size limit beyond which some of these |
| 1295 | /// instructions will require a scratch register during their expansion later. |
| 1296 | // FIXME: Move to TII? |
| 1297 | static unsigned estimateRSStackSizeLimit(MachineFunction &MF, |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1298 | const TargetFrameLowering *TFI) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1299 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1300 | unsigned Limit = (1 << 12) - 1; |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1301 | for (auto &MBB : MF) { |
| 1302 | for (auto &MI : MBB) { |
| 1303 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1304 | if (!MI.getOperand(i).isFI()) |
| 1305 | continue; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1306 | |
| 1307 | // When using ADDri to get the address of a stack object, 255 is the |
| 1308 | // largest offset guaranteed to fit in the immediate offset. |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1309 | if (MI.getOpcode() == ARM::ADDri) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1310 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1311 | break; |
| 1312 | } |
| 1313 | |
| 1314 | // Otherwise check the addressing mode. |
Jim Grosbach | f92e8f5 | 2014-04-04 02:10:55 +0000 | [diff] [blame] | 1315 | switch (MI.getDesc().TSFlags & ARMII::AddrModeMask) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1316 | case ARMII::AddrMode3: |
| 1317 | case ARMII::AddrModeT2_i8: |
| 1318 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1319 | break; |
| 1320 | case ARMII::AddrMode5: |
| 1321 | case ARMII::AddrModeT2_i8s4: |
| 1322 | Limit = std::min(Limit, ((1U << 8) - 1) * 4); |
| 1323 | break; |
| 1324 | case ARMII::AddrModeT2_i12: |
| 1325 | // i12 supports only positive offset so these will be converted to |
| 1326 | // i8 opcodes. See llvm::rewriteT2FrameIndex. |
| 1327 | if (TFI->hasFP(MF) && AFI->hasStackFrame()) |
| 1328 | Limit = std::min(Limit, (1U << 8) - 1); |
| 1329 | break; |
| 1330 | case ARMII::AddrMode4: |
| 1331 | case ARMII::AddrMode6: |
| 1332 | // Addressing modes 4 & 6 (load/store) instructions can't encode an |
| 1333 | // immediate offset for stack references. |
| 1334 | return 0; |
| 1335 | default: |
| 1336 | break; |
| 1337 | } |
| 1338 | break; // At most one FI per instruction |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | return Limit; |
| 1344 | } |
| 1345 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1346 | // In functions that realign the stack, it can be an advantage to spill the |
| 1347 | // callee-saved vector registers after realigning the stack. The vst1 and vld1 |
| 1348 | // instructions take alignment hints that can improve performance. |
| 1349 | // |
| 1350 | static void checkNumAlignedDPRCS2Regs(MachineFunction &MF) { |
| 1351 | MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(0); |
| 1352 | if (!SpillAlignedNEONRegs) |
| 1353 | return; |
| 1354 | |
| 1355 | // Naked functions don't spill callee-saved registers. |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 1356 | if (MF.getFunction()->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 1357 | Attribute::Naked)) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1358 | return; |
| 1359 | |
| 1360 | // We are planning to use NEON instructions vst1 / vld1. |
| 1361 | if (!MF.getTarget().getSubtarget<ARMSubtarget>().hasNEON()) |
| 1362 | return; |
| 1363 | |
| 1364 | // Don't bother if the default stack alignment is sufficiently high. |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1365 | if (MF.getTarget() |
| 1366 | .getSubtargetImpl() |
| 1367 | ->getFrameLowering() |
| 1368 | ->getStackAlignment() >= 8) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1369 | return; |
| 1370 | |
| 1371 | // Aligned spills require stack realignment. |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1372 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1373 | MF.getSubtarget().getRegisterInfo()); |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1374 | if (!RegInfo->canRealignStack(MF)) |
| 1375 | return; |
| 1376 | |
| 1377 | // We always spill contiguous d-registers starting from d8. Count how many |
| 1378 | // needs spilling. The register allocator will almost always use the |
| 1379 | // callee-saved registers in order, but it can happen that there are holes in |
| 1380 | // the range. Registers above the hole will be spilled to the standard DPRCS |
| 1381 | // area. |
| 1382 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 1383 | unsigned NumSpills = 0; |
| 1384 | for (; NumSpills < 8; ++NumSpills) |
Jakob Stoklund Olesen | 0736442 | 2012-10-17 18:44:18 +0000 | [diff] [blame] | 1385 | if (!MRI.isPhysRegUsed(ARM::D8 + NumSpills)) |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | |
| 1388 | // Don't do this for just one d-register. It's not worth it. |
| 1389 | if (NumSpills < 2) |
| 1390 | return; |
| 1391 | |
| 1392 | // Spill the first NumSpills D-registers after realigning the stack. |
| 1393 | MF.getInfo<ARMFunctionInfo>()->setNumAlignedDPRCS2Regs(NumSpills); |
| 1394 | |
| 1395 | // A scratch register is required for the vst1 / vld1 instructions. |
| 1396 | MF.getRegInfo().setPhysRegUsed(ARM::R4); |
| 1397 | } |
| 1398 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1399 | void |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1400 | ARMFrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, |
Bob Wilson | 657f227 | 2011-01-13 21:10:12 +0000 | [diff] [blame] | 1401 | RegScavenger *RS) const { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1402 | // This tells PEI to spill the FP as if it is any other callee-save register |
| 1403 | // to take advantage the eliminateFrameIndex machinery. This also ensures it |
| 1404 | // is spilled in the order specified by getCalleeSavedRegs() to make it easier |
| 1405 | // to combine multiple loads / stores. |
| 1406 | bool CanEliminateFrame = true; |
| 1407 | bool CS1Spilled = false; |
| 1408 | bool LRSpilled = false; |
| 1409 | unsigned NumGPRSpills = 0; |
| 1410 | SmallVector<unsigned, 4> UnspilledCS1GPRs; |
| 1411 | SmallVector<unsigned, 4> UnspilledCS2GPRs; |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 1412 | const ARMBaseRegisterInfo *RegInfo = static_cast<const ARMBaseRegisterInfo *>( |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1413 | MF.getSubtarget().getRegisterInfo()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1414 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1415 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1416 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1417 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1418 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1419 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| 1420 | |
| 1421 | // Spill R4 if Thumb2 function requires stack realignment - it will be used as |
| 1422 | // scratch register. Also spill R4 if Thumb2 function has varsized objects, |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1423 | // since it's not always possible to restore sp from fp in a single |
| 1424 | // instruction. |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1425 | // FIXME: It will be better just to find spare register here. |
| 1426 | if (AFI->isThumb2Function() && |
| 1427 | (MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(MF))) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1428 | MRI.setPhysRegUsed(ARM::R4); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1429 | |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1430 | if (AFI->isThumb1OnlyFunction()) { |
| 1431 | // Spill LR if Thumb1 function uses variable length argument lists. |
Stepan Dyatkovskiy | f5aa83d | 2013-04-30 07:19:58 +0000 | [diff] [blame] | 1432 | if (AFI->getArgRegsSaveSize() > 0) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1433 | MRI.setPhysRegUsed(ARM::LR); |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1434 | |
Jim Grosbach | dca8531 | 2011-06-13 21:18:25 +0000 | [diff] [blame] | 1435 | // Spill R4 if Thumb1 epilogue has to restore SP from FP. We don't know |
| 1436 | // for sure what the stack size will be, but for this, an estimate is good |
| 1437 | // enough. If there anything changes it, it'll be a spill, which implies |
| 1438 | // we've used all the registers and so R4 is already used, so not marking |
Chad Rosier | add38c1 | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 1439 | // it here will be OK. |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1440 | // FIXME: It will be better just to find spare register here. |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 1441 | unsigned StackSize = MFI->estimateStackSize(MF); |
Chad Rosier | add38c1 | 2011-10-20 00:07:12 +0000 | [diff] [blame] | 1442 | if (MFI->hasVarSizedObjects() || StackSize > 508) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1443 | MRI.setPhysRegUsed(ARM::R4); |
Evan Cheng | 572756a | 2011-01-16 05:14:33 +0000 | [diff] [blame] | 1444 | } |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1445 | |
Jakob Stoklund Olesen | 0965585 | 2011-12-23 00:36:18 +0000 | [diff] [blame] | 1446 | // See if we can spill vector registers to aligned stack. |
| 1447 | checkNumAlignedDPRCS2Regs(MF); |
| 1448 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1449 | // Spill the BasePtr if it's used. |
| 1450 | if (RegInfo->hasBasePointer(MF)) |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1451 | MRI.setPhysRegUsed(RegInfo->getBaseRegister()); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Don't spill FP if the frame can be eliminated. This is determined |
| 1454 | // by scanning the callee-save registers to see if any is used. |
Craig Topper | 840beec | 2014-04-04 05:16:06 +0000 | [diff] [blame] | 1455 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1456 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 1457 | unsigned Reg = CSRegs[i]; |
| 1458 | bool Spilled = false; |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1459 | if (MRI.isPhysRegUsed(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1460 | Spilled = true; |
| 1461 | CanEliminateFrame = false; |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1464 | if (!ARM::GPRRegClass.contains(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1465 | continue; |
| 1466 | |
| 1467 | if (Spilled) { |
| 1468 | NumGPRSpills++; |
| 1469 | |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 1470 | if (!STI.isTargetDarwin()) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1471 | if (Reg == ARM::LR) |
| 1472 | LRSpilled = true; |
| 1473 | CS1Spilled = true; |
| 1474 | continue; |
| 1475 | } |
| 1476 | |
| 1477 | // Keep track if LR and any of R4, R5, R6, and R7 is spilled. |
| 1478 | switch (Reg) { |
| 1479 | case ARM::LR: |
| 1480 | LRSpilled = true; |
| 1481 | // Fallthrough |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1482 | case ARM::R0: case ARM::R1: |
| 1483 | case ARM::R2: case ARM::R3: |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1484 | case ARM::R4: case ARM::R5: |
| 1485 | case ARM::R6: case ARM::R7: |
| 1486 | CS1Spilled = true; |
| 1487 | break; |
| 1488 | default: |
| 1489 | break; |
| 1490 | } |
| 1491 | } else { |
Tim Northover | 86f60b7 | 2014-05-30 13:23:06 +0000 | [diff] [blame] | 1492 | if (!STI.isTargetDarwin()) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1493 | UnspilledCS1GPRs.push_back(Reg); |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | switch (Reg) { |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1498 | case ARM::R0: case ARM::R1: |
| 1499 | case ARM::R2: case ARM::R3: |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1500 | case ARM::R4: case ARM::R5: |
| 1501 | case ARM::R6: case ARM::R7: |
| 1502 | case ARM::LR: |
| 1503 | UnspilledCS1GPRs.push_back(Reg); |
| 1504 | break; |
| 1505 | default: |
| 1506 | UnspilledCS2GPRs.push_back(Reg); |
| 1507 | break; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | bool ForceLRSpill = false; |
| 1513 | if (!LRSpilled && AFI->isThumb1OnlyFunction()) { |
| 1514 | unsigned FnSize = GetFunctionSizeInBytes(MF, TII); |
| 1515 | // Force LR to be spilled if the Thumb function size is > 2048. This enables |
| 1516 | // use of BL to implement far jump. If it turns out that it's not needed |
| 1517 | // then the branch fix up path will undo it. |
| 1518 | if (FnSize >= (1 << 11)) { |
| 1519 | CanEliminateFrame = false; |
| 1520 | ForceLRSpill = true; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | // If any of the stack slot references may be out of range of an immediate |
| 1525 | // offset, make sure a register (or a spill slot) is available for the |
| 1526 | // register scavenger. Note that if we're indexing off the frame pointer, the |
| 1527 | // effective stack size is 4 bytes larger since the FP points to the stack |
| 1528 | // slot of the previous FP. Also, if we have variable sized objects in the |
| 1529 | // function, stack slot references will often be negative, and some of |
| 1530 | // our instructions are positive-offset only, so conservatively consider |
| 1531 | // that case to want a spill slot (or register) as well. Similarly, if |
| 1532 | // the function adjusts the stack pointer during execution and the |
| 1533 | // adjustments aren't already part of our stack size estimate, our offset |
| 1534 | // calculations may be off, so be conservative. |
| 1535 | // FIXME: We could add logic to be more precise about negative offsets |
| 1536 | // and which instructions will need a scratch register for them. Is it |
| 1537 | // worth the effort and added fragility? |
| 1538 | bool BigStack = |
| 1539 | (RS && |
Hal Finkel | 628ba12 | 2013-03-14 21:15:20 +0000 | [diff] [blame] | 1540 | (MFI->estimateStackSize(MF) + |
| 1541 | ((hasFP(MF) && AFI->hasStackFrame()) ? 4:0) >= |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1542 | estimateRSStackSizeLimit(MF, this))) |
| 1543 | || MFI->hasVarSizedObjects() |
| 1544 | || (MFI->adjustsStack() && !canSimplifyCallFramePseudos(MF)); |
| 1545 | |
| 1546 | bool ExtraCSSpill = false; |
| 1547 | if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) { |
| 1548 | AFI->setHasStackFrame(true); |
| 1549 | |
| 1550 | // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled. |
| 1551 | // Spill LR as well so we can fold BX_RET to the registers restore (LDM). |
| 1552 | if (!LRSpilled && CS1Spilled) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1553 | MRI.setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1554 | NumGPRSpills++; |
Tim Northover | d840745 | 2013-10-01 14:33:28 +0000 | [diff] [blame] | 1555 | SmallVectorImpl<unsigned>::iterator LRPos; |
| 1556 | LRPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), |
| 1557 | (unsigned)ARM::LR); |
| 1558 | if (LRPos != UnspilledCS1GPRs.end()) |
| 1559 | UnspilledCS1GPRs.erase(LRPos); |
| 1560 | |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1561 | ForceLRSpill = false; |
| 1562 | ExtraCSSpill = true; |
| 1563 | } |
| 1564 | |
| 1565 | if (hasFP(MF)) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1566 | MRI.setPhysRegUsed(FramePtr); |
Joerg Sonnenberger | 818e725 | 2014-05-06 20:43:01 +0000 | [diff] [blame] | 1567 | auto FPPos = std::find(UnspilledCS1GPRs.begin(), UnspilledCS1GPRs.end(), |
| 1568 | FramePtr); |
| 1569 | if (FPPos != UnspilledCS1GPRs.end()) |
| 1570 | UnspilledCS1GPRs.erase(FPPos); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1571 | NumGPRSpills++; |
| 1572 | } |
| 1573 | |
| 1574 | // If stack and double are 8-byte aligned and we are spilling an odd number |
| 1575 | // of GPRs, spill one extra callee save GPR so we won't have to pad between |
| 1576 | // the integer and double callee save areas. |
Anton Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1577 | unsigned TargetAlign = getStackAlignment(); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1578 | if (TargetAlign == 8 && (NumGPRSpills & 1)) { |
| 1579 | if (CS1Spilled && !UnspilledCS1GPRs.empty()) { |
| 1580 | for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) { |
| 1581 | unsigned Reg = UnspilledCS1GPRs[i]; |
| 1582 | // Don't spill high register if the function is thumb1 |
| 1583 | if (!AFI->isThumb1OnlyFunction() || |
| 1584 | isARMLowRegister(Reg) || Reg == ARM::LR) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1585 | MRI.setPhysRegUsed(Reg); |
| 1586 | if (!MRI.isReserved(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1587 | ExtraCSSpill = true; |
| 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | } else if (!UnspilledCS2GPRs.empty() && !AFI->isThumb1OnlyFunction()) { |
| 1592 | unsigned Reg = UnspilledCS2GPRs.front(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1593 | MRI.setPhysRegUsed(Reg); |
| 1594 | if (!MRI.isReserved(Reg)) |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1595 | ExtraCSSpill = true; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | // Estimate if we might need to scavenge a register at some point in order |
| 1600 | // to materialize a stack offset. If so, either spill one additional |
| 1601 | // callee-saved register or reserve a special spill slot to facilitate |
| 1602 | // register scavenging. Thumb1 needs a spill slot for stack pointer |
| 1603 | // adjustments also, even when the frame itself is small. |
| 1604 | if (BigStack && !ExtraCSSpill) { |
| 1605 | // If any non-reserved CS register isn't spilled, just spill one or two |
| 1606 | // extra. That should take care of it! |
| 1607 | unsigned NumExtras = TargetAlign / 4; |
| 1608 | SmallVector<unsigned, 2> Extras; |
| 1609 | while (NumExtras && !UnspilledCS1GPRs.empty()) { |
| 1610 | unsigned Reg = UnspilledCS1GPRs.back(); |
| 1611 | UnspilledCS1GPRs.pop_back(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1612 | if (!MRI.isReserved(Reg) && |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1613 | (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) || |
| 1614 | Reg == ARM::LR)) { |
| 1615 | Extras.push_back(Reg); |
| 1616 | NumExtras--; |
| 1617 | } |
| 1618 | } |
| 1619 | // For non-Thumb1 functions, also check for hi-reg CS registers |
| 1620 | if (!AFI->isThumb1OnlyFunction()) { |
| 1621 | while (NumExtras && !UnspilledCS2GPRs.empty()) { |
| 1622 | unsigned Reg = UnspilledCS2GPRs.back(); |
| 1623 | UnspilledCS2GPRs.pop_back(); |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1624 | if (!MRI.isReserved(Reg)) { |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1625 | Extras.push_back(Reg); |
| 1626 | NumExtras--; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | if (Extras.size() && NumExtras == 0) { |
| 1631 | for (unsigned i = 0, e = Extras.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1632 | MRI.setPhysRegUsed(Extras[i]); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } else if (!AFI->isThumb1OnlyFunction()) { |
| 1635 | // note: Thumb1 functions spill to R12, not the stack. Reserve a slot |
| 1636 | // closest to SP or frame pointer. |
Craig Topper | c7242e0 | 2012-04-20 07:30:17 +0000 | [diff] [blame] | 1637 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
Hal Finkel | 9e331c2 | 2013-03-22 23:32:27 +0000 | [diff] [blame] | 1638 | RS->addScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(), |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1639 | RC->getAlignment(), |
| 1640 | false)); |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | if (ForceLRSpill) { |
Jakob Stoklund Olesen | 410eae5 | 2012-10-26 21:43:05 +0000 | [diff] [blame] | 1646 | MRI.setPhysRegUsed(ARM::LR); |
Anton Korobeynikov | 7283b8d | 2010-11-27 23:05:25 +0000 | [diff] [blame] | 1647 | AFI->setLRIsSpilledForFarJump(true); |
| 1648 | } |
| 1649 | } |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1650 | |
| 1651 | |
| 1652 | void ARMFrameLowering:: |
| 1653 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 1654 | MachineBasicBlock::iterator I) const { |
| 1655 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1656 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Eli Bendersky | 8da8716 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 1657 | if (!hasReservedCallFrame(MF)) { |
| 1658 | // If we have alloca, convert as follows: |
| 1659 | // ADJCALLSTACKDOWN -> sub, sp, sp, amount |
| 1660 | // ADJCALLSTACKUP -> add, sp, sp, amount |
| 1661 | MachineInstr *Old = I; |
| 1662 | DebugLoc dl = Old->getDebugLoc(); |
| 1663 | unsigned Amount = Old->getOperand(0).getImm(); |
| 1664 | if (Amount != 0) { |
| 1665 | // We need to keep the stack aligned properly. To do this, we round the |
| 1666 | // amount of space needed for the outgoing arguments up to the next |
| 1667 | // alignment boundary. |
| 1668 | unsigned Align = getStackAlignment(); |
| 1669 | Amount = (Amount+Align-1)/Align*Align; |
| 1670 | |
| 1671 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 1672 | assert(!AFI->isThumb1OnlyFunction() && |
| 1673 | "This eliminateCallFramePseudoInstr does not support Thumb1!"); |
| 1674 | bool isARM = !AFI->isThumbFunction(); |
| 1675 | |
| 1676 | // Replace the pseudo instruction with a new instruction... |
| 1677 | unsigned Opc = Old->getOpcode(); |
| 1678 | int PIdx = Old->findFirstPredOperandIdx(); |
| 1679 | ARMCC::CondCodes Pred = (PIdx == -1) |
| 1680 | ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm(); |
| 1681 | if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) { |
| 1682 | // Note: PredReg is operand 2 for ADJCALLSTACKDOWN. |
| 1683 | unsigned PredReg = Old->getOperand(2).getReg(); |
| 1684 | emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, MachineInstr::NoFlags, |
| 1685 | Pred, PredReg); |
| 1686 | } else { |
| 1687 | // Note: PredReg is operand 3 for ADJCALLSTACKUP. |
| 1688 | unsigned PredReg = Old->getOperand(3).getReg(); |
| 1689 | assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP); |
| 1690 | emitSPUpdate(isARM, MBB, I, dl, TII, Amount, MachineInstr::NoFlags, |
| 1691 | Pred, PredReg); |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | MBB.erase(I); |
| 1696 | } |
| 1697 | |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1698 | /// Get the minimum constant for ARM that is greater than or equal to the |
| 1699 | /// argument. In ARM, constants can have any value that can be produced by |
| 1700 | /// rotating an 8-bit value to the right by an even number of bits within a |
| 1701 | /// 32-bit word. |
| 1702 | static uint32_t alignToARMConstant(uint32_t Value) { |
| 1703 | unsigned Shifted = 0; |
| 1704 | |
| 1705 | if (Value == 0) |
| 1706 | return 0; |
| 1707 | |
| 1708 | while (!(Value & 0xC0000000)) { |
| 1709 | Value = Value << 2; |
| 1710 | Shifted += 2; |
| 1711 | } |
| 1712 | |
| 1713 | bool Carry = (Value & 0x00FFFFFF); |
| 1714 | Value = ((Value & 0xFF000000) >> 24) + Carry; |
| 1715 | |
| 1716 | if (Value & 0x0000100) |
| 1717 | Value = Value & 0x000001FC; |
| 1718 | |
| 1719 | if (Shifted > 24) |
| 1720 | Value = Value >> (Shifted - 24); |
| 1721 | else |
| 1722 | Value = Value << (24 - Shifted); |
| 1723 | |
| 1724 | return Value; |
| 1725 | } |
| 1726 | |
| 1727 | // The stack limit in the TCB is set to this many bytes above the actual |
| 1728 | // stack limit. |
| 1729 | static const uint64_t kSplitStackAvailable = 256; |
| 1730 | |
| 1731 | // Adjust the function prologue to enable split stacks. This currently only |
| 1732 | // supports android and linux. |
| 1733 | // |
| 1734 | // The ABI of the segmented stack prologue is a little arbitrarily chosen, but |
| 1735 | // must be well defined in order to allow for consistent implementations of the |
| 1736 | // __morestack helper function. The ABI is also not a normal ABI in that it |
| 1737 | // doesn't follow the normal calling conventions because this allows the |
| 1738 | // prologue of each function to be optimized further. |
| 1739 | // |
| 1740 | // Currently, the ABI looks like (when calling __morestack) |
| 1741 | // |
| 1742 | // * r4 holds the minimum stack size requested for this function call |
| 1743 | // * r5 holds the stack size of the arguments to the function |
| 1744 | // * the beginning of the function is 3 instructions after the call to |
| 1745 | // __morestack |
| 1746 | // |
| 1747 | // Implementations of __morestack should use r4 to allocate a new stack, r5 to |
| 1748 | // place the arguments on to the new stack, and the 3-instruction knowledge to |
| 1749 | // jump directly to the body of the function when working on the new stack. |
| 1750 | // |
| 1751 | // An old (and possibly no longer compatible) implementation of __morestack for |
| 1752 | // ARM can be found at [1]. |
| 1753 | // |
| 1754 | // [1] - https://github.com/mozilla/rust/blob/86efd9/src/rt/arch/arm/morestack.S |
| 1755 | void ARMFrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const { |
| 1756 | unsigned Opcode; |
| 1757 | unsigned CFIIndex; |
| 1758 | const ARMSubtarget *ST = &MF.getTarget().getSubtarget<ARMSubtarget>(); |
| 1759 | bool Thumb = ST->isThumb(); |
| 1760 | |
| 1761 | // Sadly, this currently doesn't support varargs, platforms other than |
| 1762 | // android/linux. Note that thumb1/thumb2 are support for android/linux. |
| 1763 | if (MF.getFunction()->isVarArg()) |
| 1764 | report_fatal_error("Segmented stacks do not support vararg functions."); |
| 1765 | if (!ST->isTargetAndroid() && !ST->isTargetLinux()) |
Alp Toker | 16f98b2 | 2014-04-09 14:47:27 +0000 | [diff] [blame] | 1766 | report_fatal_error("Segmented stacks not supported on this platform."); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1767 | |
| 1768 | MachineBasicBlock &prologueMBB = MF.front(); |
| 1769 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1770 | MachineModuleInfo &MMI = MF.getMMI(); |
| 1771 | MCContext &Context = MMI.getContext(); |
| 1772 | const MCRegisterInfo *MRI = Context.getRegisterInfo(); |
| 1773 | const ARMBaseInstrInfo &TII = |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1774 | *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1775 | ARMFunctionInfo *ARMFI = MF.getInfo<ARMFunctionInfo>(); |
| 1776 | DebugLoc DL; |
| 1777 | |
Tim Northover | f9e798b | 2014-05-22 13:03:43 +0000 | [diff] [blame] | 1778 | uint64_t StackSize = MFI->getStackSize(); |
| 1779 | |
| 1780 | // Do not generate a prologue for functions with a stack of size zero |
| 1781 | if (StackSize == 0) |
| 1782 | return; |
| 1783 | |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1784 | // Use R4 and R5 as scratch registers. |
| 1785 | // We save R4 and R5 before use and restore them before leaving the function. |
| 1786 | unsigned ScratchReg0 = ARM::R4; |
| 1787 | unsigned ScratchReg1 = ARM::R5; |
| 1788 | uint64_t AlignedStackSize; |
| 1789 | |
| 1790 | MachineBasicBlock *PrevStackMBB = MF.CreateMachineBasicBlock(); |
| 1791 | MachineBasicBlock *PostStackMBB = MF.CreateMachineBasicBlock(); |
| 1792 | MachineBasicBlock *AllocMBB = MF.CreateMachineBasicBlock(); |
| 1793 | MachineBasicBlock *GetMBB = MF.CreateMachineBasicBlock(); |
| 1794 | MachineBasicBlock *McrMBB = MF.CreateMachineBasicBlock(); |
| 1795 | |
| 1796 | for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(), |
| 1797 | e = prologueMBB.livein_end(); |
| 1798 | i != e; ++i) { |
| 1799 | AllocMBB->addLiveIn(*i); |
| 1800 | GetMBB->addLiveIn(*i); |
| 1801 | McrMBB->addLiveIn(*i); |
| 1802 | PrevStackMBB->addLiveIn(*i); |
| 1803 | PostStackMBB->addLiveIn(*i); |
| 1804 | } |
| 1805 | |
| 1806 | MF.push_front(PostStackMBB); |
| 1807 | MF.push_front(AllocMBB); |
| 1808 | MF.push_front(GetMBB); |
| 1809 | MF.push_front(McrMBB); |
| 1810 | MF.push_front(PrevStackMBB); |
| 1811 | |
| 1812 | // The required stack size that is aligned to ARM constant criterion. |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1813 | AlignedStackSize = alignToARMConstant(StackSize); |
| 1814 | |
| 1815 | // When the frame size is less than 256 we just compare the stack |
| 1816 | // boundary directly to the value of the stack pointer, per gcc. |
| 1817 | bool CompareStackPointer = AlignedStackSize < kSplitStackAvailable; |
| 1818 | |
| 1819 | // We will use two of the callee save registers as scratch registers so we |
| 1820 | // need to save those registers onto the stack. |
| 1821 | // We will use SR0 to hold stack limit and SR1 to hold the stack size |
| 1822 | // requested and arguments for __morestack(). |
| 1823 | // SR0: Scratch Register #0 |
| 1824 | // SR1: Scratch Register #1 |
| 1825 | // push {SR0, SR1} |
| 1826 | if (Thumb) { |
| 1827 | AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::tPUSH))) |
| 1828 | .addReg(ScratchReg0).addReg(ScratchReg1); |
| 1829 | } else { |
| 1830 | AddDefaultPred(BuildMI(PrevStackMBB, DL, TII.get(ARM::STMDB_UPD)) |
| 1831 | .addReg(ARM::SP, RegState::Define).addReg(ARM::SP)) |
| 1832 | .addReg(ScratchReg0).addReg(ScratchReg1); |
| 1833 | } |
| 1834 | |
| 1835 | // Emit the relevant DWARF information about the change in stack pointer as |
| 1836 | // well as where to find both r4 and r5 (the callee-save registers) |
| 1837 | CFIIndex = |
| 1838 | MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -8)); |
| 1839 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1840 | .addCFIIndex(CFIIndex); |
| 1841 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 1842 | nullptr, MRI->getDwarfRegNum(ScratchReg1, true), -4)); |
| 1843 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1844 | .addCFIIndex(CFIIndex); |
| 1845 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 1846 | nullptr, MRI->getDwarfRegNum(ScratchReg0, true), -8)); |
| 1847 | BuildMI(PrevStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1848 | .addCFIIndex(CFIIndex); |
| 1849 | |
| 1850 | // mov SR1, sp |
| 1851 | if (Thumb) { |
| 1852 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::tMOVr), ScratchReg1) |
| 1853 | .addReg(ARM::SP)); |
| 1854 | } else if (CompareStackPointer) { |
| 1855 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MOVr), ScratchReg1) |
| 1856 | .addReg(ARM::SP)).addReg(0); |
| 1857 | } |
| 1858 | |
| 1859 | // sub SR1, sp, #StackSize |
| 1860 | if (!CompareStackPointer && Thumb) { |
| 1861 | AddDefaultPred( |
| 1862 | AddDefaultCC(BuildMI(McrMBB, DL, TII.get(ARM::tSUBi8), ScratchReg1)) |
| 1863 | .addReg(ScratchReg1).addImm(AlignedStackSize)); |
| 1864 | } else if (!CompareStackPointer) { |
| 1865 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::SUBri), ScratchReg1) |
| 1866 | .addReg(ARM::SP).addImm(AlignedStackSize)).addReg(0); |
| 1867 | } |
| 1868 | |
| 1869 | if (Thumb && ST->isThumb1Only()) { |
| 1870 | unsigned PCLabelId = ARMFI->createPICLabelUId(); |
| 1871 | ARMConstantPoolValue *NewCPV = ARMConstantPoolSymbol::Create( |
Oliver Stannard | 92e0fc0 | 2014-04-03 08:45:16 +0000 | [diff] [blame] | 1872 | MF.getFunction()->getContext(), "__STACK_LIMIT", PCLabelId, 0); |
Oliver Stannard | b14c625 | 2014-04-02 16:10:33 +0000 | [diff] [blame] | 1873 | MachineConstantPool *MCP = MF.getConstantPool(); |
| 1874 | unsigned CPI = MCP->getConstantPoolIndex(NewCPV, MF.getAlignment()); |
| 1875 | |
| 1876 | // ldr SR0, [pc, offset(STACK_LIMIT)] |
| 1877 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRpci), ScratchReg0) |
| 1878 | .addConstantPoolIndex(CPI)); |
| 1879 | |
| 1880 | // ldr SR0, [SR0] |
| 1881 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::tLDRi), ScratchReg0) |
| 1882 | .addReg(ScratchReg0).addImm(0)); |
| 1883 | } else { |
| 1884 | // Get TLS base address from the coprocessor |
| 1885 | // mrc p15, #0, SR0, c13, c0, #3 |
| 1886 | AddDefaultPred(BuildMI(McrMBB, DL, TII.get(ARM::MRC), ScratchReg0) |
| 1887 | .addImm(15) |
| 1888 | .addImm(0) |
| 1889 | .addImm(13) |
| 1890 | .addImm(0) |
| 1891 | .addImm(3)); |
| 1892 | |
| 1893 | // Use the last tls slot on android and a private field of the TCP on linux. |
| 1894 | assert(ST->isTargetAndroid() || ST->isTargetLinux()); |
| 1895 | unsigned TlsOffset = ST->isTargetAndroid() ? 63 : 1; |
| 1896 | |
| 1897 | // Get the stack limit from the right offset |
| 1898 | // ldr SR0, [sr0, #4 * TlsOffset] |
| 1899 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(ARM::LDRi12), ScratchReg0) |
| 1900 | .addReg(ScratchReg0).addImm(4 * TlsOffset)); |
| 1901 | } |
| 1902 | |
| 1903 | // Compare stack limit with stack size requested. |
| 1904 | // cmp SR0, SR1 |
| 1905 | Opcode = Thumb ? ARM::tCMPr : ARM::CMPrr; |
| 1906 | AddDefaultPred(BuildMI(GetMBB, DL, TII.get(Opcode)) |
| 1907 | .addReg(ScratchReg0) |
| 1908 | .addReg(ScratchReg1)); |
| 1909 | |
| 1910 | // This jump is taken if StackLimit < SP - stack required. |
| 1911 | Opcode = Thumb ? ARM::tBcc : ARM::Bcc; |
| 1912 | BuildMI(GetMBB, DL, TII.get(Opcode)).addMBB(PostStackMBB) |
| 1913 | .addImm(ARMCC::LO) |
| 1914 | .addReg(ARM::CPSR); |
| 1915 | |
| 1916 | |
| 1917 | // Calling __morestack(StackSize, Size of stack arguments). |
| 1918 | // __morestack knows that the stack size requested is in SR0(r4) |
| 1919 | // and amount size of stack arguments is in SR1(r5). |
| 1920 | |
| 1921 | // Pass first argument for the __morestack by Scratch Register #0. |
| 1922 | // The amount size of stack required |
| 1923 | if (Thumb) { |
| 1924 | AddDefaultPred(AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), |
| 1925 | ScratchReg0)).addImm(AlignedStackSize)); |
| 1926 | } else { |
| 1927 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg0) |
| 1928 | .addImm(AlignedStackSize)).addReg(0); |
| 1929 | } |
| 1930 | // Pass second argument for the __morestack by Scratch Register #1. |
| 1931 | // The amount size of stack consumed to save function arguments. |
| 1932 | if (Thumb) { |
| 1933 | AddDefaultPred( |
| 1934 | AddDefaultCC(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVi8), ScratchReg1)) |
| 1935 | .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))); |
| 1936 | } else { |
| 1937 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::MOVi), ScratchReg1) |
| 1938 | .addImm(alignToARMConstant(ARMFI->getArgumentStackSize()))) |
| 1939 | .addReg(0); |
| 1940 | } |
| 1941 | |
| 1942 | // push {lr} - Save return address of this function. |
| 1943 | if (Thumb) { |
| 1944 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPUSH))) |
| 1945 | .addReg(ARM::LR); |
| 1946 | } else { |
| 1947 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::STMDB_UPD)) |
| 1948 | .addReg(ARM::SP, RegState::Define) |
| 1949 | .addReg(ARM::SP)) |
| 1950 | .addReg(ARM::LR); |
| 1951 | } |
| 1952 | |
| 1953 | // Emit the DWARF info about the change in stack as well as where to find the |
| 1954 | // previous link register |
| 1955 | CFIIndex = |
| 1956 | MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, -12)); |
| 1957 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1958 | .addCFIIndex(CFIIndex); |
| 1959 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( |
| 1960 | nullptr, MRI->getDwarfRegNum(ARM::LR, true), -12)); |
| 1961 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 1962 | .addCFIIndex(CFIIndex); |
| 1963 | |
| 1964 | // Call __morestack(). |
| 1965 | if (Thumb) { |
| 1966 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tBL))) |
| 1967 | .addExternalSymbol("__morestack"); |
| 1968 | } else { |
| 1969 | BuildMI(AllocMBB, DL, TII.get(ARM::BL)) |
| 1970 | .addExternalSymbol("__morestack"); |
| 1971 | } |
| 1972 | |
| 1973 | // pop {lr} - Restore return address of this original function. |
| 1974 | if (Thumb) { |
| 1975 | if (ST->isThumb1Only()) { |
| 1976 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) |
| 1977 | .addReg(ScratchReg0); |
| 1978 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tMOVr), ARM::LR) |
| 1979 | .addReg(ScratchReg0)); |
| 1980 | } else { |
| 1981 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::t2LDR_POST)) |
| 1982 | .addReg(ARM::LR, RegState::Define) |
| 1983 | .addReg(ARM::SP, RegState::Define) |
| 1984 | .addReg(ARM::SP) |
| 1985 | .addImm(4)); |
| 1986 | } |
| 1987 | } else { |
| 1988 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 1989 | .addReg(ARM::SP, RegState::Define) |
| 1990 | .addReg(ARM::SP)) |
| 1991 | .addReg(ARM::LR); |
| 1992 | } |
| 1993 | |
| 1994 | // Restore SR0 and SR1 in case of __morestack() was called. |
| 1995 | // __morestack() will skip PostStackMBB block so we need to restore |
| 1996 | // scratch registers from here. |
| 1997 | // pop {SR0, SR1} |
| 1998 | if (Thumb) { |
| 1999 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::tPOP))) |
| 2000 | .addReg(ScratchReg0) |
| 2001 | .addReg(ScratchReg1); |
| 2002 | } else { |
| 2003 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 2004 | .addReg(ARM::SP, RegState::Define) |
| 2005 | .addReg(ARM::SP)) |
| 2006 | .addReg(ScratchReg0) |
| 2007 | .addReg(ScratchReg1); |
| 2008 | } |
| 2009 | |
| 2010 | // Update the CFA offset now that we've popped |
| 2011 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); |
| 2012 | BuildMI(AllocMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2013 | .addCFIIndex(CFIIndex); |
| 2014 | |
| 2015 | // bx lr - Return from this function. |
| 2016 | Opcode = Thumb ? ARM::tBX_RET : ARM::BX_RET; |
| 2017 | AddDefaultPred(BuildMI(AllocMBB, DL, TII.get(Opcode))); |
| 2018 | |
| 2019 | // Restore SR0 and SR1 in case of __morestack() was not called. |
| 2020 | // pop {SR0, SR1} |
| 2021 | if (Thumb) { |
| 2022 | AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::tPOP))) |
| 2023 | .addReg(ScratchReg0) |
| 2024 | .addReg(ScratchReg1); |
| 2025 | } else { |
| 2026 | AddDefaultPred(BuildMI(PostStackMBB, DL, TII.get(ARM::LDMIA_UPD)) |
| 2027 | .addReg(ARM::SP, RegState::Define) |
| 2028 | .addReg(ARM::SP)) |
| 2029 | .addReg(ScratchReg0) |
| 2030 | .addReg(ScratchReg1); |
| 2031 | } |
| 2032 | |
| 2033 | // Update the CFA offset now that we've popped |
| 2034 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset(nullptr, 0)); |
| 2035 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2036 | .addCFIIndex(CFIIndex); |
| 2037 | |
| 2038 | // Tell debuggers that r4 and r5 are now the same as they were in the |
| 2039 | // previous function, that they're the "Same Value". |
| 2040 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( |
| 2041 | nullptr, MRI->getDwarfRegNum(ScratchReg0, true))); |
| 2042 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2043 | .addCFIIndex(CFIIndex); |
| 2044 | CFIIndex = MMI.addFrameInst(MCCFIInstruction::createSameValue( |
| 2045 | nullptr, MRI->getDwarfRegNum(ScratchReg1, true))); |
| 2046 | BuildMI(PostStackMBB, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) |
| 2047 | .addCFIIndex(CFIIndex); |
| 2048 | |
| 2049 | // Organizing MBB lists |
| 2050 | PostStackMBB->addSuccessor(&prologueMBB); |
| 2051 | |
| 2052 | AllocMBB->addSuccessor(PostStackMBB); |
| 2053 | |
| 2054 | GetMBB->addSuccessor(PostStackMBB); |
| 2055 | GetMBB->addSuccessor(AllocMBB); |
| 2056 | |
| 2057 | McrMBB->addSuccessor(GetMBB); |
| 2058 | |
| 2059 | PrevStackMBB->addSuccessor(McrMBB); |
| 2060 | |
| 2061 | #ifdef XDEBUG |
| 2062 | MF.verify(); |
| 2063 | #endif |
| 2064 | } |