Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -1255,6 +1255,11 @@ /// If not, this returns null. static Value *simplifySDivInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse) { + // If the divisor is 0, the result is undefined, so assume the divisor is 1. + // sdiv Op0, (1 srem X) --> sdiv Op0, 1 --> Op1 + if (match(Op1, m_SRem(m_One(), m_Value()))) + return Op0; + // If two operands are negated and no signed overflow, return -1. if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true)) return Constant::getAllOnesValue(Op0->getType()); Index: llvm/test/Transforms/InstSimplify/div.ll =================================================================== --- llvm/test/Transforms/InstSimplify/div.ll +++ llvm/test/Transforms/InstSimplify/div.ll @@ -434,3 +434,21 @@ } !0 = !{i32 0, i32 3} + +define i32 @sdiv_one_srem_divisor(i32 %a, i32 %b) { +; CHECK-LABEL: @sdiv_one_srem_divisor( +; CHECK-NEXT: ret i32 [[O:%.*]] +; + %srem = srem i32 1, %b + %sdiv = sdiv i32 %a, %srem + ret i32 %sdiv +} + +define <2 x i8> @sdiv_one_vec_srem_divisor(<2 x i8> %a, <2 x i8> %b) { +; CHECK-LABEL: @sdiv_one_vec_srem_divisor( +; CHECK-NEXT: ret <2 x i8> [[O:%.*]] +; + %srem = srem <2 x i8> , %b + %sdiv = sdiv <2 x i8> %a, %srem + ret <2 x i8> %sdiv +} \ No newline at end of file