-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[SelectionDAG] Detect impossible conditions using known bits analysis #150715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13529,6 +13529,78 @@ SDValue DAGCombiner::visitSETCC(SDNode *N) { | |
SDValue N0 = N->getOperand(0), N1 = N->getOperand(1); | ||
SDLoc DL(N); | ||
|
||
// Detect impossible conditions using known bits analysis. | ||
if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) { | ||
const APInt &C1 = N1C->getAPIntValue(); | ||
KnownBits KnownRHS = KnownBits::makeConstant(C1); | ||
|
||
// Bail out early if RHS is unknown (shouldn't happen for constants) | ||
if (KnownRHS.isUnknown()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can KnownBits::makeConstant ever create an unknown? At the very best this should just be an assert but really I don't think it needs to be here at all. |
||
return SDValue(); | ||
|
||
std::optional<bool> KnownVal; | ||
|
||
// Handle special cases first (like GlobalISel does) | ||
if (KnownRHS.isZero()) { | ||
// x >=u 0 -> always true | ||
// x <u 0 -> always false | ||
if (Cond == ISD::SETUGE) | ||
KnownVal = true; | ||
else if (Cond == ISD::SETULT) | ||
KnownVal = false; | ||
} | ||
|
||
if (!KnownVal) { | ||
bool SupportedPredicate = true; | ||
KnownBits KnownLHS = DAG.computeKnownBits(N0); | ||
|
||
// Convert ISD::CondCode to CmpInst::Predicate | ||
CmpInst::Predicate Pred; | ||
switch (Cond) { | ||
case ISD::SETEQ: | ||
Pred = CmpInst::ICMP_EQ; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have an existing ISD::CondCode -> CmpInst::Predicate helper we can use? Or we just call the KnownBits compares directly in the switch |
||
break; | ||
case ISD::SETNE: | ||
Pred = CmpInst::ICMP_NE; | ||
break; | ||
case ISD::SETULT: | ||
Pred = CmpInst::ICMP_ULT; | ||
break; | ||
case ISD::SETULE: | ||
Pred = CmpInst::ICMP_ULE; | ||
break; | ||
case ISD::SETUGT: | ||
Pred = CmpInst::ICMP_UGT; | ||
break; | ||
case ISD::SETUGE: | ||
Pred = CmpInst::ICMP_UGE; | ||
break; | ||
case ISD::SETLT: | ||
Pred = CmpInst::ICMP_SLT; | ||
break; | ||
case ISD::SETLE: | ||
AZero13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Pred = CmpInst::ICMP_SLE; | ||
break; | ||
case ISD::SETGT: | ||
Pred = CmpInst::ICMP_SGT; | ||
break; | ||
case ISD::SETGE: | ||
Pred = CmpInst::ICMP_SGE; | ||
break; | ||
default: | ||
SupportedPredicate = false; | ||
break; | ||
} | ||
|
||
if (SupportedPredicate) | ||
KnownVal = ICmpInst::compare(KnownLHS, KnownRHS, Pred); | ||
} | ||
|
||
// If the comparison result is known, replace with constant | ||
if (KnownVal) | ||
return DAG.getBoolConstant(*KnownVal, DL, VT, N1.getValueType()); | ||
} | ||
AZero13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (SDValue Combined = SimplifySetCC(VT, N0, N1, Cond, DL, !PreferSetCC)) { | ||
// If we prefer to have a setcc, and we don't, we'll try our best to | ||
AZero13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// recreate one using rebuildSetCC. | ||
|
Uh oh!
There was an error while loading. Please reload this page.