[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
Tue Sep 17 07:57:47 PDT 2024


================
@@ -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))) ||
----------------
goldsteinn wrote:

This should look something like:
```
if(!match(XEq, m_SpecificICmp(ICMP_EQ, m_Value(X), m_Value(Z))
  return nullptr;
if(!match(m_c_SpecificICmp(ICMP_EQ, m_Value(Y), m_Specific(Z)))
  std::swap(X, Z);
if(!match(m_c_SpecificICmp(ICMP_EQ, m_Value(Y), m_Specific(Z)))
  return nullptr;
```

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


More information about the llvm-commits mailing list