[llvm] r327212 - [InstSimplify] fp_binop X, undef --> NaN
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 10 08:51:28 PST 2018
Author: spatel
Date: Sat Mar 10 08:51:28 2018
New Revision: 327212
URL: http://llvm.org/viewvc/llvm-project?rev=327212&view=rev
Log:
[InstSimplify] fp_binop X, undef --> NaN
The variable operand could be NaN, so it's always safe to propagate NaN.
Modified:
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/test/Transforms/InstCombine/fsub.ll
llvm/trunk/test/Transforms/InstSimplify/fp-undef.ll
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=327212&r1=327211&r2=327212&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sat Mar 10 08:51:28 2018
@@ -4156,6 +4156,9 @@ static Value *SimplifyFAddInst(Value *Op
if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
return C;
+ if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
+ return ConstantFP::getNaN(Op0->getType());
+
// fadd X, -0 ==> X
if (match(Op1, m_NegZero()))
return Op0;
@@ -4190,6 +4193,9 @@ static Value *SimplifyFSubInst(Value *Op
if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
return C;
+ if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
+ return ConstantFP::getNaN(Op0->getType());
+
// fsub X, 0 ==> X
if (match(Op1, m_Zero()))
return Op0;
@@ -4222,6 +4228,9 @@ static Value *SimplifyFMulInst(Value *Op
if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
return C;
+ if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
+ return ConstantFP::getNaN(Op0->getType());
+
// fmul X, 1.0 ==> X
if (match(Op1, m_FPOne()))
return Op0;
@@ -4260,13 +4269,8 @@ static Value *SimplifyFDivInst(Value *Op
if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
return C;
- // undef / X -> undef (the undef could be a snan).
- if (match(Op0, m_Undef()))
- return Op0;
-
- // X / undef -> undef
- if (match(Op1, m_Undef()))
- return Op1;
+ if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
+ return ConstantFP::getNaN(Op0->getType());
// X / 1.0 -> X
if (match(Op1, m_FPOne()))
@@ -4312,13 +4316,8 @@ static Value *SimplifyFRemInst(Value *Op
if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
return C;
- // undef % X -> undef (the undef could be a snan).
- if (match(Op0, m_Undef()))
- return Op0;
-
- // X % undef -> undef
- if (match(Op1, m_Undef()))
- return Op1;
+ if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
+ return ConstantFP::getNaN(Op0->getType());
// 0 % X -> 0
// Requires that NaNs are off (X could be zero) and signed zeroes are
Modified: llvm/trunk/test/Transforms/InstCombine/fsub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fsub.ll?rev=327212&r1=327211&r2=327212&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fsub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fsub.ll Sat Mar 10 08:51:28 2018
@@ -27,23 +27,3 @@ define double @test2(double %x, double %
ret double %t2
}
-define float @fsub_undef(float %val) {
-; CHECK-LABEL: @fsub_undef(
-; CHECK-NEXT: [[SUB:%.*]] = fsub float [[VAL:%.*]], undef
-; CHECK-NEXT: ret float [[SUB]]
-;
- %sub = fsub float %val, undef
- ret float %sub
-}
-
-; XXX - Why doesn't this fold to undef?
-
-define float @fsub_fast_undef(float %val) {
-; CHECK-LABEL: @fsub_fast_undef(
-; CHECK-NEXT: [[SUB:%.*]] = fsub fast float [[VAL:%.*]], undef
-; CHECK-NEXT: ret float [[SUB]]
-;
- %sub = fsub fast float %val, undef
- ret float %sub
-}
-
Modified: llvm/trunk/test/Transforms/InstSimplify/fp-undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/fp-undef.ll?rev=327212&r1=327211&r2=327212&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/fp-undef.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/fp-undef.ll Sat Mar 10 08:51:28 2018
@@ -3,8 +3,7 @@
define float @fadd_undef_op0(float %x) {
; CHECK-LABEL: @fadd_undef_op0(
-; CHECK-NEXT: [[R:%.*]] = fadd float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fadd float undef, %x
ret float %r
@@ -12,8 +11,7 @@ define float @fadd_undef_op0(float %x) {
define float @fadd_undef_op1(float %x) {
; CHECK-LABEL: @fadd_undef_op1(
-; CHECK-NEXT: [[R:%.*]] = fadd float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fadd float %x, undef
ret float %r
@@ -21,8 +19,7 @@ define float @fadd_undef_op1(float %x) {
define float @fsub_undef_op0(float %x) {
; CHECK-LABEL: @fsub_undef_op0(
-; CHECK-NEXT: [[R:%.*]] = fsub float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fsub float undef, %x
ret float %r
@@ -30,8 +27,7 @@ define float @fsub_undef_op0(float %x) {
define float @fsub_undef_op1(float %x) {
; CHECK-LABEL: @fsub_undef_op1(
-; CHECK-NEXT: [[R:%.*]] = fsub float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fsub float %x, undef
ret float %r
@@ -39,8 +35,7 @@ define float @fsub_undef_op1(float %x) {
define float @fmul_undef_op0(float %x) {
; CHECK-LABEL: @fmul_undef_op0(
-; CHECK-NEXT: [[R:%.*]] = fmul float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fmul float undef, %x
ret float %r
@@ -48,8 +43,7 @@ define float @fmul_undef_op0(float %x) {
define float @fmul_undef_op1(float %x) {
; CHECK-LABEL: @fmul_undef_op1(
-; CHECK-NEXT: [[R:%.*]] = fmul float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fmul float %x, undef
ret float %r
@@ -57,7 +51,7 @@ define float @fmul_undef_op1(float %x) {
define float @fdiv_undef_op0(float %x) {
; CHECK-LABEL: @fdiv_undef_op0(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fdiv float undef, %x
ret float %r
@@ -65,7 +59,7 @@ define float @fdiv_undef_op0(float %x) {
define float @fdiv_undef_op1(float %x) {
; CHECK-LABEL: @fdiv_undef_op1(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fdiv float %x, undef
ret float %r
@@ -73,7 +67,7 @@ define float @fdiv_undef_op1(float %x) {
define float @frem_undef_op0(float %x) {
; CHECK-LABEL: @frem_undef_op0(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = frem float undef, %x
ret float %r
@@ -81,7 +75,7 @@ define float @frem_undef_op0(float %x) {
define float @frem_undef_op1(float %x) {
; CHECK-LABEL: @frem_undef_op1(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = frem float %x, undef
ret float %r
@@ -91,8 +85,7 @@ define float @frem_undef_op1(float %x) {
define float @fadd_undef_op0_nnan(float %x) {
; CHECK-LABEL: @fadd_undef_op0_nnan(
-; CHECK-NEXT: [[R:%.*]] = fadd nnan float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fadd nnan float undef, %x
ret float %r
@@ -100,8 +93,7 @@ define float @fadd_undef_op0_nnan(float
define float @fadd_undef_op1_fast(float %x) {
; CHECK-LABEL: @fadd_undef_op1_fast(
-; CHECK-NEXT: [[R:%.*]] = fadd fast float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fadd fast float %x, undef
ret float %r
@@ -109,8 +101,7 @@ define float @fadd_undef_op1_fast(float
define float @fsub_undef_op0_fast(float %x) {
; CHECK-LABEL: @fsub_undef_op0_fast(
-; CHECK-NEXT: [[R:%.*]] = fsub fast float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fsub fast float undef, %x
ret float %r
@@ -118,8 +109,7 @@ define float @fsub_undef_op0_fast(float
define float @fsub_undef_op1_nnan(float %x) {
; CHECK-LABEL: @fsub_undef_op1_nnan(
-; CHECK-NEXT: [[R:%.*]] = fsub nnan float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fsub nnan float %x, undef
ret float %r
@@ -127,8 +117,7 @@ define float @fsub_undef_op1_nnan(float
define float @fmul_undef_op0_nnan(float %x) {
; CHECK-LABEL: @fmul_undef_op0_nnan(
-; CHECK-NEXT: [[R:%.*]] = fmul nnan float undef, [[X:%.*]]
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fmul nnan float undef, %x
ret float %r
@@ -136,8 +125,7 @@ define float @fmul_undef_op0_nnan(float
define float @fmul_undef_op1_fast(float %x) {
; CHECK-LABEL: @fmul_undef_op1_fast(
-; CHECK-NEXT: [[R:%.*]] = fmul fast float [[X:%.*]], undef
-; CHECK-NEXT: ret float [[R]]
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fmul fast float %x, undef
ret float %r
@@ -145,7 +133,7 @@ define float @fmul_undef_op1_fast(float
define float @fdiv_undef_op0_fast(float %x) {
; CHECK-LABEL: @fdiv_undef_op0_fast(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fdiv fast float undef, %x
ret float %r
@@ -153,7 +141,7 @@ define float @fdiv_undef_op0_fast(float
define float @fdiv_undef_op1_nnan(float %x) {
; CHECK-LABEL: @fdiv_undef_op1_nnan(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = fdiv nnan float %x, undef
ret float %r
@@ -161,7 +149,7 @@ define float @fdiv_undef_op1_nnan(float
define float @frem_undef_op0_nnan(float %x) {
; CHECK-LABEL: @frem_undef_op0_nnan(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = frem nnan float undef, %x
ret float %r
@@ -169,7 +157,7 @@ define float @frem_undef_op0_nnan(float
define float @frem_undef_op1_fast(float %x) {
; CHECK-LABEL: @frem_undef_op1_fast(
-; CHECK-NEXT: ret float undef
+; CHECK-NEXT: ret float 0x7FF8000000000000
;
%r = frem fast float %x, undef
ret float %r
More information about the llvm-commits
mailing list