Skip to content
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

[InstSimplify] Generalize simplification of icmps with monotonic operands #69471

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Oct 18, 2023

InstSimplify currently folds patterns like (x | y) uge x and (x & y) ule x to true. However, it cannot handle combinations of such situations, such as (x | y) uge (x & z) etc.

To support this, recursively collect operands of monotonic instructions (that preserve either a greater-or-equal or less-or-equal relationship) and then check whether any of them match.

@@ -3103,6 +3103,54 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
return nullptr;
}

/// Get values V_i such that V uge V_i (Greater) or V ule V_i (!Greater).
static void getUnsignedMonotonicValues(SmallPtrSetImpl<Value *> &Res, Value *V,
bool Greater, unsigned Depth = 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would replace greater with an enum that also support GreaterOrEq and LessOrEq. Also think a bool for Signed makes sense.

Value *X, *Y;
if (Greater) {
if (match(V, m_Or(m_Value(X), m_Value(Y))) ||
match(V, m_Intrinsic<Intrinsic::uadd_sat>(m_Value(X), m_Value(Y)))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can imagine a lot more cases for this. Imo should just be a switch from the start.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these matches aren't Greater, they are GreaterOrEq.

match(V, m_UDiv(m_Value(X), m_Value())) ||
match(V, m_LShr(m_Value(X), m_Value())) ||
match(V, m_Intrinsic<Intrinsic::usub_sat>(m_Value(X)))) {
getUnsignedMonotonicValues(Res, X, Greater, Depth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need complete coverage here, but can you add TODO for some additional freebies (min/max/add+nuw/sub+nuw/mul+nuw/shl+nuw/ashr+signbit/etc...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants