[PATCH] D123396: [FPEnv][InstSimplify] Fold fsub -0.0, -X ==> X

Kevin P. Neal via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 8 08:52:53 PDT 2022


kpn created this revision.
kpn added reviewers: spatel, nlopes.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kpn requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently the fsub optimizations in InstSimplify don't know how to fold -0.0 - (-X) to X when the constrained intrinsics are used. This adds partial support. The rest of the support will come later with work on the IR matchers.

This review is split out from D107285 <https://reviews.llvm.org/D107285>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123396

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/strictfp-fsub.ll


Index: llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
+++ llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
@@ -251,12 +251,9 @@
   ret float %ret
 }
 
-; TODO: This will fold if we allow non-default floating point environments.
 define float @fsub_fneg_nnan_n0_fnX_ebmaytrap(float %a) #0 {
 ; CHECK-LABEL: @fsub_fneg_nnan_n0_fnX_ebmaytrap(
-; CHECK-NEXT:    [[NEGA:%.*]] = fneg float [[A:%.*]]
-; CHECK-NEXT:    [[RET:%.*]] = call nnan float @llvm.experimental.constrained.fsub.f32(float -0.000000e+00, float [[NEGA]], metadata !"round.tonearest", metadata !"fpexcept.maytrap") #[[ATTR0]]
-; CHECK-NEXT:    ret float [[RET]]
+; CHECK-NEXT:    ret float [[A:%.*]]
 ;
   %nega = fneg float %a
   %ret = call nnan float @llvm.experimental.constrained.fsub.f32(float -0.0, float %nega, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
@@ -275,13 +272,12 @@
   ret float %ret
 }
 
-; TODO: This will fold if we allow non-default floating point environments.
-; TODO: The instruction is expected to remain, but the result isn't used.
+; NOTE: The instruction is expected to remain, but the result isn't used.
 define float @fsub_fneg_nnan_n0_fnX_ebstrict(float %a) #0 {
 ; CHECK-LABEL: @fsub_fneg_nnan_n0_fnX_ebstrict(
 ; CHECK-NEXT:    [[NEGA:%.*]] = fneg float [[A:%.*]]
 ; CHECK-NEXT:    [[RET:%.*]] = call nnan float @llvm.experimental.constrained.fsub.f32(float -0.000000e+00, float [[NEGA]], metadata !"round.tonearest", metadata !"fpexcept.strict") #[[ATTR0]]
-; CHECK-NEXT:    ret float [[RET]]
+; CHECK-NEXT:    ret float [[A]]
 ;
   %nega = fneg float %a
   %ret = call nnan float @llvm.experimental.constrained.fsub.f32(float -0.0, float %nega, metadata !"round.tonearest", metadata !"fpexcept.strict") #0
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5200,15 +5200,16 @@
         (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
       return Op0;
 
-  if (!isDefaultFPEnvironment(ExBehavior, Rounding))
-    return nullptr;
-
   // fsub -0.0, (fsub -0.0, X) ==> X
   // fsub -0.0, (fneg X) ==> X
   Value *X;
-  if (match(Op0, m_NegZeroFP()) &&
-      match(Op1, m_FNeg(m_Value(X))))
-    return X;
+  if (canIgnoreSNaN(ExBehavior, FMF))
+    if (match(Op0, m_NegZeroFP()) &&
+        match(Op1, m_FNeg(m_Value(X))))
+      return X;
+
+  if (!isDefaultFPEnvironment(ExBehavior, Rounding))
+    return nullptr;
 
   // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
   // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123396.421538.patch
Type: text/x-patch
Size: 2764 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220408/f361a350/attachment.bin>


More information about the llvm-commits mailing list