From c36da86394dc896c36dda2896cd8bc8336a735f8 Mon Sep 17 00:00:00 2001 From: Evandro Menezes Date: Fri, 12 Jul 2019 00:33:49 +0000 Subject: [PATCH] [InstCombine] Reorder pow() transformations (NFC) Move the transformation from `powf(x, itofp(y))` to `powi(x, y)` to the group of transformations related to the exponent. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365851 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyLibCalls.cpp | 42 ++++++++++------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index bc55e77c44f..e0def81d5ee 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1236,7 +1236,7 @@ static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilder<> &B) { /// Use exp{,2}(x * y) for pow(exp{,2}(x), y); /// exp2(n * x) for pow(2.0 ** n, x); exp10(x) for pow(10.0, x); -/// exp2(log2(C)*x) for pow(C,x). +/// exp2(log2(n) * x) for pow(n, x). Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) { Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1); AttributeList Attrs = Pow->getCalledFunction()->getAttributes(); @@ -1348,7 +1348,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) { return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l, B, Attrs); - // pow(C,x) -> exp2(log2(C)*x) + // pow(n, x) -> exp2(log2(n) * x) if (Pow->hasOneUse() && Pow->hasApproxFunc() && Pow->hasNoNaNs() && Pow->hasNoInfs() && BaseF->isNormal() && !BaseF->isNegative()) { Value *Log = nullptr; @@ -1474,21 +1474,6 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { if (Value *Exp = replacePowWithExp(Pow, B)) return Exp; - // powf(x, sitofp(e)) -> powi(x, e) - // powf(x, uitofp(e)) -> powi(x, e) - if (AllowApprox && (isa(Expo) || isa(Expo))) { - Value *IntExpo = cast(Expo)->getOperand(0); - Value *NewExpo = nullptr; - unsigned BitWidth = IntExpo->getType()->getPrimitiveSizeInBits(); - if (isa(Expo) && BitWidth == 32) - NewExpo = IntExpo; - else if (BitWidth < 32) - NewExpo = isa(Expo) ? B.CreateSExt(IntExpo, B.getInt32Ty()) - : B.CreateZExt(IntExpo, B.getInt32Ty()); - if (NewExpo) - return createPowWithIntegerExponent(Base, NewExpo, M, B); - } - // Evaluate special cases related to the exponent. // pow(x, -1.0) -> 1.0 / x @@ -1510,12 +1495,9 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { if (Value *Sqrt = replacePowWithSqrt(Pow, B)) return Sqrt; - if (!AllowApprox) - return Shrunk; - // pow(x, n) -> x * x * x * ... const APFloat *ExpoF; - if (match(Expo, m_APFloat(ExpoF))) { + if (AllowApprox && match(Expo, m_APFloat(ExpoF))) { // We limit to a max of 7 multiplications, thus the maximum exponent is 32. // If the exponent is an integer+0.5 we generate a call to sqrt and an // additional fmul. @@ -1565,7 +1547,7 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { } APSInt IntExpo(32, /*isUnsigned=*/false); - // powf(x, C) -> powi(x, C) iff C is a constant signed integer value + // powf(x, n) -> powi(x, n) if n is a constant signed integer value if (ExpoF->isInteger() && ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) == APFloat::opOK) { @@ -1574,6 +1556,20 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { } } + // powf(x, itofp(y)) -> powi(x, y) + if (AllowApprox && (isa(Expo) || isa(Expo))) { + Value *IntExpo = cast(Expo)->getOperand(0); + Value *NewExpo = nullptr; + unsigned BitWidth = IntExpo->getType()->getPrimitiveSizeInBits(); + if (isa(Expo) && BitWidth == 32) + NewExpo = IntExpo; + else if (BitWidth < 32) + NewExpo = isa(Expo) ? B.CreateSExt(IntExpo, B.getInt32Ty()) + : B.CreateZExt(IntExpo, B.getInt32Ty()); + if (NewExpo) + return createPowWithIntegerExponent(Base, NewExpo, M, B); + } + return Shrunk; } @@ -3160,4 +3156,4 @@ Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) { FortifiedLibCallSimplifier::FortifiedLibCallSimplifier( const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize) - : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {} \ No newline at end of file + : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {} -- 2.40.0