[llvm] r365851 - [InstCombine] Reorder pow() transformations (NFC)
Evandro Menezes via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 11 17:33:49 PDT 2019
Author: evandro
Date: Thu Jul 11 17:33:49 2019
New Revision: 365851
URL: http://llvm.org/viewvc/llvm-project?rev=365851&view=rev
Log:
[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.
Modified:
llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=365851&r1=365850&r2=365851&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Thu Jul 11 17:33:49 2019
@@ -1236,7 +1236,7 @@ static Value *getPow(Value *InnerChain[3
/// 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::replacePowWith
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(Ca
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<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
- Value *IntExpo = cast<Instruction>(Expo)->getOperand(0);
- Value *NewExpo = nullptr;
- unsigned BitWidth = IntExpo->getType()->getPrimitiveSizeInBits();
- if (isa<SIToFPInst>(Expo) && BitWidth == 32)
- NewExpo = IntExpo;
- else if (BitWidth < 32)
- NewExpo = isa<SIToFPInst>(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(Ca
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(Ca
}
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(Ca
}
}
+ // powf(x, itofp(y)) -> powi(x, y)
+ if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
+ Value *IntExpo = cast<Instruction>(Expo)->getOperand(0);
+ Value *NewExpo = nullptr;
+ unsigned BitWidth = IntExpo->getType()->getPrimitiveSizeInBits();
+ if (isa<SIToFPInst>(Expo) && BitWidth == 32)
+ NewExpo = IntExpo;
+ else if (BitWidth < 32)
+ NewExpo = isa<SIToFPInst>(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::optim
FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
- : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
\ No newline at end of file
+ : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
More information about the llvm-commits
mailing list