Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -1556,10 +1556,15 @@ return false; ExtUsers.push_back(User); } + // We'll prove some facts that should be true in the context of ext users. IF + // there is no users, we are done now. If there are some, pick their common + // dominator as context. if (ExtUsers.empty()) { DeadInsts.emplace_back(NarrowUse); return true; } + const Instruction *Context = DT->findNearestCommonDominatorInst(ExtUsers); + assert(Context && "No common dominator?"); if (!CanSignExtend && !CanZeroExtend) { // Because InstCombine turns 'sub nuw' to 'add' losing the no-wrap flag, we @@ -1573,7 +1578,7 @@ if (!SE->isKnownNegative(RHS)) return false; bool ProvedSubNUW = SE->isKnownPredicateAt( - ICmpInst::ICMP_UGE, LHS, SE->getNegativeSCEV(RHS), NarrowUse); + ICmpInst::ICMP_UGE, LHS, SE->getNegativeSCEV(RHS), Context); if (!ProvedSubNUW) return false; // In fact, our 'add' is 'sub nuw'. We will need to widen the 2nd operand as