[llvm] c603fd2 - ValueTracking: Implement computeKnownFPClass for sin/cos

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 11:37:03 PDT 2023


Author: Matt Arsenault
Date: 2023-04-14T14:36:55-04:00
New Revision: c603fd2f39beab13cd1df700467afcc324ec1d85

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

LOG: ValueTracking: Implement computeKnownFPClass for sin/cos

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/Attributor/nofpclass-sin-cos.ll
    llvm/test/Transforms/Attributor/nosync.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8a23100ee6005..48a9dc1ac4907 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4437,6 +4437,17 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
         Known.copysign(KnownSign);
         break;
       }
+      case Intrinsic::sin:
+      case Intrinsic::cos: {
+        // Return NaN on infinite inputs.
+        KnownFPClass KnownSrc;
+        computeKnownFPClass(II->getArgOperand(0), DemandedElts,
+                            InterestedClasses, KnownSrc, Depth + 1, Q, TLI);
+        Known.knownNot(fcInf);
+        if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
+          Known.knownNot(fcNan);
+        break;
+      }
       default:
         break;
       }

diff  --git a/llvm/test/Transforms/Attributor/nofpclass-sin-cos.ll b/llvm/test/Transforms/Attributor/nofpclass-sin-cos.ll
index 8d8790d7fb3ad..3170e8b06e7ea 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-sin-cos.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-sin-cos.ll
@@ -5,9 +5,9 @@ declare float @llvm.sin.f32(float)
 declare float @llvm.cos.f32(float)
 
 define float @ret_sin(float %arg) {
-; CHECK-LABEL: define float @ret_sin
+; CHECK-LABEL: define nofpclass(inf) float @ret_sin
 ; CHECK-SAME: (float [[ARG:%.*]]) #[[ATTR1:[0-9]+]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.sin.f32(float [[ARG]]) #[[ATTR2:[0-9]+]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.sin.f32(float [[ARG]]) #[[ATTR2:[0-9]+]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.sin.f32(float %arg)
@@ -15,9 +15,9 @@ define float @ret_sin(float %arg) {
 }
 
 define float @ret_cos(float %arg) {
-; CHECK-LABEL: define float @ret_cos
+; CHECK-LABEL: define nofpclass(inf) float @ret_cos
 ; CHECK-SAME: (float [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.cos.f32(float %arg)
@@ -25,9 +25,9 @@ define float @ret_cos(float %arg) {
 }
 
 define float @ret_sin_noinf(float nofpclass(inf) %arg) {
-; CHECK-LABEL: define float @ret_sin_noinf
+; CHECK-LABEL: define nofpclass(inf) float @ret_sin_noinf
 ; CHECK-SAME: (float nofpclass(inf) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.sin.f32(float %arg)
@@ -35,9 +35,9 @@ define float @ret_sin_noinf(float nofpclass(inf) %arg) {
 }
 
 define float @ret_cos_noinf(float nofpclass(inf) %arg) {
-; CHECK-LABEL: define float @ret_cos_noinf
+; CHECK-LABEL: define nofpclass(inf) float @ret_cos_noinf
 ; CHECK-SAME: (float nofpclass(inf) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.cos.f32(float %arg)
@@ -45,9 +45,9 @@ define float @ret_cos_noinf(float nofpclass(inf) %arg) {
 }
 
 define float @ret_sin_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define float @ret_sin_nonan
+; CHECK-LABEL: define nofpclass(inf) float @ret_sin_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.sin.f32(float %arg)
@@ -55,9 +55,9 @@ define float @ret_sin_nonan(float nofpclass(nan) %arg) {
 }
 
 define float @ret_cos_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define float @ret_cos_nonan
+; CHECK-LABEL: define nofpclass(inf) float @ret_cos_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.cos.f32(float %arg)
@@ -65,9 +65,9 @@ define float @ret_cos_nonan(float nofpclass(nan) %arg) {
 }
 
 define float @ret_sin_nonan_noinf(float nofpclass(nan inf) %arg) {
-; CHECK-LABEL: define float @ret_sin_nonan_noinf
+; CHECK-LABEL: define nofpclass(nan inf) float @ret_sin_nonan_noinf
 ; CHECK-SAME: (float nofpclass(nan inf) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf) float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.sin.f32(float %arg)
@@ -75,9 +75,9 @@ define float @ret_sin_nonan_noinf(float nofpclass(nan inf) %arg) {
 }
 
 define float @ret_cos_nonan_noinf(float nofpclass(nan inf) %arg) {
-; CHECK-LABEL: define float @ret_cos_nonan_noinf
+; CHECK-LABEL: define nofpclass(nan inf) float @ret_cos_nonan_noinf
 ; CHECK-SAME: (float nofpclass(nan inf) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf) float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.cos.f32(float %arg)
@@ -86,9 +86,9 @@ define float @ret_cos_nonan_noinf(float nofpclass(nan inf) %arg) {
 
 
 define float @ret_sin_noqnan(float nofpclass(qnan) %arg) {
-; CHECK-LABEL: define float @ret_sin_noqnan
+; CHECK-LABEL: define nofpclass(inf) float @ret_sin_noqnan
 ; CHECK-SAME: (float nofpclass(qnan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.sin.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.sin.f32(float %arg)
@@ -96,9 +96,9 @@ define float @ret_sin_noqnan(float nofpclass(qnan) %arg) {
 }
 
 define float @ret_cos_noqnan(float nofpclass(qnan) %arg) {
-; CHECK-LABEL: define float @ret_cos_noqnan
+; CHECK-LABEL: define nofpclass(inf) float @ret_cos_noqnan
 ; CHECK-SAME: (float nofpclass(qnan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.cos.f32(float [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.cos.f32(float %arg)

diff  --git a/llvm/test/Transforms/Attributor/nosync.ll b/llvm/test/Transforms/Attributor/nosync.ll
index 79c0595179399..f65f641b298ae 100644
--- a/llvm/test/Transforms/Attributor/nosync.ll
+++ b/llvm/test/Transforms/Attributor/nosync.ll
@@ -436,7 +436,7 @@ define float @cos_test2(float %x) {
 ; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
 ; CHECK-LABEL: define {{[^@]+}}@cos_test2
 ; CHECK-SAME: (float [[X:%.*]]) #[[ATTR15]] {
-; CHECK-NEXT:    [[C:%.*]] = call float @llvm.cos.f32(float [[X]]) #[[ATTR20]]
+; CHECK-NEXT:    [[C:%.*]] = call nofpclass(inf) float @llvm.cos.f32(float [[X]]) #[[ATTR20]]
 ; CHECK-NEXT:    ret float [[C]]
 ;
   %c = call float @llvm.cos(float %x)


        


More information about the llvm-commits mailing list