[InstCombine] pow(x, +/- 0.0) -> 1.0
authorJF Bastien <jfbastien@apple.com>
Fri, 6 Sep 2019 16:26:59 +0000 (16:26 +0000)
committerJF Bastien <jfbastien@apple.com>
Fri, 6 Sep 2019 16:26:59 +0000 (16:26 +0000)
commit52614dfc7fd09d3de608068415eaa632c21c661d
treee0595fcb7354060c8413a6723ecf2321c888b97d
parentd1cc181d03b3c0d1f0e500f5826562a6e4667018
[InstCombine] pow(x, +/- 0.0) -> 1.0

Summary:
This isn't an important optimization at all... We're already doing:
  pow(x, 0.0) -> 1.0
My patch merely teaches instcombine that -0.0 does the same.

However, doing this fixes an AMAZING bug! Compile this program:

  extern "C" double pow(double, double);
  double boom(double base) {
    return pow(base, -0.0);
  }

With:
  clang++ ~/Desktop/fast-math.cpp -ffast-math -O2 -S

And clang will crash with a signal. Wow, fast math is so fast it ICEs the
compiler! Arguably, the generated math is infinitely fast.

What's actually happening is that we recurse infinitely in getPow. In debug we
hit its assertion:
  assert(Exp != 0 && "Incorrect exponent 0 not handled");

We avoid this entire mess if we instead recognize that an exponent of positive
and negative zero yield 1.0.

A separate commit, r371221, fixed the same problem. This only contains the added
tests.

<rdar://problem/54598300>

Reviewers: scanon

Subscribers: hiraditya, jkorous, dexonsmith, ributzka, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D67248

llvm-svn: 371224
llvm/test/Transforms/InstCombine/pow-0.ll [new file with mode: 0644]