Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -926,6 +926,37 @@ return nullptr; } +/// Convert a table lookup to shufflevector if the mask is constant. +/// This could benefit tbl1 if the mask is { 7,6,5,4,3,2,1,0 }, in +/// which case we could lower the shufflevector with rev64 instructions +/// as it's actually a byte reverse. +static Value *simplifyTableLookup(const IntrinsicInst &II, + InstCombiner::BuilderTy &Builder) { + auto C = dyn_cast(II.getArgOperand(1)); + if (!C) + return nullptr; + + auto VecTy = cast(II.getType()); + auto EltTy = Type::getInt32Ty(II.getContext()); + unsigned NumElts = VecTy->getNumElements(); + assert((NumElts == 8) && "Unexpected number of elements in shuffle mask!"); + + Constant *Indexes[8] = {nullptr}; + for (unsigned I = 0; I < NumElts; ++I) { + Constant *COp = C->getAggregateElement(I); + if (!COp || !isa(COp)) + return nullptr; + + uint8_t Index = cast(COp)->getValue().getZExtValue(); + Indexes[I] = ConstantInt::get(EltTy, Index); + } + + auto ShuffleMask = ConstantVector::get(makeArrayRef(Indexes, NumElts)); + auto V1 = II.getArgOperand(0); + auto V2 = Constant::getNullValue(V1->getType()); + return Builder.CreateShuffleVector(V1, V2, ShuffleMask); +} + /// Attempt to convert pshufb* to shufflevector if the mask is constant. static Value *simplifyX86pshufb(const IntrinsicInst &II, InstCombiner::BuilderTy &Builder) { @@ -2995,6 +3026,12 @@ break; } + case Intrinsic::arm_neon_vtbl1: + case Intrinsic::aarch64_neon_tbl1: + if (Value *V = simplifyTableLookup(*II, Builder)) + return replaceInstUsesWith(*II, V); + break; + case Intrinsic::arm_neon_vmulls: case Intrinsic::arm_neon_vmullu: case Intrinsic::aarch64_neon_smull: Index: test/Transforms/InstCombine/AArch64/tbl1.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/AArch64/tbl1.ll @@ -0,0 +1,19 @@ +; RUN: opt -instcombine -S -o - %s | FileCheck %s + +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +target triple = "aarch64-arm-none-eabi" + +; Turning a table lookup intrinsic into a shuffle vector instruction +; can be beneficial. If the mask used for the lookup is the constant +; vector {7,6,5,4,3,2,1,0}, then the back-end generates rev64 +; instructions instead. + +define <8 x i8> @table_lookup(<16 x i8> %vec) { +entry: +;CHECK-NOT: call <8 x i8> @llvm.aarch64.neon.tbl1.v8i8 +;CHECK: shufflevector <16 x i8> %vec, <16 x i8> undef, <8 x i32> + %tbl1 = call <8 x i8> @llvm.aarch64.neon.tbl1.v8i8(<16 x i8> %vec, <8 x i8> ) + ret <8 x i8> %tbl1 +} + +declare <8 x i8> @llvm.aarch64.neon.tbl1.v8i8(<16 x i8>, <8 x i8>) Index: test/Transforms/InstCombine/ARM/tbl1.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/ARM/tbl1.ll @@ -0,0 +1,19 @@ +; RUN: opt -instcombine -S -o - %s | FileCheck %s + +target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "armv8-arm-none-eabi" + +; Turning a table lookup intrinsic into a shuffle vector instruction +; can be beneficial. If the mask used for the lookup is the constant +; vector {7,6,5,4,3,2,1,0}, then the back-end generates rev64 +; instructions instead. + +define <8 x i8> @table_lookup(<8 x i8> %vec) { +entry: +;CHECK-NOT: call <8 x i8> @llvm.arm.neon.vtbl1 +;CHECK: shufflevector <8 x i8> %vec, <8 x i8> undef, <8 x i32> + %vtbl1 = call <8 x i8> @llvm.arm.neon.vtbl1(<8 x i8> %vec, <8 x i8> ) + ret <8 x i8> %vtbl1 +} + +declare <8 x i8> @llvm.arm.neon.vtbl1(<8 x i8>, <8 x i8>)