[llvm] 7bb9a2f - [InstSimplify] fix miscompiles with maximum/minimum intrinsics

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 06:10:02 PDT 2020


Author: Sanjay Patel
Date: 2020-09-14T09:06:41-04:00
New Revision: 7bb9a2f996a33fde689fc0b7603fce0115fb92b4

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

LOG: [InstSimplify] fix miscompiles with maximum/minimum intrinsics

As discussed in the sibling codegen functionality patch D87571,
this transform was created with D52766, but it is not correct.

The incorrect test diffs were missed during review, but the
'TODO' comment about this functionality was still in the code -
we need 'nnan' to enable this fold.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index f7f5105f9383..271e79df7153 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5476,9 +5476,9 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     bool UseNegInf = IID == Intrinsic::minnum || IID == Intrinsic::minimum;
     const APFloat *C;
     if ((match(Op0, m_APFloat(C)) && C->isInfinity() &&
-         C->isNegative() == UseNegInf) ||
+         C->isNegative() == UseNegInf && !PropagateNaN) ||
         (match(Op1, m_APFloat(C)) && C->isInfinity() &&
-         C->isNegative() == UseNegInf))
+         C->isNegative() == UseNegInf && !PropagateNaN))
       return ConstantFP::getInfinity(ReturnType, UseNegInf);
 
     // TODO: minnum(nnan x, inf) -> x

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index 8b606dca2e21..0707f08bf69b 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -1064,7 +1064,8 @@ define float @minimum_x_y_minimum_z(float %x, float %y, float %z) {
 
 define float @minimum_neginf(float %x) {
 ; CHECK-LABEL: @minimum_neginf(
-; CHECK-NEXT:    ret float 0xFFF0000000000000
+; CHECK-NEXT:    [[VAL:%.*]] = call float @llvm.minimum.f32(float [[X:%.*]], float 0xFFF0000000000000)
+; CHECK-NEXT:    ret float [[VAL]]
 ;
   %val = call float @llvm.minimum.f32(float %x, float 0xFFF0000000000000)
   ret float %val
@@ -1072,7 +1073,8 @@ define float @minimum_neginf(float %x) {
 
 define <2 x double> @minimum_neginf_commute_vec(<2 x double> %x) {
 ; CHECK-LABEL: @minimum_neginf_commute_vec(
-; CHECK-NEXT:    ret <2 x double> <double 0xFFF0000000000000, double 0xFFF0000000000000>
+; CHECK-NEXT:    [[R:%.*]] = call <2 x double> @llvm.minimum.v2f64(<2 x double> <double 0xFFF0000000000000, double 0xFFF0000000000000>, <2 x double> [[X:%.*]])
+; CHECK-NEXT:    ret <2 x double> [[R]]
 ;
   %r = call <2 x double> @llvm.minimum.v2f64(<2 x double> <double 0xFFF0000000000000, double 0xFFF0000000000000>, <2 x double> %x)
   ret <2 x double> %r
@@ -1158,7 +1160,8 @@ define float @maximum_x_y_maximum_z(float %x, float %y, float %z) {
 
 define <2 x double> @maximum_inf(<2 x double> %x) {
 ; CHECK-LABEL: @maximum_inf(
-; CHECK-NEXT:    ret <2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>
+; CHECK-NEXT:    [[VAL:%.*]] = call <2 x double> @llvm.maximum.v2f64(<2 x double> [[X:%.*]], <2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000>)
+; CHECK-NEXT:    ret <2 x double> [[VAL]]
 ;
   %val = call <2 x double> @llvm.maximum.v2f64(<2 x double> %x, <2 x double><double 0x7FF0000000000000, double 0x7FF0000000000000>)
   ret <2 x double> %val
@@ -1166,7 +1169,8 @@ define <2 x double> @maximum_inf(<2 x double> %x) {
 
 define float @maximum_inf_commute(float %x) {
 ; CHECK-LABEL: @maximum_inf_commute(
-; CHECK-NEXT:    ret float 0x7FF0000000000000
+; CHECK-NEXT:    [[VAL:%.*]] = call float @llvm.maximum.f32(float 0x7FF0000000000000, float [[X:%.*]])
+; CHECK-NEXT:    ret float [[VAL]]
 ;
   %val = call float @llvm.maximum.f32(float 0x7FF0000000000000, float %x)
   ret float %val


        


More information about the llvm-commits mailing list