Skip to content

Commit 323d08e

Browse files
committed
[InstCombine] Fix bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) vector support
Use getScalarSizeInBits not getPrimitiveSizeInBits to determine the shift value at the element level.
1 parent b85de2c commit 323d08e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
828828

829829
// bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
830830
if (match(IIOperand, m_Trunc(m_BSwap(m_Value(X))))) {
831-
unsigned C = X->getType()->getPrimitiveSizeInBits() -
832-
IIOperand->getType()->getPrimitiveSizeInBits();
831+
unsigned C = X->getType()->getScalarSizeInBits() -
832+
IIOperand->getType()->getScalarSizeInBits();
833833
Value *CV = ConstantInt::get(X->getType(), C);
834834
Value *V = Builder.CreateLShr(X, CV);
835835
return new TruncInst(V, IIOperand->getType());

llvm/test/Transforms/InstCombine/bswap-fold.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ define i16 @test7(i32 %A) {
3939

4040
define <2 x i16> @test7_vector(<2 x i32> %A) {
4141
; CHECK-LABEL: @test7_vector(
42-
; CHECK-NEXT: ret <2 x i16> undef
42+
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[A:%.*]], <i32 16, i32 16>
43+
; CHECK-NEXT: [[D:%.*]] = trunc <2 x i32> [[TMP1]] to <2 x i16>
44+
; CHECK-NEXT: ret <2 x i16> [[D]]
4345
;
4446
%B = tail call <2 x i32> @llvm.bswap.v2i32(<2 x i32> %A) nounwind
4547
%C = trunc <2 x i32> %B to <2 x i16>
@@ -61,7 +63,9 @@ define i16 @test8(i64 %A) {
6163

6264
define <2 x i16> @test8_vector(<2 x i64> %A) {
6365
; CHECK-LABEL: @test8_vector(
64-
; CHECK-NEXT: ret <2 x i16> undef
66+
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i64> [[A:%.*]], <i64 48, i64 48>
67+
; CHECK-NEXT: [[D:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i16>
68+
; CHECK-NEXT: ret <2 x i16> [[D]]
6569
;
6670
%B = tail call <2 x i64> @llvm.bswap.v2i64(<2 x i64> %A) nounwind
6771
%C = trunc <2 x i64> %B to <2 x i16>

0 commit comments

Comments
 (0)