[llvm] [InstCombine] do not assume the operand position of reverse shuffle (PR #205747)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 25 02:34:59 PDT 2026


https://github.com/XChy updated https://github.com/llvm/llvm-project/pull/205747

>From b2a225e1d7d0e110e30a15f5d28f5746cc46eb9b Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Thu, 25 Jun 2026 16:06:51 +0800
Subject: [PATCH 1/2] [InstCombine] do not assume the operand position of
 reverse shuffle

---
 .../InstCombine/InstCombineCasts.cpp          |  3 +--
 llvm/test/Transforms/InstCombine/bswap.ll     | 26 +++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 574838a52db2d..c78e0d512b255 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -3392,7 +3392,7 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
     // bitcast <N x i8> (shuf X, undef, <N, N-1,...0>) -> bswap (bitcast X)
     // bitcast <N x i1> (shuf X, undef, <N, N-1,...0>) -> bitreverse (bitcast X)
     if (DestTy->isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 &&
-        Shuf->hasOneUse() && Shuf->isReverse()) {
+        Shuf->hasOneUse() && Shuf->isReverse() && match(ShufOp1, m_Undef())) {
       unsigned IntrinsicNum = 0;
       if (DL.isLegalInteger(DestTy->getScalarSizeInBits()) &&
           SrcTy->getScalarSizeInBits() == 8) {
@@ -3402,7 +3402,6 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
       }
       if (IntrinsicNum != 0) {
         assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask");
-        assert(match(ShufOp1, m_Undef()) && "Unexpected shuffle op");
         Function *BswapOrBitreverse = Intrinsic::getOrInsertDeclaration(
             CI.getModule(), IntrinsicNum, DestTy);
         Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy);
diff --git a/llvm/test/Transforms/InstCombine/bswap.ll b/llvm/test/Transforms/InstCombine/bswap.ll
index 44e1616af86c5..cf04243d412b3 100644
--- a/llvm/test/Transforms/InstCombine/bswap.ll
+++ b/llvm/test/Transforms/InstCombine/bswap.ll
@@ -685,6 +685,32 @@ define i32 @shuf_bitcast_twice_4bytes(i32 %x) {
   ret i32 %cast2
 }
 
+define i64 @shuf_reverse_of_op1_crash(i16 %call, ptr %coerce, <2 x i8> %vec) {
+; CHECK-LABEL: @shuf_reverse_of_op1_crash(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    store i16 [[CALL:%.*]], ptr [[COERCE:%.*]], align 2
+; CHECK-NEXT:    br label [[LBL_CONT1:%.*]]
+; CHECK:       lbl_cont1:
+; CHECK-NEXT:    store i16 poison, ptr null, align 2
+; CHECK-NEXT:    br label [[IF_THEN:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    br label [[LBL_CONT1]]
+;
+entry:
+  store i16 %call, ptr %coerce, align 2
+  %load = load <2 x i8>, ptr %coerce, align 2
+  store <2 x i8> %load, ptr null, align 2
+  br label %lbl_cont1
+
+lbl_cont1:                                        ; preds = %if.then, %entry
+  br label %if.then
+
+if.then:                                          ; preds = %lbl_cont1
+  %shuffle = shufflevector <2 x i8> %vec, <2 x i8> zeroinitializer, <2 x i32> <i32 3, i32 2>
+  store <2 x i8> %shuffle, ptr null, align 2
+  br label %lbl_cont1
+}
+
 ; Negative test - extra use
 declare void @use(<4 x i8>)
 

>From ac4d15e50924d82d4e0a6141424666ccb445624e Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Thu, 25 Jun 2026 17:34:41 +0800
Subject: [PATCH 2/2] use m_Poison

---
 llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index c78e0d512b255..bc6b23ae6039b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -3392,7 +3392,7 @@ Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) {
     // bitcast <N x i8> (shuf X, undef, <N, N-1,...0>) -> bswap (bitcast X)
     // bitcast <N x i1> (shuf X, undef, <N, N-1,...0>) -> bitreverse (bitcast X)
     if (DestTy->isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 &&
-        Shuf->hasOneUse() && Shuf->isReverse() && match(ShufOp1, m_Undef())) {
+        Shuf->hasOneUse() && Shuf->isReverse() && match(ShufOp1, m_Poison())) {
       unsigned IntrinsicNum = 0;
       if (DL.isLegalInteger(DestTy->getScalarSizeInBits()) &&
           SrcTy->getScalarSizeInBits() == 8) {



More information about the llvm-commits mailing list