[llvm] d799190 - [ConstantFold] fold fsub -0.0, undef to undef rather than NaN

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 05:13:05 PST 2020


Author: Sanjay Patel
Date: 2020-02-21T08:03:19-05:00
New Revision: d799190851fdd94800428ba335f864ce5fd8135b

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

LOG: [ConstantFold] fold fsub -0.0, undef to undef rather than NaN

A question about this behavior came up on llvm-dev:
http://lists.llvm.org/pipermail/llvm-dev/2020-February/139003.html
...and as part of backend improvements in D73978, but this is an IR
change first because we already have fairly thorough tests in place
here.

We decided not to implement a more general change that would have
folded any FP binop with nearly arbitrary constant + undef operand
to undef because that is not theoretically correct (even if it is
practically correct).

Differential Revision: https://reviews.llvm.org/D74713

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Analysis/ConstantFolding/fp-undef.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index ef679fbb5ab5..acd10b46c86f 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1116,8 +1116,12 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
         return C1;
       // undef << X -> 0
       return Constant::getNullValue(C1->getType());
-    case Instruction::FAdd:
     case Instruction::FSub:
+      // -0.0 - undef --> undef (consistent with "fneg undef")
+      if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
+        return C2;
+      LLVM_FALLTHROUGH;
+    case Instruction::FAdd:
     case Instruction::FMul:
     case Instruction::FDiv:
     case Instruction::FRem:

diff  --git a/llvm/test/Analysis/ConstantFolding/fp-undef.ll b/llvm/test/Analysis/ConstantFolding/fp-undef.ll
index e2433092f0a0..ad9cca7cff91 100644
--- a/llvm/test/Analysis/ConstantFolding/fp-undef.ll
+++ b/llvm/test/Analysis/ConstantFolding/fp-undef.ll
@@ -235,7 +235,7 @@ define double @fsub_undef_op0_constant_neg0(double %x) {
 
 define double @fsub_undef_op1_constant_neg0(double %x) {
 ; CHECK-LABEL: @fsub_undef_op1_constant_neg0(
-; CHECK-NEXT:    ret double 0x7FF8000000000000
+; CHECK-NEXT:    ret double undef
 ;
   %r = fsub double 0x8000000000000000, undef
   ret double %r


        


More information about the llvm-commits mailing list