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

[InstCombine] reduce test-for-overflow of shifted value #57338

Closed
rotateright opened this issue Aug 24, 2022 · 8 comments
Closed

[InstCombine] reduce test-for-overflow of shifted value #57338

rotateright opened this issue Aug 24, 2022 · 8 comments
Labels
good first issue https://github.com/llvm/llvm-project/contribute llvm:instcombine

Comments

@rotateright
Copy link
Contributor

Forking this off from #57330:

define i1 @src(i8 %x) {
  %add = shl i8 %x, 1
  %cmp = icmp ult i8 %add, %x
  ret i1 %cmp
}

define i1 @tgt(i8 %x) {
  %r = icmp slt i8 %x, 0
  ret i1 %r
}

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

@rotateright rotateright added the good first issue https://github.com/llvm/llvm-project/contribute label Aug 24, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Aug 24, 2022

@llvm/issue-subscribers-good-first-issue

@tianz
Copy link
Contributor

tianz commented Aug 24, 2022

I would like to take a look at this as my first issue!

@rotateright
Copy link
Contributor Author

Sounds good. Your solution should be flexible enough to handle patterns where the icmp operands are swapped and also when the icmp predicate is inverted.

If you have questions, post here or post a patch to Phabricator and ask there. I am "spatel" on Phabricator if you want to add me as a reviewer.

@RKSimon
Copy link
Collaborator

RKSimon commented Aug 25, 2022

See also #51889

@tianz
Copy link
Contributor

tianz commented Aug 25, 2022

I spent some time getting a better understanding of this issue. Basically, we want to reduce
ult (x << 1), x into slt x, 0, because unsigned x << 1 only overflows when signed x < 0

Similarly,
ugt x, (x << 1) is equivalent to slt x, 0
ugt (x << 1), x is equivalent to sgt x, 0
ult x, (x << 1) is equivalent to sgt x, 0

Are there any other patterns I'm missing?

My next step is to find the best place in InstCombineCompares.cpp to implement this change.

Any feedbacks, comments and suggestions are much appreciated!

@rotateright
Copy link
Contributor Author

Test all of the unsigned predicates and see if there is a pattern.

For example uge (x << 1), x is equivalent to sgt x, -1:
https://alive2.llvm.org/ce/z/MB7wv3

We can also reduce equality predicates:
https://alive2.llvm.org/ce/z/7mRDRz

@rotateright
Copy link
Contributor Author

Also, you can look at the related bug (#51889) and the patch that was proposed to solve it to get ideas about where in InstCombineCompares to add code.

@tianz
Copy link
Contributor

tianz commented Aug 29, 2022

Proposed fix: https://reviews.llvm.org/D132888
@rotateright @RKSimon I've added both of you as the potential reviewers. Thank you both for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue https://github.com/llvm/llvm-project/contribute llvm:instcombine
Projects
None yet
Development

No branches or pull requests

5 participants