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