mirror of
https://github.com/RPCS3/llvm.git
synced 2026-07-21 03:05:26 -04:00
[InstCombine] Expand the simplification of log()
Expand the simplification of special cases of `log()` to include `log2()` and `log10()` as well as intrinsics and more types. Differential revision: https://reviews.llvm.org/D67199 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373261 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
#include "llvm/IR/PatternMatch.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/KnownBits.h"
|
||||
#include "llvm/Support/MathExtras.h"
|
||||
#include "llvm/Transforms/Utils/BuildLibCalls.h"
|
||||
#include "llvm/Transforms/Utils/SizeOpts.h"
|
||||
|
||||
@@ -1457,9 +1458,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) {
|
||||
StringRef ExpName;
|
||||
Intrinsic::ID ID;
|
||||
Value *ExpFn;
|
||||
LibFunc LibFnFloat;
|
||||
LibFunc LibFnDouble;
|
||||
LibFunc LibFnLongDouble;
|
||||
LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
|
||||
|
||||
switch (LibFn) {
|
||||
default:
|
||||
@@ -1809,48 +1808,155 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) {
|
||||
return B.CreateCall(F, { CI->getArgOperand(0), CI->getArgOperand(1) });
|
||||
}
|
||||
|
||||
Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) {
|
||||
Function *Callee = CI->getCalledFunction();
|
||||
Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilder<> &B) {
|
||||
Function *LogFn = Log->getCalledFunction();
|
||||
AttributeList Attrs = LogFn->getAttributes();
|
||||
StringRef LogNm = LogFn->getName();
|
||||
Intrinsic::ID LogID = LogFn->getIntrinsicID();
|
||||
Module *Mod = Log->getModule();
|
||||
Type *Ty = Log->getType();
|
||||
Value *Ret = nullptr;
|
||||
StringRef Name = Callee->getName();
|
||||
if (UnsafeFPShrink && hasFloatVersion(Name))
|
||||
Ret = optimizeUnaryDoubleFP(CI, B, true);
|
||||
|
||||
if (!CI->isFast())
|
||||
return Ret;
|
||||
Value *Op1 = CI->getArgOperand(0);
|
||||
auto *OpC = dyn_cast<CallInst>(Op1);
|
||||
if (UnsafeFPShrink && hasFloatVersion(LogNm))
|
||||
Ret = optimizeUnaryDoubleFP(Log, B, true);
|
||||
|
||||
// The earlier call must also be 'fast' in order to do these transforms.
|
||||
if (!OpC || !OpC->isFast())
|
||||
CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
|
||||
if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
|
||||
return Ret;
|
||||
|
||||
// log(pow(x,y)) -> y*log(x)
|
||||
// This is only applicable to log, log2, log10.
|
||||
if (Name != "log" && Name != "log2" && Name != "log10")
|
||||
LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
|
||||
|
||||
// This is only applicable to log(), log2(), log10().
|
||||
if (TLI->getLibFunc(LogNm, LogLb))
|
||||
switch (LogLb) {
|
||||
case LibFunc_logf:
|
||||
LogID = Intrinsic::log;
|
||||
ExpLb = LibFunc_expf;
|
||||
Exp2Lb = LibFunc_exp2f;
|
||||
Exp10Lb = LibFunc_exp10f;
|
||||
PowLb = LibFunc_powf;
|
||||
break;
|
||||
case LibFunc_log:
|
||||
LogID = Intrinsic::log;
|
||||
ExpLb = LibFunc_exp;
|
||||
Exp2Lb = LibFunc_exp2;
|
||||
Exp10Lb = LibFunc_exp10;
|
||||
PowLb = LibFunc_pow;
|
||||
break;
|
||||
case LibFunc_logl:
|
||||
LogID = Intrinsic::log;
|
||||
ExpLb = LibFunc_expl;
|
||||
Exp2Lb = LibFunc_exp2l;
|
||||
Exp10Lb = LibFunc_exp10l;
|
||||
PowLb = LibFunc_powl;
|
||||
break;
|
||||
case LibFunc_log2f:
|
||||
LogID = Intrinsic::log2;
|
||||
ExpLb = LibFunc_expf;
|
||||
Exp2Lb = LibFunc_exp2f;
|
||||
Exp10Lb = LibFunc_exp10f;
|
||||
PowLb = LibFunc_powf;
|
||||
break;
|
||||
case LibFunc_log2:
|
||||
LogID = Intrinsic::log2;
|
||||
ExpLb = LibFunc_exp;
|
||||
Exp2Lb = LibFunc_exp2;
|
||||
Exp10Lb = LibFunc_exp10;
|
||||
PowLb = LibFunc_pow;
|
||||
break;
|
||||
case LibFunc_log2l:
|
||||
LogID = Intrinsic::log2;
|
||||
ExpLb = LibFunc_expl;
|
||||
Exp2Lb = LibFunc_exp2l;
|
||||
Exp10Lb = LibFunc_exp10l;
|
||||
PowLb = LibFunc_powl;
|
||||
break;
|
||||
case LibFunc_log10f:
|
||||
LogID = Intrinsic::log10;
|
||||
ExpLb = LibFunc_expf;
|
||||
Exp2Lb = LibFunc_exp2f;
|
||||
Exp10Lb = LibFunc_exp10f;
|
||||
PowLb = LibFunc_powf;
|
||||
break;
|
||||
case LibFunc_log10:
|
||||
LogID = Intrinsic::log10;
|
||||
ExpLb = LibFunc_exp;
|
||||
Exp2Lb = LibFunc_exp2;
|
||||
Exp10Lb = LibFunc_exp10;
|
||||
PowLb = LibFunc_pow;
|
||||
break;
|
||||
case LibFunc_log10l:
|
||||
LogID = Intrinsic::log10;
|
||||
ExpLb = LibFunc_expl;
|
||||
Exp2Lb = LibFunc_exp2l;
|
||||
Exp10Lb = LibFunc_exp10l;
|
||||
PowLb = LibFunc_powl;
|
||||
break;
|
||||
default:
|
||||
return Ret;
|
||||
}
|
||||
else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
|
||||
LogID == Intrinsic::log10) {
|
||||
if (Ty->getScalarType()->isFloatTy()) {
|
||||
ExpLb = LibFunc_expf;
|
||||
Exp2Lb = LibFunc_exp2f;
|
||||
Exp10Lb = LibFunc_exp10f;
|
||||
PowLb = LibFunc_powf;
|
||||
} else if (Ty->getScalarType()->isDoubleTy()) {
|
||||
ExpLb = LibFunc_exp;
|
||||
Exp2Lb = LibFunc_exp2;
|
||||
Exp10Lb = LibFunc_exp10;
|
||||
PowLb = LibFunc_pow;
|
||||
} else
|
||||
return Ret;
|
||||
} else
|
||||
return Ret;
|
||||
|
||||
IRBuilder<>::FastMathFlagGuard Guard(B);
|
||||
FastMathFlags FMF;
|
||||
FMF.setFast();
|
||||
B.setFastMathFlags(FMF);
|
||||
B.setFastMathFlags(FastMathFlags::getFast());
|
||||
|
||||
LibFunc Func;
|
||||
Function *F = OpC->getCalledFunction();
|
||||
if (F && ((TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
|
||||
Func == LibFunc_pow) || F->getIntrinsicID() == Intrinsic::pow))
|
||||
return B.CreateFMul(OpC->getArgOperand(1),
|
||||
emitUnaryFloatFnCall(OpC->getOperand(0), Callee->getName(), B,
|
||||
Callee->getAttributes()), "mul");
|
||||
Function *ArgFn = Arg->getCalledFunction();
|
||||
StringRef ArgNm = ArgFn->getName();
|
||||
Intrinsic::ID ArgID = ArgFn->getIntrinsicID();
|
||||
LibFunc ArgLb = NotLibFunc;
|
||||
TLI->getLibFunc(ArgNm, ArgLb);
|
||||
|
||||
// log(pow(x,y)) -> y*log(x)
|
||||
if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
|
||||
Value *LogX =
|
||||
Log->doesNotAccessMemory()
|
||||
? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
|
||||
Arg->getOperand(0), "log")
|
||||
: emitUnaryFloatFnCall(Arg->getOperand(0), LogNm, B, Attrs);
|
||||
Value *MulY = B.CreateFMul(Arg->getArgOperand(1), LogX, "mul");
|
||||
// Since pow() may have side effects, e.g. errno,
|
||||
// dead code elimination may not be trusted to remove it.
|
||||
substituteInParent(Arg, MulY);
|
||||
return MulY;
|
||||
}
|
||||
// log(exp{,2,10}(y)) -> y*log({e,2,10})
|
||||
// TODO: There is no exp10() intrinsic yet.
|
||||
else if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
|
||||
ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
|
||||
Constant *Eul;
|
||||
if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
|
||||
Eul = ConstantFP::get(Log->getType(), M_E);
|
||||
else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
|
||||
Eul = ConstantFP::get(Log->getType(), 2.0);
|
||||
else
|
||||
Eul = ConstantFP::get(Log->getType(), 10.0);
|
||||
Value *LogE = Log->doesNotAccessMemory()
|
||||
? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
|
||||
Eul, "log")
|
||||
: emitUnaryFloatFnCall(Eul, LogNm, B, Attrs);
|
||||
Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
|
||||
// Since exp() may have side effects, e.g. errno,
|
||||
// dead code elimination may not be trusted to remove it.
|
||||
substituteInParent(Arg, MulY);
|
||||
return MulY;
|
||||
}
|
||||
|
||||
// log(exp2(y)) -> y*log(2)
|
||||
if (F && Name == "log" && TLI->getLibFunc(F->getName(), Func) &&
|
||||
TLI->has(Func) && Func == LibFunc_exp2)
|
||||
return B.CreateFMul(
|
||||
OpC->getArgOperand(0),
|
||||
emitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0),
|
||||
Callee->getName(), B, Callee->getAttributes()),
|
||||
"logmul");
|
||||
return Ret;
|
||||
}
|
||||
|
||||
@@ -2801,11 +2907,21 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
|
||||
case LibFunc_sqrt:
|
||||
case LibFunc_sqrtl:
|
||||
return optimizeSqrt(CI, Builder);
|
||||
case LibFunc_logf:
|
||||
case LibFunc_log:
|
||||
case LibFunc_logl:
|
||||
case LibFunc_log10f:
|
||||
case LibFunc_log10:
|
||||
case LibFunc_log10l:
|
||||
case LibFunc_log1pf:
|
||||
case LibFunc_log1p:
|
||||
case LibFunc_log1pl:
|
||||
case LibFunc_log2f:
|
||||
case LibFunc_log2:
|
||||
case LibFunc_log2l:
|
||||
case LibFunc_logbf:
|
||||
case LibFunc_logb:
|
||||
case LibFunc_logbl:
|
||||
return optimizeLog(CI, Builder);
|
||||
case LibFunc_tan:
|
||||
case LibFunc_tanf:
|
||||
@@ -2896,6 +3012,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
|
||||
case Intrinsic::exp2:
|
||||
return optimizeExp2(CI, Builder);
|
||||
case Intrinsic::log:
|
||||
case Intrinsic::log2:
|
||||
case Intrinsic::log10:
|
||||
return optimizeLog(CI, Builder);
|
||||
case Intrinsic::sqrt:
|
||||
return optimizeSqrt(CI, Builder);
|
||||
|
||||
Reference in New Issue
Block a user