https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/176123
>From 76a5fef806f53b39b752b045c851a3735cc29106 Mon Sep 17 00:00:00 2001 From: Matt Arsenault <[email protected]> Date: Thu, 15 Jan 2026 11:21:53 +0100 Subject: [PATCH 1/3] InstCombine: Fold known-qnan results to a literal nan Previously we only considered fcNan to fold to qnan for canonicalizing results, ignoring the simpler case where we know the nan is already quiet. --- .../InstCombine/InstCombineSimplifyDemanded.cpp | 2 ++ .../InstCombine/simplify-demanded-fpclass-exp.ll | 2 +- .../simplify-demanded-fpclass-extractelement.ll | 3 +-- .../InstCombine/simplify-demanded-fpclass-fadd.ll | 3 +-- .../InstCombine/simplify-demanded-fpclass-fmul.ll | 6 ++---- .../InstCombine/simplify-demanded-fpclass-fpext.ll | 3 +-- .../simplify-demanded-fpclass-fptrunc-round.ll | 3 +-- .../InstCombine/simplify-demanded-fpclass-fptrunc.ll | 3 +-- .../InstCombine/simplify-demanded-fpclass-frexp.ll | 7 +++---- .../simplify-demanded-fpclass-insertelement.ll | 7 +++---- .../InstCombine/simplify-demanded-fpclass-maximum.ll | 3 +-- .../simplify-demanded-fpclass-maximumnum.ll | 3 +-- .../InstCombine/simplify-demanded-fpclass-minimum.ll | 3 +-- .../simplify-demanded-fpclass-minimumnum.ll | 3 +-- .../simplify-demanded-fpclass-rounding-intrinsics.ll | 3 +-- .../simplify-demanded-fpclass-shufflevector.ll | 7 +++---- .../InstCombine/simplify-demanded-fpclass-sqrt.ll | 12 +++--------- .../InstCombine/simplify-demanded-fpclass.ll | 2 +- 18 files changed, 28 insertions(+), 47 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index f19449a003e9f..c2cdca4134b3e 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -2028,6 +2028,8 @@ static Constant *getFPClassConstant(Type *Ty, FPClassTest Mask, return ConstantFP::getInfinity(Ty); case fcNegInf: return ConstantFP::getInfinity(Ty, true); + case fcQNan: + return ConstantFP::getQNaN(Ty); default: return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll index ef68699c73166..2a33ff195911e 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll @@ -490,7 +490,7 @@ define nofpclass(nzero) float @source_is_known_snan(float nofpclass(inf norm sub define nofpclass(nzero) float @source_is_known_qnan(float nofpclass(inf norm sub zero snan) %must.be.qnan) { ; CHECK-LABEL: define nofpclass(nzero) float @source_is_known_qnan( ; CHECK-SAME: float nofpclass(snan inf zero sub norm) [[MUST_BE_QNAN:%.*]]) { -; CHECK-NEXT: ret float [[MUST_BE_QNAN]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %exp = call float @llvm.exp2.f32(float %must.be.qnan) ret float %exp diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-extractelement.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-extractelement.ll index e571774c72f2b..6cabee258d2b6 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-extractelement.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-extractelement.ll @@ -17,8 +17,7 @@ define nofpclass(inf norm sub zero) half @ret_only_nan__extractelement_unknown(< define nofpclass(snan inf norm sub zero) half @ret_only_qnan__extractelement_unknown(<4 x half> %vec, i32 %idx) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) half @ret_only_qnan__extractelement_unknown( ; CHECK-SAME: <4 x half> [[VEC:%.*]], i32 [[IDX:%.*]]) { -; CHECK-NEXT: [[EXT:%.*]] = extractelement <4 x half> [[VEC]], i32 [[IDX]] -; CHECK-NEXT: ret half [[EXT]] +; CHECK-NEXT: ret half 0xH7E00 ; %ext = extractelement <4 x half> %vec, i32 %idx ret half %ext diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fadd.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fadd.ll index 099624e912912..4386d22f82df4 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fadd.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fadd.ll @@ -43,8 +43,7 @@ define nofpclass(inf zero sub norm) half @ret_only_nan(half %x, half %y) { define nofpclass(snan inf zero sub norm) half @ret_only_qnan(half %x, half %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) half @ret_only_qnan( ; CHECK-SAME: half [[X:%.*]], half [[Y:%.*]]) { -; CHECK-NEXT: [[ADD:%.*]] = fadd half [[X]], [[Y]] -; CHECK-NEXT: ret half [[ADD]] +; CHECK-NEXT: ret half 0xH7E00 ; %add = fadd half %x, %y ret half %add diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fmul.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fmul.ll index 4ed2e781a30d5..b5ad0ad4cafe3 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fmul.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fmul.ll @@ -112,8 +112,7 @@ define nofpclass(inf norm sub zero) float @ret_only_nan_results_square(float nou define nofpclass(inf norm sub zero snan) float @ret_only_qnan_results_square(float noundef %x) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan_results_square( ; CHECK-SAME: float noundef [[X:%.*]]) { -; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X]], [[X]] -; CHECK-NEXT: ret float [[MUL]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %mul = fmul float %x, %x ret float %mul @@ -885,8 +884,7 @@ define nofpclass(inf norm sub zero) float @ret_only_nan_results_fmul(float %x, f define nofpclass(inf norm sub zero snan) float @ret_only_qnan_results_fmul(float %x, float %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan_results_fmul( ; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) { -; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X]], [[Y]] -; CHECK-NEXT: ret float [[MUL]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %mul = fmul float %x, %y ret float %mul diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll index c1070954a26ff..7334277750d7e 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fpext.ll @@ -36,8 +36,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan__fpext(half %x) { define nofpclass(inf norm sub zero snan) float @ret_only_qnan__fpext(half %x) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan__fpext( ; CHECK-SAME: half [[X:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = fpext half [[X]] to float -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = fpext half %x to float ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc-round.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc-round.ll index 9ca3956be5358..33fe7977701ab 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc-round.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc-round.ll @@ -40,8 +40,7 @@ define nofpclass(inf norm sub zero qnan) half @ret_only_snan__fptrunc(float %x) define nofpclass(inf norm sub zero snan) half @ret_only_qnan__fptrunc(float %x) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) half @ret_only_qnan__fptrunc( ; CHECK-SAME: float [[X:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call half @llvm.fptrunc.round.f16.f32(float [[X]], metadata !"round.downward") -; CHECK-NEXT: ret half [[RESULT]] +; CHECK-NEXT: ret half 0xH7E00 ; %result = call half @llvm.fptrunc.round.f16.f32(float %x, metadata !"round.downward") ret half %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc.ll index c1d5dacbc26c9..2edb04956d407 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-fptrunc.ll @@ -40,8 +40,7 @@ define nofpclass(inf norm sub zero qnan) half @ret_only_snan__fptrunc(float %x) define nofpclass(inf norm sub zero snan) half @ret_only_qnan__fptrunc(float %x) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) half @ret_only_qnan__fptrunc( ; CHECK-SAME: float [[X:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = fptrunc float [[X]] to half -; CHECK-NEXT: ret half [[RESULT]] +; CHECK-NEXT: ret half 0xH7E00 ; %result = fptrunc float %x to half ret half %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-frexp.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-frexp.ll index c0715b637295a..d4954a22fd186 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-frexp.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-frexp.ll @@ -65,7 +65,7 @@ define nofpclass(nan) half @extractvalue_not_frexp() { define nofpclass(snan inf norm sub zero) half @ret_only_qnan__frexp(half %unknown) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) half @ret_only_qnan__frexp( ; CHECK-SAME: half [[UNKNOWN:%.*]]) { -; CHECK-NEXT: ret half [[UNKNOWN]] +; CHECK-NEXT: ret half 0xH7E00 ; %frexp = call { half, i32 } @llvm.frexp.f16.i32(half %unknown) %frexp.mant = extractvalue { half, i32 } %frexp, 0 @@ -314,8 +314,7 @@ define nofpclass(qnan) half @ret_nofpclass_qnan__frexp_select_unknown_qnan(i1 %c ; CHECK-LABEL: define nofpclass(qnan) half @ret_nofpclass_qnan__frexp_select_unknown_qnan( ; CHECK-SAME: i1 [[COND:%.*]], half [[UNKNOWN:%.*]]) { ; CHECK-NEXT: [[ONLY_QNAN:%.*]] = call half @returns_qnan() -; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], half [[UNKNOWN]], half [[ONLY_QNAN]] -; CHECK-NEXT: [[FREXP:%.*]] = call { half, i32 } @llvm.frexp.f16.i32(half [[SELECT]]) +; CHECK-NEXT: [[FREXP:%.*]] = call { half, i32 } @llvm.frexp.f16.i32(half [[UNKNOWN]]) ; CHECK-NEXT: [[FREXP_MANT:%.*]] = extractvalue { half, i32 } [[FREXP]], 0 ; CHECK-NEXT: ret half [[FREXP_MANT]] ; @@ -531,7 +530,7 @@ define nofpclass(snan) half @src_only_inf__frexp() { define nofpclass(snan) half @src_only_nan__frexp() { ; CHECK-LABEL: define nofpclass(snan) half @src_only_nan__frexp() { ; CHECK-NEXT: [[ONLY_NAN:%.*]] = call half @returns_nan() -; CHECK-NEXT: ret half [[ONLY_NAN]] +; CHECK-NEXT: ret half 0xH7E00 ; %only.nan = call half @returns_nan() %frexp = call { half, i32 } @llvm.frexp.f16.i32(half %only.nan) diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-insertelement.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-insertelement.ll index 36d8766d967aa..8c6679d8be038 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-insertelement.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-insertelement.ll @@ -29,8 +29,7 @@ define nofpclass(qnan inf zero sub norm) <4 x half> @ret_only_snan__insert_unkno define nofpclass(snan inf zero sub norm) <4 x half> @ret_only_qnan__insert_unknown_unknown(<4 x half> %vec, half %elt, i32 %idx) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) <4 x half> @ret_only_qnan__insert_unknown_unknown( ; CHECK-SAME: <4 x half> [[VEC:%.*]], half [[ELT:%.*]], i32 [[IDX:%.*]]) { -; CHECK-NEXT: [[INSERT:%.*]] = insertelement <4 x half> [[VEC]], half [[ELT]], i32 [[IDX]] -; CHECK-NEXT: ret <4 x half> [[INSERT]] +; CHECK-NEXT: ret <4 x half> splat (half 0xH7E00) ; %insert = insertelement <4 x half> %vec, half %elt, i32 %idx ret <4 x half> %insert @@ -160,7 +159,7 @@ define nofpclass(snan) <4 x half> @insert_unknown_taints_known_nan_vector_input( ; CHECK-LABEL: define nofpclass(snan) <4 x half> @insert_unknown_taints_known_nan_vector_input( ; CHECK-SAME: half [[UNKNOWN_ELT:%.*]], i32 [[IDX:%.*]]) { ; CHECK-NEXT: [[NAN_VEC:%.*]] = call <4 x half> @returns_nan() -; CHECK-NEXT: [[INSERT:%.*]] = insertelement <4 x half> [[NAN_VEC]], half [[UNKNOWN_ELT]], i32 [[IDX]] +; CHECK-NEXT: [[INSERT:%.*]] = insertelement <4 x half> splat (half 0xH7E00), half [[UNKNOWN_ELT]], i32 [[IDX]] ; CHECK-NEXT: [[EXP:%.*]] = call <4 x half> @llvm.exp.v4f16(<4 x half> [[INSERT]]) ; CHECK-NEXT: ret <4 x half> [[EXP]] ; @@ -175,7 +174,7 @@ define nofpclass(snan) <4 x half> @insert_unknown_vector_taints_known_nan_vector ; CHECK-LABEL: define nofpclass(snan) <4 x half> @insert_unknown_vector_taints_known_nan_vector_insert( ; CHECK-SAME: <4 x half> [[UNKNOWN_VEC:%.*]], i32 [[IDX:%.*]]) { ; CHECK-NEXT: [[NAN_ELT:%.*]] = call half @returns_nan_f16() -; CHECK-NEXT: [[INSERT:%.*]] = insertelement <4 x half> [[UNKNOWN_VEC]], half [[NAN_ELT]], i32 [[IDX]] +; CHECK-NEXT: [[INSERT:%.*]] = insertelement <4 x half> [[UNKNOWN_VEC]], half 0xH7E00, i32 [[IDX]] ; CHECK-NEXT: [[EXP:%.*]] = call <4 x half> @llvm.exp.v4f16(<4 x half> [[INSERT]]) ; CHECK-NEXT: ret <4 x half> [[EXP]] ; diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximum.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximum.ll index 4c0f2f9d7ae78..b6a6e63af8acb 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximum.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximum.ll @@ -68,8 +68,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan(float %x, float %y define nofpclass(inf norm sub zero snan) float @ret_only_qnan(float %x, float %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan( ; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call nsz float @llvm.maximum.f32(float [[X]], float [[Y]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = call float @llvm.maximum.f32(float %x, float %y) ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximumnum.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximumnum.ll index 4c87ca17a3512..2589d80a256d6 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximumnum.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-maximumnum.ll @@ -67,8 +67,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan(float %x, float %y define nofpclass(inf norm sub zero snan) float @ret_only_qnan(float %x, float %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan( ; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call nsz float @llvm.maximumnum.f32(float [[X]], float [[Y]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = call float @llvm.maximumnum.f32(float %x, float %y) ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimum.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimum.ll index e62ec503c78ab..fba6e9c45da4d 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimum.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimum.ll @@ -67,8 +67,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan(float %x, float %y define nofpclass(inf norm sub zero snan) float @ret_only_qnan(float %x, float %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan( ; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call nsz float @llvm.minimum.f32(float [[X]], float [[Y]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = call float @llvm.minimum.f32(float %x, float %y) ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimumnum.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimumnum.ll index e66e3afb52c77..b67077cb7dbd9 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimumnum.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-minimumnum.ll @@ -67,8 +67,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan(float %x, float %y define nofpclass(inf norm sub zero snan) float @ret_only_qnan(float %x, float %y) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan( ; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call nsz float @llvm.minimumnum.f32(float [[X]], float [[Y]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = call float @llvm.minimumnum.f32(float %x, float %y) ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-rounding-intrinsics.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-rounding-intrinsics.ll index 1d8a08170677f..d1642569c6610 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-rounding-intrinsics.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-rounding-intrinsics.ll @@ -42,8 +42,7 @@ define nofpclass(inf norm sub zero qnan) float @ret_only_snan__floor(float %x) { define nofpclass(inf norm sub zero snan) float @ret_only_qnan__floor(float %x) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan__floor( ; CHECK-SAME: float [[X:%.*]]) { -; CHECK-NEXT: [[RESULT:%.*]] = call float @llvm.floor.f32(float [[X]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %result = call float @llvm.floor.f32(float %x) ret float %result diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-shufflevector.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-shufflevector.ll index ee8be3ca1bf89..565e9b76ddea3 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-shufflevector.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-shufflevector.ll @@ -28,8 +28,7 @@ define nofpclass(qnan inf zero sub norm) <4 x half> @ret_only_snan__shufflevecto define nofpclass(snan inf zero sub norm) <4 x half> @ret_only_qnan__shufflevector_unknown(<4 x half> %vec0, <4 x half> %vec1) { ; CHECK-LABEL: define nofpclass(snan inf zero sub norm) <4 x half> @ret_only_qnan__shufflevector_unknown( ; CHECK-SAME: <4 x half> [[VEC0:%.*]], <4 x half> [[VEC1:%.*]]) { -; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x half> [[VEC0]], <4 x half> [[VEC1]], <4 x i32> <i32 6, i32 2, i32 3, i32 0> -; CHECK-NEXT: ret <4 x half> [[SHUFFLE]] +; CHECK-NEXT: ret <4 x half> splat (half 0xH7E00) ; %shuffle = shufflevector <4 x half> %vec0, <4 x half> %vec1, <4 x i32> <i32 6, i32 2, i32 3, i32 0> ret <4 x half> %shuffle @@ -242,7 +241,7 @@ define nofpclass(snan) <4 x half> @ret_nonnan_rhs_taints_known_nan_result(i1 %co ; CHECK-LABEL: define nofpclass(snan) <4 x half> @ret_nonnan_rhs_taints_known_nan_result( ; CHECK-SAME: i1 [[COND:%.*]], <4 x half> [[UNKNOWN:%.*]]) { ; CHECK-NEXT: [[NAN:%.*]] = call <4 x half> @returns_nan() -; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x half> [[NAN]], <4 x half> [[UNKNOWN]], <4 x i32> <i32 5, i32 0, i32 1, i32 3> +; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x half> <half 0xH7E00, half 0xH7E00, half poison, half 0xH7E00>, <4 x half> [[UNKNOWN]], <4 x i32> <i32 5, i32 0, i32 1, i32 3> ; CHECK-NEXT: [[EXP:%.*]] = call <4 x half> @llvm.exp.v4f16(<4 x half> [[SHUFFLE]]) ; CHECK-NEXT: ret <4 x half> [[EXP]] ; @@ -257,7 +256,7 @@ define nofpclass(snan) <4 x half> @ret_nonnan_lhs_taints_known_nan_result(i1 %co ; CHECK-LABEL: define nofpclass(snan) <4 x half> @ret_nonnan_lhs_taints_known_nan_result( ; CHECK-SAME: i1 [[COND:%.*]], <4 x half> [[UNKNOWN:%.*]]) { ; CHECK-NEXT: [[NAN:%.*]] = call <4 x half> @returns_nan() -; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x half> [[UNKNOWN]], <4 x half> [[NAN]], <4 x i32> <i32 5, i32 0, i32 1, i32 3> +; CHECK-NEXT: [[SHUFFLE:%.*]] = shufflevector <4 x half> [[UNKNOWN]], <4 x half> <half poison, half 0xH7E00, half poison, half poison>, <4 x i32> <i32 5, i32 0, i32 1, i32 3> ; CHECK-NEXT: [[EXP:%.*]] = call <4 x half> @llvm.exp.v4f16(<4 x half> [[SHUFFLE]]) ; CHECK-NEXT: ret <4 x half> [[EXP]] ; diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-sqrt.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-sqrt.ll index b09faf0f4c3af..0520fef0f0e79 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-sqrt.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-sqrt.ll @@ -163,9 +163,7 @@ define nofpclass(inf norm zero) float @ret_only_nan_or_sub__sqrt__select_unknown ; CHECK-LABEL: define nofpclass(inf zero norm) float @ret_only_nan_or_sub__sqrt__select_unknown_or_maybe_ninf( ; CHECK-SAME: i1 [[COND:%.*]], float nofpclass(nan) [[X:%.*]]) { ; CHECK-NEXT: [[MAYBE_NINF:%.*]] = call nofpclass(nan pinf sub norm) float @func() -; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], float [[X]], float 0xFFF0000000000000 -; CHECK-NEXT: [[RESULT:%.*]] = call float @llvm.sqrt.f32(float [[SELECT]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %maybe.ninf = call nofpclass(pinf norm sub nan) float @func() %select = select i1 %cond, float %x, float %maybe.ninf @@ -178,9 +176,7 @@ define nofpclass(inf norm zero) float @ret_only_nan_or_sub__sqrt__select_unknown ; CHECK-LABEL: define nofpclass(inf zero norm) float @ret_only_nan_or_sub__sqrt__select_unknown_or_maybe_nnorm( ; CHECK-SAME: i1 [[COND:%.*]], float nofpclass(nan) [[X:%.*]]) { ; CHECK-NEXT: [[MAYBE_NNORM:%.*]] = call nofpclass(nan inf sub pnorm) float @func() -; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], float [[X]], float [[MAYBE_NNORM]] -; CHECK-NEXT: [[RESULT:%.*]] = call float @llvm.sqrt.f32(float [[SELECT]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %maybe.nnorm = call nofpclass(inf pnorm sub nan) float @func() %select = select i1 %cond, float %x, float %maybe.nnorm @@ -193,9 +189,7 @@ define nofpclass(inf norm zero) float @ret_only_nan_or_sub__sqrt__select_unknown ; CHECK-LABEL: define nofpclass(inf zero norm) float @ret_only_nan_or_sub__sqrt__select_unknown_or_maybe_nsub( ; CHECK-SAME: i1 [[COND:%.*]], float nofpclass(nan) [[X:%.*]]) { ; CHECK-NEXT: [[MAYBE_NSUB:%.*]] = call nofpclass(nan inf psub norm) float @func() -; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[COND]], float [[X]], float [[MAYBE_NSUB]] -; CHECK-NEXT: [[RESULT:%.*]] = call float @llvm.sqrt.f32(float [[SELECT]]) -; CHECK-NEXT: ret float [[RESULT]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %maybe.nsub = call nofpclass(inf norm psub nan) float @func() %select = select i1 %cond, float %x, float %maybe.nsub diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll index 714e4ff4398e2..359fb83c35123 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll @@ -1348,7 +1348,7 @@ define nofpclass(snan) float @ret_nofpclass_snan__nofpclass_call_only_nan(i1 %co ; CHECK-LABEL: define nofpclass(snan) float @ret_nofpclass_snan__nofpclass_call_only_nan ; CHECK-SAME: (i1 [[COND:%.*]], float [[Y:%.*]]) { ; CHECK-NEXT: [[MUST_BE_NAN:%.*]] = call nofpclass(inf zero sub norm) float @extern() -; CHECK-NEXT: ret float [[MUST_BE_NAN]] +; CHECK-NEXT: ret float 0x7FF8000000000000 ; %must.be.nan = call nofpclass(inf norm zero sub) float @extern() ret float %must.be.nan >From 509fe223a61a66e00a858da598d277e7edd011ce Mon Sep 17 00:00:00 2001 From: Matt Arsenault <[email protected]> Date: Thu, 15 Jan 2026 17:26:57 +0100 Subject: [PATCH 2/3] Move isAggregateType, although this can't break for any existing case --- .../InstCombine/InstCombineSimplifyDemanded.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index c2cdca4134b3e..e3e6893df71c4 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -2013,14 +2013,14 @@ static Constant *getFPClassConstant(Type *Ty, FPClassTest Mask, if (Mask == fcPosZero) return Constant::getNullValue(Ty); - // Turn any possible snans into quiet if we can. - if (Mask == fcNan && IsCanonicalizing) - return ConstantFP::getQNaN(Ty); - // TODO: Support aggregate types that are allowed by FPMathOperator. if (Ty->isAggregateType()) return nullptr; + // Turn any possible snans into quiet if we can. + if (Mask == fcNan && IsCanonicalizing) + return ConstantFP::getQNaN(Ty); + switch (Mask) { case fcNegZero: return ConstantFP::getZero(Ty, true); >From b998e580495d09e1cf3d8dc165399c1bef665534 Mon Sep 17 00:00:00 2001 From: Matt Arsenault <[email protected]> Date: Thu, 15 Jan 2026 17:24:36 +0100 Subject: [PATCH 3/3] Check IsCanonicalizing --- .../InstCombineSimplifyDemanded.cpp | 3 +- .../InstCombine/simplify-demanded-fpclass.ll | 86 ++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp index e3e6893df71c4..1bc42c4e55646 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -2029,7 +2029,8 @@ static Constant *getFPClassConstant(Type *Ty, FPClassTest Mask, case fcNegInf: return ConstantFP::getInfinity(Ty, true); case fcQNan: - return ConstantFP::getQNaN(Ty); + // Payload bits cannot be dropped for pure signbit operations. + return IsCanonicalizing ? ConstantFP::getQNaN(Ty) : nullptr; default: return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll index 359fb83c35123..4761569200770 100644 --- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll +++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass.ll @@ -485,6 +485,90 @@ define nofpclass(inf) float @ret_nofpclass_inf__select_chain_inf_nan_1(i1 %cond, ret float %select1 } +define nofpclass(inf norm sub zero) float @ret_only_nan__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(inf zero sub norm) float @ret_only_nan__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[UNKNOWN]]) +; CHECK-NEXT: ret float [[FABS]] +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(snan inf norm sub zero) float @ret_only_qnan__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(snan inf zero sub norm) float @ret_only_qnan__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[UNKNOWN]]) +; CHECK-NEXT: ret float [[FABS]] +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(qnan inf norm sub zero) float @ret_only_snan__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(qnan inf zero sub norm) float @ret_only_snan__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[UNKNOWN]]) +; CHECK-NEXT: ret float [[FABS]] +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan norm sub zero) float @ret_only_inf__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan zero sub norm) float @ret_only_inf__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float 0x7FF0000000000000 +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan ninf norm sub zero) float @ret_only_pinf__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan ninf zero sub norm) float @ret_only_pinf__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float 0x7FF0000000000000 +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan pinf norm sub zero) float @ret_only_ninf__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan pinf zero sub norm) float @ret_only_ninf__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float poison +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan inf norm sub) float @ret_only_zero__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan inf sub norm) float @ret_only_zero__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float 0.000000e+00 +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan inf norm sub pzero) float @ret_only_nzero__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan inf pzero sub norm) float @ret_only_nzero__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float poison +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + +define nofpclass(nan inf norm sub nzero) float @ret_only_pzero__fabs(float %unknown) { +; CHECK-LABEL: define nofpclass(nan inf nzero sub norm) float @ret_only_pzero__fabs +; CHECK-SAME: (float [[UNKNOWN:%.*]]) { +; CHECK-NEXT: ret float 0.000000e+00 +; + %fabs = call float @llvm.fabs.f32(float %unknown) + ret float %fabs +} + ; Simplify to fabs %x define nofpclass(inf) float @ret_nofpclass_inf__fabs_select_ninf_rhs(i1 %cond, float %x) { ; CHECK-LABEL: define nofpclass(inf) float @ret_nofpclass_inf__fabs_select_ninf_rhs @@ -1348,7 +1432,7 @@ define nofpclass(snan) float @ret_nofpclass_snan__nofpclass_call_only_nan(i1 %co ; CHECK-LABEL: define nofpclass(snan) float @ret_nofpclass_snan__nofpclass_call_only_nan ; CHECK-SAME: (i1 [[COND:%.*]], float [[Y:%.*]]) { ; CHECK-NEXT: [[MUST_BE_NAN:%.*]] = call nofpclass(inf zero sub norm) float @extern() -; CHECK-NEXT: ret float 0x7FF8000000000000 +; CHECK-NEXT: ret float [[MUST_BE_NAN]] ; %must.be.nan = call nofpclass(inf norm zero sub) float @extern() ret float %must.be.nan _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
