From 8b2302f3d11be5cf951261612f3c74ef8d5c9cd1 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 7 Mar 2017 16:10:36 +0000 Subject: [PATCH] [InstCombine] shrink truncated splat shuffle This is one part of solving a recent bug report: http://lists.llvm.org/pipermail/llvm-dev/2017-February/110293.html This keeps with our general approach: changing arbitrary shuffles is off-limts, but changing splat is ok. The transform is very similar to the existing shrinkBitwiseLogic() canonicalization. Differential Revision: https://reviews.llvm.org/D30123 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297155 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineCasts.cpp | 20 ++++++++++++++++++++ test/Transforms/InstCombine/trunc.ll | 10 ++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index e60687fa7ad..a1ac64dda35 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -463,6 +463,23 @@ Instruction *InstCombiner::shrinkBitwiseLogic(TruncInst &Trunc) { return BinaryOperator::Create(LogicOp->getOpcode(), NarrowOp0, NarrowC); } +/// Try to narrow the width of a splat shuffle. This could be generalized to any +/// shuffle with a constant operand, but we limit the transform to avoid +/// creating a shuffle type that targets may not be able to lower effectively. +static Instruction *shrinkSplatShuffle(TruncInst &Trunc, + InstCombiner::BuilderTy &Builder) { + auto *Shuf = dyn_cast(Trunc.getOperand(0)); + if (Shuf && Shuf->hasOneUse() && isa(Shuf->getOperand(1)) && + Shuf->getMask()->getSplatValue()) { + // trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Undef, SplatMask + Constant *NarrowUndef = UndefValue::get(Trunc.getType()); + Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType()); + return new ShuffleVectorInst(NarrowOp, NarrowUndef, Shuf->getMask()); + } + + return nullptr; +} + Instruction *InstCombiner::visitTrunc(TruncInst &CI) { if (Instruction *Result = commonCastTransforms(CI)) return Result; @@ -554,6 +571,9 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) { if (Instruction *I = shrinkBitwiseLogic(CI)) return I; + if (Instruction *I = shrinkSplatShuffle(CI, *Builder)) + return I; + if (Src->hasOneUse() && isa(SrcTy) && shouldChangeType(SrcTy, DestTy)) { // Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the diff --git a/test/Transforms/InstCombine/trunc.ll b/test/Transforms/InstCombine/trunc.ll index 581e990df0f..ba72908d3c4 100644 --- a/test/Transforms/InstCombine/trunc.ll +++ b/test/Transforms/InstCombine/trunc.ll @@ -478,13 +478,12 @@ define <4 x i8> @wide_shuf(<4 x i32> %x) { ret <4 x i8> %trunc } -; FIXME: ; trunc (shuffle X, undef, SplatMask) --> shuffle (trunc X), undef, SplatMask define <4 x i8> @wide_splat1(<4 x i32> %x) { ; CHECK-LABEL: @wide_splat1( -; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> -; CHECK-NEXT: [[TRUNC:%.*]] = trunc <4 x i32> [[SHUF]] to <4 x i8> +; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> %x to <4 x i8> +; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> undef, <4 x i32> ; CHECK-NEXT: ret <4 x i8> [[TRUNC]] ; %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> @@ -492,14 +491,13 @@ define <4 x i8> @wide_splat1(<4 x i32> %x) { ret <4 x i8> %trunc } -; FIXME: ; Test weird types. ; trunc (shuffle X, undef, SplatMask) --> shuffle (trunc X), undef, SplatMask define <3 x i31> @wide_splat2(<3 x i33> %x) { ; CHECK-LABEL: @wide_splat2( -; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <3 x i33> %x, <3 x i33> undef, <3 x i32> -; CHECK-NEXT: [[TRUNC:%.*]] = trunc <3 x i33> [[SHUF]] to <3 x i31> +; CHECK-NEXT: [[TMP1:%.*]] = trunc <3 x i33> %x to <3 x i31> +; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <3 x i31> [[TMP1]], <3 x i31> undef, <3 x i32> ; CHECK-NEXT: ret <3 x i31> [[TRUNC]] ; %shuf = shufflevector <3 x i33> %x, <3 x i33> undef, <3 x i32> -- 2.11.0