[llvm] r368570 - [InstCombine] x /c fabs(x) -> copysign(1.0, x)
David Bolvansky via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 12 06:43:36 PDT 2019
Author: xbolva00
Date: Mon Aug 12 06:43:35 2019
New Revision: 368570
URL: http://llvm.org/viewvc/llvm-project?rev=368570&view=rev
Log:
[InstCombine] x /c fabs(x) -> copysign(1.0, x)
Summary:
x / fabs(x) -> copysign(1.0, x)
fabs(x) / x -> copysign(1.0, x)
Reviewers: spatel, foad, RKSimon, efriedma
Reviewed By: spatel
Subscribers: lebedev.ri, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65898
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/fabs-copysign.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=368570&r1=368569&r2=368570&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Aug 12 06:43:35 2019
@@ -1234,6 +1234,17 @@ Instruction *InstCombiner::visitFDiv(Bin
return &I;
}
+ // X / fabs(X) -> copysign(1.0, X)
+ // fabs(X) / X -> copysign(1.0, X)
+ if (I.hasNoNaNs() && I.hasNoInfs() &&
+ (match(&I,
+ m_FDiv(m_Value(X), m_Intrinsic<Intrinsic::fabs>(m_Deferred(X)))) ||
+ match(&I, m_FDiv(m_Intrinsic<Intrinsic::fabs>(m_Value(X)),
+ m_Deferred(X))))) {
+ Value *V = Builder.CreateBinaryIntrinsic(
+ Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), X, &I);
+ return replaceInstUsesWith(I, V);
+ }
return nullptr;
}
Modified: llvm/trunk/test/Transforms/InstCombine/fabs-copysign.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fabs-copysign.ll?rev=368570&r1=368569&r2=368570&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fabs-copysign.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/fabs-copysign.ll Mon Aug 12 06:43:35 2019
@@ -3,6 +3,7 @@
declare double @llvm.fabs.f64(double)
declare float @llvm.fabs.f32(float)
+declare <4 x double> @llvm.fabs.v4f64(<4 x double>)
declare void @use(double)
declare double @llvm.copysign.f64(double, double)
@@ -10,9 +11,8 @@ declare float @llvm.copysign.f32(float,
define double @fabs_copysign(double %x) {
; CHECK-LABEL: @fabs_copysign(
-; CHECK-NEXT: [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
-; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf double [[X]], [[F]]
-; CHECK-NEXT: ret double [[DIV]]
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf double @llvm.copysign.f64(double 1.000000e+00, double [[X:%.*]])
+; CHECK-NEXT: ret double [[TMP1]]
;
%f = tail call double @llvm.fabs.f64(double %x)
%div = fdiv nnan ninf double %x, %f
@@ -21,21 +21,38 @@ define double @fabs_copysign(double %x)
define double @fabs_copysign_commuted(double %x) {
; CHECK-LABEL: @fabs_copysign_commuted(
-; CHECK-NEXT: [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
-; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf double [[F]], [[X]]
-; CHECK-NEXT: ret double [[DIV]]
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf double @llvm.copysign.f64(double 1.000000e+00, double [[X:%.*]])
+; CHECK-NEXT: ret double [[TMP1]]
;
%f = tail call double @llvm.fabs.f64(double %x)
%div = fdiv nnan ninf double %f, %x
ret double %div
}
+define <4 x double> @fabs_copysign_vec(<4 x double> %x) {
+; CHECK-LABEL: @fabs_copysign_vec(
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <4 x double> @llvm.copysign.v4f64(<4 x double> <double 1.000000e+00, double 1.000000e+00, double 1.000000e+00, double 1.000000e+00>, <4 x double> [[X:%.*]])
+; CHECK-NEXT: ret <4 x double> [[TMP1]]
+;
+ %f = call <4 x double> @llvm.fabs.v4f64(<4 x double> %x)
+ %div = fdiv nnan ninf <4 x double> %x, %f
+ ret <4 x double> %div
+}
+
+define <4 x double> @fabs_copysign_vec_commuted(<4 x double> %x) {
+; CHECK-LABEL: @fabs_copysign_vec_commuted(
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf <4 x double> @llvm.copysign.v4f64(<4 x double> <double 1.000000e+00, double 1.000000e+00, double 1.000000e+00, double 1.000000e+00>, <4 x double> [[X:%.*]])
+; CHECK-NEXT: ret <4 x double> [[TMP1]]
+;
+ %f = call <4 x double> @llvm.fabs.v4f64(<4 x double> %x)
+ %div = fdiv nnan ninf <4 x double> %f, %x
+ ret <4 x double> %div
+}
define float @fabs_copysignf(float %x) {
; CHECK-LABEL: @fabs_copysignf(
-; CHECK-NEXT: [[F:%.*]] = tail call float @llvm.fabs.f32(float [[X:%.*]])
-; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf float [[X]], [[F]]
-; CHECK-NEXT: ret float [[DIV]]
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf float @llvm.copysign.f32(float 1.000000e+00, float [[X:%.*]])
+; CHECK-NEXT: ret float [[TMP1]]
;
%f = tail call float @llvm.fabs.f32(float %x)
%div = fdiv nnan ninf float %x, %f
@@ -46,8 +63,8 @@ define double @fabs_copysign_use(double
; CHECK-LABEL: @fabs_copysign_use(
; CHECK-NEXT: [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: call void @use(double [[F]])
-; CHECK-NEXT: [[DIV:%.*]] = fdiv nnan ninf double [[X]], [[F]]
-; CHECK-NEXT: ret double [[DIV]]
+; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf double @llvm.copysign.f64(double 1.000000e+00, double [[X]])
+; CHECK-NEXT: ret double [[TMP1]]
;
%f = tail call double @llvm.fabs.f64(double %x)
call void @use(double %f)
More information about the llvm-commits
mailing list