Skip to content

Commit a5445e7

Browse files
committed
Check for scalar type
1 parent 9db0ba7 commit a5445e7

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,18 +3936,18 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
39363936
break;
39373937

39383938
if (auto *DstVTy = dyn_cast<FixedVectorType>(Ty)) {
3939-
unsigned Scale = SrcBits / TyBits;
3939+
if (auto *SrcVTy = dyn_cast<FixedVectorType>(SrcTy)) {
3940+
APInt SrcDemandedElts =
3941+
APInt::getSplat(SrcVTy->getNumElements(), APInt(1, 1));
39403942

3941-
APInt SrcDemandedElts =
3942-
APInt::getSplat(DstVTy->getNumElements() / Scale, APInt(1, 1));
3943-
3944-
Tmp = ComputeNumSignBits(Src, SrcDemandedElts, Depth + 1, Q);
3945-
if (Tmp == SrcBits)
3946-
return TyBits;
3947-
} else {
3948-
Tmp = ComputeNumSignBits(Src, APInt(1, 1), Depth + 1, Q);
3949-
if (Tmp == SrcBits)
3950-
return TyBits;
3943+
Tmp = ComputeNumSignBits(Src, SrcDemandedElts, Depth + 1, Q);
3944+
if (Tmp == SrcBits)
3945+
return TyBits;
3946+
} else {
3947+
Tmp = ComputeNumSignBits(Src, APInt(1, 1), Depth + 1, Q);
3948+
if (Tmp == SrcBits)
3949+
return TyBits;
3950+
}
39513951
}
39523952
break;
39533953
}
Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
33

4-
define i32 @test_compute_sign_bits() {
5-
; CHECK-LABEL: define i32 @test_compute_sign_bits() {
6-
; CHECK-NEXT: [[ENTRY:.*:]]
7-
; CHECK-NEXT: ret i32 -1
8-
;
9-
entry:
10-
%a = add i8 -1, 0
11-
%b = bitcast i8 %a to <4 x i2>
12-
%c = ashr <4 x i2> %b, <i2 1, i2 1, i2 1, i2 1>
13-
%d = bitcast <4 x i2> %c to i8
14-
%e = sext i8 %d to i32
15-
ret i32 %e
16-
}
17-
18-
; Test with sign extension to ensure proper sign bit tracking
19-
define <4 x i2> @test_sext_bitcast(<1 x i8> %a0, <1 x i8> %a1) {
20-
; CHECK-LABEL: define <4 x i2> @test_sext_bitcast(
4+
; Case 1: Vector to Vector bitcast
5+
define <4 x i2> @test_vector_to_vector(<1 x i8> %a0, <1 x i8> %a1) {
6+
; CHECK-LABEL: define <4 x i2> @test_vector_to_vector(
217
; CHECK-SAME: <1 x i8> [[A0:%.*]], <1 x i8> [[A1:%.*]]) {
22-
; CHECK-NEXT: [[ENTRY:.*:]]
238
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <1 x i8> [[A0]], [[A1]]
249
; CHECK-NEXT: [[EXT:%.*]] = sext <1 x i1> [[CMP]] to <1 x i8>
2510
; CHECK-NEXT: [[SUB:%.*]] = bitcast <1 x i8> [[EXT]] to <4 x i2>
2611
; CHECK-NEXT: ret <4 x i2> [[SUB]]
2712
;
28-
entry:
2913
%cmp = icmp sgt <1 x i8> %a0, %a1
3014
%ext = sext <1 x i1> %cmp to <1 x i8>
3115
%sub = bitcast <1 x i8> %ext to <4 x i2>
32-
%result = ashr <4 x i2> %sub, <i2 1, i2 1, i2 1, i2 1>
33-
ret <4 x i2> %result
16+
%sra = ashr <4 x i2> %sub, <i2 1, i2 1, i2 1, i2 1>
17+
ret <4 x i2> %sra
18+
}
19+
20+
; Case 2: Scalar to Vector bitcast
21+
define <2 x i16> @test_scalar_to_vector(i1 %cond) {
22+
; CHECK-LABEL: define <2 x i16> @test_scalar_to_vector(
23+
; CHECK-SAME: i1 [[COND:%.*]]) {
24+
; CHECK-NEXT: [[EXT:%.*]] = sext i1 [[COND]] to i32
25+
; CHECK-NEXT: [[BC:%.*]] = bitcast i32 [[EXT]] to <2 x i16>
26+
; CHECK-NEXT: ret <2 x i16> [[BC]]
27+
;
28+
%ext = sext i1 %cond to i32
29+
%bc = bitcast i32 %ext to <2 x i16>
30+
%sra = ashr <2 x i16> %bc, <i16 8, i16 8>
31+
ret <2 x i16> %sra
32+
}
33+
34+
35+
; Case 3: Multiple right shifts
36+
define <8 x i8> @test_multiple_shifts(i1 %cond) {
37+
; CHECK-LABEL: define <8 x i8> @test_multiple_shifts(
38+
; CHECK-SAME: i1 [[COND:%.*]]) {
39+
; CHECK-NEXT: [[EXT:%.*]] = sext i1 [[COND]] to i64
40+
; CHECK-NEXT: [[BC:%.*]] = bitcast i64 [[EXT]] to <8 x i8>
41+
; CHECK-NEXT: ret <8 x i8> [[BC]]
42+
;
43+
%ext = sext i1 %cond to i64
44+
%bc = bitcast i64 %ext to <8 x i8>
45+
%sra1 = ashr <8 x i8> %bc, <i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 1>
46+
%sra2 = ashr <8 x i8> %sra1, <i8 2, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
47+
ret <8 x i8> %sra2
48+
}
49+
50+
; Case 4: Test with non-sign-extended source
51+
define <4 x i8> @test_non_sign_extended(i32 %val) {
52+
; CHECK-LABEL: define <4 x i8> @test_non_sign_extended(
53+
; CHECK-SAME: i32 [[VAL:%.*]]) {
54+
; CHECK-NEXT: [[BC:%.*]] = bitcast i32 [[VAL]] to <4 x i8>
55+
; CHECK-NEXT: [[SRA:%.*]] = ashr <4 x i8> [[BC]], splat (i8 1)
56+
; CHECK-NEXT: ret <4 x i8> [[SRA]]
57+
;
58+
%bc = bitcast i32 %val to <4 x i8>
59+
%sra = ashr <4 x i8> %bc, <i8 1, i8 1, i8 1, i8 1>
60+
ret <4 x i8> %sra
3461
}

0 commit comments

Comments
 (0)