[PATCH] D13994: Optimization for pow(x, n) where n is some constant

Mandeep Singh Grang via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 22 14:04:31 PDT 2015


mgrang created this revision.
mgrang added reviewers: weimingz, apazos, zinob, beanz.
mgrang added a subscriber: llvm-commits.
mgrang set the repository for this revision to rL LLVM.

In order to avoid calling pow function we generate repeated fmul when n is a
positive or negative whole number. The idea here is that:
  x^(2^i) = x^(2^(i/2)) * x^(2^(i/2))

For eg:
  pow(x, 4.0) ==> y = fmul x, x
                  x = fmul y, y
                  ret x

For exponents which are not powers of 2, we generate fmul as follows:
  pow(x, 5.0) ==> y = fmul x, x
                  z = fmul y, y
                  x = fmul z, x
                  ret x

For negative exponents we simply divide the final result by 1.0 thus generating
an fdiv instruction.

The max no. of fmul generated = ceil(log2 n)
where, log2 is log to the base 2
       n = the exponent of x

By default, fmul would be generated only when exponent <= 32.
For exponents greater than 32 we generate pow instruction.
We provide a new flag:
  -mllvm -power-opt-threshold=<limit>
which can be set to override the default behavior.

Note: The above behavior is enabled only when -ffast-math is enabled (this is
similar to GCC's behavior).

Repository:
  rL LLVM

http://reviews.llvm.org/D13994

Files:
  lib/Transforms/Utils/SimplifyLibCalls.cpp
  test/Transforms/InstCombine/pow-4.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13994.38171.patch
Type: text/x-patch
Size: 7549 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151022/2f0926e9/attachment.bin>


More information about the llvm-commits mailing list