[KnownBits] Improve minimum denominator bounds for `sdiv(negative, positive-or-zero)`
For the division functions, we can assume that the denominator is non-zero even if we are unable to rule out a zero denominator (i.e. 0b000?, 0b0???, 0b?000, etc). This occurs when the denominator has no known set bits.
udiv chooses the minimum non-zero denominator by setting the lowest unknown bit.
https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1295-L1307
However, sdiv does not perform this trick, and will always choose 1 as the minimum non-zero denominator.
https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1253-L1260
We can probably copy this trick over to sdiv.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Eric Ross (ZERICO2005)
udiv chooses the minimum non-zero denominator by setting the lowest unknown bit.
https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1295-L1307
However, sdiv does not perform this trick, and will always choose 1 as the minimum non-zero denominator.
https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1253-L1260
We can probably copy this trick over to sdiv.
Can i try this
is this open, can i be assigned?
@Vara715 I have assigned this issue to @anandkvinu since they were the first to reply. You can ask them in a week if they are still working on this issue.
@Vara715 hi im working on this and already added the fix , currently running unit tests.
@ZERICO2005 Hi, good day. I have tested the 0b000? 0b0??0, 0b0???, using GoogleTest, but the variant 0b?000 is handled by another condition where both Num and Denom are negative. Should I verify that as well? thanks.
0b?000 would either be 0 or INT_MIN, so the only valid denominator would be negative in that case. x / INT_MIN is more of an edge case since the result can only be 0 or 1 (if x == INT_MIN).
You can test that case if there is not any coverage for it currently.
@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks
@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks
x / INT_MIN probably folds to result = (x == INT_MIN) ? 1 : 0, so it would not go through division in that case.
It is probably okay if we skip testing the unknown signbit case.
@ZERICO2005 I have looked for the logic in the sdiv part where it handles the unknown sign-bit denominator (0b?000), and I couldn't find one. I have tested and used dbgs at different places in sdiv for 0b? 000; none of them trigger. Am I missing something? thanks
x / INT_MINprobably folds toresult = (x == INT_MIN) ? 1 : 0, so it would not go through division in that case.It is probably okay if we skip testing the unknown signbit case.
okey thank you.
@ZERICO2005 hi, I have opened a PR. Kindly review. Thanks