Skip to content

Commit 5d0ca20

Browse files
committed
[KnownBits] Add API for nuw flag in computeForAddSub; NFC
1 parent 64422cf commit 5d0ca20

File tree

11 files changed

+60
-46
lines changed

11 files changed

+60
-46
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ struct KnownBits {
329329
const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry);
330330

331331
/// Compute known bits resulting from adding LHS and RHS.
332-
static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
333-
KnownBits RHS);
332+
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW,
333+
const KnownBits &LHS, KnownBits RHS);
334334

335335
/// Compute known bits results from subtracting RHS from LHS with 1-bit
336336
/// Borrow.

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,19 @@ unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL,
350350
}
351351

352352
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
353-
bool NSW, const APInt &DemandedElts,
353+
bool NSW, bool NUW,
354+
const APInt &DemandedElts,
354355
KnownBits &KnownOut, KnownBits &Known2,
355356
unsigned Depth, const SimplifyQuery &Q) {
356357
computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
357358

358359
// If one operand is unknown and we have no nowrap information,
359360
// the result will be unknown independently of the second operand.
360-
if (KnownOut.isUnknown() && !NSW)
361+
if (KnownOut.isUnknown() && !NSW && !NUW)
361362
return;
362363

363364
computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
364-
KnownOut = KnownBits::computeForAddSub(Add, NSW, Known2, KnownOut);
365+
KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
365366
}
366367

367368
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
@@ -1145,13 +1146,15 @@ static void computeKnownBitsFromOperator(const Operator *I,
11451146
}
11461147
case Instruction::Sub: {
11471148
bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1148-
computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
1149+
bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1150+
computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
11491151
DemandedElts, Known, Known2, Depth, Q);
11501152
break;
11511153
}
11521154
case Instruction::Add: {
11531155
bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1154-
computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
1156+
bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1157+
computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
11551158
DemandedElts, Known, Known2, Depth, Q);
11561159
break;
11571160
}
@@ -1245,12 +1248,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
12451248
// Note that inbounds does *not* guarantee nsw for the addition, as only
12461249
// the offset is signed, while the base address is unsigned.
12471250
Known = KnownBits::computeForAddSub(
1248-
/*Add=*/true, /*NSW=*/false, Known, IndexBits);
1251+
/*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, IndexBits);
12491252
}
12501253
if (!Known.isUnknown() && !AccConstIndices.isZero()) {
12511254
KnownBits Index = KnownBits::makeConstant(AccConstIndices);
12521255
Known = KnownBits::computeForAddSub(
1253-
/*Add=*/true, /*NSW=*/false, Known, Index);
1256+
/*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, Index);
12541257
}
12551258
break;
12561259
}
@@ -1689,15 +1692,15 @@ static void computeKnownBitsFromOperator(const Operator *I,
16891692
default: break;
16901693
case Intrinsic::uadd_with_overflow:
16911694
case Intrinsic::sadd_with_overflow:
1692-
computeKnownBitsAddSub(true, II->getArgOperand(0),
1693-
II->getArgOperand(1), false, DemandedElts,
1694-
Known, Known2, Depth, Q);
1695+
computeKnownBitsAddSub(
1696+
true, II->getArgOperand(0), II->getArgOperand(1), /*NSW*/ false,
1697+
/* NUW*/ false, DemandedElts, Known, Known2, Depth, Q);
16951698
break;
16961699
case Intrinsic::usub_with_overflow:
16971700
case Intrinsic::ssub_with_overflow:
1698-
computeKnownBitsAddSub(false, II->getArgOperand(0),
1699-
II->getArgOperand(1), false, DemandedElts,
1700-
Known, Known2, Depth, Q);
1701+
computeKnownBitsAddSub(
1702+
false, II->getArgOperand(0), II->getArgOperand(1), /*NSW*/ false,
1703+
/* NUW*/ false, DemandedElts, Known, Known2, Depth, Q);
17011704
break;
17021705
case Intrinsic::umul_with_overflow:
17031706
case Intrinsic::smul_with_overflow:
@@ -2318,7 +2321,11 @@ static bool isNonZeroRecurrence(const PHINode *PN) {
23182321

23192322
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
23202323
const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2321-
Value *Y, bool NSW) {
2324+
Value *Y, bool NSW, bool NUW) {
2325+
if (NUW)
2326+
return isKnownNonZero(Y, DemandedElts, Depth, Q) ||
2327+
isKnownNonZero(X, DemandedElts, Depth, Q);
2328+
23222329
KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
23232330
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
23242331

@@ -2351,7 +2358,7 @@ static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
23512358
isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
23522359
return true;
23532360

2354-
return KnownBits::computeForAddSub(/*Add*/ true, NSW, XKnown, YKnown)
2361+
return KnownBits::computeForAddSub(/*Add*/ true, NSW, NUW, XKnown, YKnown)
23552362
.isNonZero();
23562363
}
23572364

@@ -2556,12 +2563,9 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
25562563
// If Add has nuw wrap flag, then if either X or Y is non-zero the result is
25572564
// non-zero.
25582565
auto *BO = cast<OverflowingBinaryOperator>(I);
2559-
if (Q.IIQ.hasNoUnsignedWrap(BO))
2560-
return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) ||
2561-
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
2562-
25632566
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2564-
I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO));
2567+
I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2568+
Q.IIQ.hasNoUnsignedWrap(BO));
25652569
}
25662570
case Instruction::Mul: {
25672571
// If X and Y are non-zero then so is X * Y as long as the multiplication
@@ -2716,7 +2720,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
27162720
case Intrinsic::sadd_sat:
27172721
return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
27182722
II->getArgOperand(0), II->getArgOperand(1),
2719-
/*NSW*/ true);
2723+
/*NSW*/ true, /* NUW*/ false);
27202724
case Intrinsic::umax:
27212725
case Intrinsic::uadd_sat:
27222726
return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) ||

llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
269269
Depth + 1);
270270
computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
271271
Depth + 1);
272-
Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false, Known,
273-
Known2);
272+
Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false,
273+
/* NUW*/ false, Known, Known2);
274274
break;
275275
}
276276
case TargetOpcode::G_XOR: {
@@ -296,8 +296,8 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
296296
Depth + 1);
297297
computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
298298
Depth + 1);
299-
Known =
300-
KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false, Known, Known2);
299+
Known = KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false,
300+
/* NUW*/ false, Known, Known2);
301301
break;
302302
}
303303
case TargetOpcode::G_AND: {
@@ -564,7 +564,7 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
564564
// right.
565565
KnownBits ExtKnown = KnownBits::makeConstant(APInt(BitWidth, BitWidth));
566566
KnownBits ShiftKnown = KnownBits::computeForAddSub(
567-
/*Add*/ false, /*NSW*/ false, ExtKnown, WidthKnown);
567+
/*Add*/ false, /*NSW*/ false, /* NUW*/ false, ExtKnown, WidthKnown);
568568
Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
569569
break;
570570
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,8 +3753,9 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
37533753
SDNodeFlags Flags = Op.getNode()->getFlags();
37543754
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
37553755
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3756-
Known = KnownBits::computeForAddSub(Op.getOpcode() == ISD::ADD,
3757-
Flags.hasNoSignedWrap(), Known, Known2);
3756+
Known = KnownBits::computeForAddSub(
3757+
Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3758+
Flags.hasNoUnsignedWrap(), Known, Known2);
37583759
break;
37593760
}
37603761
case ISD::USUBO:

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,9 +2876,9 @@ bool TargetLowering::SimplifyDemandedBits(
28762876
if (Op.getOpcode() == ISD::MUL) {
28772877
Known = KnownBits::mul(KnownOp0, KnownOp1);
28782878
} else { // Op.getOpcode() is either ISD::ADD or ISD::SUB.
2879-
Known = KnownBits::computeForAddSub(Op.getOpcode() == ISD::ADD,
2880-
Flags.hasNoSignedWrap(), KnownOp0,
2881-
KnownOp1);
2879+
Known = KnownBits::computeForAddSub(
2880+
Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
2881+
Flags.hasNoUnsignedWrap(), KnownOp0, KnownOp1);
28822882
}
28832883
break;
28842884
}

llvm/lib/Support/KnownBits.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ KnownBits KnownBits::computeForAddCarry(
5454
LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
5555
}
5656

57-
KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
57+
KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, bool /*NUW*/,
5858
const KnownBits &LHS, KnownBits RHS) {
5959
KnownBits KnownOut;
6060
if (Add) {
@@ -443,7 +443,7 @@ KnownBits KnownBits::abs(bool IntMinIsPoison) const {
443443
Tmp.One.setBit(countMinTrailingZeros());
444444

445445
KnownAbs = computeForAddSub(
446-
/*Add*/ false, IntMinIsPoison,
446+
/*Add*/ false, IntMinIsPoison, /*NUW*/ false,
447447
KnownBits::makeConstant(APInt(getBitWidth(), 0)), Tmp);
448448

449449
// One more special case for IntMinIsPoison. If we don't know any ones other
@@ -489,7 +489,8 @@ static KnownBits computeForSatAddSub(bool Add, bool Signed,
489489
assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
490490
// We don't see NSW even for sadd/ssub as we want to check if the result has
491491
// signed overflow.
492-
KnownBits Res = KnownBits::computeForAddSub(Add, /*NSW*/ false, LHS, RHS);
492+
KnownBits Res =
493+
KnownBits::computeForAddSub(Add, /*NSW*/ false, /*NUW*/ false, LHS, RHS);
493494
unsigned BitWidth = Res.getBitWidth();
494495
auto SignBitKnown = [&](const KnownBits &K) {
495496
return K.Zero[BitWidth - 1] || K.One[BitWidth - 1];

llvm/lib/Target/AMDGPU/AMDGPUISelDAGToDAG.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,8 @@ bool AMDGPUDAGToDAGISel::checkFlatScratchSVSSwizzleBug(
19031903
// voffset to (soffset + inst_offset).
19041904
KnownBits VKnown = CurDAG->computeKnownBits(VAddr);
19051905
KnownBits SKnown = KnownBits::computeForAddSub(
1906-
true, false, CurDAG->computeKnownBits(SAddr),
1906+
/*Add*/ true, /*NSW*/ false, /*NUW*/ false,
1907+
CurDAG->computeKnownBits(SAddr),
19071908
KnownBits::makeConstant(APInt(32, ImmOffset)));
19081909
uint64_t VMax = VKnown.getMaxValue().getZExtValue();
19091910
uint64_t SMax = SKnown.getMaxValue().getZExtValue();

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4581,7 +4581,7 @@ bool AMDGPUInstructionSelector::checkFlatScratchSVSSwizzleBug(
45814581
// voffset to (soffset + inst_offset).
45824582
auto VKnown = KB->getKnownBits(VAddr);
45834583
auto SKnown = KnownBits::computeForAddSub(
4584-
true, false, KB->getKnownBits(SAddr),
4584+
/*Add*/ true, /*NSW*/ false, /*NUW*/ false, KB->getKnownBits(SAddr),
45854585
KnownBits::makeConstant(APInt(32, ImmOffset)));
45864586
uint64_t VMax = VKnown.getMaxValue().getZExtValue();
45874587
uint64_t SMax = SKnown.getMaxValue().getZExtValue();

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20156,7 +20156,8 @@ void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2015620156
// CSNEG: KnownOp0 or KnownOp1 * -1
2015720157
if (Op.getOpcode() == ARMISD::CSINC)
2015820158
KnownOp1 = KnownBits::computeForAddSub(
20159-
true, false, KnownOp1, KnownBits::makeConstant(APInt(32, 1)));
20159+
/*Add*/ true, /*NSW*/ false, /*NUW*/ false, KnownOp1,
20160+
KnownBits::makeConstant(APInt(32, 1)));
2016020161
else if (Op.getOpcode() == ARMISD::CSINV)
2016120162
std::swap(KnownOp1.Zero, KnownOp1.One);
2016220163
else if (Op.getOpcode() == ARMISD::CSNEG)

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
565565

566566
// Otherwise just compute the known bits of the result.
567567
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
568-
Known = KnownBits::computeForAddSub(true, NSW, LHSKnown, RHSKnown);
568+
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
569+
Known = KnownBits::computeForAddSub(true, NSW, NUW, LHSKnown, RHSKnown);
569570
break;
570571
}
571572
case Instruction::Sub: {
@@ -598,7 +599,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
598599

599600
// Otherwise just compute the known bits of the result.
600601
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
601-
Known = KnownBits::computeForAddSub(false, NSW, LHSKnown, RHSKnown);
602+
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
603+
Known = KnownBits::computeForAddSub(false, NSW, NUW, LHSKnown, RHSKnown);
602604
break;
603605
}
604606
case Instruction::Mul: {
@@ -1206,7 +1208,9 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
12061208
return I->getOperand(1);
12071209

12081210
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1209-
Known = KnownBits::computeForAddSub(/*Add*/ true, NSW, LHSKnown, RHSKnown);
1211+
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
1212+
Known =
1213+
KnownBits::computeForAddSub(/*Add*/ true, NSW, NUW, LHSKnown, RHSKnown);
12101214
computeKnownBitsFromContext(I, Known, Depth, SQ.getWithInstruction(CxtI));
12111215
break;
12121216
}
@@ -1221,8 +1225,10 @@ Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits(
12211225
return I->getOperand(0);
12221226

12231227
bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap();
1228+
bool NUW = cast<OverflowingBinaryOperator>(I)->hasNoUnsignedWrap();
12241229
computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI);
1225-
Known = KnownBits::computeForAddSub(/*Add*/ false, NSW, LHSKnown, RHSKnown);
1230+
Known = KnownBits::computeForAddSub(/*Add*/ false, NSW, NUW, LHSKnown,
1231+
RHSKnown);
12261232
computeKnownBitsFromContext(I, Known, Depth, SQ.getWithInstruction(CxtI));
12271233
break;
12281234
}

llvm/unittests/Support/KnownBitsTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ static void TestAddSubExhaustive(bool IsAdd) {
194194
});
195195
});
196196

197-
KnownBits KnownComputed =
198-
KnownBits::computeForAddSub(IsAdd, /*NSW*/ false, Known1, Known2);
197+
KnownBits KnownComputed = KnownBits::computeForAddSub(
198+
IsAdd, /*NSW*/ false, /*NUW*/ false, Known1, Known2);
199199
EXPECT_EQ(Known, KnownComputed);
200200

201201
// The NSW calculation is not precise, only check that it's
202202
// conservatively correct.
203203
KnownBits KnownNSWComputed = KnownBits::computeForAddSub(
204-
IsAdd, /*NSW*/true, Known1, Known2);
204+
IsAdd, /*NSW*/ true, /*NUW*/ false, Known1, Known2);
205205
EXPECT_TRUE(KnownNSWComputed.Zero.isSubsetOf(KnownNSW.Zero));
206206
EXPECT_TRUE(KnownNSWComputed.One.isSubsetOf(KnownNSW.One));
207207
});

0 commit comments

Comments
 (0)