Skip to content

Commit

Permalink
[InstCombine] Convert @log to @llvm.log if the input is known positive.
Browse files Browse the repository at this point in the history
Similar to 112aac4, this converts log libcalls
to llvm.log.f64 intrinsics if we know they do not set errno, as the input is
not zero and not negative. As log will produce errno if the input is 0
(returning -inf) or if the input is negative (returning nan), we also perform
the conversion when we have noinf and nonan.
  • Loading branch information
davemgreen committed Oct 7, 2024
1 parent 0e8555d commit 493cea5
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 30 deletions.
49 changes: 32 additions & 17 deletions llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2510,20 +2510,15 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
Intrinsic::ID LogID = LogFn->getIntrinsicID();
Module *Mod = Log->getModule();
Type *Ty = Log->getType();
Value *Ret = nullptr;

if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);

// The earlier call must also be 'fast' in order to do these transforms.
CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
return Ret;
if (Value *Ret = optimizeUnaryDoubleFP(Log, B, TLI, true))
return Ret;

LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;

// This is only applicable to log(), log2(), log10().
if (TLI->getLibFunc(LogNm, LogLb))
if (TLI->getLibFunc(LogNm, LogLb)) {
switch (LogLb) {
case LibFunc_logf:
LogID = Intrinsic::log;
Expand Down Expand Up @@ -2589,10 +2584,26 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
PowLb = LibFunc_powl;
break;
default:
return Ret;
return nullptr;
}

// Convert libcall to intrinsic if the value is known > 0.
bool IsKnownNoErrno = Log->hasNoNaNs() && Log->hasNoInfs();
if (!IsKnownNoErrno) {
SimplifyQuery SQ(DL, TLI, DT, AC, Log, true, true, DC);
KnownFPClass Known = computeKnownFPClass(
Log->getOperand(0), KnownFPClass::OrderedLessThanZeroMask,
/*Depth=*/0, SQ);
IsKnownNoErrno =
Known.isKnownNeverZero() && Known.cannotBeOrderedLessThanZero();
}
if (IsKnownNoErrno) {
IRBuilderBase::FastMathFlagGuard Guard(B);
B.setFastMathFlags(Log->getFastMathFlags());
return B.CreateIntrinsic(LogID, {Ty}, {Log->getArgOperand(0)});
}
else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
LogID == Intrinsic::log10) {
} else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
LogID == Intrinsic::log10) {
if (Ty->getScalarType()->isFloatTy()) {
ExpLb = LibFunc_expf;
Exp2Lb = LibFunc_exp2f;
Expand All @@ -2604,9 +2615,14 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
Exp10Lb = LibFunc_exp10;
PowLb = LibFunc_pow;
} else
return Ret;
return nullptr;
} else
return Ret;
return nullptr;

// The earlier call must also be 'fast' in order to do these transforms.
CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
return nullptr;

IRBuilderBase::FastMathFlagGuard Guard(B);
B.setFastMathFlags(FastMathFlags::getFast());
Expand Down Expand Up @@ -2655,7 +2671,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
return MulY;
}

return Ret;
return nullptr;
}

// sqrt(exp(X)) -> exp(X * 0.5)
Expand Down Expand Up @@ -2797,22 +2813,21 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
}

Value *LibCallSimplifier::optimizeFMod(CallInst *CI, IRBuilderBase &B) {
SimplifyQuery SQ(DL, TLI, DT, AC, CI, true, true, DC);

// fmod(x,y) can set errno if y == 0 or x == +/-inf, and returns Nan in those
// case. If we know those do not happen, then we can convert the fmod into
// frem.
bool IsNoNan = CI->hasNoNaNs();
if (!IsNoNan) {
SimplifyQuery SQ(DL, TLI, DT, AC, CI, true, true, DC);
KnownFPClass Known0 = computeKnownFPClass(CI->getOperand(0), fcInf,
/*Depth=*/0, SQ);
if (Known0.isKnownNeverInfinity()) {
KnownFPClass Known1 =
computeKnownFPClass(CI->getOperand(1), fcZero | fcSubnormal,
/*Depth=*/0, SQ);
Function *F = CI->getParent()->getParent();
if (Known1.isKnownNeverLogicalZero(*F, CI->getType()))
IsNoNan = true;
IsNoNan = Known1.isKnownNeverLogicalZero(*F, CI->getType());
}
}

Expand Down
21 changes: 11 additions & 10 deletions llvm/test/Transforms/InstCombine/double-float-shrink-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ define double @exp10_test2(float %f) {

define float @log_test1(float %f) {
; CHECK-LABEL: @log_test1(
; LINUX-NEXT: [[LOGF:%.*]] = call fast float @logf(float [[F:%.*]])
; LINUX-NEXT: [[LOGF:%.*]] = call fast float @llvm.log.f32(float [[F:%.*]])
; LINUX-NEXT: ret float [[LOGF]]
; MS32: [[LOGF:%.*]] = call fast double @log(double [[F:%.*]])
; MS64-NEXT: [[LOGF:%.*]] = call fast float @logf(float [[F:%.*]])
; MS32: [[LOGF:%.*]] = call fast double @llvm.log.f64(double [[F:%.*]])
; MS64-NEXT: [[LOGF:%.*]] = call fast float @llvm.log.f32(float [[F:%.*]])
;
%conv = fpext float %f to double
%call = call fast double @log(double %conv)
Expand All @@ -263,7 +263,7 @@ define float @log_test1(float %f) {
define double @log_test2(float %f) {
; CHECK-LABEL: @log_test2(
; CHECK-NEXT: [[CONV:%.*]] = fpext float [[F:%.*]] to double
; CHECK-NEXT: [[CALL:%.*]] = call fast double @log(double [[CONV]])
; CHECK-NEXT: [[CALL:%.*]] = call fast double @llvm.log.f64(double [[CONV]])
; CHECK-NEXT: ret double [[CALL]]
;
%conv = fpext float %f to double
Expand All @@ -273,10 +273,10 @@ define double @log_test2(float %f) {

define float @log10_test1(float %f) {
; CHECK-LABEL: @log10_test1(
; LINUX-NEXT: [[LOG10F:%.*]] = call fast float @log10f(float [[F:%.*]])
; LINUX-NEXT: [[LOG10F:%.*]] = call fast float @llvm.log10.f32(float [[F:%.*]])
; LINUX-NEXT: ret float [[LOG10F]]
; MS32: [[LOG10F:%.*]] = call fast double @log10(double [[F:%.*]])
; MS64-NEXT: [[LOG10F:%.*]] = call fast float @log10f(float [[F:%.*]])
; MS32: [[LOG10F:%.*]] = call fast double @llvm.log10.f64(double [[F:%.*]])
; MS64-NEXT: [[LOG10F:%.*]] = call fast float @llvm.log10.f32(float [[F:%.*]])
;
%conv = fpext float %f to double
%call = call fast double @log10(double %conv)
Expand All @@ -287,7 +287,7 @@ define float @log10_test1(float %f) {
define double @log10_test2(float %f) {
; CHECK-LABEL: @log10_test2(
; CHECK-NEXT: [[CONV:%.*]] = fpext float [[F:%.*]] to double
; CHECK-NEXT: [[CALL:%.*]] = call fast double @log10(double [[CONV]])
; CHECK-NEXT: [[CALL:%.*]] = call fast double @llvm.log10.f64(double [[CONV]])
; CHECK-NEXT: ret double [[CALL]]
;
%conv = fpext float %f to double
Expand Down Expand Up @@ -320,7 +320,7 @@ define double @log1p_test2(float %f) {

define float @log2_test1(float %f) {
; CHECK-LABEL: @log2_test1(
; ISC99-NEXT: [[LOG2F:%.*]] = call fast float @log2f(float [[F:%.*]])
; ISC99-NEXT: [[LOG2F:%.*]] = call fast float @llvm.log2.f32(float [[F:%.*]])
; ISC99-NEXT: ret float [[LOG2F]]
; ISC89: [[LOG2F:%.*]] = call fast double @log2(double [[F:%.*]])
;
Expand All @@ -333,7 +333,8 @@ define float @log2_test1(float %f) {
define double @log2_test2(float %f) {
; CHECK-LABEL: @log2_test2(
; CHECK-NEXT: [[CONV:%.*]] = fpext float [[F:%.*]] to double
; CHECK-NEXT: [[CALL:%.*]] = call fast double @log2(double [[CONV]])
; ISC99-NEXT: [[CALL:%.*]] = call fast double @llvm.log2.f64(double [[CONV]])
; ISC89-NEXT: [[LOG2F:%.*]] = call fast double @log2(double [[F:%.*]])
; CHECK-NEXT: ret double [[CALL]]
;
%conv = fpext float %f to double
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/Transforms/InstCombine/log-pow.ll
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ define float @logf_powfi_nonconst(float %x, i32 %y) {
define double @log_powi_not_fast(double %x, i32 %y) {
; CHECK-LABEL: @log_powi_not_fast(
; CHECK-NEXT: [[POW:%.*]] = call double @llvm.powi.f64.i32(double [[X:%.*]], i32 [[Y:%.*]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @log(double [[POW]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @llvm.log.f64(double [[POW]])
; CHECK-NEXT: ret double [[LOG]]
;
%pow = call double @llvm.powi.f64.i32(double %x, i32 %y)
Expand Down Expand Up @@ -106,7 +106,7 @@ define <2 x double> @log2v_powv(<2 x double> %x, <2 x double> %y) {
define double @log_pow_not_fast(double %x, double %y) {
; CHECK-LABEL: @log_pow_not_fast(
; CHECK-NEXT: [[POW:%.*]] = call double @pow(double [[X:%.*]], double [[Y:%.*]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @log(double [[POW]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @llvm.log.f64(double [[POW]])
; CHECK-NEXT: ret double [[LOG]]
;
%pow = call double @pow(double %x, double %y)
Expand Down Expand Up @@ -158,7 +158,7 @@ define float @log2f_exp10f(float %x) {
define double @log_exp2_not_fast(double %x) {
; CHECK-LABEL: @log_exp2_not_fast(
; CHECK-NEXT: [[EXP:%.*]] = call double @exp2(double [[X:%.*]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @log(double [[EXP]])
; CHECK-NEXT: [[LOG:%.*]] = call fast double @llvm.log.f64(double [[EXP]])
; CHECK-NEXT: ret double [[LOG]]
;
%exp = call double @exp2(double %x)
Expand Down
Loading

0 comments on commit 493cea5

Please sign in to comment.