[PATCH] D159203: [InstCombine] Fold (A/-B)==(A/B) to (A/B)==0
Marc Auberer via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 2 05:19:55 PDT 2023
marcauberer updated this revision to Diff 555587.
marcauberer added a comment.
Rebase onto ValueTracking change
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D159203/new/
https://reviews.llvm.org/D159203
Files:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-sdiv-sdiv.ll
Index: llvm/test/Transforms/InstCombine/icmp-sdiv-sdiv.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-sdiv-sdiv.ll
+++ llvm/test/Transforms/InstCombine/icmp-sdiv-sdiv.ll
@@ -9,10 +9,8 @@
; CHECK-LABEL: @icmp_sdiv_sdiv_normal_i8(
; CHECK-NEXT: [[PRECOND:%.*]] = icmp ne i8 [[C:%.*]], -128
; CHECK-NEXT: call void @llvm.assume(i1 [[PRECOND]])
-; CHECK-NEXT: [[NEGC:%.*]] = sub i8 0, [[C]]
-; CHECK-NEXT: [[D1:%.*]] = sdiv i8 [[X:%.*]], [[NEGC]]
-; CHECK-NEXT: [[D2:%.*]] = sdiv i8 [[X]], [[C]]
-; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[D1]], [[D2]]
+; CHECK-NEXT: [[D2:%.*]] = sdiv i8 [[X:%.*]], [[C]]
+; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[D2]], 0
; CHECK-NEXT: ret i1 [[C]]
;
%precond = icmp ne i8 %C, -128
@@ -28,10 +26,8 @@
; CHECK-LABEL: @icmp_sdiv_sdiv_normal_i64(
; CHECK-NEXT: [[PRECOND:%.*]] = icmp ne i64 [[C:%.*]], -9223372036854775808
; CHECK-NEXT: call void @llvm.assume(i1 [[PRECOND]])
-; CHECK-NEXT: [[NEGC:%.*]] = sub i64 0, [[C]]
-; CHECK-NEXT: [[D1:%.*]] = sdiv i64 [[X:%.*]], [[NEGC]]
-; CHECK-NEXT: [[D2:%.*]] = sdiv i64 [[X]], [[C]]
-; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[D1]], [[D2]]
+; CHECK-NEXT: [[D2:%.*]] = sdiv i64 [[X:%.*]], [[C]]
+; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[D2]], 0
; CHECK-NEXT: ret i1 [[C]]
;
%precond = icmp ne i64 %C, -9223372036854775808
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5164,6 +5164,23 @@
return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType()));
}
+ // ((A / -B) == (A / B)) -> ((A / B) == 0)
+ // if known A != INT_MIN or B != INT_MIN
+ {
+ const unsigned OpWidth = Op0->getType()->getScalarSizeInBits();
+ APInt SignedMinValue = APInt::getSignedMinValue(OpWidth);
+ Constant *MinInt = ConstantInt::get(Op0->getType(), SignedMinValue);
+ if (match(Op0, m_OneUse(m_SDiv(m_Value(A), m_OneUse(m_Neg(m_Value(B)))))) &&
+ match(Op1, m_SDiv(m_Specific(A), m_Specific(B)))) {
+ // Check if A is known to be != INT_MIN
+ if (isKnownNonEqual(A, MinInt, DL, &AC, &I, &DT, true))
+ return new ICmpInst(Pred, Op1, Constant::getNullValue(A->getType()));
+ // Check if B is known to be != INT_MIN
+ if (isKnownNonEqual(B, MinInt, DL, &AC, &I, &DT, true))
+ return new ICmpInst(Pred, Op1, Constant::getNullValue(A->getType()));
+ }
+ }
+
// (X&Z) == (Y&Z) -> (X^Y) & Z == 0
if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159203.555587.patch
Type: text/x-patch
Size: 2781 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230902/2fb81c53/attachment.bin>
More information about the llvm-commits
mailing list