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