Index: lib/Target/ARM/Thumb1FrameLowering.cpp =================================================================== --- lib/Target/ARM/Thumb1FrameLowering.cpp +++ lib/Target/ARM/Thumb1FrameLowering.cpp @@ -406,11 +406,11 @@ if (AFI->getArgRegsSaveSize()) return true; - bool IsV4PopReturn = false; for (const CalleeSavedInfo &CSI : MF.getFrameInfo()->getCalleeSavedInfo()) if (CSI.getReg() == ARM::LR) - IsV4PopReturn = true; - return IsV4PopReturn && STI.hasV4TOps() && !STI.hasV5TOps(); + return true; + + return false; } bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock &MBB, @@ -429,8 +429,9 @@ // and copy that value into LR. auto MBBI = MBB.getFirstTerminator(); if (!ArgRegsSaveSize && MBBI != MBB.end() && - MBBI->getOpcode() == ARM::tBX_RET) { - if (!DoIt) + (MBBI->getOpcode() == ARM::tBX_RET || + MBBI->getOpcode() == ARM::tPOP_RET)) { + if (!DoIt || MBBI->getOpcode() == ARM::tPOP_RET) return true; MachineInstrBuilder MIB = AddDefaultPred( @@ -459,10 +460,10 @@ if (MBBI != MBB.end()) { dl = MBBI->getDebugLoc(); auto InstUpToMBBI = MBB.end(); - // The post-decrement is on purpose here. - // We want to have the liveness right before MBBI. - while (InstUpToMBBI-- != MBBI) - UsedRegs.stepBackward(*InstUpToMBBI); + while (InstUpToMBBI != MBBI) + // The pre-decrement is on purpose here. + // We want to have the liveness right before MBBI. + UsedRegs.stepBackward(*--InstUpToMBBI); } // Look for a register that can be directly use in the POP. @@ -509,8 +510,14 @@ } assert(PopReg && "Do not know how to get LR"); - AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))) - .addReg(PopReg, RegState::Define); + MachineInstr *Pop; + if (MBBI == MBB.end()) { + Pop = &MBB.back(); + assert(Pop->getOpcode() == ARM::tPOP); + Pop->RemoveOperand(Pop->findRegisterDefOperandIdx(ARM::LR)); + } else + Pop = AddDefaultPred(BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP))); + Pop->addOperand(MachineOperand::CreateReg(PopReg, true)); emitSPUpdate(MBB, MBBI, TII, dl, *RegInfo, ArgRegsSaveSize); Index: test/CodeGen/Thumb/thumb-shrink-wrapping.ll =================================================================== --- test/CodeGen/Thumb/thumb-shrink-wrapping.ll +++ test/CodeGen/Thumb/thumb-shrink-wrapping.ll @@ -39,7 +39,8 @@ ; ; With shrink-wrapping, epilogue is just after the call. ; ENABLE-NEXT: add sp, #8 -; ENABLE-NEXT: pop {r7, lr} +; ENABLE-NEXT: pop {r7, r1} +; ENABLE-NEXT: mov lr, r1 ; ; CHECK: [[EXIT_LABEL]]: ;