Skip to content

Commit eb8cac3

Browse files
author
Adar Dagan
committed
Answer comments
1 parent 6fa9358 commit eb8cac3

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -708,17 +708,15 @@ 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()->getScalarType() ==
712-
Shuf->getOperand(0)->getType()->getScalarType()) {
711+
ElementCount::isKnownGE(Shuf->getType()->getElementCount(),
712+
cast<VectorType>(Shuf->getOperand(0)->getType())
713+
->getElementCount())) {
713714
// trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask
714715
// trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask
715-
auto *const NewTruncTy = VectorType::get(
716-
Trunc.getType()->getScalarType(),
717-
cast<VectorType>(Shuf->getOperand(0)->getType())->getElementCount());
718-
Value *NarrowOp =
719-
Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy, Trunc.getName());
720-
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask(),
721-
Shuf->getName());
716+
Type *const NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType(
717+
Trunc.getType()->getScalarType());
718+
Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy);
719+
return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask());
722720
}
723721

724722
return nullptr;

llvm/test/Transforms/InstCombine/trunc.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,19 @@ define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
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)