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