Skip to content

Commit 9472225

Browse files
authored
[KnownBits] Add operator<<=(unsigned) and operator>>=(unsigned). NFC (llvm#155751)
Add operators to shift left or right and insert unknown bits.
1 parent 8db7571 commit 9472225

File tree

7 files changed

+40
-45
lines changed

7 files changed

+40
-45
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,20 @@ struct KnownBits {
487487
/// Update known bits based on XORing with RHS.
488488
LLVM_ABI KnownBits &operator^=(const KnownBits &RHS);
489489

490+
/// Shift known bits left by ShAmt. Shift in bits are unknown.
491+
KnownBits &operator<<=(unsigned ShAmt) {
492+
Zero <<= ShAmt;
493+
One <<= ShAmt;
494+
return *this;
495+
}
496+
497+
/// Shift known bits right by ShAmt. Shifted in bits are unknown.
498+
KnownBits &operator>>=(unsigned ShAmt) {
499+
Zero.lshrInPlace(ShAmt);
500+
One.lshrInPlace(ShAmt);
501+
return *this;
502+
}
503+
490504
/// Compute known bits for the absolute value.
491505
LLVM_ABI KnownBits abs(bool IntMinIsPoison = false) const;
492506

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -727,17 +727,16 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
727727
// For those bits in C that are known, we can propagate them to known
728728
// bits in V shifted to the right by ShAmt.
729729
KnownBits RHSKnown = KnownBits::makeConstant(*C);
730-
RHSKnown.Zero.lshrInPlace(ShAmt);
731-
RHSKnown.One.lshrInPlace(ShAmt);
730+
RHSKnown >>= ShAmt;
732731
Known = Known.unionWith(RHSKnown);
733732
// assume(V >> ShAmt = C)
734733
} else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
735734
ShAmt < BitWidth) {
736-
KnownBits RHSKnown = KnownBits::makeConstant(*C);
737735
// For those bits in RHS that are known, we can propagate them to known
738736
// bits in V shifted to the right by C.
739-
Known.Zero |= RHSKnown.Zero << ShAmt;
740-
Known.One |= RHSKnown.One << ShAmt;
737+
KnownBits RHSKnown = KnownBits::makeConstant(*C);
738+
RHSKnown <<= ShAmt;
739+
Known = Known.unionWith(RHSKnown);
741740
}
742741
break;
743742
case ICmpInst::ICMP_NE: {
@@ -1890,10 +1889,9 @@ static void computeKnownBitsFromOperator(const Operator *I,
18901889
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
18911890
computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
18921891

1893-
Known.Zero =
1894-
Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1895-
Known.One =
1896-
Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1892+
Known2 <<= ShiftAmt;
1893+
Known3 >>= BitWidth - ShiftAmt;
1894+
Known = Known2.unionWith(Known3);
18971895
break;
18981896
}
18991897
case Intrinsic::uadd_sat:

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3868,15 +3868,11 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
38683868
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
38693869
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
38703870
if (Opcode == ISD::FSHL) {
3871-
Known.One <<= Amt;
3872-
Known.Zero <<= Amt;
3873-
Known2.One.lshrInPlace(BitWidth - Amt);
3874-
Known2.Zero.lshrInPlace(BitWidth - Amt);
3871+
Known <<= Amt;
3872+
Known2 >>= BitWidth - Amt;
38753873
} else {
3876-
Known.One <<= BitWidth - Amt;
3877-
Known.Zero <<= BitWidth - Amt;
3878-
Known2.One.lshrInPlace(Amt);
3879-
Known2.Zero.lshrInPlace(Amt);
3874+
Known <<= BitWidth - Amt;
3875+
Known2 >>= Amt;
38803876
}
38813877
Known = Known.unionWith(Known2);
38823878
}

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,8 +1858,7 @@ bool TargetLowering::SimplifyDemandedBits(
18581858
Op->dropFlags(SDNodeFlags::NoWrap);
18591859
return true;
18601860
}
1861-
Known.Zero <<= ShAmt;
1862-
Known.One <<= ShAmt;
1861+
Known <<= ShAmt;
18631862
// low bits known zero.
18641863
Known.Zero.setLowBits(ShAmt);
18651864

@@ -2042,8 +2041,7 @@ bool TargetLowering::SimplifyDemandedBits(
20422041
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
20432042
Depth + 1))
20442043
return true;
2045-
Known.Zero.lshrInPlace(ShAmt);
2046-
Known.One.lshrInPlace(ShAmt);
2044+
Known >>= ShAmt;
20472045
// High bits known zero.
20482046
Known.Zero.setHighBits(ShAmt);
20492047

@@ -2153,8 +2151,7 @@ bool TargetLowering::SimplifyDemandedBits(
21532151
if (SimplifyDemandedBits(Op0, InDemandedMask, DemandedElts, Known, TLO,
21542152
Depth + 1))
21552153
return true;
2156-
Known.Zero.lshrInPlace(ShAmt);
2157-
Known.One.lshrInPlace(ShAmt);
2154+
Known >>= ShAmt;
21582155

21592156
// If the input sign bit is known to be zero, or if none of the top bits
21602157
// are demanded, turn this into an unsigned shift right.
@@ -2225,10 +2222,8 @@ bool TargetLowering::SimplifyDemandedBits(
22252222
Depth + 1))
22262223
return true;
22272224

2228-
Known2.One <<= (IsFSHL ? Amt : (BitWidth - Amt));
2229-
Known2.Zero <<= (IsFSHL ? Amt : (BitWidth - Amt));
2230-
Known.One.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
2231-
Known.Zero.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
2225+
Known2 <<= (IsFSHL ? Amt : (BitWidth - Amt));
2226+
Known >>= (IsFSHL ? (BitWidth - Amt) : Amt);
22322227
Known = Known.unionWith(Known2);
22332228

22342229
// Attempt to avoid multi-use ops if we don't need anything from them.

llvm/lib/Support/KnownBits.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
372372
unsigned BitWidth = LHS.getBitWidth();
373373
auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
374374
KnownBits Known = LHS;
375-
Known.Zero.lshrInPlace(ShiftAmt);
376-
Known.One.lshrInPlace(ShiftAmt);
375+
Known >>= ShiftAmt;
377376
// High bits are known zero.
378377
Known.Zero.setHighBits(ShiftAmt);
379378
return Known;

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38676,13 +38676,11 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
3867638676

3867738677
Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3867838678
if (Opc == X86ISD::VSHLI) {
38679-
Known.Zero <<= ShAmt;
38680-
Known.One <<= ShAmt;
38679+
Known <<= ShAmt;
3868138680
// Low bits are known zero.
3868238681
Known.Zero.setLowBits(ShAmt);
3868338682
} else if (Opc == X86ISD::VSRLI) {
38684-
Known.Zero.lshrInPlace(ShAmt);
38685-
Known.One.lshrInPlace(ShAmt);
38683+
Known >>= ShAmt;
3868638684
// High bits are known zero.
3868738685
Known.Zero.setHighBits(ShAmt);
3868838686
} else {
@@ -44518,8 +44516,7 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
4451844516
TLO, Depth + 1))
4451944517
return true;
4452044518

44521-
Known.Zero <<= ShAmt;
44522-
Known.One <<= ShAmt;
44519+
Known <<= ShAmt;
4452344520

4452444521
// Low bits known zero.
4452544522
Known.Zero.setLowBits(ShAmt);
@@ -44549,8 +44546,7 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
4454944546
TLO, Depth + 1))
4455044547
return true;
4455144548

44552-
Known.Zero.lshrInPlace(ShAmt);
44553-
Known.One.lshrInPlace(ShAmt);
44549+
Known >>= ShAmt;
4455444550

4455544551
// High bits known zero.
4455644552
Known.Zero.setHighBits(ShAmt);
@@ -44598,8 +44594,7 @@ bool X86TargetLowering::SimplifyDemandedBitsForTargetNode(
4459844594
TLO, Depth + 1))
4459944595
return true;
4460044596

44601-
Known.Zero.lshrInPlace(ShAmt);
44602-
Known.One.lshrInPlace(ShAmt);
44597+
Known >>= ShAmt;
4460344598

4460444599
// If the input sign bit is known to be zero, or if none of the top bits
4460544600
// are demanded, turn this into an unsigned shift right.

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
795795
I->dropPoisonGeneratingFlags();
796796
return I;
797797
}
798-
Known.Zero.lshrInPlace(ShiftAmt);
799-
Known.One.lshrInPlace(ShiftAmt);
798+
Known >>= ShiftAmt;
800799
if (ShiftAmt)
801800
Known.Zero.setHighBits(ShiftAmt); // high bits known zero.
802801
} else {
@@ -1066,10 +1065,9 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
10661065
}
10671066
}
10681067

1069-
Known.Zero = LHSKnown.Zero.shl(ShiftAmt) |
1070-
RHSKnown.Zero.lshr(BitWidth - ShiftAmt);
1071-
Known.One = LHSKnown.One.shl(ShiftAmt) |
1072-
RHSKnown.One.lshr(BitWidth - ShiftAmt);
1068+
LHSKnown <<= ShiftAmt;
1069+
RHSKnown >>= BitWidth - ShiftAmt;
1070+
Known = LHSKnown.unionWith(RHSKnown);
10731071
KnownBitsComputed = true;
10741072
break;
10751073
}

0 commit comments

Comments
 (0)