Skip to content

Commit 8f80f15

Browse files
committed
ConstantFold logl calls
This is a follow up patch from llvm#90611 which folds logl calls in the same manner as log.f128 calls. Logl suffers from the same problem as logf128 of having slow calls to fp128 log functions which can be constant folded. However, Logl is emitted at the O3 level instead whereas log.f128 is emitted by Ofast by various intrinsics.
1 parent b01ac51 commit 8f80f15

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,9 +1669,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
16691669
Name == "floor" || Name == "floorf" ||
16701670
Name == "fmod" || Name == "fmodf";
16711671
case 'l':
1672-
return Name == "log" || Name == "logf" ||
1673-
Name == "log2" || Name == "log2f" ||
1674-
Name == "log10" || Name == "log10f";
1672+
return Name == "log" || Name == "logf" || Name == "log2" ||
1673+
Name == "log2f" || Name == "log10" || Name == "log10f" ||
1674+
Name == "logl";
16751675
case 'n':
16761676
return Name == "nearbyint" || Name == "nearbyintf";
16771677
case 'p':
@@ -2085,15 +2085,19 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
20852085
if (IntrinsicID == Intrinsic::canonicalize)
20862086
return constantFoldCanonicalize(Ty, Call, U);
20872087

2088+
// Try to handle special fp128 cases before bailing
20882089
#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
20892090
if (Ty->isFP128Ty()) {
2090-
switch (IntrinsicID) {
2091-
default:
2092-
return nullptr;
2093-
case Intrinsic::log:
2094-
return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
2095-
}
2091+
const APFloat &Fp128APF = Op->getValueAPF();
2092+
if (IntrinsicID == Intrinsic::log)
2093+
return ConstantFP::get(Ty, logf128(Fp128APF.convertToQuad()));
20962094
}
2095+
2096+
LibFunc Fp128Func = NotLibFunc;
2097+
if (Ty->isFP128Ty() && TLI->getLibFunc(Name, Fp128Func) &&
2098+
TLI->has(Fp128Func) && Fp128Func == LibFunc_logl &&
2099+
!Op->getValueAPF().isNegative() && !Op->getValueAPF().isZero())
2100+
return ConstantFP::get(Ty, logf128(Op->getValueAPF().convertToQuad()));
20972101
#endif
20982102

20992103
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
@@ -2356,6 +2360,8 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
23562360
// TODO: What about hosts that lack a C99 library?
23572361
return ConstantFoldFP(log10, APF, Ty);
23582362
break;
2363+
case LibFunc_logl:
2364+
return nullptr;
23592365
case LibFunc_nearbyint:
23602366
case LibFunc_nearbyintf:
23612367
case LibFunc_rint:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
; REQUIRES: has_logf128
55
declare fp128 @llvm.log.f128(fp128)
6+
declare fp128 @logl(fp128)
67

78
define fp128 @log_e_64(){
89
; CHECK-LABEL: define fp128 @log_e_64() {
@@ -124,3 +125,12 @@ define <2 x fp128> @log_e_negative_2_vector(){
124125
%A = call <2 x fp128> @llvm.log.v2f128(<2 x fp128> <fp128 0xL0000000000000000C000000000000000, fp128 0xL0000000000000000C000000000000001>)
125126
ret <2 x fp128> %A
126127
}
128+
129+
define fp128 @logl_e_64(){
130+
; CHECK-LABEL: define fp128 @logl_e_64() {
131+
; CHECK-NEXT: %A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
132+
; CHECK-NEXT: ret fp128 0xL300000000000000040010A2B23F3BAB7
133+
;
134+
%A = call fp128 @logl(fp128 noundef 0xL00000000000000004005000000000000)
135+
ret fp128 %A
136+
}

0 commit comments

Comments
 (0)