diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -1079,6 +1079,16 @@ } // IsSigned == false. + + // Is the unsigned dividend known to be less than a constant divisor? + // TODO: Convert this (and above) to range analysis + // ("computeConstantRangeIncludingKnownBits")? + const APInt *C; + if (match(Y, m_APInt(C)) && + computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT).getMaxValue().ult(*C)) + return true; + + // Try again for any divisor: // Is the dividend unsigned less than the divisor? return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse); } diff --git a/llvm/test/Transforms/InstSimplify/div.ll b/llvm/test/Transforms/InstSimplify/div.ll --- a/llvm/test/Transforms/InstSimplify/div.ll +++ b/llvm/test/Transforms/InstSimplify/div.ll @@ -155,10 +155,7 @@ define i8 @udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) { ; CHECK-LABEL: @udiv_dividend_known_smaller_than_constant_divisor2( -; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8 -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[T0]], 12 -; CHECK-NEXT: [[R:%.*]] = udiv i8 [[XOR]], 14 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 0 ; %t0 = zext i1 %b to i8 %xor = xor i8 %t0, 12 @@ -166,6 +163,8 @@ ret i8 %r } +; negative test - dividend can equal 13 + define i8 @not_udiv_dividend_known_smaller_than_constant_divisor2(i1 %b) { ; CHECK-LABEL: @not_udiv_dividend_known_smaller_than_constant_divisor2( ; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8 diff --git a/llvm/test/Transforms/InstSimplify/rem.ll b/llvm/test/Transforms/InstSimplify/rem.ll --- a/llvm/test/Transforms/InstSimplify/rem.ll +++ b/llvm/test/Transforms/InstSimplify/rem.ll @@ -187,8 +187,7 @@ ; CHECK-LABEL: @urem_dividend_known_smaller_than_constant_divisor2( ; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8 ; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[T0]], 12 -; CHECK-NEXT: [[R:%.*]] = urem i8 [[XOR]], 14 -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 [[XOR]] ; %t0 = zext i1 %b to i8 %xor = xor i8 %t0, 12 @@ -196,6 +195,8 @@ ret i8 %r } +; negative test - dividend can equal 13 + define i8 @not_urem_dividend_known_smaller_than_constant_divisor2(i1 %b) { ; CHECK-LABEL: @not_urem_dividend_known_smaller_than_constant_divisor2( ; CHECK-NEXT: [[T0:%.*]] = zext i1 [[B:%.*]] to i8