diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index 2f5f1089067bf..53d78edda2e9f 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -1215,11 +1215,17 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { for (const auto &A : FrameData.Allocas) { AllocaInst *Alloca = A.Alloca; UsersToUpdate.clear(); - for (User *U : Alloca->users()) { + for (User *U : make_early_inc_range(Alloca->users())) { auto *I = cast(U); - if (DT.dominates(Shape.CoroBegin, I)) + // It is meaningless to retain the lifetime intrinsics refer for the + // member of coroutine frames and the meaningless lifetime intrinsics + // are possible to block further optimizations. + if (I->isLifetimeStartOrEnd()) + I->eraseFromParent(); + else if (DT.dominates(Shape.CoroBegin, I)) UsersToUpdate.push_back(I); } + if (UsersToUpdate.empty()) continue; auto *G = GetFramePointer(Alloca); @@ -1233,17 +1239,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { for (auto *DVR : DbgVariableRecords) DVR->replaceVariableLocationOp(Alloca, G); - for (Instruction *I : UsersToUpdate) { - // It is meaningless to retain the lifetime intrinsics refer for the - // member of coroutine frames and the meaningless lifetime intrinsics - // are possible to block further optimizations. - if (I->isLifetimeStartOrEnd()) { - I->eraseFromParent(); - continue; - } - + for (Instruction *I : UsersToUpdate) I->replaceUsesOfWith(Alloca, G); - } } Builder.SetInsertPoint(&*Shape.getInsertPtAfterFramePtr()); for (const auto &A : FrameData.Allocas) {