[llvm] [ValueTracking] ComputeNumSignBitsImpl - add basic handling of BITCAST nodes (PR #127218)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 24 00:46:22 PST 2025
https://github.com/vortex73 updated https://github.com/llvm/llvm-project/pull/127218
>From dcb49a088ad6b34071a181923acdb142e281e7a4 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Fri, 14 Feb 2025 21:17:15 +0530
Subject: [PATCH 1/2] [ValueTracking] Pre-Commit Tests
---
.../InstCombine/compute-sign-bits-bitcast.ll | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
diff --git a/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll b/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
new file mode 100644
index 0000000000000..887720000477a
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i32 @test_compute_sign_bits() {
+; CHECK-LABEL: define i32 @test_compute_sign_bits() {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: ret i32 -1
+;
+entry:
+ %a = add i8 -1, 0
+ %b = bitcast i8 %a to <4 x i2>
+ %c = ashr <4 x i2> %b, <i2 1, i2 1, i2 1, i2 1>
+ %d = bitcast <4 x i2> %c to i8
+ %e = sext i8 %d to i32
+ ret i32 %e
+}
+
+; Test with sign extension to ensure proper sign bit tracking
+define <4 x i2> @test_sext_bitcast(<1 x i8> %a0, <1 x i8> %a1) {
+; CHECK-LABEL: define <4 x i2> @test_sext_bitcast(
+; CHECK-SAME: <1 x i8> [[A0:%.*]], <1 x i8> [[A1:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <1 x i8> [[A0]], [[A1]]
+; CHECK-NEXT: [[EXT:%.*]] = sext <1 x i1> [[CMP]] to <1 x i8>
+; CHECK-NEXT: [[SUB:%.*]] = bitcast <1 x i8> [[EXT]] to <4 x i2>
+; CHECK-NEXT: [[RESULT:%.*]] = ashr <4 x i2> [[SUB]], splat (i2 1)
+; CHECK-NEXT: ret <4 x i2> [[RESULT]]
+;
+entry:
+ %cmp = icmp sgt <1 x i8> %a0, %a1
+ %ext = sext <1 x i1> %cmp to <1 x i8>
+ %sub = bitcast <1 x i8> %ext to <4 x i2>
+ %result = ashr <4 x i2> %sub, <i2 1, i2 1, i2 1, i2 1>
+ ret <4 x i2> %result
+}
>From 9db0ba72aa532d9bf4e9949f783589f738678045 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Sat, 15 Feb 2025 01:54:15 +0530
Subject: [PATCH 2/2] [ValueTracking] add basic handling of BITCAST nodes
---
llvm/lib/Analysis/ValueTracking.cpp | 29 +++++++++++++++++++
.../InstCombine/compute-sign-bits-bitcast.ll | 3 +-
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8a9ad55366ee7..c28c91b0a511e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3922,6 +3922,35 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
if (auto *U = dyn_cast<Operator>(V)) {
switch (Operator::getOpcode(V)) {
default: break;
+ case Instruction::BitCast: {
+ Value *Src = U->getOperand(0);
+ Type *SrcTy = Src->getType();
+ Type *SrcScalarTy = SrcTy->getScalarType();
+
+ if (!SrcScalarTy->isIntegerTy())
+ break;
+
+ unsigned SrcBits = SrcTy->getScalarSizeInBits();
+
+ if ((SrcBits % TyBits) != 0)
+ break;
+
+ if (auto *DstVTy = dyn_cast<FixedVectorType>(Ty)) {
+ unsigned Scale = SrcBits / TyBits;
+
+ APInt SrcDemandedElts =
+ APInt::getSplat(DstVTy->getNumElements() / Scale, APInt(1, 1));
+
+ Tmp = ComputeNumSignBits(Src, SrcDemandedElts, Depth + 1, Q);
+ if (Tmp == SrcBits)
+ return TyBits;
+ } else {
+ Tmp = ComputeNumSignBits(Src, APInt(1, 1), Depth + 1, Q);
+ if (Tmp == SrcBits)
+ return TyBits;
+ }
+ break;
+ }
case Instruction::SExt:
Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
return ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q) +
diff --git a/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll b/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
index 887720000477a..35ba53687c646 100644
--- a/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll
@@ -23,8 +23,7 @@ define <4 x i2> @test_sext_bitcast(<1 x i8> %a0, <1 x i8> %a1) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <1 x i8> [[A0]], [[A1]]
; CHECK-NEXT: [[EXT:%.*]] = sext <1 x i1> [[CMP]] to <1 x i8>
; CHECK-NEXT: [[SUB:%.*]] = bitcast <1 x i8> [[EXT]] to <4 x i2>
-; CHECK-NEXT: [[RESULT:%.*]] = ashr <4 x i2> [[SUB]], splat (i2 1)
-; CHECK-NEXT: ret <4 x i2> [[RESULT]]
+; CHECK-NEXT: ret <4 x i2> [[SUB]]
;
entry:
%cmp = icmp sgt <1 x i8> %a0, %a1
More information about the llvm-commits
mailing list