Skip to content

ConstantFold logl calls #94944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,9 +1669,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
Name == "floor" || Name == "floorf" ||
Name == "fmod" || Name == "fmodf";
case 'l':
return Name == "log" || Name == "logf" ||
Name == "log2" || Name == "log2f" ||
Name == "log10" || Name == "log10f";
return Name == "log" || Name == "logf" || Name == "log2" ||
Name == "log2f" || Name == "log10" || Name == "log10f" ||
Name == "logl";
case 'n':
return Name == "nearbyint" || Name == "nearbyintf";
case 'p':
Expand Down Expand Up @@ -1734,6 +1734,14 @@ Constant *GetConstantFoldFPValue(double V, Type *Ty) {
llvm_unreachable("Can only constant fold half/float/double");
}

#if defined(HAS_IEE754_FLOAT128)
Constant *GetConstantFoldFPValue128(__float128 V, Type *Ty) {
if (Ty->isFP128Ty())
return ConstantFP::get(Ty, V);
llvm_unreachable("Can only constant fold fp128");
}
#endif

/// Clear the floating-point exception state.
inline void llvm_fenv_clearexcept() {
#if defined(HAVE_FENV_H) && HAVE_DECL_FE_ALL_EXCEPT
Expand Down Expand Up @@ -1766,6 +1774,20 @@ Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
return GetConstantFoldFPValue(Result, Ty);
}

#if defined(HAS_IEE754_FLOAT128)
Constant *ConstantFoldFP128(long double (*NativeFP)(long double),
const APFloat &V, Type *Ty) {
llvm_fenv_clearexcept();
__float128 Result = NativeFP(V.convertToQuad());
if (llvm_fenv_testexcept()) {
llvm_fenv_clearexcept();
return nullptr;
}

return GetConstantFoldFPValue128(Result, Ty);
}
#endif

Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
const APFloat &V, const APFloat &W, Type *Ty) {
llvm_fenv_clearexcept();
Expand Down Expand Up @@ -2087,12 +2109,15 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,

#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
if (Ty->isFP128Ty()) {
switch (IntrinsicID) {
default:
return nullptr;
case Intrinsic::log:
return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
if (IntrinsicID == Intrinsic::log) {
__float128 Result = logf128(Op->getValueAPF().convertToQuad());
return GetConstantFoldFPValue128(Result, Ty);
}

LibFunc Fp128Func = NotLibFunc;
if (TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
Fp128Func == LibFunc_logl)
return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
}
#endif

Expand Down Expand Up @@ -2356,6 +2381,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
// TODO: What about hosts that lack a C99 library?
return ConstantFoldFP(log10, APF, Ty);
break;
case LibFunc_logl:
return nullptr;
case LibFunc_nearbyint:
case LibFunc_nearbyintf:
case LibFunc_rint:
Expand Down
47 changes: 47 additions & 0 deletions llvm/test/Transforms/InstSimplify/ConstProp/logf128.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

; REQUIRES: has_logf128
declare fp128 @llvm.log.f128(fp128)
declare fp128 @logl(fp128)

define fp128 @log_e_64(){
; CHECK-LABEL: define fp128 @log_e_64() {
Expand Down Expand Up @@ -124,3 +125,49 @@ define <2 x fp128> @log_e_negative_2_vector(){
%A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
ret <2 x fp128> %A
}

define fp128 @logl_e_64(){
; CHECK-LABEL: define fp128 @logl_e_64() {
; CHECK-NEXT: [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
; CHECK-NEXT: ret fp128 0xL300000000000000040010A2B23F3BAB7
;
%A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
ret fp128 %A
}

define fp128 @logl_e_0(){
; CHECK-LABEL: define fp128 @logl_e_0() {
; CHECK-NEXT: [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
; CHECK-NEXT: ret fp128 [[A]]
;
%A = call fp128 @logl(fp128 noundef 0xL00000000000000000000000000000000)
ret fp128 %A
}

define fp128 @logl_e_infinity(){
; CHECK-LABEL: define fp128 @logl_e_infinity() {
; CHECK-NEXT: [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000000)
; CHECK-NEXT: ret fp128 0xL00000000000000007FFF000000000000
;
%A = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000000)
ret fp128 %A
}

define fp128 @logl_e_nan(){
; CHECK-LABEL: define fp128 @logl_e_nan() {
; CHECK-NEXT: [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
; CHECK-NEXT: ret fp128 [[A]]
;
%A = call fp128 @logl(fp128 noundef 0xL00000000000000007FFF000000000001)
ret fp128 %A
}


define fp128 @logl_e_negative_2(){
; CHECK-LABEL: define fp128 @logl_e_negative_2() {
; CHECK-NEXT: [[A:%.*]] = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
; CHECK-NEXT: ret fp128 [[A]]
;
%A = call fp128 @logl(fp128 noundef 0xL0000000000000000C000000000000000)
ret fp128 %A
}
Loading