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