[PATCH] D27932: InstSimplify: Eliminate fabs on known positive

Steve Canon via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 4 11:28:50 PST 2017


scanon added inline comments.


================
Comment at: lib/Analysis/ValueTracking.cpp:2614
     if (I->getOperand(0) == I->getOperand(1))
       return true;
     LLVM_FALLTHROUGH;
----------------
efriedma wrote:
> scanon wrote:
> > arsenm wrote:
> > > efriedma wrote:
> > > > IIRC, A*A->A if A is a NaN, so fabs(x*x) isn't equivalent to x*x.
> > > I think the sign of a NaN is OK to ignore
> > It's OK [in the IEEE 754 sense] to ignore the sign of NaN with all operations *except* abs, copysign, copy, and negate.  So `fabs(x)` is not equivalent to `x` if `x` is NaN, but `fabs(x*x) -> x*x` is OK if `x` might be NaN, because the sign of `x*x` has no meaning if `x` is NaN.
> Okay, since there seems to be some confusion here, I went and took a look at the standard.  It says "For all other operations, this standard does not specify the sign bit of a NaN result."  So x*x has an unspecified sign, and fabs(x*x) has a sign bit which is always clear.
> 
> From this we can conclude that something like `fabs(x*x) + 1 -> x*x + 1` is a legal transformation, but the general transform proposed here is not legal.  For example, `copysign(1, fabs(x*x)) == 1`, but `copysign(1, x*x)` could be either 1 or -1 depending on the target.
Correct. Sorry for the confusion.


https://reviews.llvm.org/D27932





More information about the llvm-commits mailing list