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