From 12f39e0fede92ca04c811660530e750585124ed2 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 8 Dec 2019 10:05:28 -0500 Subject: [PATCH] [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. --- llvm/lib/Analysis/InstructionSimplify.cpp | 3 +++ llvm/test/Transforms/InstSimplify/call.ll | 8 ++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 7942cb0..d151b78 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5090,6 +5090,9 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, // copysign X, X --> X if (Op0 == Op1) return Op0; + // copysign -X, X --> X + if (match(Op0, m_FNeg(m_Specific(Op1)))) + return Op1; break; case Intrinsic::maxnum: case Intrinsic::minnum: diff --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll index 4a0dc41..a8a9bf5 100644 --- a/llvm/test/Transforms/InstSimplify/call.ll +++ b/llvm/test/Transforms/InstSimplify/call.ll @@ -964,9 +964,7 @@ define <2 x double> @negated_sign_arg_vec(<2 x double> %x) { define float @negated_mag_arg(float %x) { ; CHECK-LABEL: @negated_mag_arg( -; CHECK-NEXT: [[NEGX:%.*]] = fneg nnan float [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = call ninf float @llvm.copysign.f32(float [[NEGX]], float [[X]]) -; CHECK-NEXT: ret float [[R]] +; CHECK-NEXT: ret float [[X:%.*]] ; %negx = fneg nnan float %x %r = call ninf float @llvm.copysign.f32(float %negx, float %x) @@ -975,9 +973,7 @@ define float @negated_mag_arg(float %x) { define <2 x double> @negated_mag_arg_vec(<2 x double> %x) { ; CHECK-LABEL: @negated_mag_arg_vec( -; CHECK-NEXT: [[NEGX:%.*]] = fneg afn <2 x double> [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> [[NEGX]], <2 x double> [[X]]) -; CHECK-NEXT: ret <2 x double> [[R]] +; CHECK-NEXT: ret <2 x double> [[X:%.*]] ; %negx = fneg afn <2 x double> %x %r = call arcp <2 x double> @llvm.copysign.v2f64(<2 x double> %negx, <2 x double> %x) -- 2.7.4