diff --git a/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp b/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp --- a/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp +++ b/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp @@ -458,19 +458,19 @@ (!UseAtEnd && (&MBB->getParent()->front() == MBB))) return true; - RS.enterBasicBlock(*MBB); - - if (UseAtEnd && !MBB->empty()) { - // The scratch register will be used at the end of the block, so must - // consider all registers used within the block - + if (UseAtEnd) { + // The scratch register will be used before the first terminator (or at the + // end of the block if there are no terminators). MachineBasicBlock::iterator MBBI = MBB->getFirstTerminator(); - // If no terminator, back iterator up to previous instruction. - if (MBBI == MBB->end()) - MBBI = std::prev(MBBI); - - if (MBBI != MBB->begin()) - RS.forward(MBBI); + if (MBBI == MBB->begin()) { + RS.enterBasicBlock(*MBB); + } else { + RS.enterBasicBlockEnd(*MBB); + RS.backward(std::prev(MBBI)); + } + } else { + // The scratch register will be used at the start of the block. + RS.enterBasicBlock(*MBB); } // If the two registers are available, we're all good.