[llvm-branch-commits] [llvm] InstCombine: Fixup known fp class for select and compare (PR #174958)
Matt Arsenault via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jan 8 04:05:15 PST 2026
https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/174958
Improve reported known fp-class when simplifying select.
Previously we just reported the conservative result that
the result class could be either of the inputs. Use the new
utility to apply the compare+select logic the default case of
computeKnownFPClass would catch.
>From 5b1e74b87646b4fe8b7e70359215cddb53fb8d22 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 8 Jan 2026 11:37:43 +0100
Subject: [PATCH] InstCombine: Fixup known fp class for select and compare
Improve reported known fp-class when simplifying select.
Previously we just reported the conservative result that
the result class could be either of the inputs. Use the new
utility to apply the compare+select logic the default case of
computeKnownFPClass would catch.
---
.../InstCombineSimplifyDemanded.cpp | 8 +++--
.../InstCombine/simplify-demanded-fpclass.ll | 30 +++++++++++++++++--
2 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index e816e943380f5..f900fd74a9dbb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -2589,8 +2589,12 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Value *V,
if (KnownRHS.isKnownNever(DemandedMask))
return I->getOperand(1);
- // TODO: Recognize clamping patterns
- Known = KnownLHS | KnownRHS;
+ const SimplifyQuery &SQ = getSimplifyQuery();
+ adjustKnownFPClassForSelectArm(KnownLHS, I->getOperand(0), I->getOperand(1),
+ /*Invert=*/false, SQ, Depth);
+ adjustKnownFPClassForSelectArm(KnownRHS, I->getOperand(0), I->getOperand(2),
+ /*Invert=*/true, SQ, Depth);
+ Known = KnownLHS.intersectWith(KnownRHS);
break;
}
case Instruction::ExtractElement: {
diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
index 844769e960b98..656a3f55b5faf 100644
--- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
+++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll
@@ -879,9 +879,7 @@ define nofpclass(ninf nnorm nsub nzero) float @ret_nofpclass_negatives__select_c
define nofpclass(nan ninf nnorm nsub nzero) float @ret_nofpclass_nan_negatives__select_clamp_pos_to_zero(float %x) {
; CHECK-LABEL: define nofpclass(nan ninf nzero nsub nnorm) float @ret_nofpclass_nan_negatives__select_clamp_pos_to_zero
; CHECK-SAME: (float [[X:%.*]]) {
-; CHECK-NEXT: [[IS_GT_ZERO:%.*]] = fcmp ogt float [[X]], 0.000000e+00
-; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[IS_GT_ZERO]], float 0.000000e+00, float [[X]]
-; CHECK-NEXT: ret float [[SELECT]]
+; CHECK-NEXT: ret float 0.000000e+00
;
%is.gt.zero = fcmp ogt float %x, 0.0
%select = select i1 %is.gt.zero, float 0.0, float %x
@@ -1385,3 +1383,29 @@ define nofpclass(zero) <3 x float> @partially_defined_0_splat_to_poison() {
;
ret <3 x float> <float 0.0, float 0.0, float poison>
}
+
+; The select must be 0, so the exp should fold to 1. This requires
+; analysis of the select condition.
+define nofpclass(nan) float @exp_select_must_be_0(float %arg, i1 %cond, float nofpclass(inf sub norm) %zero.or.nan) {
+; CHECK-LABEL: define nofpclass(nan) float @exp_select_must_be_0
+; CHECK-SAME: (float [[ARG:%.*]], i1 [[COND:%.*]], float nofpclass(inf sub norm) [[ZERO_OR_NAN:%.*]]) {
+; CHECK-NEXT: ret float 1.000000e+00
+;
+ %not.zero = fcmp one float %arg, 0.0
+ %select = select i1 %not.zero, float %zero.or.nan, float %arg
+ %exp = call float @llvm.exp.f32(float %select)
+ ret float %exp
+}
+
+; The select must be 0, so the exp should fold to 1. This requires
+; analysis of the select condition.
+define nofpclass(nan) float @exp_select_must_be_0_commute(float %arg, i1 %cond, float nofpclass(inf sub norm) %zero.or.nan) {
+; CHECK-LABEL: define nofpclass(nan) float @exp_select_must_be_0_commute
+; CHECK-SAME: (float [[ARG:%.*]], i1 [[COND:%.*]], float nofpclass(inf sub norm) [[ZERO_OR_NAN:%.*]]) {
+; CHECK-NEXT: ret float 1.000000e+00
+;
+ %is.zero = fcmp oeq float %arg, 0.0
+ %select = select i1 %is.zero, float %arg, float %zero.or.nan
+ %exp = call float @llvm.exp.f32(float %select)
+ ret float %exp
+}
More information about the llvm-branch-commits
mailing list