[llvm] [InstCombine] Fold vector.reduce.op(vector.reverse(X)) -> vector.reduce.op(X) (PR #91743)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 09:40:45 PDT 2024


================
@@ -1435,6 +1435,33 @@ static Instruction *foldBitOrderCrossLogicOp(Value *V,
   return nullptr;
 }
 
+static Value *simplifyReductionOperand(Value *Arg, bool CanReorderLanes) {
+  if (!CanReorderLanes)
+    return nullptr;
+
+  Value *V;
+  if (match(Arg, m_VecReverse(m_Value(V))))
+    return V;
+
+  ArrayRef<int> Mask;
+  if (!isa<FixedVectorType>(Arg->getType()) ||
+      !match(Arg, m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask))) ||
+      !cast<ShuffleVectorInst>(Arg)->isSingleSource())
+    return nullptr;
+
+  int Sz = Mask.size();
+  SmallBitVector UsedIndices(Sz);
+  for (int Idx : Mask) {
+    if (Idx == PoisonMaskElem || UsedIndices.test(Idx))
+      return nullptr;
+    UsedIndices.set(Idx);
+  }
+
+  // Can remove shuffle iff just shuffled elements, no repeats, undefs, or
+  // other changes.
+  return UsedIndices.all() ? V : nullptr;
----------------
goldsteinn wrote:

Think the shuffle check could be simplified with `getShuffleDemandedElts`.

Then the check would just be:
```
if(DemandedLHS.isAllOnes() && DemandedRHS.isZero()) {
  return LHS;
}
if(DemandedRHS.isAllOnes() && DemandedLHS.isZero()) {
  return RHS;
}
return nullptr;
```

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


More information about the llvm-commits mailing list