Skip to content

[InstCombine] Simplifiy sdiv -X, X into X == INT_MIN ? 1 : -1 #71768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,13 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
}
}

// -X / X --> X == INT_MIN ? 1 : -1
if (isKnownNegation(Op0, Op1)) {
APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
Value *Cond = Builder.CreateICmpEQ(Op0, ConstantInt::get(Ty, MinVal));
return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
ConstantInt::getAllOnesValue(Ty));
}
return nullptr;
}

Expand Down
82 changes: 82 additions & 0 deletions llvm/test/Transforms/InstCombine/div.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,88 @@ define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
ret <2 x i8> %r
}

define i32 @sdiv_sub1(i32 %arg) {
; CHECK-LABEL: @sdiv_sub1(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[DIV]]
;
%neg = sub i32 0, %arg
%div = sdiv i32 %neg, %arg
ret i32 %div
}

define i32 @sdiv_sub2(i32 %arg) {
; CHECK-LABEL: @sdiv_sub2(
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[DIV]]
;
%neg = sub i32 0, %arg
%div = sdiv i32 %arg, %neg
ret i32 %div
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a multi-use test (extra use of %neg). As a div is removed, we want to allow the transform with multi-use.

Please also test the case (x - y) / (y - x), which is one of the special cases isKnownNegation() handles.


define i32 @sub_sdiv_multiuse(i32 %arg) {
; CHECK-LABEL: @sub_sdiv_multiuse(
; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[ARG:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG]], -2147483648
; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: call void @use(i32 [[NEG]])
; CHECK-NEXT: ret i32 [[DIV]]
;
%neg = sub i32 0, %arg
%div = sdiv i32 %arg, %neg
call void @use(i32 %neg)
ret i32 %div
}

define i32 @sdiv_sub_sub(i32 %x ,i32 %y) {
; CHECK-LABEL: @sdiv_sub_sub(
; CHECK-NEXT: [[S:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[S]], -2147483648
; CHECK-NEXT: [[D:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[D]]
;
%s = sub i32 %x, %y
%u = sub i32 %y, %x
%d = sdiv i32 %s, %u
ret i32 %d
}

define i32 @sdiv_mul_sub(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_mul_sub(
; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[M]], -2147483648
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
; CHECK-NEXT: ret i32 [[R]]
;
%m = mul i32 %y, %x
%d = sub i32 0, %m
%r = sdiv i32 %d, %m
ret i32 %r
}

define i32 @sdiv_mul_sub_nsw(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_mul_sub_nsw(
; CHECK-NEXT: ret i32 -1
;
%m = mul i32 %y, %x
%n = sub nsw i32 0, %m
%d = sdiv i32 %m, %n
ret i32 %d
}

define i32 @sdiv_mul_nsw_sub_nsw(i32 %x, i32 %y) {
; CHECK-LABEL: @sdiv_mul_nsw_sub_nsw(
; CHECK-NEXT: ret i32 -1
;
%m = mul nsw i32 %y, %x
%n = sub nsw i32 0, %m
%d = sdiv i32 %m, %n
ret i32 %d
}

; exact propagates

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