diff --git a/llvm/lib/Target/X86/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/X86LegalizerInfo.cpp --- a/llvm/lib/Target/X86/X86LegalizerInfo.cpp +++ b/llvm/lib/Target/X86/X86LegalizerInfo.cpp @@ -75,6 +75,7 @@ const LLT s16 = LLT::scalar(16); const LLT s32 = LLT::scalar(32); const LLT s64 = LLT::scalar(64); + const LLT maxScalar = Subtarget.is64Bit() ? s64 : s32; getActionDefinitionsBuilder(G_INTRINSIC_ROUNDEVEN) .scalarize(0) @@ -97,6 +98,12 @@ getActionDefinitionsBuilder({G_MEMCPY, G_MEMMOVE, G_MEMSET}).libcall(); + getActionDefinitionsBuilder(G_BSWAP) + .legalIf([=](const LegalityQuery &Query) { + return Query.Types[0] == s32 || (Subtarget.is64Bit() && Query.Types[0] == s64); + }) + .widenScalarToNextPow2(0, /*Min=*/32) + .clampScalar(0, s32, maxScalar); if (Subtarget.is64Bit()) { if (Subtarget.hasPOPCNT()) { diff --git a/llvm/test/CodeGen/X86/GlobalISel/legalize-bswap.mir b/llvm/test/CodeGen/X86/GlobalISel/legalize-bswap.mir new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/X86/GlobalISel/legalize-bswap.mir @@ -0,0 +1,106 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2 +# RUN: llc -mtriple=i386-linux-gnu -run-pass=legalizer %s -o - | FileCheck %s --check-prefix=X86-32 +# RUN: llc -mtriple=x86_64-linux-gnu -run-pass=legalizer %s -o - | FileCheck %s --check-prefix=X86-64 + +# test bswap for s16, s17, s32, and s64 + +... +--- +name: test_bswap17 +body: | + bb.1: + ; X86-32-LABEL: name: test_bswap17 + ; X86-32: [[DEF:%[0-9]+]]:_(s17) = IMPLICIT_DEF + ; X86-32-NEXT: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[DEF]](s17) + ; X86-32-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[ANYEXT]] + ; X86-32-NEXT: [[C:%[0-9]+]]:_(s8) = G_CONSTANT i8 15 + ; X86-32-NEXT: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[BSWAP]], [[C]](s8) + ; X86-32-NEXT: [[TRUNC:%[0-9]+]]:_(s17) = G_TRUNC [[LSHR]](s32) + ; X86-32-NEXT: [[COPY:%[0-9]+]]:_(s17) = COPY [[TRUNC]](s17) + ; X86-32-NEXT: RET 0, implicit [[COPY]](s17) + ; X86-64-LABEL: name: test_bswap17 + ; X86-64: [[DEF:%[0-9]+]]:_(s17) = IMPLICIT_DEF + ; X86-64-NEXT: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[DEF]](s17) + ; X86-64-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[ANYEXT]] + ; X86-64-NEXT: [[C:%[0-9]+]]:_(s8) = G_CONSTANT i8 15 + ; X86-64-NEXT: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[BSWAP]], [[C]](s8) + ; X86-64-NEXT: [[TRUNC:%[0-9]+]]:_(s17) = G_TRUNC [[LSHR]](s32) + ; X86-64-NEXT: [[COPY:%[0-9]+]]:_(s17) = COPY [[TRUNC]](s17) + ; X86-64-NEXT: RET 0, implicit [[COPY]](s17) + %0:_(s17) = IMPLICIT_DEF + %1:_(s17) = G_BSWAP %0 + %2:_(s17) = COPY %1(s17) + RET 0, implicit %2 + +... +--- +name: test_bswap64 +body: | + bb.1: + ; X86-32-LABEL: name: test_bswap64 + ; X86-32: [[DEF:%[0-9]+]]:_(s64) = IMPLICIT_DEF + ; X86-32-NEXT: [[UV:%[0-9]+]]:_(s32), [[UV1:%[0-9]+]]:_(s32) = G_UNMERGE_VALUES [[DEF]](s64) + ; X86-32-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[UV1]] + ; X86-32-NEXT: [[BSWAP1:%[0-9]+]]:_(s32) = G_BSWAP [[UV]] + ; X86-32-NEXT: [[MV:%[0-9]+]]:_(s64) = G_MERGE_VALUES [[BSWAP]](s32), [[BSWAP1]](s32) + ; X86-32-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY [[MV]](s64) + ; X86-32-NEXT: RET 0, implicit [[COPY]](s64) + ; X86-64-LABEL: name: test_bswap64 + ; X86-64: [[DEF:%[0-9]+]]:_(s64) = IMPLICIT_DEF + ; X86-64-NEXT: [[BSWAP:%[0-9]+]]:_(s64) = G_BSWAP [[DEF]] + ; X86-64-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY [[BSWAP]](s64) + ; X86-64-NEXT: RET 0, implicit [[COPY]](s64) + %0:_(s64) = IMPLICIT_DEF + %1:_(s64) = G_BSWAP %0 + %2:_(s64) = COPY %1(s64) + RET 0, implicit %2 + +... +--- +name: test_bswap32 +body: | + bb.1: + ; X86-32-LABEL: name: test_bswap32 + ; X86-32: [[DEF:%[0-9]+]]:_(s32) = IMPLICIT_DEF + ; X86-32-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[DEF]] + ; X86-32-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY [[BSWAP]](s32) + ; X86-32-NEXT: RET 0, implicit [[COPY]](s32) + ; X86-64-LABEL: name: test_bswap32 + ; X86-64: [[DEF:%[0-9]+]]:_(s32) = IMPLICIT_DEF + ; X86-64-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[DEF]] + ; X86-64-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY [[BSWAP]](s32) + ; X86-64-NEXT: RET 0, implicit [[COPY]](s32) + %0:_(s32) = IMPLICIT_DEF + %1:_(s32) = G_BSWAP %0 + %2:_(s32) = COPY %1(s32) + RET 0, implicit %2 + +... +--- +name: test_bswap16 +body: | + bb.1: + ; X86-32-LABEL: name: test_bswap16 + ; X86-32: [[DEF:%[0-9]+]]:_(s16) = IMPLICIT_DEF + ; X86-32-NEXT: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[DEF]](s16) + ; X86-32-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[ANYEXT]] + ; X86-32-NEXT: [[C:%[0-9]+]]:_(s8) = G_CONSTANT i8 16 + ; X86-32-NEXT: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[BSWAP]], [[C]](s8) + ; X86-32-NEXT: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[LSHR]](s32) + ; X86-32-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY [[TRUNC]](s16) + ; X86-32-NEXT: RET 0, implicit [[COPY]](s16) + ; X86-64-LABEL: name: test_bswap16 + ; X86-64: [[DEF:%[0-9]+]]:_(s16) = IMPLICIT_DEF + ; X86-64-NEXT: [[ANYEXT:%[0-9]+]]:_(s32) = G_ANYEXT [[DEF]](s16) + ; X86-64-NEXT: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[ANYEXT]] + ; X86-64-NEXT: [[C:%[0-9]+]]:_(s8) = G_CONSTANT i8 16 + ; X86-64-NEXT: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[BSWAP]], [[C]](s8) + ; X86-64-NEXT: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[LSHR]](s32) + ; X86-64-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY [[TRUNC]](s16) + ; X86-64-NEXT: RET 0, implicit [[COPY]](s16) + %0:_(s16) = IMPLICIT_DEF + %1:_(s16) = G_BSWAP %0 + %2:_(s16) = COPY %1(s16) + RET 0, implicit %2 + +...