[KnownBits] Tighten division bounds if the division is exact
https://github.com/llvm/llvm-project/blob/8267555e8a7cbe000f1e25c484635162c1420239/llvm/lib/Support/KnownBits.cpp#L1301-L1307
If the division is exact, then we can tighten the bounds on minimum result (or equivalently the minimum numerator).
The theory is if the minimum numerator is not a multiple of the denominator, then we should should round up the minimum numerator to next multiple of the denominator.
I think the best way to implement this would be to increase the minimum result by one if the minimum numerator is not a multiple of the denominator.
We do not need to adjust the maximum numerator since x / y and (x - x % y) / y return the same result.
In other words:
- Current implementation:
udiv_floor(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator) - Optimized:
udiv_ceil(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator)
When the denominator is not a constant and the numerator is known non-zero, then we know that the numerator must be at least as large as the denominator.
Considerations:
- Increasing the minimum result might make it greater than the maximum result in poison cases.
- Similarly: Rounding up the minimum numerator could make it overflow, or otherwise become larger than maximum numerator in poison cases.
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)
If the division is exact, then we can tighten the bounds on minimum result (or equivalently the minimum numerator).
The theory if the minimum numerator is not a multiple of the denominator, then we should should round up the minimum numerator to next multiple of the denominator.
I think the best way to implement this would be to increase the minimum result by one if the minimum numerator is not a multiple of the denominator.
We do not need to adjust the maximum numerator since x / y and (x - x % y) / y return the same result.
In other words:
- Current implementation:
udiv_floor(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator) - Optimized:
udiv_ceil(min-numerator, max-denominator) <= udiv_exact(x, y) <= udiv_floor(max-numerator, min-denominator)
When the denominator is not a constant and the numerator is known non-zero, then we know that the numerator must be at least as large as the denominator.
Considerations:
- Increasing the minimum result might make it greater than the maximum result in poison cases.
- Similarly: Rounding up the minimum numerator could make it overflow, or otherwise become larger than maximum numerator in poison cases.
Hi, I would like to be assigned to this issue
i would like to work on this
@Vara715 I have assigned this issue to @Talb057 since they were the first to reply. You can ask them in a week if they are still working on this issue.
Hi is anyone working on this issue? If not, i would like to take it up. Thanks!
@Talb057 Are you still working on this issue? Feel free to ask if you have any questions.
Hi, yes I am still actively working on it