Skip to content

Commit 11ba72e

Browse files
authored
[KnownBits] Add KnownBits::add and KnownBits::sub helper wrappers. (#99468)
1 parent db0603c commit 11ba72e

File tree

9 files changed

+49
-42
lines changed

9 files changed

+49
-42
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ struct KnownBits {
329329
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
330330
const KnownBits &Borrow);
331331

332+
/// Compute knownbits resulting from addition of LHS and RHS.
333+
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS,
334+
bool NSW = false, bool NUW = false) {
335+
return computeForAddSub(/*Add=*/true, NSW, NUW, LHS, RHS);
336+
}
337+
338+
/// Compute knownbits resulting from subtraction of LHS and RHS.
339+
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS,
340+
bool NSW = false, bool NUW = false) {
341+
return computeForAddSub(/*Add=*/false, NSW, NUW, LHS, RHS);
342+
}
343+
332344
/// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
333345
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS);
334346

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,13 +1399,11 @@ static void computeKnownBitsFromOperator(const Operator *I,
13991399

14001400
// Note that inbounds does *not* guarantee nsw for the addition, as only
14011401
// the offset is signed, while the base address is unsigned.
1402-
Known = KnownBits::computeForAddSub(
1403-
/*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, IndexBits);
1402+
Known = KnownBits::add(Known, IndexBits);
14041403
}
14051404
if (!Known.isUnknown() && !AccConstIndices.isZero()) {
14061405
KnownBits Index = KnownBits::makeConstant(AccConstIndices);
1407-
Known = KnownBits::computeForAddSub(
1408-
/*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, Index);
1406+
Known = KnownBits::add(Known, Index);
14091407
}
14101408
break;
14111409
}
@@ -1802,9 +1800,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
18021800
Known = computeKnownBitsForHorizontalOperation(
18031801
I, DemandedElts, Depth, Q,
18041802
[](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1805-
return KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
1806-
/*NUW=*/false, KnownLHS,
1807-
KnownRHS);
1803+
return KnownBits::add(KnownLHS, KnownRHS);
18081804
});
18091805
break;
18101806
}
@@ -1821,9 +1817,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
18211817
Known = computeKnownBitsForHorizontalOperation(
18221818
I, DemandedElts, Depth, Q,
18231819
[](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1824-
return KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
1825-
/*NUW=*/false, KnownLHS,
1826-
KnownRHS);
1820+
return KnownBits::sub(KnownLHS, KnownRHS);
18271821
});
18281822
break;
18291823
}
@@ -2642,8 +2636,7 @@ static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
26422636
isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
26432637
return true;
26442638

2645-
return KnownBits::computeForAddSub(/*Add=*/true, NSW, NUW, XKnown, YKnown)
2646-
.isNonZero();
2639+
return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
26472640
}
26482641

26492642
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
271271
Depth + 1);
272272
computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
273273
Depth + 1);
274-
Known = KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
275-
/* NUW=*/false, Known, Known2);
274+
Known = KnownBits::sub(Known, Known2);
276275
break;
277276
}
278277
case TargetOpcode::G_XOR: {
@@ -298,8 +297,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
298297
Depth + 1);
299298
computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
300299
Depth + 1);
301-
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
302-
/* NUW=*/false, Known, Known2);
300+
Known = KnownBits::add(Known, Known2);
303301
break;
304302
}
305303
case TargetOpcode::G_AND: {
@@ -571,8 +569,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
571569
// Sign extend the extracted value using shift left and arithmetic shift
572570
// right.
573571
KnownBits ExtKnown = KnownBits::makeConstant(APInt(BitWidth, BitWidth));
574-
KnownBits ShiftKnown = KnownBits::computeForAddSub(
575-
/*Add=*/false, /*NSW=*/false, /* NUW=*/false, ExtKnown, WidthKnown);
572+
KnownBits ShiftKnown = KnownBits::sub(ExtKnown, WidthKnown);
576573
Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
577574
break;
578575
}

llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,10 +1928,9 @@ bool AMDGPUDAGToDAGISel::checkFlatScratchSVSSwizzleBug(
19281928
// from the two low order bits (i.e. from bit 1 into bit 2) when adding
19291929
// voffset to (soffset + inst_offset).
19301930
KnownBits VKnown = CurDAG->computeKnownBits(VAddr);
1931-
KnownBits SKnown = KnownBits::computeForAddSub(
1932-
/*Add=*/true, /*NSW=*/false, /*NUW=*/false,
1933-
CurDAG->computeKnownBits(SAddr),
1934-
KnownBits::makeConstant(APInt(32, ImmOffset)));
1931+
KnownBits SKnown =
1932+
KnownBits::add(CurDAG->computeKnownBits(SAddr),
1933+
KnownBits::makeConstant(APInt(32, ImmOffset)));
19351934
uint64_t VMax = VKnown.getMaxValue().getZExtValue();
19361935
uint64_t SMax = SKnown.getMaxValue().getZExtValue();
19371936
return (VMax & 3) + (SMax & 3) >= 4;

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,9 +4466,8 @@ bool AMDGPUInstructionSelector::checkFlatScratchSVSSwizzleBug(
44664466
// from the two low order bits (i.e. from bit 1 into bit 2) when adding
44674467
// voffset to (soffset + inst_offset).
44684468
auto VKnown = KB->getKnownBits(VAddr);
4469-
auto SKnown = KnownBits::computeForAddSub(
4470-
/*Add=*/true, /*NSW=*/false, /*NUW=*/false, KB->getKnownBits(SAddr),
4471-
KnownBits::makeConstant(APInt(32, ImmOffset)));
4469+
auto SKnown = KnownBits::add(KB->getKnownBits(SAddr),
4470+
KnownBits::makeConstant(APInt(32, ImmOffset)));
44724471
uint64_t VMax = VKnown.getMaxValue().getZExtValue();
44734472
uint64_t SMax = SKnown.getMaxValue().getZExtValue();
44744473
return (VMax & 3) + (SMax & 3) >= 4;

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20186,9 +20186,8 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2018620186
// CSINV: KnownOp0 or ~KnownOp1
2018720187
// CSNEG: KnownOp0 or KnownOp1 * -1
2018820188
if (Op.getOpcode() == ARMISD::CSINC)
20189-
KnownOp1 = KnownBits::computeForAddSub(
20190-
/*Add=*/true, /*NSW=*/false, /*NUW=*/false, KnownOp1,
20191-
KnownBits::makeConstant(APInt(32, 1)));
20189+
KnownOp1 =
20190+
KnownBits::add(KnownOp1, KnownBits::makeConstant(APInt(32, 1)));
2019220191
else if (Op.getOpcode() == ARMISD::CSINV)
2019320192
std::swap(KnownOp1.Zero, KnownOp1.One);
2019420193
else if (Op.getOpcode() == ARMISD::CSNEG)

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37135,12 +37135,9 @@ static void computeKnownBitsForPSADBW(SDValue LHS, SDValue RHS,
3713537135
Known2 = DAG.computeKnownBits(LHS, DemandedSrcElts, Depth + 1);
3713637136
Known = KnownBits::abdu(Known, Known2).zext(16);
3713737137
// Known = (((D0 + D1) + (D2 + D3)) + ((D4 + D5) + (D6 + D7)))
37138-
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/true, /*NUW=*/true,
37139-
Known, Known);
37140-
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/true, /*NUW=*/true,
37141-
Known, Known);
37142-
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/true, /*NUW=*/true,
37143-
Known, Known);
37138+
Known = KnownBits::add(Known, Known, /*NSW=*/true, /*NUW=*/true);
37139+
Known = KnownBits::add(Known, Known, /*NSW=*/true, /*NUW=*/true);
37140+
Known = KnownBits::add(Known, Known, /*NSW=*/true, /*NUW=*/true);
3714437141
Known = Known.zext(64);
3714537142
}
3714637143

@@ -37163,8 +37160,7 @@ static void computeKnownBitsForPMADDWD(SDValue LHS, SDValue RHS,
3716337160
KnownBits RHSHi = DAG.computeKnownBits(RHS, DemandedHiElts, Depth + 1);
3716437161
KnownBits Lo = KnownBits::mul(LHSLo.sext(32), RHSLo.sext(32));
3716537162
KnownBits Hi = KnownBits::mul(LHSHi.sext(32), RHSHi.sext(32));
37166-
Known = KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
37167-
/*NUW=*/false, Lo, Hi);
37163+
Known = KnownBits::add(Lo, Hi, /*NSW=*/false, /*NUW=*/false);
3716837164
}
3716937165

3717037166
static void computeKnownBitsForPMADDUBSW(SDValue LHS, SDValue RHS,

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
561561
// Otherwise just compute the known bits of the result.
562562
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
563563
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
564-
Known = KnownBits::computeForAddSub(true, NSW, NUW, LHSKnown, RHSKnown);
564+
Known = KnownBits::add(LHSKnown, RHSKnown, NSW, NUW);
565565
break;
566566
}
567567
case Instruction::Sub: {
@@ -595,7 +595,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
595595
// Otherwise just compute the known bits of the result.
596596
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
597597
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
598-
Known = KnownBits::computeForAddSub(false, NSW, NUW, LHSKnown, RHSKnown);
598+
Known = KnownBits::sub(LHSKnown, RHSKnown, NSW, NUW);
599599
break;
600600
}
601601
case Instruction::Mul: {
@@ -1232,8 +1232,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
12321232

12331233
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
12341234
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
1235-
Known =
1236-
KnownBits::computeForAddSub(/*Add=*/true, NSW, NUW, LHSKnown, RHSKnown);
1235+
Known = KnownBits::add(LHSKnown, RHSKnown, NSW, NUW);
12371236
computeKnownBitsFromContext(I, Known, Depth, Q);
12381237
break;
12391238
}
@@ -1250,8 +1249,7 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
12501249
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
12511250
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
12521251
llvm::computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, Q);
1253-
Known = KnownBits::computeForAddSub(/*Add=*/false, NSW, NUW, LHSKnown,
1254-
RHSKnown);
1252+
Known = KnownBits::sub(LHSKnown, RHSKnown, NSW, NUW);
12551253
computeKnownBitsFromContext(I, Known, Depth, Q);
12561254
break;
12571255
}

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ TEST(KnownBitsTest, BinaryExhaustive) {
300300
return Known1 ^ Known2;
301301
},
302302
[](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
303+
testBinaryOpExhaustive(
304+
"add",
305+
[](const KnownBits &Known1, const KnownBits &Known2) {
306+
return KnownBits::add(Known1, Known2);
307+
},
308+
[](const APInt &N1, const APInt &N2) { return N1 + N2; },
309+
/*CheckOptimality=*/false);
310+
testBinaryOpExhaustive(
311+
"sub",
312+
[](const KnownBits &Known1, const KnownBits &Known2) {
313+
return KnownBits::sub(Known1, Known2);
314+
},
315+
[](const APInt &N1, const APInt &N2) { return N1 - N2; },
316+
/*CheckOptimality=*/false);
303317
testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
304318
testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
305319
testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);

0 commit comments

Comments
 (0)