diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 289976718e52f..0222c93faf24e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4966,7 +4966,8 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I, return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); case Instruction::SDiv: - if (!I.isEquality() || !BO0->isExact() || !BO1->isExact()) + if (!(I.isEquality() || match(BO0->getOperand(1), m_NonNegative())) || + !BO0->isExact() || !BO1->isExact()) break; return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index 1c7bb36f0d34c..9b2e141bdb050 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -854,6 +854,107 @@ define i1 @PR32949(i32 %X, i32 %Y, i32 %Z) { ret i1 %C } +define i1 @test_sdiv_pos_slt(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_slt( +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp slt i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_sle(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_sle( +; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp sle i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_sgt(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_sgt( +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp sgt i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_sge(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_sge( +; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp sge i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_ult(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_ult( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp ult i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_ule(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_ule( +; CHECK-NEXT: [[CMP:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp ule i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_ugt(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_ugt( +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp ugt i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_pos_uge(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_pos_uge( +; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, 40 + %divy = sdiv exact i32 %y, 40 + %cmp = icmp uge i32 %divx, %divy + ret i1 %cmp +} + +define i1 @test_sdiv_neg_slt(i32 %x, i32 %y) { +; CHECK-LABEL: @test_sdiv_neg_slt( +; CHECK-NEXT: [[DIVX:%.*]] = sdiv exact i32 [[X:%.*]], -40 +; CHECK-NEXT: [[DIVY:%.*]] = sdiv exact i32 [[Y:%.*]], -40 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[DIVX]], [[DIVY]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %divx = sdiv exact i32 %x, -40 + %divy = sdiv exact i32 %y, -40 + %cmp = icmp slt i32 %divx, %divy + ret i1 %cmp +} + ; PR8469 define <2 x i1> @test49(<2 x i32> %i3) { ; CHECK-LABEL: @test49(