-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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) { |
There was a problem hiding this comment.
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)))) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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...)
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.