Skip to content

Commit a403ad9

Browse files
committed
[VectorCombine] foldBitcastShuffle - limit bitcast(shuffle(x,y)) -> shuffle(bitcast(x),bitcast(y))
Only fold bitcast(shuffle(x,y)) -> shuffle(bitcast(x),bitcast(y)) if we won't actually increase the number of bitcasts (i.e. x or y is already bitcasted from the correct type).
1 parent e2d4823 commit a403ad9

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,18 @@ bool VectorCombine::foldBitcastShuffle(Instruction &I) {
713713
if (SrcTy->getPrimitiveSizeInBits() % DestEltSize != 0)
714714
return false;
715715

716+
bool IsUnary = isa<UndefValue>(V1);
717+
718+
// For binary shuffles, only fold bitcast(shuffle(X,Y))
719+
// if it won't increase the number of bitcasts.
720+
if (!IsUnary) {
721+
auto *BCTy0 = dyn_cast<FixedVectorType>(peekThroughBitcasts(V0)->getType());
722+
auto *BCTy1 = dyn_cast<FixedVectorType>(peekThroughBitcasts(V1)->getType());
723+
if (!(BCTy0 && BCTy0->getElementType() == DestTy->getElementType()) &&
724+
!(BCTy1 && BCTy1->getElementType() == DestTy->getElementType()))
725+
return false;
726+
}
727+
716728
SmallVector<int, 16> NewMask;
717729
if (DestEltSize <= SrcEltSize) {
718730
// The bitcast is from wide to narrow/equal elements. The shuffle mask can
@@ -736,7 +748,6 @@ bool VectorCombine::foldBitcastShuffle(Instruction &I) {
736748
FixedVectorType::get(DestTy->getScalarType(), NumSrcElts);
737749
auto *OldShuffleTy =
738750
FixedVectorType::get(SrcTy->getScalarType(), Mask.size());
739-
bool IsUnary = isa<UndefValue>(V1);
740751
unsigned NumOps = IsUnary ? 1 : 2;
741752

742753
// The new shuffle must not cost more than the old shuffle.

llvm/test/Transforms/VectorCombine/X86/shuffle.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,12 @@ define <8 x i32> @bitcast_shuf_one_bitcast(<4 x i32> %a0, <2 x i64> %a1) {
149149
ret <8 x i32> %r
150150
}
151151

152-
; TODO - Negative test - shuffle of 2 operands must not increase bitcasts
152+
; Negative test - shuffle of 2 operands must not increase bitcasts
153153

154154
define <8 x i32> @bitcast_shuf_too_many_bitcasts(<2 x i64> %a0, <2 x i64> %a1) {
155155
; CHECK-LABEL: @bitcast_shuf_too_many_bitcasts(
156-
; CHECK-NEXT: [[TMP1:%.*]] = bitcast <2 x i64> [[A0:%.*]] to <4 x i32>
157-
; CHECK-NEXT: [[TMP2:%.*]] = bitcast <2 x i64> [[A1:%.*]] to <4 x i32>
158-
; CHECK-NEXT: [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> [[TMP2]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
156+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x i64> [[A0:%.*]], <2 x i64> [[A1:%.*]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
157+
; CHECK-NEXT: [[R:%.*]] = bitcast <4 x i64> [[SHUF]] to <8 x i32>
159158
; CHECK-NEXT: ret <8 x i32> [[R]]
160159
;
161160
%shuf = shufflevector <2 x i64> %a0, <2 x i64> %a1, <4 x i32> <i32 0, i32 1, i32 2, i32 3>

0 commit comments

Comments
 (0)