Skip to content

Commit d8229e2

Browse files
committed
[KnownBits] Define and use intersectWith and unionWith
Define intersectWith and unionWith as two complementary ways of combining KnownBits. The names are chosen for consistency with ConstantRange. Deprecate commonBits as a synonym for intersectWith. Differential Revision: https://reviews.llvm.org/D150443
1 parent 71ac47f commit d8229e2

File tree

15 files changed

+76
-61
lines changed

15 files changed

+76
-61
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,30 @@ struct KnownBits {
293293
return KnownBits(~C, C);
294294
}
295295

296+
/// Returns KnownBits information that is known to be true for both this and
297+
/// RHS.
298+
///
299+
/// When an operation is known to return one of its operands, this can be used
300+
/// to combine information about the known bits of the operands to get the
301+
/// information that must be true about the result.
302+
KnownBits intersectWith(const KnownBits &RHS) const {
303+
return KnownBits(Zero & RHS.Zero, One & RHS.One);
304+
}
305+
306+
/// Returns KnownBits information that is known to be true for either this or
307+
/// RHS or both.
308+
///
309+
/// This can be used to combine different sources of information about the
310+
/// known bits of a single value, e.g. information about the low bits and the
311+
/// high bits of the result of a multiplication.
312+
KnownBits unionWith(const KnownBits &RHS) const {
313+
return KnownBits(Zero | RHS.Zero, One | RHS.One);
314+
}
315+
296316
/// Compute known bits common to LHS and RHS.
317+
LLVM_DEPRECATED("use intersectWith instead", "intersectWith")
297318
static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) {
298-
return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);
319+
return LHS.intersectWith(RHS);
299320
}
300321

301322
/// Return true if LHS and RHS have no common bits set.

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,7 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
668668
if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A)))) {
669669
KnownBits RHSKnown =
670670
computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth);
671-
Known.Zero |= RHSKnown.Zero;
672-
Known.One |= RHSKnown.One;
671+
Known = Known.unionWith(RHSKnown);
673672
// assume(v & b = a)
674673
} else if (match(Cmp,
675674
m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A)))) {
@@ -758,9 +757,8 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
758757
// For those bits in RHS that are known, we can propagate them to known
759758
// bits in V shifted to the right by C.
760759
RHSKnown.Zero.lshrInPlace(C);
761-
Known.Zero |= RHSKnown.Zero;
762760
RHSKnown.One.lshrInPlace(C);
763-
Known.One |= RHSKnown.One;
761+
Known = Known.unionWith(RHSKnown);
764762
// assume(~(v << c) = a)
765763
} else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
766764
m_Value(A))) &&
@@ -1053,8 +1051,8 @@ static void computeKnownBitsFromShiftOperator(
10531051
continue;
10541052
}
10551053

1056-
Known = KnownBits::commonBits(
1057-
Known, KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt))));
1054+
Known = Known.intersectWith(
1055+
KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt))));
10581056
}
10591057

10601058
// If the known bits conflict, the result is poison. Return a 0 and hope the
@@ -1242,7 +1240,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
12421240
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
12431241

12441242
// Only known if known in both the LHS and RHS.
1245-
Known = KnownBits::commonBits(Known, Known2);
1243+
Known = Known.intersectWith(Known2);
12461244

12471245
if (SPF == SPF_ABS) {
12481246
// RHS from matchSelectPattern returns the negation part of abs pattern.
@@ -1680,7 +1678,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
16801678
}
16811679
}
16821680

1683-
Known = KnownBits::commonBits(Known, Known2);
1681+
Known = Known.intersectWith(Known2);
16841682
// If all bits have been ruled out, there's no need to check
16851683
// more operands.
16861684
if (Known.isUnknown())
@@ -1699,8 +1697,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
16991697
computeKnownBitsFromRangeMetadata(*MD, Known);
17001698
if (const Value *RV = cast<CallBase>(I)->getReturnedArgOperand()) {
17011699
computeKnownBits(RV, Known2, Depth + 1, Q);
1702-
Known.Zero |= Known2.Zero;
1703-
Known.One |= Known2.One;
1700+
Known = Known.unionWith(Known2);
17041701
}
17051702
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
17061703
switch (II->getIntrinsicID()) {
@@ -1872,7 +1869,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
18721869
if (!!DemandedRHS) {
18731870
const Value *RHS = Shuf->getOperand(1);
18741871
computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
1875-
Known = KnownBits::commonBits(Known, Known2);
1872+
Known = Known.intersectWith(Known2);
18761873
}
18771874
break;
18781875
}
@@ -1905,7 +1902,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
19051902
DemandedVecElts.clearBit(EltIdx);
19061903
if (!!DemandedVecElts) {
19071904
computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
1908-
Known = KnownBits::commonBits(Known, Known2);
1905+
Known = Known.intersectWith(Known2);
19091906
}
19101907
break;
19111908
}

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void GISelKnownBits::computeKnownBitsMin(Register Src0, Register Src1,
115115
computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
116116

117117
// Only known if known in both the LHS and RHS.
118-
Known = KnownBits::commonBits(Known, Known2);
118+
Known = Known.intersectWith(Known2);
119119
}
120120

121121
// Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
@@ -191,7 +191,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
191191
Depth + 1);
192192

193193
// Known bits are the values that are shared by every demanded element.
194-
Known = KnownBits::commonBits(Known, Known2);
194+
Known = Known.intersectWith(Known2);
195195

196196
// If we don't know any bits, early out.
197197
if (Known.isUnknown())
@@ -235,7 +235,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
235235
// For COPYs we don't do anything, don't increase the depth.
236236
computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
237237
Depth + (Opcode != TargetOpcode::COPY));
238-
Known = KnownBits::commonBits(Known, Known2);
238+
Known = Known.intersectWith(Known2);
239239
// If we reach a point where we don't know anything
240240
// just stop looking through the operands.
241241
if (Known.isUnknown())

llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
505505
return;
506506
}
507507
DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
508-
DestLOI.Known = KnownBits::commonBits(DestLOI.Known, SrcLOI->Known);
508+
DestLOI.Known = DestLOI.Known.intersectWith(SrcLOI->Known);
509509
}
510510
}
511511

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,7 +3051,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
30513051
}
30523052

30533053
// Known bits are the values that are shared by every demanded element.
3054-
Known = KnownBits::commonBits(Known, Known2);
3054+
Known = Known.intersectWith(Known2);
30553055

30563056
// If we don't know any bits, early out.
30573057
if (Known.isUnknown())
@@ -3074,15 +3074,15 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
30743074
if (!!DemandedLHS) {
30753075
SDValue LHS = Op.getOperand(0);
30763076
Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3077-
Known = KnownBits::commonBits(Known, Known2);
3077+
Known = Known.intersectWith(Known2);
30783078
}
30793079
// If we don't know any bits, early out.
30803080
if (Known.isUnknown())
30813081
break;
30823082
if (!!DemandedRHS) {
30833083
SDValue RHS = Op.getOperand(1);
30843084
Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3085-
Known = KnownBits::commonBits(Known, Known2);
3085+
Known = Known.intersectWith(Known2);
30863086
}
30873087
break;
30883088
}
@@ -3100,7 +3100,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
31003100
if (!!DemandedSub) {
31013101
SDValue Sub = Op.getOperand(i);
31023102
Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3103-
Known = KnownBits::commonBits(Known, Known2);
3103+
Known = Known.intersectWith(Known2);
31043104
}
31053105
// If we don't know any bits, early out.
31063106
if (Known.isUnknown())
@@ -3130,7 +3130,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
31303130
}
31313131
if (!!DemandedSrcElts) {
31323132
Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3133-
Known = KnownBits::commonBits(Known, Known2);
3133+
Known = Known.intersectWith(Known2);
31343134
}
31353135
break;
31363136
}
@@ -3220,8 +3220,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
32203220
if (DemandedElts[i]) {
32213221
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
32223222
unsigned Offset = (Shifts % SubScale) * BitWidth;
3223-
Known = KnownBits::commonBits(Known,
3224-
Known2.extractBits(BitWidth, Offset));
3223+
Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
32253224
// If we don't know any bits, early out.
32263225
if (Known.isUnknown())
32273226
break;
@@ -3319,7 +3318,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
33193318
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
33203319

33213320
// Only known if known in both the LHS and RHS.
3322-
Known = KnownBits::commonBits(Known, Known2);
3321+
Known = Known.intersectWith(Known2);
33233322
break;
33243323
case ISD::SELECT_CC:
33253324
Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
@@ -3329,7 +3328,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
33293328
Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
33303329

33313330
// Only known if known in both the LHS and RHS.
3332-
Known = KnownBits::commonBits(Known, Known2);
3331+
Known = Known.intersectWith(Known2);
33333332
break;
33343333
case ISD::SMULO:
33353334
case ISD::UMULO:
@@ -3409,8 +3408,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34093408
Known2.One.lshrInPlace(Amt);
34103409
Known2.Zero.lshrInPlace(Amt);
34113410
}
3412-
Known.One |= Known2.One;
3413-
Known.Zero |= Known2.Zero;
3411+
Known = Known.unionWith(Known2);
34143412
}
34153413
break;
34163414
case ISD::SHL_PARTS:
@@ -3780,11 +3778,11 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
37803778
Known.Zero.setAllBits();
37813779
if (DemandedVal) {
37823780
Known2 = computeKnownBits(InVal, Depth + 1);
3783-
Known = KnownBits::commonBits(Known, Known2.zextOrTrunc(BitWidth));
3781+
Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
37843782
}
37853783
if (!!DemandedVecElts) {
37863784
Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
3787-
Known = KnownBits::commonBits(Known, Known2);
3785+
Known = Known.intersectWith(Known2);
37883786
}
37893787
break;
37903788
}

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ bool TargetLowering::SimplifyDemandedBits(
11951195
return true;
11961196

11971197
if (!!DemandedVecElts)
1198-
Known = KnownBits::commonBits(Known, KnownVec);
1198+
Known = Known.intersectWith(KnownVec);
11991199

12001200
return false;
12011201
}
@@ -1223,9 +1223,9 @@ bool TargetLowering::SimplifyDemandedBits(
12231223
Known.Zero.setAllBits();
12241224
Known.One.setAllBits();
12251225
if (!!DemandedSubElts)
1226-
Known = KnownBits::commonBits(Known, KnownSub);
1226+
Known = Known.intersectWith(KnownSub);
12271227
if (!!DemandedSrcElts)
1228-
Known = KnownBits::commonBits(Known, KnownSrc);
1228+
Known = Known.intersectWith(KnownSrc);
12291229

12301230
// Attempt to avoid multi-use src if we don't need anything from it.
12311231
if (!DemandedBits.isAllOnes() || !DemandedSubElts.isAllOnes() ||
@@ -1287,7 +1287,7 @@ bool TargetLowering::SimplifyDemandedBits(
12871287
return true;
12881288
// Known bits are shared by every demanded subvector element.
12891289
if (!!DemandedSubElts)
1290-
Known = KnownBits::commonBits(Known, Known2);
1290+
Known = Known.intersectWith(Known2);
12911291
}
12921292
break;
12931293
}
@@ -1311,13 +1311,13 @@ bool TargetLowering::SimplifyDemandedBits(
13111311
if (SimplifyDemandedBits(Op0, DemandedBits, DemandedLHS, Known2, TLO,
13121312
Depth + 1))
13131313
return true;
1314-
Known = KnownBits::commonBits(Known, Known2);
1314+
Known = Known.intersectWith(Known2);
13151315
}
13161316
if (!!DemandedRHS) {
13171317
if (SimplifyDemandedBits(Op1, DemandedBits, DemandedRHS, Known2, TLO,
13181318
Depth + 1))
13191319
return true;
1320-
Known = KnownBits::commonBits(Known, Known2);
1320+
Known = Known.intersectWith(Known2);
13211321
}
13221322

13231323
// Attempt to avoid multi-use ops if we don't need anything from them.
@@ -1619,7 +1619,7 @@ bool TargetLowering::SimplifyDemandedBits(
16191619
return true;
16201620

16211621
// Only known if known in both the LHS and RHS.
1622-
Known = KnownBits::commonBits(Known, Known2);
1622+
Known = Known.intersectWith(Known2);
16231623
break;
16241624
case ISD::VSELECT:
16251625
if (SimplifyDemandedBits(Op.getOperand(2), DemandedBits, DemandedElts,
@@ -1632,7 +1632,7 @@ bool TargetLowering::SimplifyDemandedBits(
16321632
assert(!Known2.hasConflict() && "Bits known to be one AND zero?");
16331633

16341634
// Only known if known in both the LHS and RHS.
1635-
Known = KnownBits::commonBits(Known, Known2);
1635+
Known = Known.intersectWith(Known2);
16361636
break;
16371637
case ISD::SELECT_CC:
16381638
if (SimplifyDemandedBits(Op.getOperand(3), DemandedBits, Known, TLO,
@@ -1649,7 +1649,7 @@ bool TargetLowering::SimplifyDemandedBits(
16491649
return true;
16501650

16511651
// Only known if known in both the LHS and RHS.
1652-
Known = KnownBits::commonBits(Known, Known2);
1652+
Known = Known.intersectWith(Known2);
16531653
break;
16541654
case ISD::SETCC: {
16551655
SDValue Op0 = Op.getOperand(0);
@@ -1997,8 +1997,7 @@ bool TargetLowering::SimplifyDemandedBits(
19971997
Known2.Zero <<= (IsFSHL ? Amt : (BitWidth - Amt));
19981998
Known.One.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
19991999
Known.Zero.lshrInPlace(IsFSHL ? (BitWidth - Amt) : Amt);
2000-
Known.One |= Known2.One;
2001-
Known.Zero |= Known2.Zero;
2000+
Known = Known.unionWith(Known2);
20022001

20032002
// Attempt to avoid multi-use ops if we don't need anything from them.
20042003
if (!Demanded0.isAllOnes() || !Demanded1.isAllOnes() ||

llvm/lib/Support/KnownBits.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ KnownBits KnownBits::umax(const KnownBits &LHS, const KnownBits &RHS) {
129129
// are common to these two values are also known in the result.
130130
KnownBits L = LHS.makeGE(RHS.getMinValue());
131131
KnownBits R = RHS.makeGE(LHS.getMinValue());
132-
return KnownBits::commonBits(L, R);
132+
return L.intersectWith(R);
133133
}
134134

135135
KnownBits KnownBits::umin(const KnownBits &LHS, const KnownBits &RHS) {
@@ -211,7 +211,7 @@ KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS) {
211211
SpecificShift.Zero = LHS.Zero << ShiftAmt;
212212
SpecificShift.Zero.setLowBits(ShiftAmt);
213213
SpecificShift.One = LHS.One << ShiftAmt;
214-
Known = KnownBits::commonBits(Known, SpecificShift);
214+
Known = Known.intersectWith(SpecificShift);
215215
if (Known.isUnknown())
216216
break;
217217
}
@@ -267,7 +267,7 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS) {
267267
SpecificShift.Zero.lshrInPlace(ShiftAmt);
268268
SpecificShift.Zero.setHighBits(ShiftAmt);
269269
SpecificShift.One.lshrInPlace(ShiftAmt);
270-
Known = KnownBits::commonBits(Known, SpecificShift);
270+
Known = Known.intersectWith(SpecificShift);
271271
if (Known.isUnknown())
272272
break;
273273
}
@@ -327,7 +327,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS) {
327327
KnownBits SpecificShift = LHS;
328328
SpecificShift.Zero.ashrInPlace(ShiftAmt);
329329
SpecificShift.One.ashrInPlace(ShiftAmt);
330-
Known = KnownBits::commonBits(Known, SpecificShift);
330+
Known = Known.intersectWith(SpecificShift);
331331
if (Known.isUnknown())
332332
break;
333333
}

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ void AArch64TargetLowering::computeKnownBitsForTargetNode(
20642064
KnownBits Known2;
20652065
Known = DAG.computeKnownBits(Op->getOperand(0), Depth + 1);
20662066
Known2 = DAG.computeKnownBits(Op->getOperand(1), Depth + 1);
2067-
Known = KnownBits::commonBits(Known, Known2);
2067+
Known = Known.intersectWith(Known2);
20682068
break;
20692069
}
20702070
case AArch64ISD::BICi: {

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20010,7 +20010,7 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2001020010
return;
2001120011

2001220012
KnownBits KnownRHS = DAG.computeKnownBits(Op.getOperand(1), Depth+1);
20013-
Known = KnownBits::commonBits(Known, KnownRHS);
20013+
Known = Known.intersectWith(KnownRHS);
2001420014
return;
2001520015
}
2001620016
case ISD::INTRINSIC_W_CHAIN: {
@@ -20092,7 +20092,7 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2009220092
KnownOp1 = KnownBits::mul(
2009320093
KnownOp1, KnownBits::makeConstant(APInt(32, -1)));
2009420094

20095-
Known = KnownBits::commonBits(KnownOp0, KnownOp1);
20095+
Known = KnownOp0.intersectWith(KnownOp1);
2009620096
break;
2009720097
}
2009820098
}

llvm/lib/Target/Lanai/LanaiISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ void LanaiTargetLowering::computeKnownBitsForTargetNode(
14991499
KnownBits Known2;
15001500
Known = DAG.computeKnownBits(Op->getOperand(0), Depth + 1);
15011501
Known2 = DAG.computeKnownBits(Op->getOperand(1), Depth + 1);
1502-
Known = KnownBits::commonBits(Known, Known2);
1502+
Known = Known.intersectWith(Known2);
15031503
break;
15041504
}
15051505
}

0 commit comments

Comments
 (0)