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