From: Sanjay Patel Date: Thu, 10 Nov 2022 22:35:36 +0000 (-0500) Subject: [InstSimplify] fold fsub nnan with Inf operand X-Git-Tag: upstream/17.0.6~27934 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=21f1b2da95a32307d42128d9fb446c57f0fb8ee9;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index b134234..bfb960a 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 14d179d..31025c1 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> , [[X:%.*]] -; CHECK-NEXT: ret <2 x double> [[R]] +; CHECK-NEXT: ret <2 x double> ; %r = fsub nnan <2 x double> , %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 4ec782e..7da1c95 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