Skip to content

Commit b58ae6b

Browse files
committed
[InstCombine] Sync KnownBits logic for select arms
Extract an adjustKnownBitsForSelectArm() helper for the ValueTracking logic and make use of it in SimplifyDemandedBits(). This fixes a consistency violation under instcombine-verify-known-bits.
1 parent d698760 commit b58ae6b

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I,
100100
const KnownBits &KnownRHS,
101101
unsigned Depth, const SimplifyQuery &SQ);
102102

103+
/// Adjust \p Known for the given select \p Arm to include information from the
104+
/// select \p Cond.
105+
void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm,
106+
bool Invert, unsigned Depth,
107+
const SimplifyQuery &Q);
108+
103109
/// Return true if LHS and RHS have no common bits set.
104110
bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,
105111
const WithCache<const Value *> &RHSCache,

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,40 @@ ConstantRange llvm::getVScaleRange(const Function *F, unsigned BitWidth) {
992992
return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
993993
}
994994

995+
void llvm::adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond,
996+
Value *Arm, bool Invert, unsigned Depth,
997+
const SimplifyQuery &Q) {
998+
// If we have a constant arm, we are done.
999+
if (Known.isConstant())
1000+
return;
1001+
1002+
// See what condition implies about the bits of the select arm.
1003+
KnownBits CondRes(Known.getBitWidth());
1004+
computeKnownBitsFromCond(Arm, Cond, CondRes, Depth + 1, Q, Invert);
1005+
// If we don't get any information from the condition, no reason to
1006+
// proceed.
1007+
if (CondRes.isUnknown())
1008+
return;
1009+
1010+
// We can have conflict if the condition is dead. I.e if we have
1011+
// (x | 64) < 32 ? (x | 64) : y
1012+
// we will have conflict at bit 6 from the condition/the `or`.
1013+
// In that case just return. Its not particularly important
1014+
// what we do, as this select is going to be simplified soon.
1015+
CondRes = CondRes.unionWith(Known);
1016+
if (CondRes.hasConflict())
1017+
return;
1018+
1019+
// Finally make sure the information we found is valid. This is relatively
1020+
// expensive so it's left for the very end.
1021+
if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1022+
return;
1023+
1024+
// Finally, we know we get information from the condition and its valid,
1025+
// so return it.
1026+
Known = CondRes;
1027+
}
1028+
9951029
static void computeKnownBitsFromOperator(const Operator *I,
9961030
const APInt &DemandedElts,
9971031
KnownBits &Known, unsigned Depth,
@@ -1048,36 +1082,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
10481082
auto ComputeForArm = [&](Value *Arm, bool Invert) {
10491083
KnownBits Res(Known.getBitWidth());
10501084
computeKnownBits(Arm, Res, Depth + 1, Q);
1051-
// If we have a constant arm, we are done.
1052-
if (Res.isConstant())
1053-
return Res;
1054-
1055-
// See what condition implies about the bits of the two select arms.
1056-
KnownBits CondRes(Res.getBitWidth());
1057-
computeKnownBitsFromCond(Arm, I->getOperand(0), CondRes, Depth + 1, Q,
1058-
Invert);
1059-
// If we don't get any information from the condition, no reason to
1060-
// proceed.
1061-
if (CondRes.isUnknown())
1062-
return Res;
1063-
1064-
// We can have conflict if the condition is dead. I.e if we have
1065-
// (x | 64) < 32 ? (x | 64) : y
1066-
// we will have conflict at bit 6 from the condition/the `or`.
1067-
// In that case just return. Its not particularly important
1068-
// what we do, as this select is going to be simplified soon.
1069-
CondRes = CondRes.unionWith(Res);
1070-
if (CondRes.hasConflict())
1071-
return Res;
1072-
1073-
// Finally make sure the information we found is valid. This is relatively
1074-
// expensive so it's left for the very end.
1075-
if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1076-
return Res;
1077-
1078-
// Finally, we know we get information from the condition and its valid,
1079-
// so return it.
1080-
return CondRes;
1085+
adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Depth, Q);
1086+
return Res;
10811087
};
10821088
// Only known if known in both the LHS and RHS.
10831089
Known =

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
408408
return I;
409409

410410
// Only known if known in both the LHS and RHS.
411+
adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(1),
412+
/*Invert=*/false, Depth, Q);
413+
adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(2),
414+
/*Invert=*/true, Depth, Q);
411415
Known = LHSKnown.intersectWith(RHSKnown);
412416
break;
413417
}

0 commit comments

Comments
 (0)