Skip to content

Commit 1afb42b

Browse files
authored
[InstCombine] Let shrinkSplatShuffle act on vectors of different lengths (#148593)
shrinkSplatShuffle in InstCombine would only move truncs up through shuffles if those shuffles inputs had the exact same type as their output, this PR weakens this constraint to only requiring that the scalar type of the input and output match.
1 parent 5ad7ef6 commit 1afb42b

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,14 @@ static Instruction *shrinkSplatShuffle(TruncInst &Trunc,
708708
auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
709709
if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) &&
710710
all_equal(Shuf->getShuffleMask()) &&
711-
Shuf->getType() == Shuf->getOperand(0)->getType()) {
711+
ElementCount::isKnownGE(Shuf->getType()->getElementCount(),
712+
cast<VectorType>(Shuf->getOperand(0)->getType())
713+
->getElementCount())) {
712714
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask
713715
// trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask
714-
Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType());
716+
Type *NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType(
717+
Trunc.getType()->getScalarType());
718+
Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy);
715719
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask());
716720
}
717721

llvm/test/Transforms/InstCombine/trunc-inseltpoison.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,8 @@ define <3 x i31> @wide_splat3(<3 x i33> %x) {
959959

960960
define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
961961
; CHECK-LABEL: @wide_lengthening_splat(
962-
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> poison, <8 x i32> zeroinitializer
963-
; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8>
962+
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i16> [[V:%.*]] to <4 x i8>
963+
; CHECK-NEXT: [[TR:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> poison, <8 x i32> zeroinitializer
964964
; CHECK-NEXT: ret <8 x i8> [[TR]]
965965
;
966966
%shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer

llvm/test/Transforms/InstCombine/trunc.ll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,28 @@ define <3 x i31> @wide_splat3(<3 x i33> %x) {
960960

961961
define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
962962
; CHECK-LABEL: @wide_lengthening_splat(
963-
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> poison, <8 x i32> zeroinitializer
964-
; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8>
963+
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i16> [[V:%.*]] to <4 x i8>
964+
; CHECK-NEXT: [[TR:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> poison, <8 x i32> zeroinitializer
965965
; CHECK-NEXT: ret <8 x i8> [[TR]]
966966
;
967967
%shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer
968968
%tr = trunc <8 x i16> %shuf to <8 x i8>
969969
ret <8 x i8> %tr
970970
}
971971

972+
; This is a negative test, we expect the trunc to remain after the shuffle as it
973+
; might not be beneficial to preform trunc on a wider type
974+
define <4 x i8> @wide_shortening_splat(<8 x i16> %v) {
975+
; CHECK-LABEL: @wide_shortening_splat(
976+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <8 x i16> [[V:%.*]], <8 x i16> poison, <4 x i32> zeroinitializer
977+
; CHECK-NEXT: [[TR:%.*]] = trunc <4 x i16> [[SHUF]] to <4 x i8>
978+
; CHECK-NEXT: ret <4 x i8> [[TR]]
979+
;
980+
%shuf = shufflevector <8 x i16> %v, <8 x i16> %v, <4 x i32> zeroinitializer
981+
%tr = trunc <4 x i16> %shuf to <4 x i8>
982+
ret <4 x i8> %tr
983+
}
984+
972985
define <2 x i8> @narrow_add_vec_constant(<2 x i32> %x) {
973986
; CHECK-LABEL: @narrow_add_vec_constant(
974987
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8>

0 commit comments

Comments
 (0)