Index: lib/Target/AArch64/AArch64FrameLowering.h =================================================================== --- lib/Target/AArch64/AArch64FrameLowering.h +++ lib/Target/AArch64/AArch64FrameLowering.h @@ -37,6 +37,8 @@ void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override; void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override; + bool canUseAsPrologue(const MachineBasicBlock &MBB) const override; + int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const override; int resolveFrameIndexReference(const MachineFunction &MF, int FI, Index: lib/Target/AArch64/AArch64FrameLowering.cpp =================================================================== --- lib/Target/AArch64/AArch64FrameLowering.cpp +++ lib/Target/AArch64/AArch64FrameLowering.cpp @@ -250,6 +250,38 @@ } } +// Find a scratch register that we can use at the start of the prologue to +// re-align the stack pointer. +static unsigned findScratchRegister(MachineBasicBlock *MBB) { + // If MBB is an entry block, use X9 as the scratch register + if (&MBB->getParent()->front() == MBB) + return AArch64::X9; + + RegScavenger RS; + RS.enterBasicBlock(MBB); + + // Prefer X9 since it was historically used for the prologue scratch reg. + if (!RS.isRegUsed(AArch64::X9)) + return AArch64::X9; + + return RS.FindUnusedReg(&AArch64::GPR64RegClass); +} + +bool AArch64FrameLowering::canUseAsPrologue( + const MachineBasicBlock &MBB) const { + const MachineFunction *MF = MBB.getParent(); + MachineBasicBlock *TmpMBB = const_cast(&MBB); + const AArch64Subtarget &Subtarget = MF->getSubtarget(); + const AArch64RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); + + // Don't need a scratch register if we're not going to re-align the stack. + if (!RegInfo->needsStackRealignment(*MF)) + return true; + // Otherwise, we can use any block as long as it has a scratch register + // available. + return findScratchRegister(TmpMBB) != AArch64::NoRegister; +} + void AArch64FrameLowering::emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const { MachineBasicBlock::iterator MBBI = MBB.begin(); @@ -331,8 +363,8 @@ const bool NeedsRealignment = RegInfo->needsStackRealignment(MF); unsigned scratchSPReg = AArch64::SP; if (NumBytes && NeedsRealignment) { - // Use the first callee-saved register as a scratch register. - scratchSPReg = AArch64::X9; + scratchSPReg = findScratchRegister(&MBB); + assert(scratchSPReg != AArch64::NoRegister); } // If we're a leaf function, try using the red zone. @@ -926,19 +958,14 @@ if (RegInfo->hasBasePointer(MF)) BasePointerReg = RegInfo->getBaseRegister(); - unsigned StackAlignReg = AArch64::NoRegister; - if (RegInfo->needsStackRealignment(MF) && !RegInfo->hasBasePointer(MF)) - StackAlignReg = AArch64::X9; - bool ExtraCSSpill = false; const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&MF); // Figure out which callee-saved registers to save/restore. for (unsigned i = 0; CSRegs[i]; ++i) { const unsigned Reg = CSRegs[i]; - // Add the stack re-align scratch register and base pointer register to - // SavedRegs set only if they are callee-save. - if (Reg == BasePointerReg || Reg == StackAlignReg) + // Add the base pointer register to SavedRegs if it is callee-save. + if (Reg == BasePointerReg) SavedRegs.set(Reg); bool RegUsed = SavedRegs.test(Reg); Index: test/CodeGen/AArch64/arm64-shrink-wrapping.ll =================================================================== --- test/CodeGen/AArch64/arm64-shrink-wrapping.ll +++ test/CodeGen/AArch64/arm64-shrink-wrapping.ll @@ -630,3 +630,34 @@ end: ret void } + +; Re-aligned stack pointer. See bug 26642. Avoid clobbering live +; values in the prologue when re-aligning the stack pointer. +; CHECK-LABEL: stack_realign: +; ENABLE-DAG: lsl w[[DIV1:[0-9]+]], w0, w1 +; ENABLE-DAG: lsl w[[DIV2:[0-9]+]], w1, w0 +; ENABLE: stp x29, x30, [sp, #-16]! +; ENABLE: mov x29, sp +; ENABLE-NOT: sub x[[DIV1]], sp, #16 +; ENABLE-NOT: sub x[[DIV2]], sp, #16 +; ENABLE-DAG: str w[[DIV1]], +; ENABLE-DAG: str w[[DIV2]], + +define i32 @stack_realign(i32 %a, i32 %b, i32* %ptr1, i32* %ptr2) { + %tmp = alloca i32, align 32 + %shl1 = shl i32 %a, %b + %shl2 = shl i32 %b, %a + %tmp2 = icmp slt i32 %a, %b + br i1 %tmp2, label %true, label %false + +true: + store i32 %a, i32* %tmp, align 4 + %tmp4 = load i32, i32* %tmp + br label %false + +false: + %tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ] + store i32 %shl1, i32* %ptr1 + store i32 %shl2, i32* %ptr2 + ret i32 %tmp.0 +}