This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] add nsw on InstCombineAdd ~X + C --> (C-1) - X
ClosedPublic

Authored by khei4 on Jun 3 2023, 8:59 PM.

Details

Summary

This is the final step for https://reviews.llvm.org/D146903 .

On transform ~X + C --> (C-1) - X, we can safely attach NSW to (C-1) - X if the 1,2 are satisfied

  1. ~X + C has NSW
  2. C-1 won't signed overflow

proof: https://alive2.llvm.org/ce/z/ZWmZLv
precommit tests: https://reviews.llvm.org/D152089

This will resolve https://reviews.llvm.org/D146903 's InstCombine part.
Motivational Transformation: https://alive2.llvm.org/ce/z/-_FdJU

Diff Detail

Event Timeline

khei4 created this revision.Jun 3 2023, 8:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 3 2023, 8:59 PM
khei4 requested review of this revision.Jun 3 2023, 8:59 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 3 2023, 8:59 PM
nikic added a comment.Jun 4 2023, 12:25 AM

proof: https://alive2.llvm.org/ce/z/DXXaoC

This looks more complicated than necessary, just this should be enough: https://alive2.llvm.org/ce/z/ZWmZLv

llvm/test/Transforms/InstCombine/add.ll
391

I think we could have a few additional tests here:

  • Negative test with nuw instead of nsw.
  • Non-splat vector tests with constants -127, -126 (ok) and -128, -127 (not ok)? This fold handles non-splat vectors, so we should check the overflow behavior there as well.
khei4 updated this revision to Diff 528272.Jun 4 2023, 8:58 PM
khei4 edited the summary of this revision. (Show Details)

rebase for tests update

nikic added inline comments.Jun 4 2023, 11:52 PM
llvm/test/Transforms/InstCombine/add.ll
421

You've adjusted the constants here so it now folds, which doesn't match the TODO comment. Not sure what the intention for this test was.

khei4 added a comment.Jun 5 2023, 3:53 AM

This looks more complicated than necessary, just this should be enough: https://alive2.llvm.org/ce/z/ZWmZLv

Thanks! Seems cool! I'll put it on description :)

llvm/test/Transforms/InstCombine/add.ll
421

Sorry, I tweaked it not to attach nsw, I'll rebase.

khei4 edited the summary of this revision. (Show Details)Jun 5 2023, 3:53 AM
nikic accepted this revision.Jun 5 2023, 5:42 AM

LGTM

This revision is now accepted and ready to land.Jun 5 2023, 5:42 AM
This revision was landed with ongoing or failed builds.Jun 5 2023, 8:43 PM
This revision was automatically updated to reflect the committed changes.
khei4 edited the summary of this revision. (Show Details)Sep 19 2023, 5:40 AM