[PATCH] D65898: [InstCombine] x /c fabs(x) -> copysign(1.0, x)

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 9 07:34:58 PDT 2019


xbolva00 updated this revision to Diff 214378.
xbolva00 retitled this revision from "[InstCombine] x / fabs(x) -> copysign(1.0, x)" to "[InstCombine] x /c fabs(x) -> copysign(1.0, x)".
xbolva00 edited the summary of this revision.
xbolva00 added a comment.

Addressed review notes


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65898/new/

https://reviews.llvm.org/D65898

Files:
  lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  test/Transforms/InstCombine/fabs-copysign.ll


Index: test/Transforms/InstCombine/fabs-copysign.ll
===================================================================
--- test/Transforms/InstCombine/fabs-copysign.ll
+++ test/Transforms/InstCombine/fabs-copysign.ll
@@ -10,21 +10,29 @@
 
 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
   ret double %div
 }
 
+define double @fabs_copysign_commuted(double %x) {
+; CHECK-LABEL: @fabs_copysign_commuted(
+; 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 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
@@ -34,10 +42,12 @@
 define double @fabs_copysign_use(double %x) {
 ; CHECK-LABEL: @fabs_copysign_use(
 ; 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:    call void @use(double [[F]])
+; 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)
   %div = fdiv nnan ninf double %x, %f
   ret double %div
 }
@@ -55,6 +65,17 @@
   ret double %div
 }
 
+define double @fabs_copysign_commuted_mismatch(double %x, double %y) {
+; CHECK-LABEL: @fabs_copysign_commuted_mismatch(
+; CHECK-NEXT:    [[F:%.*]] = tail call double @llvm.fabs.f64(double [[Y:%.*]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv double [[F]], [[X:%.*]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %f = tail call double @llvm.fabs.f64(double %y)
+  %div = fdiv double %f, %x
+  ret double %div
+}
+
 define double @fabs_copysign_no_nnan(double %x) {
 ; CHECK-LABEL: @fabs_copysign_no_nnan(
 ; CHECK-NEXT:    [[F:%.*]] = tail call double @llvm.fabs.f64(double [[X:%.*]])
Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1234,6 +1234,20 @@
     return &I;
   }
 
+  // X / fabs(X) -> copysign(1.0, X)
+  if (I.hasNoNaNs() && I.hasNoInfs() &&
+      match(Op1, m_Intrinsic<Intrinsic::fabs>(m_Specific(Op0)))) {
+    Value *V = Builder.CreateBinaryIntrinsic(
+        Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), Op0, &I);
+    return replaceInstUsesWith(I, V);
+  }
+  // fabs(X) / X -> copysign(1.0, X)
+  if (I.hasNoNaNs() && I.hasNoInfs() &&
+      match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Specific(Op1)))) {
+    Value *V = Builder.CreateBinaryIntrinsic(
+        Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), Op1, &I);
+    return replaceInstUsesWith(I, V);
+  }
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65898.214378.patch
Type: text/x-patch
Size: 3803 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190809/00c1a802/attachment.bin>


More information about the llvm-commits mailing list