diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index 68edabb083be3..35832b594e9a3 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -1213,11 +1213,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); @@ -1231,17 +1237,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) {