]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Reorder pow() transformations (NFC)
authorEvandro Menezes <e.menezes@samsung.com>
Fri, 12 Jul 2019 00:33:49 +0000 (00:33 +0000)
committerEvandro Menezes <e.menezes@samsung.com>
Fri, 12 Jul 2019 00:33:49 +0000 (00:33 +0000)
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

index bc55e77c44fa58f4c07730fd2fb636c198592fef..e0def81d5eeefb05379102ca9b62b6925861b40b 100644 (file)
@@ -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<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(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<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::optimizeCall(CallInst *CI) {
 
 FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
     const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
-    : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
\ No newline at end of file
+    : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}