[llvm] 21f1b2d - [InstSimplify] fold fsub nnan with Inf operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 11 05:42:50 PST 2022


Author: Sanjay Patel
Date: 2022-11-11T08:42:44-05:00
New Revision: 21f1b2da95a32307d42128d9fb446c57f0fb8ee9

URL: https://github.com/llvm/llvm-project/commit/21f1b2da95a32307d42128d9fb446c57f0fb8ee9
DIFF: https://github.com/llvm/llvm-project/commit/21f1b2da95a32307d42128d9fb446c57f0fb8ee9.diff

LOG: [InstSimplify] fold fsub nnan with Inf operand

Similar to fbc2c8f2fbbb, but if we have a non-canonical
fsub with constant operand 1, then flip the sign of the
Infinity:
https://alive2.llvm.org/ce/z/vKWfhW

If Infinity is operand 0, then the sign remains:
https://alive2.llvm.org/ce/z/73d97C

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
    llvm/test/Transforms/InstSimplify/fp-undef-poison.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index b134234dda07a..bfb960a02a98a 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5322,9 +5322,19 @@ simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
   if (!isDefaultFPEnvironment(ExBehavior, Rounding))
     return nullptr;
 
-  // fsub nnan x, x ==> 0.0
-  if (FMF.noNaNs() && Op0 == Op1)
-    return Constant::getNullValue(Op0->getType());
+  if (FMF.noNaNs()) {
+    // fsub nnan x, x ==> 0.0
+    if (Op0 == Op1)
+      return Constant::getNullValue(Op0->getType());
+
+    // With nnan: {+/-}Inf - X --> {+/-}Inf
+    if (match(Op0, m_Inf()))
+      return Op0;
+
+    // With nnan: X - {+/-}Inf --> {-/+}Inf
+    if (match(Op1, m_Inf()))
+      return foldConstant(Instruction::FNeg, Op1, Q);
+  }
 
   // Y - (Y - X) --> X
   // (X + Y) - Y --> X

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index 14d179d811793..31025c128f940 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -861,17 +861,17 @@ define double @fadd_inf_op0(double %x) {
 
 define double @fsub_nnan_inf_op0(double %x) {
 ; CHECK-LABEL: @fsub_nnan_inf_op0(
-; CHECK-NEXT:    [[R:%.*]] = fsub nnan double 0x7FF0000000000000, [[X:%.*]]
-; CHECK-NEXT:    ret double [[R]]
+; CHECK-NEXT:    ret double 0x7FF0000000000000
 ;
   %r = fsub nnan double 0x7ff0000000000000, %x
   ret double %r
 }
 
+; flip sign
+
 define double @fsub_nnan_inf_op1(double %x) {
 ; CHECK-LABEL: @fsub_nnan_inf_op1(
-; CHECK-NEXT:    [[R:%.*]] = fsub nnan double [[X:%.*]], 0x7FF0000000000000
-; CHECK-NEXT:    ret double [[R]]
+; CHECK-NEXT:    ret double 0xFFF0000000000000
 ;
   %r = fsub nnan double %x, 0x7ff0000000000000
   ret double %r
@@ -879,22 +879,24 @@ define double @fsub_nnan_inf_op1(double %x) {
 
 define <2 x double> @fsub_nnan_neginf_op0(<2 x double> %x) {
 ; CHECK-LABEL: @fsub_nnan_neginf_op0(
-; CHECK-NEXT:    [[R:%.*]] = fsub nnan <2 x double> <double 0xFFF0000000000000, double poison>, [[X:%.*]]
-; CHECK-NEXT:    ret <2 x double> [[R]]
+; CHECK-NEXT:    ret <2 x double> <double 0xFFF0000000000000, double poison>
 ;
   %r = fsub nnan <2 x double> <double 0xfff0000000000000, double poison>, %x
   ret <2 x double> %r
 }
 
+; flip sign
+
 define double @fsub_nnan_neginf_op1(double %x) {
 ; CHECK-LABEL: @fsub_nnan_neginf_op1(
-; CHECK-NEXT:    [[R:%.*]] = fsub nnan double [[X:%.*]], 0xFFF0000000000000
-; CHECK-NEXT:    ret double [[R]]
+; CHECK-NEXT:    ret double 0x7FF0000000000000
 ;
   %r = fsub nnan double %x, 0xfff0000000000000
   ret double %r
 }
 
+; negative test - requires nnan
+
 define double @fsub_inf_op0(double %x) {
 ; CHECK-LABEL: @fsub_inf_op0(
 ; CHECK-NEXT:    [[R:%.*]] = fsub double 0x7FF0000000000000, [[X:%.*]]

diff  --git a/llvm/test/Transforms/InstSimplify/fp-undef-poison.ll b/llvm/test/Transforms/InstSimplify/fp-undef-poison.ll
index 4ec782e0636c3..7da1c95121b4d 100644
--- a/llvm/test/Transforms/InstSimplify/fp-undef-poison.ll
+++ b/llvm/test/Transforms/InstSimplify/fp-undef-poison.ll
@@ -275,12 +275,9 @@ define double @fadd_ninf_inf_op1(double %x) {
   ret double %r
 }
 
-; TODO: Should simplify to inf.
-
 define double @fsub_nnan_inf_op0(double %x) {
 ; CHECK-LABEL: @fsub_nnan_inf_op0(
-; CHECK-NEXT:    [[R:%.*]] = fsub nnan double 0x7FF0000000000000, [[X:%.*]]
-; CHECK-NEXT:    ret double [[R]]
+; CHECK-NEXT:    ret double 0x7FF0000000000000
 ;
   %r = fsub nnan double 0x7ff0000000000000, %x
   ret double %r


        


More information about the llvm-commits mailing list