[llvm] [InstCombine] Fold patterns which uses <2N x iM> type for comparisons on <N x i2M> vector types (PR #184328)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 06:23:16 PDT 2026


================
@@ -1292,6 +1292,217 @@ Value *InstCombinerImpl::foldUsingDistributiveLaws(BinaryOperator &I) {
   return SimplifySelectsFeedingBinaryOp(I, LHS, RHS);
 }
 
+// Folds patterns which uses comparisons on <2N x iM> type for a <N x i2M>
+// equality comparison.
+//
+// (A1, ..., AN) -> (A1Lower, A1Upper, ..., ANLower, ANUpper)
+// (B1, ..., BN) -> (B1Lower, B1Upper, ..., BNLower, BNUpper)
+// (Result1, ..., ResultN) -> (Result1, Result1, ..., ResultN, ResultN)
+//
+// where,
+//
+// ResultX = EqLowerX & EqUpperX
+// EqLowerX = AXLower == BXLower
+// EqUpperX = AXUpper == BXUpper
+//
+// Bitwise AND between the upper and lower parts can be achived by performing
+// the operation between the original and shuffled equality vector.
+static Value *foldVecCmpEqOnHalfElementSize(Instruction &I,
+                                            FixedVectorType *ResultVecType,
+                                            InstCombiner::BuilderTy &Builder) {
+  // Check pattern existance
+  Value *L, *R;
+  CmpPredicate Pred;
+  ArrayRef<int> Mask;
+
+  auto Equal = m_SExtOrSelf(m_ICmp(Pred, m_Value(L), m_Value(R)));
+  auto Shuffle = m_SExtOrSelf(m_Shuffle(Equal, m_Poison(), m_Mask(Mask)));
+  auto And = m_SExtOrSelf(m_c_And(Equal, Shuffle));
+  auto Select = m_SExtOrSelf(m_Select(Equal, Shuffle, m_Zero()));
+
+  // We can only treat "select" instruction as "logical and" if we can guarantee
+  // the input is not poisoned. Since, "logical and" may allow poison to
+  // propagate in cases where "select" instruction do not.
+  if (!match(&I, And) &&
+      !(match(&I, Select) && isGuaranteedNotToBeUndefOrPoison(L) &&
+        isGuaranteedNotToBeUndefOrPoison(R)))
+    return nullptr;
+
+  if (Pred != CmpInst::ICMP_EQ)
+    return nullptr;
+
+  auto *OldVecType = cast<FixedVectorType>(L->getType());
+
+  if (OldVecType != ResultVecType)
+    return nullptr;
+
+  // Example shuffle mask: {1, 0, 3, 2}
+  for (int I = 0; I < static_cast<int>(Mask.size()); I += 2)
----------------
RKSimon wrote:

```suggestion
  for (int I = 0, E = Mask.size(); I != E; I += 2)
```

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


More information about the llvm-commits mailing list