Skip to content

Commit cb460c2

Browse files
committed
[InstCombine] Fold icmp spred (X *nsw Z), (Y *nsw Z) i-> icmp pred Z, 0 if scmp(X, Y) is known
1 parent adde6a8 commit cb460c2

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5337,6 +5337,15 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
53375337
return new ICmpInst(Pred, X, Y);
53385338
if (ZKnown.isNegative())
53395339
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), X, Y);
5340+
Value *LessThan = simplifyICmpInst(ICmpInst::ICMP_SLT, X, Y,
5341+
SQ.getWithInstruction(&I));
5342+
if (LessThan && match(LessThan, m_One()))
5343+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Z,
5344+
Constant::getNullValue(Z->getType()));
5345+
Value *GreaterThan = simplifyICmpInst(ICmpInst::ICMP_SGT, X, Y,
5346+
SQ.getWithInstruction(&I));
5347+
if (GreaterThan && match(GreaterThan, m_One()))
5348+
return new ICmpInst(Pred, Z, Constant::getNullValue(Z->getType()));
53405349
}
53415350
} else {
53425351
bool NonZero;

llvm/test/Transforms/InstCombine/icmp-mul.ll

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,10 +1468,7 @@ entry:
14681468
define i1 @test_icmp_slt_mul_known_sgt(i64 %x, i64 %z) {
14691469
; CHECK-LABEL: @test_icmp_slt_mul_known_sgt(
14701470
; CHECK-NEXT: entry:
1471-
; CHECK-NEXT: [[Y:%.*]] = add nsw i64 [[X:%.*]], 1
1472-
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i64 [[X]], [[Z:%.*]]
1473-
; CHECK-NEXT: [[MUL2:%.*]] = mul nsw i64 [[Y]], [[Z]]
1474-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[MUL1]], [[MUL2]]
1471+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[Z:%.*]], 0
14751472
; CHECK-NEXT: ret i1 [[CMP]]
14761473
;
14771474
entry:
@@ -1485,10 +1482,7 @@ entry:
14851482
define i1 @test_icmp_sle_mul_known_sgt(i64 %x, i64 %z) {
14861483
; CHECK-LABEL: @test_icmp_sle_mul_known_sgt(
14871484
; CHECK-NEXT: entry:
1488-
; CHECK-NEXT: [[Y:%.*]] = add nsw i64 [[X:%.*]], 1
1489-
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i64 [[X]], [[Z:%.*]]
1490-
; CHECK-NEXT: [[MUL2:%.*]] = mul nsw i64 [[Y]], [[Z]]
1491-
; CHECK-NEXT: [[CMP:%.*]] = icmp sle i64 [[MUL1]], [[MUL2]]
1485+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[Z:%.*]], -1
14921486
; CHECK-NEXT: ret i1 [[CMP]]
14931487
;
14941488
entry:
@@ -1502,10 +1496,7 @@ entry:
15021496
define i1 @test_icmp_mul_known_slt(i64 %x, i64 %z) {
15031497
; CHECK-LABEL: @test_icmp_mul_known_slt(
15041498
; CHECK-NEXT: entry:
1505-
; CHECK-NEXT: [[Y:%.*]] = add nsw i64 [[X:%.*]], 1
1506-
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i64 [[X]], [[Z:%.*]]
1507-
; CHECK-NEXT: [[MUL2:%.*]] = mul nsw i64 [[Y]], [[Z]]
1508-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[MUL2]], [[MUL1]]
1499+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[Z:%.*]], 0
15091500
; CHECK-NEXT: ret i1 [[CMP]]
15101501
;
15111502
entry:
@@ -1519,10 +1510,7 @@ entry:
15191510
define i1 @test_icmp_slt_mul_known_sgt_commuted1(i64 %x, i64 %z) {
15201511
; CHECK-LABEL: @test_icmp_slt_mul_known_sgt_commuted1(
15211512
; CHECK-NEXT: entry:
1522-
; CHECK-NEXT: [[Y:%.*]] = add nsw i64 [[X:%.*]], 1
1523-
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i64 [[Z:%.*]], [[X]]
1524-
; CHECK-NEXT: [[MUL2:%.*]] = mul nsw i64 [[Y]], [[Z]]
1525-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[MUL1]], [[MUL2]]
1513+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[Z:%.*]], 0
15261514
; CHECK-NEXT: ret i1 [[CMP]]
15271515
;
15281516
entry:
@@ -1536,10 +1524,7 @@ entry:
15361524
define i1 @test_icmp_slt_mul_known_sgt_commuted2(i64 %x, i64 %z) {
15371525
; CHECK-LABEL: @test_icmp_slt_mul_known_sgt_commuted2(
15381526
; CHECK-NEXT: entry:
1539-
; CHECK-NEXT: [[Y:%.*]] = add nsw i64 [[X:%.*]], 1
1540-
; CHECK-NEXT: [[MUL1:%.*]] = mul nsw i64 [[X]], [[Z:%.*]]
1541-
; CHECK-NEXT: [[MUL2:%.*]] = mul nsw i64 [[Z]], [[Y]]
1542-
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[MUL1]], [[MUL2]]
1527+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[Z:%.*]], 0
15431528
; CHECK-NEXT: ret i1 [[CMP]]
15441529
;
15451530
entry:

0 commit comments

Comments
 (0)