Skip to content

Commit 352b901

Browse files
authored
[ConstantFolding] Handle roundeven libcalls (#170692)
Basically identical to nearbyint and rint, which we already treat as rounding to nearest with ties to even during constant folding.
1 parent 63e9d60 commit 352b901

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,7 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
19841984
switch (Name[0]) {
19851985
default:
19861986
return false;
1987+
// clang-format off
19871988
case 'a':
19881989
return Name == "acos" || Name == "acosf" ||
19891990
Name == "asin" || Name == "asinf" ||
@@ -2014,7 +2015,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
20142015
case 'r':
20152016
return Name == "remainder" || Name == "remainderf" ||
20162017
Name == "rint" || Name == "rintf" ||
2017-
Name == "round" || Name == "roundf";
2018+
Name == "round" || Name == "roundf" ||
2019+
Name == "roundeven" || Name == "roundevenf";
20182020
case 's':
20192021
return Name == "sin" || Name == "sinf" ||
20202022
Name == "sinh" || Name == "sinhf" ||
@@ -2052,6 +2054,7 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
20522054
case 's':
20532055
return Name == "__sinh_finite" || Name == "__sinhf_finite";
20542056
}
2057+
// clang-format on
20552058
}
20562059
}
20572060

@@ -2516,7 +2519,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
25162519

25172520
// Use internal versions of these intrinsics.
25182521

2519-
if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
2522+
if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint ||
2523+
IntrinsicID == Intrinsic::roundeven) {
25202524
U.roundToIntegral(APFloat::rmNearestTiesToEven);
25212525
return ConstantFP::get(Ty->getContext(), U);
25222526
}
@@ -2988,6 +2992,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
29882992
case LibFunc_nearbyintf:
29892993
case LibFunc_rint:
29902994
case LibFunc_rintf:
2995+
case LibFunc_roundeven:
2996+
case LibFunc_roundevenf:
29912997
if (TLI->has(Func)) {
29922998
U.roundToIntegral(APFloat::rmNearestTiesToEven);
29932999
return ConstantFP::get(Ty->getContext(), U);

llvm/test/Transforms/InstSimplify/ConstProp/roundeven.ll

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ declare double @llvm.roundeven.f64(double)
99
define float @constant_fold_roundeven_f32_01() #0 {
1010
; CHECK-LABEL: define float @constant_fold_roundeven_f32_01(
1111
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
12-
; CHECK-NEXT: [[X:%.*]] = call float @roundevenf(float 1.250000e+00) #[[ATTR0]]
13-
; CHECK-NEXT: ret float [[X]]
12+
; CHECK-NEXT: ret float 1.000000e+00
1413
;
1514
%x = call float @roundevenf(float 1.25) #0
1615
ret float %x
@@ -29,8 +28,7 @@ define float @constant_fold_roundeven_f32_02() #0 {
2928
define float @constant_fold_roundeven_f32_03() #0 {
3029
; CHECK-LABEL: define float @constant_fold_roundeven_f32_03(
3130
; CHECK-SAME: ) #[[ATTR0]] {
32-
; CHECK-NEXT: [[X:%.*]] = call float @roundevenf(float 1.500000e+00) #[[ATTR0]]
33-
; CHECK-NEXT: ret float [[X]]
31+
; CHECK-NEXT: ret float 2.000000e+00
3432
;
3533
%x = call float @roundevenf(float 1.5) #0
3634
ret float %x
@@ -50,8 +48,7 @@ define float @constant_fold_roundeven_f32_04() #0 {
5048
define float @constant_fold_roundeven_f32_05() #0 {
5149
; CHECK-LABEL: define float @constant_fold_roundeven_f32_05(
5250
; CHECK-SAME: ) #[[ATTR0]] {
53-
; CHECK-NEXT: [[X:%.*]] = call float @roundevenf(float 2.500000e+00) #[[ATTR0]]
54-
; CHECK-NEXT: ret float [[X]]
51+
; CHECK-NEXT: ret float 2.000000e+00
5552
;
5653
%x = call float @roundevenf(float 2.5) #0
5754
ret float %x
@@ -70,8 +67,7 @@ define float @constant_fold_roundeven_f32_06() #0 {
7067
define float @constant_fold_roundeven_f32_07() #0 {
7168
; CHECK-LABEL: define float @constant_fold_roundeven_f32_07(
7269
; CHECK-SAME: ) #[[ATTR0]] {
73-
; CHECK-NEXT: [[X:%.*]] = call float @roundevenf(float 2.750000e+00) #[[ATTR0]]
74-
; CHECK-NEXT: ret float [[X]]
70+
; CHECK-NEXT: ret float 3.000000e+00
7571
;
7672
%x = call float @roundevenf(float 2.75) #0
7773
ret float %x
@@ -89,8 +85,7 @@ define float @constant_fold_roundeven_f32_08() #0 {
8985
define double @constant_fold_roundeven_f64_01() #0 {
9086
; CHECK-LABEL: define double @constant_fold_roundeven_f64_01(
9187
; CHECK-SAME: ) #[[ATTR0]] {
92-
; CHECK-NEXT: [[X:%.*]] = call double @roundeven(double 1.300000e+00) #[[ATTR0]]
93-
; CHECK-NEXT: ret double [[X]]
88+
; CHECK-NEXT: ret double 1.000000e+00
9489
;
9590
%x = call double @roundeven(double 1.3) #0
9691
ret double %x
@@ -108,8 +103,7 @@ define double @constant_fold_roundeven_f64_02() #0 {
108103
define double @constant_fold_roundeven_f64_03() #0 {
109104
; CHECK-LABEL: define double @constant_fold_roundeven_f64_03(
110105
; CHECK-SAME: ) #[[ATTR0]] {
111-
; CHECK-NEXT: [[X:%.*]] = call double @roundeven(double 1.500000e+00) #[[ATTR0]]
112-
; CHECK-NEXT: ret double [[X]]
106+
; CHECK-NEXT: ret double 2.000000e+00
113107
;
114108
%x = call double @roundeven(double 1.5) #0
115109
ret double %x
@@ -127,8 +121,7 @@ define double @constant_fold_roundeven_f64_04() #0 {
127121
define double @constant_fold_roundeven_f64_05() #0 {
128122
; CHECK-LABEL: define double @constant_fold_roundeven_f64_05(
129123
; CHECK-SAME: ) #[[ATTR0]] {
130-
; CHECK-NEXT: [[X:%.*]] = call double @roundeven(double 2.500000e+00) #[[ATTR0]]
131-
; CHECK-NEXT: ret double [[X]]
124+
; CHECK-NEXT: ret double 2.000000e+00
132125
;
133126
%x = call double @roundeven(double 2.5) #0
134127
ret double %x
@@ -146,8 +139,7 @@ define double @constant_fold_roundeven_f64_06() #0 {
146139
define double @constant_fold_roundeven_f64_07() #0 {
147140
; CHECK-LABEL: define double @constant_fold_roundeven_f64_07(
148141
; CHECK-SAME: ) #[[ATTR0]] {
149-
; CHECK-NEXT: [[X:%.*]] = call double @roundeven(double 2.700000e+00) #[[ATTR0]]
150-
; CHECK-NEXT: ret double [[X]]
142+
; CHECK-NEXT: ret double 3.000000e+00
151143
;
152144
%x = call double @roundeven(double 2.7) #0
153145
ret double %x

0 commit comments

Comments
 (0)