aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2020-02-21 07:58:36 -0500
committerSanjay Patel <spatel@rotateright.com>2020-02-21 08:03:19 -0500
commitd799190851fdd94800428ba335f864ce5fd8135b (patch)
tree5de0d299a4f8e0b8d8bce0b2c3b07b5c1d86abf3
parent99b03c1c18de3e4228e31ef04d38f2d530d335be (diff)
downloadllvm-d799190851fdd94800428ba335f864ce5fd8135b.zip
llvm-d799190851fdd94800428ba335f864ce5fd8135b.tar.gz
llvm-d799190851fdd94800428ba335f864ce5fd8135b.tar.bz2
[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
-rw-r--r--llvm/lib/IR/ConstantFold.cpp6
-rw-r--r--llvm/test/Analysis/ConstantFolding/fp-undef.ll2
2 files changed, 6 insertions, 2 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index ef679fb..acd10b4 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 e243309..ad9cca7 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