[llvm] [SimplifyLibCalls] Constant fold `remquo` (PR #99647)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 05:35:07 PDT 2024


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/99647

>From 3ca034696564b5bcc33fd3f7c4e26dd141da5a11 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 19 Jul 2024 21:11:45 +0800
Subject: [PATCH 1/7] [SimplfiyLibCalls] Constant fold remquo

---
 .../llvm/Transforms/Utils/SimplifyLibCalls.h  |   1 +
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp |  33 +++++
 llvm/test/Transforms/InstCombine/remquo.ll    | 116 ++++++++++++++++++
 3 files changed, 150 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/remquo.ll

diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index e2682b429e9db..770da12e01233 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -205,6 +205,7 @@ class LibCallSimplifier {
   Value *optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B);
   Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
   Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
+  Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
   // Wrapper for all floating point library call optimizations
   Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
                                       IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 89c8c5bf08954..f62c53c208f9f 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3018,6 +3018,35 @@ void LibCallSimplifier::classifyArgUse(
   }
 }
 
+/// Constant folds remquo
+Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
+  const APFloat *X, *Y;
+  if (!match(CI->getArgOperand(0), m_APFloat(X)) || !match(CI->getArgOperand(1), m_APFloat(Y)))
+    return nullptr;
+
+  if (X->isNaN() || Y->isNaN() || X->isInfinity() || Y->isZero())
+    return nullptr;
+
+  APFloat::opStatus Status;
+  APFloat Quot = *X;
+  Status = Quot.divide(*Y, APFloat::rmNearestTiesToEven);
+  if (Status != APFloat::opOK && Status != APFloat::opInexact)
+    return nullptr;
+  APFloat Rem = *X;
+  if (Rem.remainder(*Y) != APFloat::opOK)
+    return nullptr;
+
+  // TODO: We can only keep at least the three of the last bits of x/y
+  APSInt QuotInt(32, /*isUnsigned=*/false);
+  bool IsExact;
+  Status = Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
+  if (Status != APFloat::opOK || Status != APFloat::opInexact)
+    return nullptr;
+
+  B.CreateAlignedStore(ConstantInt::get(B.getInt32Ty(), QuotInt.getExtValue()), CI->getArgOperand(2), CI->getParamAlign(2));
+  return ConstantFP::get(CI->getType(), Rem);
+}
+
 //===----------------------------------------------------------------------===//
 // Integer Library Call Optimizations
 //===----------------------------------------------------------------------===//
@@ -3926,6 +3955,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
   case LibFunc_cabsf:
   case LibFunc_cabsl:
     return optimizeCAbs(CI, Builder);
+  case LibFunc_remquo:
+  case LibFunc_remquof:
+  case LibFunc_remquol:
+    return optimizeRemquo(CI, Builder);
   default:
     return nullptr;
   }
diff --git a/llvm/test/Transforms/InstCombine/remquo.ll b/llvm/test/Transforms/InstCombine/remquo.ll
new file mode 100644
index 0000000000000..691086ac1c8f7
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/remquo.ll
@@ -0,0 +1,116 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define float @remquo_f32(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret float 1.000000e+00
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float 3.000000e+00, ptr %quo)
+  ret float %call
+}
+
+define float @remquo_f32_quo_sign(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_quo_sign(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret float -1.000000e+00
+;
+entry:
+  %call = call float @remquof(float 5.000000e+00, float 3.000000e+00, ptr %quo)
+  ret float %call
+}
+
+define float @remquo_f32_round(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_round(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -6, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret float 0xBFC9999900000000
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float 0x3FE99999A0000000, ptr %quo)
+  ret float %call
+}
+
+define double @remquo_f64(ptr %quo) {
+; CHECK-LABEL: define double @remquo_f64(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -5, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret double -0.000000e+00
+;
+entry:
+  %call = call double @remquo(double -5.000000e+00, double 1.000000e+00, ptr %quo)
+  ret double %call
+}
+
+define double @remquo_f80(ptr %quo) {
+; CHECK-LABEL: define double @remquo_f80(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call double @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr [[QUO]])
+; CHECK-NEXT:    ret double [[CALL]]
+;
+entry:
+  %call = call double @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr %quo)
+  ret double %call
+}
+
+; Negative tests
+
+define float @remquo_f32_inf_x(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_inf_x(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float 0x7FF0000000000000, float 1.000000e+00, ptr [[QUO]])
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float 0x7FF0000000000000, float 1.000000e+00, ptr %quo)
+  ret float %call
+}
+
+define float @remquo_f32_zero_y(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_zero_y(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float -5.000000e+00, float 0.000000e+00, ptr [[QUO]])
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float 0.000000e+00, ptr %quo)
+  ret float %call
+}
+
+define float @remquo_f32_nan_x(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_nan_x(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float 0x7FF8000000000000, float 1.000000e+00, ptr [[QUO]])
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float 0x7FF8000000000000, float 1.000000e+00, ptr %quo)
+  ret float %call
+}
+
+define float @remquo_f32_nan_y(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_nan_y(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float 1.000000e+00, float 0x7FF8000000000000, ptr [[QUO]])
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float 1.000000e+00, float 0x7FF8000000000000, ptr %quo)
+  ret float %call
+}
+
+declare float @remquof(float, float, ptr)
+declare double @remquo(double, double, ptr)
+declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr)

>From 5034ef4bcc74c079edde2d8230a7ad600fb62cb1 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 19 Jul 2024 21:31:42 +0800
Subject: [PATCH 2/7] [SimplifyLibCalls] Fix typo

---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index f62c53c208f9f..a92efe34eeee7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3021,7 +3021,8 @@ void LibCallSimplifier::classifyArgUse(
 /// Constant folds remquo
 Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
   const APFloat *X, *Y;
-  if (!match(CI->getArgOperand(0), m_APFloat(X)) || !match(CI->getArgOperand(1), m_APFloat(Y)))
+  if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
+      !match(CI->getArgOperand(1), m_APFloat(Y)))
     return nullptr;
 
   if (X->isNaN() || Y->isNaN() || X->isInfinity() || Y->isZero())
@@ -3039,11 +3040,13 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
   // TODO: We can only keep at least the three of the last bits of x/y
   APSInt QuotInt(32, /*isUnsigned=*/false);
   bool IsExact;
-  Status = Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
-  if (Status != APFloat::opOK || Status != APFloat::opInexact)
+  Status =
+      Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
+  if (Status != APFloat::opOK && Status != APFloat::opInexact)
     return nullptr;
 
-  B.CreateAlignedStore(ConstantInt::get(B.getInt32Ty(), QuotInt.getExtValue()), CI->getArgOperand(2), CI->getParamAlign(2));
+  B.CreateAlignedStore(ConstantInt::get(B.getInt32Ty(), QuotInt.getExtValue()),
+                       CI->getArgOperand(2), CI->getParamAlign(2));
   return ConstantFP::get(CI->getType(), Rem);
 }
 

>From 96a0ef3e86c8b39e1ab2851853b7decbfba745b8 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 19 Jul 2024 22:36:28 +0800
Subject: [PATCH 3/7] [SimplifyLibCalls] Address review comments.

---
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp |  5 +--
 llvm/test/Transforms/InstCombine/remquo.ll    | 36 +++++++++++++++----
 2 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index a92efe34eeee7..18224db4e4ad7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3038,14 +3038,15 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
     return nullptr;
 
   // TODO: We can only keep at least the three of the last bits of x/y
-  APSInt QuotInt(32, /*isUnsigned=*/false);
+  unsigned IntBW = TLI->getIntSize();
+  APSInt QuotInt(IntBW, /*isUnsigned=*/false);
   bool IsExact;
   Status =
       Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
   if (Status != APFloat::opOK && Status != APFloat::opInexact)
     return nullptr;
 
-  B.CreateAlignedStore(ConstantInt::get(B.getInt32Ty(), QuotInt.getExtValue()),
+  B.CreateAlignedStore(ConstantInt::get(B.getIntNTy(IntBW), QuotInt.getExtValue()),
                        CI->getArgOperand(2), CI->getParamAlign(2));
   return ConstantFP::get(CI->getType(), Rem);
 }
diff --git a/llvm/test/Transforms/InstCombine/remquo.ll b/llvm/test/Transforms/InstCombine/remquo.ll
index 691086ac1c8f7..ec25bab7cfad7 100644
--- a/llvm/test/Transforms/InstCombine/remquo.ll
+++ b/llvm/test/Transforms/InstCombine/remquo.ll
@@ -49,16 +49,40 @@ entry:
   ret double %call
 }
 
-define double @remquo_f80(ptr %quo) {
-; CHECK-LABEL: define double @remquo_f80(
+define x86_fp80 @remquo_f80(ptr %quo) {
+; CHECK-LABEL: define x86_fp80 @remquo_f80(
 ; CHECK-SAME: ptr [[QUO:%.*]]) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[CALL:%.*]] = call double @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr [[QUO]])
-; CHECK-NEXT:    ret double [[CALL]]
+; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret x86_fp80 0xK3FFF8000000000000000
 ;
 entry:
-  %call = call double @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr %quo)
-  ret double %call
+  %call = call x86_fp80 @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr %quo)
+  ret x86_fp80 %call
+}
+
+define fp128 @remquo_fp128(ptr %quo) {
+; CHECK-LABEL: define fp128 @remquo_fp128(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call fp128 @remquol(fp128 0xL0000000000000000C001400000000000, fp128 0xL00000000000000004000800000000000, ptr [[QUO]])
+; CHECK-NEXT:    ret fp128 [[CALL]]
+;
+entry:
+  %call = call fp128 @remquol(fp128 0xL0000000000000000C001400000000000, fp128 0xL00000000000000004000800000000000, ptr %quo)
+  ret fp128 %call
+}
+
+define ppc_fp128 @remquo_ppc_fp128(ptr %quo) {
+; CHECK-LABEL: define ppc_fp128 @remquo_ppc_fp128(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call ppc_fp128 @remquol(ppc_fp128 0xMC0140000000000000000000000000000, ppc_fp128 0xM40080000000000000000000000000000, ptr [[QUO]])
+; CHECK-NEXT:    ret ppc_fp128 [[CALL]]
+;
+entry:
+  %call = call ppc_fp128 @remquol(ppc_fp128 0xMC0140000000000000000000000000000, ppc_fp128 0xM40080000000000000000000000000000, ptr %quo)
+  ret ppc_fp128 %call
 }
 
 ; Negative tests

>From 7050286a4881ad2fe5063dba75c1ec9858dc1c8c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 19 Jul 2024 23:03:35 +0800
Subject: [PATCH 4/7] [SimplifyLibCalls] Put remquol variants into separate
 files. NFC.

---
 llvm/test/Transforms/InstCombine/remquo.ll    | 37 -------------------
 .../Transforms/InstCombine/remquol-fp128.ll   | 16 ++++++++
 .../Transforms/InstCombine/remquol-fp80.ll    | 16 ++++++++
 .../InstCombine/remquol-ppc-fp128.ll          | 16 ++++++++
 4 files changed, 48 insertions(+), 37 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/remquol-fp128.ll
 create mode 100644 llvm/test/Transforms/InstCombine/remquol-fp80.ll
 create mode 100644 llvm/test/Transforms/InstCombine/remquol-ppc-fp128.ll

diff --git a/llvm/test/Transforms/InstCombine/remquo.ll b/llvm/test/Transforms/InstCombine/remquo.ll
index ec25bab7cfad7..31de8b27dfcb2 100644
--- a/llvm/test/Transforms/InstCombine/remquo.ll
+++ b/llvm/test/Transforms/InstCombine/remquo.ll
@@ -49,42 +49,6 @@ entry:
   ret double %call
 }
 
-define x86_fp80 @remquo_f80(ptr %quo) {
-; CHECK-LABEL: define x86_fp80 @remquo_f80(
-; CHECK-SAME: ptr [[QUO:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
-; CHECK-NEXT:    ret x86_fp80 0xK3FFF8000000000000000
-;
-entry:
-  %call = call x86_fp80 @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr %quo)
-  ret x86_fp80 %call
-}
-
-define fp128 @remquo_fp128(ptr %quo) {
-; CHECK-LABEL: define fp128 @remquo_fp128(
-; CHECK-SAME: ptr [[QUO:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[CALL:%.*]] = call fp128 @remquol(fp128 0xL0000000000000000C001400000000000, fp128 0xL00000000000000004000800000000000, ptr [[QUO]])
-; CHECK-NEXT:    ret fp128 [[CALL]]
-;
-entry:
-  %call = call fp128 @remquol(fp128 0xL0000000000000000C001400000000000, fp128 0xL00000000000000004000800000000000, ptr %quo)
-  ret fp128 %call
-}
-
-define ppc_fp128 @remquo_ppc_fp128(ptr %quo) {
-; CHECK-LABEL: define ppc_fp128 @remquo_ppc_fp128(
-; CHECK-SAME: ptr [[QUO:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[CALL:%.*]] = call ppc_fp128 @remquol(ppc_fp128 0xMC0140000000000000000000000000000, ppc_fp128 0xM40080000000000000000000000000000, ptr [[QUO]])
-; CHECK-NEXT:    ret ppc_fp128 [[CALL]]
-;
-entry:
-  %call = call ppc_fp128 @remquol(ppc_fp128 0xMC0140000000000000000000000000000, ppc_fp128 0xM40080000000000000000000000000000, ptr %quo)
-  ret ppc_fp128 %call
-}
-
 ; Negative tests
 
 define float @remquo_f32_inf_x(ptr %quo) {
@@ -137,4 +101,3 @@ entry:
 
 declare float @remquof(float, float, ptr)
 declare double @remquo(double, double, ptr)
-declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr)
diff --git a/llvm/test/Transforms/InstCombine/remquol-fp128.ll b/llvm/test/Transforms/InstCombine/remquol-fp128.ll
new file mode 100644
index 0000000000000..38e0a6040a140
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/remquol-fp128.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define fp128 @remquo_fp128(ptr %quo) {
+; CHECK-LABEL: define fp128 @remquo_fp128(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret fp128 0xL00000000000000003FFF000000000000
+;
+entry:
+  %call = call fp128 @remquol(fp128 0xL0000000000000000C001400000000000, fp128 0xL00000000000000004000800000000000, ptr %quo)
+  ret fp128 %call
+}
+
+declare fp128 @remquol(fp128, fp128, ptr)
diff --git a/llvm/test/Transforms/InstCombine/remquol-fp80.ll b/llvm/test/Transforms/InstCombine/remquol-fp80.ll
new file mode 100644
index 0000000000000..fe65ee1acc902
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/remquol-fp80.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define x86_fp80 @remquo_fp80(ptr %quo) {
+; CHECK-LABEL: define x86_fp80 @remquo_fp80(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret x86_fp80 0xK3FFF8000000000000000
+;
+entry:
+  %call = call x86_fp80 @remquol(x86_fp80 0xKC001A000000000000000, x86_fp80 0xK4000C000000000000000, ptr %quo)
+  ret x86_fp80 %call
+}
+
+declare x86_fp80 @remquol(x86_fp80, x86_fp80, ptr)
diff --git a/llvm/test/Transforms/InstCombine/remquol-ppc-fp128.ll b/llvm/test/Transforms/InstCombine/remquol-ppc-fp128.ll
new file mode 100644
index 0000000000000..86dfd01f859ac
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/remquol-ppc-fp128.ll
@@ -0,0 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define ppc_fp128 @remquo_ppc_fp128(ptr %quo) {
+; CHECK-LABEL: define ppc_fp128 @remquo_ppc_fp128(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    store i32 -2, ptr [[QUO]], align 4
+; CHECK-NEXT:    ret ppc_fp128 0xM3FF00000000000000000000000000000
+;
+entry:
+  %call = call ppc_fp128 @remquol(ppc_fp128 0xMC0140000000000000000000000000000, ppc_fp128 0xM40080000000000000000000000000000, ptr %quo)
+  ret ppc_fp128 %call
+}
+
+declare ppc_fp128 @remquol(ppc_fp128, ppc_fp128, ptr)

>From f6adf3b9d39f12d74026c57a40e464955979551c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 22 Jul 2024 18:24:56 +0800
Subject: [PATCH 5/7] [SimplifyLibCalls] Add more tests. NFC.

---
 llvm/test/Transforms/InstCombine/remquo.ll | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/remquo.ll b/llvm/test/Transforms/InstCombine/remquo.ll
index 31de8b27dfcb2..b2499ea13179e 100644
--- a/llvm/test/Transforms/InstCombine/remquo.ll
+++ b/llvm/test/Transforms/InstCombine/remquo.ll
@@ -75,6 +75,18 @@ entry:
   ret float %call
 }
 
+define float @remquo_f32_nzero_y(ptr %quo) {
+; CHECK-LABEL: define float @remquo_f32_nzero_y(
+; CHECK-SAME: ptr [[QUO:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float -5.000000e+00, float -0.000000e+00, ptr [[QUO]])
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float -0.000000e+00, ptr %quo)
+  ret float %call
+}
+
 define float @remquo_f32_nan_x(ptr %quo) {
 ; CHECK-LABEL: define float @remquo_f32_nan_x(
 ; CHECK-SAME: ptr [[QUO:%.*]]) {
@@ -99,5 +111,29 @@ entry:
   ret float %call
 }
 
+define float @remquo_f32_strictfp(ptr %quo) strictfp {
+; CHECK-LABEL: define float @remquo_f32_strictfp(
+; CHECK-SAME: ptr [[QUO:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float -5.000000e+00, float 3.000000e+00, ptr [[QUO]]) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float 3.000000e+00, ptr %quo) strictfp
+  ret float %call
+}
+
+define float @remquo_f32_zero_y_strictfp(ptr %quo) strictfp {
+; CHECK-LABEL: define float @remquo_f32_zero_y_strictfp(
+; CHECK-SAME: ptr [[QUO:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[CALL:%.*]] = call float @remquof(float -5.000000e+00, float 0.000000e+00, ptr [[QUO]]) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[CALL]]
+;
+entry:
+  %call = call float @remquof(float -5.000000e+00, float 0.000000e+00, ptr %quo) strictfp
+  ret float %call
+}
+
 declare float @remquof(float, float, ptr)
 declare double @remquo(double, double, ptr)

>From 45847750fc36538ddaf9297d4702005eca0c51bf Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 22 Jul 2024 18:38:01 +0800
Subject: [PATCH 6/7] [SimplifyLibCalls] Format code. NFC.

---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 18224db4e4ad7..1ae263239a1ed 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3046,8 +3046,9 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
   if (Status != APFloat::opOK && Status != APFloat::opInexact)
     return nullptr;
 
-  B.CreateAlignedStore(ConstantInt::get(B.getIntNTy(IntBW), QuotInt.getExtValue()),
-                       CI->getArgOperand(2), CI->getParamAlign(2));
+  B.CreateAlignedStore(
+      ConstantInt::get(B.getIntNTy(IntBW), QuotInt.getExtValue()),
+      CI->getArgOperand(2), CI->getParamAlign(2));
   return ConstantFP::get(CI->getType(), Rem);
 }
 

>From d171826144a710822a06bc5b1dafa284e03d6f7b Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 22 Jul 2024 20:34:32 +0800
Subject: [PATCH 7/7] [SimplifyLibCalls] Address review comments. NFC.

---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 1ae263239a1ed..92c44265ce8b6 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3025,9 +3025,6 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
       !match(CI->getArgOperand(1), m_APFloat(Y)))
     return nullptr;
 
-  if (X->isNaN() || Y->isNaN() || X->isInfinity() || Y->isZero())
-    return nullptr;
-
   APFloat::opStatus Status;
   APFloat Quot = *X;
   Status = Quot.divide(*Y, APFloat::rmNearestTiesToEven);



More information about the llvm-commits mailing list