[PATCH] D104634: [SCEV] Generalize MatchBinaryAddToConst to support non-add expressions.

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 21 09:15:30 PDT 2021


reames added a comment.

For an optional follow up change, I believe we can avoid checking the wrap flags for one operand, if C1 and C2 share the same sign.

Consider:
// (X + C1) s<= (X + C2)<nsw> if C1 s<= C2 and both are positive
// (X + C1)<nsw> s<= (X + C2) if C1 s<= C2 and both are negative

If X + C2 does not signed wrap, and C1 s<= C2, then X+C1 also can not wrap if both are positive.  As a result, we don't need to check for the wrap flag on LHS as we've proven it.  The negative case is the inverse.  The mixed sign case does require both checks as the two could overflow off different ends of the range.  (Actually, can they?  Is there any single value of X which you can add a value to and both wrap above and wrap below?  Might be worth further thought.)

It's also worth noting that we can *disprove* cases here as well.

Consider:
// (X + C1)<nsw> s<= (X + C2)<nsw> == false if C1 s> C2.

This would require a change to the API (probably returning an optional bool), but might be interesting to short circuit later checks.  Again, follow up change (if interested).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104634/new/

https://reviews.llvm.org/D104634



More information about the llvm-commits mailing list