Skip to content

Commit ed5f452

Browse files
author
Evandro Menezes
committed
[ConstantFolding] Refactor math functions to use LLVM ones (NFC)
When possible, replace calls to library routines on the host with equivalent ones in LLVM. Differential revision: https://reviews.llvm.org/D67459 llvm-svn: 371677
1 parent bc40836 commit ed5f452

File tree

1 file changed

+42
-37
lines changed

1 file changed

+42
-37
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,40 +1718,37 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
17181718
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
17191719
return nullptr;
17201720

1721-
if (IntrinsicID == Intrinsic::round) {
1722-
APFloat V = Op->getValueAPF();
1723-
V.roundToIntegral(APFloat::rmNearestTiesToAway);
1724-
return ConstantFP::get(Ty->getContext(), V);
1721+
// Use internal versions of these intrinsics.
1722+
APFloat U = Op->getValueAPF();
1723+
1724+
if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
1725+
U.roundToIntegral(APFloat::rmNearestTiesToEven);
1726+
return ConstantFP::get(Ty->getContext(), U);
17251727
}
17261728

1727-
if (IntrinsicID == Intrinsic::floor) {
1728-
APFloat V = Op->getValueAPF();
1729-
V.roundToIntegral(APFloat::rmTowardNegative);
1730-
return ConstantFP::get(Ty->getContext(), V);
1729+
if (IntrinsicID == Intrinsic::round) {
1730+
U.roundToIntegral(APFloat::rmNearestTiesToAway);
1731+
return ConstantFP::get(Ty->getContext(), U);
17311732
}
17321733

17331734
if (IntrinsicID == Intrinsic::ceil) {
1734-
APFloat V = Op->getValueAPF();
1735-
V.roundToIntegral(APFloat::rmTowardPositive);
1736-
return ConstantFP::get(Ty->getContext(), V);
1735+
U.roundToIntegral(APFloat::rmTowardPositive);
1736+
return ConstantFP::get(Ty->getContext(), U);
17371737
}
17381738

1739-
if (IntrinsicID == Intrinsic::trunc) {
1740-
APFloat V = Op->getValueAPF();
1741-
V.roundToIntegral(APFloat::rmTowardZero);
1742-
return ConstantFP::get(Ty->getContext(), V);
1739+
if (IntrinsicID == Intrinsic::floor) {
1740+
U.roundToIntegral(APFloat::rmTowardNegative);
1741+
return ConstantFP::get(Ty->getContext(), U);
17431742
}
17441743

1745-
if (IntrinsicID == Intrinsic::rint) {
1746-
APFloat V = Op->getValueAPF();
1747-
V.roundToIntegral(APFloat::rmNearestTiesToEven);
1748-
return ConstantFP::get(Ty->getContext(), V);
1744+
if (IntrinsicID == Intrinsic::trunc) {
1745+
U.roundToIntegral(APFloat::rmTowardZero);
1746+
return ConstantFP::get(Ty->getContext(), U);
17491747
}
17501748

1751-
if (IntrinsicID == Intrinsic::nearbyint) {
1752-
APFloat V = Op->getValueAPF();
1753-
V.roundToIntegral(APFloat::rmNearestTiesToEven);
1754-
return ConstantFP::get(Ty->getContext(), V);
1749+
if (IntrinsicID == Intrinsic::fabs) {
1750+
U.clearSign();
1751+
return ConstantFP::get(Ty->getContext(), U);
17551752
}
17561753

17571754
/// We only fold functions with finite arguments. Folding NaN and inf is
@@ -1768,8 +1765,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
17681765

17691766
switch (IntrinsicID) {
17701767
default: break;
1771-
case Intrinsic::fabs:
1772-
return ConstantFoldFP(fabs, V, Ty);
17731768
case Intrinsic::log:
17741769
return ConstantFoldFP(log, V, Ty);
17751770
case Intrinsic::log2:
@@ -1796,7 +1791,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
17961791

17971792
LibFunc Func = NotLibFunc;
17981793
TLI->getLibFunc(Name, Func);
1799-
18001794
switch (Func) {
18011795
default:
18021796
break;
@@ -1821,8 +1815,10 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
18211815
break;
18221816
case LibFunc_ceil:
18231817
case LibFunc_ceilf:
1824-
if (TLI->has(Func))
1825-
return ConstantFoldFP(ceil, V, Ty);
1818+
if (TLI->has(Func)) {
1819+
U.roundToIntegral(APFloat::rmTowardPositive);
1820+
return ConstantFP::get(Ty->getContext(), U);
1821+
}
18261822
break;
18271823
case LibFunc_cos:
18281824
case LibFunc_cosf:
@@ -1853,16 +1849,21 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
18531849
break;
18541850
case LibFunc_fabs:
18551851
case LibFunc_fabsf:
1856-
if (TLI->has(Func))
1857-
return ConstantFoldFP(fabs, V, Ty);
1852+
if (TLI->has(Func)) {
1853+
U.clearSign();
1854+
return ConstantFP::get(Ty->getContext(), U);
1855+
}
18581856
break;
18591857
case LibFunc_floor:
18601858
case LibFunc_floorf:
1861-
if (TLI->has(Func))
1862-
return ConstantFoldFP(floor, V, Ty);
1859+
if (TLI->has(Func)) {
1860+
U.roundToIntegral(APFloat::rmTowardNegative);
1861+
return ConstantFP::get(Ty->getContext(), U);
1862+
}
18631863
break;
18641864
case LibFunc_log:
18651865
case LibFunc_logf:
1866+
18661867
case LibFunc_log_finite:
18671868
case LibFunc_logf_finite:
18681869
if (V > 0.0 && TLI->has(Func))
@@ -1878,8 +1879,10 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
18781879
break;
18791880
case LibFunc_round:
18801881
case LibFunc_roundf:
1881-
if (TLI->has(Func))
1882-
return ConstantFoldFP(round, V, Ty);
1882+
if (TLI->has(Func)) {
1883+
U.roundToIntegral(APFloat::rmNearestTiesToAway);
1884+
return ConstantFP::get(Ty->getContext(), U);
1885+
}
18831886
break;
18841887
case LibFunc_sin:
18851888
case LibFunc_sinf:
@@ -2040,9 +2043,11 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
20402043
break;
20412044
case LibFunc_fmod:
20422045
case LibFunc_fmodf:
2043-
if (TLI->has(Func))
2044-
// TODO: What about hosts that lack a C99 library?
2045-
return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
2046+
if (TLI->has(Func)) {
2047+
APFloat V = Op1->getValueAPF();
2048+
if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
2049+
return ConstantFP::get(Ty->getContext(), V);
2050+
}
20462051
break;
20472052
case LibFunc_atan2:
20482053
case LibFunc_atan2f:

0 commit comments

Comments
 (0)