diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -6083,12 +6083,13 @@ case Instruction::SRem: { // x / y is undefined if y == 0 or x == INT_MIN and y == -1 const DataLayout &DL = Inst->getModule()->getDataLayout(); - KnownBits KnownDenominator = - computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT); // We cannot hoist this division if the denominator is 0. - if (!KnownDenominator.isNonZero()) + if (!isKnownNonZero(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT)) return false; + KnownBits KnownDenominator = + computeKnownBits(Inst->getOperand(1), DL, /*Depth*/ 0, AC, CtxI, DT); + // It's safe to hoist if the denominator is not 0 or -1. if (!KnownDenominator.Zero.isZero()) return true; diff --git a/llvm/test/Transforms/InstCombine/binop-select.ll b/llvm/test/Transforms/InstCombine/binop-select.ll --- a/llvm/test/Transforms/InstCombine/binop-select.ll +++ b/llvm/test/Transforms/InstCombine/binop-select.ll @@ -479,8 +479,8 @@ define <2 x i32> @test_sdiv_to_const_Csdiv_todo_no_common_bit(<2 x i32> %x) { ; CHECK-LABEL: @test_sdiv_to_const_Csdiv_todo_no_common_bit( ; CHECK-NEXT: [[C_NOT:%.*]] = icmp eq <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[C_NOT]], <2 x i32> , <2 x i32> -; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[X]], [[COND]] +; CHECK-NEXT: [[TMP1:%.*]] = sdiv <2 x i32> [[X]], +; CHECK-NEXT: [[DIV:%.*]] = select <2 x i1> [[C_NOT]], <2 x i32> , <2 x i32> [[TMP1]] ; CHECK-NEXT: ret <2 x i32> [[DIV]] ; %c = icmp ne <2 x i32> %x, @@ -505,8 +505,8 @@ define i32 @test_srem_to_const_Csrem_todo_no_common_bits(i32 %x) { ; CHECK-LABEL: @test_srem_to_const_Csrem_todo_no_common_bits( ; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 24 -; CHECK-NEXT: [[COND:%.*]] = select i1 [[C]], i32 7, i32 16 -; CHECK-NEXT: [[DIV:%.*]] = srem i32 [[X]], [[COND]] +; CHECK-NEXT: [[TMP1:%.*]] = srem i32 [[X]], 16 +; CHECK-NEXT: [[DIV:%.*]] = select i1 [[C]], i32 3, i32 [[TMP1]] ; CHECK-NEXT: ret i32 [[DIV]] ; %c = icmp eq i32 %x, 24 diff --git a/llvm/test/Transforms/LICM/speculate-div.ll b/llvm/test/Transforms/LICM/speculate-div.ll --- a/llvm/test/Transforms/LICM/speculate-div.ll +++ b/llvm/test/Transforms/LICM/speculate-div.ll @@ -97,10 +97,10 @@ ; CHECK-NEXT: entry: ; CHECK-NEXT: [[N:%.*]] = and i16 [[NN:%.*]], 123 ; CHECK-NEXT: [[X:%.*]] = add nuw nsw i16 [[XX:%.*]], 1 +; CHECK-NEXT: [[DIV:%.*]] = srem i16 [[N]], [[X]] ; CHECK-NEXT: br label [[LOOP:%.*]] ; CHECK: loop: ; CHECK-NEXT: call void @maythrow() -; CHECK-NEXT: [[DIV:%.*]] = srem i16 [[N]], [[X]] ; CHECK-NEXT: call void @use(i16 [[DIV]]) ; CHECK-NEXT: br label [[LOOP]] ;