[llvm] [Reland][ValueTracking] Improve Bitcast handling to match SDAG (PR #145223)

Abhishek Kaushik via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 3 02:57:21 PDT 2025


================
@@ -499,3 +499,35 @@ define <1 x i64> @bitcast_noshift_vector_wrong_type(<2 x float> %v1, <1 x i64> %
   %r = shl <1 x i64> %v2, %b
   ret <1 x i64> %r
 }
+
+; Test that verifies correct handling of known bits when bitcasting from a smaller vector
+; to a larger one (e.g., <2 x i32> to <8 x i8>). Previously, only the subscale portion
+; (e.g., 4 elements) was checked instead of the full demanded vector width (8 elements),
+; leading to incorrect known bits and removal of the `ashr` instruction.
+
+define <8 x i8> @bitcast_knownbits_subscale_miscompile(i32 %x) {
+; CHECK-LABEL: @bitcast_knownbits_subscale_miscompile(
+; CHECK-NEXT:    [[MASKED:%.*]] = and i32 [[X:%.*]], -256
+; CHECK-NEXT:    [[BITCAST:%.*]] = bitcast i32 [[MASKED]] to <4 x i8>
+; CHECK-NEXT:    [[EXTRACT:%.*]] = extractelement <4 x i8> [[BITCAST]], i32 3
+; CHECK-NEXT:    [[COND:%.*]] = icmp eq i8 [[EXTRACT]], -113
+; CHECK-NEXT:    call void @llvm.assume(i1 [[COND]])
+; CHECK-NEXT:    [[INSERT:%.*]] = insertelement <2 x i32> poison, i32 [[MASKED]], i32 0
+; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <2 x i32> [[INSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
+; CHECK-NEXT:    [[VEC:%.*]] = bitcast <2 x i32> [[SPLAT]] to <8 x i8>
+; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <8 x i8> [[VEC]], <8 x i8> zeroinitializer, <8 x i32> <i32 7, i32 7, i32 7, i32 7, i32 0, i32 0, i32 0, i32 0>
+; CHECK-NEXT:    [[SHR:%.*]] = ashr <8 x i8> [[SHUF]], splat (i8 1)
+; CHECK-NEXT:    ret <8 x i8> [[SHR]]
+;
+  %masked = and i32 %x, u0xFFFFFF00
+  %bitcast = bitcast i32 %masked to <4 x i8>
+  %extract = extractelement <4 x i8> %bitcast, i32 3
+  %cond = icmp eq i8 %extract, u0x8F
+  call void @llvm.assume(i1 %cond)
----------------
abhishek-kaushik22 wrote:

I use the assume to set the known 1 bits, the exact bits are not important for to show a miscompile but we need at least one bit set

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


More information about the llvm-commits mailing list