/// 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();
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;
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
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.
}
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) {
}
}
+ // 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;
}
FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
- : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
\ No newline at end of file
+ : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}