Skip to content

Commit

Permalink
[ConstantFolding] Refactor math functions to use LLVM ones (NFC)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Evandro Menezes committed Sep 11, 2019
1 parent bc40836 commit ed5f452
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,40 +1718,37 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
return nullptr;

if (IntrinsicID == Intrinsic::round) {
APFloat V = Op->getValueAPF();
V.roundToIntegral(APFloat::rmNearestTiesToAway);
return ConstantFP::get(Ty->getContext(), V);
// Use internal versions of these intrinsics.
APFloat U = Op->getValueAPF();

if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
U.roundToIntegral(APFloat::rmNearestTiesToEven);
return ConstantFP::get(Ty->getContext(), U);
}

if (IntrinsicID == Intrinsic::floor) {
APFloat V = Op->getValueAPF();
V.roundToIntegral(APFloat::rmTowardNegative);
return ConstantFP::get(Ty->getContext(), V);
if (IntrinsicID == Intrinsic::round) {
U.roundToIntegral(APFloat::rmNearestTiesToAway);
return ConstantFP::get(Ty->getContext(), U);
}

if (IntrinsicID == Intrinsic::ceil) {
APFloat V = Op->getValueAPF();
V.roundToIntegral(APFloat::rmTowardPositive);
return ConstantFP::get(Ty->getContext(), V);
U.roundToIntegral(APFloat::rmTowardPositive);
return ConstantFP::get(Ty->getContext(), U);
}

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

if (IntrinsicID == Intrinsic::rint) {
APFloat V = Op->getValueAPF();
V.roundToIntegral(APFloat::rmNearestTiesToEven);
return ConstantFP::get(Ty->getContext(), V);
if (IntrinsicID == Intrinsic::trunc) {
U.roundToIntegral(APFloat::rmTowardZero);
return ConstantFP::get(Ty->getContext(), U);
}

if (IntrinsicID == Intrinsic::nearbyint) {
APFloat V = Op->getValueAPF();
V.roundToIntegral(APFloat::rmNearestTiesToEven);
return ConstantFP::get(Ty->getContext(), V);
if (IntrinsicID == Intrinsic::fabs) {
U.clearSign();
return ConstantFP::get(Ty->getContext(), U);
}

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

switch (IntrinsicID) {
default: break;
case Intrinsic::fabs:
return ConstantFoldFP(fabs, V, Ty);
case Intrinsic::log:
return ConstantFoldFP(log, V, Ty);
case Intrinsic::log2:
Expand All @@ -1796,7 +1791,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,

LibFunc Func = NotLibFunc;
TLI->getLibFunc(Name, Func);

switch (Func) {
default:
break;
Expand All @@ -1821,8 +1815,10 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
break;
case LibFunc_ceil:
case LibFunc_ceilf:
if (TLI->has(Func))
return ConstantFoldFP(ceil, V, Ty);
if (TLI->has(Func)) {
U.roundToIntegral(APFloat::rmTowardPositive);
return ConstantFP::get(Ty->getContext(), U);
}
break;
case LibFunc_cos:
case LibFunc_cosf:
Expand Down Expand Up @@ -1853,16 +1849,21 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
break;
case LibFunc_fabs:
case LibFunc_fabsf:
if (TLI->has(Func))
return ConstantFoldFP(fabs, V, Ty);
if (TLI->has(Func)) {
U.clearSign();
return ConstantFP::get(Ty->getContext(), U);
}
break;
case LibFunc_floor:
case LibFunc_floorf:
if (TLI->has(Func))
return ConstantFoldFP(floor, V, Ty);
if (TLI->has(Func)) {
U.roundToIntegral(APFloat::rmTowardNegative);
return ConstantFP::get(Ty->getContext(), U);
}
break;
case LibFunc_log:
case LibFunc_logf:

case LibFunc_log_finite:
case LibFunc_logf_finite:
if (V > 0.0 && TLI->has(Func))
Expand All @@ -1878,8 +1879,10 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
break;
case LibFunc_round:
case LibFunc_roundf:
if (TLI->has(Func))
return ConstantFoldFP(round, V, Ty);
if (TLI->has(Func)) {
U.roundToIntegral(APFloat::rmNearestTiesToAway);
return ConstantFP::get(Ty->getContext(), U);
}
break;
case LibFunc_sin:
case LibFunc_sinf:
Expand Down Expand Up @@ -2040,9 +2043,11 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
break;
case LibFunc_fmod:
case LibFunc_fmodf:
if (TLI->has(Func))
// TODO: What about hosts that lack a C99 library?
return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
if (TLI->has(Func)) {
APFloat V = Op1->getValueAPF();
if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
return ConstantFP::get(Ty->getContext(), V);
}
break;
case LibFunc_atan2:
case LibFunc_atan2f:
Expand Down

0 comments on commit ed5f452

Please sign in to comment.