-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Add missing fold for fsqrt(select(b, c1, c2)) => select(b, fsqrt(c1), fsqrt(c2))
#113084
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
Conversation
|
@llvm/pr-subscribers-llvm-transforms Author: Rajveer Singh Bharadwaj (Rajveer100) ChangesResolves #110591 Full diff: https://github.com/llvm/llvm-project/pull/113084.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c8b9f166b16020..911e5baba8a4de 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1699,6 +1699,14 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
if (SI->getType()->isIntOrIntVectorTy(1))
return nullptr;
+ if (auto *II = dyn_cast<IntrinsicInst>(&Op)) {
+ if (II->getIntrinsicID() == Intrinsic::sqrt) {
+ Value *NewTV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, TV, nullptr, "sqrt_fold");
+ Value *NewFV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, FV, nullptr, "sqrt_fold");
+ return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
+ }
+ }
+
// Test if a FCmpInst instruction is used exclusively by a select as
// part of a minimum or maximum operation. If so, refrain from doing
// any other folding. This helps out other analyses which understand
|
|
@RKSimon |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
…t(b, fsqrt(c1), fsqrt(c2))` Resolves llvm#110591
711b46b to
8c0278c
Compare
|
Missing tests? |
| return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, | ||
| SI); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this currently exists, it will also fold if one of the arms is not constant.
arsenm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs tests
| Value *NewTV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, TV, nullptr, | ||
| "sqrt_fold"); | ||
| Value *NewFV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, FV, nullptr, | ||
| "sqrt_fold"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't bother naming these values.
You should also be able to preserve at least some of the flags
|
The fold should only occur if at least one of the new sqrt calls will constant fold away. |
|
Quick question: https://alive2.llvm.org/ce/z/YJkX7m Isn't this fold already applied in the existing source code? |
Yes, that looks like this is already happening |
|
In that case the original issue and this PR can be closed? Edit: Issue was closed, hence closing this. |
Resolves #110591