[llvm] [InstCombine] Fold `(X==Z || Y==Z) ? (X==Z && Y==Z) : X==Y --> X==Y` (PR #108619)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 13 11:10:44 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Marina Taylor (citymarina)

<details>
<summary>Changes</summary>

This logic was seen in code generated by Swift for comparing optional pointers,
with `Z` being the constant `0`.

https://alive2.llvm.org/ce/z/Bpd8Yo

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


3 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineInternal.h (+1) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+45) 
- (added) llvm/test/Transforms/InstCombine/icmp-equality-test.ll (+202) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index a051a568bfd62e..9bd06a0abb5e53 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -734,6 +734,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Instruction *foldSelectOfBools(SelectInst &SI);
   Instruction *foldSelectToCmp(SelectInst &SI);
   Instruction *foldSelectExtConst(SelectInst &Sel);
+  Instruction *foldSelectEqualityTest(SelectInst &SI);
   Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
   Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
   Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 66f7c4592457c2..0020054c775e8e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1406,6 +1406,46 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
   return nullptr;
 }
 
+/// Fold the following code sequence:
+/// \code
+///   %XEq = icmp eq i64 %X, %Z
+///   %YEq = icmp eq i64 %Y, %Z
+///   %either = select i1 %XEq, i1 true, i1 %YEq
+///   %both = select i1 %XEq, i1 %YEq, i1 false
+///   %cmp = icmp eq i64 %X, %Y
+///   %equal = select i1 %either, i1 %both, i1 %cmp
+/// \code
+///
+/// into:
+///   %equal = icmp eq i64 %X, %Y
+///
+/// Equivalently:
+///   (X==Z || Y==Z) ? (X==Z && Y==Z) : X==Y --> X==Y
+Instruction *InstCombinerImpl::foldSelectEqualityTest(SelectInst &Sel) {
+  Value *X, *Y, *Z, *XEq, *YEq;
+  Value *Either = Sel.getCondition(), *Both = Sel.getTrueValue(),
+        *Cmp = Sel.getFalseValue();
+
+  if (!match(Either, m_Select(m_Value(XEq), m_One(), m_Value(YEq))))
+    return nullptr;
+
+  if (!match(XEq,
+             m_c_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(X), m_Value(Z))) ||
+      !match(YEq,
+             m_c_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(Y), m_Specific(Z))))
+    return nullptr;
+
+  if (!match(Both, m_Select(m_Specific(XEq), m_Specific(YEq), m_Zero())) &&
+      !match(Both, m_Select(m_Specific(YEq), m_Specific(XEq), m_Zero())))
+    return nullptr;
+
+  if (!match(Cmp,
+             m_c_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(X), m_Specific(Y))))
+    return nullptr;
+
+  return replaceInstUsesWith(Sel, Cmp);
+}
+
 // See if this is a pattern like:
 //   %old_cmp1 = icmp slt i32 %x, C2
 //   %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
@@ -4068,6 +4108,11 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
   if (Instruction *I = foldSelectOfSymmetricSelect(SI, Builder))
     return I;
 
+  // This needs to happen before foldNestedSelects, as that could break the
+  // patterns that we test for.
+  if (Instruction *I = foldSelectEqualityTest(SI))
+    return I;
+
   if (Instruction *I = foldNestedSelects(SI, Builder))
     return I;
 
diff --git a/llvm/test/Transforms/InstCombine/icmp-equality-test.ll b/llvm/test/Transforms/InstCombine/icmp-equality-test.ll
new file mode 100644
index 00000000000000..1ee2db4cb318b6
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-equality-test.ll
@@ -0,0 +1,202 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @icmp_equality_test(i64 %X, i64 %Y, i64 %Z) {
+; CHECK-LABEL: @icmp_equality_test(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %XEq = icmp eq i64 %X, %Z
+  %YEq = icmp eq i64 %Y, %Z
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_constant(i42 %X, i42 %Y) {
+; CHECK-LABEL: @icmp_equality_test_constant(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i42 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %XEq = icmp eq i42 %X, -42
+  %YEq = icmp eq i42 %Y, -42
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i42 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define <2 x i1> @icmp_equality_test_vector(<2 x i64> %X, <2 x i64> %Y) {
+; CHECK-LABEL: @icmp_equality_test_vector(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i64> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+entry:
+  %XEq = icmp eq <2 x i64> %X, <i64 123, i64 456>
+  %YEq = icmp eq <2 x i64> %Y, <i64 123, i64 456>
+  %either = select <2 x i1> %XEq, <2 x i1> <i1 true, i1 true>, <2 x i1> %YEq
+  %both = select <2 x i1> %XEq, <2 x i1> %YEq, <2 x i1> <i1 false, i1 false>
+  %cmp = icmp eq <2 x i64> %X, %Y
+  %equal = select <2 x i1> %either, <2 x i1> %both, <2 x i1> %cmp
+  ret <2 x i1> %equal
+}
+
+define i1 @icmp_equality_test_commute_icmp(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_commute_icmp(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %XEq = icmp eq i64 0, %X
+  %YEq = icmp eq i64 0, %Y
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %Y, %X
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_commute_select1(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_commute_select1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 0
+  %either = select i1 %YEq, i1 true, i1 %XEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_commute_select2(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_commute_select2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 0
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %YEq, i1 %XEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+; Negative tests below
+
+define i1 @icmp_equality_test_wrong_constant(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_wrong_constant(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[XEQ:%.*]] = icmp eq i64 [[X:%.*]], 0
+; CHECK-NEXT:    [[YEQ:%.*]] = icmp eq i64 [[Y:%.*]], 999
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X]], [[Y]]
+; CHECK-NEXT:    [[NOT_YEQ:%.*]] = xor i1 [[YEQ]], true
+; CHECK-NEXT:    [[BOTH:%.*]] = select i1 [[NOT_YEQ]], i1 [[CMP]], i1 false
+; CHECK-NEXT:    [[EQUAL:%.*]] = select i1 [[XEQ]], i1 [[YEQ]], i1 [[BOTH]]
+; CHECK-NEXT:    ret i1 [[EQUAL]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 999
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_wrong_either(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_wrong_either(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[XEQ_NOT:%.*]] = icmp eq i64 [[X:%.*]], 0
+; CHECK-NEXT:    [[YEQ:%.*]] = icmp eq i64 [[Y:%.*]], 999
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[Y]], 0
+; CHECK-NEXT:    [[BOTH:%.*]] = or i1 [[YEQ]], [[CMP]]
+; CHECK-NEXT:    [[EQUAL:%.*]] = select i1 [[XEQ_NOT]], i1 [[BOTH]], i1 false
+; CHECK-NEXT:    ret i1 [[EQUAL]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 999
+  %either = select i1 %XEq, i1 %YEq, i1 true
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_wrong_both(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_wrong_both(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[XEQ:%.*]] = icmp ne i64 [[X:%.*]], 0
+; CHECK-NEXT:    [[YEQ:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X]], [[Y]]
+; CHECK-NEXT:    [[BOTH:%.*]] = select i1 [[YEQ]], i1 true, i1 [[CMP]]
+; CHECK-NEXT:    [[EQUAL:%.*]] = select i1 [[XEQ]], i1 [[BOTH]], i1 false
+; CHECK-NEXT:    ret i1 [[EQUAL]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 0
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 false, i1 %YEq
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_wrong_cmp(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_wrong_cmp(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[XEQ:%.*]] = icmp eq i64 [[X:%.*]], 0
+; CHECK-NEXT:    [[YEQ:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X]], 999
+; CHECK-NEXT:    [[NOT_YEQ:%.*]] = xor i1 [[YEQ]], true
+; CHECK-NEXT:    [[BOTH:%.*]] = select i1 [[NOT_YEQ]], i1 [[CMP]], i1 false
+; CHECK-NEXT:    [[EQUAL:%.*]] = select i1 [[XEQ]], i1 [[YEQ]], i1 [[BOTH]]
+; CHECK-NEXT:    ret i1 [[EQUAL]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 0
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, 999
+  %equal = select i1 %either, i1 %both, i1 %cmp
+  ret i1 %equal
+}
+
+define i1 @icmp_equality_test_wrong_equal(i64 %X, i64 %Y) {
+; CHECK-LABEL: @icmp_equality_test_wrong_equal(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[XEQ:%.*]] = icmp eq i64 [[X:%.*]], 0
+; CHECK-NEXT:    [[YEQ:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT:    [[EITHER:%.*]] = select i1 [[XEQ]], i1 true, i1 [[YEQ]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[X]], [[Y]]
+; CHECK-NEXT:    [[EQUAL:%.*]] = select i1 [[EITHER]], i1 [[CMP]], i1 false
+; CHECK-NEXT:    ret i1 [[EQUAL]]
+;
+entry:
+  %XEq = icmp eq i64 %X, 0
+  %YEq = icmp eq i64 %Y, 0
+  %either = select i1 %XEq, i1 true, i1 %YEq
+  %both = select i1 %XEq, i1 %YEq, i1 false
+  %cmp = icmp eq i64 %X, %Y
+  %equal = select i1 %either, i1 %cmp, i1 %both
+  ret i1 %equal
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/108619


More information about the llvm-commits mailing list