Skip to content

Commit 203b618

Browse files
committed
[InstCombine] Simplifiy sdiv -X, X into X == INT_MIN ? 1 : -1
1 parent 8a454e1 commit 203b618

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,13 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
15441544
}
15451545
}
15461546

1547+
// -X / X --> X == INT_MIN ? 1 : -1
1548+
if (isKnownNegation(Op0, Op1)) {
1549+
APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
1550+
Value *Cond = Builder.CreateICmpEQ(Op0, ConstantInt::get(Ty, MinVal));
1551+
return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
1552+
ConstantInt::getAllOnesValue(Ty));
1553+
}
15471554
return nullptr;
15481555
}
15491556

llvm/test/Transforms/InstCombine/div.ll

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,61 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
14321432
ret <2 x i8> %r
14331433
}
14341434

1435+
define i32 @sdiv_sub1(i32 %arg) {
1436+
; CHECK-LABEL: @sdiv_sub1(
1437+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
1438+
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1439+
; CHECK-NEXT: ret i32 [[DIV]]
1440+
;
1441+
%neg = sub i32 0, %arg
1442+
%div = sdiv i32 %neg, %arg
1443+
ret i32 %div
1444+
}
1445+
1446+
define i32 @sdiv_sub2(i32 %arg) {
1447+
; CHECK-LABEL: @sdiv_sub2(
1448+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
1449+
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1450+
; CHECK-NEXT: ret i32 [[DIV]]
1451+
;
1452+
%neg = sub i32 0, %arg
1453+
%div = sdiv i32 %arg, %neg
1454+
ret i32 %div
1455+
}
1456+
1457+
define i32 @sdiv_mul_sub(i32 %x, i32 %y) {
1458+
; CHECK-LABEL: @sdiv_mul_sub(
1459+
; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
1460+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[M]], -2147483648
1461+
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1462+
; CHECK-NEXT: ret i32 [[R]]
1463+
;
1464+
%m = mul i32 %y, %x
1465+
%d = sub i32 0, %m
1466+
%r = sdiv i32 %d, %m
1467+
ret i32 %r
1468+
}
1469+
1470+
define i32 @sdiv_mul_sub_nsw(i32 %x, i32 %y) {
1471+
; CHECK-LABEL: @sdiv_mul_sub_nsw(
1472+
; CHECK-NEXT: ret i32 -1
1473+
;
1474+
%m = mul i32 %y, %x
1475+
%n = sub nsw i32 0, %m
1476+
%d = sdiv i32 %m, %n
1477+
ret i32 %d
1478+
}
1479+
1480+
define i32 @sdiv_mul_nsw_sub_nsw(i32 %x, i32 %y) {
1481+
; CHECK-LABEL: @sdiv_mul_nsw_sub_nsw(
1482+
; CHECK-NEXT: ret i32 -1
1483+
;
1484+
%m = mul nsw i32 %y, %x
1485+
%n = sub nsw i32 0, %m
1486+
%d = sdiv i32 %m, %n
1487+
ret i32 %d
1488+
}
1489+
14351490
; exact propagates
14361491

14371492
define i8 @sdiv_sdiv_mul_nsw_exact_exact(i8 %x, i8 %y, i8 %z) {

0 commit comments

Comments
 (0)