Skip to content

Commit 9db0ba7

Browse files
committed
[ValueTracking] add basic handling of BITCAST nodes
1 parent dcb49a0 commit 9db0ba7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,35 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
39223922
if (auto *U = dyn_cast<Operator>(V)) {
39233923
switch (Operator::getOpcode(V)) {
39243924
default: break;
3925+
case Instruction::BitCast: {
3926+
Value *Src = U->getOperand(0);
3927+
Type *SrcTy = Src->getType();
3928+
Type *SrcScalarTy = SrcTy->getScalarType();
3929+
3930+
if (!SrcScalarTy->isIntegerTy())
3931+
break;
3932+
3933+
unsigned SrcBits = SrcTy->getScalarSizeInBits();
3934+
3935+
if ((SrcBits % TyBits) != 0)
3936+
break;
3937+
3938+
if (auto *DstVTy = dyn_cast<FixedVectorType>(Ty)) {
3939+
unsigned Scale = SrcBits / TyBits;
3940+
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;
3951+
}
3952+
break;
3953+
}
39253954
case Instruction::SExt:
39263955
Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
39273956
return ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q) +

llvm/test/Transforms/InstCombine/compute-sign-bits-bitcast.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ define <4 x i2> @test_sext_bitcast(<1 x i8> %a0, <1 x i8> %a1) {
2323
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <1 x i8> [[A0]], [[A1]]
2424
; CHECK-NEXT: [[EXT:%.*]] = sext <1 x i1> [[CMP]] to <1 x i8>
2525
; CHECK-NEXT: [[SUB:%.*]] = bitcast <1 x i8> [[EXT]] to <4 x i2>
26-
; CHECK-NEXT: [[RESULT:%.*]] = ashr <4 x i2> [[SUB]], splat (i2 1)
27-
; CHECK-NEXT: ret <4 x i2> [[RESULT]]
26+
; CHECK-NEXT: ret <4 x i2> [[SUB]]
2827
;
2928
entry:
3029
%cmp = icmp sgt <1 x i8> %a0, %a1

0 commit comments

Comments
 (0)