This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Retain AddExpr flags when subtracting a foldable constant.
ClosedPublic

Authored by fhahn on Jun 15 2021, 1:28 PM.

Details

Summary

Currently we drop wrapping flags for expressions like (A + C1)<flags> - C2.
If C1 >= 0, C2 >= 0 and C1 >= C2, we should be able to fold (C1 - C2),
which will be >= 0 && <= C1, so we should be able to retain the flags of
the original expression. The key here is that we fold (C1 - C2) to a
non-negative constant.

I would appreciate a careful look in case I am missing a case. The logic
at the moment is quite specialized, do you think this could/should be
generalized?

This can improve results after using SimplifyICmpOperands, which may
subtract one in order to use stricter predicates, as is the case for
isKnownPredicate.

Simple example with Alive2. Not how to best construct an example without
concrete constants.

https://alive2.llvm.org/ce/z/QtNrFd

Diff Detail

Event Timeline

fhahn created this revision.Jun 15 2021, 1:28 PM
fhahn requested review of this revision.Jun 15 2021, 1:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2021, 1:28 PM

For nuw, if adding a constant doesn't overflow, adding a smaller one doesn't overflow. For nsw, similarly, if adding a constant doesn't overflow, adding a constant with the same sign and smaller magnitude doesn't overflow.

Not sure I like the way the logic is written here; it's not obvious that the above rules have a common subset, so it's sort of hard to follow.

I don't think the AddExpr->getNumOperands() == 2 check is necessary.

I agree w/Eli's comments.

+1 to what Eli said.

fhahn updated this revision to Diff 353322.Jun 21 2021, 3:35 AM

Thanks Eli! I split up the code to handle NSW & NUW separately. Is this along the lines of your suggestion?

This revision is now accepted and ready to land.Jun 21 2021, 10:23 AM