[llvm] InstCombine: Infer nnan and ninf on fpext (PR #177768)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 24 06:24:11 PST 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/177768

Teach SimplifyDemandedFPClass to do this, although this is
not yet applied directly to the cast.

>From d7763477a79f03a80fe1b698daa6a1f8e1098a4e Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sat, 24 Jan 2026 13:49:31 +0100
Subject: [PATCH] InstCombine: Infer nnan and ninf on fpext

Teach SimplifyDemandedFPClass to do this, although this is
not yet applied directly to the cast.
---
 .../InstCombineSimplifyDemanded.cpp           |  7 +++---
 .../simplify-demanded-fpclass-fpext.ll        | 24 +++++++++----------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 6846f13dced66..de05e3b73f519 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -2096,7 +2096,8 @@ static FPClassTest adjustDemandedMaskFromFlags(FPClassTest DemandedMask,
 
 /// Apply epilog fixups to a floating-point intrinsic. See if the result can
 /// fold to a constant, or apply fast math flags.
-static Value *simplifyDemandedFPClassResult(CallInst *FPOp, FastMathFlags FMF,
+static Value *simplifyDemandedFPClassResult(Instruction *FPOp,
+                                            FastMathFlags FMF,
                                             FPClassTest DemandedMask,
                                             KnownFPClass &Known,
                                             ArrayRef<KnownFPClass> KnownSrcs) {
@@ -2660,8 +2661,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Instruction *I,
     Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
     Known.knownNot(~DemandedMask);
 
-    return getFPClassConstant(VTy, Known.KnownFPClasses,
-                              /*IsCanonicalizing=*/true);
+    return simplifyDemandedFPClassResult(I, FMF, DemandedMask, Known,
+                                         {KnownSrc});
   }
   case Instruction::Call: {
     CallInst *CI = cast<CallInst>(I);
diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll
index 7334277750d7e..d138885efd0e6 100644
--- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll
+++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll
@@ -26,7 +26,7 @@ declare nofpclass(inf norm sub) half @returns_zero_or_nan_f16()
 define nofpclass(inf norm sub zero qnan) float @ret_only_snan__fpext(half %x) {
 ; CHECK-LABEL: define nofpclass(qnan inf zero sub norm) float @ret_only_snan__fpext(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[X]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext ninf half [[X]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %result = fpext half %x to float
@@ -54,7 +54,7 @@ define nofpclass(inf norm sub zero) float @ret_only_nan__fpext(half %x) {
 define nofpclass(nan norm sub zero) float @ret_only_inf__fpext(half %x) {
 ; CHECK-LABEL: define nofpclass(nan zero sub norm) float @ret_only_inf__fpext(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[X]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan half [[X]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %result = fpext half %x to float
@@ -82,7 +82,7 @@ define nofpclass(nan ninf norm sub zero) float @ret_only_pinf__fpext(half %x) {
 define nofpclass(inf nan norm sub) float @ret_only_zero__fpext(half %x) {
 ; CHECK-LABEL: define nofpclass(nan inf sub norm) float @ret_only_zero__fpext(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[X]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan ninf half [[X]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %result = fpext half %x to float
@@ -112,7 +112,7 @@ define nofpclass(nan) float @ret_no_nan__fpext__select_nan_or_unknown(i1 %cond,
 ; CHECK-LABEL: define nofpclass(nan) float @ret_no_nan__fpext__select_nan_or_unknown(
 ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) {
 ; CHECK-NEXT:    [[NAN:%.*]] = call half @returns_nan_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[UNKNOWN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan half [[UNKNOWN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %nan = call half @returns_nan_f16()
@@ -154,7 +154,7 @@ define nofpclass(inf) float @ret_no_inf__fpext__select_inf_or_unknown(i1 %cond,
 ; CHECK-LABEL: define nofpclass(inf) float @ret_no_inf__fpext__select_inf_or_unknown(
 ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) {
 ; CHECK-NEXT:    [[INF:%.*]] = call half @returns_inf_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[UNKNOWN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext ninf half [[UNKNOWN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %inf = call half @returns_inf_f16()
@@ -168,7 +168,7 @@ define nofpclass(nan inf) float @ret_no_inf_no_nan__fpext__select_inf_or_nan_or_
 ; CHECK-LABEL: define nofpclass(nan inf) float @ret_no_inf_no_nan__fpext__select_inf_or_nan_or_unknown(
 ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) {
 ; CHECK-NEXT:    [[INF_OR_NAN:%.*]] = call half @returns_inf_or_nan_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[UNKNOWN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan ninf half [[UNKNOWN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %inf.or.nan = call half @returns_inf_or_nan_f16()
@@ -259,7 +259,7 @@ define nofpclass(nan pinf pnorm psub pzero) float @ret_no_positive_no_nan__fpext
 ; CHECK-LABEL: define nofpclass(nan pinf pzero psub pnorm) float @ret_no_positive_no_nan__fpext__select_positive_nan_or_unknown(
 ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) {
 ; CHECK-NEXT:    [[POSITIVE_OR_NAN:%.*]] = call half @returns_positive_or_nan_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[UNKNOWN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan half [[UNKNOWN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %positive.or.nan = call half @returns_positive_or_nan_f16()
@@ -302,7 +302,7 @@ define nofpclass(nan ninf nnorm nsub nzero) float @ret_no_negative_no_nan__fpext
 ; CHECK-LABEL: define nofpclass(nan ninf nzero nsub nnorm) float @ret_no_negative_no_nan__fpext__select_negative_nan_or_unknown(
 ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) {
 ; CHECK-NEXT:    [[NEGATIVE_OR_NAN:%.*]] = call half @returns_negative_or_nan_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[UNKNOWN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan half [[UNKNOWN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %negative.or.nan = call half @returns_negative_or_nan_f16()
@@ -314,7 +314,7 @@ define nofpclass(nan ninf nnorm nsub nzero) float @ret_no_negative_no_nan__fpext
 define nofpclass(snan) float @ret_no_snan__fpext__always_zero() {
 ; CHECK-LABEL: define nofpclass(snan) float @ret_no_snan__fpext__always_zero() {
 ; CHECK-NEXT:    [[ZERO:%.*]] = call half @returns_zero_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[ZERO]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan ninf half [[ZERO]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %zero = call half @returns_zero_f16()
@@ -325,7 +325,7 @@ define nofpclass(snan) float @ret_no_snan__fpext__always_zero() {
 define nofpclass(snan) float @ret_no_snan__fpext__always_zero_or_nan() {
 ; CHECK-LABEL: define nofpclass(snan) float @ret_no_snan__fpext__always_zero_or_nan() {
 ; CHECK-NEXT:    [[ZERO_OR_NAN:%.*]] = call half @returns_zero_or_nan_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[ZERO_OR_NAN]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext ninf half [[ZERO_OR_NAN]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %zero.or.nan = call half @returns_zero_or_nan_f16()
@@ -336,7 +336,7 @@ define nofpclass(snan) float @ret_no_snan__fpext__always_zero_or_nan() {
 define nofpclass(snan) float @ret_no_snan__fpext__always_inf() {
 ; CHECK-LABEL: define nofpclass(snan) float @ret_no_snan__fpext__always_inf() {
 ; CHECK-NEXT:    [[INF:%.*]] = call half @returns_inf_f16()
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[INF]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan half [[INF]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %inf = call half @returns_inf_f16()
@@ -385,7 +385,7 @@ define nofpclass(inf nan norm zero psub) float @ret_only_nsub__fpext(half %x) {
 define nofpclass(inf nan norm) float @ret_only_sub_zero__fpext(half %x) {
 ; CHECK-LABEL: define nofpclass(nan inf norm) float @ret_only_sub_zero__fpext(
 ; CHECK-SAME: half [[X:%.*]]) {
-; CHECK-NEXT:    [[RESULT:%.*]] = fpext half [[X]] to float
+; CHECK-NEXT:    [[RESULT:%.*]] = fpext nnan ninf half [[X]] to float
 ; CHECK-NEXT:    ret float [[RESULT]]
 ;
   %result = fpext half %x to float



More information about the llvm-commits mailing list