Skip to content

Revert "[InstCombine] Teach foldSelectOpOp about samesign" #124123

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 1 commit into from
Jan 24, 2025

Conversation

alexfh
Copy link
Contributor

@alexfh alexfh commented Jan 23, 2025

Reverts #122723 due to a miscompilation

See #122723 (comment) for details and the test case.

@alexfh alexfh requested a review from nikic as a code owner January 23, 2025 14:40
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Jan 23, 2025
@alexfh alexfh requested a review from artagnon January 23, 2025 14:41
@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Alexander Kornienko (alexfh)

Changes

Reverts llvm/llvm-project#122723 due to a miscompilation

See #122723 (comment) for details and the test case.


Full diff: https://github.com/llvm/llvm-project/pull/124123.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+4-4)
  • (modified) llvm/test/Transforms/InstCombine/select-cmp.ll (-96)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index d5d9a829c3068a..f66a976ccb47fe 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -428,10 +428,10 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
     CmpPredicate TPred, FPred;
     if (match(TI, m_ICmp(TPred, m_Value(), m_Value())) &&
         match(FI, m_ICmp(FPred, m_Value(), m_Value()))) {
-      bool Swapped = ICmpInst::isRelational(FPred) &&
-                     CmpPredicate::getMatching(
-                         TPred, ICmpInst::getSwappedCmpPredicate(FPred));
-      if (CmpPredicate::getMatching(TPred, FPred) || Swapped) {
+      // FIXME: Use CmpPredicate::getMatching here.
+      CmpInst::Predicate T = TPred, F = FPred;
+      if (T == F || T == ICmpInst::getSwappedCmpPredicate(F)) {
+        bool Swapped = T != F;
         if (Value *MatchOp =
                 getCommonOp(TI, FI, ICmpInst::isEquality(TPred), Swapped)) {
           Value *NewSel = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
diff --git a/llvm/test/Transforms/InstCombine/select-cmp.ll b/llvm/test/Transforms/InstCombine/select-cmp.ll
index 7e5d5821d9f6a7..f7505bd85f89eb 100644
--- a/llvm/test/Transforms/InstCombine/select-cmp.ll
+++ b/llvm/test/Transforms/InstCombine/select-cmp.ll
@@ -23,18 +23,6 @@ define i1 @icmp_ne_common_op00(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_ne_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_ne_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ne i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp samesign ne i6 %x, %y
-  %cmp2 = icmp ne i6 %x, %z
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_ne_common_op01(i1 %c, i3 %x, i3 %y, i3 %z) {
 ; CHECK-LABEL: @icmp_ne_common_op01(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i3 [[Y:%.*]], i3 [[Z:%.*]]
@@ -83,18 +71,6 @@ define i1 @icmp_eq_common_op00(i1 %c, i5 %x, i5 %y, i5 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_eq_samesign_common(i1 %c, i5 %x, i5 %y, i5 %z) {
-; CHECK-LABEL: @icmp_eq_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i5 [[Y:%.*]], i5 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i5 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp eq i5 %x, %y
-  %cmp2 = icmp samesign eq i5 %x, %z
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define <5 x i1> @icmp_eq_common_op01(<5 x i1> %c, <5 x i7> %x, <5 x i7> %y, <5 x i7> %z) {
 ; CHECK-LABEL: @icmp_eq_common_op01(
 ; CHECK-NEXT:    [[R_V:%.*]] = select <5 x i1> [[C:%.*]], <5 x i7> [[Y:%.*]], <5 x i7> [[Z:%.*]]
@@ -158,18 +134,6 @@ define i1 @icmp_slt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_slt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_slt_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ult i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp samesign ult i6 %x, %y
-  %cmp2 = icmp slt i6 %x, %z
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_sgt_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -182,18 +146,6 @@ define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_sgt_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ugt i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp samesign ugt i6 %x, %y
-  %cmp2 = icmp sgt i6 %x, %z
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_sle_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -206,18 +158,6 @@ define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_sle_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_sle_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sge i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp sle i6 %y, %x
-  %cmp2 = icmp samesign ule i6 %z, %x
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_sge_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -230,18 +170,6 @@ define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_sge_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sle i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp sge i6 %y, %x
-  %cmp2 = icmp samesign uge i6 %z, %x
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_slt_sgt_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -254,18 +182,6 @@ define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_slt_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_slt_sgt_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ult i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp samesign ult i6 %x, %y
-  %cmp2 = icmp sgt i6 %z, %x
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_sle_sge_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -278,18 +194,6 @@ define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
   ret i1 %r
 }
 
-define i1 @icmp_sle_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
-; CHECK-LABEL: @icmp_sle_sge_samesign_common(
-; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp sge i6 [[X:%.*]], [[R_V]]
-; CHECK-NEXT:    ret i1 [[R]]
-;
-  %cmp1 = icmp sle i6 %y, %x
-  %cmp2 = icmp samesign uge i6 %x, %z
-  %r = select i1 %c, i1 %cmp1, i1 %cmp2
-  ret i1 %r
-}
-
 define i1 @icmp_ult_common(i1 %c, i6 %x, i6 %y, i6 %z) {
 ; CHECK-LABEL: @icmp_ult_common(
 ; CHECK-NEXT:    [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

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

I don't think #122723 is problematic per-se, but I think it exposed an underlying problem: I need more time to investigate. However, I think the revert is okay while we investigate the problem, but would defer to @nikic on the final approval.

@goldsteinn
Copy link
Contributor

Unless the fix is obvious / immediate, should revert.

Copy link
Contributor

@artagnon artagnon left a comment

Choose a reason for hiding this comment

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

Since one other person (@goldsteinn) has verified, this should land without delay.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@alexfh alexfh merged commit 7883184 into main Jan 24, 2025
11 checks passed
@alexfh alexfh deleted the revert-122723-ic-samesign-getmatching branch January 24, 2025 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants