-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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] Missing fold for (1 + 2 * a * b > 0) ? a : a * b --> a * b < 0 ? a * b : a #61538
Comments
@llvm/issue-subscribers-backend-aarch64 |
It's not applied to the AArch64 target but to other targets as well. I believe it's a missing fold in the middle end. |
Thank you for your comment. I'm sorry but the issue title was misleading. |
Even these:
|
Feel free to correct it! |
We can eliminate the or operation based on the predicate and the relation between OrC and C. sge: X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0 sgt: X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0 sle: X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0 slt: X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0 Alive2 links: sge: https://alive2.llvm.org/ce/z/W-6FHE sgt: https://alive2.llvm.org/ce/z/TKK2yJ sle: https://alive2.llvm.org/ce/z/vURQGM slt: https://alive2.llvm.org/ce/z/JAsVfw Related issue: #61538 Signed-off-by: Jun Zhang <jun@junz.org> Differential Revision: https://reviews.llvm.org/D147597
Can you please confirm whether this report was based on a real-world motivation? I want to understand whether we should accept a non-principled fix for this or not. |
Sorry, I don't understand. What do you mean by "based on a real-world motivation"? I'm not the original bug reporter so I don't know if this is based on some real codebase or just random fuzzing. However, IMHO it's an optimizing compiler's job to optimize the code as long as 1. the transform is beneficial 2. it doesn't cause noticeable compile time regression. I'll be surprised to see clang can't optimize this trivial example: https://godbolt.org/z/bvPhno5xT |
I'm the original reporter. The conditional expression considered in this issue (I mean "1 + 2 * a * b > 0" ) was originally inspired by the following derivative formula. Such calculations are necessary when considering some kind of differential equation. If limited to integers, we can fold as indicated in the title of this issue.
I agree with junaire. I believe that compilers are based on steady improvement. I would like to express my sincere respect for junaire's efforts (creating patches and responding to reviews). |
My question here is whether there is production code that will benefit from this optimization. If yes, I am much more willing to accept a hacky solution to the problem. If no, then I will insist on a principled solution. |
What do you mean by " hacky solution to the problem"? I guess there may be some misunderstanding here.
That's too arbitrary and I want some principled solutions as well. For example, I think we at least can improve the following cases:
After all of these low-hanging fruits, I can still see some unnecessary computation but that's another story. |
The existing logic in `foldICmpMulConstant` can only optimize the constant if we have (X * Y) * C. However, reassociate pass currently produces this form with the constant on the inner multiply, so let's reassociate it in foldICmpMulConstant to enable further optimizations. Related issue: llvm#61538 Signed-off-by: Jun Zhang <jun@junz.org> Differential Revision: https://reviews.llvm.org/D148210
We can eliminate the or operation based on the predicate and the relation between OrC and C. sge: X | OrC s>= C --> X s>= 0 iff OrC s>= C s>= 0 sgt: X | OrC s> C --> X s>= 0 iff OrC s> C s>= 0 sle: X | OrC s<= C --> X s< 0 iff OrC s> C s>= 0 slt: X | OrC s< C --> X s< 0 iff OrC s>= C s>= 0 Alive2 links: sge: https://alive2.llvm.org/ce/z/W-6FHE sgt: https://alive2.llvm.org/ce/z/TKK2yJ sle: https://alive2.llvm.org/ce/z/vURQGM slt: https://alive2.llvm.org/ce/z/JAsVfw Related issue: llvm#61538 Signed-off-by: Jun Zhang <jun@junz.org> Differential Revision: https://reviews.llvm.org/D147597
Hi, |
Note the missed optimization in this case is something more like:
Looks like LLVM is converting it to:
Note GCC does not use csel for the ?: until late(r) in the pipeline and expands it into if/then/else after the front-end. Also |
Test code:
clang
gcc
https://godbolt.org/z/b79ehqG77
The text was updated successfully, but these errors were encountered: