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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri May 17 01:17:18 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;
----------------
david-arm wrote:

Hmm, this is not a NFC change. I am not quite sure how to use `getShuffleDemandedElts`, but I'm pretty sure your example above now permits use of a RHS whereas previously it didn't (see `!match(Arg, m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask)))`). I would prefer to limit this to refactoring only as it's not relevant to this patch. I can keep the `if(DemandedLHS.isAllOnes() ...` check.

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


More information about the llvm-commits mailing list