[llvm] 5379f76 - [InstCombine] try harder to preserve 'nsz' in fneg-of-select transform
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 24 07:44:08 PST 2022
Author: Sanjay Patel
Date: 2022-02-24T10:43:53-05:00
New Revision: 5379f76e6328ecfa2e15b3c7ab32c4a053f4a663
URL: https://github.com/llvm/llvm-project/commit/5379f76e6328ecfa2e15b3c7ab32c4a053f4a663
DIFF: https://github.com/llvm/llvm-project/commit/5379f76e6328ecfa2e15b3c7ab32c4a053f4a663.diff
LOG: [InstCombine] try harder to preserve 'nsz' in fneg-of-select transform
The corner case where 'nsz' needs to be removed is very narrow
as discussed here:
https://reviews.llvm.org/rG3cdd05e519dd
If the select condition is not undef, there's no problem with
propagating 'nsz':
https://alive2.llvm.org/ce/z/4GWJdq
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/fneg.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 8a881924ab09f..37c05529c6501 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2301,7 +2301,8 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
auto propagateSelectFMF = [&](SelectInst *S) {
S->copyFastMathFlags(&I);
if (auto *OldSel = dyn_cast<SelectInst>(Op))
- if (!OldSel->hasNoSignedZeros())
+ if (!OldSel->hasNoSignedZeros() &&
+ !isGuaranteedNotToBeUndefOrPoison(OldSel->getCondition()))
S->setHasNoSignedZeros(false);
};
// -(Cond ? -P : Y) --> Cond ? P : -Y
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index 4c60a20d09ab3..b8df66ee74aa7 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -648,12 +648,12 @@ define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) {
ret float %r
}
-; TODO: The removal of nsz in this pattern is not needed if the select condition can't be poison.
+; The removal of nsz in this pattern is not needed if the select condition can't be poison.
define float @select_fneg_false_nsz_ok(float %x, float %y, i1 noundef %b) {
; CHECK-LABEL: @select_fneg_false_nsz_ok(
; CHECK-NEXT: [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select nnan ninf i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
; CHECK-NEXT: ret float [[R]]
;
%ny = fneg float %y
More information about the llvm-commits
mailing list