[llvm] e177c5a - [InstSimplify] fold copysign with same args to the arg

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 14:40:21 PST 2019


Author: Sanjay Patel
Date: 2019-11-26T17:35:10-05:00
New Revision: e177c5a00da34ba61b762e2b32bd96e33b0c10b4

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

LOG: [InstSimplify] fold copysign with same args to the arg

This is correct for any value including NaN/inf.

We don't have this fold directly in the backend either,
but x86 manages to get it after converting things to bitops.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/call.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index d997acb365c4..7942cb09e84c 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5086,6 +5086,11 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
         return Op0;
     }
     break;
+  case Intrinsic::copysign:
+    // copysign X, X --> X
+    if (Op0 == Op1)
+      return Op0;
+    break;
   case Intrinsic::maxnum:
   case Intrinsic::minnum:
   case Intrinsic::maximum:

diff  --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index b0f35ab37155..4736adb972d9 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -926,8 +926,7 @@ declare <2 x double> @llvm.copysign.v2f64(<2 x double>, <2 x double>)
 
 define float @copysign_same_operand(float %x) {
 ; CHECK-LABEL: @copysign_same_operand(
-; CHECK-NEXT:    [[R:%.*]] = call float @llvm.copysign.f32(float [[X:%.*]], float [[X]])
-; CHECK-NEXT:    ret float [[R]]
+; CHECK-NEXT:    ret float [[X:%.*]]
 ;
   %r = call float @llvm.copysign.f32(float %x, float %x)
   ret float %r
@@ -935,8 +934,7 @@ define float @copysign_same_operand(float %x) {
 
 define <2 x double> @copysign_same_operand_vec(<2 x double> %x) {
 ; CHECK-LABEL: @copysign_same_operand_vec(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x double> @llvm.copysign.v2f64(<2 x double> [[X:%.*]], <2 x double> [[X]])
-; CHECK-NEXT:    ret <2 x double> [[R]]
+; CHECK-NEXT:    ret <2 x double> [[X:%.*]]
 ;
   %r = call <2 x double> @llvm.copysign.v2f64(<2 x double> %x, <2 x double> %x)
   ret <2 x double> %r


        


More information about the llvm-commits mailing list