Skip to content

Commit 2a3cc4d

Browse files
committed
[Analysis] add utility function for unary shuffle mask creation
This is NFC-intended for the callers. Posting in case there are other potential users that I missed. I would also use this from VectorCombine in a patch for: https://llvm.org/PR52178 ( D111901 ) Differential Revision: https://reviews.llvm.org/D111891
1 parent 72d04d7 commit 2a3cc4d

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

llvm/include/llvm/Analysis/VectorUtils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ llvm::SmallVector<int, 16> createStrideMask(unsigned Start, unsigned Stride,
533533
llvm::SmallVector<int, 16>
534534
createSequentialMask(unsigned Start, unsigned NumInts, unsigned NumUndefs);
535535

536+
/// Given a shuffle mask for a binary shuffle, create the equivalent shuffle
537+
/// mask assuming both operands are identical. This assumes that the unary
538+
/// shuffle will use elements from operand 0 (operand 1 will be unused).
539+
llvm::SmallVector<int, 16> createUnaryMask(ArrayRef<int> Mask,
540+
unsigned NumElts);
541+
536542
/// Concatenate a list of vectors.
537543
///
538544
/// This function generates code that concatenate the vectors in \p Vecs into a

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,23 @@ llvm::SmallVector<int, 16> llvm::createSequentialMask(unsigned Start,
830830
return Mask;
831831
}
832832

833+
llvm::SmallVector<int, 16> llvm::createUnaryMask(ArrayRef<int> Mask,
834+
unsigned NumElts) {
835+
// Avoid casts in the loop and make sure we have a reasonable number.
836+
int NumEltsSigned = NumElts;
837+
assert(NumEltsSigned > 0 && "Expected smaller or non-zero element count");
838+
839+
// If the mask chooses an element from operand 1, reduce it to choose from the
840+
// corresponding element of operand 0. Undef mask elements are unchanged.
841+
SmallVector<int, 16> UnaryMask;
842+
for (int MaskElt : Mask) {
843+
assert((MaskElt < NumEltsSigned * 2) && "Expected valid shuffle mask");
844+
int UnaryElt = MaskElt >= NumEltsSigned ? MaskElt - NumEltsSigned : MaskElt;
845+
UnaryMask.push_back(UnaryElt);
846+
}
847+
return UnaryMask;
848+
}
849+
833850
/// A helper function for concatenating vectors. This function concatenates two
834851
/// vectors having the same element type. If the second vector has fewer
835852
/// elements than the first, it is padded with undefs.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21254,15 +21254,9 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
2125421254
ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
2125521255

2125621256
// Canonicalize shuffle v, v -> v, undef
21257-
if (N0 == N1) {
21258-
SmallVector<int, 8> NewMask;
21259-
for (unsigned i = 0; i != NumElts; ++i) {
21260-
int Idx = SVN->getMaskElt(i);
21261-
if (Idx >= (int)NumElts) Idx -= NumElts;
21262-
NewMask.push_back(Idx);
21263-
}
21264-
return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT), NewMask);
21265-
}
21257+
if (N0 == N1)
21258+
return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
21259+
createUnaryMask(SVN->getMask(), NumElts));
2126621260

2126721261
// Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2126821262
if (N0.isUndef())

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,16 +2484,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
24842484
if (LHS == RHS) {
24852485
assert(!match(RHS, m_Undef()) &&
24862486
"Shuffle with 2 undef ops not simplified?");
2487-
// Remap any references to RHS to use LHS.
2488-
SmallVector<int, 16> Elts;
2489-
for (unsigned i = 0; i != VWidth; ++i) {
2490-
// Propagate undef elements or force mask to LHS.
2491-
if (Mask[i] < 0)
2492-
Elts.push_back(UndefMaskElem);
2493-
else
2494-
Elts.push_back(Mask[i] % LHSWidth);
2495-
}
2496-
return new ShuffleVectorInst(LHS, Elts);
2487+
return new ShuffleVectorInst(LHS, createUnaryMask(Mask, LHSWidth));
24972488
}
24982489

24992490
// shuffle undef, x, mask --> shuffle x, undef, mask'

0 commit comments

Comments
 (0)