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