From: Sanjay Patel Date: Mon, 2 Sep 2019 13:33:20 +0000 (+0000) Subject: [InstCombine] recognize bswap disguised as shufflevector X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fdd20c5ebfdcdf3fd0ae820f719088f56b640a6e;p=llvm [InstCombine] recognize bswap disguised as shufflevector bitcast (shuf X, undef, ) to i{N*8} --> bswap (bitcast X to i{N*8}) In PR43146: https://bugs.llvm.org/show_bug.cgi?id=43146 ...we have a more complicated case where SLP is making a mess of bswap. This patch won't do anything for that currently, but we need to improve bswap recognition in instcombine, SLP, and/or a standalone pass to avoid that problem. This is limited using the data-layout so we don't try to do this transform with actual vector types. The backend does not appear to have folds to convert in either direction, so we don't want to mess up something that is actually better lowered as a shuffle. On x86, we're trading something like this: vmovd %edi, %xmm0 vpshufb LCPI0_0(%rip), %xmm0, %xmm0 ## xmm0 = xmm0[3,2,1,0,u,u,u,u,u,u,u,u,u,u,u,u] vmovd %xmm0, %eax For: movl %edi, %eax bswapl %eax Differential Revision: https://reviews.llvm.org/D66965 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@370659 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index 4be44dac4ca..1a569d2638f 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -2416,6 +2416,22 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { return new ShuffleVectorInst(LHS, RHS, Shuf->getOperand(2)); } } + + // A bitcasted-to-scalar and byte-reversing shuffle is better recognized as + // a byte-swap: + // bitcast (shuf X, undef, ) --> bswap (bitcast X) + // TODO: We should match the related pattern for bitreverse. + if (DestTy->isIntegerTy() && + DL.isLegalInteger(DestTy->getScalarSizeInBits()) && + SrcTy->getScalarSizeInBits() == 8 && NumShufElts % 2 == 0 && + Shuf->hasOneUse() && Shuf->isReverse()) { + assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask"); + assert(isa(ShufOp1) && "Unexpected shuffle op"); + Function *Bswap = + Intrinsic::getDeclaration(CI.getModule(), Intrinsic::bswap, DestTy); + Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy); + return IntrinsicInst::Create(Bswap, { ScalarX }); + } } // Handle the A->B->A cast, and there is an intervening PHI node. diff --git a/test/Transforms/InstCombine/bswap.ll b/test/Transforms/InstCombine/bswap.ll index 9d5d499e643..2d8a69b86a5 100644 --- a/test/Transforms/InstCombine/bswap.ll +++ b/test/Transforms/InstCombine/bswap.ll @@ -233,8 +233,8 @@ define i16 @test10(i32 %a) { define i32 @shuf_4bytes(<4 x i8> %x) { ; CHECK-LABEL: @shuf_4bytes( -; CHECK-NEXT: [[BSWAP:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> undef, <4 x i32> -; CHECK-NEXT: [[CAST:%.*]] = bitcast <4 x i8> [[BSWAP]] to i32 +; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i8> [[X:%.*]] to i32 +; CHECK-NEXT: [[CAST:%.*]] = call i32 @llvm.bswap.i32(i32 [[TMP1]]) ; CHECK-NEXT: ret i32 [[CAST]] ; %bswap = shufflevector <4 x i8> %x, <4 x i8> undef, <4 x i32> @@ -244,9 +244,9 @@ define i32 @shuf_4bytes(<4 x i8> %x) { define i32 @shuf_load_4bytes(<4 x i8>* %p) { ; CHECK-LABEL: @shuf_load_4bytes( -; CHECK-NEXT: [[X:%.*]] = load <4 x i8>, <4 x i8>* [[P:%.*]], align 4 -; CHECK-NEXT: [[BSWAP:%.*]] = shufflevector <4 x i8> [[X]], <4 x i8> undef, <4 x i32> -; CHECK-NEXT: [[CAST:%.*]] = bitcast <4 x i8> [[BSWAP]] to i32 +; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i8>* [[P:%.*]] to i32* +; CHECK-NEXT: [[X1:%.*]] = load i32, i32* [[TMP1]], align 4 +; CHECK-NEXT: [[CAST:%.*]] = call i32 @llvm.bswap.i32(i32 [[X1]]) ; CHECK-NEXT: ret i32 [[CAST]] ; %x = load <4 x i8>, <4 x i8>* %p @@ -257,9 +257,7 @@ define i32 @shuf_load_4bytes(<4 x i8>* %p) { define i32 @shuf_bitcast_twice_4bytes(i32 %x) { ; CHECK-LABEL: @shuf_bitcast_twice_4bytes( -; CHECK-NEXT: [[CAST1:%.*]] = bitcast i32 [[X:%.*]] to <4 x i8> -; CHECK-NEXT: [[BSWAP:%.*]] = shufflevector <4 x i8> [[CAST1]], <4 x i8> undef, <4 x i32> -; CHECK-NEXT: [[CAST2:%.*]] = bitcast <4 x i8> [[BSWAP]] to i32 +; CHECK-NEXT: [[CAST2:%.*]] = call i32 @llvm.bswap.i32(i32 [[X:%.*]]) ; CHECK-NEXT: ret i32 [[CAST2]] ; %cast1 = bitcast i32 %x to <4 x i8> @@ -268,6 +266,7 @@ define i32 @shuf_bitcast_twice_4bytes(i32 %x) { ret i32 %cast2 } +; Negative test - extra use declare void @use(<4 x i8>) define i32 @shuf_4bytes_extra_use(<4 x i8> %x) { @@ -283,6 +282,8 @@ define i32 @shuf_4bytes_extra_use(<4 x i8> %x) { ret i32 %cast } +; Negative test - scalar type is not in the data layout + define i128 @shuf_16bytes(<16 x i8> %x) { ; CHECK-LABEL: @shuf_16bytes( ; CHECK-NEXT: [[BSWAP:%.*]] = shufflevector <16 x i8> [[X:%.*]], <16 x i8> undef, <16 x i32> @@ -294,6 +295,8 @@ define i128 @shuf_16bytes(<16 x i8> %x) { ret i128 %cast } +; Negative test - don't touch widening shuffles (for now) + define i32 @shuf_2bytes_widening(<2 x i8> %x) { ; CHECK-LABEL: @shuf_2bytes_widening( ; CHECK-NEXT: [[BSWAP:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> undef, <4 x i32>