Index: llvm/include/llvm/Analysis/InstructionSimplify.h =================================================================== --- llvm/include/llvm/Analysis/InstructionSimplify.h +++ llvm/include/llvm/Analysis/InstructionSimplify.h @@ -360,6 +360,12 @@ Function &); const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &, const DataLayout &); + +// The function returns compare result if they can be concluded with assume. +Value *simplifyICmpWithDominatingAssume(CmpInst::Predicate Predicate, + Value *LHS, Value *RHS, + const SimplifyQuery &Q, + const Instruction *CxtI = nullptr); } // end namespace llvm #endif Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -3627,11 +3627,15 @@ return nullptr; } -static Value *simplifyICmpWithDominatingAssume(CmpInst::Predicate Predicate, - Value *LHS, Value *RHS, - const SimplifyQuery &Q) { +namespace llvm { +Value *simplifyICmpWithDominatingAssume(CmpInst::Predicate Predicate, + Value *LHS, Value *RHS, + const SimplifyQuery &Q, + const Instruction *CxtI) { + auto *Inst = (CxtI != nullptr) ? CxtI : Q.CxtI; + // Gracefully handle instructions that have not been inserted yet. - if (!Q.AC || !Q.CxtI || !Q.CxtI->getParent()) + if (!Q.AC || !Inst || !Inst->getParent()) return nullptr; for (Value *AssumeBaseOp : {LHS, RHS}) { @@ -3642,13 +3646,14 @@ CallInst *Assume = cast(AssumeVH); if (std::optional Imp = isImpliedCondition( Assume->getArgOperand(0), Predicate, LHS, RHS, Q.DL)) - if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT)) + if (isValidAssumeForContext(Assume, Inst, Q.DT)) return ConstantInt::get(getCompareTy(LHS), *Imp); } } return nullptr; } +} // namespace llvm /// Given operands for an ICmpInst, see if we can fold the result. /// If not, this returns null. Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1786,6 +1786,14 @@ return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0); } + // If X / Y == 0, then (X + 1) % Y => (X + 1 == Y) ? 0 : X + 1 . + // Value *X; + if (match(Op0, m_NUWAdd(m_Value(X), m_One())) && + simplifyICmpWithDominatingAssume(ICmpInst::ICMP_ULT, X, Op1, SQ, &I)) { + Value *Cmp = Builder.CreateICmpEQ(Op0, Op1); + return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0);; + } + return nullptr; } Index: llvm/test/Transforms/InstCombine/urem-via-cmp-select.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/urem-via-cmp-select.ll @@ -0,0 +1,23 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +; https://alive2.llvm.org/ce/z/UNmz9j +define noundef i64 @src(i64 noundef %x, i64 noundef %n) { +; CHECK-LABEL: @src( +; CHECK-NEXT: start: +; CHECK-NEXT: [[_3:%.*]] = icmp ult i64 [[X:%.*]], [[N:%.*]] +; CHECK-NEXT: tail call void @llvm.assume(i1 [[_3]]) +; CHECK-NEXT: [[_6:%.*]] = add nuw i64 [[X]], 1 +; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i64 [[_6]], [[N]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i64 0, i64 [[_6]] +; CHECK-NEXT: ret i64 [[TMP1]] +; +start: + %_3 = icmp ult i64 %x, %n + tail call void @llvm.assume(i1 %_3) + %_6 = add nuw i64 %x, 1 + %0 = urem i64 %_6, %n + ret i64 %0 +} + +declare void @llvm.assume(i1 noundef)