-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[InstCombine] fold icmp with add/sub instructions having the same operands #143241
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
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Acthinks Yang (Acthinks) ChangesCloses#143211 Full diff: https://github.com/llvm/llvm-project/pull/143241.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index c112fae351817..1054d091fd226 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7728,6 +7728,47 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
}
}
+ // In case of a comparison with add/sub instructions having the same operands,
+ // check whether cmp operands have same signed no wrap. If so, just compare
+ // the sub's second operand and zero.
+ // For example:
+ // %tmp1 = sub nsw i8 %x, %y
+ // %tmp2 = add nsw i8 %x, %y
+ // %cmp = icmp sgt i8 %tmp1, %tmp2
+ // transform this into:
+ // %cmp = icmp slt i32 %y, 0
+ // This handles similar cases to transform.
+ {
+ Value *A, *B;
+ auto *I0 = dyn_cast<OverflowingBinaryOperator>(Op0);
+ auto *I1 = dyn_cast<OverflowingBinaryOperator>(Op1);
+ bool UnsignedCmp = ICmpInst::isUnsigned(Pred);
+ bool SignedCmp = ICmpInst::isSigned(Pred);
+ bool EqualityCmp = ICmpInst::isEquality(Pred);
+
+ if (I0 && I1) {
+ bool I0NUW = I0->hasNoUnsignedWrap();
+ bool I1NUW = I1->hasNoUnsignedWrap();
+ bool I0NSW = I0->hasNoSignedWrap();
+ bool I1NSW = I1->hasNoSignedWrap();
+ bool Swaped = false;
+ if ((UnsignedCmp && I0NUW && I1NUW) || (SignedCmp && I0NSW && I1NSW) ||
+ (EqualityCmp && I0NUW && I0NSW && I1NUW && I1NSW)) {
+ if (I0->getOpcode() == Instruction::Add &&
+ I1->getOpcode() == Instruction::Sub) {
+ std::swap(I0, I1);
+ Swaped = true;
+ }
+ if (match(I0, m_Sub(m_Value(A), m_Value(B))) &&
+ (match(I1, m_Add(m_Specific(A), m_Specific(B))) ||
+ match(I1, m_Add(m_Specific(B), m_Specific(A))))) {
+ return new ICmpInst(Swaped ? Pred : I.getSwappedPredicate(), B,
+ ConstantInt::get(Op0->getType(), 0));
+ }
+ }
+ }
+ }
+
// Try to optimize equality comparisons against alloca-based pointers.
if (Op0->getType()->isPointerTy() && I.isEquality()) {
assert(Op1->getType()->isPointerTy() &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-subadd.ll b/llvm/test/Transforms/InstCombine/icmp-subadd.ll
new file mode 100644
index 0000000000000..46e5ebe57a1b0
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-subadd.ll
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @test-same-operands-sub-add-nsw-icmp-sgt(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-sgt(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[B]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nsw i8 %a, %b
+ %add = add nsw i8 %a, %b
+ %cmp = icmp sgt i8 %sub, %add
+ ret i1 %cmp
+}
+
+define i1 @test-same-operands-sub-add-nsw-icmp-slt(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-slt(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nsw i8 %a, %b
+ %add = add nsw i8 %a, %b
+ %cmp = icmp slt i8 %sub, %add
+ ret i1 %cmp
+}
+
+define i1 @test-same-operands-sub-add-nsw-icmp-sle(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-sle(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], -1
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nsw i8 %a, %b
+ %add = add nsw i8 %a, %b
+ %cmp = icmp sle i8 %sub, %add
+ ret i1 %cmp
+}
+
+define i1 @test-same-operands-sub-add-nsw-nuw-icmp-eq(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-nuw-icmp-eq(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[B]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nuw nsw i8 %a, %b
+ %add = add nuw nsw i8 %a, %b
+ %cmp = icmp eq i8 %sub, %add
+ ret i1 %cmp
+}
+
+; Check not folded
+define i1 @test-same-operands-sub-add-nsw-icmp-eq(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-same-operands-sub-add-nsw-icmp-eq(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[A]], [[B]]
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[A]], [[B]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[SUB]], [[ADD]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nsw i8 %a, %b
+ %add = add nsw i8 %a, %b
+ %cmp = icmp eq i8 %sub, %add
+ ret i1 %cmp
+}
+
+define i1 @test-add-sub-nsw-icmp-sgt(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-add-sub-nsw-icmp-sgt(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[B]], 0
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nsw i8 %a, %b
+ %add = add nsw i8 %a, %b
+ %cmp = icmp sgt i8 %add, %sub
+ ret i1 %cmp
+}
+
+define i1 @test-add-sub-nuw-icmp-uge(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-add-sub-nuw-icmp-uge(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %sub = sub nuw i8 %a, %b
+ %add = add nuw i8 %a, %b
+ %cmp = icmp uge i8 %add, %sub
+ ret i1 %cmp
+}
+
+; Check not folded
+define i1 @test-add-sub-nuw-icmp-sge(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-add-sub-nuw-icmp-sge(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: [[SUB:%.*]] = sub nuw i8 [[A]], [[B]]
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[A]], [[B]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[ADD]], [[SUB]]
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %sub = sub nuw i8 %a, %b
+ %add = add nuw i8 %a, %b
+ %cmp = icmp sge i8 %add, %sub
+ ret i1 %cmp
+}
+
+define i1 @test-add-swap-sub-nuw-icmp-uge(i8 %a, i8 %b) {
+; CHECK-LABEL: define i1 @test-add-swap-sub-nuw-icmp-uge(
+; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) {
+; CHECK-NEXT: ret i1 true
+;
+ %sub = sub nuw i8 %a, %b
+ %add = add nuw i8 %b, %a
+ %cmp = icmp uge i8 %add, %sub
+ ret i1 %cmp
+}
|
Please see https://llvm.org/docs/InstCombineContributorGuide.html. Can you please attach alive2 proofs? For example your equality condition looks too strict, it needs either nuw or nsw, not both: https://alive2.llvm.org/ce/z/krSOPt |
https://alive2.llvm.org/ce/z/ZxseER Add alive2 proofs @nikic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relax equality no wrap constraints & remove swap handling
632cf4c
to
23585d3
Compare
3b73945
to
406f607
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG
…rands Closes#143211
2. looser equality constraints, such as nsw/nuw combinations, can be folded
406f607
to
6975edb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Nikita Popov <[email protected]>
Closes #143211