| 1 | //===- AArch64FrameLowering.cpp - AArch64 Frame Lowering -------*- C++ -*-====// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains the AArch64 implementation of TargetFrameLowering class. |
| 10 | // |
| 11 | // On AArch64, stack frames are structured as follows: |
| 12 | // |
| 13 | // The stack grows downward. |
| 14 | // |
| 15 | // All of the individual frame areas on the frame below are optional, i.e. it's |
| 16 | // possible to create a function so that the particular area isn't present |
| 17 | // in the frame. |
| 18 | // |
| 19 | // At function entry, the "frame" looks as follows: |
| 20 | // |
| 21 | // | | Higher address |
| 22 | // |-----------------------------------| |
| 23 | // | | |
| 24 | // | arguments passed on the stack | |
| 25 | // | | |
| 26 | // |-----------------------------------| <- sp |
| 27 | // | | Lower address |
| 28 | // |
| 29 | // |
| 30 | // After the prologue has run, the frame has the following general structure. |
| 31 | // Note that this doesn't depict the case where a red-zone is used. Also, |
| 32 | // technically the last frame area (VLAs) doesn't get created until in the |
| 33 | // main function body, after the prologue is run. However, it's depicted here |
| 34 | // for completeness. |
| 35 | // |
| 36 | // | | Higher address |
| 37 | // |-----------------------------------| |
| 38 | // | | |
| 39 | // | arguments passed on the stack | |
| 40 | // | | |
| 41 | // |-----------------------------------| |
| 42 | // | | |
| 43 | // | (Win64 only) varargs from reg | |
| 44 | // | | |
| 45 | // |-----------------------------------| |
| 46 | // | | |
| 47 | // | (Win64 only) callee-saved SVE reg | |
| 48 | // | | |
| 49 | // |-----------------------------------| |
| 50 | // | | |
| 51 | // | callee-saved gpr registers | <--. |
| 52 | // | | | On Darwin platforms these |
| 53 | // |- - - - - - - - - - - - - - - - - -| | callee saves are swapped, |
| 54 | // | prev_lr | | (frame record first) |
| 55 | // | prev_fp | <--' |
| 56 | // | async context if needed | |
| 57 | // | (a.k.a. "frame record") | |
| 58 | // |-----------------------------------| <- fp(=x29) |
| 59 | // Default SVE stack layout Split SVE objects |
| 60 | // (aarch64-split-sve-objects=false) (aarch64-split-sve-objects=true) |
| 61 | // |-----------------------------------| |-----------------------------------| |
| 62 | // | <hazard padding> | | callee-saved PPR registers | |
| 63 | // |-----------------------------------| |-----------------------------------| |
| 64 | // | | | PPR stack objects | |
| 65 | // | callee-saved fp/simd/SVE regs | |-----------------------------------| |
| 66 | // | | | <hazard padding> | |
| 67 | // |-----------------------------------| |-----------------------------------| |
| 68 | // | | | callee-saved ZPR/FPR registers | |
| 69 | // | SVE stack objects | |-----------------------------------| |
| 70 | // | | | ZPR stack objects | |
| 71 | // |-----------------------------------| |-----------------------------------| |
| 72 | // ^ NB: FPR CSRs are promoted to ZPRs |
| 73 | // |-----------------------------------| |
| 74 | // |.empty.space.to.make.part.below....| |
| 75 | // |.aligned.in.case.it.needs.more.than| (size of this area is unknown at |
| 76 | // |.the.standard.16-byte.alignment....| compile time; if present) |
| 77 | // |-----------------------------------| |
| 78 | // | local variables of fixed size | |
| 79 | // | including spill slots | |
| 80 | // | <FPR> | |
| 81 | // | <hazard padding> | |
| 82 | // | <GPR> | |
| 83 | // |-----------------------------------| <- bp(not defined by ABI, |
| 84 | // |.variable-sized.local.variables....| LLVM chooses X19) |
| 85 | // |.(VLAs)............................| (size of this area is unknown at |
| 86 | // |...................................| compile time) |
| 87 | // |-----------------------------------| <- sp |
| 88 | // | | Lower address |
| 89 | // |
| 90 | // |
| 91 | // To access the data in a frame, at-compile time, a constant offset must be |
| 92 | // computable from one of the pointers (fp, bp, sp) to access it. The size |
| 93 | // of the areas with a dotted background cannot be computed at compile-time |
| 94 | // if they are present, making it required to have all three of fp, bp and |
| 95 | // sp to be set up to be able to access all contents in the frame areas, |
| 96 | // assuming all of the frame areas are non-empty. |
| 97 | // |
| 98 | // For most functions, some of the frame areas are empty. For those functions, |
| 99 | // it may not be necessary to set up fp or bp: |
| 100 | // * A base pointer is definitely needed when there are both VLAs and local |
| 101 | // variables with more-than-default alignment requirements. |
| 102 | // * A frame pointer is definitely needed when there are local variables with |
| 103 | // more-than-default alignment requirements. |
| 104 | // |
| 105 | // For Darwin platforms the frame-record (fp, lr) is stored at the top of the |
| 106 | // callee-saved area, since the unwind encoding does not allow for encoding |
| 107 | // this dynamically and existing tools depend on this layout. For other |
| 108 | // platforms, the frame-record is stored at the bottom of the (gpr) callee-saved |
| 109 | // area to allow SVE stack objects (allocated directly below the callee-saves, |
| 110 | // if available) to be accessed directly from the framepointer. |
| 111 | // The SVE spill/fill instructions have VL-scaled addressing modes such |
| 112 | // as: |
| 113 | // ldr z8, [fp, #-7 mul vl] |
| 114 | // For SVE the size of the vector length (VL) is not known at compile-time, so |
| 115 | // '#-7 mul vl' is an offset that can only be evaluated at runtime. With this |
| 116 | // layout, we don't need to add an unscaled offset to the framepointer before |
| 117 | // accessing the SVE object in the frame. |
| 118 | // |
| 119 | // In some cases when a base pointer is not strictly needed, it is generated |
| 120 | // anyway when offsets from the frame pointer to access local variables become |
| 121 | // so large that the offset can't be encoded in the immediate fields of loads |
| 122 | // or stores. |
| 123 | // |
| 124 | // Outgoing function arguments must be at the bottom of the stack frame when |
| 125 | // calling another function. If we do not have variable-sized stack objects, we |
| 126 | // can allocate a "reserved call frame" area at the bottom of the local |
| 127 | // variable area, large enough for all outgoing calls. If we do have VLAs, then |
| 128 | // the stack pointer must be decremented and incremented around each call to |
| 129 | // make space for the arguments below the VLAs. |
| 130 | // |
| 131 | // FIXME: also explain the redzone concept. |
| 132 | // |
| 133 | // About stack hazards: Under some SME contexts, a coprocessor with its own |
| 134 | // separate cache can used for FP operations. This can create hazards if the CPU |
| 135 | // and the SME unit try to access the same area of memory, including if the |
| 136 | // access is to an area of the stack. To try to alleviate this we attempt to |
| 137 | // introduce extra padding into the stack frame between FP and GPR accesses, |
| 138 | // controlled by the aarch64-stack-hazard-size option. Without changing the |
| 139 | // layout of the stack frame in the diagram above, a stack object of size |
| 140 | // aarch64-stack-hazard-size is added between GPR and FPR CSRs. Another is added |
| 141 | // to the stack objects section, and stack objects are sorted so that FPR > |
| 142 | // Hazard padding slot > GPRs (where possible). Unfortunately some things are |
| 143 | // not handled well (VLA area, arguments on the stack, objects with both GPR and |
| 144 | // FPR accesses), but if those are controlled by the user then the entire stack |
| 145 | // frame becomes GPR at the start/end with FPR in the middle, surrounded by |
| 146 | // Hazard padding. |
| 147 | // |
| 148 | // An example of the prologue: |
| 149 | // |
| 150 | // .globl __foo |
| 151 | // .align 2 |
| 152 | // __foo: |
| 153 | // Ltmp0: |
| 154 | // .cfi_startproc |
| 155 | // .cfi_personality 155, ___gxx_personality_v0 |
| 156 | // Leh_func_begin: |
| 157 | // .cfi_lsda 16, Lexception33 |
| 158 | // |
| 159 | // stp xa,bx, [sp, -#offset]! |
| 160 | // ... |
| 161 | // stp x28, x27, [sp, #offset-32] |
| 162 | // stp fp, lr, [sp, #offset-16] |
| 163 | // add fp, sp, #offset - 16 |
| 164 | // sub sp, sp, #1360 |
| 165 | // |
| 166 | // The Stack: |
| 167 | // +-------------------------------------------+ |
| 168 | // 10000 | ........ | ........ | ........ | ........ | |
| 169 | // 10004 | ........ | ........ | ........ | ........ | |
| 170 | // +-------------------------------------------+ |
| 171 | // 10008 | ........ | ........ | ........ | ........ | |
| 172 | // 1000c | ........ | ........ | ........ | ........ | |
| 173 | // +===========================================+ |
| 174 | // 10010 | X28 Register | |
| 175 | // 10014 | X28 Register | |
| 176 | // +-------------------------------------------+ |
| 177 | // 10018 | X27 Register | |
| 178 | // 1001c | X27 Register | |
| 179 | // +===========================================+ |
| 180 | // 10020 | Frame Pointer | |
| 181 | // 10024 | Frame Pointer | |
| 182 | // +-------------------------------------------+ |
| 183 | // 10028 | Link Register | |
| 184 | // 1002c | Link Register | |
| 185 | // +===========================================+ |
| 186 | // 10030 | ........ | ........ | ........ | ........ | |
| 187 | // 10034 | ........ | ........ | ........ | ........ | |
| 188 | // +-------------------------------------------+ |
| 189 | // 10038 | ........ | ........ | ........ | ........ | |
| 190 | // 1003c | ........ | ........ | ........ | ........ | |
| 191 | // +-------------------------------------------+ |
| 192 | // |
| 193 | // [sp] = 10030 :: >>initial value<< |
| 194 | // sp = 10020 :: stp fp, lr, [sp, #-16]! |
| 195 | // fp = sp == 10020 :: mov fp, sp |
| 196 | // [sp] == 10020 :: stp x28, x27, [sp, #-16]! |
| 197 | // sp == 10010 :: >>final value<< |
| 198 | // |
| 199 | // The frame pointer (w29) points to address 10020. If we use an offset of |
| 200 | // '16' from 'w29', we get the CFI offsets of -8 for w30, -16 for w29, -24 |
| 201 | // for w27, and -32 for w28: |
| 202 | // |
| 203 | // Ltmp1: |
| 204 | // .cfi_def_cfa w29, 16 |
| 205 | // Ltmp2: |
| 206 | // .cfi_offset w30, -8 |
| 207 | // Ltmp3: |
| 208 | // .cfi_offset w29, -16 |
| 209 | // Ltmp4: |
| 210 | // .cfi_offset w27, -24 |
| 211 | // Ltmp5: |
| 212 | // .cfi_offset w28, -32 |
| 213 | // |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | |
| 216 | #include "AArch64FrameLowering.h" |
| 217 | #include "AArch64InstrInfo.h" |
| 218 | #include "AArch64MachineFunctionInfo.h" |
| 219 | #include "AArch64PrologueEpilogue.h" |
| 220 | #include "AArch64RegisterInfo.h" |
| 221 | #include "AArch64SMEAttributes.h" |
| 222 | #include "AArch64Subtarget.h" |
| 223 | #include "MCTargetDesc/AArch64AddressingModes.h" |
| 224 | #include "MCTargetDesc/AArch64MCTargetDesc.h" |
| 225 | #include "llvm/ADT/ScopeExit.h" |
| 226 | #include "llvm/ADT/SmallVector.h" |
| 227 | #include "llvm/Analysis/ValueTracking.h" |
| 228 | #include "llvm/CodeGen/CFIInstBuilder.h" |
| 229 | #include "llvm/CodeGen/LivePhysRegs.h" |
| 230 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 231 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 232 | #include "llvm/CodeGen/MachineFunction.h" |
| 233 | #include "llvm/CodeGen/MachineInstr.h" |
| 234 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 235 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 236 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 237 | #include "llvm/CodeGen/MachineOperand.h" |
| 238 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 239 | #include "llvm/CodeGen/RegisterScavenging.h" |
| 240 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 241 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 242 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 243 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
| 244 | #include "llvm/IR/Attributes.h" |
| 245 | #include "llvm/IR/CallingConv.h" |
| 246 | #include "llvm/IR/DataLayout.h" |
| 247 | #include "llvm/IR/DebugLoc.h" |
| 248 | #include "llvm/IR/Function.h" |
| 249 | #include "llvm/IR/Module.h" |
| 250 | #include "llvm/MC/MCAsmInfo.h" |
| 251 | #include "llvm/MC/MCDwarf.h" |
| 252 | #include "llvm/Support/Debug.h" |
| 253 | #include "llvm/Support/ErrorHandling.h" |
| 254 | #include "llvm/Support/FormatVariadic.h" |
| 255 | #include "llvm/Support/MathExtras.h" |
| 256 | #include "llvm/Support/raw_ostream.h" |
| 257 | #include "llvm/Target/TargetMachine.h" |
| 258 | #include <cassert> |
| 259 | #include <cstdint> |
| 260 | #include <iterator> |
| 261 | #include <optional> |
| 262 | #include <vector> |
| 263 | |
| 264 | using namespace llvm; |
| 265 | |
| 266 | #define DEBUG_TYPE "frame-info" |
| 267 | |
| 268 | int64_t |
| 269 | AArch64FrameLowering::getArgumentStackToRestore(MachineFunction &MF, |
| 270 | MachineBasicBlock &MBB) const { |
| 271 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 272 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 273 | bool IsTailCallReturn = (MBB.end() != MBBI) |
| 274 | ? AArch64InstrInfo::isTailCallReturnInst(MI: *MBBI) |
| 275 | : false; |
| 276 | |
| 277 | int64_t ArgumentPopSize = 0; |
| 278 | if (IsTailCallReturn) { |
| 279 | MachineOperand &StackAdjust = MBBI->getOperand(i: 1); |
| 280 | |
| 281 | // For a tail-call in a callee-pops-arguments environment, some or all of |
| 282 | // the stack may actually be in use for the call's arguments, this is |
| 283 | // calculated during LowerCall and consumed here... |
| 284 | ArgumentPopSize = StackAdjust.getImm(); |
| 285 | } else { |
| 286 | // ... otherwise the amount to pop is *all* of the argument space, |
| 287 | // conveniently stored in the MachineFunctionInfo by |
| 288 | // LowerFormalArguments. This will, of course, be zero for the C calling |
| 289 | // convention. |
| 290 | ArgumentPopSize = AFI->getArgumentStackToRestore(); |
| 291 | } |
| 292 | |
| 293 | return ArgumentPopSize; |
| 294 | } |
| 295 | |
| 296 | bool AArch64FrameLowering::authenticatesReturnAddressEarly( |
| 297 | MachineFunction &MF, MachineBasicBlock &MBB) const { |
| 298 | if (!MF.needsWinCFI() || |
| 299 | !MF.getInfo<AArch64FunctionInfo>()->shouldSignReturnAddress(MF)) |
| 300 | return false; |
| 301 | |
| 302 | // Funclets don't tail call, and are not what the unwind info is about. |
| 303 | MachineBasicBlock::iterator LastI = MBB.getLastNonDebugInstr(); |
| 304 | if (LastI != MBB.end() && (LastI->getOpcode() == AArch64::CATCHRET || |
| 305 | LastI->getOpcode() == AArch64::CLEANUPRET)) |
| 306 | return false; |
| 307 | |
| 308 | return getArgumentStackToRestore(MF, MBB) != 0; |
| 309 | } |
| 310 | |
| 311 | static bool produceCompactUnwindFrame(const AArch64FrameLowering &, |
| 312 | MachineFunction &MF); |
| 313 | |
| 314 | enum class AssignObjectOffsets { No, Yes }; |
| 315 | /// Process all the SVE stack objects and the SVE stack size and offsets for |
| 316 | /// each object. If AssignOffsets is "Yes", the offsets get assigned (and SVE |
| 317 | /// stack sizes set). Returns the size of the SVE stack. |
| 318 | static SVEStackSizes determineSVEStackSizes(MachineFunction &MF, |
| 319 | AssignObjectOffsets AssignOffsets); |
| 320 | |
| 321 | static unsigned getStackHazardSize(const MachineFunction &MF) { |
| 322 | return MF.getSubtarget<AArch64Subtarget>().getStreamingHazardSize(); |
| 323 | } |
| 324 | |
| 325 | StackOffset |
| 326 | AArch64FrameLowering::getZPRStackSize(const MachineFunction &MF) const { |
| 327 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 328 | return StackOffset::getScalable(Scalable: AFI->getStackSizeZPR()); |
| 329 | } |
| 330 | |
| 331 | StackOffset |
| 332 | AArch64FrameLowering::getPPRStackSize(const MachineFunction &MF) const { |
| 333 | // With split SVE objects, the hazard padding is added to the PPR region, |
| 334 | // which places it between the [GPR, PPR] area and the [ZPR, FPR] area. This |
| 335 | // avoids hazards between both GPRs and FPRs and ZPRs and PPRs. |
| 336 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 337 | return StackOffset::get(Fixed: AFI->hasSplitSVEObjects() ? getStackHazardSize(MF) |
| 338 | : 0, |
| 339 | Scalable: AFI->getStackSizePPR()); |
| 340 | } |
| 341 | |
| 342 | // Conservatively, returns true if the function is likely to have SVE vectors |
| 343 | // on the stack. This function is safe to be called before callee-saves or |
| 344 | // object offsets have been determined. |
| 345 | static bool isLikelyToHaveSVEStack(const AArch64FrameLowering &AFL, |
| 346 | const MachineFunction &MF) { |
| 347 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 348 | if (AFI->isSVECC()) |
| 349 | return true; |
| 350 | |
| 351 | if (AFI->hasCalculatedStackSizeSVE()) |
| 352 | return bool(AFL.getSVEStackSize(MF)); |
| 353 | |
| 354 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 355 | for (int FI = MFI.getObjectIndexBegin(); FI < MFI.getObjectIndexEnd(); FI++) { |
| 356 | if (MFI.hasScalableStackID(ObjectIdx: FI)) |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | static bool isTargetWindows(const MachineFunction &MF) { |
| 364 | // Note: UEFI follows the Windows ARM64 ABI and uses Windows CFI. The value |
| 365 | // returned here must align with the predicate used for returning the list of |
| 366 | // callee saved regs in AArch64RegisterInfo::getCalleeSavedRegs(), so that we |
| 367 | // use invalidateWindowsRegisterPairing() where appropriate. |
| 368 | return MF.getSubtarget<AArch64Subtarget>().isTargetWindowsOrUEFI(); |
| 369 | } |
| 370 | |
| 371 | bool AArch64FrameLowering::hasSVECalleeSavesAboveFrameRecord( |
| 372 | const MachineFunction &MF) const { |
| 373 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 374 | return isTargetWindows(MF) && AFI->getSVECalleeSavedStackSize(); |
| 375 | } |
| 376 | |
| 377 | /// Returns true if a homogeneous prolog or epilog code can be emitted |
| 378 | /// for the size optimization. If possible, a frame helper call is injected. |
| 379 | /// When Exit block is given, this check is for epilog. |
| 380 | bool AArch64FrameLowering::homogeneousPrologEpilog( |
| 381 | MachineFunction &MF, MachineBasicBlock *Exit) const { |
| 382 | if (!MF.getFunction().hasMinSize()) |
| 383 | return false; |
| 384 | const AArch64Options &CLOpts = |
| 385 | MF.getSubtarget<AArch64Subtarget>().getCLOpts(); |
| 386 | if (!CLOpts.homogeneous_prolog_epilog) |
| 387 | return false; |
| 388 | if (CLOpts.redzone) |
| 389 | return false; |
| 390 | |
| 391 | // TODO: Window is supported yet. |
| 392 | if (isTargetWindows(MF)) |
| 393 | return false; |
| 394 | |
| 395 | // TODO: SVE is not supported yet. |
| 396 | if (isLikelyToHaveSVEStack(AFL: *this, MF)) |
| 397 | return false; |
| 398 | |
| 399 | // Bail on stack adjustment needed on return for simplicity. |
| 400 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 401 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
| 402 | if (MFI.hasVarSizedObjects() || RegInfo->hasStackRealignment(MF)) |
| 403 | return false; |
| 404 | if (Exit && getArgumentStackToRestore(MF, MBB&: *Exit)) |
| 405 | return false; |
| 406 | |
| 407 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 408 | if (AFI->hasSwiftAsyncContext() || AFI->hasStreamingModeChanges()) |
| 409 | return false; |
| 410 | |
| 411 | // If there are an odd number of GPRs before LR and FP in the CSRs list, |
| 412 | // they will not be paired into one RegGroupInfo, which is incompatible with |
| 413 | // the assumption made by the homogeneous prolog epilog pass. |
| 414 | const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); |
| 415 | unsigned NumGPRs = 0; |
| 416 | for (unsigned I = 0; CSRegs[I]; ++I) { |
| 417 | Register Reg = CSRegs[I]; |
| 418 | if (Reg == AArch64::LR) { |
| 419 | assert(CSRegs[I + 1] == AArch64::FP); |
| 420 | if (NumGPRs % 2 != 0) |
| 421 | return false; |
| 422 | break; |
| 423 | } |
| 424 | if (AArch64::GPR64RegClass.contains(Reg)) |
| 425 | ++NumGPRs; |
| 426 | } |
| 427 | |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | /// Returns true if CSRs should be paired. |
| 432 | bool AArch64FrameLowering::producePairRegisters(MachineFunction &MF) const { |
| 433 | return produceCompactUnwindFrame(*this, MF) || homogeneousPrologEpilog(MF); |
| 434 | } |
| 435 | |
| 436 | /// This is the biggest offset to the stack pointer we can encode in aarch64 |
| 437 | /// instructions (without using a separate calculation and a temp register). |
| 438 | /// Note that the exception here are vector stores/loads which cannot encode any |
| 439 | /// displacements (see estimateRSStackSizeLimit(), isAArch64FrameOffsetLegal()). |
| 440 | static const unsigned DefaultSafeSPDisplacement = 255; |
| 441 | |
| 442 | /// Look at each instruction that references stack frames and return the stack |
| 443 | /// size limit beyond which some of these instructions will require a scratch |
| 444 | /// register during their expansion later. |
| 445 | static unsigned (MachineFunction &MF) { |
| 446 | // FIXME: For now, just conservatively guesstimate based on unscaled indexing |
| 447 | // range. We'll end up allocating an unnecessary spill slot a lot, but |
| 448 | // realistically that's not a big deal at this stage of the game. |
| 449 | for (MachineBasicBlock &MBB : MF) { |
| 450 | for (MachineInstr &MI : MBB) { |
| 451 | if (MI.isDebugInstr() || MI.isPseudo() || |
| 452 | MI.getOpcode() == AArch64::ADDXri || |
| 453 | MI.getOpcode() == AArch64::ADDSXri) |
| 454 | continue; |
| 455 | |
| 456 | for (const MachineOperand &MO : MI.operands()) { |
| 457 | if (!MO.isFI()) |
| 458 | continue; |
| 459 | |
| 460 | StackOffset Offset; |
| 461 | if (isAArch64FrameOffsetLegal(MI, Offset, OutUseUnscaledOp: nullptr, OutUnscaledOp: nullptr, EmittableOffset: nullptr) == |
| 462 | AArch64FrameOffsetCannotUpdate) |
| 463 | return 0; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | return DefaultSafeSPDisplacement; |
| 468 | } |
| 469 | |
| 470 | TargetStackID::Value |
| 471 | AArch64FrameLowering::getStackIDForScalableVectors() const { |
| 472 | return TargetStackID::ScalableVector; |
| 473 | } |
| 474 | |
| 475 | unsigned |
| 476 | AArch64FrameLowering::getFixedObjectSize(const MachineFunction &MF, |
| 477 | const AArch64FunctionInfo *AFI, |
| 478 | bool IsWin64, bool IsFunclet) const { |
| 479 | assert(AFI->getTailCallReservedStack() % 16 == 0 && |
| 480 | "Tail call reserved stack must be aligned to 16 bytes" ); |
| 481 | if (!IsWin64) { |
| 482 | return AFI->getTailCallReservedStack(); |
| 483 | } else if (IsFunclet) { |
| 484 | // A funclet has its own frame; the tail call reserve belongs to the parent |
| 485 | // function's incoming argument area. |
| 486 | return 0; |
| 487 | } else { |
| 488 | unsigned FixedObjectSize = AFI->getTailCallReservedStack(); |
| 489 | |
| 490 | // Var args are stored here in the primary function. |
| 491 | FixedObjectSize += AFI->getVarArgsGPRSize(); |
| 492 | |
| 493 | if (MF.hasEHFunclets()) { |
| 494 | // Catch objects are stored here in the primary function. |
| 495 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 496 | const WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo(); |
| 497 | SmallSetVector<int, 8> CatchObjFrameIndices; |
| 498 | for (const WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 499 | for (const WinEHHandlerType &H : TBME.HandlerArray) { |
| 500 | int FrameIndex = H.CatchObj.FrameIndex; |
| 501 | if ((FrameIndex != INT_MAX) && |
| 502 | CatchObjFrameIndices.insert(X: FrameIndex)) { |
| 503 | FixedObjectSize = alignTo(Value: FixedObjectSize, |
| 504 | Align: MFI.getObjectAlign(ObjectIdx: FrameIndex).value()) + |
| 505 | MFI.getObjectSize(ObjectIdx: FrameIndex); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | // To support EH funclets we allocate an UnwindHelp object |
| 510 | FixedObjectSize += 8; |
| 511 | } |
| 512 | return alignTo(Value: FixedObjectSize, Align: 16); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | bool AArch64FrameLowering::canUseRedZone(const MachineFunction &MF) const { |
| 517 | if (!MF.getSubtarget<AArch64Subtarget>().getCLOpts().redzone) |
| 518 | return false; |
| 519 | |
| 520 | // Don't use the red zone if the function explicitly asks us not to. |
| 521 | // This is typically used for kernel code. |
| 522 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 523 | const unsigned RedZoneSize = |
| 524 | Subtarget.getTargetLowering()->getRedZoneSize(F: MF.getFunction()); |
| 525 | if (!RedZoneSize) |
| 526 | return false; |
| 527 | |
| 528 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 529 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 530 | uint64_t NumBytes = AFI->getLocalStackSize(); |
| 531 | |
| 532 | // If neither NEON or SVE are available, a COPY from one Q-reg to |
| 533 | // another requires a spill -> reload sequence. We can do that |
| 534 | // using a pre-decrementing store/post-decrementing load, but |
| 535 | // if we do so, we can't use the Red Zone. |
| 536 | bool LowerQRegCopyThroughMem = Subtarget.hasFPARMv8() && |
| 537 | !Subtarget.isNeonAvailable() && |
| 538 | !Subtarget.hasSVE(); |
| 539 | |
| 540 | return !(MFI.hasCalls() || hasFP(MF) || NumBytes > RedZoneSize || |
| 541 | AFI->hasSVEStackSize() || LowerQRegCopyThroughMem); |
| 542 | } |
| 543 | |
| 544 | /// hasFPImpl - Return true if the specified function should have a dedicated |
| 545 | /// frame pointer register. |
| 546 | bool AArch64FrameLowering::hasFPImpl(const MachineFunction &MF) const { |
| 547 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 548 | const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); |
| 549 | const AArch64FunctionInfo &AFI = *MF.getInfo<AArch64FunctionInfo>(); |
| 550 | |
| 551 | // Win64 EH requires a frame pointer if funclets are present, as the locals |
| 552 | // are accessed off the frame pointer in both the parent function and the |
| 553 | // funclets. |
| 554 | if (MF.hasEHFunclets()) |
| 555 | return true; |
| 556 | |
| 557 | // Unwinding out of a callee that pops stack arguments we reserved in our |
| 558 | // prologue leaves SP higher than our unwind info expects, so on Windows we |
| 559 | // must be found via the frame pointer instead. |
| 560 | if (AFI.hasWinCallToCalleePopFunction()) |
| 561 | return true; |
| 562 | |
| 563 | // When the stack guard is mixed with the frame pointer, a dedicated FP is |
| 564 | // required so the guard value remains stable in the presence of dynamic |
| 565 | // stack allocations (e.g. _alloca on MSVCRT). |
| 566 | if (MFI.hasStackProtectorIndex()) { |
| 567 | const auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 568 | if (Subtarget.getTargetLowering()->useStackGuardMixFP()) |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // Retain behavior of always omitting the FP for leaf functions when possible. |
| 573 | if (MF.disableFramePointerElim()) |
| 574 | return true; |
| 575 | if (MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() || |
| 576 | MFI.hasStackMap() || MFI.hasPatchPoint() || |
| 577 | RegInfo->hasStackRealignment(MF)) |
| 578 | return true; |
| 579 | |
| 580 | // If we: |
| 581 | // |
| 582 | // 1. Have streaming mode changes |
| 583 | // OR: |
| 584 | // 2. Have a streaming body with SVE stack objects |
| 585 | // |
| 586 | // Then the value of VG restored when unwinding to this function may not match |
| 587 | // the value of VG used to set up the stack. |
| 588 | // |
| 589 | // This is a problem as the CFA can be described with an expression of the |
| 590 | // form: CFA = SP + NumBytes + VG * NumScalableBytes. |
| 591 | // |
| 592 | // If the value of VG used in that expression does not match the value used to |
| 593 | // set up the stack, an incorrect address for the CFA will be computed, and |
| 594 | // unwinding will fail. |
| 595 | // |
| 596 | // We work around this issue by ensuring the frame-pointer can describe the |
| 597 | // CFA in either of these cases. |
| 598 | if (AFI.needsDwarfUnwindInfo(MF) && |
| 599 | ((requiresSaveVG(MF) || AFI.getSMEFnAttrs().hasStreamingBody()) && |
| 600 | (!AFI.hasCalculatedStackSizeSVE() || AFI.hasSVEStackSize()))) |
| 601 | return true; |
| 602 | // With large callframes around we may need to use FP to access the scavenging |
| 603 | // emergency spillslot. |
| 604 | // |
| 605 | // Unfortunately some calls to hasFP() like machine verifier -> |
| 606 | // getReservedReg() -> hasFP in the middle of global isel are too early |
| 607 | // to know the max call frame size. Hopefully conservatively returning "true" |
| 608 | // in those cases is fine. |
| 609 | // DefaultSafeSPDisplacement is fine as we only emergency spill GP regs. |
| 610 | if (!MFI.isMaxCallFrameSizeComputed() || |
| 611 | MFI.getMaxCallFrameSize() > DefaultSafeSPDisplacement) |
| 612 | return true; |
| 613 | |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | /// Should the Frame Pointer be reserved for the current function? |
| 618 | bool AArch64FrameLowering::isFPReserved(const MachineFunction &MF) const { |
| 619 | const Triple &TT = MF.getFunction().getParent()->getTargetTriple(); |
| 620 | |
| 621 | // These OSes require the frame chain is valid, even if the current frame does |
| 622 | // not use a frame pointer. |
| 623 | if (TT.isOSDarwin() || TT.isOSWindowsOrUEFI()) |
| 624 | return true; |
| 625 | |
| 626 | // If the function has a frame pointer, it is reserved. |
| 627 | if (hasFP(MF)) |
| 628 | return true; |
| 629 | |
| 630 | // Frontend has requested to preserve the frame pointer. |
| 631 | if (MF.framePointerIsReserved()) |
| 632 | return true; |
| 633 | |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is |
| 638 | /// not required, we reserve argument space for call sites in the function |
| 639 | /// immediately on entry to the current function. This eliminates the need for |
| 640 | /// add/sub sp brackets around call sites. Returns true if the call frame is |
| 641 | /// included as part of the stack frame. |
| 642 | bool AArch64FrameLowering::hasReservedCallFrame( |
| 643 | const MachineFunction &MF) const { |
| 644 | // The stack probing code for the dynamically allocated outgoing arguments |
| 645 | // area assumes that the stack is probed at the top - either by the prologue |
| 646 | // code, which issues a probe if `hasVarSizedObjects` return true, or by the |
| 647 | // most recent variable-sized object allocation. Changing the condition here |
| 648 | // may need to be followed up by changes to the probe issuing logic. |
| 649 | return !MF.getFrameInfo().hasVarSizedObjects(); |
| 650 | } |
| 651 | |
| 652 | MachineBasicBlock::iterator AArch64FrameLowering::eliminateCallFramePseudoInstr( |
| 653 | MachineFunction &MF, MachineBasicBlock &MBB, |
| 654 | MachineBasicBlock::iterator I) const { |
| 655 | |
| 656 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 657 | const AArch64InstrInfo *TII = Subtarget.getInstrInfo(); |
| 658 | const AArch64TargetLowering *TLI = Subtarget.getTargetLowering(); |
| 659 | [[maybe_unused]] MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 660 | DebugLoc DL = I->getDebugLoc(); |
| 661 | unsigned Opc = I->getOpcode(); |
| 662 | bool IsDestroy = Opc == TII->getCallFrameDestroyOpcode(); |
| 663 | uint64_t CalleePopAmount = IsDestroy ? I->getOperand(i: 1).getImm() : 0; |
| 664 | |
| 665 | if (!hasReservedCallFrame(MF)) { |
| 666 | int64_t Amount = I->getOperand(i: 0).getImm(); |
| 667 | Amount = alignTo(Size: Amount, A: getStackAlign()); |
| 668 | if (!IsDestroy) |
| 669 | Amount = -Amount; |
| 670 | |
| 671 | // N.b. if CalleePopAmount is valid but zero (i.e. callee would pop, but it |
| 672 | // doesn't have to pop anything), then the first operand will be zero too so |
| 673 | // this adjustment is a no-op. |
| 674 | if (CalleePopAmount == 0) { |
| 675 | // FIXME: in-function stack adjustment for calls is limited to 24-bits |
| 676 | // because there's no guaranteed temporary register available. |
| 677 | // |
| 678 | // ADD/SUB (immediate) has only LSL #0 and LSL #12 available. |
| 679 | // 1) For offset <= 12-bit, we use LSL #0 |
| 680 | // 2) For 12-bit <= offset <= 24-bit, we use two instructions. One uses |
| 681 | // LSL #0, and the other uses LSL #12. |
| 682 | // |
| 683 | // Most call frames will be allocated at the start of a function so |
| 684 | // this is OK, but it is a limitation that needs dealing with. |
| 685 | assert(Amount > -0xffffff && Amount < 0xffffff && "call frame too large" ); |
| 686 | |
| 687 | if (TLI->hasInlineStackProbe(MF) && |
| 688 | -Amount >= AArch64::StackProbeMaxUnprobedStack) { |
| 689 | // When stack probing is enabled, the decrement of SP may need to be |
| 690 | // probed. We only need to do this if the call site needs 1024 bytes of |
| 691 | // space or more, because a region smaller than that is allowed to be |
| 692 | // unprobed at an ABI boundary. We rely on the fact that SP has been |
| 693 | // probed exactly at this point, either by the prologue or most recent |
| 694 | // dynamic allocation. |
| 695 | assert(MFI.hasVarSizedObjects() && |
| 696 | "non-reserved call frame without var sized objects?" ); |
| 697 | Register ScratchReg = |
| 698 | MF.getRegInfo().createVirtualRegister(RegClass: &AArch64::GPR64RegClass); |
| 699 | inlineStackProbeFixed(MBBI: I, ScratchReg, FrameSize: -Amount, CFAOffset: StackOffset::get(Fixed: 0, Scalable: 0)); |
| 700 | } else { |
| 701 | emitFrameOffset(MBB, MBBI: I, DL, DestReg: AArch64::SP, SrcReg: AArch64::SP, |
| 702 | Offset: StackOffset::getFixed(Fixed: Amount), TII); |
| 703 | } |
| 704 | } |
| 705 | } else if (CalleePopAmount != 0) { |
| 706 | // If the calling convention demands that the callee pops arguments from the |
| 707 | // stack, we want to add it back if we have a reserved call frame. |
| 708 | assert(CalleePopAmount < 0xffffff && "call frame too large" ); |
| 709 | emitFrameOffset(MBB, MBBI: I, DL, DestReg: AArch64::SP, SrcReg: AArch64::SP, |
| 710 | Offset: StackOffset::getFixed(Fixed: -(int64_t)CalleePopAmount), TII); |
| 711 | } |
| 712 | return MBB.erase(I); |
| 713 | } |
| 714 | |
| 715 | void AArch64FrameLowering::resetCFIToInitialState( |
| 716 | MachineBasicBlock &MBB) const { |
| 717 | |
| 718 | MachineFunction &MF = *MBB.getParent(); |
| 719 | const auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 720 | const auto &TRI = *Subtarget.getRegisterInfo(); |
| 721 | const auto &MFI = *MF.getInfo<AArch64FunctionInfo>(); |
| 722 | |
| 723 | CFIInstBuilder CFIBuilder(MBB, MBB.begin(), MachineInstr::NoFlags); |
| 724 | |
| 725 | // Reset the CFA to `SP + 0`. |
| 726 | CFIBuilder.buildDefCFA(Reg: AArch64::SP, Offset: 0); |
| 727 | |
| 728 | // Flip the RA sign state. |
| 729 | if (MFI.shouldSignReturnAddress(MF)) { |
| 730 | if (MFI.branchProtectionPAuthLR()) { |
| 731 | CFIBuilder.buildNegateRAStateWithPC(); |
| 732 | } else if (!MF.getTarget().getTargetTriple().isOSBinFormatMachO()) { |
| 733 | CFIBuilder.buildNegateRAState(); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // Shadow call stack uses X18, reset it. |
| 738 | if (MFI.needsShadowCallStackPrologueEpilogue(MF)) |
| 739 | CFIBuilder.buildSameValue(Reg: AArch64::X18); |
| 740 | |
| 741 | // Emit .cfi_same_value for callee-saved registers. |
| 742 | const std::vector<CalleeSavedInfo> &CSI = |
| 743 | MF.getFrameInfo().getCalleeSavedInfo(); |
| 744 | for (const auto &Info : CSI) { |
| 745 | MCRegister Reg = Info.getReg(); |
| 746 | if (!TRI.regNeedsCFI(Reg, RegToUseForCFI&: Reg)) |
| 747 | continue; |
| 748 | CFIBuilder.buildSameValue(Reg); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static MCRegister getRegisterOrZero(MCRegister Reg, bool HasSVE) { |
| 753 | switch (Reg.id()) { |
| 754 | default: |
| 755 | // The called routine is expected to preserve r19-r28 |
| 756 | // r29 and r30 are used as frame pointer and link register resp. |
| 757 | return 0; |
| 758 | |
| 759 | // GPRs |
| 760 | #define CASE(n) \ |
| 761 | case AArch64::W##n: \ |
| 762 | case AArch64::X##n: \ |
| 763 | return AArch64::X##n |
| 764 | CASE(0); |
| 765 | CASE(1); |
| 766 | CASE(2); |
| 767 | CASE(3); |
| 768 | CASE(4); |
| 769 | CASE(5); |
| 770 | CASE(6); |
| 771 | CASE(7); |
| 772 | CASE(8); |
| 773 | CASE(9); |
| 774 | CASE(10); |
| 775 | CASE(11); |
| 776 | CASE(12); |
| 777 | CASE(13); |
| 778 | CASE(14); |
| 779 | CASE(15); |
| 780 | CASE(16); |
| 781 | CASE(17); |
| 782 | CASE(18); |
| 783 | #undef CASE |
| 784 | |
| 785 | // FPRs |
| 786 | #define CASE(n) \ |
| 787 | case AArch64::B##n: \ |
| 788 | case AArch64::H##n: \ |
| 789 | case AArch64::S##n: \ |
| 790 | case AArch64::D##n: \ |
| 791 | case AArch64::Q##n: \ |
| 792 | return HasSVE ? AArch64::Z##n : AArch64::Q##n |
| 793 | CASE(0); |
| 794 | CASE(1); |
| 795 | CASE(2); |
| 796 | CASE(3); |
| 797 | CASE(4); |
| 798 | CASE(5); |
| 799 | CASE(6); |
| 800 | CASE(7); |
| 801 | CASE(8); |
| 802 | CASE(9); |
| 803 | CASE(10); |
| 804 | CASE(11); |
| 805 | CASE(12); |
| 806 | CASE(13); |
| 807 | CASE(14); |
| 808 | CASE(15); |
| 809 | CASE(16); |
| 810 | CASE(17); |
| 811 | CASE(18); |
| 812 | CASE(19); |
| 813 | CASE(20); |
| 814 | CASE(21); |
| 815 | CASE(22); |
| 816 | CASE(23); |
| 817 | CASE(24); |
| 818 | CASE(25); |
| 819 | CASE(26); |
| 820 | CASE(27); |
| 821 | CASE(28); |
| 822 | CASE(29); |
| 823 | CASE(30); |
| 824 | CASE(31); |
| 825 | #undef CASE |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void AArch64FrameLowering::emitZeroCallUsedRegs(BitVector RegsToZero, |
| 830 | MachineBasicBlock &MBB, |
| 831 | RegScavenger *) const { |
| 832 | // Insertion point. |
| 833 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 834 | |
| 835 | // Fake a debug loc. |
| 836 | DebugLoc DL; |
| 837 | if (MBBI != MBB.end()) |
| 838 | DL = MBBI->getDebugLoc(); |
| 839 | |
| 840 | const MachineFunction &MF = *MBB.getParent(); |
| 841 | const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>(); |
| 842 | const AArch64RegisterInfo &TRI = *STI.getRegisterInfo(); |
| 843 | |
| 844 | BitVector GPRsToZero(TRI.getNumRegs()); |
| 845 | BitVector FPRsToZero(TRI.getNumRegs()); |
| 846 | bool HasSVE = STI.isSVEorStreamingSVEAvailable(); |
| 847 | // Without an FP unit (e.g. -mgeneral-regs-only) the FP/vector registers can't |
| 848 | // hold a value and there is no instruction to clear them, so leave them out. |
| 849 | bool HasFPR = STI.hasFPARMv8(); |
| 850 | for (MCRegister Reg : RegsToZero.set_bits()) { |
| 851 | if (TRI.isGeneralPurposeRegister(MF, Reg)) { |
| 852 | // For GPRs, we only care to clear out the 64-bit register. |
| 853 | if (MCRegister XReg = getRegisterOrZero(Reg, HasSVE)) |
| 854 | GPRsToZero.set(XReg); |
| 855 | } else if (HasFPR && AArch64InstrInfo::isFpOrNEON(Reg)) { |
| 856 | // For FPRs, |
| 857 | if (MCRegister XReg = getRegisterOrZero(Reg, HasSVE)) |
| 858 | FPRsToZero.set(XReg); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | const AArch64InstrInfo &TII = *STI.getInstrInfo(); |
| 863 | |
| 864 | // Zero out GPRs. |
| 865 | for (MCRegister Reg : GPRsToZero.set_bits()) |
| 866 | TII.buildClearRegister(Reg, MBB, Iter: MBBI, DL); |
| 867 | |
| 868 | // Zero out FP/vector registers. |
| 869 | for (MCRegister Reg : FPRsToZero.set_bits()) |
| 870 | TII.buildClearRegister(Reg, MBB, Iter: MBBI, DL); |
| 871 | |
| 872 | if (HasSVE) { |
| 873 | for (MCRegister PReg : |
| 874 | {AArch64::P0, AArch64::P1, AArch64::P2, AArch64::P3, AArch64::P4, |
| 875 | AArch64::P5, AArch64::P6, AArch64::P7, AArch64::P8, AArch64::P9, |
| 876 | AArch64::P10, AArch64::P11, AArch64::P12, AArch64::P13, AArch64::P14, |
| 877 | AArch64::P15}) { |
| 878 | if (RegsToZero[PReg]) |
| 879 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::PFALSE), DestReg: PReg); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | bool AArch64FrameLowering::windowsRequiresStackProbe( |
| 885 | const MachineFunction &MF, uint64_t StackSizeInBytes) const { |
| 886 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 887 | const AArch64FunctionInfo &MFI = *MF.getInfo<AArch64FunctionInfo>(); |
| 888 | // TODO: When implementing stack protectors, take that into account |
| 889 | // for the probe threshold. |
| 890 | return Subtarget.isTargetWindowsOrUEFI() && MFI.hasStackProbing() && |
| 891 | StackSizeInBytes >= uint64_t(MFI.getStackProbeSize()); |
| 892 | } |
| 893 | |
| 894 | static void getLiveRegsForEntryMBB(LivePhysRegs &LiveRegs, |
| 895 | const MachineBasicBlock &MBB) { |
| 896 | const MachineFunction *MF = MBB.getParent(); |
| 897 | LiveRegs.addLiveIns(MBB); |
| 898 | // Mark callee saved registers as used so we will not choose them. |
| 899 | const MCPhysReg *CSRegs = MF->getRegInfo().getCalleeSavedRegs(); |
| 900 | for (unsigned i = 0; CSRegs[i]; ++i) |
| 901 | LiveRegs.addReg(Reg: CSRegs[i]); |
| 902 | } |
| 903 | |
| 904 | Register |
| 905 | AArch64FrameLowering::findScratchNonCalleeSaveRegister(MachineBasicBlock *MBB, |
| 906 | bool HasCall) const { |
| 907 | MachineFunction *MF = MBB->getParent(); |
| 908 | |
| 909 | // If MBB is an entry block, use X9 as the scratch register |
| 910 | // preserve_none functions may be using X9 to pass arguments, |
| 911 | // so prefer to pick an available register below. |
| 912 | if (&MF->front() == MBB && |
| 913 | MF->getFunction().getCallingConv() != CallingConv::PreserveNone) |
| 914 | return AArch64::X9; |
| 915 | |
| 916 | const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>(); |
| 917 | const AArch64RegisterInfo &TRI = *Subtarget.getRegisterInfo(); |
| 918 | LivePhysRegs LiveRegs(TRI); |
| 919 | getLiveRegsForEntryMBB(LiveRegs, MBB: *MBB); |
| 920 | if (HasCall) { |
| 921 | LiveRegs.addReg(Reg: AArch64::X16); |
| 922 | LiveRegs.addReg(Reg: AArch64::X17); |
| 923 | LiveRegs.addReg(Reg: AArch64::X18); |
| 924 | } |
| 925 | |
| 926 | // Prefer X9 since it was historically used for the prologue scratch reg. |
| 927 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 928 | if (LiveRegs.available(MRI, Reg: AArch64::X9)) |
| 929 | return AArch64::X9; |
| 930 | |
| 931 | for (unsigned Reg : AArch64::GPR64RegClass) { |
| 932 | if (LiveRegs.available(MRI, Reg)) |
| 933 | return Reg; |
| 934 | } |
| 935 | return Register(); |
| 936 | } |
| 937 | |
| 938 | bool AArch64FrameLowering::canUseAsPrologue( |
| 939 | const MachineBasicBlock &MBB) const { |
| 940 | const MachineFunction *MF = MBB.getParent(); |
| 941 | MachineBasicBlock *TmpMBB = const_cast<MachineBasicBlock *>(&MBB); |
| 942 | const AArch64Subtarget &Subtarget = MF->getSubtarget<AArch64Subtarget>(); |
| 943 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 944 | const AArch64TargetLowering *TLI = Subtarget.getTargetLowering(); |
| 945 | const AArch64FunctionInfo *AFI = MF->getInfo<AArch64FunctionInfo>(); |
| 946 | |
| 947 | if (AFI->hasSwiftAsyncContext()) { |
| 948 | const AArch64RegisterInfo &TRI = *Subtarget.getRegisterInfo(); |
| 949 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 950 | LivePhysRegs LiveRegs(TRI); |
| 951 | getLiveRegsForEntryMBB(LiveRegs, MBB); |
| 952 | // The StoreSwiftAsyncContext clobbers X16 and X17. Make sure they are |
| 953 | // available. |
| 954 | if (!LiveRegs.available(MRI, Reg: AArch64::X16) || |
| 955 | !LiveRegs.available(MRI, Reg: AArch64::X17)) |
| 956 | return false; |
| 957 | } |
| 958 | |
| 959 | // Certain stack probing sequences might clobber flags, then we can't use |
| 960 | // the block as a prologue if the flags register is a live-in. |
| 961 | if (MF->getInfo<AArch64FunctionInfo>()->hasStackProbing() && |
| 962 | MBB.isLiveIn(Reg: AArch64::NZCV)) |
| 963 | return false; |
| 964 | |
| 965 | if (RegInfo->hasStackRealignment(MF: *MF) || TLI->hasInlineStackProbe(MF: *MF)) |
| 966 | if (!findScratchNonCalleeSaveRegister(MBB: TmpMBB).isValid()) |
| 967 | return false; |
| 968 | |
| 969 | // May need a scratch register (for return value) if require making a special |
| 970 | // call |
| 971 | if (requiresSaveVG(MF: *MF) || |
| 972 | windowsRequiresStackProbe(MF: *MF, StackSizeInBytes: std::numeric_limits<uint64_t>::max())) |
| 973 | if (!findScratchNonCalleeSaveRegister(MBB: TmpMBB, HasCall: true).isValid()) |
| 974 | return false; |
| 975 | |
| 976 | return true; |
| 977 | } |
| 978 | |
| 979 | bool AArch64FrameLowering::shouldSignReturnAddressEverywhere( |
| 980 | const MachineFunction &MF) const { |
| 981 | // FIXME: With WinCFI, extra care should be taken to place SEH_PACSignLR |
| 982 | // and SEH_EpilogEnd instructions in the correct order. |
| 983 | if (MF.usesWindowsUnwindInfo()) |
| 984 | return false; |
| 985 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 986 | return AFI->getSignReturnAddressCondition() == SignReturnAddress::All; |
| 987 | } |
| 988 | |
| 989 | // Given a load or a store instruction, generate an appropriate unwinding SEH |
| 990 | // code on Windows. |
| 991 | MachineBasicBlock::iterator |
| 992 | AArch64FrameLowering::insertSEH(MachineBasicBlock::iterator MBBI, |
| 993 | const AArch64InstrInfo &TII, |
| 994 | MachineInstr::MIFlag Flag) const { |
| 995 | unsigned Opc = MBBI->getOpcode(); |
| 996 | MachineBasicBlock *MBB = MBBI->getParent(); |
| 997 | MachineFunction &MF = *MBB->getParent(); |
| 998 | DebugLoc DL = MBBI->getDebugLoc(); |
| 999 | unsigned ImmIdx = MBBI->getNumOperands() - 1; |
| 1000 | int Imm = MBBI->getOperand(i: ImmIdx).getImm(); |
| 1001 | MachineInstrBuilder MIB; |
| 1002 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1003 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 1004 | |
| 1005 | switch (Opc) { |
| 1006 | default: |
| 1007 | report_fatal_error(reason: "No SEH Opcode for this instruction" ); |
| 1008 | case AArch64::STR_ZXI: |
| 1009 | case AArch64::LDR_ZXI: { |
| 1010 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1011 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveZReg)) |
| 1012 | .addImm(Val: Reg0) |
| 1013 | .addImm(Val: Imm) |
| 1014 | .setMIFlag(Flag); |
| 1015 | break; |
| 1016 | } |
| 1017 | case AArch64::STR_PXI: |
| 1018 | case AArch64::LDR_PXI: { |
| 1019 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1020 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SavePReg)) |
| 1021 | .addImm(Val: Reg0) |
| 1022 | .addImm(Val: Imm) |
| 1023 | .setMIFlag(Flag); |
| 1024 | break; |
| 1025 | } |
| 1026 | case AArch64::LDPDpost: |
| 1027 | Imm = -Imm; |
| 1028 | [[fallthrough]]; |
| 1029 | case AArch64::STPDpre: { |
| 1030 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1031 | unsigned Reg1 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 2).getReg()); |
| 1032 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFRegP_X)) |
| 1033 | .addImm(Val: Reg0) |
| 1034 | .addImm(Val: Reg1) |
| 1035 | .addImm(Val: Imm * 8) |
| 1036 | .setMIFlag(Flag); |
| 1037 | break; |
| 1038 | } |
| 1039 | case AArch64::LDPXpost: |
| 1040 | Imm = -Imm; |
| 1041 | [[fallthrough]]; |
| 1042 | case AArch64::STPXpre: { |
| 1043 | Register Reg0 = MBBI->getOperand(i: 1).getReg(); |
| 1044 | Register Reg1 = MBBI->getOperand(i: 2).getReg(); |
| 1045 | if (Reg0 == AArch64::FP && Reg1 == AArch64::LR) |
| 1046 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFPLR_X)) |
| 1047 | .addImm(Val: Imm * 8) |
| 1048 | .setMIFlag(Flag); |
| 1049 | else |
| 1050 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveRegP_X)) |
| 1051 | .addImm(Val: RegInfo->getSEHRegNum(i: Reg0)) |
| 1052 | .addImm(Val: RegInfo->getSEHRegNum(i: Reg1)) |
| 1053 | .addImm(Val: Imm * 8) |
| 1054 | .setMIFlag(Flag); |
| 1055 | break; |
| 1056 | } |
| 1057 | case AArch64::LDRDpost: |
| 1058 | Imm = -Imm; |
| 1059 | [[fallthrough]]; |
| 1060 | case AArch64::STRDpre: { |
| 1061 | unsigned Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1062 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFReg_X)) |
| 1063 | .addImm(Val: Reg) |
| 1064 | .addImm(Val: Imm) |
| 1065 | .setMIFlag(Flag); |
| 1066 | break; |
| 1067 | } |
| 1068 | case AArch64::LDRXpost: |
| 1069 | Imm = -Imm; |
| 1070 | [[fallthrough]]; |
| 1071 | case AArch64::STRXpre: { |
| 1072 | unsigned Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1073 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveReg_X)) |
| 1074 | .addImm(Val: Reg) |
| 1075 | .addImm(Val: Imm) |
| 1076 | .setMIFlag(Flag); |
| 1077 | break; |
| 1078 | } |
| 1079 | case AArch64::STPDi: |
| 1080 | case AArch64::LDPDi: { |
| 1081 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1082 | unsigned Reg1 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1083 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFRegP)) |
| 1084 | .addImm(Val: Reg0) |
| 1085 | .addImm(Val: Reg1) |
| 1086 | .addImm(Val: Imm * 8) |
| 1087 | .setMIFlag(Flag); |
| 1088 | break; |
| 1089 | } |
| 1090 | case AArch64::STPXi: |
| 1091 | case AArch64::LDPXi: { |
| 1092 | Register Reg0 = MBBI->getOperand(i: 0).getReg(); |
| 1093 | Register Reg1 = MBBI->getOperand(i: 1).getReg(); |
| 1094 | |
| 1095 | int SEHReg0 = RegInfo->getSEHRegNum(i: Reg0); |
| 1096 | int SEHReg1 = RegInfo->getSEHRegNum(i: Reg1); |
| 1097 | |
| 1098 | if (Reg0 == AArch64::FP && Reg1 == AArch64::LR) |
| 1099 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFPLR)) |
| 1100 | .addImm(Val: Imm * 8) |
| 1101 | .setMIFlag(Flag); |
| 1102 | else if (SEHReg0 >= 19 && SEHReg1 >= 19) |
| 1103 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveRegP)) |
| 1104 | .addImm(Val: SEHReg0) |
| 1105 | .addImm(Val: SEHReg1) |
| 1106 | .addImm(Val: Imm * 8) |
| 1107 | .setMIFlag(Flag); |
| 1108 | else |
| 1109 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegIP)) |
| 1110 | .addImm(Val: SEHReg0) |
| 1111 | .addImm(Val: SEHReg1) |
| 1112 | .addImm(Val: Imm * 8) |
| 1113 | .setMIFlag(Flag); |
| 1114 | break; |
| 1115 | } |
| 1116 | case AArch64::STRXui: |
| 1117 | case AArch64::LDRXui: { |
| 1118 | int Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1119 | if (Reg >= 19) |
| 1120 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveReg)) |
| 1121 | .addImm(Val: Reg) |
| 1122 | .addImm(Val: Imm * 8) |
| 1123 | .setMIFlag(Flag); |
| 1124 | else |
| 1125 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegI)) |
| 1126 | .addImm(Val: Reg) |
| 1127 | .addImm(Val: Imm * 8) |
| 1128 | .setMIFlag(Flag); |
| 1129 | break; |
| 1130 | } |
| 1131 | case AArch64::STRDui: |
| 1132 | case AArch64::LDRDui: { |
| 1133 | unsigned Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1134 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveFReg)) |
| 1135 | .addImm(Val: Reg) |
| 1136 | .addImm(Val: Imm * 8) |
| 1137 | .setMIFlag(Flag); |
| 1138 | break; |
| 1139 | } |
| 1140 | case AArch64::STRQui: |
| 1141 | case AArch64::LDRQui: { |
| 1142 | unsigned Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1143 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegQ)) |
| 1144 | .addImm(Val: Reg) |
| 1145 | .addImm(Val: Imm * 16) |
| 1146 | .setMIFlag(Flag); |
| 1147 | break; |
| 1148 | } |
| 1149 | case AArch64::STPQi: |
| 1150 | case AArch64::LDPQi: { |
| 1151 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 0).getReg()); |
| 1152 | unsigned Reg1 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1153 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegQP)) |
| 1154 | .addImm(Val: Reg0) |
| 1155 | .addImm(Val: Reg1) |
| 1156 | .addImm(Val: Imm * 16) |
| 1157 | .setMIFlag(Flag); |
| 1158 | break; |
| 1159 | } |
| 1160 | case AArch64::LDRQpost: |
| 1161 | Imm = -Imm; |
| 1162 | [[fallthrough]]; |
| 1163 | case AArch64::STRQpre: { |
| 1164 | unsigned Reg = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1165 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegQX)) |
| 1166 | .addImm(Val: Reg) |
| 1167 | .addImm(Val: Imm) |
| 1168 | .setMIFlag(Flag); |
| 1169 | break; |
| 1170 | } |
| 1171 | case AArch64::LDPQpost: |
| 1172 | Imm = -Imm; |
| 1173 | [[fallthrough]]; |
| 1174 | case AArch64::STPQpre: { |
| 1175 | unsigned Reg0 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 1).getReg()); |
| 1176 | unsigned Reg1 = RegInfo->getSEHRegNum(i: MBBI->getOperand(i: 2).getReg()); |
| 1177 | MIB = BuildMI(MF, MIMD: DL, MCID: TII.get(Opcode: AArch64::SEH_SaveAnyRegQPX)) |
| 1178 | .addImm(Val: Reg0) |
| 1179 | .addImm(Val: Reg1) |
| 1180 | .addImm(Val: Imm * 16) |
| 1181 | .setMIFlag(Flag); |
| 1182 | break; |
| 1183 | } |
| 1184 | } |
| 1185 | auto I = MBB->insertAfter(I: MBBI, MI: MIB); |
| 1186 | return I; |
| 1187 | } |
| 1188 | |
| 1189 | bool AArch64FrameLowering::requiresSaveVG(const MachineFunction &MF) const { |
| 1190 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1191 | if (!AFI->needsDwarfUnwindInfo(MF) || !AFI->hasStreamingModeChanges()) |
| 1192 | return false; |
| 1193 | // For Darwin platforms we don't save VG for non-SVE functions, even if SME |
| 1194 | // is enabled with streaming mode changes. |
| 1195 | auto &ST = MF.getSubtarget<AArch64Subtarget>(); |
| 1196 | if (ST.isTargetDarwin()) |
| 1197 | return ST.hasSVE(); |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | void AArch64FrameLowering::emitPacRetPlusLeafHardening( |
| 1202 | MachineFunction &MF) const { |
| 1203 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1204 | const AArch64InstrInfo *TII = Subtarget.getInstrInfo(); |
| 1205 | |
| 1206 | auto EmitSignRA = [&](MachineBasicBlock &MBB) { |
| 1207 | DebugLoc DL; // Set debug location to unknown. |
| 1208 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 1209 | |
| 1210 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: AArch64::PAUTH_PROLOGUE)) |
| 1211 | .setMIFlag(MachineInstr::FrameSetup); |
| 1212 | }; |
| 1213 | |
| 1214 | auto EmitAuthRA = [&](MachineBasicBlock &MBB) { |
| 1215 | DebugLoc DL; |
| 1216 | MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 1217 | if (MBBI != MBB.end()) |
| 1218 | DL = MBBI->getDebugLoc(); |
| 1219 | |
| 1220 | TII->createPauthEpilogueInstr(MBB, DL); |
| 1221 | }; |
| 1222 | |
| 1223 | // This should be in sync with PEIImpl::calculateSaveRestoreBlocks. |
| 1224 | EmitSignRA(MF.front()); |
| 1225 | for (MachineBasicBlock &MBB : MF) { |
| 1226 | if (MBB.isEHFuncletEntry()) |
| 1227 | EmitSignRA(MBB); |
| 1228 | if (MBB.isReturnBlock()) |
| 1229 | EmitAuthRA(MBB); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void AArch64FrameLowering::emitPrologue(MachineFunction &MF, |
| 1234 | MachineBasicBlock &MBB) const { |
| 1235 | AArch64PrologueEmitter PrologueEmitter(MF, MBB, *this); |
| 1236 | PrologueEmitter.emitPrologue(); |
| 1237 | } |
| 1238 | |
| 1239 | void AArch64FrameLowering::emitEpilogue(MachineFunction &MF, |
| 1240 | MachineBasicBlock &MBB) const { |
| 1241 | AArch64EpilogueEmitter EpilogueEmitter(MF, MBB, *this); |
| 1242 | EpilogueEmitter.emitEpilogue(); |
| 1243 | } |
| 1244 | |
| 1245 | bool AArch64FrameLowering::enableCFIFixup(const MachineFunction &MF) const { |
| 1246 | return TargetFrameLowering::enableCFIFixup(MF) && |
| 1247 | MF.getInfo<AArch64FunctionInfo>()->needsDwarfUnwindInfo(MF); |
| 1248 | } |
| 1249 | |
| 1250 | bool AArch64FrameLowering::enableFullCFIFixup(const MachineFunction &MF) const { |
| 1251 | return enableCFIFixup(MF) && |
| 1252 | MF.getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(MF); |
| 1253 | } |
| 1254 | |
| 1255 | /// getFrameIndexReference - Provide a base+offset reference to an FI slot for |
| 1256 | /// debug info. It's the same as what we use for resolving the code-gen |
| 1257 | /// references for now. FIXME: This can go wrong when references are |
| 1258 | /// SP-relative and simple call frames aren't used. |
| 1259 | StackOffset |
| 1260 | AArch64FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, |
| 1261 | Register &FrameReg) const { |
| 1262 | return resolveFrameIndexReference( |
| 1263 | MF, FI, FrameReg, |
| 1264 | /*PreferFP=*/ |
| 1265 | MF.getFunction().hasFnAttribute(Kind: Attribute::SanitizeHWAddress) || |
| 1266 | MF.getFunction().hasFnAttribute(Kind: Attribute::SanitizeMemTag), |
| 1267 | /*ForSimm=*/false); |
| 1268 | } |
| 1269 | |
| 1270 | StackOffset |
| 1271 | AArch64FrameLowering::getFrameIndexReferenceFromSP(const MachineFunction &MF, |
| 1272 | int FI) const { |
| 1273 | // This function serves to provide a comparable offset from a single reference |
| 1274 | // point (the value of SP at function entry) that can be used for analysis, |
| 1275 | // e.g. the stack-frame-layout analysis pass. It is not guaranteed to be |
| 1276 | // correct for all objects in the presence of VLA-area objects or dynamic |
| 1277 | // stack re-alignment. |
| 1278 | |
| 1279 | const auto &MFI = MF.getFrameInfo(); |
| 1280 | |
| 1281 | int64_t ObjectOffset = MFI.getObjectOffset(ObjectIdx: FI); |
| 1282 | StackOffset ZPRStackSize = getZPRStackSize(MF); |
| 1283 | StackOffset PPRStackSize = getPPRStackSize(MF); |
| 1284 | StackOffset SVEStackSize = ZPRStackSize + PPRStackSize; |
| 1285 | |
| 1286 | // For VLA-area objects, just emit an offset at the end of the stack frame. |
| 1287 | // Whilst not quite correct, these objects do live at the end of the frame and |
| 1288 | // so it is more useful for analysis for the offset to reflect this. |
| 1289 | if (MFI.isVariableSizedObjectIndex(ObjectIdx: FI)) { |
| 1290 | return StackOffset::getFixed(Fixed: -((int64_t)MFI.getStackSize())) - SVEStackSize; |
| 1291 | } |
| 1292 | |
| 1293 | // This is correct in the absence of any SVE stack objects. |
| 1294 | if (!SVEStackSize) |
| 1295 | return StackOffset::getFixed(Fixed: ObjectOffset - getOffsetOfLocalArea()); |
| 1296 | |
| 1297 | const auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1298 | bool FPAfterSVECalleeSaves = hasSVECalleeSavesAboveFrameRecord(MF); |
| 1299 | if (MFI.hasScalableStackID(ObjectIdx: FI)) { |
| 1300 | if (FPAfterSVECalleeSaves && |
| 1301 | -ObjectOffset <= (int64_t)AFI->getSVECalleeSavedStackSize()) { |
| 1302 | assert(!AFI->hasSplitSVEObjects() && |
| 1303 | "split-sve-objects not supported with FPAfterSVECalleeSaves" ); |
| 1304 | return StackOffset::getScalable(Scalable: ObjectOffset); |
| 1305 | } |
| 1306 | StackOffset AccessOffset{}; |
| 1307 | // The scalable vectors are below (lower address) the scalable predicates |
| 1308 | // with split SVE objects, so we must subtract the size of the predicates. |
| 1309 | if (AFI->hasSplitSVEObjects() && |
| 1310 | MFI.getStackID(ObjectIdx: FI) == TargetStackID::ScalableVector) |
| 1311 | AccessOffset = -PPRStackSize; |
| 1312 | return AccessOffset + |
| 1313 | StackOffset::get(Fixed: -((int64_t)AFI->getCalleeSavedStackSize()), |
| 1314 | Scalable: ObjectOffset); |
| 1315 | } |
| 1316 | |
| 1317 | bool IsFixed = MFI.isFixedObjectIndex(ObjectIdx: FI); |
| 1318 | bool IsCSR = |
| 1319 | !IsFixed && ObjectOffset >= -((int)AFI->getCalleeSavedStackSize(MFI)); |
| 1320 | |
| 1321 | StackOffset ScalableOffset = {}; |
| 1322 | if (!IsFixed && !IsCSR) { |
| 1323 | ScalableOffset = -SVEStackSize; |
| 1324 | } else if (FPAfterSVECalleeSaves && IsCSR) { |
| 1325 | ScalableOffset = |
| 1326 | -StackOffset::getScalable(Scalable: AFI->getSVECalleeSavedStackSize()); |
| 1327 | } |
| 1328 | |
| 1329 | return StackOffset::getFixed(Fixed: ObjectOffset) + ScalableOffset; |
| 1330 | } |
| 1331 | |
| 1332 | StackOffset |
| 1333 | AArch64FrameLowering::getNonLocalFrameIndexReference(const MachineFunction &MF, |
| 1334 | int FI) const { |
| 1335 | return StackOffset::getFixed(Fixed: getSEHFrameIndexOffset(MF, FI)); |
| 1336 | } |
| 1337 | |
| 1338 | StackOffset AArch64FrameLowering::getFPOffset(const MachineFunction &MF, |
| 1339 | int64_t ObjectOffset) const { |
| 1340 | const auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1341 | const auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1342 | const Function &F = MF.getFunction(); |
| 1343 | bool IsWin64 = Subtarget.isCallingConvWin64(CC: F.getCallingConv(), IsVarArg: F.isVarArg()); |
| 1344 | unsigned FixedObject = |
| 1345 | getFixedObjectSize(MF, AFI, IsWin64, /*IsFunclet=*/false); |
| 1346 | int64_t CalleeSaveSize = AFI->getCalleeSavedStackSize(MFI: MF.getFrameInfo()); |
| 1347 | int64_t FPAdjust = |
| 1348 | CalleeSaveSize - AFI->getCalleeSaveBaseToFrameRecordOffset(); |
| 1349 | return StackOffset::getFixed(Fixed: ObjectOffset + FixedObject + FPAdjust); |
| 1350 | } |
| 1351 | |
| 1352 | StackOffset AArch64FrameLowering::getStackOffset(const MachineFunction &MF, |
| 1353 | int64_t ObjectOffset) const { |
| 1354 | const auto &MFI = MF.getFrameInfo(); |
| 1355 | return StackOffset::getFixed(Fixed: ObjectOffset + (int64_t)MFI.getStackSize()); |
| 1356 | } |
| 1357 | |
| 1358 | // TODO: This function currently does not work for scalable vectors. |
| 1359 | int AArch64FrameLowering::getSEHFrameIndexOffset(const MachineFunction &MF, |
| 1360 | int FI) const { |
| 1361 | const AArch64RegisterInfo *RegInfo = |
| 1362 | MF.getSubtarget<AArch64Subtarget>().getRegisterInfo(); |
| 1363 | int ObjectOffset = MF.getFrameInfo().getObjectOffset(ObjectIdx: FI); |
| 1364 | return RegInfo->getLocalAddressRegister(MF) == AArch64::FP |
| 1365 | ? getFPOffset(MF, ObjectOffset).getFixed() |
| 1366 | : getStackOffset(MF, ObjectOffset).getFixed(); |
| 1367 | } |
| 1368 | |
| 1369 | StackOffset AArch64FrameLowering::resolveFrameIndexReference( |
| 1370 | const MachineFunction &MF, int FI, Register &FrameReg, bool PreferFP, |
| 1371 | bool ForSimm) const { |
| 1372 | const auto &MFI = MF.getFrameInfo(); |
| 1373 | int64_t ObjectOffset = MFI.getObjectOffset(ObjectIdx: FI); |
| 1374 | bool isFixed = MFI.isFixedObjectIndex(ObjectIdx: FI); |
| 1375 | auto StackID = static_cast<TargetStackID::Value>(MFI.getStackID(ObjectIdx: FI)); |
| 1376 | return resolveFrameOffsetReference(MF, ObjectOffset, isFixed, StackID, |
| 1377 | FrameReg, PreferFP, ForSimm); |
| 1378 | } |
| 1379 | |
| 1380 | StackOffset AArch64FrameLowering::resolveFrameOffsetReference( |
| 1381 | const MachineFunction &MF, int64_t ObjectOffset, bool isFixed, |
| 1382 | TargetStackID::Value StackID, Register &FrameReg, bool PreferFP, |
| 1383 | bool ForSimm) const { |
| 1384 | const auto &MFI = MF.getFrameInfo(); |
| 1385 | const auto &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1386 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 1387 | const auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1388 | |
| 1389 | int64_t FPOffset = getFPOffset(MF, ObjectOffset).getFixed(); |
| 1390 | int64_t Offset = getStackOffset(MF, ObjectOffset).getFixed(); |
| 1391 | |
| 1392 | // The fixed object area sits above the callee-saved area and can grow |
| 1393 | // large enough to displace it; account for that displacement here so CSR |
| 1394 | // objects aren't misclassified as locals and addressed via the base |
| 1395 | // pointer. |
| 1396 | bool IsWin64 = Subtarget.isCallingConvWin64(CC: MF.getFunction().getCallingConv(), |
| 1397 | IsVarArg: MF.getFunction().isVarArg()); |
| 1398 | const int64_t FixedObjectSize = |
| 1399 | getFixedObjectSize(MF, AFI, IsWin64, /*IsFunclet*/ false); |
| 1400 | bool isCSR = |
| 1401 | !isFixed && ObjectOffset >= -((int64_t)AFI->getCalleeSavedStackSize(MFI) + |
| 1402 | FixedObjectSize); |
| 1403 | bool isSVE = MFI.isScalableStackID(StackID); |
| 1404 | |
| 1405 | StackOffset ZPRStackSize = getZPRStackSize(MF); |
| 1406 | StackOffset PPRStackSize = getPPRStackSize(MF); |
| 1407 | StackOffset SVEStackSize = ZPRStackSize + PPRStackSize; |
| 1408 | |
| 1409 | // Use frame pointer to reference fixed objects. Use it for locals if |
| 1410 | // there are VLAs or a dynamically realigned SP (and thus the SP isn't |
| 1411 | // reliable as a base). Make sure useFPForScavengingIndex() does the |
| 1412 | // right thing for the emergency spill slot. |
| 1413 | bool UseFP = false; |
| 1414 | if (AFI->hasStackFrame() && !isSVE) { |
| 1415 | // We shouldn't prefer using the FP to access fixed-sized stack objects when |
| 1416 | // there are scalable (SVE) objects in between the FP and the fixed-sized |
| 1417 | // objects. |
| 1418 | PreferFP &= !SVEStackSize; |
| 1419 | |
| 1420 | // Note: Keeping the following as multiple 'if' statements rather than |
| 1421 | // merging to a single expression for readability. |
| 1422 | // |
| 1423 | // Argument access should always use the FP. |
| 1424 | if (isFixed) { |
| 1425 | UseFP = hasFP(MF); |
| 1426 | } else if (isCSR && RegInfo->hasStackRealignment(MF)) { |
| 1427 | // References to the CSR area must use FP if we're re-aligning the stack |
| 1428 | // since the dynamically-sized alignment padding is between the SP/BP and |
| 1429 | // the CSR area. |
| 1430 | assert(hasFP(MF) && "Re-aligned stack must have frame pointer" ); |
| 1431 | UseFP = true; |
| 1432 | } else if (hasFP(MF) && !RegInfo->hasStackRealignment(MF)) { |
| 1433 | // If the FPOffset is negative and we're producing a signed immediate, we |
| 1434 | // have to keep in mind that the available offset range for negative |
| 1435 | // offsets is smaller than for positive ones. If an offset is available |
| 1436 | // via the FP and the SP, use whichever is closest. |
| 1437 | bool FPOffsetFits = !ForSimm || FPOffset >= -256; |
| 1438 | PreferFP |= Offset > -FPOffset && !SVEStackSize; |
| 1439 | |
| 1440 | if (FPOffset >= 0) { |
| 1441 | // If the FPOffset is positive, that'll always be best, as the SP/BP |
| 1442 | // will be even further away. |
| 1443 | UseFP = true; |
| 1444 | } else if (MFI.hasVarSizedObjects()) { |
| 1445 | // If we have variable sized objects, we can use either FP or BP, as the |
| 1446 | // SP offset is unknown. We can use the base pointer if we have one and |
| 1447 | // FP is not preferred. If not, we're stuck with using FP. |
| 1448 | bool CanUseBP = RegInfo->hasBasePointer(MF); |
| 1449 | if (FPOffsetFits && CanUseBP) // Both are ok. Pick the best. |
| 1450 | UseFP = PreferFP; |
| 1451 | else if (!CanUseBP) // Can't use BP. Forced to use FP. |
| 1452 | UseFP = true; |
| 1453 | // else we can use BP and FP, but the offset from FP won't fit. |
| 1454 | // That will make us scavenge registers which we can probably avoid by |
| 1455 | // using BP. If it won't fit for BP either, we'll scavenge anyway. |
| 1456 | } else if (MF.hasEHFunclets() && !RegInfo->hasBasePointer(MF)) { |
| 1457 | // Funclets access the locals contained in the parent's stack frame |
| 1458 | // via the frame pointer, so we have to use the FP in the parent |
| 1459 | // function. |
| 1460 | (void) Subtarget; |
| 1461 | assert(Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv(), |
| 1462 | MF.getFunction().isVarArg()) && |
| 1463 | "Funclets should only be present on Win64" ); |
| 1464 | UseFP = true; |
| 1465 | } else { |
| 1466 | // We have the choice between FP and (SP or BP). |
| 1467 | if (FPOffsetFits && PreferFP) // If FP is the best fit, use it. |
| 1468 | UseFP = true; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | assert( |
| 1474 | ((isFixed || isCSR) || !RegInfo->hasStackRealignment(MF) || !UseFP) && |
| 1475 | "In the presence of dynamic stack pointer realignment, " |
| 1476 | "non-argument/CSR objects cannot be accessed through the frame pointer" ); |
| 1477 | |
| 1478 | bool FPAfterSVECalleeSaves = hasSVECalleeSavesAboveFrameRecord(MF); |
| 1479 | |
| 1480 | if (isSVE) { |
| 1481 | StackOffset FPOffset = StackOffset::get( |
| 1482 | Fixed: -AFI->getCalleeSaveBaseToFrameRecordOffset(), Scalable: ObjectOffset); |
| 1483 | StackOffset SPOffset = |
| 1484 | SVEStackSize + |
| 1485 | StackOffset::get(Fixed: MFI.getStackSize() - AFI->getCalleeSavedStackSize(), |
| 1486 | Scalable: ObjectOffset); |
| 1487 | |
| 1488 | // With split SVE objects the ObjectOffset is relative to the split area |
| 1489 | // (i.e. the PPR area or ZPR area respectively). |
| 1490 | if (AFI->hasSplitSVEObjects() && StackID == TargetStackID::ScalableVector) { |
| 1491 | // If we're accessing an SVE vector with split SVE objects... |
| 1492 | // - From the FP we need to move down past the PPR area: |
| 1493 | FPOffset -= PPRStackSize; |
| 1494 | // - From the SP we only need to move up to the ZPR area: |
| 1495 | SPOffset -= PPRStackSize; |
| 1496 | // Note: `SPOffset = SVEStackSize + ...`, so `-= PPRStackSize` results in |
| 1497 | // `SPOffset = ZPRStackSize + ...`. |
| 1498 | } |
| 1499 | |
| 1500 | if (FPAfterSVECalleeSaves) { |
| 1501 | FPOffset += StackOffset::getScalable(Scalable: AFI->getSVECalleeSavedStackSize()); |
| 1502 | if (-ObjectOffset <= (int64_t)AFI->getSVECalleeSavedStackSize()) { |
| 1503 | FPOffset += StackOffset::getFixed(Fixed: AFI->getCalleeSavedStackSize()); |
| 1504 | SPOffset += StackOffset::getFixed(Fixed: AFI->getCalleeSavedStackSize()); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | // Always use the FP for SVE spills if available and beneficial. |
| 1509 | if (hasFP(MF) && (SPOffset.getFixed() || |
| 1510 | FPOffset.getScalable() < SPOffset.getScalable() || |
| 1511 | RegInfo->hasStackRealignment(MF))) { |
| 1512 | FrameReg = RegInfo->getFrameRegister(MF); |
| 1513 | return FPOffset; |
| 1514 | } |
| 1515 | FrameReg = RegInfo->hasBasePointer(MF) ? RegInfo->getBaseRegister() |
| 1516 | : MCRegister(AArch64::SP); |
| 1517 | |
| 1518 | return SPOffset; |
| 1519 | } |
| 1520 | |
| 1521 | StackOffset SVEAreaOffset = {}; |
| 1522 | if (FPAfterSVECalleeSaves) { |
| 1523 | // In this stack layout, the FP is in between the callee saves and other |
| 1524 | // SVE allocations. |
| 1525 | StackOffset SVECalleeSavedStack = |
| 1526 | StackOffset::getScalable(Scalable: AFI->getSVECalleeSavedStackSize()); |
| 1527 | if (UseFP) { |
| 1528 | if (isFixed) |
| 1529 | SVEAreaOffset = SVECalleeSavedStack; |
| 1530 | else if (!isCSR) |
| 1531 | SVEAreaOffset = SVECalleeSavedStack - SVEStackSize; |
| 1532 | } else { |
| 1533 | if (isFixed) |
| 1534 | SVEAreaOffset = SVEStackSize; |
| 1535 | else if (isCSR) |
| 1536 | SVEAreaOffset = SVEStackSize - SVECalleeSavedStack; |
| 1537 | } |
| 1538 | } else { |
| 1539 | if (UseFP && !(isFixed || isCSR)) |
| 1540 | SVEAreaOffset = -SVEStackSize; |
| 1541 | if (!UseFP && (isFixed || isCSR)) |
| 1542 | SVEAreaOffset = SVEStackSize; |
| 1543 | } |
| 1544 | |
| 1545 | if (UseFP) { |
| 1546 | FrameReg = RegInfo->getFrameRegister(MF); |
| 1547 | return StackOffset::getFixed(Fixed: FPOffset) + SVEAreaOffset; |
| 1548 | } |
| 1549 | |
| 1550 | // Use the base pointer if we have one. |
| 1551 | if (RegInfo->hasBasePointer(MF)) |
| 1552 | FrameReg = RegInfo->getBaseRegister(); |
| 1553 | else { |
| 1554 | assert(!MFI.hasVarSizedObjects() && |
| 1555 | "Can't use SP when we have var sized objects." ); |
| 1556 | FrameReg = AArch64::SP; |
| 1557 | // If we're using the red zone for this function, the SP won't actually |
| 1558 | // be adjusted, so the offsets will be negative. They're also all |
| 1559 | // within range of the signed 9-bit immediate instructions. |
| 1560 | if (canUseRedZone(MF)) |
| 1561 | Offset -= AFI->getLocalStackSize(); |
| 1562 | } |
| 1563 | |
| 1564 | return StackOffset::getFixed(Fixed: Offset) + SVEAreaOffset; |
| 1565 | } |
| 1566 | |
| 1567 | static RegState getPrologueDeath(MachineFunction &MF, unsigned Reg) { |
| 1568 | // Do not set a kill flag on values that are also marked as live-in. This |
| 1569 | // happens with the @llvm-returnaddress intrinsic and with arguments passed in |
| 1570 | // callee saved registers. |
| 1571 | // Omitting the kill flags is conservatively correct even if the live-in |
| 1572 | // is not used after all. |
| 1573 | bool IsLiveIn = MF.getRegInfo().isLiveIn(Reg); |
| 1574 | return getKillRegState(B: !IsLiveIn); |
| 1575 | } |
| 1576 | |
| 1577 | static bool produceCompactUnwindFrame(const AArch64FrameLowering &AFL, |
| 1578 | MachineFunction &MF) { |
| 1579 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 1580 | AttributeList Attrs = MF.getFunction().getAttributes(); |
| 1581 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1582 | return Subtarget.isTargetMachO() && |
| 1583 | !(Subtarget.getTargetLowering()->supportSwiftError() && |
| 1584 | Attrs.hasAttrSomewhere(Kind: Attribute::SwiftError)) && |
| 1585 | MF.getFunction().getCallingConv() != CallingConv::SwiftTail && |
| 1586 | !AFL.requiresSaveVG(MF) && !AFI->isSVECC(); |
| 1587 | } |
| 1588 | |
| 1589 | static bool invalidateWindowsRegisterPairing(bool SpillExtendedVolatile, |
| 1590 | int PairOffset, unsigned Reg1, |
| 1591 | unsigned Reg2, bool NeedsWinCFI, |
| 1592 | const TargetRegisterInfo *TRI) { |
| 1593 | // If we are generating register pairs for a Windows function that requires |
| 1594 | // EH support, then pair consecutive registers only. There are no unwind |
| 1595 | // opcodes for saves/restores of non-consecutive register pairs. |
| 1596 | // The unwind opcodes are save_regp, save_regp_x, save_fregp, save_frepg_x, |
| 1597 | // save_lrpair. |
| 1598 | // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling |
| 1599 | |
| 1600 | if (Reg2 == AArch64::FP) |
| 1601 | return true; |
| 1602 | if (!NeedsWinCFI) |
| 1603 | return false; |
| 1604 | |
| 1605 | // ARM64EC introduced `save_any_regp`, which expects 16-byte alignment. |
| 1606 | // This is handled by only allowing paired spills for registers spilled at |
| 1607 | // even positions (which should be 16-byte aligned, as other GPRs/FPRs are |
| 1608 | // 8-bytes). We carve out an exception for {FP,LR}, which does not require |
| 1609 | // 16-byte alignment in the uop representation. |
| 1610 | if (TRI->getEncodingValue(Reg: Reg2) == TRI->getEncodingValue(Reg: Reg1) + 1) |
| 1611 | return SpillExtendedVolatile |
| 1612 | ? !((Reg1 == AArch64::FP && Reg2 == AArch64::LR) || |
| 1613 | (PairOffset % 16) == 0) |
| 1614 | : false; |
| 1615 | |
| 1616 | // If pairing a GPR with LR, the pair can be described by the save_lrpair |
| 1617 | // opcode. The save_lrpair opcode requires the first register to be odd. |
| 1618 | if (Reg1 >= AArch64::X19 && Reg1 <= AArch64::X27 && |
| 1619 | (Reg1 - AArch64::X19) % 2 == 0 && Reg2 == AArch64::LR) |
| 1620 | return false; |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | /// Returns true if Reg1 and Reg2 cannot be paired using a ldp/stp instruction. |
| 1625 | /// WindowsCFI requires that only consecutive registers can be paired. |
| 1626 | /// LR and FP need to be allocated together when the frame needs to save |
| 1627 | /// the frame-record. This means any other register pairing with LR is invalid. |
| 1628 | static bool invalidateRegisterPairing(bool SpillExtendedVolatile, |
| 1629 | int PairOffset, unsigned Reg1, |
| 1630 | unsigned Reg2, bool UsesWinAAPCS, |
| 1631 | bool NeedsWinCFI, bool NeedsFrameRecord, |
| 1632 | const TargetRegisterInfo *TRI) { |
| 1633 | if (UsesWinAAPCS) |
| 1634 | return invalidateWindowsRegisterPairing(SpillExtendedVolatile, PairOffset, |
| 1635 | Reg1, Reg2, NeedsWinCFI, TRI); |
| 1636 | |
| 1637 | // If we need to store the frame record, don't pair any register |
| 1638 | // with LR other than FP. |
| 1639 | if (NeedsFrameRecord) |
| 1640 | return Reg2 == AArch64::LR; |
| 1641 | |
| 1642 | return false; |
| 1643 | } |
| 1644 | |
| 1645 | // Returns true if Offset (in bytes) is aligned to the instruction's scale and |
| 1646 | // the scaled immediate is within the instruction's valid range. |
| 1647 | static bool isValidMemOpOffset(const AArch64InstrInfo *TII, unsigned Opcode, |
| 1648 | int Offset) { |
| 1649 | int64_t MinOff, MaxOff; |
| 1650 | TypeSize ScaleValue(0U, false), Width(0U, false); |
| 1651 | if (!TII->getMemOpInfo(Opcode, Scale&: ScaleValue, Width, MinOffset&: MinOff, MaxOffset&: MaxOff)) |
| 1652 | return false; |
| 1653 | |
| 1654 | if (Offset % ScaleValue.getKnownMinValue() != 0) |
| 1655 | return false; |
| 1656 | |
| 1657 | Offset /= ScaleValue.getKnownMinValue(); |
| 1658 | return Offset >= MinOff && Offset <= MaxOff; |
| 1659 | } |
| 1660 | |
| 1661 | namespace { |
| 1662 | |
| 1663 | struct RegGroupInfo { |
| 1664 | Register Reg1; |
| 1665 | Register Reg2; |
| 1666 | int FrameIdx; |
| 1667 | int Offset; |
| 1668 | enum RegType { GPR, FPR64, FPR128, PPR, ZPR, VG } Type; |
| 1669 | const TargetRegisterClass *RC; |
| 1670 | |
| 1671 | RegGroupInfo() = default; |
| 1672 | |
| 1673 | bool isPaired() const { return getNumRegs() == 2; } |
| 1674 | |
| 1675 | bool isQuad() const { return getNumRegs() == 4; } |
| 1676 | |
| 1677 | bool isGrouped() const { return isPaired() || isQuad(); } |
| 1678 | |
| 1679 | unsigned getNumRegs() const { |
| 1680 | if (!Reg2.isValid()) |
| 1681 | return 1; |
| 1682 | if (Type != ZPR) |
| 1683 | return 2; |
| 1684 | // ZPR groups are contiguous with Reg2 holding the lower register. |
| 1685 | unsigned NumRegs = (Reg1 - Reg2) + 1; |
| 1686 | assert(NumRegs == 2 || NumRegs == 4); |
| 1687 | return NumRegs; |
| 1688 | } |
| 1689 | |
| 1690 | bool isScalable() const { return Type == PPR || Type == ZPR; } |
| 1691 | |
| 1692 | SmallVector<Register, 4> getRegisters() const { |
| 1693 | if (isPaired()) |
| 1694 | return {Reg1, Reg2}; |
| 1695 | if (isQuad()) { |
| 1696 | assert(Reg1 - 3 == Reg2 && "Quad not contiguous" ); |
| 1697 | return {Reg1, Reg1 - 1, Reg1 - 2, Reg1 - 3}; |
| 1698 | } |
| 1699 | return {Reg1}; |
| 1700 | } |
| 1701 | |
| 1702 | auto getFrameIndexes() const { |
| 1703 | return seq(Begin: FrameIdx, End: FrameIdx + int(getNumRegs())); |
| 1704 | } |
| 1705 | |
| 1706 | Register getAsMultiVectorRegister() const { |
| 1707 | assert(Type == ZPR && isGrouped() && "Expected ZPR register group" ); |
| 1708 | return (isQuad() ? AArch64::Z0_Z1_Z2_Z3 : AArch64::Z0_Z1) + |
| 1709 | (Reg2 - AArch64::Z0); |
| 1710 | } |
| 1711 | |
| 1712 | void dump(const TargetRegisterInfo *TRI) const { |
| 1713 | dbgs() << "(" ; |
| 1714 | interleaveComma(c: getRegisters(), os&: dbgs(), |
| 1715 | each_fn: [&](Register Reg) { dbgs() << printReg(Reg, TRI); }); |
| 1716 | dbgs() << ") -> fi#(" ; |
| 1717 | interleaveComma(c: getFrameIndexes(), os&: dbgs(), |
| 1718 | each_fn: [&](int FrameIdx) { dbgs() << FrameIdx; }); |
| 1719 | dbgs() << ")\n" ; |
| 1720 | } |
| 1721 | }; |
| 1722 | |
| 1723 | } // end anonymous namespace |
| 1724 | |
| 1725 | MCRegister findFreePredicateReg(BitVector &SavedRegs) { |
| 1726 | for (unsigned PReg = AArch64::P8; PReg <= AArch64::P15; ++PReg) { |
| 1727 | if (SavedRegs.test(Idx: PReg)) { |
| 1728 | unsigned PNReg = PReg - AArch64::P0 + AArch64::PN0; |
| 1729 | return MCRegister(PNReg); |
| 1730 | } |
| 1731 | } |
| 1732 | return MCRegister(); |
| 1733 | } |
| 1734 | |
| 1735 | // The multivector LD/ST are available only for SME or SVE2p1 targets |
| 1736 | bool enableMultiVectorSpillFill(const AArch64Subtarget &Subtarget, |
| 1737 | MachineFunction &MF) { |
| 1738 | if (Subtarget.getCLOpts().disable_multivector_spill_fill) |
| 1739 | return false; |
| 1740 | |
| 1741 | SMEAttrs FuncAttrs = MF.getInfo<AArch64FunctionInfo>()->getSMEFnAttrs(); |
| 1742 | bool IsLocallyStreaming = |
| 1743 | FuncAttrs.hasStreamingBody() && !FuncAttrs.hasStreamingInterface(); |
| 1744 | |
| 1745 | // Only when in streaming mode SME2 instructions can be safely used. |
| 1746 | // It is not safe to use SME2 instructions when in streaming compatible or |
| 1747 | // locally streaming mode. |
| 1748 | return Subtarget.hasSVE2p1() || |
| 1749 | (Subtarget.hasSME2() && |
| 1750 | (!IsLocallyStreaming && Subtarget.isStreaming())); |
| 1751 | } |
| 1752 | |
| 1753 | void computeCalleeSaveRegisterGroups(const AArch64FrameLowering &AFL, |
| 1754 | MachineFunction &MF, |
| 1755 | ArrayRef<CalleeSavedInfo> CSI, |
| 1756 | const TargetRegisterInfo *TRI, |
| 1757 | SmallVectorImpl<RegGroupInfo> &RegGroups, |
| 1758 | bool NeedsFrameRecord) { |
| 1759 | |
| 1760 | if (CSI.empty()) |
| 1761 | return; |
| 1762 | |
| 1763 | const AArch64InstrInfo *TII = |
| 1764 | MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 1765 | bool IsWindows = isTargetWindows(MF); |
| 1766 | bool NeedsWinCFI = MF.needsWinCFI(); |
| 1767 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 1768 | unsigned StackHazardSize = getStackHazardSize(MF); |
| 1769 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1770 | CallingConv::ID CC = MF.getFunction().getCallingConv(); |
| 1771 | unsigned Count = CSI.size(); |
| 1772 | (void)CC; |
| 1773 | // MachO's compact unwind format relies on all registers being stored in |
| 1774 | // pairs. |
| 1775 | assert((!produceCompactUnwindFrame(AFL, MF) || |
| 1776 | CC == CallingConv::PreserveMost || CC == CallingConv::PreserveAll || |
| 1777 | CC == CallingConv::CXX_FAST_TLS || CC == CallingConv::Win64 || |
| 1778 | (Count & 1) == 0) && |
| 1779 | "Odd number of callee-saved regs to spill!" ); |
| 1780 | int ByteOffset = AFI->getCalleeSavedStackSize(); |
| 1781 | int StackFillDir = -1; |
| 1782 | int RegInc = 1; |
| 1783 | unsigned FirstReg = 0; |
| 1784 | if (IsWindows) { |
| 1785 | // For WinCFI, fill the stack from the bottom up. |
| 1786 | ByteOffset = 0; |
| 1787 | StackFillDir = 1; |
| 1788 | // As the CSI array is reversed to match PrologEpilogInserter, iterate |
| 1789 | // backwards, to pair up registers starting from lower numbered registers. |
| 1790 | RegInc = -1; |
| 1791 | FirstReg = Count - 1; |
| 1792 | } |
| 1793 | |
| 1794 | bool FPAfterSVECalleeSaves = AFL.hasSVECalleeSavesAboveFrameRecord(MF); |
| 1795 | // Windows AAPCS has x9-x15 as volatile registers, x16-x17 as intra-procedural |
| 1796 | // scratch, x18 as platform reserved. However, clang has extended calling |
| 1797 | // convensions such as preserve_most and preserve_all which treat these as |
| 1798 | // CSR. As such, the ARM64 unwind uOPs bias registers by 19. We use ARM64EC |
| 1799 | // uOPs which have separate restrictions. We need to check for that. |
| 1800 | // |
| 1801 | // NOTE: we currently do not account for the D registers as LLVM does not |
| 1802 | // support non-ABI compliant D register spills. |
| 1803 | bool SpillExtendedVolatile = |
| 1804 | IsWindows && llvm::any_of(Range&: CSI, P: [](const CalleeSavedInfo &CSI) { |
| 1805 | const auto &Reg = CSI.getReg(); |
| 1806 | return Reg >= AArch64::X0 && Reg <= AArch64::X18; |
| 1807 | }); |
| 1808 | |
| 1809 | int ZPRByteOffset = 0; |
| 1810 | int PPRByteOffset = 0; |
| 1811 | bool SplitPPRs = AFI->hasSplitSVEObjects(); |
| 1812 | if (SplitPPRs) { |
| 1813 | ZPRByteOffset = AFI->getZPRCalleeSavedStackSize(); |
| 1814 | PPRByteOffset = AFI->getPPRCalleeSavedStackSize(); |
| 1815 | } else if (!FPAfterSVECalleeSaves) { |
| 1816 | ZPRByteOffset = |
| 1817 | AFI->getZPRCalleeSavedStackSize() + AFI->getPPRCalleeSavedStackSize(); |
| 1818 | // Unused: Everything goes in ZPR space. |
| 1819 | PPRByteOffset = 0; |
| 1820 | } |
| 1821 | |
| 1822 | bool NeedGapToAlignStack = AFI->hasCalleeSaveStackFreeSpace(); |
| 1823 | Register LastReg = 0; |
| 1824 | bool HasCSHazardPadding = AFI->hasStackHazardSlotIndex() && !SplitPPRs; |
| 1825 | |
| 1826 | auto AlignOffset = [StackFillDir](int Offset, int Align) { |
| 1827 | if (StackFillDir < 0) |
| 1828 | return alignDown(Value: Offset, Align); |
| 1829 | return alignTo(Value: Offset, Align); |
| 1830 | }; |
| 1831 | |
| 1832 | // When iterating backwards, the loop condition relies on unsigned wraparound. |
| 1833 | for (unsigned i = FirstReg; i < Count; i += RegInc) { |
| 1834 | RegGroupInfo RGI; |
| 1835 | RGI.Reg1 = CSI[i].getReg(); |
| 1836 | |
| 1837 | if (AArch64::GPR64RegClass.contains(Reg: RGI.Reg1)) { |
| 1838 | RGI.Type = RegGroupInfo::GPR; |
| 1839 | RGI.RC = &AArch64::GPR64RegClass; |
| 1840 | } else if (AArch64::FPR64RegClass.contains(Reg: RGI.Reg1)) { |
| 1841 | RGI.Type = RegGroupInfo::FPR64; |
| 1842 | RGI.RC = &AArch64::FPR64RegClass; |
| 1843 | } else if (AArch64::FPR128RegClass.contains(Reg: RGI.Reg1)) { |
| 1844 | RGI.Type = RegGroupInfo::FPR128; |
| 1845 | RGI.RC = &AArch64::FPR128RegClass; |
| 1846 | } else if (AArch64::ZPRRegClass.contains(Reg: RGI.Reg1)) { |
| 1847 | RGI.Type = RegGroupInfo::ZPR; |
| 1848 | RGI.RC = &AArch64::ZPRRegClass; |
| 1849 | } else if (AArch64::PPRRegClass.contains(Reg: RGI.Reg1)) { |
| 1850 | RGI.Type = RegGroupInfo::PPR; |
| 1851 | RGI.RC = &AArch64::PPRRegClass; |
| 1852 | } else if (RGI.Reg1 == AArch64::VG) { |
| 1853 | RGI.Type = RegGroupInfo::VG; |
| 1854 | RGI.RC = &AArch64::FIXED_REGSRegClass; |
| 1855 | } else { |
| 1856 | llvm_unreachable("Unsupported register class." ); |
| 1857 | } |
| 1858 | |
| 1859 | int &ScalableByteOffset = RGI.Type == RegGroupInfo::PPR && SplitPPRs |
| 1860 | ? PPRByteOffset |
| 1861 | : ZPRByteOffset; |
| 1862 | |
| 1863 | // Add the stack hazard size as we transition from GPR->FPR CSRs. |
| 1864 | if (HasCSHazardPadding && |
| 1865 | (!LastReg || !AArch64InstrInfo::isFpOrNEON(Reg: LastReg)) && |
| 1866 | AArch64InstrInfo::isFpOrNEON(Reg: RGI.Reg1)) |
| 1867 | ByteOffset += StackFillDir * StackHazardSize; |
| 1868 | LastReg = RGI.Reg1; |
| 1869 | |
| 1870 | int Scale = TRI->getSpillSize(RC: *RGI.RC); |
| 1871 | // Add the next reg to the pair if it is in the same register class. |
| 1872 | if (unsigned(i + RegInc) < Count && !HasCSHazardPadding) { |
| 1873 | MCRegister NextReg = CSI[i + RegInc].getReg(); |
| 1874 | int Aligned = AlignOffset(ByteOffset, Scale); |
| 1875 | int PairOffset = IsWindows ? Aligned : Aligned + StackFillDir * 2 * Scale; |
| 1876 | bool PairFitsImmRange = |
| 1877 | PairOffset / Scale >= -64 && PairOffset / Scale <= 63; |
| 1878 | switch (RGI.Type) { |
| 1879 | case RegGroupInfo::GPR: |
| 1880 | case RegGroupInfo::FPR64: |
| 1881 | case RegGroupInfo::FPR128: { |
| 1882 | const TargetRegisterClass &SpillRC = |
| 1883 | RGI.Type == RegGroupInfo::GPR ? AArch64::GPR64RegClass |
| 1884 | : RGI.Type == RegGroupInfo::FPR64 ? AArch64::FPR64RegClass |
| 1885 | : AArch64::FPR128RegClass; |
| 1886 | |
| 1887 | if (SpillRC.contains(Reg: NextReg) && PairFitsImmRange && |
| 1888 | !invalidateRegisterPairing(SpillExtendedVolatile, PairOffset, |
| 1889 | Reg1: RGI.Reg1, Reg2: NextReg, UsesWinAAPCS: IsWindows, |
| 1890 | NeedsWinCFI, NeedsFrameRecord, TRI)) |
| 1891 | RGI.Reg2 = NextReg; |
| 1892 | break; |
| 1893 | } |
| 1894 | case RegGroupInfo::PPR: |
| 1895 | break; |
| 1896 | case RegGroupInfo::ZPR: { |
| 1897 | // Windows support is possible but the order requirement is reversed. |
| 1898 | // Also, WinCFI has no support for group ZPR loads/stores yet. |
| 1899 | if (isTargetWindows(MF) || AFI->getPredicateRegForFillSpill() == 0) |
| 1900 | break; |
| 1901 | |
| 1902 | // We expect to see pairs and quads in descending order (e.g. [z7, z6, |
| 1903 | // z5, z4]). We ensure this in `orderZPRCalleeSavesForGroups()`. This |
| 1904 | // is required as (for Linux) StackFillDir is negative (so we start at |
| 1905 | // higher addresses) and we need to ensure the lower register in the |
| 1906 | // pair has the lower address to store the registers in the correct |
| 1907 | // order. |
| 1908 | auto GetZPRTupleLowestReg = |
| 1909 | [&](unsigned NumRegs) -> std::optional<Register> { |
| 1910 | // Check Reg1 is the last register in a tuple. |
| 1911 | if (RGI.Reg1 % NumRegs != NumRegs - 1) |
| 1912 | return std::nullopt; |
| 1913 | |
| 1914 | // Check there's enough registers for the tuple. |
| 1915 | if (i + RegInc * NumRegs > Count) |
| 1916 | return std::nullopt; |
| 1917 | |
| 1918 | for (unsigned Idx = 1; Idx < NumRegs; ++Idx) { |
| 1919 | Register Reg = CSI[i + RegInc * Idx].getReg(); |
| 1920 | Register ExpectedReg = RGI.Reg1 - Idx; |
| 1921 | if (Reg != ExpectedReg) |
| 1922 | return std::nullopt; |
| 1923 | } |
| 1924 | |
| 1925 | return RGI.Reg1 - NumRegs + 1; |
| 1926 | }; |
| 1927 | |
| 1928 | for (unsigned TupleSize : {4, 2}) { |
| 1929 | if (auto LowReg = GetZPRTupleLowestReg(TupleSize)) { |
| 1930 | // Note: ST1B has the same offset constraints. |
| 1931 | unsigned Opcode = |
| 1932 | TupleSize == 4 ? AArch64::LD1B_4Z_IMM : AArch64::LD1B_2Z_IMM; |
| 1933 | const int Offset = |
| 1934 | (ScalableByteOffset + StackFillDir * TupleSize * Scale); |
| 1935 | if (isValidMemOpOffset(TII, Opcode, Offset)) { |
| 1936 | RGI.Reg2 = *LowReg; |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | break; |
| 1943 | } |
| 1944 | case RegGroupInfo::VG: |
| 1945 | break; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | // GPRs and FPRs are saved in pairs of 64-bit regs. We expect the CSI |
| 1950 | // list to come in sorted by frame index so that we can issue the store |
| 1951 | // pair instructions directly. Assert if we see anything otherwise. |
| 1952 | // |
| 1953 | // The order of the registers in the list is controlled by |
| 1954 | // getCalleeSavedRegs(), so they will always be in-order, as well. |
| 1955 | [[maybe_unused]] bool RegsOrdered = |
| 1956 | all_of(Range: seq(Size: RGI.getNumRegs() - 1), P: [&](int RegOffset) { |
| 1957 | return CSI[i + RegInc * RegOffset].getFrameIdx() + RegInc == |
| 1958 | CSI[i + RegInc * RegOffset + RegInc].getFrameIdx(); |
| 1959 | }); |
| 1960 | assert(RegsOrdered && "Out of order callee saved regs!" ); |
| 1961 | |
| 1962 | assert((!RGI.isQuad() || RGI.Type == RegGroupInfo::ZPR) && |
| 1963 | "Currently only ZPR's support four-register spills" ); |
| 1964 | |
| 1965 | assert((!RGI.isPaired() || !NeedsFrameRecord || RGI.Reg2 != AArch64::FP || |
| 1966 | RGI.Reg1 == AArch64::LR) && |
| 1967 | "FrameRecord must be allocated together with LR" ); |
| 1968 | |
| 1969 | // Windows AAPCS has FP and LR reversed. |
| 1970 | assert((!RGI.isPaired() || !NeedsFrameRecord || RGI.Reg1 != AArch64::FP || |
| 1971 | RGI.Reg2 == AArch64::LR) && |
| 1972 | "FrameRecord must be allocated together with LR" ); |
| 1973 | |
| 1974 | // MachO's compact unwind format relies on all registers being stored in |
| 1975 | // adjacent register pairs. |
| 1976 | assert((!produceCompactUnwindFrame(AFL, MF) || |
| 1977 | CC == CallingConv::PreserveMost || CC == CallingConv::PreserveAll || |
| 1978 | CC == CallingConv::CXX_FAST_TLS || CC == CallingConv::Win64 || |
| 1979 | (RGI.isPaired() && |
| 1980 | ((RGI.Reg1 == AArch64::LR && RGI.Reg2 == AArch64::FP) || |
| 1981 | RGI.Reg1 + 1 == RGI.Reg2))) && |
| 1982 | "Callee-save registers not saved as adjacent register pair!" ); |
| 1983 | |
| 1984 | RGI.FrameIdx = CSI[i].getFrameIdx(); |
| 1985 | if (IsWindows) |
| 1986 | // RGI.FrameIdx must be the lower index of the group |
| 1987 | RGI.FrameIdx = CSI[i + RegInc * (RGI.getNumRegs() - 1)].getFrameIdx(); |
| 1988 | |
| 1989 | // Realign the scalable offset if necessary. This is relevant when spilling |
| 1990 | // predicates on Windows. |
| 1991 | if (RGI.isScalable() && ScalableByteOffset % Scale != 0) |
| 1992 | ScalableByteOffset = AlignOffset(ScalableByteOffset, Scale); |
| 1993 | |
| 1994 | // Realign the fixed offset if necessary. This is relevant when spilling Q |
| 1995 | // registers after spilling an odd amount of X registers. |
| 1996 | if (!RGI.isScalable() && ByteOffset % Scale != 0) |
| 1997 | ByteOffset = AlignOffset(ByteOffset, Scale); |
| 1998 | |
| 1999 | int OffsetPre = RGI.isScalable() ? ScalableByteOffset : ByteOffset; |
| 2000 | assert(OffsetPre % Scale == 0); |
| 2001 | |
| 2002 | if (RGI.isScalable()) |
| 2003 | ScalableByteOffset += StackFillDir * RGI.getNumRegs() * Scale; |
| 2004 | else |
| 2005 | ByteOffset += StackFillDir * RGI.getNumRegs() * Scale; |
| 2006 | |
| 2007 | // Swift's async context is directly before FP, so allocate an extra |
| 2008 | // 8 bytes for it. |
| 2009 | if (NeedsFrameRecord && AFI->hasSwiftAsyncContext() && |
| 2010 | ((!IsWindows && RGI.Reg2 == AArch64::FP) || |
| 2011 | (IsWindows && RGI.Reg2 == AArch64::LR))) |
| 2012 | ByteOffset += StackFillDir * 8; |
| 2013 | |
| 2014 | // Round up size of non-pair to pair size if we need to pad the |
| 2015 | // callee-save area to ensure 16-byte alignment. |
| 2016 | if (NeedGapToAlignStack && !IsWindows && !RGI.isScalable() && |
| 2017 | RGI.Type != RegGroupInfo::FPR128 && !RGI.isPaired() && |
| 2018 | ByteOffset % 16 != 0) { |
| 2019 | ByteOffset += 8 * StackFillDir; |
| 2020 | assert(MFI.getObjectAlign(RGI.FrameIdx) <= Align(16)); |
| 2021 | // A stack frame with a gap looks like this, bottom up: |
| 2022 | // d9, d8. x21, gap, x20, x19. |
| 2023 | // Set extra alignment on the x21 object to create the gap above it. |
| 2024 | MFI.setObjectAlignment(ObjectIdx: RGI.FrameIdx, Alignment: Align(16)); |
| 2025 | NeedGapToAlignStack = false; |
| 2026 | } |
| 2027 | |
| 2028 | int OffsetPost = RGI.isScalable() ? ScalableByteOffset : ByteOffset; |
| 2029 | assert(OffsetPost % Scale == 0); |
| 2030 | // If filling top down (default), we want the offset after incrementing it. |
| 2031 | // If filling bottom up (WinCFI) we need the original offset. |
| 2032 | int Offset = IsWindows ? OffsetPre : OffsetPost; |
| 2033 | |
| 2034 | // The FP, LR pair goes 8 bytes into our expanded 24-byte slot so that the |
| 2035 | // Swift context can directly precede FP. |
| 2036 | if (NeedsFrameRecord && AFI->hasSwiftAsyncContext() && |
| 2037 | ((!IsWindows && RGI.Reg2 == AArch64::FP) || |
| 2038 | (IsWindows && RGI.Reg2 == AArch64::LR))) |
| 2039 | Offset += 8; |
| 2040 | |
| 2041 | assert( |
| 2042 | (!RGI.isGrouped() || RGI.Type != RegGroupInfo::ZPR || |
| 2043 | isValidMemOpOffset( |
| 2044 | TII, (RGI.isQuad() ? AArch64::LD1B_4Z_IMM : AArch64::LD1B_2Z_IMM), |
| 2045 | Offset)) && |
| 2046 | "Offset invalid for LD1B/ST1B pair/quad variant immediate" ); |
| 2047 | |
| 2048 | RGI.Offset = Offset / Scale; |
| 2049 | assert((!RGI.isGrouped() || RGI.Type == RegGroupInfo::ZPR || |
| 2050 | (!RGI.isScalable() && RGI.Offset >= -64 && RGI.Offset <= 63) || |
| 2051 | (RGI.isScalable() && RGI.Offset >= -256 && RGI.Offset <= 255)) && |
| 2052 | "Offset out of bounds for LDP/STP immediate" ); |
| 2053 | |
| 2054 | auto isFrameRecord = [&] { |
| 2055 | if (RGI.isPaired()) |
| 2056 | return IsWindows ? RGI.Reg1 == AArch64::FP && RGI.Reg2 == AArch64::LR |
| 2057 | : RGI.Reg1 == AArch64::LR && RGI.Reg2 == AArch64::FP; |
| 2058 | // Otherwise, look for the frame record as two unpaired registers. This is |
| 2059 | // needed for -aarch64-stack-hazard-size=<val>, which disables register |
| 2060 | // pairing (as the padding may be too large for the LDP/STP offset). Note: |
| 2061 | // On Windows, this check works out as current reg == FP, next reg == LR, |
| 2062 | // and on other platforms current reg == FP, previous reg == LR. This |
| 2063 | // works out as the correct pre-increment or post-increment offsets |
| 2064 | // respectively. |
| 2065 | return i > 0 && RGI.Reg1 == AArch64::FP && |
| 2066 | CSI[i - 1].getReg() == AArch64::LR; |
| 2067 | }; |
| 2068 | |
| 2069 | // Save the offset to frame record so that the FP register can point to the |
| 2070 | // innermost frame record (spilled FP and LR registers). |
| 2071 | if (NeedsFrameRecord && isFrameRecord()) |
| 2072 | AFI->setCalleeSaveBaseToFrameRecordOffset(Offset); |
| 2073 | |
| 2074 | RegGroups.push_back(Elt: RGI); |
| 2075 | i += RegInc * (RGI.getNumRegs() - 1); |
| 2076 | } |
| 2077 | if (IsWindows) { |
| 2078 | // If we need an alignment gap in the stack, align the topmost stack |
| 2079 | // object. A stack frame with a gap looks like this, bottom up: |
| 2080 | // x19, d8. d9, gap. |
| 2081 | // Set extra alignment on the topmost stack object (the first element in |
| 2082 | // CSI, which goes top down), to create the gap above it. |
| 2083 | if (AFI->hasCalleeSaveStackFreeSpace()) |
| 2084 | MFI.setObjectAlignment(ObjectIdx: CSI[0].getFrameIdx(), Alignment: Align(16)); |
| 2085 | // We iterated bottom up over the registers; flip RegGroups back to top |
| 2086 | // down order. |
| 2087 | std::reverse(first: RegGroups.begin(), last: RegGroups.end()); |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | bool AArch64FrameLowering::spillCalleeSavedRegisters( |
| 2092 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
| 2093 | ArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const { |
| 2094 | MachineFunction &MF = *MBB.getParent(); |
| 2095 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 2096 | auto &TLI = *Subtarget.getTargetLowering(); |
| 2097 | const AArch64InstrInfo &TII = *Subtarget.getInstrInfo(); |
| 2098 | bool NeedsWinCFI = MF.needsWinCFI(); |
| 2099 | DebugLoc DL; |
| 2100 | SmallVector<RegGroupInfo, 8> RegGroups; |
| 2101 | |
| 2102 | computeCalleeSaveRegisterGroups(AFL: *this, MF, CSI, TRI, RegGroups, NeedsFrameRecord: hasFP(MF)); |
| 2103 | |
| 2104 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 2105 | // Refresh the reserved regs in case there are any potential changes since the |
| 2106 | // last freeze. |
| 2107 | MRI.freezeReservedRegs(); |
| 2108 | |
| 2109 | if (homogeneousPrologEpilog(MF)) { |
| 2110 | auto MIB = BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: AArch64::HOM_Prolog)) |
| 2111 | .setMIFlag(MachineInstr::FrameSetup); |
| 2112 | |
| 2113 | for (auto &RGI : RegGroups) { |
| 2114 | MIB.addReg(RegNo: RGI.Reg1); |
| 2115 | MIB.addReg(RegNo: RGI.Reg2); |
| 2116 | |
| 2117 | // Update register live in. |
| 2118 | if (!MRI.isReserved(PhysReg: RGI.Reg1)) |
| 2119 | MBB.addLiveIn(PhysReg: RGI.Reg1); |
| 2120 | if (RGI.isPaired() && !MRI.isReserved(PhysReg: RGI.Reg2)) |
| 2121 | MBB.addLiveIn(PhysReg: RGI.Reg2); |
| 2122 | } |
| 2123 | return true; |
| 2124 | } |
| 2125 | bool PTrueCreated = false; |
| 2126 | for (const RegGroupInfo &RGI : llvm::reverse(C&: RegGroups)) { |
| 2127 | Register Reg1 = RGI.Reg1; |
| 2128 | Register Reg2 = RGI.Reg2; |
| 2129 | unsigned StrOpc; |
| 2130 | |
| 2131 | // Issue sequence of spills for cs regs. The first spill may be converted |
| 2132 | // to a pre-decrement store later by emitPrologue if the callee-save stack |
| 2133 | // area allocation can't be combined with the local stack area allocation. |
| 2134 | // For example: |
| 2135 | // stp x22, x21, [sp, #0] // addImm(+0) |
| 2136 | // stp x20, x19, [sp, #16] // addImm(+2) |
| 2137 | // stp fp, lr, [sp, #32] // addImm(+4) |
| 2138 | // Rationale: This sequence saves uop updates compared to a sequence of |
| 2139 | // pre-increment spills like stp xi,xj,[sp,#-16]! |
| 2140 | // Note: Similar rationale and sequence for restores in epilog. |
| 2141 | unsigned Size = TRI->getSpillSize(RC: *RGI.RC); |
| 2142 | Align Alignment = TRI->getSpillAlign(RC: *RGI.RC); |
| 2143 | switch (RGI.Type) { |
| 2144 | case RegGroupInfo::GPR: |
| 2145 | StrOpc = RGI.isPaired() ? AArch64::STPXi : AArch64::STRXui; |
| 2146 | break; |
| 2147 | case RegGroupInfo::FPR64: |
| 2148 | StrOpc = RGI.isPaired() ? AArch64::STPDi : AArch64::STRDui; |
| 2149 | break; |
| 2150 | case RegGroupInfo::FPR128: |
| 2151 | StrOpc = RGI.isPaired() ? AArch64::STPQi : AArch64::STRQui; |
| 2152 | break; |
| 2153 | case RegGroupInfo::ZPR: |
| 2154 | StrOpc = RGI.isQuad() |
| 2155 | ? AArch64::ST1B_4Z_IMM |
| 2156 | : (RGI.isPaired() ? AArch64::ST1B_2Z_IMM : AArch64::STR_ZXI); |
| 2157 | break; |
| 2158 | case RegGroupInfo::PPR: |
| 2159 | StrOpc = AArch64::STR_PXI; |
| 2160 | break; |
| 2161 | case RegGroupInfo::VG: |
| 2162 | StrOpc = AArch64::STRXui; |
| 2163 | break; |
| 2164 | } |
| 2165 | |
| 2166 | Register X0Scratch; |
| 2167 | llvm::scope_exit RestoreX0([&] { |
| 2168 | if (X0Scratch.isValid()) |
| 2169 | BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: TargetOpcode::COPY), DestReg: AArch64::X0) |
| 2170 | .addReg(RegNo: X0Scratch) |
| 2171 | .setMIFlag(MachineInstr::FrameSetup); |
| 2172 | }); |
| 2173 | |
| 2174 | if (Reg1 == AArch64::VG) { |
| 2175 | // Find an available register to store value of VG to. |
| 2176 | Reg1 = findScratchNonCalleeSaveRegister(MBB: &MBB, HasCall: true); |
| 2177 | assert(Reg1.isValid()); |
| 2178 | if (MF.getSubtarget<AArch64Subtarget>().hasSVE()) { |
| 2179 | BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: AArch64::CNTD_XPiI), DestReg: Reg1) |
| 2180 | .addImm(Val: 31) |
| 2181 | .addImm(Val: 1) |
| 2182 | .setMIFlag(MachineInstr::FrameSetup); |
| 2183 | } else { |
| 2184 | const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>(); |
| 2185 | if (any_of(Range: MBB.liveins(), |
| 2186 | P: [&STI](const MachineBasicBlock::RegisterMaskPair &LiveIn) { |
| 2187 | return STI.getRegisterInfo()->isSuperOrSubRegisterEq( |
| 2188 | RegA: AArch64::X0, RegB: LiveIn.PhysReg); |
| 2189 | })) { |
| 2190 | X0Scratch = Reg1; |
| 2191 | BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: TargetOpcode::COPY), DestReg: X0Scratch) |
| 2192 | .addReg(RegNo: AArch64::X0) |
| 2193 | .setMIFlag(MachineInstr::FrameSetup); |
| 2194 | } |
| 2195 | |
| 2196 | RTLIB::Libcall LC = RTLIB::SMEABI_GET_CURRENT_VG; |
| 2197 | const uint32_t *RegMask = |
| 2198 | TRI->getCallPreservedMask(MF, TLI.getLibcallCallingConv(Call: LC)); |
| 2199 | BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: AArch64::BL)) |
| 2200 | .addExternalSymbol(FnName: TLI.getLibcallName(Call: LC)) |
| 2201 | .addRegMask(Mask: RegMask) |
| 2202 | .addReg(RegNo: AArch64::X0, Flags: RegState::ImplicitDefine) |
| 2203 | .setMIFlag(MachineInstr::FrameSetup); |
| 2204 | Reg1 = AArch64::X0; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | LLVM_DEBUG(dbgs() << "CSR spill: " ); |
| 2209 | LLVM_DEBUG(RGI.dump(TRI)); |
| 2210 | |
| 2211 | assert((!isTargetWindows(MF) || |
| 2212 | !(Reg1 == AArch64::LR && Reg2 == AArch64::FP)) && |
| 2213 | "Windows unwdinding requires a consecutive (FP,LR) pair" ); |
| 2214 | |
| 2215 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2216 | unsigned PnReg = AFI->getPredicateRegForFillSpill(); |
| 2217 | |
| 2218 | if (!PTrueCreated && RGI.isGrouped() && RGI.isScalable()) { |
| 2219 | [[maybe_unused]] const AArch64Subtarget &Subtarget = |
| 2220 | MF.getSubtarget<AArch64Subtarget>(); |
| 2221 | assert((PnReg != 0 && enableMultiVectorSpillFill(Subtarget, MF)) && |
| 2222 | "Expects SVE2.1 or SME2 target and a predicate register" ); |
| 2223 | PTrueCreated = true; |
| 2224 | BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: AArch64::PTRUE_C_B), DestReg: PnReg) |
| 2225 | .setMIFlags(MachineInstr::FrameSetup); |
| 2226 | } |
| 2227 | |
| 2228 | SmallVector<Register, 4> Registers = RGI.getRegisters(); |
| 2229 | for (Register Reg : reverse_conditionally(C&: Registers, ShouldReverse: isTargetWindows(MF))) |
| 2230 | if (!MRI.isReserved(PhysReg: Reg)) |
| 2231 | MBB.addLiveIn(PhysReg: Reg); |
| 2232 | |
| 2233 | MachineInstrBuilder MIB = BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII.get(Opcode: StrOpc)); |
| 2234 | if (RGI.isGrouped() && RGI.isScalable()) { |
| 2235 | assert(!isTargetWindows(MF) && |
| 2236 | "Scalable register groups are not supported by Windows CFI yet" ); |
| 2237 | #ifdef EXPENSIVE_CHECKS |
| 2238 | auto IsPPR = [](const RegGroupInfo &c) { |
| 2239 | return c.Type == RegGroupInfo::PPR; |
| 2240 | }; |
| 2241 | auto PPRBegin = std::find_if(RegGroups.begin(), RegGroups.end(), IsPPR); |
| 2242 | auto IsZPR = [](const RegGroupInfo &c) { |
| 2243 | return c.Type == RegGroupInfo::ZPR; |
| 2244 | }; |
| 2245 | auto ZPRBegin = std::find_if(RegGroups.begin(), RegGroups.end(), IsZPR); |
| 2246 | assert(!(PPRBegin < ZPRBegin) && |
| 2247 | "Expected callee save predicate to be handled first" ); |
| 2248 | #endif |
| 2249 | |
| 2250 | assert(RGI.Reg2 < RGI.Reg1 && "Expected reversed ZPR group" ); |
| 2251 | MIB.addReg(RegNo: RGI.getAsMultiVectorRegister()); |
| 2252 | |
| 2253 | MIB.addReg(RegNo: PnReg); |
| 2254 | MIB.addReg(RegNo: AArch64::SP) |
| 2255 | .addImm(Val: RGI.Offset / |
| 2256 | RGI.getNumRegs()) // [sp, #imm*size*vscale], |
| 2257 | // where size*vscale is implicit |
| 2258 | .setMIFlag(MachineInstr::FrameSetup); |
| 2259 | } else { // The code when the pair of ZReg is not present |
| 2260 | // Windows unwind codes require consecutive registers if registers are |
| 2261 | // paired. Make the switch here, so that the code below will save (x,x+1) |
| 2262 | // and not (x+1,x). |
| 2263 | if (isTargetWindows(MF) && RGI.isPaired()) |
| 2264 | std::swap(a&: Reg1, b&: Reg2); |
| 2265 | if (RGI.isPaired()) { |
| 2266 | MIB.addReg(RegNo: Reg2, Flags: getPrologueDeath(MF, Reg: Reg2)); |
| 2267 | } |
| 2268 | MIB.addReg(RegNo: Reg1, Flags: getPrologueDeath(MF, Reg: Reg1)) |
| 2269 | .addReg(RegNo: AArch64::SP) |
| 2270 | .addImm(Val: RGI.Offset) // [sp, #offset*vscale], |
| 2271 | // where factor*vscale is implicit |
| 2272 | .setMIFlag(MachineInstr::FrameSetup); |
| 2273 | if (NeedsWinCFI) |
| 2274 | insertSEH(MBBI: MIB, TII, Flag: MachineInstr::FrameSetup); |
| 2275 | } |
| 2276 | |
| 2277 | // Handle the StackIDs. |
| 2278 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 2279 | for (int FI : |
| 2280 | reverse_conditionally(C: RGI.getFrameIndexes(), ShouldReverse: !isTargetWindows(MF))) { |
| 2281 | MIB.addMemOperand( |
| 2282 | MMO: MF.getMachineMemOperand(PtrInfo: MachinePointerInfo::getFixedStack(MF, FI), |
| 2283 | F: MachineMemOperand::MOStore, Size, BaseAlignment: Alignment)); |
| 2284 | if (RGI.Type == RegGroupInfo::ZPR) |
| 2285 | MFI.setStackID(ObjectIdx: FI, ID: TargetStackID::ScalableVector); |
| 2286 | else if (RGI.Type == RegGroupInfo::PPR) |
| 2287 | MFI.setStackID(ObjectIdx: FI, ID: TargetStackID::ScalablePredicateVector); |
| 2288 | } |
| 2289 | } |
| 2290 | return true; |
| 2291 | } |
| 2292 | |
| 2293 | bool AArch64FrameLowering::restoreCalleeSavedRegisters( |
| 2294 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, |
| 2295 | MutableArrayRef<CalleeSavedInfo> CSI, const TargetRegisterInfo *TRI) const { |
| 2296 | MachineFunction &MF = *MBB.getParent(); |
| 2297 | const AArch64InstrInfo &TII = |
| 2298 | *MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 2299 | DebugLoc DL; |
| 2300 | SmallVector<RegGroupInfo, 8> RegGroups; |
| 2301 | bool NeedsWinCFI = MF.needsWinCFI(); |
| 2302 | |
| 2303 | if (MBBI != MBB.end()) |
| 2304 | DL = MBBI->getDebugLoc(); |
| 2305 | |
| 2306 | computeCalleeSaveRegisterGroups(AFL: *this, MF, CSI, TRI, RegGroups, NeedsFrameRecord: hasFP(MF)); |
| 2307 | if (homogeneousPrologEpilog(MF, Exit: &MBB)) { |
| 2308 | auto MIB = BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::HOM_Epilog)) |
| 2309 | .setMIFlag(MachineInstr::FrameDestroy); |
| 2310 | for (auto &RGI : RegGroups) { |
| 2311 | MIB.addReg(RegNo: RGI.Reg1, Flags: RegState::Define); |
| 2312 | MIB.addReg(RegNo: RGI.Reg2, Flags: RegState::Define); |
| 2313 | } |
| 2314 | return true; |
| 2315 | } |
| 2316 | |
| 2317 | // For performance reasons restore SVE register in increasing order |
| 2318 | auto IsPPR = [](const RegGroupInfo &c) { |
| 2319 | return c.Type == RegGroupInfo::PPR; |
| 2320 | }; |
| 2321 | auto PPRBegin = llvm::find_if(Range&: RegGroups, P: IsPPR); |
| 2322 | auto PPREnd = std::find_if_not(first: PPRBegin, last: RegGroups.end(), pred: IsPPR); |
| 2323 | std::reverse(first: PPRBegin, last: PPREnd); |
| 2324 | auto IsZPR = [](const RegGroupInfo &c) { |
| 2325 | return c.Type == RegGroupInfo::ZPR; |
| 2326 | }; |
| 2327 | auto ZPRBegin = llvm::find_if(Range&: RegGroups, P: IsZPR); |
| 2328 | auto ZPREnd = std::find_if_not(first: ZPRBegin, last: RegGroups.end(), pred: IsZPR); |
| 2329 | std::reverse(first: ZPRBegin, last: ZPREnd); |
| 2330 | |
| 2331 | // If the return address is authenticated before the epilogue, it stays in LR |
| 2332 | // rather than being restored here, as that would replace it with the |
| 2333 | // unauthenticated value from the stack. |
| 2334 | bool KeepLR = authenticatesReturnAddressEarly(MF, MBB); |
| 2335 | |
| 2336 | bool PTrueCreated = false; |
| 2337 | for (const RegGroupInfo &RGI : RegGroups) { |
| 2338 | Register Reg1 = RGI.Reg1; |
| 2339 | Register Reg2 = RGI.Reg2; |
| 2340 | |
| 2341 | // Issue sequence of restores for cs regs. The last restore may be converted |
| 2342 | // to a post-increment load later by emitEpilogue if the callee-save stack |
| 2343 | // area allocation can't be combined with the local stack area allocation. |
| 2344 | // For example: |
| 2345 | // ldp fp, lr, [sp, #32] // addImm(+4) |
| 2346 | // ldp x20, x19, [sp, #16] // addImm(+2) |
| 2347 | // ldp x22, x21, [sp, #0] // addImm(+0) |
| 2348 | // Note: see comment in spillCalleeSavedRegisters() |
| 2349 | unsigned LdrOpc; |
| 2350 | unsigned Size = TRI->getSpillSize(RC: *RGI.RC); |
| 2351 | Align Alignment = TRI->getSpillAlign(RC: *RGI.RC); |
| 2352 | switch (RGI.Type) { |
| 2353 | case RegGroupInfo::GPR: |
| 2354 | LdrOpc = RGI.isPaired() ? AArch64::LDPXi : AArch64::LDRXui; |
| 2355 | break; |
| 2356 | case RegGroupInfo::FPR64: |
| 2357 | LdrOpc = RGI.isPaired() ? AArch64::LDPDi : AArch64::LDRDui; |
| 2358 | break; |
| 2359 | case RegGroupInfo::FPR128: |
| 2360 | LdrOpc = RGI.isPaired() ? AArch64::LDPQi : AArch64::LDRQui; |
| 2361 | break; |
| 2362 | case RegGroupInfo::ZPR: |
| 2363 | LdrOpc = RGI.isQuad() |
| 2364 | ? AArch64::LD1B_4Z_IMM |
| 2365 | : (RGI.isPaired() ? AArch64::LD1B_2Z_IMM : AArch64::LDR_ZXI); |
| 2366 | break; |
| 2367 | case RegGroupInfo::PPR: |
| 2368 | LdrOpc = AArch64::LDR_PXI; |
| 2369 | break; |
| 2370 | case RegGroupInfo::VG: |
| 2371 | continue; |
| 2372 | } |
| 2373 | |
| 2374 | LLVM_DEBUG(dbgs() << "CSR restore: " ); |
| 2375 | LLVM_DEBUG(RGI.dump(TRI)); |
| 2376 | |
| 2377 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2378 | unsigned PnReg = AFI->getPredicateRegForFillSpill(); |
| 2379 | |
| 2380 | if (!PTrueCreated && RGI.isGrouped() && RGI.isScalable()) { |
| 2381 | [[maybe_unused]] const AArch64Subtarget &Subtarget = |
| 2382 | MF.getSubtarget<AArch64Subtarget>(); |
| 2383 | assert((PnReg != 0 && enableMultiVectorSpillFill(Subtarget, MF)) && |
| 2384 | "Expects SVE2.1 or SME2 target and a predicate register" ); |
| 2385 | PTrueCreated = true; |
| 2386 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::PTRUE_C_B), DestReg: PnReg) |
| 2387 | .setMIFlags(MachineInstr::FrameDestroy); |
| 2388 | } |
| 2389 | |
| 2390 | if (KeepLR && RGI.Type == RegGroupInfo::GPR && |
| 2391 | (Reg1 == AArch64::LR || Reg2 == AArch64::LR)) { |
| 2392 | // Windows pairs LR with the register before it, which is the one at the |
| 2393 | // lower address, so restore just that register. The pair has not been |
| 2394 | // swapped yet at this point, so that register is Reg1. |
| 2395 | if (!RGI.isPaired()) |
| 2396 | continue; |
| 2397 | assert(Reg2 == AArch64::LR && "Expected LR to be the second of the pair" ); |
| 2398 | MachineInstrBuilder MIB = |
| 2399 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::LDRXui)); |
| 2400 | MIB.addReg(RegNo: Reg1, Flags: getDefRegState(B: true)); |
| 2401 | MIB.addReg(RegNo: AArch64::SP) |
| 2402 | .addImm(Val: RGI.Offset) |
| 2403 | .setMIFlag(MachineInstr::FrameDestroy); |
| 2404 | MIB.addMemOperand(MMO: MF.getMachineMemOperand( |
| 2405 | PtrInfo: MachinePointerInfo::getFixedStack(MF, FI: RGI.FrameIdx), |
| 2406 | F: MachineMemOperand::MOLoad, Size: 8, BaseAlignment: Align(8))); |
| 2407 | if (NeedsWinCFI) |
| 2408 | insertSEH(MBBI: MIB, TII, Flag: MachineInstr::FrameDestroy); |
| 2409 | continue; |
| 2410 | } |
| 2411 | |
| 2412 | MachineInstrBuilder MIB = BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: LdrOpc)); |
| 2413 | if (RGI.isGrouped() && RGI.isScalable()) { |
| 2414 | assert(!isTargetWindows(MF) && |
| 2415 | "Scalable register groups are not supported by Windows CFI yet" ); |
| 2416 | #ifdef EXPENSIVE_CHECKS |
| 2417 | assert(!(PPRBegin < ZPRBegin) && |
| 2418 | "Expected callee save predicate to be handled first" ); |
| 2419 | #endif |
| 2420 | assert(RGI.Reg2 < RGI.Reg1 && "Expected reversed ZPR group" ); |
| 2421 | MIB.addReg(RegNo: RGI.getAsMultiVectorRegister(), Flags: getDefRegState(B: true)); |
| 2422 | MIB.addReg(RegNo: PnReg); |
| 2423 | MIB.addReg(RegNo: AArch64::SP) |
| 2424 | .addImm(Val: RGI.Offset / |
| 2425 | RGI.getNumRegs()) // [sp, #imm*size*vscale] |
| 2426 | // where size*vscale is implicit |
| 2427 | .setMIFlag(MachineInstr::FrameDestroy); |
| 2428 | } else { |
| 2429 | // Windows unwind codes require consecutive registers if registers are |
| 2430 | // paired. Make the switch here, so that the code below will save (x,x+1) |
| 2431 | // and not (x+1,x). |
| 2432 | if (isTargetWindows(MF) && RGI.isPaired()) |
| 2433 | std::swap(a&: Reg1, b&: Reg2); |
| 2434 | if (RGI.isPaired()) |
| 2435 | MIB.addReg(RegNo: Reg2, Flags: getDefRegState(B: true)); |
| 2436 | MIB.addReg(RegNo: Reg1, Flags: getDefRegState(B: true)); |
| 2437 | MIB.addReg(RegNo: AArch64::SP) |
| 2438 | .addImm(Val: RGI.Offset) // [sp, #offset*vscale] |
| 2439 | // where factor*vscale is implicit |
| 2440 | .setMIFlag(MachineInstr::FrameDestroy); |
| 2441 | if (NeedsWinCFI) |
| 2442 | insertSEH(MBBI: MIB, TII, Flag: MachineInstr::FrameDestroy); |
| 2443 | } |
| 2444 | |
| 2445 | for (int FI : |
| 2446 | reverse_conditionally(C: RGI.getFrameIndexes(), ShouldReverse: !isTargetWindows(MF))) |
| 2447 | MIB.addMemOperand( |
| 2448 | MMO: MF.getMachineMemOperand(PtrInfo: MachinePointerInfo::getFixedStack(MF, FI), |
| 2449 | F: MachineMemOperand::MOLoad, Size, BaseAlignment: Alignment)); |
| 2450 | } |
| 2451 | return true; |
| 2452 | } |
| 2453 | |
| 2454 | // Return the FrameID for a MMO. |
| 2455 | static std::optional<int> getMMOFrameID(MachineMemOperand *MMO, |
| 2456 | const MachineFrameInfo &MFI) { |
| 2457 | auto *PSV = |
| 2458 | dyn_cast_or_null<FixedStackPseudoSourceValue>(Val: MMO->getPseudoValue()); |
| 2459 | if (PSV) |
| 2460 | return std::optional<int>(PSV->getFrameIndex()); |
| 2461 | |
| 2462 | if (MMO->getValue()) { |
| 2463 | if (auto *Al = dyn_cast<AllocaInst>(Val: getUnderlyingObject(V: MMO->getValue()))) { |
| 2464 | for (int FI = MFI.getObjectIndexBegin(); FI < MFI.getObjectIndexEnd(); |
| 2465 | FI++) |
| 2466 | if (MFI.getObjectAllocation(ObjectIdx: FI) == Al) |
| 2467 | return FI; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | return std::nullopt; |
| 2472 | } |
| 2473 | |
| 2474 | // Return the FrameID for a Load/Store instruction by looking at the first MMO. |
| 2475 | static std::optional<int> getLdStFrameID(const MachineInstr &MI, |
| 2476 | const MachineFrameInfo &MFI) { |
| 2477 | if (!MI.mayLoadOrStore() || MI.getNumMemOperands() < 1) |
| 2478 | return std::nullopt; |
| 2479 | |
| 2480 | return getMMOFrameID(MMO: *MI.memoperands_begin(), MFI); |
| 2481 | } |
| 2482 | |
| 2483 | // Returns true if the LDST MachineInstr \p MI is a PPR access. |
| 2484 | static bool isPPRAccess(const MachineInstr &MI) { |
| 2485 | return AArch64::PPRRegClass.contains(Reg: MI.getOperand(i: 0).getReg()); |
| 2486 | } |
| 2487 | |
| 2488 | // Check if a Hazard slot is needed for the current function, and if so create |
| 2489 | // one for it. The index is stored in AArch64FunctionInfo->StackHazardSlotIndex, |
| 2490 | // which can be used to determine if any hazard padding is needed. |
| 2491 | void AArch64FrameLowering::determineStackHazardSlot( |
| 2492 | MachineFunction &MF, BitVector &SavedRegs) const { |
| 2493 | unsigned StackHazardSize = getStackHazardSize(MF); |
| 2494 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2495 | if (StackHazardSize == 0 || StackHazardSize % 16 != 0 || |
| 2496 | AFI->hasStackHazardSlotIndex()) |
| 2497 | return; |
| 2498 | |
| 2499 | // Stack hazards are only needed in streaming functions. |
| 2500 | SMEAttrs Attrs = AFI->getSMEFnAttrs(); |
| 2501 | const AArch64Options &CLOpts = |
| 2502 | MF.getSubtarget<AArch64Subtarget>().getCLOpts(); |
| 2503 | if (!CLOpts.stack_hazard_in_non_streaming && |
| 2504 | Attrs.hasNonStreamingInterfaceAndBody()) |
| 2505 | return; |
| 2506 | |
| 2507 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 2508 | |
| 2509 | // Add a hazard slot if there are any CSR FPR registers, or are any fp-only |
| 2510 | // stack objects. |
| 2511 | bool HasFPRCSRs = any_of(Range: SavedRegs.set_bits(), P: [](unsigned Reg) { |
| 2512 | return AArch64::FPR64RegClass.contains(Reg) || |
| 2513 | AArch64::FPR128RegClass.contains(Reg) || |
| 2514 | AArch64::ZPRRegClass.contains(Reg); |
| 2515 | }); |
| 2516 | bool HasPPRCSRs = any_of(Range: SavedRegs.set_bits(), P: [](unsigned Reg) { |
| 2517 | return AArch64::PPRRegClass.contains(Reg); |
| 2518 | }); |
| 2519 | bool HasFPRStackObjects = false; |
| 2520 | bool HasPPRStackObjects = false; |
| 2521 | if (!HasFPRCSRs || CLOpts.split_sve_objects) { |
| 2522 | enum SlotType : uint8_t { |
| 2523 | Unknown = 0, |
| 2524 | ZPRorFPR = 1 << 0, |
| 2525 | PPR = 1 << 1, |
| 2526 | GPR = 1 << 2, |
| 2527 | LLVM_MARK_AS_BITMASK_ENUM(GPR) |
| 2528 | }; |
| 2529 | |
| 2530 | // Find stack slots solely used for one kind of register (ZPR, PPR, etc.), |
| 2531 | // based on the kinds of accesses used in the function. |
| 2532 | SmallVector<SlotType> SlotTypes(MFI.getObjectIndexEnd(), SlotType::Unknown); |
| 2533 | for (auto &MBB : MF) { |
| 2534 | for (auto &MI : MBB) { |
| 2535 | std::optional<int> FI = getLdStFrameID(MI, MFI); |
| 2536 | if (!FI || FI < 0 || FI > int(SlotTypes.size())) |
| 2537 | continue; |
| 2538 | if (MFI.hasScalableStackID(ObjectIdx: *FI)) { |
| 2539 | SlotTypes[*FI] |= |
| 2540 | isPPRAccess(MI) ? SlotType::PPR : SlotType::ZPRorFPR; |
| 2541 | } else { |
| 2542 | SlotTypes[*FI] |= AArch64InstrInfo::isFpOrNEON(MI) |
| 2543 | ? SlotType::ZPRorFPR |
| 2544 | : SlotType::GPR; |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | for (int FI = 0; FI < int(SlotTypes.size()); ++FI) { |
| 2550 | HasFPRStackObjects |= SlotTypes[FI] == SlotType::ZPRorFPR; |
| 2551 | // For SplitSVEObjects remember that this stack slot is a predicate, this |
| 2552 | // will be needed later when determining the frame layout. |
| 2553 | if (SlotTypes[FI] == SlotType::PPR) { |
| 2554 | MFI.setStackID(ObjectIdx: FI, ID: TargetStackID::ScalablePredicateVector); |
| 2555 | HasPPRStackObjects = true; |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (HasFPRCSRs || HasFPRStackObjects) { |
| 2561 | int ID = MFI.CreateStackObject(Size: StackHazardSize, Alignment: Align(16), isSpillSlot: false); |
| 2562 | LLVM_DEBUG(dbgs() << "Created Hazard slot at " << ID << " size " |
| 2563 | << StackHazardSize << "\n" ); |
| 2564 | AFI->setStackHazardSlotIndex(ID); |
| 2565 | } |
| 2566 | |
| 2567 | if (!AFI->hasStackHazardSlotIndex()) |
| 2568 | return; |
| 2569 | |
| 2570 | if (CLOpts.split_sve_objects) { |
| 2571 | CallingConv::ID CC = MF.getFunction().getCallingConv(); |
| 2572 | if (AFI->isSVECC() || CC == CallingConv::AArch64_SVE_VectorCall) { |
| 2573 | AFI->setSplitSVEObjects(true); |
| 2574 | LLVM_DEBUG(dbgs() << "Using SplitSVEObjects for SVE CC function\n" ); |
| 2575 | return; |
| 2576 | } |
| 2577 | |
| 2578 | // We only use SplitSVEObjects in non-SVE CC functions if there's a |
| 2579 | // possibility of a stack hazard between PPRs and ZPRs/FPRs. |
| 2580 | LLVM_DEBUG(dbgs() << "Determining if SplitSVEObjects should be used in " |
| 2581 | "non-SVE CC function...\n" ); |
| 2582 | |
| 2583 | // If another calling convention is explicitly set FPRs can't be promoted to |
| 2584 | // ZPR callee-saves. |
| 2585 | if (!is_contained(Set: {CallingConv::C, CallingConv::Fast}, Element: CC)) { |
| 2586 | LLVM_DEBUG( |
| 2587 | dbgs() |
| 2588 | << "Calling convention is not supported with SplitSVEObjects\n" ); |
| 2589 | return; |
| 2590 | } |
| 2591 | |
| 2592 | if (!HasPPRCSRs && !HasPPRStackObjects) { |
| 2593 | LLVM_DEBUG( |
| 2594 | dbgs() << "Not using SplitSVEObjects as no PPRs are on the stack\n" ); |
| 2595 | return; |
| 2596 | } |
| 2597 | |
| 2598 | if (!HasFPRCSRs && !HasFPRStackObjects) { |
| 2599 | LLVM_DEBUG( |
| 2600 | dbgs() |
| 2601 | << "Not using SplitSVEObjects as no FPRs or ZPRs are on the stack\n" ); |
| 2602 | return; |
| 2603 | } |
| 2604 | |
| 2605 | [[maybe_unused]] const AArch64Subtarget &Subtarget = |
| 2606 | MF.getSubtarget<AArch64Subtarget>(); |
| 2607 | assert(Subtarget.isSVEorStreamingSVEAvailable() && |
| 2608 | "Expected SVE to be available for PPRs" ); |
| 2609 | |
| 2610 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 2611 | // With SplitSVEObjects the CS hazard padding is placed between the |
| 2612 | // PPRs and ZPRs. If there are any FPR CS there would be a hazard between |
| 2613 | // them and the CS GRPs. Avoid this by promoting all FPR CS to ZPRs. |
| 2614 | BitVector FPRZRegs(SavedRegs.size()); |
| 2615 | for (size_t Reg = 0, E = SavedRegs.size(); HasFPRCSRs && Reg < E; ++Reg) { |
| 2616 | BitVector::reference RegBit = SavedRegs[Reg]; |
| 2617 | if (!RegBit) |
| 2618 | continue; |
| 2619 | unsigned SubRegIdx = 0; |
| 2620 | if (AArch64::FPR64RegClass.contains(Reg)) |
| 2621 | SubRegIdx = AArch64::dsub; |
| 2622 | else if (AArch64::FPR128RegClass.contains(Reg)) |
| 2623 | SubRegIdx = AArch64::zsub; |
| 2624 | else |
| 2625 | continue; |
| 2626 | // Clear the bit for the FPR save. |
| 2627 | RegBit = false; |
| 2628 | // Mark that we should save the corresponding ZPR. |
| 2629 | Register ZReg = |
| 2630 | TRI->getMatchingSuperReg(Reg, SubIdx: SubRegIdx, RC: &AArch64::ZPRRegClass); |
| 2631 | FPRZRegs.set(ZReg); |
| 2632 | } |
| 2633 | SavedRegs |= FPRZRegs; |
| 2634 | |
| 2635 | AFI->setSplitSVEObjects(true); |
| 2636 | LLVM_DEBUG(dbgs() << "SplitSVEObjects enabled!\n" ); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | void AArch64FrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 2641 | BitVector &SavedRegs, |
| 2642 | RegScavenger *RS) const { |
| 2643 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2644 | // All calls are tail calls in GHC calling conv, and functions have no |
| 2645 | // prologue/epilogue. |
| 2646 | if (MF.getFunction().getCallingConv() == CallingConv::GHC) { |
| 2647 | AFI->setCalleeSavedStackSize(0); |
| 2648 | AFI->setSVECalleeSavedStackSize(/*ZPR=*/0, /*PPR=*/0); |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 2653 | |
| 2654 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 2655 | const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); |
| 2656 | Register UnspilledCSGPR; |
| 2657 | Register UnspilledCSGPRPaired; |
| 2658 | |
| 2659 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 2660 | const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); |
| 2661 | |
| 2662 | MCRegister BasePointerReg = |
| 2663 | RegInfo->hasBasePointer(MF) ? RegInfo->getBaseRegister() : MCRegister(); |
| 2664 | |
| 2665 | Register ; |
| 2666 | bool HasUnpairedGPR64 = false; |
| 2667 | bool HasPairZReg = false; |
| 2668 | BitVector UserReservedRegs = RegInfo->getUserReservedRegs(MF); |
| 2669 | BitVector ReservedRegs = RegInfo->getReservedRegs(MF); |
| 2670 | |
| 2671 | // Figure out which callee-saved registers to save/restore. |
| 2672 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 2673 | const MCRegister Reg = CSRegs[i]; |
| 2674 | |
| 2675 | // Add the base pointer register to SavedRegs if it is callee-save. |
| 2676 | if (Reg == BasePointerReg) |
| 2677 | SavedRegs.set(Reg); |
| 2678 | |
| 2679 | // Don't save manually reserved registers set through +reserve-x#i, |
| 2680 | // even for callee-saved registers, as per GCC's behavior. |
| 2681 | if (UserReservedRegs[Reg]) { |
| 2682 | SavedRegs.reset(Idx: Reg); |
| 2683 | continue; |
| 2684 | } |
| 2685 | |
| 2686 | bool RegUsed = SavedRegs.test(Idx: Reg); |
| 2687 | MCRegister PairedReg; |
| 2688 | const bool RegIsGPR64 = AArch64::GPR64RegClass.contains(Reg); |
| 2689 | if (RegIsGPR64 || AArch64::FPR64RegClass.contains(Reg) || |
| 2690 | AArch64::FPR128RegClass.contains(Reg)) { |
| 2691 | // Compensate for odd numbers of GP CSRs. |
| 2692 | // For now, all the known cases of odd number of CSRs are of GPRs. |
| 2693 | if (HasUnpairedGPR64) |
| 2694 | PairedReg = CSRegs[i % 2 == 0 ? i - 1 : i + 1]; |
| 2695 | else |
| 2696 | PairedReg = CSRegs[i ^ 1]; |
| 2697 | } |
| 2698 | |
| 2699 | // If the function requires all the GP registers to save (SavedRegs), |
| 2700 | // and there are an odd number of GP CSRs at the same time (CSRegs), |
| 2701 | // PairedReg could be in a different register class from Reg, which would |
| 2702 | // lead to a FPR (usually D8) accidentally being marked saved. |
| 2703 | if (RegIsGPR64 && !AArch64::GPR64RegClass.contains(Reg: PairedReg)) { |
| 2704 | PairedReg = Register(); |
| 2705 | HasUnpairedGPR64 = true; |
| 2706 | } |
| 2707 | assert(!PairedReg.isValid() || |
| 2708 | AArch64::GPR64RegClass.contains(Reg, PairedReg) || |
| 2709 | AArch64::FPR64RegClass.contains(Reg, PairedReg) || |
| 2710 | AArch64::FPR128RegClass.contains(Reg, PairedReg)); |
| 2711 | |
| 2712 | if (!RegUsed) { |
| 2713 | if (AArch64::GPR64RegClass.contains(Reg) && !ReservedRegs[Reg]) { |
| 2714 | UnspilledCSGPR = Reg; |
| 2715 | UnspilledCSGPRPaired = PairedReg; |
| 2716 | } |
| 2717 | continue; |
| 2718 | } |
| 2719 | |
| 2720 | // MachO's compact unwind format relies on all registers being stored in |
| 2721 | // pairs. |
| 2722 | // FIXME: the usual format is actually better if unwinding isn't needed. |
| 2723 | if (producePairRegisters(MF) && PairedReg.isValid() && |
| 2724 | !SavedRegs.test(Idx: PairedReg)) { |
| 2725 | SavedRegs.set(PairedReg); |
| 2726 | if (AArch64::GPR64RegClass.contains(Reg: PairedReg) && |
| 2727 | !ReservedRegs[PairedReg]) |
| 2728 | ExtraCSSpill = PairedReg; |
| 2729 | } |
| 2730 | // Check if there is a pair of ZRegs, so it can select PReg for spill/fill |
| 2731 | HasPairZReg |= (AArch64::ZPRRegClass.contains(Reg1: Reg, Reg2: CSRegs[i ^ 1]) && |
| 2732 | SavedRegs.test(Idx: CSRegs[i ^ 1])); |
| 2733 | } |
| 2734 | |
| 2735 | if (HasPairZReg && enableMultiVectorSpillFill(Subtarget, MF)) { |
| 2736 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2737 | // Find a suitable predicate register for the multi-vector spill/fill |
| 2738 | // instructions. |
| 2739 | MCRegister PnReg = findFreePredicateReg(SavedRegs); |
| 2740 | if (PnReg.isValid()) |
| 2741 | AFI->setPredicateRegForFillSpill(PnReg); |
| 2742 | // If no free callee-save has been found assign one. |
| 2743 | if (!AFI->getPredicateRegForFillSpill() && |
| 2744 | MF.getFunction().getCallingConv() == |
| 2745 | CallingConv::AArch64_SVE_VectorCall) { |
| 2746 | SavedRegs.set(AArch64::P8); |
| 2747 | AFI->setPredicateRegForFillSpill(AArch64::PN8); |
| 2748 | } |
| 2749 | |
| 2750 | assert(!ReservedRegs[AFI->getPredicateRegForFillSpill()] && |
| 2751 | "Predicate cannot be a reserved register" ); |
| 2752 | } |
| 2753 | |
| 2754 | if (MF.getFunction().getCallingConv() == CallingConv::Win64 && |
| 2755 | !Subtarget.isTargetWindowsOrUEFI()) { |
| 2756 | // For Windows calling convention on a non-windows OS, where X18 is treated |
| 2757 | // as reserved, back up X18 when entering non-windows code (marked with the |
| 2758 | // Windows calling convention) and restore when returning regardless of |
| 2759 | // whether the individual function uses it - it might call other functions |
| 2760 | // that clobber it. |
| 2761 | SavedRegs.set(AArch64::X18); |
| 2762 | } |
| 2763 | |
| 2764 | // Determine if a Hazard slot should be used and where it should go. |
| 2765 | // If SplitSVEObjects is used, the hazard padding is placed between the PPRs |
| 2766 | // and ZPRs. Otherwise, it goes in the callee save area. |
| 2767 | determineStackHazardSlot(MF, SavedRegs); |
| 2768 | |
| 2769 | // Calculates the callee saved stack size. |
| 2770 | unsigned CSStackSize = 0; |
| 2771 | unsigned ZPRCSStackSize = 0; |
| 2772 | unsigned PPRCSStackSize = 0; |
| 2773 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 2774 | for (unsigned Reg : SavedRegs.set_bits()) { |
| 2775 | auto *RC = TRI->getMinimalPhysRegClass(Reg: MCRegister(Reg)); |
| 2776 | assert(RC && "expected register class!" ); |
| 2777 | auto SpillSize = TRI->getSpillSize(RC: *RC); |
| 2778 | bool IsZPR = AArch64::ZPRRegClass.contains(Reg); |
| 2779 | bool IsPPR = !IsZPR && AArch64::PPRRegClass.contains(Reg); |
| 2780 | if (IsZPR) |
| 2781 | ZPRCSStackSize += SpillSize; |
| 2782 | else if (IsPPR) |
| 2783 | PPRCSStackSize += SpillSize; |
| 2784 | else { |
| 2785 | // A register and its super-register can both appear in SavedRegs. |
| 2786 | // Only the widest register is actually spilled, so skip such |
| 2787 | // sub-registers here to avoid double-counting the overlap. |
| 2788 | bool SavedSuper = any_of(Range: TRI->superregs(Reg), P: [&](MCPhysReg SuperReg) { |
| 2789 | return SavedRegs.test(Idx: SuperReg); |
| 2790 | }); |
| 2791 | if (!SavedSuper) |
| 2792 | CSStackSize += SpillSize; |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | // Save number of saved regs, so we can easily update CSStackSize later to |
| 2797 | // account for any additional 64-bit GPR saves. Note: After this point |
| 2798 | // only 64-bit GPRs can be added to SavedRegs. |
| 2799 | unsigned NumSavedRegs = SavedRegs.count(); |
| 2800 | |
| 2801 | // If we have hazard padding in the CS area add that to the size. |
| 2802 | if (AFI->isStackHazardIncludedInCalleeSaveArea()) |
| 2803 | CSStackSize += getStackHazardSize(MF); |
| 2804 | |
| 2805 | // Increase the callee-saved stack size if the function has streaming mode |
| 2806 | // changes, as we will need to spill the value of the VG register. |
| 2807 | if (requiresSaveVG(MF)) |
| 2808 | CSStackSize += 8; |
| 2809 | |
| 2810 | // If we must call __arm_get_current_vg in the prologue preserve the LR. |
| 2811 | if (requiresSaveVG(MF) && !Subtarget.hasSVE()) |
| 2812 | SavedRegs.set(AArch64::LR); |
| 2813 | |
| 2814 | // The frame record needs to be created by saving the appropriate registers |
| 2815 | uint64_t EstimatedStackSize = MFI.estimateStackSize(MF); |
| 2816 | if (hasFP(MF) || |
| 2817 | windowsRequiresStackProbe(MF, StackSizeInBytes: EstimatedStackSize + CSStackSize + 16)) { |
| 2818 | SavedRegs.set(AArch64::FP); |
| 2819 | SavedRegs.set(AArch64::LR); |
| 2820 | } |
| 2821 | |
| 2822 | LLVM_DEBUG({ |
| 2823 | dbgs() << "*** determineCalleeSaves\nSaved CSRs:" ; |
| 2824 | for (unsigned Reg : SavedRegs.set_bits()) |
| 2825 | dbgs() << ' ' << printReg(MCRegister(Reg), RegInfo); |
| 2826 | dbgs() << "\n" ; |
| 2827 | }); |
| 2828 | |
| 2829 | // If any callee-saved registers are used, the frame cannot be eliminated. |
| 2830 | auto [ZPRLocalStackSize, PPRLocalStackSize] = |
| 2831 | determineSVEStackSizes(MF, AssignOffsets: AssignObjectOffsets::No); |
| 2832 | uint64_t SVELocals = ZPRLocalStackSize + PPRLocalStackSize; |
| 2833 | uint64_t SVEStackSize = |
| 2834 | alignTo(Value: ZPRCSStackSize + PPRCSStackSize + SVELocals, Align: 16); |
| 2835 | bool CanEliminateFrame = (SavedRegs.count() == 0) && !SVEStackSize; |
| 2836 | |
| 2837 | // The CSR spill slots have not been allocated yet, so estimateStackSize |
| 2838 | // won't include them. |
| 2839 | unsigned EstimatedStackSizeLimit = estimateRSStackSizeLimit(MF); |
| 2840 | |
| 2841 | // We may address some of the stack above the canonical frame address, either |
| 2842 | // for our own arguments or during a call. Include that in calculating whether |
| 2843 | // we have complicated addressing concerns. |
| 2844 | int64_t CalleeStackUsed = 0; |
| 2845 | for (int I = MFI.getObjectIndexBegin(); I != 0; ++I) { |
| 2846 | int64_t FixedOff = MFI.getObjectOffset(ObjectIdx: I); |
| 2847 | if (FixedOff > CalleeStackUsed) |
| 2848 | CalleeStackUsed = FixedOff; |
| 2849 | } |
| 2850 | |
| 2851 | // Conservatively always assume BigStack when there are SVE spills. |
| 2852 | bool BigStack = SVEStackSize || (EstimatedStackSize + CSStackSize + |
| 2853 | CalleeStackUsed) > EstimatedStackSizeLimit; |
| 2854 | if (BigStack || !CanEliminateFrame || RegInfo->cannotEliminateFrame(MF)) |
| 2855 | AFI->setHasStackFrame(true); |
| 2856 | |
| 2857 | // Estimate if we might need to scavenge a register at some point in order |
| 2858 | // to materialize a stack offset. If so, either spill one additional |
| 2859 | // callee-saved register or reserve a special spill slot to facilitate |
| 2860 | // register scavenging. If we already spilled an extra callee-saved register |
| 2861 | // above to keep the number of spills even, we don't need to do anything else |
| 2862 | // here. |
| 2863 | if (BigStack) { |
| 2864 | if (!ExtraCSSpill.isValid() && UnspilledCSGPR.isValid()) { |
| 2865 | LLVM_DEBUG(dbgs() << "Spilling " << printReg(UnspilledCSGPR, RegInfo) |
| 2866 | << " to get a scratch register.\n" ); |
| 2867 | SavedRegs.set(UnspilledCSGPR); |
| 2868 | ExtraCSSpill = UnspilledCSGPR; |
| 2869 | |
| 2870 | // MachO's compact unwind format relies on all registers being stored in |
| 2871 | // pairs, so if we need to spill one extra for BigStack, then we need to |
| 2872 | // store the pair. |
| 2873 | if (producePairRegisters(MF)) { |
| 2874 | if (!UnspilledCSGPRPaired.isValid()) { |
| 2875 | // Failed to make a pair for compact unwind format, revert spilling. |
| 2876 | if (produceCompactUnwindFrame(AFL: *this, MF)) { |
| 2877 | SavedRegs.reset(Idx: UnspilledCSGPR); |
| 2878 | ExtraCSSpill = Register(); |
| 2879 | } |
| 2880 | } else |
| 2881 | SavedRegs.set(UnspilledCSGPRPaired); |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | // If we didn't find an extra callee-saved register to spill, create |
| 2886 | // an emergency spill slot. |
| 2887 | if (!ExtraCSSpill.isValid() || |
| 2888 | MF.getRegInfo().isPhysRegUsed(PhysReg: ExtraCSSpill)) { |
| 2889 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 2890 | const TargetRegisterClass &RC = AArch64::GPR64RegClass; |
| 2891 | unsigned Size = TRI->getSpillSize(RC); |
| 2892 | Align Alignment = TRI->getSpillAlign(RC); |
| 2893 | int FI = MFI.CreateSpillStackObject(Size, Alignment); |
| 2894 | RS->addScavengingFrameIndex(FI); |
| 2895 | LLVM_DEBUG(dbgs() << "No available CS registers, allocated fi#" << FI |
| 2896 | << " as the emergency spill slot.\n" ); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | // Adding the size of additional 64bit GPR saves. |
| 2901 | CSStackSize += 8 * (SavedRegs.count() - NumSavedRegs); |
| 2902 | |
| 2903 | // A Swift asynchronous context extends the frame record with a pointer |
| 2904 | // directly before FP. |
| 2905 | if (hasFP(MF) && AFI->hasSwiftAsyncContext()) |
| 2906 | CSStackSize += 8; |
| 2907 | |
| 2908 | uint64_t AlignedCSStackSize = alignTo(Value: CSStackSize, Align: 16); |
| 2909 | LLVM_DEBUG(dbgs() << "Estimated stack frame size: " |
| 2910 | << EstimatedStackSize + AlignedCSStackSize << " bytes.\n" ); |
| 2911 | |
| 2912 | assert((!MFI.isCalleeSavedInfoValid() || |
| 2913 | AFI->getCalleeSavedStackSize() == AlignedCSStackSize) && |
| 2914 | "Should not invalidate callee saved info" ); |
| 2915 | |
| 2916 | // Round up to register pair alignment to avoid additional SP adjustment |
| 2917 | // instructions. |
| 2918 | AFI->setCalleeSavedStackSize(AlignedCSStackSize); |
| 2919 | AFI->setCalleeSaveStackHasFreeSpace(AlignedCSStackSize != CSStackSize); |
| 2920 | AFI->setSVECalleeSavedStackSize(ZPR: ZPRCSStackSize, PPR: alignTo(Value: PPRCSStackSize, Align: 16)); |
| 2921 | } |
| 2922 | |
| 2923 | // Reorder ZPRs to maximize grouping, quads and pairs. The main condition we try |
| 2924 | // to satisfy is the offset and the register number. |
| 2925 | // Quads require a (scaled offset % 4 == 0) && (LowestRegister % 4 == 0), |
| 2926 | // e.g: (z4, z5, z6, z7) where z4 is at offset 8 would be legal quad |
| 2927 | // Pairs require a (scaled offset % 2 == 0) && (LowestRegister % 2 == 0). |
| 2928 | // e.g: (z4, z5) where z4 is at offset 6 would be a legal pair |
| 2929 | static void orderZPRCalleeSavesForGroups(MachineFunction &MF, |
| 2930 | const TargetRegisterInfo *RegInfo, |
| 2931 | std::vector<CalleeSavedInfo> &CSI) { |
| 2932 | assert(!isTargetWindows(MF) && |
| 2933 | "ZPR callee-save reordering not supported on Windows" ); |
| 2934 | |
| 2935 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 2936 | if (!AFI->getPredicateRegForFillSpill()) |
| 2937 | return; |
| 2938 | |
| 2939 | auto IsZPR = [](CalleeSavedInfo &CS) { |
| 2940 | return AArch64::ZPRRegClass.contains(Reg: CS.getReg()); |
| 2941 | }; |
| 2942 | |
| 2943 | MutableArrayRef<CalleeSavedInfo> ZPRSaves(CSI); |
| 2944 | ZPRSaves = ZPRSaves.drop_until(Pred: IsZPR).take_while(Pred: IsZPR); |
| 2945 | |
| 2946 | if (ZPRSaves.size() < 2) |
| 2947 | return; |
| 2948 | |
| 2949 | int ZPRByteOffset = AFI->getZPRCalleeSavedStackSize(); |
| 2950 | if (!AFI->hasSplitSVEObjects()) |
| 2951 | ZPRByteOffset += AFI->getPPRCalleeSavedStackSize(); |
| 2952 | |
| 2953 | const int Scale = RegInfo->getSpillSize(RC: AArch64::ZPRRegClass); |
| 2954 | const int LowestOffset = |
| 2955 | (ZPRByteOffset / Scale) - static_cast<int>(ZPRSaves.size()); |
| 2956 | |
| 2957 | int NumToAlign = alignTo(Value: LowestOffset, Align: 4) - LowestOffset; |
| 2958 | if (NumToAlign > int(ZPRSaves.size())) |
| 2959 | return; |
| 2960 | |
| 2961 | sort(C&: ZPRSaves, |
| 2962 | Comp: [](const auto &A, const auto &B) { return A.getReg() < B.getReg(); }); |
| 2963 | |
| 2964 | SmallVector<CalleeSavedInfo> Singles; |
| 2965 | SmallVector<std::array<CalleeSavedInfo, 2>> Pairs; |
| 2966 | SmallVector<std::array<CalleeSavedInfo, 4>> Quads; |
| 2967 | |
| 2968 | int ZPRsNeededForAlignment = NumToAlign; |
| 2969 | for (size_t i = 0; i < ZPRSaves.size();) { |
| 2970 | int ZPRsRemaining = int(ZPRSaves.size()) - i; |
| 2971 | if (i + 3 < ZPRSaves.size() && |
| 2972 | (ZPRSaves[i].getReg() - AArch64::Z0) % 4 == 0 && |
| 2973 | ZPRSaves[i].getReg() + 3 == ZPRSaves[i + 3].getReg() && |
| 2974 | ZPRsRemaining - 4 >= ZPRsNeededForAlignment) { |
| 2975 | Quads.push_back( |
| 2976 | Elt: {ZPRSaves[i], ZPRSaves[i + 1], ZPRSaves[i + 2], ZPRSaves[i + 3]}); |
| 2977 | i += 4; |
| 2978 | continue; |
| 2979 | } |
| 2980 | // We only need to split a pair if there's no singles. If we have a single |
| 2981 | // ZPR then all required alignments (1, 2, or 3) can use that single and a |
| 2982 | // ZPR pair. |
| 2983 | bool MustSplitPairForSingle = |
| 2984 | ZPRsNeededForAlignment == 1 && Singles.empty() && ZPRsRemaining == 2; |
| 2985 | |
| 2986 | if (i + 1 < ZPRSaves.size() && |
| 2987 | (ZPRSaves[i].getReg() - AArch64::Z0) % 2 == 0 && |
| 2988 | ZPRSaves[i].getReg() + 1 == ZPRSaves[i + 1].getReg() && |
| 2989 | !MustSplitPairForSingle) { |
| 2990 | Pairs.push_back(Elt: {ZPRSaves[i], ZPRSaves[i + 1]}); |
| 2991 | // Don't decrement the ZPRs needed if we require one ZPR for alignment and |
| 2992 | // we've yet to find a single ZPR. We may still need to split a pair. |
| 2993 | if (ZPRsNeededForAlignment != 1 || !Singles.empty()) |
| 2994 | ZPRsNeededForAlignment -= 2; |
| 2995 | |
| 2996 | i += 2; |
| 2997 | continue; |
| 2998 | } |
| 2999 | |
| 3000 | Singles.push_back(Elt: ZPRSaves[i++]); |
| 3001 | ZPRsNeededForAlignment--; |
| 3002 | } |
| 3003 | |
| 3004 | if (Quads.empty() && Pairs.empty()) |
| 3005 | return; |
| 3006 | |
| 3007 | int NumUnassigned = ZPRSaves.size(); |
| 3008 | auto Emit = [&](ArrayRef<CalleeSavedInfo> Saves) { |
| 3009 | // During pair/quad formation, we check for the register groups in reverse |
| 3010 | // order. This is to ensures that stack indexes remain consistent with the |
| 3011 | // instruction. |
| 3012 | for (const CalleeSavedInfo &CS : Saves) |
| 3013 | ZPRSaves[--NumUnassigned] = CS; |
| 3014 | }; |
| 3015 | |
| 3016 | // Align the offset to a multiple of 4 (to allow for the most quads/pairs). |
| 3017 | if (NumToAlign & 0b01) |
| 3018 | Emit(Singles.pop_back_val()); |
| 3019 | if (NumToAlign & 0b10) { |
| 3020 | if (!Pairs.empty()) |
| 3021 | Emit(Pairs.pop_back_val()); |
| 3022 | else |
| 3023 | Emit({Singles.pop_back_val(), Singles.pop_back_val()}); |
| 3024 | } |
| 3025 | |
| 3026 | for (const auto &Quad : Quads) |
| 3027 | Emit(Quad); |
| 3028 | |
| 3029 | for (const auto &Pair : Pairs) |
| 3030 | Emit(Pair); |
| 3031 | |
| 3032 | for (const auto &Single : Singles) |
| 3033 | Emit(Single); |
| 3034 | } |
| 3035 | |
| 3036 | bool AArch64FrameLowering::assignCalleeSavedSpillSlots( |
| 3037 | MachineFunction &MF, const TargetRegisterInfo *RegInfo, |
| 3038 | std::vector<CalleeSavedInfo> &CSI) const { |
| 3039 | bool IsWindows = isTargetWindows(MF); |
| 3040 | unsigned StackHazardSize = getStackHazardSize(MF); |
| 3041 | // To match the canonical windows frame layout, reverse the list of |
| 3042 | // callee saved registers to get them laid out by PrologEpilogInserter |
| 3043 | // in the right order. (PrologEpilogInserter allocates stack objects top |
| 3044 | // down. Windows canonical prologs store higher numbered registers at |
| 3045 | // the top, thus have the CSI array start from the highest registers.) |
| 3046 | if (IsWindows) |
| 3047 | std::reverse(first: CSI.begin(), last: CSI.end()); |
| 3048 | |
| 3049 | if (CSI.empty()) |
| 3050 | return true; // Early exit if no callee saved registers are modified! |
| 3051 | |
| 3052 | // Now that we know which registers need to be saved and restored, allocate |
| 3053 | // stack slots for them. |
| 3054 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3055 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 3056 | |
| 3057 | // Insert VG into the list of CSRs, immediately before LR if saved. |
| 3058 | if (requiresSaveVG(MF)) { |
| 3059 | CalleeSavedInfo VGInfo(AArch64::VG); |
| 3060 | auto It = |
| 3061 | find_if(Range&: CSI, P: [](auto &Info) { return Info.getReg() == AArch64::LR; }); |
| 3062 | if (It != CSI.end()) |
| 3063 | CSI.insert(position: It, x: VGInfo); |
| 3064 | else |
| 3065 | CSI.push_back(x: VGInfo); |
| 3066 | } |
| 3067 | |
| 3068 | const AArch64Subtarget &Subtarget = MF.getSubtarget<AArch64Subtarget>(); |
| 3069 | if (!IsWindows && enableMultiVectorSpillFill(Subtarget, MF)) |
| 3070 | // The Windows stack layout is not supported by this reordering function |
| 3071 | // yet. |
| 3072 | orderZPRCalleeSavesForGroups(MF, RegInfo, CSI); |
| 3073 | |
| 3074 | Register LastReg = 0; |
| 3075 | int HazardSlotIndex = std::numeric_limits<int>::max(); |
| 3076 | for (auto &CS : CSI) { |
| 3077 | MCRegister Reg = CS.getReg(); |
| 3078 | const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg); |
| 3079 | |
| 3080 | // Create a hazard slot as we switch between GPR and FPR CSRs. |
| 3081 | if (AFI->isStackHazardIncludedInCalleeSaveArea() && |
| 3082 | (!LastReg || !AArch64InstrInfo::isFpOrNEON(Reg: LastReg)) && |
| 3083 | AArch64InstrInfo::isFpOrNEON(Reg)) { |
| 3084 | assert(HazardSlotIndex == std::numeric_limits<int>::max() && |
| 3085 | "Unexpected register order for hazard slot" ); |
| 3086 | HazardSlotIndex = MFI.CreateStackObject(Size: StackHazardSize, Alignment: Align(8), isSpillSlot: true); |
| 3087 | LLVM_DEBUG(dbgs() << "Created CSR Hazard at slot " << HazardSlotIndex |
| 3088 | << "\n" ); |
| 3089 | AFI->setStackHazardCSRSlotIndex(HazardSlotIndex); |
| 3090 | MFI.setIsCalleeSavedObjectIndex(ObjectIdx: HazardSlotIndex, IsCalleeSaved: true); |
| 3091 | } |
| 3092 | |
| 3093 | unsigned Size = RegInfo->getSpillSize(RC: *RC); |
| 3094 | Align Alignment(RegInfo->getSpillAlign(RC: *RC)); |
| 3095 | int FrameIdx = MFI.CreateStackObject(Size, Alignment, isSpillSlot: true); |
| 3096 | CS.setFrameIdx(FrameIdx); |
| 3097 | MFI.setIsCalleeSavedObjectIndex(ObjectIdx: FrameIdx, IsCalleeSaved: true); |
| 3098 | |
| 3099 | // Grab 8 bytes below FP for the extended asynchronous frame info. |
| 3100 | if (hasFP(MF) && AFI->hasSwiftAsyncContext() && Reg == AArch64::FP) { |
| 3101 | FrameIdx = MFI.CreateStackObject(Size: 8, Alignment, isSpillSlot: true); |
| 3102 | AFI->setSwiftAsyncContextFrameIdx(FrameIdx); |
| 3103 | MFI.setIsCalleeSavedObjectIndex(ObjectIdx: FrameIdx, IsCalleeSaved: true); |
| 3104 | } |
| 3105 | LastReg = Reg; |
| 3106 | } |
| 3107 | |
| 3108 | // Add hazard slot in the case where no FPR CSRs are present. |
| 3109 | if (AFI->isStackHazardIncludedInCalleeSaveArea() && |
| 3110 | HazardSlotIndex == std::numeric_limits<int>::max()) { |
| 3111 | HazardSlotIndex = MFI.CreateStackObject(Size: StackHazardSize, Alignment: Align(8), isSpillSlot: true); |
| 3112 | LLVM_DEBUG(dbgs() << "Created CSR Hazard at slot " << HazardSlotIndex |
| 3113 | << "\n" ); |
| 3114 | AFI->setStackHazardCSRSlotIndex(HazardSlotIndex); |
| 3115 | MFI.setIsCalleeSavedObjectIndex(ObjectIdx: HazardSlotIndex, IsCalleeSaved: true); |
| 3116 | } |
| 3117 | |
| 3118 | return true; |
| 3119 | } |
| 3120 | |
| 3121 | bool AArch64FrameLowering::enableStackSlotScavenging( |
| 3122 | const MachineFunction &MF) const { |
| 3123 | const AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 3124 | // If the function has streaming-mode changes, don't scavenge a |
| 3125 | // spillslot in the callee-save area, as that might require an |
| 3126 | // 'addvl' in the streaming-mode-changing call-sequence when the |
| 3127 | // function doesn't use a FP. |
| 3128 | if (AFI->hasStreamingModeChanges() && !hasFP(MF)) |
| 3129 | return false; |
| 3130 | // Don't allow register salvaging with hazard slots, in case it moves objects |
| 3131 | // into the wrong place. |
| 3132 | if (AFI->hasStackHazardSlotIndex()) |
| 3133 | return false; |
| 3134 | return AFI->hasCalleeSaveStackFreeSpace(); |
| 3135 | } |
| 3136 | |
| 3137 | /// returns true if there are any SVE callee saves. |
| 3138 | static bool getSVECalleeSaveSlotRange(const MachineFrameInfo &MFI, |
| 3139 | int &Min, int &Max) { |
| 3140 | Min = std::numeric_limits<int>::max(); |
| 3141 | Max = std::numeric_limits<int>::min(); |
| 3142 | |
| 3143 | if (!MFI.isCalleeSavedInfoValid()) |
| 3144 | return false; |
| 3145 | |
| 3146 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
| 3147 | for (auto &CS : CSI) { |
| 3148 | if (AArch64::ZPRRegClass.contains(Reg: CS.getReg()) || |
| 3149 | AArch64::PPRRegClass.contains(Reg: CS.getReg())) { |
| 3150 | assert((Max == std::numeric_limits<int>::min() || |
| 3151 | Max + 1 == CS.getFrameIdx()) && |
| 3152 | "SVE CalleeSaves are not consecutive" ); |
| 3153 | Min = std::min(a: Min, b: CS.getFrameIdx()); |
| 3154 | Max = std::max(a: Max, b: CS.getFrameIdx()); |
| 3155 | } |
| 3156 | } |
| 3157 | return Min != std::numeric_limits<int>::max(); |
| 3158 | } |
| 3159 | |
| 3160 | static SVEStackSizes determineSVEStackSizes(MachineFunction &MF, |
| 3161 | AssignObjectOffsets AssignOffsets) { |
| 3162 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3163 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 3164 | |
| 3165 | SVEStackSizes SVEStack{}; |
| 3166 | |
| 3167 | // With SplitSVEObjects we maintain separate stack offsets for predicates |
| 3168 | // (PPRs) and SVE vectors (ZPRs). When SplitSVEObjects is disabled predicates |
| 3169 | // are included in the SVE vector area. |
| 3170 | uint64_t &ZPRStackTop = SVEStack.ZPRStackSize; |
| 3171 | uint64_t &PPRStackTop = |
| 3172 | AFI->hasSplitSVEObjects() ? SVEStack.PPRStackSize : SVEStack.ZPRStackSize; |
| 3173 | |
| 3174 | #ifndef NDEBUG |
| 3175 | // First process all fixed stack objects. |
| 3176 | for (int I = MFI.getObjectIndexBegin(); I != 0; ++I) |
| 3177 | assert(!MFI.hasScalableStackID(I) && |
| 3178 | "SVE vectors should never be passed on the stack by value, only by " |
| 3179 | "reference." ); |
| 3180 | #endif |
| 3181 | |
| 3182 | auto AllocateObject = [&](int FI) { |
| 3183 | uint64_t &StackTop = MFI.getStackID(ObjectIdx: FI) == TargetStackID::ScalableVector |
| 3184 | ? ZPRStackTop |
| 3185 | : PPRStackTop; |
| 3186 | |
| 3187 | // FIXME: Given that the length of SVE vectors is not necessarily a power of |
| 3188 | // two, we'd need to align every object dynamically at runtime if the |
| 3189 | // alignment is larger than 16. This is not yet supported. |
| 3190 | Align Alignment = MFI.getObjectAlign(ObjectIdx: FI); |
| 3191 | if (Alignment > Align(16)) |
| 3192 | report_fatal_error( |
| 3193 | reason: "Alignment of scalable vectors > 16 bytes is not yet supported" ); |
| 3194 | |
| 3195 | StackTop += MFI.getObjectSize(ObjectIdx: FI); |
| 3196 | StackTop = alignTo(Size: StackTop, A: Alignment); |
| 3197 | |
| 3198 | assert(StackTop < (uint64_t)std::numeric_limits<int64_t>::max() && |
| 3199 | "SVE StackTop far too large?!" ); |
| 3200 | |
| 3201 | int64_t Offset = -int64_t(StackTop); |
| 3202 | if (AssignOffsets == AssignObjectOffsets::Yes) |
| 3203 | MFI.setObjectOffset(ObjectIdx: FI, SPOffset: Offset); |
| 3204 | |
| 3205 | LLVM_DEBUG(dbgs() << "alloc FI(" << FI << ") at SP[" << Offset << "]\n" ); |
| 3206 | }; |
| 3207 | |
| 3208 | // Then process all callee saved slots. |
| 3209 | int MinCSFrameIndex, MaxCSFrameIndex; |
| 3210 | if (getSVECalleeSaveSlotRange(MFI, Min&: MinCSFrameIndex, Max&: MaxCSFrameIndex)) { |
| 3211 | for (int FI = MinCSFrameIndex; FI <= MaxCSFrameIndex; ++FI) |
| 3212 | AllocateObject(FI); |
| 3213 | } |
| 3214 | |
| 3215 | // Ensure the CS area is 16-byte aligned. |
| 3216 | PPRStackTop = alignTo(Size: PPRStackTop, A: Align(16U)); |
| 3217 | ZPRStackTop = alignTo(Size: ZPRStackTop, A: Align(16U)); |
| 3218 | |
| 3219 | // Create a buffer of SVE objects to allocate and sort it. |
| 3220 | SmallVector<int, 8> ObjectsToAllocate; |
| 3221 | // If we have a stack protector, and we've previously decided that we have SVE |
| 3222 | // objects on the stack and thus need it to go in the SVE stack area, then it |
| 3223 | // needs to go first. |
| 3224 | int StackProtectorFI = -1; |
| 3225 | if (MFI.hasStackProtectorIndex()) { |
| 3226 | StackProtectorFI = MFI.getStackProtectorIndex(); |
| 3227 | if (MFI.getStackID(ObjectIdx: StackProtectorFI) == TargetStackID::ScalableVector) |
| 3228 | ObjectsToAllocate.push_back(Elt: StackProtectorFI); |
| 3229 | } |
| 3230 | |
| 3231 | for (int FI = 0, E = MFI.getObjectIndexEnd(); FI != E; ++FI) { |
| 3232 | if (FI == StackProtectorFI || MFI.isDeadObjectIndex(ObjectIdx: FI) || |
| 3233 | MFI.isCalleeSavedObjectIndex(ObjectIdx: FI)) |
| 3234 | continue; |
| 3235 | |
| 3236 | if (MFI.getStackID(ObjectIdx: FI) != TargetStackID::ScalableVector && |
| 3237 | MFI.getStackID(ObjectIdx: FI) != TargetStackID::ScalablePredicateVector) |
| 3238 | continue; |
| 3239 | |
| 3240 | ObjectsToAllocate.push_back(Elt: FI); |
| 3241 | } |
| 3242 | |
| 3243 | // Allocate all SVE locals and spills |
| 3244 | for (unsigned FI : ObjectsToAllocate) |
| 3245 | AllocateObject(FI); |
| 3246 | |
| 3247 | PPRStackTop = alignTo(Size: PPRStackTop, A: Align(16U)); |
| 3248 | ZPRStackTop = alignTo(Size: ZPRStackTop, A: Align(16U)); |
| 3249 | |
| 3250 | if (AssignOffsets == AssignObjectOffsets::Yes) |
| 3251 | AFI->setStackSizeSVE(ZPR: SVEStack.ZPRStackSize, PPR: SVEStack.PPRStackSize); |
| 3252 | |
| 3253 | return SVEStack; |
| 3254 | } |
| 3255 | |
| 3256 | void AArch64FrameLowering::processFunctionBeforeFrameFinalized( |
| 3257 | MachineFunction &MF, RegScavenger *RS) const { |
| 3258 | assert(getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown && |
| 3259 | "Upwards growing stack unsupported" ); |
| 3260 | |
| 3261 | (void)determineSVEStackSizes(MF, AssignOffsets: AssignObjectOffsets::Yes); |
| 3262 | |
| 3263 | // If this function isn't doing Win64-style C++ EH, we don't need to do |
| 3264 | // anything. |
| 3265 | if (!MF.hasEHFunclets()) |
| 3266 | return; |
| 3267 | |
| 3268 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3269 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 3270 | |
| 3271 | // Win64 C++ EH needs to allocate space for the catch objects in the fixed |
| 3272 | // object area right next to the UnwindHelp object. |
| 3273 | WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo(); |
| 3274 | int64_t CurrentOffset = |
| 3275 | AFI->getVarArgsGPRSize() + AFI->getTailCallReservedStack(); |
| 3276 | for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 3277 | for (WinEHHandlerType &H : TBME.HandlerArray) { |
| 3278 | int FrameIndex = H.CatchObj.FrameIndex; |
| 3279 | if ((FrameIndex != INT_MAX) && MFI.getObjectOffset(ObjectIdx: FrameIndex) == 0) { |
| 3280 | CurrentOffset = |
| 3281 | alignTo(Value: CurrentOffset, Align: MFI.getObjectAlign(ObjectIdx: FrameIndex).value()); |
| 3282 | CurrentOffset += MFI.getObjectSize(ObjectIdx: FrameIndex); |
| 3283 | MFI.setObjectOffset(ObjectIdx: FrameIndex, SPOffset: -CurrentOffset); |
| 3284 | } |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | // Create an UnwindHelp object. |
| 3289 | // The UnwindHelp object is allocated at the start of the fixed object area |
| 3290 | int64_t UnwindHelpOffset = alignTo(Size: CurrentOffset + 8, A: Align(16)); |
| 3291 | assert(UnwindHelpOffset == getFixedObjectSize(MF, AFI, /*IsWin64*/ true, |
| 3292 | /*IsFunclet*/ false) && |
| 3293 | "UnwindHelpOffset must be at the start of the fixed object area" ); |
| 3294 | int UnwindHelpFI = MFI.CreateFixedObject(/*Size*/ 8, SPOffset: -UnwindHelpOffset, |
| 3295 | /*IsImmutable=*/false); |
| 3296 | EHInfo.UnwindHelpFrameIdx = UnwindHelpFI; |
| 3297 | |
| 3298 | MachineBasicBlock &MBB = MF.front(); |
| 3299 | auto MBBI = MBB.begin(); |
| 3300 | while (MBBI != MBB.end() && MBBI->getFlag(Flag: MachineInstr::FrameSetup)) |
| 3301 | ++MBBI; |
| 3302 | |
| 3303 | // We need to store -2 into the UnwindHelp object at the start of the |
| 3304 | // function. |
| 3305 | DebugLoc DL; |
| 3306 | RS->enterBasicBlockEnd(MBB); |
| 3307 | RS->backward(I: MBBI); |
| 3308 | Register DstReg = RS->FindUnusedReg(RC: &AArch64::GPR64commonRegClass); |
| 3309 | assert(DstReg && "There must be a free register after frame setup" ); |
| 3310 | const AArch64InstrInfo &TII = |
| 3311 | *MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 3312 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::MOVi64imm), DestReg: DstReg).addImm(Val: -2); |
| 3313 | BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII.get(Opcode: AArch64::STURXi)) |
| 3314 | .addReg(RegNo: DstReg, Flags: getKillRegState(B: true)) |
| 3315 | .addFrameIndex(Idx: UnwindHelpFI) |
| 3316 | .addImm(Val: 0); |
| 3317 | } |
| 3318 | |
| 3319 | namespace { |
| 3320 | struct TagStoreInstr { |
| 3321 | MachineInstr *MI; |
| 3322 | int64_t Offset, Size; |
| 3323 | explicit TagStoreInstr(MachineInstr *MI, int64_t Offset, int64_t Size) |
| 3324 | : MI(MI), Offset(Offset), Size(Size) {} |
| 3325 | }; |
| 3326 | |
| 3327 | class TagStoreEdit { |
| 3328 | MachineFunction *MF; |
| 3329 | MachineBasicBlock *MBB; |
| 3330 | MachineRegisterInfo *MRI; |
| 3331 | // Tag store instructions that are being replaced. |
| 3332 | SmallVector<TagStoreInstr, 8> TagStores; |
| 3333 | // Combined memref arguments of the above instructions. |
| 3334 | SmallVector<MachineMemOperand *, 8> CombinedMemRefs; |
| 3335 | |
| 3336 | // Replace allocation tags in [FrameReg + FrameRegOffset, FrameReg + |
| 3337 | // FrameRegOffset + Size) with the address tag of SP. |
| 3338 | Register FrameReg; |
| 3339 | StackOffset FrameRegOffset; |
| 3340 | int64_t Size; |
| 3341 | // If not std::nullopt, move FrameReg to (FrameReg + FrameRegUpdate) at the |
| 3342 | // end. |
| 3343 | std::optional<int64_t> FrameRegUpdate; |
| 3344 | // MIFlags for any FrameReg updating instructions. |
| 3345 | unsigned FrameRegUpdateFlags; |
| 3346 | |
| 3347 | // Use zeroing instruction variants. |
| 3348 | bool ZeroData; |
| 3349 | DebugLoc DL; |
| 3350 | |
| 3351 | void emitUnrolled(MachineBasicBlock::iterator InsertI); |
| 3352 | void emitLoop(MachineBasicBlock::iterator InsertI); |
| 3353 | |
| 3354 | public: |
| 3355 | TagStoreEdit(MachineBasicBlock *MBB, bool ZeroData) |
| 3356 | : MBB(MBB), ZeroData(ZeroData) { |
| 3357 | MF = MBB->getParent(); |
| 3358 | MRI = &MF->getRegInfo(); |
| 3359 | } |
| 3360 | // Add an instruction to be replaced. Instructions must be added in the |
| 3361 | // ascending order of Offset, and have to be adjacent. |
| 3362 | void addInstruction(TagStoreInstr I) { |
| 3363 | assert((TagStores.empty() || |
| 3364 | TagStores.back().Offset + TagStores.back().Size == I.Offset) && |
| 3365 | "Non-adjacent tag store instructions." ); |
| 3366 | TagStores.push_back(Elt: I); |
| 3367 | } |
| 3368 | void clear() { TagStores.clear(); } |
| 3369 | // Emit equivalent code at the given location, and erase the current set of |
| 3370 | // instructions. May skip if the replacement is not profitable. May invalidate |
| 3371 | // the input iterator and replace it with a valid one. |
| 3372 | void emitCode(MachineBasicBlock::iterator &InsertI, |
| 3373 | const AArch64FrameLowering *TFI, bool TryMergeSPUpdate); |
| 3374 | }; |
| 3375 | |
| 3376 | void TagStoreEdit::emitUnrolled(MachineBasicBlock::iterator InsertI) { |
| 3377 | const AArch64InstrInfo *TII = |
| 3378 | MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 3379 | |
| 3380 | const int64_t kMinOffset = -256 * 16; |
| 3381 | const int64_t kMaxOffset = 255 * 16; |
| 3382 | |
| 3383 | Register BaseReg = FrameReg; |
| 3384 | int64_t BaseRegOffsetBytes = FrameRegOffset.getFixed(); |
| 3385 | if (BaseRegOffsetBytes < kMinOffset || |
| 3386 | BaseRegOffsetBytes + (Size - Size % 32) > kMaxOffset || |
| 3387 | // BaseReg can be FP, which is not necessarily aligned to 16-bytes. In |
| 3388 | // that case, BaseRegOffsetBytes will not be aligned to 16 bytes, which |
| 3389 | // is required for the offset of ST2G. |
| 3390 | BaseRegOffsetBytes % 16 != 0) { |
| 3391 | Register ScratchReg = MRI->createVirtualRegister(RegClass: &AArch64::GPR64RegClass); |
| 3392 | emitFrameOffset(MBB&: *MBB, MBBI: InsertI, DL, DestReg: ScratchReg, SrcReg: BaseReg, |
| 3393 | Offset: StackOffset::getFixed(Fixed: BaseRegOffsetBytes), TII); |
| 3394 | BaseReg = ScratchReg; |
| 3395 | BaseRegOffsetBytes = 0; |
| 3396 | } |
| 3397 | |
| 3398 | MachineInstr *LastI = nullptr; |
| 3399 | while (Size) { |
| 3400 | int64_t InstrSize = (Size > 16) ? 32 : 16; |
| 3401 | unsigned Opcode = |
| 3402 | InstrSize == 16 |
| 3403 | ? (ZeroData ? AArch64::STZGi : AArch64::STGi) |
| 3404 | : (ZeroData ? AArch64::STZ2Gi : AArch64::ST2Gi); |
| 3405 | assert(BaseRegOffsetBytes % 16 == 0); |
| 3406 | MachineInstr *I = BuildMI(BB&: *MBB, I: InsertI, MIMD: DL, MCID: TII->get(Opcode)) |
| 3407 | .addReg(RegNo: AArch64::SP) |
| 3408 | .addReg(RegNo: BaseReg) |
| 3409 | .addImm(Val: BaseRegOffsetBytes / 16) |
| 3410 | .setMemRefs(CombinedMemRefs); |
| 3411 | // A store to [BaseReg, #0] should go last for an opportunity to fold the |
| 3412 | // final SP adjustment in the epilogue. |
| 3413 | if (BaseRegOffsetBytes == 0) |
| 3414 | LastI = I; |
| 3415 | BaseRegOffsetBytes += InstrSize; |
| 3416 | Size -= InstrSize; |
| 3417 | } |
| 3418 | |
| 3419 | if (LastI) |
| 3420 | MBB->splice(Where: InsertI, Other: MBB, From: LastI); |
| 3421 | } |
| 3422 | |
| 3423 | void TagStoreEdit::emitLoop(MachineBasicBlock::iterator InsertI) { |
| 3424 | const AArch64InstrInfo *TII = |
| 3425 | MF->getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 3426 | |
| 3427 | Register BaseReg = FrameRegUpdate |
| 3428 | ? FrameReg |
| 3429 | : MRI->createVirtualRegister(RegClass: &AArch64::GPR64RegClass); |
| 3430 | Register SizeReg = MRI->createVirtualRegister(RegClass: &AArch64::GPR64RegClass); |
| 3431 | |
| 3432 | emitFrameOffset(MBB&: *MBB, MBBI: InsertI, DL, DestReg: BaseReg, SrcReg: FrameReg, Offset: FrameRegOffset, TII); |
| 3433 | |
| 3434 | int64_t LoopSize = Size; |
| 3435 | // If the loop size is not a multiple of 32, split off one 16-byte store at |
| 3436 | // the end to fold BaseReg update into. |
| 3437 | if (FrameRegUpdate && *FrameRegUpdate) |
| 3438 | LoopSize -= LoopSize % 32; |
| 3439 | MachineInstr *LoopI = BuildMI(BB&: *MBB, I: InsertI, MIMD: DL, |
| 3440 | MCID: TII->get(Opcode: ZeroData ? AArch64::STZGloop_wback |
| 3441 | : AArch64::STGloop_wback)) |
| 3442 | .addDef(RegNo: SizeReg) |
| 3443 | .addDef(RegNo: BaseReg) |
| 3444 | .addImm(Val: LoopSize) |
| 3445 | .addReg(RegNo: BaseReg) |
| 3446 | .setMemRefs(CombinedMemRefs); |
| 3447 | if (FrameRegUpdate) |
| 3448 | LoopI->setFlags(FrameRegUpdateFlags); |
| 3449 | |
| 3450 | int64_t = |
| 3451 | FrameRegUpdate ? (*FrameRegUpdate - FrameRegOffset.getFixed() - Size) : 0; |
| 3452 | LLVM_DEBUG(dbgs() << "TagStoreEdit::emitLoop: LoopSize=" << LoopSize |
| 3453 | << ", Size=" << Size |
| 3454 | << ", ExtraBaseRegUpdate=" << ExtraBaseRegUpdate |
| 3455 | << ", FrameRegUpdate=" << FrameRegUpdate |
| 3456 | << ", FrameRegOffset.getFixed()=" |
| 3457 | << FrameRegOffset.getFixed() << "\n" ); |
| 3458 | if (LoopSize < Size) { |
| 3459 | assert(FrameRegUpdate); |
| 3460 | assert(Size - LoopSize == 16); |
| 3461 | // Tag 16 more bytes at BaseReg and update BaseReg. |
| 3462 | int64_t STGOffset = ExtraBaseRegUpdate + 16; |
| 3463 | assert(STGOffset % 16 == 0 && STGOffset >= -4096 && STGOffset <= 4080 && |
| 3464 | "STG immediate out of range" ); |
| 3465 | BuildMI(BB&: *MBB, I: InsertI, MIMD: DL, |
| 3466 | MCID: TII->get(Opcode: ZeroData ? AArch64::STZGPostIndex : AArch64::STGPostIndex)) |
| 3467 | .addDef(RegNo: BaseReg) |
| 3468 | .addReg(RegNo: BaseReg) |
| 3469 | .addReg(RegNo: BaseReg) |
| 3470 | .addImm(Val: STGOffset / 16) |
| 3471 | .setMemRefs(CombinedMemRefs) |
| 3472 | .setMIFlags(FrameRegUpdateFlags); |
| 3473 | } else if (ExtraBaseRegUpdate) { |
| 3474 | // Update BaseReg. |
| 3475 | int64_t AddSubOffset = std::abs(i: ExtraBaseRegUpdate); |
| 3476 | assert(AddSubOffset <= 4095 && "ADD/SUB immediate out of range" ); |
| 3477 | BuildMI( |
| 3478 | BB&: *MBB, I: InsertI, MIMD: DL, |
| 3479 | MCID: TII->get(Opcode: ExtraBaseRegUpdate > 0 ? AArch64::ADDXri : AArch64::SUBXri)) |
| 3480 | .addDef(RegNo: BaseReg) |
| 3481 | .addReg(RegNo: BaseReg) |
| 3482 | .addImm(Val: AddSubOffset) |
| 3483 | .addImm(Val: 0) |
| 3484 | .setMIFlags(FrameRegUpdateFlags); |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | // Check if *II is a register update that can be merged into STGloop that ends |
| 3489 | // at (Reg + Size). RemainingOffset is the required adjustment to Reg after the |
| 3490 | // end of the loop. |
| 3491 | bool canMergeRegUpdate(MachineBasicBlock::iterator II, unsigned Reg, |
| 3492 | int64_t Size, int64_t *TotalOffset) { |
| 3493 | MachineInstr &MI = *II; |
| 3494 | if ((MI.getOpcode() == AArch64::ADDXri || |
| 3495 | MI.getOpcode() == AArch64::SUBXri) && |
| 3496 | MI.getOperand(i: 0).getReg() == Reg && MI.getOperand(i: 1).getReg() == Reg) { |
| 3497 | unsigned Shift = AArch64_AM::getShiftValue(Imm: MI.getOperand(i: 3).getImm()); |
| 3498 | int64_t Offset = MI.getOperand(i: 2).getImm() << Shift; |
| 3499 | if (MI.getOpcode() == AArch64::SUBXri) |
| 3500 | Offset = -Offset; |
| 3501 | int64_t PostOffset = Offset - Size; |
| 3502 | // TagStoreEdit::emitLoop might emit either an ADD/SUB after the loop, or |
| 3503 | // an STGPostIndex which does the last 16 bytes of tag write. Which one is |
| 3504 | // chosen depends on the alignment of the loop size, but the difference |
| 3505 | // between the valid ranges for the two instructions is small, so we |
| 3506 | // conservatively assume that it could be either case here. |
| 3507 | // |
| 3508 | // Max offset of STGPostIndex, minus the 16 byte tag write folded into that |
| 3509 | // instruction. |
| 3510 | const int64_t kMaxOffset = 4080 - 16; |
| 3511 | // Max offset of SUBXri. |
| 3512 | const int64_t kMinOffset = -4095; |
| 3513 | if (PostOffset <= kMaxOffset && PostOffset >= kMinOffset && |
| 3514 | PostOffset % 16 == 0) { |
| 3515 | *TotalOffset = Offset; |
| 3516 | return true; |
| 3517 | } |
| 3518 | } |
| 3519 | return false; |
| 3520 | } |
| 3521 | |
| 3522 | void mergeMemRefs(const SmallVectorImpl<TagStoreInstr> &TSE, |
| 3523 | SmallVectorImpl<MachineMemOperand *> &MemRefs) { |
| 3524 | MemRefs.clear(); |
| 3525 | for (auto &TS : TSE) { |
| 3526 | MachineInstr *MI = TS.MI; |
| 3527 | // An instruction without memory operands may access anything. Be |
| 3528 | // conservative and return an empty list. |
| 3529 | if (MI->memoperands_empty()) { |
| 3530 | MemRefs.clear(); |
| 3531 | return; |
| 3532 | } |
| 3533 | MemRefs.append(in_start: MI->memoperands_begin(), in_end: MI->memoperands_end()); |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | void TagStoreEdit::emitCode(MachineBasicBlock::iterator &InsertI, |
| 3538 | const AArch64FrameLowering *TFI, |
| 3539 | bool TryMergeSPUpdate) { |
| 3540 | if (TagStores.empty()) |
| 3541 | return; |
| 3542 | TagStoreInstr &FirstTagStore = TagStores[0]; |
| 3543 | TagStoreInstr &LastTagStore = TagStores[TagStores.size() - 1]; |
| 3544 | Size = LastTagStore.Offset - FirstTagStore.Offset + LastTagStore.Size; |
| 3545 | DL = TagStores[0].MI->getDebugLoc(); |
| 3546 | |
| 3547 | Register Reg; |
| 3548 | FrameRegOffset = TFI->resolveFrameOffsetReference( |
| 3549 | MF: *MF, ObjectOffset: FirstTagStore.Offset, isFixed: false /*isFixed*/, |
| 3550 | StackID: TargetStackID::Default /*StackID*/, FrameReg&: Reg, |
| 3551 | /*PreferFP=*/false, /*ForSimm=*/true); |
| 3552 | FrameReg = Reg; |
| 3553 | FrameRegUpdate = std::nullopt; |
| 3554 | |
| 3555 | mergeMemRefs(TSE: TagStores, MemRefs&: CombinedMemRefs); |
| 3556 | |
| 3557 | LLVM_DEBUG({ |
| 3558 | dbgs() << "Replacing adjacent STG instructions:\n" ; |
| 3559 | for (const auto &Instr : TagStores) { |
| 3560 | dbgs() << " " << *Instr.MI; |
| 3561 | } |
| 3562 | }); |
| 3563 | |
| 3564 | // Size threshold where a loop becomes shorter than a linear sequence of |
| 3565 | // tagging instructions. |
| 3566 | const int kSetTagLoopThreshold = 176; |
| 3567 | if (Size < kSetTagLoopThreshold) { |
| 3568 | if (TagStores.size() < 2) |
| 3569 | return; |
| 3570 | emitUnrolled(InsertI); |
| 3571 | } else { |
| 3572 | MachineInstr *UpdateInstr = nullptr; |
| 3573 | int64_t TotalOffset = 0; |
| 3574 | if (TryMergeSPUpdate) { |
| 3575 | // See if we can merge base register update into the STGloop. |
| 3576 | // This is done in AArch64LoadStoreOptimizer for "normal" stores, |
| 3577 | // but STGloop is way too unusual for that, and also it only |
| 3578 | // realistically happens in function epilogue. Also, STGloop is expanded |
| 3579 | // before that pass. |
| 3580 | auto UpdateI = skipDebugInstructionsForward(It: InsertI, End: MBB->end(), |
| 3581 | /*SkipPseudoOp=*/false); |
| 3582 | if (UpdateI != MBB->end() && |
| 3583 | canMergeRegUpdate(II: UpdateI, Reg: FrameReg, Size: FrameRegOffset.getFixed() + Size, |
| 3584 | TotalOffset: &TotalOffset)) { |
| 3585 | // Keep skipped debug instructions before the loop so debug values see |
| 3586 | // the pre-update SP; only the tag stores move past them. |
| 3587 | UpdateInstr = &*UpdateI++; |
| 3588 | InsertI = UpdateI; |
| 3589 | LLVM_DEBUG(dbgs() << "Folding SP update into loop:\n " |
| 3590 | << *UpdateInstr); |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | if (!UpdateInstr && TagStores.size() < 2) |
| 3595 | return; |
| 3596 | |
| 3597 | if (UpdateInstr) { |
| 3598 | FrameRegUpdate = TotalOffset; |
| 3599 | FrameRegUpdateFlags = UpdateInstr->getFlags(); |
| 3600 | } |
| 3601 | emitLoop(InsertI); |
| 3602 | if (UpdateInstr) |
| 3603 | UpdateInstr->eraseFromParent(); |
| 3604 | } |
| 3605 | |
| 3606 | for (auto &TS : TagStores) |
| 3607 | TS.MI->eraseFromParent(); |
| 3608 | } |
| 3609 | |
| 3610 | bool isMergeableStackTaggingInstruction(MachineInstr &MI, int64_t &Offset, |
| 3611 | int64_t &Size, bool &ZeroData) { |
| 3612 | MachineFunction &MF = *MI.getParent()->getParent(); |
| 3613 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3614 | |
| 3615 | unsigned Opcode = MI.getOpcode(); |
| 3616 | ZeroData = (Opcode == AArch64::STZGloop || Opcode == AArch64::STZGi || |
| 3617 | Opcode == AArch64::STZ2Gi); |
| 3618 | |
| 3619 | if (Opcode == AArch64::STGloop || Opcode == AArch64::STZGloop) { |
| 3620 | if (!MI.getOperand(i: 0).isDead() || !MI.getOperand(i: 1).isDead()) |
| 3621 | return false; |
| 3622 | if (!MI.getOperand(i: 2).isImm() || !MI.getOperand(i: 3).isFI()) |
| 3623 | return false; |
| 3624 | Offset = MFI.getObjectOffset(ObjectIdx: MI.getOperand(i: 3).getIndex()); |
| 3625 | Size = MI.getOperand(i: 2).getImm(); |
| 3626 | return true; |
| 3627 | } |
| 3628 | |
| 3629 | if (Opcode == AArch64::STGi || Opcode == AArch64::STZGi) |
| 3630 | Size = 16; |
| 3631 | else if (Opcode == AArch64::ST2Gi || Opcode == AArch64::STZ2Gi) |
| 3632 | Size = 32; |
| 3633 | else |
| 3634 | return false; |
| 3635 | |
| 3636 | if (MI.getOperand(i: 0).getReg() != AArch64::SP || !MI.getOperand(i: 1).isFI()) |
| 3637 | return false; |
| 3638 | |
| 3639 | Offset = MFI.getObjectOffset(ObjectIdx: MI.getOperand(i: 1).getIndex()) + |
| 3640 | 16 * MI.getOperand(i: 2).getImm(); |
| 3641 | return true; |
| 3642 | } |
| 3643 | |
| 3644 | static size_t countAvailableScavengerSlots(LivePhysRegs &LiveRegs, |
| 3645 | MachineRegisterInfo &MRI, |
| 3646 | RegScavenger *RS) { |
| 3647 | auto FreeGPRs = |
| 3648 | llvm::count_if(Range: AArch64::GPR64RegClass, P: [&LiveRegs, &MRI](auto Reg) { |
| 3649 | return LiveRegs.available(MRI, Reg); |
| 3650 | }); |
| 3651 | |
| 3652 | size_t NumEmergencySlots = 0; |
| 3653 | if (RS) |
| 3654 | NumEmergencySlots = RS->getNumScavengingFrameIndices(); |
| 3655 | |
| 3656 | return FreeGPRs + NumEmergencySlots; |
| 3657 | } |
| 3658 | |
| 3659 | // Detect a run of memory tagging instructions for adjacent stack frame slots, |
| 3660 | // and replace them with a shorter instruction sequence: |
| 3661 | // * replace STG + STG with ST2G |
| 3662 | // * replace STGloop + STGloop with STGloop |
| 3663 | // This code needs to run when stack slot offsets are already known, but before |
| 3664 | // FrameIndex operands in STG instructions are eliminated. |
| 3665 | MachineBasicBlock::iterator tryMergeAdjacentSTG(MachineBasicBlock::iterator II, |
| 3666 | const AArch64FrameLowering *TFI, |
| 3667 | RegScavenger *RS) { |
| 3668 | bool FirstZeroData; |
| 3669 | int64_t Size, Offset; |
| 3670 | MachineInstr &MI = *II; |
| 3671 | MachineBasicBlock *MBB = MI.getParent(); |
| 3672 | MachineBasicBlock::iterator NextI = ++II; |
| 3673 | if (&MI == &MBB->instr_back()) |
| 3674 | return II; |
| 3675 | if (!isMergeableStackTaggingInstruction(MI, Offset, Size, ZeroData&: FirstZeroData)) |
| 3676 | return II; |
| 3677 | |
| 3678 | SmallVector<TagStoreInstr, 4> Instrs; |
| 3679 | Instrs.emplace_back(Args: &MI, Args&: Offset, Args&: Size); |
| 3680 | |
| 3681 | constexpr int kScanLimit = 10; |
| 3682 | int Count = 0; |
| 3683 | for (MachineBasicBlock::iterator E = MBB->end(); |
| 3684 | NextI != E && Count < kScanLimit; ++NextI) { |
| 3685 | MachineInstr &MI = *NextI; |
| 3686 | bool ZeroData; |
| 3687 | int64_t Size, Offset; |
| 3688 | // Collect instructions that update memory tags with a FrameIndex operand |
| 3689 | // and (when applicable) constant size, and whose output registers are dead |
| 3690 | // (the latter is almost always the case in practice). Since these |
| 3691 | // instructions effectively have no inputs or outputs, we are free to skip |
| 3692 | // any non-aliasing instructions in between without tracking used registers. |
| 3693 | if (isMergeableStackTaggingInstruction(MI, Offset, Size, ZeroData)) { |
| 3694 | if (ZeroData != FirstZeroData) |
| 3695 | break; |
| 3696 | Instrs.emplace_back(Args: &MI, Args&: Offset, Args&: Size); |
| 3697 | continue; |
| 3698 | } |
| 3699 | |
| 3700 | // Only count non-transient, non-tagging instructions toward the scan |
| 3701 | // limit. |
| 3702 | if (!MI.isTransient()) |
| 3703 | ++Count; |
| 3704 | |
| 3705 | // Just in case, stop before the epilogue code starts. |
| 3706 | if (MI.getFlag(Flag: MachineInstr::FrameSetup) || |
| 3707 | MI.getFlag(Flag: MachineInstr::FrameDestroy)) |
| 3708 | break; |
| 3709 | |
| 3710 | // Reject anything that may alias the collected instructions. |
| 3711 | if (MI.mayLoadOrStore() || MI.hasUnmodeledSideEffects() || MI.isCall()) |
| 3712 | break; |
| 3713 | } |
| 3714 | |
| 3715 | // New code will be inserted after the last tagging instruction we've found. |
| 3716 | MachineBasicBlock::iterator InsertI = Instrs.back().MI; |
| 3717 | |
| 3718 | // All the gathered stack tag instructions are merged and placed after |
| 3719 | // last tag store in the list. The check should be made if the nzcv |
| 3720 | // flag is live at the point where we are trying to insert. Otherwise |
| 3721 | // the nzcv flag might get clobbered if any stg loops are present. |
| 3722 | |
| 3723 | // FIXME : This approach of bailing out from merge is conservative in |
| 3724 | // some ways like even if stg loops are not present after merge the |
| 3725 | // insert list, this liveness check is done (which is not needed). |
| 3726 | LivePhysRegs LiveRegs(*(MBB->getParent()->getSubtarget().getRegisterInfo())); |
| 3727 | LiveRegs.addLiveOuts(MBB: *MBB); |
| 3728 | for (auto I = MBB->rbegin();; ++I) { |
| 3729 | MachineInstr &MI = *I; |
| 3730 | if (MI == InsertI) |
| 3731 | break; |
| 3732 | LiveRegs.stepBackward(MI: *I); |
| 3733 | } |
| 3734 | InsertI++; |
| 3735 | if (LiveRegs.contains(Reg: AArch64::NZCV)) |
| 3736 | return InsertI; |
| 3737 | |
| 3738 | // Emitting an MTE loop requires two physical registers (BaseReg and |
| 3739 | // SizeReg). If the function is under register pressure, the register |
| 3740 | // scavenger will crash trying to allocate them. If we don't have at least |
| 3741 | // two free slots (free registers + emergency slots), bail out and fall back |
| 3742 | // to the unrolled sequence. |
| 3743 | if (countAvailableScavengerSlots(LiveRegs, MRI&: MBB->getParent()->getRegInfo(), |
| 3744 | RS) < 2) { |
| 3745 | LLVM_DEBUG( |
| 3746 | dbgs() << "Failed to merge MTE stack tagging instructions into loop " |
| 3747 | << "due to high register pressure.\n" ); |
| 3748 | return InsertI; |
| 3749 | } |
| 3750 | |
| 3751 | llvm::stable_sort(Range&: Instrs, |
| 3752 | C: [](const TagStoreInstr &Left, const TagStoreInstr &Right) { |
| 3753 | return Left.Offset < Right.Offset; |
| 3754 | }); |
| 3755 | |
| 3756 | // Make sure that we don't have any overlapping stores. |
| 3757 | int64_t CurOffset = Instrs[0].Offset; |
| 3758 | for (auto &Instr : Instrs) { |
| 3759 | if (CurOffset > Instr.Offset) |
| 3760 | return NextI; |
| 3761 | CurOffset = Instr.Offset + Instr.Size; |
| 3762 | } |
| 3763 | |
| 3764 | // Find contiguous runs of tagged memory and emit shorter instruction |
| 3765 | // sequences for them when possible. |
| 3766 | TagStoreEdit TSE(MBB, FirstZeroData); |
| 3767 | std::optional<int64_t> EndOffset; |
| 3768 | for (auto &Instr : Instrs) { |
| 3769 | if (EndOffset && *EndOffset != Instr.Offset) { |
| 3770 | // Found a gap. |
| 3771 | TSE.emitCode(InsertI, TFI, /*TryMergeSPUpdate = */ false); |
| 3772 | TSE.clear(); |
| 3773 | } |
| 3774 | |
| 3775 | TSE.addInstruction(I: Instr); |
| 3776 | EndOffset = Instr.Offset + Instr.Size; |
| 3777 | } |
| 3778 | |
| 3779 | const MachineFunction *MF = MBB->getParent(); |
| 3780 | // Multiple FP/SP updates in a loop cannot be described by CFI instructions. |
| 3781 | TSE.emitCode( |
| 3782 | InsertI, TFI, /*TryMergeSPUpdate = */ |
| 3783 | !MF->getInfo<AArch64FunctionInfo>()->needsAsyncDwarfUnwindInfo(MF: *MF)); |
| 3784 | |
| 3785 | return InsertI; |
| 3786 | } |
| 3787 | } // namespace |
| 3788 | |
| 3789 | void AArch64FrameLowering::processFunctionBeforeFrameIndicesReplaced( |
| 3790 | MachineFunction &MF, RegScavenger *RS = nullptr) const { |
| 3791 | bool MergeSetTag = MF.getSubtarget<AArch64Subtarget>() |
| 3792 | .getCLOpts() |
| 3793 | .stack_tagging_merge_settag; |
| 3794 | for (auto &BB : MF) |
| 3795 | for (MachineBasicBlock::iterator II = BB.begin(); II != BB.end();) { |
| 3796 | if (MergeSetTag) |
| 3797 | II = tryMergeAdjacentSTG(II, TFI: this, RS); |
| 3798 | } |
| 3799 | |
| 3800 | // By the time this method is called, most of the prologue/epilogue code is |
| 3801 | // already emitted, whether its location was affected by the shrink-wrapping |
| 3802 | // optimization or not. |
| 3803 | if (!MF.getFunction().hasFnAttribute(Kind: Attribute::Naked) && |
| 3804 | shouldSignReturnAddressEverywhere(MF)) |
| 3805 | emitPacRetPlusLeafHardening(MF); |
| 3806 | } |
| 3807 | |
| 3808 | /// For Win64 AArch64 EH, the offset to the Unwind object is from the SP |
| 3809 | /// before the update. This is easily retrieved as it is exactly the offset |
| 3810 | /// that is set in processFunctionBeforeFrameFinalized. |
| 3811 | StackOffset AArch64FrameLowering::getFrameIndexReferencePreferSP( |
| 3812 | const MachineFunction &MF, int FI, Register &FrameReg, |
| 3813 | bool IgnoreSPUpdates) const { |
| 3814 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3815 | if (IgnoreSPUpdates) { |
| 3816 | LLVM_DEBUG(dbgs() << "Offset from the SP for " << FI << " is " |
| 3817 | << MFI.getObjectOffset(FI) << "\n" ); |
| 3818 | FrameReg = AArch64::SP; |
| 3819 | return StackOffset::getFixed(Fixed: MFI.getObjectOffset(ObjectIdx: FI)); |
| 3820 | } |
| 3821 | |
| 3822 | // Go to common code if we cannot provide sp + offset. |
| 3823 | if (MFI.hasVarSizedObjects() || |
| 3824 | MF.getInfo<AArch64FunctionInfo>()->hasSVEStackSize() || |
| 3825 | MF.getSubtarget().getRegisterInfo()->hasStackRealignment(MF)) |
| 3826 | return getFrameIndexReference(MF, FI, FrameReg); |
| 3827 | |
| 3828 | FrameReg = AArch64::SP; |
| 3829 | return getStackOffset(MF, ObjectOffset: MFI.getObjectOffset(ObjectIdx: FI)); |
| 3830 | } |
| 3831 | |
| 3832 | /// The parent frame offset (aka dispFrame) is only used on X86_64 to retrieve |
| 3833 | /// the parent's frame pointer |
| 3834 | unsigned AArch64FrameLowering::getWinEHParentFrameOffset( |
| 3835 | const MachineFunction &MF) const { |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
| 3839 | /// Funclets only need to account for space for the callee saved registers, |
| 3840 | /// as the locals are accounted for in the parent's stack frame. |
| 3841 | unsigned AArch64FrameLowering::getWinEHFuncletFrameSize( |
| 3842 | const MachineFunction &MF) const { |
| 3843 | // This is the size of the pushed CSRs. |
| 3844 | unsigned CSSize = |
| 3845 | MF.getInfo<AArch64FunctionInfo>()->getCalleeSavedStackSize(); |
| 3846 | // This is the amount of stack a funclet needs to allocate. |
| 3847 | return alignTo(Size: CSSize + MF.getFrameInfo().getMaxCallFrameSize(), |
| 3848 | A: getStackAlign()); |
| 3849 | } |
| 3850 | |
| 3851 | namespace { |
| 3852 | struct FrameObject { |
| 3853 | bool IsValid = false; |
| 3854 | // Index of the object in MFI. |
| 3855 | int ObjectIndex = 0; |
| 3856 | // Group ID this object belongs to. |
| 3857 | int GroupIndex = -1; |
| 3858 | // This object should be placed first (closest to SP). |
| 3859 | bool ObjectFirst = false; |
| 3860 | // This object's group (which always contains the object with |
| 3861 | // ObjectFirst==true) should be placed first. |
| 3862 | bool GroupFirst = false; |
| 3863 | |
| 3864 | // Used to distinguish between FP and GPR accesses. The values are decided so |
| 3865 | // that they sort FPR < Hazard < GPR and they can be or'd together. |
| 3866 | unsigned Accesses = 0; |
| 3867 | enum { AccessFPR = 1, AccessHazard = 2, AccessGPR = 4 }; |
| 3868 | }; |
| 3869 | |
| 3870 | class GroupBuilder { |
| 3871 | SmallVector<int, 8> CurrentMembers; |
| 3872 | int NextGroupIndex = 0; |
| 3873 | std::vector<FrameObject> &Objects; |
| 3874 | |
| 3875 | public: |
| 3876 | GroupBuilder(std::vector<FrameObject> &Objects) : Objects(Objects) {} |
| 3877 | void AddMember(int Index) { CurrentMembers.push_back(Elt: Index); } |
| 3878 | void EndCurrentGroup() { |
| 3879 | if (CurrentMembers.size() > 1) { |
| 3880 | // Create a new group with the current member list. This might remove them |
| 3881 | // from their pre-existing groups. That's OK, dealing with overlapping |
| 3882 | // groups is too hard and unlikely to make a difference. |
| 3883 | LLVM_DEBUG(dbgs() << "group:" ); |
| 3884 | for (int Index : CurrentMembers) { |
| 3885 | Objects[Index].GroupIndex = NextGroupIndex; |
| 3886 | LLVM_DEBUG(dbgs() << " " << Index); |
| 3887 | } |
| 3888 | LLVM_DEBUG(dbgs() << "\n" ); |
| 3889 | NextGroupIndex++; |
| 3890 | } |
| 3891 | CurrentMembers.clear(); |
| 3892 | } |
| 3893 | }; |
| 3894 | |
| 3895 | bool FrameObjectCompare(const FrameObject &A, const FrameObject &B) { |
| 3896 | // Objects at a lower index are closer to FP; objects at a higher index are |
| 3897 | // closer to SP. |
| 3898 | // |
| 3899 | // For consistency in our comparison, all invalid objects are placed |
| 3900 | // at the end. This also allows us to stop walking when we hit the |
| 3901 | // first invalid item after it's all sorted. |
| 3902 | // |
| 3903 | // If we want to include a stack hazard region, order FPR accesses < the |
| 3904 | // hazard object < GPRs accesses in order to create a separation between the |
| 3905 | // two. For the Accesses field 1 = FPR, 2 = Hazard Object, 4 = GPR. |
| 3906 | // |
| 3907 | // Otherwise the "first" object goes first (closest to SP), followed by the |
| 3908 | // members of the "first" group. |
| 3909 | // |
| 3910 | // The rest are sorted by the group index to keep the groups together. |
| 3911 | // Higher numbered groups are more likely to be around longer (i.e. untagged |
| 3912 | // in the function epilogue and not at some earlier point). Place them closer |
| 3913 | // to SP. |
| 3914 | // |
| 3915 | // If all else equal, sort by the object index to keep the objects in the |
| 3916 | // original order. |
| 3917 | return std::make_tuple(args: !A.IsValid, args: A.Accesses, args: A.ObjectFirst, args: A.GroupFirst, |
| 3918 | args: A.GroupIndex, args: A.ObjectIndex) < |
| 3919 | std::make_tuple(args: !B.IsValid, args: B.Accesses, args: B.ObjectFirst, args: B.GroupFirst, |
| 3920 | args: B.GroupIndex, args: B.ObjectIndex); |
| 3921 | } |
| 3922 | } // namespace |
| 3923 | |
| 3924 | void AArch64FrameLowering::orderFrameObjects( |
| 3925 | const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const { |
| 3926 | const AArch64FunctionInfo &AFI = *MF.getInfo<AArch64FunctionInfo>(); |
| 3927 | |
| 3928 | if ((!MF.getSubtarget<AArch64Subtarget>().getCLOpts().order_frame_objects && |
| 3929 | !AFI.hasSplitSVEObjects()) || |
| 3930 | ObjectsToAllocate.empty()) |
| 3931 | return; |
| 3932 | |
| 3933 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 3934 | std::vector<FrameObject> FrameObjects(MFI.getObjectIndexEnd()); |
| 3935 | for (auto &Obj : ObjectsToAllocate) { |
| 3936 | FrameObjects[Obj].IsValid = true; |
| 3937 | FrameObjects[Obj].ObjectIndex = Obj; |
| 3938 | } |
| 3939 | |
| 3940 | // Identify FPR vs GPR slots for hazards, and stack slots that are tagged at |
| 3941 | // the same time. |
| 3942 | GroupBuilder GB(FrameObjects); |
| 3943 | for (auto &MBB : MF) { |
| 3944 | for (auto &MI : MBB) { |
| 3945 | if (MI.isDebugInstr()) |
| 3946 | continue; |
| 3947 | |
| 3948 | if (AFI.hasStackHazardSlotIndex()) { |
| 3949 | std::optional<int> FI = getLdStFrameID(MI, MFI); |
| 3950 | if (FI && *FI >= 0 && *FI < (int)FrameObjects.size()) { |
| 3951 | if (MFI.getStackID(ObjectIdx: *FI) == TargetStackID::ScalableVector || |
| 3952 | AArch64InstrInfo::isFpOrNEON(MI)) |
| 3953 | FrameObjects[*FI].Accesses |= FrameObject::AccessFPR; |
| 3954 | else |
| 3955 | FrameObjects[*FI].Accesses |= FrameObject::AccessGPR; |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | int OpIndex; |
| 3960 | switch (MI.getOpcode()) { |
| 3961 | case AArch64::STGloop: |
| 3962 | case AArch64::STZGloop: |
| 3963 | OpIndex = 3; |
| 3964 | break; |
| 3965 | case AArch64::STGi: |
| 3966 | case AArch64::STZGi: |
| 3967 | case AArch64::ST2Gi: |
| 3968 | case AArch64::STZ2Gi: |
| 3969 | OpIndex = 1; |
| 3970 | break; |
| 3971 | default: |
| 3972 | OpIndex = -1; |
| 3973 | } |
| 3974 | |
| 3975 | int TaggedFI = -1; |
| 3976 | if (OpIndex >= 0) { |
| 3977 | const MachineOperand &MO = MI.getOperand(i: OpIndex); |
| 3978 | if (MO.isFI()) { |
| 3979 | int FI = MO.getIndex(); |
| 3980 | if (FI >= 0 && FI < MFI.getObjectIndexEnd() && |
| 3981 | FrameObjects[FI].IsValid) |
| 3982 | TaggedFI = FI; |
| 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | // If this is a stack tagging instruction for a slot that is not part of a |
| 3987 | // group yet, either start a new group or add it to the current one. |
| 3988 | if (TaggedFI >= 0) |
| 3989 | GB.AddMember(Index: TaggedFI); |
| 3990 | else |
| 3991 | GB.EndCurrentGroup(); |
| 3992 | } |
| 3993 | // Groups should never span multiple basic blocks. |
| 3994 | GB.EndCurrentGroup(); |
| 3995 | } |
| 3996 | |
| 3997 | if (AFI.hasStackHazardSlotIndex()) { |
| 3998 | FrameObjects[AFI.getStackHazardSlotIndex()].Accesses = |
| 3999 | FrameObject::AccessHazard; |
| 4000 | // If a stack object is unknown or both GPR and FPR, sort it into GPR. |
| 4001 | for (auto &Obj : FrameObjects) |
| 4002 | if (!Obj.Accesses || |
| 4003 | Obj.Accesses == (FrameObject::AccessGPR | FrameObject::AccessFPR)) |
| 4004 | Obj.Accesses = FrameObject::AccessGPR; |
| 4005 | } |
| 4006 | |
| 4007 | // If the function's tagged base pointer is pinned to a stack slot, we want to |
| 4008 | // put that slot first when possible. This will likely place it at SP + 0, |
| 4009 | // and save one instruction when generating the base pointer because IRG does |
| 4010 | // not allow an immediate offset. |
| 4011 | std::optional<int> TBPI = AFI.getTaggedBasePointerIndex(); |
| 4012 | if (TBPI) { |
| 4013 | FrameObjects[*TBPI].ObjectFirst = true; |
| 4014 | FrameObjects[*TBPI].GroupFirst = true; |
| 4015 | int FirstGroupIndex = FrameObjects[*TBPI].GroupIndex; |
| 4016 | if (FirstGroupIndex >= 0) |
| 4017 | for (FrameObject &Object : FrameObjects) |
| 4018 | if (Object.GroupIndex == FirstGroupIndex) |
| 4019 | Object.GroupFirst = true; |
| 4020 | } |
| 4021 | |
| 4022 | llvm::stable_sort(Range&: FrameObjects, C: FrameObjectCompare); |
| 4023 | |
| 4024 | int i = 0; |
| 4025 | for (auto &Obj : FrameObjects) { |
| 4026 | // All invalid items are sorted at the end, so it's safe to stop. |
| 4027 | if (!Obj.IsValid) |
| 4028 | break; |
| 4029 | ObjectsToAllocate[i++] = Obj.ObjectIndex; |
| 4030 | } |
| 4031 | |
| 4032 | LLVM_DEBUG({ |
| 4033 | dbgs() << "Final frame order:\n" ; |
| 4034 | for (auto &Obj : FrameObjects) { |
| 4035 | if (!Obj.IsValid) |
| 4036 | break; |
| 4037 | dbgs() << " " << Obj.ObjectIndex << ": group " << Obj.GroupIndex; |
| 4038 | if (Obj.ObjectFirst) |
| 4039 | dbgs() << ", first" ; |
| 4040 | if (Obj.GroupFirst) |
| 4041 | dbgs() << ", group-first" ; |
| 4042 | dbgs() << "\n" ; |
| 4043 | } |
| 4044 | }); |
| 4045 | } |
| 4046 | |
| 4047 | /// Emit a loop to decrement SP until it is equal to TargetReg, with probes at |
| 4048 | /// least every ProbeSize bytes. Returns an iterator of the first instruction |
| 4049 | /// after the loop. The difference between SP and TargetReg must be an exact |
| 4050 | /// multiple of ProbeSize. |
| 4051 | MachineBasicBlock::iterator |
| 4052 | AArch64FrameLowering::inlineStackProbeLoopExactMultiple( |
| 4053 | MachineBasicBlock::iterator MBBI, int64_t ProbeSize, |
| 4054 | Register TargetReg) const { |
| 4055 | MachineBasicBlock &MBB = *MBBI->getParent(); |
| 4056 | MachineFunction &MF = *MBB.getParent(); |
| 4057 | const AArch64InstrInfo *TII = |
| 4058 | MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 4059 | DebugLoc DL = MBB.findDebugLoc(MBBI); |
| 4060 | |
| 4061 | MachineFunction::iterator MBBInsertPoint = std::next(x: MBB.getIterator()); |
| 4062 | MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(BB: MBB.getBasicBlock()); |
| 4063 | MF.insert(MBBI: MBBInsertPoint, MBB: LoopMBB); |
| 4064 | MachineBasicBlock *ExitMBB = MF.CreateMachineBasicBlock(BB: MBB.getBasicBlock()); |
| 4065 | MF.insert(MBBI: MBBInsertPoint, MBB: ExitMBB); |
| 4066 | |
| 4067 | // SUB SP, SP, #ProbeSize (or equivalent if ProbeSize is not encodable |
| 4068 | // in SUB). |
| 4069 | emitFrameOffset(MBB&: *LoopMBB, MBBI: LoopMBB->end(), DL, DestReg: AArch64::SP, SrcReg: AArch64::SP, |
| 4070 | Offset: StackOffset::getFixed(Fixed: -ProbeSize), TII, |
| 4071 | MachineInstr::FrameSetup); |
| 4072 | // LDR XZR, [SP] |
| 4073 | BuildMI(BB&: *LoopMBB, I: LoopMBB->end(), MIMD: DL, MCID: TII->get(Opcode: AArch64::LDRXui)) |
| 4074 | .addDef(RegNo: AArch64::XZR) |
| 4075 | .addReg(RegNo: AArch64::SP) |
| 4076 | .addImm(Val: 0) |
| 4077 | .addMemOperand(MMO: MF.getMachineMemOperand( |
| 4078 | PtrInfo: MachinePointerInfo::getUnknownStack(MF), |
| 4079 | F: MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, Size: 8, |
| 4080 | BaseAlignment: Align(8))) |
| 4081 | .setMIFlags(MachineInstr::FrameSetup); |
| 4082 | // CMP SP, TargetReg |
| 4083 | BuildMI(BB&: *LoopMBB, I: LoopMBB->end(), MIMD: DL, MCID: TII->get(Opcode: AArch64::SUBSXrx64), |
| 4084 | DestReg: AArch64::XZR) |
| 4085 | .addReg(RegNo: AArch64::SP) |
| 4086 | .addReg(RegNo: TargetReg) |
| 4087 | .addImm(Val: AArch64_AM::getArithExtendImm(ET: AArch64_AM::UXTX, Imm: 0)) |
| 4088 | .setMIFlags(MachineInstr::FrameSetup); |
| 4089 | // B.CC Loop |
| 4090 | BuildMI(BB&: *LoopMBB, I: LoopMBB->end(), MIMD: DL, MCID: TII->get(Opcode: AArch64::Bcc)) |
| 4091 | .addImm(Val: AArch64CC::NE) |
| 4092 | .addMBB(MBB: LoopMBB) |
| 4093 | .setMIFlags(MachineInstr::FrameSetup); |
| 4094 | |
| 4095 | LoopMBB->addSuccessor(Succ: ExitMBB); |
| 4096 | LoopMBB->addSuccessor(Succ: LoopMBB); |
| 4097 | // Synthesize the exit MBB. |
| 4098 | ExitMBB->splice(Where: ExitMBB->end(), Other: &MBB, From: MBBI, To: MBB.end()); |
| 4099 | ExitMBB->transferSuccessorsAndUpdatePHIs(FromMBB: &MBB); |
| 4100 | MBB.addSuccessor(Succ: LoopMBB); |
| 4101 | // Update liveins. |
| 4102 | fullyRecomputeLiveIns(MBBs: {ExitMBB, LoopMBB}); |
| 4103 | |
| 4104 | return ExitMBB->begin(); |
| 4105 | } |
| 4106 | |
| 4107 | void AArch64FrameLowering::inlineStackProbeFixed( |
| 4108 | MachineBasicBlock::iterator MBBI, Register ScratchReg, int64_t FrameSize, |
| 4109 | StackOffset CFAOffset) const { |
| 4110 | MachineBasicBlock *MBB = MBBI->getParent(); |
| 4111 | MachineFunction &MF = *MBB->getParent(); |
| 4112 | const AArch64InstrInfo *TII = |
| 4113 | MF.getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 4114 | AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 4115 | bool EmitAsyncCFI = AFI->needsAsyncDwarfUnwindInfo(MF); |
| 4116 | bool HasFP = hasFP(MF); |
| 4117 | |
| 4118 | DebugLoc DL; |
| 4119 | int64_t ProbeSize = MF.getInfo<AArch64FunctionInfo>()->getStackProbeSize(); |
| 4120 | int64_t NumBlocks = FrameSize / ProbeSize; |
| 4121 | int64_t ResidualSize = FrameSize % ProbeSize; |
| 4122 | |
| 4123 | LLVM_DEBUG(dbgs() << "Stack probing: total " << FrameSize << " bytes, " |
| 4124 | << NumBlocks << " blocks of " << ProbeSize |
| 4125 | << " bytes, plus " << ResidualSize << " bytes\n" ); |
| 4126 | |
| 4127 | // Decrement SP by NumBlock * ProbeSize bytes, with either unrolled or |
| 4128 | // ordinary loop. |
| 4129 | if (NumBlocks <= AArch64::StackProbeMaxLoopUnroll) { |
| 4130 | for (int i = 0; i < NumBlocks; ++i) { |
| 4131 | // SUB SP, SP, #ProbeSize (or equivalent if ProbeSize is not |
| 4132 | // encodable in a SUB). |
| 4133 | emitFrameOffset(MBB&: *MBB, MBBI, DL, DestReg: AArch64::SP, SrcReg: AArch64::SP, |
| 4134 | Offset: StackOffset::getFixed(Fixed: -ProbeSize), TII, |
| 4135 | MachineInstr::FrameSetup, SetNZCV: false, NeedsWinCFI: false, HasWinCFI: nullptr, |
| 4136 | EmitCFAOffset: EmitAsyncCFI && !HasFP, InitialOffset: CFAOffset); |
| 4137 | CFAOffset += StackOffset::getFixed(Fixed: ProbeSize); |
| 4138 | // LDR XZR, [SP] |
| 4139 | BuildMI(BB&: *MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: AArch64::LDRXui)) |
| 4140 | .addDef(RegNo: AArch64::XZR) |
| 4141 | .addReg(RegNo: AArch64::SP) |
| 4142 | .addImm(Val: 0) |
| 4143 | .addMemOperand(MMO: MF.getMachineMemOperand( |
| 4144 | PtrInfo: MachinePointerInfo::getUnknownStack(MF), |
| 4145 | F: MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, Size: 8, |
| 4146 | BaseAlignment: Align(8))) |
| 4147 | .setMIFlags(MachineInstr::FrameSetup); |
| 4148 | } |
| 4149 | } else if (NumBlocks != 0) { |
| 4150 | // SUB ScratchReg, SP, #FrameSize (or equivalent if FrameSize is not |
| 4151 | // encodable in ADD). ScrathReg may temporarily become the CFA register. |
| 4152 | emitFrameOffset(MBB&: *MBB, MBBI, DL, DestReg: ScratchReg, SrcReg: AArch64::SP, |
| 4153 | Offset: StackOffset::getFixed(Fixed: -ProbeSize * NumBlocks), TII, |
| 4154 | MachineInstr::FrameSetup, SetNZCV: false, NeedsWinCFI: false, HasWinCFI: nullptr, |
| 4155 | EmitCFAOffset: EmitAsyncCFI && !HasFP, InitialOffset: CFAOffset); |
| 4156 | CFAOffset += StackOffset::getFixed(Fixed: ProbeSize * NumBlocks); |
| 4157 | MBBI = inlineStackProbeLoopExactMultiple(MBBI, ProbeSize, TargetReg: ScratchReg); |
| 4158 | MBB = MBBI->getParent(); |
| 4159 | if (EmitAsyncCFI && !HasFP) { |
| 4160 | // Set the CFA register back to SP. |
| 4161 | CFIInstBuilder(*MBB, MBBI, MachineInstr::FrameSetup) |
| 4162 | .buildDefCFARegister(Reg: AArch64::SP); |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | if (ResidualSize != 0) { |
| 4167 | // SUB SP, SP, #ResidualSize (or equivalent if ResidualSize is not encodable |
| 4168 | // in SUB). |
| 4169 | emitFrameOffset(MBB&: *MBB, MBBI, DL, DestReg: AArch64::SP, SrcReg: AArch64::SP, |
| 4170 | Offset: StackOffset::getFixed(Fixed: -ResidualSize), TII, |
| 4171 | MachineInstr::FrameSetup, SetNZCV: false, NeedsWinCFI: false, HasWinCFI: nullptr, |
| 4172 | EmitCFAOffset: EmitAsyncCFI && !HasFP, InitialOffset: CFAOffset); |
| 4173 | if (ResidualSize > AArch64::StackProbeMaxUnprobedStack) { |
| 4174 | // LDR XZR, [SP] |
| 4175 | BuildMI(BB&: *MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: AArch64::LDRXui)) |
| 4176 | .addDef(RegNo: AArch64::XZR) |
| 4177 | .addReg(RegNo: AArch64::SP) |
| 4178 | .addImm(Val: 0) |
| 4179 | .addMemOperand(MMO: MF.getMachineMemOperand( |
| 4180 | PtrInfo: MachinePointerInfo::getUnknownStack(MF), |
| 4181 | F: MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, Size: 8, |
| 4182 | BaseAlignment: Align(8))) |
| 4183 | .setMIFlags(MachineInstr::FrameSetup); |
| 4184 | } |
| 4185 | } |
| 4186 | } |
| 4187 | |
| 4188 | void AArch64FrameLowering::inlineStackProbe(MachineFunction &MF, |
| 4189 | MachineBasicBlock &MBB) const { |
| 4190 | // Get the instructions that need to be replaced. We emit at most two of |
| 4191 | // these. Remember them in order to avoid complications coming from the need |
| 4192 | // to traverse the block while potentially creating more blocks. |
| 4193 | SmallVector<MachineInstr *, 4> ToReplace; |
| 4194 | for (MachineInstr &MI : MBB) |
| 4195 | if (MI.getOpcode() == AArch64::PROBED_STACKALLOC || |
| 4196 | MI.getOpcode() == AArch64::PROBED_STACKALLOC_VAR) |
| 4197 | ToReplace.push_back(Elt: &MI); |
| 4198 | |
| 4199 | for (MachineInstr *MI : ToReplace) { |
| 4200 | if (MI->getOpcode() == AArch64::PROBED_STACKALLOC) { |
| 4201 | Register ScratchReg = MI->getOperand(i: 0).getReg(); |
| 4202 | int64_t FrameSize = MI->getOperand(i: 1).getImm(); |
| 4203 | StackOffset CFAOffset = StackOffset::get(Fixed: MI->getOperand(i: 2).getImm(), |
| 4204 | Scalable: MI->getOperand(i: 3).getImm()); |
| 4205 | inlineStackProbeFixed(MBBI: MI->getIterator(), ScratchReg, FrameSize, |
| 4206 | CFAOffset); |
| 4207 | } else { |
| 4208 | assert(MI->getOpcode() == AArch64::PROBED_STACKALLOC_VAR && |
| 4209 | "Stack probe pseudo-instruction expected" ); |
| 4210 | const AArch64InstrInfo *TII = |
| 4211 | MI->getMF()->getSubtarget<AArch64Subtarget>().getInstrInfo(); |
| 4212 | Register TargetReg = MI->getOperand(i: 0).getReg(); |
| 4213 | (void)TII->probedStackAlloc(MBBI: MI->getIterator(), TargetReg, FrameSetup: true); |
| 4214 | } |
| 4215 | MI->eraseFromParent(); |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | struct StackAccess { |
| 4220 | enum AccessType { |
| 4221 | NotAccessed = 0, // Stack object not accessed by load/store instructions. |
| 4222 | GPR = 1 << 0, // A general purpose register. |
| 4223 | PPR = 1 << 1, // A predicate register. |
| 4224 | FPR = 1 << 2, // A floating point/Neon/SVE register. |
| 4225 | }; |
| 4226 | |
| 4227 | int Idx; |
| 4228 | StackOffset Offset; |
| 4229 | int64_t Size; |
| 4230 | unsigned AccessTypes; |
| 4231 | |
| 4232 | StackAccess() : Idx(0), Offset(), Size(0), AccessTypes(NotAccessed) {} |
| 4233 | |
| 4234 | bool operator<(const StackAccess &Rhs) const { |
| 4235 | return std::make_tuple(args: start(), args: Idx) < |
| 4236 | std::make_tuple(args: Rhs.start(), args: Rhs.Idx); |
| 4237 | } |
| 4238 | |
| 4239 | bool isCPU() const { |
| 4240 | // Predicate register load and store instructions execute on the CPU. |
| 4241 | return AccessTypes & (AccessType::GPR | AccessType::PPR); |
| 4242 | } |
| 4243 | bool isSME() const { return AccessTypes & AccessType::FPR; } |
| 4244 | bool isMixed() const { return isCPU() && isSME(); } |
| 4245 | |
| 4246 | int64_t start() const { return Offset.getFixed() + Offset.getScalable(); } |
| 4247 | int64_t end() const { return start() + Size; } |
| 4248 | |
| 4249 | std::string getTypeString() const { |
| 4250 | switch (AccessTypes) { |
| 4251 | case AccessType::FPR: |
| 4252 | return "FPR" ; |
| 4253 | case AccessType::PPR: |
| 4254 | return "PPR" ; |
| 4255 | case AccessType::GPR: |
| 4256 | return "GPR" ; |
| 4257 | case AccessType::NotAccessed: |
| 4258 | return "NA" ; |
| 4259 | default: |
| 4260 | return "Mixed" ; |
| 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | void print(raw_ostream &OS) const { |
| 4265 | OS << getTypeString() << " stack object at [SP" |
| 4266 | << (Offset.getFixed() < 0 ? "" : "+" ) << Offset.getFixed(); |
| 4267 | if (Offset.getScalable()) |
| 4268 | OS << (Offset.getScalable() < 0 ? "" : "+" ) << Offset.getScalable() |
| 4269 | << " * vscale" ; |
| 4270 | OS << "]" ; |
| 4271 | } |
| 4272 | }; |
| 4273 | |
| 4274 | static inline raw_ostream &operator<<(raw_ostream &OS, const StackAccess &SA) { |
| 4275 | SA.print(OS); |
| 4276 | return OS; |
| 4277 | } |
| 4278 | |
| 4279 | void AArch64FrameLowering::( |
| 4280 | const MachineFunction &MF, MachineOptimizationRemarkEmitter *ORE) const { |
| 4281 | |
| 4282 | auto *AFI = MF.getInfo<AArch64FunctionInfo>(); |
| 4283 | if (AFI->getSMEFnAttrs().hasNonStreamingInterfaceAndBody()) |
| 4284 | return; |
| 4285 | |
| 4286 | uint64_t HazardSize = getStackHazardSize(MF); |
| 4287 | if (!HazardSize) |
| 4288 | HazardSize = MF.getSubtarget<AArch64Subtarget>() |
| 4289 | .getCLOpts() |
| 4290 | .stack_hazard_remark_size; |
| 4291 | |
| 4292 | if (HazardSize == 0) |
| 4293 | return; |
| 4294 | |
| 4295 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 4296 | // Bail if function has no stack objects. |
| 4297 | if (!MFI.hasStackObjects()) |
| 4298 | return; |
| 4299 | |
| 4300 | std::vector<StackAccess> StackAccesses(MFI.getNumObjects()); |
| 4301 | |
| 4302 | size_t NumFPLdSt = 0; |
| 4303 | size_t NumNonFPLdSt = 0; |
| 4304 | |
| 4305 | // Collect stack accesses via Load/Store instructions. |
| 4306 | for (const MachineBasicBlock &MBB : MF) { |
| 4307 | for (const MachineInstr &MI : MBB) { |
| 4308 | if (!MI.mayLoadOrStore() || MI.getNumMemOperands() < 1) |
| 4309 | continue; |
| 4310 | for (MachineMemOperand *MMO : MI.memoperands()) { |
| 4311 | std::optional<int> FI = getMMOFrameID(MMO, MFI); |
| 4312 | if (FI && !MFI.isDeadObjectIndex(ObjectIdx: *FI)) { |
| 4313 | int FrameIdx = *FI; |
| 4314 | |
| 4315 | size_t ArrIdx = FrameIdx + MFI.getNumFixedObjects(); |
| 4316 | if (StackAccesses[ArrIdx].AccessTypes == StackAccess::NotAccessed) { |
| 4317 | StackAccesses[ArrIdx].Idx = FrameIdx; |
| 4318 | StackAccesses[ArrIdx].Offset = |
| 4319 | getFrameIndexReferenceFromSP(MF, FI: FrameIdx); |
| 4320 | StackAccesses[ArrIdx].Size = MFI.getObjectSize(ObjectIdx: FrameIdx); |
| 4321 | } |
| 4322 | |
| 4323 | unsigned RegTy = StackAccess::AccessType::GPR; |
| 4324 | if (MFI.hasScalableStackID(ObjectIdx: FrameIdx)) |
| 4325 | RegTy = isPPRAccess(MI) ? StackAccess::PPR : StackAccess::FPR; |
| 4326 | else if (AArch64InstrInfo::isFpOrNEON(MI)) |
| 4327 | RegTy = StackAccess::FPR; |
| 4328 | |
| 4329 | StackAccesses[ArrIdx].AccessTypes |= RegTy; |
| 4330 | |
| 4331 | if (RegTy == StackAccess::FPR) |
| 4332 | ++NumFPLdSt; |
| 4333 | else |
| 4334 | ++NumNonFPLdSt; |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | if (NumFPLdSt == 0 || NumNonFPLdSt == 0) |
| 4341 | return; |
| 4342 | |
| 4343 | llvm::sort(C&: StackAccesses); |
| 4344 | llvm::erase_if(C&: StackAccesses, P: [](const StackAccess &S) { |
| 4345 | return S.AccessTypes == StackAccess::NotAccessed; |
| 4346 | }); |
| 4347 | |
| 4348 | SmallVector<const StackAccess *> MixedObjects; |
| 4349 | SmallVector<std::pair<const StackAccess *, const StackAccess *>> HazardPairs; |
| 4350 | |
| 4351 | if (StackAccesses.front().isMixed()) |
| 4352 | MixedObjects.push_back(Elt: &StackAccesses.front()); |
| 4353 | |
| 4354 | for (auto It = StackAccesses.begin(), End = std::prev(x: StackAccesses.end()); |
| 4355 | It != End; ++It) { |
| 4356 | const auto &First = *It; |
| 4357 | const auto &Second = *(It + 1); |
| 4358 | |
| 4359 | if (Second.isMixed()) |
| 4360 | MixedObjects.push_back(Elt: &Second); |
| 4361 | |
| 4362 | if ((First.isSME() && Second.isCPU()) || |
| 4363 | (First.isCPU() && Second.isSME())) { |
| 4364 | uint64_t Distance = static_cast<uint64_t>(Second.start() - First.end()); |
| 4365 | if (Distance < HazardSize) |
| 4366 | HazardPairs.emplace_back(Args: &First, Args: &Second); |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | auto = [&](llvm::StringRef Str) { |
| 4371 | ORE->emit(RemarkBuilder: [&]() { |
| 4372 | auto R = MachineOptimizationRemarkAnalysis( |
| 4373 | "sme" , "StackHazard" , MF.getFunction().getSubprogram(), &MF.front()); |
| 4374 | return R << formatv(Fmt: "stack hazard in '{0}': " , Vals: MF.getName()).str() << Str; |
| 4375 | }); |
| 4376 | }; |
| 4377 | |
| 4378 | for (const auto &P : HazardPairs) |
| 4379 | EmitRemark(formatv(Fmt: "{0} is too close to {1}" , Vals: *P.first, Vals: *P.second).str()); |
| 4380 | |
| 4381 | for (const auto *Obj : MixedObjects) |
| 4382 | EmitRemark( |
| 4383 | formatv(Fmt: "{0} accessed by both GP and FP instructions" , Vals: *Obj).str()); |
| 4384 | } |
| 4385 | |