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