[llvm] [SimplfiyLibCalls] Constant fold `remquo` (PR #99647)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 19 07:37:01 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/3] [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/3] [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/3] [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
More information about the llvm-commits
mailing list