[llvm] [InstCombine] Bail out on type mismatch in foldBitCastBitwiseLogic (PR #179640)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 4 02:59:22 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Kacper Doga (varev-dev)

<details>
<summary>Changes</summary>

Fixes #<!-- -->179626 

---
Full diff: https://github.com/llvm/llvm-project/pull/179640.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (+4) 
- (modified) llvm/test/Transforms/InstCombine/bitcast.ll (+13) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 0cd2c09726a2d..f77d16531e77e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2828,6 +2828,10 @@ static Instruction *foldBitCastBitwiseLogic(BitCastInst &BitCast,
     // bitcast (logic X, C) --> logic (bitcast X, C')
     Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
     Value *CastedC = Builder.CreateBitCast(C, DestTy);
+
+    if (CastedC->getType() != DestTy)
+      return nullptr;
+
     return BinaryOperator::Create(BO->getOpcode(), CastedOp0, CastedC);
   }
 
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 37d41de3e9991..fe473ff3698d5 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -903,3 +903,16 @@ end:                                        ; preds = %unreachable, %entry
   %3 = extractelement <2 x i16> %2, i64 0
   ret i16 %3
 }
+
+; Ensure we do not crash when constant folding bitcasts of exotic vector types
+define <3 x i8> @bitcast_exotic_vector_type(<3 x i32> %LGV) {
+; CHECK-LABEL: @bitcast_exotic_vector_type(
+; CHECK-NEXT:    ret <3 x i8> zeroinitializer
+;
+  %B1 = xor <3 x i32> %LGV, splat (i32 1)
+  %1 = bitcast <3 x i32> %B1 to i96
+  %2 = trunc i96 %1 to i24
+  %3 = bitcast i24 %2 to <3 x i8>
+  %B4 = lshr <3 x i8> zeroinitializer, %3
+  ret <3 x i8> %B4
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/179640


More information about the llvm-commits mailing list