| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1 | //===- AArch64FrameLowering.cpp - AArch64 Frame Lowering -------*- C++ -*-====// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the AArch64 implementation of TargetFrameLowering class. |
| 11 | // |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 12 | // On AArch64, stack frames are structured as follows: |
| 13 | // |
| 14 | // The stack grows downward. |
| 15 | // |
| 16 | // All of the individual frame areas on the frame below are optional, i.e. it's |
| 17 | // possible to create a function so that the particular area isn't present |
| 18 | // in the frame. |
| 19 | // |
| 20 | // At function entry, the "frame" looks as follows: |
| 21 | // |
| 22 | // | | Higher address |
| 23 | // |-----------------------------------| |
| 24 | // | | |
| 25 | // | arguments passed on the stack | |
| 26 | // | | |
| 27 | // |-----------------------------------| <- sp |
| 28 | // | | Lower address |
| 29 | // |
| 30 | // |
| 31 | // After the prologue has run, the frame has the following general structure. |
| 32 | // Note that this doesn't depict the case where a red-zone is used. Also, |
| 33 | // technically the last frame area (VLAs) doesn't get created until in the |
| 34 | // main function body, after the prologue is run. However, it's depicted here |
| 35 | // for completeness. |
| 36 | // |
| 37 | // | | Higher address |
| 38 | // |-----------------------------------| |
| 39 | // | | |
| 40 | // | arguments passed on the stack | |
| 41 | // | | |
| 42 | // |-----------------------------------| |
| 43 | // | | |
| Martin Storsjo | 68266fa | 2017-07-13 17:03:12 +0000 | [diff] [blame] | 44 | // | (Win64 only) varargs from reg | |
| 45 | // | | |
| 46 | // |-----------------------------------| |
| 47 | // | | |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 48 | // | prev_fp, prev_lr | |
| 49 | // | (a.k.a. "frame record") | |
| 50 | // |-----------------------------------| <- fp(=x29) |
| 51 | // | | |
| 52 | // | other callee-saved registers | |
| 53 | // | | |
| 54 | // |-----------------------------------| |
| 55 | // |.empty.space.to.make.part.below....| |
| 56 | // |.aligned.in.case.it.needs.more.than| (size of this area is unknown at |
| 57 | // |.the.standard.16-byte.alignment....| compile time; if present) |
| 58 | // |-----------------------------------| |
| 59 | // | | |
| 60 | // | local variables of fixed size | |
| 61 | // | including spill slots | |
| 62 | // |-----------------------------------| <- bp(not defined by ABI, |
| 63 | // |.variable-sized.local.variables....| LLVM chooses X19) |
| 64 | // |.(VLAs)............................| (size of this area is unknown at |
| 65 | // |...................................| compile time) |
| 66 | // |-----------------------------------| <- sp |
| 67 | // | | Lower address |
| 68 | // |
| 69 | // |
| 70 | // To access the data in a frame, at-compile time, a constant offset must be |
| 71 | // computable from one of the pointers (fp, bp, sp) to access it. The size |
| 72 | // of the areas with a dotted background cannot be computed at compile-time |
| 73 | // if they are present, making it required to have all three of fp, bp and |
| 74 | // sp to be set up to be able to access all contents in the frame areas, |
| 75 | // assuming all of the frame areas are non-empty. |
| 76 | // |
| 77 | // For most functions, some of the frame areas are empty. For those functions, |
| 78 | // it may not be necessary to set up fp or bp: |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 79 | // * A base pointer is definitely needed when there are both VLAs and local |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 80 | // variables with more-than-default alignment requirements. |
| Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 81 | // * A frame pointer is definitely needed when there are local variables with |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 82 | // more-than-default alignment requirements. |
| 83 | // |
| 84 | // In some cases when a base pointer is not strictly needed, it is generated |
| 85 | // anyway when offsets from the frame pointer to access local variables become |
| 86 | // so large that the offset can't be encoded in the immediate fields of loads |
| 87 | // or stores. |
| 88 | // |
| 89 | // FIXME: also explain the redzone concept. |
| 90 | // FIXME: also explain the concept of reserved call frames. |
| 91 | // |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 92 | //===----------------------------------------------------------------------===// |
| 93 | |
| 94 | #include "AArch64FrameLowering.h" |
| 95 | #include "AArch64InstrInfo.h" |
| 96 | #include "AArch64MachineFunctionInfo.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 97 | #include "AArch64RegisterInfo.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 98 | #include "AArch64Subtarget.h" |
| 99 | #include "AArch64TargetMachine.h" |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 100 | #include "MCTargetDesc/AArch64AddressingModes.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 101 | #include "llvm/ADT/SmallVector.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 102 | #include "llvm/ADT/Statistic.h" |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 103 | #include "llvm/CodeGen/LivePhysRegs.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 104 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 105 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 106 | #include "llvm/CodeGen/MachineFunction.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 107 | #include "llvm/CodeGen/MachineInstr.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 108 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 109 | #include "llvm/CodeGen/MachineMemOperand.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 110 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 111 | #include "llvm/CodeGen/MachineOperand.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 112 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 113 | #include "llvm/CodeGen/RegisterScavenging.h" |
| David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 114 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 115 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 116 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 117 | #include "llvm/IR/Attributes.h" |
| 118 | #include "llvm/IR/CallingConv.h" |
| Benjamin Kramer | 1f8930e | 2014-07-25 11:42:14 +0000 | [diff] [blame] | 119 | #include "llvm/IR/DataLayout.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 120 | #include "llvm/IR/DebugLoc.h" |
| Benjamin Kramer | 1f8930e | 2014-07-25 11:42:14 +0000 | [diff] [blame] | 121 | #include "llvm/IR/Function.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 122 | #include "llvm/MC/MCDwarf.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 123 | #include "llvm/Support/CommandLine.h" |
| Benjamin Kramer | 1f8930e | 2014-07-25 11:42:14 +0000 | [diff] [blame] | 124 | #include "llvm/Support/Debug.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 125 | #include "llvm/Support/ErrorHandling.h" |
| 126 | #include "llvm/Support/MathExtras.h" |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 127 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 128 | #include "llvm/Target/TargetMachine.h" |
| 129 | #include "llvm/Target/TargetOptions.h" |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 130 | #include <cassert> |
| 131 | #include <cstdint> |
| 132 | #include <iterator> |
| 133 | #include <vector> |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 134 | |
| 135 | using namespace llvm; |
| 136 | |
| 137 | #define DEBUG_TYPE "frame-info" |
| 138 | |
| 139 | static cl::opt<bool> EnableRedZone("aarch64-redzone", |
| 140 | cl::desc("enable use of redzone on AArch64"), |
| 141 | cl::init(false), cl::Hidden); |
| 142 | |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 143 | static cl::opt<bool> |
| 144 | ReverseCSRRestoreSeq("reverse-csr-restore-seq", |
| 145 | cl::desc("reverse the CSR restore sequence"), |
| 146 | cl::init(false), cl::Hidden); |
| 147 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 148 | STATISTIC(NumRedZoneFunctions, "Number of functions using red zone"); |
| 149 | |
| Matthias Braun | 5c290dc | 2018-01-19 03:16:36 +0000 | [diff] [blame] | 150 | /// This is the biggest offset to the stack pointer we can encode in aarch64 |
| 151 | /// instructions (without using a separate calculation and a temp register). |
| 152 | /// Note that the exception here are vector stores/loads which cannot encode any |
| 153 | /// displacements (see estimateRSStackSizeLimit(), isAArch64FrameOffsetLegal()). |
| 154 | static const unsigned DefaultSafeSPDisplacement = 255; |
| 155 | |
| Kristof Beyls | 2af1e90 | 2017-05-30 06:58:41 +0000 | [diff] [blame] | 156 | /// Look at each instruction that references stack frames and return the stack |
| 157 | /// size limit beyond which some of these instructions will require a scratch |
| 158 | /// register during their expansion later. |
| 159 | static unsigned estimateRSStackSizeLimit(MachineFunction &MF) { |
| 160 | // FIXME: For now, just conservatively guestimate based on unscaled indexing |
| 161 | // range. We'll end up allocating an unnecessary spill slot a lot, but |
| 162 | // realistically that's not a big deal at this stage of the game. |
| 163 | for (MachineBasicBlock &MBB : MF) { |
| 164 | for (MachineInstr &MI : MBB) { |
| 165 | if (MI.isDebugValue() || MI.isPseudo() || |
| 166 | MI.getOpcode() == AArch64::ADDXri || |
| 167 | MI.getOpcode() == AArch64::ADDSXri) |
| 168 | continue; |
| 169 | |
| Javed Absar | d13d419 | 2017-10-30 22:00:06 +0000 | [diff] [blame] | 170 | for (const MachineOperand &MO : MI.operands()) { |
| 171 | if (!MO.isFI()) |
| Kristof Beyls | 2af1e90 | 2017-05-30 06:58:41 +0000 | [diff] [blame] | 172 | continue; |
| 173 | |
| 174 | int Offset = 0; |
| 175 | if (isAArch64FrameOffsetLegal(MI, Offset, nullptr, nullptr, nullptr) == |
| 176 | AArch64FrameOffsetCannotUpdate) |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | } |
| Matthias Braun | 5c290dc | 2018-01-19 03:16:36 +0000 | [diff] [blame] | 181 | return DefaultSafeSPDisplacement; |
| Kristof Beyls | 2af1e90 | 2017-05-30 06:58:41 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 184 | bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const { |
| 185 | if (!EnableRedZone) |
| 186 | return false; |
| 187 | // Don't use the red zone if the function explicitly asks us not to. |
| 188 | // This is typically used for kernel code. |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 189 | if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone)) |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 190 | return false; |
| 191 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 192 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 193 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 194 | unsigned NumBytes = AFI->getLocalStackSize(); |
| 195 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 196 | return !(MFI.hasCalls() || hasFP(MF) || NumBytes > 128); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /// hasFP - Return true if the specified function should have a dedicated frame |
| 200 | /// pointer register. |
| 201 | bool AArch64FrameLowering::hasFP(const MachineFunction &MF) const { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 202 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 203 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
| Geoff Berry | 62c1a1e | 2016-03-02 17:58:31 +0000 | [diff] [blame] | 204 | // Retain behavior of always omitting the FP for leaf functions when possible. |
| Matthias Braun | 5c290dc | 2018-01-19 03:16:36 +0000 | [diff] [blame] | 205 | if (MFI.hasCalls() && MF.getTarget().Options.DisableFramePointerElim(MF)) |
| 206 | return true; |
| 207 | if (MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() || |
| 208 | MFI.hasStackMap() || MFI.hasPatchPoint() || |
| 209 | RegInfo->needsStackRealignment(MF)) |
| 210 | return true; |
| 211 | // With large callframes around we may need to use FP to access the scavenging |
| 212 | // emergency spillslot. |
| 213 | // |
| 214 | // Unfortunately some calls to hasFP() like machine verifier -> |
| 215 | // getReservedReg() -> hasFP in the middle of global isel are too early |
| 216 | // to know the max call frame size. Hopefully conservatively returning "true" |
| 217 | // in those cases is fine. |
| 218 | // DefaultSafeSPDisplacement is fine as we only emergency spill GP regs. |
| 219 | if (!MFI.isMaxCallFrameSizeComputed() || |
| 220 | MFI.getMaxCallFrameSize() > DefaultSafeSPDisplacement) |
| 221 | return true; |
| 222 | |
| 223 | return false; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 227 | /// not required, we reserve argument space for call sites in the function |
| 228 | /// immediately on entry to the current function. This eliminates the need for |
| 229 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 230 | /// included as part of the stack frame. |
| 231 | bool |
| 232 | AArch64FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 233 | return !MF.getFrameInfo().hasVarSizedObjects(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 236 | MachineBasicBlock::iterator AArch64FrameLowering::eliminateCallFramePseudoInstr( |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 237 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 238 | MachineBasicBlock::iterator I) const { |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 239 | const AArch64InstrInfo *TII = |
| 240 | static_cast<const AArch64InstrInfo *>(MF.getSubtarget().getInstrInfo()); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 241 | DebugLoc DL = I->getDebugLoc(); |
| Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 242 | unsigned Opc = I->getOpcode(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 243 | bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode(); |
| 244 | uint64_t CalleePopAmount = IsDestroy ? I->getOperand(1).getImm() : 0; |
| 245 | |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 246 | const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 247 | if (!TFI->hasReservedCallFrame(MF)) { |
| 248 | unsigned Align = getStackAlignment(); |
| 249 | |
| 250 | int64_t Amount = I->getOperand(0).getImm(); |
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 251 | Amount = alignTo(Amount, Align); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 252 | if (!IsDestroy) |
| 253 | Amount = -Amount; |
| 254 | |
| 255 | // N.b. if CalleePopAmount is valid but zero (i.e. callee would pop, but it |
| 256 | // doesn't have to pop anything), then the first operand will be zero too so |
| 257 | // this adjustment is a no-op. |
| 258 | if (CalleePopAmount == 0) { |
| 259 | // FIXME: in-function stack adjustment for calls is limited to 24-bits |
| 260 | // because there's no guaranteed temporary register available. |
| 261 | // |
| Sylvestre Ledru | 469de19 | 2014-08-11 18:04:46 +0000 | [diff] [blame] | 262 | // ADD/SUB (immediate) has only LSL #0 and LSL #12 available. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 263 | // 1) For offset <= 12-bit, we use LSL #0 |
| 264 | // 2) For 12-bit <= offset <= 24-bit, we use two instructions. One uses |
| 265 | // LSL #0, and the other uses LSL #12. |
| 266 | // |
| Chad Rosier | 401a4ab | 2016-01-19 16:50:45 +0000 | [diff] [blame] | 267 | // Most call frames will be allocated at the start of a function so |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 268 | // this is OK, but it is a limitation that needs dealing with. |
| 269 | assert(Amount > -0xffffff && Amount < 0xffffff && "call frame too large"); |
| 270 | emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, Amount, TII); |
| 271 | } |
| 272 | } else if (CalleePopAmount != 0) { |
| 273 | // If the calling convention demands that the callee pops arguments from the |
| 274 | // stack, we want to add it back if we have a reserved call frame. |
| 275 | assert(CalleePopAmount < 0xffffff && "call frame too large"); |
| 276 | emitFrameOffset(MBB, I, DL, AArch64::SP, AArch64::SP, -CalleePopAmount, |
| 277 | TII); |
| 278 | } |
| Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 279 | return MBB.erase(I); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void AArch64FrameLowering::emitCalleeSavedFrameMoves( |
| Geoff Berry | 62d4725 | 2016-02-25 16:36:08 +0000 | [diff] [blame] | 283 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 284 | MachineFunction &MF = *MBB.getParent(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 285 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 286 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 287 | const MCRegisterInfo *MRI = STI.getRegisterInfo(); |
| 288 | const TargetInstrInfo *TII = STI.getInstrInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 289 | DebugLoc DL = MBB.findDebugLoc(MBBI); |
| 290 | |
| 291 | // Add callee saved registers to move list. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 292 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 293 | if (CSI.empty()) |
| 294 | return; |
| 295 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 296 | for (const auto &Info : CSI) { |
| 297 | unsigned Reg = Info.getReg(); |
| Geoff Berry | 62d4725 | 2016-02-25 16:36:08 +0000 | [diff] [blame] | 298 | int64_t Offset = |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 299 | MFI.getObjectOffset(Info.getFrameIdx()) - getOffsetOfLocalArea(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 300 | unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); |
| Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 301 | unsigned CFIIndex = MF.addFrameInst( |
| Geoff Berry | 62d4725 | 2016-02-25 16:36:08 +0000 | [diff] [blame] | 302 | MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 303 | BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) |
| Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 304 | .addCFIIndex(CFIIndex) |
| 305 | .setMIFlags(MachineInstr::FrameSetup); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 309 | // Find a scratch register that we can use at the start of the prologue to |
| 310 | // re-align the stack pointer. We avoid using callee-save registers since they |
| 311 | // may appear to be free when this is called from canUseAsPrologue (during |
| 312 | // shrink wrapping), but then no longer be free when this is called from |
| 313 | // emitPrologue. |
| 314 | // |
| 315 | // FIXME: This is a bit conservative, since in the above case we could use one |
| 316 | // of the callee-save registers as a scratch temp to re-align the stack pointer, |
| 317 | // but we would then have to make sure that we were in fact saving at least one |
| 318 | // callee-save register in the prologue, which is additional complexity that |
| 319 | // doesn't seem worth the benefit. |
| 320 | static unsigned findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB) { |
| 321 | MachineFunction *MF = MBB->getParent(); |
| 322 | |
| 323 | // If MBB is an entry block, use X9 as the scratch register |
| 324 | if (&MF->front() == MBB) |
| 325 | return AArch64::X9; |
| 326 | |
| Eric Christopher | 60a245e | 2017-03-31 23:12:27 +0000 | [diff] [blame] | 327 | const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>(); |
| Matthias Braun | ac4307c | 2017-05-26 21:51:00 +0000 | [diff] [blame] | 328 | const AArch64RegisterInfo &TRI = *Subtarget.getRegisterInfo(); |
| Eric Christopher | 60a245e | 2017-03-31 23:12:27 +0000 | [diff] [blame] | 329 | LivePhysRegs LiveRegs(TRI); |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 330 | LiveRegs.addLiveIns(*MBB); |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 331 | |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 332 | // Mark callee saved registers as used so we will not choose them. |
| Matthias Braun | ac4307c | 2017-05-26 21:51:00 +0000 | [diff] [blame] | 333 | const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(MF); |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 334 | for (unsigned i = 0; CSRegs[i]; ++i) |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 335 | LiveRegs.addReg(CSRegs[i]); |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 336 | |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 337 | // Prefer X9 since it was historically used for the prologue scratch reg. |
| 338 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 339 | if (LiveRegs.available(MRI, AArch64::X9)) |
| 340 | return AArch64::X9; |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 341 | |
| Matthias Braun | 332bb5c | 2016-07-06 21:31:27 +0000 | [diff] [blame] | 342 | for (unsigned Reg : AArch64::GPR64RegClass) { |
| 343 | if (LiveRegs.available(MRI, Reg)) |
| 344 | return Reg; |
| 345 | } |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 346 | return AArch64::NoRegister; |
| 347 | } |
| 348 | |
| 349 | bool AArch64FrameLowering::canUseAsPrologue( |
| 350 | const MachineBasicBlock &MBB) const { |
| 351 | const MachineFunction *MF = MBB.getParent(); |
| 352 | MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); |
| 353 | const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>(); |
| 354 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 355 | |
| 356 | // Don't need a scratch register if we're not going to re-align the stack. |
| 357 | if (!RegInfo->needsStackRealignment(*MF)) |
| 358 | return true; |
| 359 | // Otherwise, we can use any block as long as it has a scratch register |
| 360 | // available. |
| 361 | return findScratchNonCalleeSaveRegister(TmpMBB) != AArch64::NoRegister; |
| 362 | } |
| 363 | |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 364 | static bool windowsRequiresStackProbe(MachineFunction &MF, |
| 365 | unsigned StackSizeInBytes) { |
| 366 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 367 | if (!Subtarget.isTargetWindows()) |
| 368 | return false; |
| 369 | const Function &F = MF.getFunction(); |
| 370 | // TODO: When implementing stack protectors, take that into account |
| 371 | // for the probe threshold. |
| 372 | unsigned StackProbeSize = 4096; |
| 373 | if (F.hasFnAttribute("stack-probe-size")) |
| 374 | F.getFnAttribute("stack-probe-size") |
| 375 | .getValueAsString() |
| 376 | .getAsInteger(0, StackProbeSize); |
| Hans Wennborg | 89c35fc | 2018-02-23 13:46:25 +0000 | [diff] [blame] | 377 | return (StackSizeInBytes >= StackProbeSize) && |
| 378 | !F.hasFnAttribute("no-stack-arg-probe"); |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 381 | bool AArch64FrameLowering::shouldCombineCSRLocalStackBump( |
| 382 | MachineFunction &MF, unsigned StackBumpBytes) const { |
| 383 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 384 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 385 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 386 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 387 | |
| 388 | if (AFI->getLocalStackSize() == 0) |
| 389 | return false; |
| 390 | |
| 391 | // 512 is the maximum immediate for stp/ldp that will be used for |
| 392 | // callee-save save/restores |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 393 | if (StackBumpBytes >= 512 || windowsRequiresStackProbe(MF, StackBumpBytes)) |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 394 | return false; |
| 395 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 396 | if (MFI.hasVarSizedObjects()) |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 397 | return false; |
| 398 | |
| 399 | if (RegInfo->needsStackRealignment(MF)) |
| 400 | return false; |
| 401 | |
| 402 | // This isn't strictly necessary, but it simplifies things a bit since the |
| 403 | // current RedZone handling code assumes the SP is adjusted by the |
| 404 | // callee-save save/restore code. |
| 405 | if (canUseRedZone(MF)) |
| 406 | return false; |
| 407 | |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | // Convert callee-save register save/restore instruction to do stack pointer |
| 412 | // decrement/increment to allocate/deallocate the callee-save stack area by |
| 413 | // converting store/load to use pre/post increment version. |
| 414 | static MachineBasicBlock::iterator convertCalleeSaveRestoreToSPPrePostIncDec( |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 415 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 416 | const DebugLoc &DL, const TargetInstrInfo *TII, int CSStackSizeInc) { |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 417 | // Ignore instructions that do not operate on SP, i.e. shadow call stack |
| 418 | // instructions. |
| 419 | while (MBBI->getOpcode() == AArch64::STRXpost || |
| 420 | MBBI->getOpcode() == AArch64::LDRXpre) { |
| 421 | assert(MBBI->getOperand(0).getReg() != AArch64::SP); |
| 422 | ++MBBI; |
| 423 | } |
| 424 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 425 | unsigned NewOpc; |
| 426 | bool NewIsUnscaled = false; |
| 427 | switch (MBBI->getOpcode()) { |
| 428 | default: |
| 429 | llvm_unreachable("Unexpected callee-save save/restore opcode!"); |
| 430 | case AArch64::STPXi: |
| 431 | NewOpc = AArch64::STPXpre; |
| 432 | break; |
| 433 | case AArch64::STPDi: |
| 434 | NewOpc = AArch64::STPDpre; |
| 435 | break; |
| 436 | case AArch64::STRXui: |
| 437 | NewOpc = AArch64::STRXpre; |
| 438 | NewIsUnscaled = true; |
| 439 | break; |
| 440 | case AArch64::STRDui: |
| 441 | NewOpc = AArch64::STRDpre; |
| 442 | NewIsUnscaled = true; |
| 443 | break; |
| 444 | case AArch64::LDPXi: |
| 445 | NewOpc = AArch64::LDPXpost; |
| 446 | break; |
| 447 | case AArch64::LDPDi: |
| 448 | NewOpc = AArch64::LDPDpost; |
| 449 | break; |
| 450 | case AArch64::LDRXui: |
| 451 | NewOpc = AArch64::LDRXpost; |
| 452 | NewIsUnscaled = true; |
| 453 | break; |
| 454 | case AArch64::LDRDui: |
| 455 | NewOpc = AArch64::LDRDpost; |
| 456 | NewIsUnscaled = true; |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc)); |
| 461 | MIB.addReg(AArch64::SP, RegState::Define); |
| 462 | |
| 463 | // Copy all operands other than the immediate offset. |
| 464 | unsigned OpndIdx = 0; |
| 465 | for (unsigned OpndEnd = MBBI->getNumOperands() - 1; OpndIdx < OpndEnd; |
| 466 | ++OpndIdx) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 467 | MIB.add(MBBI->getOperand(OpndIdx)); |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 468 | |
| 469 | assert(MBBI->getOperand(OpndIdx).getImm() == 0 && |
| 470 | "Unexpected immediate offset in first/last callee-save save/restore " |
| 471 | "instruction!"); |
| 472 | assert(MBBI->getOperand(OpndIdx - 1).getReg() == AArch64::SP && |
| 473 | "Unexpected base register in callee-save save/restore instruction!"); |
| 474 | // Last operand is immediate offset that needs fixing. |
| 475 | assert(CSStackSizeInc % 8 == 0); |
| 476 | int64_t CSStackSizeIncImm = CSStackSizeInc; |
| 477 | if (!NewIsUnscaled) |
| 478 | CSStackSizeIncImm /= 8; |
| 479 | MIB.addImm(CSStackSizeIncImm); |
| 480 | |
| 481 | MIB.setMIFlags(MBBI->getFlags()); |
| 482 | MIB.setMemRefs(MBBI->memoperands_begin(), MBBI->memoperands_end()); |
| 483 | |
| 484 | return std::prev(MBB.erase(MBBI)); |
| 485 | } |
| 486 | |
| 487 | // Fixup callee-save register save/restore instructions to take into account |
| 488 | // combined SP bump by adding the local stack size to the stack offsets. |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 489 | static void fixupCalleeSaveRestoreStackOffset(MachineInstr &MI, |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 490 | unsigned LocalStackSize) { |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 491 | unsigned Opc = MI.getOpcode(); |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 492 | |
| 493 | // Ignore instructions that do not operate on SP, i.e. shadow call stack |
| 494 | // instructions. |
| 495 | if (Opc == AArch64::STRXpost || Opc == AArch64::LDRXpre) { |
| 496 | assert(MI.getOperand(0).getReg() != AArch64::SP); |
| 497 | return; |
| 498 | } |
| 499 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 500 | (void)Opc; |
| 501 | assert((Opc == AArch64::STPXi || Opc == AArch64::STPDi || |
| 502 | Opc == AArch64::STRXui || Opc == AArch64::STRDui || |
| 503 | Opc == AArch64::LDPXi || Opc == AArch64::LDPDi || |
| 504 | Opc == AArch64::LDRXui || Opc == AArch64::LDRDui) && |
| 505 | "Unexpected callee-save save/restore opcode!"); |
| 506 | |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 507 | unsigned OffsetIdx = MI.getNumExplicitOperands() - 1; |
| 508 | assert(MI.getOperand(OffsetIdx - 1).getReg() == AArch64::SP && |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 509 | "Unexpected base register in callee-save save/restore instruction!"); |
| 510 | // Last operand is immediate offset that needs fixing. |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 511 | MachineOperand &OffsetOpnd = MI.getOperand(OffsetIdx); |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 512 | // All generated opcodes have scaled offsets. |
| 513 | assert(LocalStackSize % 8 == 0); |
| 514 | OffsetOpnd.setImm(OffsetOpnd.getImm() + LocalStackSize / 8); |
| 515 | } |
| 516 | |
| Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 517 | void AArch64FrameLowering::emitPrologue(MachineFunction &MF, |
| 518 | MachineBasicBlock &MBB) const { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 519 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 520 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 521 | const Function &F = MF.getFunction(); |
| Ahmed Bougacha | 66834ec | 2015-12-16 22:54:06 +0000 | [diff] [blame] | 522 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 523 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 524 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 525 | MachineModuleInfo &MMI = MF.getMMI(); |
| Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 526 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 527 | bool needsFrameMoves = MMI.hasDebugInfo() || F.needsUnwindTableEntry(); |
| Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 528 | bool HasFP = hasFP(MF); |
| 529 | |
| Jessica Paquette | 8aa6cd5 | 2018-04-12 16:16:18 +0000 | [diff] [blame^] | 530 | // At this point, we're going to decide whether or not the function uses a |
| 531 | // redzone. In most cases, the function doesn't have a redzone so let's |
| 532 | // assume that's false and set it to true in the case that there's a redzone. |
| 533 | AFI->setHasRedZone(false); |
| 534 | |
| Tim Northover | 775aaeb | 2015-11-05 21:54:58 +0000 | [diff] [blame] | 535 | // Debug location must be unknown since the first debug location is used |
| 536 | // to determine the end of the prologue. |
| 537 | DebugLoc DL; |
| 538 | |
| 539 | // All calls are tail calls in GHC calling conv, and functions have no |
| 540 | // prologue/epilogue. |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 541 | if (MF.getFunction().getCallingConv() == CallingConv::GHC) |
| Greg Fitzgerald | fa78d08 | 2015-01-19 17:40:05 +0000 | [diff] [blame] | 542 | return; |
| 543 | |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 544 | int NumBytes = (int)MFI.getStackSize(); |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 545 | if (!AFI->hasStackFrame() && !windowsRequiresStackProbe(MF, NumBytes)) { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 546 | assert(!HasFP && "unexpected function without stack frame but with FP"); |
| 547 | |
| 548 | // All of the stack allocation is for locals. |
| 549 | AFI->setLocalStackSize(NumBytes); |
| 550 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 551 | if (!NumBytes) |
| 552 | return; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 553 | // REDZONE: If the stack size is less than 128 bytes, we don't need |
| 554 | // to actually allocate. |
| Jessica Paquette | 642f6c6 | 2018-04-03 21:56:10 +0000 | [diff] [blame] | 555 | if (canUseRedZone(MF)) { |
| 556 | AFI->setHasRedZone(true); |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 557 | ++NumRedZoneFunctions; |
| Jessica Paquette | 642f6c6 | 2018-04-03 21:56:10 +0000 | [diff] [blame] | 558 | } else { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 559 | emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII, |
| 560 | MachineInstr::FrameSetup); |
| 561 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 562 | // Label used to tie together the PROLOG_LABEL and the MachineMoves. |
| 563 | MCSymbol *FrameLabel = MMI.getContext().createTempSymbol(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 564 | // Encode the stack size of the leaf function. |
| Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 565 | unsigned CFIIndex = MF.addFrameInst( |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 566 | MCCFIInstruction::createDefCfaOffset(FrameLabel, -NumBytes)); |
| 567 | BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) |
| Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 568 | .addCFIIndex(CFIIndex) |
| 569 | .setMIFlags(MachineInstr::FrameSetup); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 570 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 574 | bool IsWin64 = |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 575 | Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv()); |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 576 | unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0; |
| 577 | |
| 578 | auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject; |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 579 | // All of the remaining stack allocations are for locals. |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 580 | AFI->setLocalStackSize(NumBytes - PrologueSaveSize); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 581 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 582 | bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes); |
| 583 | if (CombineSPBump) { |
| 584 | emitFrameOffset(MBB, MBBI, DL, AArch64::SP, AArch64::SP, -NumBytes, TII, |
| 585 | MachineInstr::FrameSetup); |
| 586 | NumBytes = 0; |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 587 | } else if (PrologueSaveSize != 0) { |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 588 | MBBI = convertCalleeSaveRestoreToSPPrePostIncDec(MBB, MBBI, DL, TII, |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 589 | -PrologueSaveSize); |
| 590 | NumBytes -= PrologueSaveSize; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 591 | } |
| 592 | assert(NumBytes >= 0 && "Negative stack allocation size!?"); |
| 593 | |
| 594 | // Move past the saves of the callee-saved registers, fixing up the offsets |
| 595 | // and pre-inc if we decided to combine the callee-save and local stack |
| 596 | // pointer bump above. |
| Geoff Berry | 04bf91a | 2016-02-01 16:29:19 +0000 | [diff] [blame] | 597 | MachineBasicBlock::iterator End = MBB.end(); |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 598 | while (MBBI != End && MBBI->getFlag(MachineInstr::FrameSetup)) { |
| 599 | if (CombineSPBump) |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 600 | fixupCalleeSaveRestoreStackOffset(*MBBI, AFI->getLocalStackSize()); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 601 | ++MBBI; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 602 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 603 | if (HasFP) { |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 604 | // Only set up FP if we actually need to. Frame pointer is fp = |
| 605 | // sp - fixedobject - 16. |
| 606 | int FPOffset = AFI->getCalleeSavedStackSize() - 16; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 607 | if (CombineSPBump) |
| 608 | FPOffset += AFI->getLocalStackSize(); |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 609 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 610 | // Issue sub fp, sp, FPOffset or |
| 611 | // mov fp,sp when FPOffset is zero. |
| 612 | // Note: All stores of callee-saved registers are marked as "FrameSetup". |
| 613 | // This code marks the instruction(s) that set the FP also. |
| 614 | emitFrameOffset(MBB, MBBI, DL, AArch64::FP, AArch64::SP, FPOffset, TII, |
| 615 | MachineInstr::FrameSetup); |
| 616 | } |
| 617 | |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 618 | if (windowsRequiresStackProbe(MF, NumBytes)) { |
| 619 | uint32_t NumWords = NumBytes >> 4; |
| 620 | |
| 621 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVi64imm), AArch64::X15) |
| 622 | .addImm(NumWords) |
| 623 | .setMIFlags(MachineInstr::FrameSetup); |
| 624 | |
| 625 | switch (MF.getTarget().getCodeModel()) { |
| 626 | case CodeModel::Small: |
| 627 | case CodeModel::Medium: |
| 628 | case CodeModel::Kernel: |
| 629 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::BL)) |
| 630 | .addExternalSymbol("__chkstk") |
| 631 | .addReg(AArch64::X15, RegState::Implicit) |
| 632 | .setMIFlags(MachineInstr::FrameSetup); |
| 633 | break; |
| 634 | case CodeModel::Large: |
| 635 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVaddrEXT)) |
| 636 | .addReg(AArch64::X16, RegState::Define) |
| 637 | .addExternalSymbol("__chkstk") |
| 638 | .addExternalSymbol("__chkstk") |
| 639 | .setMIFlags(MachineInstr::FrameSetup); |
| 640 | |
| 641 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::BLR)) |
| 642 | .addReg(AArch64::X16, RegState::Kill) |
| 643 | .addReg(AArch64::X15, RegState::Implicit | RegState::Define) |
| 644 | .setMIFlags(MachineInstr::FrameSetup); |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::SUBXrx64), AArch64::SP) |
| 649 | .addReg(AArch64::SP, RegState::Kill) |
| 650 | .addReg(AArch64::X15, RegState::Kill) |
| 651 | .addImm(AArch64_AM::getArithExtendImm(AArch64_AM::UXTX, 4)) |
| 652 | .setMIFlags(MachineInstr::FrameSetup); |
| 653 | NumBytes = 0; |
| 654 | } |
| 655 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 656 | // Allocate space for the rest of the frame. |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 657 | if (NumBytes) { |
| 658 | const bool NeedsRealignment = RegInfo->needsStackRealignment(MF); |
| 659 | unsigned scratchSPReg = AArch64::SP; |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 660 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 661 | if (NeedsRealignment) { |
| 662 | scratchSPReg = findScratchNonCalleeSaveRegister(&MBB); |
| 663 | assert(scratchSPReg != AArch64::NoRegister); |
| 664 | } |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 665 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 666 | // If we're a leaf function, try using the red zone. |
| 667 | if (!canUseRedZone(MF)) |
| 668 | // FIXME: in the case of dynamic re-alignment, NumBytes doesn't have |
| 669 | // the correct value here, as NumBytes also includes padding bytes, |
| 670 | // which shouldn't be counted here. |
| 671 | emitFrameOffset(MBB, MBBI, DL, scratchSPReg, AArch64::SP, -NumBytes, TII, |
| 672 | MachineInstr::FrameSetup); |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 673 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 674 | if (NeedsRealignment) { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 675 | const unsigned Alignment = MFI.getMaxAlignment(); |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 676 | const unsigned NrBitsToZero = countTrailingZeros(Alignment); |
| 677 | assert(NrBitsToZero > 1); |
| 678 | assert(scratchSPReg != AArch64::SP); |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 679 | |
| Chad Rosier | 27c352d | 2016-03-14 18:24:34 +0000 | [diff] [blame] | 680 | // SUB X9, SP, NumBytes |
| 681 | // -- X9 is temporary register, so shouldn't contain any live data here, |
| 682 | // -- free to use. This is already produced by emitFrameOffset above. |
| 683 | // AND SP, X9, 0b11111...0000 |
| 684 | // The logical immediates have a non-trivial encoding. The following |
| 685 | // formula computes the encoded immediate with all ones but |
| 686 | // NrBitsToZero zero bits as least significant bits. |
| 687 | uint32_t andMaskEncoded = (1 << 12) // = N |
| 688 | | ((64 - NrBitsToZero) << 6) // immr |
| 689 | | ((64 - NrBitsToZero - 1) << 0); // imms |
| 690 | |
| 691 | BuildMI(MBB, MBBI, DL, TII->get(AArch64::ANDXri), AArch64::SP) |
| 692 | .addReg(scratchSPReg, RegState::Kill) |
| 693 | .addImm(andMaskEncoded); |
| 694 | AFI->setStackRealigned(true); |
| 695 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | // If we need a base pointer, set it up here. It's whatever the value of the |
| 699 | // stack pointer is at this point. Any variable size objects will be allocated |
| 700 | // after this, so we can still use the base pointer to reference locals. |
| 701 | // |
| 702 | // FIXME: Clarify FrameSetup flags here. |
| 703 | // Note: Use emitFrameOffset() like above for FP if the FrameSetup flag is |
| 704 | // needed. |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 705 | if (RegInfo->hasBasePointer(MF)) { |
| 706 | TII->copyPhysReg(MBB, MBBI, DL, RegInfo->getBaseRegister(), AArch64::SP, |
| 707 | false); |
| 708 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 709 | |
| 710 | if (needsFrameMoves) { |
| Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 711 | const DataLayout &TD = MF.getDataLayout(); |
| 712 | const int StackGrowth = -TD.getPointerSize(0); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 713 | unsigned FramePtr = RegInfo->getFrameRegister(MF); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 714 | // An example of the prologue: |
| 715 | // |
| 716 | // .globl __foo |
| 717 | // .align 2 |
| 718 | // __foo: |
| 719 | // Ltmp0: |
| 720 | // .cfi_startproc |
| 721 | // .cfi_personality 155, ___gxx_personality_v0 |
| 722 | // Leh_func_begin: |
| 723 | // .cfi_lsda 16, Lexception33 |
| 724 | // |
| 725 | // stp xa,bx, [sp, -#offset]! |
| 726 | // ... |
| 727 | // stp x28, x27, [sp, #offset-32] |
| 728 | // stp fp, lr, [sp, #offset-16] |
| 729 | // add fp, sp, #offset - 16 |
| 730 | // sub sp, sp, #1360 |
| 731 | // |
| 732 | // The Stack: |
| 733 | // +-------------------------------------------+ |
| 734 | // 10000 | ........ | ........ | ........ | ........ | |
| 735 | // 10004 | ........ | ........ | ........ | ........ | |
| 736 | // +-------------------------------------------+ |
| 737 | // 10008 | ........ | ........ | ........ | ........ | |
| 738 | // 1000c | ........ | ........ | ........ | ........ | |
| 739 | // +===========================================+ |
| 740 | // 10010 | X28 Register | |
| 741 | // 10014 | X28 Register | |
| 742 | // +-------------------------------------------+ |
| 743 | // 10018 | X27 Register | |
| 744 | // 1001c | X27 Register | |
| 745 | // +===========================================+ |
| 746 | // 10020 | Frame Pointer | |
| 747 | // 10024 | Frame Pointer | |
| 748 | // +-------------------------------------------+ |
| 749 | // 10028 | Link Register | |
| 750 | // 1002c | Link Register | |
| 751 | // +===========================================+ |
| 752 | // 10030 | ........ | ........ | ........ | ........ | |
| 753 | // 10034 | ........ | ........ | ........ | ........ | |
| 754 | // +-------------------------------------------+ |
| 755 | // 10038 | ........ | ........ | ........ | ........ | |
| 756 | // 1003c | ........ | ........ | ........ | ........ | |
| 757 | // +-------------------------------------------+ |
| 758 | // |
| 759 | // [sp] = 10030 :: >>initial value<< |
| 760 | // sp = 10020 :: stp fp, lr, [sp, #-16]! |
| 761 | // fp = sp == 10020 :: mov fp, sp |
| 762 | // [sp] == 10020 :: stp x28, x27, [sp, #-16]! |
| 763 | // sp == 10010 :: >>final value<< |
| 764 | // |
| 765 | // The frame pointer (w29) points to address 10020. If we use an offset of |
| 766 | // '16' from 'w29', we get the CFI offsets of -8 for w30, -16 for w29, -24 |
| 767 | // for w27, and -32 for w28: |
| 768 | // |
| 769 | // Ltmp1: |
| 770 | // .cfi_def_cfa w29, 16 |
| 771 | // Ltmp2: |
| 772 | // .cfi_offset w30, -8 |
| 773 | // Ltmp3: |
| 774 | // .cfi_offset w29, -16 |
| 775 | // Ltmp4: |
| 776 | // .cfi_offset w27, -24 |
| 777 | // Ltmp5: |
| 778 | // .cfi_offset w28, -32 |
| 779 | |
| 780 | if (HasFP) { |
| 781 | // Define the current CFA rule to use the provided FP. |
| 782 | unsigned Reg = RegInfo->getDwarfRegNum(FramePtr, true); |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 783 | unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfa( |
| 784 | nullptr, Reg, 2 * StackGrowth - FixedObject)); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 785 | BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) |
| Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 786 | .addCFIIndex(CFIIndex) |
| 787 | .setMIFlags(MachineInstr::FrameSetup); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 788 | } else { |
| 789 | // Encode the stack size of the leaf function. |
| Matthias Braun | f23ef43 | 2016-11-30 23:48:42 +0000 | [diff] [blame] | 790 | unsigned CFIIndex = MF.addFrameInst( |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 791 | MCCFIInstruction::createDefCfaOffset(nullptr, -MFI.getStackSize())); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 792 | BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION)) |
| Adrian Prantl | b9fa945 | 2014-12-16 00:20:49 +0000 | [diff] [blame] | 793 | .addCFIIndex(CFIIndex) |
| 794 | .setMIFlags(MachineInstr::FrameSetup); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| Geoff Berry | 62d4725 | 2016-02-25 16:36:08 +0000 | [diff] [blame] | 797 | // Now emit the moves for whatever callee saved regs we have (including FP, |
| 798 | // LR if those are saved). |
| 799 | emitCalleeSavedFrameMoves(MBB, MBBI); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 803 | void AArch64FrameLowering::emitEpilogue(MachineFunction &MF, |
| 804 | MachineBasicBlock &MBB) const { |
| 805 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 806 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Ahmed Bougacha | 66834ec | 2015-12-16 22:54:06 +0000 | [diff] [blame] | 807 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| Ahmed Bougacha | 66834ec | 2015-12-16 22:54:06 +0000 | [diff] [blame] | 808 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); |
| Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 809 | DebugLoc DL; |
| 810 | bool IsTailCallReturn = false; |
| 811 | if (MBB.end() != MBBI) { |
| 812 | DL = MBBI->getDebugLoc(); |
| 813 | unsigned RetOpcode = MBBI->getOpcode(); |
| 814 | IsTailCallReturn = RetOpcode == AArch64::TCRETURNdi || |
| 815 | RetOpcode == AArch64::TCRETURNri; |
| 816 | } |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 817 | int NumBytes = MFI.getStackSize(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 818 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 819 | |
| Greg Fitzgerald | fa78d08 | 2015-01-19 17:40:05 +0000 | [diff] [blame] | 820 | // All calls are tail calls in GHC calling conv, and functions have no |
| 821 | // prologue/epilogue. |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 822 | if (MF.getFunction().getCallingConv() == CallingConv::GHC) |
| Greg Fitzgerald | fa78d08 | 2015-01-19 17:40:05 +0000 | [diff] [blame] | 823 | return; |
| 824 | |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 825 | // Initial and residual are named for consistency with the prologue. Note that |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 826 | // in the epilogue, the residual adjustment is executed first. |
| 827 | uint64_t ArgumentPopSize = 0; |
| Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 828 | if (IsTailCallReturn) { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 829 | MachineOperand &StackAdjust = MBBI->getOperand(1); |
| 830 | |
| 831 | // For a tail-call in a callee-pops-arguments environment, some or all of |
| 832 | // the stack may actually be in use for the call's arguments, this is |
| 833 | // calculated during LowerCall and consumed here... |
| 834 | ArgumentPopSize = StackAdjust.getImm(); |
| 835 | } else { |
| 836 | // ... otherwise the amount to pop is *all* of the argument space, |
| 837 | // conveniently stored in the MachineFunctionInfo by |
| 838 | // LowerFormalArguments. This will, of course, be zero for the C calling |
| 839 | // convention. |
| 840 | ArgumentPopSize = AFI->getArgumentStackToRestore(); |
| 841 | } |
| 842 | |
| 843 | // The stack frame should be like below, |
| 844 | // |
| 845 | // ---------------------- --- |
| 846 | // | | | |
| 847 | // | BytesInStackArgArea| CalleeArgStackSize |
| 848 | // | (NumReusableBytes) | (of tail call) |
| 849 | // | | --- |
| 850 | // | | | |
| 851 | // ---------------------| --- | |
| 852 | // | | | | |
| 853 | // | CalleeSavedReg | | | |
| Geoff Berry | 04bf91a | 2016-02-01 16:29:19 +0000 | [diff] [blame] | 854 | // | (CalleeSavedStackSize)| | | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 855 | // | | | | |
| 856 | // ---------------------| | NumBytes |
| 857 | // | | StackSize (StackAdjustUp) |
| 858 | // | LocalStackSize | | | |
| 859 | // | (covering callee | | | |
| 860 | // | args) | | | |
| 861 | // | | | | |
| 862 | // ---------------------- --- --- |
| 863 | // |
| 864 | // So NumBytes = StackSize + BytesInStackArgArea - CalleeArgStackSize |
| 865 | // = StackSize + ArgumentPopSize |
| 866 | // |
| 867 | // AArch64TargetLowering::LowerCall figures out ArgumentPopSize and keeps |
| 868 | // it as the 2nd argument of AArch64ISD::TC_RETURN. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 869 | |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 870 | bool IsWin64 = |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 871 | Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv()); |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 872 | unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0; |
| 873 | |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 874 | uint64_t AfterCSRPopSize = ArgumentPopSize; |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 875 | auto PrologueSaveSize = AFI->getCalleeSavedStackSize() + FixedObject; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 876 | bool CombineSPBump = shouldCombineCSRLocalStackBump(MF, NumBytes); |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 877 | // Assume we can't combine the last pop with the sp restore. |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 878 | |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 879 | if (!CombineSPBump && PrologueSaveSize != 0) { |
| 880 | MachineBasicBlock::iterator Pop = std::prev(MBB.getFirstTerminator()); |
| 881 | // Converting the last ldp to a post-index ldp is valid only if the last |
| 882 | // ldp's offset is 0. |
| 883 | const MachineOperand &OffsetOp = Pop->getOperand(Pop->getNumOperands() - 1); |
| 884 | // If the offset is 0, convert it to a post-index ldp. |
| 885 | if (OffsetOp.getImm() == 0) { |
| 886 | convertCalleeSaveRestoreToSPPrePostIncDec(MBB, Pop, DL, TII, |
| 887 | PrologueSaveSize); |
| 888 | } else { |
| 889 | // If not, make sure to emit an add after the last ldp. |
| 890 | // We're doing this by transfering the size to be restored from the |
| 891 | // adjustment *before* the CSR pops to the adjustment *after* the CSR |
| 892 | // pops. |
| 893 | AfterCSRPopSize += PrologueSaveSize; |
| 894 | } |
| 895 | } |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 896 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 897 | // Move past the restores of the callee-saved registers. |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 898 | // If we plan on combining the sp bump of the local stack size and the callee |
| 899 | // save stack size, we might need to adjust the CSR save and restore offsets. |
| Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 900 | MachineBasicBlock::iterator LastPopI = MBB.getFirstTerminator(); |
| Matthias Braun | 4541929 | 2015-12-17 03:18:47 +0000 | [diff] [blame] | 901 | MachineBasicBlock::iterator Begin = MBB.begin(); |
| 902 | while (LastPopI != Begin) { |
| 903 | --LastPopI; |
| Geoff Berry | 04bf91a | 2016-02-01 16:29:19 +0000 | [diff] [blame] | 904 | if (!LastPopI->getFlag(MachineInstr::FrameDestroy)) { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 905 | ++LastPopI; |
| Matthias Braun | 4541929 | 2015-12-17 03:18:47 +0000 | [diff] [blame] | 906 | break; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 907 | } else if (CombineSPBump) |
| Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 908 | fixupCalleeSaveRestoreStackOffset(*LastPopI, AFI->getLocalStackSize()); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 909 | } |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 910 | |
| 911 | // If there is a single SP update, insert it before the ret and we're done. |
| 912 | if (CombineSPBump) { |
| 913 | emitFrameOffset(MBB, MBB.getFirstTerminator(), DL, AArch64::SP, AArch64::SP, |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 914 | NumBytes + AfterCSRPopSize, TII, |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 915 | MachineInstr::FrameDestroy); |
| 916 | return; |
| 917 | } |
| 918 | |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 919 | NumBytes -= PrologueSaveSize; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 920 | assert(NumBytes >= 0 && "Negative stack allocation size!?"); |
| 921 | |
| 922 | if (!hasFP(MF)) { |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 923 | bool RedZone = canUseRedZone(MF); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 924 | // If this was a redzone leaf function, we don't need to restore the |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 925 | // stack pointer (but we may need to pop stack args for fastcc). |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 926 | if (RedZone && AfterCSRPopSize == 0) |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 927 | return; |
| 928 | |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 929 | bool NoCalleeSaveRestore = PrologueSaveSize == 0; |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 930 | int StackRestoreBytes = RedZone ? 0 : NumBytes; |
| 931 | if (NoCalleeSaveRestore) |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 932 | StackRestoreBytes += AfterCSRPopSize; |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 933 | emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP, |
| 934 | StackRestoreBytes, TII, MachineInstr::FrameDestroy); |
| 935 | // If we were able to combine the local stack pop with the argument pop, |
| 936 | // then we're done. |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 937 | if (NoCalleeSaveRestore || AfterCSRPopSize == 0) |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 938 | return; |
| 939 | NumBytes = 0; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // Restore the original stack pointer. |
| 943 | // FIXME: Rather than doing the math here, we should instead just use |
| 944 | // non-post-indexed loads for the restores if we aren't actually going to |
| 945 | // be able to save any instructions. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 946 | if (MFI.hasVarSizedObjects() || AFI->isStackRealigned()) |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 947 | emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::FP, |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 948 | -AFI->getCalleeSavedStackSize() + 16, TII, |
| 949 | MachineInstr::FrameDestroy); |
| Chad Rosier | 6d98655 | 2016-03-14 18:17:41 +0000 | [diff] [blame] | 950 | else if (NumBytes) |
| 951 | emitFrameOffset(MBB, LastPopI, DL, AArch64::SP, AArch64::SP, NumBytes, TII, |
| 952 | MachineInstr::FrameDestroy); |
| Geoff Berry | a1c6269 | 2016-02-23 16:54:36 +0000 | [diff] [blame] | 953 | |
| 954 | // This must be placed after the callee-save restore code because that code |
| 955 | // assumes the SP is at the same location as it was after the callee-save save |
| 956 | // code in the prologue. |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 957 | if (AfterCSRPopSize) { |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 958 | // Find an insertion point for the first ldp so that it goes before the |
| 959 | // shadow call stack epilog instruction. This ensures that the restore of |
| 960 | // lr from x18 is placed after the restore from sp. |
| 961 | auto FirstSPPopI = MBB.getFirstTerminator(); |
| 962 | while (FirstSPPopI != Begin) { |
| 963 | auto Prev = std::prev(FirstSPPopI); |
| 964 | if (Prev->getOpcode() != AArch64::LDRXpre || |
| 965 | Prev->getOperand(0).getReg() == AArch64::SP) |
| 966 | break; |
| 967 | FirstSPPopI = Prev; |
| 968 | } |
| 969 | |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 970 | // Sometimes (when we restore in the same order as we save), we can end up |
| 971 | // with code like this: |
| 972 | // |
| 973 | // ldp x26, x25, [sp] |
| 974 | // ldp x24, x23, [sp, #16] |
| 975 | // ldp x22, x21, [sp, #32] |
| 976 | // ldp x20, x19, [sp, #48] |
| 977 | // add sp, sp, #64 |
| 978 | // |
| 979 | // In this case, it is always better to put the first ldp at the end, so |
| 980 | // that the load-store optimizer can run and merge the ldp and the add into |
| 981 | // a post-index ldp. |
| 982 | // If we managed to grab the first pop instruction, move it to the end. |
| 983 | if (LastPopI != Begin) |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 984 | MBB.splice(FirstSPPopI, &MBB, LastPopI); |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 985 | // We should end up with something like this now: |
| 986 | // |
| 987 | // ldp x24, x23, [sp, #16] |
| 988 | // ldp x22, x21, [sp, #32] |
| 989 | // ldp x20, x19, [sp, #48] |
| 990 | // ldp x26, x25, [sp] |
| 991 | // add sp, sp, #64 |
| 992 | // |
| 993 | // and the load-store optimizer can merge the last two instructions into: |
| 994 | // |
| 995 | // ldp x26, x25, [sp], #64 |
| 996 | // |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 997 | emitFrameOffset(MBB, FirstSPPopI, DL, AArch64::SP, AArch64::SP, |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 998 | AfterCSRPopSize, TII, MachineInstr::FrameDestroy); |
| 999 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1002 | /// getFrameIndexReference - Provide a base+offset reference to an FI slot for |
| 1003 | /// debug info. It's the same as what we use for resolving the code-gen |
| 1004 | /// references for now. FIXME: This can go wrong when references are |
| 1005 | /// SP-relative and simple call frames aren't used. |
| 1006 | int AArch64FrameLowering::getFrameIndexReference(const MachineFunction &MF, |
| 1007 | int FI, |
| 1008 | unsigned &FrameReg) const { |
| 1009 | return resolveFrameIndexReference(MF, FI, FrameReg); |
| 1010 | } |
| 1011 | |
| 1012 | int AArch64FrameLowering::resolveFrameIndexReference(const MachineFunction &MF, |
| 1013 | int FI, unsigned &FrameReg, |
| 1014 | bool PreferFP) const { |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1015 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1016 | const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>( |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1017 | MF.getSubtarget().getRegisterInfo()); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1018 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 1019 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1020 | bool IsWin64 = |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1021 | Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv()); |
| Martin Storsjo | eacf4e4 | 2017-08-01 21:13:54 +0000 | [diff] [blame] | 1022 | unsigned FixedObject = IsWin64 ? alignTo(AFI->getVarArgsGPRSize(), 16) : 0; |
| 1023 | int FPOffset = MFI.getObjectOffset(FI) + FixedObject + 16; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1024 | int Offset = MFI.getObjectOffset(FI) + MFI.getStackSize(); |
| 1025 | bool isFixed = MFI.isFixedObjectIndex(FI); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Use frame pointer to reference fixed objects. Use it for locals if |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 1028 | // there are VLAs or a dynamically realigned SP (and thus the SP isn't |
| 1029 | // reliable as a base). Make sure useFPForScavengingIndex() does the |
| 1030 | // right thing for the emergency spill slot. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1031 | bool UseFP = false; |
| 1032 | if (AFI->hasStackFrame()) { |
| 1033 | // Note: Keeping the following as multiple 'if' statements rather than |
| 1034 | // merging to a single expression for readability. |
| 1035 | // |
| 1036 | // Argument access should always use the FP. |
| 1037 | if (isFixed) { |
| 1038 | UseFP = hasFP(MF); |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1039 | } else if (hasFP(MF) && !RegInfo->needsStackRealignment(MF)) { |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1040 | // If the FPOffset is negative, we have to keep in mind that the |
| 1041 | // available offset range for negative offsets is smaller than for |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1042 | // positive ones. If an offset is |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1043 | // available via the FP and the SP, use whichever is closest. |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1044 | bool FPOffsetFits = FPOffset >= -256; |
| 1045 | PreferFP |= Offset > -FPOffset; |
| 1046 | |
| 1047 | if (MFI.hasVarSizedObjects()) { |
| 1048 | // If we have variable sized objects, we can use either FP or BP, as the |
| 1049 | // SP offset is unknown. We can use the base pointer if we have one and |
| 1050 | // FP is not preferred. If not, we're stuck with using FP. |
| 1051 | bool CanUseBP = RegInfo->hasBasePointer(MF); |
| 1052 | if (FPOffsetFits && CanUseBP) // Both are ok. Pick the best. |
| 1053 | UseFP = PreferFP; |
| 1054 | else if (!CanUseBP) // Can't use BP. Forced to use FP. |
| 1055 | UseFP = true; |
| 1056 | // else we can use BP and FP, but the offset from FP won't fit. |
| 1057 | // That will make us scavenge registers which we can probably avoid by |
| 1058 | // using BP. If it won't fit for BP either, we'll scavenge anyway. |
| Francis Visoiu Mistrih | 6463922 | 2018-04-11 12:36:55 +0000 | [diff] [blame] | 1059 | } else if (FPOffset >= 0) { |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1060 | // Use SP or FP, whichever gives us the best chance of the offset |
| 1061 | // being in range for direct access. If the FPOffset is positive, |
| 1062 | // that'll always be best, as the SP will be even further away. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1063 | UseFP = true; |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1064 | } else { |
| 1065 | // We have the choice between FP and (SP or BP). |
| 1066 | if (FPOffsetFits && PreferFP) // If FP is the best fit, use it. |
| 1067 | UseFP = true; |
| 1068 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
| Kristof Beyls | 17cb898 | 2015-04-09 08:49:47 +0000 | [diff] [blame] | 1072 | assert((isFixed || !RegInfo->needsStackRealignment(MF) || !UseFP) && |
| 1073 | "In the presence of dynamic stack pointer realignment, " |
| 1074 | "non-argument objects cannot be accessed through the frame pointer"); |
| 1075 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1076 | if (UseFP) { |
| 1077 | FrameReg = RegInfo->getFrameRegister(MF); |
| 1078 | return FPOffset; |
| 1079 | } |
| 1080 | |
| 1081 | // Use the base pointer if we have one. |
| 1082 | if (RegInfo->hasBasePointer(MF)) |
| 1083 | FrameReg = RegInfo->getBaseRegister(); |
| 1084 | else { |
| Francis Visoiu Mistrih | f2c2205 | 2018-04-10 11:29:40 +0000 | [diff] [blame] | 1085 | assert(!MFI.hasVarSizedObjects() && |
| 1086 | "Can't use SP when we have var sized objects."); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1087 | FrameReg = AArch64::SP; |
| 1088 | // If we're using the red zone for this function, the SP won't actually |
| 1089 | // be adjusted, so the offsets will be negative. They're also all |
| 1090 | // within range of the signed 9-bit immediate instructions. |
| 1091 | if (canUseRedZone(MF)) |
| 1092 | Offset -= AFI->getLocalStackSize(); |
| 1093 | } |
| 1094 | |
| 1095 | return Offset; |
| 1096 | } |
| 1097 | |
| 1098 | static unsigned getPrologueDeath(MachineFunction &MF, unsigned Reg) { |
| Matthias Braun | 74a0bd3 | 2016-04-13 21:43:16 +0000 | [diff] [blame] | 1099 | // Do not set a kill flag on values that are also marked as live-in. This |
| 1100 | // happens with the @llvm-returnaddress intrinsic and with arguments passed in |
| 1101 | // callee saved registers. |
| 1102 | // Omitting the kill flags is conservatively correct even if the live-in |
| 1103 | // is not used after all. |
| 1104 | bool IsLiveIn = MF.getRegInfo().isLiveIn(Reg); |
| 1105 | return getKillRegState(!IsLiveIn); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1108 | static bool produceCompactUnwindFrame(MachineFunction &MF) { |
| 1109 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1110 | AttributeList Attrs = MF.getFunction().getAttributes(); |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1111 | return Subtarget.isTargetMachO() && |
| 1112 | !(Subtarget.getTargetLowering()->supportSwiftError() && |
| 1113 | Attrs.hasAttrSomewhere(Attribute::SwiftError)); |
| 1114 | } |
| 1115 | |
| Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 1116 | namespace { |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 1117 | |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1118 | struct RegPairInfo { |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 1119 | unsigned Reg1 = AArch64::NoRegister; |
| 1120 | unsigned Reg2 = AArch64::NoRegister; |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1121 | int FrameIdx; |
| 1122 | int Offset; |
| 1123 | bool IsGPR; |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 1124 | |
| 1125 | RegPairInfo() = default; |
| 1126 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1127 | bool isPaired() const { return Reg2 != AArch64::NoRegister; } |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1128 | }; |
| Eugene Zelenko | 11f6907 | 2017-01-25 00:29:26 +0000 | [diff] [blame] | 1129 | |
| Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 1130 | } // end anonymous namespace |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1131 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1132 | static void computeCalleeSaveRegisterPairs( |
| 1133 | MachineFunction &MF, const std::vector<CalleeSavedInfo> &CSI, |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1134 | const TargetRegisterInfo *TRI, SmallVectorImpl<RegPairInfo> &RegPairs, |
| 1135 | bool &NeedShadowCallStackProlog) { |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1136 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1137 | if (CSI.empty()) |
| 1138 | return; |
| 1139 | |
| 1140 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1141 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1142 | CallingConv::ID CC = MF.getFunction().getCallingConv(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1143 | unsigned Count = CSI.size(); |
| Roman Levenstein | 2792b3f | 2016-03-10 04:35:09 +0000 | [diff] [blame] | 1144 | (void)CC; |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1145 | // MachO's compact unwind format relies on all registers being stored in |
| 1146 | // pairs. |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1147 | assert((!produceCompactUnwindFrame(MF) || |
| Roman Levenstein | 2792b3f | 2016-03-10 04:35:09 +0000 | [diff] [blame] | 1148 | CC == CallingConv::PreserveMost || |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1149 | (Count & 1) == 0) && |
| 1150 | "Odd number of callee-saved regs to spill!"); |
| Martin Storsjo | 68266fa | 2017-07-13 17:03:12 +0000 | [diff] [blame] | 1151 | int Offset = AFI->getCalleeSavedStackSize(); |
| 1152 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1153 | for (unsigned i = 0; i < Count; ++i) { |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1154 | RegPairInfo RPI; |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1155 | RPI.Reg1 = CSI[i].getReg(); |
| 1156 | |
| 1157 | assert(AArch64::GPR64RegClass.contains(RPI.Reg1) || |
| 1158 | AArch64::FPR64RegClass.contains(RPI.Reg1)); |
| 1159 | RPI.IsGPR = AArch64::GPR64RegClass.contains(RPI.Reg1); |
| 1160 | |
| 1161 | // Add the next reg to the pair if it is in the same register class. |
| 1162 | if (i + 1 < Count) { |
| 1163 | unsigned NextReg = CSI[i + 1].getReg(); |
| 1164 | if ((RPI.IsGPR && AArch64::GPR64RegClass.contains(NextReg)) || |
| 1165 | (!RPI.IsGPR && AArch64::FPR64RegClass.contains(NextReg))) |
| 1166 | RPI.Reg2 = NextReg; |
| 1167 | } |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1168 | |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1169 | // If either of the registers to be saved is the lr register, it means that |
| 1170 | // we also need to save lr in the shadow call stack. |
| 1171 | if ((RPI.Reg1 == AArch64::LR || RPI.Reg2 == AArch64::LR) && |
| 1172 | MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) { |
| 1173 | if (!MF.getSubtarget<AArch64Subtarget>().isX18Reserved()) |
| 1174 | report_fatal_error("Must reserve x18 to use shadow call stack"); |
| 1175 | NeedShadowCallStackProlog = true; |
| 1176 | } |
| 1177 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1178 | // GPRs and FPRs are saved in pairs of 64-bit regs. We expect the CSI |
| 1179 | // list to come in sorted by frame index so that we can issue the store |
| 1180 | // pair instructions directly. Assert if we see anything otherwise. |
| 1181 | // |
| 1182 | // The order of the registers in the list is controlled by |
| 1183 | // getCalleeSavedRegs(), so they will always be in-order, as well. |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1184 | assert((!RPI.isPaired() || |
| 1185 | (CSI[i].getFrameIdx() + 1 == CSI[i + 1].getFrameIdx())) && |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1186 | "Out of order callee saved regs!"); |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1187 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1188 | // MachO's compact unwind format relies on all registers being stored in |
| 1189 | // adjacent register pairs. |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1190 | assert((!produceCompactUnwindFrame(MF) || |
| Roman Levenstein | 2792b3f | 2016-03-10 04:35:09 +0000 | [diff] [blame] | 1191 | CC == CallingConv::PreserveMost || |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1192 | (RPI.isPaired() && |
| 1193 | ((RPI.Reg1 == AArch64::LR && RPI.Reg2 == AArch64::FP) || |
| 1194 | RPI.Reg1 + 1 == RPI.Reg2))) && |
| 1195 | "Callee-save registers not saved as adjacent register pair!"); |
| 1196 | |
| 1197 | RPI.FrameIdx = CSI[i].getFrameIdx(); |
| 1198 | |
| 1199 | if (Count * 8 != AFI->getCalleeSavedStackSize() && !RPI.isPaired()) { |
| 1200 | // Round up size of non-pair to pair size if we need to pad the |
| 1201 | // callee-save area to ensure 16-byte alignment. |
| 1202 | Offset -= 16; |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1203 | assert(MFI.getObjectAlignment(RPI.FrameIdx) <= 16); |
| 1204 | MFI.setObjectAlignment(RPI.FrameIdx, 16); |
| Geoff Berry | 66f6b65 | 2016-06-02 16:22:07 +0000 | [diff] [blame] | 1205 | AFI->setCalleeSaveStackHasFreeSpace(true); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1206 | } else |
| 1207 | Offset -= RPI.isPaired() ? 16 : 8; |
| 1208 | assert(Offset % 8 == 0); |
| 1209 | RPI.Offset = Offset / 8; |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1210 | assert((RPI.Offset >= -64 && RPI.Offset <= 63) && |
| 1211 | "Offset out of bounds for LDP/STP immediate"); |
| 1212 | |
| 1213 | RegPairs.push_back(RPI); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1214 | if (RPI.isPaired()) |
| 1215 | ++i; |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | bool AArch64FrameLowering::spillCalleeSavedRegisters( |
| 1220 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 1221 | const std::vector<CalleeSavedInfo> &CSI, |
| 1222 | const TargetRegisterInfo *TRI) const { |
| 1223 | MachineFunction &MF = *MBB.getParent(); |
| 1224 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
| 1225 | DebugLoc DL; |
| 1226 | SmallVector<RegPairInfo, 8> RegPairs; |
| 1227 | |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1228 | bool NeedShadowCallStackProlog = false; |
| 1229 | computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs, |
| 1230 | NeedShadowCallStackProlog); |
| Matthias Braun | 88c8c98 | 2017-05-27 03:38:02 +0000 | [diff] [blame] | 1231 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1232 | |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1233 | if (NeedShadowCallStackProlog) { |
| 1234 | // Shadow call stack prolog: str x30, [x18], #8 |
| 1235 | BuildMI(MBB, MI, DL, TII.get(AArch64::STRXpost)) |
| 1236 | .addReg(AArch64::X18, RegState::Define) |
| 1237 | .addReg(AArch64::LR) |
| 1238 | .addReg(AArch64::X18) |
| 1239 | .addImm(8) |
| 1240 | .setMIFlag(MachineInstr::FrameSetup); |
| 1241 | |
| 1242 | // This instruction also makes x18 live-in to the entry block. |
| 1243 | MBB.addLiveIn(AArch64::X18); |
| 1244 | } |
| 1245 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1246 | for (auto RPII = RegPairs.rbegin(), RPIE = RegPairs.rend(); RPII != RPIE; |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1247 | ++RPII) { |
| 1248 | RegPairInfo RPI = *RPII; |
| 1249 | unsigned Reg1 = RPI.Reg1; |
| 1250 | unsigned Reg2 = RPI.Reg2; |
| 1251 | unsigned StrOpc; |
| 1252 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1253 | // Issue sequence of spills for cs regs. The first spill may be converted |
| 1254 | // to a pre-decrement store later by emitPrologue if the callee-save stack |
| 1255 | // area allocation can't be combined with the local stack area allocation. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1256 | // For example: |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1257 | // stp x22, x21, [sp, #0] // addImm(+0) |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1258 | // stp x20, x19, [sp, #16] // addImm(+2) |
| 1259 | // stp fp, lr, [sp, #32] // addImm(+4) |
| 1260 | // Rationale: This sequence saves uop updates compared to a sequence of |
| 1261 | // pre-increment spills like stp xi,xj,[sp,#-16]! |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1262 | // Note: Similar rationale and sequence for restores in epilog. |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1263 | if (RPI.IsGPR) |
| 1264 | StrOpc = RPI.isPaired() ? AArch64::STPXi : AArch64::STRXui; |
| 1265 | else |
| 1266 | StrOpc = RPI.isPaired() ? AArch64::STPDi : AArch64::STRDui; |
| Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1267 | DEBUG(dbgs() << "CSR spill: (" << printReg(Reg1, TRI); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1268 | if (RPI.isPaired()) |
| Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1269 | dbgs() << ", " << printReg(Reg2, TRI); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1270 | dbgs() << ") -> fi#(" << RPI.FrameIdx; |
| 1271 | if (RPI.isPaired()) |
| 1272 | dbgs() << ", " << RPI.FrameIdx+1; |
| 1273 | dbgs() << ")\n"); |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1274 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1275 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(StrOpc)); |
| Matthias Braun | 88c8c98 | 2017-05-27 03:38:02 +0000 | [diff] [blame] | 1276 | if (!MRI.isReserved(Reg1)) |
| 1277 | MBB.addLiveIn(Reg1); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1278 | if (RPI.isPaired()) { |
| Matthias Braun | 88c8c98 | 2017-05-27 03:38:02 +0000 | [diff] [blame] | 1279 | if (!MRI.isReserved(Reg2)) |
| 1280 | MBB.addLiveIn(Reg2); |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1281 | MIB.addReg(Reg2, getPrologueDeath(MF, Reg2)); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1282 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 1283 | MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx + 1), |
| 1284 | MachineMemOperand::MOStore, 8, 8)); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1285 | } |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1286 | MIB.addReg(Reg1, getPrologueDeath(MF, Reg1)) |
| 1287 | .addReg(AArch64::SP) |
| 1288 | .addImm(RPI.Offset) // [sp, #offset*8], where factor*8 is implicit |
| 1289 | .setMIFlag(MachineInstr::FrameSetup); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1290 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 1291 | MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx), |
| 1292 | MachineMemOperand::MOStore, 8, 8)); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1293 | } |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | bool AArch64FrameLowering::restoreCalleeSavedRegisters( |
| 1298 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| Krzysztof Parzyszek | bea30c6 | 2017-08-10 16:17:32 +0000 | [diff] [blame] | 1299 | std::vector<CalleeSavedInfo> &CSI, |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1300 | const TargetRegisterInfo *TRI) const { |
| 1301 | MachineFunction &MF = *MBB.getParent(); |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1302 | const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1303 | DebugLoc DL; |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1304 | SmallVector<RegPairInfo, 8> RegPairs; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (MI != MBB.end()) |
| 1307 | DL = MI->getDebugLoc(); |
| 1308 | |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1309 | bool NeedShadowCallStackProlog = false; |
| 1310 | computeCalleeSaveRegisterPairs(MF, CSI, TRI, RegPairs, |
| 1311 | NeedShadowCallStackProlog); |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1312 | |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 1313 | auto EmitMI = [&](const RegPairInfo &RPI) { |
| Geoff Berry | 29d4a69 | 2016-02-01 19:07:06 +0000 | [diff] [blame] | 1314 | unsigned Reg1 = RPI.Reg1; |
| 1315 | unsigned Reg2 = RPI.Reg2; |
| 1316 | |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1317 | // Issue sequence of restores for cs regs. The last restore may be converted |
| 1318 | // to a post-increment load later by emitEpilogue if the callee-save stack |
| 1319 | // area allocation can't be combined with the local stack area allocation. |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1320 | // For example: |
| 1321 | // ldp fp, lr, [sp, #32] // addImm(+4) |
| 1322 | // ldp x20, x19, [sp, #16] // addImm(+2) |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1323 | // ldp x22, x21, [sp, #0] // addImm(+0) |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1324 | // Note: see comment in spillCalleeSavedRegisters() |
| 1325 | unsigned LdrOpc; |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1326 | if (RPI.IsGPR) |
| 1327 | LdrOpc = RPI.isPaired() ? AArch64::LDPXi : AArch64::LDRXui; |
| 1328 | else |
| 1329 | LdrOpc = RPI.isPaired() ? AArch64::LDPDi : AArch64::LDRDui; |
| Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1330 | DEBUG(dbgs() << "CSR restore: (" << printReg(Reg1, TRI); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1331 | if (RPI.isPaired()) |
| Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1332 | dbgs() << ", " << printReg(Reg2, TRI); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1333 | dbgs() << ") -> fi#(" << RPI.FrameIdx; |
| 1334 | if (RPI.isPaired()) |
| 1335 | dbgs() << ", " << RPI.FrameIdx+1; |
| 1336 | dbgs() << ")\n"); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1337 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1338 | MachineInstrBuilder MIB = BuildMI(MBB, MI, DL, TII.get(LdrOpc)); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1339 | if (RPI.isPaired()) { |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1340 | MIB.addReg(Reg2, getDefRegState(true)); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1341 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 1342 | MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx + 1), |
| 1343 | MachineMemOperand::MOLoad, 8, 8)); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1344 | } |
| Geoff Berry | a533564 | 2016-05-06 16:34:59 +0000 | [diff] [blame] | 1345 | MIB.addReg(Reg1, getDefRegState(true)) |
| 1346 | .addReg(AArch64::SP) |
| 1347 | .addImm(RPI.Offset) // [sp, #offset*8] where the factor*8 is implicit |
| 1348 | .setMIFlag(MachineInstr::FrameDestroy); |
| Geoff Berry | c376406 | 2016-04-15 15:16:19 +0000 | [diff] [blame] | 1349 | MIB.addMemOperand(MF.getMachineMemOperand( |
| 1350 | MachinePointerInfo::getFixedStack(MF, RPI.FrameIdx), |
| 1351 | MachineMemOperand::MOLoad, 8, 8)); |
| Francis Visoiu Mistrih | 164560b | 2018-03-14 20:34:03 +0000 | [diff] [blame] | 1352 | }; |
| 1353 | |
| 1354 | if (ReverseCSRRestoreSeq) |
| 1355 | for (const RegPairInfo &RPI : reverse(RegPairs)) |
| 1356 | EmitMI(RPI); |
| 1357 | else |
| 1358 | for (const RegPairInfo &RPI : RegPairs) |
| 1359 | EmitMI(RPI); |
| Peter Collingbourne | f11eb3e | 2018-04-04 21:55:44 +0000 | [diff] [blame] | 1360 | |
| 1361 | if (NeedShadowCallStackProlog) { |
| 1362 | // Shadow call stack epilog: ldr x30, [x18, #-8]! |
| 1363 | BuildMI(MBB, MI, DL, TII.get(AArch64::LDRXpre)) |
| 1364 | .addReg(AArch64::X18, RegState::Define) |
| 1365 | .addReg(AArch64::LR, RegState::Define) |
| 1366 | .addReg(AArch64::X18) |
| 1367 | .addImm(-8) |
| 1368 | .setMIFlag(MachineInstr::FrameDestroy); |
| 1369 | } |
| 1370 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | |
| Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1374 | void AArch64FrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 1375 | BitVector &SavedRegs, |
| 1376 | RegScavenger *RS) const { |
| 1377 | // All calls are tail calls in GHC calling conv, and functions have no |
| 1378 | // prologue/epilogue. |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1379 | if (MF.getFunction().getCallingConv() == CallingConv::GHC) |
| Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | |
| 1382 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1383 | const AArch64RegisterInfo *RegInfo = static_cast<const AArch64RegisterInfo *>( |
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1384 | MF.getSubtarget().getRegisterInfo()); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1385 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1386 | unsigned UnspilledCSGPR = AArch64::NoRegister; |
| 1387 | unsigned UnspilledCSGPRPaired = AArch64::NoRegister; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1388 | |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 1389 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1390 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); |
| 1391 | |
| 1392 | unsigned BasePointerReg = RegInfo->hasBasePointer(MF) |
| 1393 | ? RegInfo->getBaseRegister() |
| 1394 | : (unsigned)AArch64::NoRegister; |
| 1395 | |
| 1396 | unsigned SpillEstimate = SavedRegs.count(); |
| 1397 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 1398 | unsigned Reg = CSRegs[i]; |
| 1399 | unsigned PairedReg = CSRegs[i ^ 1]; |
| 1400 | if (Reg == BasePointerReg) |
| 1401 | SpillEstimate++; |
| 1402 | if (produceCompactUnwindFrame(MF) && !SavedRegs.test(PairedReg)) |
| 1403 | SpillEstimate++; |
| 1404 | } |
| 1405 | SpillEstimate += 2; // Conservatively include FP+LR in the estimate |
| 1406 | unsigned StackEstimate = MFI.estimateStackSize(MF) + 8 * SpillEstimate; |
| 1407 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1408 | // The frame record needs to be created by saving the appropriate registers |
| Martin Storsjo | 2778fd0 | 2017-12-20 06:51:45 +0000 | [diff] [blame] | 1409 | if (hasFP(MF) || windowsRequiresStackProbe(MF, StackEstimate)) { |
| Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 1410 | SavedRegs.set(AArch64::FP); |
| 1411 | SavedRegs.set(AArch64::LR); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| Matthias Braun | d78597e | 2017-04-21 22:42:08 +0000 | [diff] [blame] | 1414 | unsigned ExtraCSSpill = 0; |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1415 | // Figure out which callee-saved registers to save/restore. |
| 1416 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 1417 | const unsigned Reg = CSRegs[i]; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1418 | |
| Geoff Berry | 7e4ba3d | 2016-02-19 18:27:32 +0000 | [diff] [blame] | 1419 | // Add the base pointer register to SavedRegs if it is callee-save. |
| 1420 | if (Reg == BasePointerReg) |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1421 | SavedRegs.set(Reg); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1422 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1423 | bool RegUsed = SavedRegs.test(Reg); |
| 1424 | unsigned PairedReg = CSRegs[i ^ 1]; |
| 1425 | if (!RegUsed) { |
| 1426 | if (AArch64::GPR64RegClass.contains(Reg) && |
| 1427 | !RegInfo->isReservedReg(MF, Reg)) { |
| 1428 | UnspilledCSGPR = Reg; |
| 1429 | UnspilledCSGPRPaired = PairedReg; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1430 | } |
| 1431 | continue; |
| 1432 | } |
| 1433 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1434 | // MachO's compact unwind format relies on all registers being stored in |
| 1435 | // pairs. |
| 1436 | // FIXME: the usual format is actually better if unwinding isn't needed. |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1437 | if (produceCompactUnwindFrame(MF) && !SavedRegs.test(PairedReg)) { |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1438 | SavedRegs.set(PairedReg); |
| Geoff Berry | 74cb718 | 2016-05-16 20:52:28 +0000 | [diff] [blame] | 1439 | if (AArch64::GPR64RegClass.contains(PairedReg) && |
| 1440 | !RegInfo->isReservedReg(MF, PairedReg)) |
| Matthias Braun | d78597e | 2017-04-21 22:42:08 +0000 | [diff] [blame] | 1441 | ExtraCSSpill = PairedReg; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1442 | } |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1443 | } |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1444 | |
| 1445 | DEBUG(dbgs() << "*** determineCalleeSaves\nUsed CSRs:"; |
| Francis Visoiu Mistrih | b52e036 | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 1446 | for (unsigned Reg : SavedRegs.set_bits()) |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1447 | dbgs() << ' ' << printReg(Reg, RegInfo); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1448 | dbgs() << "\n";); |
| 1449 | |
| 1450 | // If any callee-saved registers are used, the frame cannot be eliminated. |
| 1451 | unsigned NumRegsSpilled = SavedRegs.count(); |
| 1452 | bool CanEliminateFrame = NumRegsSpilled == 0; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1453 | |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1454 | // The CSR spill slots have not been allocated yet, so estimateStackSize |
| 1455 | // won't include them. |
| Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1456 | unsigned CFSize = MFI.estimateStackSize(MF) + 8 * NumRegsSpilled; |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1457 | DEBUG(dbgs() << "Estimated stack frame size: " << CFSize << " bytes.\n"); |
| Kristof Beyls | 2af1e90 | 2017-05-30 06:58:41 +0000 | [diff] [blame] | 1458 | unsigned EstimatedStackSizeLimit = estimateRSStackSizeLimit(MF); |
| 1459 | bool BigStack = (CFSize > EstimatedStackSizeLimit); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1460 | if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) |
| 1461 | AFI->setHasStackFrame(true); |
| 1462 | |
| 1463 | // Estimate if we might need to scavenge a register at some point in order |
| 1464 | // to materialize a stack offset. If so, either spill one additional |
| 1465 | // callee-saved register or reserve a special spill slot to facilitate |
| 1466 | // register scavenging. If we already spilled an extra callee-saved register |
| 1467 | // above to keep the number of spills even, we don't need to do anything else |
| 1468 | // here. |
| Matthias Braun | d78597e | 2017-04-21 22:42:08 +0000 | [diff] [blame] | 1469 | if (BigStack) { |
| 1470 | if (!ExtraCSSpill && UnspilledCSGPR != AArch64::NoRegister) { |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1471 | DEBUG(dbgs() << "Spilling " << printReg(UnspilledCSGPR, RegInfo) |
| Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1472 | << " to get a scratch register.\n"); |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1473 | SavedRegs.set(UnspilledCSGPR); |
| 1474 | // MachO's compact unwind format relies on all registers being stored in |
| 1475 | // pairs, so if we need to spill one extra for BigStack, then we need to |
| 1476 | // store the pair. |
| Manman Ren | 5751814 | 2016-04-11 21:08:06 +0000 | [diff] [blame] | 1477 | if (produceCompactUnwindFrame(MF)) |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1478 | SavedRegs.set(UnspilledCSGPRPaired); |
| Matthias Braun | d78597e | 2017-04-21 22:42:08 +0000 | [diff] [blame] | 1479 | ExtraCSSpill = UnspilledCSGPRPaired; |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1480 | NumRegsSpilled = SavedRegs.count(); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | // If we didn't find an extra callee-saved register to spill, create |
| 1484 | // an emergency spill slot. |
| Matthias Braun | d78597e | 2017-04-21 22:42:08 +0000 | [diff] [blame] | 1485 | if (!ExtraCSSpill || MF.getRegInfo().isPhysRegUsed(ExtraCSSpill)) { |
| Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1486 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 1487 | const TargetRegisterClass &RC = AArch64::GPR64RegClass; |
| 1488 | unsigned Size = TRI->getSpillSize(RC); |
| 1489 | unsigned Align = TRI->getSpillAlignment(RC); |
| 1490 | int FI = MFI.CreateStackObject(Size, Align, false); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1491 | RS->addScavengingFrameIndex(FI); |
| 1492 | DEBUG(dbgs() << "No available CS registers, allocated fi#" << FI |
| 1493 | << " as the emergency spill slot.\n"); |
| 1494 | } |
| 1495 | } |
| Geoff Berry | 04bf91a | 2016-02-01 16:29:19 +0000 | [diff] [blame] | 1496 | |
| Geoff Berry | c25d3bd | 2016-02-12 16:31:41 +0000 | [diff] [blame] | 1497 | // Round up to register pair alignment to avoid additional SP adjustment |
| 1498 | // instructions. |
| 1499 | AFI->setCalleeSavedStackSize(alignTo(8 * NumRegsSpilled, 16)); |
| Tim Northover | 3b0846e | 2014-05-24 12:50:23 +0000 | [diff] [blame] | 1500 | } |
| Geoff Berry | 66f6b65 | 2016-06-02 16:22:07 +0000 | [diff] [blame] | 1501 | |
| 1502 | bool AArch64FrameLowering::enableStackSlotScavenging( |
| 1503 | const MachineFunction &MF) const { |
| 1504 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1505 | return AFI->hasCalleeSaveStackFreeSpace(); |
| 1506 | } |