Skip to content

Commit f6a326a

Browse files
committed
[ValueTracking] computeKnownBitsFromShiftOperator - merge zero/one callbacks to single KnownBits callback. NFCI.
Another cleanup for D90479 - handle the Known Ones/Zeros in a single callback, which will make it much easier to jump over to the KnownBits shift handling.
1 parent 3109ce5 commit f6a326a

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -979,25 +979,22 @@ static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
979979
/// Compute known bits from a shift operator, including those with a
980980
/// non-constant shift amount. Known is the output of this function. Known2 is a
981981
/// pre-allocated temporary with the same bit width as Known and on return
982-
/// contains the known bit of the shift value source. KZF and KOF are
983-
/// operator-specific functions that, given the known-zero or known-one bits
984-
/// respectively, and a shift amount, compute the implied known-zero or
985-
/// known-one bits of the shift operator's result respectively for that shift
986-
/// amount. The results from calling KZF and KOF are conservatively combined for
987-
/// all permitted shift amounts.
982+
/// contains the known bit of the shift value source. KF is an
983+
/// operator-specific function that, given the known-bits and a shift amount,
984+
/// compute the implied known-bits of the shift operator's result respectively
985+
/// for that shift amount. The results from calling KF are conservatively
986+
/// combined for all permitted shift amounts.
988987
static void computeKnownBitsFromShiftOperator(
989988
const Operator *I, const APInt &DemandedElts, KnownBits &Known,
990989
KnownBits &Known2, unsigned Depth, const Query &Q,
991-
function_ref<APInt(const APInt &, unsigned)> KZF,
992-
function_ref<APInt(const APInt &, unsigned)> KOF) {
990+
function_ref<KnownBits(const KnownBits &, unsigned)> KF) {
993991
unsigned BitWidth = Known.getBitWidth();
994992
computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
995993
computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
996994

997995
if (Known.isConstant()) {
998996
unsigned ShiftAmt = Known.getConstant().getLimitedValue(BitWidth - 1);
999-
Known.Zero = KZF(Known2.Zero, ShiftAmt);
1000-
Known.One = KOF(Known2.One, ShiftAmt);
997+
Known = KF(Known2, ShiftAmt);
1001998

1002999
// If the known bits conflict, this must be an overflowing left shift, so
10031000
// the shift result is poison. We can return anything we want. Choose 0 for
@@ -1061,8 +1058,7 @@ static void computeKnownBitsFromShiftOperator(
10611058
continue;
10621059
}
10631060

1064-
Known.Zero &= KZF(Known2.Zero, ShiftAmt);
1065-
Known.One &= KOF(Known2.One, ShiftAmt);
1061+
Known = KnownBits::commonBits(Known, KF(Known2, ShiftAmt));
10661062
}
10671063

10681064
// If the known bits conflict, the result is poison. Return a 0 and hope the
@@ -1227,56 +1223,50 @@ static void computeKnownBitsFromOperator(const Operator *I,
12271223
case Instruction::Shl: {
12281224
// (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
12291225
bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1230-
auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) {
1231-
APInt KZResult = KnownZero << ShiftAmt;
1232-
KZResult.setLowBits(ShiftAmt); // Low bits known 0.
1226+
auto KF = [NSW](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
1227+
KnownBits Result;
1228+
Result.Zero = KnownShiftVal.Zero << ShiftAmt;
1229+
Result.One = KnownShiftVal.One << ShiftAmt;
1230+
// Low bits known zero.
1231+
Result.Zero.setLowBits(ShiftAmt);
12331232
// If this shift has "nsw" keyword, then the result is either a poison
12341233
// value or has the same sign bit as the first operand.
1235-
if (NSW && KnownZero.isSignBitSet())
1236-
KZResult.setSignBit();
1237-
return KZResult;
1238-
};
1239-
1240-
auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) {
1241-
APInt KOResult = KnownOne << ShiftAmt;
1242-
if (NSW && KnownOne.isSignBitSet())
1243-
KOResult.setSignBit();
1244-
return KOResult;
1234+
if (NSW) {
1235+
if (KnownShiftVal.Zero.isSignBitSet())
1236+
Result.Zero.setSignBit();
1237+
if (KnownShiftVal.One.isSignBitSet())
1238+
Result.One.setSignBit();
1239+
}
1240+
return Result;
12451241
};
1246-
12471242
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1248-
KZF, KOF);
1243+
KF);
12491244
break;
12501245
}
12511246
case Instruction::LShr: {
12521247
// (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1253-
auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1254-
APInt KZResult = KnownZero.lshr(ShiftAmt);
1248+
auto KF = [](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
1249+
KnownBits Result;
1250+
Result.Zero = KnownShiftVal.Zero.lshr(ShiftAmt);
1251+
Result.One = KnownShiftVal.One.lshr(ShiftAmt);
12551252
// High bits known zero.
1256-
KZResult.setHighBits(ShiftAmt);
1257-
return KZResult;
1258-
};
1259-
1260-
auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1261-
return KnownOne.lshr(ShiftAmt);
1253+
Result.Zero.setHighBits(ShiftAmt);
1254+
return Result;
12621255
};
1263-
12641256
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1265-
KZF, KOF);
1257+
KF);
12661258
break;
12671259
}
12681260
case Instruction::AShr: {
12691261
// (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1270-
auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
1271-
return KnownZero.ashr(ShiftAmt);
1262+
auto KF = [](const KnownBits &KnownShiftVal, unsigned ShiftAmt) {
1263+
KnownBits Result;
1264+
Result.Zero = KnownShiftVal.Zero.ashr(ShiftAmt);
1265+
Result.One = KnownShiftVal.One.ashr(ShiftAmt);
1266+
return Result;
12721267
};
1273-
1274-
auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
1275-
return KnownOne.ashr(ShiftAmt);
1276-
};
1277-
12781268
computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1279-
KZF, KOF);
1269+
KF);
12801270
break;
12811271
}
12821272
case Instruction::Sub: {

0 commit comments

Comments
 (0)