Skip to content

Commit 6d1a8fd

Browse files
committed
[SelectionDAG] ComputeKnownBits - Add DemandedElts support to getValidShiftAmountConstant/getValidMinimumShiftAmountConstant()
1 parent 89ba150 commit 6d1a8fd

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,9 +2411,10 @@ SDValue SelectionDAG::getSplatValue(SDValue V) {
24112411

24122412
/// If a SHL/SRA/SRL node has a constant or splat constant shift amount that
24132413
/// is less than the element bit-width of the shift node, return it.
2414-
static const APInt *getValidShiftAmountConstant(SDValue V) {
2414+
static const APInt *getValidShiftAmountConstant(SDValue V,
2415+
const APInt &DemandedElts) {
24152416
unsigned BitWidth = V.getScalarValueSizeInBits();
2416-
if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1))) {
2417+
if (ConstantSDNode *SA = isConstOrConstSplat(V.getOperand(1), DemandedElts)) {
24172418
// Shifting more than the bitwidth is not valid.
24182419
const APInt &ShAmt = SA->getAPIntValue();
24192420
if (ShAmt.ult(BitWidth))
@@ -2424,13 +2425,16 @@ static const APInt *getValidShiftAmountConstant(SDValue V) {
24242425

24252426
/// If a SHL/SRA/SRL node has constant vector shift amounts that are all less
24262427
/// than the element bit-width of the shift node, return the minimum value.
2427-
static const APInt *getValidMinimumShiftAmountConstant(SDValue V) {
2428+
static const APInt *
2429+
getValidMinimumShiftAmountConstant(SDValue V, const APInt &DemandedElts) {
24282430
unsigned BitWidth = V.getScalarValueSizeInBits();
24292431
auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1));
24302432
if (!BV)
24312433
return nullptr;
24322434
const APInt *MinShAmt = nullptr;
24332435
for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
2436+
if (!DemandedElts[i])
2437+
continue;
24342438
auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
24352439
if (!SA)
24362440
return nullptr;
@@ -2827,14 +2831,15 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
28272831
break;
28282832
}
28292833
case ISD::SHL:
2830-
if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
2834+
if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
28312835
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
28322836
unsigned Shift = ShAmt->getZExtValue();
28332837
Known.Zero <<= Shift;
28342838
Known.One <<= Shift;
28352839
// Low bits are known zero.
28362840
Known.Zero.setLowBits(Shift);
2837-
} else if (const APInt *ShMinAmt = getValidMinimumShiftAmountConstant(Op)) {
2841+
} else if (const APInt *ShMinAmt =
2842+
getValidMinimumShiftAmountConstant(Op, DemandedElts)) {
28382843
// Minimum shift low bits are known zero.
28392844
Known.Zero.setLowBits(ShMinAmt->getZExtValue());
28402845
} else {
@@ -2846,14 +2851,15 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
28462851
}
28472852
break;
28482853
case ISD::SRL:
2849-
if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
2854+
if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
28502855
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
28512856
unsigned Shift = ShAmt->getZExtValue();
28522857
Known.Zero.lshrInPlace(Shift);
28532858
Known.One.lshrInPlace(Shift);
28542859
// High bits are known zero.
28552860
Known.Zero.setHighBits(Shift);
2856-
} else if (const APInt *ShMinAmt = getValidMinimumShiftAmountConstant(Op)) {
2861+
} else if (const APInt *ShMinAmt =
2862+
getValidMinimumShiftAmountConstant(Op, DemandedElts)) {
28572863
// Minimum shift high bits are known zero.
28582864
Known.Zero.setHighBits(ShMinAmt->getZExtValue());
28592865
} else {
@@ -2864,7 +2870,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
28642870
}
28652871
break;
28662872
case ISD::SRA:
2867-
if (const APInt *ShAmt = getValidShiftAmountConstant(Op)) {
2873+
if (const APInt *ShAmt = getValidShiftAmountConstant(Op, DemandedElts)) {
28682874
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
28692875
unsigned Shift = ShAmt->getZExtValue();
28702876
// Sign extend known zero/one bit (else is unknown).

llvm/test/CodeGen/X86/combine-shl.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ define <4 x i32> @combine_vec_add_shuffle_shl(<4 x i32> %a0) {
891891
; AVX-NEXT: vpsllvd {{.*}}(%rip), %xmm0, %xmm0
892892
; AVX-NEXT: vpshufd {{.*#+}} xmm0 = xmm0[0,1,1,0]
893893
; AVX-NEXT: vpbroadcastd {{.*#+}} xmm1 = [3,3,3,3]
894-
; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm0
894+
; AVX-NEXT: vpor %xmm1, %xmm0, %xmm0
895895
; AVX-NEXT: retq
896896
%1 = shl <4 x i32> %a0, <i32 2, i32 3, i32 0, i32 1>
897897
%2 = shufflevector <4 x i32> %1, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 1, i32 0>

llvm/test/CodeGen/X86/known-bits-vector.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,15 @@ define <2 x double> @knownbits_lshr_subvector_uitofp(<4 x i32> %x) {
671671
; X32-NEXT: vpsrld $2, %xmm0, %xmm1
672672
; X32-NEXT: vpsrld $1, %xmm0, %xmm0
673673
; X32-NEXT: vpblendw {{.*#+}} xmm0 = xmm0[0,1],xmm1[2,3],xmm0[4,5,6,7]
674-
; X32-NEXT: vpmovzxdq {{.*#+}} xmm0 = xmm0[0],zero,xmm0[1],zero
675-
; X32-NEXT: vmovdqa {{.*#+}} xmm1 = [4.503599627370496E+15,4.503599627370496E+15]
676-
; X32-NEXT: vpor %xmm1, %xmm0, %xmm0
677-
; X32-NEXT: vsubpd %xmm1, %xmm0, %xmm0
674+
; X32-NEXT: vcvtdq2pd %xmm0, %xmm0
678675
; X32-NEXT: retl
679676
;
680677
; X64-LABEL: knownbits_lshr_subvector_uitofp:
681678
; X64: # %bb.0:
682679
; X64-NEXT: vpsrld $2, %xmm0, %xmm1
683680
; X64-NEXT: vpsrld $1, %xmm0, %xmm0
684681
; X64-NEXT: vpblendw {{.*#+}} xmm0 = xmm0[0,1],xmm1[2,3],xmm0[4,5,6,7]
685-
; X64-NEXT: vpmovzxdq {{.*#+}} xmm0 = xmm0[0],zero,xmm0[1],zero
686-
; X64-NEXT: vmovdqa {{.*#+}} xmm1 = [4.503599627370496E+15,4.503599627370496E+15]
687-
; X64-NEXT: vpor %xmm1, %xmm0, %xmm0
688-
; X64-NEXT: vsubpd %xmm1, %xmm0, %xmm0
682+
; X64-NEXT: vcvtdq2pd %xmm0, %xmm0
689683
; X64-NEXT: retq
690684
%1 = lshr <4 x i32> %x, <i32 1, i32 2, i32 0, i32 0>
691685
%2 = shufflevector <4 x i32> %1, <4 x i32> undef, <2 x i32> <i32 0, i32 1>

0 commit comments

Comments
 (0)