Skip to content

InstCombine: Positive normal exp results imply possible negative normal inputs#174273

Merged
arsenm merged 1 commit intomainfrom
users/arsenm/instcombine/exp-negnormal-results-imply-posnormal-sources
Jan 3, 2026
Merged

InstCombine: Positive normal exp results imply possible negative normal inputs#174273
arsenm merged 1 commit intomainfrom
users/arsenm/instcombine/exp-negnormal-results-imply-posnormal-sources

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Jan 3, 2026

Fix mishandling exp where the result is known to only be a negative normal.

Copy link
Contributor Author

arsenm commented Jan 3, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@arsenm arsenm added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes floating-point Floating-point math labels Jan 3, 2026 — with Graphite App
@arsenm arsenm requested review from dtcxzyw and jayfoad January 3, 2026 10:50
…al inputs

Fix mishandling exp where the result is known to only be a positive normal,
but the input is only known to be negative.
@arsenm arsenm force-pushed the users/arsenm/instcombine/exp-negnormal-results-imply-posnormal-sources branch from 796296c to 612ca55 Compare January 3, 2026 10:51
@arsenm arsenm changed the title InstCombine: Negative normal exp results imply possible positive normal inputs InstCombine: Positive normal exp results imply possible negative normal inputs Jan 3, 2026
@arsenm arsenm marked this pull request as ready for review January 3, 2026 10:52
@arsenm arsenm requested a review from nikic as a code owner January 3, 2026 10:52
@llvmbot
Copy link
Member

llvmbot commented Jan 3, 2026

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Fix mishandling exp where the result is known to only be a negative normal.


Full diff: https://github.com/llvm/llvm-project/pull/174273.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp (+2-1)
  • (modified) llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll (+13)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 3ad1caa6baa63..7bc1070613acd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -2159,7 +2159,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseFPClass(Value *V,
         // exp(+/- smallest_normal) = 1
         // exp(+/- largest_denormal) = 1
         // exp(+/- smallest_denormal) = 1
-        SrcDemandedMask |= fcPosNormal | fcSubnormal | fcZero;
+        // exp(-1) = pos normal
+        SrcDemandedMask |= fcNormal | fcSubnormal | fcZero;
       }
 
       // exp(inf), exp(largest_normal) = inf
diff --git a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll
index 1f042868ea879..8a2ad8ba31d32 100644
--- a/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll
+++ b/llvm/test/Transforms/InstCombine/simplify-demanded-fpclass-exp.ll
@@ -509,6 +509,19 @@ define nofpclass(pzero nan) float @source_is_known_zero_or_nan__nnan_result(floa
   ret float %exp
 }
 
+; Cannot eliminate the select
+define nofpclass(nan inf nnorm sub zero) float @posnormal_result_demands_negnormal_source(i1 %cond, float nofpclass(inf nan pnorm sub zero) %neg.normal, float %unknown) {
+; CHECK-LABEL: define nofpclass(nan inf zero sub nnorm) float @posnormal_result_demands_negnormal_source(
+; CHECK-SAME: i1 [[COND:%.*]], float nofpclass(nan inf zero sub pnorm) [[NEG_NORMAL:%.*]], float [[UNKNOWN:%.*]]) {
+; CHECK-NEXT:    [[SELECT:%.*]] = select i1 [[COND]], float [[UNKNOWN]], float [[NEG_NORMAL]]
+; CHECK-NEXT:    [[EXP:%.*]] = call float @llvm.exp2.f32(float [[SELECT]])
+; CHECK-NEXT:    ret float [[EXP]]
+;
+  %select = select i1 %cond, float %unknown, float %neg.normal
+  %exp = call float @llvm.exp2.f32(float %select)
+  ret float %exp
+}
+
 !0 = !{!"function_entry_count", i64 1000}
 ;.
 ; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }

@arsenm arsenm enabled auto-merge (squash) January 3, 2026 10:53
@arsenm arsenm merged commit 56905be into main Jan 3, 2026
12 of 13 checks passed
@arsenm arsenm deleted the users/arsenm/instcombine/exp-negnormal-results-imply-posnormal-sources branch January 3, 2026 11:20
@llvm-ci
Copy link

llvm-ci commented Jan 3, 2026

LLVM Buildbot has detected a new failure on builder ppc64le-flang-rhel-clang running on ppc64le-flang-rhel-test while building llvm at step 6 "test-build-unified-tree-check-flang".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/157/builds/43426

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-flang) failure: 1200 seconds without output running [b'ninja', b'check-flang'], attempting to kill
...
PASS: Flang :: Transforms/constant-argument-globalisation.fir (4062 of 4072)
PASS: Flang :: Transforms/debug-dwarf-version.fir (4063 of 4072)
PASS: Flang :: Transforms/debug-line-table.fir (4064 of 4072)
PASS: Flang :: Semantics/numeric_storage_size.f90 (4065 of 4072)
PASS: Flang :: Transforms/OpenACC/acc-implicit-data-fortran.F90 (4066 of 4072)
PASS: Flang :: Transforms/stack-arrays.f90 (4067 of 4072)
PASS: Flang :: Driver/omp-driver-offload.f90 (4068 of 4072)
PASS: Flang :: Transforms/tbaa-local-alloc-threshold.fir (4069 of 4072)
PASS: Flang :: Driver/linker-options.f90 (4070 of 4072)
PASS: Flang :: Intrinsics/math-codegen.fir (4071 of 4072)
command timed out: 1200 seconds without output running [b'ninja', b'check-flang'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2488.916229

@jayfoad
Copy link
Contributor

jayfoad commented Jan 5, 2026

Fix mishandling exp where the result is known to only be a negative normal.

... positive normal?

@arsenm
Copy link
Contributor Author

arsenm commented Jan 5, 2026

I remember fixing the commit message locally, but then the final commit message comes from the GitHub summary which doesn't update from the latest commit

mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jan 6, 2026
…al inputs (llvm#174273)

Fix mishandling exp where the result is known to only be a negative
normal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

floating-point Floating-point math llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants