Skip to content

Commit a9d913e

Browse files
committed
[KnownBits] Add API support for exact in lshr/ashr; NFC
1 parent f19d9e1 commit a9d913e

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

llvm/include/llvm/Support/KnownBits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ struct KnownBits {
402402
/// Compute known bits for lshr(LHS, RHS).
403403
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
404404
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS,
405-
bool ShAmtNonZero = false);
405+
bool ShAmtNonZero = false, bool Exact = false);
406406

407407
/// Compute known bits for ashr(LHS, RHS).
408408
/// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
409409
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS,
410-
bool ShAmtNonZero = false);
410+
bool ShAmtNonZero = false, bool Exact = false);
411411

412412
/// Determine if these known bits always give the same ICMP_EQ result.
413413
static std::optional<bool> eq(const KnownBits &LHS, const KnownBits &RHS);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,9 +1142,10 @@ static void computeKnownBitsFromOperator(const Operator *I,
11421142
break;
11431143
}
11441144
case Instruction::LShr: {
1145-
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1146-
bool ShAmtNonZero) {
1147-
return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero);
1145+
bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1146+
auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1147+
bool ShAmtNonZero) {
1148+
return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
11481149
};
11491150
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
11501151
KF);
@@ -1155,9 +1156,10 @@ static void computeKnownBitsFromOperator(const Operator *I,
11551156
break;
11561157
}
11571158
case Instruction::AShr: {
1158-
auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1159-
bool ShAmtNonZero) {
1160-
return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero);
1159+
bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1160+
auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1161+
bool ShAmtNonZero) {
1162+
return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
11611163
};
11621164
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
11631165
KF);

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,8 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34853485
case ISD::SRL:
34863486
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
34873487
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3488-
Known = KnownBits::lshr(Known, Known2);
3488+
Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3489+
Op->getFlags().hasExact());
34893490

34903491
// Minimum shift high bits are known zero.
34913492
if (const APInt *ShMinAmt =
@@ -3495,7 +3496,8 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
34953496
case ISD::SRA:
34963497
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
34973498
Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3498-
Known = KnownBits::ashr(Known, Known2);
3499+
Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3500+
Op->getFlags().hasExact());
34993501
break;
35003502
case ISD::FSHL:
35013503
case ISD::FSHR:

llvm/lib/Support/KnownBits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW,
343343
}
344344

345345
KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
346-
bool ShAmtNonZero) {
346+
bool ShAmtNonZero, bool /*Exact*/) {
347347
unsigned BitWidth = LHS.getBitWidth();
348348
auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
349349
KnownBits Known = LHS;
@@ -389,7 +389,7 @@ KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS,
389389
}
390390

391391
KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
392-
bool ShAmtNonZero) {
392+
bool ShAmtNonZero, bool /*Exact*/) {
393393
unsigned BitWidth = LHS.getBitWidth();
394394
auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
395395
KnownBits Known = LHS;

0 commit comments

Comments
 (0)