Skip to content

Commit 12f39e0

Browse files
committed
[InstSimplify] fold copysign with negated operand
This is another transform suggested in PR44153: https://bugs.llvm.org/show_bug.cgi?id=44153 The backend for some targets already manages to get this if it converts copysign to bitwise logic.
1 parent b324902 commit 12f39e0

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,6 +5090,9 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
50905090
// copysign X, X --> X
50915091
if (Op0 == Op1)
50925092
return Op0;
5093+
// copysign -X, X --> X
5094+
if (match(Op0, m_FNeg(m_Specific(Op1))))
5095+
return Op1;
50935096
break;
50945097
case Intrinsic::maxnum:
50955098
case Intrinsic::minnum:

llvm/test/Transforms/InstSimplify/call.ll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,7 @@ define <2 x double> @negated_sign_arg_vec(<2 x double> %x) {
964964

965965
define float @negated_mag_arg(float %x) {
966966
; CHECK-LABEL: @negated_mag_arg(
967-
; CHECK-NEXT: [[NEGX:%.*]] = fneg nnan float [[X:%.*]]
968-
; CHECK-NEXT: [[R:%.*]] = call ninf float @llvm.copysign.f32(float [[NEGX]], float [[X]])
969-
; CHECK-NEXT: ret float [[R]]
967+
; CHECK-NEXT: ret float [[X:%.*]]
970968
;
971969
%negx = fneg nnan float %x
972970
%r = call ninf float @llvm.copysign.f32(float %negx, float %x)
@@ -975,9 +973,7 @@ define float @negated_mag_arg(float %x) {
975973

976974
define <2 x double> @negated_mag_arg_vec(<2 x double> %x) {
977975
; CHECK-LABEL: @negated_mag_arg_vec(
978-
; CHECK-NEXT: [[NEGX:%.*]] = fneg afn <2 x double> [[X:%.*]]
979-
; CHECK-NEXT: [[R:%.*]] = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> [[NEGX]], <2 x double> [[X]])
980-
; CHECK-NEXT: ret <2 x double> [[R]]
976+
; CHECK-NEXT: ret <2 x double> [[X:%.*]]
981977
;
982978
%negx = fneg afn <2 x double> %x
983979
%r = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> %negx, <2 x double> %x)

0 commit comments

Comments
 (0)