diff --git a/llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp b/llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp --- a/llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp +++ b/llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp @@ -1763,6 +1763,11 @@ return false; } +static bool needsWinCFI(const MachineFunction *MF) { + return MF->getTarget().getMCAsmInfo()->usesWindowsCFI() && + MF->getFunction().needsUnwindTableEntry(); +} + MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward( MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) { MachineBasicBlock::iterator E = I->getParent()->end(); @@ -1803,14 +1808,11 @@ // the memory access (I) and the increment (MBBI) can access the memory // region defined by [SP, MBBI]. const bool BaseRegSP = BaseReg == AArch64::SP; - if (BaseRegSP) { + if (BaseRegSP && needsWinCFI(I->getMF())) { // FIXME: For now, we always block the optimization over SP in windows // targets as it requires to adjust the unwind/debug info, messing up // the unwind info can actually cause a miscompile. - const MCAsmInfo *MAI = I->getMF()->getTarget().getMCAsmInfo(); - if (MAI->usesWindowsCFI() && - I->getMF()->getFunction().needsUnwindTableEntry()) - return E; + return E; } for (unsigned Count = 0; MBBI != E && Count < Limit; @@ -1866,6 +1868,14 @@ } } + const bool BaseRegSP = BaseReg == AArch64::SP; + if (BaseRegSP && needsWinCFI(I->getMF())) { + // FIXME: For now, we always block the optimization over SP in windows + // targets as it requires to adjust the unwind/debug info, messing up + // the unwind info can actually cause a miscompile. + return E; + } + // Track which register units have been modified and used between the first // insn (inclusive) and the second insn. ModifiedRegUnits.clear(); diff --git a/llvm/test/CodeGen/AArch64/arm64-windows-calls.ll b/llvm/test/CodeGen/AArch64/arm64-windows-calls.ll --- a/llvm/test/CodeGen/AArch64/arm64-windows-calls.ll +++ b/llvm/test/CodeGen/AArch64/arm64-windows-calls.ll @@ -26,7 +26,10 @@ entry: ; FIXME: Missed optimization, the entire SP push/pop could be removed ; CHECK-LABEL: f2 -; CHECK: stp xzr, xzr, [sp, #-16]! +; CHECK: sub sp, sp, #16 +; CHECK-NEXT: .seh_stackalloc 16 +; CHECK-NEXT: .seh_endprologue +; CHECK-NEXT: stp xzr, xzr, [sp] ; CHECK-NEXT: mov x0, xzr ; CHECK-NEXT: mov x1, xzr ; CHECK-NEXT: .seh_startepilogue