diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -533,9 +533,17 @@ auto Iter = AdditionalUsers.find(I); if (Iter != AdditionalUsers.end()) { - for (User *U : Iter->second) - if (auto *UI = dyn_cast(U)) - OperandChangedState(UI); + // Cache Instructions to update and update later + // in order to avoid iterator invalidation + SmallPtrSet toNotify; + for (User *U : Iter->second) { + if (auto *UI = dyn_cast(U)) { + toNotify.insert(UI); + } + } + for (Instruction *UI : toNotify) { + OperandChangedState(UI); + } } } void handleCallOverdefined(CallBase &CB);